aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--js/binaryXHR.js91
1 files changed, 43 insertions, 48 deletions
diff --git a/js/binaryXHR.js b/js/binaryXHR.js
index 8ce4b0f..2cd02dd 100644
--- a/js/binaryXHR.js
+++ b/js/binaryXHR.js
@@ -33,12 +33,8 @@ InvalidBinaryFile.prototype.toString = function() {
33function BinaryFile(data) { 33function BinaryFile(data) {
34 "use strict"; 34 "use strict";
35 var dataLength; 35 var dataLength;
36 // added 36 // whether the data is in little endian format
37 var doubleMantExpHi=Math.pow(2,-28); 37 var littleEndian = true;
38 var doubleMantExpLo=Math.pow(2,-52);
39 var doubleMantExpFast=Math.pow(2,-20);
40
41 var switch_endian = false;
42 38
43 this.getRawData = function() { 39 this.getRawData = function() {
44 return data; 40 return data;
@@ -61,20 +57,32 @@ function BinaryFile(data) {
61 throw new InvalidBinaryFile("Unsupported type " + (typeof data)); 57 throw new InvalidBinaryFile("Unsupported type " + (typeof data));
62 } 58 }
63 59
64 if (switch_endian) { 60 this.getLength = function() {
65 this.getEndianByteAt = function(iOffset, width, delta) { 61 return dataLength;
66 return this.getByteAt(iOffset + width - delta - 1); 62 };
63
64 // antique browser, use slower fallback implementation
65 this.extendWithFallback(data, littleEndian);
66}
67
68BinaryFile.prototype.extendWithFallback = function(data, littleEndian) {
69 "use strict";
70 var doubleMantExpHi = Math.pow(2,-28);
71 var doubleMantExpLo = Math.pow(2,-52);
72 var doubleMantExpFast = Math.pow(2,-20);
73
74 // private function for getting bytes depending on endianess
75 var that = this, getEndianByteAt;
76 if (littleEndian) {
77 getEndianByteAt = function(iOffset, width, delta) {
78 return that.getByteAt(iOffset + delta);
67 }; 79 };
68 } else { 80 } else {
69 this.getEndianByteAt = function(iOffset, width, delta) { 81 getEndianByteAt = function(iOffset, width, delta) {
70 return this.getByteAt(iOffset + delta); 82 return that.getByteAt(iOffset + width - delta - 1);
71 }; 83 };
72 } 84 }
73 85
74 this.getLength = function() {
75 return dataLength;
76 };
77
78 this.getSByteAt = function(iOffset) { 86 this.getSByteAt = function(iOffset) {
79 var iByte = this.getByteAt(iOffset); 87 var iByte = this.getByteAt(iOffset);
80 if (iByte > 127) 88 if (iByte > 127)
@@ -82,9 +90,8 @@ function BinaryFile(data) {
82 else 90 else
83 return iByte; 91 return iByte;
84 }; 92 };
85
86 this.getShortAt = function(iOffset) { 93 this.getShortAt = function(iOffset) {
87 var iShort = (this.getEndianByteAt(iOffset,2,1) << 8) + this.getEndianByteAt(iOffset,2,0); 94 var iShort = (getEndianByteAt(iOffset,2,1) << 8) + getEndianByteAt(iOffset,2,0);
88 if (iShort < 0) iShort += 65536; 95 if (iShort < 0) iShort += 65536;
89 return iShort; 96 return iShort;
90 }; 97 };
@@ -96,10 +103,10 @@ function BinaryFile(data) {
96 return iUShort; 103 return iUShort;
97 }; 104 };
98 this.getLongAt = function(iOffset) { 105 this.getLongAt = function(iOffset) {
99 var iByte1 = this.getEndianByteAt(iOffset,4,0), 106 var iByte1 = getEndianByteAt(iOffset,4,0),
100 iByte2 = this.getEndianByteAt(iOffset,4,1), 107 iByte2 = getEndianByteAt(iOffset,4,1),
101 iByte3 = this.getEndianByteAt(iOffset,4,2), 108 iByte3 = getEndianByteAt(iOffset,4,2),
102 iByte4 = this.getEndianByteAt(iOffset,4,3); 109 iByte4 = getEndianByteAt(iOffset,4,3);
103 110
104 var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; 111 var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
105 if (iLong < 0) iLong += 4294967296; 112 if (iLong < 0) iLong += 4294967296;
@@ -112,15 +119,9 @@ function BinaryFile(data) {
112 else 119 else
113 return iULong; 120 return iULong;
114 }; 121 };
115 this.getStringAt = function(iOffset, iLength) { 122 this.getCharAt = function(iOffset) {
116 var aStr = []; 123 return String.fromCharCode(this.getByteAt(iOffset));
117 for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) {
118 aStr[j] = String.fromCharCode(this.getByteAt(i));
119 }
120 return aStr.join("");
121 }; 124 };
122
123 // Added
124 this.getCStringAt = function(iOffset, iMaxLength) { 125 this.getCStringAt = function(iOffset, iMaxLength) {
125 var aStr = []; 126 var aStr = [];
126 for (var i=iOffset,j=0;(i<iOffset+iMaxLength) && (this.getByteAt(i)>0);i++,j++) { 127 for (var i=iOffset,j=0;(i<iOffset+iMaxLength) && (this.getByteAt(i)>0);i++,j++) {
@@ -128,17 +129,15 @@ function BinaryFile(data) {
128 } 129 }
129 return aStr.join(""); 130 return aStr.join("");
130 }; 131 };
131
132 // Added
133 this.getDoubleAt = function(iOffset) { 132 this.getDoubleAt = function(iOffset) {
134 var iByte1 = this.getEndianByteAt(iOffset,8,0), 133 var iByte1 = getEndianByteAt(iOffset,8,0),
135 iByte2 = this.getEndianByteAt(iOffset,8,1), 134 iByte2 = getEndianByteAt(iOffset,8,1),
136 iByte3 = this.getEndianByteAt(iOffset,8,2), 135 iByte3 = getEndianByteAt(iOffset,8,2),
137 iByte4 = this.getEndianByteAt(iOffset,8,3), 136 iByte4 = getEndianByteAt(iOffset,8,3),
138 iByte5 = this.getEndianByteAt(iOffset,8,4), 137 iByte5 = getEndianByteAt(iOffset,8,4),
139 iByte6 = this.getEndianByteAt(iOffset,8,5), 138 iByte6 = getEndianByteAt(iOffset,8,5),
140 iByte7 = this.getEndianByteAt(iOffset,8,6), 139 iByte7 = getEndianByteAt(iOffset,8,6),
141 iByte8 = this.getEndianByteAt(iOffset,8,7); 140 iByte8 = getEndianByteAt(iOffset,8,7);
142 var iSign=iByte8 >> 7; 141 var iSign=iByte8 >> 7;
143 var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); 142 var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4);
144 var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4; 143 var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4;
@@ -152,13 +151,12 @@ function BinaryFile(data) {
152 var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi); 151 var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi);
153 return dDouble; 152 return dDouble;
154 }; 153 };
155 // added
156 // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa) 154 // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa)
157 this.getFastDoubleAt = function(iOffset) { 155 this.getFastDoubleAt = function(iOffset) {
158 var iByte5 = this.getEndianByteAt(iOffset,8,4), 156 var iByte5 = getEndianByteAt(iOffset,8,4),
159 iByte6 = this.getEndianByteAt(iOffset,8,5), 157 iByte6 = getEndianByteAt(iOffset,8,5),
160 iByte7 = this.getEndianByteAt(iOffset,8,6), 158 iByte7 = getEndianByteAt(iOffset,8,6),
161 iByte8 = this.getEndianByteAt(iOffset,8,7); 159 iByte8 = getEndianByteAt(iOffset,8,7);
162 var iSign=iByte8 >> 7; 160 var iSign=iByte8 >> 7;
163 var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); 161 var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4);
164 var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5; 162 var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5;
@@ -172,11 +170,8 @@ function BinaryFile(data) {
172 dDouble *= Math.pow(2,iExp) * (1.0 + iMant*doubleMantExpFast); 170 dDouble *= Math.pow(2,iExp) * (1.0 + iMant*doubleMantExpFast);
173 return dDouble; 171 return dDouble;
174 }; 172 };
173};
175 174
176 this.getCharAt = function(iOffset) {
177 return String.fromCharCode(this.getByteAt(iOffset));
178 };
179}
180 175
181 176
182// Use document.write only for stone-age browsers. 177// Use document.write only for stone-age browsers.