zkjson 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/decoder-v2.js ADDED
@@ -0,0 +1,592 @@
1
+ const {
2
+ strmap_rev,
3
+ base64_rev,
4
+ bits,
5
+ tobits,
6
+ strmap,
7
+ base64,
8
+ } = require("./utils.js")
9
+
10
+ module.exports = class decoder {
11
+ constructor() {
12
+ this.c = 0
13
+ this.json = null
14
+ }
15
+ n(len) {
16
+ let result = 0
17
+ for (let i = 0; i < len; i++) {
18
+ const bitPos = this.c + i
19
+ const byteIndex = Math.floor(bitPos / 8)
20
+ const bitIndex = 7 - (bitPos % 8)
21
+ const bit = (this.o[byteIndex] >> bitIndex) & 1
22
+ result = (result << 1) | bit
23
+ }
24
+ this.c += len
25
+ return result
26
+ }
27
+ leb128() {
28
+ let n = 0
29
+ let i = 0
30
+ let len_len = null
31
+ do {
32
+ len_len = this.n(8)
33
+ n += Math.pow(128, i) * (len_len >= 128 ? len_len - 128 : len_len)
34
+ i++
35
+ } while (len_len >= 128)
36
+ return n
37
+ }
38
+ decode(v) {
39
+ this.o = v
40
+ this.c = 0
41
+ this.nc = 0
42
+ this.bc = 0
43
+ this.len = 0
44
+ this.str_len = 0
45
+ this.str = {}
46
+ this.str_rev = {}
47
+ this.key_length = 0
48
+ this.num_cache = null
49
+ this.vflags = []
50
+ this.kflags = []
51
+ this.bools = []
52
+ this.krefs = []
53
+ this.vrefs = []
54
+ this.keylens = []
55
+ this.types = []
56
+ this.nums = []
57
+ this.keys = []
58
+ this.json = {}
59
+ this.single = this.n(1) === 1
60
+ if (this.single) this.getSingle()
61
+ else {
62
+ this.getLen()
63
+ this.getVflags()
64
+ this.getVlinks()
65
+ this.getKflags()
66
+ this.getKlinks()
67
+ this.getKeyLens()
68
+ this.getTypes()
69
+ this.getBools()
70
+ this.getNums()
71
+ this.getKeys()
72
+ this.build()
73
+ }
74
+ }
75
+ getSingle() {
76
+ const vals = [null, true, false, "", [], {}]
77
+ const isNum = this.n(1)
78
+ if (isNum) {
79
+ const num = this.n(6)
80
+ this.json = num
81
+ if (num < 63) {
82
+ } else {
83
+ this.json += this.leb128()
84
+ }
85
+ } else {
86
+ const code = this.n(6)
87
+ if (code < 6) {
88
+ this.json = vals[code]
89
+ } else if (code < 9) {
90
+ if (code === 7 || code === 8) {
91
+ const moved = this.uint()
92
+ const n = this.uint()
93
+ const neg = code === 7 ? 1 : -1
94
+ this.json = (n / Math.pow(10, moved)) * neg
95
+ } else {
96
+ const n = this.uint()
97
+ this.json = -n
98
+ }
99
+ } else if (code < 61) {
100
+ this.json = strmap_rev[(code - 9).toString()]
101
+ } else if (code === 61) {
102
+ this.json = String.fromCharCode(Number(this.leb128()))
103
+ } else if (code === 62) {
104
+ const len = this.short()
105
+ this.json = ""
106
+ for (let i = 0; i < len; i++) {
107
+ this.json += base64_rev[this.n(6).toString()]
108
+ }
109
+ } else if (code === 63) {
110
+ const len = this.short()
111
+ this.json = ""
112
+ for (let i = 0; i < len; i++) {
113
+ this.json += String.fromCharCode(Number(this.leb128()))
114
+ }
115
+ }
116
+ }
117
+ }
118
+ getLen() {
119
+ this.len = this.short()
120
+ }
121
+ show() {
122
+ console.log()
123
+ console.log("len", this.len)
124
+ console.log("krefs", this.krefs)
125
+ console.log("vrefs", this.vrefs)
126
+ console.log("keylens", this.keylens)
127
+ console.log("types", this.types)
128
+ console.log("nums", this.nums)
129
+ console.log("keys", this.keys)
130
+ console.log()
131
+ }
132
+ getVflags() {
133
+ let i = 0
134
+ while (i < this.len) {
135
+ const flag = this.n(1)
136
+ this.vflags.push(flag)
137
+ i++
138
+ }
139
+ }
140
+ getKflags() {
141
+ let i = 0
142
+ while (i < this.key_length - 1) {
143
+ const flag = this.n(1)
144
+ this.kflags.push(flag)
145
+ i++
146
+ }
147
+ }
148
+
149
+ getKlinks() {
150
+ let i = 0
151
+ let count = 1
152
+ let prev = 0
153
+ while (i < this.kflags.length) {
154
+ if (this.kflags[i] === 1) {
155
+ let val = this.n(3)
156
+ if (val === 0) {
157
+ let len = this.short()
158
+ val = this.n(3)
159
+ let i3 = i
160
+ for (let i2 = 0; i2 < len; i2++) {
161
+ const diff = this.kflags[i3 + i2]
162
+ prev = this.addKlink(diff === 1, val, prev)
163
+ i++
164
+ }
165
+ } else {
166
+ prev = this.addKlink(true, val, prev)
167
+ i++
168
+ }
169
+ } else {
170
+ let val = 0
171
+ do {
172
+ val = this.n(count)
173
+ if (val === 0) count += 1
174
+ } while (val === 0)
175
+
176
+ if (val === 0) {
177
+ let len = this.short()
178
+ val = this.n(count)
179
+ let i3 = i
180
+ for (let i2 = 0; i2 < len; i2++) {
181
+ const diff = this.kflags[i3 + i2]
182
+ prev = this.addKlink(diff === 1, val, prev)
183
+ i++
184
+ }
185
+ } else {
186
+ prev = this.addKlink(false, val, prev)
187
+ i++
188
+ }
189
+ }
190
+ }
191
+ }
192
+
193
+ addVlink(diff, val, prev) {
194
+ val -= 1
195
+ if (diff) {
196
+ if (val > 3) val = prev - (val - 3)
197
+ else val += prev
198
+ }
199
+ this.vrefs.push(val)
200
+ if (this.key_length < val) this.key_length = val
201
+ prev = val
202
+ return prev
203
+ }
204
+ addKlink(diff, val, prev) {
205
+ val -= 1
206
+ if (diff) {
207
+ if (val > 3) val = prev - (val - 3)
208
+ else val += prev
209
+ }
210
+ this.krefs.push(val)
211
+ prev = val
212
+ return prev
213
+ }
214
+
215
+ getVlinks() {
216
+ let i = 0
217
+ let count = 1
218
+ let prev = 0
219
+ while (i < this.vflags.length) {
220
+ if (this.vflags[i] === 1) {
221
+ let val = this.n(3)
222
+ if (val === 0) {
223
+ let len = this.short()
224
+ val = this.n(3)
225
+ let i3 = i
226
+ for (let i2 = 0; i2 < len; i2++) {
227
+ const diff = this.vflags[i3 + i2]
228
+ prev = this.addVlink(diff === 1, val, prev)
229
+ i++
230
+ }
231
+ } else {
232
+ prev = this.addVlink(true, val, prev)
233
+ i++
234
+ }
235
+ } else {
236
+ let val = 0
237
+ do {
238
+ val = this.n(count)
239
+ if (val === 0) count += 1
240
+ } while (val === 0)
241
+ if (val === 0) {
242
+ let len = this.short()
243
+ val = this.n(count)
244
+ let i3 = i
245
+ for (let i2 = 0; i2 < len; i2++) {
246
+ const diff = this.vflags[i3 + i2]
247
+ prev = this.addVlink(diff === 1, val, prev)
248
+ i++
249
+ }
250
+ } else {
251
+ prev = this.addVlink(false, val, prev)
252
+ i++
253
+ }
254
+ }
255
+ }
256
+ }
257
+
258
+ getKeyLens() {
259
+ if (this.krefs.length === 0 && this.len === 0) return
260
+ for (let i = 0; i < this.krefs.length + 1; i++) {
261
+ const type = this.n(2)
262
+ if (type < 2) {
263
+ this.keylens.push([type])
264
+ } else {
265
+ const int = this.short()
266
+ this.keylens.push([type, int])
267
+ }
268
+ }
269
+ }
270
+
271
+ getTypes() {
272
+ let i2 = -1
273
+ let len = Math.max(1, this.vrefs.length)
274
+ for (let i = 0; i < len; i++) {
275
+ let type = this.n(3)
276
+ if (type === 0) {
277
+ const count = this.short()
278
+ i += count - 1
279
+ let type2 = this.n(3)
280
+ for (let i2 = 0; i2 < count; i2++) this.types.push(type2)
281
+ } else this.types.push(type)
282
+ }
283
+ }
284
+ short() {
285
+ const x = this.n(2)
286
+ return x === 3 ? this.leb128() : this.n(x === 2 ? 4 : x === 1 ? 3 : 2)
287
+ }
288
+ uint() {
289
+ const x = this.n(2)
290
+ return x === 3 ? this.leb128() : this.n(x === 2 ? 6 : x === 1 ? 4 : 3)
291
+ }
292
+ dint(prev = 0) {
293
+ if (this.num_cache !== null) {
294
+ let n = this.num_cache.diff ? prev + this.num_cache.n : this.num_cache.n
295
+ this.num_cache.len -= 1
296
+ if (this.num_cache.len === 0) this.num_cache = null
297
+ return n
298
+ }
299
+ const x = this.n(2)
300
+ const diff = x === 0
301
+ let num = x === 3 ? this.leb128() : this.n(x === 2 ? 6 : x === 1 ? 4 : 3)
302
+ if (num === 7 && diff) {
303
+ const len = this.short()
304
+ const x2 = this.n(2)
305
+ let diff = x2 === 0
306
+ let n = null
307
+ if (x2 === 3) n = this.leb128()
308
+ else {
309
+ const d = x2 === 0 ? 3 : x2 === 1 ? 4 : 6
310
+ n = this.n(d)
311
+ }
312
+ if (diff) {
313
+ if (n > 3) n = prev - (n - 3)
314
+ else n = prev + n
315
+ }
316
+ num = n
317
+ this.num_cache = { len: len - 1, n, diff }
318
+ return n
319
+ } else if (diff) {
320
+ if (num > 3) num = prev - (num - 3)
321
+ else num = prev + num
322
+ }
323
+ return num
324
+ }
325
+ getBools() {
326
+ for (let v of this.types) {
327
+ if (v === 3) this.bools.push(this.n(1) === 1)
328
+ }
329
+ }
330
+ getNums() {
331
+ let prev = 0
332
+ for (let v of this.types) {
333
+ if (v >= 4 && v <= 6) {
334
+ let num = this.dint(prev)
335
+ prev = num
336
+ if (v === 4) this.nums.push(num)
337
+ else if (v === 5) this.nums.push(-num)
338
+ else if (v === 6) {
339
+ if (num === 0 || num === 4) {
340
+ const moved = this.dint(prev)
341
+ prev = moved
342
+ const int = this.dint(prev)
343
+ prev = int
344
+ const neg = num === 0 ? 1 : -1
345
+ this.nums.push((int / Math.pow(10, moved - 1)) * neg)
346
+ } else {
347
+ const moved = num > 4 ? num - 4 : num
348
+ const neg = num > 4 ? -1 : 1
349
+ if (moved === 1) this.nums.push(neg === -1 ? {} : [])
350
+ else {
351
+ const int = this.dint(prev)
352
+ prev = int
353
+ this.nums.push((int / Math.pow(10, moved - 1)) * neg)
354
+ }
355
+ }
356
+ }
357
+ }
358
+ }
359
+ }
360
+ getKeys() {
361
+ let arr = 0
362
+ let obj = 0
363
+ for (let i = 0; i < this.keylens.length; i++) {
364
+ const [type, len] = this.keylens[i]
365
+ if (type < 2) {
366
+ this.keys.push(type === 0 ? arr++ : obj++)
367
+ } else {
368
+ if (type === 2) {
369
+ if (len === 0) {
370
+ const code = this.short()
371
+ this.keys.push([code])
372
+ } else {
373
+ let key = ""
374
+ for (let i2 = 0; i2 < len - 2; i2++) key += base64_rev[this.n(6)]
375
+ this.keys.push(key)
376
+ }
377
+ } else {
378
+ if (len === 2) {
379
+ this.keys.push("")
380
+ } else {
381
+ let key = ""
382
+ for (let i2 = 0; i2 < len - 2; i2++) {
383
+ key += String.fromCharCode(Number(this.leb128()))
384
+ }
385
+ this.keys.push(key)
386
+ }
387
+ }
388
+ }
389
+ }
390
+ }
391
+ bits() {
392
+ console.log(this.c, tobits(this.o, this.c))
393
+ }
394
+ movePads() {
395
+ if (this.c % 8 > 0) this.c += 8 - (this.c % 8)
396
+ }
397
+ getKey(i, keys) {
398
+ const k = this.keys[i - 1]
399
+ if (Array.isArray(k)) keys.unshift([2, k[0]])
400
+ else if (typeof k === "number") keys.unshift([this.keylens[i - 1][0], k])
401
+ else keys.unshift(k)
402
+ if (i > 1) {
403
+ const d = this.krefs[i - 2]
404
+ if (d > 0) this.getKey(this.krefs[d - 1], keys)
405
+ }
406
+ let i2 = 0
407
+ for (let k of keys) {
408
+ if (typeof k === "string") {
409
+ if (typeof this.str_rev[k] === "undefined") {
410
+ const _key = (this.str_len++).toString()
411
+ this.str[_key] = k
412
+ this.str_rev[k] = _key
413
+ }
414
+ } else if (Array.isArray(k) && k[0] === 2) {
415
+ keys[i2] = this.str[k[1].toString()]
416
+ }
417
+ i2++
418
+ }
419
+ }
420
+ build() {
421
+ const get = i => {
422
+ const type = this.types[i]
423
+ let val = null
424
+ if (type === 7 || type === 2) {
425
+ let len = this.short()
426
+ if (type === 2 && len === 0) {
427
+ const code = this.short().toString()
428
+ val = this.str[code]
429
+ } else {
430
+ val = ""
431
+ for (let i2 = 0; i2 < len; i2++) {
432
+ if (type === 7) {
433
+ val += String.fromCharCode(Number(this.leb128()))
434
+ } else {
435
+ val += base64_rev[this.n(6).toString()]
436
+ }
437
+ }
438
+ if (typeof this.str_rev[val] === "undefined") {
439
+ const _key = (this.str_len++).toString()
440
+ this.str[_key] = val
441
+ this.str_rev[val] = _key
442
+ }
443
+ }
444
+ } else if (type === 4) {
445
+ val = this.nums[this.nc++]
446
+ } else if (type === 5) val = this.nums[this.nc++]
447
+ else if (type === 6) val = this.nums[this.nc++]
448
+ else if (type === 1) val = null
449
+ else if (type === 3) val = this.bools[this.bc++]
450
+ return val
451
+ }
452
+ if (this.vrefs.length === 0) return (this.json = get(0))
453
+ this.json = null
454
+ let i = 0
455
+ let init = [[], []]
456
+ let type = key => (typeof key === "string" ? 2 : key[0])
457
+ let set = k => (init[k[0]][k[1]] = true)
458
+ let ex = k => init[k[0]][k[1]] === true
459
+ for (let v of this.vrefs) {
460
+ let keys = []
461
+ this.getKey(v, keys)
462
+ const val = get(i)
463
+ i++
464
+ let json = this.json
465
+ for (let i2 = 0; i2 < keys.length; i2++) {
466
+ let k = keys[i2]
467
+ if (json === null) {
468
+ let t = type(k)
469
+ set(k)
470
+ if (t === 0) {
471
+ this.json = []
472
+ json = this.json
473
+ if (i2 === keys.length - 1) {
474
+ json[0] = val
475
+ break
476
+ }
477
+ if (i2 === keys.length - 2) {
478
+ const k2 = keys[i2 + 1]
479
+ if (type(k2) === 0) {
480
+ json.push([val])
481
+ break
482
+ }
483
+ } else {
484
+ const k2 = keys[i2 + 1]
485
+ if (type(k2) === 0) {
486
+ set(k2)
487
+ json.push([])
488
+ json = json[json.length - 1]
489
+ } else if (type(k2) === 1) {
490
+ set(k2)
491
+ json.push({})
492
+ json = json[json.length - 1]
493
+ }
494
+ }
495
+ } else {
496
+ this.json = {}
497
+ json = this.json
498
+ if (i2 === keys.length - 2) {
499
+ const k2 = keys[i2 + 1]
500
+ json[k2] = val
501
+ break
502
+ }
503
+ }
504
+ if (i2 !== keys.length - 2) continue
505
+ } else if (i2 === 0) {
506
+ const k2 = keys[i2 + 1]
507
+ const t1 = type(k)
508
+ if (t1 === 0) {
509
+ if (keys.length === 1) {
510
+ json.push(val)
511
+ break
512
+ } else if (keys.length === 2) {
513
+ if (!ex(k2)) set(k2) && json.push([])
514
+ json = json[json.length - 1]
515
+ json.push(val)
516
+ break
517
+ }
518
+ const t2 = type(k2)
519
+ if (t2 === 0) {
520
+ if (!ex(k2)) set(k2) && json.push([])
521
+ json = json[json.length - 1]
522
+ } else if (t2 === 1) {
523
+ if (!ex(k2)) set(k2) && json.push({})
524
+ json = json[json.length - 1]
525
+ }
526
+ } else if (t1 === 1) {
527
+ if (keys.length === 2) {
528
+ json[k2] = val
529
+ break
530
+ }
531
+ }
532
+ continue
533
+ }
534
+ if (i2 === keys.length - 2) {
535
+ const jtype = Array.isArray(json) ? 0 : 1
536
+ const ctype = type(k)
537
+ const k2 = keys[i2 + 1]
538
+ const ntype = type(k2)
539
+ if (ctype === 0 && ntype === 0) {
540
+ if (!ex(k2)) set(k2) && json.push([])
541
+ json = json[json.length - 1]
542
+ json.push(val)
543
+ break
544
+ } else if (ctype === 1 && ntype === 2) {
545
+ json[k2] = val
546
+ break
547
+ } else if (jtype === 1 && ctype === 2) {
548
+ if (ntype === 0) {
549
+ json[k] ??= []
550
+ json[k].push(val)
551
+ }
552
+ break
553
+ } else {
554
+ //console.log("impossible 4")
555
+ }
556
+ } else {
557
+ const jtype = Array.isArray(json) ? 0 : 1
558
+ const ctype = type(k)
559
+ const k2 = keys[i2 + 1]
560
+ const ntype = type(k2)
561
+ if (jtype === 1 && ctype === 2) {
562
+ if (ntype === 0) {
563
+ json[k] ??= []
564
+ json = json[k]
565
+ } else if (ntype === 1) {
566
+ json[k] ??= {}
567
+ json = json[k]
568
+ } else {
569
+ //console.log("impossible 5")
570
+ }
571
+ } else if (jtype === 0 && ctype === 1) {
572
+ // console.log("impossible 6")
573
+ } else if (jtype === 0 && ctype === 0) {
574
+ if (ntype === 0) {
575
+ if (!ex(k2)) set(k2) && json.push([])
576
+ json = json[json.length - 1]
577
+ } else if (ntype === 1) {
578
+ if (!ex(k2)) set(k2) && json.push({})
579
+ json = json[json.length - 1]
580
+ } else {
581
+ // console.log("impossible 7")
582
+ }
583
+ } else if (jtype === 1 && ctype === 1) {
584
+ // nothing
585
+ } else {
586
+ //console.log("impossible 8")
587
+ }
588
+ }
589
+ }
590
+ }
591
+ }
592
+ }