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