diff --git a/src/Voxel/Voxel_Reader.cxx b/src/Voxel/Voxel_Reader.cxx index 682eca3b3b..17a6e5bd9d 100644 --- a/src/Voxel/Voxel_Reader.cxx +++ b/src/Voxel/Voxel_Reader.cxx @@ -40,7 +40,11 @@ Standard_Boolean Voxel_Reader::Read(const TCollection_ExtendedString& file) Standard_Byte type; // 0 - bool, 1 - color, 2 - float Voxel_VoxelFileFormat format; Standard_Character svoxels[9], sformat[9], stype[9]; - fscanf(f, "%8s %8s %8s\n", svoxels, sformat, stype); + if (fscanf(f, "%8s %8s %8s\n", svoxels, sformat, stype) != 3) + { + fclose(f); + return Standard_False; + } fclose(f); // Take format, type of voxels. @@ -148,8 +152,11 @@ Standard_Boolean Voxel_Reader::ReadBoolAsciiVoxels(const TCollection_ExtendedStr Standard_Character line[65], sx[33], sy[33], sz[33]; // Header: skip it - fgets(line, 64, f); - + if (fgets(line, 64, f) == NULL) + { + return Standard_False; + } + // Location, size, number of splits Standard_Integer nbx = 0, nby = 0, nbz = 0; Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0; @@ -182,9 +189,9 @@ Standard_Boolean Voxel_Reader::ReadBoolAsciiVoxels(const TCollection_ExtendedStr if (nb_slices) { Standard_Integer i1 = 0, i2 = 0, value = 0; - while (!feof(f)) + while (!feof(f) + && fgets(line, 64, f) != NULL) { - fgets(line, 64, f); if (has_slice(line)) { if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3) @@ -225,8 +232,12 @@ Standard_Boolean Voxel_Reader::ReadColorAsciiVoxels(const TCollection_ExtendedSt Standard_Character line[65], sx[33], sy[33], sz[33]; // Header: skip it - fgets(line, 64, f); - + if (fgets(line, 64, f) == NULL) + { + fclose(f); + return Standard_False; + } + // Location, size, number of splits Standard_Integer nbx = 0, nby = 0, nbz = 0; Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0; @@ -259,9 +270,9 @@ Standard_Boolean Voxel_Reader::ReadColorAsciiVoxels(const TCollection_ExtendedSt if (nb_slices) { Standard_Integer i1 = 0, i2 = 0, value = 0; - while (!feof(f)) + while (!feof(f) + && fgets(line, 64, f) != NULL) { - fgets(line, 64, f); if (has_slice(line)) { if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3) @@ -302,8 +313,12 @@ Standard_Boolean Voxel_Reader::ReadFloatAsciiVoxels(const TCollection_ExtendedSt Standard_Character line[65], sx[33], sy[33], sz[33]; // Header: skip it - fgets(line, 64, f); - + if (fgets(line, 64, f) == NULL) + { + fclose(f); + return Standard_False; + } + // Location, size, number of splits Standard_Integer nbx = 0, nby = 0, nbz = 0; Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0; @@ -337,9 +352,9 @@ Standard_Boolean Voxel_Reader::ReadFloatAsciiVoxels(const TCollection_ExtendedSt { Standard_Integer i1 = 0, i2 = 0; Standard_ShortReal value = 0.0; - while (!feof(f)) + while (!feof(f) + && fgets(line, 64, f) != NULL) { - fgets(line, 64, f); if (has_slice(line)) { if (sscanf(line, "%d %d %64s\n", &i1, &i2, line) != 3) @@ -381,20 +396,28 @@ Standard_Boolean Voxel_Reader::ReadBoolBinaryVoxels(const TCollection_ExtendedSt // Header: skip it Standard_Character line[65]; - fgets(line, 64, f); - + if (fgets(line, 64, f) == NULL) + { + fclose(f); + return Standard_False; + } + // Location, size, number of splits Standard_Integer nbx = 0, nby = 0, nbz = 0; Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0; - fread(&x, sizeof(Standard_Real), 1, f); - fread(&y, sizeof(Standard_Real), 1, f); - fread(&z, sizeof(Standard_Real), 1, f); - fread(&xlen, sizeof(Standard_Real), 1, f); - fread(&ylen, sizeof(Standard_Real), 1, f); - fread(&zlen, sizeof(Standard_Real), 1, f); - fread(&nbx, sizeof(Standard_Integer), 1, f); - fread(&nby, sizeof(Standard_Integer), 1, f); - fread(&nbz, sizeof(Standard_Integer), 1, f); + if (fread(&x, sizeof(Standard_Real), 1, f) != 1 + || fread(&y, sizeof(Standard_Real), 1, f) != 1 + || fread(&z, sizeof(Standard_Real), 1, f) != 1 + || fread(&xlen, sizeof(Standard_Real), 1, f) != 1 + || fread(&ylen, sizeof(Standard_Real), 1, f) != 1 + || fread(&zlen, sizeof(Standard_Real), 1, f) != 1 + || fread(&nbx, sizeof(Standard_Integer), 1, f) != 1 + || fread(&nby, sizeof(Standard_Integer), 1, f) != 1 + || fread(&nbz, sizeof(Standard_Integer), 1, f) != 1) + { + fclose(f); + return Standard_False; + } // Allocate the voxels myBoolVoxels = (Standard_Address) new Voxel_BoolDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz); @@ -407,16 +430,15 @@ Standard_Boolean Voxel_Reader::ReadBoolBinaryVoxels(const TCollection_ExtendedSt if (nb_slices) { Standard_Integer i1 = 0, i2 = 0, value = 0; - while (!feof(f)) + while (!feof(f) + && fread(&i1, sizeof(Standard_Integer), 1, f) == 1 + && fread(&i2, sizeof(Standard_Integer), 1, f) == 1 + && fread(&value, sizeof(Standard_Byte), 1, f) == 1) { - fread(&i1, sizeof(Standard_Integer), 1, f); - fread(&i2, sizeof(Standard_Integer), 1, f); - fread(&value, sizeof(Standard_Byte), 1, f); - // Set value if (!((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1]) { - ((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] = + ((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] = (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte)); } (((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])[i2] = (Standard_Byte)value; @@ -436,20 +458,28 @@ Standard_Boolean Voxel_Reader::ReadColorBinaryVoxels(const TCollection_ExtendedS // Header: skip it Standard_Character line[65]; - fgets(line, 64, f); - + if (fgets(line, 64, f) == NULL) + { + fclose(f); + return Standard_False; + } + // Location, size, number of splits Standard_Integer nbx = 0, nby = 0, nbz = 0; Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0; - fread(&x, sizeof(Standard_Real), 1, f); - fread(&y, sizeof(Standard_Real), 1, f); - fread(&z, sizeof(Standard_Real), 1, f); - fread(&xlen, sizeof(Standard_Real), 1, f); - fread(&ylen, sizeof(Standard_Real), 1, f); - fread(&zlen, sizeof(Standard_Real), 1, f); - fread(&nbx, sizeof(Standard_Integer), 1, f); - fread(&nby, sizeof(Standard_Integer), 1, f); - fread(&nbz, sizeof(Standard_Integer), 1, f); + if (fread(&x, sizeof(Standard_Real), 1, f) != 1 + || fread(&y, sizeof(Standard_Real), 1, f) != 1 + || fread(&z, sizeof(Standard_Real), 1, f) != 1 + || fread(&xlen, sizeof(Standard_Real), 1, f) != 1 + || fread(&ylen, sizeof(Standard_Real), 1, f) != 1 + || fread(&zlen, sizeof(Standard_Real), 1, f) != 1 + || fread(&nbx, sizeof(Standard_Integer), 1, f) != 1 + || fread(&nby, sizeof(Standard_Integer), 1, f) != 1 + || fread(&nbz, sizeof(Standard_Integer), 1, f) != 1) + { + fclose(f); + return Standard_False; + } // Allocate the voxels myColorVoxels = (Standard_Address) new Voxel_ColorDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz); @@ -462,12 +492,11 @@ Standard_Boolean Voxel_Reader::ReadColorBinaryVoxels(const TCollection_ExtendedS if (nb_slices) { Standard_Integer i1 = 0, i2 = 0, value = 0; - while (!feof(f)) + while (!feof(f) + && fread(&i1, sizeof(Standard_Integer), 1, f) == 1 + && fread(&i2, sizeof(Standard_Integer), 1, f) == 1 + && fread(&value, sizeof(Standard_Byte), 1, f) == 1) { - fread(&i1, sizeof(Standard_Integer), 1, f); - fread(&i2, sizeof(Standard_Integer), 1, f); - fread(&value, sizeof(Standard_Byte), 1, f); - // Set value if (!((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1]) { @@ -491,20 +520,28 @@ Standard_Boolean Voxel_Reader::ReadFloatBinaryVoxels(const TCollection_ExtendedS // Header: skip it Standard_Character line[65]; - fgets(line, 64, f); - + if (fgets(line, 64, f) == NULL) + { + fclose(f); + return Standard_False; + } + // Location, size, number of splits Standard_Integer nbx = 0, nby = 0, nbz = 0; Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0; - fread(&x, sizeof(Standard_Real), 1, f); - fread(&y, sizeof(Standard_Real), 1, f); - fread(&z, sizeof(Standard_Real), 1, f); - fread(&xlen, sizeof(Standard_Real), 1, f); - fread(&ylen, sizeof(Standard_Real), 1, f); - fread(&zlen, sizeof(Standard_Real), 1, f); - fread(&nbx, sizeof(Standard_Integer), 1, f); - fread(&nby, sizeof(Standard_Integer), 1, f); - fread(&nbz, sizeof(Standard_Integer), 1, f); + if (fread(&x, sizeof(Standard_Real), 1, f) != 1 + || fread(&y, sizeof(Standard_Real), 1, f) != 1 + || fread(&z, sizeof(Standard_Real), 1, f) != 1 + || fread(&xlen, sizeof(Standard_Real), 1, f) != 1 + || fread(&ylen, sizeof(Standard_Real), 1, f) != 1 + || fread(&zlen, sizeof(Standard_Real), 1, f) != 1 + || fread(&nbx, sizeof(Standard_Integer), 1, f) != 1 + || fread(&nby, sizeof(Standard_Integer), 1, f) != 1 + || fread(&nbz, sizeof(Standard_Integer), 1, f) != 1) + { + fclose(f); + return Standard_False; + } // Allocate the voxels myFloatVoxels = (Standard_Address) new Voxel_FloatDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz); @@ -518,12 +555,11 @@ Standard_Boolean Voxel_Reader::ReadFloatBinaryVoxels(const TCollection_ExtendedS { Standard_Integer i1 = 0, i2 = 0; Standard_ShortReal value = 0.0; - while (!feof(f)) + while (!feof(f) + && fread(&i1, sizeof(Standard_Integer), 1, f) == 1 + && fread(&i2, sizeof(Standard_Integer), 1, f) == 1 + && fread(&value, sizeof(Standard_ShortReal), 1, f) == 1) { - fread(&i1, sizeof(Standard_Integer), 1, f); - fread(&i2, sizeof(Standard_Integer), 1, f); - fread(&value, sizeof(Standard_ShortReal), 1, f); - // Set value if (!((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1]) {