changeset 118:c0789661f232 endian-neutral

Towards endian-neutrality, part 3. The feature files have an integer expressing their dimensionality. If it's out of bounds, but the byte-swapped version of it isn't, then assume that we're reading a feature file created on an other-endian machine, and silently (or verbosely, if verbosity is on) Do The Right Thing.
author mas01cr
date Fri, 12 Oct 2007 11:52:28 +0000
parents e800eac265c3
children 6062e6f1dcc1
files audioDB.cpp
diffstat 1 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB.cpp	Fri Oct 12 11:37:38 2007 +0000
+++ b/audioDB.cpp	Fri Oct 12 11:52:28 2007 +0000
@@ -10,6 +10,13 @@
 }
 #endif
 
+uint32_t uint32_byte_swap(uint32_t arg) {
+  return (((arg & 0xff000000) >> 24) |
+          ((arg &   0xff0000) >> 8) |
+          ((arg &     0xff00) << 8) |
+          ((arg &       0xff) << 24));
+}
+
 void audioDB::error(const char* a, const char* b, const char *sysFunc) {
   if(isServer) {
     /* FIXME: I think this is leaky -- we never delete err.  actually
@@ -484,6 +491,15 @@
   if(inFile) {
     uint32_t inDim;
     read(infid, &inDim, sizeof(uint32_t));
+    if (inDim > O2_MAXDIM && uint32_byte_swap(inDim) < O2_MAXDIM) {
+      // quite possibly we're being asked to read a feature file
+      // generated on the other endianness platform.  Do The Right
+      // Thing.  -- CSR, 2007-10-12
+      if (verbosity) {
+        cerr << "byte swapping dimensionality: " << inDim << endl;
+      }
+      inDim = uint32_byte_swap(inDim);
+    }
     if(ntohl(dbH->dim) == 0 && ntohl(dbH->length) == 0) {
       // empty database: initialize with input dimensionality
       dbH->dim = htonl(inDim);
@@ -725,6 +741,15 @@
     if(thisFile) {
       uint32_t thisDim;
       read(infid,&thisDim,sizeof(uint32_t));
+      if (thisDim > O2_MAXDIM && uint32_byte_swap(thisDim) < O2_MAXDIM) {
+        // quite possibly we're being asked to read a feature file
+        // generated on the other endianness platform.  Do The Right
+        // Thing.  -- CSR, 2007-10-12
+        if (verbosity) {
+          cerr << "byte swapping dimensionality: " << thisDim << endl;
+        }
+        thisDim = uint32_byte_swap(thisDim);
+      }
       if(ntohl(dbH->dim) == 0 && ntohl(dbH->length)==0) {
         // empty database: initialize with input dimensionality
         dbH->dim = htonl(thisDim);