From fed787ebb9ffc6cc35eb53af1d06980618eecca6 Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Tue, 2 Mar 2021 14:25:30 +0100 Subject: [PATCH] fix stl filesize computation for >2gb files (see https://github.com/cnr-isti-vclab/meshlab/issues/924) --- wrap/io_trimesh/import_stl.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/wrap/io_trimesh/import_stl.h b/wrap/io_trimesh/import_stl.h index 2a202098..106067ee 100644 --- a/wrap/io_trimesh/import_stl.h +++ b/wrap/io_trimesh/import_stl.h @@ -24,7 +24,7 @@ #ifndef __VCGLIB_IMPORT_STL #define __VCGLIB_IMPORT_STL #include -#include +#include #include namespace vcg { @@ -153,13 +153,13 @@ static bool IsSTLBinary(const char * filename, bool &binaryFlag) FILE *fp = fopen(filename, "rb"); /* Find size of file */ fseek(fp, 0, SEEK_END); - long file_size = ftell(fp); + unsigned long file_size = ftell(fp); unsigned int facenum; /* Check for binary or ASCII file */ fseek(fp, STL_LABEL_SIZE, SEEK_SET); fread(&facenum, sizeof(unsigned int), 1, fp); - long expected_file_size=STL_LABEL_SIZE + 4 + (sizeof(short)+sizeof(STLFacet) )*facenum ; + unsigned long expected_file_size=STL_LABEL_SIZE + 4 + (sizeof(short)+sizeof(STLFacet) )*facenum ; if(file_size == expected_file_size) { binaryFlag = true; @@ -169,15 +169,16 @@ static bool IsSTLBinary(const char * filename, bool &binaryFlag) // second check, sometimes the size is a bit wrong, // lets'make a test to check that we find only ascii stuff before assuming it is ascii unsigned char tmpbuf[1000]; - int byte_to_read = std::min(int(sizeof(tmpbuf)), int(file_size - 80)); + unsigned long byte_to_read = std::min(sizeof(tmpbuf), file_size - 80); fread(tmpbuf, byte_to_read,1,fp); fclose(fp); - for(int i = 0; i < byte_to_read; i++) + for(unsigned long i = 0; i < byte_to_read; i++) { if(tmpbuf[i] > 127) { binaryFlag=true; - if(abs(file_size-expected_file_size) > file_size/20 ) + unsigned long diff = (file_size > expected_file_size) ? file_size-expected_file_size : expected_file_size-file_size; + if(diff > file_size/20 ) return false; // break; }