-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.js
65 lines (53 loc) · 1.21 KB
/
decode.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
module.exports = decode
var decoder = new TextDecoder
var c = 58
var b = 98
var d = 100
var e = 101
var i = 105
var l = 108
var dash = 45
var zero = 48
var nine = 57
function decode (val) {
return next({ i: 0, val: new Uint8Array(val), l: val.byteLength })
}
function string ($) {
return decoder.decode(bytes($))
}
function dictionary ($, val) {
$.i++, val = {}
while ($.val[$.i] !== e) val[string($)] = next($)
return $.i++, val
}
function list ($, val) {
$.i++, val = []
while ($.val[$.i] !== e) val[val.length] = next($)
return $.i++, val
}
function integer ($) {
return intval($, ++$.i, $.i = ndx($, e) + 1)
}
function bytes ($, l) {
l = intval($, $.i, $.i = ndx($, c) + 1)
return $.val.subarray($.i, $.i += l)
}
function next ($, ch) {
return (ch = $.val[$.i]), (
ch === l ? list($) :
ch === i ? integer($) :
ch === d ? dictionary($) :
ch === b ? bytes($) :
string($)
)
}
function ndx ($, ch, i) {
for (i = $.i; i < $.l; i++) if ($.val[i] === ch) return i
}
function intval ($, i, l, val, sign, ch) {
for (val = 0, sign = 1; i < l; i++) {
if ((ch = $.val[i]) === dash) sign = -1
if ((ch >= zero && ch <= nine)) val = val * 10 + ch - 48
}
return val * sign
}