3
0

bzip2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com>
  3. *
  4. * This file uses bzip2 library code which is written
  5. * by Julian Seward <jseward@bzip.org>.
  6. * See README and LICENSE files in bz/ directory for more information
  7. * about bzip2 library code.
  8. */
  9. #include "libbb.h"
  10. #define CONFIG_BZIP2_FEATURE_SPEED 1
  11. /* Speed test:
  12. * Compiled with gcc 4.2.1, run on Athlon 64 1800 MHz (512K L2 cache).
  13. * Stock bzip2 is 26.4% slower than bbox bzip2 at SPEED 1
  14. * (time to compress gcc-4.2.1.tar is 126.4% compared to bbox).
  15. * At SPEED 5 difference is 32.7%.
  16. *
  17. * Test run of all CONFIG_BZIP2_FEATURE_SPEED values on a 11Mb text file:
  18. * Size Time (3 runs)
  19. * 0: 10828 4.145 4.146 4.148
  20. * 1: 11097 3.845 3.860 3.861
  21. * 2: 11392 3.763 3.767 3.768
  22. * 3: 11892 3.722 3.724 3.727
  23. * 4: 12740 3.637 3.640 3.644
  24. * 5: 17273 3.497 3.509 3.509
  25. */
  26. #define BZ_DEBUG 0
  27. /* Takes ~300 bytes, detects corruption caused by bad RAM etc */
  28. #define BZ_LIGHT_DEBUG 0
  29. #include "bz/bzlib.h"
  30. #include "bz/bzlib_private.h"
  31. #include "bz/blocksort.c"
  32. #include "bz/bzlib.c"
  33. #include "bz/compress.c"
  34. #include "bz/huffman.c"
  35. /* No point in being shy and having very small buffer here.
  36. * bzip2 internal buffers are much bigger anyway, hundreds of kbytes.
  37. * If iobuf is several pages long, malloc() may use mmap,
  38. * making iobuf is page aligned and thus (maybe) have one memcpy less
  39. * if kernel is clever enough.
  40. */
  41. enum {
  42. IOBUF_SIZE = 8 * 1024
  43. };
  44. static uint8_t level;
  45. /* NB: compressStream() has to return -1 on errors, not die.
  46. * bbunpack() will correctly clean up in this case
  47. * (delete incomplete .bz2 file)
  48. */
  49. /* Returns:
  50. * -1 on errors
  51. * total written bytes so far otherwise
  52. */
  53. static
  54. USE_DESKTOP(long long) int bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, void *wbuf)
  55. {
  56. int n, n2, ret;
  57. strm->avail_in = rlen;
  58. strm->next_in = rbuf;
  59. while (1) {
  60. strm->avail_out = IOBUF_SIZE;
  61. strm->next_out = wbuf;
  62. ret = BZ2_bzCompress(strm, rlen ? BZ_RUN : BZ_FINISH);
  63. if (ret != BZ_RUN_OK /* BZ_RUNning */
  64. && ret != BZ_FINISH_OK /* BZ_FINISHing, but not done yet */
  65. && ret != BZ_STREAM_END /* BZ_FINISHed */
  66. ) {
  67. bb_error_msg_and_die("internal error %d", ret);
  68. }
  69. n = IOBUF_SIZE - strm->avail_out;
  70. if (n) {
  71. n2 = full_write(STDOUT_FILENO, wbuf, n);
  72. if (n2 != n) {
  73. if (n2 >= 0)
  74. errno = 0; /* prevent bogus error message */
  75. bb_perror_msg(n2 >= 0 ? "short write" : "write error");
  76. return -1;
  77. }
  78. }
  79. if (ret == BZ_STREAM_END)
  80. break;
  81. if (rlen && strm->avail_in == 0)
  82. break;
  83. }
  84. return 0 USE_DESKTOP( + strm->total_out );
  85. }
  86. static
  87. USE_DESKTOP(long long) int compressStream(void)
  88. {
  89. USE_DESKTOP(long long) int total;
  90. ssize_t count;
  91. bz_stream bzs; /* it's small */
  92. #define strm (&bzs)
  93. char *iobuf;
  94. #define rbuf iobuf
  95. #define wbuf (iobuf + IOBUF_SIZE)
  96. iobuf = xmalloc(2 * IOBUF_SIZE);
  97. BZ2_bzCompressInit(strm, level);
  98. while (1) {
  99. count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
  100. if (count < 0) {
  101. bb_perror_msg("read error");
  102. total = -1;
  103. break;
  104. }
  105. /* if count == 0, bz_write finalizes compression */
  106. total = bz_write(strm, rbuf, count, wbuf);
  107. if (count == 0 || total < 0)
  108. break;
  109. }
  110. #if ENABLE_FEATURE_CLEAN_UP
  111. BZ2_bzCompressEnd(strm);
  112. free(iobuf);
  113. #endif
  114. return total;
  115. }
  116. static
  117. char* make_new_name_bzip2(char *filename)
  118. {
  119. return xasprintf("%s.bz2", filename);
  120. }
  121. int bzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  122. int bzip2_main(int argc ATTRIBUTE_UNUSED, char **argv)
  123. {
  124. unsigned opt;
  125. /* standard bzip2 flags
  126. * -d --decompress force decompression
  127. * -z --compress force compression
  128. * -k --keep keep (don't delete) input files
  129. * -f --force overwrite existing output files
  130. * -t --test test compressed file integrity
  131. * -c --stdout output to standard out
  132. * -q --quiet suppress noncritical error messages
  133. * -v --verbose be verbose (a 2nd -v gives more)
  134. * -s --small use less memory (at most 2500k)
  135. * -1 .. -9 set block size to 100k .. 900k
  136. * --fast alias for -1
  137. * --best alias for -9
  138. */
  139. opt_complementary = "s2"; /* -s means -2 (compatibility) */
  140. /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
  141. opt = getopt32(argv, "cfv" USE_BUNZIP2("d") "123456789qzs" );
  142. #if ENABLE_BUNZIP2 /* bunzip2_main may not be visible... */
  143. if (opt & 0x8) // -d
  144. return bunzip2_main(argc, argv);
  145. opt >>= 4;
  146. #else
  147. opt >>= 3;
  148. #endif
  149. opt = (uint8_t)opt; /* isolate bits for -1..-8 */
  150. opt |= 0x100; /* if nothing else, assume -9 */
  151. level = 1;
  152. while (!(opt & 1)) {
  153. level++;
  154. opt >>= 1;
  155. }
  156. argv += optind;
  157. option_mask32 &= 0x7; /* ignore all except -cfv */
  158. return bbunpack(argv, make_new_name_bzip2, compressStream);
  159. }