aboutsummaryrefslogtreecommitdiff
path: root/archival/bunzip2.c
blob: da9808e82bf9ca46d3a3a80351e220923c313c23 (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
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
/* Modified for busybox by Glenn McGrath <bug1@optushome.com.au> */
/*--
  This file is a part of bzip2 and/or libbzip2, a program and
  library for lossless, block-sorting data compression.

  Copyright (C) 1996-2000 Julian R Seward.  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

  2. The origin of this software must not be misrepresented; you must 
     not claim that you wrote the original software.  If you use this 
     software in a product, an acknowledgment in the product 
     documentation would be appreciated but is not required.

  3. Altered source versions must be plainly marked as such, and must
     not be misrepresented as being the original software.

  4. The name of the author may not be used to endorse or promote 
     products derived from this software without specific prior written 
     permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

  Julian Seward, Cambridge, UK.
  jseward@acm.org
  bzip2/libbzip2 version 1.0 of 21 March 2000

  This program is based on (at least) the work of:
     Mike Burrows
     David Wheeler
     Peter Fenwick
     Alistair Moffat
     Radford Neal
     Ian H. Witten
     Robert Sedgewick
     Jon L. Bentley

  For more information on these sources, see the manual.
--*/

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

//#define TRUE 1
//#define FALSE 0

#define MTFA_SIZE 4096
#define MTFL_SIZE 16
#define BZ_N_GROUPS 6
#define BZ_G_SIZE   50
#define BZ_MAX_ALPHA_SIZE 258

#define BZ_OK                0
#define BZ_RUN_OK            1
#define BZ_FLUSH_OK          2
#define BZ_FINISH_OK         3
#define BZ_STREAM_END        4
#define BZ_SEQUENCE_ERROR    (-1)
#define BZ_PARAM_ERROR       (-2)
#define BZ_MEM_ERROR         (-3)
#define BZ_DATA_ERROR        (-4)
#define BZ_DATA_ERROR_MAGIC  (-5)
#define BZ_IO_ERROR          (-6)
#define BZ_UNEXPECTED_EOF    (-7)
#define BZ_OUTBUFF_FULL      (-8)
#define BZ_CONFIG_ERROR      (-9)

#define BZ_RUNA 0
#define BZ_RUNB 1

#define BZ_MAX_UNUSED 5000
#define FILE_NAME_LEN 1034
/*-- states for decompression. --*/

#define BZ_X_IDLE        1
#define BZ_X_OUTPUT      2

#define BZ_X_MAGIC_1     10
#define BZ_X_MAGIC_2     11
#define BZ_X_MAGIC_3     12
#define BZ_X_MAGIC_4     13
#define BZ_X_BLKHDR_1    14
#define BZ_X_BLKHDR_2    15
#define BZ_X_BLKHDR_3    16
#define BZ_X_BLKHDR_4    17
#define BZ_X_BLKHDR_5    18
#define BZ_X_BLKHDR_6    19
#define BZ_X_BCRC_1      20
#define BZ_X_BCRC_2      21
#define BZ_X_BCRC_3      22
#define BZ_X_BCRC_4      23
#define BZ_X_RANDBIT     24
#define BZ_X_ORIGPTR_1   25
#define BZ_X_ORIGPTR_2   26
#define BZ_X_ORIGPTR_3   27
#define BZ_X_MAPPING_1   28
#define BZ_X_MAPPING_2   29
#define BZ_X_SELECTOR_1  30
#define BZ_X_SELECTOR_2  31
#define BZ_X_SELECTOR_3  32
#define BZ_X_CODING_1    33
#define BZ_X_CODING_2    34
#define BZ_X_CODING_3    35
#define BZ_X_MTF_1       36
#define BZ_X_MTF_2       37
#define BZ_X_MTF_3       38
#define BZ_X_MTF_4       39
#define BZ_X_MTF_5       40
#define BZ_X_MTF_6       41
#define BZ_X_ENDHDR_2    42
#define BZ_X_ENDHDR_3    43
#define BZ_X_ENDHDR_4    44
#define BZ_X_ENDHDR_5    45
#define BZ_X_ENDHDR_6    46
#define BZ_X_CCRC_1      47
#define BZ_X_CCRC_2      48
#define BZ_X_CCRC_3      49
#define BZ_X_CCRC_4      50

#define BZ_MAX_CODE_LEN    23
#define BZ_VERSION  "1.0.1, 23-June-2000"
#define OM_TEST          3
#define SM_F2F 3

typedef struct {
	char *next_in;
	unsigned int avail_in;
	unsigned int total_in_lo32;
	unsigned int total_in_hi32;

	char *next_out;
	unsigned int avail_out;
	unsigned int total_out_lo32;
	unsigned int total_out_hi32;

	void *state;

	void *(*bzalloc)(void *,int,int);
	void (*bzfree)(void *,void *);
	void *opaque;
} bz_stream;

typedef struct {
	bz_stream	strm;
	FILE	*handle;
    unsigned char	initialisedOk;
	unsigned char	writing;
	char	buf[BZ_MAX_UNUSED];
	int		lastErr;
	int		bufN;
} bzFile;

/*-- Structure holding all the decompression-side stuff. --*/
typedef struct {
	/* pointer back to the struct bz_stream */
	bz_stream* strm;

	/* state indicator for this stream */
	int	state;

	/* for doing the final run-length decoding */
	unsigned char    state_out_ch;
	int    state_out_len;
	unsigned char     blockRandomised;
	int rNToGo;
	int rTPos;

	/* the buffer for bit stream reading */
	unsigned int   bsBuff;
	int    bsLive;

	/* misc administratium */
	int    blockSize100k;
	unsigned char     smallDecompress;
	int    currBlockNo;
	int    verbosity;

	/* for undoing the Burrows-Wheeler transform */
	int    origPtr;
	unsigned int   tPos;
	int    k0;
	int    unzftab[256];
	int    nblock_used;
	int    cftab[257];
	int    cftabCopy[257];

	/* for undoing the Burrows-Wheeler transform (FAST) */
	unsigned int *tt;

	/* for undoing the Burrows-Wheeler transform (SMALL) */
	unsigned short *ll16;
	unsigned char *ll4;

	/* stored and calculated CRCs */
	unsigned int   storedBlockCRC;
	unsigned int   storedCombinedCRC;
	unsigned int   calculatedBlockCRC;
	unsigned int   calculatedCombinedCRC;

	/* map of bytes used in block */
	int    nInUse;
	unsigned char     inUse[256];
	unsigned char     inUse16[16];
	unsigned char    seqToUnseq[256];

	/* for decoding the MTF values */
	unsigned char    mtfa   [MTFA_SIZE];
	unsigned char    selector   [2 + (900000 / BZ_G_SIZE)];
	unsigned char    selectorMtf[2 + (900000 / BZ_G_SIZE)];
	unsigned char    len  [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
	int    mtfbase[256 / MTFL_SIZE];

	int    limit  [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
	int    base   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
	int    perm   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
	int    minLens[BZ_N_GROUPS];

	/* save area for scalars in the main decompress code */
	int    save_i;
	int    save_j;
	int    save_t;
	int    save_alphaSize;
	int    save_nGroups;
	int    save_nSelectors;
	int    save_EOB;
	int    save_groupNo;
	int    save_groupPos;
	int    save_nextSym;
	int    save_nblockMAX;
	int    save_nblock;
	int    save_es;
	int    save_N;
	int    save_curr;
	int    save_zt;
	int    save_zn; 
	int    save_zvec;
	int    save_zj;
	int    save_gSel;
	int    save_gMinlen;
	int	*save_gLimit;
	int	*save_gBase;
	int	*save_gPerm;
} DState;

int BZ2_rNums[512];
//int	verbosity_level;
unsigned char smallMode;
unsigned char noisy;
char *progName;
char inName[FILE_NAME_LEN];
char outName[FILE_NAME_LEN];
int srcMode;
int opMode;
unsigned char deleteOutputOnInterrupt;
FILE *outputHandleJustInCase;
int numFileNames;
int numFilesProcessed;
int exitValue;

unsigned int BZ2_crc32Table[256] = {

   /*-- Ugly, innit? --*/

   0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L,
   0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L,
   0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L,
   0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL,
   0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L,
   0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L,
   0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L,
   0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL,
   0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L,
   0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L,
   0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L,
   0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL,
   0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L,
   0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L,
   0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L,
   0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL,
   0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL,
   0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L,
   0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L,
   0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL,
   0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL,
   0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L,
   0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L,
   0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL,
   0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL,
   0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L,
   0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L,
   0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL,
   0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL,
   0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L,
   0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L,
   0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL,
   0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L,
   0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL,
   0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL,
   0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L,
   0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L,
   0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL,
   0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL,
   0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L,
   0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L,
   0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL,
   0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL,
   0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L,
   0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L,
   0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL,
   0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL,
   0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L,
   0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L,
   0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL,
   0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L,
   0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L,
   0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L,
   0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL,
   0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L,
   0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L,
   0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L,
   0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL,
   0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L,
   0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L,
   0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L,
   0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL,
   0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L,
   0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L
};

void bz_rand_udp_mask(DState *s)
{
	if (s->rNToGo == 0) {
		s->rNToGo = BZ2_rNums[s->rTPos];
		s->rTPos++;
		if (s->rTPos == 512) {
			s->rTPos = 0;
		}
	}
	s->rNToGo--;
}

static unsigned char myfeof(FILE *f)
{
	int c = fgetc(f);
	if (c == EOF) {
		return(TRUE);
	}
	ungetc(c, f);
	return(FALSE);
}

static void cleanUpAndFail(int ec)
{
	int retVal;

	if ((srcMode == SM_F2F) && (opMode != OM_TEST) && deleteOutputOnInterrupt) {
		if (noisy) {
			error_msg("%s: Deleting output file %s, if it exists.\n", progName, outName);
		}
		if (outputHandleJustInCase != NULL) {
			fclose(outputHandleJustInCase);
		}
		retVal = remove(outName);
		if (retVal != 0) {
			error_msg("%s: WARNING: deletion of output file (apparently) failed.\n", progName);
		}
	}
	if (noisy && (numFileNames > 0) && (numFilesProcessed < numFileNames)) {
		error_msg("%s: WARNING: some files have not been processed:\n"
			"\t%d specified on command line, %d not processed yet.\n\n",
			progName, numFileNames, numFileNames - numFilesProcessed );
	}

	exit(ec);
}


void panic(char *s)
{
	error_msg("\n%s: PANIC -- internal consistency error:\n"
             "\t%s\n"
             "\tThis is a BUG.  Please report it to me at:\n"
             "\tjseward@acm.org\n",
             progName, s);
	cleanUpAndFail( 3 );
}

void BZ2_hbCreateDecodeTables(int *limit, int *base, int *perm, unsigned char *length, int minLen, int maxLen, int alphaSize )
{
	int pp, i, j, vec;

	pp = 0;
	for (i = minLen; i <= maxLen; i++) {
		for (j = 0; j < alphaSize; j++) {
			if (length[j] == i) {
				perm[pp] = j;
				pp++;
			}
		}
	}

	for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
		base[i] = 0;
	}

	for (i = 0; i < alphaSize; i++) {
		base[length[i]+1]++;
	}

	for (i = 1; i < BZ_MAX_CODE_LEN; i++) {
		base[i] += base[i-1];
	}

	for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
		limit[i] = 0;
	}
	vec = 0;

	for (i = minLen; i <= maxLen; i++) {
		vec += (base[i+1] - base[i]);
		limit[i] = vec-1;
		vec <<= 1;
	}
	for (i = minLen + 1; i <= maxLen; i++) {
		base[i] = ((limit[i-1] + 1) << 1) - base[i];
	}
}

int bz_get_small(DState *s)
{
	int cccc;
	int nb, na, mid;
	nb = 0;
	na = 256;
	do {
		mid = (nb + na) >> 1;
		if (s->tPos >= s->cftab[mid]) {
			nb = mid;
		} else {
			na = mid;
		}
	}
	while (na - nb != 1);
	cccc = nb;
	s->tPos = (((unsigned int)s->ll16[s->tPos]) |
		(((((unsigned int)(s->ll4[(s->tPos) >> 1])) >>
		(((s->tPos) << 2) & 0x4)) & 0xF) << 16));
	return(cccc);
}

void assert_h(int errcode)
{
	error_msg_and_die("\n\nbzip2/libbzip2: internal error number %d.\n"
		"This is a bug in bzip2/libbzip2, %s.\n"
		"Please report it to me at: jseward@acm.org.  If this happened\n"
		"when you were using some program which uses libbzip2 as a\n"
		"component, you should also report this bug to the author(s)\n"
		"of that program.  Please make an effort to report this bug;\n"
		"timely and accurate bug reports eventually lead to higher\n"
		"quality software.  Thanks.  Julian Seward, 21 March 2000.\n\n",
		errcode, BZ_VERSION);
}

static int get_bits(DState *s, int *vvv, char nnn)
{
	while (1) {
		if (s->bsLive >= nnn) {
			*vvv = (s->bsBuff >> (s->bsLive-nnn)) & ((1 << nnn)-1);
			s->bsLive -= nnn;
			break;
		}
		if (s->strm->avail_in == 0) {
			return(FALSE);
		}
		s->bsBuff = (s->bsBuff << 8) | ((unsigned int) (*((unsigned char*)(s->strm->next_in))));
		s->bsLive += 8;
		s->strm->next_in++;
		s->strm->avail_in--;
		s->strm->total_in_lo32++;
		if (s->strm->total_in_lo32 == 0) {
			s->strm->total_in_hi32++;
		}
	}
	return(TRUE);
}

int bz_get_fast(DState *s)
{
	int cccc;
	s->tPos = s->tt[s->tPos];
	cccc = (unsigned char)(s->tPos & 0xff);
	s->tPos >>= 8;
	return(cccc);
}

/*---------------------------------------------------*/
int BZ2_decompress(DState *s)
{
	int uc = 0;
	int	retVal;
	int	minLen,	maxLen;
	bz_stream	*strm = s->strm;

	/* stuff that needs to be saved/restored */
	int  i;
	int  j;
	int  t;
	int  alphaSize;
	int  nGroups;
	int  nSelectors;
	int  EOB;
	int  groupNo;
	int  groupPos;
	int  nextSym;
	int  nblockMAX;
	int  nblock;
	int  es;
	int  N;
	int  curr;
	int  zt;
	int  zn; 
	int  zvec;
	int  zj;
	int  gSel;
	int  gMinlen;
	int *gLimit;
	int *gBase;
	int *gPerm;
	int switch_val;

	int get_mtf_val_init(void)
	{
		if (groupPos == 0) {
			groupNo++;
			if (groupNo >= nSelectors) {
				retVal = BZ_DATA_ERROR;
				return(FALSE);
			}
			groupPos = BZ_G_SIZE;
			gSel = s->selector[groupNo];
			gMinlen = s->minLens[gSel];
			gLimit = &(s->limit[gSel][0]);
			gPerm = &(s->perm[gSel][0]);
			gBase = &(s->base[gSel][0]);
		}
		groupPos--;
		zn = gMinlen;
		return(TRUE);
	}

	if (s->state == BZ_X_MAGIC_1) {
		/*initialise the save area*/
		s->save_i           = 0;
		s->save_j           = 0;
		s->save_t           = 0;
		s->save_alphaSize   = 0;
		s->save_nGroups     = 0;
		s->save_nSelectors  = 0;
		s->save_EOB         = 0;
		s->save_groupNo     = 0;
		s->save_groupPos    = 0;
		s->save_nextSym     = 0;
		s->save_nblockMAX   = 0;
		s->save_nblock      = 0;
		s->save_es          = 0;
		s->save_N           = 0;
		s->save_curr        = 0;
		s->save_zt          = 0;
		s->save_zn          = 0;
		s->save_zvec        = 0;
		s->save_zj          = 0;
		s->save_gSel        = 0;
		s->save_gMinlen     = 0;
		s->save_gLimit      = NULL;
		s->save_gBase       = NULL;
		s->save_gPerm       = NULL;
	}

	/*restore from the save area*/
	i           = s->save_i;
	j           = s->save_j;
	t           = s->save_t;
	alphaSize   = s->save_alphaSize;
	nGroups     = s->save_nGroups;
	nSelectors  = s->save_nSelectors;
	EOB         = s->save_EOB;
	groupNo     = s->save_groupNo;
	groupPos    = s->save_groupPos;
	nextSym     = s->save_nextSym;
	nblockMAX   = s->save_nblockMAX;
	nblock      = s->save_nblock;
	es          = s->save_es;
	N           = s->save_N;
	curr        = s->save_curr;
	zt          = s->save_zt;
	zn          = s->save_zn; 
	zvec        = s->save_zvec;
	zj          = s->save_zj;
	gSel        = s->save_gSel;
	gMinlen     = s->save_gMinlen;
	gLimit      = s->save_gLimit;
	gBase       = s->save_gBase;
	gPerm       = s->save_gPerm;

	retVal = BZ_OK;
	switch_val = s->state;
	switch (switch_val) {
		case BZ_X_MAGIC_1:
			s->state = BZ_X_MAGIC_1;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 'B') {
				retVal = BZ_DATA_ERROR_MAGIC;
				goto save_state_and_return;
			}

		case BZ_X_MAGIC_2:
			s->state = BZ_X_MAGIC_2;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 'Z') {
				retVal = BZ_DATA_ERROR_MAGIC;
				goto save_state_and_return;
			}

		case BZ_X_MAGIC_3:
			s->state = BZ_X_MAGIC_3;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 'h') {
				retVal = BZ_DATA_ERROR_MAGIC;
				goto save_state_and_return;
			}

		case BZ_X_MAGIC_4:
			s->state = BZ_X_MAGIC_4;
			if (get_bits(s, &s->blockSize100k, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if ((s->blockSize100k < '1') || (s->blockSize100k > '9')) {
				retVal = BZ_DATA_ERROR_MAGIC;
				goto save_state_and_return;
			}
			s->blockSize100k -= '0';

			if (s->smallDecompress) {
				s->ll16 = (strm->bzalloc)(strm->opaque, s->blockSize100k * 100000 * sizeof(unsigned short), 1);
				s->ll4 = (strm->bzalloc)(strm->opaque, ((1 + s->blockSize100k * 100000) >> 1) * sizeof(unsigned char), 1);

				if (s->ll16 == NULL || s->ll4 == NULL) {
					retVal = BZ_MEM_ERROR;
					goto save_state_and_return;
				}
			} else {
				s->tt = (strm->bzalloc)(strm->opaque, s->blockSize100k * 100000 * sizeof(int), 1);
				if (s->tt == NULL) {
					retVal = BZ_MEM_ERROR;
					goto save_state_and_return;
				}
			}

		case BZ_X_BLKHDR_1:
			s->state = BZ_X_BLKHDR_1;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}

			if (uc == 0x17) {
				goto endhdr_2;
			}
			if (uc != 0x31) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_BLKHDR_2:
			s->state = BZ_X_BLKHDR_2;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x41) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_BLKHDR_3:
			s->state = BZ_X_BLKHDR_3;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x59) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_BLKHDR_4:
			s->state = BZ_X_BLKHDR_4;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x26) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_BLKHDR_5:
			s->state = BZ_X_BLKHDR_5;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x53) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_BLKHDR_6:
			s->state = BZ_X_BLKHDR_6;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x59) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		s->currBlockNo++;
		if (s->verbosity >= 2) {
			error_msg("\n    [%d: huff+mtf ", s->currBlockNo);
		}
		s->storedBlockCRC = 0;

		case BZ_X_BCRC_1:
			s->state = BZ_X_BCRC_1;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);

		case BZ_X_BCRC_2:
			s->state = BZ_X_BCRC_2;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);

		case BZ_X_BCRC_3:
			s->state = BZ_X_BCRC_3;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);

		case BZ_X_BCRC_4:
			s->state = BZ_X_BCRC_4;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);

		case BZ_X_RANDBIT:
			s->state = BZ_X_RANDBIT;
			{
				int tmp = s->blockRandomised;
				const int ret = get_bits(s, &tmp, 1);
				s->blockRandomised = tmp;
				if (ret == FALSE) {
					retVal = BZ_OK;
					goto save_state_and_return;
				}
			}

			s->origPtr = 0;

		case BZ_X_ORIGPTR_1:
			s->state = BZ_X_ORIGPTR_1;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->origPtr = (s->origPtr << 8) | ((int)uc);

		case BZ_X_ORIGPTR_2:
			s->state = BZ_X_ORIGPTR_2;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->origPtr = (s->origPtr << 8) | ((int)uc);

		case BZ_X_ORIGPTR_3:
			s->state = BZ_X_ORIGPTR_3;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->origPtr = (s->origPtr << 8) | ((int)uc);

			if (s->origPtr < 0) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}
			if (s->origPtr > 10 + 100000*s->blockSize100k) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

			/*--- Receive the mapping table ---*/
		case BZ_X_MAPPING_1:
			for (i = 0; i < 16; i++) {
				s->state = BZ_X_MAPPING_1;
				if (get_bits(s, &uc, 1) == FALSE) {
					retVal = BZ_OK;
					goto save_state_and_return;
				}
				if (uc == 1) {
					s->inUse16[i] = TRUE;
				} else {
					s->inUse16[i] = FALSE;
				}
			}

			for (i = 0; i < 256; i++) {
				s->inUse[i] = FALSE;
			}

			for (i = 0; i < 16; i++) {
				if (s->inUse16[i]) {
					for (j = 0; j < 16; j++) {
					case BZ_X_MAPPING_2:
						s->state = BZ_X_MAPPING_2;
						if (get_bits(s, &uc, 1) == FALSE) {
							retVal = BZ_OK;
							goto save_state_and_return;
						}
						if (uc == 1) {
							s->inUse[i * 16 + j] = TRUE;
						}
					}
				}
			}

			s->nInUse = 0;
			for (i = 0; i < 256; i++) {
				if (s->inUse[i]) {
					s->seqToUnseq[s->nInUse] = i;
					s->nInUse++;
				}
			}
			if (s->nInUse == 0) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}
			alphaSize = s->nInUse+2;

		/*--- Now the selectors ---*/
		case BZ_X_SELECTOR_1:
			s->state = BZ_X_SELECTOR_1;
			if (get_bits(s, &nGroups, 3) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (nGroups < 2 || nGroups > 6) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_SELECTOR_2:
			s->state = BZ_X_SELECTOR_2;
			if (get_bits(s, &nSelectors, 15) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (nSelectors < 1) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}



			for (i = 0; i < nSelectors; i++) {
				j = 0;
				while (1) {
					case BZ_X_SELECTOR_3:
					s->state = BZ_X_SELECTOR_3;
					if (get_bits(s, &uc, 1) == FALSE) {
						retVal = BZ_OK;
						goto save_state_and_return;
					}
					if (uc == 0) {
						break;
					}
					j++;
					if (j >= nGroups) {
						retVal = BZ_DATA_ERROR;
						goto save_state_and_return;
					}
				}
				s->selectorMtf[i] = j;
			}

			/*--- Undo the MTF values for the selectors. ---*/
			{
				unsigned char pos[BZ_N_GROUPS], tmp, v;
				for (v = 0; v < nGroups; v++) {
					pos[v] = v;
				}
				for (i = 0; i < nSelectors; i++) {
					v = s->selectorMtf[i];
					tmp = pos[v];
					while (v > 0) {
						pos[v] = pos[v-1];
						v--;
					}
					pos[0] = tmp;
					s->selector[i] = tmp;
				}
			}

			/*--- Now the coding tables ---*/
			for (t = 0; t < nGroups; t++) {
			case BZ_X_CODING_1:
				s->state = BZ_X_CODING_1;
				if (get_bits(s, &curr, 5) == FALSE) {
					retVal = BZ_OK;
					goto save_state_and_return;
				}
			for (i = 0; i < alphaSize; i++) {
				while (TRUE) {
					if (curr < 1 || curr > 20) {
						retVal = BZ_DATA_ERROR;
						goto save_state_and_return;
					}

					case BZ_X_CODING_2:
						s->state = BZ_X_CODING_2;
						if (get_bits(s, &uc, 1) == FALSE) {
							retVal = BZ_OK;
							goto save_state_and_return;
						}
						if (uc == 0) {
							break;
						}

					case BZ_X_CODING_3:
						s->state = BZ_X_CODING_3;
						if (get_bits(s, &uc, 1) == FALSE) {
							retVal = BZ_OK;
							goto save_state_and_return;
						}
						if (uc == 0) {
							curr++;
						} else {
							curr--;
						}
				}
				s->len[t][i] = curr;
			}
		}

		/*--- Create the Huffman decoding tables ---*/
		for (t = 0; t < nGroups; t++) {
			minLen = 32;
			maxLen = 0;
			for (i = 0; i < alphaSize; i++) {
				if (s->len[t][i] > maxLen) {
					maxLen = s->len[t][i];
				}
				if (s->len[t][i] < minLen) {
					minLen = s->len[t][i];
				}
			}

			BZ2_hbCreateDecodeTables ( 
				&(s->limit[t][0]), 
				&(s->base[t][0]), 
				&(s->perm[t][0]), 
				&(s->len[t][0]),
				minLen, maxLen, alphaSize
				);


			s->minLens[t] = minLen;
		}

		/*--- Now the MTF values ---*/

		EOB      = s->nInUse+1;
		nblockMAX = 100000 * s->blockSize100k;
		groupNo  = -1;
		groupPos = 0;

		for (i = 0; i <= 255; i++) {
			s->unzftab[i] = 0;
		}
		/*-- MTF init --*/
		{
			int ii, jj, kk;
			kk = MTFA_SIZE-1;
			for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
				for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
					s->mtfa[kk] = (unsigned char)(ii * MTFL_SIZE + jj);
					kk--;
				}
				s->mtfbase[ii] = kk + 1;
			}
		}
		/*-- end MTF init --*/

		nblock = 0;

		if (get_mtf_val_init() == FALSE) {
			goto save_state_and_return;
		}
		case BZ_X_MTF_1:
			s->state = BZ_X_MTF_1;
			if (get_bits(s, &zvec, zn) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			while (1) {
				if (zn > 20 /* the longest code */) {
					retVal = BZ_DATA_ERROR;
					goto save_state_and_return;
				}
				if (zvec <= gLimit[zn]) {
					break;
				}
				zn++;

				case BZ_X_MTF_2:
					s->state = BZ_X_MTF_2;
					if (get_bits(s, &zj, 1) == FALSE) {
						retVal = BZ_OK;
						goto save_state_and_return;
					}
					zvec = (zvec << 1) | zj;
			}
			if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}
			nextSym = gPerm[zvec - gBase[zn]];

		while (1) {
			if (nextSym == EOB) {
				break;
			}

		if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
			es = -1;
			N = 1;
			do {
				if (nextSym == BZ_RUNA) {
					es = es + (0+1) * N;
				} else {
					if (nextSym == BZ_RUNB) {
						es = es + (1+1) * N;
					}
				}
				N = N * 2;
				if (get_mtf_val_init() == FALSE) {
					goto save_state_and_return;
				}
				case BZ_X_MTF_3:
					s->state = BZ_X_MTF_3;
					if (get_bits(s, &zvec, zn) == FALSE) {
						retVal = BZ_OK;
						goto save_state_and_return;
					}
					while (1) {
						if (zn > 20 /* the longest code */) {
							retVal = BZ_DATA_ERROR;
							goto save_state_and_return;
						}
						if (zvec <= gLimit[zn]) {
							break;
						}
						zn++;

						case BZ_X_MTF_4:
							s->state = BZ_X_MTF_4;
							if (get_bits(s, &zj, 1) == FALSE) {
								retVal = BZ_OK;
								goto save_state_and_return;
							}
							zvec = (zvec << 1) | zj;
					}
					if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
						retVal = BZ_DATA_ERROR;
						goto save_state_and_return;

					}
					nextSym = gPerm[zvec - gBase[zn]];
			}
			while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);

			es++;
			uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
			s->unzftab[uc] += es;

			if (s->smallDecompress) {
				while (es > 0) {
					if (nblock >= nblockMAX) {
						retVal = BZ_DATA_ERROR;
						goto save_state_and_return;
					}
					s->ll16[nblock] = (unsigned short)uc;
					nblock++;
					es--;
				}
			} else {
				while (es > 0) {
					if (nblock >= nblockMAX) {
						retVal = BZ_DATA_ERROR;
						goto save_state_and_return;
					}
					s->tt[nblock] = (unsigned int)uc;
					nblock++;
					es--;
				}
			}
			continue;
		} else {
			if (nblock >= nblockMAX) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}
			/*-- uc = MTF ( nextSym-1 ) --*/
			{
				int ii, jj, kk, pp, lno, off;
				unsigned int nn;
				nn = (unsigned int)(nextSym - 1);

				if (nn < MTFL_SIZE) {
					/* avoid general-case expense */
					pp = s->mtfbase[0];
					uc = s->mtfa[pp+nn];
					while (nn > 3) {
						int z = pp+nn;
						s->mtfa[(z)  ] = s->mtfa[(z)-1];
						s->mtfa[(z)-1] = s->mtfa[(z)-2];
						s->mtfa[(z)-2] = s->mtfa[(z)-3];
						s->mtfa[(z)-3] = s->mtfa[(z)-4];
						nn -= 4;
					}
					while (nn > 0) { 
						s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--; 
					}
					s->mtfa[pp] = uc;
				} else { 
					/* general case */
					lno = nn / MTFL_SIZE;
					off = nn % MTFL_SIZE;
					pp = s->mtfbase[lno] + off;
					uc = s->mtfa[pp];
					while (pp > s->mtfbase[lno]) { 
						s->mtfa[pp] = s->mtfa[pp-1];
						pp--; 
					}
					s->mtfbase[lno]++;
					while (lno > 0) {
						s->mtfbase[lno]--;
						s->mtfa[s->mtfbase[lno]] = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
						lno--;
					}
					s->mtfbase[0]--;
					s->mtfa[s->mtfbase[0]] = uc;
					if (s->mtfbase[0] == 0) {
						kk = MTFA_SIZE-1;
						for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
							for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
								s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
								kk--;
							}
							s->mtfbase[ii] = kk + 1;
						}
					}
				}
			}
			/*-- end uc = MTF ( nextSym-1 ) --*/

			s->unzftab[s->seqToUnseq[uc]]++;
            if (s->smallDecompress) {
				s->ll16[nblock] = (unsigned short)(s->seqToUnseq[uc]);
			} else {
				s->tt[nblock]   = (unsigned int)(s->seqToUnseq[uc]);
			}
			nblock++;

			if (get_mtf_val_init() == FALSE) {
				goto save_state_and_return;
			}
			case BZ_X_MTF_5:
				s->state = BZ_X_MTF_5;
				if (get_bits(s, &zvec, zn) == FALSE) {
					retVal = BZ_OK;
					goto save_state_and_return;
				}
				while (1) {
					if (zn > 20 /* the longest code */) {
						retVal = BZ_DATA_ERROR;
						goto save_state_and_return;
					}
					if (zvec <= gLimit[zn]) {
						break;
					}
					zn++;

					case BZ_X_MTF_6:
						s->state = BZ_X_MTF_6;
						if (get_bits(s, &zj, 1) == FALSE) {
							retVal = BZ_OK;
							goto save_state_and_return;
						}
						zvec = (zvec << 1) | zj;
				}
			if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}
			nextSym = gPerm[zvec - gBase[zn]];
			continue;
		}
	}

	/* Now we know what nblock is, we can do a better sanity
		check on s->origPtr.
	*/
	if (s->origPtr < 0 || s->origPtr >= nblock) {
		retVal = BZ_DATA_ERROR;
		goto save_state_and_return;
	}
	s->state_out_len = 0;
	s->state_out_ch  = 0;
	s->calculatedBlockCRC = 0xffffffffL;
	s->state = BZ_X_OUTPUT;
	if (s->verbosity >= 2) {
		error_msg("rt+rld");
	}

	/*-- Set up cftab to facilitate generation of T^(-1) --*/
	s->cftab[0] = 0;
	for (i = 1; i <= 256; i++) {
		s->cftab[i] = s->unzftab[i-1];
	}
	for (i = 1; i <= 256; i++) {
		s->cftab[i] += s->cftab[i-1];
	}

	if (s->smallDecompress) {

		/*-- Make a copy of cftab, used in generation of T --*/
		for (i = 0; i <= 256; i++) {
			s->cftabCopy[i] = s->cftab[i];
		}

		/*-- compute the T vector --*/
		for (i = 0; i < nblock; i++) {
			uc = (unsigned char)(s->ll16[i]);
			s->ll16[i] = (unsigned short)(s->cftabCopy[uc] & 0x0000ffff);
			if (((i) & 0x1) == 0) {
				s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (s->cftabCopy[uc] >> 16);
			} else {
				s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((s->cftabCopy[uc] >> 16) << 4);
			}
			s->cftabCopy[uc]++;
		}

		/*-- Compute T^(-1) by pointer reversal on T --*/
		i = s->origPtr;
		j = (((unsigned int)s->ll16[i]) |
			(((((unsigned int)(s->ll4[(i) >> 1])) >>
			(((i) << 2) & 0x4)) & 0xF) << 16));

		do {
			const int tmp = (((unsigned int)s->ll16[j]) |
				(((((unsigned int)(s->ll4[(j) >> 1])) >>
				(((j) << 2) & 0x4)) & 0xF) << 16));

			s->ll16[j] = (unsigned short)(i & 0x0000ffff);
			if (((j) & 0x1) == 0) {
				s->ll4[(j) >> 1] = (s->ll4[(j) >> 1] & 0xf0) | (i >> 16);
			} else {
				s->ll4[(j) >> 1] = (s->ll4[(j) >> 1] & 0x0f) | ((i >> 16) << 4);
			}
			i = j;
			j = tmp;
		}
		while (i != s->origPtr);
			s->tPos = s->origPtr;
			s->nblock_used = 0;
			if (s->blockRandomised) {
				s->rNToGo = 0;
				s->rTPos  = 0;
				s->k0 = bz_get_small(s);
				s->nblock_used++;
				bz_rand_udp_mask(s);
				s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
			} else {
				s->k0 = bz_get_small(s);
				s->nblock_used++;
			}
		} else {
			/*-- compute the T^(-1) vector --*/
			for (i = 0; i < nblock; i++) {
				uc = (unsigned char)(s->tt[i] & 0xff);
				s->tt[s->cftab[uc]] |= (i << 8);
				s->cftab[uc]++;
			}

			s->tPos = s->tt[s->origPtr] >> 8;
			s->nblock_used = 0;
			if (s->blockRandomised) {
				s->rNToGo = 0;
				s->rTPos  = 0;
				s->k0 = bz_get_fast(s);

				s->nblock_used++;
				bz_rand_udp_mask(s);
				s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
			} else {
				s->k0 = bz_get_fast(s);
				s->nblock_used++;
			}
		}

		retVal = BZ_OK;
		goto save_state_and_return;

endhdr_2:
		case BZ_X_ENDHDR_2:
			s->state = BZ_X_ENDHDR_2;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x72) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_ENDHDR_3:
			s->state = BZ_X_ENDHDR_3;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x45) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_ENDHDR_4:
			s->state = BZ_X_ENDHDR_4;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x38) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_ENDHDR_5:
			s->state = BZ_X_ENDHDR_5;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x50) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}

		case BZ_X_ENDHDR_6:
			s->state = BZ_X_ENDHDR_6;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			if (uc != 0x90) {
				retVal = BZ_DATA_ERROR;
				goto save_state_and_return;
			}
			s->storedCombinedCRC = 0;

		case BZ_X_CCRC_1:
			s->state = BZ_X_CCRC_1;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
		case BZ_X_CCRC_2:
			s->state = BZ_X_CCRC_2;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);

		case BZ_X_CCRC_3:
			s->state = BZ_X_CCRC_3;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);

		case BZ_X_CCRC_4:
			s->state = BZ_X_CCRC_4;
			if (get_bits(s, &uc, 8) == FALSE) {
				retVal = BZ_OK;
				goto save_state_and_return;
			}
			s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);

		s->state = BZ_X_IDLE;
		retVal = BZ_STREAM_END;
		goto save_state_and_return;

default:
		printf("switch val is %d\n", switch_val);
		assert_h(4001);
	}

	assert_h(4002);

save_state_and_return:
	s->save_i           = i;
	s->save_j           = j;
	s->save_t           = t;
	s->save_alphaSize   = alphaSize;
	s->save_nGroups     = nGroups;
	s->save_nSelectors  = nSelectors;
	s->save_EOB         = EOB;
	s->save_groupNo     = groupNo;
	s->save_groupPos    = groupPos;
	s->save_nextSym     = nextSym;
	s->save_nblockMAX   = nblockMAX;
	s->save_nblock      = nblock;
	s->save_es          = es;
	s->save_N           = N;
	s->save_curr        = curr;
	s->save_zt          = zt;
	s->save_zn          = zn;
	s->save_zvec        = zvec;
	s->save_zj          = zj;
	s->save_gSel        = gSel;
	s->save_gMinlen     = gMinlen;
	s->save_gLimit      = gLimit;
	s->save_gBase       = gBase;
	s->save_gPerm       = gPerm;

	return retVal;   
}

static void *default_bzalloc(void *opaque, int items, int size)
{
	void *v = xmalloc(items *size);
	return v;
}

static void default_bzfree(void *opaque, void *addr)
{
	if (addr != NULL) {
		free(addr);
	}
}

//int BZ2_bzDecompressInit(bz_stream* strm, int verbosity_level, int small)
int BZ2_bzDecompressInit(bz_stream* strm, int small)
{
	DState* s;

	if (sizeof(int) != 4) {
		return BZ_CONFIG_ERROR;
	}
	if (sizeof(short) != 2) {
		return BZ_CONFIG_ERROR;
	}
	if (sizeof(char) != 1) {
		return BZ_CONFIG_ERROR;
	}
	if (strm == NULL) {
		return BZ_PARAM_ERROR;
	}
	if (small != 0 && small != 1) {
		return BZ_PARAM_ERROR;
	}
//	if (verbosity_level < 0 || verbosity_level > 4) {
//		return BZ_PARAM_ERROR;
//	}
	if (strm->bzalloc == NULL) {
		strm->bzalloc = default_bzalloc;
	}
	if (strm->bzfree == NULL) {
		strm->bzfree = default_bzfree;
	}
	s = (strm->bzalloc)(strm->opaque, sizeof(DState), 1);
	if (s == NULL) {
		return BZ_MEM_ERROR;
	}
	s->strm                  = strm;
	strm->state              = s;
	s->state                 = BZ_X_MAGIC_1;
	s->bsLive                = 0;
	s->bsBuff                = 0;
	s->calculatedCombinedCRC = 0;
	strm->total_in_lo32      = 0;
	strm->total_in_hi32      = 0;
	strm->total_out_lo32     = 0;
	strm->total_out_hi32     = 0;
	s->smallDecompress       = (unsigned char)small;
	s->ll4                   = NULL;
	s->ll16                  = NULL;
	s->tt                    = NULL;
	s->currBlockNo           = 0;
//	s->verbosity             = verbosity_level;

	return BZ_OK;
}

void bz_seterr(int eee, int *bzerror, bzFile **bzf)
{
	if (bzerror != NULL) {
		*bzerror = eee;
	}
	if (*bzf != NULL) {
		(*bzf)->lastErr = eee;
	}
}

void BZ2_bzReadClose(int *bzerror, void *b)
{
	bzFile* bzf = (bzFile*)b;

	bz_seterr(BZ_OK, bzerror, &bzf);
	if (bzf == NULL) {
		bz_seterr(BZ_OK, bzerror, &bzf);
		return;
	}

	if (bzf->writing) {
		bz_seterr(BZ_SEQUENCE_ERROR, bzerror, &bzf);
		return;
	}

	if (bzf->initialisedOk) {
		bz_stream *strm = &(bzf->strm);
		DState *s;
		if (strm == NULL) {
			return;
		}
		s = strm->state;
		if ((s == NULL) || (s->strm != strm)) {
			return;
		}
		if (s->tt != NULL) {
			(strm->bzfree)(strm->opaque,(s->tt));
		}
		if (s->ll16 != NULL) {
			(strm->bzfree)(strm->opaque,(s->ll16));
		}
		if (s->ll4 != NULL) {
			(strm->bzfree)(strm->opaque,(s->ll4));
		}
		(strm->bzfree)(strm->opaque,(strm->state));
		strm->state = NULL;
		return;
	}
	free(bzf);
}

static void unRLE_obuf_to_output_FAST(DState *s)
{
	unsigned char k1;

	if (s->blockRandomised) {
		while (1) {
			/* try to finish existing run */
			while (1) {
				if (s->strm->avail_out == 0) {
					return;
				}
				if (s->state_out_len == 0) {
					break;
				}
				*((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
				s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
					BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
					((unsigned char)s->state_out_ch)];
				s->state_out_len--;
				s->strm->next_out++;
				s->strm->avail_out--;
				s->strm->total_out_lo32++;
				if (s->strm->total_out_lo32 == 0) {
					s->strm->total_out_hi32++;
				}
			}
   
			/* can a new run be started? */
			if (s->nblock_used == s->save_nblock+1) {
				return;
			}
			s->state_out_len = 1;
			s->state_out_ch = s->k0;
			k1 = bz_get_fast(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}

			s->state_out_len = 2;
			k1 = bz_get_fast(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}
			s->state_out_len = 3;
			k1 = bz_get_fast(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}

			k1 = bz_get_fast(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			s->state_out_len = ((int)k1) + 4;
			s->k0 = bz_get_fast(s);
			bz_rand_udp_mask(s);
			s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
		}
	} else {
		/* restore */
		unsigned int c_calculatedBlockCRC = s->calculatedBlockCRC;
		unsigned char c_state_out_ch       = s->state_out_ch;
		int c_state_out_len      = s->state_out_len;
		int c_nblock_used        = s->nblock_used;
		int c_k0                 = s->k0;
		unsigned int	*c_tt                 = s->tt;
		unsigned int	c_tPos               = s->tPos;
		char	*cs_next_out          = s->strm->next_out;
		unsigned int  cs_avail_out         = s->strm->avail_out;
		/* end restore */

		unsigned int avail_out_INIT = cs_avail_out;
		int        s_save_nblockPP = s->save_nblock+1;
		unsigned int total_out_lo32_old;

		while (1) {
			/* try to finish existing run */
			if (c_state_out_len > 0) {
				while (TRUE) {
					if (cs_avail_out == 0) {
						goto return_notr;
					}
					if (c_state_out_len == 1) {
						break;
					}
					*((unsigned char *)(cs_next_out)) = c_state_out_ch;
					c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
						BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
						((unsigned char)c_state_out_ch)];
					c_state_out_len--;
					cs_next_out++;
					cs_avail_out--;
				}
s_state_out_len_eq_one:
				{
					if (cs_avail_out == 0) { 
						c_state_out_len = 1;
						goto return_notr;
					}
					*((unsigned char *)(cs_next_out)) = c_state_out_ch;
					c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
						BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
						((unsigned char)c_state_out_ch)];
					cs_next_out++;
					cs_avail_out--;
				}
			}   
			/* can a new run be started? */
			if (c_nblock_used == s_save_nblockPP) {
				c_state_out_len = 0; goto return_notr;
			}
			c_state_out_ch = c_k0;
			c_tPos = c_tt[c_tPos];
			k1 = (unsigned char)(c_tPos & 0xff);
			c_tPos >>= 8;

			c_nblock_used++;

			if (k1 != c_k0) { 
				c_k0 = k1;
				goto s_state_out_len_eq_one; 
			}

			if (c_nblock_used == s_save_nblockPP) {
				goto s_state_out_len_eq_one;
			}

			c_state_out_len = 2;
			c_tPos = c_tt[c_tPos];
			k1 = (unsigned char)(c_tPos & 0xff);
			c_tPos >>= 8;

			c_nblock_used++;
			if (c_nblock_used == s_save_nblockPP) {
				continue;
			}
			if (k1 != c_k0) {
				c_k0 = k1;
				continue;
			}

			c_state_out_len = 3;
			c_tPos = c_tt[c_tPos];
			k1 = (unsigned char)(c_tPos & 0xff);
			c_tPos >>= 8;

			c_nblock_used++;
			if (c_nblock_used == s_save_nblockPP) {
				continue;
			}
			if (k1 != c_k0) {
				c_k0 = k1;
				continue;
			}
   
			c_tPos = c_tt[c_tPos];
			k1 = (unsigned char)(c_tPos & 0xff);
			c_tPos >>= 8;

			c_nblock_used++;
			c_state_out_len = ((int)k1) + 4;

			c_tPos = c_tt[c_tPos];
			c_k0 = (unsigned char)(c_tPos & 0xff);
			c_tPos >>= 8;

			c_nblock_used++;
		}

return_notr:
		total_out_lo32_old = s->strm->total_out_lo32;
		s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
		if (s->strm->total_out_lo32 < total_out_lo32_old) {
			s->strm->total_out_hi32++;
		}

		/* save */
		s->calculatedBlockCRC = c_calculatedBlockCRC;
		s->state_out_ch       = c_state_out_ch;
		s->state_out_len      = c_state_out_len;
		s->nblock_used        = c_nblock_used;
		s->k0                 = c_k0;
		s->tt                 = c_tt;
		s->tPos               = c_tPos;
		s->strm->next_out     = cs_next_out;
		s->strm->avail_out    = cs_avail_out;
		/* end save */
	}
}

static void unRLE_obuf_to_output_SMALL(DState *s)
{
	unsigned char k1;

	if (s->blockRandomised) {
		while (1) {
			/* try to finish existing run */
			while (1) {
				if (s->strm->avail_out == 0) {
					return;
				}
				if (s->state_out_len == 0) {
					break;
				}
				*((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
				s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
					BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
					((unsigned char)s->state_out_ch)];
				s->state_out_len--;
				s->strm->next_out++;
				s->strm->avail_out--;
				s->strm->total_out_lo32++;
				if (s->strm->total_out_lo32 == 0) {
					s->strm->total_out_hi32++;
				}
			}

			/* can a new run be started? */
			if (s->nblock_used == s->save_nblock+1) {
				return;
			}
               
			s->state_out_len = 1;
			s->state_out_ch = s->k0;
			k1 = bz_get_small(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}

			s->state_out_len = 2;
			k1 = bz_get_small(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}

			s->state_out_len = 3;
			k1 = bz_get_small(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}
			k1 = bz_get_small(s);
			bz_rand_udp_mask(s);
			k1 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
			s->state_out_len = ((int)k1) + 4;
			s->k0 = bz_get_small(s);
			bz_rand_udp_mask(s);
			s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
			s->nblock_used++;
		}
	} else {
		while (1) {
			/* try to finish existing run */
			while (1) {
				if (s->strm->avail_out == 0) {
					return;
				}
				if (s->state_out_len == 0) {
					break;
				}
				*((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
				s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
					BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
					((unsigned char)s->state_out_ch)];
				s->state_out_len--;
				s->strm->next_out++;
				s->strm->avail_out--;
				s->strm->total_out_lo32++;
				if (s->strm->total_out_lo32 == 0) {
					s->strm->total_out_hi32++;
				}
			}

			/* can a new run be started? */
			if (s->nblock_used == s->save_nblock+1) {
				return;
			}

			s->state_out_len = 1;
			s->state_out_ch = s->k0;
			k1 = bz_get_small(s);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}

			s->state_out_len = 2;
			k1 = bz_get_small(s);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}

			s->state_out_len = 3;
			k1 = bz_get_small(s);
			s->nblock_used++;
			if (s->nblock_used == s->save_nblock+1) {
				continue;
			}
			if (k1 != s->k0) {
				s->k0 = k1;
				continue;
			}

			k1 = bz_get_small(s);
			s->nblock_used++;
			s->state_out_len = ((int)k1) + 4;
			s->k0 = bz_get_small(s);
			s->nblock_used++;
		}
	}
}

int BZ2_bzDecompress(bz_stream *strm)
{
	DState* s;
	if (strm == NULL) {
		return BZ_PARAM_ERROR;
	}
	s = strm->state;
	if (s == NULL) {
		return BZ_PARAM_ERROR;
	}
	if (s->strm != strm) {
		return BZ_PARAM_ERROR;
	}

	while (1) {
		if (s->state == BZ_X_IDLE) {
			return BZ_SEQUENCE_ERROR;
		}
		if (s->state == BZ_X_OUTPUT) {
			if (s->smallDecompress) {
				unRLE_obuf_to_output_SMALL(s);
			} else {
				unRLE_obuf_to_output_FAST(s);
			}
			if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
				s->calculatedBlockCRC = ~(s->calculatedBlockCRC);
				if (s->verbosity >= 3) {
					error_msg("{0x%x, 0x%x}", s->storedBlockCRC, s->calculatedBlockCRC);
				}
				if (s->verbosity >= 2) {
					error_msg("]");
				}
				if (s->calculatedBlockCRC != s->storedBlockCRC) {
					return BZ_DATA_ERROR;
				}
				s->calculatedCombinedCRC = (s->calculatedCombinedCRC << 1) | (s->calculatedCombinedCRC >> 31);
				s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
				s->state = BZ_X_BLKHDR_1;
			} else {
				return BZ_OK;
			}
		}
		if (s->state >= BZ_X_MAGIC_1) {
			int r = BZ2_decompress(s);
			if (r == BZ_STREAM_END) {
				if (s->verbosity >= 3) {
					error_msg("\n    combined CRCs: stored = 0x%x, computed = 0x%x",
                          s->storedCombinedCRC, s->calculatedCombinedCRC );
				}
				if (s->calculatedCombinedCRC != s->storedCombinedCRC) {
					return BZ_DATA_ERROR;
				}
				return r;
			}
			if (s->state != BZ_X_OUTPUT) {
				return r;
			}
		}
	}

	assert_h(6001);

	return(0);  /*NOTREACHED*/
}

int BZ2_bzRead(int *bzerror, void *b, void *buf, int len)
{
	int n, ret;
	bzFile *bzf = (bzFile*)b;

	bz_seterr(BZ_OK, bzerror, &bzf);

	if (bzf == NULL || buf == NULL || len < 0) {
		bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
		return 0;
	}

	if (bzf->writing) {
		bz_seterr(BZ_SEQUENCE_ERROR, bzerror, &bzf);
		return 0;
	}

	if (len == 0) {
		bz_seterr(BZ_OK, bzerror, &bzf);
		return 0;
	}

	bzf->strm.avail_out = len;
	bzf->strm.next_out = buf;

	while (1) {
		if (ferror(bzf->handle)) {
			bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
			return 0;
		}
		if ((bzf->strm.avail_in == 0) && !myfeof(bzf->handle)) {
			n = fread(bzf->buf, sizeof(unsigned char), BZ_MAX_UNUSED, bzf->handle);
			if (ferror(bzf->handle)) {
				bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
				return 0;
			}
			bzf->bufN = n;
			bzf->strm.avail_in = bzf->bufN;
			bzf->strm.next_in = bzf->buf;
		}

		ret = BZ2_bzDecompress(&(bzf->strm));

		if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) {
			bz_seterr(ret, bzerror, &bzf);
			return 0;
		}

		if ((ret == BZ_OK) && myfeof(bzf->handle) &&
			(bzf->strm.avail_in == 0) && (bzf->strm.avail_out > 0)) {
			bz_seterr(BZ_UNEXPECTED_EOF, bzerror, &bzf);
			return(0);
		}

		if (ret == BZ_STREAM_END) {
			bz_seterr(BZ_STREAM_END, bzerror, &bzf);
			return(len - bzf->strm.avail_out);
		}
		if (bzf->strm.avail_out == 0) {
			bz_seterr(BZ_OK, bzerror, &bzf);
			return(len);
		}
	}
	return(0); /*not reached*/
}

void BZ2_bzReadGetUnused(int *bzerror, void *b, void **unused, int *nUnused)
{
	bzFile *bzf = (bzFile*)b;
	if (bzf == NULL) {
		bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
		return;
	}
	if (bzf->lastErr != BZ_STREAM_END) {
		bz_seterr(BZ_SEQUENCE_ERROR, bzerror, &bzf);
		return;
	}
	if (unused == NULL || nUnused == NULL) {
		bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
		return;
	}

	bz_seterr(BZ_OK, bzerror, &bzf);
	*nUnused = bzf->strm.avail_in;
	*unused = bzf->strm.next_in;
}

void *BZ2_bzReadOpen(int *bzerror, FILE *f, int small, void *unused, int nUnused)
{
	bzFile *bzf = NULL;
	int ret;

	bz_seterr(BZ_OK, bzerror, &bzf);

	if (f == NULL || (small != 0 && small != 1) ||
		(unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)) ||
//		(verbosity_level < 0 || verbosity_level > 4) ||
		(unused == NULL && nUnused != 0)) {
		bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
		return NULL;
	}

	if (ferror(f)) {
		bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
		return NULL;
	}

	bzf = xmalloc(sizeof(bzFile));
	if (bzf == NULL) {
		bz_seterr(BZ_MEM_ERROR, bzerror, &bzf);
		return NULL;
	}
	bz_seterr(BZ_OK, bzerror, &bzf);

	bzf->initialisedOk = FALSE;
	bzf->handle        = f;
	bzf->bufN          = 0;
	bzf->writing       = FALSE;
	bzf->strm.bzalloc  = NULL;
	bzf->strm.bzfree   = NULL;
	bzf->strm.opaque   = NULL;
   
	while (nUnused > 0) {
		bzf->buf[bzf->bufN] = *((unsigned char *)(unused)); bzf->bufN++;
		unused = ((void *)( 1 + ((unsigned char *)(unused))  ));
		nUnused--;
	}

	ret = BZ2_bzDecompressInit(&(bzf->strm), small);
	if (ret != BZ_OK) {
		bz_seterr(ret, bzerror, &bzf);
		free(bzf);
		return NULL;
	}

	bzf->strm.avail_in = bzf->bufN;
	bzf->strm.next_in  = bzf->buf;

	bzf->initialisedOk = TRUE;
	return bzf;   
}

static unsigned char uncompressStream(FILE *zStream, FILE *stream)
{
	unsigned char unused[BZ_MAX_UNUSED];
	unsigned char *unusedTmp;
	unsigned char obuf[5000];
	void *bzf = NULL;
	int bzerr_dummy;
	int bzerr;
	int nread;
	int nUnused;
	int streamNo;
	int ret;
	int i;

	nUnused = 0;
	streamNo = 0;

	if (ferror(stream)) {
		goto errhandler_io;
	}
	if (ferror(zStream)) {
		goto errhandler_io;
	}

	while(1) {
		bzf = BZ2_bzReadOpen(&bzerr, zStream, (int)smallMode, unused, nUnused);
		if (bzf == NULL || bzerr != BZ_OK) {
			goto errhandler;
		}
		streamNo++;

		while (bzerr == BZ_OK) {
			nread = BZ2_bzRead(&bzerr, bzf, obuf, 5000);
			if (bzerr == BZ_DATA_ERROR_MAGIC) {
				goto errhandler;
			}
			if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0) {
				fwrite(obuf, sizeof(unsigned char), nread, stream);
			}
			if (ferror(stream)) {
				goto errhandler_io;
			}
		}
		if (bzerr != BZ_STREAM_END) {
			goto errhandler;
		}
		BZ2_bzReadGetUnused(&bzerr, bzf, (void **)(&unusedTmp), &nUnused);
		if (bzerr != BZ_OK) {
			panic("decompress:bzReadGetUnused");
		}
		for (i = 0; i < nUnused; i++) {
			unused[i] = unusedTmp[i];
		}
		BZ2_bzReadClose(&bzerr, bzf);
		if (bzerr != BZ_OK) {
			panic("decompress:bzReadGetUnused");
		}
		if ((nUnused == 0) && myfeof(zStream)) {
			break;
		}
	}

	if (ferror(zStream)) {
		goto errhandler_io;
	}
	ret = fclose(zStream);
	if (ret == EOF) {
		goto errhandler_io;
	}
	if (ferror(stream)) {
		goto errhandler_io;
	}
	ret = fflush(stream);
	if (ret != 0) {
		goto errhandler_io;
	}
	if (stream != stdout) {
		ret = fclose(stream);
		if (ret == EOF) {
			goto errhandler_io;
		}
	}
//	if (verbosity_level >= 2) {
//		fprintf(stderr,"\n    ");
//	}
	return TRUE;

errhandler:
	BZ2_bzReadClose ( &bzerr_dummy, bzf );
	switch (bzerr) {
		case BZ_CONFIG_ERROR:
			error_msg("bzip2: I'm not configured correctly for this platform!\n"
				"\tI require Int32, Int16 and Char to have sizes\n"
				"\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
				"\tProbably you can fix this by defining them correctly,\n"
				"\tand recompiling.  Bye!\n" );
			exit(3);
		case BZ_IO_ERROR:
errhandler_io:
			error_msg("\n%s: I/O or other error, bailing out.  "
				"Possible reason follows.\n", progName);
			perror(progName);
			cleanUpAndFail(1);
		case BZ_DATA_ERROR:
			error_msg("\n%s: Data integrity error when decompressing.\n", progName);
			cleanUpAndFail(2);
		case BZ_MEM_ERROR:
			error_msg("\n%s: couldn't allocate enough memory\n", progName);
			cleanUpAndFail(1);
		case BZ_UNEXPECTED_EOF:
			error_msg("\n%s: Compressed file ends unexpectedly;\n\t"
				"perhaps it is corrupted?  *Possible* reason follows.\n", progName);
			perror(progName);
			cleanUpAndFail(2);
		case BZ_DATA_ERROR_MAGIC:
			if (zStream != stdin) {
				fclose(zStream);
			}
			if (stream != stdout) {
				fclose(stream);
			}
			if (streamNo == 1) {
				return FALSE;
			} else {
				if (noisy) {
					error_msg("\n%s: %s: trailing garbage after EOF ignored\n", progName, inName );
				}
				return TRUE;       
			}
		default:
			panic ( "decompress:unexpected error" );
	}

	panic("decompress:end");
	return(TRUE); /*notreached*/
}

int bunzip2_main(int argc, char **argv)
{
	FILE *src_stream;
	FILE *dst_stream;
	char *save_name;
	char *save_name_ptr;
	if (argc != 2) {
		show_usage();
	}
	src_stream = xfopen(argv[1], "r");
	save_name = xstrdup(argv[1]);
	save_name_ptr = strrchr(save_name, '.');
	if (save_name_ptr == NULL) {
		return(FALSE);
	}
	if (strcmp(save_name_ptr, ".bz2") != 0) {
		error_msg("Invalid extension, expected .bz2");
	}
	*save_name_ptr = '\0';	
	dst_stream = xfopen(save_name, "w");
	uncompressStream(src_stream, dst_stream);

	return(TRUE);
}