From a52a04550e2ee869712ac340bb4d459ba6be3058 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Tue, 3 Oct 2017 10:57:16 +0200 Subject: Big endian fix, thanks to @hyperair for hints and initial implementation. --- xs/src/admesh/stlinit.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'xs/src/admesh/stlinit.cpp') diff --git a/xs/src/admesh/stlinit.cpp b/xs/src/admesh/stlinit.cpp index f5110d394..84fc8d2c1 100644 --- a/xs/src/admesh/stlinit.cpp +++ b/xs/src/admesh/stlinit.cpp @@ -27,6 +27,7 @@ #include #include +#include #include "stl.h" @@ -50,11 +51,15 @@ stl_initialize(stl_file *stl) { stl->stats.volume = -1.0; } +#ifndef BOOST_LITTLE_ENDIAN +extern void stl_internal_reverse_quads(char *buf, size_t cnt); +#endif /* BOOST_LITTLE_ENDIAN */ + void stl_count_facets(stl_file *stl, const char *file) { long file_size; int header_num_facets; - int num_facets; + uint32_t num_facets; int i; size_t s; unsigned char chtest[128]; @@ -113,7 +118,12 @@ stl_count_facets(stl_file *stl, const char *file) { } /* Read the int following the header. This should contain # of facets */ - if((!fread(&header_num_facets, sizeof(int), 1, stl->fp)) || (num_facets != header_num_facets)) { + bool header_num_faces_read = fread(&header_num_facets, sizeof(uint32_t), 1, stl->fp); +#ifndef BOOST_LITTLE_ENDIAN + // Convert from little endian to big endian. + stl_internal_reverse_quads((char*)&header_num_facets, 4); +#endif /* BOOST_LITTLE_ENDIAN */ + if (! header_num_faces_read || num_facets != header_num_facets) { fprintf(stderr, "Warning: File size doesn't match number of facets in the header\n"); } @@ -268,6 +278,10 @@ stl_read(stl_file *stl, int first_facet, int first) { stl->error = 1; return; } +#ifndef BOOST_LITTLE_ENDIAN + // Convert the loaded little endian data to big endian. + stl_internal_reverse_quads((char*)&facet, 48); +#endif /* BOOST_LITTLE_ENDIAN */ } else /* Read a single facet from an ASCII .STL file */ { -- cgit v1.2.3