forked from kensongzhu/jsdap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdr.js
323 lines (281 loc) · 9.08 KB
/
xdr.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"use strict";
// Lots of code from http://jsfromhell.com/classes/binary-parser
// Jonas Raoni Soares Silva
// http://jsfromhell.com/classes/binary-parser [v1.0]
const END_OF_SEQUENCE = "\xa5\x00\x00\x00";
const START_OF_SEQUENCE = "\x5a\x00\x00\x00";
export class dapUnpacker {
constructor(xdrdata, dapvar) {
this._buf = xdrdata;
this.dapvar = dapvar;
this._pos = 0;
}
getValue() {
var i = this._pos;
var type = this.dapvar.type.toLowerCase();
if (type == "structure" || type == "dataset") {
var out = [],
tmp;
var dapvar = this.dapvar;
for (var child in dapvar) {
if (dapvar[child].type) {
this.dapvar = dapvar[child];
tmp = this.getValue();
out.push(tmp);
}
}
this.dapvar = dapvar;
return out;
} else if (type == "grid") {
var out = [],
tmp;
dapvar = this.dapvar;
this.dapvar = dapvar.array;
tmp = this.getValue();
out.push(tmp);
for (var map in dapvar.maps) {
this.dapvar = dapvar.maps[map];
tmp = this.getValue();
out.push(tmp);
}
this.dapvar = dapvar;
return out;
} else if (type == "sequence") {
var mark = this._unpack_uint32();
var out = [],
struct,
tmp;
dapvar = this.dapvar;
while (mark != 2768240640) {
struct = [];
for (var child in dapvar) {
if (dapvar[child].type) {
this.dapvar = dapvar[child];
tmp = this.getValue();
struct.push(tmp);
}
}
out.push(struct);
mark = this._unpack_uint32();
}
this.dapvar = dapvar;
return out;
// This is a request for a base type variable inside a
// sequence.
} else if (this._buf.slice(i, i + 4) == START_OF_SEQUENCE) {
var mark = this._unpack_uint32();
var out = [],
tmp;
while (mark != 2768240640) {
tmp = this.getValue();
out.push(tmp);
mark = this._unpack_uint32();
}
return out;
}
var n = 1;
if (this.dapvar.shape.length) {
n = this._unpack_uint32();
if (type != "url" && type != "string") {
this._unpack_uint32();
}
}
// Bytes?
var out;
if (type == "byte") {
out = this._unpack_bytes(n);
// String?
} else if (type == "url" || type == "string") {
out = this._unpack_string(n);
} else {
out = [];
var func;
switch (type) {
case "float32":
func = "_unpack_float32";
break;
case "float64":
func = "_unpack_float64";
break;
case "int":
func = "_unpack_int32";
break;
case "uint":
func = "_unpack_uint32";
break;
case "int16":
func = "_unpack_int16";
break;
case "uint16":
func = "_unpack_uint16";
break;
case "int32":
func = "_unpack_int32";
break;
case "uint32":
func = "_unpack_uint32";
break;
}
for (var i = 0; i < n; i++) {
out.push(this[func]());
}
}
if (this.dapvar.shape) {
out = reshape(out, this.dapvar.shape);
} else {
out = out[0];
}
return out;
}
_unpack_byte() {
var bytes = 1;
var signed = false;
var i = this._pos;
this._pos = i + bytes;
var data = this._buf.slice(i, i + bytes);
return decodeInt(data, bytes, signed);
}
_unpack_uint16() {
var bytes = 4;
var signed = false;
var i = this._pos;
this._pos = i + bytes;
var data = this._buf.slice(i, i + bytes);
return decodeInt(data, bytes, signed);
}
_unpack_uint32() {
var bytes = 4;
var signed = false;
var i = this._pos;
this._pos = i + bytes;
var data = this._buf.slice(i, i + bytes);
return decodeInt(data, bytes, signed);
}
_unpack_int16() {
var bytes = 4;
var signed = true;
var i = this._pos;
this._pos = i + bytes;
var data = this._buf.slice(i, i + bytes);
return decodeInt(data, bytes, signed);
}
_unpack_int32() {
var bytes = 4;
var signed = true;
var i = this._pos;
this._pos = i + bytes;
var data = this._buf.slice(i, i + bytes);
return decodeInt(data, bytes, signed);
}
_unpack_float32() {
var precision = 23;
var exponent = 8;
var bytes = 4;
var i = this._pos;
this._pos = i + bytes;
var data = this._buf.slice(i, i + bytes);
return decodeFloat(data, precision, exponent);
}
_unpack_float64() {
var precision = 52;
var exponent = 11;
var bytes = 8;
var i = this._pos;
this._pos = i + bytes;
var data = this._buf.slice(i, i + bytes);
return decodeFloat(data, precision, exponent);
}
_unpack_bytes(count) {
var i = this._pos;
var out = [];
for (var c = 0; c < count; c++) {
out.push(this._unpack_byte());
}
var padding = (4 - count % 4) % 4;
this._pos = i + count + padding;
return out;
}
_unpack_string(count) {
var out = [];
var n, i, j, data, padding;
for (var c = 0; c < count; c++) {
n = this._unpack_uint32();
i = this._pos;
data = this._buf.slice(i, i + n);
padding = (4 - n % 4) % 4;
this._pos = i + n + padding;
// convert back to string
var str = "";
for (var i = 0; i < n; i++) {
str += String.fromCharCode(data[i]);
}
out.push(str);
}
return out;
}
}
export function getBuffer(data) {
var b = new Array(data.length);
for (var i = 0; i < data.length; i++) {
b[i] = data.charCodeAt(i) & 0xff;
}
return b;
}
function reshape(array, shape) {
if (!shape.length) return array[0];
var out = [];
var size, start, stop;
for (var i = 0; i < shape[0]; i++) {
size = array.length / shape[0];
start = i * size;
stop = start + size;
out.push(reshape(array.slice(start, stop), shape.slice(1)));
}
return out;
}
function shl(a, b) {
for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
return a;
}
function readBits(buffer, start, length) {
if (start < 0 || length <= 0) return 0;
for (
var offsetLeft,
offsetRight = start % 8,
curByte = buffer.length - (start >> 3) - 1,
lastByte = buffer.length + (-(start + length) >> 3),
diff = curByte - lastByte,
sum =
((buffer[curByte] >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1)) +
(diff && (offsetLeft = (start + length) % 8) ? (buffer[lastByte++] & ((1 << offsetLeft) - 1)) << ((diff-- << 3) - offsetRight) : 0);
diff;
sum += shl(buffer[lastByte++], (diff-- << 3) - offsetRight)
);
return sum;
}
function decodeInt(data, bytes, signed) {
var x = readBits(data, 0, bytes * 8);
var max = Math.pow(2, bytes * 8);
var integer;
if (signed && x >= max / 2) {
integer = x - max;
} else {
integer = x;
}
return integer;
}
function decodeFloat(buffer, precisionBits, exponentBits) {
// var buffer = data;
var bias = Math.pow(2, exponentBits - 1) - 1;
var signal = readBits(buffer, precisionBits + exponentBits, 1);
var exponent = readBits(buffer, precisionBits, exponentBits);
var significand = 0;
var divisor = 2;
var curByte = buffer.length + (-precisionBits >> 3) - 1;
var byteValue, startBit, mask;
do for (byteValue = buffer[++curByte], startBit = precisionBits % 8 || 8, mask = 1 << startBit; (mask >>= 1); byteValue & mask && (significand += 1 / divisor), divisor *= 2);
while ((precisionBits -= startBit));
return exponent == (bias << 1) + 1
? significand ? NaN : signal ? -Infinity : +Infinity
: (1 + signal * -2) * (exponent || significand ? (!exponent ? Math.pow(2, -bias + 1) * significand : Math.pow(2, exponent - bias) * (1 + significand)) : 0);
}