summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rwxr-xr-xsrc/common/endianness.c59
1 files changed, 58 insertions, 1 deletions
diff --git a/src/common/endianness.c b/src/common/endianness.c
index 3a2a653..5a14e1c 100755
--- a/src/common/endianness.c
+++ b/src/common/endianness.c
@@ -257,7 +257,64 @@ bool read_u32(uint32_t *target, const bin_t *data, off_t *pos, off_t len, Source
bool read_u64(uint64_t *target, const bin_t *data, off_t *pos, off_t len, SourceEndian endian)
{
+ if (*pos < 0) return false;
+ if ((len - *pos) < 8) return false;
+
+ switch (endian)
+ {
+ case SRE_LITTLE:
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+
+ *target = (uint64_t)data[*pos] | (uint64_t)data[*pos + 1] << 8;
+ *target |= (uint64_t)data[*pos + 2] << 16 | (uint64_t)data[*pos + 3] << 24;
+ *target |= (uint64_t)data[*pos + 4] << 32 | (uint64_t)data[*pos + 5] << 40;
+ *target |= (uint64_t)data[*pos + 6] << 48 | (uint64_t)data[*pos + 7] << 56;
+
+#elif __BYTE_ORDER == __BIG_ENDIAN
+
+ *target = (uint64_t)data[*pos + 7] | (uint64_t)data[*pos + 6] << 8;
+ *target |= (uint64_t)data[*pos + 5] << 16 | (uint64_t)data[*pos + 4] << 24;
+ *target |= (uint64_t)data[*pos + 3] << 32 | (uint64_t)data[*pos + 2] << 40;
+ *target |= (uint64_t)data[*pos + 1] << 48 | (uint64_t)data[*pos] << 56;
+
+#else
+
+# error "TODO : PDP !"
+
+#endif
+
+ break;
+
+ case SRE_BIG:
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+
+ *target = (uint64_t)data[*pos + 7] | (uint64_t)data[*pos + 6] << 8;
+ *target |= (uint64_t)data[*pos + 5] << 16 | (uint64_t)data[*pos + 4] << 24;
+ *target |= (uint64_t)data[*pos + 3] << 32 | (uint64_t)data[*pos + 2] << 40;
+ *target |= (uint64_t)data[*pos + 1] << 48 | (uint64_t)data[*pos] << 56;
+
+#elif __BYTE_ORDER == __BIG_ENDIAN
+
+ *target = (uint64_t)data[*pos] | (uint64_t)data[*pos + 1] << 8;
+ *target |= (uint64_t)data[*pos + 2] << 16 | (uint64_t)data[*pos + 3] << 24;
+ *target |= (uint64_t)data[*pos + 4] << 32 | (uint64_t)data[*pos + 5] << 40;
+ *target |= (uint64_t)data[*pos + 6] << 48 | (uint64_t)data[*pos + 7] << 56;
+
+#else
+
+# error "TODO : PDP !"
+
+#endif
+
+ break;
- return false;
+
+ }
+
+ *pos += 8;
+
+ return true;
}