aboutsummaryrefslogtreecommitdiff
path: root/winsup/bz2lib/manual_3.html
blob: a8fa7e6828fad0255281ca1bdb3c29a0efc3d9dd (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
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54
     from manual.texi on 23 March 2000 -->

<TITLE>bzip2 and libbzip2 - Programming with libbzip2</TITLE>
<link href="manual_4.html" rel=Next>
<link href="manual_2.html" rel=Previous>
<link href="manual_toc.html" rel=ToC>

</HEAD>
<BODY>
<p>Go to the <A HREF="manual_1.html">first</A>, <A HREF="manual_2.html">previous</A>, <A HREF="manual_4.html">next</A>, <A HREF="manual_4.html">last</A> section, <A HREF="manual_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC12" HREF="manual_toc.html#TOC12">Programming with <CODE>libbzip2</CODE></A></H1>

<P>
This chapter describes the programming interface to <CODE>libbzip2</CODE>.

</P>
<P>
For general background information, particularly about memory
use and performance aspects, you'd be well advised to read Chapter 2
as well.

</P>


<H2><A NAME="SEC13" HREF="manual_toc.html#TOC13">Top-level structure</A></H2>

<P>
<CODE>libbzip2</CODE> is a flexible library for compressing and decompressing
data in the <CODE>bzip2</CODE> data format.  Although packaged as a single
entity, it helps to regard the library as three separate parts: the low
level interface, and the high level interface, and some utility
functions.

</P>
<P>
The structure of <CODE>libbzip2</CODE>'s interfaces is similar to
that of Jean-loup Gailly's and Mark Adler's excellent <CODE>zlib</CODE> 
library.

</P>
<P>
All externally visible symbols have names beginning <CODE>BZ2_</CODE>.
This is new in version 1.0.  The intention is to minimise pollution
of the namespaces of library clients.

</P>


<H3><A NAME="SEC14" HREF="manual_toc.html#TOC14">Low-level summary</A></H3>

<P>
This interface provides services for compressing and decompressing
data in memory.  There's no provision for dealing with files, streams
or any other I/O mechanisms, just straight memory-to-memory work.
In fact, this part of the library can be compiled without inclusion
of <CODE>stdio.h</CODE>, which may be helpful for embedded applications.

</P>
<P>
The low-level part of the library has no global variables and
is therefore thread-safe.

</P>
<P>
Six routines make up the low level interface: 
<CODE>BZ2_bzCompressInit</CODE>, <CODE>BZ2_bzCompress</CODE>, and <BR> <CODE>BZ2_bzCompressEnd</CODE>
for compression,
and a corresponding trio <CODE>BZ2_bzDecompressInit</CODE>, <BR> <CODE>BZ2_bzDecompress</CODE>
and <CODE>BZ2_bzDecompressEnd</CODE> for decompression.  
The <CODE>*Init</CODE> functions allocate
memory for compression/decompression and do other
initialisations, whilst the <CODE>*End</CODE> functions close down operations
and release memory.

</P>
<P>
The real work is done by <CODE>BZ2_bzCompress</CODE> and <CODE>BZ2_bzDecompress</CODE>.  
These compress and decompress data from a user-supplied input buffer
to a user-supplied output buffer.  These buffers can be any size;
arbitrary quantities of data are handled by making repeated calls
to these functions.  This is a flexible mechanism allowing a 
consumer-pull style of activity, or producer-push, or a mixture of
both.

</P>



<H3><A NAME="SEC15" HREF="manual_toc.html#TOC15">High-level summary</A></H3>

<P>
This interface provides some handy wrappers around the low-level
interface to facilitate reading and writing <CODE>bzip2</CODE> format
files (<CODE>.bz2</CODE> files).  The routines provide hooks to facilitate
reading files in which the <CODE>bzip2</CODE> data stream is embedded 
within some larger-scale file structure, or where there are
multiple <CODE>bzip2</CODE> data streams concatenated end-to-end.

</P>
<P>
For reading files, <CODE>BZ2_bzReadOpen</CODE>, <CODE>BZ2_bzRead</CODE>,
<CODE>BZ2_bzReadClose</CODE> and <BR> <CODE>BZ2_bzReadGetUnused</CODE> are supplied.  For
writing files, <CODE>BZ2_bzWriteOpen</CODE>, <CODE>BZ2_bzWrite</CODE> and
<CODE>BZ2_bzWriteFinish</CODE> are available.

</P>
<P>
As with the low-level library, no global variables are used
so the library is per se thread-safe.  However, if I/O errors
occur whilst reading or writing the underlying compressed files,
you may have to consult <CODE>errno</CODE> to determine the cause of
the error.  In that case, you'd need a C library which correctly
supports <CODE>errno</CODE> in a multithreaded environment.

</P>
<P>
To make the library a little simpler and more portable,
<CODE>BZ2_bzReadOpen</CODE> and <CODE>BZ2_bzWriteOpen</CODE> require you to pass them file
handles (<CODE>FILE*</CODE>s) which have previously been opened for reading or
writing respectively.  That avoids portability problems associated with
file operations and file attributes, whilst not being much of an
imposition on the programmer.

</P>



<H3><A NAME="SEC16" HREF="manual_toc.html#TOC16">Utility functions summary</A></H3>
<P>
For very simple needs, <CODE>BZ2_bzBuffToBuffCompress</CODE> and
<CODE>BZ2_bzBuffToBuffDecompress</CODE> are provided.  These compress
data in memory from one buffer to another buffer in a single
function call.  You should assess whether these functions
fulfill your memory-to-memory compression/decompression
requirements before investing effort in understanding the more
general but more complex low-level interface.

</P>
<P>
Yoshioka Tsuneo (<CODE>QWF00133@niftyserve.or.jp</CODE> /
<CODE>tsuneo-y@is.aist-nara.ac.jp</CODE>) has contributed some functions to
give better <CODE>zlib</CODE> compatibility.  These functions are
<CODE>BZ2_bzopen</CODE>, <CODE>BZ2_bzread</CODE>, <CODE>BZ2_bzwrite</CODE>, <CODE>BZ2_bzflush</CODE>,
<CODE>BZ2_bzclose</CODE>,
<CODE>BZ2_bzerror</CODE> and <CODE>BZ2_bzlibVersion</CODE>.  You may find these functions
more convenient for simple file reading and writing, than those in the
high-level interface.  These functions are not (yet) officially part of
the library, and are minimally documented here.  If they break, you
get to keep all the pieces.  I hope to document them properly when time
permits.

</P>
<P>
Yoshioka also contributed modifications to allow the library to be
built as a Windows DLL.

</P>



<H2><A NAME="SEC17" HREF="manual_toc.html#TOC17">Error handling</A></H2>

<P>
The library is designed to recover cleanly in all situations, including
the worst-case situation of decompressing random data.  I'm not 
100% sure that it can always do this, so you might want to add
a signal handler to catch segmentation violations during decompression
if you are feeling especially paranoid.  I would be interested in
hearing more about the robustness of the library to corrupted
compressed data.

</P>
<P>
Version 1.0 is much more robust in this respect than
0.9.0 or 0.9.5.  Investigations with Checker (a tool for 
detecting problems with memory management, similar to Purify)
indicate that, at least for the few files I tested, all single-bit
errors in the decompressed data are caught properly, with no
segmentation faults, no reads of uninitialised data and no 
out of range reads or writes.  So it's certainly much improved,
although I wouldn't claim it to be totally bombproof.

</P>
<P>
The file <CODE>bzlib.h</CODE> contains all definitions needed to use
the library.  In particular, you should definitely not include
<CODE>bzlib_private.h</CODE>.

</P>
<P>
In <CODE>bzlib.h</CODE>, the various return values are defined.  The following
list is not intended as an exhaustive description of the circumstances 
in which a given value may be returned -- those descriptions are given
later.  Rather, it is intended to convey the rough meaning of each
return value.  The first five actions are normal and not intended to 
denote an error situation.
<DL COMPACT>

<DT><CODE>BZ_OK</CODE>
<DD>
The requested action was completed successfully.
<DT><CODE>BZ_RUN_OK</CODE>
<DD>
<DT><CODE>BZ_FLUSH_OK</CODE>
<DD>
<DT><CODE>BZ_FINISH_OK</CODE>
<DD>
In <CODE>BZ2_bzCompress</CODE>, the requested flush/finish/nothing-special action
was completed successfully.
<DT><CODE>BZ_STREAM_END</CODE>
<DD>
Compression of data was completed, or the logical stream end was
detected during decompression.
</DL>

<P>
The following return values indicate an error of some kind.
<DL COMPACT>

<DT><CODE>BZ_CONFIG_ERROR</CODE>
<DD>
Indicates that the library has been improperly compiled on your
platform -- a major configuration error.  Specifically, it means
that <CODE>sizeof(char)</CODE>, <CODE>sizeof(short)</CODE> and <CODE>sizeof(int)</CODE>
are not 1, 2 and 4 respectively, as they should be.  Note that the 
library should still work properly on 64-bit platforms which follow
the LP64 programming model -- that is, where <CODE>sizeof(long)</CODE>
and <CODE>sizeof(void*)</CODE> are 8.  Under LP64, <CODE>sizeof(int)</CODE> is
still 4, so <CODE>libbzip2</CODE>, which doesn't use the <CODE>long</CODE> type,
is OK.
<DT><CODE>BZ_SEQUENCE_ERROR</CODE>
<DD>
When using the library, it is important to call the functions in the
correct sequence and with data structures (buffers etc) in the correct
states.  <CODE>libbzip2</CODE> checks as much as it can to ensure this is
happening, and returns <CODE>BZ_SEQUENCE_ERROR</CODE> if not.  Code which
complies precisely with the function semantics, as detailed below,
should never receive this value; such an event denotes buggy code
which you should investigate.
<DT><CODE>BZ_PARAM_ERROR</CODE>
<DD>
Returned when a parameter to a function call is out of range 
or otherwise manifestly incorrect.  As with <CODE>BZ_SEQUENCE_ERROR</CODE>,
this denotes a bug in the client code.  The distinction between
<CODE>BZ_PARAM_ERROR</CODE> and <CODE>BZ_SEQUENCE_ERROR</CODE> is a bit hazy, but still worth
making.
<DT><CODE>BZ_MEM_ERROR</CODE>
<DD>
Returned when a request to allocate memory failed.  Note that the
quantity of memory needed to decompress a stream cannot be determined
until the stream's header has been read.  So <CODE>BZ2_bzDecompress</CODE> and
<CODE>BZ2_bzRead</CODE> may return <CODE>BZ_MEM_ERROR</CODE> even though some of
the compressed data has been read.  The same is not true for
compression; once <CODE>BZ2_bzCompressInit</CODE> or <CODE>BZ2_bzWriteOpen</CODE> have
successfully completed, <CODE>BZ_MEM_ERROR</CODE> cannot occur.
<DT><CODE>BZ_DATA_ERROR</CODE>
<DD>
Returned when a data integrity error is detected during decompression.
Most importantly, this means when stored and computed CRCs for the
data do not match.  This value is also returned upon detection of any
other anomaly in the compressed data.
<DT><CODE>BZ_DATA_ERROR_MAGIC</CODE>
<DD>
As a special case of <CODE>BZ_DATA_ERROR</CODE>, it is sometimes useful to
know when the compressed stream does not start with the correct
magic bytes (<CODE>'B' 'Z' 'h'</CODE>).  
<DT><CODE>BZ_IO_ERROR</CODE>
<DD>
Returned by <CODE>BZ2_bzRead</CODE> and <CODE>BZ2_bzWrite</CODE> when there is an error
reading or writing in the compressed file, and by <CODE>BZ2_bzReadOpen</CODE>
and <CODE>BZ2_bzWriteOpen</CODE> for attempts to use a file for which the
error indicator (viz, <CODE>ferror(f)</CODE>) is set.
On receipt of <CODE>BZ_IO_ERROR</CODE>, the caller should consult
<CODE>errno</CODE> and/or <CODE>perror</CODE> to acquire operating-system
specific information about the problem.
<DT><CODE>BZ_UNEXPECTED_EOF</CODE>
<DD>
Returned by <CODE>BZ2_bzRead</CODE> when the compressed file finishes
before the logical end of stream is detected.
<DT><CODE>BZ_OUTBUFF_FULL</CODE>
<DD>
Returned by <CODE>BZ2_bzBuffToBuffCompress</CODE> and
<CODE>BZ2_bzBuffToBuffDecompress</CODE> to indicate that the output data
will not fit into the output buffer provided.
</DL>



<H2><A NAME="SEC18" HREF="manual_toc.html#TOC18">Low-level interface</A></H2>



<H3><A NAME="SEC19" HREF="manual_toc.html#TOC19"><CODE>BZ2_bzCompressInit</CODE></A></H3>

<PRE>
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;

int BZ2_bzCompressInit ( bz_stream *strm, 
                         int blockSize100k, 
                         int verbosity,
                         int workFactor );

</PRE>

<P>
Prepares for compression.  The <CODE>bz_stream</CODE> structure
holds all data pertaining to the compression activity.  
A <CODE>bz_stream</CODE> structure should be allocated and initialised
prior to the call.
The fields of <CODE>bz_stream</CODE>
comprise the entirety of the user-visible data.  <CODE>state</CODE>
is a pointer to the private data structures required for compression.

</P>
<P>
Custom memory allocators are supported, via fields <CODE>bzalloc</CODE>, 
<CODE>bzfree</CODE>,
and <CODE>opaque</CODE>.  The value 
<CODE>opaque</CODE> is passed to as the first argument to
all calls to <CODE>bzalloc</CODE> and <CODE>bzfree</CODE>, but is 
otherwise ignored by the library.
The call <CODE>bzalloc ( opaque, n, m )</CODE> is expected to return a 
pointer <CODE>p</CODE> to
<CODE>n * m</CODE> bytes of memory, and <CODE>bzfree ( opaque, p )</CODE> 
should free
that memory.

</P>
<P>
If you don't want to use a custom memory allocator, set <CODE>bzalloc</CODE>, 
<CODE>bzfree</CODE> and
<CODE>opaque</CODE> to <CODE>NULL</CODE>, 
and the library will then use the standard <CODE>malloc</CODE>/<CODE>free</CODE>
routines.

</P>
<P>
Before calling <CODE>BZ2_bzCompressInit</CODE>, fields <CODE>bzalloc</CODE>, 
<CODE>bzfree</CODE> and <CODE>opaque</CODE> should
be filled appropriately, as just described.  Upon return, the internal
state will have been allocated and initialised, and <CODE>total_in_lo32</CODE>, 
<CODE>total_in_hi32</CODE>, <CODE>total_out_lo32</CODE> and 
<CODE>total_out_hi32</CODE> will have been set to zero.  
These four fields are used by the library
to inform the caller of the total amount of data passed into and out of
the library, respectively.  You should not try to change them.
As of version 1.0, 64-bit counts are maintained, even on 32-bit
platforms, using the <CODE>_hi32</CODE> fields to store the upper 32 bits
of the count.  So, for example, the total amount of data in
is <CODE>(total_in_hi32 &#60;&#60; 32) + total_in_lo32</CODE>.

</P>
<P>
Parameter <CODE>blockSize100k</CODE> specifies the block size to be used for
compression.  It should be a value between 1 and 9 inclusive, and the
actual block size used is 100000 x this figure.  9 gives the best
compression but takes most memory.

</P>
<P>
Parameter <CODE>verbosity</CODE> should be set to a number between 0 and 4
inclusive.  0 is silent, and greater numbers give increasingly verbose
monitoring/debugging output.  If the library has been compiled with
<CODE>-DBZ_NO_STDIO</CODE>, no such output will appear for any verbosity
setting.

</P>
<P>
Parameter <CODE>workFactor</CODE> controls how the compression phase behaves
when presented with worst case, highly repetitive, input data.  If
compression runs into difficulties caused by repetitive data, the
library switches from the standard sorting algorithm to a fallback
algorithm.  The fallback is slower than the standard algorithm by
perhaps a factor of three, but always behaves reasonably, no matter how
bad the input.

</P>
<P>
Lower values of <CODE>workFactor</CODE> reduce the amount of effort the
standard algorithm will expend before resorting to the fallback.  You
should set this parameter carefully; too low, and many inputs will be
handled by the fallback algorithm and so compress rather slowly, too
high, and your average-to-worst case compression times can become very
large.  The default value of 30 gives reasonable behaviour over a wide
range of circumstances.

</P>
<P>
Allowable values range from 0 to 250 inclusive.  0 is a special case,
equivalent to using the default value of 30.

</P>
<P>
Note that the compressed output generated is the same regardless of
whether or not the fallback algorithm is used.

</P>
<P>
Be aware also that this parameter may disappear entirely in future
versions of the library.  In principle it should be possible to devise a
good way to automatically choose which algorithm to use.  Such a
mechanism would render the parameter obsolete.

</P>
<P>
Possible return values:

<PRE>
      <CODE>BZ_CONFIG_ERROR</CODE>
         if the library has been mis-compiled
      <CODE>BZ_PARAM_ERROR</CODE> 
         if <CODE>strm</CODE> is <CODE>NULL</CODE> 
         or <CODE>blockSize</CODE> &#60; 1 or <CODE>blockSize</CODE> &#62; 9
         or <CODE>verbosity</CODE> &#60; 0 or <CODE>verbosity</CODE> &#62; 4
         or <CODE>workFactor</CODE> &#60; 0 or <CODE>workFactor</CODE> &#62; 250
      <CODE>BZ_MEM_ERROR</CODE> 
         if not enough memory is available
      <CODE>BZ_OK</CODE> 
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      <CODE>BZ2_bzCompress</CODE> 
         if <CODE>BZ_OK</CODE> is returned
      no specific action needed in case of error
</PRE>



<H3><A NAME="SEC20" HREF="manual_toc.html#TOC20"><CODE>BZ2_bzCompress</CODE></A></H3>

<PRE>
   int BZ2_bzCompress ( bz_stream *strm, int action );
</PRE>

<P>
Provides more input and/or output buffer space for the library.  The
caller maintains input and output buffers, and calls <CODE>BZ2_bzCompress</CODE> to
transfer data between them.

</P>
<P>
Before each call to <CODE>BZ2_bzCompress</CODE>, <CODE>next_in</CODE> should point at
the data to be compressed, and <CODE>avail_in</CODE> should indicate how many
bytes the library may read.  <CODE>BZ2_bzCompress</CODE> updates <CODE>next_in</CODE>,
<CODE>avail_in</CODE> and <CODE>total_in</CODE> to reflect the number of bytes it
has read.

</P>
<P>
Similarly, <CODE>next_out</CODE> should point to a buffer in which the
compressed data is to be placed, with <CODE>avail_out</CODE> indicating how
much output space is available.  <CODE>BZ2_bzCompress</CODE> updates
<CODE>next_out</CODE>, <CODE>avail_out</CODE> and <CODE>total_out</CODE> to reflect the
number of bytes output.

</P>
<P>
You may provide and remove as little or as much data as you like on each
call of <CODE>BZ2_bzCompress</CODE>.  In the limit, it is acceptable to supply and
remove data one byte at a time, although this would be terribly
inefficient.  You should always ensure that at least one byte of output
space is available at each call.

</P>
<P>
A second purpose of <CODE>BZ2_bzCompress</CODE> is to request a change of mode of the
compressed stream.  

</P>
<P>
Conceptually, a compressed stream can be in one of four states: IDLE,
RUNNING, FLUSHING and FINISHING.  Before initialisation
(<CODE>BZ2_bzCompressInit</CODE>) and after termination (<CODE>BZ2_bzCompressEnd</CODE>), a
stream is regarded as IDLE.

</P>
<P>
Upon initialisation (<CODE>BZ2_bzCompressInit</CODE>), the stream is placed in the
RUNNING state.  Subsequent calls to <CODE>BZ2_bzCompress</CODE> should pass
<CODE>BZ_RUN</CODE> as the requested action; other actions are illegal and
will result in <CODE>BZ_SEQUENCE_ERROR</CODE>.

</P>
<P>
At some point, the calling program will have provided all the input data
it wants to.  It will then want to finish up -- in effect, asking the
library to process any data it might have buffered internally.  In this
state, <CODE>BZ2_bzCompress</CODE> will no longer attempt to read data from
<CODE>next_in</CODE>, but it will want to write data to <CODE>next_out</CODE>.
Because the output buffer supplied by the user can be arbitrarily small,
the finishing-up operation cannot necessarily be done with a single call
of <CODE>BZ2_bzCompress</CODE>.

</P>
<P>
Instead, the calling program passes <CODE>BZ_FINISH</CODE> as an action to
<CODE>BZ2_bzCompress</CODE>.  This changes the stream's state to FINISHING.  Any
remaining input (ie, <CODE>next_in[0 .. avail_in-1]</CODE>) is compressed and
transferred to the output buffer.  To do this, <CODE>BZ2_bzCompress</CODE> must be
called repeatedly until all the output has been consumed.  At that
point, <CODE>BZ2_bzCompress</CODE> returns <CODE>BZ_STREAM_END</CODE>, and the stream's
state is set back to IDLE.  <CODE>BZ2_bzCompressEnd</CODE> should then be
called.

</P>
<P>
Just to make sure the calling program does not cheat, the library makes
a note of <CODE>avail_in</CODE> at the time of the first call to
<CODE>BZ2_bzCompress</CODE> which has <CODE>BZ_FINISH</CODE> as an action (ie, at the
time the program has announced its intention to not supply any more
input).  By comparing this value with that of <CODE>avail_in</CODE> over
subsequent calls to <CODE>BZ2_bzCompress</CODE>, the library can detect any
attempts to slip in more data to compress.  Any calls for which this is
detected will return <CODE>BZ_SEQUENCE_ERROR</CODE>.  This indicates a
programming mistake which should be corrected.

</P>
<P>
Instead of asking to finish, the calling program may ask
<CODE>BZ2_bzCompress</CODE> to take all the remaining input, compress it and
terminate the current (Burrows-Wheeler) compression block.  This could
be useful for error control purposes.  The mechanism is analogous to
that for finishing: call <CODE>BZ2_bzCompress</CODE> with an action of
<CODE>BZ_FLUSH</CODE>, remove output data, and persist with the
<CODE>BZ_FLUSH</CODE> action until the value <CODE>BZ_RUN</CODE> is returned.  As
with finishing, <CODE>BZ2_bzCompress</CODE> detects any attempt to provide more
input data once the flush has begun.

</P>
<P>
Once the flush is complete, the stream returns to the normal RUNNING
state.

</P>
<P>
This all sounds pretty complex, but isn't really.  Here's a table
which shows which actions are allowable in each state, what action
will be taken, what the next state is, and what the non-error return
values are.  Note that you can't explicitly ask what state the
stream is in, but nor do you need to -- it can be inferred from the
values returned by <CODE>BZ2_bzCompress</CODE>.

<PRE>
IDLE/<CODE>any</CODE>           
      Illegal.  IDLE state only exists after <CODE>BZ2_bzCompressEnd</CODE> or
      before <CODE>BZ2_bzCompressInit</CODE>.
      Return value = <CODE>BZ_SEQUENCE_ERROR</CODE>

RUNNING/<CODE>BZ_RUN</CODE>     
      Compress from <CODE>next_in</CODE> to <CODE>next_out</CODE> as much as possible.
      Next state = RUNNING
      Return value = <CODE>BZ_RUN_OK</CODE>

RUNNING/<CODE>BZ_FLUSH</CODE>   
      Remember current value of <CODE>next_in</CODE>.  Compress from <CODE>next_in</CODE>
      to <CODE>next_out</CODE> as much as possible, but do not accept any more input.  
      Next state = FLUSHING
      Return value = <CODE>BZ_FLUSH_OK</CODE>

RUNNING/<CODE>BZ_FINISH</CODE>  
      Remember current value of <CODE>next_in</CODE>.  Compress from <CODE>next_in</CODE>
      to <CODE>next_out</CODE> as much as possible, but do not accept any more input.
      Next state = FINISHING
      Return value = <CODE>BZ_FINISH_OK</CODE>

FLUSHING/<CODE>BZ_FLUSH</CODE>  
      Compress from <CODE>next_in</CODE> to <CODE>next_out</CODE> as much as possible, 
      but do not accept any more input.  
      If all the existing input has been used up and all compressed
      output has been removed
         Next state = RUNNING; Return value = <CODE>BZ_RUN_OK</CODE>
      else
         Next state = FLUSHING; Return value = <CODE>BZ_FLUSH_OK</CODE>

FLUSHING/other     
      Illegal.
      Return value = <CODE>BZ_SEQUENCE_ERROR</CODE>

FINISHING/<CODE>BZ_FINISH</CODE>  
      Compress from <CODE>next_in</CODE> to <CODE>next_out</CODE> as much as possible,
      but to not accept any more input.  
      If all the existing input has been used up and all compressed
      output has been removed
         Next state = IDLE; Return value = <CODE>BZ_STREAM_END</CODE>
      else
         Next state = FINISHING; Return value = <CODE>BZ_FINISHING</CODE>

FINISHING/other
      Illegal.
      Return value = <CODE>BZ_SEQUENCE_ERROR</CODE>
</PRE>

<P>
That still looks complicated?  Well, fair enough.  The usual sequence
of calls for compressing a load of data is:

<UL>
<LI>Get started with <CODE>BZ2_bzCompressInit</CODE>.

<LI>Shovel data in and shlurp out its compressed form using zero or more

calls of <CODE>BZ2_bzCompress</CODE> with action = <CODE>BZ_RUN</CODE>.
<LI>Finish up.

Repeatedly call <CODE>BZ2_bzCompress</CODE> with action = <CODE>BZ_FINISH</CODE>, 
copying out the compressed output, until <CODE>BZ_STREAM_END</CODE> is returned.
<LI>Close up and go home.  Call <CODE>BZ2_bzCompressEnd</CODE>.

</UL>

<P>
If the data you want to compress fits into your input buffer all
at once, you can skip the calls of <CODE>BZ2_bzCompress ( ..., BZ_RUN )</CODE> and 
just do the <CODE>BZ2_bzCompress ( ..., BZ_FINISH )</CODE> calls.

</P>
<P>
All required memory is allocated by <CODE>BZ2_bzCompressInit</CODE>.  The
compression library can accept any data at all (obviously).  So you
shouldn't get any error return values from the <CODE>BZ2_bzCompress</CODE> calls.
If you do, they will be <CODE>BZ_SEQUENCE_ERROR</CODE>, and indicate a bug in
your programming.

</P>
<P>
Trivial other possible return values:

<PRE>
      <CODE>BZ_PARAM_ERROR</CODE>   
         if <CODE>strm</CODE> is <CODE>NULL</CODE>, or <CODE>strm-&#62;s</CODE> is <CODE>NULL</CODE>
</PRE>



<H3><A NAME="SEC21" HREF="manual_toc.html#TOC21"><CODE>BZ2_bzCompressEnd</CODE></A></H3>

<PRE>
int BZ2_bzCompressEnd ( bz_stream *strm );
</PRE>

<P>
Releases all memory associated with a compression stream.

</P>
<P>
Possible return values:

<PRE>
   <CODE>BZ_PARAM_ERROR</CODE>    if <CODE>strm</CODE> is <CODE>NULL</CODE> or <CODE>strm-&#62;s</CODE> is <CODE>NULL</CODE>
   <CODE>BZ_OK</CODE>    otherwise
</PRE>



<H3><A NAME="SEC22" HREF="manual_toc.html#TOC22"><CODE>BZ2_bzDecompressInit</CODE></A></H3>

<PRE>
int BZ2_bzDecompressInit ( bz_stream *strm, int verbosity, int small );
</PRE>

<P>
Prepares for decompression.  As with <CODE>BZ2_bzCompressInit</CODE>, a
<CODE>bz_stream</CODE> record should be allocated and initialised before the
call.  Fields <CODE>bzalloc</CODE>, <CODE>bzfree</CODE> and <CODE>opaque</CODE> should be
set if a custom memory allocator is required, or made <CODE>NULL</CODE> for
the normal <CODE>malloc</CODE>/<CODE>free</CODE> routines.  Upon return, the internal
state will have been initialised, and <CODE>total_in</CODE> and
<CODE>total_out</CODE> will be zero.

</P>
<P>
For the meaning of parameter <CODE>verbosity</CODE>, see <CODE>BZ2_bzCompressInit</CODE>.

</P>
<P>
If <CODE>small</CODE> is nonzero, the library will use an alternative
decompression algorithm which uses less memory but at the cost of
decompressing more slowly (roughly speaking, half the speed, but the
maximum memory requirement drops to around 2300k).  See Chapter 2 for
more information on memory management.

</P>
<P>
Note that the amount of memory needed to decompress
a stream cannot be determined until the stream's header has been read,
so even if <CODE>BZ2_bzDecompressInit</CODE> succeeds, a subsequent
<CODE>BZ2_bzDecompress</CODE> could fail with <CODE>BZ_MEM_ERROR</CODE>.

</P>
<P>
Possible return values:

<PRE>
      <CODE>BZ_CONFIG_ERROR</CODE>
         if the library has been mis-compiled
      <CODE>BZ_PARAM_ERROR</CODE>
         if <CODE>(small != 0 &#38;&#38; small != 1)</CODE>
         or <CODE>(verbosity &#60; 0 || verbosity &#62; 4)</CODE>
      <CODE>BZ_MEM_ERROR</CODE>
         if insufficient memory is available
</PRE>

<P>
Allowable next actions:

<PRE>
      <CODE>BZ2_bzDecompress</CODE>
         if <CODE>BZ_OK</CODE> was returned
      no specific action required in case of error
</PRE>

<P>
 

</P>


<H3><A NAME="SEC23" HREF="manual_toc.html#TOC23"><CODE>BZ2_bzDecompress</CODE></A></H3>

<PRE>
int BZ2_bzDecompress ( bz_stream *strm );
</PRE>

<P>
Provides more input and/out output buffer space for the library.  The
caller maintains input and output buffers, and uses <CODE>BZ2_bzDecompress</CODE>
to transfer data between them.

</P>
<P>
Before each call to <CODE>BZ2_bzDecompress</CODE>, <CODE>next_in</CODE> 
should point at the compressed data,
and <CODE>avail_in</CODE> should indicate how many bytes the library
may read.  <CODE>BZ2_bzDecompress</CODE> updates <CODE>next_in</CODE>, <CODE>avail_in</CODE> 
and <CODE>total_in</CODE>
to reflect the number of bytes it has read.

</P>
<P>
Similarly, <CODE>next_out</CODE> should point to a buffer in which the uncompressed
output is to be placed, with <CODE>avail_out</CODE> indicating how much output space
is available.  <CODE>BZ2_bzCompress</CODE> updates <CODE>next_out</CODE>,
<CODE>avail_out</CODE> and <CODE>total_out</CODE> to reflect
the number of bytes output.

</P>
<P>
You may provide and remove as little or as much data as you like on
each call of <CODE>BZ2_bzDecompress</CODE>.  
In the limit, it is acceptable to
supply and remove data one byte at a time, although this would be
terribly inefficient.  You should always ensure that at least one
byte of output space is available at each call.

</P>
<P>
Use of <CODE>BZ2_bzDecompress</CODE> is simpler than <CODE>BZ2_bzCompress</CODE>.

</P>
<P>
You should provide input and remove output as described above, and
repeatedly call <CODE>BZ2_bzDecompress</CODE> until <CODE>BZ_STREAM_END</CODE> is
returned.  Appearance of <CODE>BZ_STREAM_END</CODE> denotes that
<CODE>BZ2_bzDecompress</CODE> has detected the logical end of the compressed
stream.  <CODE>BZ2_bzDecompress</CODE> will not produce <CODE>BZ_STREAM_END</CODE> until
all output data has been placed into the output buffer, so once
<CODE>BZ_STREAM_END</CODE> appears, you are guaranteed to have available all
the decompressed output, and <CODE>BZ2_bzDecompressEnd</CODE> can safely be
called.

</P>
<P>
If case of an error return value, you should call <CODE>BZ2_bzDecompressEnd</CODE>
to clean up and release memory.

</P>
<P>
Possible return values:

<PRE>
      <CODE>BZ_PARAM_ERROR</CODE>
         if <CODE>strm</CODE> is <CODE>NULL</CODE> or <CODE>strm-&#62;s</CODE> is <CODE>NULL</CODE>
         or <CODE>strm-&#62;avail_out &#60; 1</CODE>
      <CODE>BZ_DATA_ERROR</CODE>
         if a data integrity error is detected in the compressed stream
      <CODE>BZ_DATA_ERROR_MAGIC</CODE>
         if the compressed stream doesn't begin with the right magic bytes
      <CODE>BZ_MEM_ERROR</CODE>
         if there wasn't enough memory available
      <CODE>BZ_STREAM_END</CODE>
         if the logical end of the data stream was detected and all
         output in has been consumed, eg <CODE>s-&#62;avail_out &#62; 0</CODE>
      <CODE>BZ_OK</CODE>
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      <CODE>BZ2_bzDecompress</CODE>
         if <CODE>BZ_OK</CODE> was returned
      <CODE>BZ2_bzDecompressEnd</CODE>
         otherwise
</PRE>



<H3><A NAME="SEC24" HREF="manual_toc.html#TOC24"><CODE>BZ2_bzDecompressEnd</CODE></A></H3>

<PRE>
int BZ2_bzDecompressEnd ( bz_stream *strm );
</PRE>

<P>
Releases all memory associated with a decompression stream.

</P>
<P>
Possible return values:

<PRE>
      <CODE>BZ_PARAM_ERROR</CODE>
         if <CODE>strm</CODE> is <CODE>NULL</CODE> or <CODE>strm-&#62;s</CODE> is <CODE>NULL</CODE>
      <CODE>BZ_OK</CODE>
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      None.
</PRE>



<H2><A NAME="SEC25" HREF="manual_toc.html#TOC25">High-level interface</A></H2>

<P>
This interface provides functions for reading and writing 
<CODE>bzip2</CODE> format files.  First, some general points.

</P>

<UL>
<LI>All of the functions take an <CODE>int*</CODE> first argument,

  <CODE>bzerror</CODE>.
  After each call, <CODE>bzerror</CODE> should be consulted first to determine
  the outcome of the call.  If <CODE>bzerror</CODE> is <CODE>BZ_OK</CODE>, 
  the call completed
  successfully, and only then should the return value of the function
  (if any) be consulted.  If <CODE>bzerror</CODE> is <CODE>BZ_IO_ERROR</CODE>, 
  there was an error
  reading/writing the underlying compressed file, and you should
  then consult <CODE>errno</CODE>/<CODE>perror</CODE> to determine the 
  cause of the difficulty.
  <CODE>bzerror</CODE> may also be set to various other values; precise details are
  given on a per-function basis below.
<LI>If <CODE>bzerror</CODE> indicates an error

  (ie, anything except <CODE>BZ_OK</CODE> and <CODE>BZ_STREAM_END</CODE>),
  you should immediately call <CODE>BZ2_bzReadClose</CODE> (or <CODE>BZ2_bzWriteClose</CODE>,
  depending on whether you are attempting to read or to write)
  to free up all resources associated
  with the stream.  Once an error has been indicated, behaviour of all calls
  except <CODE>BZ2_bzReadClose</CODE> (<CODE>BZ2_bzWriteClose</CODE>) is undefined.  
  The implication is that (1) <CODE>bzerror</CODE> should
  be checked after each call, and (2) if <CODE>bzerror</CODE> indicates an error, 
  <CODE>BZ2_bzReadClose</CODE> (<CODE>BZ2_bzWriteClose</CODE>) should then be called to clean up.
<LI>The <CODE>FILE*</CODE> arguments passed to

   <CODE>BZ2_bzReadOpen</CODE>/<CODE>BZ2_bzWriteOpen</CODE>  
  should be set to binary mode.
  Most Unix systems will do this by default, but other platforms,
  including Windows and Mac, will not.  If you omit this, you may
  encounter problems when moving code to new platforms.
<LI>Memory allocation requests are handled by

  <CODE>malloc</CODE>/<CODE>free</CODE>.  
  At present
  there is no facility for user-defined memory allocators in the file I/O
  functions (could easily be added, though).
</UL>



<H3><A NAME="SEC26" HREF="manual_toc.html#TOC26"><CODE>BZ2_bzReadOpen</CODE></A></H3>

<PRE>
   typedef void BZFILE;

   BZFILE *BZ2_bzReadOpen ( int *bzerror, FILE *f, 
                            int small, int verbosity,
                            void *unused, int nUnused );
</PRE>

<P>
Prepare to read compressed data from file handle <CODE>f</CODE>.  <CODE>f</CODE>
should refer to a file which has been opened for reading, and for which
the error indicator (<CODE>ferror(f)</CODE>)is not set.  If <CODE>small</CODE> is 1,
the library will try to decompress using less memory, at the expense of
speed.

</P>
<P>
For reasons explained below, <CODE>BZ2_bzRead</CODE> will decompress the
<CODE>nUnused</CODE> bytes starting at <CODE>unused</CODE>, before starting to read
from the file <CODE>f</CODE>.  At most <CODE>BZ_MAX_UNUSED</CODE> bytes may be
supplied like this.  If this facility is not required, you should pass
<CODE>NULL</CODE> and <CODE>0</CODE> for <CODE>unused</CODE> and n<CODE>Unused</CODE>
respectively.

</P>
<P>
For the meaning of parameters <CODE>small</CODE> and <CODE>verbosity</CODE>,
see <CODE>BZ2_bzDecompressInit</CODE>.

</P>
<P>
The amount of memory needed to decompress a file cannot be determined
until the file's header has been read.  So it is possible that
<CODE>BZ2_bzReadOpen</CODE> returns <CODE>BZ_OK</CODE> but a subsequent call of
<CODE>BZ2_bzRead</CODE> will return <CODE>BZ_MEM_ERROR</CODE>.

</P>
<P>
Possible assignments to <CODE>bzerror</CODE>:

<PRE>
      <CODE>BZ_CONFIG_ERROR</CODE>
         if the library has been mis-compiled
      <CODE>BZ_PARAM_ERROR</CODE>
         if <CODE>f</CODE> is <CODE>NULL</CODE> 
         or <CODE>small</CODE> is neither <CODE>0</CODE> nor <CODE>1</CODE>                 
         or <CODE>(unused == NULL &#38;&#38; nUnused != 0)</CODE>
         or <CODE>(unused != NULL &#38;&#38; !(0 &#60;= nUnused &#60;= BZ_MAX_UNUSED))</CODE>
      <CODE>BZ_IO_ERROR</CODE>    
         if <CODE>ferror(f)</CODE> is nonzero
      <CODE>BZ_MEM_ERROR</CODE>   
         if insufficient memory is available
      <CODE>BZ_OK</CODE>
         otherwise.
</PRE>

<P>
Possible return values:

<PRE>
      Pointer to an abstract <CODE>BZFILE</CODE>        
         if <CODE>bzerror</CODE> is <CODE>BZ_OK</CODE>   
      <CODE>NULL</CODE>
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      <CODE>BZ2_bzRead</CODE>
         if <CODE>bzerror</CODE> is <CODE>BZ_OK</CODE>   
      <CODE>BZ2_bzClose</CODE> 
         otherwise
</PRE>



<H3><A NAME="SEC27" HREF="manual_toc.html#TOC27"><CODE>BZ2_bzRead</CODE></A></H3>

<PRE>
   int BZ2_bzRead ( int *bzerror, BZFILE *b, void *buf, int len );
</PRE>

<P>
Reads up to <CODE>len</CODE> (uncompressed) bytes from the compressed file 
<CODE>b</CODE> into
the buffer <CODE>buf</CODE>.  If the read was successful, 
<CODE>bzerror</CODE> is set to <CODE>BZ_OK</CODE>
and the number of bytes read is returned.  If the logical end-of-stream
was detected, <CODE>bzerror</CODE> will be set to <CODE>BZ_STREAM_END</CODE>, 
and the number
of bytes read is returned.  All other <CODE>bzerror</CODE> values denote an error.

</P>
<P>
<CODE>BZ2_bzRead</CODE> will supply <CODE>len</CODE> bytes,
unless the logical stream end is detected
or an error occurs.  Because of this, it is possible to detect the 
stream end by observing when the number of bytes returned is 
less than the number
requested.  Nevertheless, this is regarded as inadvisable; you should
instead check <CODE>bzerror</CODE> after every call and watch out for
<CODE>BZ_STREAM_END</CODE>.

</P>
<P>
Internally, <CODE>BZ2_bzRead</CODE> copies data from the compressed file in chunks
of size <CODE>BZ_MAX_UNUSED</CODE> bytes
before decompressing it.  If the file contains more bytes than strictly
needed to reach the logical end-of-stream, <CODE>BZ2_bzRead</CODE> will almost certainly
read some of the trailing data before signalling <CODE>BZ_SEQUENCE_END</CODE>.
To collect the read but unused data once <CODE>BZ_SEQUENCE_END</CODE> has 
appeared, call <CODE>BZ2_bzReadGetUnused</CODE> immediately before <CODE>BZ2_bzReadClose</CODE>.

</P>
<P>
Possible assignments to <CODE>bzerror</CODE>:

<PRE>
      <CODE>BZ_PARAM_ERROR</CODE>
         if <CODE>b</CODE> is <CODE>NULL</CODE> or <CODE>buf</CODE> is <CODE>NULL</CODE> or <CODE>len &#60; 0</CODE>
      <CODE>BZ_SEQUENCE_ERROR</CODE> 
         if <CODE>b</CODE> was opened with <CODE>BZ2_bzWriteOpen</CODE>
      <CODE>BZ_IO_ERROR</CODE> 
         if there is an error reading from the compressed file
      <CODE>BZ_UNEXPECTED_EOF</CODE> 
         if the compressed file ended before the logical end-of-stream was detected
      <CODE>BZ_DATA_ERROR</CODE> 
         if a data integrity error was detected in the compressed stream
      <CODE>BZ_DATA_ERROR_MAGIC</CODE>
         if the stream does not begin with the requisite header bytes (ie, is not 
         a <CODE>bzip2</CODE> data file).  This is really a special case of <CODE>BZ_DATA_ERROR</CODE>.
      <CODE>BZ_MEM_ERROR</CODE> 
         if insufficient memory was available
      <CODE>BZ_STREAM_END</CODE> 
         if the logical end of stream was detected.
      <CODE>BZ_OK</CODE>
         otherwise.
</PRE>

<P>
Possible return values:

<PRE>
      number of bytes read
         if <CODE>bzerror</CODE> is <CODE>BZ_OK</CODE> or <CODE>BZ_STREAM_END</CODE>
      undefined
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      collect data from <CODE>buf</CODE>, then <CODE>BZ2_bzRead</CODE> or <CODE>BZ2_bzReadClose</CODE>
         if <CODE>bzerror</CODE> is <CODE>BZ_OK</CODE> 
      collect data from <CODE>buf</CODE>, then <CODE>BZ2_bzReadClose</CODE> or <CODE>BZ2_bzReadGetUnused</CODE> 
         if <CODE>bzerror</CODE> is <CODE>BZ_SEQUENCE_END</CODE>   
      <CODE>BZ2_bzReadClose</CODE> 
         otherwise
</PRE>



<H3><A NAME="SEC28" HREF="manual_toc.html#TOC28"><CODE>BZ2_bzReadGetUnused</CODE></A></H3>

<PRE>
   void BZ2_bzReadGetUnused ( int* bzerror, BZFILE *b, 
                              void** unused, int* nUnused );
</PRE>

<P>
Returns data which was read from the compressed file but was not needed
to get to the logical end-of-stream.  <CODE>*unused</CODE> is set to the address
of the data, and <CODE>*nUnused</CODE> to the number of bytes.  <CODE>*nUnused</CODE> will
be set to a value between <CODE>0</CODE> and <CODE>BZ_MAX_UNUSED</CODE> inclusive.

</P>
<P>
This function may only be called once <CODE>BZ2_bzRead</CODE> has signalled 
<CODE>BZ_STREAM_END</CODE> but before <CODE>BZ2_bzReadClose</CODE>.

</P>
<P>
Possible assignments to <CODE>bzerror</CODE>:

<PRE>
      <CODE>BZ_PARAM_ERROR</CODE> 
         if <CODE>b</CODE> is <CODE>NULL</CODE> 
         or <CODE>unused</CODE> is <CODE>NULL</CODE> or <CODE>nUnused</CODE> is <CODE>NULL</CODE>
      <CODE>BZ_SEQUENCE_ERROR</CODE> 
         if <CODE>BZ_STREAM_END</CODE> has not been signalled
         or if <CODE>b</CODE> was opened with <CODE>BZ2_bzWriteOpen</CODE>
     <CODE>BZ_OK</CODE>
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      <CODE>BZ2_bzReadClose</CODE>
</PRE>



<H3><A NAME="SEC29" HREF="manual_toc.html#TOC29"><CODE>BZ2_bzReadClose</CODE></A></H3>

<PRE>
   void BZ2_bzReadClose ( int *bzerror, BZFILE *b );
</PRE>

<P>
Releases all memory pertaining to the compressed file <CODE>b</CODE>.  
<CODE>BZ2_bzReadClose</CODE> does not call <CODE>fclose</CODE> on the underlying file
handle, so you should do that yourself if appropriate.
<CODE>BZ2_bzReadClose</CODE> should be called to clean up after all error
situations.

</P>
<P>
Possible assignments to <CODE>bzerror</CODE>:

<PRE>
      <CODE>BZ_SEQUENCE_ERROR</CODE> 
         if <CODE>b</CODE> was opened with <CODE>BZ2_bzOpenWrite</CODE> 
      <CODE>BZ_OK</CODE> 
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      none
</PRE>



<H3><A NAME="SEC30" HREF="manual_toc.html#TOC30"><CODE>BZ2_bzWriteOpen</CODE></A></H3>

<PRE>
   BZFILE *BZ2_bzWriteOpen ( int *bzerror, FILE *f, 
                             int blockSize100k, int verbosity,
                             int workFactor );
</PRE>

<P>
Prepare to write compressed data to file handle <CODE>f</CODE>.  
<CODE>f</CODE> should refer to
a file which has been opened for writing, and for which the error
indicator (<CODE>ferror(f)</CODE>)is not set.  

</P>
<P>
For the meaning of parameters <CODE>blockSize100k</CODE>,
<CODE>verbosity</CODE> and <CODE>workFactor</CODE>, see
<BR> <CODE>BZ2_bzCompressInit</CODE>.

</P>
<P>
All required memory is allocated at this stage, so if the call
completes successfully, <CODE>BZ_MEM_ERROR</CODE> cannot be signalled by a
subsequent call to <CODE>BZ2_bzWrite</CODE>.

</P>
<P>
Possible assignments to <CODE>bzerror</CODE>:

<PRE>
      <CODE>BZ_CONFIG_ERROR</CODE>
         if the library has been mis-compiled
      <CODE>BZ_PARAM_ERROR</CODE> 
         if <CODE>f</CODE> is <CODE>NULL</CODE> 
         or <CODE>blockSize100k &#60; 1</CODE> or <CODE>blockSize100k &#62; 9</CODE>
      <CODE>BZ_IO_ERROR</CODE> 
         if <CODE>ferror(f)</CODE> is nonzero
      <CODE>BZ_MEM_ERROR</CODE> 
         if insufficient memory is available
      <CODE>BZ_OK</CODE> 
         otherwise
</PRE>

<P>
Possible return values:

<PRE>
      Pointer to an abstract <CODE>BZFILE</CODE>  
         if <CODE>bzerror</CODE> is <CODE>BZ_OK</CODE>   
      <CODE>NULL</CODE> 
         otherwise
</PRE>

<P>
Allowable next actions:

<PRE>
      <CODE>BZ2_bzWrite</CODE> 
         if <CODE>bzerror</CODE> is <CODE>BZ_OK</CODE> 
         (you could go directly to <CODE>BZ2_bzWriteClose</CODE>, but this would be pretty pointless)
      <CODE>BZ2_bzWriteClose</CODE> 
         otherwise
</PRE>



<H3><A NAME="SEC31" HREF="manual_toc.html#TOC31"><CODE>BZ2_bzWrite</CODE></A></H3>

<PRE>
   void BZ2_bzWrite ( int *bzerror, BZFILE *b, void *buf, int len );
</PRE>

<P>
Absorbs <CODE>len</CODE> bytes from the buffer <CODE>buf</CODE>, eventually to be
compressed and written to the file.

</P>
<P>
Possible assignments to <CODE>bzerror</CODE>:

<PRE>
      <CODE>BZ_PARAM_ERROR</CODE> 
         if <CODE>b</CODE> is <CODE>NULL</CODE> or <CODE>buf</CODE> is <CODE>NULL</CODE> or <CODE>len &#60; 0</CODE>
      <CODE>BZ_SEQUENCE_ERROR</CODE> 
         if b was opened with <CODE>BZ2_bzReadOpen</CODE>
      <CODE>BZ_IO_ERROR</CODE> 
         if there is an error writing the compressed file.
      <CODE>BZ_OK</CODE> 
         otherwise
</PRE>



<H3><A NAME="SEC32" HREF="manual_toc.html#TOC32"><CODE>BZ2_bzWriteClose</CODE></A></H3>

<PRE>
   void BZ2_bzWriteClose ( int *bzerror, BZFILE* f,
                           int abandon,
                           unsigned int* nbytes_in,
                           unsigned int* nbytes_out );

   void BZ2_bzWriteClose64 ( int *bzerror, BZFILE* f,
                             int abandon,
                             unsigned int* nbytes_in_lo32,
                             unsigned int* nbytes_in_hi32,
                             unsigned int* nbytes_out_lo32,
                             unsigned int* nbytes_out_hi32 );
</PRE>

<P>
Compresses and flushes to the compressed file all data so far supplied
by <CODE>BZ2_bzWrite</CODE>.  The logical end-of-stream markers are also written, so
subsequent calls to <CODE>BZ2_bzWrite</CODE> are illegal.  All memory associated 
with the compressed file <CODE>b</CODE> is released.  
<CODE>fflush</CODE> is called on the
compressed file, but it is not <CODE>fclose</CODE>'d.

</P>
<P>
If <CODE>BZ2_bzWriteClose</CODE> is called to clean up after an error, the only
action is to release the memory.  The library records the error codes
issued by previous calls, so this situation will be detected
automatically.  There is no attempt to complete the compression
operation, nor to <CODE>fflush</CODE> the compressed file.  You can force this
behaviour to happen even in the case of no error, by passing a nonzero
value to <CODE>abandon</CODE>.

</P>
<P>
If <CODE>nbytes_in</CODE> is non-null, <CODE>*nbytes_in</CODE> will be set to be the
total volume of uncompressed data handled.  Similarly, <CODE>nbytes_out</CODE>
will be set to the total volume of compressed data written.  For 
compatibility with older versions of the library, <CODE>BZ2_bzWriteClose</CODE>
only yields the lower 32 bits of these counts.  Use
<CODE>BZ2_bzWriteClose64</CODE> if you want the full 64 bit counts.  These
two functions are otherwise absolutely identical.

</P>

<P>
Possible assignments to <CODE>bzerror</CODE>:

<PRE>
      <CODE>BZ_SEQUENCE_ERROR</CODE> 
         if <CODE>b</CODE> was opened with <CODE>BZ2_bzReadOpen</CODE>
      <CODE>BZ_IO_ERROR</CODE> 
         if there is an error writing the compressed file
      <CODE>BZ_OK</CODE> 
         otherwise
</PRE>



<H3><A NAME="SEC33" HREF="manual_toc.html#TOC33">Handling embedded compressed data streams</A></H3>

<P>
The high-level library facilitates use of
<CODE>bzip2</CODE> data streams which form some part of a surrounding, larger
data stream.

<UL>
<LI>For writing, the library takes an open file handle, writes

compressed data to it, <CODE>fflush</CODE>es it but does not <CODE>fclose</CODE> it.
The calling application can write its own data before and after the
compressed data stream, using that same file handle.
<LI>Reading is more complex, and the facilities are not as general

as they could be since generality is hard to reconcile with efficiency.
<CODE>BZ2_bzRead</CODE> reads from the compressed file in blocks of size
<CODE>BZ_MAX_UNUSED</CODE> bytes, and in doing so probably will overshoot
the logical end of compressed stream.
To recover this data once decompression has
ended, call <CODE>BZ2_bzReadGetUnused</CODE> after the last call of <CODE>BZ2_bzRead</CODE>
(the one returning <CODE>BZ_STREAM_END</CODE>) but before calling
<CODE>BZ2_bzReadClose</CODE>.
</UL>

<P>
This mechanism makes it easy to decompress multiple <CODE>bzip2</CODE>
streams placed end-to-end.  As the end of one stream, when <CODE>BZ2_bzRead</CODE>
returns <CODE>BZ_STREAM_END</CODE>, call <CODE>BZ2_bzReadGetUnused</CODE> to collect the
unused data (copy it into your own buffer somewhere).  
That data forms the start of the next compressed stream.
To start uncompressing that next stream, call <CODE>BZ2_bzReadOpen</CODE> again,
feeding in the unused data via the <CODE>unused</CODE>/<CODE>nUnused</CODE>
parameters.
Keep doing this until <CODE>BZ_STREAM_END</CODE> return coincides with the
physical end of file (<CODE>feof(f)</CODE>).  In this situation
<CODE>BZ2_bzReadGetUnused</CODE>
will of course return no data.

</P>
<P>
This should give some feel for how the high-level interface can be used.
If you require extra flexibility, you'll have to bite the bullet and get
to grips with the low-level interface.

</P>


<H3><A NAME="SEC34" HREF="manual_toc.html#TOC34">Standard file-reading/writing code</A></H3>
<P>
Here's how you'd write data to a compressed file:

<PRE>
FILE*   f;
BZFILE* b;
int     nBuf;
char    buf[ /* whatever size you like */ ];
int     bzerror;
int     nWritten;

f = fopen ( "myfile.bz2", "w" );
if (!f) {
   /* handle error */
}
b = BZ2_bzWriteOpen ( &#38;bzerror, f, 9 );
if (bzerror != BZ_OK) {
   BZ2_bzWriteClose ( b );
   /* handle error */
}

while ( /* condition */ ) {
   /* get data to write into buf, and set nBuf appropriately */
   nWritten = BZ2_bzWrite ( &#38;bzerror, b, buf, nBuf );
   if (bzerror == BZ_IO_ERROR) { 
      BZ2_bzWriteClose ( &#38;bzerror, b );
      /* handle error */
   }
}

BZ2_bzWriteClose ( &#38;bzerror, b );
if (bzerror == BZ_IO_ERROR) {
   /* handle error */
}
</PRE>

<P>
And to read from a compressed file:

<PRE>
FILE*   f;
BZFILE* b;
int     nBuf;
char    buf[ /* whatever size you like */ ];
int     bzerror;
int     nWritten;

f = fopen ( "myfile.bz2", "r" );
if (!f) {
   /* handle error */
}
b = BZ2_bzReadOpen ( &#38;bzerror, f, 0, NULL, 0 );
if (bzerror != BZ_OK) {
   BZ2_bzReadClose ( &#38;bzerror, b );
   /* handle error */
}

bzerror = BZ_OK;
while (bzerror == BZ_OK &#38;&#38; /* arbitrary other conditions */) {
   nBuf = BZ2_bzRead ( &#38;bzerror, b, buf, /* size of buf */ );
   if (bzerror == BZ_OK) {
      /* do something with buf[0 .. nBuf-1] */
   }
}
if (bzerror != BZ_STREAM_END) {
   BZ2_bzReadClose ( &#38;bzerror, b );
   /* handle error */
} else {
   BZ2_bzReadClose ( &#38;bzerror );
}
</PRE>



<H2><A NAME="SEC35" HREF="manual_toc.html#TOC35">Utility functions</A></H2>


<H3><A NAME="SEC36" HREF="manual_toc.html#TOC36"><CODE>BZ2_bzBuffToBuffCompress</CODE></A></H3>

<PRE>
   int BZ2_bzBuffToBuffCompress( char*         dest,
                                 unsigned int* destLen,
                                 char*         source,
                                 unsigned int  sourceLen,
                                 int           blockSize100k,
                                 int           verbosity,
                                 int           workFactor );
</PRE>

<P>
Attempts to compress the data in <CODE>source[0 .. sourceLen-1]</CODE>
into the destination buffer, <CODE>dest[0 .. *destLen-1]</CODE>.
If the destination buffer is big enough, <CODE>*destLen</CODE> is
set to the size of the compressed data, and <CODE>BZ_OK</CODE> is
returned.  If the compressed data won't fit, <CODE>*destLen</CODE>
is unchanged, and <CODE>BZ_OUTBUFF_FULL</CODE> is returned.

</P>
<P>
Compression in this manner is a one-shot event, done with a single call
to this function.  The resulting compressed data is a complete
<CODE>bzip2</CODE> format data stream.  There is no mechanism for making
additional calls to provide extra input data.  If you want that kind of
mechanism, use the low-level interface.

</P>
<P>
For the meaning of parameters <CODE>blockSize100k</CODE>, <CODE>verbosity</CODE>
and <CODE>workFactor</CODE>, <BR> see <CODE>BZ2_bzCompressInit</CODE>.

</P>
<P>
To guarantee that the compressed data will fit in its buffer, allocate
an output buffer of size 1% larger than the uncompressed data, plus
six hundred extra bytes.

</P>
<P>
<CODE>BZ2_bzBuffToBuffDecompress</CODE> will not write data at or
beyond <CODE>dest[*destLen]</CODE>, even in case of buffer overflow.

</P>
<P>
Possible return values:

<PRE>
      <CODE>BZ_CONFIG_ERROR</CODE>
         if the library has been mis-compiled
      <CODE>BZ_PARAM_ERROR</CODE> 
         if <CODE>dest</CODE> is <CODE>NULL</CODE> or <CODE>destLen</CODE> is <CODE>NULL</CODE>
         or <CODE>blockSize100k &#60; 1</CODE> or <CODE>blockSize100k &#62; 9</CODE>
         or <CODE>verbosity &#60; 0</CODE> or <CODE>verbosity &#62; 4</CODE> 
         or <CODE>workFactor &#60; 0</CODE> or <CODE>workFactor &#62; 250</CODE>
      <CODE>BZ_MEM_ERROR</CODE>
         if insufficient memory is available 
      <CODE>BZ_OUTBUFF_FULL</CODE>
         if the size of the compressed data exceeds <CODE>*destLen</CODE>
      <CODE>BZ_OK</CODE> 
         otherwise
</PRE>



<H3><A NAME="SEC37" HREF="manual_toc.html#TOC37"><CODE>BZ2_bzBuffToBuffDecompress</CODE></A></H3>

<PRE>
   int BZ2_bzBuffToBuffDecompress ( char*         dest,
                                    unsigned int* destLen,
                                    char*         source,
                                    unsigned int  sourceLen,
                                    int           small,
                                    int           verbosity );
</PRE>

<P>
Attempts to decompress the data in <CODE>source[0 .. sourceLen-1]</CODE>
into the destination buffer, <CODE>dest[0 .. *destLen-1]</CODE>.
If the destination buffer is big enough, <CODE>*destLen</CODE> is
set to the size of the uncompressed data, and <CODE>BZ_OK</CODE> is
returned.  If the compressed data won't fit, <CODE>*destLen</CODE>
is unchanged, and <CODE>BZ_OUTBUFF_FULL</CODE> is returned.

</P>
<P>
<CODE>source</CODE> is assumed to hold a complete <CODE>bzip2</CODE> format
data stream.  <BR> <CODE>BZ2_bzBuffToBuffDecompress</CODE> tries to decompress
the entirety of the stream into the output buffer.

</P>
<P>
For the meaning of parameters <CODE>small</CODE> and <CODE>verbosity</CODE>,
see <CODE>BZ2_bzDecompressInit</CODE>.

</P>
<P>
Because the compression ratio of the compressed data cannot be known in
advance, there is no easy way to guarantee that the output buffer will
be big enough.  You may of course make arrangements in your code to
record the size of the uncompressed data, but such a mechanism is beyond
the scope of this library.

</P>
<P>
<CODE>BZ2_bzBuffToBuffDecompress</CODE> will not write data at or
beyond <CODE>dest[*destLen]</CODE>, even in case of buffer overflow.

</P>
<P>
Possible return values:

<PRE>
      <CODE>BZ_CONFIG_ERROR</CODE>
         if the library has been mis-compiled
      <CODE>BZ_PARAM_ERROR</CODE> 
         if <CODE>dest</CODE> is <CODE>NULL</CODE> or <CODE>destLen</CODE> is <CODE>NULL</CODE>
         or <CODE>small != 0 &#38;&#38; small != 1</CODE>
         or <CODE>verbosity &#60; 0</CODE> or <CODE>verbosity &#62; 4</CODE> 
      <CODE>BZ_MEM_ERROR</CODE>
         if insufficient memory is available 
      <CODE>BZ_OUTBUFF_FULL</CODE>
         if the size of the compressed data exceeds <CODE>*destLen</CODE>
      <CODE>BZ_DATA_ERROR</CODE>
         if a data integrity error was detected in the compressed data
      <CODE>BZ_DATA_ERROR_MAGIC</CODE>
         if the compressed data doesn't begin with the right magic bytes
      <CODE>BZ_UNEXPECTED_EOF</CODE>
         if the compressed data ends unexpectedly
      <CODE>BZ_OK</CODE> 
         otherwise
</PRE>



<H2><A NAME="SEC38" HREF="manual_toc.html#TOC38"><CODE>zlib</CODE> compatibility functions</A></H2>
<P>
Yoshioka Tsuneo has contributed some functions to
give better <CODE>zlib</CODE> compatibility.  These functions are
<CODE>BZ2_bzopen</CODE>, <CODE>BZ2_bzread</CODE>, <CODE>BZ2_bzwrite</CODE>, <CODE>BZ2_bzflush</CODE>,
<CODE>BZ2_bzclose</CODE>,
<CODE>BZ2_bzerror</CODE> and <CODE>BZ2_bzlibVersion</CODE>.
These functions are not (yet) officially part of
the library.  If they break, you get to keep all the pieces.
Nevertheless, I think they work ok.

<PRE>
typedef void BZFILE;

const char * BZ2_bzlibVersion ( void );
</PRE>

<P>
Returns a string indicating the library version.

<PRE>
BZFILE * BZ2_bzopen  ( const char *path, const char *mode );
BZFILE * BZ2_bzdopen ( int        fd,    const char *mode );
</PRE>

<P>
Opens a <CODE>.bz2</CODE> file for reading or writing, using either its name
or a pre-existing file descriptor. 
Analogous to <CODE>fopen</CODE> and <CODE>fdopen</CODE>.

<PRE>
int BZ2_bzread  ( BZFILE* b, void* buf, int len );
int BZ2_bzwrite ( BZFILE* b, void* buf, int len );
</PRE>

<P>
Reads/writes data from/to a previously opened <CODE>BZFILE</CODE>.
Analogous to <CODE>fread</CODE> and <CODE>fwrite</CODE>.

<PRE>
int  BZ2_bzflush ( BZFILE* b );
void BZ2_bzclose ( BZFILE* b );
</PRE>

<P>
Flushes/closes a <CODE>BZFILE</CODE>.  <CODE>BZ2_bzflush</CODE> doesn't actually do
anything.  Analogous to <CODE>fflush</CODE> and <CODE>fclose</CODE>.

</P>

<PRE>
const char * BZ2_bzerror ( BZFILE *b, int *errnum )
</PRE>

<P>
Returns a string describing the more recent error status of
<CODE>b</CODE>, and also sets <CODE>*errnum</CODE> to its numerical value.

</P>



<H2><A NAME="SEC39" HREF="manual_toc.html#TOC39">Using the library in a <CODE>stdio</CODE>-free environment</A></H2>



<H3><A NAME="SEC40" HREF="manual_toc.html#TOC40">Getting rid of <CODE>stdio</CODE></A></H3>

<P>
In a deeply embedded application, you might want to use just
the memory-to-memory functions.  You can do this conveniently
by compiling the library with preprocessor symbol <CODE>BZ_NO_STDIO</CODE>
defined.  Doing this gives you a library containing only the following
eight functions:

</P>
<P>
<CODE>BZ2_bzCompressInit</CODE>, <CODE>BZ2_bzCompress</CODE>, <CODE>BZ2_bzCompressEnd</CODE> <BR>
<CODE>BZ2_bzDecompressInit</CODE>, <CODE>BZ2_bzDecompress</CODE>, <CODE>BZ2_bzDecompressEnd</CODE> <BR>
<CODE>BZ2_bzBuffToBuffCompress</CODE>, <CODE>BZ2_bzBuffToBuffDecompress</CODE>

</P>
<P>
When compiled like this, all functions will ignore <CODE>verbosity</CODE>
settings.

</P>


<H3><A NAME="SEC41" HREF="manual_toc.html#TOC41">Critical error handling</A></H3>
<P>
<CODE>libbzip2</CODE> contains a number of internal assertion checks which
should, needless to say, never be activated.  Nevertheless, if an
assertion should fail, behaviour depends on whether or not the library
was compiled with <CODE>BZ_NO_STDIO</CODE> set.

</P>
<P>
For a normal compile, an assertion failure yields the message

<PRE>
   bzip2/libbzip2: internal error number N.
   This is a bug in bzip2/libbzip2, 1.0 of 21-Mar-2000.
   Please report it to me at: jseward@acm.org.  If this happened
   when you were using some program which uses libbzip2 as a
   component, you should also report this bug to the author(s)
   of that program.  Please make an effort to report this bug;
   timely and accurate bug reports eventually lead to higher
   quality software.  Thanks.  Julian Seward, 21 March 2000.
</PRE>

<P>
where <CODE>N</CODE> is some error code number.  <CODE>exit(3)</CODE>
is then called.

</P>
<P>
For a <CODE>stdio</CODE>-free library, assertion failures result
in a call to a function declared as:

<PRE>
   extern void bz_internal_error ( int errcode );
</PRE>

<P>
The relevant code is passed as a parameter.  You should supply
such a function.

</P>
<P>
In either case, once an assertion failure has occurred, any 
<CODE>bz_stream</CODE> records involved can be regarded as invalid.
You should not attempt to resume normal operation with them.

</P>
<P>
You may, of course, change critical error handling to suit
your needs.  As I said above, critical errors indicate bugs
in the library and should not occur.  All "normal" error
situations are indicated via error return codes from functions,
and can be recovered from.

</P>



<H2><A NAME="SEC42" HREF="manual_toc.html#TOC42">Making a Windows DLL</A></H2>
<P>
Everything related to Windows has been contributed by Yoshioka Tsuneo
<BR> (<CODE>QWF00133@niftyserve.or.jp</CODE> /
<CODE>tsuneo-y@is.aist-nara.ac.jp</CODE>), so you should send your queries to
him (but perhaps Cc: me, <CODE>jseward@acm.org</CODE>).

</P>
<P>
My vague understanding of what to do is: using Visual C++ 5.0,
open the project file <CODE>libbz2.dsp</CODE>, and build.  That's all.

</P>
<P>
If you can't
open the project file for some reason, make a new one, naming these files:
<CODE>blocksort.c</CODE>, <CODE>bzlib.c</CODE>, <CODE>compress.c</CODE>, 
<CODE>crctable.c</CODE>, <CODE>decompress.c</CODE>, <CODE>huffman.c</CODE>, <BR>
<CODE>randtable.c</CODE> and <CODE>libbz2.def</CODE>.  You will also need
to name the header files <CODE>bzlib.h</CODE> and <CODE>bzlib_private.h</CODE>.

</P>
<P>
If you don't use VC++, you may need to define the proprocessor symbol
<CODE>_WIN32</CODE>. 

</P>
<P>
Finally, <CODE>dlltest.c</CODE> is a sample program using the DLL.  It has a
project file, <CODE>dlltest.dsp</CODE>.

</P>
<P>
If you just want a makefile for Visual C, have a look at
<CODE>makefile.msc</CODE>.

</P>
<P>
Be aware that if you compile <CODE>bzip2</CODE> itself on Win32, you must set
<CODE>BZ_UNIX</CODE> to 0 and <CODE>BZ_LCCWIN32</CODE> to 1, in the file
<CODE>bzip2.c</CODE>, before compiling.  Otherwise the resulting binary won't
work correctly.

</P>
<P>
I haven't tried any of this stuff myself, but it all looks plausible.

</P>

<P><HR><P>
<p>Go to the <A HREF="manual_1.html">first</A>, <A HREF="manual_2.html">previous</A>, <A HREF="manual_4.html">next</A>, <A HREF="manual_4.html">last</A> section, <A HREF="manual_toc.html">table of contents</A>.
</BODY>
</HTML>