summaryrefslogtreecommitdiff
path: root/jbig2dec/jbig2_huffman.c
blob: e265ceed96ffd618883e5c97b1bfaea80b14d735 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/*
    jbig2dec
*/

/* Huffman table decoding procedures
    -- See Annex B of the JBIG2 specification */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os_types.h"

#include <stdlib.h>
#include <string.h>

#ifdef JBIG2_DEBUG
#include <stdio.h>
#endif

#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_huffman.h"
#include "jbig2_hufftab.h"
#include "jbig2_image.h"
#include "jbig2_segment.h"

#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
#define JBIG2_HUFFMAN_FLAGS_ISEXT 4

struct _Jbig2HuffmanState {
    /* The current bit offset is equal to (offset * 8) + offset_bits.
       The MSB of this_word is the current bit offset. The MSB of next_word
       is (offset + 4) * 8. */
    uint32_t this_word;
    uint32_t next_word;
    uint32_t offset_bits;
    uint32_t offset;
    uint32_t offset_limit;

    Jbig2WordStream *ws;
    Jbig2Ctx *ctx;
};

#define huff_get_next_word(hs, offset, word) \
    (hs)->ws->get_next_word((hs)->ctx, (hs)->ws, (offset), (word))

/** Allocate and initialize a new huffman coding state
 *  the returned pointer can simply be freed; this does
 *  not affect the associated Jbig2WordStream.
 */
Jbig2HuffmanState *
jbig2_huffman_new(Jbig2Ctx *ctx, Jbig2WordStream *ws)
{
    Jbig2HuffmanState *result = NULL;
    int code;

    result = jbig2_new(ctx, Jbig2HuffmanState, 1);

    if (result != NULL) {
        result->offset = 0;
        result->offset_bits = 0;
        result->offset_limit = 0;
        result->ws = ws;
        result->ctx = ctx;
        code = huff_get_next_word(result, 0, &result->this_word);
        if (code < 0) {
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read first huffman word");
            jbig2_huffman_free(ctx, result);
            return NULL;
        }
        code = huff_get_next_word(result, 4, &result->next_word);
        if (code < 0) {
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read second huffman word");
            jbig2_huffman_free(ctx, result);
            return NULL;
        }
    } else {
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate new huffman coding state");
        return NULL;
    }

    return result;
}

/** Free an allocated huffman coding state.
 *  This just calls jbig2_free() if the pointer is not NULL
 */
void
jbig2_huffman_free(Jbig2Ctx *ctx, Jbig2HuffmanState *hs)
{
    jbig2_free(ctx->allocator, hs);
}

/** debug routines **/
#ifdef JBIG2_DEBUG

/** print current huffman state */
void
jbig2_dump_huffman_state(Jbig2HuffmanState *hs)
{
    fprintf(stderr, "huffman state %08x %08x offset %d.%d\n", hs->this_word, hs->next_word, hs->offset, hs->offset_bits);
}

/** print the binary string we're reading from */
void
jbig2_dump_huffman_binary(Jbig2HuffmanState *hs)
{
    const uint32_t word = hs->this_word;
    int i;

    fprintf(stderr, "huffman binary ");
    for (i = 31; i >= 0; i--)
        fprintf(stderr, ((word >> i) & 1) ? "1" : "0");
    fprintf(stderr, "\n");
}

/** print huffman table */
void
jbig2_dump_huffman_table(const Jbig2HuffmanTable *table)
{
    int i;
    int table_size = (1 << table->log_table_size);

    fprintf(stderr, "huffman table %p (log_table_size=%d, %d entries, entries=%p):\n", table, table->log_table_size, table_size, table->entries);
    for (i = 0; i < table_size; i++) {
        fprintf(stderr, "%6d: PREFLEN=%d, RANGELEN=%d, ", i, table->entries[i].PREFLEN, table->entries[i].RANGELEN);
        if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
            fprintf(stderr, "ext=%p", table->entries[i].u.ext_table);
        } else {
            fprintf(stderr, "RANGELOW=%d", table->entries[i].u.RANGELOW);
        }
        if (table->entries[i].flags) {
            int need_comma = 0;

            fprintf(stderr, ", flags=0x%x(", table->entries[i].flags);
            if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISOOB) {
                fprintf(stderr, "OOB");
                need_comma = 1;
            }
            if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISLOW) {
                if (need_comma)
                    fprintf(stderr, ",");
                fprintf(stderr, "LOW");
                need_comma = 1;
            }
            if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
                if (need_comma)
                    fprintf(stderr, ",");
                fprintf(stderr, "EXT");
            }
            fprintf(stderr, ")");
        }
        fprintf(stderr, "\n");
    }
    fprintf(stderr, "\n");
}

#endif /* JBIG2_DEBUG */

/** Skip bits up to the next byte boundary
 */
int
jbig2_huffman_skip(Jbig2HuffmanState *hs)
{
    uint32_t bits = hs->offset_bits & 7;
    int code;

    if (bits) {
        bits = 8 - bits;
        hs->offset_bits += bits;
        hs->this_word = (hs->this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
    }

    if (hs->offset_bits >= 32) {
        hs->this_word = hs->next_word;
        hs->offset += 4;
        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
        if (code < 0) {
            return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read next huffman word when skipping");
        }
        hs->offset_bits -= 32;
        if (hs->offset_bits) {
            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
        }
    }
    return 0;
}

/* skip ahead a specified number of bytes in the word stream
 */
int
jbig2_huffman_advance(Jbig2HuffmanState *hs, size_t advance)
{
    int code;
    hs->offset += advance & ~3;
    hs->offset_bits += (advance & 3) << 3;
    if (hs->offset_bits >= 32) {
        hs->offset += 4;
        hs->offset_bits -= 32;
    }
    code = huff_get_next_word(hs, hs->offset, &hs->this_word);
    if (code < 0) {
        return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get first huffman word after advancing");
    }
    code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
    if (code < 0) {
        return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get second huffman word after advancing");
    }
    if (hs->offset_bits > 0)
        hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
    return 0;
}

/* return the offset of the huffman decode pointer (in bytes)
 * from the beginning of the WordStream
 */
uint32_t
jbig2_huffman_offset(Jbig2HuffmanState *hs)
{
    return hs->offset + (hs->offset_bits >> 3);
}

/* read a number of bits directly from the huffman state
 * without decoding against a table
 */
int32_t
jbig2_huffman_get_bits(Jbig2HuffmanState *hs, const int bits, int *err)
{
    uint32_t this_word = hs->this_word;
    int32_t result;
    int code;

    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
        *err = -1;
        return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of jbig2 buffer reached at offset %d", hs->offset);
    }

    result = this_word >> (32 - bits);
    hs->offset_bits += bits;
    if (hs->offset_bits >= 32) {
        hs->offset += 4;
        hs->offset_bits -= 32;
        hs->this_word = hs->next_word;
        code = huff_get_next_word(hs, hs->offset + 4, &hs->next_word);
        if (code < 0) {
            return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
        }
        if (hs->offset_bits) {
            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
        } else {
            hs->this_word = (hs->this_word << hs->offset_bits);
        }
    } else {
        hs->this_word = (this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
    }

    return result;
}

int32_t
jbig2_huffman_get(Jbig2HuffmanState *hs, const Jbig2HuffmanTable *table, bool *oob)
{
    Jbig2HuffmanEntry *entry;
    byte flags;
    int offset_bits = hs->offset_bits;
    uint32_t this_word = hs->this_word;
    uint32_t next_word;
    int RANGELEN;
    int32_t result;

    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
        if (oob)
            *oob = -1;
        return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "end of Jbig2WordStream reached at offset %d", hs->offset);
    }

    for (;;) {
        int log_table_size = table->log_table_size;
        int PREFLEN;
        int code;

        /* SumatraPDF: shifting by the size of the operand is undefined */
        entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0];
        flags = entry->flags;
        PREFLEN = entry->PREFLEN;
        if (flags == (byte) -1 || PREFLEN == (byte) -1) {
            if (oob)
                *oob = -1;
            return jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "encountered unpopulated huffman table entry");
        }

        next_word = hs->next_word;
        offset_bits += PREFLEN;
        if (offset_bits >= 32) {
            this_word = next_word;
            hs->offset += 4;
            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
            if (code < 0) {
                return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
            }
            offset_bits -= 32;
            hs->next_word = next_word;
            PREFLEN = offset_bits;
        }
        if (PREFLEN)
            this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits));
        if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
            table = entry->u.ext_table;
        } else
            break;
    }
    result = entry->u.RANGELOW;
    RANGELEN = entry->RANGELEN;
    if (RANGELEN > 0) {
        int32_t HTOFFSET;
        int code;

        HTOFFSET = this_word >> (32 - RANGELEN);
        if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW)
            result -= HTOFFSET;
        else
            result += HTOFFSET;

        offset_bits += RANGELEN;
        if (offset_bits >= 32) {
            this_word = next_word;
            hs->offset += 4;
            code = huff_get_next_word(hs, hs->offset + 4, &next_word);
            if (code < 0) {
                return jbig2_error(hs->ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to get next huffman word");
            }
            offset_bits -= 32;
            hs->next_word = next_word;
            RANGELEN = offset_bits;
        }
        if (RANGELEN)
            this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits));
    }

    hs->this_word = this_word;
    hs->offset_bits = offset_bits;

    if (oob != NULL)
        *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);

    return result;
}

/* TODO: more than 8 bits here is wasteful of memory. We have support
   for sub-trees in jbig2_huffman_get() above, but don't use it here.
   We should, and then revert to 8 bits */
#define LOG_TABLE_SIZE_MAX 16

/** Build an in-memory representation of a Huffman table from the
 *  set of template params provided by the spec or a table segment
 */
Jbig2HuffmanTable *
jbig2_build_huffman_table(Jbig2Ctx *ctx, const Jbig2HuffmanParams *params)
{
    int *LENCOUNT;
    int LENMAX = -1;
    const int lencountcount = 256;
    const Jbig2HuffmanLine *lines = params->lines;
    int n_lines = params->n_lines;
    int i, j;
    uint32_t max_j;
    int log_table_size = 0;
    Jbig2HuffmanTable *result;
    Jbig2HuffmanEntry *entries;
    int CURLEN;
    int firstcode = 0;
    int CURCODE;
    int CURTEMP;

    LENCOUNT = jbig2_new(ctx, int, lencountcount);

    if (LENCOUNT == NULL) {
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate huffman histogram");
        return NULL;
    }
    memset(LENCOUNT, 0, sizeof(int) * lencountcount);

    /* B.3, 1. */
    for (i = 0; i < params->n_lines; i++) {
        int PREFLEN = lines[i].PREFLEN;
        int lts;

        if (PREFLEN > LENMAX) {
            for (j = LENMAX + 1; j < PREFLEN + 1; j++)
                LENCOUNT[j] = 0;
            LENMAX = PREFLEN;
        }
        LENCOUNT[PREFLEN]++;

        lts = PREFLEN + lines[i].RANGELEN;
        if (lts > LOG_TABLE_SIZE_MAX)
            lts = PREFLEN;
        if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts)
            log_table_size = lts;
    }
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "constructing huffman table log size %d", log_table_size);
    max_j = 1 << log_table_size;

    result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
    if (result == NULL) {
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate result");
        jbig2_free(ctx->allocator, LENCOUNT);
        return NULL;
    }
    result->log_table_size = log_table_size;
    entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
    if (entries == NULL) {
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate result entries");
        jbig2_free(ctx->allocator, result);
        jbig2_free(ctx->allocator, LENCOUNT);
        return NULL;
    }
    /* fill now to catch missing JBIG2Globals later */
    memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j);
    result->entries = entries;

    LENCOUNT[0] = 0;

    for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) {
        int shift = log_table_size - CURLEN;

        /* B.3 3.(a) */
        firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1;
        CURCODE = firstcode;
        /* B.3 3.(b) */
        for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) {
            int PREFLEN = lines[CURTEMP].PREFLEN;

            if (PREFLEN == CURLEN) {
                int RANGELEN = lines[CURTEMP].RANGELEN;
                uint32_t start_j = CURCODE << shift;
                uint32_t end_j = (CURCODE + 1) << shift;
                uint32_t cur_j;
                byte eflags = 0;

                if (end_j > max_j) {
                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "ran off the end of the entries table! (%d >= %d)", end_j, max_j);
                    jbig2_free(ctx->allocator, result->entries);
                    jbig2_free(ctx->allocator, result);
                    jbig2_free(ctx->allocator, LENCOUNT);
                    return NULL;
                }
                /* todo: build extension tables */
                if (params->HTOOB && CURTEMP == n_lines - 1)
                    eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
                if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
                    eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
                if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
                        entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW;
                        entries[cur_j].PREFLEN = PREFLEN;
                        entries[cur_j].RANGELEN = RANGELEN;
                        entries[cur_j].flags = eflags;
                    }
                } else {
                    for (cur_j = start_j; cur_j < end_j; cur_j++) {
                        int32_t HTOFFSET = (cur_j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1);

                        if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW)
                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET;
                        else
                            entries[cur_j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET;
                        entries[cur_j].PREFLEN = PREFLEN + RANGELEN;
                        entries[cur_j].RANGELEN = 0;
                        entries[cur_j].flags = eflags;
                    }
                }
                CURCODE++;
            }
        }
    }

    jbig2_free(ctx->allocator, LENCOUNT);

    return result;
}

/** Free the memory associated with the representation of table */
void
jbig2_release_huffman_table(Jbig2Ctx *ctx, Jbig2HuffmanTable *table)
{
    if (table != NULL) {
        jbig2_free(ctx->allocator, table->entries);
        jbig2_free(ctx->allocator, table);
    }
}

/* Routines to handle "code table segment (53)" */

/* return 'bitlen' bits from 'bitoffset' of 'data' */
static uint32_t
jbig2_table_read_bits(const byte *data, size_t *bitoffset, const int bitlen)
{
    uint32_t result = 0;
    uint32_t byte_offset = *bitoffset / 8;
    const int endbit = (*bitoffset & 7) + bitlen;
    const int n_proc_bytes = (endbit + 7) / 8;
    const int rshift = n_proc_bytes * 8 - endbit;
    int i;

    for (i = n_proc_bytes - 1; i >= 0; i--) {
        uint32_t d = data[byte_offset++];
        const int nshift = i * 8 - rshift;

        if (nshift > 0)
            d <<= nshift;
        else if (nshift < 0)
            d >>= -nshift;
        result |= d;
    }
    result &= ~(-1 << bitlen);
    *bitoffset += bitlen;
    return result;
}

/* Parse a code table segment, store Jbig2HuffmanParams in segment->result */
int
jbig2_table(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
{
    Jbig2HuffmanParams *params = NULL;
    Jbig2HuffmanLine *line = NULL;

    segment->result = NULL;
    if (segment->data_length < 10)
        goto too_short;

    {
        /* B.2 1) (B.2.1) Code table flags */
        const int code_table_flags = segment_data[0];
        const int HTOOB = code_table_flags & 0x01;      /* Bit 0: HTOOB */

        /* Bits 1-3: Number of bits used in code table line prefix size fields */
        const int HTPS = (code_table_flags >> 1 & 0x07) + 1;

        /* Bits 4-6: Number of bits used in code table line range size fields */
        const int HTRS = (code_table_flags >> 4 & 0x07) + 1;

        /* B.2 2) (B.2.2) The lower bound of the first table line in the encoded table */
        const int32_t HTLOW = jbig2_get_int32(segment_data + 1);

        /* B.2 3) (B.2.3) One larger than the upper bound of
           the last normal table line in the encoded table */
        const int32_t HTHIGH = jbig2_get_int32(segment_data + 5);

        /* estimated number of lines in this table, used for allocating memory for lines */
        const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2);

        /* points to a first table line data */
        const byte *lines_data = segment_data + 9;
        const size_t lines_data_bitlen = (segment->data_length - 9) * 8;        /* length in bit */

        /* bit offset: controls bit reading */
        size_t boffset = 0;

        /* B.2 4) */
        int32_t CURRANGELOW = HTLOW;
        size_t NTEMP = 0;

#ifdef JBIG2_DEBUG
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
                    "DECODING USER TABLE... Flags: %d, HTOOB: %d, HTPS: %d, HTRS: %d, HTLOW: %d, HTHIGH: %d",
                    code_table_flags, HTOOB, HTPS, HTRS, HTLOW, HTHIGH);
#endif

        if (HTLOW >= HTHIGH) {
            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid Huffman Table range");
            goto error_exit;
        }

        /* allocate HuffmanParams & HuffmanLine */
        params = jbig2_new(ctx, Jbig2HuffmanParams, 1);
        if (params == NULL) {
            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate Huffman Table Parameter");
            goto error_exit;
        }
        line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max);
        if (line == NULL) {
            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate huffman table lines");
            goto error_exit;
        }
        /* B.2 5) */
        while (CURRANGELOW < HTHIGH) {
            /* B.2 5) a) */
            if (boffset + HTPS >= lines_data_bitlen)
                goto too_short;
            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
            /* B.2 5) b) */
            if (boffset + HTRS >= lines_data_bitlen)
                goto too_short;
            line[NTEMP].RANGELEN = jbig2_table_read_bits(lines_data, &boffset, HTRS);
            /* B.2 5) c) */
            line[NTEMP].RANGELOW = CURRANGELOW;
            CURRANGELOW += (1 << line[NTEMP].RANGELEN);
            NTEMP++;
        }
        /* B.2 6), B.2 7) lower range table line */
        if (boffset + HTPS >= lines_data_bitlen)
            goto too_short;
        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
        line[NTEMP].RANGELEN = 32;
        line[NTEMP].RANGELOW = HTLOW - 1;
        NTEMP++;
        /* B.2 8), B.2 9) upper range table line */
        if (boffset + HTPS >= lines_data_bitlen)
            goto too_short;
        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
        line[NTEMP].RANGELEN = 32;
        line[NTEMP].RANGELOW = HTHIGH;
        NTEMP++;
        /* B.2 10) */
        if (HTOOB) {
            /* B.2 10) a), B.2 10) b) out-of-bound table line */
            if (boffset + HTPS >= lines_data_bitlen)
                goto too_short;
            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
            line[NTEMP].RANGELEN = 0;
            line[NTEMP].RANGELOW = 0;
            NTEMP++;
        }
        if (NTEMP != lines_max) {
            Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line,
                                                     Jbig2HuffmanLine, NTEMP);

            if (new_line == NULL) {
                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to reallocate huffman table lines");
                goto error_exit;
            }
            line = new_line;
        }
        params->HTOOB = HTOOB;
        params->n_lines = NTEMP;
        params->lines = line;
        segment->result = params;

#ifdef JBIG2_DEBUG
        {
            int i;

            for (i = 0; i < NTEMP; i++) {
                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
                            "Line: %d, PREFLEN: %d, RANGELEN: %d, RANGELOW: %d",
                            i, params->lines[i].PREFLEN, params->lines[i].RANGELEN, params->lines[i].RANGELOW);
            }
        }
#endif
    }
    return 0;

too_short:
    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
error_exit:
    jbig2_free(ctx->allocator, line);
    jbig2_free(ctx->allocator, params);
    return -1;
}

/* free Jbig2HuffmanParams allocated by jbig2_huffman_table() */
void
jbig2_table_free(Jbig2Ctx *ctx, Jbig2HuffmanParams *params)
{
    if (params != NULL) {
        jbig2_free(ctx->allocator, (void *)params->lines);
        jbig2_free(ctx->allocator, params);
    }
}

/* find a user supplied table used by 'segment' and by 'index' */
const Jbig2HuffmanParams *
jbig2_find_table(Jbig2Ctx *ctx, Jbig2Segment *segment, int index)
{
    int i, table_index = 0;

    for (i = 0; i < segment->referred_to_segment_count; i++) {
        const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]);

        if (rsegment && (rsegment->flags & 63) == 53) {
            if (table_index == index)
                return (const Jbig2HuffmanParams *)rsegment->result;
            ++table_index;
        }
    }

    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "huffman table not found (%d)", index);
    return NULL;
}

#ifdef TEST
#include <stdio.h>

/* cc -g -o jbig2_huffman.test1 -DTEST jbig2_huffman.c .libs/libjbig2dec.a */

/* a test bitstream, and a list of the table indices
   to use in decoding it. 1 = table B.1 (A), 2 = table B.2 (B), and so on */
/* this test stream should decode to { 8, 5, oob, 8 } */

static const byte test_stream[] = { 0xe9, 0xcb, 0xf4, 0x00 };
static const byte test_tabindex[] = { 4, 2, 2, 1 };

static int
test_get_word1(Jbig2Ctx *ctx, Jbig2WordStream *self, size_t offset, uint32_t *word)
{
    uint32_t val = 0;
    int ret = 0;

    if (self == NULL || word == NULL)
        return -1;
    if (offset >= sizeof (test_stream))
        return 0;

    if (offset < sizeof(test_stream)) {
        val |= test_stream[offset] << 24;
        ret++;
    }
    if (offset + 1 < sizeof(test_stream)) {
        val |= test_stream[offset + 1] << 16;
        ret++;
    }
    if (offset + 2 < sizeof(test_stream)) {
        val |= test_stream[offset + 2] << 8;
        ret++;
    }
    if (offset + 3 < sizeof(test_stream)) {
        val |= test_stream[offset + 3];
        ret++;
    }
    *word = val;
    return ret;
}

static int test1()
{
    Jbig2Ctx *ctx;
    Jbig2HuffmanTable *tables[5];
    Jbig2HuffmanState *hs = NULL;
    Jbig2WordStream ws;
    bool oob;
    int32_t code;
    int i;
    int success = 0;

    ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
    if (ctx == NULL) {
        fprintf(stderr, "Failed to allocate jbig2 context\n");
        goto cleanup;
    }

    tables[0] = NULL;
    tables[1] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
    tables[2] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
    tables[3] = NULL;
    tables[4] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
    if (tables[1] == NULL || tables[2] == NULL || tables[4] == NULL)
    {
        fprintf(stderr, "Failed to build huffman tables");
        goto cleanup;
    }

    ws.get_next_word = test_get_word1;
    hs = jbig2_huffman_new(ctx, &ws);
    if (hs == NULL) {
        fprintf(stderr, "Failed to allocate huffman state");
        goto cleanup;
    }

    printf("testing jbig2 huffman decoding...");
    printf("\t(should be 8 5 (oob) 8)\n");

    {
        int i;
        int sequence_length = sizeof(test_tabindex);

        for (i = 0; i < sequence_length; i++) {
            code = jbig2_huffman_get(hs, tables[test_tabindex[i]], &oob);
            if (oob)
                printf("(oob) ");
            else
                printf("%d ", code);
        }
    }

    printf("\n");

    success = 1;

cleanup:
    jbig2_huffman_free(ctx, hs);
    for (i = 0; i < 5; i++)
        jbig2_release_huffman_table(ctx, tables[i]);
    jbig2_ctx_free(ctx);

    return success;
}

#include <stdio.h>

/* a decoding test of each line from each standard table */

/* test code for Table B.1 - Standard Huffman table A */
const int32_t test_output_A[] = {
    /* line 0, PREFLEN=1, RANGELEN=4, VAL=0..15, 0+VAL */
    0,      /* 0 0000 */
    1,      /* 0 0001 */
    14,     /* 0 1110 */
    15,     /* 0 1111 */
    /* line 1, PREFLEN=2, RANGELEN=8, VAL=16..271, 10+(VAL-16) */
    16,     /* 10 00000000 */
    17,     /* 10 00000001 */
    270,    /* 10 11111110 */
    271,    /* 10 11111111 */
    /* line 2, PREFLEN=3, RANGELEN=16, VAL=272..65807, 110+(VAL-272) */
    272,    /* 110 00000000 00000000 */
    273,    /* 110 00000000 00000001 */
    65806,  /* 110 11111111 11111110 */
    65807,  /* 110 11111111 11111111 */
    /* line 3, PREFLEN=3, RANGELEN=32, VAL=65808..INF, 111+(VAL-65808) */
    65808,  /* 111 00000000 00000000 00000000 00000000 */
    65809,  /* 111 00000000 00000000 00000000 00000001 */
};
const byte test_input_A[] = {
    /* 0000 0000 0101 1100 1111 1000 0000 0010 */
       0x00,     0x5c,     0xf8,     0x02,
    /* 0000 0001 1011 1111 1010 1111 1111 1100 */
       0x01,     0xbf,     0xaf,     0xfc,
    /* 0000 0000 0000 0001 1000 0000 0000 0000 */
       0x00,     0x01,     0x80,     0x00,
    /* 0111 0111 1111 1111 1111 0110 1111 1111 */
       0x77,     0xff,     0xf6,     0xff,
    /* 1111 1111 1110 0000 0000 0000 0000 0000 */
       0xff,     0xe0,     0x00,     0x00,
    /* 0000 0000 0001 1100 0000 0000 0000 0000 */
       0x00,     0x1c,     0x00,     0x00,
    /* 0000 0000 0000 01 */
       0x00,     0x04,
};

/* test code for Table B.2 - Standard Huffman table B */
const int32_t test_output_B[] = {
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=0, 0 */
    0,      /* 0 */
    /* line 1, PREFLEN=2, RANGELEN=0, VAL=1, 10 */
    1,      /* 10 */
    /* line 2, PREFLEN=3, RANGELEN=0, VAL=2, 110 */
    2,      /* 110 */
    /* line 3, PREFLEN=4, RANGELEN=3, VAL=3..10, 1110+(VAL-3) */
    3,      /* 1110 000 */
    4,      /* 1110 001 */
    9,      /* 1110 110 */
    10,     /* 1110 111 */
    /* line 4, PREFLEN=5, RANGELEN=6, VAL=11..74, 11110+(VAL-11) */
    11,     /* 11110 000000 */
    12,     /* 11110 000001 */
    73,     /* 11110 111110 */
    74,     /* 11110 111111 */
    /* line 5, PREFLEN=6, RANGELEN=32, VAL=75..INF, 111110+(VAL-75) */
    75,     /* 111110 00000000 00000000 00000000 00000000 */
    76,     /* 111110 00000000 00000000 00000000 00000001 */
    /* line 6, PREFLEN=6, VAL=OOB, 111111 */
    /*OOB*/ /* 111111 */
};
const byte test_input_B[] = {
    /* 0101 1011 1000 0111 0001 1110 1101 1101 */
       0x5b,     0x87,     0x1e,     0xdd,
    /* 1111 1100 0000 0111 1000 0001 1111 0111 */
       0xfc,     0x07,     0x81,     0xf7,
    /* 1101 1110 1111 1111 1110 0000 0000 0000 */
       0xde,     0xff,     0xe0,     0x00,
    /* 0000 0000 0000 0000 0000 1111 1000 0000 */
       0x00,     0x00,     0x0f,     0x80,
    /* 0000 0000 0000 0000 0000 0000 0111 1111 */
       0x00,     0x00,     0x00,     0x7f,
};

/* test code for Table B.3 - Standard Huffman table C */
const int32_t test_output_C[] = {
    /* line 0, PREFLEN=8, RANGELEN=8, VAL=-256..-1, 11111110+(VAL+256) */
    -256,   /* 11111110 00000000 */
    -255,   /* 11111110 00000001 */
    -2,     /* 11111110 11111110 */
    -1,     /* 11111110 11111111 */
    /* line 1, PREFLEN=1, RANGELEN=0, VAL=0, 0 */
    0,      /* 0 */
    /* line 2, PREFLEN=2, RANGELEN=0, VAL=1, 10 */
    1,      /* 10 */
    /* line 3, PREFLEN=3, RANGELEN=0, VAL=2, 110 */
    2,      /* 110 */
    /* line 4, PREFLEN=4, RANGELEN=3, VAL=3..10, 1110+(VAL-3) */
    3,      /* 1110 000 */
    4,      /* 1110 001 */
    9,      /* 1110 110 */
    10,     /* 1110 111 */
    /* line 5, PREFLEN=5, RANGELEN=6, VAL=11..74, 11110+(VAL-11) */
    11,     /* 11110 000000 */
    12,     /* 11110 000001 */
    73,     /* 11110 111110 */
    74,     /* 11110 111111 */
    /* line 6, PREFLEN=8, RANGELEN=32, VAL=-INF..-257, 11111111+(-257-VAL) */
    -257,   /* 11111111 00000000 00000000 00000000 00000000 */
    -258,   /* 11111111 00000000 00000000 00000000 00000001 */
    /* line 7, PREFLEN=7, RANGELEN=32, VAL=75..INF, 1111110+(VAL-75) */
    75,     /* 1111110 00000000 00000000 00000000 00000000 */
    76,     /* 1111110 00000000 00000000 00000000 00000001 */
    /* line 8, PREFLEN=6, VAL=OOB, 111110 */
    /*OOB*/ /* 111110 */
};
const byte test_input_C[] = {
    /* 1111 1110 0000 0000 1111 1110 0000 0001 */
       0xfe,     0x00,     0xfe,     0x01,
    /* 1111 1110 1111 1110 1111 1110 1111 1111 */
       0xfe,     0xfe,     0xfe,     0xff,
    /* 0101 1011 1000 0111 0001 1110 1101 1101 */
       0x5b,     0x87,     0x1e,     0xdd,
    /* 1111 1100 0000 0111 1000 0001 1111 0111 */
       0xfc,     0x07,     0x81,     0xf7,
    /* 1101 1110 1111 1111 1111 1100 0000 0000 */
       0xde,     0xff,     0xfc,     0x00,
    /* 0000 0000 0000 0000 0000 0011 1111 1100 */
       0x00,     0x00,     0x03,     0xfc,
    /* 0000 0000 0000 0000 0000 0000 0000 0111 */
       0x00,     0x00,     0x00,     0x07,
    /* 1111 0000 0000 0000 0000 0000 0000 0000 */
       0xf0,     0x00,     0x00,     0x00,
    /* 0000 0111 1110 0000 0000 0000 0000 0000 */
       0x07,     0xe0,     0x00,     0x00,
    /* 0000 0000 0001 1111 10 */
       0x00,     0x1f,     0x80,
};

/* test code for Table B.4 - Standard Huffman table D */
const int32_t test_output_D[] = {
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
    1,      /* 0 */
    /* line 1, PREFLEN=2, RANGELEN=0, VAL=2, 10 */
    2,      /* 10 */
    /* line 2, PREFLEN=3, RANGELEN=0, VAL=3, 110 */
    3,      /* 110 */
    /* line 3, PREFLEN=4, RANGELEN=3, VAL=4..11, 1110+(VAL-4) */
    4,      /* 1110 000 */
    5,      /* 1110 001 */
    10,     /* 1110 110 */
    11,     /* 1110 111 */
    /* line 4, PREFLEN=5, RANGELEN=6, VAL=12..75, 11110+(VAL-12) */
    12,     /* 11110 000000 */
    13,     /* 11110 000001 */
    74,     /* 11110 111110 */
    75,     /* 11110 111111 */
    /* line 5, PREFLEN=5, RANGELEN=32, VAL=76..INF, 11111+(VAL-76) */
    76,     /* 11111 00000000 00000000 00000000 00000000 */
    77,     /* 11111 00000000 00000000 00000000 00000001 */
};
const byte test_input_D[] = {
    /* 0101 1011 1000 0111 0001 1110 1101 1101 */
       0x5b,     0x87,     0x1e,     0xdd,
    /* 1111 1100 0000 0111 1000 0001 1111 0111 */
       0xfc,     0x07,     0x81,     0xf7,
    /* 1101 1110 1111 1111 1110 0000 0000 0000 */
       0xde,     0xff,     0xe0,     0x00,
    /* 0000 0000 0000 0000 0001 1111 0000 0000 */
       0x00,     0x00,     0x1f,     0x00,
    /* 0000 0000 0000 0000 0000 0001 */
       0x00,     0x00,     0x01,
};

/* test code for Table B.5 - Standard Huffman table E */
const int32_t test_output_E[] = {
    /* line 0, PREFLEN=7, RANGELEN=8, VAL=-255..0, 1111110+(VAL+255) */
    -255,   /* 1111110 00000000 */
    -254,   /* 1111110 00000001 */
    -1,     /* 1111110 11111110 */
    0,      /* 1111110 11111111 */
    /* line 1, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
    1,      /* 0 */
    /* line 2, PREFLEN=2, RANGELEN=0, VAL=2, 10 */
    2,      /* 10 */
    /* line 3, PREFLEN=3, RANGELEN=0, VAL=3, 110 */
    3,      /* 110 */
    /* line 4, PREFLEN=4, RANGELEN=3, VAL=4..11, 1110+(VAL-4) */
    4,      /* 1110 000 */
    5,      /* 1110 001 */
    10,     /* 1110 110 */
    11,     /* 1110 111 */
    /* line 5, PREFLEN=5, RANGELEN=6, VAL=12..75, 11110+(VAL-12) */
    12,     /* 11110 000000 */
    13,     /* 11110 000001 */
    74,     /* 11110 111110 */
    75,     /* 11110 111111 */
    /* line 6, PREFLEN=7, RANGELEN=32, VAL=-INF..-256, 1111111+(-256-VAL) */
    -256,   /* 1111111 00000000 00000000 00000000 00000000 */
    -257,   /* 1111111 00000000 00000000 00000000 00000001 */
    /* line 6, PREFLEN=6, RANGELEN=32, VAL=76..INF, 111110+(VAL-76) */
    76,     /* 111110 00000000 00000000 00000000 00000000 */
    77,     /* 111110 00000000 00000000 00000000 00000001 */
};
const byte test_input_E[] = {
    /* 1111 1100 0000 0001 1111 1000 0000 0111 */
       0xfc,     0x01,     0xf8,     0x07,
    /* 1111 0111 1111 0111 1110 1111 1111 0101 */
       0xf7,     0xf7,     0xef,     0xf5,
    /* 1011 1000 0111 0001 1110 1101 1101 1111 */
       0xb8,     0x71,     0xed,     0xdf,
    /* 1100 0000 0111 1000 0001 1111 0111 1101 */
       0xc0,     0x78,     0x1f,     0x7d,
    /* 1110 1111 1111 1111 1000 0000 0000 0000 */
       0xef,     0xff,     0x80,     0x00,
    /* 0000 0000 0000 0000 0111 1111 0000 0000 */
       0x00,     0x00,     0x7f,     0x00,
    /* 0000 0000 0000 0000 0000 0001 1111 1000 */
       0x00,     0x00,     0x01,     0xf8,
    /* 0000 0000 0000 0000 0000 0000 0000 0011 */
       0x00,     0x00,     0x00,     0x03,
    /* 1110 0000 0000 0000 0000 0000 0000 0000 */
       0xe0,     0x00,     0x00,     0x00,
    /* 0001 */
       0x10,
};

/* test code for Table B.6 - Standard Huffman table F */
const int32_t test_output_F[] = {
    /* line 0, PREFLEN=5, RANGELEN=10, VAL=-2048..-1025, 11100+(VAL+2048) */
    -2048,  /* 11100 00000000 00 */
    -2047,  /* 11100 00000000 01 */
    -1026,  /* 11100 11111111 10 */
    -1025,  /* 11100 11111111 11 */
    /* line 1, PREFLEN=4, RANGELEN=9, VAL=-1024..-513, 1000+(VAL+1024) */
    -1024,  /* 1000 00000000 0 */
    -1023,  /* 1000 00000000 1 */
    -514,   /* 1000 11111111 0 */
    -513,   /* 1000 11111111 1 */
    /* line 2, PREFLEN=4, RANGELEN=8, VAL=-512..-257, 1001+(VAL+512) */
    -512,   /* 1001 00000000 */
    -511,   /* 1001 00000001 */
    -258,   /* 1001 11111110 */
    -257,   /* 1001 11111111 */
    /* line 3, PREFLEN=4, RANGELEN=7, VAL=-256..-129, 1010+(VAL+256) */
    -256,   /* 1010 0000000 */
    -255,   /* 1010 0000001 */
    -130,   /* 1010 1111110 */
    -129,   /* 1010 1111111 */
    /* line 4, PREFLEN=5, RANGELEN=6, VAL=-128..-65, 11101+(VAL+128) */
    -128,   /* 11101 000000 */
    -127,   /* 11101 000001 */
    -66,    /* 11101 111110 */
    -65,    /* 11101 111111 */
    /* line 5, PREFLEN=5, RANGELEN=5, VAL=-64..-33, 11110+(VAL+64) */
    -64,    /* 11110 00000 */
    -63,    /* 11110 00001 */
    -34,    /* 11110 11110 */
    -33,    /* 11110 11111 */
    /* line 6, PREFLEN=4, RANGELEN=5, VAL=-32..-1, 1011+(VAL+32) */
    -32,    /* 1011 00000 */
    -31,    /* 1011 00001 */
    -2,     /* 1011 11110 */
    -1,     /* 1011 11111 */
    /* line 7, PREFLEN=2, RANGELEN=7, VAL=0..127, 00+VAL */
    0,      /* 00 0000000 */
    1,      /* 00 0000001 */
    126,    /* 00 1111110 */
    127,    /* 00 1111111 */
    /* line 8, PREFLEN=3, RANGELEN=7, VAL=128..255, 010+(VAL-128) */
    128,    /* 010 0000000 */
    129,    /* 010 0000001 */
    254,    /* 010 1111110 */
    255,    /* 010 1111111 */
    /* line 9, PREFLEN=3, RANGELEN=8, VAL=256..511, 011+(VAL-256) */
    256,    /* 011 00000000 */
    257,    /* 011 00000001 */
    510,    /* 011 11111110 */
    511,    /* 011 11111111 */
    /* line 10, PREFLEN=4, RANGELEN=9, VAL=512..1023, 1100+(VAL-512) */
    512,    /* 1100 00000000 0 */
    513,    /* 1100 00000000 1 */
    1022,   /* 1100 11111111 0 */
    1023,   /* 1100 11111111 1 */
    /* line 11, PREFLEN=4, RANGELEN=10, VAL=1024..2047, 1101+(VAL-1024) */
    1024,   /* 1101 00000000 00 */
    1025,   /* 1101 00000000 01 */
    2046,   /* 1101 11111111 10 */
    2047,   /* 1101 11111111 11 */
    /* line 12, PREFLEN=6, RANGELEN=32, VAL=-INF..-2049, 111110+(-2049-VAL) */
    -2049,  /* 111110 00000000 00000000 00000000 00000000 */
    -2050,  /* 111110 00000000 00000000 00000000 00000001 */
    /* line 13, PREFLEN=6, RANGELEN=32, VAL=2048..INF, 111111+(VAL-2048) */
    2048,   /* 111111 00000000 00000000 00000000 00000000 */
    2049,   /* 111111 00000000 00000000 00000000 00000001 */
};
const byte test_input_F[] = {
    /* 1110 0000 0000 0001 1100 0000 0000 0111 */
       0xe0,     0x01,     0xc0,     0x07,
    /* 1001 1111 1111 0111 0011 1111 1111 1000 */
       0x9f,     0xf7,     0x3f,     0xf8,
    /* 0000 0000 0100 0000 0000 0110 0011 1111 */
       0x00,     0x40,     0x06,     0x3f,
    /* 1101 0001 1111 1111 1001 0000 0000 1001 */
       0xd1,     0xff,     0x90,     0x09,
    /* 0000 0001 1001 1111 1110 1001 1111 1111 */
       0x01,     0x9f,     0xe9,     0xff,
    /* 1010 0000 0001 0100 0000 0110 1011 1111 */
       0xa0,     0x14,     0x06,     0xbf,
    /* 0101 0111 1111 1110 1000 0001 1101 0000 */
       0x57,     0xfe,     0x81,     0xd0,
    /* 0111 1011 1111 0111 0111 1111 1111 0000 */
       0x7b,     0xf7,     0x7f,     0xf0,
    /* 0011 1100 0001 1111 0111 1011 1101 1111 */
       0x3c,     0x1f,     0x7b,     0xdf,
    /* 1011 0000 0101 1000 0110 1111 1101 0111 */
       0xb0,     0x58,     0x6f,     0xd7,
    /* 1111 0000 0000 0000 0000 0100 1111 1100 */
       0xf0,     0x00,     0x04,     0xfc,
    /* 0111 1111 0100 0000 0001 0000 0001 0101 */
       0x7f,     0x40,     0x10,     0x15,
    /* 1111 1001 0111 1111 0110 0000 0000 1100 */
       0xf9,     0x7f,     0x60,     0x0c,
    /* 0000 0101 1111 1111 0011 1111 1111 1100 */
       0x05,     0xff,     0x3f,     0xfc,
    /* 0000 0000 0110 0000 0000 0111 0011 1111 */
       0x00,     0x60,     0x07,     0x3f,
    /* 1101 1001 1111 1111 1101 0000 0000 0011 */
       0xd9,     0xff,     0xd0,     0x03,
    /* 0100 0000 0001 1101 1111 1111 1011 0111 */
       0x40,     0x1d,     0xff,     0xb7,
    /* 1111 1111 1111 1000 0000 0000 0000 0000 */
       0xff,     0xf8,     0x00,     0x00,
    /* 0000 0000 0000 0011 1110 0000 0000 0000 */
       0x00,     0x03,     0xe0,     0x00,
    /* 0000 0000 0000 0000 0001 1111 1100 0000 */
       0x00,     0x00,     0x1f,     0xc0,
    /* 0000 0000 0000 0000 0000 0000 0011 1111 */
       0x00,     0x00,     0x00,     0x3f,
    /* 0000 0000 0000 0000 0000 0000 0000 0001 */
       0x00,     0x00,     0x00,     0x01,
};

/* test code for Table B.7 - Standard Huffman table G */
const int32_t test_output_G[] = {
    /* line 0, PREFLEN=4, RANGELEN=9, VAL=-1024..-513, 1000+(VAL+1024) */
    -1024,  /* 1000 00000000 0 */
    -1023,  /* 1000 00000000 1 */
    -514,   /* 1000 11111111 0 */
    -513,   /* 1000 11111111 1 */
    /* line 1, PREFLEN=3, RANGELEN=8, VAL=-512..-257, 000+(VAL+512) */
    -512,   /* 000 00000000 */
    -511,   /* 000 00000001 */
    -258,   /* 000 11111110 */
    -257,   /* 000 11111111 */
    /* line 2, PREFLEN=4, RANGELEN=7, VAL=-256..-129, 1001+(VAL+256) */
    -256,   /* 1001 0000000 */
    -255,   /* 1001 0000001 */
    -130,   /* 1001 1111110 */
    -129,   /* 1001 1111111 */
    /* line 3, PREFLEN=5, RANGELEN=6, VAL=-128..-65, 11010+(VAL+128) */
    -128,   /* 11010 000000 */
    -127,   /* 11010 000001 */
    -66,    /* 11010 111110 */
    -65,    /* 11010 111111 */
    /* line 4, PREFLEN=5, RANGELEN=5, VAL=-64..-33, 11011+(VAL+64) */
    -64,    /* 11011 00000 */
    -63,    /* 11011 00001 */
    -34,    /* 11011 11110 */
    -33,    /* 11011 11111 */
    /* line 5, PREFLEN=4, RANGELEN=5, VAL=-32..-1, 1010+(VAL+32) */
    -32,    /* 1010 00000 */
    -31,    /* 1010 00001 */
    -2,     /* 1010 11110 */
    -1,     /* 1010 11111 */
    /* line 6, PREFLEN=4, RANGELEN=5, VAL=0..31, 1011+VAL */
    0,      /* 1011 00000 */
    1,      /* 1011 00001 */
    30,     /* 1011 11110 */
    31,     /* 1011 11111 */
    /* line 7, PREFLEN=5, RANGELEN=5, VAL=32..63, 11100+(VAL-32) */
    32,     /* 11100 00000 */
    33,     /* 11100 00001 */
    62,     /* 11100 11110 */
    63,     /* 11100 11111 */
    /* line 8, PREFLEN=5, RANGELEN=6, VAL=64..127, 11101+(VAL-64) */
    64,     /* 11101 000000 */
    65,     /* 11101 000001 */
    126,    /* 11101 111110 */
    127,    /* 11101 111111 */
    /* line 9, PREFLEN=4, RANGELEN=7, VAL=128..255, 1100+(VAL-128) */
    128,    /* 1100 0000000 */
    129,    /* 1100 0000001 */
    254,    /* 1100 1111110 */
    255,    /* 1100 1111111 */
    /* line 10, PREFLEN=3, RANGELEN=8, VAL=256..511, 001+(VAL-256) */
    256,    /* 001 00000000 */
    257,    /* 001 00000001 */
    510,    /* 001 11111110 */
    511,    /* 001 11111111 */
    /* line 11, PREFLEN=3, RANGELEN=9, VAL=512..1023, 010+(VAL-512) */
    512,    /* 010 00000000 0 */
    513,    /* 010 00000000 1 */
    1022,   /* 010 11111111 0 */
    1023,   /* 010 11111111 1 */
    /* line 12, PREFLEN=3, RANGELEN=10, VAL=1024..2047, 011+(VAL-1024) */
    1024,   /* 011 00000000 00 */
    1025,   /* 011 00000000 01 */
    2046,   /* 011 11111111 10 */
    2047,   /* 011 11111111 11 */
    /* line 13, PREFLEN=5, RANGELEN=32, VAL=-INF..-1025, 11110+(-1025-VAL) */
    -1025,  /* 11110 00000000 00000000 00000000 00000000 */
    -1026,  /* 11110 00000000 00000000 00000000 00000001 */
    /* line 14, PREFLEN=5, RANGELEN=32, VAL=2048..INF, 11111+(VAL-2048) */
    2048,   /* 11111 00000000 00000000 00000000 00000000 */
    2049,   /* 11111 00000000 00000000 00000000 00000001 */
};
const byte test_input_G[] = {
    /* 1000 0000 0000 0100 0000 0000 0110 0011 */
       0x80,     0x04,     0x00,     0x63,
    /* 1111 1101 0001 1111 1111 0000 0000 0000 */
       0xfd,     0x1f,     0xf0,     0x00,
    /* 0000 0000 0100 0111 1111 0000 1111 1111 */
       0x00,     0x47,     0xf0,     0xff,
    /* 1001 0000 0001 0010 0000 0110 0111 1111 */
       0x90,     0x12,     0x06,     0x7f,
    /* 0100 1111 1111 1101 0000 0001 1010 0000 */
       0x4f,     0xfd,     0x01,     0xa0,
    /* 0111 0101 1111 0110 1011 1111 1101 1000 */
       0x75,     0xf6,     0xbf,     0xd8,
    /* 0011 0110 0001 1101 1111 1011 0111 1111 */
       0x36,     0x1d,     0xfb,     0x7f,
    /* 1010 0000 0101 0000 0110 1011 1101 0101 */
       0xa0,     0x50,     0x6b,     0xd5,
    /* 1111 1011 0000 0101 1000 0110 1111 1101 */
       0xfb,     0x05,     0x86,     0xfd,
    /* 0111 1111 1110 0000 0011 1000 0001 1110 */
       0x7f,     0xe0,     0x38,     0x1e,
    /* 0111 1011 1001 1111 1110 1000 0001 1101 */
       0x7b,     0x9f,     0xe8,     0x1d,
    /* 0000 0111 1011 1111 0111 0111 1111 1100 */
       0x07,     0xbf,     0x77,     0xfc,
    /* 0000 0001 1000 0000 0111 0011 1111 0110 */
       0x01,     0x80,     0x73,     0xf6,
    /* 0111 1111 0010 0000 0000 0100 0000 0100 */
       0x7f,     0x20,     0x04,     0x04,
    /* 1111 1111 0001 1111 1111 0100 0000 0000 */
       0xff,     0x1f,     0xf4,     0x00,
    /* 0100 0000 0001 0101 1111 1110 0101 1111 */
       0x40,     0x15,     0xfe,     0x5f,
    /* 1111 0110 0000 0000 0011 0000 0000 0101 */
       0xf6,     0x00,     0x30,     0x05,
    /* 1111 1111 1100 1111 1111 1111 1111 0000 */
       0xff,     0xcf,     0xff,     0xf0,
    /* 0000 0000 0000 0000 0000 0000 0000 0111 */
       0x00,     0x00,     0x00,     0x07,
    /* 1000 0000 0000 0000 0000 0000 0000 0000 */
       0x80,     0x00,     0x00,     0x00,
    /* 0111 1110 0000 0000 0000 0000 0000 0000 */
       0x7e,     0x00,     0x00,     0x00,
    /* 0000 0001 1111 0000 0000 0000 0000 0000 */
       0x01,     0xf0,     0x00,     0x00,
    /* 0000 0000 0001 */
       0x00,     0x10,
};

/* test code for Table B.8 - Standard Huffman table H */
const int32_t test_output_H[] = {
    /* line 0, PREFLEN=8, RANGELEN=3, VAL=-15..-8, 11111100+(VAL+15) */
    -15,    /* 11111100 000 */
    -14,    /* 11111100 001 */
    -9,     /* 11111100 110 */
    -8,     /* 11111100 111 */
    /* line 1, PREFLEN=9, RANGELEN=1, VAL=-7..-6, 111111100+(VAL+7) */
    -7,     /* 111111100 0 */
    -6,     /* 111111100 1 */
    /* line 2, PREFLEN=8, RANGELEN=1, VAL=-5..-4, 11111101+(VAL+5) */
    -5,     /* 11111101 0 */
    -4,     /* 11111101 1 */
    /* line 3, PREFLEN=9, RANGELEN=0, VAL=-3, 111111101 */
    -3,     /* 111111101 */
    /* line 4, PREFLEN=7, RANGELEN=0, VAL=-2, 1111100 */
    -2,     /* 1111100 */
    /* line 5, PREFLEN=4, RANGELEN=0, VAL=-1, 1010 */
    -1,     /* 1010 */
    /* line 6, PREFLEN=2, RANGELEN=1, VAL=0..1, 00+VAL */
    0,      /* 00 0 */
    1,      /* 00 1 */
    /* line 7, PREFLEN=5, RANGELEN=0, VAL=2, 11010 */
    2,      /* 11010 */
    /* line 8, PREFLEN=6, RANGELEN=0, VAL=3, 111010 */
    3,      /* 111010 */
    /* line 9, PREFLEN=3, RANGELEN=4, VAL=4..19, 100+(VAL-4) */
    4,      /* 100 0000 */
    5,      /* 100 0001 */
    18,     /* 100 1110 */
    19,     /* 100 1111 */
    /* line 10, PREFLEN=6, RANGELEN=1, VAL=20..21, 111011+(VAL-20) */
    20,     /* 111011 0 */
    21,     /* 111011 1 */
    /* line 11, PREFLEN=4, RANGELEN=4, VAL=22..37, 1011+(VAL-22) */
    22,     /* 1011 0000 */
    23,     /* 1011 0001 */
    36,     /* 1011 1110 */
    37,     /* 1011 1111 */
    /* line 12, PREFLEN=4, RANGELEN=5, VAL=38..69, 1100+(VAL-38) */
    38,     /* 1100 00000 */
    39,     /* 1100 00001 */
    68,     /* 1100 11110 */
    69,     /* 1100 11111 */
    /* line 13, PREFLEN=5, RANGELEN=6, VAL=70..133, 11011+(VAL-70) */
    70,     /* 11011 000000 */
    71,     /* 11011 000001 */
    132,    /* 11011 111110 */
    133,    /* 11011 111111 */
    /* line 14, PREFLEN=5, RANGELEN=7, VAL=134..261, 11100+(VAL-134) */
    134,    /* 11100 0000000 */
    135,    /* 11100 0000001 */
    260,    /* 11100 1111110 */
    261,    /* 11100 1111111 */
    /* line 15, PREFLEN=6, RANGELEN=7, VAL=262..389, 111100+(VAL-262) */
    262,    /* 111100 0000000 */
    263,    /* 111100 0000001 */
    388,    /* 111100 1111110 */
    389,    /* 111100 1111111 */
    /* line 16, PREFLEN=7, RANGELEN=8, VAL=390..645, 1111101+(VAL-390) */
    390,    /* 1111101 00000000 */
    391,    /* 1111101 00000001 */
    644,    /* 1111101 11111110 */
    645,    /* 1111101 11111111 */
    /* line 17, PREFLEN=6, RANGELEN=10, VAL=646..1669, 111101+(VAL-646) */
    646,    /* 111101 00000000 00 */
    647,    /* 111101 00000000 01 */
    1668,   /* 111101 11111111 10 */
    1669,   /* 111101 11111111 11 */
    /* line 18, PREFLEN=9, RANGELEN=32, VAL=-INF..-16, 111111110+(-16-VAL) */
    -16,    /* 111111110 00000000 00000000 00000000 00000000 */
    -17,    /* 111111110 00000000 00000000 00000000 00000001 */
    /* line 19, PREFLEN=9, RANGELEN=32, VAL=1670..INF, 111111111+(VAL-1670) */
    1670,   /* 111111111 00000000 00000000 00000000 00000000 */
    1671,   /* 111111111 00000000 00000000 00000000 00000001 */
    /* line 20, PREFLEN=2, VAL=OOB, 01 */
    /*OOB*/ /* 01 */
};
const byte test_input_H[] = {
    /* 1111 1100  0001 1111 1000 0111 1111 0011 */
       0xfc,     0x1f,     0x87,     0xf3,
    /* 0111 1110  0111 1111 1110 0011 1111 1001 */
       0x7e,     0x7f,     0xe3,     0xf9,
    /* 1111 1101  0111 1110 1111 1111 1011 1111 */
       0xfd,     0x7e,     0xff,     0xbf,
    /* 0010 1000  0001 1101 0111 0101 0000 0010 */
       0x28,     0x1d,     0x75,     0x02,
    /* 0000 1100  1110 1001 1111 1101 1011 1011 */
       0x0c,     0xe9,     0xfd,     0xbb,
    /* 1101 1000  0101 1000 1101 1111 0101 1111 */
       0xd8,     0x58,     0xdf,     0x5f,
    /* 1110 0000  0011 0000 0011 1001 1110 1100 */
       0xe0,     0x30,     0x39,     0xec,
    /* 1111 1110  1100 0000 1101 1000 0011 1011 */
       0xfe,     0xc0,     0xd8,     0x3b,
    /* 1111 1011  0111 1111 1111 0000 0000 0111 */
       0xfb,     0x7f,     0xf0,     0x07,
    /* 0000 0000  1111 0011 1111 0111 0011 1111 */
       0x00,     0xf3,     0xf7,     0x3f,
    /* 1111 1000  0000 0011 1100 0000 0011 1110 */
       0xf8,     0x03,     0xc0,     0x3e,
    /* 0111 1110  1111 0011 1111 1111 1101 0000 */
       0x7e,     0xf3,     0xff,     0xd0,
    /* 0000 1111  1010 0000 0011 1111 0111 1111 */
       0x0f,     0xa0,     0x3f,     0x7f,
    /* 1011 1110  1111 1111 1111 1010 0000 0000 */
       0xbe,     0xff,     0xfa,     0x00,
    /* 0111 1010  0000 0000 1111 1011 1111 1111 */
       0x7a,     0x00,     0xfb,     0xff,
    /* 0111 1011  1111 1111 1111 1111 1000 0000 */
       0x7b,     0xff,     0xff,     0x80,
    /* 0000 0000  0000 0000 0000 0000 0011 1111 */
       0x00,     0x00,     0x00,     0x3f,
    /* 1100 0000  0000 0000 0000 0000 0000 0000 */
       0xc0,     0x00,     0x00,     0x00,
    /* 0011 1111  1111 0000 0000 0000 0000 0000 */
       0x3f,     0xf0,     0x00,     0x00,
    /* 0000 0000  0000 1111 1111 1000 0000 0000 */
       0x00,     0x0f,     0xf8,     0x00,
    /* 0000 0000  0000 0000 0000 101 */
       0x00,     0x00,     0x0a,
};

/* test code for Table B.9 - Standard Huffman table I */
const int32_t test_output_I[] = {
    /* line 0, PREFLEN=8, RANGELEN=4, VAL=-31..-16, 11111100+(VAL+31) */
    -31,    /* 11111100 0000 */
    -30,    /* 11111100 0001 */
    -17,    /* 11111100 1110 */
    -16,    /* 11111100 1111 */
    /* line 1, PREFLEN=9, RANGELEN=2, VAL=-15..-12, 111111100+(VAL+15) */
    -15,    /* 111111100 00 */
    -14,    /* 111111100 01 */
    -13,    /* 111111100 10 */
    -12,    /* 111111100 11 */
    /* line 2, PREFLEN=8, RANGELEN=2, VAL=-11..-8, 11111101+(VAL+11) */
    -11,    /* 11111101 00 */
    -10,    /* 11111101 01 */
    -9,     /* 11111101 10 */
    -8,     /* 11111101 11 */
    /* line 3, PREFLEN=9, RANGELEN=1, VAL=-7..-6, 111111101+(VAL+7) */
    -7,     /* 111111101 0 */
    -6,     /* 111111101 1 */
    /* line 4, PREFLEN=7, RANGELEN=1, VAL=-5..-4, 1111100+(VAL+5) */
    -5,     /* 1111100 0 */
    -4,     /* 1111100 1 */
    /* line 5, PREFLEN=4, RANGELEN=1, VAL=-3..-2, 1010+(VAL+3) */
    -3,     /* 1010 0 */
    -2,     /* 1010 1 */
    /* line 6, PREFLEN=3, RANGELEN=1, VAL=-1..0, 010+(VAL+1) */
    -1,     /* 010 0 */
    0,      /* 010 1 */
    /* line 7, PREFLEN=3, RANGELEN=1, VAL=1..2, 011+(VAL-1) */
    1,      /* 011 0 */
    2,      /* 011 1 */
    /* line 8, PREFLEN=5, RANGELEN=1, VAL=3..4, 11010+(VAL-3) */
    3,      /* 11010 0 */
    4,      /* 11010 1 */
    /* line 9, PREFLEN=6, RANGELEN=1, VAL=5..6, 111010+(VAL-5) */
    5,      /* 111010 0 */
    6,      /* 111010 1 */
    /* line 10, PREFLEN=3, RANGELEN=5, VAL=7..38, 100+(VAL-7) */
    7,      /* 100 00000 */
    8,      /* 100 00001 */
    37,     /* 100 11110 */
    38,     /* 100 11111 */
    /* line 11, PREFLEN=6, RANGELEN=2, VAL=39..42, 111011+(VAL-39) */
    39,     /* 111011 00 */
    40,     /* 111011 01 */
    41,     /* 111011 10 */
    42,     /* 111011 11 */
    /* line 12, PREFLEN=4, RANGELEN=5, VAL=43..74, 1011+(VAL-43) */
    43,     /* 1011 00000 */
    44,     /* 1011 00001 */
    73,     /* 1011 11110 */
    74,     /* 1011 11111 */
    /* line 13, PREFLEN=4, RANGELEN=6, VAL=75..138, 1100+(VAL-75) */
    75,     /* 1100 000000 */
    76,     /* 1100 000001 */
    137,    /* 1100 111110 */
    138,    /* 1100 111111 */
    /* line 14, PREFLEN=5, RANGELEN=7, VAL=139..266, 11011+(VAL-139) */
    139,    /* 11011 0000000 */
    140,    /* 11011 0000001 */
    265,    /* 11011 1111110 */
    266,    /* 11011 1111111 */
    /* line 15, PREFLEN=5, RANGELEN=8, VAL=267..522, 11100+(VAL-267) */
    267,    /* 11100 00000000 */
    268,    /* 11100 00000001 */
    521,    /* 11100 11111110 */
    522,    /* 11100 11111111 */
    /* line 16, PREFLEN=6, RANGELEN=8, VAL=523..778, 111100+(VAL-523) */
    523,    /* 111100 00000000 */
    524,    /* 111100 00000001 */
    777,    /* 111100 11111110 */
    778,    /* 111100 11111111 */
    /* line 17, PREFLEN=7, RANGELEN=9, VAL=779..1290, 1111101+(VAL-779) */
    779,    /* 1111101 00000000 0 */
    780,    /* 1111101 00000000 1 */
    1289,   /* 1111101 11111111 0 */
    1290,   /* 1111101 11111111 1 */
    /* line 18, PREFLEN=6, RANGELEN=11, VAL=1291..3338, 111101+(VAL-1291) */
    1291,   /* 111101 00000000 000 */
    1292,   /* 111101 00000000 001 */
    3337,   /* 111101 11111111 110 */
    3338,   /* 111101 11111111 111 */
    /* line 19, PREFLEN=9, RANGELEN=32, VAL=-INF..-32, 111111110+(-32-VAL) */
    -32,    /* 111111110 00000000 00000000 00000000 00000000 */
    -33,    /* 111111110 00000000 00000000 00000000 00000001 */
    /* line 20, PREFLEN=9, RANGELEN=32, VAL=3339..INF, 111111111+(VAL-3339) */
    3339,   /* 111111111 00000000 00000000 00000000 00000000 */
    3340,   /* 111111111 00000000 00000000 00000000 00000001 */
    /* line 21, PREFLEN=2, VAL=OOB, 00 */
    /*OOB*/ /* 00 */
};
const byte test_input_I[] = {
    /* 1111 1100 0000 1111 1100 0001 1111 1100 */
       0xfc,     0x0f,     0xc1,     0xfc,
    /* 1110 1111 1100 1111 1111 1110 0001 1111 */
       0xef,     0xcf,     0xfe,     0x1f,
    /* 1100 0111 1111 1001 0111 1111 0011 1111 */
       0xc7,     0xf9,     0x7f,     0x3f,
    /* 1101 0011 1111 0101 1111 1101 1011 1111 */
       0xd3,     0xf5,     0xfd,     0xbf,
    /* 0111 1111 1110 1011 1111 1011 1111 1000 */
       0x7f,     0xeb,     0xfb,     0xf8,
    /* 1111 1001 1010 0101 0101 0001 0101 1001 */
       0xf9,     0xa5,     0x51,     0x59,
    /* 1111 0100 1101 0111 1010 0111 0101 1000 */
       0xf4,     0xd7,     0xa7,     0x58,
    /* 0000 1000 0001 1001 1110 1001 1111 1110 */
       0x08,     0x19,     0xe9,     0xfe,
    /* 1100 1110 1101 1110 1110 1110 1111 1011 */
       0xce,     0xde,     0xee,     0xfb,
    /* 0000 0101 1000 0110 1111 1101 0111 1111 */
       0x05,     0x86,     0xfd,     0x7f,
    /* 1100 0000 0011 0000 0001 1100 1111 1011 */
       0xc0,     0x30,     0x1c,     0xfb,
    /* 0011 1111 1101 1000 0000 1101 1000 0001 */
       0x3f,     0xd8,     0x0d,     0x81,
    /* 1101 1111 1110 1101 1111 1111 1110 0000 */
       0xdf,     0xed,     0xff,     0xe0,
    /* 0000 0111 0000 0000 0111 1001 1111 1101 */
       0x07,     0x00,     0x79,     0xfd,
    /* 1100 1111 1111 1111 0000 0000 0011 1100 */
       0xcf,     0xff,     0x00,     0x3c,
    /* 0000 0001 1111 0011 1111 1011 1100 1111 */
       0x01,     0xf3,     0xfb,     0xcf,
    /* 1111 1111 1010 0000 0000 1111 1010 0000 */
       0xff,     0xa0,     0x0f,     0xa0,
    /* 0001 1111 1011 1111 1110 1111 1011 1111 */
       0x1f,     0xbf,     0xef,     0xbf,
    /* 1111 1111 0100 0000 0000 0111 1010 0000 */
       0xff,     0x40,     0x07,     0xa0,
    /* 0000 0111 1101 1111 1111 1101 1110 1111 */
       0x07,     0xdf,     0xfd,     0xef,
    /* 1111 1111 1111 1111 0000 0000 0000 0000 */
       0xff,     0xff,     0x00,     0x00,
    /* 0000 0000 0000 0000 0111 1111 1000 0000 */
       0x00,     0x00,     0x7f,     0x80,
    /* 0000 0000 0000 0000 0000 0000 0111 1111 */
       0x00,     0x00,     0x00,     0x7f,
    /* 1110 0000 0000 0000 0000 0000 0000 0000 */
       0xe0,     0x00,     0x00,     0x00,
    /* 0001 1111 1111 0000 0000 0000 0000 0000 */
       0x1f,     0xf0,     0x00,     0x00,
    /* 0000 0000 0001 00 */
       0x00,     0x10,
};

/* test code for Table B.10 - Standard Huffman table J */
const int32_t test_output_J[] = {
    /* line 0, PREFLEN=7, RANGELEN=4, VAL=-21..-6, 1111010+(VAL+21) */
    -21,    /* 1111010 0000 */
    -20,    /* 1111010 0001 */
    -7,     /* 1111010 1110 */
    -6,     /* 1111010 1111 */
    /* line 1, PREFLEN=8, RANGELEN=0, VAL=-5, 11111100 */
    -5,     /* 11111100 */
    /* line 2, PREFLEN=7, RANGELEN=0, VAL=-5, 1111011 */
    -4,     /* 1111011 */
    /* line 3, PREFLEN=5, RANGELEN=0, VAL=-3, 11000 */
    -3,     /* 11000 */
    /* line 4, PREFLEN=2, RANGELEN=2, VAL=-2..1, 00+(VAL+2) */
    -2,     /* 00 00 */
    -1,     /* 00 01 */
    0,      /* 00 10 */
    1,      /* 00 11 */
    /* line 5, PREFLEN=5, RANGELEN=0, VAL=2, 11001 */
    2,      /* 11001 */
    /* line 6, PREFLEN=6, RANGELEN=0, VAL=3, 110110 */
    3,      /* 110110 */
    /* line 7, PREFLEN=7, RANGELEN=0, VAL=4, 1111100 */
    4,      /* 1111100 */
    /* line 8, PREFLEN=8, RANGELEN=0, VAL=5, 11111101 */
    5,      /* 11111101 */
    /* line 9, PREFLEN=2, RANGELEN=6, VAL=6..69, 01+(VAL-6) */
    6,      /* 01 000000 */
    7,      /* 01 000001 */
    68,     /* 01 111110 */
    69,     /* 01 111111 */
    /* line 8, PREFLEN=5, RANGELEN=5, VAL=70..101, 11010+(VAL-70) */
    70,     /* 11010 00000 */
    71,     /* 11010 00001 */
    100,    /* 11010 11110 */
    101,    /* 11010 11111 */
    /* line 9, PREFLEN=6, RANGELEN=5, VAL=102..133, 110111+(VAL-102) */
    102,    /* 110111 00000 */
    103,    /* 110111 00001 */
    132,    /* 110111 11110 */
    133,    /* 110111 11111 */
    /* line 10, PREFLEN=6, RANGELEN=6, VAL=134..197, 111000+(VAL-134) */
    134,    /* 111000 000000 */
    135,    /* 111000 000001 */
    196,    /* 111000 111110 */
    197,    /* 111000 111111 */
    /* line 11, PREFLEN=6, RANGELEN=7, VAL=198..325, 111001+(VAL-198) */
    198,    /* 111001 0000000 */
    199,    /* 111001 0000001 */
    324,    /* 111001 1111110 */
    325,    /* 111001 1111111 */
    /* line 12, PREFLEN=6, RANGELEN=8, VAL=326..581, 111010+(VAL-326) */
    326,    /* 111010 00000000 */
    327,    /* 111010 00000001 */
    580,    /* 111010 11111110 */
    581,    /* 111010 11111111 */
    /* line 13, PREFLEN=6, RANGELEN=9, VAL=582..1093, 111011+(VAL-582) */
    582,    /* 111011 00000000 0 */
    583,    /* 111011 00000000 1 */
    1092,   /* 111011 11111111 0 */
    1093,   /* 111011 11111111 1 */
    /* line 14, PREFLEN=6, RANGELEN=10, VAL=1094..2117, 111100+(VAL-1094) */
    1094,   /* 111100 00000000 00 */
    1095,   /* 111100 00000000 01 */
    2116,   /* 111100 11111111 10 */
    2117,   /* 111100 11111111 11 */
    /* line 15, PREFLEN=7, RANGELEN=11, VAL=2118..4165, 1111101+(VAL-2118) */
    2118,   /* 1111101 00000000 000 */
    2119,   /* 1111101 00000000 001 */
    4164,   /* 1111101 11111111 110 */
    4165,   /* 1111101 11111111 111 */
    /* line 16, PREFLEN=8, RANGELEN=32, VAL=-INF..-22, 11111110+(-22-VAL) */
    -22,    /* 11111110 00000000 00000000 00000000 00000000 */
    -23,    /* 11111110 00000000 00000000 00000000 00000001 */
    /* line 17, PREFLEN=8, RANGELEN=32, VAL=4166..INF, 11111111+(VAL-4166) */
    4166,   /* 11111111 00000000 00000000 00000000 00000000 */
    4167,   /* 11111111 00000000 00000000 00000000 00000001 */
    /* line 8, PREFLEN=2, VAL=OOB, 10 */
    /*OOB*/ /* 10 */
};
const byte test_input_J[] = {
    /* 1111 0100 0001 1110 1000 0111 1101 0111 */
       0xf4,     0x1e,     0x87,     0xd7,
    /* 0111 1010 1111 1111 1100 1111 0111 1000 */
       0x7a,     0xff,     0xcf,     0x78,
    /* 0000 0001 0010 0011 1100 1110 1101 1111 */
       0x01,     0x23,     0xce,     0xdf,
    /* 0011 1111 0101 0000 0001 0000 0101 1111 */
       0x3f,     0x50,     0x10,     0x5f,
    /* 1001 1111 1111 0100 0000 1101 0000 0111 */
       0x9f,     0xf4,     0x0d,     0x07,
    /* 0101 1110 1101 0111 1111 0111 0000 0110 */
       0x5e,     0xd7,     0xf7,     0x06,
    /* 1110 0001 1101 1111 1101 1011 1111 1111 */
       0xe1,     0xdf,     0xdb,     0xff,
    /* 1000 0000 0011 1000 0000 0111 1000 1111 */
       0x80,     0x38,     0x07,     0x8f,
    /* 1011 1000 1111 1111 1001 0000 0001 1100 */
       0xb8,     0xff,     0x90,     0x1c,
    /* 1000 0001 1110 0111 1111 0111 0011 1111 */
       0x81,     0xe7,     0xf7,     0x3f,
    /* 1111 1010 0000 0000 1110 1000 0000 0111 */
       0xfa,     0x00,     0xe8,     0x07,
    /* 1010 1111 1110 1110 1011 1111 1111 1011 */
       0xaf,     0xee,     0xbf,     0xfb,
    /* 0000 0000 0111 0110 0000 0001 1110 1111 */
       0x00,     0x76,     0x01,     0xef,
    /* 1111 1101 1101 1111 1111 1111 1100 0000 */
       0xfd,     0xdf,     0xff,     0xc0,
    /* 0000 0011 1100 0000 0000 0111 1100 1111 */
       0x03,     0xc0,     0x07,     0xcf,
    /* 1111 1011 1100 1111 1111 1111 1110 1000 */
       0xfb,     0xcf,     0xff,     0xe8,
    /* 0000 0000 1111 1010 0000 0000 0111 1110 */
       0x00,     0xfa,     0x00,     0x7e,
    /* 1111 1111 1110 1111 1011 1111 1111 1111 */
       0xff,     0xef,     0xbf,     0xff,
    /* 1111 1000 0000 0000 0000 0000 0000 0000 */
       0xf8,     0x00,     0x00,     0x00,
    /* 0000 0011 1111 1000 0000 0000 0000 0000 */
       0x03,     0xf8,     0x00,     0x00,
    /* 0000 0000 0000 0111 1111 1100 0000 0000 */
       0x00,     0x07,     0xfc,     0x00,
    /* 0000 0000 0000 0000 0000 0011 1111 1100 */
       0x00,     0x00,     0x03,     0xfc,
    /* 0000 0000 0000 0000 0000 0000 0000 0110 */
       0x00,     0x00,     0x00,     0x06,
};

/* test code for Table B.11 - Standard Huffman table K */
const int32_t test_output_K[] = {
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
    1,      /* 0 */
    /* line 1, PREFLEN=2, RANGELEN=1, VAL=2..3, 10+(VAL-2) */
    2,      /* 10 0 */
    3,      /* 10 1 */
    /* line 2, PREFLEN=4, RANGELEN=0, VAL=4, 1100 */
    4,      /* 1100 */
    /* line 3, PREFLEN=4, RANGELEN=1, VAL=5..6, 1101+(VAL-5) */
    5,      /* 1101 0 */
    6,      /* 1101 1 */
    /* line 4, PREFLEN=5, RANGELEN=1, VAL=7..8, 11100+(VAL-7) */
    7,      /* 11100 0 */
    8,      /* 11100 1 */
    /* line 5, PREFLEN=5, RANGELEN=2, VAL=9..12, 11101+(VAL-9) */
    9,      /* 11101 00 */
    10,     /* 11101 01 */
    11,     /* 11101 10 */
    12,     /* 11101 11 */
    /* line 6, PREFLEN=6, RANGELEN=2, VAL=13..16, 111100+(VAL-13) */
    13,     /* 111100 00 */
    14,     /* 111100 01 */
    15,     /* 111100 10 */
    16,     /* 111100 11 */
    /* line 7, PREFLEN=7, RANGELEN=2, VAL=17..20, 1111010+(VAL-17) */
    17,     /* 1111010 00 */
    18,     /* 1111010 01 */
    19,     /* 1111010 10 */
    20,     /* 1111010 11 */
    /* line 8, PREFLEN=7, RANGELEN=3, VAL=21..28, 1111011+(VAL-21) */
    21,     /* 1111011 000 */
    22,     /* 1111011 001 */
    27,     /* 1111011 110 */
    28,     /* 1111011 111 */
    /* line 9, PREFLEN=7, RANGELEN=4, VAL=29..44, 1111100+(VAL-29) */
    29,     /* 1111100 0000 */
    30,     /* 1111100 0001 */
    43,     /* 1111100 1110 */
    44,     /* 1111100 1111 */
    /* line 10, PREFLEN=7, RANGELEN=5, VAL=45..76, 1111101+(VAL-45) */
    45,     /* 1111101 00000 */
    46,     /* 1111101 00001 */
    75,     /* 1111101 11110 */
    76,     /* 1111101 11111 */
    /* line 11, PREFLEN=7, RANGELEN=6, VAL=77..140, 1111110+(VAL-77) */
    77,     /* 1111110 000000 */
    78,     /* 1111110 000001 */
    139,    /* 1111110 111110 */
    140,    /* 1111110 111111 */
    /* line 12, PREFLEN=7, RANGELEN=32, VAL=141..INF, 1111111+(VAL-141) */
    141,    /* 1111111 00000000 00000000 00000000 00000000 */
    142,    /* 1111111 00000000 00000000 00000000 00000001 */
};
const byte test_input_K[] = {
    /* 0100 1011 1001 1010 1101 1111 0001 1100 */
       0x4b,     0x9a,     0xdf,     0x1c,
    /* 1111 0100 1110 1011 1101 1011 1011 1111 */
       0xf4,     0xeb,     0xdb,     0xbf,
    /* 1000 0111 1000 1111 1001 0111 1001 1111 */
       0x87,     0x8f,     0x97,     0x9f,
    /* 1010 0011 1101 0011 1110 1010 1111 0101 */
       0xa3,     0xd3,     0xea,     0xf5,
    /* 1111 1011 0001 1110 1100 1111 1011 1101 */
       0xfb,     0x1e,     0xcf,     0xbd,
    /* 1110 1111 1111 1100 0000 1111 1000 0011 */
       0xef,     0xfc,     0x0f,     0x83,
    /* 1111 0011 1011 1110 0111 1111 1101 0000 */
       0xf3,     0xbe,     0x7f,     0xd0,
    /* 0111 1101 0000 1111 1101 1111 0111 1101 */
       0x7d,     0x0f,     0xdf,     0x7d,
    /* 1111 1111 1110 0000 0011 1111 0000 0011 */
       0xff,     0xe0,     0x3f,     0x03,
    /* 1111 1011 1110 1111 1101 1111 1111 1111 */
       0xfb,     0xef,     0xdf,     0xff,
    /* 0000 0000 0000 0000 0000 0000 0000 0000 */
       0x00,     0x00,     0x00,     0x00,
    /* 1111 1110 0000 0000 0000 0000 0000 0000 */
       0xfe,     0x00,     0x00,     0x00,
    /* 0000 001 */
       0x02,
};

/* test code for Table B.12 - Standard Huffman table L */
const int32_t test_output_L[] = {
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
    1,      /* 0 */
    /* line 1, PREFLEN=2, RANGELEN=0, VAL=2, 10 */
    2,      /* 10 */
    /* line 2, PREFLEN=3, RANGELEN=1, VAL=3..4, 110+(VAL-3) */
    3,      /* 110 0 */
    4,      /* 110 1 */
    /* line 3, PREFLEN=5, RANGELEN=0, VAL=5, 11100 */
    5,      /* 11100 */
    /* line 4, PREFLEN=5, RANGELEN=1, VAL=6..7, 11101+(VAL-7) */
    6,      /* 11101 0 */
    7,      /* 11101 1 */
    /* line 5, PREFLEN=6, RANGELEN=1, VAL=8..9, 111100+(VAL-8) */
    8,      /* 111100 0 */
    9,      /* 111100 1 */
    /* line 6, PREFLEN=7, RANGELEN=0, VAL=10, 1111010 */
    10,     /* 1111010 */
    /* line 7, PREFLEN=7, RANGELEN=1, VAL=11..12, 1111011+(VAL-11) */
    11,     /* 1111011 0 */
    12,     /* 1111011 1 */
    /* line 8, PREFLEN=7, RANGELEN=2, VAL=13..16, 1111100+(VAL-13) */
    13,     /* 1111100 00 */
    14,     /* 1111100 01 */
    15,     /* 1111100 10 */
    16,     /* 1111100 11 */
    /* line 9, PREFLEN=7, RANGELEN=3, VAL=17..24, 1111101+(VAL-17) */
    17,     /* 1111101 000 */
    18,     /* 1111101 001 */
    23,     /* 1111101 110 */
    24,     /* 1111101 111 */
    /* line 10, PREFLEN=7, RANGELEN=4, VAL=25..40, 1111110+(VAL-25) */
    25,     /* 1111110 0000 */
    26,     /* 1111110 0001 */
    39,     /* 1111110 1110 */
    40,     /* 1111110 1111 */
    /* line 11, PREFLEN=8, RANGELEN=5, VAL=41..72, 11111110+(VAL-41) */
    41,     /* 11111110 00000 */
    42,     /* 11111110 00001 */
    71,     /* 11111110 11110 */
    72,     /* 11111110 11111 */
    /* line 12, PREFLEN=8, RANGELEN=32, VAL=73..INF, 11111111+(VAL-73) */
    73,     /* 11111111 00000000 00000000 00000000 00000000 */
    74,     /* 11111111 00000000 00000000 00000000 00000001 */
};
const byte test_input_L[] = {
    /* 0101 1001 1011 1100 1110 1011 1011 1111 */
       0x59,     0xbc,     0xeb,     0xbf,
    /* 0001 1110 0111 1101 0111 1011 0111 1011 */
       0x1e,     0x7d,     0x7b,     0x7b,
    /* 1111 1100 0011 1110 0011 1111 0010 1111 */
       0xfc,     0x3e,     0x3f,     0x2f,
    /* 1001 1111 1101 0001 1111 0100 1111 1101 */
       0x9f,     0xd1,     0xf4,     0xfd,
    /* 1101 1111 0111 1111 1110 0000 1111 1100 */
       0xdf,     0x7f,     0xe0,     0xfc,
    /* 0011 1111 1011 1011 1111 0111 1111 1111 */
       0x3f,     0xbb,     0xf7,     0xff,
    /* 0000 0011 1111 1000 0011 1111 1101 1110 */
       0x03,     0xf8,     0x3f,     0xde,
    /* 1111 1110 1111 1111 1111 1000 0000 0000 */
       0xfe,     0xff,     0xf8,     0x00,
    /* 0000 0000 0000 0000 0000 0111 1111 1000 */
       0x00,     0x00,     0x07,     0xf8,
    /* 0000 0000 0000 0000 0000 0000 0000 1 */
       0x00,     0x00,     0x00,     0x08,
};

/* test code for Table B.13 - Standard Huffman table M */
const int32_t test_output_M[] = {
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
    1,      /* 0 */
    /* line 1, PREFLEN=3, RANGELEN=0, VAL=2, 100 */
    2,      /* 100 */
    /* line 2, PREFLEN=3, RANGELEN=0, VAL=3, 1100 */
    3,      /* 1100 */
    /* line 3, PREFLEN=5, RANGELEN=0, VAL=4, 11100 */
    4,      /* 11100 */
    /* line 4, PREFLEN=4, RANGELEN=1, VAL=5..6, 1101+(VAL-5) */
    5,      /* 1101 0 */
    6,      /* 1101 1 */
    /* line 5, PREFLEN=3, RANGELEN=3, VAL=7..14, 101+(VAL-7) */
    7,      /* 101 000 */
    8,      /* 101 001 */
    13,     /* 101 110 */
    14,     /* 101 111 */
    /* line 6, PREFLEN=6, RANGELEN=1, VAL=15..16, 111010+(VAL-15) */
    15,     /* 111010 0 */
    16,     /* 111010 1 */
    /* line 7, PREFLEN=6, RANGELEN=2, VAL=17..20, 111011+(VAL-17) */
    17,     /* 111011 00 */
    18,     /* 111011 01 */
    19,     /* 111011 10 */
    20,     /* 111011 11 */
    /* line 8, PREFLEN=6, RANGELEN=3, VAL=21..28, 111100+(VAL-21) */
    21,     /* 111100 000 */
    22,     /* 111100 001 */
    27,     /* 111100 110 */
    28,     /* 111100 111 */
    /* line 9, PREFLEN=6, RANGELEN=4, VAL=29..44, 111101+(VAL-29) */
    29,     /* 111101 0000 */
    30,     /* 111101 0001 */
    43,     /* 111101 1110 */
    44,     /* 111101 1111 */
    /* line 10, PREFLEN=6, RANGELEN=5, VAL=45..76, 111110+(VAL-45) */
    45,     /* 111110 00000 */
    46,     /* 111110 00001 */
    75,     /* 111110 11110 */
    76,     /* 111110 11111 */
    /* line 11, PREFLEN=7, RANGELEN=6, VAL=77..140, 1111110+(VAL-77) */
    77,     /* 1111110 000000 */
    78,     /* 1111110 000001 */
    139,    /* 1111110 111110 */
    140,    /* 1111110 111111 */
    /* line 12, PREFLEN=7, RANGELEN=32, VAL=141..INF, 1111111+(VAL-141) */
    141,    /* 1111111 00000000 00000000 00000000 00000000 */
    142,    /* 1111111 00000000 00000000 00000000 00000001 */
};
const byte test_input_M[] = {
    /* 0100 1100 1110 0110 1011 0111 0100 0101 */
       0x4c,     0xe6,     0xb7,     0x45,
    /* 0011 0111 0101 1111 1101 0011 1010 1111 */
       0x37,     0x5f,     0xd3,     0xaf,
    /* 0110 0111 0110 1111 0111 0111 0111 1111 */
       0x67,     0x6f,     0x77,     0x7f,
    /* 1000 0011 1100 0011 1110 0110 1111 0011 */
       0x83,     0xc3,     0xe6,     0xf3,
    /* 1111 1010 0001 1110 1000 1111 1011 1101 */
       0xfa,     0x1e,     0x8f,     0xbd,
    /* 1110 1111 1111 1100 0000 1111 1000 0011 */
       0xef,     0xfc,     0x0f,     0x83,
    /* 1111 0111 1011 1110 1111 1111 1110 0000 */
       0xf7,     0xbe,     0xff,     0xe0,
    /* 0011 1111 0000 0011 1111 1011 1110 1111 */
       0x3f,     0x03,     0xfb,     0xef,
    /* 1101 1111 1111 1111 0000 0000 0000 0000 */
       0xdf,     0xff,     0x00,     0x00,
    /* 0000 0000 0000 0000 1111 1110 0000 0000 */
       0x00,     0x00,     0xfe,     0x00,
    /* 0000 0000 0000 0000 0000 001 */
       0x00,     0x00,     0x02,
};

/* test code for Table B.14 - Standard Huffman table N */
const int32_t test_output_N[] = {
    /* line 0, PREFLEN=3, RANGELEN=0, VAL=-2, 100 */
    -2,     /* 100 */
    /* line 1, PREFLEN=3, RANGELEN=0, VAL=-1, 101 */
    -1,     /* 101 */
    /* line 2, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
    0,      /* 0 */
    /* line 3, PREFLEN=3, RANGELEN=0, VAL=1, 110 */
    1,      /* 110 */
    /* line 4, PREFLEN=3, RANGELEN=0, VAL=2, 111 */
    2,      /* 111 */
};
const byte test_input_N[] = {
    /* 1001 0101 1011 1 */
       0x95,     0xb8,
};

/* test code for Table B.15 - Standard Huffman table O */
const int32_t test_output_O[] = {
    /* line 0, PREFLEN=7, RANGELEN=4, VAL=-24..-9, 1111100+(VAL+24) */
    -24,    /* 1111100 0000 */
    -23,    /* 1111100 0001 */
    -10,    /* 1111100 1110 */
    -9,     /* 1111100 1111 */
    /* line 1, PREFLEN=6, RANGELEN=2, VAL=-8..-5, 111100+(VAL+8) */
    -8,     /* 111100 00 */
    -7,     /* 111100 01 */
    -6,     /* 111100 10 */
    -5,     /* 111100 11 */
    /* line 2, PREFLEN=5, RANGELEN=1, VAL=-4..-3, 11100+(VAL+4) */
    -4,     /* 11100 0 */
    -3,     /* 11100 1 */
    /* line 3, PREFLEN=4, RANGELEN=0, VAL=-2, 1100 */
    -2,     /* 1100 */
    /* line 4, PREFLEN=3, RANGELEN=0, VAL=-1, 100 */
    -1,     /* 100 */
    /* line 5, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
    0,      /* 0 */
    /* line 6, PREFLEN=3, RANGELEN=0, VAL=1, 101 */
    1,      /* 101 */
    /* line 7, PREFLEN=4, RANGELEN=0, VAL=2, 1101 */
    2,      /* 1101 */
    /* line 8, PREFLEN=5, RANGELEN=1, VAL=3..4, 11101+(VAL-3) */
    3,      /* 11101 0 */
    4,      /* 11101 1 */
    /* line 9, PREFLEN=6, RANGELEN=2, VAL=5..8, 111101+(VAL-5) */
    5,      /* 111101 00 */
    6,      /* 111101 01 */
    7,      /* 111101 10 */
    8,      /* 111101 11 */
    /* line 10, PREFLEN=7, RANGELEN=4, VAL=9..24, 1111101+(VAL-9) */
    9,      /* 1111101 0000 */
    10,     /* 1111101 0001 */
    23,     /* 1111101 1110 */
    24,     /* 1111101 1111 */
    /* line 11, PREFLEN=7, RANGELEN=32, VAL=-INF..-25, 1111110+(-25-VAL) */
    -25,    /* 1111110 00000000 00000000 00000000 00000000 */
    -26,    /* 1111110 00000000 00000000 00000000 00000001 */
    /* line 12, PREFLEN=7, RANGELEN=32, VAL=25..INF, 1111111+(VAL-25) */
    25,     /* 1111111 00000000 00000000 00000000 00000000 */
    26,     /* 1111111 00000000 00000000 00000000 00000001 */
};
const byte test_input_O[] = {
    /* 1111 1000 0001 1111 0000 0111 1110 0111 */
       0xf8,     0x1f,     0x07,     0xe7,
    /* 0111 1100 1111 1111 0000 1111 0001 1111 */
       0x7c,     0xff,     0x0f,     0x1f,
    /* 0010 1111 0011 1110 0011 1001 1100 1000 */
       0x2f,     0x3e,     0x39,     0xc8,
    /* 1011 1011 1101 0111 0111 1110 1001 1110 */
       0xbb,     0xd7,     0x7e,     0x9e,
    /* 1011 1110 1101 1110 1111 1111 0100 0011 */
       0xbe,     0xde,     0xff,     0x43,
    /* 1110 1000 1111 1101 1110 1111 1011 1111 */
       0xe8,     0xfd,     0xef,     0xbf,
    /* 1111 1000 0000 0000 0000 0000 0000 0000 */
       0xf8,     0x00,     0x00,     0x00,
    /* 0000 0011 1111 0000 0000 0000 0000 0000 */
       0x03,     0xf0,     0x00,     0x00,
    /* 0000 0000 0000 1111 1111 0000 0000 0000 */
       0x00,     0x0f,     0xf0,     0x00,
    /* 0000 0000 0000 0000 0000 1111 1110 0000 */
       0x00,     0x00,     0x0f,     0xe0,
    /* 0000 0000 0000 0000 0000 0000 001 */
       0x00,     0x00,     0x00,     0x20,
};

typedef struct test_huffmancodes {
    const char *name;
    const Jbig2HuffmanParams *params;
    const byte *input;
    const size_t input_len;
    const int32_t *output;
    const size_t output_len;
} test_huffmancodes_t;

#define countof(x) (sizeof((x)) / sizeof((x)[0]))

#define DEF_TEST_HUFFMANCODES(x) { \
    #x, \
    &jbig2_huffman_params_##x, \
    test_input_##x, countof(test_input_##x), \
    test_output_##x, countof(test_output_##x), \
}

test_huffmancodes_t tests[] = {
    DEF_TEST_HUFFMANCODES(A),
    DEF_TEST_HUFFMANCODES(B),
    DEF_TEST_HUFFMANCODES(C),
    DEF_TEST_HUFFMANCODES(D),
    DEF_TEST_HUFFMANCODES(E),
    DEF_TEST_HUFFMANCODES(F),
    DEF_TEST_HUFFMANCODES(G),
    DEF_TEST_HUFFMANCODES(H),
    DEF_TEST_HUFFMANCODES(I),
    DEF_TEST_HUFFMANCODES(J),
    DEF_TEST_HUFFMANCODES(K),
    DEF_TEST_HUFFMANCODES(L),
    DEF_TEST_HUFFMANCODES(M),
    DEF_TEST_HUFFMANCODES(N),
    DEF_TEST_HUFFMANCODES(O),
};

typedef struct test_stream {
    Jbig2WordStream ws;
    test_huffmancodes_t *h;
} test_stream_t;

static int
test_get_word2(Jbig2Ctx *ctx, Jbig2WordStream *self, size_t offset, uint32_t *word)
{
    test_stream_t *st = (test_stream_t *) self;
    uint32_t val = 0;
    int ret = 0;

    if (st == NULL || st->h == NULL || word == NULL)
        return -1;
    if (offset >= st->h->input_len)
        return 0;

    if (offset < st->h->input_len) {
        val |= (st->h->input[offset] << 24);
        ret++;
    }
    if (offset + 1 < st->h->input_len) {
        val |= (st->h->input[offset + 1] << 16);
        ret++;
    }
    if (offset + 2 < st->h->input_len) {
        val |= (st->h->input[offset + 2] << 8);
        ret++;
    }
    if (offset + 3 < st->h->input_len) {
        val |= st->h->input[offset + 3];
        ret++;
    }
    *word = val;
    return ret;
}

static int test2()
{
    Jbig2Ctx *ctx;
    int success = 0;
    int i;

    ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
    if (ctx == NULL) {
        fprintf(stderr, "Failed to allocate jbig2 context\n");
        return 0;
    }

    for (i = 0; i < (int) countof(tests); i++) {
        Jbig2HuffmanTable *table;
        Jbig2HuffmanState *hs;
        test_stream_t st;
        int32_t code;
        bool oob;
        size_t j;

        st.ws.get_next_word = test_get_word2;
        st.h = &tests[i];
        printf("testing Standard Huffman table %s: ", st.h->name);
        table = jbig2_build_huffman_table(ctx, st.h->params);
        if (table == NULL) {
            fprintf(stderr, "jbig2_build_huffman_table() returned NULL!\n");
            jbig2_ctx_free(ctx);
            return 0;
        }
        /* jbig2_dump_huffman_table(table); */
        hs = jbig2_huffman_new(ctx, &st.ws);
        if (hs == NULL) {
            fprintf(stderr, "jbig2_huffman_new() returned NULL!\n");
            jbig2_release_huffman_table(ctx, table);
            jbig2_ctx_free(ctx);
            return 0;
        }
        for (j = 0; j < st.h->output_len; j++) {
            printf("%d...", st.h->output[j]);
            code = jbig2_huffman_get(hs, table, &oob);
            if (code == st.h->output[j] && !oob) {
                printf("ok, ");
            } else {
                int need_comma = 0;

                printf("NG(");
                if (code != st.h->output[j]) {
                    printf("%d", code);
                    need_comma = 1;
                }
                if (oob) {
                    if (need_comma)
                        printf(",");
                    printf("OOB");
                }
                printf("), ");
            }
        }
        if (st.h->params->HTOOB) {
            printf("OOB...");
            code = jbig2_huffman_get(hs, table, &oob);
            if (oob) {
                printf("ok");
            } else {
                printf("NG(%d)", code);
            }
        }
        printf("\n");
        jbig2_huffman_free(ctx, hs);
        jbig2_release_huffman_table(ctx, table);
    }

    jbig2_ctx_free(ctx);

    if (i == countof(tests))
        success = 1;

    return success;
}

int
main(int argc, char **argv)
{
    return test1() && test2() ? 0 : 1;
}
#endif