-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdata_loading.h
1797 lines (1601 loc) · 63.1 KB
/
data_loading.h
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
#ifndef BWGAME_DATA_LOADING_H
#define BWGAME_DATA_LOADING_H
#include "util.h"
#include "containers.h"
#include "data_types.h"
#include <type_traits>
#include <array>
#include <cstring>
#include <cstdio>
namespace bwgame {
namespace data_loading {
static_assert(std::is_same<uint8_t, unsigned char>::value || std::is_same<uint8_t, char>::value, "uint8_t must be (unsigned) char (we use it for aliasing)");
template<typename T>
struct is_std_array : std::false_type {};
template<typename T, size_t N>
struct is_std_array<std::array<T, N>> : std::true_type{};
// todo: Nothing here should throw exceptions. We should instead have a type to report errors,
// and all functions should set it through a reference or similar (optional/variant?).
// (allocators can still throw exceptions if they want to)
static inline bool is_native_little_endian() {
union endian_t {uint32_t a; uint8_t b;};
return (endian_t{1}).b == 1;
}
template<typename T, bool little_endian, typename std::enable_if<!is_std_array<T>::value>::type* = nullptr>
static inline T value_at(const uint8_t* ptr) {
static_assert(std::is_integral<T>::value, "can only read integers and arrays of integers");
union endian_t {uint32_t a; uint8_t b;};
const bool native_little_endian = (endian_t{1}).b == 1;
if (little_endian == native_little_endian) {
if (((uintptr_t)(void*)ptr & (alignof(T) - 1)) == 0) {
return *(T*)(void*)ptr;
} else {
typename std::aligned_storage<sizeof(T), alignof(T)>::type buf;
memcpy(&buf, ptr, sizeof(T));
return *(T*)(void*)&buf;
}
} else {
T r = 0;
for (size_t i = 0; i < sizeof(T); ++i) {
r |= (T)ptr[i] << ((little_endian ? i : sizeof(T) - 1 - i) * 8);
}
return r;
}
}
template<typename T, bool little_endian, typename std::enable_if<is_std_array<T>::value>::type* = nullptr>
static inline T value_at(const uint8_t* ptr) {
T r;
for (auto& v : r) {
v = value_at<typename std::remove_reference<decltype(v)>::type, little_endian>(ptr);
ptr += sizeof(v);
}
return r;
}
template<bool little_endian, typename T>
static inline void set_value_at(uint8_t* ptr, T value) {
static_assert(std::is_integral<T>::value, "can only write integers");
union endian_t {uint32_t a; uint8_t b;};
const bool native_little_endian = (endian_t{1}).b == 1;
if (little_endian == native_little_endian) {
if (((uintptr_t)(void*)ptr & (alignof(T) - 1)) == 0) {
*(T*)(void*)ptr = value;
} else {
memcpy(ptr, &value, sizeof(T));
}
} else {
for (size_t i = 0; i < sizeof(T); ++i) {
ptr[i] = value >> ((little_endian ? i : sizeof(T) - 1 - i) * 8);
}
}
}
template<typename T, bool little_endian, typename self_T>
T get_impl(self_T& self) {
typename std::aligned_storage<sizeof(T), alignof(T)>::type buf;
self.get_bytes((uint8_t*)&buf, sizeof(T));
return value_at<T, little_endian>((uint8_t*)&buf);
}
template<bool default_little_endian = true, bool bounds_checking = true>
struct data_reader {
const uint8_t* ptr = nullptr;
const uint8_t* begin = nullptr;
const uint8_t* end = nullptr;
data_reader() = default;
data_reader(const uint8_t* ptr, const uint8_t* end) : ptr(ptr), begin(ptr), end(end) {}
template<typename T, bool little_endian = default_little_endian>
T get() {
if (bounds_checking && left() < sizeof(T)) error("data_reader: attempt to read past end");
ptr += sizeof(T);
return value_at<T, little_endian>(ptr - sizeof(T));
}
const uint8_t* get_n(size_t n) {
const uint8_t* r = ptr;
if (bounds_checking && left() < n) error("data_reader: attempt to read past end");
ptr += n;
return r;
}
template<typename T, bool little_endian = default_little_endian>
a_vector<T> get_vec(size_t n) {
const uint8_t* data = get_n(n*sizeof(T));
a_vector<T> r(n);
for (size_t i = 0; i < n; ++i, data += sizeof(T)) {
r[i] = value_at<T, little_endian>(data);
}
return r;
}
void skip(size_t n) {
if (bounds_checking && left() < n) error("data_reader: attempt to seek past end");
ptr += n;
}
void get_bytes(uint8_t* dst, size_t n) {
memcpy(dst, get_n(n), n);
}
void seek(size_t offset) {
if (offset > size()) error("data_reader: attempt to seek past end");
ptr = begin + offset;
}
size_t left() const {
return end - ptr;
}
size_t size() const {
return (size_t)(end - begin);
}
size_t tell() const {
return (size_t)(ptr - begin);
}
};
using data_reader_le = data_reader<true>;
using data_reader_be = data_reader<false>;
template<typename base_reader_T, size_t page_size = 0x1000, bool default_little_endian = true>
struct paged_reader {
base_reader_T& reader;
std::array<uint8_t, page_size> page;
size_t current_page = ~(size_t)0;
size_t file_pointer = 0;
size_t file_size = 0;
explicit paged_reader(base_reader_T& reader) : reader(reader) {
file_size = reader.size();
}
void get_bytes(uint8_t* dst, size_t n) {
if (left() < n) error("paged_reader: attempt to read past end");
if (n > page_size / 2) {
reader.seek(file_pointer);
reader.get_bytes(dst, n);
} else {
while (n) {
size_t page_n = file_pointer / page_size;
size_t page_offset = file_pointer % page_size;
size_t read_n = std::min(n, page_size - page_offset);
if (current_page == page_n) {
memcpy(dst, &page[page_offset], read_n);
} else {
if (read_n == page_size - page_offset) {
reader.seek(file_pointer);
reader.get_bytes(dst, read_n);
} else {
current_page = page_n;
reader.seek(page_n * page_size);
reader.get_bytes(page.data(), std::min(page_size, file_size - page_n * page_size));
memcpy(dst, &page[page_offset], read_n);
}
}
dst += read_n;
file_pointer += read_n;
n -= read_n;
}
}
}
template<typename T, bool little_endian = default_little_endian>
T get() {
uint8_t buffer[sizeof(T)];
get_bytes(buffer, sizeof(T));
return value_at<T, little_endian>(buffer);
}
void seek(size_t offset) {
if (offset > file_size) error("paged_reader: attempt to seek past end");
file_pointer = offset;
}
size_t left() const {
return file_size - file_pointer;
}
size_t size() const {
return file_size;
}
size_t tell() const {
return file_pointer;
}
bool eof() const {
return file_pointer == file_size;
}
};
template<bool default_little_endian = true>
struct file_reader {
a_string filename;
FILE* f = nullptr;
file_reader() = default;
explicit file_reader(a_string filename) {
open(std::move(filename));
}
~file_reader() {
if (f) fclose(f);
}
file_reader(const file_reader&) = delete;
file_reader(file_reader&& n) {
f = n.f;
n.f = nullptr;
}
file_reader& operator=(const file_reader&) = delete;
file_reader& operator=(file_reader&& n) {
std::swap(f, n.f);
return *this;
}
void open(a_string filename) {
if (f) fclose(f);
f = fopen(filename.c_str(), "rb");
if (!f) error("file_reader: failed to open %s for reading", filename.c_str());
this->filename = std::move(filename);
}
void get_bytes(uint8_t* dst, size_t n) {
if (!fread(dst, n, 1, f)) {
if (feof(f)) error("file_reader: %s: attempt to read past end", filename);
error("file_reader: %s: read error", filename);
}
}
template<typename T, bool little_endian = default_little_endian>
T get() {
uint8_t buffer[sizeof(T)];
get_bytes(buffer, sizeof(T));
return value_at<T, little_endian>(buffer);
}
template<typename T, bool little_endian = default_little_endian, typename std::enable_if<sizeof(T) == 1>::type* = nullptr>
a_vector<T> get_vec(size_t n) {
a_vector<T> r(n);
get_bytes((uint8_t*)(void*)r.data(), n * sizeof(T));
return r;
}
template<typename T, bool little_endian = default_little_endian, typename std::enable_if<(sizeof(T) > 1)>::type* = nullptr>
a_vector<T> get_vec(size_t n) {
a_vector<T> r(n);
for (size_t i = 0; i < n; ++i) {
r[i] = get<T, little_endian>();
}
return r;
}
void seek(size_t offset) {
if ((size_t)(long)offset != offset || fseek(f, (long)offset, SEEK_SET)) error("file_reader: %s: failed to seek to offset %d", filename, offset);
}
bool eof() {
return feof(f);
}
size_t left() const {
return size() - tell();
}
size_t tell() const {
return (size_t)ftell(f);
}
size_t size() {
auto prev_pos = ftell(f);
fseek(f, 0, SEEK_END);
auto r = ftell(f);
fseek(f, prev_pos, SEEK_SET);
return r;
}
};
using crypt_table_t = std::array<uint32_t, 256 * 5>;
static auto get_crypt_table() {
uint32_t n = 0x100001;
crypt_table_t crypt_table;
auto next = [&]() {
n = (n * 125 + 3) % 0x2aaaab;
return n & 0xffff;
};
for (size_t i = 0; i != 256; ++i) {
for (size_t i2 = 0; i2 != 5; ++i2) {
crypt_table[256 * i2 + i] = next() << 16;
crypt_table[256 * i2 + i] |= next();
}
}
return crypt_table;
}
template<typename base_reader_T, bool default_little_endian = true>
struct encrypted_reader {
base_reader_T& reader;
size_t end_pos = 0;
uint32_t key;
uint32_t add_n = 0xeeeeeeee;
const crypt_table_t& crypt_table;
uint32_t data;
size_t data_n = 0;
size_t pos = 0;
encrypted_reader(base_reader_T& reader, size_t end_pos, uint32_t key, const crypt_table_t& crypt_table) : reader(reader), end_pos(end_pos), key(key), crypt_table(crypt_table) {
}
void next() {
pos += 4;
auto d = reader.template get<uint32_t, true>();
add_n += crypt_table[(key&0xff) + 1024];
uint32_t xor_n = key + add_n;
data = d ^ xor_n;
add_n = add_n * 33 + data + 3;
key = ((~key << 21) + 0x11111111) | key >> 11;
}
void get_bytes(uint8_t* dst, size_t n) {
if (left() < n) error("encrypted_reader: attempt to read past end");
size_t i = 0;
if (data_n) {
for (;i != std::min(n, data_n); ++i) {
dst[i] = data >> (8 * (4 - data_n + i));
}
data_n -= std::min(n, data_n);
}
while (i + 4 <= n) {
next();
set_value_at<true>(dst + i, data);
i += 4;
}
if (i != n) {
if (end_pos - pos < 4) {
reader.get_bytes(&dst[i], n - i);
data_n = 0;
pos += n - i;
} else {
next();
data_n = 4 - (n - i);
for (size_t i2 = 0; i != n; ++i, ++i2) {
dst[i] = data >> (8 * i2);
}
}
}
}
template<typename T, bool little_endian = default_little_endian>
T get() {
return get_impl<T, little_endian>(*this);
}
size_t left() const {
return end_pos - pos + data_n;
}
size_t size() const {
return end_pos;
}
size_t tell() const {
return pos - data_n;
}
};
template<bool little_endian = true, typename base_reader_T>
auto make_encrypted_reader(base_reader_T& reader, size_t end_pos, uint32_t key, const crypt_table_t& crypt_table) {
return encrypted_reader<base_reader_T, little_endian>(reader, end_pos, key, crypt_table);
}
static uint32_t string_hash(const char* str, int n, const crypt_table_t& crypt_table) {
uint32_t r = 0x7FED7FED;
uint32_t add_n = 0xeeeeeeee;
for (;*str; ++str) {
char c = *str;
if (c == '/') c = '\\';
else if (c >= 'a' && c <= 'z') c += 'A' - 'a';
r = (r + add_n) ^ crypt_table[256 * n + c];
add_n = add_n * 33 + c + r + 3;
}
return r;
}
template<typename base_reader_T, bool default_little_endian = true>
struct bit_reader {
base_reader_T& r;
uint64_t data{};
size_t bits_n = 0;
explicit bit_reader(base_reader_T& r) : r(r) {}
void next() {
auto left = r.left();
if (left >= 4) {
data |= (uint64_t)r.template get<uint32_t, true>() << bits_n;
bits_n += 32;
return;
}
switch (left) {
case 0:
case 1:
data |= (uint64_t)r.template get<uint8_t>() << bits_n;
bits_n += 8;
break;
case 2:
data |= (uint64_t)r.template get<uint16_t, true>() << bits_n;
bits_n += 16;
break;
case 3:
data |= (uint64_t)r.template get<uint16_t, true>() << bits_n;
bits_n += 16;
data |= (uint64_t)r.template get<uint8_t>() << bits_n;
bits_n += 8;
break;
}
}
template<size_t bits, bool little_endian = default_little_endian>
auto get_bits() {
static_assert(bits <= 32, "bit_reader: only up to 32 bit reads are supported");
using r_t = uint_fastn_t<bits>;
if (bits_n < bits) {
next();
}
r_t r = data & (((r_t)1 << bits) - 1);
data >>= bits;
bits_n -= bits;
return r;
}
template<typename T, bool little_endian = default_little_endian>
T get() {
return get_bits<int_bits<T>::value, little_endian>();
}
};
template<bool little_endian = true, typename base_reader_T>
auto make_bit_reader(base_reader_T& reader) {
return bit_reader<base_reader_T, little_endian>(reader);
}
template<bool little_endian = true>
void decompress(uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) {
data_reader<little_endian> source_r(input, input + input_size);
auto r = make_bit_reader(source_r);
int type = r.template get<uint8_t>();
int distance_bits = r.template get<uint8_t>();
if (distance_bits != 4 && distance_bits != 5 && distance_bits != 6) error("decompress: invalid distance bits %d", distance_bits);
auto get_length = [&]() {
switch (r.template get_bits<2>()) {
case 3: return 1;
case 0:
switch (r.template get_bits<2>()) {
case 3: return 6;
case 0:
switch (r.template get_bits<6>()) {
case 3: return 22;
case 7: return 23;
case 11: return 24;
case 15: return 25;
case 19: return 26;
case 23: return 27;
case 27: return 28;
case 31: return 29;
case 35: return 30;
case 39: return 31;
case 43: return 32;
case 47: return 33;
case 51: return 34;
case 55: return 35;
case 59: return 36;
case 63: return 37;
case 0: return 262 + 8 * r.template get_bits<5>();
case 1: return r.template get_bits<1>() ? 54 : 38;
case 2: return 70 + 16 * r.template get_bits<2>();
case 4: return 134 + 8 * r.template get_bits<4>();
case 5: return r.template get_bits<1>() ? 55 : 39;
case 6: return 71 + 16 * r.template get_bits<2>();
case 8: return 263 + 8 * r.template get_bits<5>();
case 9: return r.template get_bits<1>() ? 56 : 40;
case 10: return 72 + 16 * r.template get_bits<2>();
case 12: return 135 + 8 * r.template get_bits<4>();
case 13: return r.template get_bits<1>() ? 57 : 41;
case 14: return 73 + 16 * r.template get_bits<2>();
case 16: return 264 + 8 * r.template get_bits<5>();
case 17: return r.template get_bits<1>() ? 58 : 42;
case 18: return 74 + 16 * r.template get_bits<2>();
case 20: return 136 + 8 * r.template get_bits<4>();
case 21: return r.template get_bits<1>() ? 59 : 43;
case 22: return 75 + 16 * r.template get_bits<2>();
case 24: return 265 + 8 * r.template get_bits<5>();
case 25: return r.template get_bits<1>() ? 60 : 44;
case 26: return 76 + 16 * r.template get_bits<2>();
case 28: return 137 + 8 * r.template get_bits<4>();
case 29: return r.template get_bits<1>() ? 61 : 45;
case 30: return 77 + 16 * r.template get_bits<2>();
case 32: return 266 + 8 * r.template get_bits<5>();
case 33: return r.template get_bits<1>() ? 62 : 46;
case 34: return 78 + 16 * r.template get_bits<2>();
case 36: return 138 + 8 * r.template get_bits<4>();
case 37: return r.template get_bits<1>() ? 63 : 47;
case 38: return 79 + 16 * r.template get_bits<2>();
case 40: return 267 + 8 * r.template get_bits<5>();
case 41: return r.template get_bits<1>() ? 64 : 48;
case 42: return 80 + 16 * r.template get_bits<2>();
case 44: return 139 + 8 * r.template get_bits<4>();
case 45: return r.template get_bits<1>() ? 65 : 49;
case 46: return 81 + 16 * r.template get_bits<2>();
case 48: return 268 + 8 * r.template get_bits<5>();
case 49: return r.template get_bits<1>() ? 66 : 50;
case 50: return 82 + 16 * r.template get_bits<2>();
case 52: return 140 + 8 * r.template get_bits<4>();
case 53: return r.template get_bits<1>() ? 67 : 51;
case 54: return 83 + 16 * r.template get_bits<2>();
case 56: return 269 + 8 * r.template get_bits<5>();
case 57: return r.template get_bits<1>() ? 68 : 52;
case 58: return 84 + 16 * r.template get_bits<2>();
case 60: return 141 + 8 * r.template get_bits<4>();
case 61: return r.template get_bits<1>() ? 69 : 53;
case 62: return 85 + 16 * r.template get_bits<2>();
}
case 1:
switch (r.template get_bits<1>()) {
case 1: return 7;
case 0: return r.template get_bits<1>() ? 9 : 8;
}
case 2:
switch (r.template get_bits<3>()) {
case 1: return 10;
case 3: return 11;
case 5: return 12;
case 7: return 13;
case 0: return r.template get_bits<1>() ? 18 : 14;
case 2: return r.template get_bits<1>() ? 19 : 15;
case 4: return r.template get_bits<1>() ? 20 : 16;
case 6: return r.template get_bits<1>() ? 21 : 17;
}
}
case 1: return r.template get_bits<1>() ? 0 : 2;
case 2:
switch (r.template get_bits<1>()) {
case 1: return 3;
case 0: return r.template get_bits<1>() ? 4 : 5;
}
}
return -1;
};
auto get_distance = [&]() {
switch (r.template get_bits<2>()) {
case 3: return 0;
case 0:
switch (r.template get_bits<5>()) {
case 1: return 39;
case 2: return 47;
case 3: return 31;
case 5: return 35;
case 6: return 43;
case 7: return 27;
case 9: return 37;
case 10: return 45;
case 11: return 29;
case 13: return 33;
case 14: return 41;
case 15: return 25;
case 17: return 38;
case 18: return 46;
case 19: return 30;
case 21: return 34;
case 22: return 42;
case 23: return 26;
case 25: return 36;
case 26: return 44;
case 27: return 28;
case 29: return 32;
case 30: return 40;
case 31: return 24;
case 0: return r.template get_bits<1>() ? 62 : 63;
case 4: return r.template get_bits<1>() ? 54 : 55;
case 8: return r.template get_bits<1>() ? 58 : 59;
case 12: return r.template get_bits<1>() ? 50 : 51;
case 16: return r.template get_bits<1>() ? 60 : 61;
case 20: return r.template get_bits<1>() ? 52 : 53;
case 24: return r.template get_bits<1>() ? 56 : 57;
case 28: return r.template get_bits<1>() ? 48 : 49;
}
case 1:
switch (r.template get_bits<2>()) {
case 1: return 2;
case 3: return 1;
case 0: return r.template get_bits<1>() ? 5 : 6;
case 2: return r.template get_bits<1>() ? 3 : 4;
}
case 2:
switch (r.template get_bits<4>()) {
case 1: return 14;
case 2: return 18;
case 3: return 10;
case 4: return 20;
case 5: return 12;
case 6: return 16;
case 7: return 8;
case 8: return 21;
case 9: return 13;
case 10: return 17;
case 11: return 9;
case 12: return 19;
case 13: return 11;
case 14: return 15;
case 15: return 7;
case 0: return r.template get_bits<1>() ? 22 : 23;
}
}
return -1;
};
size_t out_pos = 0;
if (type == 0) {
while (out_pos != output_size) {
if (r.template get_bits<1>()) {
size_t len = 2 + get_length();
size_t distance = 0;
if (len == 519) error("decompress: eof marker found too early");
if (len == 2) {
distance = get_distance() << 2;
distance |= r.template get_bits<2>();
} else {
distance = get_distance() << distance_bits;
if (distance_bits == 4) distance |= r.template get_bits<4>();
else if (distance_bits == 5) distance |= r.template get_bits<5>();
else distance |= r.template get_bits<6>();
}
size_t src_pos = out_pos - 1 - distance;
if (src_pos > output_size) {
len = 0;
}
if (src_pos + len > output_size) {
len = output_size - src_pos;
}
if (out_pos + len > output_size) {
len = output_size - out_pos;
}
for (size_t i = 0; i != len; ++i) {
output[out_pos + i] = output[src_pos + i];
}
out_pos += len;
} else {
output[out_pos] = r.template get<uint8_t>();
++out_pos;
}
}
} else error("decompress: type %d not supported", type);
}
static const uint8_t huffman_weight_tables[9][256 + 2] = {
{ 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0x00, 0x00 },
{ 0x54, 0x16, 0x16, 0x0d, 0x0c, 0x08, 0x06, 0x05, 0x06, 0x05, 0x06, 0x03, 0x04, 0x04, 0x03, 0x05,
0x0e, 0x0b, 0x14, 0x13, 0x13, 0x09, 0x0b, 0x06, 0x05, 0x04, 0x03, 0x02, 0x03, 0x02, 0x02, 0x02,
0x0d, 0x07, 0x09, 0x06, 0x06, 0x04, 0x03, 0x02, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02,
0x09, 0x06, 0x04, 0x04, 0x04, 0x04, 0x03, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x04,
0x08, 0x03, 0x04, 0x07, 0x09, 0x05, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02,
0x06, 0x0a, 0x08, 0x08, 0x06, 0x07, 0x04, 0x03, 0x04, 0x04, 0x02, 0x02, 0x04, 0x02, 0x03, 0x03,
0x04, 0x03, 0x07, 0x07, 0x09, 0x06, 0x04, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02,
0x0a, 0x02, 0x02, 0x03, 0x02, 0x02, 0x01, 0x01, 0x02, 0x02, 0x02, 0x06, 0x03, 0x05, 0x02, 0x03,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x04, 0x04, 0x04, 0x07, 0x09, 0x08, 0x0c, 0x02,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x03,
0x04, 0x01, 0x02, 0x04, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x01, 0x02, 0x02, 0x02, 0x06, 0x4b,
0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x27, 0x00, 0x00, 0x23, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x01, 0x06, 0x0e, 0x10, 0x04,
0x06, 0x08, 0x05, 0x04, 0x04, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01,
0x01, 0x04, 0x02, 0x04, 0x02, 0x02, 0x02, 0x01, 0x01, 0x04, 0x01, 0x01, 0x02, 0x03, 0x03, 0x02,
0x03, 0x01, 0x03, 0x06, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x01, 0x01,
0x01, 0x29, 0x07, 0x16, 0x12, 0x40, 0x0a, 0x0a, 0x11, 0x25, 0x01, 0x03, 0x17, 0x10, 0x26, 0x2a,
0x10, 0x01, 0x23, 0x23, 0x2f, 0x10, 0x06, 0x07, 0x02, 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 },
{ 0xff, 0x0b, 0x07, 0x05, 0x0b, 0x02, 0x02, 0x02, 0x06, 0x02, 0x02, 0x01, 0x04, 0x02, 0x01, 0x03,
0x09, 0x01, 0x01, 0x01, 0x03, 0x04, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
0x05, 0x01, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01,
0x0a, 0x04, 0x02, 0x01, 0x06, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01,
0x05, 0x02, 0x03, 0x04, 0x03, 0x03, 0x03, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x03, 0x03,
0x01, 0x03, 0x01, 0x01, 0x02, 0x05, 0x01, 0x01, 0x04, 0x03, 0x05, 0x01, 0x03, 0x01, 0x03, 0x03,
0x02, 0x01, 0x04, 0x03, 0x0a, 0x06, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x02, 0x01, 0x0a, 0x02, 0x05, 0x01, 0x01, 0x02, 0x07, 0x02, 0x17, 0x01, 0x05, 0x01, 0x01,
0x0e, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x06, 0x02, 0x01, 0x04, 0x05, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x07, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x11,
0x00, 0x00 },
{ 0xff, 0xfb, 0x98, 0x9a, 0x84, 0x85, 0x63, 0x64, 0x3e, 0x3e, 0x22, 0x22, 0x13, 0x13, 0x18, 0x17,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 },
{ 0xff, 0xf1, 0x9d, 0x9e, 0x9a, 0x9b, 0x9a, 0x97, 0x93, 0x93, 0x8c, 0x8e, 0x86, 0x88, 0x80, 0x82,
0x7c, 0x7c, 0x72, 0x73, 0x69, 0x6b, 0x5f, 0x60, 0x55, 0x56, 0x4a, 0x4b, 0x40, 0x41, 0x37, 0x37,
0x2f, 0x2f, 0x27, 0x27, 0x21, 0x21, 0x1b, 0x1c, 0x17, 0x17, 0x13, 0x13, 0x10, 0x10, 0x0d, 0x0d,
0x0b, 0x0b, 0x09, 0x09, 0x08, 0x08, 0x07, 0x07, 0x06, 0x05, 0x05, 0x04, 0x04, 0x04, 0x19, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 },
{ 0xc3, 0xcb, 0xf5, 0x41, 0xff, 0x7b, 0xf7, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xbf, 0xcc, 0xf2, 0x40, 0xfd, 0x7c, 0xf7, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7a, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 },
{ 0xc3, 0xd9, 0xef, 0x3d, 0xf9, 0x7c, 0xe9, 0x1e, 0xfd, 0xab, 0xf1, 0x2c, 0xfc, 0x5b, 0xfe, 0x17,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xbd, 0xd9, 0xec, 0x3d, 0xf5, 0x7d, 0xe8, 0x1d, 0xfb, 0xae, 0xf0, 0x2c, 0xfb, 0x5c, 0xff, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x70, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 },
{ 0xba, 0xc5, 0xda, 0x33, 0xe3, 0x6d, 0xd8, 0x18, 0xe5, 0x94, 0xda, 0x23, 0xdf, 0x4a, 0xd1, 0x10,
0xee, 0xaf, 0xe4, 0x2c, 0xea, 0x5a, 0xde, 0x15, 0xf4, 0x87, 0xe9, 0x21, 0xf6, 0x43, 0xfc, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb0, 0xc7, 0xd8, 0x33, 0xe3, 0x6b, 0xd6, 0x18, 0xe7, 0x95, 0xd8, 0x23, 0xdb, 0x49, 0xd0, 0x11,
0xe9, 0xb2, 0xe2, 0x2b, 0xe8, 0x5c, 0xdd, 0x15, 0xf1, 0x87, 0xe7, 0x20, 0xf7, 0x44, 0xff, 0x13,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x5f, 0x9e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00 }
};
template<bool little_endian = true>
size_t decompress_huffman(uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) {
data_reader<little_endian> source_r(input, input + input_size);
auto r = make_bit_reader(source_r);
size_t weights_index = r.template get<uint8_t>();
if (weights_index >= 9) error("decompress_huffman: invalid weights index %d", weights_index);
const uint8_t* weights = huffman_weight_tables[weights_index];
struct tree_node;
using tree_node_iterator = typename a_list<tree_node>::iterator;
struct tree_node {
tree_node_iterator left;
tree_node_iterator parent;
int weight;
int symbol;
};
a_list<tree_node> all_nodes;
auto end = all_nodes.end();
all_nodes.push_back({end, end, 1, 0x101});
all_nodes.push_back({end, end, 1, 0x100});
for (int i = 256; i != 0;) {
--i;
int w = weights[i];
if (w == 0) continue;
all_nodes.push_back({end, end, w, i});
}
all_nodes.sort([&](auto& a, auto& b) {
return a.weight < b.weight;
});
for (auto a = all_nodes.begin(); a != end;) {
auto b = std::next(a);
if (b == end) break;
int w = a->weight + b->weight;
auto it = std::find_if(all_nodes.begin(), all_nodes.end(), [&](auto& v) {
return v.weight >= w;
});
it = all_nodes.insert(it, {a, end, w, -1});
a->parent = it;
b->parent = it;
a = std::next(b);
}
auto increment_weight = [&](tree_node_iterator n) {
for (; n != end; n = n->parent) {
++n->weight;
int w = n->weight;
auto swap_n_next = std::find_if(std::next(n), end, [&](auto& v) {
return v.weight >= w;
});
auto swap_n = std::prev(swap_n_next);
if (swap_n == n) continue;
all_nodes.splice(n, all_nodes, swap_n);
all_nodes.splice(swap_n_next, all_nodes, n);
if (n->parent->left == n) {
if (swap_n->parent->left == swap_n) swap_n->parent->left = n;
n->parent->left = swap_n;
} else if (swap_n->parent->left == swap_n) swap_n->parent->left = n;
std::swap(n->parent, swap_n->parent);
}
};
size_t out_pos = 0;
while (out_pos < output_size) {
tree_node_iterator n = std::prev(end);
while (n->symbol == -1) {
int bit = r.template get_bits<1>();
if (bit == 0) n = n->left;
else n = std::next(n->left);
}
if (n->symbol == 256) break;
uint8_t value;
if (n->symbol == 257) {
int symbol = r.template get_bits<8>();
value = symbol;
n = all_nodes.begin();
int n_symbol = n->symbol;
n->symbol = -1;
all_nodes.push_front({end, n, 1, n_symbol});
all_nodes.push_front({end, n, 0, symbol});
n->left = all_nodes.begin();
n = n->left;
increment_weight(n);
if (weights_index != 0) increment_weight(n);
} else value = n->symbol;
output[out_pos] = value;
++out_pos;
if (weights_index == 0) increment_weight(n);
}
return out_pos;
}
static const int adpcm_index_add_table[32] = {
-1, 0, -1, 4, -1, 2, -1, 6,
-1, 1, -1, 5, -1, 3, -1, 7,
-1, 1, -1, 5, -1, 3, -1, 7,
-1, 2, -1, 4, -1, 6, -1, 8
};
static const int32_t adpcm_step_table[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
template<bool little_endian = true>
size_t decompress_adpcm(uint8_t* input, size_t input_size, uint8_t* output, size_t output_size, size_t channels) {
data_reader<little_endian> r(input, input + input_size);
size_t out_pos = 0;
a_vector<int16_t> previous_sample(channels);
a_vector<int> step_index(channels, 44);
r.template get<uint8_t>();
auto shift = r.template get<uint8_t>();
for (size_t i = 0; i != channels; ++i) {
auto sample = r.template get<int16_t>();
previous_sample[i] = sample;
if (out_pos - output_size < 2) error("decompress_adpcm: attempt to write past end");
set_value_at<little_endian, int16_t>(output + out_pos, sample);
out_pos += 2;
}
while (r.left()) {
for (size_t channel = 0; channel != channels; ++channel) {
auto in_value = r.template get<uint8_t>();
if (~in_value & 0x80) {
int index = step_index[channel];
int32_t step = adpcm_step_table[index];
int32_t sample = step >> shift;
if (in_value & 1) sample += step;
if (in_value & 2) sample += step >> 1;
if (in_value & 4) sample += step >> 2;
if (in_value & 8) sample += step >> 3;
if (in_value & 0x10) sample += step >> 4;
if (in_value & 0x20) sample += step >> 5;
if (in_value & 0x40) {
sample = previous_sample[channel] - sample;
if (sample < -32768) sample = -32768;
} else {
sample = previous_sample[channel] + sample;
if (sample > 32767) sample = 32767;
}
previous_sample[channel] = sample;
if (out_pos - output_size < 2) error("decompress_adpcm: attempt to write past end");
set_value_at<true, int16_t>(output + out_pos, sample);
out_pos += 2;
index += adpcm_index_add_table[in_value & 0x1f];
if (index < 0) index = 0;
else if (index > 88) index = 88;