diff --git a/src/gmio_stl/internal/stlb_byte_swap.c b/src/gmio_stl/internal/stlb_byte_swap.c index d65220c..99622ab 100644 --- a/src/gmio_stl/internal/stlb_byte_swap.c +++ b/src/gmio_stl/internal/stlb_byte_swap.c @@ -21,9 +21,10 @@ void gmio_stl_triangle_bswap(gmio_stl_triangle_t* triangle) { int i; uint32_t* uintcoord_ptr = (uint32_t*)&(triangle->normal.x); + const uint16_t attr_byte_count = triangle->attribute_byte_count; for (i = 0; i < 12; ++i) *(uintcoord_ptr + i) = gmio_uint32_bswap(*(uintcoord_ptr + i)); - if (triangle->attribute_byte_count != 0) - triangle->attribute_byte_count = gmio_uint16_bswap(triangle->attribute_byte_count); + if (attr_byte_count != 0) + triangle->attribute_byte_count = gmio_uint16_bswap(attr_byte_count); } diff --git a/src/gmio_stl/stlb_read.c b/src/gmio_stl/stlb_read.c index 0384bdb..71aa529 100644 --- a/src/gmio_stl/stlb_read.c +++ b/src/gmio_stl/stlb_read.c @@ -78,6 +78,7 @@ int gmio_stlb_read( trsf->buffer.size / GMIO_STLB_TRIANGLE_RAWSIZE) : 0; /* Variables */ + void* buffer_ptr = trsf != NULL ? trsf->buffer.ptr : NULL; gmio_stlb_readwrite_helper_t rparams = {0}; uint8_t header_data[GMIO_STLB_HEADER_SIZE]; uint32_t total_facet_count = 0; /* Facet count, as declared in the stream */ @@ -99,13 +100,13 @@ int gmio_stlb_read( } /* Read facet count */ - if (gmio_stream_read(&trsf->stream, trsf->buffer.ptr, sizeof(uint32_t), 1) + if (gmio_stream_read(&trsf->stream, buffer_ptr, sizeof(uint32_t), 1) != 1) { return GMIO_STLB_READ_FACET_COUNT_ERROR; } - memcpy(&total_facet_count, trsf->buffer.ptr, sizeof(uint32_t)); + memcpy(&total_facet_count, buffer_ptr, sizeof(uint32_t)); if (byte_order != GMIO_HOST_ENDIANNESS) total_facet_count = gmio_uint32_bswap(total_facet_count); @@ -126,7 +127,7 @@ int gmio_stlb_read( gmio_size_to_uint32( gmio_stream_read( &trsf->stream, - trsf->buffer.ptr, + buffer_ptr, GMIO_STLB_TRIANGLE_RAWSIZE, max_facet_count_per_read)); if (gmio_stream_error(&trsf->stream) != 0) @@ -137,7 +138,7 @@ int gmio_stlb_read( break; /* Exit if no facet to read */ if (gmio_no_error(error)) { - gmio_stlb_read_facets(creator, trsf->buffer.ptr, &rparams); + gmio_stlb_read_facets(creator, buffer_ptr, &rparams); rparams.i_facet_offset += rparams.facet_count; if (gmio_transfer_is_stop_requested(trsf)) error = GMIO_TRANSFER_STOPPED_ERROR; diff --git a/src/gmio_stl/stlb_write.c b/src/gmio_stl/stlb_write.c index 7b95c59..a418617 100644 --- a/src/gmio_stl/stlb_write.c +++ b/src/gmio_stl/stlb_write.c @@ -80,6 +80,7 @@ int gmio_stlb_write( const uint32_t facet_count = mesh != NULL ? mesh->triangle_count : 0; /* Variables */ + void* buffer_ptr = trsf != NULL ? trsf->buffer.ptr : NULL; gmio_stlb_readwrite_helper_t wparams = {0}; uint32_t i_facet = 0; int error = GMIO_NO_ERROR; @@ -99,8 +100,8 @@ int gmio_stlb_write( /* Write header */ if (header_data == NULL) { /* Use buffer to store an empty header (filled with zeroes) */ - memset(trsf->buffer.ptr, 0, GMIO_STLB_HEADER_SIZE); - header_data = (const uint8_t*)trsf->buffer.ptr; + memset(buffer_ptr, 0, GMIO_STLB_HEADER_SIZE); + header_data = (const uint8_t*)buffer_ptr; } if (gmio_stream_write(&trsf->stream, header_data, GMIO_STLB_HEADER_SIZE, 1) != 1) @@ -110,14 +111,11 @@ int gmio_stlb_write( /* Write facet count */ if (byte_order == GMIO_LITTLE_ENDIAN) - gmio_encode_uint32_le(facet_count, trsf->buffer.ptr); + gmio_encode_uint32_le(facet_count, buffer_ptr); else - gmio_encode_uint32_be(facet_count, trsf->buffer.ptr); - if (gmio_stream_write(&trsf->stream, trsf->buffer.ptr, sizeof(uint32_t), 1) - != 1) - { + gmio_encode_uint32_be(facet_count, buffer_ptr); + if (gmio_stream_write(&trsf->stream, buffer_ptr, sizeof(uint32_t), 1) != 1) return GMIO_STREAM_ERROR; - } /* Write triangles */ for (i_facet = 0; @@ -130,13 +128,13 @@ int gmio_stlb_write( wparams.facet_count = GMIO_MIN(wparams.facet_count, facet_count - wparams.i_facet_offset); - gmio_stlb_write_facets(mesh, trsf->buffer.ptr, &wparams); + gmio_stlb_write_facets(mesh, buffer_ptr, &wparams); wparams.i_facet_offset += wparams.facet_count; /* Write buffer to stream */ if (gmio_stream_write( &trsf->stream, - trsf->buffer.ptr, + buffer_ptr, GMIO_STLB_TRIANGLE_RAWSIZE, wparams.facet_count) != wparams.facet_count)