-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathlegacyzip.js
3236 lines (3058 loc) · 101 KB
/
legacyzip.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview Library for decompressing various "legacy" ZIP formats
* @author Jeff Parsons <[email protected]>
* @copyright © 2012-2024 Jeff Parsons
* @license MIT <https://www.pcjs.org/LICENSE.txt>
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* Unpack(), Unsqueeze(), Uncrunch(), and Uncrush() decompression code adapted from ARC source code
* (C) Copyright 1985-87 by System Enhancement Associates and Thom Henderson, as released under the terms
* of the GPL at https://github.com/hyc/arc/blob/master/LICENSE
*
* Stretch(), Expand(), and Explode() functionality adapted from https://www.hanshq.net/files/hwzip/hwzip-2.1.zip,
* as released into the public domain by Hans Wennborg; see https://www.hanshq.net/zip2.html for more details.
*
* Blast() functionality adapted from https://github.com/madler/zlib/tree/master/contrib/blast
* Copyright (C) 2003, 2012, 2013 Mark Adler
*/
import { Buffer } from 'node:buffer';
const DEBUG = true;
/**
* Legacy ARC Support (decompression only)
*
* Programming notes (from ARC.C v5.21q):
*
* ARC Version 2 differs from version 1 in that archive entries
* are automatically compressed when they are added to the archive,
* making a separate compression step unnecessary. The nature of the
* compression is indicated by the header version number placed in
* each archive entry, as follows:
*
* 1 = Old style, no compression
* 2 = New style, no compression
* 3 = Compression of repeated characters only
* 4 = Compression of repeated characters plus Huffman SQueezing
* 5 = Lempel-Zev packing of repeated strings (old style)
* 6 = Lempel-Zev packing of repeated strings (new style)
* 7 = Lempel-Zev Williams packing with improved hash function
* 8 = Dynamic Lempel-Zev packing with adaptive reset
* 9 = Dynamic Lempel-Zev packing, larger hash table
*
* Type 5, Lempel-Zev packing, was added as of version 4.0
*
* Type 6 is Lempel-Zev packing where runs of repeated characters
* have been collapsed, and was added as of version 4.1
*
* Type 7 is a variation of Lempel-Zev using a different hash
* function which yields speed improvements of 20-25%, and was
* added as of version 4.6
*
* Type 8 is a different implementation of Lempel-Zev, using a
* variable code size and an adaptive block reset, and was added
* as of version 5.0
*
* Type 9 is a slight modification of type 8, first used by Phil
* Katz in his PKARC utilities. The primary difference is the use
* of a hash table twice as large as for type 8, and that this
* algorithm called Squashing, doesn't perform run-length encoding
* on the input data.
*
* Version 4.3 introduced a temporary file for holding the result
* of the first crunch pass, thus speeding up crunching.
*
* Version 4.4 introduced the ARCTEMP environment string, so that
* the temporary crunch file may be placed on a ramdisk. Also
* added was the distinction between Adding a file in all cases,
* and Updating a file only if the disk file is newer than the
* corresponding archive entry.
*
* The compression method to use is determined when the file is
* added, based on whichever method yields the smallest result.
*
* @class LegacyArc
*/
export class LegacyArc
{
/**
* unpackSync(src, dst_len)
*
* @param {Buffer} src (packed data)
* @param {number} dst_len
* @returns {Decompress}
*/
static unpackSync(src, dst_len)
{
let unpack = new ArcUnpack();
if (!unpack.decomp(src, dst_len)) {
delete unpack.dst;
}
return unpack;
}
/**
* unsqueezeSync(src, dst_len)
*
* @param {Buffer} src (squeezed data)
* @param {number} dst_len
* @returns {Decompress}
*/
static unsqueezeSync(src, dst_len)
{
let unsqueeze = new ArcUnsqueeze();
if (!unsqueeze.decomp(src, dst_len)) {
delete unsqueeze.dst;
}
return unsqueeze;
}
/**
* uncrunchSync(src, dst_len, type)
*
* @param {Buffer} src (crunched data)
* @param {number} dst_len
* @param {number} type (0 for unpacked, 1 for packed, 2 for packed w/new hash)
* @returns {Decompress}
*/
static uncrunchSync(src, dst_len, type)
{
let uncrunch = new ArcUncrunch();
if (!uncrunch.decomp(src, dst_len, type >= 1, type == 2)) {
delete uncrunch.dst;
}
return uncrunch;
}
/**
* uncrushSync(src, dst_len, squashed)
*
* @param {Buffer} src (crushed or squashed data)
* @param {number} dst_len
* @param {boolean} squashed (false if crushed, true if squashed)
* @returns {Decompress}
*/
static uncrushSync(src, dst_len, squashed)
{
let uncrush = new ArcUncrush();
if (!uncrush.decomp(src, dst_len, squashed)) {
delete uncrush.dst;
}
return uncrush;
}
static crctab = [ // CRC lookup table
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
];
/**
* getCRC(buf, crc)
*
* Calculate CRC for the given ARC data.
*
* The logic for this method of calculating the CRC 16 bit polynomial is taken
* from an article by David Schwaderer in the April 1985 issue of PC Tech Journal.
*
* @param {Buffer} buf
* @param {number} [crc] (running CRC value, if any)
*/
static getCRC(buf, crc = 0)
{
for (let i = 0; i < buf.length; i++) {
crc = ((crc >> 8) & 0xff) ^ LegacyArc.crctab[(crc ^ buf.readUInt8(i)) & 0xff];
}
return crc;
}
}
/**
* Legacy ZIP Support (decompression only)
*
* @class LegacyZip
*/
export class LegacyZip
{
/**
* stretchSync(src, dst_len)
*
* @param {Buffer} src (SHRINK data)
* @param {number} dst_len
* @returns {Decompress}
*/
static stretchSync(src, dst_len)
{
let stretch = new ZipStretch();
if (!stretch.decomp(src, dst_len)) {
delete stretch.dst;
}
return stretch;
}
/**
* expandSync(src, dst_len, comp_factor)
*
* @param {Buffer} src (REDUCE data)
* @param {number} dst_len
* @param {number} comp_factor
* @returns {Decompress}
*/
static expandSync(src, dst_len, comp_factor)
{
let expand = new ZipExpand();
if (!expand.decomp(src, dst_len, comp_factor)) {
delete expand.dst;
}
return expand;
}
/**
* explodeSync(src, dst_len, large_wnd, lit_tree)
*
* Decompress (explode) the data in src. The uncompressed data is dst_len
* bytes long. large_wnd is true if a large window was used for compression,
* and lit_tree is true if literals were Huffman coded.
*
* Returns a Decompress object. Call getBytesRead() to get the number of source
* bytes read, and getOutput() to the destination data.
*
* @param {Buffer} src (IMPLODE data)
* @param {number} dst_len
* @param {boolean} large_wnd
* @param {boolean} lit_tree
* @returns {Decompress}
*/
static explodeSync(src, dst_len, large_wnd, lit_tree)
{
let explode = new ZipExplode();
if (!explode.decomp(src, dst_len, large_wnd, lit_tree, false) || explode.getBytesRead() != src.length) {
if (!explode.decomp(src, dst_len, large_wnd, lit_tree, true) || explode.getBytesRead() != src.length) {
delete explode.dst;
}
// else {
// assert(false, "explodeSync() recovered"); // would like to catch just one example of that "PKZIP 1.01/1.02" bug
// }
}
return explode;
}
/**
* blastSync(src)
*
* @param {Buffer} src (IMPLODE_DCL data)
* @returns {ZipBlast}
*/
static blastSync(src)
{
let blast = new ZipBlast();
// let test = Buffer.from([0x00, 0x04, 0x82, 0x24, 0x25, 0x8f, 0x80, 0x7f]);
// blast.decomp(test);
blast.decomp(src);
return blast;
}
}
/**
* @class BitStream
* @property {Buffer} src // Source bytes
* @property {number} bit_pos // Bit position of the next bit to read
* @property {number} bit_end // Bit position of the past-the-end bit
*/
class BitStream
{
/**
* The original code set MIN_BITS to (64 - 7), because it read 64 bits at a time,
* and if bit_pos wasn't on a byte boundary, it would shift 1-7 zeros into the top
* of the result, and the caller would assert that it never used more than MIN_BITS.
*
* Well, this version reads 32 bits at a time, because while there is now BigInt
* support in JavaScript, it's faster sticking with 32-bit values. Unfortunately,
* setting MIN_BITS to (32 - 7) triggered assertions that the caller (ZipExplode.decomp())
* was using more than 25 bits from a single bits() call, which was risky.
*
* So now we set MIN_BITS to 32, and at the same time, whenever bit_pos isn't on a
* byte boundary, we pad the result with more bits from the stream instead of zeros;
* it may often be unnecessary, but if we can't predict the caller's needs, then
* we have no choice. Callers that specify how many bits they need will not pay that
* penalty.
*/
static MIN_BITS = 32;
/**
* constructor(src)
*
* @this {BitStream}
* @param {Buffer} src
*/
constructor(src)
{
this.src = src;
this.end = src.length;
this.bit_pos = 0;
this.bit_end = this.end * 8;
this.cachePos = -1;
this.cacheBits = 0;
}
/**
* avail(n)
*
* @this {BitStream}
* @param {number} n
* @returns {boolean} (true if at least n bits are available)
*/
avail(n)
{
return (this.bit_pos + n <= this.bit_end);
}
/**
* empty()
*
* Equivalent to !avail(0)
*
* @this {BitStream}
* @returns {boolean} (true if no more bits available)
*/
empty()
{
return (this.bit_pos >= this.bit_end);
}
/**
* bits(lsb, fAdvance)
*
* Gets the next bits from the input stream. The number of bits returned is
* between MIN_BITS and 32, depending on the position in the stream, or fewer
* if the end of stream is reached. The upper bits are zero-padded.
*
* Calling bits(n, true) is equivalent to lsb(bits(), n) followed by advance(n).
*
* @this {BitStream}
* @param {number} [lsb] (optional number of least significant bits)
* @param {boolean} [fAdvance] (true to advance the bit position by lsb bits)
*/
bits(lsb = 0, fAdvance = false)
{
let bits;
let next = (this.bit_pos >> 3);
let bit_off = this.bit_pos % 8;
assert(!lsb || lsb <= BitStream.MIN_BITS);
assert(next <= this.end, "reading past end of BitStream");
let bytesAvail = this.end - next;
if (bytesAvail >= 4) {
if (next == this.cachePos) {
bits = this.cacheBits;
} else {
bits = this.cacheBits = this.src.readUInt32LE(next);
this.cachePos = next;
}
next += 4;
bytesAvail = 4;
} else {
bits = 0;
for (let i = 0; i < bytesAvail; i++) {
bits |= this.src.readUInt8(next++) << (i << 3);
}
}
if (bit_off) {
bits >>>= bit_off;
let bitsAvail = (bytesAvail << 3) - bit_off;
/**
* If we're not sure how many bits the caller wants (because lsb is zero), or
* the caller wants more bits than we just read, then try to read one more byte.
*/
if ((!lsb && BitStream.MIN_BITS == 32 || lsb > bitsAvail) && next < this.end) {
let b = this.src.readUInt8(next) << (32 - bit_off);
bits |= b;
}
}
if (lsb) {
bits &= (1 << lsb) - 1;
if (fAdvance) this.advance(lsb);
}
return bits;
}
/**
* bitsSigned(lsb, fAdvance)
*
* Identical to bits(), but the highest requested bit is treated as a sign bit,
* so if that bit is set, then it is propagated to the sign bit of our return value.
*
* @this {BitStream}
* @param {number} [lsb] (optional number of least significant bits)
* @param {boolean} [fAdvance] (true to advance the bit position by lsb bits)
*/
bitsSigned(lsb = 0, fAdvance = false)
{
let bits = this.bits(lsb, fAdvance);
if (lsb > 0 && lsb < 32 && (bits & (1 << (lsb - 1)))) {
bits |= -1 << lsb;
}
return bits;
}
/**
* advance(n)
*
* Advances n bits in the BitStream.
*
* @this {BitStream}
* @param {number} n
* @returns {boolean} (true if successful, false if that many bits are not available in the stream)
*/
advance(n)
{
assert(this.bit_pos <= this.bit_end);
if (this.bit_end - this.bit_pos < n) {
return false;
}
this.bit_pos += n;
return true;
}
/**
* getRead(n)
*
* Returns the number of n-bit units read (ie, number of bits if n is 1, number of bytes if n is 8, etc)
*
* @this {BitStream}
* @param {number} [n]
* @returns {number}
*/
getRead(n = 1)
{
return (BitStream.roundUp(this.bit_pos, n) / n);
}
/**
* lsb(v, n)
*
* Returns the bottom n bits ("least significant bits") of v.
*
* @param {number} v
* @param {number} n
* @returns {number}
*/
static lsb(v, n)
{
return v & ((1 << n) - 1);
}
/**
* roundUp(x, m)
*
* Rounds x up to the next multiple of m, which must be a power of 2.
*
* @param {number} x
* @param {number} m
* @returns {number}
*/
static roundUp(x, m)
{
assert((m & (m - 1)) == 0, `${m} must be a power of two`);
return (x + m - 1) & (-m); // Hacker's Delight (2nd), 3-1.
}
}
/**
* @typedef {Object} Lookup
* @property {number} sym
* @property {number} len
*
* @class HuffmanDecoder
* @property {Array.<Lookup>} table
*/
class HuffmanDecoder
{
static MAX_HUFFMAN_SYMBOLS = 288; // deflate uses max 288 symbols
static MAX_HUFFMAN_BITS = 16; // implode uses max 16-bit codewords
static HUFFMAN_LOOKUP_TABLE_BITS = 8; // seems a good trade-off
/**
* constructor()
*
* @this {HuffmanDecoder}
*/
constructor()
{
/**
* In the original implementation, table was an array of 16-bit values,
* split into two bit-fields:
*
* sym: 9 bits (wide enough to fit the max symbol nbr)
* len: 8 bits (0 means no symbol)
*
* In this implementation, each entry is simply an object with 'sym' and 'len' properties.
*/
this.table = new Array(1 << HuffmanDecoder.HUFFMAN_LOOKUP_TABLE_BITS);
this.sentinel_bits = new Array(HuffmanDecoder.MAX_HUFFMAN_BITS + 1);
this.offset_first_sym_idx = new Array(HuffmanDecoder.MAX_HUFFMAN_BITS + 1);
this.syms = new Array(HuffmanDecoder.MAX_HUFFMAN_SYMBOLS);
this.num_syms = 0; // for debugging only
}
/**
* init(lengths, n)
*
* @this {HuffmanDecoder}
* @param {Array.<number>} lengths
* @param {number} n
* @returns {boolean}
*/
init(lengths, n)
{
let /* uint16_t */ count = new Array(HuffmanDecoder.MAX_HUFFMAN_BITS + 1).fill(0);
let /* uint16_t */ code = new Array(HuffmanDecoder.MAX_HUFFMAN_BITS + 1);
let /* uint16_t */ sym_idx = new Array(HuffmanDecoder.MAX_HUFFMAN_BITS + 1);
assert(n <= HuffmanDecoder.MAX_HUFFMAN_SYMBOLS);
this.num_syms = n;
/**
* Zero-initialize the lookup table
*/
for (let i = 0; i < this.table.length; i++) {
this.table[i] = {
sym: 0,
len: 0
};
}
/**
* Count the number of codewords of each length
*/
for (let i = 0; i < n; i++) {
assert(lengths[i] <= HuffmanDecoder.MAX_HUFFMAN_BITS);
count[lengths[i]]++;
}
count[0] = 0; // ignore zero-length codewords
/**
* Compute sentinel_bits and offset_first_sym_idx for each length
*/
code[0] = 0;
sym_idx[0] = 0;
for (let l = 1; l <= HuffmanDecoder.MAX_HUFFMAN_BITS; l++) {
/**
* First canonical codeword of this length
*/
code[l] = ((code[l - 1] + count[l - 1]) << 1) & 0xffff;
if (count[l] != 0 && code[l] + count[l] - 1 > (1 << l) - 1) {
/**
* The last codeword is longer than l bits
*/
return false;
}
let s = ((code[l] + count[l]) << (HuffmanDecoder.MAX_HUFFMAN_BITS - l)) & 0xffffffff;
this.sentinel_bits[l] = s;
assert(this.sentinel_bits[l] >= code[l], "overflow");
sym_idx[l] = sym_idx[l - 1] + count[l - 1];
this.offset_first_sym_idx[l] = (sym_idx[l] - code[l]) & 0xffff;
}
/**
* Build mapping from index to symbol and populate the lookup table
*/
for (let i = 0; i < n; i++) {
let l = lengths[i];
if (l == 0) continue;
this.syms[sym_idx[l]] = i & 0xffff;
sym_idx[l]++;
if (l <= HuffmanDecoder.HUFFMAN_LOOKUP_TABLE_BITS) {
this.insert(i, l, code[l]);
code[l]++;
}
}
return true;
}
/**
* insert(sym, len, codeword)
*
* @this {HuffmanDecoder}
* @param {number} sym
* @param {number} len
* @param {number} codeword
*/
insert(/* size_t */ sym, /* int */ len, /* uint16_t */ codeword)
{
assert(len <= HuffmanDecoder.HUFFMAN_LOOKUP_TABLE_BITS);
codeword = HuffmanDecoder.reverse16(codeword, len); // Make it LSB-first
let pad_len = HuffmanDecoder.HUFFMAN_LOOKUP_TABLE_BITS - len;
/**
* Pad the pad_len upper bits with all bit combinations
*/
for (let padding = 0; padding < (1 << pad_len); padding++) {
let index = (codeword | (padding << len)) & 0xffff;
this.table[index].sym = sym & 0xffff;
this.table[index].len = len & 0xffff;
assert(this.table[index].sym == sym && this.table[index].len == len, "bitfield overflow");
}
}
/**
* decode(bits)
*
* Decode a symbol from the LSB-first zero-padded bits.
*
* Returns {sym, used}, where sym is the decoded symbol number or -1 if no
* symbol could be decoded, and used is the number of bits used to decode the
* symbol, or zero if no symbol could be decoded.
*
* @this {HuffmanDecoder}
* @param {number} bits
* @returns {Object} ({sym, used})
*/
decode(/* uint16_t */ bits)
{
bits &= 0xffff;
/**
* First try the lookup table
*/
let lookup_bits = BitStream.lsb(bits, HuffmanDecoder.HUFFMAN_LOOKUP_TABLE_BITS);
assert(lookup_bits < this.table.length);
if (this.table[lookup_bits].len) {
assert(this.table[lookup_bits].len <= HuffmanDecoder.HUFFMAN_LOOKUP_TABLE_BITS);
assert(this.table[lookup_bits].sym < this.num_syms);
return {sym: this.table[lookup_bits].sym, used: this.table[lookup_bits].len};
}
/**
* Then do canonical decoding with the bits in MSB-first order
*/
bits = HuffmanDecoder.reverse16(bits, HuffmanDecoder.MAX_HUFFMAN_BITS);
for (let l = HuffmanDecoder.HUFFMAN_LOOKUP_TABLE_BITS + 1; l <= HuffmanDecoder.MAX_HUFFMAN_BITS; l++) {
if (bits < this.sentinel_bits[l]) {
bits >>= HuffmanDecoder.MAX_HUFFMAN_BITS - l;
let sym_idx = (this.offset_first_sym_idx[l] + bits) & 0xffff;
assert(sym_idx < this.num_syms);
return {sym: this.syms[sym_idx], used: l};
}
}
return {sym: -1, used: 0};
}
/**
* reverse16(x, n)
*
* Reverse the n least significant bits of x.
* The (16 - n) most significant bits of the result will be zero.
*
* @param {number} x
* @param {number} n
* @returns {number}
*/
static reverse16(/* uint16_t */ x, /* int */ n)
{
let /* uint16_t */ lo, hi;
let /* uint16_t */ reversed;
assert(n > 0 && n <= 16);
lo = x & 0xff;
hi = x >> 8;
reversed = ((HuffmanDecoder.reverse8_tbl[lo] << 8) | HuffmanDecoder.reverse8_tbl[hi]) & 0xffff;
return reversed >> (16 - n);
}
static /* uint8_t */ reverse8_tbl = [
/* 0x00 */ 0x00,
/* 0x01 */ 0x80,
/* 0x02 */ 0x40,
/* 0x03 */ 0xc0,
/* 0x04 */ 0x20,
/* 0x05 */ 0xa0,
/* 0x06 */ 0x60,
/* 0x07 */ 0xe0,
/* 0x08 */ 0x10,
/* 0x09 */ 0x90,
/* 0x0a */ 0x50,
/* 0x0b */ 0xd0,
/* 0x0c */ 0x30,
/* 0x0d */ 0xb0,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0xd2 */ 0x4b,
/* 0xd3 */ 0xcb,
/* 0xd4 */ 0x2b,
/* 0xd5 */ 0xab,
/* 0xd6 */ 0x6b,
/* 0xd7 */ 0xeb,
/* 0xd8 */ 0x1b,
/* 0xd9 */ 0x9b,
/* 0xda */ 0x5b,
/* 0xdb */ 0xdb,
/* 0xdc */ 0x3b,
/* 0xdd */ 0xbb,
/* 0xde */ 0x7b,
/* 0xdf */ 0xfb,
/* 0xe0 */ 0x07,
/* 0xe1 */ 0x87,
/* 0xe2 */ 0x47,
/* 0xe3 */ 0xc7,
/* 0xe4 */ 0x27,
/* 0xe5 */ 0xa7,
/* 0xe6 */ 0x67,
/* 0xe7 */ 0xe7,
/* 0xe8 */ 0x17,
/* 0xe9 */ 0x97,
/* 0xea */ 0x57,
/* 0xeb */ 0xd7,
/* 0xec */ 0x37,
/* 0xed */ 0xb7,
/* 0xee */ 0x77,
/* 0xef */ 0xf7,
/* 0xf0 */ 0x0f,
/* 0xf1 */ 0x8f,
/* 0xf2 */ 0x4f,
/* 0xf3 */ 0xcf,
/* 0xf4 */ 0x2f,
/* 0xf5 */ 0xaf,
/* 0xf6 */ 0x6f,
/* 0xf7 */ 0xef,
/* 0xf8 */ 0x1f,
/* 0xf9 */ 0x9f,
/* 0xfa */ 0x5f,
/* 0xfb */ 0xdf,
/* 0xfc */ 0x3f,
/* 0xfd */ 0xbf,
/* 0xfe */ 0x7f,
/* 0xff */ 0xff
];
}
/**
* @class Decompress
* @property {BitStream} bs
* @property {Buffer} dst
*
* Decompress is the base class for ArcUnpack, ZipStretch, ZipExpand, and ZipExplode classes.
*
* ArcUnpack, in turn, is the base class for ArcUnsqueeze, ArcUncrunch, and ArcUncrush classes.
*/
class Decompress
{
/**
* init(src, dst_len)
*
* Initialize buffers and decompression state.
*
* @this {Decompress}
* @param {Buffer} src
* @param {number} dst_len
*/
init(src, dst_len)
{
this.bs = new BitStream(src);
this.dst = Buffer.alloc(dst_len);
this.dst_pos = 0;
}
/**
* getBytesRead()
*
* Returns the number of bytes read from the source BitStream.
*
* @this {Decompress}
* @returns {number}
*/
getBytesRead()
{
return this.bs.getRead(8);
}
/**
* getOutput()