4 * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/
5 * MIT License [http://www.opensource.org/licenses/mit-license.php]
9 var BinaryFile = function(strData, iDataOffset, iDataLength) {
11 var dataOffset = iDataOffset || 0;
14 this.getRawData = function() {
18 if (typeof strData == "string") {
19 dataLength = iDataLength || data.length;
21 this.getByteAt = function(iOffset) {
22 return data.charCodeAt(iOffset + dataOffset) & 0xFF;
24 } else if (typeof strData == "unknown") {
25 dataLength = iDataLength || IEBinary_getLength(data);
27 this.getByteAt = function(iOffset) {
28 return IEBinary_getByteAt(data, iOffset + dataOffset);
32 this.getLength = function() {
36 this.getSByteAt = function(iOffset) {
37 var iByte = this.getByteAt(iOffset);
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;
51 this.getSShortAt = function(iOffset, bBigEndian) {
52 var iUShort = this.getShortAt(iOffset, bBigEndian);
54 return iUShort - 65536;
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);
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;
70 this.getSLongAt = function(iOffset, bBigEndian) {
71 var iULong = this.getLongAt(iOffset, bBigEndian);
72 if (iULong > 2147483647)
73 return iULong - 4294967296;
77 this.getStringAt = function(iOffset, iLength) {
79 for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) {
80 aStr[j] = String.fromCharCode(this.getByteAt(i));
85 this.getCharAt = function(iOffset) {
86 return String.fromCharCode(this.getByteAt(iOffset));
88 this.toBase64 = function() {
89 return window.btoa(data);
91 this.fromBase64 = function(strBase64) {
92 data = window.atob(strBase64);
97 var BinaryAjax = (function() {
99 function createRequest() {
101 if (window.XMLHttpRequest) {
102 oHTTP = new XMLHttpRequest();
103 } else if (window.ActiveXObject) {
104 oHTTP = new ActiveXObject("Microsoft.XMLHTTP");
109 function getHead(strURL, fncCallback, fncError) {
110 var oHTTP = createRequest();
113 if (typeof(oHTTP.onload) != "undefined") {
114 oHTTP.onload = function() {
115 if (oHTTP.status == "200") {
118 if (fncError) fncError();
123 oHTTP.onreadystatechange = function() {
124 if (oHTTP.readyState == 4) {
125 if (oHTTP.status == "200") {
128 if (fncError) fncError();
135 oHTTP.open("HEAD", strURL, true);
138 if (fncError) fncError();
142 function sendRequest(strURL, fncCallback, fncError, aRange, bAcceptRanges, iFileSize) {
143 var oHTTP = createRequest();
147 if (aRange && !bAcceptRanges) {
148 iDataOffset = aRange[0];
152 iDataLen = aRange[1]-aRange[0]+1;
156 if (typeof(oHTTP.onload) != "undefined") {
157 oHTTP.onload = function() {
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");
164 if (fncError) fncError();
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");
176 if (fncError) fncError();
183 oHTTP.open("GET", strURL, true);
185 if (oHTTP.overrideMimeType) oHTTP.overrideMimeType('text/plain; charset=x-user-defined');
187 if (aRange && bAcceptRanges) {
188 oHTTP.setRequestHeader("Range", "bytes=" + aRange[0] + "-" + aRange[1]);
191 oHTTP.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT");
195 if (fncError) fncError();
199 return function(strURL, fncCallback, fncError, aRange) {
205 var iLength = parseInt(oHTTP.getResponseHeader("Content-Length"),10);
206 var strAcceptRanges = oHTTP.getResponseHeader("Accept-Ranges");
212 iEnd = iStart + aRange[1] - 1;
214 sendRequest(strURL, fncCallback, fncError, [iStart, iEnd], (strAcceptRanges == "bytes"), iLength);
219 sendRequest(strURL, fncCallback, fncError);
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"
231 + "Function IEBinary_getLength(strBinary)\r\n"
232 + " IEBinary_getLength = LenB(strBinary)\r\n"