bzlib.c 42 KB

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