summaryrefslogtreecommitdiff
path: root/binaryajax.js
blob: 532303437c29d32254864a93e284cfa993c90d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

/*
 * Binary Ajax 0.2
 * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/
 * Licensed under the MPL License [http://www.nihilogic.dk/licenses/mpl-license.txt]
 */


var BinaryFile = function(data, dataOffset, dataLength) {
	var dataOffset = dataOffset || 0;
	var dataLength = 0;

	this.getRawData = function() {
		return data;
	}

	if (typeof data == "string") {
		dataLength = dataLength || data.length;

		this.getByteAt = function(offset) {
			return data.charCodeAt(offset + dataOffset) & 0xFF;
		}
	} else if (typeof data == "unknown") {
		dataLength = dataLength || IEBinary_getLength(data);

		this.getByteAt = function(offset) {
			return IEBinary_getByteAt(data, offset + dataOffset);
		}
	} else {

	}

	this.getLength = function() {
		return dataLength;
	}

	this.getSByteAt = function(offset) {
		var byte = this.getByteAt(offset);
		if (byte > 127)
			return byte - 256;
		else
			return byte;
	}

	this.getShortAt = function(offset, bigEndian) {
		var short = bigEndian ?
			(this.getByteAt(offset) << 8) + this.getByteAt(offset + 1)
			: (this.getByteAt(offset + 1) << 8) + this.getByteAt(offset)
		if (short < 0) short += 65536;
		return short;
	}
	this.getSShortAt = function(offset, bigEndian) {
		var ushort = this.getShortAt(offset, bigEndian);
		if (ushort > 32767)
			return ushort - 65536;
		else
			return ushort;
	}
	this.getLongAt = function(offset, bigEndian) {
		var byte1 = this.getByteAt(offset),
			byte2 = this.getByteAt(offset + 1),
			byte3 = this.getByteAt(offset + 2),
			byte4 = this.getByteAt(offset + 3);

		var long = bigEndian ?
			(((((byte1 << 8) + byte2) << 8) + byte3) << 8) + byte4
			: (((((byte4 << 8) + byte3) << 8) + byte2) << 8) + byte1;
		if (long < 0) long += 4294967296;
		return long;
	}
	this.getSLongAt = function(offset, bigEndian) {
		var ulong = this.getLongAt(offset, bigEndian);
		if (ulong > 2147483647)
			return ulong - 4294967296;
		else
			return ulong;
	}
	this.getStringAt = function(offset, length) {
		var chars = [];
		for (var i=offset,j=0;i<offset+length;i++,j++) {
			chars[j] = String.fromCharCode(this.getByteAt(i));
		}
		return chars.join("");
	}

	this.getCharAt = function(offset) {
		return String.fromCharCode(this.getByteAt(offset));
	}
	this.toBase64 = function() {
		return window.btoa(data);
	}
	this.fromBase64 = function(str) {
		data = window.atob(str);
	}
}


var BinaryAjax = (function() {

	function createRequest() {
		var http = null;
		if (window.XMLHttpRequest) {
			http = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return http;
	}

	function getHead(url, callback, error) {
		var http = createRequest();
		if (http) {
			if (callback) {
				if (typeof(http.onload) != "undefined") {
					http.onload = function() {
						if (http.status == "200") {
							callback(this);
						} else {
							if (error) error();
						}
						http = null;
					};
				} else {
					http.onreadystatechange = function() {
						if (http.readyState == 4) {
							if (http.status == "200") {
								callback(this);
							} else {
								if (error) error();
							}
							http = null;
						}
					};
				}
			}
			http.open("HEAD", url, true);
			http.send(null);
		} else {
			if (error) error();
		}
	}

	function sendRequest(url, callback, error, range, acceptRanges, fileSize) {
		var http = createRequest();
		if (http) {

			var dataOffset = 0;
			if (range && !acceptRanges) {
				dataOffset = range[0];
			}
			var dataLen = 0;
			if (range) {
				dataLen = range[1]-range[0]+1;
			}

			if (callback) {
				if (typeof(http.onload) != "undefined") {
					http.onload = function() {
						if (http.status == "200" || http.status == "206" || http.status == "0") {
							http.binaryResponse = new BinaryFile(http.responseText, dataOffset, dataLen);
							http.fileSize = fileSize || http.getResponseHeader("Content-Length");
							callback(http);
						} else {
							if (error) error();
						}
						http = null;
					};
				} else {
					http.onreadystatechange = function() {
						if (http.readyState == 4) {
							if (http.status == "200" || http.status == "206" || http.status == "0") {
								// IE6 craps if we try to extend the XHR object
								var res = {
									status : http.status,
									// IE needs responseBody, Chrome/Safari needs responseText
									binaryResponse : new BinaryFile(http.responseBody || http.responseText, dataOffset, dataLen),
									fileSize : fileSize || http.getResponseHeader("Content-Length")
								};
								callback(res);
							} else {
								if (error) error();
							}
							http = null;
						}
					};
				}
			}
			http.open("GET", url, true);

			if (http.overrideMimeType) http.overrideMimeType('text/plain; charset=x-user-defined');

			if (range && acceptRanges) {
				http.setRequestHeader("Range", "bytes=" + range[0] + "-" + range[1]);
			}

			http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT");

			http.send(null);
		} else {
			if (error) error();
		}
	}

	return function(url, callback, error, range) {
		if (range) {
			getHead(
				url,
				function(http) {
					var length = parseInt(http.getResponseHeader("Content-Length"),10);
					var acceptRanges = http.getResponseHeader("Accept-Ranges");

					var start, end;
					start = range[0];
					if (range[0] < 0)
						start += length;
					end = start + range[1] - 1;

					sendRequest(url, callback, error, [start, end], (acceptRanges == "bytes"), length);
				}
			);
		} else {
			sendRequest(url, callback, error);
		}
	}

}());


document.write(
	"<script type='text/vbscript'>\r\n"
	+ "Function IEBinary_getByteAt(strBinary, offset)\r\n"
	+ "	IEBinary_getByteAt = AscB(MidB(strBinary,offset+1,1))\r\n"
	+ "End Function\r\n"
	+ "Function IEBinary_getLength(strBinary)\r\n"
	+ "	IEBinary_getLength = LenB(strBinary)\r\n"
	+ "End Function\r\n"
	+ "</script>\r\n"
);