From efd809ebc45ef23c06a02f53217241567f02d544 Mon Sep 17 00:00:00 2001 From: Igor Sfiligoi Date: Fri, 1 Nov 2013 19:43:56 +0000 Subject: Add support for other endian --- src/lib/binaryXHR.js | 43 ++++++++++++++++++++++++++----------------- src/lib/rrdFile.js | 8 ++++++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/lib/binaryXHR.js b/src/lib/binaryXHR.js index d91ad72..b92d732 100644 --- a/src/lib/binaryXHR.js +++ b/src/lib/binaryXHR.js @@ -37,6 +37,8 @@ function BinaryFile(strData, iDataOffset, iDataLength) { var doubleMantExpLo=Math.pow(2,-52); var doubleMantExpFast=Math.pow(2,-20); + var switch_endian = false; + this.getRawData = function() { return data; } @@ -57,6 +59,13 @@ function BinaryFile(strData, iDataOffset, iDataLength) { throw new InvalidBinaryFile("Unsupported type " + (typeof strData)); } + this.getEndianByteAt = function(iOffset,width,delta) { + if (this.switch_endian) + return this.getByteAt(iOffset+width-delta-1); + else + return this.getByteAt(iOffset+delta); + } + this.getLength = function() { return dataLength; } @@ -70,7 +79,7 @@ function BinaryFile(strData, iDataOffset, iDataLength) { } this.getShortAt = function(iOffset) { - var iShort = (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset) + var iShort = (this.getEndianByteAt(iOffset,2,1) << 8) + this.getEndianByteAt(iOffset,2,0) if (iShort < 0) iShort += 65536; return iShort; } @@ -82,10 +91,10 @@ function BinaryFile(strData, iDataOffset, iDataLength) { return iUShort; } this.getLongAt = function(iOffset) { - var iByte1 = this.getByteAt(iOffset), - iByte2 = this.getByteAt(iOffset + 1), - iByte3 = this.getByteAt(iOffset + 2), - iByte4 = this.getByteAt(iOffset + 3); + var iByte1 = this.getEndianByteAt(iOffset,4,0), + iByte2 = this.getEndianByteAt(iOffset,4,1), + iByte3 = this.getEndianByteAt(iOffset,4,2), + iByte4 = this.getEndianByteAt(iOffset,4,3); var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; if (iLong < 0) iLong += 4294967296; @@ -117,14 +126,14 @@ function BinaryFile(strData, iDataOffset, iDataLength) { // Added this.getDoubleAt = function(iOffset) { - var iByte1 = this.getByteAt(iOffset), - iByte2 = this.getByteAt(iOffset + 1), - iByte3 = this.getByteAt(iOffset + 2), - iByte4 = this.getByteAt(iOffset + 3), - iByte5 = this.getByteAt(iOffset + 4), - iByte6 = this.getByteAt(iOffset + 5), - iByte7 = this.getByteAt(iOffset + 6), - iByte8 = this.getByteAt(iOffset + 7); + var iByte1 = this.getEndianByteAt(iOffset,8,0), + iByte2 = this.getEndianByteAt(iOffset,8,1), + iByte3 = this.getEndianByteAt(iOffset,8,2), + iByte4 = this.getEndianByteAt(iOffset,8,3), + iByte5 = this.getEndianByteAt(iOffset,8,4), + iByte6 = this.getEndianByteAt(iOffset,8,5), + iByte7 = this.getEndianByteAt(iOffset,8,6), + iByte8 = this.getEndianByteAt(iOffset,8,7); var iSign=iByte8 >> 7; var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4; @@ -141,10 +150,10 @@ function BinaryFile(strData, iDataOffset, iDataLength) { // added // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa) this.getFastDoubleAt = function(iOffset) { - var iByte5 = this.getByteAt(iOffset + 4), - iByte6 = this.getByteAt(iOffset + 5), - iByte7 = this.getByteAt(iOffset + 6), - iByte8 = this.getByteAt(iOffset + 7); + var iByte5 = this.getEndianByteAt(iOffset,8,4), + iByte6 = this.getEndianByteAt(iOffset,8,5), + iByte7 = this.getEndianByteAt(iOffset,8,6), + iByte8 = this.getEndianByteAt(iOffset,8,7); var iSign=iByte8 >> 7; var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5; diff --git a/src/lib/rrdFile.js b/src/lib/rrdFile.js index 81f7731..35a892b 100644 --- a/src/lib/rrdFile.js +++ b/src/lib/rrdFile.js @@ -217,6 +217,10 @@ RRDHeader.prototype.validate_rrd = function() { if (this.rrd_data.getLongAt(12)==0) { // not a double here... likely 64 bit this.float_align=8; + if (! (this.rrd_data.getDoubleAt(16)==8.642135e+130)) { + // uhm... wrong endian? + this.rrd_data.switch_endian=true; + } if (this.rrd_data.getDoubleAt(16)==8.642135e+130) { // now, is it all 64bit or only float 64 bit? if (this.rrd_data.getLongAt(28)==0) { @@ -233,6 +237,10 @@ RRDHeader.prototype.validate_rrd = function() { } } else { /// should be 32 bit alignment + if (! (this.rrd_data.getDoubleAt(12)==8.642135e+130)) { + // uhm... wrong endian? + this.rrd_data.switch_endian=true; + } if (this.rrd_data.getDoubleAt(12)==8.642135e+130) { this.float_align=4; this.int_align=4; -- cgit v1.1-4-g5e80