PageRenderTime 134ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/StormLib/stormlib/bzip2/manual.xml

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