PageRenderTime 73ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/StormLib/stormlib/bzip2/manual.html

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