PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/StormLib/stormlib/bzip2/bzip2.c

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