Commit a00719d9 authored by Will Schroeder's avatar Will Schroeder
Browse files

ENH: Added error checking for bad binary files.

parent e5ebd8b9
...@@ -37,8 +37,8 @@ public: ...@@ -37,8 +37,8 @@ public:
protected: protected:
char *Filename; char *Filename;
void Execute(); void Execute();
void ReadBinarySTL(FILE *fp, vlFloatPoints*, vlCellArray*); int ReadBinarySTL(FILE *fp, vlFloatPoints*, vlCellArray*);
void ReadASCIISTL(FILE *fp, vlFloatPoints*, vlCellArray*); int ReadASCIISTL(FILE *fp, vlFloatPoints*, vlCellArray*);
int GetSTLFileType(FILE *fp); int GetSTLFileType(FILE *fp);
}; };
......
...@@ -44,11 +44,11 @@ void vlSTLReader::Execute() ...@@ -44,11 +44,11 @@ void vlSTLReader::Execute()
// //
if ( this->GetSTLFileType(fp) == ASCII ) if ( this->GetSTLFileType(fp) == ASCII )
{ {
this->ReadASCIISTL(fp,newPts,newPolys); if ( this->ReadASCIISTL(fp,newPts,newPolys) ) return;
} }
else else
{ {
this->ReadBinarySTL(fp,newPts,newPolys); if ( this->ReadBinarySTL(fp,newPts,newPolys) ) return;
} }
vlDebugMacro(<< "Read " << newPts->NumberOfPoints() << " points\n"); vlDebugMacro(<< "Read " << newPts->NumberOfPoints() << " points\n");
...@@ -66,9 +66,9 @@ void vlSTLReader::Execute() ...@@ -66,9 +66,9 @@ void vlSTLReader::Execute()
this->SetPolys(newPolys); this->SetPolys(newPolys);
} }
void vlSTLReader::ReadBinarySTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *newPolys) int vlSTLReader::ReadBinarySTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *newPolys)
{ {
int i, no_tris, pts[3]; int i, numTris, pts[3];
unsigned long ulint; unsigned long ulint;
unsigned short ibuff2; unsigned short ibuff2;
char header[81]; char header[81];
...@@ -83,9 +83,13 @@ void vlSTLReader::ReadBinarySTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *ne ...@@ -83,9 +83,13 @@ void vlSTLReader::ReadBinarySTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *ne
fread (header, 1, 80, fp); fread (header, 1, 80, fp);
fread (&ulint, 1, 4, fp); fread (&ulint, 1, 4, fp);
swap.Swap4 (&ulint); swap.Swap4 (&ulint);
no_tris = (int) ulint; if ( (numTris = (int) ulint) <= 0 )
{
vlErrorMacro(<< "Bad binary STL file\n");
return 1;
}
for (i=0; fread(&facet,48,1,fp) && i<no_tris; i++) for (i=0; fread(&facet,48,1,fp) && i<numTris; i++)
{ {
fread(&ibuff2,2,1,fp); /* read extra junk */ fread(&ibuff2,2,1,fp); /* read extra junk */
...@@ -111,14 +115,13 @@ void vlSTLReader::ReadBinarySTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *ne ...@@ -111,14 +115,13 @@ void vlSTLReader::ReadBinarySTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *ne
newPolys->InsertNextCell(3,pts); newPolys->InsertNextCell(3,pts);
if (this->Debug && (i % 5000) == 0 && i != 0 ) if (this->Debug && (i % 5000) == 0 && i != 0 )
fprintf (stderr,"%s: triangle #%d\n", this->GetClassName(), newPolys->GetNumberOfCells()); fprintf (stderr,"%s: triangle #%d\n", this->GetClassName(), i);
} }
return; return 0;
} }
void vlSTLReader::ReadASCIISTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *newPolys) int vlSTLReader::ReadASCIISTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *newPolys)
{ {
char line[256]; char line[256];
int i; int i;
...@@ -151,7 +154,7 @@ void vlSTLReader::ReadASCIISTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *new ...@@ -151,7 +154,7 @@ void vlSTLReader::ReadASCIISTL(FILE *fp, vlFloatPoints *newPts, vlCellArray *new
fprintf (stderr,"%s: triangle #%d\n", this->GetClassName(), newPolys->GetNumberOfCells()); fprintf (stderr,"%s: triangle #%d\n", this->GetClassName(), newPolys->GetNumberOfCells());
} }
return; return 0;
} }
int vlSTLReader::GetSTLFileType(FILE *fp) int vlSTLReader::GetSTLFileType(FILE *fp)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment