-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnpacker.js
246 lines (219 loc) · 6.33 KB
/
Unpacker.js
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
239
240
241
242
243
244
245
246
(function(global){
var Unpacker = (function() {
var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
var hasBlob;
try {
hasBlob = Boolean(Blob);
} catch(e) {
hasBlob = false;
}
if (!ArrayBuffer.prototype.slice) {
//Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's
//bytes from `begin`, inclusive, up to `end`, exclusive
ArrayBuffer.prototype.slice = function (begin, end) {
//If `begin` is unspecified, Chrome assumes 0, so we do the same
if (begin === void 0) {
begin = 0;
}
//If `end` is unspecified, the new ArrayBuffer contains all
//bytes from `begin` to the end of this ArrayBuffer.
if (end === void 0) {
end = this.byteLength;
}
//Chrome converts the values to integers via flooring
begin = Math.floor(begin);
end = Math.floor(end);
//If either `begin` or `end` is negative, it refers to an
//index from the end of the array, as opposed to from the beginning.
if (begin < 0) {
begin += this.byteLength;
}
if (end < 0) {
end += this.byteLength;
}
//The range specified by the `begin` and `end` values is clamped to the
//valid index range for the current array.
begin = Math.min(Math.max(0, begin), this.byteLength);
end = Math.min(Math.max(0, end), this.byteLength);
//If the computed length of the new ArrayBuffer would be negative, it
//is clamped to zero.
if (end - begin <= 0) {
return new ArrayBuffer(0);
}
var result = new ArrayBuffer(end - begin);
var resultBytes = new Uint8Array(result);
var sourceBytes = new Uint8Array(this, begin, end - begin);
resultBytes.set(sourceBytes);
return result;
};
}
function b64encodeString(value) {
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split("");
var l = value.length;
var i = cb = b = bl = v = 0;
var b0, b1, b2;
var c0, c1, c2, c3;
var ret = '';
// String
if(typeof value == "string" || value instanceof String){
while(i < l) {
b0 = value.charCodeAt(i + 0) & 0xFF;
b1 = value.charCodeAt(i + 1) & 0xFF;
b2 = value.charCodeAt(i + 2) & 0xFF;
c0 = b0 >> 2 & 0x3F;
c1 = (b0 << 4 | b1 >> 4) & 0x3F;
c2 = (b1 << 2 | b2 >> 6) & 0x3F;
c3 = b2 & 0x3F;
ret += chars[c0] + chars[c1] + chars[c2] + chars[c3];
i += 3;
}
}
// Array like
else{
while(i < l) {
b0 = value[i + 0] & 0xFF;
b1 = value[i + 1] & 0xFF;
b2 = value[i + 2] & 0xFF;
c0 = b0 >> 2 & 0x3F;
c1 = (b0 << 4 | b1 >> 4) & 0x3F;
c2 = (b1 << 2 | b2 >> 6) & 0x3F;
c3 = b2 & 0x3F;
ret += chars[c0] + chars[c1] + chars[c2] + chars[c3];
i += 3;
}
}
i = l % 3;
l = ret.length;
if(i == 1) {
ret = ret.substr(0, l - 2) + "==";
} else if(i == 2) {
ret = ret.substr(0, l - 1) + "=";
}
return ret;
}
function Unpacker(pack, config) {
if(pack) {
this._init(pack, config);
}
}
Unpacker.prototype._init = function(pack, config) {
this.config = config;
this.pack = pack;
};
/**
* Get entry media type
* @param {String} id
* @returns {String}
*/
Unpacker.prototype.getType = function(id) {
return this._findFile(id).type || "text/plain";
};
/**
* Get entry content as String
* Support only ASCII encoding (if pack data is stored as typed array)
* @param {String} id
* @returns {String}
*/
Unpacker.prototype.getAsString = function(id){
if(this.pack == null) {
return "";
}
var data = this.getData(id);
if(typeof data == "string" || data instanceof String){
return data;
}
// Read by 65KB chunks
data = new Uint8Array(data);
var buffer = "";
var step = 65535;// 2^16 - 1
var nbSteps = Math.ceil(data.byteLength / step);
var offset = 0;
for(var i = 0; i < nbSteps; i++) {
buffer += String.fromCharCode.apply(null, new Uint8Array(data.buffer.slice(offset, offset + step)));
offset += step;
}
buffer = decodeURIComponent(escape(buffer));
return buffer;
};
/**
* Get entry content as URI
* @param {String} id
* @returns {String} blob URI or data URI
*/
Unpacker.prototype.getAsURI = function(id){
var data = this.getData(id);
var type = this.getType(id);
if(hasBlob) {
return URL.createObjectURL(new Blob([data], { type: type }));
} else {
return 'data:' + type + ';base64,' + b64encodeString(data);
}
};
/**
* Get entry content as typed array
* Support only ASCII decoding (if pack data is stored as string)
* @param {String} id
* @returns {TypedArray} bytes
*/
Unpacker.prototype.getAsBytes = function(id){
var data = this.getData(id);
if(typeof data == "string" || data instanceof String){
if(typeof Uint8Array !== "function"){
throw new Error("TypedArray are not supported");
}
return new Uint8Array(data.split("").map(function(value){return value.charCodeAt(0);}));
}
return data;
};
/**
* Get entry content as same as packer content (string or typed array)
* @param {String} name
* @return {String|TypedArray}
*/
Unpacker.prototype.getData = function(name) {
var file = this._findFile(name);
return this._slice(file.begin, file.end);
};
/**
* Get data between begin and end indexes
* @param {Number} begin
* @param {Number} end
* @return {String|TypedArray}
*/
Unpacker.prototype._slice = function(begin, end) {
if (this.pack == null) {
return typeof Uint8Array == "function" ? new Uint8Array([]) : "";
}
if (typeof this.pack.substr == "function") {
return this.pack.substr(begin, end - begin);
}
return this.pack.slice(begin, end);
};
Unpacker.prototype._findFile = function(name) {
var i = this.config.length;
while (i-- > 0) {
if(this.config[i][0] == name)
{
var config = this.config[i];
return {
name: config[0],
begin: config[1],
end: config[2],
type: config[3]
};
}
}
};
return Unpacker;
})();
//exports to multiple environments
if(typeof define === 'function' && define.amd){ //AMD
define(function () { return Unpacker; });
} else if (typeof module !== 'undefined' && module.exports){ //node
module.exports = Unpacker;
} else { //browser
//use string because of Google closure compiler ADVANCED_MODE
/*jslint sub:true */
global['Unpacker'] = Unpacker;
}
}(this));