summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Handle.cc123
1 files changed, 107 insertions, 16 deletions
diff --git a/lib/Handle.cc b/lib/Handle.cc
index 81c72e7..7124c38 100644
--- a/lib/Handle.cc
+++ b/lib/Handle.cc
@@ -31,7 +31,6 @@
#include "generic.h"
-#if defined WORDS_BIGENDIAN
#if defined HAVE_BYTESWAP_H
#include <byteswap.h>
#else
@@ -42,7 +41,20 @@ inline Uint16 bswap_16(Uint16 w) {
inline Uint32 bswap_32(Uint32 w) {
return (w >> 24) | ((w >> 8) & 0x0000ff00) | ((w << 8) & 0x00ff0000) | (w << 24);
}
-#endif
+
+inline Uint64 bswap_64(Uint64 w) {
+ union {
+ Uint64 v;
+ struct { Uint32 v1, v2; };
+ } x;
+ Uint32 v1, v2;
+ x.v = w;
+ v1 = bswap32(x.v1);
+ v2 = bswap32(x.v2);
+ x.v1 = v2;
+ x.v2 = v1;
+ return x.v;
+}
#endif
#ifdef HAVE_UNISTD_H
@@ -503,6 +515,12 @@ Uint8 Handle::readU8() {
return r;
}
+int8 Handle::read8() {
+ int8 r = 0;
+ read(&r, 1);
+ return r;
+}
+
Uint16 Handle::readU16() {
Uint16 r = 0;
read(&r, 2);
@@ -513,6 +531,16 @@ Uint16 Handle::readU16() {
#endif
}
+int16 Handle::read16() {
+ int16 r = 0;
+ read(&r, 2);
+#ifdef WORDS_BIGENDIAN
+ return (int16)bswap_16((Uint16)r);
+#else
+ return r;
+#endif
+}
+
Uint32 Handle::readU32() {
Uint32 r = 0;
read(&r, 4);
@@ -523,30 +551,54 @@ Uint32 Handle::readU32() {
#endif
}
-float Handle::readFloat() {
+int32 Handle::read32() {
+ int32 r = 0;
+ read(&r, 4);
#ifdef WORDS_BIGENDIAN
- Uint32 t = 0;
- read(&t, 4);
- t = bswap_32(t);
- return *((float *)&t);
+ return (int32)bswap_32((Uint32)r);
+#else
+ return r;
+#endif
+}
+
+Uint64 Handle::readU64() {
+ Uint64 r = 0;
+ read(&r, 8);
+#ifdef WORDS_BIGENDIAN
+ return bswap_64(r);
+#else
+ return r;
+#endif
+}
+
+int64 Handle::read64() {
+ int64 r = 0;
+ read(&r, 8);
+#ifdef WORDS_BIGENDIAN
+ return (int64)bswap_64((Uint64)r);
#else
+ return r;
+#endif
+}
+
+float Handle::readFloat() {
float r = 0;
read(&r, 4);
+#ifdef WORDS_BIGENDIAN
+ Uint32 t = bswap_32(*((Uint32 *)&r);
+ return *((float *)&t);
+#else
return r;
#endif
}
double Handle::readDouble() {
+ double r = 0;
+ read(&r, 8);
#ifdef WORDS_BIGENDIAN
- Uint32 t[3] = { 0, 0 };
- read(t, 8);
- t[2] = bswap_32(t[0]);
- t[0] = bswap_32(t[1]);
- t[1] = t[2];
+ Uint64 t = bswap_64(*((Uint64 *)&r);
return *((double *)t);
#else
- double r = 0;
- read(&r, 8);
return r;
#endif
}
@@ -555,6 +607,10 @@ void Handle::writeU8(Uint8 v) {
write(&v, 1);
}
+void Handle::write8(int8 v) {
+ write(&v, 1);
+}
+
void Handle::writeU16(Uint16 v) {
#ifdef WORDS_BIGENDIAN
Uint16 t = bswap_16(v);
@@ -564,6 +620,15 @@ void Handle::writeU16(Uint16 v) {
#endif
}
+void Handle::write16(int16 v) {
+#ifdef WORDS_BIGENDIAN
+ Uint16 t = bswap_16((Uint16)v);
+ write(&t, 2);
+#else
+ write(&v, 2);
+#endif
+}
+
void Handle::writeU32(Uint32 v) {
#ifdef WORDS_BIGENDIAN
Uint32 t = bswap_32(v);
@@ -573,6 +638,33 @@ void Handle::writeU32(Uint32 v) {
#endif
}
+void Handle::write32(int32 v) {
+#ifdef WORDS_BIGENDIAN
+ Uint32 t = bswap_32((Uint32)v);
+ write(&t, 4);
+#else
+ write(&v, 4);
+#endif
+}
+
+void Handle::writeU64(Uint64 v) {
+#ifdef WORDS_BIGENDIAN
+ Uint64 t = bswap_64(v);
+ write(&t, 8);
+#else
+ write(&v, 8);
+#endif
+}
+
+void Handle::write64(int64 v) {
+#ifdef WORDS_BIGENDIAN
+ Uint64 t = bswap_64((Uint64)v);
+ write(&t, 8);
+#else
+ write(&v, 8);
+#endif
+}
+
void Handle::writeFloat(float v) {
#ifdef WORDS_BIGENDIAN
writeU32(*((Uint32 *)&v));
@@ -583,8 +675,7 @@ void Handle::writeFloat(float v) {
void Handle::writeDouble(double v) {
#ifdef WORDS_BIGENDIAN
- writeU32(*(((Uint32 *)&v) + 1));
- writeU32(*(((Uint32 *)&v)));
+ writeU64(*(((Uint64 *)&v)));
#else
write(&v, 8);
#endif