From 7becf20ebd765d26d664a3a771578bf3b40add4e Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Mon, 30 Sep 2019 11:54:00 +0200 Subject: [PATCH] Ignore leading whitespaces in STL files Treat STL files as binary if non-printable characters appear in first 80 bytes --- libsrc/stlgeom/stltopology.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/libsrc/stlgeom/stltopology.cpp b/libsrc/stlgeom/stltopology.cpp index 98798d49..8b0b9665 100644 --- a/libsrc/stlgeom/stltopology.cpp +++ b/libsrc/stlgeom/stltopology.cpp @@ -8,6 +8,7 @@ #include "stlgeom.hpp" #include +#include namespace netgen { @@ -340,14 +341,28 @@ STLGeometry * STLTopology ::Load (istream & ist) { // Check if the file starts with "solid". If not, the file is binary { - char buf[5+1]; - FIOReadStringE(ist,buf,5); - if (strcmp(buf, "solid") != 0) - { - for (auto i : Range(5)) + // binary header is 80 bytes, so don't load more than that + constexpr int buflen = 80; + char buf[buflen+1]; + FIOReadStringE(ist,buf,buflen); + + // ignore whitespaces at start of line + int istart; + for (istart=0; istart binary file + if (strncmp(buf+istart, "solid", 5) != 0) return LoadBinary(ist); - } + + // Check if there is a non-printable character in first 80 bytes + for (auto i : Range(istart, buflen)) + if(std::isprint(buf[i])==0 && buf[i]!='\n') + return LoadBinary(ist); } STLGeometry * geom = new STLGeometry(); @@ -361,6 +376,7 @@ STLGeometry * STLTopology ::Load (istream & ist) int cntface = 0; int vertex = 0; bool badnormals = false; + ist >> buf; // skip first line while (ist.good()) {