bzlib.c 43 KB

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