diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/binaryXHR.js | 521 |
1 files changed, 264 insertions, 257 deletions
diff --git a/js/binaryXHR.js b/js/binaryXHR.js index d82c808..63186b3 100644 --- a/js/binaryXHR.js +++ b/js/binaryXHR.js | |||
@@ -1,257 +1,264 @@ | |||
1 | 1 | // jshint browser:true | |
2 | /* | 2 | /* |
3 | * BinaryFile over XMLHttpRequest | 3 | * BinaryFile over XMLHttpRequest |
4 | * Part of the javascriptRRD package | 4 | * Part of the javascriptRRD package |
5 | * Copyright (c) 2009 Frank Wuerthwein, fkw@ucsd.edu | 5 | * Copyright (c) 2009 Frank Wuerthwein, fkw@ucsd.edu |
6 | * MIT License [http://www.opensource.org/licenses/mit-license.php] | 6 | * MIT License [http://www.opensource.org/licenses/mit-license.php] |
7 | * | 7 | * |
8 | * Original repository: http://javascriptrrd.sourceforge.net/ | 8 | * Original repository: http://javascriptrrd.sourceforge.net/ |
9 | * | 9 | * |
10 | * Based on: | 10 | * Based on: |
11 | * Binary Ajax 0.1.5 | 11 | * Binary Ajax 0.1.5 |
12 | * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/ | 12 | * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/ |
13 | * MIT License [http://www.opensource.org/licenses/mit-license.php] | 13 | * MIT License [http://www.opensource.org/licenses/mit-license.php] |
14 | */ | 14 | */ |
15 | 15 | ||
16 | // ============================================================ | 16 | // ============================================================ |
17 | // Exception class | 17 | // Exception class |
18 | function InvalidBinaryFile(msg) { | 18 | function InvalidBinaryFile(msg) { |
19 | this.message=msg; | 19 | "use strict"; |
20 | this.name="Invalid BinaryFile"; | 20 | this.message = msg; |
21 | } | 21 | this.name = "Invalid BinaryFile"; |
22 | 22 | } | |
23 | // pretty print | 23 | |
24 | InvalidBinaryFile.prototype.toString = function() { | 24 | // pretty print |
25 | return this.name + ': "' + this.message + '"'; | 25 | InvalidBinaryFile.prototype.toString = function() { |
26 | } | 26 | "use strict"; |
27 | 27 | return this.name + ': "' + this.message + '"'; | |
28 | // ===================================================================== | 28 | }; |
29 | // BinaryFile class | 29 | |
30 | // Allows access to element inside a binary stream | 30 | // ===================================================================== |
31 | function BinaryFile(strData, iDataOffset, iDataLength) { | 31 | // BinaryFile class |
32 | var data = strData; | 32 | // Allows access to element inside a binary stream |
33 | var dataOffset = iDataOffset || 0; | 33 | function BinaryFile(strData, iDataOffset, iDataLength) { |
34 | var dataLength = 0; | 34 | "use strict"; |
35 | // added | 35 | var data = strData; |
36 | var doubleMantExpHi=Math.pow(2,-28); | 36 | var dataOffset = iDataOffset || 0; |
37 | var doubleMantExpLo=Math.pow(2,-52); | 37 | var dataLength = 0; |
38 | var doubleMantExpFast=Math.pow(2,-20); | 38 | // added |
39 | 39 | var doubleMantExpHi=Math.pow(2,-28); | |
40 | var switch_endian = false; | 40 | var doubleMantExpLo=Math.pow(2,-52); |
41 | 41 | var doubleMantExpFast=Math.pow(2,-20); | |
42 | this.getRawData = function() { | 42 | |
43 | return data; | 43 | var switch_endian = false; |
44 | } | 44 | |
45 | 45 | this.getRawData = function() { | |
46 | if (typeof strData == "string") { | 46 | return data; |
47 | dataLength = iDataLength || data.length; | 47 | }; |
48 | 48 | ||
49 | this.getByteAt = function(iOffset) { | 49 | if (typeof strData === "string") { |
50 | return data.charCodeAt(iOffset + dataOffset) & 0xFF; | 50 | dataLength = iDataLength || data.length; |
51 | } | 51 | |
52 | } else if (typeof strData == "unknown") { | 52 | this.getByteAt = function(iOffset) { |
53 | dataLength = iDataLength || IEBinary_getLength(data); | 53 | return data.charCodeAt(iOffset + dataOffset) & 0xFF; |
54 | 54 | }; | |
55 | this.getByteAt = function(iOffset) { | 55 | } else if (typeof strData === "unknown") { |
56 | return IEBinary_getByteAt(data, iOffset + dataOffset); | 56 | // Correct. "unknown" as type. MS JScript 8 added this. |
57 | } | 57 | dataLength = iDataLength || IEBinary_getLength(data); |
58 | } else { | 58 | |
59 | throw new InvalidBinaryFile("Unsupported type " + (typeof strData)); | 59 | this.getByteAt = function(iOffset) { |
60 | } | 60 | return IEBinary_getByteAt(data, iOffset + dataOffset); |
61 | 61 | }; | |
62 | this.getEndianByteAt = function(iOffset,width,delta) { | 62 | } else { |
63 | if (this.switch_endian) | 63 | throw new InvalidBinaryFile("Unsupported type " + (typeof strData)); |
64 | return this.getByteAt(iOffset+width-delta-1); | 64 | } |
65 | else | 65 | |
66 | return this.getByteAt(iOffset+delta); | 66 | this.getEndianByteAt = function(iOffset,width,delta) { |
67 | } | 67 | if (this.switch_endian) |
68 | 68 | return this.getByteAt(iOffset+width-delta-1); | |
69 | this.getLength = function() { | 69 | else |
70 | return dataLength; | 70 | return this.getByteAt(iOffset+delta); |
71 | } | 71 | }; |
72 | 72 | ||
73 | this.getSByteAt = function(iOffset) { | 73 | this.getLength = function() { |
74 | var iByte = this.getByteAt(iOffset); | 74 | return dataLength; |
75 | if (iByte > 127) | 75 | }; |
76 | return iByte - 256; | 76 | |
77 | else | 77 | this.getSByteAt = function(iOffset) { |
78 | return iByte; | 78 | var iByte = this.getByteAt(iOffset); |
79 | } | 79 | if (iByte > 127) |
80 | 80 | return iByte - 256; | |
81 | this.getShortAt = function(iOffset) { | 81 | else |
82 | var iShort = (this.getEndianByteAt(iOffset,2,1) << 8) + this.getEndianByteAt(iOffset,2,0) | 82 | return iByte; |
83 | if (iShort < 0) iShort += 65536; | 83 | }; |
84 | return iShort; | 84 | |
85 | } | 85 | this.getShortAt = function(iOffset) { |
86 | this.getSShortAt = function(iOffset) { | 86 | var iShort = (this.getEndianByteAt(iOffset,2,1) << 8) + this.getEndianByteAt(iOffset,2,0); |
87 | var iUShort = this.getShortAt(iOffset); | 87 | if (iShort < 0) iShort += 65536; |
88 | if (iUShort > 32767) | 88 | return iShort; |
89 | return iUShort - 65536; | 89 | }; |
90 | else | 90 | this.getSShortAt = function(iOffset) { |
91 | return iUShort; | 91 | var iUShort = this.getShortAt(iOffset); |
92 | } | 92 | if (iUShort > 32767) |
93 | this.getLongAt = function(iOffset) { | 93 | return iUShort - 65536; |
94 | var iByte1 = this.getEndianByteAt(iOffset,4,0), | 94 | else |
95 | iByte2 = this.getEndianByteAt(iOffset,4,1), | 95 | return iUShort; |
96 | iByte3 = this.getEndianByteAt(iOffset,4,2), | 96 | }; |
97 | iByte4 = this.getEndianByteAt(iOffset,4,3); | 97 | this.getLongAt = function(iOffset) { |
98 | 98 | var iByte1 = this.getEndianByteAt(iOffset,4,0), | |
99 | var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; | 99 | iByte2 = this.getEndianByteAt(iOffset,4,1), |
100 | if (iLong < 0) iLong += 4294967296; | 100 | iByte3 = this.getEndianByteAt(iOffset,4,2), |
101 | return iLong; | 101 | iByte4 = this.getEndianByteAt(iOffset,4,3); |
102 | } | 102 | |
103 | this.getSLongAt = function(iOffset) { | 103 | var iLong = (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; |
104 | var iULong = this.getLongAt(iOffset); | 104 | if (iLong < 0) iLong += 4294967296; |
105 | if (iULong > 2147483647) | 105 | return iLong; |
106 | return iULong - 4294967296; | 106 | }; |
107 | else | 107 | this.getSLongAt = function(iOffset) { |
108 | return iULong; | 108 | var iULong = this.getLongAt(iOffset); |
109 | } | 109 | if (iULong > 2147483647) |
110 | this.getStringAt = function(iOffset, iLength) { | 110 | return iULong - 4294967296; |
111 | var aStr = []; | 111 | else |
112 | for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) { | 112 | return iULong; |
113 | aStr[j] = String.fromCharCode(this.getByteAt(i)); | 113 | }; |
114 | } | 114 | this.getStringAt = function(iOffset, iLength) { |
115 | return aStr.join(""); | 115 | var aStr = []; |
116 | } | 116 | for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) { |
117 | 117 | aStr[j] = String.fromCharCode(this.getByteAt(i)); | |
118 | // Added | 118 | } |
119 | this.getCStringAt = function(iOffset, iMaxLength) { | 119 | return aStr.join(""); |
120 | var aStr = []; | 120 | }; |
121 | for (var i=iOffset,j=0;(i<iOffset+iMaxLength) && (this.getByteAt(i)>0);i++,j++) { | 121 | |
122 | aStr[j] = String.fromCharCode(this.getByteAt(i)); | 122 | // Added |
123 | } | 123 | this.getCStringAt = function(iOffset, iMaxLength) { |
124 | return aStr.join(""); | 124 | var aStr = []; |
125 | } | 125 | for (var i=iOffset,j=0;(i<iOffset+iMaxLength) && (this.getByteAt(i)>0);i++,j++) { |
126 | 126 | aStr[j] = String.fromCharCode(this.getByteAt(i)); | |
127 | // Added | 127 | } |
128 | this.getDoubleAt = function(iOffset) { | 128 | return aStr.join(""); |
129 | var iByte1 = this.getEndianByteAt(iOffset,8,0), | 129 | }; |
130 | iByte2 = this.getEndianByteAt(iOffset,8,1), | 130 | |
131 | iByte3 = this.getEndianByteAt(iOffset,8,2), | 131 | // Added |
132 | iByte4 = this.getEndianByteAt(iOffset,8,3), | 132 | this.getDoubleAt = function(iOffset) { |
133 | iByte5 = this.getEndianByteAt(iOffset,8,4), | 133 | var iByte1 = this.getEndianByteAt(iOffset,8,0), |
134 | iByte6 = this.getEndianByteAt(iOffset,8,5), | 134 | iByte2 = this.getEndianByteAt(iOffset,8,1), |
135 | iByte7 = this.getEndianByteAt(iOffset,8,6), | 135 | iByte3 = this.getEndianByteAt(iOffset,8,2), |
136 | iByte8 = this.getEndianByteAt(iOffset,8,7); | 136 | iByte4 = this.getEndianByteAt(iOffset,8,3), |
137 | var iSign=iByte8 >> 7; | 137 | iByte5 = this.getEndianByteAt(iOffset,8,4), |
138 | var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); | 138 | iByte6 = this.getEndianByteAt(iOffset,8,5), |
139 | var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4; | 139 | iByte7 = this.getEndianByteAt(iOffset,8,6), |
140 | var iMantLo=((((iByte3) << 8) + iByte2) << 8) + iByte1; | 140 | iByte8 = this.getEndianByteAt(iOffset,8,7); |
141 | 141 | var iSign=iByte8 >> 7; | |
142 | if (iExpRaw==0) return 0.0; | 142 | var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); |
143 | if (iExpRaw==0x7ff) return undefined; | 143 | var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4; |
144 | 144 | var iMantLo=((((iByte3) << 8) + iByte2) << 8) + iByte1; | |
145 | var iExp=(iExpRaw & 0x7FF)-1023; | 145 | |
146 | 146 | if (iExpRaw === 0) return 0.0; | |
147 | var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi); | 147 | if (iExpRaw === 0x7ff) return undefined; |
148 | return dDouble; | 148 | |
149 | } | 149 | var iExp=(iExpRaw & 0x7FF)-1023; |
150 | // added | 150 | |
151 | // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa) | 151 | var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi); |
152 | this.getFastDoubleAt = function(iOffset) { | 152 | return dDouble; |
153 | var iByte5 = this.getEndianByteAt(iOffset,8,4), | 153 | }; |
154 | iByte6 = this.getEndianByteAt(iOffset,8,5), | 154 | // added |
155 | iByte7 = this.getEndianByteAt(iOffset,8,6), | 155 | // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa) |
156 | iByte8 = this.getEndianByteAt(iOffset,8,7); | 156 | this.getFastDoubleAt = function(iOffset) { |
157 | var iSign=iByte8 >> 7; | 157 | var iByte5 = this.getEndianByteAt(iOffset,8,4), |
158 | var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); | 158 | iByte6 = this.getEndianByteAt(iOffset,8,5), |
159 | var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5; | 159 | iByte7 = this.getEndianByteAt(iOffset,8,6), |
160 | 160 | iByte8 = this.getEndianByteAt(iOffset,8,7); | |
161 | if (iExpRaw==0) return 0.0; | 161 | var iSign=iByte8 >> 7; |
162 | if (iExpRaw==0x7ff) return undefined; | 162 | var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4); |
163 | 163 | var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5; | |
164 | var iExp=(iExpRaw & 0x7FF)-1023; | 164 | |
165 | 165 | if (iExpRaw === 0) return 0.0; | |
166 | var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMant*doubleMantExpFast); | 166 | if (iExpRaw === 0x7ff) return undefined; |
167 | return dDouble; | 167 | |
168 | } | 168 | var iExp=(iExpRaw & 0x7FF)-1023; |
169 | 169 | ||
170 | this.getCharAt = function(iOffset) { | 170 | var dDouble = ((iSign === 1) ? -1 : 1); |
171 | return String.fromCharCode(this.getByteAt(iOffset)); | 171 | dDouble *= Math.pow(2,iExp) * (1.0 + iMant*doubleMantExpFast); |
172 | } | 172 | return dDouble; |
173 | } | 173 | }; |
174 | 174 | ||
175 | 175 | this.getCharAt = function(iOffset) { | |
176 | document.write( | 176 | return String.fromCharCode(this.getByteAt(iOffset)); |
177 | "<script type='text/vbscript'>\r\n" | 177 | }; |
178 | + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" | 178 | } |
179 | + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n" | 179 | |
180 | + "End Function\r\n" | 180 | |
181 | + "Function IEBinary_getLength(strBinary)\r\n" | 181 | document.write( |
182 | + " IEBinary_getLength = LenB(strBinary)\r\n" | 182 | "<script type='text/vbscript'>\r\n" |
183 | + "End Function\r\n" | 183 | + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" |
184 | + "</script>\r\n" | 184 | + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n" |
185 | ); | 185 | + "End Function\r\n" |
186 | 186 | + "Function IEBinary_getLength(strBinary)\r\n" | |
187 | 187 | + " IEBinary_getLength = LenB(strBinary)\r\n" | |
188 | 188 | + "End Function\r\n" | |
189 | // =============================================================== | 189 | + "</script>\r\n" |
190 | // Load a binary file from the specified URL | 190 | ); |
191 | // Will return an object of type BinaryFile | 191 | |
192 | function FetchBinaryURL(url) { | 192 | |
193 | var request = new XMLHttpRequest(); | 193 | |
194 | request.open("GET", url,false); | 194 | // =============================================================== |
195 | try { | 195 | // Load a binary file from the specified URL |
196 | request.overrideMimeType('text/plain; charset=x-user-defined'); | 196 | // Will return an object of type BinaryFile |
197 | } catch (err) { | 197 | function FetchBinaryURL(url) { |
198 | // ignore any error, just to make both FF and IE work | 198 | "use strict"; |
199 | } | 199 | var request = new XMLHttpRequest(); |
200 | request.send(null); | 200 | request.open("GET", url,false); |
201 | 201 | try { | |
202 | var response=request.responseText; | 202 | request.overrideMimeType('text/plain; charset=x-user-defined'); |
203 | try { | 203 | } catch (err) { |
204 | // for older IE versions, the value in responseText is not usable | 204 | // ignore any error, just to make both FF and IE work |
205 | if (IEBinary_getLength(this.responseBody)>0) { | 205 | } |
206 | // will get here only for older verson of IE | 206 | request.send(null); |
207 | response=this.responseBody; | 207 | |
208 | } | 208 | var response=request.responseText; |
209 | } catch (err) { | 209 | try { |
210 | // not IE, do nothing | 210 | // for older IE versions, the value in responseText is not usable |
211 | } | 211 | if (IEBinary_getLength(this.responseBody)>0) { |
212 | 212 | // will get here only for older verson of IE | |
213 | var bf=new BinaryFile(response); | 213 | response=this.responseBody; |
214 | return bf; | 214 | } |
215 | } | 215 | } catch (err) { |
216 | 216 | // not IE, do nothing | |
217 | 217 | } | |
218 | // =============================================================== | 218 | |
219 | // Asyncronously load a binary file from the specified URL | 219 | var bf=new BinaryFile(response); |
220 | // | 220 | return bf; |
221 | // callback must be a function with one or two arguments: | 221 | } |
222 | // - bf = an object of type BinaryFile | 222 | |
223 | // - optional argument object (used only if callback_arg not undefined) | 223 | |
224 | function FetchBinaryURLAsync(url, callback, callback_arg) { | 224 | // =============================================================== |
225 | var callback_wrapper = function() { | 225 | // Asyncronously load a binary file from the specified URL |
226 | if(this.readyState == 4) { | 226 | // |
227 | var response=this.responseText; | 227 | // callback must be a function with one or two arguments: |
228 | try { | 228 | // - bf = an object of type BinaryFile |
229 | // for older IE versions, the value in responseText is not usable | 229 | // - optional argument object (used only if callback_arg not undefined) |
230 | if (IEBinary_getLength(this.responseBody)>0) { | 230 | function FetchBinaryURLAsync(url, callback, callback_arg) { |
231 | // will get here only for older verson of IE | 231 | "use strict"; |
232 | response=this.responseBody; | 232 | var callback_wrapper = function() { |
233 | } | 233 | if(this.readyState === 4) { |
234 | } catch (err) { | 234 | var response=this.responseText; |
235 | // not IE, do nothing | 235 | try { |
236 | } | 236 | // for older IE versions, the value in responseText is not usable |
237 | 237 | if (IEBinary_getLength(this.responseBody)>0) { | |
238 | var bf=new BinaryFile(response); | 238 | // will get here only for older verson of IE |
239 | if (callback_arg!=null) { | 239 | response=this.responseBody; |
240 | callback(bf,callback_arg); | 240 | } |
241 | } else { | 241 | } catch (err) { |
242 | callback(bf); | 242 | // not IE, do nothing |
243 | } | 243 | } |
244 | } | 244 | |
245 | } | 245 | var bf=new BinaryFile(response); |
246 | 246 | if (callback_arg) { | |
247 | var request = new XMLHttpRequest(); | 247 | callback(bf, callback_arg); |
248 | request.onreadystatechange = callback_wrapper; | 248 | } else { |
249 | request.open("GET", url,true); | 249 | callback(bf); |
250 | try { | 250 | } |
251 | request.overrideMimeType('text/plain; charset=x-user-defined'); | 251 | } |
252 | } catch (err) { | 252 | }; |
253 | // ignore any error, just to make both FF and IE work | 253 | |
254 | } | 254 | var request = new XMLHttpRequest(); |
255 | request.send(null); | 255 | request.onreadystatechange = callback_wrapper; |
256 | return request | 256 | request.open("GET", url,true); |
257 | } | 257 | try { |
258 | request.overrideMimeType('text/plain; charset=x-user-defined'); | ||
259 | } catch (err) { | ||
260 | // ignore any error, just to make both FF and IE work | ||
261 | } | ||
262 | request.send(null); | ||
263 | return request; | ||
264 | } | ||