bzcompress.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * THIS FILE IS NOT IDENTICAL TO THE ORIGINAL
  3. * FROM THE BZIP2 DISTRIBUTION.
  4. *
  5. * It has been modified, mainly to break the library
  6. * into smaller pieces.
  7. *
  8. * Russ Cox
  9. * rsc@plan9.bell-labs.com
  10. * July 2000
  11. */
  12. /*-------------------------------------------------------------*/
  13. /*--- Library top-level functions. ---*/
  14. /*--- bzlib.c ---*/
  15. /*-------------------------------------------------------------*/
  16. /*--
  17. This file is a part of bzip2 and/or libbzip2, a program and
  18. library for lossless, block-sorting data compression.
  19. Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
  20. Redistribution and use in source and binary forms, with or without
  21. modification, are permitted provided that the following conditions
  22. are met:
  23. 1. Redistributions of source code must retain the above copyright
  24. notice, this list of conditions and the following disclaimer.
  25. 2. The origin of this software must not be misrepresented; you must
  26. not claim that you wrote the original software. If you use this
  27. software in a product, an acknowledgment in the product
  28. documentation would be appreciated but is not required.
  29. 3. Altered source versions must be plainly marked as such, and must
  30. not be misrepresented as being the original software.
  31. 4. The name of the author may not be used to endorse or promote
  32. products derived from this software without specific prior written
  33. permission.
  34. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  35. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  38. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  39. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  40. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  41. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  42. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  44. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. Julian Seward, Cambridge, UK.
  46. jseward@acm.org
  47. bzip2/libbzip2 version 1.0 of 21 March 2000
  48. This program is based on (at least) the work of:
  49. Mike Burrows
  50. David Wheeler
  51. Peter Fenwick
  52. Alistair Moffat
  53. Radford Neal
  54. Ian H. Witten
  55. Robert Sedgewick
  56. Jon L. Bentley
  57. For more information on these sources, see the manual.
  58. --*/
  59. /*--
  60. CHANGES
  61. ~~~~~~~
  62. 0.9.0 -- original version.
  63. 0.9.0a/b -- no changes in this file.
  64. 0.9.0c
  65. * made zero-length BZ_FLUSH work correctly in bzCompress().
  66. * fixed bzWrite/bzRead to ignore zero-length requests.
  67. * fixed bzread to correctly handle read requests after EOF.
  68. * wrong parameter order in call to bzDecompressInit in
  69. bzBuffToBuffDecompress. Fixed.
  70. --*/
  71. #include "os.h"
  72. #include "bzlib.h"
  73. #include "bzlib_private.h"
  74. /*---------------------------------------------------*/
  75. /*--- Compression stuff ---*/
  76. /*---------------------------------------------------*/
  77. /*---------------------------------------------------*/
  78. static
  79. void prepare_new_block ( EState* s )
  80. {
  81. Int32 i;
  82. s->nblock = 0;
  83. s->numZ = 0;
  84. s->state_out_pos = 0;
  85. BZ_INITIALISE_CRC ( s->blockCRC );
  86. for (i = 0; i < 256; i++) s->inUse[i] = False;
  87. s->blockNo++;
  88. }
  89. /*---------------------------------------------------*/
  90. static
  91. void init_RL ( EState* s )
  92. {
  93. s->state_in_ch = 256;
  94. s->state_in_len = 0;
  95. }
  96. static
  97. Bool isempty_RL ( EState* s )
  98. {
  99. if (s->state_in_ch < 256 && s->state_in_len > 0)
  100. return False; else
  101. return True;
  102. }
  103. /*---------------------------------------------------*/
  104. int BZ_API(BZ2_bzCompressInit)
  105. ( bz_stream* strm,
  106. int blockSize100k,
  107. int verbosity,
  108. int workFactor )
  109. {
  110. Int32 n;
  111. EState* s;
  112. if (!bz_config_ok()) return BZ_CONFIG_ERROR;
  113. if (strm == NULL ||
  114. blockSize100k < 1 || blockSize100k > 9 ||
  115. workFactor < 0 || workFactor > 250)
  116. return BZ_PARAM_ERROR;
  117. if (workFactor == 0) workFactor = 30;
  118. if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
  119. if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
  120. s = BZALLOC( sizeof(EState) );
  121. if (s == NULL) return BZ_MEM_ERROR;
  122. s->strm = strm;
  123. s->arr1 = NULL;
  124. s->arr2 = NULL;
  125. s->ftab = NULL;
  126. n = 100000 * blockSize100k;
  127. s->arr1 = BZALLOC( n * sizeof(UInt32) );
  128. s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
  129. s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
  130. if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
  131. if (s->arr1 != NULL) BZFREE(s->arr1);
  132. if (s->arr2 != NULL) BZFREE(s->arr2);
  133. if (s->ftab != NULL) BZFREE(s->ftab);
  134. if (s != NULL) BZFREE(s);
  135. return BZ_MEM_ERROR;
  136. }
  137. s->blockNo = 0;
  138. s->state = BZ_S_INPUT;
  139. s->mode = BZ_M_RUNNING;
  140. s->combinedCRC = 0;
  141. s->blockSize100k = blockSize100k;
  142. s->nblockMAX = 100000 * blockSize100k - 19;
  143. s->verbosity = verbosity;
  144. s->workFactor = workFactor;
  145. s->block = (UChar*)s->arr2;
  146. s->mtfv = (UInt16*)s->arr1;
  147. s->zbits = NULL;
  148. s->ptr = (UInt32*)s->arr1;
  149. strm->state = s;
  150. strm->total_in_lo32 = 0;
  151. strm->total_in_hi32 = 0;
  152. strm->total_out_lo32 = 0;
  153. strm->total_out_hi32 = 0;
  154. init_RL ( s );
  155. prepare_new_block ( s );
  156. return BZ_OK;
  157. }
  158. /*---------------------------------------------------*/
  159. static
  160. void add_pair_to_block ( EState* s )
  161. {
  162. Int32 i;
  163. UChar ch = (UChar)(s->state_in_ch);
  164. for (i = 0; i < s->state_in_len; i++) {
  165. BZ_UPDATE_CRC( s->blockCRC, ch );
  166. }
  167. s->inUse[s->state_in_ch] = True;
  168. switch (s->state_in_len) {
  169. case 1:
  170. s->block[s->nblock] = (UChar)ch; s->nblock++;
  171. break;
  172. case 2:
  173. s->block[s->nblock] = (UChar)ch; s->nblock++;
  174. s->block[s->nblock] = (UChar)ch; s->nblock++;
  175. break;
  176. case 3:
  177. s->block[s->nblock] = (UChar)ch; s->nblock++;
  178. s->block[s->nblock] = (UChar)ch; s->nblock++;
  179. s->block[s->nblock] = (UChar)ch; s->nblock++;
  180. break;
  181. default:
  182. s->inUse[s->state_in_len-4] = True;
  183. s->block[s->nblock] = (UChar)ch; s->nblock++;
  184. s->block[s->nblock] = (UChar)ch; s->nblock++;
  185. s->block[s->nblock] = (UChar)ch; s->nblock++;
  186. s->block[s->nblock] = (UChar)ch; s->nblock++;
  187. s->block[s->nblock] = ((UChar)(s->state_in_len-4));
  188. s->nblock++;
  189. break;
  190. }
  191. }
  192. /*---------------------------------------------------*/
  193. static
  194. void flush_RL ( EState* s )
  195. {
  196. if (s->state_in_ch < 256) add_pair_to_block ( s );
  197. init_RL ( s );
  198. }
  199. /*---------------------------------------------------*/
  200. #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
  201. { \
  202. UInt32 zchh = (UInt32)(zchh0); \
  203. /*-- fast track the common case --*/ \
  204. if (zchh != zs->state_in_ch && \
  205. zs->state_in_len == 1) { \
  206. UChar ch = (UChar)(zs->state_in_ch); \
  207. BZ_UPDATE_CRC( zs->blockCRC, ch ); \
  208. zs->inUse[zs->state_in_ch] = True; \
  209. zs->block[zs->nblock] = (UChar)ch; \
  210. zs->nblock++; \
  211. zs->state_in_ch = zchh; \
  212. } \
  213. else \
  214. /*-- general, uncommon cases --*/ \
  215. if (zchh != zs->state_in_ch || \
  216. zs->state_in_len == 255) { \
  217. if (zs->state_in_ch < 256) \
  218. add_pair_to_block ( zs ); \
  219. zs->state_in_ch = zchh; \
  220. zs->state_in_len = 1; \
  221. } else { \
  222. zs->state_in_len++; \
  223. } \
  224. }
  225. /*---------------------------------------------------*/
  226. static
  227. Bool copy_input_until_stop ( EState* s )
  228. {
  229. Bool progress_in = False;
  230. if (s->mode == BZ_M_RUNNING) {
  231. /*-- fast track the common case --*/
  232. while (True) {
  233. /*-- block full? --*/
  234. if (s->nblock >= s->nblockMAX) break;
  235. /*-- no input? --*/
  236. if (s->strm->avail_in == 0) break;
  237. progress_in = True;
  238. ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
  239. s->strm->next_in++;
  240. s->strm->avail_in--;
  241. s->strm->total_in_lo32++;
  242. if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
  243. }
  244. } else {
  245. /*-- general, uncommon case --*/
  246. while (True) {
  247. /*-- block full? --*/
  248. if (s->nblock >= s->nblockMAX) break;
  249. /*-- no input? --*/
  250. if (s->strm->avail_in == 0) break;
  251. /*-- flush/finish end? --*/
  252. if (s->avail_in_expect == 0) break;
  253. progress_in = True;
  254. ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
  255. s->strm->next_in++;
  256. s->strm->avail_in--;
  257. s->strm->total_in_lo32++;
  258. if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
  259. s->avail_in_expect--;
  260. }
  261. }
  262. return progress_in;
  263. }
  264. /*---------------------------------------------------*/
  265. static
  266. Bool copy_output_until_stop ( EState* s )
  267. {
  268. Bool progress_out = False;
  269. while (True) {
  270. /*-- no output space? --*/
  271. if (s->strm->avail_out == 0) break;
  272. /*-- block done? --*/
  273. if (s->state_out_pos >= s->numZ) break;
  274. progress_out = True;
  275. *(s->strm->next_out) = s->zbits[s->state_out_pos];
  276. s->state_out_pos++;
  277. s->strm->avail_out--;
  278. s->strm->next_out++;
  279. s->strm->total_out_lo32++;
  280. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  281. }
  282. return progress_out;
  283. }
  284. /*---------------------------------------------------*/
  285. static
  286. Bool handle_compress ( bz_stream* strm )
  287. {
  288. Bool progress_in = False;
  289. Bool progress_out = False;
  290. EState* s = strm->state;
  291. while (True) {
  292. if (s->state == BZ_S_OUTPUT) {
  293. progress_out |= copy_output_until_stop ( s );
  294. if (s->state_out_pos < s->numZ) break;
  295. if (s->mode == BZ_M_FINISHING &&
  296. s->avail_in_expect == 0 &&
  297. isempty_RL(s)) break;
  298. prepare_new_block ( s );
  299. s->state = BZ_S_INPUT;
  300. if (s->mode == BZ_M_FLUSHING &&
  301. s->avail_in_expect == 0 &&
  302. isempty_RL(s)) break;
  303. }
  304. if (s->state == BZ_S_INPUT) {
  305. progress_in |= copy_input_until_stop ( s );
  306. if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
  307. flush_RL ( s );
  308. BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
  309. s->state = BZ_S_OUTPUT;
  310. }
  311. else
  312. if (s->nblock >= s->nblockMAX) {
  313. BZ2_compressBlock ( s, False );
  314. s->state = BZ_S_OUTPUT;
  315. }
  316. else
  317. if (s->strm->avail_in == 0) {
  318. break;
  319. }
  320. }
  321. }
  322. return progress_in || progress_out;
  323. }
  324. /*---------------------------------------------------*/
  325. int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
  326. {
  327. Bool progress;
  328. EState* s;
  329. if (strm == NULL) return BZ_PARAM_ERROR;
  330. s = strm->state;
  331. if (s == NULL) return BZ_PARAM_ERROR;
  332. if (s->strm != strm) return BZ_PARAM_ERROR;
  333. preswitch:
  334. switch (s->mode) {
  335. case BZ_M_IDLE:
  336. return BZ_SEQUENCE_ERROR;
  337. case BZ_M_RUNNING:
  338. if (action == BZ_RUN) {
  339. progress = handle_compress ( strm );
  340. return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
  341. }
  342. else
  343. if (action == BZ_FLUSH) {
  344. s->avail_in_expect = strm->avail_in;
  345. s->mode = BZ_M_FLUSHING;
  346. goto preswitch;
  347. }
  348. else
  349. if (action == BZ_FINISH) {
  350. s->avail_in_expect = strm->avail_in;
  351. s->mode = BZ_M_FINISHING;
  352. goto preswitch;
  353. }
  354. else
  355. return BZ_PARAM_ERROR;
  356. case BZ_M_FLUSHING:
  357. if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
  358. if (s->avail_in_expect != s->strm->avail_in)
  359. return BZ_SEQUENCE_ERROR;
  360. progress = handle_compress ( strm );
  361. if (!progress) return BZ_SEQUENCE_ERROR; //rsc added
  362. if (s->avail_in_expect > 0 || !isempty_RL(s) ||
  363. s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
  364. s->mode = BZ_M_RUNNING;
  365. return BZ_RUN_OK;
  366. case BZ_M_FINISHING:
  367. if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
  368. if (s->avail_in_expect != s->strm->avail_in)
  369. return BZ_SEQUENCE_ERROR;
  370. progress = handle_compress ( strm );
  371. if (!progress) return BZ_SEQUENCE_ERROR;
  372. if (s->avail_in_expect > 0 || !isempty_RL(s) ||
  373. s->state_out_pos < s->numZ) return BZ_FINISH_OK;
  374. s->mode = BZ_M_IDLE;
  375. return BZ_STREAM_END;
  376. }
  377. return BZ_OK; /*--not reached--*/
  378. }
  379. /*---------------------------------------------------*/
  380. int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
  381. {
  382. EState* s;
  383. if (strm == NULL) return BZ_PARAM_ERROR;
  384. s = strm->state;
  385. if (s == NULL) return BZ_PARAM_ERROR;
  386. if (s->strm != strm) return BZ_PARAM_ERROR;
  387. if (s->arr1 != NULL) BZFREE(s->arr1);
  388. if (s->arr2 != NULL) BZFREE(s->arr2);
  389. if (s->ftab != NULL) BZFREE(s->ftab);
  390. BZFREE(strm->state);
  391. strm->state = NULL;
  392. return BZ_OK;
  393. }