• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>bzip2 and libbzip2, version 1.0.8</title>
5<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
6<link rel="stylesheet" type="text/css" href="bzip.css" />
7  <style type="text/css" media="screen">/* Colours:
8#74240f  dark brown      h1, h2, h3, h4
9#336699  medium blue     links
10#339999  turquoise       link hover colour
11#202020  almost black    general text
12#761596  purple          md5sum text
13#626262  dark gray       pre border
14#eeeeee  very light gray pre background
15#f2f2f9  very light blue nav table background
16#3366cc  medium blue     nav table border
17*/
18
19a, a:link, a:visited, a:active { color: #336699; }
20a:hover { color: #339999; }
21
22body { font: 80%/126% sans-serif; }
23h1, h2, h3, h4 { color: #74240f; }
24
25dt { color: #336699; font-weight: bold }
26dd {
27 margin-left: 1.5em;
28 padding-bottom: 0.8em;
29}
30
31/* -- ruler -- */
32div.hr_blue {
33  height:  3px;
34  background:#ffffff url("../images/hr_blue.png") repeat-x; }
35div.hr_blue hr { display:none; }
36
37/* release styles */
38#release p { margin-top: 0.4em; }
39#release .md5sum { color: #761596; }
40
41
42/* ------ styles for docs|manuals|howto ------ */
43/* -- lists -- */
44ul  {
45 margin:     0px 4px 16px 16px;
46 padding:    0px;
47 list-style: url("../images/li-blue.png");
48}
49ul li {
50 margin-bottom: 10px;
51}
52ul ul	{
53 list-style-type:  none;
54 list-style-image: none;
55 margin-left:      0px;
56}
57
58/* header / footer nav tables */
59table.nav {
60 border:     solid 1px #3366cc;
61 background: #f2f2f9;
62 background-color: #f2f2f9;
63 margin-bottom: 0.5em;
64}
65/* don't have underlined links in chunked nav menus */
66table.nav a { text-decoration: none; }
67table.nav a:hover { text-decoration: underline; }
68table.nav td { font-size: 85%; }
69
70code, tt, pre { font-size: 120%; }
71code, tt { color: #761596; }
72
73div.literallayout, pre.programlisting, pre.screen {
74 color:      #000000;
75 padding:    0.5em;
76 background: #eeeeee;
77 border:     1px solid #626262;
78 background-color: #eeeeee;
79 margin: 4px 0px 4px 0px;
80}
81</style>
82</head>
83<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div lang="en" class="book">
84<div class="titlepage">
85<div>
86<div><h1 class="title">
87<a name="userman"></a>bzip2 and libbzip2, version 1.0.8</h1></div>
88<div><h2 class="subtitle">A program and library for data compression</h2></div>
89<div><div class="authorgroup"><div class="author">
90<h3 class="author">
91<span class="firstname">Julian</span> <span class="surname">Seward</span>
92</h3>
93<div class="affiliation"><span class="orgname">https://sourceware.org/bzip2/<br></span></div>
94</div></div></div>
95<div><p class="releaseinfo">Version 1.0.8 of 13 July 2019</p></div>
96<div><p class="copyright">Copyright © 1996-2019 Julian Seward</p></div>
97<div><div class="legalnotice">
98<a name="legal"></a><p>This program, <code class="computeroutput">bzip2</code>, the
99  associated library <code class="computeroutput">libbzip2</code>, and
100  all documentation, are copyright © 1996-2019 Julian Seward.
101  All rights reserved.</p>
102<p>Redistribution and use in source and binary forms, with
103  or without modification, are permitted provided that the
104  following conditions are met:</p>
105<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
106<li class="listitem" style="list-style-type: disc"><p>Redistributions of source code must retain the
107   above copyright notice, this list of conditions and the
108   following disclaimer.</p></li>
109<li class="listitem" style="list-style-type: disc"><p>The origin of this software must not be
110   misrepresented; you must not claim that you wrote the original
111   software.  If you use this software in a product, an
112   acknowledgment in the product documentation would be
113   appreciated but is not required.</p></li>
114<li class="listitem" style="list-style-type: disc"><p>Altered source versions must be plainly marked
115   as such, and must not be misrepresented as being the original
116   software.</p></li>
117<li class="listitem" style="list-style-type: disc"><p>The name of the author may not be used to
118   endorse or promote products derived from this software without
119   specific prior written permission.</p></li>
120</ul></div>
121<p>THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY
122  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
123  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
124  PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
125  AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
126  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
127  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
128  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
129  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
130  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
131  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
132  THE POSSIBILITY OF SUCH DAMAGE.</p>
133<p>PATENTS: To the best of my knowledge,
134 <code class="computeroutput">bzip2</code> and
135 <code class="computeroutput">libbzip2</code> do not use any patented
136 algorithms.  However, I do not have the resources to carry
137 out a patent search.  Therefore I cannot give any guarantee of
138 the above statement.
139 </p>
140</div></div>
141</div>
142<hr>
143</div>
144<div class="toc">
145<p><b>Table of Contents</b></p>
146<dl class="toc">
147<dt><span class="chapter"><a href="#intro">1. Introduction</a></span></dt>
148<dt><span class="chapter"><a href="#using">2. How to use bzip2</a></span></dt>
149<dd><dl>
150<dt><span class="sect1"><a href="#name">2.1. NAME</a></span></dt>
151<dt><span class="sect1"><a href="#synopsis">2.2. SYNOPSIS</a></span></dt>
152<dt><span class="sect1"><a href="#description">2.3. DESCRIPTION</a></span></dt>
153<dt><span class="sect1"><a href="#options">2.4. OPTIONS</a></span></dt>
154<dt><span class="sect1"><a href="#memory-management">2.5. MEMORY MANAGEMENT</a></span></dt>
155<dt><span class="sect1"><a href="#recovering">2.6. RECOVERING DATA FROM DAMAGED FILES</a></span></dt>
156<dt><span class="sect1"><a href="#performance">2.7. PERFORMANCE NOTES</a></span></dt>
157<dt><span class="sect1"><a href="#caveats">2.8. CAVEATS</a></span></dt>
158<dt><span class="sect1"><a href="#author">2.9. AUTHOR</a></span></dt>
159</dl></dd>
160<dt><span class="chapter"><a href="#libprog">3.
161Programming with <code class="computeroutput">libbzip2</code>
162</a></span></dt>
163<dd><dl>
164<dt><span class="sect1"><a href="#top-level">3.1. Top-level structure</a></span></dt>
165<dd><dl>
166<dt><span class="sect2"><a href="#ll-summary">3.1.1. Low-level summary</a></span></dt>
167<dt><span class="sect2"><a href="#hl-summary">3.1.2. High-level summary</a></span></dt>
168<dt><span class="sect2"><a href="#util-fns-summary">3.1.3. Utility functions summary</a></span></dt>
169</dl></dd>
170<dt><span class="sect1"><a href="#err-handling">3.2. Error handling</a></span></dt>
171<dt><span class="sect1"><a href="#low-level">3.3. Low-level interface</a></span></dt>
172<dd><dl>
173<dt><span class="sect2"><a href="#bzcompress-init">3.3.1. BZ2_bzCompressInit</a></span></dt>
174<dt><span class="sect2"><a href="#bzCompress">3.3.2. BZ2_bzCompress</a></span></dt>
175<dt><span class="sect2"><a href="#bzCompress-end">3.3.3. BZ2_bzCompressEnd</a></span></dt>
176<dt><span class="sect2"><a href="#bzDecompress-init">3.3.4. BZ2_bzDecompressInit</a></span></dt>
177<dt><span class="sect2"><a href="#bzDecompress">3.3.5. BZ2_bzDecompress</a></span></dt>
178<dt><span class="sect2"><a href="#bzDecompress-end">3.3.6. BZ2_bzDecompressEnd</a></span></dt>
179</dl></dd>
180<dt><span class="sect1"><a href="#hl-interface">3.4. High-level interface</a></span></dt>
181<dd><dl>
182<dt><span class="sect2"><a href="#bzreadopen">3.4.1. BZ2_bzReadOpen</a></span></dt>
183<dt><span class="sect2"><a href="#bzread">3.4.2. BZ2_bzRead</a></span></dt>
184<dt><span class="sect2"><a href="#bzreadgetunused">3.4.3. BZ2_bzReadGetUnused</a></span></dt>
185<dt><span class="sect2"><a href="#bzreadclose">3.4.4. BZ2_bzReadClose</a></span></dt>
186<dt><span class="sect2"><a href="#bzwriteopen">3.4.5. BZ2_bzWriteOpen</a></span></dt>
187<dt><span class="sect2"><a href="#bzwrite">3.4.6. BZ2_bzWrite</a></span></dt>
188<dt><span class="sect2"><a href="#bzwriteclose">3.4.7. BZ2_bzWriteClose</a></span></dt>
189<dt><span class="sect2"><a href="#embed">3.4.8. Handling embedded compressed data streams</a></span></dt>
190<dt><span class="sect2"><a href="#std-rdwr">3.4.9. Standard file-reading/writing code</a></span></dt>
191</dl></dd>
192<dt><span class="sect1"><a href="#util-fns">3.5. Utility functions</a></span></dt>
193<dd><dl>
194<dt><span class="sect2"><a href="#bzbufftobuffcompress">3.5.1. BZ2_bzBuffToBuffCompress</a></span></dt>
195<dt><span class="sect2"><a href="#bzbufftobuffdecompress">3.5.2. BZ2_bzBuffToBuffDecompress</a></span></dt>
196</dl></dd>
197<dt><span class="sect1"><a href="#zlib-compat">3.6. zlib compatibility functions</a></span></dt>
198<dt><span class="sect1"><a href="#stdio-free">3.7. Using the library in a stdio-free environment</a></span></dt>
199<dd><dl>
200<dt><span class="sect2"><a href="#stdio-bye">3.7.1. Getting rid of stdio</a></span></dt>
201<dt><span class="sect2"><a href="#critical-error">3.7.2. Critical error handling</a></span></dt>
202</dl></dd>
203<dt><span class="sect1"><a href="#win-dll">3.8. Making a Windows DLL</a></span></dt>
204</dl></dd>
205<dt><span class="chapter"><a href="#misc">4. Miscellanea</a></span></dt>
206<dd><dl>
207<dt><span class="sect1"><a href="#limits">4.1. Limitations of the compressed file format</a></span></dt>
208<dt><span class="sect1"><a href="#port-issues">4.2. Portability issues</a></span></dt>
209<dt><span class="sect1"><a href="#bugs">4.3. Reporting bugs</a></span></dt>
210<dt><span class="sect1"><a href="#package">4.4. Did you get the right package?</a></span></dt>
211<dt><span class="sect1"><a href="#reading">4.5. Further Reading</a></span></dt>
212</dl></dd>
213</dl>
214</div>
215<div class="chapter">
216<div class="titlepage"><div><div><h1 class="title">
217<a name="intro"></a>1. Introduction</h1></div></div></div>
218<p><code class="computeroutput">bzip2</code> compresses files
219using the Burrows-Wheeler block-sorting text compression
220algorithm, and Huffman coding.  Compression is generally
221considerably better than that achieved by more conventional
222LZ77/LZ78-based compressors, and approaches the performance of
223the PPM family of statistical compressors.</p>
224<p><code class="computeroutput">bzip2</code> is built on top of
225<code class="computeroutput">libbzip2</code>, a flexible library for
226handling compressed data in the
227<code class="computeroutput">bzip2</code> format.  This manual
228describes both how to use the program and how to work with the
229library interface.  Most of the manual is devoted to this
230library, not the program, which is good news if your interest is
231only in the program.</p>
232<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
233<li class="listitem" style="list-style-type: disc"><p><a class="xref" href="#using" title="2. How to use bzip2">How to use bzip2</a> describes how to use
234 <code class="computeroutput">bzip2</code>; this is the only part
235 you need to read if you just want to know how to operate the
236 program.</p></li>
237<li class="listitem" style="list-style-type: disc"><p><a class="xref" href="#libprog" title="3.  Programming with libbzip2">Programming with libbzip2</a> describes the
238 programming interfaces in detail, and</p></li>
239<li class="listitem" style="list-style-type: disc"><p><a class="xref" href="#misc" title="4. Miscellanea">Miscellanea</a> records some
240 miscellaneous notes which I thought ought to be recorded
241 somewhere.</p></li>
242</ul></div>
243</div>
244<div class="chapter">
245<div class="titlepage"><div><div><h1 class="title">
246<a name="using"></a>2. How to use bzip2</h1></div></div></div>
247<div class="toc">
248<p><b>Table of Contents</b></p>
249<dl class="toc">
250<dt><span class="sect1"><a href="#name">2.1. NAME</a></span></dt>
251<dt><span class="sect1"><a href="#synopsis">2.2. SYNOPSIS</a></span></dt>
252<dt><span class="sect1"><a href="#description">2.3. DESCRIPTION</a></span></dt>
253<dt><span class="sect1"><a href="#options">2.4. OPTIONS</a></span></dt>
254<dt><span class="sect1"><a href="#memory-management">2.5. MEMORY MANAGEMENT</a></span></dt>
255<dt><span class="sect1"><a href="#recovering">2.6. RECOVERING DATA FROM DAMAGED FILES</a></span></dt>
256<dt><span class="sect1"><a href="#performance">2.7. PERFORMANCE NOTES</a></span></dt>
257<dt><span class="sect1"><a href="#caveats">2.8. CAVEATS</a></span></dt>
258<dt><span class="sect1"><a href="#author">2.9. AUTHOR</a></span></dt>
259</dl>
260</div>
261<p>This chapter contains a copy of the
262<code class="computeroutput">bzip2</code> man page, and nothing
263else.</p>
264<div class="sect1">
265<div class="titlepage"><div><div><h2 class="title" style="clear: both">
266<a name="name"></a>2.1. NAME</h2></div></div></div>
267<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
268<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bzip2</code>,
269  <code class="computeroutput">bunzip2</code> - a block-sorting file
270  compressor, v1.0.8</p></li>
271<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bzcat</code> -
272   decompresses files to stdout</p></li>
273<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bzip2recover</code> -
274   recovers data from damaged bzip2 files</p></li>
275</ul></div>
276</div>
277<div class="sect1">
278<div class="titlepage"><div><div><h2 class="title" style="clear: both">
279<a name="synopsis"></a>2.2. SYNOPSIS</h2></div></div></div>
280<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
281<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bzip2</code> [
282  -cdfkqstvzVL123456789 ] [ filenames ...  ]</p></li>
283<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bunzip2</code> [
284  -fkvsVL ] [ filenames ...  ]</p></li>
285<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bzcat</code> [ -s ] [
286  filenames ...  ]</p></li>
287<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bzip2recover</code>
288  filename</p></li>
289</ul></div>
290</div>
291<div class="sect1">
292<div class="titlepage"><div><div><h2 class="title" style="clear: both">
293<a name="description"></a>2.3. DESCRIPTION</h2></div></div></div>
294<p><code class="computeroutput">bzip2</code> compresses files
295using the Burrows-Wheeler block sorting text compression
296algorithm, and Huffman coding.  Compression is generally
297considerably better than that achieved by more conventional
298LZ77/LZ78-based compressors, and approaches the performance of
299the PPM family of statistical compressors.</p>
300<p>The command-line options are deliberately very similar to
301those of GNU <code class="computeroutput">gzip</code>, but they are
302not identical.</p>
303<p><code class="computeroutput">bzip2</code> expects a list of
304file names to accompany the command-line flags.  Each file is
305replaced by a compressed version of itself, with the name
306<code class="computeroutput">original_name.bz2</code>.  Each
307compressed file has the same modification date, permissions, and,
308when possible, ownership as the corresponding original, so that
309these properties can be correctly restored at decompression time.
310File name handling is naive in the sense that there is no
311mechanism for preserving original file names, permissions,
312ownerships or dates in filesystems which lack these concepts, or
313have serious file name length restrictions, such as
314MS-DOS.</p>
315<p><code class="computeroutput">bzip2</code> and
316<code class="computeroutput">bunzip2</code> will by default not
317overwrite existing files.  If you want this to happen, specify
318the <code class="computeroutput">-f</code> flag.</p>
319<p>If no file names are specified,
320<code class="computeroutput">bzip2</code> compresses from standard
321input to standard output.  In this case,
322<code class="computeroutput">bzip2</code> will decline to write
323compressed output to a terminal, as this would be entirely
324incomprehensible and therefore pointless.</p>
325<p><code class="computeroutput">bunzip2</code> (or
326<code class="computeroutput">bzip2 -d</code>) decompresses all
327specified files.  Files which were not created by
328<code class="computeroutput">bzip2</code> will be detected and
329ignored, and a warning issued.
330<code class="computeroutput">bzip2</code> attempts to guess the
331filename for the decompressed file from that of the compressed
332file as follows:</p>
333<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
334<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">filename.bz2 </code>
335  becomes
336  <code class="computeroutput">filename</code></p></li>
337<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">filename.bz </code>
338  becomes
339  <code class="computeroutput">filename</code></p></li>
340<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">filename.tbz2</code>
341  becomes
342  <code class="computeroutput">filename.tar</code></p></li>
343<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">filename.tbz </code>
344  becomes
345  <code class="computeroutput">filename.tar</code></p></li>
346<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">anyothername </code>
347  becomes
348  <code class="computeroutput">anyothername.out</code></p></li>
349</ul></div>
350<p>If the file does not end in one of the recognised endings,
351<code class="computeroutput">.bz2</code>,
352<code class="computeroutput">.bz</code>,
353<code class="computeroutput">.tbz2</code> or
354<code class="computeroutput">.tbz</code>,
355<code class="computeroutput">bzip2</code> complains that it cannot
356guess the name of the original file, and uses the original name
357with <code class="computeroutput">.out</code> appended.</p>
358<p>As with compression, supplying no filenames causes
359decompression from standard input to standard output.</p>
360<p><code class="computeroutput">bunzip2</code> will correctly
361decompress a file which is the concatenation of two or more
362compressed files.  The result is the concatenation of the
363corresponding uncompressed files.  Integrity testing
364(<code class="computeroutput">-t</code>) of concatenated compressed
365files is also supported.</p>
366<p>You can also compress or decompress files to the standard
367output by giving the <code class="computeroutput">-c</code> flag.
368Multiple files may be compressed and decompressed like this.  The
369resulting outputs are fed sequentially to stdout.  Compression of
370multiple files in this manner generates a stream containing
371multiple compressed file representations.  Such a stream can be
372decompressed correctly only by
373<code class="computeroutput">bzip2</code> version 0.9.0 or later.
374Earlier versions of <code class="computeroutput">bzip2</code> will
375stop after decompressing the first file in the stream.</p>
376<p><code class="computeroutput">bzcat</code> (or
377<code class="computeroutput">bzip2 -dc</code>) decompresses all
378specified files to the standard output.</p>
379<p><code class="computeroutput">bzip2</code> will read arguments
380from the environment variables
381<code class="computeroutput">BZIP2</code> and
382<code class="computeroutput">BZIP</code>, in that order, and will
383process them before any arguments read from the command line.
384This gives a convenient way to supply default arguments.</p>
385<p>Compression is always performed, even if the compressed
386file is slightly larger than the original.  Files of less than
387about one hundred bytes tend to get larger, since the compression
388mechanism has a constant overhead in the region of 50 bytes.
389Random data (including the output of most file compressors) is
390coded at about 8.05 bits per byte, giving an expansion of around
3910.5%.</p>
392<p>As a self-check for your protection,
393<code class="computeroutput">bzip2</code> uses 32-bit CRCs to make
394sure that the decompressed version of a file is identical to the
395original.  This guards against corruption of the compressed data,
396and against undetected bugs in
397<code class="computeroutput">bzip2</code> (hopefully very unlikely).
398The chances of data corruption going undetected is microscopic,
399about one chance in four billion for each file processed.  Be
400aware, though, that the check occurs upon decompression, so it
401can only tell you that something is wrong.  It can't help you
402recover the original uncompressed data.  You can use
403<code class="computeroutput">bzip2recover</code> to try to recover
404data from damaged files.</p>
405<p>Return values: 0 for a normal exit, 1 for environmental
406problems (file not found, invalid flags, I/O errors, etc.), 2
407to indicate a corrupt compressed file, 3 for an internal
408consistency error (eg, bug) which caused
409<code class="computeroutput">bzip2</code> to panic.</p>
410</div>
411<div class="sect1">
412<div class="titlepage"><div><div><h2 class="title" style="clear: both">
413<a name="options"></a>2.4. OPTIONS</h2></div></div></div>
414<div class="variablelist"><dl class="variablelist">
415<dt><span class="term"><code class="computeroutput">-c --stdout</code></span></dt>
416<dd><p>Compress or decompress to standard
417  output.</p></dd>
418<dt><span class="term"><code class="computeroutput">-d --decompress</code></span></dt>
419<dd><p>Force decompression.
420  <code class="computeroutput">bzip2</code>,
421  <code class="computeroutput">bunzip2</code> and
422  <code class="computeroutput">bzcat</code> are really the same
423  program, and the decision about what actions to take is done on
424  the basis of which name is used.  This flag overrides that
425  mechanism, and forces bzip2 to decompress.</p></dd>
426<dt><span class="term"><code class="computeroutput">-z --compress</code></span></dt>
427<dd><p>The complement to
428  <code class="computeroutput">-d</code>: forces compression,
429  regardless of the invokation name.</p></dd>
430<dt><span class="term"><code class="computeroutput">-t --test</code></span></dt>
431<dd><p>Check integrity of the specified file(s), but
432  don't decompress them.  This really performs a trial
433  decompression and throws away the result.</p></dd>
434<dt><span class="term"><code class="computeroutput">-f --force</code></span></dt>
435<dd>
436<p>Force overwrite of output files.  Normally,
437  <code class="computeroutput">bzip2</code> will not overwrite
438  existing output files.  Also forces
439  <code class="computeroutput">bzip2</code> to break hard links to
440  files, which it otherwise wouldn't do.</p>
441<p><code class="computeroutput">bzip2</code> normally declines
442  to decompress files which don't have the correct magic header
443  bytes. If forced (<code class="computeroutput">-f</code>),
444  however, it will pass such files through unmodified. This is
445  how GNU <code class="computeroutput">gzip</code> behaves.</p>
446</dd>
447<dt><span class="term"><code class="computeroutput">-k --keep</code></span></dt>
448<dd><p>Keep (don't delete) input files during
449  compression or decompression.</p></dd>
450<dt><span class="term"><code class="computeroutput">-s --small</code></span></dt>
451<dd>
452<p>Reduce memory usage, for compression,
453  decompression and testing.  Files are decompressed and tested
454  using a modified algorithm which only requires 2.5 bytes per
455  block byte.  This means any file can be decompressed in 2300k
456  of memory, albeit at about half the normal speed.</p>
457<p>During compression, <code class="computeroutput">-s</code>
458  selects a block size of 200k, which limits memory use to around
459  the same figure, at the expense of your compression ratio.  In
460  short, if your machine is low on memory (8 megabytes or less),
461  use <code class="computeroutput">-s</code> for everything.  See
462  <a class="xref" href="#memory-management" title="2.5. MEMORY MANAGEMENT">MEMORY MANAGEMENT</a> below.</p>
463</dd>
464<dt><span class="term"><code class="computeroutput">-q --quiet</code></span></dt>
465<dd><p>Suppress non-essential warning messages.
466  Messages pertaining to I/O errors and other critical events
467  will not be suppressed.</p></dd>
468<dt><span class="term"><code class="computeroutput">-v --verbose</code></span></dt>
469<dd><p>Verbose mode -- show the compression ratio for
470  each file processed.  Further
471  <code class="computeroutput">-v</code>'s increase the verbosity
472  level, spewing out lots of information which is primarily of
473  interest for diagnostic purposes.</p></dd>
474<dt><span class="term"><code class="computeroutput">-L --license -V --version</code></span></dt>
475<dd><p>Display the software version, license terms and
476  conditions.</p></dd>
477<dt><span class="term"><code class="computeroutput">-1</code> (or
478 <code class="computeroutput">--fast</code>) to
479 <code class="computeroutput">-9</code> (or
480 <code class="computeroutput">-best</code>)</span></dt>
481<dd><p>Set the block size to 100 k, 200 k ...  900 k
482  when compressing.  Has no effect when decompressing.  See <a class="xref" href="#memory-management" title="2.5. MEMORY MANAGEMENT">MEMORY MANAGEMENT</a> below.  The
483  <code class="computeroutput">--fast</code> and
484  <code class="computeroutput">--best</code> aliases are primarily
485  for GNU <code class="computeroutput">gzip</code> compatibility.
486  In particular, <code class="computeroutput">--fast</code> doesn't
487  make things significantly faster.  And
488  <code class="computeroutput">--best</code> merely selects the
489  default behaviour.</p></dd>
490<dt><span class="term"><code class="computeroutput">--</code></span></dt>
491<dd><p>Treats all subsequent arguments as file names,
492  even if they start with a dash.  This is so you can handle
493  files with names beginning with a dash, for example:
494  <code class="computeroutput">bzip2 --
495  -myfilename</code>.</p></dd>
496<dt>
497<span class="term"><code class="computeroutput">--repetitive-fast</code>, </span><span class="term"><code class="computeroutput">--repetitive-best</code></span>
498</dt>
499<dd><p>These flags are redundant in versions 0.9.5 and
500  above.  They provided some coarse control over the behaviour of
501  the sorting algorithm in earlier versions, which was sometimes
502  useful.  0.9.5 and above have an improved algorithm which
503  renders these flags irrelevant.</p></dd>
504</dl></div>
505</div>
506<div class="sect1">
507<div class="titlepage"><div><div><h2 class="title" style="clear: both">
508<a name="memory-management"></a>2.5. MEMORY MANAGEMENT</h2></div></div></div>
509<p><code class="computeroutput">bzip2</code> compresses large
510files in blocks.  The block size affects both the compression
511ratio achieved, and the amount of memory needed for compression
512and decompression.  The flags <code class="computeroutput">-1</code>
513through <code class="computeroutput">-9</code> specify the block
514size to be 100,000 bytes through 900,000 bytes (the default)
515respectively.  At decompression time, the block size used for
516compression is read from the header of the compressed file, and
517<code class="computeroutput">bunzip2</code> then allocates itself
518just enough memory to decompress the file.  Since block sizes are
519stored in compressed files, it follows that the flags
520<code class="computeroutput">-1</code> to
521<code class="computeroutput">-9</code> are irrelevant to and so
522ignored during decompression.</p>
523<p>Compression and decompression requirements, in bytes, can be
524estimated as:</p>
525<pre class="programlisting">Compression:   400k + ( 8 x block size )
526
527Decompression: 100k + ( 4 x block size ), or
528               100k + ( 2.5 x block size )</pre>
529<p>Larger block sizes give rapidly diminishing marginal
530returns.  Most of the compression comes from the first two or
531three hundred k of block size, a fact worth bearing in mind when
532using <code class="computeroutput">bzip2</code> on small machines.
533It is also important to appreciate that the decompression memory
534requirement is set at compression time by the choice of block
535size.</p>
536<p>For files compressed with the default 900k block size,
537<code class="computeroutput">bunzip2</code> will require about 3700
538kbytes to decompress.  To support decompression of any file on a
5394 megabyte machine, <code class="computeroutput">bunzip2</code> has
540an option to decompress using approximately half this amount of
541memory, about 2300 kbytes.  Decompression speed is also halved,
542so you should use this option only where necessary.  The relevant
543flag is <code class="computeroutput">-s</code>.</p>
544<p>In general, try and use the largest block size memory
545constraints allow, since that maximises the compression achieved.
546Compression and decompression speed are virtually unaffected by
547block size.</p>
548<p>Another significant point applies to files which fit in a
549single block -- that means most files you'd encounter using a
550large block size.  The amount of real memory touched is
551proportional to the size of the file, since the file is smaller
552than a block.  For example, compressing a file 20,000 bytes long
553with the flag <code class="computeroutput">-9</code> will cause the
554compressor to allocate around 7600k of memory, but only touch
555400k + 20000 * 8 = 560 kbytes of it.  Similarly, the decompressor
556will allocate 3700k but only touch 100k + 20000 * 4 = 180
557kbytes.</p>
558<p>Here is a table which summarises the maximum memory usage
559for different block sizes.  Also recorded is the total compressed
560size for 14 files of the Calgary Text Compression Corpus
561totalling 3,141,622 bytes.  This column gives some feel for how
562compression varies with block size.  These figures tend to
563understate the advantage of larger block sizes for larger files,
564since the Corpus is dominated by smaller files.</p>
565<pre class="programlisting">        Compress   Decompress   Decompress   Corpus
566Flag     usage      usage       -s usage     Size
567
568 -1      1200k       500k         350k      914704
569 -2      2000k       900k         600k      877703
570 -3      2800k      1300k         850k      860338
571 -4      3600k      1700k        1100k      846899
572 -5      4400k      2100k        1350k      845160
573 -6      5200k      2500k        1600k      838626
574 -7      6100k      2900k        1850k      834096
575 -8      6800k      3300k        2100k      828642
576 -9      7600k      3700k        2350k      828642</pre>
577</div>
578<div class="sect1">
579<div class="titlepage"><div><div><h2 class="title" style="clear: both">
580<a name="recovering"></a>2.6. RECOVERING DATA FROM DAMAGED FILES</h2></div></div></div>
581<p><code class="computeroutput">bzip2</code> compresses files in
582blocks, usually 900kbytes long.  Each block is handled
583independently.  If a media or transmission error causes a
584multi-block <code class="computeroutput">.bz2</code> file to become
585damaged, it may be possible to recover data from the undamaged
586blocks in the file.</p>
587<p>The compressed representation of each block is delimited by
588a 48-bit pattern, which makes it possible to find the block
589boundaries with reasonable certainty.  Each block also carries
590its own 32-bit CRC, so damaged blocks can be distinguished from
591undamaged ones.</p>
592<p><code class="computeroutput">bzip2recover</code> is a simple
593program whose purpose is to search for blocks in
594<code class="computeroutput">.bz2</code> files, and write each block
595out into its own <code class="computeroutput">.bz2</code> file.  You
596can then use <code class="computeroutput">bzip2 -t</code> to test
597the integrity of the resulting files, and decompress those which
598are undamaged.</p>
599<p><code class="computeroutput">bzip2recover</code> takes a
600single argument, the name of the damaged file, and writes a
601number of files <code class="computeroutput">rec0001file.bz2</code>,
602<code class="computeroutput">rec0002file.bz2</code>, etc, containing
603the extracted blocks.  The output filenames are designed so that
604the use of wildcards in subsequent processing -- for example,
605<code class="computeroutput">bzip2 -dc rec*file.bz2 &gt;
606recovered_data</code> -- lists the files in the correct
607order.</p>
608<p><code class="computeroutput">bzip2recover</code> should be of
609most use dealing with large <code class="computeroutput">.bz2</code>
610files, as these will contain many blocks.  It is clearly futile
611to use it on damaged single-block files, since a damaged block
612cannot be recovered.  If you wish to minimise any potential data
613loss through media or transmission errors, you might consider
614compressing with a smaller block size.</p>
615</div>
616<div class="sect1">
617<div class="titlepage"><div><div><h2 class="title" style="clear: both">
618<a name="performance"></a>2.7. PERFORMANCE NOTES</h2></div></div></div>
619<p>The sorting phase of compression gathers together similar
620strings in the file.  Because of this, files containing very long
621runs of repeated symbols, like "aabaabaabaab ..."  (repeated
622several hundred times) may compress more slowly than normal.
623Versions 0.9.5 and above fare much better than previous versions
624in this respect.  The ratio between worst-case and average-case
625compression time is in the region of 10:1.  For previous
626versions, this figure was more like 100:1.  You can use the
627<code class="computeroutput">-vvvv</code> option to monitor progress
628in great detail, if you want.</p>
629<p>Decompression speed is unaffected by these
630phenomena.</p>
631<p><code class="computeroutput">bzip2</code> usually allocates
632several megabytes of memory to operate in, and then charges all
633over it in a fairly random fashion.  This means that performance,
634both for compressing and decompressing, is largely determined by
635the speed at which your machine can service cache misses.
636Because of this, small changes to the code to reduce the miss
637rate have been observed to give disproportionately large
638performance improvements.  I imagine
639<code class="computeroutput">bzip2</code> will perform best on
640machines with very large caches.</p>
641</div>
642<div class="sect1">
643<div class="titlepage"><div><div><h2 class="title" style="clear: both">
644<a name="caveats"></a>2.8. CAVEATS</h2></div></div></div>
645<p>I/O error messages are not as helpful as they could be.
646<code class="computeroutput">bzip2</code> tries hard to detect I/O
647errors and exit cleanly, but the details of what the problem is
648sometimes seem rather misleading.</p>
649<p>This manual page pertains to version 1.0.8 of
650<code class="computeroutput">bzip2</code>.  Compressed data created by
651this version is entirely forwards and backwards compatible with the
652previous public releases, versions 0.1pl2, 0.9.0 and 0.9.5, 1.0.0,
6531.0.1, 1.0.2 and 1.0.3, but with the following exception: 0.9.0 and
654above can correctly decompress multiple concatenated compressed files.
6550.1pl2 cannot do this; it will stop after decompressing just the first
656file in the stream.</p>
657<p><code class="computeroutput">bzip2recover</code> versions
658prior to 1.0.2 used 32-bit integers to represent bit positions in
659compressed files, so it could not handle compressed files more
660than 512 megabytes long.  Versions 1.0.2 and above use 64-bit ints
661on some platforms which support them (GNU supported targets, and
662Windows). To establish whether or not
663<code class="computeroutput">bzip2recover</code> was built with such
664a limitation, run it without arguments. In any event you can
665build yourself an unlimited version if you can recompile it with
666<code class="computeroutput">MaybeUInt64</code> set to be an
667unsigned 64-bit integer.</p>
668</div>
669<div class="sect1">
670<div class="titlepage"><div><div><h2 class="title" style="clear: both">
671<a name="author"></a>2.9. AUTHOR</h2></div></div></div>
672<p>Julian Seward,
673<code class="computeroutput">jseward@acm.org</code></p>
674<p>The ideas embodied in
675<code class="computeroutput">bzip2</code> are due to (at least) the
676following people: Michael Burrows and David Wheeler (for the
677block sorting transformation), David Wheeler (again, for the
678Huffman coder), Peter Fenwick (for the structured coding model in
679the original <code class="computeroutput">bzip</code>, and many
680refinements), and Alistair Moffat, Radford Neal and Ian Witten
681(for the arithmetic coder in the original
682<code class="computeroutput">bzip</code>).  I am much indebted for
683their help, support and advice.  See the manual in the source
684distribution for pointers to sources of documentation.  Christian
685von Roques encouraged me to look for faster sorting algorithms,
686so as to speed up compression.  Bela Lubkin encouraged me to
687improve the worst-case compression performance.
688Donna Robinson XMLised the documentation.
689Many people sent
690patches, helped with portability problems, lent machines, gave
691advice and were generally helpful.</p>
692</div>
693</div>
694<div class="chapter">
695<div class="titlepage"><div><div><h1 class="title">
696<a name="libprog"></a>3. 
697Programming with <code class="computeroutput">libbzip2</code>
698</h1></div></div></div>
699<div class="toc">
700<p><b>Table of Contents</b></p>
701<dl class="toc">
702<dt><span class="sect1"><a href="#top-level">3.1. Top-level structure</a></span></dt>
703<dd><dl>
704<dt><span class="sect2"><a href="#ll-summary">3.1.1. Low-level summary</a></span></dt>
705<dt><span class="sect2"><a href="#hl-summary">3.1.2. High-level summary</a></span></dt>
706<dt><span class="sect2"><a href="#util-fns-summary">3.1.3. Utility functions summary</a></span></dt>
707</dl></dd>
708<dt><span class="sect1"><a href="#err-handling">3.2. Error handling</a></span></dt>
709<dt><span class="sect1"><a href="#low-level">3.3. Low-level interface</a></span></dt>
710<dd><dl>
711<dt><span class="sect2"><a href="#bzcompress-init">3.3.1. BZ2_bzCompressInit</a></span></dt>
712<dt><span class="sect2"><a href="#bzCompress">3.3.2. BZ2_bzCompress</a></span></dt>
713<dt><span class="sect2"><a href="#bzCompress-end">3.3.3. BZ2_bzCompressEnd</a></span></dt>
714<dt><span class="sect2"><a href="#bzDecompress-init">3.3.4. BZ2_bzDecompressInit</a></span></dt>
715<dt><span class="sect2"><a href="#bzDecompress">3.3.5. BZ2_bzDecompress</a></span></dt>
716<dt><span class="sect2"><a href="#bzDecompress-end">3.3.6. BZ2_bzDecompressEnd</a></span></dt>
717</dl></dd>
718<dt><span class="sect1"><a href="#hl-interface">3.4. High-level interface</a></span></dt>
719<dd><dl>
720<dt><span class="sect2"><a href="#bzreadopen">3.4.1. BZ2_bzReadOpen</a></span></dt>
721<dt><span class="sect2"><a href="#bzread">3.4.2. BZ2_bzRead</a></span></dt>
722<dt><span class="sect2"><a href="#bzreadgetunused">3.4.3. BZ2_bzReadGetUnused</a></span></dt>
723<dt><span class="sect2"><a href="#bzreadclose">3.4.4. BZ2_bzReadClose</a></span></dt>
724<dt><span class="sect2"><a href="#bzwriteopen">3.4.5. BZ2_bzWriteOpen</a></span></dt>
725<dt><span class="sect2"><a href="#bzwrite">3.4.6. BZ2_bzWrite</a></span></dt>
726<dt><span class="sect2"><a href="#bzwriteclose">3.4.7. BZ2_bzWriteClose</a></span></dt>
727<dt><span class="sect2"><a href="#embed">3.4.8. Handling embedded compressed data streams</a></span></dt>
728<dt><span class="sect2"><a href="#std-rdwr">3.4.9. Standard file-reading/writing code</a></span></dt>
729</dl></dd>
730<dt><span class="sect1"><a href="#util-fns">3.5. Utility functions</a></span></dt>
731<dd><dl>
732<dt><span class="sect2"><a href="#bzbufftobuffcompress">3.5.1. BZ2_bzBuffToBuffCompress</a></span></dt>
733<dt><span class="sect2"><a href="#bzbufftobuffdecompress">3.5.2. BZ2_bzBuffToBuffDecompress</a></span></dt>
734</dl></dd>
735<dt><span class="sect1"><a href="#zlib-compat">3.6. zlib compatibility functions</a></span></dt>
736<dt><span class="sect1"><a href="#stdio-free">3.7. Using the library in a stdio-free environment</a></span></dt>
737<dd><dl>
738<dt><span class="sect2"><a href="#stdio-bye">3.7.1. Getting rid of stdio</a></span></dt>
739<dt><span class="sect2"><a href="#critical-error">3.7.2. Critical error handling</a></span></dt>
740</dl></dd>
741<dt><span class="sect1"><a href="#win-dll">3.8. Making a Windows DLL</a></span></dt>
742</dl>
743</div>
744<p>This chapter describes the programming interface to
745<code class="computeroutput">libbzip2</code>.</p>
746<p>For general background information, particularly about
747memory use and performance aspects, you'd be well advised to read
748<a class="xref" href="#using" title="2. How to use bzip2">How to use bzip2</a> as well.</p>
749<div class="sect1">
750<div class="titlepage"><div><div><h2 class="title" style="clear: both">
751<a name="top-level"></a>3.1. Top-level structure</h2></div></div></div>
752<p><code class="computeroutput">libbzip2</code> is a flexible
753library for compressing and decompressing data in the
754<code class="computeroutput">bzip2</code> data format.  Although
755packaged as a single entity, it helps to regard the library as
756three separate parts: the low level interface, and the high level
757interface, and some utility functions.</p>
758<p>The structure of
759<code class="computeroutput">libbzip2</code>'s interfaces is similar
760to that of Jean-loup Gailly's and Mark Adler's excellent
761<code class="computeroutput">zlib</code> library.</p>
762<p>All externally visible symbols have names beginning
763<code class="computeroutput">BZ2_</code>.  This is new in version
7641.0.  The intention is to minimise pollution of the namespaces of
765library clients.</p>
766<p>To use any part of the library, you need to
767<code class="computeroutput">#include &lt;bzlib.h&gt;</code>
768into your sources.</p>
769<div class="sect2">
770<div class="titlepage"><div><div><h3 class="title">
771<a name="ll-summary"></a>3.1.1. Low-level summary</h3></div></div></div>
772<p>This interface provides services for compressing and
773decompressing data in memory.  There's no provision for dealing
774with files, streams or any other I/O mechanisms, just straight
775memory-to-memory work.  In fact, this part of the library can be
776compiled without inclusion of
777<code class="computeroutput">stdio.h</code>, which may be helpful
778for embedded applications.</p>
779<p>The low-level part of the library has no global variables
780and is therefore thread-safe.</p>
781<p>Six routines make up the low level interface:
782<code class="computeroutput">BZ2_bzCompressInit</code>,
783<code class="computeroutput">BZ2_bzCompress</code>, and
784<code class="computeroutput">BZ2_bzCompressEnd</code> for
785compression, and a corresponding trio
786<code class="computeroutput">BZ2_bzDecompressInit</code>,
787<code class="computeroutput">BZ2_bzDecompress</code> and
788<code class="computeroutput">BZ2_bzDecompressEnd</code> for
789decompression.  The <code class="computeroutput">*Init</code>
790functions allocate memory for compression/decompression and do
791other initialisations, whilst the
792<code class="computeroutput">*End</code> functions close down
793operations and release memory.</p>
794<p>The real work is done by
795<code class="computeroutput">BZ2_bzCompress</code> and
796<code class="computeroutput">BZ2_bzDecompress</code>.  These
797compress and decompress data from a user-supplied input buffer to
798a user-supplied output buffer.  These buffers can be any size;
799arbitrary quantities of data are handled by making repeated calls
800to these functions.  This is a flexible mechanism allowing a
801consumer-pull style of activity, or producer-push, or a mixture
802of both.</p>
803</div>
804<div class="sect2">
805<div class="titlepage"><div><div><h3 class="title">
806<a name="hl-summary"></a>3.1.2. High-level summary</h3></div></div></div>
807<p>This interface provides some handy wrappers around the
808low-level interface to facilitate reading and writing
809<code class="computeroutput">bzip2</code> format files
810(<code class="computeroutput">.bz2</code> files).  The routines
811provide hooks to facilitate reading files in which the
812<code class="computeroutput">bzip2</code> data stream is embedded
813within some larger-scale file structure, or where there are
814multiple <code class="computeroutput">bzip2</code> data streams
815concatenated end-to-end.</p>
816<p>For reading files,
817<code class="computeroutput">BZ2_bzReadOpen</code>,
818<code class="computeroutput">BZ2_bzRead</code>,
819<code class="computeroutput">BZ2_bzReadClose</code> and
820<code class="computeroutput">BZ2_bzReadGetUnused</code> are
821supplied.  For writing files,
822<code class="computeroutput">BZ2_bzWriteOpen</code>,
823<code class="computeroutput">BZ2_bzWrite</code> and
824<code class="computeroutput">BZ2_bzWriteFinish</code> are
825available.</p>
826<p>As with the low-level library, no global variables are used
827so the library is per se thread-safe.  However, if I/O errors
828occur whilst reading or writing the underlying compressed files,
829you may have to consult <code class="computeroutput">errno</code> to
830determine the cause of the error.  In that case, you'd need a C
831library which correctly supports
832<code class="computeroutput">errno</code> in a multithreaded
833environment.</p>
834<p>To make the library a little simpler and more portable,
835<code class="computeroutput">BZ2_bzReadOpen</code> and
836<code class="computeroutput">BZ2_bzWriteOpen</code> require you to
837pass them file handles (<code class="computeroutput">FILE*</code>s)
838which have previously been opened for reading or writing
839respectively.  That avoids portability problems associated with
840file operations and file attributes, whilst not being much of an
841imposition on the programmer.</p>
842</div>
843<div class="sect2">
844<div class="titlepage"><div><div><h3 class="title">
845<a name="util-fns-summary"></a>3.1.3. Utility functions summary</h3></div></div></div>
846<p>For very simple needs,
847<code class="computeroutput">BZ2_bzBuffToBuffCompress</code> and
848<code class="computeroutput">BZ2_bzBuffToBuffDecompress</code> are
849provided.  These compress data in memory from one buffer to
850another buffer in a single function call.  You should assess
851whether these functions fulfill your memory-to-memory
852compression/decompression requirements before investing effort in
853understanding the more general but more complex low-level
854interface.</p>
855<p>Yoshioka Tsuneo
856(<code class="computeroutput">tsuneo@rr.iij4u.or.jp</code>) has
857contributed some functions to give better
858<code class="computeroutput">zlib</code> compatibility.  These
859functions are <code class="computeroutput">BZ2_bzopen</code>,
860<code class="computeroutput">BZ2_bzread</code>,
861<code class="computeroutput">BZ2_bzwrite</code>,
862<code class="computeroutput">BZ2_bzflush</code>,
863<code class="computeroutput">BZ2_bzclose</code>,
864<code class="computeroutput">BZ2_bzerror</code> and
865<code class="computeroutput">BZ2_bzlibVersion</code>.  You may find
866these functions more convenient for simple file reading and
867writing, than those in the high-level interface.  These functions
868are not (yet) officially part of the library, and are minimally
869documented here.  If they break, you get to keep all the pieces.
870I hope to document them properly when time permits.</p>
871<p>Yoshioka also contributed modifications to allow the
872library to be built as a Windows DLL.</p>
873</div>
874</div>
875<div class="sect1">
876<div class="titlepage"><div><div><h2 class="title" style="clear: both">
877<a name="err-handling"></a>3.2. Error handling</h2></div></div></div>
878<p>The library is designed to recover cleanly in all
879situations, including the worst-case situation of decompressing
880random data.  I'm not 100% sure that it can always do this, so
881you might want to add a signal handler to catch segmentation
882violations during decompression if you are feeling especially
883paranoid.  I would be interested in hearing more about the
884robustness of the library to corrupted compressed data.</p>
885<p>Version 1.0.3 more robust in this respect than any
886previous version.  Investigations with Valgrind (a tool for detecting
887problems with memory management) indicate
888that, at least for the few files I tested, all single-bit errors
889in the decompressed data are caught properly, with no
890segmentation faults, no uses of uninitialised data, no out of
891range reads or writes, and no infinite looping in the decompressor.
892So it's certainly pretty robust, although
893I wouldn't claim it to be totally bombproof.</p>
894<p>The file <code class="computeroutput">bzlib.h</code> contains
895all definitions needed to use the library.  In particular, you
896should definitely not include
897<code class="computeroutput">bzlib_private.h</code>.</p>
898<p>In <code class="computeroutput">bzlib.h</code>, the various
899return values are defined.  The following list is not intended as
900an exhaustive description of the circumstances in which a given
901value may be returned -- those descriptions are given later.
902Rather, it is intended to convey the rough meaning of each return
903value.  The first five actions are normal and not intended to
904denote an error situation.</p>
905<div class="variablelist"><dl class="variablelist">
906<dt><span class="term"><code class="computeroutput">BZ_OK</code></span></dt>
907<dd><p>The requested action was completed
908   successfully.</p></dd>
909<dt><span class="term"><code class="computeroutput">BZ_RUN_OK, BZ_FLUSH_OK,
910    BZ_FINISH_OK</code></span></dt>
911<dd><p>In
912   <code class="computeroutput">BZ2_bzCompress</code>, the requested
913   flush/finish/nothing-special action was completed
914   successfully.</p></dd>
915<dt><span class="term"><code class="computeroutput">BZ_STREAM_END</code></span></dt>
916<dd><p>Compression of data was completed, or the
917   logical stream end was detected during
918   decompression.</p></dd>
919</dl></div>
920<p>The following return values indicate an error of some
921kind.</p>
922<div class="variablelist"><dl class="variablelist">
923<dt><span class="term"><code class="computeroutput">BZ_CONFIG_ERROR</code></span></dt>
924<dd><p>Indicates that the library has been improperly
925   compiled on your platform -- a major configuration error.
926   Specifically, it means that
927   <code class="computeroutput">sizeof(char)</code>,
928   <code class="computeroutput">sizeof(short)</code> and
929   <code class="computeroutput">sizeof(int)</code> are not 1, 2 and
930   4 respectively, as they should be.  Note that the library
931   should still work properly on 64-bit platforms which follow
932   the LP64 programming model -- that is, where
933   <code class="computeroutput">sizeof(long)</code> and
934   <code class="computeroutput">sizeof(void*)</code> are 8.  Under
935   LP64, <code class="computeroutput">sizeof(int)</code> is still 4,
936   so <code class="computeroutput">libbzip2</code>, which doesn't
937   use the <code class="computeroutput">long</code> type, is
938   OK.</p></dd>
939<dt><span class="term"><code class="computeroutput">BZ_SEQUENCE_ERROR</code></span></dt>
940<dd><p>When using the library, it is important to call
941   the functions in the correct sequence and with data structures
942   (buffers etc) in the correct states.
943   <code class="computeroutput">libbzip2</code> checks as much as it
944   can to ensure this is happening, and returns
945   <code class="computeroutput">BZ_SEQUENCE_ERROR</code> if not.
946   Code which complies precisely with the function semantics, as
947   detailed below, should never receive this value; such an event
948   denotes buggy code which you should
949   investigate.</p></dd>
950<dt><span class="term"><code class="computeroutput">BZ_PARAM_ERROR</code></span></dt>
951<dd><p>Returned when a parameter to a function call is
952   out of range or otherwise manifestly incorrect.  As with
953   <code class="computeroutput">BZ_SEQUENCE_ERROR</code>, this
954   denotes a bug in the client code.  The distinction between
955   <code class="computeroutput">BZ_PARAM_ERROR</code> and
956   <code class="computeroutput">BZ_SEQUENCE_ERROR</code> is a bit
957   hazy, but still worth making.</p></dd>
958<dt><span class="term"><code class="computeroutput">BZ_MEM_ERROR</code></span></dt>
959<dd><p>Returned when a request to allocate memory
960   failed.  Note that the quantity of memory needed to decompress
961   a stream cannot be determined until the stream's header has
962   been read.  So
963   <code class="computeroutput">BZ2_bzDecompress</code> and
964   <code class="computeroutput">BZ2_bzRead</code> may return
965   <code class="computeroutput">BZ_MEM_ERROR</code> even though some
966   of the compressed data has been read.  The same is not true
967   for compression; once
968   <code class="computeroutput">BZ2_bzCompressInit</code> or
969   <code class="computeroutput">BZ2_bzWriteOpen</code> have
970   successfully completed,
971   <code class="computeroutput">BZ_MEM_ERROR</code> cannot
972   occur.</p></dd>
973<dt><span class="term"><code class="computeroutput">BZ_DATA_ERROR</code></span></dt>
974<dd><p>Returned when a data integrity error is
975   detected during decompression.  Most importantly, this means
976   when stored and computed CRCs for the data do not match.  This
977   value is also returned upon detection of any other anomaly in
978   the compressed data.</p></dd>
979<dt><span class="term"><code class="computeroutput">BZ_DATA_ERROR_MAGIC</code></span></dt>
980<dd><p>As a special case of
981   <code class="computeroutput">BZ_DATA_ERROR</code>, it is
982   sometimes useful to know when the compressed stream does not
983   start with the correct magic bytes (<code class="computeroutput">'B' 'Z'
984   'h'</code>).</p></dd>
985<dt><span class="term"><code class="computeroutput">BZ_IO_ERROR</code></span></dt>
986<dd><p>Returned by
987   <code class="computeroutput">BZ2_bzRead</code> and
988   <code class="computeroutput">BZ2_bzWrite</code> when there is an
989   error reading or writing in the compressed file, and by
990   <code class="computeroutput">BZ2_bzReadOpen</code> and
991   <code class="computeroutput">BZ2_bzWriteOpen</code> for attempts
992   to use a file for which the error indicator (viz,
993   <code class="computeroutput">ferror(f)</code>) is set.  On
994   receipt of <code class="computeroutput">BZ_IO_ERROR</code>, the
995   caller should consult <code class="computeroutput">errno</code>
996   and/or <code class="computeroutput">perror</code> to acquire
997   operating-system specific information about the
998   problem.</p></dd>
999<dt><span class="term"><code class="computeroutput">BZ_UNEXPECTED_EOF</code></span></dt>
1000<dd><p>Returned by
1001   <code class="computeroutput">BZ2_bzRead</code> when the
1002   compressed file finishes before the logical end of stream is
1003   detected.</p></dd>
1004<dt><span class="term"><code class="computeroutput">BZ_OUTBUFF_FULL</code></span></dt>
1005<dd><p>Returned by
1006   <code class="computeroutput">BZ2_bzBuffToBuffCompress</code> and
1007   <code class="computeroutput">BZ2_bzBuffToBuffDecompress</code> to
1008   indicate that the output data will not fit into the output
1009   buffer provided.</p></dd>
1010</dl></div>
1011</div>
1012<div class="sect1">
1013<div class="titlepage"><div><div><h2 class="title" style="clear: both">
1014<a name="low-level"></a>3.3. Low-level interface</h2></div></div></div>
1015<div class="sect2">
1016<div class="titlepage"><div><div><h3 class="title">
1017<a name="bzcompress-init"></a>3.3.1. BZ2_bzCompressInit</h3></div></div></div>
1018<pre class="programlisting">typedef struct {
1019  char *next_in;
1020  unsigned int avail_in;
1021  unsigned int total_in_lo32;
1022  unsigned int total_in_hi32;
1023
1024  char *next_out;
1025  unsigned int avail_out;
1026  unsigned int total_out_lo32;
1027  unsigned int total_out_hi32;
1028
1029  void *state;
1030
1031  void *(*bzalloc)(void *,int,int);
1032  void (*bzfree)(void *,void *);
1033  void *opaque;
1034} bz_stream;
1035
1036int BZ2_bzCompressInit ( bz_stream *strm,
1037                         int blockSize100k,
1038                         int verbosity,
1039                         int workFactor );</pre>
1040<p>Prepares for compression.  The
1041<code class="computeroutput">bz_stream</code> structure holds all
1042data pertaining to the compression activity.  A
1043<code class="computeroutput">bz_stream</code> structure should be
1044allocated and initialised prior to the call.  The fields of
1045<code class="computeroutput">bz_stream</code> comprise the entirety
1046of the user-visible data.  <code class="computeroutput">state</code>
1047is a pointer to the private data structures required for
1048compression.</p>
1049<p>Custom memory allocators are supported, via fields
1050<code class="computeroutput">bzalloc</code>,
1051<code class="computeroutput">bzfree</code>, and
1052<code class="computeroutput">opaque</code>.  The value
1053<code class="computeroutput">opaque</code> is passed to as the first
1054argument to all calls to <code class="computeroutput">bzalloc</code>
1055and <code class="computeroutput">bzfree</code>, but is otherwise
1056ignored by the library.  The call <code class="computeroutput">bzalloc (
1057opaque, n, m )</code> is expected to return a pointer
1058<code class="computeroutput">p</code> to <code class="computeroutput">n *
1059m</code> bytes of memory, and <code class="computeroutput">bzfree (
1060opaque, p )</code> should free that memory.</p>
1061<p>If you don't want to use a custom memory allocator, set
1062<code class="computeroutput">bzalloc</code>,
1063<code class="computeroutput">bzfree</code> and
1064<code class="computeroutput">opaque</code> to
1065<code class="computeroutput">NULL</code>, and the library will then
1066use the standard <code class="computeroutput">malloc</code> /
1067<code class="computeroutput">free</code> routines.</p>
1068<p>Before calling
1069<code class="computeroutput">BZ2_bzCompressInit</code>, fields
1070<code class="computeroutput">bzalloc</code>,
1071<code class="computeroutput">bzfree</code> and
1072<code class="computeroutput">opaque</code> should be filled
1073appropriately, as just described.  Upon return, the internal
1074state will have been allocated and initialised, and
1075<code class="computeroutput">total_in_lo32</code>,
1076<code class="computeroutput">total_in_hi32</code>,
1077<code class="computeroutput">total_out_lo32</code> and
1078<code class="computeroutput">total_out_hi32</code> will have been
1079set to zero.  These four fields are used by the library to inform
1080the caller of the total amount of data passed into and out of the
1081library, respectively.  You should not try to change them.  As of
1082version 1.0, 64-bit counts are maintained, even on 32-bit
1083platforms, using the <code class="computeroutput">_hi32</code>
1084fields to store the upper 32 bits of the count.  So, for example,
1085the total amount of data in is <code class="computeroutput">(total_in_hi32
1086&lt;&lt; 32) + total_in_lo32</code>.</p>
1087<p>Parameter <code class="computeroutput">blockSize100k</code>
1088specifies the block size to be used for compression.  It should
1089be a value between 1 and 9 inclusive, and the actual block size
1090used is 100000 x this figure.  9 gives the best compression but
1091takes most memory.</p>
1092<p>Parameter <code class="computeroutput">verbosity</code> should
1093be set to a number between 0 and 4 inclusive.  0 is silent, and
1094greater numbers give increasingly verbose monitoring/debugging
1095output.  If the library has been compiled with
1096<code class="computeroutput">-DBZ_NO_STDIO</code>, no such output
1097will appear for any verbosity setting.</p>
1098<p>Parameter <code class="computeroutput">workFactor</code>
1099controls how the compression phase behaves when presented with
1100worst case, highly repetitive, input data.  If compression runs
1101into difficulties caused by repetitive data, the library switches
1102from the standard sorting algorithm to a fallback algorithm.  The
1103fallback is slower than the standard algorithm by perhaps a
1104factor of three, but always behaves reasonably, no matter how bad
1105the input.</p>
1106<p>Lower values of <code class="computeroutput">workFactor</code>
1107reduce the amount of effort the standard algorithm will expend
1108before resorting to the fallback.  You should set this parameter
1109carefully; too low, and many inputs will be handled by the
1110fallback algorithm and so compress rather slowly, too high, and
1111your average-to-worst case compression times can become very
1112large.  The default value of 30 gives reasonable behaviour over a
1113wide range of circumstances.</p>
1114<p>Allowable values range from 0 to 250 inclusive.  0 is a
1115special case, equivalent to using the default value of 30.</p>
1116<p>Note that the compressed output generated is the same
1117regardless of whether or not the fallback algorithm is
1118used.</p>
1119<p>Be aware also that this parameter may disappear entirely in
1120future versions of the library.  In principle it should be
1121possible to devise a good way to automatically choose which
1122algorithm to use.  Such a mechanism would render the parameter
1123obsolete.</p>
1124<p>Possible return values:</p>
1125<pre class="programlisting">BZ_CONFIG_ERROR
1126  if the library has been mis-compiled
1127BZ_PARAM_ERROR
1128  if strm is NULL
1129  or blockSize &lt; 1 or blockSize &gt; 9
1130  or verbosity &lt; 0 or verbosity &gt; 4
1131  or workFactor &lt; 0 or workFactor &gt; 250
1132BZ_MEM_ERROR
1133  if not enough memory is available
1134BZ_OK
1135  otherwise</pre>
1136<p>Allowable next actions:</p>
1137<pre class="programlisting">BZ2_bzCompress
1138  if BZ_OK is returned
1139  no specific action needed in case of error</pre>
1140</div>
1141<div class="sect2">
1142<div class="titlepage"><div><div><h3 class="title">
1143<a name="bzCompress"></a>3.3.2. BZ2_bzCompress</h3></div></div></div>
1144<pre class="programlisting">int BZ2_bzCompress ( bz_stream *strm, int action );</pre>
1145<p>Provides more input and/or output buffer space for the
1146library.  The caller maintains input and output buffers, and
1147calls <code class="computeroutput">BZ2_bzCompress</code> to transfer
1148data between them.</p>
1149<p>Before each call to
1150<code class="computeroutput">BZ2_bzCompress</code>,
1151<code class="computeroutput">next_in</code> should point at the data
1152to be compressed, and <code class="computeroutput">avail_in</code>
1153should indicate how many bytes the library may read.
1154<code class="computeroutput">BZ2_bzCompress</code> updates
1155<code class="computeroutput">next_in</code>,
1156<code class="computeroutput">avail_in</code> and
1157<code class="computeroutput">total_in</code> to reflect the number
1158of bytes it has read.</p>
1159<p>Similarly, <code class="computeroutput">next_out</code> should
1160point to a buffer in which the compressed data is to be placed,
1161with <code class="computeroutput">avail_out</code> indicating how
1162much output space is available.
1163<code class="computeroutput">BZ2_bzCompress</code> updates
1164<code class="computeroutput">next_out</code>,
1165<code class="computeroutput">avail_out</code> and
1166<code class="computeroutput">total_out</code> to reflect the number
1167of bytes output.</p>
1168<p>You may provide and remove as little or as much data as you
1169like on each call of
1170<code class="computeroutput">BZ2_bzCompress</code>.  In the limit,
1171it is acceptable to supply and remove data one byte at a time,
1172although this would be terribly inefficient.  You should always
1173ensure that at least one byte of output space is available at
1174each call.</p>
1175<p>A second purpose of
1176<code class="computeroutput">BZ2_bzCompress</code> is to request a
1177change of mode of the compressed stream.</p>
1178<p>Conceptually, a compressed stream can be in one of four
1179states: IDLE, RUNNING, FLUSHING and FINISHING.  Before
1180initialisation
1181(<code class="computeroutput">BZ2_bzCompressInit</code>) and after
1182termination (<code class="computeroutput">BZ2_bzCompressEnd</code>),
1183a stream is regarded as IDLE.</p>
1184<p>Upon initialisation
1185(<code class="computeroutput">BZ2_bzCompressInit</code>), the stream
1186is placed in the RUNNING state.  Subsequent calls to
1187<code class="computeroutput">BZ2_bzCompress</code> should pass
1188<code class="computeroutput">BZ_RUN</code> as the requested action;
1189other actions are illegal and will result in
1190<code class="computeroutput">BZ_SEQUENCE_ERROR</code>.</p>
1191<p>At some point, the calling program will have provided all
1192the input data it wants to.  It will then want to finish up -- in
1193effect, asking the library to process any data it might have
1194buffered internally.  In this state,
1195<code class="computeroutput">BZ2_bzCompress</code> will no longer
1196attempt to read data from
1197<code class="computeroutput">next_in</code>, but it will want to
1198write data to <code class="computeroutput">next_out</code>.  Because
1199the output buffer supplied by the user can be arbitrarily small,
1200the finishing-up operation cannot necessarily be done with a
1201single call of
1202<code class="computeroutput">BZ2_bzCompress</code>.</p>
1203<p>Instead, the calling program passes
1204<code class="computeroutput">BZ_FINISH</code> as an action to
1205<code class="computeroutput">BZ2_bzCompress</code>.  This changes
1206the stream's state to FINISHING.  Any remaining input (ie,
1207<code class="computeroutput">next_in[0 .. avail_in-1]</code>) is
1208compressed and transferred to the output buffer.  To do this,
1209<code class="computeroutput">BZ2_bzCompress</code> must be called
1210repeatedly until all the output has been consumed.  At that
1211point, <code class="computeroutput">BZ2_bzCompress</code> returns
1212<code class="computeroutput">BZ_STREAM_END</code>, and the stream's
1213state is set back to IDLE.
1214<code class="computeroutput">BZ2_bzCompressEnd</code> should then be
1215called.</p>
1216<p>Just to make sure the calling program does not cheat, the
1217library makes a note of <code class="computeroutput">avail_in</code>
1218at the time of the first call to
1219<code class="computeroutput">BZ2_bzCompress</code> which has
1220<code class="computeroutput">BZ_FINISH</code> as an action (ie, at
1221the time the program has announced its intention to not supply
1222any more input).  By comparing this value with that of
1223<code class="computeroutput">avail_in</code> over subsequent calls
1224to <code class="computeroutput">BZ2_bzCompress</code>, the library
1225can detect any attempts to slip in more data to compress.  Any
1226calls for which this is detected will return
1227<code class="computeroutput">BZ_SEQUENCE_ERROR</code>.  This
1228indicates a programming mistake which should be corrected.</p>
1229<p>Instead of asking to finish, the calling program may ask
1230<code class="computeroutput">BZ2_bzCompress</code> to take all the
1231remaining input, compress it and terminate the current
1232(Burrows-Wheeler) compression block.  This could be useful for
1233error control purposes.  The mechanism is analogous to that for
1234finishing: call <code class="computeroutput">BZ2_bzCompress</code>
1235with an action of <code class="computeroutput">BZ_FLUSH</code>,
1236remove output data, and persist with the
1237<code class="computeroutput">BZ_FLUSH</code> action until the value
1238<code class="computeroutput">BZ_RUN</code> is returned.  As with
1239finishing, <code class="computeroutput">BZ2_bzCompress</code>
1240detects any attempt to provide more input data once the flush has
1241begun.</p>
1242<p>Once the flush is complete, the stream returns to the
1243normal RUNNING state.</p>
1244<p>This all sounds pretty complex, but isn't really.  Here's a
1245table which shows which actions are allowable in each state, what
1246action will be taken, what the next state is, and what the
1247non-error return values are.  Note that you can't explicitly ask
1248what state the stream is in, but nor do you need to -- it can be
1249inferred from the values returned by
1250<code class="computeroutput">BZ2_bzCompress</code>.</p>
1251<pre class="programlisting">IDLE/any
1252  Illegal.  IDLE state only exists after BZ2_bzCompressEnd or
1253  before BZ2_bzCompressInit.
1254  Return value = BZ_SEQUENCE_ERROR
1255
1256RUNNING/BZ_RUN
1257  Compress from next_in to next_out as much as possible.
1258  Next state = RUNNING
1259  Return value = BZ_RUN_OK
1260
1261RUNNING/BZ_FLUSH
1262  Remember current value of next_in. Compress from next_in
1263  to next_out as much as possible, but do not accept any more input.
1264  Next state = FLUSHING
1265  Return value = BZ_FLUSH_OK
1266
1267RUNNING/BZ_FINISH
1268  Remember current value of next_in. Compress from next_in
1269  to next_out as much as possible, but do not accept any more input.
1270  Next state = FINISHING
1271  Return value = BZ_FINISH_OK
1272
1273FLUSHING/BZ_FLUSH
1274  Compress from next_in to next_out as much as possible,
1275  but do not accept any more input.
1276  If all the existing input has been used up and all compressed
1277  output has been removed
1278    Next state = RUNNING; Return value = BZ_RUN_OK
1279  else
1280    Next state = FLUSHING; Return value = BZ_FLUSH_OK
1281
1282FLUSHING/other
1283  Illegal.
1284  Return value = BZ_SEQUENCE_ERROR
1285
1286FINISHING/BZ_FINISH
1287  Compress from next_in to next_out as much as possible,
1288  but to not accept any more input.
1289  If all the existing input has been used up and all compressed
1290  output has been removed
1291    Next state = IDLE; Return value = BZ_STREAM_END
1292  else
1293    Next state = FINISHING; Return value = BZ_FINISH_OK
1294
1295FINISHING/other
1296  Illegal.
1297  Return value = BZ_SEQUENCE_ERROR</pre>
1298<p>That still looks complicated?  Well, fair enough.  The
1299usual sequence of calls for compressing a load of data is:</p>
1300<div class="orderedlist"><ol class="orderedlist" type="1">
1301<li class="listitem"><p>Get started with
1302  <code class="computeroutput">BZ2_bzCompressInit</code>.</p></li>
1303<li class="listitem"><p>Shovel data in and shlurp out its compressed form
1304  using zero or more calls of
1305  <code class="computeroutput">BZ2_bzCompress</code> with action =
1306  <code class="computeroutput">BZ_RUN</code>.</p></li>
1307<li class="listitem"><p>Finish up. Repeatedly call
1308  <code class="computeroutput">BZ2_bzCompress</code> with action =
1309  <code class="computeroutput">BZ_FINISH</code>, copying out the
1310  compressed output, until
1311  <code class="computeroutput">BZ_STREAM_END</code> is
1312  returned.</p></li>
1313<li class="listitem"><p>Close up and go home.  Call
1314  <code class="computeroutput">BZ2_bzCompressEnd</code>.</p></li>
1315</ol></div>
1316<p>If the data you want to compress fits into your input
1317buffer all at once, you can skip the calls of
1318<code class="computeroutput">BZ2_bzCompress ( ..., BZ_RUN )</code>
1319and just do the <code class="computeroutput">BZ2_bzCompress ( ..., BZ_FINISH
1320)</code> calls.</p>
1321<p>All required memory is allocated by
1322<code class="computeroutput">BZ2_bzCompressInit</code>.  The
1323compression library can accept any data at all (obviously).  So
1324you shouldn't get any error return values from the
1325<code class="computeroutput">BZ2_bzCompress</code> calls.  If you
1326do, they will be
1327<code class="computeroutput">BZ_SEQUENCE_ERROR</code>, and indicate
1328a bug in your programming.</p>
1329<p>Trivial other possible return values:</p>
1330<pre class="programlisting">BZ_PARAM_ERROR
1331  if strm is NULL, or strm-&gt;s is NULL</pre>
1332</div>
1333<div class="sect2">
1334<div class="titlepage"><div><div><h3 class="title">
1335<a name="bzCompress-end"></a>3.3.3. BZ2_bzCompressEnd</h3></div></div></div>
1336<pre class="programlisting">int BZ2_bzCompressEnd ( bz_stream *strm );</pre>
1337<p>Releases all memory associated with a compression
1338stream.</p>
1339<p>Possible return values:</p>
1340<pre class="programlisting">BZ_PARAM_ERROR  if strm is NULL or strm-&gt;s is NULL
1341BZ_OK           otherwise</pre>
1342</div>
1343<div class="sect2">
1344<div class="titlepage"><div><div><h3 class="title">
1345<a name="bzDecompress-init"></a>3.3.4. BZ2_bzDecompressInit</h3></div></div></div>
1346<pre class="programlisting">int BZ2_bzDecompressInit ( bz_stream *strm, int verbosity, int small );</pre>
1347<p>Prepares for decompression.  As with
1348<code class="computeroutput">BZ2_bzCompressInit</code>, a
1349<code class="computeroutput">bz_stream</code> record should be
1350allocated and initialised before the call.  Fields
1351<code class="computeroutput">bzalloc</code>,
1352<code class="computeroutput">bzfree</code> and
1353<code class="computeroutput">opaque</code> should be set if a custom
1354memory allocator is required, or made
1355<code class="computeroutput">NULL</code> for the normal
1356<code class="computeroutput">malloc</code> /
1357<code class="computeroutput">free</code> routines.  Upon return, the
1358internal state will have been initialised, and
1359<code class="computeroutput">total_in</code> and
1360<code class="computeroutput">total_out</code> will be zero.</p>
1361<p>For the meaning of parameter
1362<code class="computeroutput">verbosity</code>, see
1363<code class="computeroutput">BZ2_bzCompressInit</code>.</p>
1364<p>If <code class="computeroutput">small</code> is nonzero, the
1365library will use an alternative decompression algorithm which
1366uses less memory but at the cost of decompressing more slowly
1367(roughly speaking, half the speed, but the maximum memory
1368requirement drops to around 2300k).  See <a class="xref" href="#using" title="2. How to use bzip2">How to use bzip2</a>
1369for more information on memory management.</p>
1370<p>Note that the amount of memory needed to decompress a
1371stream cannot be determined until the stream's header has been
1372read, so even if
1373<code class="computeroutput">BZ2_bzDecompressInit</code> succeeds, a
1374subsequent <code class="computeroutput">BZ2_bzDecompress</code>
1375could fail with
1376<code class="computeroutput">BZ_MEM_ERROR</code>.</p>
1377<p>Possible return values:</p>
1378<pre class="programlisting">BZ_CONFIG_ERROR
1379  if the library has been mis-compiled
1380BZ_PARAM_ERROR
1381  if ( small != 0 &amp;&amp; small != 1 )
1382  or (verbosity &lt;; 0 || verbosity &gt; 4)
1383BZ_MEM_ERROR
1384  if insufficient memory is available</pre>
1385<p>Allowable next actions:</p>
1386<pre class="programlisting">BZ2_bzDecompress
1387  if BZ_OK was returned
1388  no specific action required in case of error</pre>
1389</div>
1390<div class="sect2">
1391<div class="titlepage"><div><div><h3 class="title">
1392<a name="bzDecompress"></a>3.3.5. BZ2_bzDecompress</h3></div></div></div>
1393<pre class="programlisting">int BZ2_bzDecompress ( bz_stream *strm );</pre>
1394<p>Provides more input and/out output buffer space for the
1395library.  The caller maintains input and output buffers, and uses
1396<code class="computeroutput">BZ2_bzDecompress</code> to transfer
1397data between them.</p>
1398<p>Before each call to
1399<code class="computeroutput">BZ2_bzDecompress</code>,
1400<code class="computeroutput">next_in</code> should point at the
1401compressed data, and <code class="computeroutput">avail_in</code>
1402should indicate how many bytes the library may read.
1403<code class="computeroutput">BZ2_bzDecompress</code> updates
1404<code class="computeroutput">next_in</code>,
1405<code class="computeroutput">avail_in</code> and
1406<code class="computeroutput">total_in</code> to reflect the number
1407of bytes it has read.</p>
1408<p>Similarly, <code class="computeroutput">next_out</code> should
1409point to a buffer in which the uncompressed output is to be
1410placed, with <code class="computeroutput">avail_out</code>
1411indicating how much output space is available.
1412<code class="computeroutput">BZ2_bzCompress</code> updates
1413<code class="computeroutput">next_out</code>,
1414<code class="computeroutput">avail_out</code> and
1415<code class="computeroutput">total_out</code> to reflect the number
1416of bytes output.</p>
1417<p>You may provide and remove as little or as much data as you
1418like on each call of
1419<code class="computeroutput">BZ2_bzDecompress</code>.  In the limit,
1420it is acceptable to supply and remove data one byte at a time,
1421although this would be terribly inefficient.  You should always
1422ensure that at least one byte of output space is available at
1423each call.</p>
1424<p>Use of <code class="computeroutput">BZ2_bzDecompress</code> is
1425simpler than
1426<code class="computeroutput">BZ2_bzCompress</code>.</p>
1427<p>You should provide input and remove output as described
1428above, and repeatedly call
1429<code class="computeroutput">BZ2_bzDecompress</code> until
1430<code class="computeroutput">BZ_STREAM_END</code> is returned.
1431Appearance of <code class="computeroutput">BZ_STREAM_END</code>
1432denotes that <code class="computeroutput">BZ2_bzDecompress</code>
1433has detected the logical end of the compressed stream.
1434<code class="computeroutput">BZ2_bzDecompress</code> will not
1435produce <code class="computeroutput">BZ_STREAM_END</code> until all
1436output data has been placed into the output buffer, so once
1437<code class="computeroutput">BZ_STREAM_END</code> appears, you are
1438guaranteed to have available all the decompressed output, and
1439<code class="computeroutput">BZ2_bzDecompressEnd</code> can safely
1440be called.</p>
1441<p>If case of an error return value, you should call
1442<code class="computeroutput">BZ2_bzDecompressEnd</code> to clean up
1443and release memory.</p>
1444<p>Possible return values:</p>
1445<pre class="programlisting">BZ_PARAM_ERROR
1446  if strm is NULL or strm-&gt;s is NULL
1447  or strm-&gt;avail_out &lt; 1
1448BZ_DATA_ERROR
1449  if a data integrity error is detected in the compressed stream
1450BZ_DATA_ERROR_MAGIC
1451  if the compressed stream doesn't begin with the right magic bytes
1452BZ_MEM_ERROR
1453  if there wasn't enough memory available
1454BZ_STREAM_END
1455  if the logical end of the data stream was detected and all
1456  output in has been consumed, eg s--&gt;avail_out &gt; 0
1457BZ_OK
1458  otherwise</pre>
1459<p>Allowable next actions:</p>
1460<pre class="programlisting">BZ2_bzDecompress
1461  if BZ_OK was returned
1462BZ2_bzDecompressEnd
1463  otherwise</pre>
1464</div>
1465<div class="sect2">
1466<div class="titlepage"><div><div><h3 class="title">
1467<a name="bzDecompress-end"></a>3.3.6. BZ2_bzDecompressEnd</h3></div></div></div>
1468<pre class="programlisting">int BZ2_bzDecompressEnd ( bz_stream *strm );</pre>
1469<p>Releases all memory associated with a decompression
1470stream.</p>
1471<p>Possible return values:</p>
1472<pre class="programlisting">BZ_PARAM_ERROR
1473  if strm is NULL or strm-&gt;s is NULL
1474BZ_OK
1475  otherwise</pre>
1476<p>Allowable next actions:</p>
1477<pre class="programlisting">  None.</pre>
1478</div>
1479</div>
1480<div class="sect1">
1481<div class="titlepage"><div><div><h2 class="title" style="clear: both">
1482<a name="hl-interface"></a>3.4. High-level interface</h2></div></div></div>
1483<p>This interface provides functions for reading and writing
1484<code class="computeroutput">bzip2</code> format files.  First, some
1485general points.</p>
1486<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
1487<li class="listitem" style="list-style-type: disc"><p>All of the functions take an
1488  <code class="computeroutput">int*</code> first argument,
1489  <code class="computeroutput">bzerror</code>.  After each call,
1490  <code class="computeroutput">bzerror</code> should be consulted
1491  first to determine the outcome of the call.  If
1492  <code class="computeroutput">bzerror</code> is
1493  <code class="computeroutput">BZ_OK</code>, the call completed
1494  successfully, and only then should the return value of the
1495  function (if any) be consulted.  If
1496  <code class="computeroutput">bzerror</code> is
1497  <code class="computeroutput">BZ_IO_ERROR</code>, there was an
1498  error reading/writing the underlying compressed file, and you
1499  should then consult <code class="computeroutput">errno</code> /
1500  <code class="computeroutput">perror</code> to determine the cause
1501  of the difficulty.  <code class="computeroutput">bzerror</code>
1502  may also be set to various other values; precise details are
1503  given on a per-function basis below.</p></li>
1504<li class="listitem" style="list-style-type: disc"><p>If <code class="computeroutput">bzerror</code> indicates
1505  an error (ie, anything except
1506  <code class="computeroutput">BZ_OK</code> and
1507  <code class="computeroutput">BZ_STREAM_END</code>), you should
1508  immediately call
1509  <code class="computeroutput">BZ2_bzReadClose</code> (or
1510  <code class="computeroutput">BZ2_bzWriteClose</code>, depending on
1511  whether you are attempting to read or to write) to free up all
1512  resources associated with the stream.  Once an error has been
1513  indicated, behaviour of all calls except
1514  <code class="computeroutput">BZ2_bzReadClose</code>
1515  (<code class="computeroutput">BZ2_bzWriteClose</code>) is
1516  undefined.  The implication is that (1)
1517  <code class="computeroutput">bzerror</code> should be checked
1518  after each call, and (2) if
1519  <code class="computeroutput">bzerror</code> indicates an error,
1520  <code class="computeroutput">BZ2_bzReadClose</code>
1521  (<code class="computeroutput">BZ2_bzWriteClose</code>) should then
1522  be called to clean up.</p></li>
1523<li class="listitem" style="list-style-type: disc"><p>The <code class="computeroutput">FILE*</code> arguments
1524  passed to <code class="computeroutput">BZ2_bzReadOpen</code> /
1525  <code class="computeroutput">BZ2_bzWriteOpen</code> should be set
1526  to binary mode.  Most Unix systems will do this by default, but
1527  other platforms, including Windows and Mac, will not.  If you
1528  omit this, you may encounter problems when moving code to new
1529  platforms.</p></li>
1530<li class="listitem" style="list-style-type: disc"><p>Memory allocation requests are handled by
1531  <code class="computeroutput">malloc</code> /
1532  <code class="computeroutput">free</code>.  At present there is no
1533  facility for user-defined memory allocators in the file I/O
1534  functions (could easily be added, though).</p></li>
1535</ul></div>
1536<div class="sect2">
1537<div class="titlepage"><div><div><h3 class="title">
1538<a name="bzreadopen"></a>3.4.1. BZ2_bzReadOpen</h3></div></div></div>
1539<pre class="programlisting">typedef void BZFILE;
1540
1541BZFILE *BZ2_bzReadOpen( int *bzerror, FILE *f,
1542                        int verbosity, int small,
1543                        void *unused, int nUnused );</pre>
1544<p>Prepare to read compressed data from file handle
1545<code class="computeroutput">f</code>.
1546<code class="computeroutput">f</code> should refer to a file which
1547has been opened for reading, and for which the error indicator
1548(<code class="computeroutput">ferror(f)</code>)is not set.  If
1549<code class="computeroutput">small</code> is 1, the library will try
1550to decompress using less memory, at the expense of speed.</p>
1551<p>For reasons explained below,
1552<code class="computeroutput">BZ2_bzRead</code> will decompress the
1553<code class="computeroutput">nUnused</code> bytes starting at
1554<code class="computeroutput">unused</code>, before starting to read
1555from the file <code class="computeroutput">f</code>.  At most
1556<code class="computeroutput">BZ_MAX_UNUSED</code> bytes may be
1557supplied like this.  If this facility is not required, you should
1558pass <code class="computeroutput">NULL</code> and
1559<code class="computeroutput">0</code> for
1560<code class="computeroutput">unused</code> and
1561n<code class="computeroutput">Unused</code> respectively.</p>
1562<p>For the meaning of parameters
1563<code class="computeroutput">small</code> and
1564<code class="computeroutput">verbosity</code>, see
1565<code class="computeroutput">BZ2_bzDecompressInit</code>.</p>
1566<p>The amount of memory needed to decompress a file cannot be
1567determined until the file's header has been read.  So it is
1568possible that <code class="computeroutput">BZ2_bzReadOpen</code>
1569returns <code class="computeroutput">BZ_OK</code> but a subsequent
1570call of <code class="computeroutput">BZ2_bzRead</code> will return
1571<code class="computeroutput">BZ_MEM_ERROR</code>.</p>
1572<p>Possible assignments to
1573<code class="computeroutput">bzerror</code>:</p>
1574<pre class="programlisting">BZ_CONFIG_ERROR
1575  if the library has been mis-compiled
1576BZ_PARAM_ERROR
1577  if f is NULL
1578  or small is neither 0 nor 1
1579  or ( unused == NULL &amp;&amp; nUnused != 0 )
1580  or ( unused != NULL &amp;&amp; !(0 &lt;= nUnused &lt;= BZ_MAX_UNUSED) )
1581BZ_IO_ERROR
1582  if ferror(f) is nonzero
1583BZ_MEM_ERROR
1584  if insufficient memory is available
1585BZ_OK
1586  otherwise.</pre>
1587<p>Possible return values:</p>
1588<pre class="programlisting">Pointer to an abstract BZFILE
1589  if bzerror is BZ_OK
1590NULL
1591  otherwise</pre>
1592<p>Allowable next actions:</p>
1593<pre class="programlisting">BZ2_bzRead
1594  if bzerror is BZ_OK
1595BZ2_bzClose
1596  otherwise</pre>
1597</div>
1598<div class="sect2">
1599<div class="titlepage"><div><div><h3 class="title">
1600<a name="bzread"></a>3.4.2. BZ2_bzRead</h3></div></div></div>
1601<pre class="programlisting">int BZ2_bzRead ( int *bzerror, BZFILE *b, void *buf, int len );</pre>
1602<p>Reads up to <code class="computeroutput">len</code>
1603(uncompressed) bytes from the compressed file
1604<code class="computeroutput">b</code> into the buffer
1605<code class="computeroutput">buf</code>.  If the read was
1606successful, <code class="computeroutput">bzerror</code> is set to
1607<code class="computeroutput">BZ_OK</code> and the number of bytes
1608read is returned.  If the logical end-of-stream was detected,
1609<code class="computeroutput">bzerror</code> will be set to
1610<code class="computeroutput">BZ_STREAM_END</code>, and the number of
1611bytes read is returned.  All other
1612<code class="computeroutput">bzerror</code> values denote an
1613error.</p>
1614<p><code class="computeroutput">BZ2_bzRead</code> will supply
1615<code class="computeroutput">len</code> bytes, unless the logical
1616stream end is detected or an error occurs.  Because of this, it
1617is possible to detect the stream end by observing when the number
1618of bytes returned is less than the number requested.
1619Nevertheless, this is regarded as inadvisable; you should instead
1620check <code class="computeroutput">bzerror</code> after every call
1621and watch out for
1622<code class="computeroutput">BZ_STREAM_END</code>.</p>
1623<p>Internally, <code class="computeroutput">BZ2_bzRead</code>
1624copies data from the compressed file in chunks of size
1625<code class="computeroutput">BZ_MAX_UNUSED</code> bytes before
1626decompressing it.  If the file contains more bytes than strictly
1627needed to reach the logical end-of-stream,
1628<code class="computeroutput">BZ2_bzRead</code> will almost certainly
1629read some of the trailing data before signalling
1630<code class="computeroutput">BZ_SEQUENCE_END</code>.  To collect the
1631read but unused data once
1632<code class="computeroutput">BZ_SEQUENCE_END</code> has appeared,
1633call <code class="computeroutput">BZ2_bzReadGetUnused</code>
1634immediately before
1635<code class="computeroutput">BZ2_bzReadClose</code>.</p>
1636<p>Possible assignments to
1637<code class="computeroutput">bzerror</code>:</p>
1638<pre class="programlisting">BZ_PARAM_ERROR
1639  if b is NULL or buf is NULL or len &lt; 0
1640BZ_SEQUENCE_ERROR
1641  if b was opened with BZ2_bzWriteOpen
1642BZ_IO_ERROR
1643  if there is an error reading from the compressed file
1644BZ_UNEXPECTED_EOF
1645  if the compressed file ended before
1646  the logical end-of-stream was detected
1647BZ_DATA_ERROR
1648  if a data integrity error was detected in the compressed stream
1649BZ_DATA_ERROR_MAGIC
1650  if the stream does not begin with the requisite header bytes
1651  (ie, is not a bzip2 data file).  This is really
1652  a special case of BZ_DATA_ERROR.
1653BZ_MEM_ERROR
1654  if insufficient memory was available
1655BZ_STREAM_END
1656  if the logical end of stream was detected.
1657BZ_OK
1658  otherwise.</pre>
1659<p>Possible return values:</p>
1660<pre class="programlisting">number of bytes read
1661  if bzerror is BZ_OK or BZ_STREAM_END
1662undefined
1663  otherwise</pre>
1664<p>Allowable next actions:</p>
1665<pre class="programlisting">collect data from buf, then BZ2_bzRead or BZ2_bzReadClose
1666  if bzerror is BZ_OK
1667collect data from buf, then BZ2_bzReadClose or BZ2_bzReadGetUnused
1668  if bzerror is BZ_SEQUENCE_END
1669BZ2_bzReadClose
1670  otherwise</pre>
1671</div>
1672<div class="sect2">
1673<div class="titlepage"><div><div><h3 class="title">
1674<a name="bzreadgetunused"></a>3.4.3. BZ2_bzReadGetUnused</h3></div></div></div>
1675<pre class="programlisting">void BZ2_bzReadGetUnused( int* bzerror, BZFILE *b,
1676                          void** unused, int* nUnused );</pre>
1677<p>Returns data which was read from the compressed file but
1678was not needed to get to the logical end-of-stream.
1679<code class="computeroutput">*unused</code> is set to the address of
1680the data, and <code class="computeroutput">*nUnused</code> to the
1681number of bytes.  <code class="computeroutput">*nUnused</code> will
1682be set to a value between <code class="computeroutput">0</code> and
1683<code class="computeroutput">BZ_MAX_UNUSED</code> inclusive.</p>
1684<p>This function may only be called once
1685<code class="computeroutput">BZ2_bzRead</code> has signalled
1686<code class="computeroutput">BZ_STREAM_END</code> but before
1687<code class="computeroutput">BZ2_bzReadClose</code>.</p>
1688<p>Possible assignments to
1689<code class="computeroutput">bzerror</code>:</p>
1690<pre class="programlisting">BZ_PARAM_ERROR
1691  if b is NULL
1692  or unused is NULL or nUnused is NULL
1693BZ_SEQUENCE_ERROR
1694  if BZ_STREAM_END has not been signalled
1695  or if b was opened with BZ2_bzWriteOpen
1696BZ_OK
1697  otherwise</pre>
1698<p>Allowable next actions:</p>
1699<pre class="programlisting">BZ2_bzReadClose</pre>
1700</div>
1701<div class="sect2">
1702<div class="titlepage"><div><div><h3 class="title">
1703<a name="bzreadclose"></a>3.4.4. BZ2_bzReadClose</h3></div></div></div>
1704<pre class="programlisting">void BZ2_bzReadClose ( int *bzerror, BZFILE *b );</pre>
1705<p>Releases all memory pertaining to the compressed file
1706<code class="computeroutput">b</code>.
1707<code class="computeroutput">BZ2_bzReadClose</code> does not call
1708<code class="computeroutput">fclose</code> on the underlying file
1709handle, so you should do that yourself if appropriate.
1710<code class="computeroutput">BZ2_bzReadClose</code> should be called
1711to clean up after all error situations.</p>
1712<p>Possible assignments to
1713<code class="computeroutput">bzerror</code>:</p>
1714<pre class="programlisting">BZ_SEQUENCE_ERROR
1715  if b was opened with BZ2_bzOpenWrite
1716BZ_OK
1717  otherwise</pre>
1718<p>Allowable next actions:</p>
1719<pre class="programlisting">none</pre>
1720</div>
1721<div class="sect2">
1722<div class="titlepage"><div><div><h3 class="title">
1723<a name="bzwriteopen"></a>3.4.5. BZ2_bzWriteOpen</h3></div></div></div>
1724<pre class="programlisting">BZFILE *BZ2_bzWriteOpen( int *bzerror, FILE *f,
1725                         int blockSize100k, int verbosity,
1726                         int workFactor );</pre>
1727<p>Prepare to write compressed data to file handle
1728<code class="computeroutput">f</code>.
1729<code class="computeroutput">f</code> should refer to a file which
1730has been opened for writing, and for which the error indicator
1731(<code class="computeroutput">ferror(f)</code>)is not set.</p>
1732<p>For the meaning of parameters
1733<code class="computeroutput">blockSize100k</code>,
1734<code class="computeroutput">verbosity</code> and
1735<code class="computeroutput">workFactor</code>, see
1736<code class="computeroutput">BZ2_bzCompressInit</code>.</p>
1737<p>All required memory is allocated at this stage, so if the
1738call completes successfully,
1739<code class="computeroutput">BZ_MEM_ERROR</code> cannot be signalled
1740by a subsequent call to
1741<code class="computeroutput">BZ2_bzWrite</code>.</p>
1742<p>Possible assignments to
1743<code class="computeroutput">bzerror</code>:</p>
1744<pre class="programlisting">BZ_CONFIG_ERROR
1745  if the library has been mis-compiled
1746BZ_PARAM_ERROR
1747  if f is NULL
1748  or blockSize100k &lt; 1 or blockSize100k &gt; 9
1749BZ_IO_ERROR
1750  if ferror(f) is nonzero
1751BZ_MEM_ERROR
1752  if insufficient memory is available
1753BZ_OK
1754  otherwise</pre>
1755<p>Possible return values:</p>
1756<pre class="programlisting">Pointer to an abstract BZFILE
1757  if bzerror is BZ_OK
1758NULL
1759  otherwise</pre>
1760<p>Allowable next actions:</p>
1761<pre class="programlisting">BZ2_bzWrite
1762  if bzerror is BZ_OK
1763  (you could go directly to BZ2_bzWriteClose, but this would be pretty pointless)
1764BZ2_bzWriteClose
1765  otherwise</pre>
1766</div>
1767<div class="sect2">
1768<div class="titlepage"><div><div><h3 class="title">
1769<a name="bzwrite"></a>3.4.6. BZ2_bzWrite</h3></div></div></div>
1770<pre class="programlisting">void BZ2_bzWrite ( int *bzerror, BZFILE *b, void *buf, int len );</pre>
1771<p>Absorbs <code class="computeroutput">len</code> bytes from the
1772buffer <code class="computeroutput">buf</code>, eventually to be
1773compressed and written to the file.</p>
1774<p>Possible assignments to
1775<code class="computeroutput">bzerror</code>:</p>
1776<pre class="programlisting">BZ_PARAM_ERROR
1777  if b is NULL or buf is NULL or len &lt; 0
1778BZ_SEQUENCE_ERROR
1779  if b was opened with BZ2_bzReadOpen
1780BZ_IO_ERROR
1781  if there is an error writing the compressed file.
1782BZ_OK
1783  otherwise</pre>
1784</div>
1785<div class="sect2">
1786<div class="titlepage"><div><div><h3 class="title">
1787<a name="bzwriteclose"></a>3.4.7. BZ2_bzWriteClose</h3></div></div></div>
1788<pre class="programlisting">void BZ2_bzWriteClose( int *bzerror, BZFILE* f,
1789                       int abandon,
1790                       unsigned int* nbytes_in,
1791                       unsigned int* nbytes_out );
1792
1793void BZ2_bzWriteClose64( int *bzerror, BZFILE* f,
1794                         int abandon,
1795                         unsigned int* nbytes_in_lo32,
1796                         unsigned int* nbytes_in_hi32,
1797                         unsigned int* nbytes_out_lo32,
1798                         unsigned int* nbytes_out_hi32 );</pre>
1799<p>Compresses and flushes to the compressed file all data so
1800far supplied by <code class="computeroutput">BZ2_bzWrite</code>.
1801The logical end-of-stream markers are also written, so subsequent
1802calls to <code class="computeroutput">BZ2_bzWrite</code> are
1803illegal.  All memory associated with the compressed file
1804<code class="computeroutput">b</code> is released.
1805<code class="computeroutput">fflush</code> is called on the
1806compressed file, but it is not
1807<code class="computeroutput">fclose</code>'d.</p>
1808<p>If <code class="computeroutput">BZ2_bzWriteClose</code> is
1809called to clean up after an error, the only action is to release
1810the memory.  The library records the error codes issued by
1811previous calls, so this situation will be detected automatically.
1812There is no attempt to complete the compression operation, nor to
1813<code class="computeroutput">fflush</code> the compressed file.  You
1814can force this behaviour to happen even in the case of no error,
1815by passing a nonzero value to
1816<code class="computeroutput">abandon</code>.</p>
1817<p>If <code class="computeroutput">nbytes_in</code> is non-null,
1818<code class="computeroutput">*nbytes_in</code> will be set to be the
1819total volume of uncompressed data handled.  Similarly,
1820<code class="computeroutput">nbytes_out</code> will be set to the
1821total volume of compressed data written.  For compatibility with
1822older versions of the library,
1823<code class="computeroutput">BZ2_bzWriteClose</code> only yields the
1824lower 32 bits of these counts.  Use
1825<code class="computeroutput">BZ2_bzWriteClose64</code> if you want
1826the full 64 bit counts.  These two functions are otherwise
1827absolutely identical.</p>
1828<p>Possible assignments to
1829<code class="computeroutput">bzerror</code>:</p>
1830<pre class="programlisting">BZ_SEQUENCE_ERROR
1831  if b was opened with BZ2_bzReadOpen
1832BZ_IO_ERROR
1833  if there is an error writing the compressed file
1834BZ_OK
1835  otherwise</pre>
1836</div>
1837<div class="sect2">
1838<div class="titlepage"><div><div><h3 class="title">
1839<a name="embed"></a>3.4.8. Handling embedded compressed data streams</h3></div></div></div>
1840<p>The high-level library facilitates use of
1841<code class="computeroutput">bzip2</code> data streams which form
1842some part of a surrounding, larger data stream.</p>
1843<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
1844<li class="listitem" style="list-style-type: disc"><p>For writing, the library takes an open file handle,
1845  writes compressed data to it,
1846  <code class="computeroutput">fflush</code>es it but does not
1847  <code class="computeroutput">fclose</code> it.  The calling
1848  application can write its own data before and after the
1849  compressed data stream, using that same file handle.</p></li>
1850<li class="listitem" style="list-style-type: disc"><p>Reading is more complex, and the facilities are not as
1851  general as they could be since generality is hard to reconcile
1852  with efficiency.  <code class="computeroutput">BZ2_bzRead</code>
1853  reads from the compressed file in blocks of size
1854  <code class="computeroutput">BZ_MAX_UNUSED</code> bytes, and in
1855  doing so probably will overshoot the logical end of compressed
1856  stream.  To recover this data once decompression has ended,
1857  call <code class="computeroutput">BZ2_bzReadGetUnused</code> after
1858  the last call of <code class="computeroutput">BZ2_bzRead</code>
1859  (the one returning
1860  <code class="computeroutput">BZ_STREAM_END</code>) but before
1861  calling
1862  <code class="computeroutput">BZ2_bzReadClose</code>.</p></li>
1863</ul></div>
1864<p>This mechanism makes it easy to decompress multiple
1865<code class="computeroutput">bzip2</code> streams placed end-to-end.
1866As the end of one stream, when
1867<code class="computeroutput">BZ2_bzRead</code> returns
1868<code class="computeroutput">BZ_STREAM_END</code>, call
1869<code class="computeroutput">BZ2_bzReadGetUnused</code> to collect
1870the unused data (copy it into your own buffer somewhere).  That
1871data forms the start of the next compressed stream.  To start
1872uncompressing that next stream, call
1873<code class="computeroutput">BZ2_bzReadOpen</code> again, feeding in
1874the unused data via the <code class="computeroutput">unused</code> /
1875<code class="computeroutput">nUnused</code> parameters.  Keep doing
1876this until <code class="computeroutput">BZ_STREAM_END</code> return
1877coincides with the physical end of file
1878(<code class="computeroutput">feof(f)</code>).  In this situation
1879<code class="computeroutput">BZ2_bzReadGetUnused</code> will of
1880course return no data.</p>
1881<p>This should give some feel for how the high-level interface
1882can be used.  If you require extra flexibility, you'll have to
1883bite the bullet and get to grips with the low-level
1884interface.</p>
1885</div>
1886<div class="sect2">
1887<div class="titlepage"><div><div><h3 class="title">
1888<a name="std-rdwr"></a>3.4.9. Standard file-reading/writing code</h3></div></div></div>
1889<p>Here's how you'd write data to a compressed file:</p>
1890<pre class="programlisting">FILE*   f;
1891BZFILE* b;
1892int     nBuf;
1893char    buf[ /* whatever size you like */ ];
1894int     bzerror;
1895int     nWritten;
1896
1897f = fopen ( "myfile.bz2", "w" );
1898if ( !f ) {
1899 /* handle error */
1900}
1901b = BZ2_bzWriteOpen( &amp;bzerror, f, 9 );
1902if (bzerror != BZ_OK) {
1903 BZ2_bzWriteClose ( b );
1904 /* handle error */
1905}
1906
1907while ( /* condition */ ) {
1908 /* get data to write into buf, and set nBuf appropriately */
1909 nWritten = BZ2_bzWrite ( &amp;bzerror, b, buf, nBuf );
1910 if (bzerror == BZ_IO_ERROR) {
1911   BZ2_bzWriteClose ( &amp;bzerror, b );
1912   /* handle error */
1913 }
1914}
1915
1916BZ2_bzWriteClose( &amp;bzerror, b );
1917if (bzerror == BZ_IO_ERROR) {
1918 /* handle error */
1919}</pre>
1920<p>And to read from a compressed file:</p>
1921<pre class="programlisting">FILE*   f;
1922BZFILE* b;
1923int     nBuf;
1924char    buf[ /* whatever size you like */ ];
1925int     bzerror;
1926int     nWritten;
1927
1928f = fopen ( "myfile.bz2", "r" );
1929if ( !f ) {
1930  /* handle error */
1931}
1932b = BZ2_bzReadOpen ( &amp;bzerror, f, 0, NULL, 0 );
1933if ( bzerror != BZ_OK ) {
1934  BZ2_bzReadClose ( &amp;bzerror, b );
1935  /* handle error */
1936}
1937
1938bzerror = BZ_OK;
1939while ( bzerror == BZ_OK &amp;&amp; /* arbitrary other conditions */) {
1940  nBuf = BZ2_bzRead ( &amp;bzerror, b, buf, /* size of buf */ );
1941  if ( bzerror == BZ_OK ) {
1942    /* do something with buf[0 .. nBuf-1] */
1943  }
1944}
1945if ( bzerror != BZ_STREAM_END ) {
1946   BZ2_bzReadClose ( &amp;bzerror, b );
1947   /* handle error */
1948} else {
1949   BZ2_bzReadClose ( &amp;bzerror, b );
1950}</pre>
1951</div>
1952</div>
1953<div class="sect1">
1954<div class="titlepage"><div><div><h2 class="title" style="clear: both">
1955<a name="util-fns"></a>3.5. Utility functions</h2></div></div></div>
1956<div class="sect2">
1957<div class="titlepage"><div><div><h3 class="title">
1958<a name="bzbufftobuffcompress"></a>3.5.1. BZ2_bzBuffToBuffCompress</h3></div></div></div>
1959<pre class="programlisting">int BZ2_bzBuffToBuffCompress( char*         dest,
1960                              unsigned int* destLen,
1961                              char*         source,
1962                              unsigned int  sourceLen,
1963                              int           blockSize100k,
1964                              int           verbosity,
1965                              int           workFactor );</pre>
1966<p>Attempts to compress the data in <code class="computeroutput">source[0
1967.. sourceLen-1]</code> into the destination buffer,
1968<code class="computeroutput">dest[0 .. *destLen-1]</code>.  If the
1969destination buffer is big enough,
1970<code class="computeroutput">*destLen</code> is set to the size of
1971the compressed data, and <code class="computeroutput">BZ_OK</code>
1972is returned.  If the compressed data won't fit,
1973<code class="computeroutput">*destLen</code> is unchanged, and
1974<code class="computeroutput">BZ_OUTBUFF_FULL</code> is
1975returned.</p>
1976<p>Compression in this manner is a one-shot event, done with a
1977single call to this function.  The resulting compressed data is a
1978complete <code class="computeroutput">bzip2</code> format data
1979stream.  There is no mechanism for making additional calls to
1980provide extra input data.  If you want that kind of mechanism,
1981use the low-level interface.</p>
1982<p>For the meaning of parameters
1983<code class="computeroutput">blockSize100k</code>,
1984<code class="computeroutput">verbosity</code> and
1985<code class="computeroutput">workFactor</code>, see
1986<code class="computeroutput">BZ2_bzCompressInit</code>.</p>
1987<p>To guarantee that the compressed data will fit in its
1988buffer, allocate an output buffer of size 1% larger than the
1989uncompressed data, plus six hundred extra bytes.</p>
1990<p><code class="computeroutput">BZ2_bzBuffToBuffDecompress</code>
1991will not write data at or beyond
1992<code class="computeroutput">dest[*destLen]</code>, even in case of
1993buffer overflow.</p>
1994<p>Possible return values:</p>
1995<pre class="programlisting">BZ_CONFIG_ERROR
1996  if the library has been mis-compiled
1997BZ_PARAM_ERROR
1998  if dest is NULL or destLen is NULL
1999  or blockSize100k &lt; 1 or blockSize100k &gt; 9
2000  or verbosity &lt; 0 or verbosity &gt; 4
2001  or workFactor &lt; 0 or workFactor &gt; 250
2002BZ_MEM_ERROR
2003  if insufficient memory is available
2004BZ_OUTBUFF_FULL
2005  if the size of the compressed data exceeds *destLen
2006BZ_OK
2007  otherwise</pre>
2008</div>
2009<div class="sect2">
2010<div class="titlepage"><div><div><h3 class="title">
2011<a name="bzbufftobuffdecompress"></a>3.5.2. BZ2_bzBuffToBuffDecompress</h3></div></div></div>
2012<pre class="programlisting">int BZ2_bzBuffToBuffDecompress( char*         dest,
2013                                unsigned int* destLen,
2014                                char*         source,
2015                                unsigned int  sourceLen,
2016                                int           small,
2017                                int           verbosity );</pre>
2018<p>Attempts to decompress the data in <code class="computeroutput">source[0
2019.. sourceLen-1]</code> into the destination buffer,
2020<code class="computeroutput">dest[0 .. *destLen-1]</code>.  If the
2021destination buffer is big enough,
2022<code class="computeroutput">*destLen</code> is set to the size of
2023the uncompressed data, and <code class="computeroutput">BZ_OK</code>
2024is returned.  If the compressed data won't fit,
2025<code class="computeroutput">*destLen</code> is unchanged, and
2026<code class="computeroutput">BZ_OUTBUFF_FULL</code> is
2027returned.</p>
2028<p><code class="computeroutput">source</code> is assumed to hold
2029a complete <code class="computeroutput">bzip2</code> format data
2030stream.
2031<code class="computeroutput">BZ2_bzBuffToBuffDecompress</code> tries
2032to decompress the entirety of the stream into the output
2033buffer.</p>
2034<p>For the meaning of parameters
2035<code class="computeroutput">small</code> and
2036<code class="computeroutput">verbosity</code>, see
2037<code class="computeroutput">BZ2_bzDecompressInit</code>.</p>
2038<p>Because the compression ratio of the compressed data cannot
2039be known in advance, there is no easy way to guarantee that the
2040output buffer will be big enough.  You may of course make
2041arrangements in your code to record the size of the uncompressed
2042data, but such a mechanism is beyond the scope of this
2043library.</p>
2044<p><code class="computeroutput">BZ2_bzBuffToBuffDecompress</code>
2045will not write data at or beyond
2046<code class="computeroutput">dest[*destLen]</code>, even in case of
2047buffer overflow.</p>
2048<p>Possible return values:</p>
2049<pre class="programlisting">BZ_CONFIG_ERROR
2050  if the library has been mis-compiled
2051BZ_PARAM_ERROR
2052  if dest is NULL or destLen is NULL
2053  or small != 0 &amp;&amp; small != 1
2054  or verbosity &lt; 0 or verbosity &gt; 4
2055BZ_MEM_ERROR
2056  if insufficient memory is available
2057BZ_OUTBUFF_FULL
2058  if the size of the compressed data exceeds *destLen
2059BZ_DATA_ERROR
2060  if a data integrity error was detected in the compressed data
2061BZ_DATA_ERROR_MAGIC
2062  if the compressed data doesn't begin with the right magic bytes
2063BZ_UNEXPECTED_EOF
2064  if the compressed data ends unexpectedly
2065BZ_OK
2066  otherwise</pre>
2067</div>
2068</div>
2069<div class="sect1">
2070<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2071<a name="zlib-compat"></a>3.6. zlib compatibility functions</h2></div></div></div>
2072<p>Yoshioka Tsuneo has contributed some functions to give
2073better <code class="computeroutput">zlib</code> compatibility.
2074These functions are <code class="computeroutput">BZ2_bzopen</code>,
2075<code class="computeroutput">BZ2_bzread</code>,
2076<code class="computeroutput">BZ2_bzwrite</code>,
2077<code class="computeroutput">BZ2_bzflush</code>,
2078<code class="computeroutput">BZ2_bzclose</code>,
2079<code class="computeroutput">BZ2_bzerror</code> and
2080<code class="computeroutput">BZ2_bzlibVersion</code>.  These
2081functions are not (yet) officially part of the library.  If they
2082break, you get to keep all the pieces.  Nevertheless, I think
2083they work ok.</p>
2084<pre class="programlisting">typedef void BZFILE;
2085
2086const char * BZ2_bzlibVersion ( void );</pre>
2087<p>Returns a string indicating the library version.</p>
2088<pre class="programlisting">BZFILE * BZ2_bzopen  ( const char *path, const char *mode );
2089BZFILE * BZ2_bzdopen ( int        fd,    const char *mode );</pre>
2090<p>Opens a <code class="computeroutput">.bz2</code> file for
2091reading or writing, using either its name or a pre-existing file
2092descriptor.  Analogous to <code class="computeroutput">fopen</code>
2093and <code class="computeroutput">fdopen</code>.</p>
2094<pre class="programlisting">int BZ2_bzread  ( BZFILE* b, void* buf, int len );
2095int BZ2_bzwrite ( BZFILE* b, void* buf, int len );</pre>
2096<p>Reads/writes data from/to a previously opened
2097<code class="computeroutput">BZFILE</code>.  Analogous to
2098<code class="computeroutput">fread</code> and
2099<code class="computeroutput">fwrite</code>.</p>
2100<pre class="programlisting">int  BZ2_bzflush ( BZFILE* b );
2101void BZ2_bzclose ( BZFILE* b );</pre>
2102<p>Flushes/closes a <code class="computeroutput">BZFILE</code>.
2103<code class="computeroutput">BZ2_bzflush</code> doesn't actually do
2104anything.  Analogous to <code class="computeroutput">fflush</code>
2105and <code class="computeroutput">fclose</code>.</p>
2106<pre class="programlisting">const char * BZ2_bzerror ( BZFILE *b, int *errnum )</pre>
2107<p>Returns a string describing the more recent error status of
2108<code class="computeroutput">b</code>, and also sets
2109<code class="computeroutput">*errnum</code> to its numerical
2110value.</p>
2111</div>
2112<div class="sect1">
2113<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2114<a name="stdio-free"></a>3.7. Using the library in a stdio-free environment</h2></div></div></div>
2115<div class="sect2">
2116<div class="titlepage"><div><div><h3 class="title">
2117<a name="stdio-bye"></a>3.7.1. Getting rid of stdio</h3></div></div></div>
2118<p>In a deeply embedded application, you might want to use
2119just the memory-to-memory functions.  You can do this
2120conveniently by compiling the library with preprocessor symbol
2121<code class="computeroutput">BZ_NO_STDIO</code> defined.  Doing this
2122gives you a library containing only the following eight
2123functions:</p>
2124<p><code class="computeroutput">BZ2_bzCompressInit</code>,
2125<code class="computeroutput">BZ2_bzCompress</code>,
2126<code class="computeroutput">BZ2_bzCompressEnd</code>
2127<code class="computeroutput">BZ2_bzDecompressInit</code>,
2128<code class="computeroutput">BZ2_bzDecompress</code>,
2129<code class="computeroutput">BZ2_bzDecompressEnd</code>
2130<code class="computeroutput">BZ2_bzBuffToBuffCompress</code>,
2131<code class="computeroutput">BZ2_bzBuffToBuffDecompress</code></p>
2132<p>When compiled like this, all functions will ignore
2133<code class="computeroutput">verbosity</code> settings.</p>
2134</div>
2135<div class="sect2">
2136<div class="titlepage"><div><div><h3 class="title">
2137<a name="critical-error"></a>3.7.2. Critical error handling</h3></div></div></div>
2138<p><code class="computeroutput">libbzip2</code> contains a number
2139of internal assertion checks which should, needless to say, never
2140be activated.  Nevertheless, if an assertion should fail,
2141behaviour depends on whether or not the library was compiled with
2142<code class="computeroutput">BZ_NO_STDIO</code> set.</p>
2143<p>For a normal compile, an assertion failure yields the
2144message:</p>
2145<div class="blockquote"><blockquote class="blockquote">
2146<p>bzip2/libbzip2: internal error number N.</p>
2147<p>This is a bug in bzip2/libbzip2, 1.0.8 of 13 July 2019.
2148Please report it to: bzip2-devel@sourceware.org.  If this happened
2149when you were using some program which uses libbzip2 as a
2150component, you should also report this bug to the author(s)
2151of that program.  Please make an effort to report this bug;
2152timely and accurate bug reports eventually lead to higher
2153quality software.  Thanks.
2154</p>
2155</blockquote></div>
2156<p>where <code class="computeroutput">N</code> is some error code
2157number.  If <code class="computeroutput">N == 1007</code>, it also
2158prints some extra text advising the reader that unreliable memory
2159is often associated with internal error 1007. (This is a
2160frequently-observed-phenomenon with versions 1.0.0/1.0.1).</p>
2161<p><code class="computeroutput">exit(3)</code> is then
2162called.</p>
2163<p>For a <code class="computeroutput">stdio</code>-free library,
2164assertion failures result in a call to a function declared
2165as:</p>
2166<pre class="programlisting">extern void bz_internal_error ( int errcode );</pre>
2167<p>The relevant code is passed as a parameter.  You should
2168supply such a function.</p>
2169<p>In either case, once an assertion failure has occurred, any
2170<code class="computeroutput">bz_stream</code> records involved can
2171be regarded as invalid.  You should not attempt to resume normal
2172operation with them.</p>
2173<p>You may, of course, change critical error handling to suit
2174your needs.  As I said above, critical errors indicate bugs in
2175the library and should not occur.  All "normal" error situations
2176are indicated via error return codes from functions, and can be
2177recovered from.</p>
2178</div>
2179</div>
2180<div class="sect1">
2181<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2182<a name="win-dll"></a>3.8. Making a Windows DLL</h2></div></div></div>
2183<p>Everything related to Windows has been contributed by
2184Yoshioka Tsuneo
2185(<code class="computeroutput">tsuneo@rr.iij4u.or.jp</code>), so
2186you should send your queries to him (but please Cc:
2187<code class="computeroutput">bzip2-devel@sourceware.org</code>).</p>
2188<p>My vague understanding of what to do is: using Visual C++
21895.0, open the project file
2190<code class="computeroutput">libbz2.dsp</code>, and build.  That's
2191all.</p>
2192<p>If you can't open the project file for some reason, make a
2193new one, naming these files:
2194<code class="computeroutput">blocksort.c</code>,
2195<code class="computeroutput">bzlib.c</code>,
2196<code class="computeroutput">compress.c</code>,
2197<code class="computeroutput">crctable.c</code>,
2198<code class="computeroutput">decompress.c</code>,
2199<code class="computeroutput">huffman.c</code>,
2200<code class="computeroutput">randtable.c</code> and
2201<code class="computeroutput">libbz2.def</code>.  You will also need
2202to name the header files <code class="computeroutput">bzlib.h</code>
2203and <code class="computeroutput">bzlib_private.h</code>.</p>
2204<p>If you don't use VC++, you may need to define the
2205proprocessor symbol
2206<code class="computeroutput">_WIN32</code>.</p>
2207<p>Finally, <code class="computeroutput">dlltest.c</code> is a
2208sample program using the DLL.  It has a project file,
2209<code class="computeroutput">dlltest.dsp</code>.</p>
2210<p>If you just want a makefile for Visual C, have a look at
2211<code class="computeroutput">makefile.msc</code>.</p>
2212<p>Be aware that if you compile
2213<code class="computeroutput">bzip2</code> itself on Win32, you must
2214set <code class="computeroutput">BZ_UNIX</code> to 0 and
2215<code class="computeroutput">BZ_LCCWIN32</code> to 1, in the file
2216<code class="computeroutput">bzip2.c</code>, before compiling.
2217Otherwise the resulting binary won't work correctly.</p>
2218<p>I haven't tried any of this stuff myself, but it all looks
2219plausible.</p>
2220</div>
2221</div>
2222<div class="chapter">
2223<div class="titlepage"><div><div><h1 class="title">
2224<a name="misc"></a>4. Miscellanea</h1></div></div></div>
2225<div class="toc">
2226<p><b>Table of Contents</b></p>
2227<dl class="toc">
2228<dt><span class="sect1"><a href="#limits">4.1. Limitations of the compressed file format</a></span></dt>
2229<dt><span class="sect1"><a href="#port-issues">4.2. Portability issues</a></span></dt>
2230<dt><span class="sect1"><a href="#bugs">4.3. Reporting bugs</a></span></dt>
2231<dt><span class="sect1"><a href="#package">4.4. Did you get the right package?</a></span></dt>
2232<dt><span class="sect1"><a href="#reading">4.5. Further Reading</a></span></dt>
2233</dl>
2234</div>
2235<p>These are just some random thoughts of mine.  Your mileage
2236may vary.</p>
2237<div class="sect1">
2238<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2239<a name="limits"></a>4.1. Limitations of the compressed file format</h2></div></div></div>
2240<p><code class="computeroutput">bzip2-1.0.X</code>,
2241<code class="computeroutput">0.9.5</code> and
2242<code class="computeroutput">0.9.0</code> use exactly the same file
2243format as the original version,
2244<code class="computeroutput">bzip2-0.1</code>.  This decision was
2245made in the interests of stability.  Creating yet another
2246incompatible compressed file format would create further
2247confusion and disruption for users.</p>
2248<p>Nevertheless, this is not a painless decision.  Development
2249work since the release of
2250<code class="computeroutput">bzip2-0.1</code> in August 1997 has
2251shown complexities in the file format which slow down
2252decompression and, in retrospect, are unnecessary.  These
2253are:</p>
2254<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
2255<li class="listitem" style="list-style-type: disc"><p>The run-length encoder, which is the first of the
2256   compression transformations, is entirely irrelevant.  The
2257   original purpose was to protect the sorting algorithm from the
2258   very worst case input: a string of repeated symbols.  But
2259   algorithm steps Q6a and Q6b in the original Burrows-Wheeler
2260   technical report (SRC-124) show how repeats can be handled
2261   without difficulty in block sorting.</p></li>
2262<li class="listitem" style="list-style-type: disc">
2263<p>The randomisation mechanism doesn't really need to be
2264   there.  Udi Manber and Gene Myers published a suffix array
2265   construction algorithm a few years back, which can be employed
2266   to sort any block, no matter how repetitive, in O(N log N)
2267   time.  Subsequent work by Kunihiko Sadakane has produced a
2268   derivative O(N (log N)^2) algorithm which usually outperforms
2269   the Manber-Myers algorithm.</p>
2270<p>I could have changed to Sadakane's algorithm, but I find
2271   it to be slower than <code class="computeroutput">bzip2</code>'s
2272   existing algorithm for most inputs, and the randomisation
2273   mechanism protects adequately against bad cases.  I didn't
2274   think it was a good tradeoff to make.  Partly this is due to
2275   the fact that I was not flooded with email complaints about
2276   <code class="computeroutput">bzip2-0.1</code>'s performance on
2277   repetitive data, so perhaps it isn't a problem for real
2278   inputs.</p>
2279<p>Probably the best long-term solution, and the one I have
2280   incorporated into 0.9.5 and above, is to use the existing
2281   sorting algorithm initially, and fall back to a O(N (log N)^2)
2282   algorithm if the standard algorithm gets into
2283   difficulties.</p>
2284</li>
2285<li class="listitem" style="list-style-type: disc"><p>The compressed file format was never designed to be
2286   handled by a library, and I have had to jump though some hoops
2287   to produce an efficient implementation of decompression.  It's
2288   a bit hairy.  Try passing
2289   <code class="computeroutput">decompress.c</code> through the C
2290   preprocessor and you'll see what I mean.  Much of this
2291   complexity could have been avoided if the compressed size of
2292   each block of data was recorded in the data stream.</p></li>
2293<li class="listitem" style="list-style-type: disc"><p>An Adler-32 checksum, rather than a CRC32 checksum,
2294   would be faster to compute.</p></li>
2295</ul></div>
2296<p>It would be fair to say that the
2297<code class="computeroutput">bzip2</code> format was frozen before I
2298properly and fully understood the performance consequences of
2299doing so.</p>
2300<p>Improvements which I was able to incorporate into 0.9.0,
2301despite using the same file format, are:</p>
2302<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
2303<li class="listitem" style="list-style-type: disc"><p>Single array implementation of the inverse BWT.  This
2304  significantly speeds up decompression, presumably because it
2305  reduces the number of cache misses.</p></li>
2306<li class="listitem" style="list-style-type: disc"><p>Faster inverse MTF transform for large MTF values.
2307  The new implementation is based on the notion of sliding blocks
2308  of values.</p></li>
2309<li class="listitem" style="list-style-type: disc"><p><code class="computeroutput">bzip2-0.9.0</code> now reads
2310  and writes files with <code class="computeroutput">fread</code>
2311  and <code class="computeroutput">fwrite</code>; version 0.1 used
2312  <code class="computeroutput">putc</code> and
2313  <code class="computeroutput">getc</code>.  Duh!  Well, you live
2314  and learn.</p></li>
2315</ul></div>
2316<p>Further ahead, it would be nice to be able to do random
2317access into files.  This will require some careful design of
2318compressed file formats.</p>
2319</div>
2320<div class="sect1">
2321<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2322<a name="port-issues"></a>4.2. Portability issues</h2></div></div></div>
2323<p>After some consideration, I have decided not to use GNU
2324<code class="computeroutput">autoconf</code> to configure 0.9.5 or
23251.0.</p>
2326<p><code class="computeroutput">autoconf</code>, admirable and
2327wonderful though it is, mainly assists with portability problems
2328between Unix-like platforms.  But
2329<code class="computeroutput">bzip2</code> doesn't have much in the
2330way of portability problems on Unix; most of the difficulties
2331appear when porting to the Mac, or to Microsoft's operating
2332systems.  <code class="computeroutput">autoconf</code> doesn't help
2333in those cases, and brings in a whole load of new
2334complexity.</p>
2335<p>Most people should be able to compile the library and
2336program under Unix straight out-of-the-box, so to speak,
2337especially if you have a version of GNU C available.</p>
2338<p>There are a couple of
2339<code class="computeroutput">__inline__</code> directives in the
2340code.  GNU C (<code class="computeroutput">gcc</code>) should be
2341able to handle them.  If you're not using GNU C, your C compiler
2342shouldn't see them at all.  If your compiler does, for some
2343reason, see them and doesn't like them, just
2344<code class="computeroutput">#define</code>
2345<code class="computeroutput">__inline__</code> to be
2346<code class="computeroutput">/* */</code>.  One easy way to do this
2347is to compile with the flag
2348<code class="computeroutput">-D__inline__=</code>, which should be
2349understood by most Unix compilers.</p>
2350<p>If you still have difficulties, try compiling with the
2351macro <code class="computeroutput">BZ_STRICT_ANSI</code> defined.
2352This should enable you to build the library in a strictly ANSI
2353compliant environment.  Building the program itself like this is
2354dangerous and not supported, since you remove
2355<code class="computeroutput">bzip2</code>'s checks against
2356compressing directories, symbolic links, devices, and other
2357not-really-a-file entities.  This could cause filesystem
2358corruption!</p>
2359<p>One other thing: if you create a
2360<code class="computeroutput">bzip2</code> binary for public distribution,
2361please consider linking it statically (<code class="computeroutput">gcc
2362-static</code>).  This avoids all sorts of library-version
2363issues that others may encounter later on.</p>
2364<p>If you build <code class="computeroutput">bzip2</code> on
2365Win32, you must set <code class="computeroutput">BZ_UNIX</code> to 0
2366and <code class="computeroutput">BZ_LCCWIN32</code> to 1, in the
2367file <code class="computeroutput">bzip2.c</code>, before compiling.
2368Otherwise the resulting binary won't work correctly.</p>
2369</div>
2370<div class="sect1">
2371<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2372<a name="bugs"></a>4.3. Reporting bugs</h2></div></div></div>
2373<p>I tried pretty hard to make sure
2374<code class="computeroutput">bzip2</code> is bug free, both by
2375design and by testing.  Hopefully you'll never need to read this
2376section for real.</p>
2377<p>Nevertheless, if <code class="computeroutput">bzip2</code> dies
2378with a segmentation fault, a bus error or an internal assertion
2379failure, it will ask you to email me a bug report.  Experience from
2380years of feedback of bzip2 users indicates that almost all these
2381problems can be traced to either compiler bugs or hardware
2382problems.</p>
2383<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: bullet; ">
2384<li class="listitem" style="list-style-type: disc">
2385<p>Recompile the program with no optimisation, and
2386  see if it works.  And/or try a different compiler.  I heard all
2387  sorts of stories about various flavours of GNU C (and other
2388  compilers) generating bad code for
2389  <code class="computeroutput">bzip2</code>, and I've run across two
2390  such examples myself.</p>
2391<p>2.7.X versions of GNU C are known to generate bad code
2392  from time to time, at high optimisation levels.  If you get
2393  problems, try using the flags
2394  <code class="computeroutput">-O2</code>
2395  <code class="computeroutput">-fomit-frame-pointer</code>
2396  <code class="computeroutput">-fno-strength-reduce</code>.  You
2397  should specifically <span class="emphasis"><em>not</em></span> use
2398  <code class="computeroutput">-funroll-loops</code>.</p>
2399<p>You may notice that the Makefile runs six tests as part
2400  of the build process.  If the program passes all of these, it's
2401  a pretty good (but not 100%) indication that the compiler has
2402  done its job correctly.</p>
2403</li>
2404<li class="listitem" style="list-style-type: disc">
2405<p>If <code class="computeroutput">bzip2</code>
2406  crashes randomly, and the crashes are not repeatable, you may
2407  have a flaky memory subsystem.
2408  <code class="computeroutput">bzip2</code> really hammers your
2409  memory hierarchy, and if it's a bit marginal, you may get these
2410  problems.  Ditto if your disk or I/O subsystem is slowly
2411  failing.  Yup, this really does happen.</p>
2412<p>Try using a different machine of the same type, and see
2413  if you can repeat the problem.</p>
2414</li>
2415<li class="listitem" style="list-style-type: disc"><p>This isn't really a bug, but ... If
2416  <code class="computeroutput">bzip2</code> tells you your file is
2417  corrupted on decompression, and you obtained the file via FTP,
2418  there is a possibility that you forgot to tell FTP to do a
2419  binary mode transfer.  That absolutely will cause the file to
2420  be non-decompressible.  You'll have to transfer it
2421  again.</p></li>
2422</ul></div>
2423<p>If you've incorporated
2424<code class="computeroutput">libbzip2</code> into your own program
2425and are getting problems, please, please, please, check that the
2426parameters you are passing in calls to the library, are correct,
2427and in accordance with what the documentation says is allowable.
2428I have tried to make the library robust against such problems,
2429but I'm sure I haven't succeeded.</p>
2430<p>Finally, if the above comments don't help, you'll have to
2431send me a bug report.  Now, it's just amazing how many people
2432will send me a bug report saying something like:</p>
2433<pre class="programlisting">bzip2 crashed with segmentation fault on my machine</pre>
2434<p>and absolutely nothing else.  Needless to say, a such a
2435report is <span class="emphasis"><em>totally, utterly, completely and
2436comprehensively 100% useless; a waste of your time, my time, and
2437net bandwidth</em></span>.  With no details at all, there's no way
2438I can possibly begin to figure out what the problem is.</p>
2439<p>The rules of the game are: facts, facts, facts.  Don't omit
2440them because "oh, they won't be relevant".  At the bare
2441minimum:</p>
2442<pre class="programlisting">Machine type.  Operating system version.
2443Exact version of bzip2 (do bzip2 -V).
2444Exact version of the compiler used.
2445Flags passed to the compiler.</pre>
2446<p>However, the most important single thing that will help me
2447is the file that you were trying to compress or decompress at the
2448time the problem happened.  Without that, my ability to do
2449anything more than speculate about the cause, is limited.</p>
2450</div>
2451<div class="sect1">
2452<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2453<a name="package"></a>4.4. Did you get the right package?</h2></div></div></div>
2454<p><code class="computeroutput">bzip2</code> is a resource hog.
2455It soaks up large amounts of CPU cycles and memory.  Also, it
2456gives very large latencies.  In the worst case, you can feed many
2457megabytes of uncompressed data into the library before getting
2458any compressed output, so this probably rules out applications
2459requiring interactive behaviour.</p>
2460<p>These aren't faults of my implementation, I hope, but more
2461an intrinsic property of the Burrows-Wheeler transform
2462(unfortunately).  Maybe this isn't what you want.</p>
2463<p>If you want a compressor and/or library which is faster,
2464uses less memory but gets pretty good compression, and has
2465minimal latency, consider Jean-loup Gailly's and Mark Adler's
2466work, <code class="computeroutput">zlib-1.2.1</code> and
2467<code class="computeroutput">gzip-1.2.4</code>.  Look for them at
2468<a class="ulink" href="http://www.zlib.org" target="_top">http://www.zlib.org</a> and
2469<a class="ulink" href="http://www.gzip.org" target="_top">http://www.gzip.org</a>
2470respectively.</p>
2471<p>For something faster and lighter still, you might try Markus F
2472X J Oberhumer's <code class="computeroutput">LZO</code> real-time
2473compression/decompression library, at
2474<a class="ulink" href="http://www.oberhumer.com/opensource" target="_top">http://www.oberhumer.com/opensource</a>.</p>
2475</div>
2476<div class="sect1">
2477<div class="titlepage"><div><div><h2 class="title" style="clear: both">
2478<a name="reading"></a>4.5. Further Reading</h2></div></div></div>
2479<p><code class="computeroutput">bzip2</code> is not research
2480work, in the sense that it doesn't present any new ideas.
2481Rather, it's an engineering exercise based on existing
2482ideas.</p>
2483<p>Four documents describe essentially all the ideas behind
2484<code class="computeroutput">bzip2</code>:</p>
2485<div class="literallayout"><p>Michael Burrows and D. J. Wheeler:<br>
2486  "A block-sorting lossless data compression algorithm"<br>
2487   10th May 1994. <br>
2488   Digital SRC Research Report 124.<br>
2489   ftp://ftp.digital.com/pub/DEC/SRC/research-reports/SRC-124.ps.gz<br>
2490   If you have trouble finding it, try searching at the<br>
2491   New Zealand Digital Library, http://www.nzdl.org.<br>
2492<br>
2493Daniel S. Hirschberg and Debra A. LeLewer<br>
2494  "Efficient Decoding of Prefix Codes"<br>
2495   Communications of the ACM, April 1990, Vol 33, Number 4.<br>
2496   You might be able to get an electronic copy of this<br>
2497   from the ACM Digital Library.<br>
2498<br>
2499David J. Wheeler<br>
2500   Program bred3.c and accompanying document bred3.ps.<br>
2501   This contains the idea behind the multi-table Huffman coding scheme.<br>
2502   ftp://ftp.cl.cam.ac.uk/users/djw3/<br>
2503<br>
2504Jon L. Bentley and Robert Sedgewick<br>
2505  "Fast Algorithms for Sorting and Searching Strings"<br>
2506   Available from Sedgewick's web page,<br>
2507   www.cs.princeton.edu/~rs<br>
2508</p></div>
2509<p>The following paper gives valuable additional insights into
2510the algorithm, but is not immediately the basis of any code used
2511in bzip2.</p>
2512<div class="literallayout"><p>Peter Fenwick:<br>
2513   Block Sorting Text Compression<br>
2514   Proceedings of the 19th Australasian Computer Science Conference,<br>
2515     Melbourne, Australia.  Jan 31 - Feb 2, 1996.<br>
2516   ftp://ftp.cs.auckland.ac.nz/pub/peter-f/ACSC96paper.ps</p></div>
2517<p>Kunihiko Sadakane's sorting algorithm, mentioned above, is
2518available from:</p>
2519<div class="literallayout"><p>http://naomi.is.s.u-tokyo.ac.jp/~sada/papers/Sada98b.ps.gz<br>
2520</p></div>
2521<p>The Manber-Myers suffix array construction algorithm is
2522described in a paper available from:</p>
2523<div class="literallayout"><p>http://www.cs.arizona.edu/people/gene/PAPERS/suffix.ps<br>
2524</p></div>
2525<p>Finally, the following papers document some
2526investigations I made into the performance of sorting
2527and decompression algorithms:</p>
2528<div class="literallayout"><p>Julian Seward<br>
2529   On the Performance of BWT Sorting Algorithms<br>
2530   Proceedings of the IEEE Data Compression Conference 2000<br>
2531     Snowbird, Utah.  28-30 March 2000.<br>
2532<br>
2533Julian Seward<br>
2534   Space-time Tradeoffs in the Inverse B-W Transform<br>
2535   Proceedings of the IEEE Data Compression Conference 2001<br>
2536     Snowbird, Utah.  27-29 March 2001.<br>
2537</p></div>
2538</div>
2539</div>
2540</div></body>
2541</html>
2542