-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanual_3.html
1855 lines (1678 loc) · 92.4 KB
/
manual_3.html
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
<HTML>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Created on January, 5 2002 by texi2html 1.64 -->
<!--
Written by: Lionel Cons <[email protected]> (original author)
Karl Berry <[email protected]>
Olaf Bachmann <[email protected]>
and many others.
Maintained by: Olaf Bachmann <[email protected]>
Send bugs and suggestions to <[email protected]>
-->
<HEAD>
<TITLE>Untitled Document: 3. Programming with <CODE>libbzip2</CODE></TITLE>
<META NAME="description" CONTENT="Untitled Document: 3. Programming with <CODE>libbzip2</CODE>">
<META NAME="keywords" CONTENT="Untitled Document: 3. Programming with <CODE>libbzip2</CODE>">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.64">
</HEAD>
<BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<A NAME="SEC12"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_2.html#SEC11"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC13"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H1> 3. Programming with <CODE>libbzip2</CODE> </H1>
<!--docid::SEC12::-->
<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><P>
<HR SIZE="6">
<A NAME="SEC13"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC12"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC14"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.1 Top-level structure </H2>
<!--docid::SEC13::-->
<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><P>
<HR SIZE="6">
<A NAME="SEC14"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC13"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC15"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.1.1 Low-level summary </H3>
<!--docid::SEC14::-->
<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><P>
<HR SIZE="6">
<A NAME="SEC15"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC14"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC16"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.1.2 High-level summary </H3>
<!--docid::SEC15::-->
<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><P>
<HR SIZE="6">
<A NAME="SEC16"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC15"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC17"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.1.3 Utility functions summary </H3>
<!--docid::SEC16::-->
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>
Yoshioka Tsuneo (<CODE>[email protected]</CODE> /
<CODE>[email protected]</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><P>
<HR SIZE="6">
<A NAME="SEC17"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC16"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC18"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.2 Error handling </H2>
<!--docid::SEC17::-->
<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>
<P>
<HR SIZE="6">
<A NAME="SEC18"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC17"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC19"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.3 Low-level interface </H2>
<!--docid::SEC18::-->
<P>
<HR SIZE="6">
<A NAME="SEC19"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC18"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC20"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.3.1 <CODE>BZ2_bzCompressInit</CODE> </H3>
<!--docid::SEC19::-->
<TABLE><tr><td> </td><td class=example><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></td></tr></table><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 << 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:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <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> < 1 or <CODE>blockSize</CODE> > 9
or <CODE>verbosity</CODE> < 0 or <CODE>verbosity</CODE> > 4
or <CODE>workFactor</CODE> < 0 or <CODE>workFactor</CODE> > 250
<CODE>BZ_MEM_ERROR</CODE>
if not enough memory is available
<CODE>BZ_OK</CODE>
otherwise
</pre></td></tr></table>Allowable next actions:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ2_bzCompress</CODE>
if <CODE>BZ_OK</CODE> is returned
no specific action needed in case of error
</pre></td></tr></table></P><P>
<HR SIZE="6">
<A NAME="SEC20"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC19"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC21"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.3.2 <CODE>BZ2_bzCompress</CODE> </H3>
<!--docid::SEC20::-->
<TABLE><tr><td> </td><td class=example><pre> int BZ2_bzCompress ( bz_stream *strm, int action );
</pre></td></tr></table>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>
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>.
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif">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></td></tr></table></P><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>
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>
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:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ_PARAM_ERROR</CODE>
if <CODE>strm</CODE> is <CODE>NULL</CODE>, or <CODE>strm->s</CODE> is <CODE>NULL</CODE>
</pre></td></tr></table></P><P>
<HR SIZE="6">
<A NAME="SEC21"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC20"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC22"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.3.3 <CODE>BZ2_bzCompressEnd</CODE> </H3>
<!--docid::SEC21::-->
<TABLE><tr><td> </td><td class=example><pre>int BZ2_bzCompressEnd ( bz_stream *strm );
</pre></td></tr></table>Releases all memory associated with a compression stream.
<P>
Possible return values:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ_PARAM_ERROR</CODE> if <CODE>strm</CODE> is <CODE>NULL</CODE> or <CODE>strm->s</CODE> is <CODE>NULL</CODE>
<CODE>BZ_OK</CODE> otherwise
</pre></td></tr></table></P><P>
<HR SIZE="6">
<A NAME="SEC22"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC21"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC23"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.3.4 <CODE>BZ2_bzDecompressInit</CODE> </H3>
<!--docid::SEC22::-->
<TABLE><tr><td> </td><td class=example><pre>int BZ2_bzDecompressInit ( bz_stream *strm, int verbosity, int small );
</pre></td></tr></table>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>
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:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ_CONFIG_ERROR</CODE>
if the library has been mis-compiled
<CODE>BZ_PARAM_ERROR</CODE>
if <CODE>(small != 0 && small != 1)</CODE>
or <CODE>(verbosity < 0 || verbosity > 4)</CODE>
<CODE>BZ_MEM_ERROR</CODE>
if insufficient memory is available
</pre></td></tr></table></P><P>
Allowable next actions:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ2_bzDecompress</CODE>
if <CODE>BZ_OK</CODE> was returned
no specific action required in case of error
</pre></td></tr></table></P><P>
</P><P>
<HR SIZE="6">
<A NAME="SEC23"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC22"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC24"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.3.5 <CODE>BZ2_bzDecompress</CODE> </H3>
<!--docid::SEC23::-->
<TABLE><tr><td> </td><td class=example><pre>int BZ2_bzDecompress ( bz_stream *strm );
</pre></td></tr></table>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>
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:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ_PARAM_ERROR</CODE>
if <CODE>strm</CODE> is <CODE>NULL</CODE> or <CODE>strm->s</CODE> is <CODE>NULL</CODE>
or <CODE>strm->avail_out < 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->avail_out > 0</CODE>
<CODE>BZ_OK</CODE>
otherwise
</pre></td></tr></table>Allowable next actions:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ2_bzDecompress</CODE>
if <CODE>BZ_OK</CODE> was returned
<CODE>BZ2_bzDecompressEnd</CODE>
otherwise
</pre></td></tr></table></P><P>
<HR SIZE="6">
<A NAME="SEC24"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC23"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC25"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.3.6 <CODE>BZ2_bzDecompressEnd</CODE> </H3>
<!--docid::SEC24::-->
<TABLE><tr><td> </td><td class=example><pre>int BZ2_bzDecompressEnd ( bz_stream *strm );
</pre></td></tr></table>Releases all memory associated with a decompression stream.
<P>
Possible return values:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ_PARAM_ERROR</CODE>
if <CODE>strm</CODE> is <CODE>NULL</CODE> or <CODE>strm->s</CODE> is <CODE>NULL</CODE>
<CODE>BZ_OK</CODE>
otherwise
</pre></td></tr></table></P><P>
Allowable next actions:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> None.
</pre></td></tr></table></P><P>
<HR SIZE="6">
<A NAME="SEC25"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC24"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC26"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.4 High-level interface </H2>
<!--docid::SEC25::-->
<P>
This interface provides functions for reading and writing
<CODE>bzip2</CODE> format files. First, some general points.
</P><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>
<P>
<HR SIZE="6">
<A NAME="SEC26"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC25"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_3.html#SEC27"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[ >> ]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="manual_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.4.1 <CODE>BZ2_bzReadOpen</CODE> </H3>
<!--docid::SEC26::-->
<TABLE><tr><td> </td><td class=example><pre> typedef void BZFILE;
BZFILE *BZ2_bzReadOpen ( int *bzerror, FILE *f,
int small, int verbosity,
void *unused, int nUnused );
</pre></td></tr></table>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>
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>:
<TABLE><tr><td> </td><td class=display><pre style="font-family: serif"> <CODE>BZ_CONFIG_ERROR</CODE>
if the library has been mis-compiled
<CODE>BZ_PARAM_ERROR</CODE>