Skip to content

Commit

Permalink
Merge pull request #96 from AdrianAtGoogle/unaligned_reads
Browse files Browse the repository at this point in the history
Unaligned reads
  • Loading branch information
syoyo authored Oct 12, 2023
2 parents 3b6c98e + 865d44a commit 86699be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/stream-reader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ class StreamReader {
return false;
}

unsigned short val =
*(reinterpret_cast<const unsigned short *>(&binary_[idx_]));
unsigned short val;
memcpy(&val, &binary_[idx_], sizeof(val));

if (swap_endian_) {
swap2(&val);
Expand All @@ -221,7 +221,8 @@ class StreamReader {
return false;
}

uint32_t val = *(reinterpret_cast<const uint32_t *>(&binary_[idx_]));
uint32_t val;
memcpy(&val, &binary_[idx_], sizeof(val));

if (swap_endian_) {
swap4(&val);
Expand All @@ -238,7 +239,8 @@ class StreamReader {
return false;
}

int val = *(reinterpret_cast<const int *>(&binary_[idx_]));
int val;
memcpy(&val, &binary_[idx_], sizeof(val));

if (swap_endian_) {
swap4(&val);
Expand All @@ -255,7 +257,8 @@ class StreamReader {
return false;
}

uint64_t val = *(reinterpret_cast<const uint64_t *>(&binary_[idx_]));
uint64_t val;
memcpy(&val, &binary_[idx_], sizeof(val));

if (swap_endian_) {
swap8(&val);
Expand All @@ -272,7 +275,8 @@ class StreamReader {
return false;
}

int64_t val = *(reinterpret_cast<const int64_t *>(&binary_[idx_]));
int64_t val;
memcpy(&val, &binary_[idx_], sizeof(val));

if (swap_endian_) {
swap8(&val);
Expand Down
4 changes: 2 additions & 2 deletions src/tinyusdz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ bool ParseUSDZHeader(const uint8_t *addr, const size_t length,

uint16_t compr_method = *reinterpret_cast<uint16_t *>(&local_header[0] + 8);
// uint32_t compr_bytes = *reinterpret_cast<uint32_t*>(&local_header[0]+18);
uint32_t uncompr_bytes =
*reinterpret_cast<uint32_t *>(&local_header[0] + 22);
uint32_t uncompr_bytes;
memcpy(&uncompr_bytes, &local_header[22], sizeof(uncompr_bytes));

// USDZ only supports uncompressed ZIP
if (compr_method != 0) {
Expand Down

0 comments on commit 86699be

Please sign in to comment.