bzlib.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. /*-------------------------------------------------------------*/
  2. /*--- Library top-level functions. ---*/
  3. /*--- bzlib.c ---*/
  4. /*-------------------------------------------------------------*/
  5. /* ------------------------------------------------------------------
  6. This file is part of bzip2/libbzip2, a program and library for
  7. lossless, block-sorting data compression.
  8. bzip2/libbzip2 version 1.0.8 of 13 July 2019
  9. Copyright (C) 1996-2019 Julian Seward <jseward@acm.org>
  10. Please read the WARNING, DISCLAIMER and PATENTS sections in the
  11. README file.
  12. This program is released under the terms of the license contained
  13. in the file LICENSE.
  14. ------------------------------------------------------------------ */
  15. /* CHANGES
  16. 0.9.0 -- original version.
  17. 0.9.0a/b -- no changes in this file.
  18. 0.9.0c -- made zero-length BZ_FLUSH work correctly in bzCompress().
  19. fixed bzWrite/bzRead to ignore zero-length requests.
  20. fixed bzread to correctly handle read requests after EOF.
  21. wrong parameter order in call to bzDecompressInit in
  22. bzBuffToBuffDecompress. Fixed.
  23. */
  24. #include "bzlib_private.h"
  25. /*---------------------------------------------------*/
  26. /*--- Compression stuff ---*/
  27. /*---------------------------------------------------*/
  28. /*---------------------------------------------------*/
  29. #ifndef BZ_NO_STDIO
  30. void BZ2_bz__AssertH__fail ( int errcode )
  31. {
  32. fprintf(stderr,
  33. "\n\nbzip2/libbzip2: internal error number %d.\n"
  34. "This is a bug in bzip2/libbzip2, %s.\n"
  35. "Please report it to: bzip2-devel@sourceware.org. If this happened\n"
  36. "when you were using some program which uses libbzip2 as a\n"
  37. "component, you should also report this bug to the author(s)\n"
  38. "of that program. Please make an effort to report this bug;\n"
  39. "timely and accurate bug reports eventually lead to higher\n"
  40. "quality software. Thanks.\n\n",
  41. errcode,
  42. BZ2_bzlibVersion()
  43. );
  44. if (errcode == 1007) {
  45. fprintf(stderr,
  46. "\n*** A special note about internal error number 1007 ***\n"
  47. "\n"
  48. "Experience suggests that a common cause of i.e. 1007\n"
  49. "is unreliable memory or other hardware. The 1007 assertion\n"
  50. "just happens to cross-check the results of huge numbers of\n"
  51. "memory reads/writes, and so acts (unintendedly) as a stress\n"
  52. "test of your memory system.\n"
  53. "\n"
  54. "I suggest the following: try compressing the file again,\n"
  55. "possibly monitoring progress in detail with the -vv flag.\n"
  56. "\n"
  57. "* If the error cannot be reproduced, and/or happens at different\n"
  58. " points in compression, you may have a flaky memory system.\n"
  59. " Try a memory-test program. I have used Memtest86\n"
  60. " (www.memtest86.com). At the time of writing it is free (GPLd).\n"
  61. " Memtest86 tests memory much more thorougly than your BIOSs\n"
  62. " power-on test, and may find failures that the BIOS doesn't.\n"
  63. "\n"
  64. "* If the error can be repeatably reproduced, this is a bug in\n"
  65. " bzip2, and I would very much like to hear about it. Please\n"
  66. " let me know, and, ideally, save a copy of the file causing the\n"
  67. " problem -- without which I will be unable to investigate it.\n"
  68. "\n"
  69. );
  70. }
  71. exit(3);
  72. }
  73. #endif
  74. /*---------------------------------------------------*/
  75. static
  76. int bz_config_ok ( void )
  77. {
  78. if (sizeof(int) != 4) return 0;
  79. if (sizeof(short) != 2) return 0;
  80. if (sizeof(char) != 1) return 0;
  81. return 1;
  82. }
  83. /*---------------------------------------------------*/
  84. static
  85. void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
  86. {
  87. void* v = malloc ( items * size );
  88. return v;
  89. }
  90. static
  91. void default_bzfree ( void* opaque, void* addr )
  92. {
  93. if (addr != NULL) free ( addr );
  94. }
  95. /*---------------------------------------------------*/
  96. static
  97. void prepare_new_block ( EState* s )
  98. {
  99. Int32 i;
  100. s->nblock = 0;
  101. s->numZ = 0;
  102. s->state_out_pos = 0;
  103. BZ_INITIALISE_CRC ( s->blockCRC );
  104. for (i = 0; i < 256; i++) s->inUse[i] = False;
  105. s->blockNo++;
  106. }
  107. /*---------------------------------------------------*/
  108. static
  109. void init_RL ( EState* s )
  110. {
  111. s->state_in_ch = 256;
  112. s->state_in_len = 0;
  113. }
  114. static
  115. Bool isempty_RL ( EState* s )
  116. {
  117. if (s->state_in_ch < 256 && s->state_in_len > 0)
  118. return False; else
  119. return True;
  120. }
  121. /*---------------------------------------------------*/
  122. int BZ_API(BZ2_bzCompressInit)
  123. ( bz_stream* strm,
  124. int blockSize100k,
  125. int verbosity,
  126. int workFactor )
  127. {
  128. Int32 n;
  129. EState* s;
  130. if (!bz_config_ok()) return BZ_CONFIG_ERROR;
  131. if (strm == NULL ||
  132. blockSize100k < 1 || blockSize100k > 9 ||
  133. workFactor < 0 || workFactor > 250)
  134. return BZ_PARAM_ERROR;
  135. if (workFactor == 0) workFactor = 30;
  136. if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
  137. if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
  138. s = BZALLOC( sizeof(EState) );
  139. if (s == NULL) return BZ_MEM_ERROR;
  140. s->strm = strm;
  141. s->arr1 = NULL;
  142. s->arr2 = NULL;
  143. s->ftab = NULL;
  144. n = 100000 * blockSize100k;
  145. s->arr1 = BZALLOC( n * sizeof(UInt32) );
  146. s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
  147. s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
  148. if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
  149. if (s->arr1 != NULL) BZFREE(s->arr1);
  150. if (s->arr2 != NULL) BZFREE(s->arr2);
  151. if (s->ftab != NULL) BZFREE(s->ftab);
  152. if (s != NULL) BZFREE(s);
  153. return BZ_MEM_ERROR;
  154. }
  155. s->blockNo = 0;
  156. s->state = BZ_S_INPUT;
  157. s->mode = BZ_M_RUNNING;
  158. s->combinedCRC = 0;
  159. s->blockSize100k = blockSize100k;
  160. s->nblockMAX = 100000 * blockSize100k - 19;
  161. s->verbosity = verbosity;
  162. s->workFactor = workFactor;
  163. s->block = (UChar*)s->arr2;
  164. s->mtfv = (UInt16*)s->arr1;
  165. s->zbits = NULL;
  166. s->ptr = (UInt32*)s->arr1;
  167. strm->state = s;
  168. strm->total_in_lo32 = 0;
  169. strm->total_in_hi32 = 0;
  170. strm->total_out_lo32 = 0;
  171. strm->total_out_hi32 = 0;
  172. init_RL ( s );
  173. prepare_new_block ( s );
  174. return BZ_OK;
  175. }
  176. /*---------------------------------------------------*/
  177. static
  178. void add_pair_to_block ( EState* s )
  179. {
  180. Int32 i;
  181. UChar ch = (UChar)(s->state_in_ch);
  182. for (i = 0; i < s->state_in_len; i++) {
  183. BZ_UPDATE_CRC( s->blockCRC, ch );
  184. }
  185. s->inUse[s->state_in_ch] = True;
  186. switch (s->state_in_len) {
  187. case 1:
  188. s->block[s->nblock] = (UChar)ch; s->nblock++;
  189. break;
  190. case 2:
  191. s->block[s->nblock] = (UChar)ch; s->nblock++;
  192. s->block[s->nblock] = (UChar)ch; s->nblock++;
  193. break;
  194. case 3:
  195. s->block[s->nblock] = (UChar)ch; s->nblock++;
  196. s->block[s->nblock] = (UChar)ch; s->nblock++;
  197. s->block[s->nblock] = (UChar)ch; s->nblock++;
  198. break;
  199. default:
  200. s->inUse[s->state_in_len-4] = True;
  201. s->block[s->nblock] = (UChar)ch; s->nblock++;
  202. s->block[s->nblock] = (UChar)ch; s->nblock++;
  203. s->block[s->nblock] = (UChar)ch; s->nblock++;
  204. s->block[s->nblock] = (UChar)ch; s->nblock++;
  205. s->block[s->nblock] = ((UChar)(s->state_in_len-4));
  206. s->nblock++;
  207. break;
  208. }
  209. }
  210. /*---------------------------------------------------*/
  211. static
  212. void flush_RL ( EState* s )
  213. {
  214. if (s->state_in_ch < 256) add_pair_to_block ( s );
  215. init_RL ( s );
  216. }
  217. /*---------------------------------------------------*/
  218. #define ADD_CHAR_TO_BLOCK(zs,zchh0) \
  219. { \
  220. UInt32 zchh = (UInt32)(zchh0); \
  221. /*-- fast track the common case --*/ \
  222. if (zchh != zs->state_in_ch && \
  223. zs->state_in_len == 1) { \
  224. UChar ch = (UChar)(zs->state_in_ch); \
  225. BZ_UPDATE_CRC( zs->blockCRC, ch ); \
  226. zs->inUse[zs->state_in_ch] = True; \
  227. zs->block[zs->nblock] = (UChar)ch; \
  228. zs->nblock++; \
  229. zs->state_in_ch = zchh; \
  230. } \
  231. else \
  232. /*-- general, uncommon cases --*/ \
  233. if (zchh != zs->state_in_ch || \
  234. zs->state_in_len == 255) { \
  235. if (zs->state_in_ch < 256) \
  236. add_pair_to_block ( zs ); \
  237. zs->state_in_ch = zchh; \
  238. zs->state_in_len = 1; \
  239. } else { \
  240. zs->state_in_len++; \
  241. } \
  242. }
  243. /*---------------------------------------------------*/
  244. static
  245. Bool copy_input_until_stop ( EState* s )
  246. {
  247. Bool progress_in = False;
  248. if (s->mode == BZ_M_RUNNING) {
  249. /*-- fast track the common case --*/
  250. while (True) {
  251. /*-- block full? --*/
  252. if (s->nblock >= s->nblockMAX) break;
  253. /*-- no input? --*/
  254. if (s->strm->avail_in == 0) break;
  255. progress_in = True;
  256. ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
  257. s->strm->next_in++;
  258. s->strm->avail_in--;
  259. s->strm->total_in_lo32++;
  260. if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
  261. }
  262. } else {
  263. /*-- general, uncommon case --*/
  264. while (True) {
  265. /*-- block full? --*/
  266. if (s->nblock >= s->nblockMAX) break;
  267. /*-- no input? --*/
  268. if (s->strm->avail_in == 0) break;
  269. /*-- flush/finish end? --*/
  270. if (s->avail_in_expect == 0) break;
  271. progress_in = True;
  272. ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
  273. s->strm->next_in++;
  274. s->strm->avail_in--;
  275. s->strm->total_in_lo32++;
  276. if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
  277. s->avail_in_expect--;
  278. }
  279. }
  280. return progress_in;
  281. }
  282. /*---------------------------------------------------*/
  283. static
  284. Bool copy_output_until_stop ( EState* s )
  285. {
  286. Bool progress_out = False;
  287. while (True) {
  288. /*-- no output space? --*/
  289. if (s->strm->avail_out == 0) break;
  290. /*-- block done? --*/
  291. if (s->state_out_pos >= s->numZ) break;
  292. progress_out = True;
  293. *(s->strm->next_out) = s->zbits[s->state_out_pos];
  294. s->state_out_pos++;
  295. s->strm->avail_out--;
  296. s->strm->next_out++;
  297. s->strm->total_out_lo32++;
  298. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  299. }
  300. return progress_out;
  301. }
  302. /*---------------------------------------------------*/
  303. static
  304. Bool handle_compress ( bz_stream* strm )
  305. {
  306. Bool progress_in = False;
  307. Bool progress_out = False;
  308. EState* s = strm->state;
  309. while (True) {
  310. if (s->state == BZ_S_OUTPUT) {
  311. progress_out |= copy_output_until_stop ( s );
  312. if (s->state_out_pos < s->numZ) break;
  313. if (s->mode == BZ_M_FINISHING &&
  314. s->avail_in_expect == 0 &&
  315. isempty_RL(s)) break;
  316. prepare_new_block ( s );
  317. s->state = BZ_S_INPUT;
  318. if (s->mode == BZ_M_FLUSHING &&
  319. s->avail_in_expect == 0 &&
  320. isempty_RL(s)) break;
  321. }
  322. if (s->state == BZ_S_INPUT) {
  323. progress_in |= copy_input_until_stop ( s );
  324. if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
  325. flush_RL ( s );
  326. BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
  327. s->state = BZ_S_OUTPUT;
  328. }
  329. else
  330. if (s->nblock >= s->nblockMAX) {
  331. BZ2_compressBlock ( s, False );
  332. s->state = BZ_S_OUTPUT;
  333. }
  334. else
  335. if (s->strm->avail_in == 0) {
  336. break;
  337. }
  338. }
  339. }
  340. return progress_in || progress_out;
  341. }
  342. /*---------------------------------------------------*/
  343. int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
  344. {
  345. Bool progress;
  346. EState* s;
  347. if (strm == NULL) return BZ_PARAM_ERROR;
  348. s = strm->state;
  349. if (s == NULL) return BZ_PARAM_ERROR;
  350. if (s->strm != strm) return BZ_PARAM_ERROR;
  351. preswitch:
  352. switch (s->mode) {
  353. case BZ_M_IDLE:
  354. return BZ_SEQUENCE_ERROR;
  355. case BZ_M_RUNNING:
  356. if (action == BZ_RUN) {
  357. progress = handle_compress ( strm );
  358. return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
  359. }
  360. else
  361. if (action == BZ_FLUSH) {
  362. s->avail_in_expect = strm->avail_in;
  363. s->mode = BZ_M_FLUSHING;
  364. goto preswitch;
  365. }
  366. else
  367. if (action == BZ_FINISH) {
  368. s->avail_in_expect = strm->avail_in;
  369. s->mode = BZ_M_FINISHING;
  370. goto preswitch;
  371. }
  372. else
  373. return BZ_PARAM_ERROR;
  374. case BZ_M_FLUSHING:
  375. if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
  376. if (s->avail_in_expect != s->strm->avail_in)
  377. return BZ_SEQUENCE_ERROR;
  378. progress = handle_compress ( strm );
  379. if (s->avail_in_expect > 0 || !isempty_RL(s) ||
  380. s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
  381. s->mode = BZ_M_RUNNING;
  382. return BZ_RUN_OK;
  383. case BZ_M_FINISHING:
  384. if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
  385. if (s->avail_in_expect != s->strm->avail_in)
  386. return BZ_SEQUENCE_ERROR;
  387. progress = handle_compress ( strm );
  388. if (!progress) return BZ_SEQUENCE_ERROR;
  389. if (s->avail_in_expect > 0 || !isempty_RL(s) ||
  390. s->state_out_pos < s->numZ) return BZ_FINISH_OK;
  391. s->mode = BZ_M_IDLE;
  392. return BZ_STREAM_END;
  393. }
  394. return BZ_OK; /*--not reached--*/
  395. }
  396. /*---------------------------------------------------*/
  397. int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
  398. {
  399. EState* s;
  400. if (strm == NULL) return BZ_PARAM_ERROR;
  401. s = strm->state;
  402. if (s == NULL) return BZ_PARAM_ERROR;
  403. if (s->strm != strm) return BZ_PARAM_ERROR;
  404. if (s->arr1 != NULL) BZFREE(s->arr1);
  405. if (s->arr2 != NULL) BZFREE(s->arr2);
  406. if (s->ftab != NULL) BZFREE(s->ftab);
  407. BZFREE(strm->state);
  408. strm->state = NULL;
  409. return BZ_OK;
  410. }
  411. /*---------------------------------------------------*/
  412. /*--- Decompression stuff ---*/
  413. /*---------------------------------------------------*/
  414. /*---------------------------------------------------*/
  415. int BZ_API(BZ2_bzDecompressInit)
  416. ( bz_stream* strm,
  417. int verbosity,
  418. int small )
  419. {
  420. DState* s;
  421. if (!bz_config_ok()) return BZ_CONFIG_ERROR;
  422. if (strm == NULL) return BZ_PARAM_ERROR;
  423. if (small != 0 && small != 1) return BZ_PARAM_ERROR;
  424. if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR;
  425. if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
  426. if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
  427. s = BZALLOC( sizeof(DState) );
  428. if (s == NULL) return BZ_MEM_ERROR;
  429. s->strm = strm;
  430. strm->state = s;
  431. s->state = BZ_X_MAGIC_1;
  432. s->bsLive = 0;
  433. s->bsBuff = 0;
  434. s->calculatedCombinedCRC = 0;
  435. strm->total_in_lo32 = 0;
  436. strm->total_in_hi32 = 0;
  437. strm->total_out_lo32 = 0;
  438. strm->total_out_hi32 = 0;
  439. s->smallDecompress = (Bool)small;
  440. s->ll4 = NULL;
  441. s->ll16 = NULL;
  442. s->tt = NULL;
  443. s->currBlockNo = 0;
  444. s->verbosity = verbosity;
  445. return BZ_OK;
  446. }
  447. /*---------------------------------------------------*/
  448. /* Return True iff data corruption is discovered.
  449. Returns False if there is no problem.
  450. */
  451. static
  452. Bool unRLE_obuf_to_output_FAST ( DState* s )
  453. {
  454. UChar k1;
  455. if (s->blockRandomised) {
  456. while (True) {
  457. /* try to finish existing run */
  458. while (True) {
  459. if (s->strm->avail_out == 0) return False;
  460. if (s->state_out_len == 0) break;
  461. *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
  462. BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
  463. s->state_out_len--;
  464. s->strm->next_out++;
  465. s->strm->avail_out--;
  466. s->strm->total_out_lo32++;
  467. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  468. }
  469. /* can a new run be started? */
  470. if (s->nblock_used == s->save_nblock+1) return False;
  471. /* Only caused by corrupt data stream? */
  472. if (s->nblock_used > s->save_nblock+1)
  473. return True;
  474. s->state_out_len = 1;
  475. s->state_out_ch = s->k0;
  476. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  477. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  478. if (s->nblock_used == s->save_nblock+1) continue;
  479. if (k1 != s->k0) { s->k0 = k1; continue; };
  480. s->state_out_len = 2;
  481. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  482. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  483. if (s->nblock_used == s->save_nblock+1) continue;
  484. if (k1 != s->k0) { s->k0 = k1; continue; };
  485. s->state_out_len = 3;
  486. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  487. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  488. if (s->nblock_used == s->save_nblock+1) continue;
  489. if (k1 != s->k0) { s->k0 = k1; continue; };
  490. BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
  491. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  492. s->state_out_len = ((Int32)k1) + 4;
  493. BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK;
  494. s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
  495. }
  496. } else {
  497. /* restore */
  498. UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC;
  499. UChar c_state_out_ch = s->state_out_ch;
  500. Int32 c_state_out_len = s->state_out_len;
  501. Int32 c_nblock_used = s->nblock_used;
  502. Int32 c_k0 = s->k0;
  503. UInt32* c_tt = s->tt;
  504. UInt32 c_tPos = s->tPos;
  505. char* cs_next_out = s->strm->next_out;
  506. unsigned int cs_avail_out = s->strm->avail_out;
  507. Int32 ro_blockSize100k = s->blockSize100k;
  508. /* end restore */
  509. UInt32 avail_out_INIT = cs_avail_out;
  510. Int32 s_save_nblockPP = s->save_nblock+1;
  511. unsigned int total_out_lo32_old;
  512. while (True) {
  513. /* try to finish existing run */
  514. if (c_state_out_len > 0) {
  515. while (True) {
  516. if (cs_avail_out == 0) goto return_notr;
  517. if (c_state_out_len == 1) break;
  518. *( (UChar*)(cs_next_out) ) = c_state_out_ch;
  519. BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
  520. c_state_out_len--;
  521. cs_next_out++;
  522. cs_avail_out--;
  523. }
  524. s_state_out_len_eq_one:
  525. {
  526. if (cs_avail_out == 0) {
  527. c_state_out_len = 1; goto return_notr;
  528. };
  529. *( (UChar*)(cs_next_out) ) = c_state_out_ch;
  530. BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
  531. cs_next_out++;
  532. cs_avail_out--;
  533. }
  534. }
  535. /* Only caused by corrupt data stream? */
  536. if (c_nblock_used > s_save_nblockPP)
  537. return True;
  538. /* can a new run be started? */
  539. if (c_nblock_used == s_save_nblockPP) {
  540. c_state_out_len = 0; goto return_notr;
  541. };
  542. c_state_out_ch = c_k0;
  543. BZ_GET_FAST_C(k1); c_nblock_used++;
  544. if (k1 != c_k0) {
  545. c_k0 = k1; goto s_state_out_len_eq_one;
  546. };
  547. if (c_nblock_used == s_save_nblockPP)
  548. goto s_state_out_len_eq_one;
  549. c_state_out_len = 2;
  550. BZ_GET_FAST_C(k1); c_nblock_used++;
  551. if (c_nblock_used == s_save_nblockPP) continue;
  552. if (k1 != c_k0) { c_k0 = k1; continue; };
  553. c_state_out_len = 3;
  554. BZ_GET_FAST_C(k1); c_nblock_used++;
  555. if (c_nblock_used == s_save_nblockPP) continue;
  556. if (k1 != c_k0) { c_k0 = k1; continue; };
  557. BZ_GET_FAST_C(k1); c_nblock_used++;
  558. c_state_out_len = ((Int32)k1) + 4;
  559. BZ_GET_FAST_C(c_k0); c_nblock_used++;
  560. }
  561. return_notr:
  562. total_out_lo32_old = s->strm->total_out_lo32;
  563. s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
  564. if (s->strm->total_out_lo32 < total_out_lo32_old)
  565. s->strm->total_out_hi32++;
  566. /* save */
  567. s->calculatedBlockCRC = c_calculatedBlockCRC;
  568. s->state_out_ch = c_state_out_ch;
  569. s->state_out_len = c_state_out_len;
  570. s->nblock_used = c_nblock_used;
  571. s->k0 = c_k0;
  572. s->tt = c_tt;
  573. s->tPos = c_tPos;
  574. s->strm->next_out = cs_next_out;
  575. s->strm->avail_out = cs_avail_out;
  576. /* end save */
  577. }
  578. return False;
  579. }
  580. /*---------------------------------------------------*/
  581. __inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
  582. {
  583. Int32 nb, na, mid;
  584. nb = 0;
  585. na = 256;
  586. do {
  587. mid = (nb + na) >> 1;
  588. if (indx >= cftab[mid]) nb = mid; else na = mid;
  589. }
  590. while (na - nb != 1);
  591. return nb;
  592. }
  593. /*---------------------------------------------------*/
  594. /* Return True iff data corruption is discovered.
  595. Returns False if there is no problem.
  596. */
  597. static
  598. Bool unRLE_obuf_to_output_SMALL ( DState* s )
  599. {
  600. UChar k1;
  601. if (s->blockRandomised) {
  602. while (True) {
  603. /* try to finish existing run */
  604. while (True) {
  605. if (s->strm->avail_out == 0) return False;
  606. if (s->state_out_len == 0) break;
  607. *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
  608. BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
  609. s->state_out_len--;
  610. s->strm->next_out++;
  611. s->strm->avail_out--;
  612. s->strm->total_out_lo32++;
  613. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  614. }
  615. /* can a new run be started? */
  616. if (s->nblock_used == s->save_nblock+1) return False;
  617. /* Only caused by corrupt data stream? */
  618. if (s->nblock_used > s->save_nblock+1)
  619. return True;
  620. s->state_out_len = 1;
  621. s->state_out_ch = s->k0;
  622. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  623. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  624. if (s->nblock_used == s->save_nblock+1) continue;
  625. if (k1 != s->k0) { s->k0 = k1; continue; };
  626. s->state_out_len = 2;
  627. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  628. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  629. if (s->nblock_used == s->save_nblock+1) continue;
  630. if (k1 != s->k0) { s->k0 = k1; continue; };
  631. s->state_out_len = 3;
  632. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  633. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  634. if (s->nblock_used == s->save_nblock+1) continue;
  635. if (k1 != s->k0) { s->k0 = k1; continue; };
  636. BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
  637. k1 ^= BZ_RAND_MASK; s->nblock_used++;
  638. s->state_out_len = ((Int32)k1) + 4;
  639. BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK;
  640. s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
  641. }
  642. } else {
  643. while (True) {
  644. /* try to finish existing run */
  645. while (True) {
  646. if (s->strm->avail_out == 0) return False;
  647. if (s->state_out_len == 0) break;
  648. *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
  649. BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
  650. s->state_out_len--;
  651. s->strm->next_out++;
  652. s->strm->avail_out--;
  653. s->strm->total_out_lo32++;
  654. if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
  655. }
  656. /* can a new run be started? */
  657. if (s->nblock_used == s->save_nblock+1) return False;
  658. /* Only caused by corrupt data stream? */
  659. if (s->nblock_used > s->save_nblock+1)
  660. return True;
  661. s->state_out_len = 1;
  662. s->state_out_ch = s->k0;
  663. BZ_GET_SMALL(k1); s->nblock_used++;
  664. if (s->nblock_used == s->save_nblock+1) continue;
  665. if (k1 != s->k0) { s->k0 = k1; continue; };
  666. s->state_out_len = 2;
  667. BZ_GET_SMALL(k1); s->nblock_used++;
  668. if (s->nblock_used == s->save_nblock+1) continue;
  669. if (k1 != s->k0) { s->k0 = k1; continue; };
  670. s->state_out_len = 3;
  671. BZ_GET_SMALL(k1); s->nblock_used++;
  672. if (s->nblock_used == s->save_nblock+1) continue;
  673. if (k1 != s->k0) { s->k0 = k1; continue; };
  674. BZ_GET_SMALL(k1); s->nblock_used++;
  675. s->state_out_len = ((Int32)k1) + 4;
  676. BZ_GET_SMALL(s->k0); s->nblock_used++;
  677. }
  678. }
  679. }
  680. /*---------------------------------------------------*/
  681. int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
  682. {
  683. Bool corrupt;
  684. DState* s;
  685. if (strm == NULL) return BZ_PARAM_ERROR;
  686. s = strm->state;
  687. if (s == NULL) return BZ_PARAM_ERROR;
  688. if (s->strm != strm) return BZ_PARAM_ERROR;
  689. while (True) {
  690. if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR;
  691. if (s->state == BZ_X_OUTPUT) {
  692. if (s->smallDecompress)
  693. corrupt = unRLE_obuf_to_output_SMALL ( s ); else
  694. corrupt = unRLE_obuf_to_output_FAST ( s );
  695. if (corrupt) return BZ_DATA_ERROR;
  696. if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
  697. BZ_FINALISE_CRC ( s->calculatedBlockCRC );
  698. if (s->verbosity >= 3)
  699. VPrintf2 ( " {0x%08x, 0x%08x}", s->storedBlockCRC,
  700. s->calculatedBlockCRC );
  701. if (s->verbosity >= 2) VPrintf0 ( "]" );
  702. if (s->calculatedBlockCRC != s->storedBlockCRC)
  703. return BZ_DATA_ERROR;
  704. s->calculatedCombinedCRC
  705. = (s->calculatedCombinedCRC << 1) |
  706. (s->calculatedCombinedCRC >> 31);
  707. s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
  708. s->state = BZ_X_BLKHDR_1;
  709. } else {
  710. return BZ_OK;
  711. }
  712. }
  713. if (s->state >= BZ_X_MAGIC_1) {
  714. Int32 r = BZ2_decompress ( s );
  715. if (r == BZ_STREAM_END) {
  716. if (s->verbosity >= 3)
  717. VPrintf2 ( "\n combined CRCs: stored = 0x%08x, computed = 0x%08x",
  718. s->storedCombinedCRC, s->calculatedCombinedCRC );
  719. if (s->calculatedCombinedCRC != s->storedCombinedCRC)
  720. return BZ_DATA_ERROR;
  721. return r;
  722. }
  723. if (s->state != BZ_X_OUTPUT) return r;
  724. }
  725. }
  726. AssertH ( 0, 6001 );
  727. return 0; /*NOTREACHED*/
  728. }
  729. /*---------------------------------------------------*/
  730. int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm )
  731. {
  732. DState* s;
  733. if (strm == NULL) return BZ_PARAM_ERROR;
  734. s = strm->state;
  735. if (s == NULL) return BZ_PARAM_ERROR;
  736. if (s->strm != strm) return BZ_PARAM_ERROR;
  737. if (s->tt != NULL) BZFREE(s->tt);
  738. if (s->ll16 != NULL) BZFREE(s->ll16);
  739. if (s->ll4 != NULL) BZFREE(s->ll4);
  740. BZFREE(strm->state);
  741. strm->state = NULL;
  742. return BZ_OK;
  743. }
  744. #ifndef BZ_NO_STDIO
  745. /*---------------------------------------------------*/
  746. /*--- File I/O stuff ---*/
  747. /*---------------------------------------------------*/
  748. #define BZ_SETERR(eee) \
  749. { \
  750. if (bzerror != NULL) *bzerror = eee; \
  751. if (bzf != NULL) bzf->lastErr = eee; \
  752. }
  753. typedef
  754. struct {
  755. FILE* handle;
  756. Char buf[BZ_MAX_UNUSED];
  757. Int32 bufN;
  758. Bool writing;
  759. bz_stream strm;
  760. Int32 lastErr;
  761. Bool initialisedOk;
  762. }
  763. bzFile;
  764. /*---------------------------------------------*/
  765. static Bool myfeof ( FILE* f )
  766. {
  767. Int32 c = fgetc ( f );
  768. if (c == EOF) return True;
  769. ungetc ( c, f );
  770. return False;
  771. }
  772. /*---------------------------------------------------*/
  773. BZFILE* BZ_API(BZ2_bzWriteOpen)
  774. ( int* bzerror,
  775. FILE* f,
  776. int blockSize100k,
  777. int verbosity,
  778. int workFactor )
  779. {
  780. Int32 ret;
  781. bzFile* bzf = NULL;
  782. BZ_SETERR(BZ_OK);
  783. if (f == NULL ||
  784. (blockSize100k < 1 || blockSize100k > 9) ||
  785. (workFactor < 0 || workFactor > 250) ||
  786. (verbosity < 0 || verbosity > 4))
  787. { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
  788. if (ferror(f))
  789. { BZ_SETERR(BZ_IO_ERROR); return NULL; };
  790. bzf = malloc ( sizeof(bzFile) );
  791. if (bzf == NULL)
  792. { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
  793. BZ_SETERR(BZ_OK);
  794. bzf->initialisedOk = False;
  795. bzf->bufN = 0;
  796. bzf->handle = f;
  797. bzf->writing = True;
  798. bzf->strm.bzalloc = NULL;
  799. bzf->strm.bzfree = NULL;
  800. bzf->strm.opaque = NULL;
  801. if (workFactor == 0) workFactor = 30;
  802. ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k,
  803. verbosity, workFactor );
  804. if (ret != BZ_OK)
  805. { BZ_SETERR(ret); free(bzf); return NULL; };
  806. bzf->strm.avail_in = 0;
  807. bzf->initialisedOk = True;
  808. return bzf;
  809. }
  810. /*---------------------------------------------------*/
  811. void BZ_API(BZ2_bzWrite)
  812. ( int* bzerror,
  813. BZFILE* b,
  814. void* buf,
  815. int len )
  816. {
  817. Int32 n, n2, ret;
  818. bzFile* bzf = (bzFile*)b;
  819. BZ_SETERR(BZ_OK);
  820. if (bzf == NULL || buf == NULL || len < 0)
  821. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  822. if (!(bzf->writing))
  823. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  824. if (ferror(bzf->handle))
  825. { BZ_SETERR(BZ_IO_ERROR); return; };
  826. if (len == 0)
  827. { BZ_SETERR(BZ_OK); return; };
  828. bzf->strm.avail_in = len;
  829. bzf->strm.next_in = buf;
  830. while (True) {
  831. bzf->strm.avail_out = BZ_MAX_UNUSED;
  832. bzf->strm.next_out = bzf->buf;
  833. ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN );
  834. if (ret != BZ_RUN_OK)
  835. { BZ_SETERR(ret); return; };
  836. if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
  837. n = BZ_MAX_UNUSED - bzf->strm.avail_out;
  838. n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
  839. n, bzf->handle );
  840. if (n != n2 || ferror(bzf->handle))
  841. { BZ_SETERR(BZ_IO_ERROR); return; };
  842. }
  843. if (bzf->strm.avail_in == 0)
  844. { BZ_SETERR(BZ_OK); return; };
  845. }
  846. }
  847. /*---------------------------------------------------*/
  848. void BZ_API(BZ2_bzWriteClose)
  849. ( int* bzerror,
  850. BZFILE* b,
  851. int abandon,
  852. unsigned int* nbytes_in,
  853. unsigned int* nbytes_out )
  854. {
  855. BZ2_bzWriteClose64 ( bzerror, b, abandon,
  856. nbytes_in, NULL, nbytes_out, NULL );
  857. }
  858. void BZ_API(BZ2_bzWriteClose64)
  859. ( int* bzerror,
  860. BZFILE* b,
  861. int abandon,
  862. unsigned int* nbytes_in_lo32,
  863. unsigned int* nbytes_in_hi32,
  864. unsigned int* nbytes_out_lo32,
  865. unsigned int* nbytes_out_hi32 )
  866. {
  867. Int32 n, n2, ret;
  868. bzFile* bzf = (bzFile*)b;
  869. if (bzf == NULL)
  870. { BZ_SETERR(BZ_OK); return; };
  871. if (!(bzf->writing))
  872. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  873. if (ferror(bzf->handle))
  874. { BZ_SETERR(BZ_IO_ERROR); return; };
  875. if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0;
  876. if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0;
  877. if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0;
  878. if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0;
  879. if ((!abandon) && bzf->lastErr == BZ_OK) {
  880. while (True) {
  881. bzf->strm.avail_out = BZ_MAX_UNUSED;
  882. bzf->strm.next_out = bzf->buf;
  883. ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH );
  884. if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
  885. { BZ_SETERR(ret); return; };
  886. if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
  887. n = BZ_MAX_UNUSED - bzf->strm.avail_out;
  888. n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
  889. n, bzf->handle );
  890. if (n != n2 || ferror(bzf->handle))
  891. { BZ_SETERR(BZ_IO_ERROR); return; };
  892. }
  893. if (ret == BZ_STREAM_END) break;
  894. }
  895. }
  896. if ( !abandon && !ferror ( bzf->handle ) ) {
  897. fflush ( bzf->handle );
  898. if (ferror(bzf->handle))
  899. { BZ_SETERR(BZ_IO_ERROR); return; };
  900. }
  901. if (nbytes_in_lo32 != NULL)
  902. *nbytes_in_lo32 = bzf->strm.total_in_lo32;
  903. if (nbytes_in_hi32 != NULL)
  904. *nbytes_in_hi32 = bzf->strm.total_in_hi32;
  905. if (nbytes_out_lo32 != NULL)
  906. *nbytes_out_lo32 = bzf->strm.total_out_lo32;
  907. if (nbytes_out_hi32 != NULL)
  908. *nbytes_out_hi32 = bzf->strm.total_out_hi32;
  909. BZ_SETERR(BZ_OK);
  910. BZ2_bzCompressEnd ( &(bzf->strm) );
  911. free ( bzf );
  912. }
  913. /*---------------------------------------------------*/
  914. BZFILE* BZ_API(BZ2_bzReadOpen)
  915. ( int* bzerror,
  916. FILE* f,
  917. int verbosity,
  918. int small,
  919. void* unused,
  920. int nUnused )
  921. {
  922. bzFile* bzf = NULL;
  923. int ret;
  924. BZ_SETERR(BZ_OK);
  925. if (f == NULL ||
  926. (small != 0 && small != 1) ||
  927. (verbosity < 0 || verbosity > 4) ||
  928. (unused == NULL && nUnused != 0) ||
  929. (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
  930. { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
  931. if (ferror(f))
  932. { BZ_SETERR(BZ_IO_ERROR); return NULL; };
  933. bzf = malloc ( sizeof(bzFile) );
  934. if (bzf == NULL)
  935. { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
  936. BZ_SETERR(BZ_OK);
  937. bzf->initialisedOk = False;
  938. bzf->handle = f;
  939. bzf->bufN = 0;
  940. bzf->writing = False;
  941. bzf->strm.bzalloc = NULL;
  942. bzf->strm.bzfree = NULL;
  943. bzf->strm.opaque = NULL;
  944. while (nUnused > 0) {
  945. bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
  946. unused = ((void*)( 1 + ((UChar*)(unused)) ));
  947. nUnused--;
  948. }
  949. ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
  950. if (ret != BZ_OK)
  951. { BZ_SETERR(ret); free(bzf); return NULL; };
  952. bzf->strm.avail_in = bzf->bufN;
  953. bzf->strm.next_in = bzf->buf;
  954. bzf->initialisedOk = True;
  955. return bzf;
  956. }
  957. /*---------------------------------------------------*/
  958. void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
  959. {
  960. bzFile* bzf = (bzFile*)b;
  961. BZ_SETERR(BZ_OK);
  962. if (bzf == NULL)
  963. { BZ_SETERR(BZ_OK); return; };
  964. if (bzf->writing)
  965. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  966. if (bzf->initialisedOk)
  967. (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
  968. free ( bzf );
  969. }
  970. /*---------------------------------------------------*/
  971. int BZ_API(BZ2_bzRead)
  972. ( int* bzerror,
  973. BZFILE* b,
  974. void* buf,
  975. int len )
  976. {
  977. Int32 n, ret;
  978. bzFile* bzf = (bzFile*)b;
  979. BZ_SETERR(BZ_OK);
  980. if (bzf == NULL || buf == NULL || len < 0)
  981. { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
  982. if (bzf->writing)
  983. { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
  984. if (len == 0)
  985. { BZ_SETERR(BZ_OK); return 0; };
  986. bzf->strm.avail_out = len;
  987. bzf->strm.next_out = buf;
  988. while (True) {
  989. if (ferror(bzf->handle))
  990. { BZ_SETERR(BZ_IO_ERROR); return 0; };
  991. if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) {
  992. n = fread ( bzf->buf, sizeof(UChar),
  993. BZ_MAX_UNUSED, bzf->handle );
  994. if (ferror(bzf->handle))
  995. { BZ_SETERR(BZ_IO_ERROR); return 0; };
  996. bzf->bufN = n;
  997. bzf->strm.avail_in = bzf->bufN;
  998. bzf->strm.next_in = bzf->buf;
  999. }
  1000. ret = BZ2_bzDecompress ( &(bzf->strm) );
  1001. if (ret != BZ_OK && ret != BZ_STREAM_END)
  1002. { BZ_SETERR(ret); return 0; };
  1003. if (ret == BZ_OK && myfeof(bzf->handle) &&
  1004. bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
  1005. { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
  1006. if (ret == BZ_STREAM_END)
  1007. { BZ_SETERR(BZ_STREAM_END);
  1008. return len - bzf->strm.avail_out; };
  1009. if (bzf->strm.avail_out == 0)
  1010. { BZ_SETERR(BZ_OK); return len; };
  1011. }
  1012. return 0; /*not reached*/
  1013. }
  1014. /*---------------------------------------------------*/
  1015. void BZ_API(BZ2_bzReadGetUnused)
  1016. ( int* bzerror,
  1017. BZFILE* b,
  1018. void** unused,
  1019. int* nUnused )
  1020. {
  1021. bzFile* bzf = (bzFile*)b;
  1022. if (bzf == NULL)
  1023. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  1024. if (bzf->lastErr != BZ_STREAM_END)
  1025. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  1026. if (unused == NULL || nUnused == NULL)
  1027. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  1028. BZ_SETERR(BZ_OK);
  1029. *nUnused = bzf->strm.avail_in;
  1030. *unused = bzf->strm.next_in;
  1031. }
  1032. #endif
  1033. /*---------------------------------------------------*/
  1034. /*--- Misc convenience stuff ---*/
  1035. /*---------------------------------------------------*/
  1036. /*---------------------------------------------------*/
  1037. int BZ_API(BZ2_bzBuffToBuffCompress)
  1038. ( char* dest,
  1039. unsigned int* destLen,
  1040. char* source,
  1041. unsigned int sourceLen,
  1042. int blockSize100k,
  1043. int verbosity,
  1044. int workFactor )
  1045. {
  1046. bz_stream strm;
  1047. int ret;
  1048. if (dest == NULL || destLen == NULL ||
  1049. source == NULL ||
  1050. blockSize100k < 1 || blockSize100k > 9 ||
  1051. verbosity < 0 || verbosity > 4 ||
  1052. workFactor < 0 || workFactor > 250)
  1053. return BZ_PARAM_ERROR;
  1054. if (workFactor == 0) workFactor = 30;
  1055. strm.bzalloc = NULL;
  1056. strm.bzfree = NULL;
  1057. strm.opaque = NULL;
  1058. ret = BZ2_bzCompressInit ( &strm, blockSize100k,
  1059. verbosity, workFactor );
  1060. if (ret != BZ_OK) return ret;
  1061. strm.next_in = source;
  1062. strm.next_out = dest;
  1063. strm.avail_in = sourceLen;
  1064. strm.avail_out = *destLen;
  1065. ret = BZ2_bzCompress ( &strm, BZ_FINISH );
  1066. if (ret == BZ_FINISH_OK) goto output_overflow;
  1067. if (ret != BZ_STREAM_END) goto errhandler;
  1068. /* normal termination */
  1069. *destLen -= strm.avail_out;
  1070. BZ2_bzCompressEnd ( &strm );
  1071. return BZ_OK;
  1072. output_overflow:
  1073. BZ2_bzCompressEnd ( &strm );
  1074. return BZ_OUTBUFF_FULL;
  1075. errhandler:
  1076. BZ2_bzCompressEnd ( &strm );
  1077. return ret;
  1078. }
  1079. /*---------------------------------------------------*/
  1080. int BZ_API(BZ2_bzBuffToBuffDecompress)
  1081. ( char* dest,
  1082. unsigned int* destLen,
  1083. char* source,
  1084. unsigned int sourceLen,
  1085. int small,
  1086. int verbosity )
  1087. {
  1088. bz_stream strm;
  1089. int ret;
  1090. if (dest == NULL || destLen == NULL ||
  1091. source == NULL ||
  1092. (small != 0 && small != 1) ||
  1093. verbosity < 0 || verbosity > 4)
  1094. return BZ_PARAM_ERROR;
  1095. strm.bzalloc = NULL;
  1096. strm.bzfree = NULL;
  1097. strm.opaque = NULL;
  1098. ret = BZ2_bzDecompressInit ( &strm, verbosity, small );
  1099. if (ret != BZ_OK) return ret;
  1100. strm.next_in = source;
  1101. strm.next_out = dest;
  1102. strm.avail_in = sourceLen;
  1103. strm.avail_out = *destLen;
  1104. ret = BZ2_bzDecompress ( &strm );
  1105. if (ret == BZ_OK) goto output_overflow_or_eof;
  1106. if (ret != BZ_STREAM_END) goto errhandler;
  1107. /* normal termination */
  1108. *destLen -= strm.avail_out;
  1109. BZ2_bzDecompressEnd ( &strm );
  1110. return BZ_OK;
  1111. output_overflow_or_eof:
  1112. if (strm.avail_out > 0) {
  1113. BZ2_bzDecompressEnd ( &strm );
  1114. return BZ_UNEXPECTED_EOF;
  1115. } else {
  1116. BZ2_bzDecompressEnd ( &strm );
  1117. return BZ_OUTBUFF_FULL;
  1118. };
  1119. errhandler:
  1120. BZ2_bzDecompressEnd ( &strm );
  1121. return ret;
  1122. }
  1123. /*---------------------------------------------------*/
  1124. /*--
  1125. Code contributed by Yoshioka Tsuneo (tsuneo@rr.iij4u.or.jp)
  1126. to support better zlib compatibility.
  1127. This code is not _officially_ part of libbzip2 (yet);
  1128. I haven't tested it, documented it, or considered the
  1129. threading-safeness of it.
  1130. If this code breaks, please contact both Yoshioka and me.
  1131. --*/
  1132. /*---------------------------------------------------*/
  1133. /*---------------------------------------------------*/
  1134. /*--
  1135. return version like "0.9.5d, 4-Sept-1999".
  1136. --*/
  1137. const char * BZ_API(BZ2_bzlibVersion)(void)
  1138. {
  1139. return BZ_VERSION;
  1140. }
  1141. #ifndef BZ_NO_STDIO
  1142. /*---------------------------------------------------*/
  1143. #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
  1144. # include <fcntl.h>
  1145. # include <io.h>
  1146. # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
  1147. #else
  1148. # define SET_BINARY_MODE(file)
  1149. #endif
  1150. static
  1151. BZFILE * bzopen_or_bzdopen
  1152. ( const char *path, /* no use when bzdopen */
  1153. int fd, /* no use when bzdopen */
  1154. const char *mode,
  1155. int open_mode) /* bzopen: 0, bzdopen:1 */
  1156. {
  1157. int bzerr;
  1158. char unused[BZ_MAX_UNUSED];
  1159. int blockSize100k = 9;
  1160. int writing = 0;
  1161. char mode2[10] = "";
  1162. FILE *fp = NULL;
  1163. BZFILE *bzfp = NULL;
  1164. int verbosity = 0;
  1165. int workFactor = 30;
  1166. int smallMode = 0;
  1167. int nUnused = 0;
  1168. if (mode == NULL) return NULL;
  1169. while (*mode) {
  1170. switch (*mode) {
  1171. case 'r':
  1172. writing = 0; break;
  1173. case 'w':
  1174. writing = 1; break;
  1175. case 's':
  1176. smallMode = 1; break;
  1177. default:
  1178. if (isdigit((int)(*mode))) {
  1179. blockSize100k = *mode-BZ_HDR_0;
  1180. }
  1181. }
  1182. mode++;
  1183. }
  1184. strcat(mode2, writing ? "w" : "r" );
  1185. strcat(mode2,"b"); /* binary mode */
  1186. if (open_mode==0) {
  1187. if (path==NULL || strcmp(path,"")==0) {
  1188. fp = (writing ? stdout : stdin);
  1189. SET_BINARY_MODE(fp);
  1190. } else {
  1191. fp = fopen(path,mode2);
  1192. }
  1193. } else {
  1194. #ifdef BZ_STRICT_ANSI
  1195. fp = NULL;
  1196. #else
  1197. fp = fdopen(fd,mode2);
  1198. #endif
  1199. }
  1200. if (fp == NULL) return NULL;
  1201. if (writing) {
  1202. /* Guard against total chaos and anarchy -- JRS */
  1203. if (blockSize100k < 1) blockSize100k = 1;
  1204. if (blockSize100k > 9) blockSize100k = 9;
  1205. bzfp = BZ2_bzWriteOpen(&bzerr,fp,blockSize100k,
  1206. verbosity,workFactor);
  1207. } else {
  1208. bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
  1209. unused,nUnused);
  1210. }
  1211. if (bzfp == NULL) {
  1212. if (fp != stdin && fp != stdout) fclose(fp);
  1213. return NULL;
  1214. }
  1215. return bzfp;
  1216. }
  1217. /*---------------------------------------------------*/
  1218. /*--
  1219. open file for read or write.
  1220. ex) bzopen("file","w9")
  1221. case path="" or NULL => use stdin or stdout.
  1222. --*/
  1223. BZFILE * BZ_API(BZ2_bzopen)
  1224. ( const char *path,
  1225. const char *mode )
  1226. {
  1227. return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
  1228. }
  1229. /*---------------------------------------------------*/
  1230. BZFILE * BZ_API(BZ2_bzdopen)
  1231. ( int fd,
  1232. const char *mode )
  1233. {
  1234. return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
  1235. }
  1236. /*---------------------------------------------------*/
  1237. int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
  1238. {
  1239. int bzerr, nread;
  1240. if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0;
  1241. nread = BZ2_bzRead(&bzerr,b,buf,len);
  1242. if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {
  1243. return nread;
  1244. } else {
  1245. return -1;
  1246. }
  1247. }
  1248. /*---------------------------------------------------*/
  1249. int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
  1250. {
  1251. int bzerr;
  1252. BZ2_bzWrite(&bzerr,b,buf,len);
  1253. if(bzerr == BZ_OK){
  1254. return len;
  1255. }else{
  1256. return -1;
  1257. }
  1258. }
  1259. /*---------------------------------------------------*/
  1260. int BZ_API(BZ2_bzflush) (BZFILE *b)
  1261. {
  1262. /* do nothing now... */
  1263. return 0;
  1264. }
  1265. /*---------------------------------------------------*/
  1266. void BZ_API(BZ2_bzclose) (BZFILE* b)
  1267. {
  1268. int bzerr;
  1269. FILE *fp;
  1270. if (b==NULL) {return;}
  1271. fp = ((bzFile *)b)->handle;
  1272. if(((bzFile*)b)->writing){
  1273. BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
  1274. if(bzerr != BZ_OK){
  1275. BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
  1276. }
  1277. }else{
  1278. BZ2_bzReadClose(&bzerr,b);
  1279. }
  1280. if(fp!=stdin && fp!=stdout){
  1281. fclose(fp);
  1282. }
  1283. }
  1284. /*---------------------------------------------------*/
  1285. /*--
  1286. return last error code
  1287. --*/
  1288. static const char *bzerrorstrings[] = {
  1289. "OK"
  1290. ,"SEQUENCE_ERROR"
  1291. ,"PARAM_ERROR"
  1292. ,"MEM_ERROR"
  1293. ,"DATA_ERROR"
  1294. ,"DATA_ERROR_MAGIC"
  1295. ,"IO_ERROR"
  1296. ,"UNEXPECTED_EOF"
  1297. ,"OUTBUFF_FULL"
  1298. ,"CONFIG_ERROR"
  1299. ,"???" /* for future */
  1300. ,"???" /* for future */
  1301. ,"???" /* for future */
  1302. ,"???" /* for future */
  1303. ,"???" /* for future */
  1304. ,"???" /* for future */
  1305. };
  1306. const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
  1307. {
  1308. int err = ((bzFile *)b)->lastErr;
  1309. if(err>0) err = 0;
  1310. *errnum = err;
  1311. return bzerrorstrings[err*-1];
  1312. }
  1313. #endif
  1314. /*-------------------------------------------------------------*/
  1315. /*--- end bzlib.c ---*/
  1316. /*-------------------------------------------------------------*/