Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2142 lines
61 KiB

  1. /*-----------------------------------------------------------*/
  2. /*--- A block-sorting, lossless compressor bzip2.c ---*/
  3. /*-----------------------------------------------------------*/
  4. /*--
  5. This file is a part of bzip2 and/or libbzip2, a program and
  6. library for lossless, block-sorting data compression.
  7. Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. The origin of this software must not be misrepresented; you must
  14. not claim that you wrote the original software. If you use this
  15. software in a product, an acknowledgment in the product
  16. documentation would be appreciated but is not required.
  17. 3. Altered source versions must be plainly marked as such, and must
  18. not be misrepresented as being the original software.
  19. 4. The name of the author may not be used to endorse or promote
  20. products derived from this software without specific prior written
  21. permission.
  22. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  23. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  26. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  28. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. Julian Seward, Cambridge, UK.
  34. jseward@acm.org
  35. bzip2/libbzip2 version 1.0 of 21 March 2000
  36. This program is based on (at least) the work of:
  37. Mike Burrows
  38. David Wheeler
  39. Peter Fenwick
  40. Alistair Moffat
  41. Radford Neal
  42. Ian H. Witten
  43. Robert Sedgewick
  44. Jon L. Bentley
  45. For more information on these sources, see the manual.
  46. --*/
  47. /*----------------------------------------------------*/
  48. /*--- IMPORTANT ---*/
  49. /*----------------------------------------------------*/
  50. /*--
  51. WARNING:
  52. This program and library (attempts to) compress data by
  53. performing several non-trivial transformations on it.
  54. Unless you are 100% familiar with *all* the algorithms
  55. contained herein, and with the consequences of modifying them,
  56. you should NOT meddle with the compression or decompression
  57. machinery. Incorrect changes can and very likely *will*
  58. lead to disasterous loss of data.
  59. DISCLAIMER:
  60. I TAKE NO RESPONSIBILITY FOR ANY LOSS OF DATA ARISING FROM THE
  61. USE OF THIS PROGRAM, HOWSOEVER CAUSED.
  62. Every compression of a file implies an assumption that the
  63. compressed file can be decompressed to reproduce the original.
  64. Great efforts in design, coding and testing have been made to
  65. ensure that this program works correctly. However, the
  66. complexity of the algorithms, and, in particular, the presence
  67. of various special cases in the code which occur with very low
  68. but non-zero probability make it impossible to rule out the
  69. possibility of bugs remaining in the program. DO NOT COMPRESS
  70. ANY DATA WITH THIS PROGRAM AND/OR LIBRARY UNLESS YOU ARE PREPARED
  71. TO ACCEPT THE POSSIBILITY, HOWEVER SMALL, THAT THE DATA WILL
  72. NOT BE RECOVERABLE.
  73. That is not to say this program is inherently unreliable.
  74. Indeed, I very much hope the opposite is true. bzip2/libbzip2
  75. has been carefully constructed and extensively tested.
  76. PATENTS:
  77. To the best of my knowledge, bzip2/libbzip2 does not use any
  78. patented algorithms. However, I do not have the resources
  79. available to carry out a full patent search. Therefore I cannot
  80. give any guarantee of the above statement.
  81. --*/
  82. /*----------------------------------------------------*/
  83. /*--- and now for something much more pleasant :-) ---*/
  84. /*----------------------------------------------------*/
  85. /*---------------------------------------------*/
  86. /*--
  87. Place a 1 beside your platform, and 0 elsewhere.
  88. --*/
  89. /*--
  90. Generic 32-bit Unix.
  91. Also works on 64-bit Unix boxes.
  92. This is the default.
  93. --*/
  94. #define BZ_UNIX 1
  95. /*--
  96. Win32, as seen by Jacob Navia's excellent
  97. port of (Chris Fraser & David Hanson)'s excellent
  98. lcc compiler. Or with MS Visual C.
  99. This is selected automatically if compiled by a compiler which
  100. defines _WIN32, not including the Cygwin GCC.
  101. --*/
  102. #define BZ_LCCWIN32 0
  103. #if defined(_WIN32) && !defined(__CYGWIN__)
  104. #undef BZ_LCCWIN32
  105. #define BZ_LCCWIN32 1
  106. #undef BZ_UNIX
  107. #define BZ_UNIX 0
  108. #endif
  109. /*---------------------------------------------*/
  110. /*--
  111. Some stuff for all platforms.
  112. --*/
  113. #include <stdio.h>
  114. #include <stdlib.h>
  115. #include <string.h>
  116. #ifndef _PS3
  117. #include <signal.h>
  118. #endif
  119. #include <math.h>
  120. #include <errno.h>
  121. #include <ctype.h>
  122. #include "bzlib_private.h"
  123. #include "bzlib.h"
  124. #define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
  125. #define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }
  126. #define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }
  127. /*---------------------------------------------*/
  128. /*--
  129. Platform-specific stuff.
  130. --*/
  131. #if BZ_UNIX
  132. # include <fcntl.h>
  133. # include <sys/types.h>
  134. # include <utime.h>
  135. # include <unistd.h>
  136. # include <sys/stat.h>
  137. # ifndef _PS3
  138. # include <sys/times.h>
  139. # endif
  140. # define PATH_SEP '/'
  141. # define MY_LSTAT lstat
  142. # define MY_STAT stat
  143. # define MY_S_ISREG S_ISREG
  144. # define MY_S_ISDIR S_ISDIR
  145. # define APPEND_FILESPEC(root, name) \
  146. root=snocString((root), (name))
  147. # define APPEND_FLAG(root, name) \
  148. root=snocString((root), (name))
  149. # define SET_BINARY_MODE(fd) /**/
  150. # ifdef __GNUC__
  151. # define NORETURN __attribute__ ((noreturn))
  152. # else
  153. # define NORETURN /**/
  154. # endif
  155. # ifdef __DJGPP__
  156. # include <io.h>
  157. # include <fcntl.h>
  158. # undef MY_LSTAT
  159. # undef MY_STAT
  160. # define MY_LSTAT stat
  161. # define MY_STAT stat
  162. # undef SET_BINARY_MODE
  163. # define SET_BINARY_MODE(fd) \
  164. do { \
  165. int retVal = setmode ( fileno ( fd ), \
  166. O_BINARY ); \
  167. ERROR_IF_MINUS_ONE ( retVal ); \
  168. } while ( 0 )
  169. # endif
  170. # ifdef __CYGWIN__
  171. # include <io.h>
  172. # include <fcntl.h>
  173. # undef SET_BINARY_MODE
  174. # define SET_BINARY_MODE(fd) \
  175. do { \
  176. int retVal = setmode ( fileno ( fd ), \
  177. O_BINARY ); \
  178. ERROR_IF_MINUS_ONE ( retVal ); \
  179. } while ( 0 )
  180. # endif
  181. #endif /* BZ_UNIX */
  182. #if BZ_LCCWIN32
  183. # include <io.h>
  184. # include <fcntl.h>
  185. # include <sys\stat.h>
  186. # define NORETURN /**/
  187. # define PATH_SEP '\\'
  188. # define MY_LSTAT _stat
  189. # define MY_STAT _stat
  190. # define MY_S_ISREG(x) ((x) & _S_IFREG)
  191. # define MY_S_ISDIR(x) ((x) & _S_IFDIR)
  192. # define APPEND_FLAG(root, name) \
  193. root=snocString((root), (name))
  194. # define APPEND_FILESPEC(root, name) \
  195. root = snocString ((root), (name))
  196. # define SET_BINARY_MODE(fd) \
  197. do { \
  198. int retVal = setmode ( fileno ( fd ), \
  199. O_BINARY ); \
  200. ERROR_IF_MINUS_ONE ( retVal ); \
  201. } while ( 0 )
  202. #endif /* BZ_LCCWIN32 */
  203. /*---------------------------------------------*/
  204. /*--
  205. Some more stuff for all platforms :-)
  206. --*/
  207. #ifndef BZ_VERSION
  208. typedef char Char;
  209. typedef unsigned char Bool;
  210. typedef unsigned char UChar;
  211. typedef int Int32;
  212. typedef unsigned int UInt32;
  213. typedef short Int16;
  214. typedef unsigned short UInt16;
  215. #endif
  216. #define True ((Bool)1)
  217. #define False ((Bool)0)
  218. /*--
  219. IntNative is your platform's `native' int size.
  220. Only here to avoid probs with 64-bit platforms.
  221. --*/
  222. typedef int IntNative;
  223. /*---------------------------------------------------*/
  224. /*--- Misc (file handling) data decls ---*/
  225. /*---------------------------------------------------*/
  226. Int32 verbosity;
  227. Bool keepInputFiles, smallMode, deleteOutputOnInterrupt;
  228. Bool forceOverwrite, testFailsExist, unzFailsExist, noisy;
  229. Int32 numFileNames, numFilesProcessed, blockSize100k;
  230. Int32 exitValue;
  231. /*-- source modes; F==file, I==stdin, O==stdout --*/
  232. #define SM_I2O 1
  233. #define SM_F2O 2
  234. #define SM_F2F 3
  235. /*-- operation modes --*/
  236. #define OM_Z 1
  237. #define OM_UNZ 2
  238. #define OM_TEST 3
  239. Int32 opMode;
  240. Int32 srcMode;
  241. #define FILE_NAME_LEN 1034
  242. Int32 longestFileName;
  243. Char inName [FILE_NAME_LEN];
  244. Char outName[FILE_NAME_LEN];
  245. Char tmpName[FILE_NAME_LEN];
  246. Char *progName;
  247. Char progNameReally[FILE_NAME_LEN];
  248. FILE *outputHandleJustInCase;
  249. Int32 workFactor;
  250. static void panic ( Char* ) NORETURN;
  251. static void ioError ( void ) NORETURN;
  252. static void outOfMemory ( void ) NORETURN;
  253. static void configError ( void ) NORETURN;
  254. static void crcError ( void ) NORETURN;
  255. static void cleanUpAndFail ( Int32 ) NORETURN;
  256. static void compressedStreamEOF ( void ) NORETURN;
  257. static void copyFileName ( Char*, Char* );
  258. static void* myMalloc ( Int32 );
  259. /*---------------------------------------------------*/
  260. /*--- An implementation of 64-bit ints. Sigh. ---*/
  261. /*--- Roll on widespread deployment of ANSI C9X ! ---*/
  262. /*---------------------------------------------------*/
  263. typedef
  264. struct { UChar b[8]; }
  265. UInt64;
  266. static
  267. void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 )
  268. {
  269. n->b[7] = (UChar)((hi32 >> 24) & 0xFF);
  270. n->b[6] = (UChar)((hi32 >> 16) & 0xFF);
  271. n->b[5] = (UChar)((hi32 >> 8) & 0xFF);
  272. n->b[4] = (UChar) (hi32 & 0xFF);
  273. n->b[3] = (UChar)((lo32 >> 24) & 0xFF);
  274. n->b[2] = (UChar)((lo32 >> 16) & 0xFF);
  275. n->b[1] = (UChar)((lo32 >> 8) & 0xFF);
  276. n->b[0] = (UChar) (lo32 & 0xFF);
  277. }
  278. static
  279. double uInt64_to_double ( UInt64* n )
  280. {
  281. Int32 i;
  282. double base = 1.0;
  283. double sum = 0.0;
  284. for (i = 0; i < 8; i++) {
  285. sum += base * (double)(n->b[i]);
  286. base *= 256.0;
  287. }
  288. return sum;
  289. }
  290. static
  291. Bool uInt64_isZero ( UInt64* n )
  292. {
  293. Int32 i;
  294. for (i = 0; i < 8; i++)
  295. if (n->b[i] != 0) return 0;
  296. return 1;
  297. }
  298. /* Divide *n by 10, and return the remainder. */
  299. static
  300. Int32 uInt64_qrm10 ( UInt64* n )
  301. {
  302. UInt32 rem, tmp;
  303. Int32 i;
  304. rem = 0;
  305. for (i = 7; i >= 0; i--) {
  306. tmp = rem * 256 + n->b[i];
  307. n->b[i] = tmp / 10;
  308. rem = tmp % 10;
  309. }
  310. return rem;
  311. }
  312. /* ... and the Whole Entire Point of all this UInt64 stuff is
  313. so that we can supply the following function.
  314. */
  315. static
  316. void uInt64_toAscii ( char* outbuf, UInt64* n )
  317. {
  318. Int32 i, q;
  319. UChar buf[32];
  320. Int32 nBuf = 0;
  321. UInt64 n_copy = *n;
  322. do {
  323. q = uInt64_qrm10 ( &n_copy );
  324. buf[nBuf] = q + '0';
  325. nBuf++;
  326. } while (!uInt64_isZero(&n_copy));
  327. outbuf[nBuf] = 0;
  328. for (i = 0; i < nBuf; i++)
  329. outbuf[i] = buf[nBuf-i-1];
  330. }
  331. /*---------------------------------------------------*/
  332. /*--- Processing of complete files and streams ---*/
  333. /*---------------------------------------------------*/
  334. /*---------------------------------------------*/
  335. static
  336. Bool myfeof ( FILE* f )
  337. {
  338. Int32 c = fgetc ( f );
  339. if (c == EOF) return True;
  340. ungetc ( c, f );
  341. return False;
  342. }
  343. /*---------------------------------------------*/
  344. static
  345. void compressStream ( FILE *stream, FILE *zStream )
  346. {
  347. BZFILE* bzf = NULL;
  348. UChar ibuf[5000];
  349. Int32 nIbuf;
  350. UInt32 nbytes_in_lo32, nbytes_in_hi32;
  351. UInt32 nbytes_out_lo32, nbytes_out_hi32;
  352. Int32 bzerr, bzerr_dummy, ret;
  353. SET_BINARY_MODE(stream);
  354. SET_BINARY_MODE(zStream);
  355. if (ferror(stream)) goto errhandler_io;
  356. if (ferror(zStream)) goto errhandler_io;
  357. bzf = BZ2_bzWriteOpen ( &bzerr, zStream,
  358. blockSize100k, verbosity, workFactor );
  359. if (bzerr != BZ_OK) goto errhandler;
  360. if (verbosity >= 2) fprintf ( stderr, "\n" );
  361. while (True) {
  362. if (myfeof(stream)) break;
  363. nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream );
  364. if (ferror(stream)) goto errhandler_io;
  365. if (nIbuf > 0) BZ2_bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf );
  366. if (bzerr != BZ_OK) goto errhandler;
  367. }
  368. BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
  369. &nbytes_in_lo32, &nbytes_in_hi32,
  370. &nbytes_out_lo32, &nbytes_out_hi32 );
  371. if (bzerr != BZ_OK) goto errhandler;
  372. if (ferror(zStream)) goto errhandler_io;
  373. ret = fflush ( zStream );
  374. if (ret == EOF) goto errhandler_io;
  375. if (zStream != stdout) {
  376. ret = fclose ( zStream );
  377. outputHandleJustInCase = NULL;
  378. if (ret == EOF) goto errhandler_io;
  379. }
  380. outputHandleJustInCase = NULL;
  381. if (ferror(stream)) goto errhandler_io;
  382. ret = fclose ( stream );
  383. if (ret == EOF) goto errhandler_io;
  384. if (verbosity >= 1) {
  385. if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) {
  386. fprintf ( stderr, " no data compressed.\n");
  387. } else {
  388. Char buf_nin[32], buf_nout[32];
  389. UInt64 nbytes_in, nbytes_out;
  390. double nbytes_in_d, nbytes_out_d;
  391. uInt64_from_UInt32s ( &nbytes_in,
  392. nbytes_in_lo32, nbytes_in_hi32 );
  393. uInt64_from_UInt32s ( &nbytes_out,
  394. nbytes_out_lo32, nbytes_out_hi32 );
  395. nbytes_in_d = uInt64_to_double ( &nbytes_in );
  396. nbytes_out_d = uInt64_to_double ( &nbytes_out );
  397. uInt64_toAscii ( buf_nin, &nbytes_in );
  398. uInt64_toAscii ( buf_nout, &nbytes_out );
  399. fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, "
  400. "%5.2f%% saved, %s in, %s out.\n",
  401. nbytes_in_d / nbytes_out_d,
  402. (8.0 * nbytes_out_d) / nbytes_in_d,
  403. 100.0 * (1.0 - nbytes_out_d / nbytes_in_d),
  404. buf_nin,
  405. buf_nout
  406. );
  407. }
  408. }
  409. return;
  410. errhandler:
  411. BZ2_bzWriteClose64 ( &bzerr_dummy, bzf, 1,
  412. &nbytes_in_lo32, &nbytes_in_hi32,
  413. &nbytes_out_lo32, &nbytes_out_hi32 );
  414. switch (bzerr) {
  415. case BZ_CONFIG_ERROR:
  416. configError(); break;
  417. case BZ_MEM_ERROR:
  418. outOfMemory (); break;
  419. case BZ_IO_ERROR:
  420. errhandler_io:
  421. ioError(); break;
  422. default:
  423. panic ( "compress:unexpected error" );
  424. }
  425. panic ( "compress:end" );
  426. /*notreached*/
  427. }
  428. /*---------------------------------------------*/
  429. static
  430. Bool uncompressStream ( FILE *zStream, FILE *stream )
  431. {
  432. BZFILE* bzf = NULL;
  433. Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
  434. UChar obuf[5000];
  435. UChar unused[BZ_MAX_UNUSED];
  436. Int32 nUnused;
  437. UChar* unusedTmp;
  438. nUnused = 0;
  439. streamNo = 0;
  440. SET_BINARY_MODE(stream);
  441. SET_BINARY_MODE(zStream);
  442. if (ferror(stream)) goto errhandler_io;
  443. if (ferror(zStream)) goto errhandler_io;
  444. while (True) {
  445. bzf = BZ2_bzReadOpen (
  446. &bzerr, zStream, verbosity,
  447. (int)smallMode, unused, nUnused
  448. );
  449. if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
  450. streamNo++;
  451. while (bzerr == BZ_OK) {
  452. nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
  453. if (bzerr == BZ_DATA_ERROR_MAGIC) goto trycat;
  454. if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0)
  455. fwrite ( obuf, sizeof(UChar), nread, stream );
  456. if (ferror(stream)) goto errhandler_io;
  457. }
  458. if (bzerr != BZ_STREAM_END) goto errhandler;
  459. BZ2_bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused );
  460. if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
  461. for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
  462. BZ2_bzReadClose ( &bzerr, bzf );
  463. if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
  464. if (nUnused == 0 && myfeof(zStream)) break;
  465. }
  466. closeok:
  467. if (ferror(zStream)) goto errhandler_io;
  468. ret = fclose ( zStream );
  469. if (ret == EOF) goto errhandler_io;
  470. if (ferror(stream)) goto errhandler_io;
  471. ret = fflush ( stream );
  472. if (ret != 0) goto errhandler_io;
  473. if (stream != stdout) {
  474. ret = fclose ( stream );
  475. outputHandleJustInCase = NULL;
  476. if (ret == EOF) goto errhandler_io;
  477. }
  478. outputHandleJustInCase = NULL;
  479. if (verbosity >= 2) fprintf ( stderr, "\n " );
  480. return True;
  481. trycat:
  482. if (forceOverwrite) {
  483. rewind(zStream);
  484. while (True) {
  485. if (myfeof(zStream)) break;
  486. nread = fread ( obuf, sizeof(UChar), 5000, zStream );
  487. if (ferror(zStream)) goto errhandler_io;
  488. if (nread > 0) fwrite ( obuf, sizeof(UChar), nread, stream );
  489. if (ferror(stream)) goto errhandler_io;
  490. }
  491. goto closeok;
  492. }
  493. errhandler:
  494. BZ2_bzReadClose ( &bzerr_dummy, bzf );
  495. switch (bzerr) {
  496. case BZ_CONFIG_ERROR:
  497. configError(); break;
  498. case BZ_IO_ERROR:
  499. errhandler_io:
  500. ioError(); break;
  501. case BZ_DATA_ERROR:
  502. crcError();
  503. case BZ_MEM_ERROR:
  504. outOfMemory();
  505. case BZ_UNEXPECTED_EOF:
  506. compressedStreamEOF();
  507. case BZ_DATA_ERROR_MAGIC:
  508. if (zStream != stdin) fclose(zStream);
  509. if (stream != stdout) fclose(stream);
  510. if (streamNo == 1) {
  511. return False;
  512. } else {
  513. if (noisy)
  514. fprintf ( stderr,
  515. "\n%s: %s: trailing garbage after EOF ignored\n",
  516. progName, inName );
  517. return True;
  518. }
  519. default:
  520. panic ( "decompress:unexpected error" );
  521. }
  522. panic ( "decompress:end" );
  523. return True; /*notreached*/
  524. }
  525. /*---------------------------------------------*/
  526. static
  527. Bool testStream ( FILE *zStream )
  528. {
  529. BZFILE* bzf = NULL;
  530. Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
  531. UChar obuf[5000];
  532. UChar unused[BZ_MAX_UNUSED];
  533. Int32 nUnused;
  534. UChar* unusedTmp;
  535. nUnused = 0;
  536. streamNo = 0;
  537. SET_BINARY_MODE(zStream);
  538. if (ferror(zStream)) goto errhandler_io;
  539. while (True) {
  540. bzf = BZ2_bzReadOpen (
  541. &bzerr, zStream, verbosity,
  542. (int)smallMode, unused, nUnused
  543. );
  544. if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
  545. streamNo++;
  546. while (bzerr == BZ_OK) {
  547. nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
  548. if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler;
  549. }
  550. if (bzerr != BZ_STREAM_END) goto errhandler;
  551. BZ2_bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused );
  552. if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
  553. for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
  554. BZ2_bzReadClose ( &bzerr, bzf );
  555. if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
  556. if (nUnused == 0 && myfeof(zStream)) break;
  557. }
  558. if (ferror(zStream)) goto errhandler_io;
  559. ret = fclose ( zStream );
  560. if (ret == EOF) goto errhandler_io;
  561. if (verbosity >= 2) fprintf ( stderr, "\n " );
  562. return True;
  563. errhandler:
  564. BZ2_bzReadClose ( &bzerr_dummy, bzf );
  565. if (verbosity == 0)
  566. fprintf ( stderr, "%s: %s: ", progName, inName );
  567. switch (bzerr) {
  568. case BZ_CONFIG_ERROR:
  569. configError(); break;
  570. case BZ_IO_ERROR:
  571. errhandler_io:
  572. ioError(); break;
  573. case BZ_DATA_ERROR:
  574. fprintf ( stderr,
  575. "data integrity (CRC) error in data\n" );
  576. return False;
  577. case BZ_MEM_ERROR:
  578. outOfMemory();
  579. case BZ_UNEXPECTED_EOF:
  580. fprintf ( stderr,
  581. "file ends unexpectedly\n" );
  582. return False;
  583. case BZ_DATA_ERROR_MAGIC:
  584. if (zStream != stdin) fclose(zStream);
  585. if (streamNo == 1) {
  586. fprintf ( stderr,
  587. "bad magic number (file not created by bzip2)\n" );
  588. return False;
  589. } else {
  590. if (noisy)
  591. fprintf ( stderr,
  592. "trailing garbage after EOF ignored\n" );
  593. return True;
  594. }
  595. default:
  596. panic ( "test:unexpected error" );
  597. }
  598. panic ( "test:end" );
  599. return True; /*notreached*/
  600. }
  601. /*---------------------------------------------------*/
  602. /*--- Error [non-] handling grunge ---*/
  603. /*---------------------------------------------------*/
  604. /*---------------------------------------------*/
  605. static
  606. void setExit ( Int32 v )
  607. {
  608. if (v > exitValue) exitValue = v;
  609. }
  610. /*---------------------------------------------*/
  611. static
  612. void cadvise ( void )
  613. {
  614. if (noisy)
  615. fprintf (
  616. stderr,
  617. "\nIt is possible that the compressed file(s) have become corrupted.\n"
  618. "You can use the -tvv option to test integrity of such files.\n\n"
  619. "You can use the `bzip2recover' program to attempt to recover\n"
  620. "data from undamaged sections of corrupted files.\n\n"
  621. );
  622. }
  623. /*---------------------------------------------*/
  624. static
  625. void showFileNames ( void )
  626. {
  627. if (noisy)
  628. fprintf (
  629. stderr,
  630. "\tInput file = %s, output file = %s\n",
  631. inName, outName
  632. );
  633. }
  634. /*---------------------------------------------*/
  635. static
  636. void cleanUpAndFail ( Int32 ec )
  637. {
  638. IntNative retVal;
  639. struct MY_STAT statBuf;
  640. if ( srcMode == SM_F2F
  641. && opMode != OM_TEST
  642. && deleteOutputOnInterrupt ) {
  643. /* Check whether input file still exists. Delete output file
  644. only if input exists to avoid loss of data. Joerg Prante, 5
  645. January 2002. (JRS 06-Jan-2002: other changes in 1.0.2 mean
  646. this is less likely to happen. But to be ultra-paranoid, we
  647. do the check anyway.) */
  648. retVal = MY_STAT ( inName, &statBuf );
  649. if (retVal == 0) {
  650. if (noisy)
  651. fprintf ( stderr,
  652. "%s: Deleting output file %s, if it exists.\n",
  653. progName, outName );
  654. if (outputHandleJustInCase != NULL)
  655. fclose ( outputHandleJustInCase );
  656. retVal = remove ( outName );
  657. if (retVal != 0)
  658. fprintf ( stderr,
  659. "%s: WARNING: deletion of output file "
  660. "(apparently) failed.\n",
  661. progName );
  662. } else {
  663. fprintf ( stderr,
  664. "%s: WARNING: deletion of output file suppressed\n",
  665. progName );
  666. fprintf ( stderr,
  667. "%s: since input file no longer exists. Output file\n",
  668. progName );
  669. fprintf ( stderr,
  670. "%s: `%s' may be incomplete.\n",
  671. progName, outName );
  672. fprintf ( stderr,
  673. "%s: I suggest doing an integrity test (bzip2 -tv)"
  674. " of it.\n",
  675. progName );
  676. }
  677. }
  678. if (noisy && numFileNames > 0 && numFilesProcessed < numFileNames) {
  679. fprintf ( stderr,
  680. "%s: WARNING: some files have not been processed:\n"
  681. "%s: %d specified on command line, %d not processed yet.\n\n",
  682. progName, progName,
  683. numFileNames, numFileNames - numFilesProcessed );
  684. }
  685. setExit(ec);
  686. exit(exitValue);
  687. }
  688. /*---------------------------------------------*/
  689. static
  690. void panic ( Char* s )
  691. {
  692. fprintf ( stderr,
  693. "\n%s: PANIC -- internal consistency error:\n"
  694. "\t%s\n"
  695. "\tThis is a BUG. Please report it to me at:\n"
  696. "\t[email protected]\n",
  697. progName, s );
  698. showFileNames();
  699. cleanUpAndFail( 3 );
  700. }
  701. /*---------------------------------------------*/
  702. static
  703. void crcError ( void )
  704. {
  705. fprintf ( stderr,
  706. "\n%s: Data integrity error when decompressing.\n",
  707. progName );
  708. showFileNames();
  709. cadvise();
  710. cleanUpAndFail( 2 );
  711. }
  712. /*---------------------------------------------*/
  713. static
  714. void compressedStreamEOF ( void )
  715. {
  716. if (noisy) {
  717. fprintf ( stderr,
  718. "\n%s: Compressed file ends unexpectedly;\n\t"
  719. "perhaps it is corrupted? *Possible* reason follows.\n",
  720. progName );
  721. perror ( progName );
  722. showFileNames();
  723. cadvise();
  724. }
  725. cleanUpAndFail( 2 );
  726. }
  727. /*---------------------------------------------*/
  728. static
  729. void ioError ( void )
  730. {
  731. fprintf ( stderr,
  732. "\n%s: I/O or other error, bailing out. "
  733. "Possible reason follows.\n",
  734. progName );
  735. perror ( progName );
  736. showFileNames();
  737. cleanUpAndFail( 1 );
  738. }
  739. /*---------------------------------------------*/
  740. static
  741. void mySignalCatcher ( IntNative n )
  742. {
  743. fprintf ( stderr,
  744. "\n%s: Control-C or similar caught, quitting.\n",
  745. progName );
  746. cleanUpAndFail(1);
  747. }
  748. /*---------------------------------------------*/
  749. static
  750. void mySIGSEGVorSIGBUScatcher ( IntNative n )
  751. {
  752. if (opMode == OM_Z)
  753. fprintf (
  754. stderr,
  755. "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n"
  756. "\n"
  757. " Possible causes are (most likely first):\n"
  758. " (1) This computer has unreliable memory or cache hardware\n"
  759. " (a surprisingly common problem; try a different machine.)\n"
  760. " (2) A bug in the compiler used to create this executable\n"
  761. " (unlikely, if you didn't compile bzip2 yourself.)\n"
  762. " (3) A real bug in bzip2 -- I hope this should never be the case.\n"
  763. " The user's manual, Section 4.3, has more info on (1) and (2).\n"
  764. " \n"
  765. " If you suspect this is a bug in bzip2, or are unsure about (1)\n"
  766. " or (2), feel free to report it to me at: [email protected].\n"
  767. " Section 4.3 of the user's manual describes the info a useful\n"
  768. " bug report should have. If the manual is available on your\n"
  769. " system, please try and read it before mailing me. If you don't\n"
  770. " have the manual or can't be bothered to read it, mail me anyway.\n"
  771. "\n",
  772. progName );
  773. else
  774. fprintf (
  775. stderr,
  776. "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n"
  777. "\n"
  778. " Possible causes are (most likely first):\n"
  779. " (1) The compressed data is corrupted, and bzip2's usual checks\n"
  780. " failed to detect this. Try bzip2 -tvv my_file.bz2.\n"
  781. " (2) This computer has unreliable memory or cache hardware\n"
  782. " (a surprisingly common problem; try a different machine.)\n"
  783. " (3) A bug in the compiler used to create this executable\n"
  784. " (unlikely, if you didn't compile bzip2 yourself.)\n"
  785. " (4) A real bug in bzip2 -- I hope this should never be the case.\n"
  786. " The user's manual, Section 4.3, has more info on (2) and (3).\n"
  787. " \n"
  788. " If you suspect this is a bug in bzip2, or are unsure about (2)\n"
  789. " or (3), feel free to report it to me at: [email protected].\n"
  790. " Section 4.3 of the user's manual describes the info a useful\n"
  791. " bug report should have. If the manual is available on your\n"
  792. " system, please try and read it before mailing me. If you don't\n"
  793. " have the manual or can't be bothered to read it, mail me anyway.\n"
  794. "\n",
  795. progName );
  796. showFileNames();
  797. if (opMode == OM_Z)
  798. cleanUpAndFail( 3 ); else
  799. { cadvise(); cleanUpAndFail( 2 ); }
  800. }
  801. /*---------------------------------------------*/
  802. static
  803. void outOfMemory ( void )
  804. {
  805. fprintf ( stderr,
  806. "\n%s: couldn't allocate enough memory\n",
  807. progName );
  808. showFileNames();
  809. cleanUpAndFail(1);
  810. }
  811. /*---------------------------------------------*/
  812. static
  813. void configError ( void )
  814. {
  815. fprintf ( stderr,
  816. "bzip2: I'm not configured correctly for this platform!\n"
  817. "\tI require Int32, Int16 and Char to have sizes\n"
  818. "\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
  819. "\tProbably you can fix this by defining them correctly,\n"
  820. "\tand recompiling. Bye!\n" );
  821. setExit(3);
  822. exit(exitValue);
  823. }
  824. /*---------------------------------------------------*/
  825. /*--- The main driver machinery ---*/
  826. /*---------------------------------------------------*/
  827. /* All rather crufty. The main problem is that input files
  828. are stat()d multiple times before use. This should be
  829. cleaned up.
  830. */
  831. /*---------------------------------------------*/
  832. static
  833. void pad ( Char *s )
  834. {
  835. Int32 i;
  836. if ( (Int32)strlen(s) >= longestFileName ) return;
  837. for (i = 1; i <= longestFileName - (Int32)strlen(s); i++)
  838. fprintf ( stderr, " " );
  839. }
  840. /*---------------------------------------------*/
  841. static
  842. void copyFileName ( Char* to, Char* from )
  843. {
  844. if ( strlen(from) > FILE_NAME_LEN-10 ) {
  845. fprintf (
  846. stderr,
  847. "bzip2: file name\n`%s'\n"
  848. "is suspiciously (more than %d chars) long.\n"
  849. "Try using a reasonable file name instead. Sorry! :-)\n",
  850. from, FILE_NAME_LEN-10
  851. );
  852. setExit(1);
  853. exit(exitValue);
  854. }
  855. strncpy(to,from,FILE_NAME_LEN-10);
  856. to[FILE_NAME_LEN-10]='\0';
  857. }
  858. /*---------------------------------------------*/
  859. static
  860. Bool fileExists ( Char* name )
  861. {
  862. FILE *tmp = fopen ( name, "rb" );
  863. Bool exists = (tmp != NULL);
  864. if (tmp != NULL) fclose ( tmp );
  865. return exists;
  866. }
  867. /*---------------------------------------------*/
  868. /* Open an output file safely with O_EXCL and good permissions.
  869. This avoids a race condition in versions < 1.0.2, in which
  870. the file was first opened and then had its interim permissions
  871. set safely. We instead use open() to create the file with
  872. the interim permissions required. (--- --- rw-).
  873. For non-Unix platforms, if we are not worrying about
  874. security issues, simple this simply behaves like fopen.
  875. */
  876. FILE* fopen_output_safely ( Char* name, const char* mode )
  877. {
  878. # if BZ_UNIX
  879. FILE* fp;
  880. IntNative fh;
  881. fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR);
  882. if (fh == -1) return NULL;
  883. fp = fdopen(fh, mode);
  884. if (fp == NULL) close(fh);
  885. return fp;
  886. # else
  887. return fopen(name, mode);
  888. # endif
  889. }
  890. /*---------------------------------------------*/
  891. /*--
  892. if in doubt, return True
  893. --*/
  894. static
  895. Bool notAStandardFile ( Char* name )
  896. {
  897. #ifdef _PS3
  898. return True;
  899. #else
  900. IntNative i;
  901. struct MY_STAT statBuf;
  902. i = MY_LSTAT ( name, &statBuf );
  903. if (i != 0) return True;
  904. if (MY_S_ISREG(statBuf.st_mode)) return False;
  905. return True;
  906. #endif
  907. }
  908. /*---------------------------------------------*/
  909. /*--
  910. rac 11/21/98 see if file has hard links to it
  911. --*/
  912. static
  913. Int32 countHardLinks ( Char* name )
  914. {
  915. #ifdef _PS3
  916. return 0;
  917. #else
  918. IntNative i;
  919. struct MY_STAT statBuf;
  920. i = MY_LSTAT ( name, &statBuf );
  921. if (i != 0) return 0;
  922. return (statBuf.st_nlink - 1);
  923. #endif
  924. }
  925. /*---------------------------------------------*/
  926. /* Copy modification date, access date, permissions and owner from the
  927. source to destination file. We have to copy this meta-info off
  928. into fileMetaInfo before starting to compress / decompress it,
  929. because doing it afterwards means we get the wrong access time.
  930. To complicate matters, in compress() and decompress() below, the
  931. sequence of tests preceding the call to saveInputFileMetaInfo()
  932. involves calling fileExists(), which in turn establishes its result
  933. by attempting to fopen() the file, and if successful, immediately
  934. fclose()ing it again. So we have to assume that the fopen() call
  935. does not cause the access time field to be updated.
  936. Reading of the man page for stat() (man 2 stat) on RedHat 7.2 seems
  937. to imply that merely doing open() will not affect the access time.
  938. Therefore we merely need to hope that the C library only does
  939. open() as a result of fopen(), and not any kind of read()-ahead
  940. cleverness.
  941. It sounds pretty fragile to me. Whether this carries across
  942. robustly to arbitrary Unix-like platforms (or even works robustly
  943. on this one, RedHat 7.2) is unknown to me. Nevertheless ...
  944. */
  945. #if BZ_UNIX
  946. static
  947. struct MY_STAT fileMetaInfo;
  948. #endif
  949. static
  950. void saveInputFileMetaInfo ( Char *srcName )
  951. {
  952. # if BZ_UNIX
  953. IntNative retVal;
  954. /* Note use of stat here, not lstat. */
  955. retVal = MY_STAT( srcName, &fileMetaInfo );
  956. ERROR_IF_NOT_ZERO ( retVal );
  957. # endif
  958. }
  959. static
  960. void applySavedMetaInfoToOutputFile ( Char *dstName )
  961. {
  962. # if BZ_UNIX
  963. IntNative retVal;
  964. struct utimbuf uTimBuf;
  965. uTimBuf.actime = fileMetaInfo.st_atime;
  966. uTimBuf.modtime = fileMetaInfo.st_mtime;
  967. retVal = chmod ( dstName, fileMetaInfo.st_mode );
  968. ERROR_IF_NOT_ZERO ( retVal );
  969. retVal = utime ( dstName, &uTimBuf );
  970. ERROR_IF_NOT_ZERO ( retVal );
  971. #ifndef _PS3
  972. retVal = chown ( dstName, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
  973. #endif
  974. /* chown() will in many cases return with EPERM, which can
  975. be safely ignored.
  976. */
  977. # endif
  978. }
  979. /*---------------------------------------------*/
  980. static
  981. Bool containsDubiousChars ( Char* name )
  982. {
  983. # if BZ_UNIX
  984. /* On unix, files can contain any characters and the file expansion
  985. * is performed by the shell.
  986. */
  987. return False;
  988. # else /* ! BZ_UNIX */
  989. /* On non-unix (Win* platforms), wildcard characters are not allowed in
  990. * filenames.
  991. */
  992. for (; *name != '\0'; name++)
  993. if (*name == '?' || *name == '*') return True;
  994. return False;
  995. # endif /* BZ_UNIX */
  996. }
  997. /*---------------------------------------------*/
  998. #define BZ_N_SUFFIX_PAIRS 4
  999. Char* zSuffix[BZ_N_SUFFIX_PAIRS]
  1000. = { ".bz2", ".bz", ".tbz2", ".tbz" };
  1001. Char* unzSuffix[BZ_N_SUFFIX_PAIRS]
  1002. = { "", "", ".tar", ".tar" };
  1003. static
  1004. Bool hasSuffix ( Char* s, Char* suffix )
  1005. {
  1006. Int32 ns = strlen(s);
  1007. Int32 nx = strlen(suffix);
  1008. if (ns < nx) return False;
  1009. if (strcmp(s + ns - nx, suffix) == 0) return True;
  1010. return False;
  1011. }
  1012. static
  1013. Bool mapSuffix ( Char* name,
  1014. Char* oldSuffix, Char* newSuffix )
  1015. {
  1016. if (!hasSuffix(name,oldSuffix)) return False;
  1017. name[strlen(name)-strlen(oldSuffix)] = 0;
  1018. strcat ( name, newSuffix );
  1019. return True;
  1020. }
  1021. /*---------------------------------------------*/
  1022. static
  1023. void compress ( Char *name )
  1024. {
  1025. FILE *inStr = NULL;
  1026. FILE *outStr = NULL;
  1027. Int32 n, i;
  1028. struct MY_STAT statBuf;
  1029. deleteOutputOnInterrupt = False;
  1030. if (name == NULL && srcMode != SM_I2O)
  1031. panic ( "compress: bad modes\n" );
  1032. switch (srcMode) {
  1033. case SM_I2O:
  1034. copyFileName ( inName, "(stdin)" );
  1035. copyFileName ( outName, "(stdout)" );
  1036. break;
  1037. case SM_F2F:
  1038. copyFileName ( inName, name );
  1039. copyFileName ( outName, name );
  1040. strcat ( outName, ".bz2" );
  1041. break;
  1042. case SM_F2O:
  1043. copyFileName ( inName, name );
  1044. copyFileName ( outName, "(stdout)" );
  1045. break;
  1046. }
  1047. if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
  1048. if (noisy)
  1049. fprintf ( stderr, "%s: There are no files matching `%s'.\n",
  1050. progName, inName );
  1051. setExit(1);
  1052. return;
  1053. }
  1054. if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
  1055. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  1056. progName, inName, strerror(errno) );
  1057. setExit(1);
  1058. return;
  1059. }
  1060. for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) {
  1061. if (hasSuffix(inName, zSuffix[i])) {
  1062. if (noisy)
  1063. fprintf ( stderr,
  1064. "%s: Input file %s already has %s suffix.\n",
  1065. progName, inName, zSuffix[i] );
  1066. setExit(1);
  1067. return;
  1068. }
  1069. }
  1070. if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
  1071. MY_STAT(inName, &statBuf);
  1072. #ifndef _PS3
  1073. if ( MY_S_ISDIR(statBuf.st_mode) ) {
  1074. fprintf( stderr,
  1075. "%s: Input file %s is a directory.\n",
  1076. progName,inName);
  1077. setExit(1);
  1078. return;
  1079. }
  1080. #endif
  1081. }
  1082. if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
  1083. if (noisy)
  1084. fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
  1085. progName, inName );
  1086. setExit(1);
  1087. return;
  1088. }
  1089. if ( srcMode == SM_F2F && fileExists ( outName ) ) {
  1090. if (forceOverwrite) {
  1091. remove(outName);
  1092. } else {
  1093. fprintf ( stderr, "%s: Output file %s already exists.\n",
  1094. progName, outName );
  1095. setExit(1);
  1096. return;
  1097. }
  1098. }
  1099. if ( srcMode == SM_F2F && !forceOverwrite &&
  1100. (n=countHardLinks ( inName )) > 0) {
  1101. fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
  1102. progName, inName, n, n > 1 ? "s" : "" );
  1103. setExit(1);
  1104. return;
  1105. }
  1106. if ( srcMode == SM_F2F ) {
  1107. /* Save the file's meta-info before we open it. Doing it later
  1108. means we mess up the access times. */
  1109. saveInputFileMetaInfo ( inName );
  1110. }
  1111. switch ( srcMode ) {
  1112. case SM_I2O:
  1113. inStr = stdin;
  1114. outStr = stdout;
  1115. #ifndef _PS3
  1116. if ( isatty ( fileno ( stdout ) ) ) {
  1117. fprintf ( stderr,
  1118. "%s: I won't write compressed data to a terminal.\n",
  1119. progName );
  1120. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  1121. progName, progName );
  1122. setExit(1);
  1123. return;
  1124. };
  1125. #endif
  1126. break;
  1127. case SM_F2O:
  1128. inStr = fopen ( inName, "rb" );
  1129. outStr = stdout;
  1130. #ifndef _PS3
  1131. if ( isatty ( fileno ( stdout ) ) ) {
  1132. fprintf ( stderr,
  1133. "%s: I won't write compressed data to a terminal.\n",
  1134. progName );
  1135. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  1136. progName, progName );
  1137. if ( inStr != NULL ) fclose ( inStr );
  1138. setExit(1);
  1139. return;
  1140. };
  1141. #endif
  1142. if ( inStr == NULL ) {
  1143. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  1144. progName, inName, strerror(errno) );
  1145. setExit(1);
  1146. return;
  1147. };
  1148. break;
  1149. case SM_F2F:
  1150. inStr = fopen ( inName, "rb" );
  1151. outStr = fopen_output_safely ( outName, "wb" );
  1152. if ( outStr == NULL) {
  1153. fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
  1154. progName, outName, strerror(errno) );
  1155. if ( inStr != NULL ) fclose ( inStr );
  1156. setExit(1);
  1157. return;
  1158. }
  1159. if ( inStr == NULL ) {
  1160. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  1161. progName, inName, strerror(errno) );
  1162. if ( outStr != NULL ) fclose ( outStr );
  1163. setExit(1);
  1164. return;
  1165. };
  1166. break;
  1167. default:
  1168. panic ( "compress: bad srcMode" );
  1169. break;
  1170. }
  1171. if (verbosity >= 1) {
  1172. fprintf ( stderr, " %s: ", inName );
  1173. pad ( inName );
  1174. fflush ( stderr );
  1175. }
  1176. /*--- Now the input and output handles are sane. Do the Biz. ---*/
  1177. outputHandleJustInCase = outStr;
  1178. deleteOutputOnInterrupt = True;
  1179. compressStream ( inStr, outStr );
  1180. outputHandleJustInCase = NULL;
  1181. /*--- If there was an I/O error, we won't get here. ---*/
  1182. if ( srcMode == SM_F2F ) {
  1183. applySavedMetaInfoToOutputFile ( outName );
  1184. deleteOutputOnInterrupt = False;
  1185. if ( !keepInputFiles ) {
  1186. IntNative retVal = remove ( inName );
  1187. ERROR_IF_NOT_ZERO ( retVal );
  1188. }
  1189. }
  1190. deleteOutputOnInterrupt = False;
  1191. }
  1192. /*---------------------------------------------*/
  1193. static
  1194. void uncompress ( Char *name )
  1195. {
  1196. FILE *inStr = NULL;
  1197. FILE *outStr = NULL;
  1198. Int32 n, i;
  1199. Bool magicNumberOK;
  1200. Bool cantGuess;
  1201. struct MY_STAT statBuf;
  1202. deleteOutputOnInterrupt = False;
  1203. if (name == NULL && srcMode != SM_I2O)
  1204. panic ( "uncompress: bad modes\n" );
  1205. cantGuess = False;
  1206. switch (srcMode) {
  1207. case SM_I2O:
  1208. copyFileName ( inName, "(stdin)" );
  1209. copyFileName ( outName, "(stdout)" );
  1210. break;
  1211. case SM_F2F:
  1212. copyFileName ( inName, name );
  1213. copyFileName ( outName, name );
  1214. for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++)
  1215. if (mapSuffix(outName,zSuffix[i],unzSuffix[i]))
  1216. goto zzz;
  1217. cantGuess = True;
  1218. strcat ( outName, ".out" );
  1219. break;
  1220. case SM_F2O:
  1221. copyFileName ( inName, name );
  1222. copyFileName ( outName, "(stdout)" );
  1223. break;
  1224. }
  1225. zzz:
  1226. if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
  1227. if (noisy)
  1228. fprintf ( stderr, "%s: There are no files matching `%s'.\n",
  1229. progName, inName );
  1230. setExit(1);
  1231. return;
  1232. }
  1233. if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
  1234. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  1235. progName, inName, strerror(errno) );
  1236. setExit(1);
  1237. return;
  1238. }
  1239. if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
  1240. MY_STAT(inName, &statBuf);
  1241. #ifndef _PS3
  1242. if ( MY_S_ISDIR(statBuf.st_mode) ) {
  1243. fprintf( stderr,
  1244. "%s: Input file %s is a directory.\n",
  1245. progName,inName);
  1246. setExit(1);
  1247. return;
  1248. }
  1249. #endif
  1250. }
  1251. if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
  1252. if (noisy)
  1253. fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
  1254. progName, inName );
  1255. setExit(1);
  1256. return;
  1257. }
  1258. if ( /* srcMode == SM_F2F implied && */ cantGuess ) {
  1259. if (noisy)
  1260. fprintf ( stderr,
  1261. "%s: Can't guess original name for %s -- using %s\n",
  1262. progName, inName, outName );
  1263. /* just a warning, no return */
  1264. }
  1265. if ( srcMode == SM_F2F && fileExists ( outName ) ) {
  1266. if (forceOverwrite) {
  1267. remove(outName);
  1268. } else {
  1269. fprintf ( stderr, "%s: Output file %s already exists.\n",
  1270. progName, outName );
  1271. setExit(1);
  1272. return;
  1273. }
  1274. }
  1275. if ( srcMode == SM_F2F && !forceOverwrite &&
  1276. (n=countHardLinks ( inName ) ) > 0) {
  1277. fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
  1278. progName, inName, n, n > 1 ? "s" : "" );
  1279. setExit(1);
  1280. return;
  1281. }
  1282. if ( srcMode == SM_F2F ) {
  1283. /* Save the file's meta-info before we open it. Doing it later
  1284. means we mess up the access times. */
  1285. saveInputFileMetaInfo ( inName );
  1286. }
  1287. switch ( srcMode ) {
  1288. case SM_I2O:
  1289. inStr = stdin;
  1290. outStr = stdout;
  1291. #ifndef _PS3
  1292. if ( isatty ( fileno ( stdin ) ) ) {
  1293. fprintf ( stderr,
  1294. "%s: I won't read compressed data from a terminal.\n",
  1295. progName );
  1296. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  1297. progName, progName );
  1298. setExit(1);
  1299. return;
  1300. };
  1301. #endif
  1302. break;
  1303. case SM_F2O:
  1304. inStr = fopen ( inName, "rb" );
  1305. outStr = stdout;
  1306. if ( inStr == NULL ) {
  1307. fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
  1308. progName, inName, strerror(errno) );
  1309. if ( inStr != NULL ) fclose ( inStr );
  1310. setExit(1);
  1311. return;
  1312. };
  1313. break;
  1314. case SM_F2F:
  1315. inStr = fopen ( inName, "rb" );
  1316. outStr = fopen_output_safely ( outName, "wb" );
  1317. if ( outStr == NULL) {
  1318. fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
  1319. progName, outName, strerror(errno) );
  1320. if ( inStr != NULL ) fclose ( inStr );
  1321. setExit(1);
  1322. return;
  1323. }
  1324. if ( inStr == NULL ) {
  1325. fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
  1326. progName, inName, strerror(errno) );
  1327. if ( outStr != NULL ) fclose ( outStr );
  1328. setExit(1);
  1329. return;
  1330. };
  1331. break;
  1332. default:
  1333. panic ( "uncompress: bad srcMode" );
  1334. break;
  1335. }
  1336. if (verbosity >= 1) {
  1337. fprintf ( stderr, " %s: ", inName );
  1338. pad ( inName );
  1339. fflush ( stderr );
  1340. }
  1341. /*--- Now the input and output handles are sane. Do the Biz. ---*/
  1342. outputHandleJustInCase = outStr;
  1343. deleteOutputOnInterrupt = True;
  1344. magicNumberOK = uncompressStream ( inStr, outStr );
  1345. outputHandleJustInCase = NULL;
  1346. /*--- If there was an I/O error, we won't get here. ---*/
  1347. if ( magicNumberOK ) {
  1348. if ( srcMode == SM_F2F ) {
  1349. applySavedMetaInfoToOutputFile ( outName );
  1350. deleteOutputOnInterrupt = False;
  1351. if ( !keepInputFiles ) {
  1352. IntNative retVal = remove ( inName );
  1353. ERROR_IF_NOT_ZERO ( retVal );
  1354. }
  1355. }
  1356. } else {
  1357. unzFailsExist = True;
  1358. deleteOutputOnInterrupt = False;
  1359. if ( srcMode == SM_F2F ) {
  1360. IntNative retVal = remove ( outName );
  1361. ERROR_IF_NOT_ZERO ( retVal );
  1362. }
  1363. }
  1364. deleteOutputOnInterrupt = False;
  1365. if ( magicNumberOK ) {
  1366. if (verbosity >= 1)
  1367. fprintf ( stderr, "done\n" );
  1368. } else {
  1369. setExit(2);
  1370. if (verbosity >= 1)
  1371. fprintf ( stderr, "not a bzip2 file.\n" ); else
  1372. fprintf ( stderr,
  1373. "%s: %s is not a bzip2 file.\n",
  1374. progName, inName );
  1375. }
  1376. }
  1377. /*---------------------------------------------*/
  1378. static
  1379. void testf ( Char *name )
  1380. {
  1381. FILE *inStr = NULL;
  1382. Bool allOK;
  1383. struct MY_STAT statBuf;
  1384. deleteOutputOnInterrupt = False;
  1385. if (name == NULL && srcMode != SM_I2O)
  1386. panic ( "testf: bad modes\n" );
  1387. copyFileName ( outName, "(none)" );
  1388. switch (srcMode) {
  1389. case SM_I2O: copyFileName ( inName, "(stdin)" ); break;
  1390. case SM_F2F: copyFileName ( inName, name ); break;
  1391. case SM_F2O: copyFileName ( inName, name ); break;
  1392. }
  1393. if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
  1394. if (noisy)
  1395. fprintf ( stderr, "%s: There are no files matching `%s'.\n",
  1396. progName, inName );
  1397. setExit(1);
  1398. return;
  1399. }
  1400. if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
  1401. fprintf ( stderr, "%s: Can't open input %s: %s.\n",
  1402. progName, inName, strerror(errno) );
  1403. setExit(1);
  1404. return;
  1405. }
  1406. if ( srcMode != SM_I2O ) {
  1407. MY_STAT(inName, &statBuf);
  1408. #ifndef _PS3
  1409. if ( MY_S_ISDIR(statBuf.st_mode) ) {
  1410. fprintf( stderr,
  1411. "%s: Input file %s is a directory.\n",
  1412. progName,inName);
  1413. setExit(1);
  1414. return;
  1415. }
  1416. #endif
  1417. }
  1418. switch ( srcMode ) {
  1419. case SM_I2O:
  1420. #ifndef _PS3
  1421. if ( isatty ( fileno ( stdin ) ) ) {
  1422. fprintf ( stderr,
  1423. "%s: I won't read compressed data from a terminal.\n",
  1424. progName );
  1425. fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
  1426. progName, progName );
  1427. setExit(1);
  1428. return;
  1429. };
  1430. #endif
  1431. inStr = stdin;
  1432. break;
  1433. case SM_F2O: case SM_F2F:
  1434. inStr = fopen ( inName, "rb" );
  1435. if ( inStr == NULL ) {
  1436. fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
  1437. progName, inName, strerror(errno) );
  1438. setExit(1);
  1439. return;
  1440. };
  1441. break;
  1442. default:
  1443. panic ( "testf: bad srcMode" );
  1444. break;
  1445. }
  1446. if (verbosity >= 1) {
  1447. fprintf ( stderr, " %s: ", inName );
  1448. pad ( inName );
  1449. fflush ( stderr );
  1450. }
  1451. /*--- Now the input handle is sane. Do the Biz. ---*/
  1452. outputHandleJustInCase = NULL;
  1453. allOK = testStream ( inStr );
  1454. if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" );
  1455. if (!allOK) testFailsExist = True;
  1456. }
  1457. /*---------------------------------------------*/
  1458. static
  1459. void license ( void )
  1460. {
  1461. fprintf ( stderr,
  1462. "bzip2, a block-sorting file compressor. "
  1463. "Version %s.\n"
  1464. " \n"
  1465. " Copyright (C) 1996-2002 by Julian Seward.\n"
  1466. " \n"
  1467. " This program is free software; you can redistribute it and/or modify\n"
  1468. " it under the terms set out in the LICENSE file, which is included\n"
  1469. " in the bzip2-1.0 source distribution.\n"
  1470. " \n"
  1471. " This program is distributed in the hope that it will be useful,\n"
  1472. " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1473. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1474. " LICENSE file for more details.\n"
  1475. " \n",
  1476. BZ2_bzlibVersion()
  1477. );
  1478. }
  1479. /*---------------------------------------------*/
  1480. static
  1481. void usage ( Char *fullProgName )
  1482. {
  1483. fprintf (
  1484. stderr,
  1485. "bzip2, a block-sorting file compressor. "
  1486. "Version %s.\n"
  1487. "\n usage: %s [flags and input files in any order]\n"
  1488. "\n"
  1489. " -h --help print this message\n"
  1490. " -d --decompress force decompression\n"
  1491. " -z --compress force compression\n"
  1492. " -k --keep keep (don't delete) input files\n"
  1493. " -f --force overwrite existing output files\n"
  1494. " -t --test test compressed file integrity\n"
  1495. " -c --stdout output to standard out\n"
  1496. " -q --quiet suppress noncritical error messages\n"
  1497. " -v --verbose be verbose (a 2nd -v gives more)\n"
  1498. " -L --license display software version & license\n"
  1499. " -V --version display software version & license\n"
  1500. " -s --small use less memory (at most 2500k)\n"
  1501. " -1 .. -9 set block size to 100k .. 900k\n"
  1502. " --fast alias for -1\n"
  1503. " --best alias for -9\n"
  1504. "\n"
  1505. " If invoked as `bzip2', default action is to compress.\n"
  1506. " as `bunzip2', default action is to decompress.\n"
  1507. " as `bzcat', default action is to decompress to stdout.\n"
  1508. "\n"
  1509. " If no file names are given, bzip2 compresses or decompresses\n"
  1510. " from standard input to standard output. You can combine\n"
  1511. " short flags, so `-v -4' means the same as -v4 or -4v, &c.\n"
  1512. # if BZ_UNIX
  1513. "\n"
  1514. # endif
  1515. ,
  1516. BZ2_bzlibVersion(),
  1517. fullProgName
  1518. );
  1519. }
  1520. /*---------------------------------------------*/
  1521. static
  1522. void redundant ( Char* flag )
  1523. {
  1524. fprintf (
  1525. stderr,
  1526. "%s: %s is redundant in versions 0.9.5 and above\n",
  1527. progName, flag );
  1528. }
  1529. /*---------------------------------------------*/
  1530. /*--
  1531. All the garbage from here to main() is purely to
  1532. implement a linked list of command-line arguments,
  1533. into which main() copies argv[1 .. argc-1].
  1534. The purpose of this exercise is to facilitate
  1535. the expansion of wildcard characters * and ? in
  1536. filenames for OSs which don't know how to do it
  1537. themselves, like MSDOS, Windows 95 and NT.
  1538. The actual Dirty Work is done by the platform-
  1539. specific macro APPEND_FILESPEC.
  1540. --*/
  1541. typedef
  1542. struct zzzz {
  1543. Char *name;
  1544. struct zzzz *link;
  1545. }
  1546. Cell;
  1547. /*---------------------------------------------*/
  1548. static
  1549. void *myMalloc ( Int32 n )
  1550. {
  1551. void* p;
  1552. p = malloc ( (size_t)n );
  1553. if (p == NULL) outOfMemory ();
  1554. return p;
  1555. }
  1556. /*---------------------------------------------*/
  1557. static
  1558. Cell *mkCell ( void )
  1559. {
  1560. Cell *c;
  1561. c = (Cell*) myMalloc ( sizeof ( Cell ) );
  1562. c->name = NULL;
  1563. c->link = NULL;
  1564. return c;
  1565. }
  1566. /*---------------------------------------------*/
  1567. static
  1568. Cell *snocString ( Cell *root, Char *name )
  1569. {
  1570. if (root == NULL) {
  1571. Cell *tmp = mkCell();
  1572. tmp->name = (Char*) myMalloc ( 5 + strlen(name) );
  1573. strcpy ( tmp->name, name );
  1574. return tmp;
  1575. } else {
  1576. Cell *tmp = root;
  1577. while (tmp->link != NULL) tmp = tmp->link;
  1578. tmp->link = snocString ( tmp->link, name );
  1579. return root;
  1580. }
  1581. }
  1582. /*---------------------------------------------*/
  1583. static
  1584. void addFlagsFromEnvVar ( Cell** argList, Char* varName )
  1585. {
  1586. #if !defined(_X360) && !defined(_PS3)
  1587. Int32 i, j, k;
  1588. Char *envbase, *p;
  1589. envbase = getenv(varName);
  1590. if (envbase != NULL) {
  1591. p = envbase;
  1592. i = 0;
  1593. while (True) {
  1594. if (p[i] == 0) break;
  1595. p += i;
  1596. i = 0;
  1597. while (V_isspace((Int32)(p[0]))) p++;
  1598. while (p[i] != 0 && !V_isspace((Int32)(p[i]))) i++;
  1599. if (i > 0) {
  1600. k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
  1601. for (j = 0; j < k; j++) tmpName[j] = p[j];
  1602. tmpName[k] = 0;
  1603. APPEND_FLAG(*argList, tmpName);
  1604. }
  1605. }
  1606. }
  1607. #endif
  1608. }
  1609. /*---------------------------------------------*/
  1610. #define ISFLAG(s) (strcmp(aa->name, (s))==0)
  1611. IntNative main ( IntNative argc, Char *argv[] )
  1612. {
  1613. Int32 i, j;
  1614. Char *tmp;
  1615. Cell *argList;
  1616. Cell *aa;
  1617. Bool decode;
  1618. /*-- Be really really really paranoid :-) --*/
  1619. if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 ||
  1620. sizeof(Int16) != 2 || sizeof(UInt16) != 2 ||
  1621. sizeof(Char) != 1 || sizeof(UChar) != 1)
  1622. configError();
  1623. /*-- Initialise --*/
  1624. outputHandleJustInCase = NULL;
  1625. smallMode = False;
  1626. keepInputFiles = False;
  1627. forceOverwrite = False;
  1628. noisy = True;
  1629. verbosity = 0;
  1630. blockSize100k = 9;
  1631. testFailsExist = False;
  1632. unzFailsExist = False;
  1633. numFileNames = 0;
  1634. numFilesProcessed = 0;
  1635. workFactor = 30;
  1636. deleteOutputOnInterrupt = False;
  1637. exitValue = 0;
  1638. i = j = 0; /* avoid bogus warning from egcs-1.1.X */
  1639. /*-- Set up signal handlers for mem access errors --*/
  1640. #ifndef _PS3
  1641. signal (SIGSEGV, mySIGSEGVorSIGBUScatcher);
  1642. #endif
  1643. # if BZ_UNIX
  1644. # ifndef __DJGPP__
  1645. #ifndef _PS3
  1646. signal (SIGBUS, mySIGSEGVorSIGBUScatcher);
  1647. #endif
  1648. # endif
  1649. # endif
  1650. copyFileName ( inName, "(none)" );
  1651. copyFileName ( outName, "(none)" );
  1652. copyFileName ( progNameReally, argv[0] );
  1653. progName = &progNameReally[0];
  1654. for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++)
  1655. if (*tmp == PATH_SEP) progName = tmp + 1;
  1656. /*-- Copy flags from env var BZIP2, and
  1657. expand filename wildcards in arg list.
  1658. --*/
  1659. argList = NULL;
  1660. addFlagsFromEnvVar ( &argList, "BZIP2" );
  1661. addFlagsFromEnvVar ( &argList, "BZIP" );
  1662. for (i = 1; i <= argc-1; i++)
  1663. APPEND_FILESPEC(argList, argv[i]);
  1664. /*-- Find the length of the longest filename --*/
  1665. longestFileName = 7;
  1666. numFileNames = 0;
  1667. decode = True;
  1668. for (aa = argList; aa != NULL; aa = aa->link) {
  1669. if (ISFLAG("--")) { decode = False; continue; }
  1670. if (aa->name[0] == '-' && decode) continue;
  1671. numFileNames++;
  1672. if (longestFileName < (Int32)strlen(aa->name) )
  1673. longestFileName = (Int32)strlen(aa->name);
  1674. }
  1675. /*-- Determine source modes; flag handling may change this too. --*/
  1676. if (numFileNames == 0)
  1677. srcMode = SM_I2O; else srcMode = SM_F2F;
  1678. /*-- Determine what to do (compress/uncompress/test/cat). --*/
  1679. /*-- Note that subsequent flag handling may change this. --*/
  1680. opMode = OM_Z;
  1681. if ( (strstr ( progName, "unzip" ) != 0) ||
  1682. (strstr ( progName, "UNZIP" ) != 0) )
  1683. opMode = OM_UNZ;
  1684. if ( (strstr ( progName, "z2cat" ) != 0) ||
  1685. (strstr ( progName, "Z2CAT" ) != 0) ||
  1686. (strstr ( progName, "zcat" ) != 0) ||
  1687. (strstr ( progName, "ZCAT" ) != 0) ) {
  1688. opMode = OM_UNZ;
  1689. srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O;
  1690. }
  1691. /*-- Look at the flags. --*/
  1692. for (aa = argList; aa != NULL; aa = aa->link) {
  1693. if (ISFLAG("--")) break;
  1694. if (aa->name[0] == '-' && aa->name[1] != '-') {
  1695. for (j = 1; aa->name[j] != '\0'; j++) {
  1696. switch (aa->name[j]) {
  1697. case 'c': srcMode = SM_F2O; break;
  1698. case 'd': opMode = OM_UNZ; break;
  1699. case 'z': opMode = OM_Z; break;
  1700. case 'f': forceOverwrite = True; break;
  1701. case 't': opMode = OM_TEST; break;
  1702. case 'k': keepInputFiles = True; break;
  1703. case 's': smallMode = True; break;
  1704. case 'q': noisy = False; break;
  1705. case '1': blockSize100k = 1; break;
  1706. case '2': blockSize100k = 2; break;
  1707. case '3': blockSize100k = 3; break;
  1708. case '4': blockSize100k = 4; break;
  1709. case '5': blockSize100k = 5; break;
  1710. case '6': blockSize100k = 6; break;
  1711. case '7': blockSize100k = 7; break;
  1712. case '8': blockSize100k = 8; break;
  1713. case '9': blockSize100k = 9; break;
  1714. case 'V':
  1715. case 'L': license(); break;
  1716. case 'v': verbosity++; break;
  1717. case 'h': usage ( progName );
  1718. exit ( 0 );
  1719. break;
  1720. default: fprintf ( stderr, "%s: Bad flag `%s'\n",
  1721. progName, aa->name );
  1722. usage ( progName );
  1723. exit ( 1 );
  1724. break;
  1725. }
  1726. }
  1727. }
  1728. }
  1729. /*-- And again ... --*/
  1730. for (aa = argList; aa != NULL; aa = aa->link) {
  1731. if (ISFLAG("--")) break;
  1732. if (ISFLAG("--stdout")) srcMode = SM_F2O; else
  1733. if (ISFLAG("--decompress")) opMode = OM_UNZ; else
  1734. if (ISFLAG("--compress")) opMode = OM_Z; else
  1735. if (ISFLAG("--force")) forceOverwrite = True; else
  1736. if (ISFLAG("--test")) opMode = OM_TEST; else
  1737. if (ISFLAG("--keep")) keepInputFiles = True; else
  1738. if (ISFLAG("--small")) smallMode = True; else
  1739. if (ISFLAG("--quiet")) noisy = False; else
  1740. if (ISFLAG("--version")) license(); else
  1741. if (ISFLAG("--license")) license(); else
  1742. if (ISFLAG("--exponential")) workFactor = 1; else
  1743. if (ISFLAG("--repetitive-best")) redundant(aa->name); else
  1744. if (ISFLAG("--repetitive-fast")) redundant(aa->name); else
  1745. if (ISFLAG("--fast")) blockSize100k = 1; else
  1746. if (ISFLAG("--best")) blockSize100k = 9; else
  1747. if (ISFLAG("--verbose")) verbosity++; else
  1748. if (ISFLAG("--help")) { usage ( progName ); exit ( 0 ); }
  1749. else
  1750. if (strncmp ( aa->name, "--", 2) == 0) {
  1751. fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name );
  1752. usage ( progName );
  1753. exit ( 1 );
  1754. }
  1755. }
  1756. if (verbosity > 4) verbosity = 4;
  1757. if (opMode == OM_Z && smallMode && blockSize100k > 2)
  1758. blockSize100k = 2;
  1759. if (opMode == OM_TEST && srcMode == SM_F2O) {
  1760. fprintf ( stderr, "%s: -c and -t cannot be used together.\n",
  1761. progName );
  1762. exit ( 1 );
  1763. }
  1764. if (srcMode == SM_F2O && numFileNames == 0)
  1765. srcMode = SM_I2O;
  1766. if (opMode != OM_Z) blockSize100k = 0;
  1767. if (srcMode == SM_F2F) {
  1768. #ifndef _PS3
  1769. signal (SIGINT, mySignalCatcher);
  1770. signal (SIGTERM, mySignalCatcher);
  1771. # if BZ_UNIX
  1772. signal (SIGHUP, mySignalCatcher);
  1773. # endif
  1774. #endif
  1775. }
  1776. if (opMode == OM_Z) {
  1777. if (srcMode == SM_I2O) {
  1778. compress ( NULL );
  1779. } else {
  1780. decode = True;
  1781. for (aa = argList; aa != NULL; aa = aa->link) {
  1782. if (ISFLAG("--")) { decode = False; continue; }
  1783. if (aa->name[0] == '-' && decode) continue;
  1784. numFilesProcessed++;
  1785. compress ( aa->name );
  1786. }
  1787. }
  1788. }
  1789. else
  1790. if (opMode == OM_UNZ) {
  1791. unzFailsExist = False;
  1792. if (srcMode == SM_I2O) {
  1793. uncompress ( NULL );
  1794. } else {
  1795. decode = True;
  1796. for (aa = argList; aa != NULL; aa = aa->link) {
  1797. if (ISFLAG("--")) { decode = False; continue; }
  1798. if (aa->name[0] == '-' && decode) continue;
  1799. numFilesProcessed++;
  1800. uncompress ( aa->name );
  1801. }
  1802. }
  1803. if (unzFailsExist) {
  1804. setExit(2);
  1805. exit(exitValue);
  1806. }
  1807. }
  1808. else {
  1809. testFailsExist = False;
  1810. if (srcMode == SM_I2O) {
  1811. testf ( NULL );
  1812. } else {
  1813. decode = True;
  1814. for (aa = argList; aa != NULL; aa = aa->link) {
  1815. if (ISFLAG("--")) { decode = False; continue; }
  1816. if (aa->name[0] == '-' && decode) continue;
  1817. numFilesProcessed++;
  1818. testf ( aa->name );
  1819. }
  1820. }
  1821. if (testFailsExist && noisy) {
  1822. fprintf ( stderr,
  1823. "\n"
  1824. "You can use the `bzip2recover' program to attempt to recover\n"
  1825. "data from undamaged sections of corrupted files.\n\n"
  1826. );
  1827. setExit(2);
  1828. exit(exitValue);
  1829. }
  1830. }
  1831. /* Free the argument list memory to mollify leak detectors
  1832. (eg) Purify, Checker. Serves no other useful purpose.
  1833. */
  1834. aa = argList;
  1835. while (aa != NULL) {
  1836. Cell* aa2 = aa->link;
  1837. if (aa->name != NULL) free(aa->name);
  1838. free(aa);
  1839. aa = aa2;
  1840. }
  1841. return exitValue;
  1842. }
  1843. /*-----------------------------------------------------------*/
  1844. /*--- end bzip2.c ---*/
  1845. /*-----------------------------------------------------------*/