1 2/* 3 * Binary Ajax 0.1.10 4 * Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/ 5 * Licensed under the MPL License [http://www.nihilogic.dk/licenses/mpl-license.txt] 6 */ 7 8var BinaryFile = function(strData, iDataOffset, iDataLength) { 9 var data = strData; 10 var dataOffset = iDataOffset || 0; 11 var dataLength = 0; 12 13 this.getRawData = function() { 14 return data; 15 } 16 17 if (typeof strData == "string") { 18 dataLength = iDataLength || data.length; 19 20 this.getByteAt = function(iOffset) { 21 return data.charCodeAt(iOffset + dataOffset) & 0xFF; 22 } 23 24 this.getBytesAt = function(iOffset, iLength) { 25 var aBytes = []; 26 27 for (var i = 0; i < iLength; i++) { 28 aBytes[i] = data.charCodeAt((iOffset + i) + dataOffset) & 0xFF 29 } 30 ; 31 32 return aBytes; 33 } 34 } else if (typeof strData == "unknown") { 35 dataLength = iDataLength || IEBinary_getLength(data); 36 37 this.getByteAt = function(iOffset) { 38 return IEBinary_getByteAt(data, iOffset + dataOffset); 39 } 40 41 this.getBytesAt = function(iOffset, iLength) { 42 return new VBArray(IEBinary_getBytesAt(data, iOffset + dataOffset, iLength)).toArray(); 43 } 44 } 45 46 this.getLength = function() { 47 return dataLength; 48 } 49 50 this.getSByteAt = function(iOffset) { 51 var iByte = this.getByteAt(iOffset); 52 if (iByte > 127) 53 return iByte - 256; 54 else 55 return iByte; 56 } 57 58 this.getShortAt = function(iOffset, bBigEndian) { 59 var iShort = bBigEndian ? 60 (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1) 61 : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset) 62 if (iShort < 0) 63 iShort += 65536; 64 return iShort; 65 } 66 this.getSShortAt = function(iOffset, bBigEndian) { 67 var iUShort = this.getShortAt(iOffset, bBigEndian); 68 if (iUShort > 32767) 69 return iUShort - 65536; 70 else 71 return iUShort; 72 } 73 this.getLongAt = function(iOffset, bBigEndian) { 74 var iByte1 = this.getByteAt(iOffset), 75 iByte2 = this.getByteAt(iOffset + 1), 76 iByte3 = this.getByteAt(iOffset + 2), 77 iByte4 = this.getByteAt(iOffset + 3); 78 79 var iLong = bBigEndian ? 80 (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4 81 : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; 82 if (iLong < 0) 83 iLong += 4294967296; 84 return iLong; 85 } 86 this.getSLongAt = function(iOffset, bBigEndian) { 87 var iULong = this.getLongAt(iOffset, bBigEndian); 88 if (iULong > 2147483647) 89 return iULong - 4294967296; 90 else 91 return iULong; 92 } 93 94 this.getStringAt = function(iOffset, iLength) { 95 var aStr = []; 96 97 var aBytes = this.getBytesAt(iOffset, iLength); 98 for (var j = 0; j < iLength; j++) { 99 aStr[j] = String.fromCharCode(aBytes[j]); 100 } 101 return aStr.join(""); 102 } 103 104 this.getCharAt = function(iOffset) { 105 return String.fromCharCode(this.getByteAt(iOffset)); 106 } 107 this.toBase64 = function() { 108 return window.btoa(data); 109 } 110 this.fromBase64 = function(strBase64) { 111 data = window.atob(strBase64); 112 } 113} 114 115 116var BinaryAjax = (function() { 117 118 function createRequest() { 119 var oHTTP = null; 120 if (window.ActiveXObject) { 121 oHTTP = new ActiveXObject("Microsoft.XMLHTTP"); 122 } else if (window.XMLHttpRequest) { 123 oHTTP = new XMLHttpRequest(); 124 } 125 return oHTTP; 126 } 127 128 function getHead(strURL, fncCallback, fncError) { 129 var oHTTP = createRequest(); 130 if (oHTTP) { 131 if (fncCallback) { 132 if (typeof(oHTTP.onload) != "undefined") { 133 oHTTP.onload = function() { 134 if (oHTTP.status == "200") { 135 fncCallback(this); 136 } else { 137 if (fncError) 138 fncError(); 139 } 140 oHTTP = null; 141 }; 142 } else { 143 oHTTP.onreadystatechange = function() { 144 if (oHTTP.readyState == 4) { 145 if (oHTTP.status == "200") { 146 fncCallback(this); 147 } else { 148 if (fncError) 149 fncError(); 150 } 151 oHTTP = null; 152 } 153 }; 154 } 155 } 156 oHTTP.open("HEAD", strURL, true); 157 oHTTP.send(null); 158 } else { 159 if (fncError) 160 fncError(); 161 } 162 } 163 164 function sendRequest(strURL, fncCallback, fncError, aRange, bAcceptRanges, iFileSize) { 165 var oHTTP = createRequest(); 166 if (oHTTP) { 167 168 var iDataOffset = 0; 169 if (aRange && !bAcceptRanges) { 170 iDataOffset = aRange[0]; 171 } 172 var iDataLen = 0; 173 if (aRange) { 174 iDataLen = aRange[1] - aRange[0] + 1; 175 } 176 177 if (fncCallback) { 178 if (typeof(oHTTP.onload) != "undefined") { 179 oHTTP.onload = function() { 180 if (oHTTP.status == "200" || oHTTP.status == "206" || oHTTP.status == "0") { 181 oHTTP.binaryResponse = new BinaryFile(oHTTP.responseText, iDataOffset, iDataLen); 182 oHTTP.fileSize = iFileSize || oHTTP.getResponseHeader("Content-Length"); 183 fncCallback(oHTTP); 184 } else { 185 if (fncError) 186 fncError(); 187 } 188 oHTTP = null; 189 }; 190 } else { 191 oHTTP.onreadystatechange = function() { 192 if (oHTTP.readyState == 4) { 193 if (oHTTP.status == "200" || oHTTP.status == "206" || oHTTP.status == "0") { 194 // IE6 craps if we try to extend the XHR object 195 var oRes = { 196 status: oHTTP.status, 197 // IE needs responseBody, Chrome/Safari needs responseText 198 binaryResponse: new BinaryFile( 199 typeof oHTTP.responseBody == "unknown" ? oHTTP.responseBody : oHTTP.responseText, iDataOffset, iDataLen 200 ), 201 fileSize: iFileSize || oHTTP.getResponseHeader("Content-Length") 202 }; 203 fncCallback(oRes); 204 } else { 205 if (fncError) 206 fncError(); 207 } 208 oHTTP = null; 209 } 210 }; 211 } 212 } 213 oHTTP.open("GET", strURL, true); 214 215 if (oHTTP.overrideMimeType) 216 oHTTP.overrideMimeType('text/plain;
216 charset=x-user-defined'); 217 218 if (aRange && bAcceptRanges) { 219 oHTTP.setRequestHeader("Range", "bytes=" + aRange[0] + "-" + aRange[1]); 220 } 221 222 oHTTP.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT"); 223 224 oHTTP.send(null); 225 } else { 226 if (fncError) 227 fncError(); 228 } 229 } 230 231 return function(strURL, fncCallback, fncError, aRange) { 232 233 if (aRange) { 234 getHead( 235 strURL, 236 function(oHTTP) { 237 var iLength = parseInt(oHTTP.getResponseHeader("Content-Length"), 10); 238 var strAcceptRanges = oHTTP.getResponseHeader("Accept-Ranges"); 239 240 var iStart, iEnd; 241 iStart = aRange[0]; 242 if (aRange[0] < 0) 243 iStart += iLength; 244 iEnd = iStart + aRange[1] - 1; 245 246 sendRequest(strURL, fncCallback, fncError, [iStart, iEnd], (strAcceptRanges == "bytes"), iLength); 247 } 248 ); 249 250 } else { 251 sendRequest(strURL, fncCallback, fncError); 252 } 253 } 254 255}()); 256 257/* 258 document.write( 259 "<script type='text/vbscript'>\r\n" 260 + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" 261 + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n" 262 + "End Function\r\n" 263 + "Function IEBinary_getLength(strBinary)\r\n" 264 + " IEBinary_getLength = LenB(strBinary)\r\n" 265 + "End Function\r\n" 266 + "</script>\r\n" 267 ); 268 */ 269 270document.write( 271 "<script type='text/vbscript'>\r\n" 272 + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" 273 + " IEBinary_getByteAt = AscB(MidB(strBinary, iOffset + 1, 1))\r\n" 274 + "End Function\r\n" 275 + "Function IEBinary_getBytesAt(strBinary, iOffset, iLength)\r\n" 276 + " Dim aBytes()\r\n" 277 + " ReDim aBytes(iLength - 1)\r\n" 278 + " For i = 0 To iLength - 1\r\n" 279 + " aBytes(i) = IEBinary_getByteAt(strBinary, iOffset + i)\r\n" 280 + " Next\r\n" 281 + " IEBinary_getBytesAt = aBytes\r\n" 282 + "End Function\r\n" 283 + "Function IEBinary_getLength(strBinary)\r\n" 284 + " IEBinary_getLength = LenB(strBinary)\r\n" 285 + "End Function\r\n" 286 + "</script>\r\n" 287 );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.