1 2/* 3 * Binary Ajax 0.1.5 4 * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/ 5 * MIT License [http://www.opensource.org/licenses/mit-license.php] 6 */ 7 8 9var BinaryFile = function(strData, iDataOffset, iDataLength) { 10 var data = strData; 11 var dataOffset = iDataOffset || 0; 12 var dataLength = 0; 13 14 this.getRawData = function() { 15 return data; 16 } 17 18 if (typeof strData == "string") { 19 dataLength = iDataLength || data.length; 20 21 this.getByteAt = function(iOffset) { 22 return data.charCodeAt(iOffset + dataOffset) & 0xFF; 23 } 24 } else if (typeof strData == "unknown") { 25 dataLength = iDataLength || IEBinary_getLength(data); 26 27 this.getByteAt = function(iOffset) { 28 return IEBinary_getByteAt(data, iOffset + dataOffset); 29 } 30 } 31 32 this.getLength = function() { 33 return dataLength; 34 } 35 36 this.getSByteAt = function(iOffset) { 37 var iByte = this.getByteAt(iOffset); 38 if (iByte > 127) 39 return iByte - 256; 40 else 41 return iByte; 42 } 43 44 this.getShortAt = function(iOffset, bBigEndian) { 45 var iShort = bBigEndian ? 46 (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1) 47 : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset) 48 if (iShort < 0) iShort += 65536; 49 return iShort; 50 } 51 this.getSShortAt = function(iOffset, bBigEndian) { 52 var iUShort = this.getShortAt(iOffset, bBigEndian); 53 if (iUShort > 32767) 54 return iUShort - 65536; 55 else 56 return iUShort; 57 } 58 this.getLongAt = function(iOffset, bBigEndian) { 59 var iByte1 = this.getByteAt(iOffset), 60 iByte2 = this.getByteAt(iOffset + 1), 61 iByte3 = this.getByteAt(iOffset + 2), 62 iByte4 = this.getByteAt(iOffset + 3); 63 64 var iLong = bBigEndian ? 65 (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4 66 : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; 67 if (iLong < 0) iLong += 4294967296; 68 return iLong; 69 } 70 this.getSLongAt = function(iOffset, bBigEndian) { 71 var iULong = this.getLongAt(iOffset, bBigEndian); 72 if (iULong > 2147483647) 73 return iULong - 4294967296; 74 else 75 return iULong; 76 } 77 this.getStringAt = function(iOffset, iLength) { 78 var aStr = []; 79 for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) { 80 aStr[j] = String.fromCharCode(this.getByteAt(i)); 81 } 82 return aStr.join(""); 83 } 84 85 this.getCharAt = function(iOffset) { 86 return String.fromCharCode(this.getByteAt(iOffset)); 87 } 88 this.toBase64 = function() { 89 return window.btoa(data); 90 } 91 this.fromBase64 = function(strBase64) { 92 data = window.atob(strBase64); 93 } 94} 95 96 97var BinaryAjax = (function() { 98 99 function createRequest() { 100 var oHTTP = null; 101 if (window.XMLHttpRequest) { 102 oHTTP = new XMLHttpRequest(); 103 } else if (window.ActiveXObject) { 104 oHTTP = new ActiveXObject("Microsoft.XMLHTTP"); 105 } 106 return oHTTP; 107 } 108 109 function getHead(strURL, fncCallback, fncError) { 110 var oHTTP = createRequest(); 111 if (oHTTP) { 112 if (fncCallback) { 113 if (typeof(oHTTP.onload) != "undefined") { 114 oHTTP.onload = function() { 115 if (oHTTP.status == "200") { 116 fncCallback(this); 117 } else { 118 if (fncError) fncError(); 119 } 120 oHTTP = null; 121 }; 122 } else { 123 oHTTP.onreadystatechange = function() { 124 if (oHTTP.readyState == 4) { 125 if (oHTTP.status == "200") { 126 fncCallback(this); 127 } else { 128 if (fncError) fncError(); 129 } 130 oHTTP = null; 131 } 132 }; 133 } 134 } 135 oHTTP.open("HEAD", strURL, true); 136 oHTTP.send(null); 137 } else { 138 if (fncError) fncError(); 139 } 140 } 141 142 function sendRequest(strURL, fncCallback, fncError, aRange, bAcceptRanges, iFileSize) { 143 var oHTTP = createRequest(); 144 if (oHTTP) { 145 146 var iDataOffset = 0; 147 if (aRange && !bAcceptRanges) { 148 iDataOffset = aRange[0]; 149 } 150 var iDataLen = 0; 151 if (aRange) { 152 iDataLen = aRange[1]-aRange[0]+1; 153 } 154 155 if (fncCallback) { 156 if (typeof(oHTTP.onload) != "undefined") { 157 oHTTP.onload = function() { 158 159 if (oHTTP.status == "200" || oHTTP.status == "206") { 160 this.binaryResponse = new BinaryFile(this.responseText, iDataOffset, iDataLen); 161 this.fileSize = iFileSize || this.getResponseHeader("Content-Length"); 162 fncCallback(this); 163 } else { 164 if (fncError) fncError(); 165 } 166 oHTTP = null; 167 }; 168 } else { 169 oHTTP.onreadystatechange = function() { 170 if (oHTTP.readyState == 4) { 171 if (oHTTP.status == "200" || oHTTP.status == "206") { 172 this.binaryResponse = new BinaryFile(oHTTP.responseBody, iDataOffset, iDataLen); 173 this.fileSize = iFileSize || this.getResponseHeader("Content-Length"); 174 fncCallback(this); 175 } else { 176 if (fncError) fncError(); 177 } 178 oHTTP = null; 179 } 180 }; 181 } 182 } 183 oHTTP.open("GET", strURL, true); 184 185 if (oHTTP.overrideMimeType) oHTTP.overrideMimeType('text/plain; charset=x-user-defined'); 186 187 if (aRange && bAcceptRanges) { 188 oHTTP.setRequestHeader("Range", "bytes=" + aRange[0] + "-" + aRange[1]); 189 } 190 191 oHTTP.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT"); 192 193 oHTTP.send(null); 194 } else { 195 if (fncError) fncError(); 196 } 197 } 198 199 return function(strURL, fncCallback, fncError, aRange) { 200 201 if (aRange) { 202 getHead( 203 strURL, 204 function(oHTTP) { 205 var iLength = parseInt(oHTTP.getResponseHeader("Content-Length"),10); 206 var strAcceptRanges = oHTTP.getResponseHeader("Accept-Ranges"); 207 208 var iStart, iEnd; 209 iStart = aRange[0]; 210 if (aRange[0] < 0) 211 iStart += iLength; 212 iEnd = iStart + aRange[1] - 1; 213 214 sendRequest(strURL, fncCallback, fncError, [iStart, iEnd], (strAcceptRanges == "bytes"), iLength); 215 } 216 ); 217 218 } else { 219 sendRequest(strURL, fncCallback, fncError); 220 } 221 } 222 223}()); 224 225 226document.write( 227 "<script type='text/vbscript'>\r\n" 228 + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" 229 + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n" 230 + "End Function\r\n" 231 + "Function IEBinary_getLength(strBinary)\r\n" 232 + " IEBinary_getLength = LenB(strBinary)\r\n" 233 + "End Function\r\n" 234 + "</script>\r\n" 235); 236