gzappend.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* gzappend -- command to append to a gzip file
  2. Copyright (C) 2003 Mark Adler, all rights reserved
  3. version 1.1, 4 Nov 2003
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the author be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. Mark Adler madler@alumni.caltech.edu
  18. */
  19. /*
  20. * Change history:
  21. *
  22. * 1.0 19 Oct 2003 - First version
  23. * 1.1 4 Nov 2003 - Expand and clarify some comments and notes
  24. * - Add version and copyright to help
  25. * - Send help to stdout instead of stderr
  26. * - Add some preemptive typecasts
  27. * - Add L to constants in lseek() calls
  28. * - Remove some debugging information in error messages
  29. * - Use new data_type definition for zlib 1.2.1
  30. * - Simplfy and unify file operations
  31. * - Finish off gzip file in gztack()
  32. * - Use deflatePrime() instead of adding empty blocks
  33. * - Keep gzip file clean on appended file read errors
  34. * - Use in-place rotate instead of auxiliary buffer
  35. * (Why you ask? Because it was fun to write!)
  36. */
  37. /*
  38. gzappend takes a gzip file and appends to it, compressing files from the
  39. command line or data from stdin. The gzip file is written to directly, to
  40. avoid copying that file, in case it's large. Note that this results in the
  41. unfriendly behavior that if gzappend fails, the gzip file is corrupted.
  42. This program was written to illustrate the use of the new Z_BLOCK option of
  43. zlib 1.2.x's inflate() function. This option returns from inflate() at each
  44. block boundary to facilitate locating and modifying the last block bit at
  45. the start of the final deflate block. Also whether using Z_BLOCK or not,
  46. another required feature of zlib 1.2.x is that inflate() now provides the
  47. number of unusued bits in the last input byte used. gzappend will not work
  48. with versions of zlib earlier than 1.2.1.
  49. gzappend first decompresses the gzip file internally, discarding all but
  50. the last 32K of uncompressed data, and noting the location of the last block
  51. bit and the number of unused bits in the last byte of the compressed data.
  52. The gzip trailer containing the CRC-32 and length of the uncompressed data
  53. is verified. This trailer will be later overwritten.
  54. Then the last block bit is cleared by seeking back in the file and rewriting
  55. the byte that contains it. Seeking forward, the last byte of the compressed
  56. data is saved along with the number of unused bits to initialize deflate.
  57. A deflate process is initialized, using the last 32K of the uncompressed
  58. data from the gzip file to initialize the dictionary. If the total
  59. uncompressed data was less than 32K, then all of it is used to initialize
  60. the dictionary. The deflate output bit buffer is also initialized with the
  61. last bits from the original deflate stream. From here on, the data to
  62. append is simply compressed using deflate, and written to the gzip file.
  63. When that is complete, the new CRC-32 and uncompressed length are written
  64. as the trailer of the gzip file.
  65. */
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include <fcntl.h>
  70. #include <unistd.h>
  71. #include "zlib.h"
  72. #define local static
  73. #define LGCHUNK 14
  74. #define CHUNK (1U << LGCHUNK)
  75. #define DSIZE 32768U
  76. /* print an error message and terminate with extreme prejudice */
  77. local void bye(char *msg1, char *msg2)
  78. {
  79. fprintf(stderr, "gzappend error: %s%s\n", msg1, msg2);
  80. exit(1);
  81. }
  82. /* return the greatest common divisor of a and b using Euclid's algorithm,
  83. modified to be fast when one argument much greater than the other, and
  84. coded to avoid unnecessary swapping */
  85. local unsigned gcd(unsigned a, unsigned b)
  86. {
  87. unsigned c;
  88. while (a && b)
  89. if (a > b) {
  90. c = b;
  91. while (a - c >= c)
  92. c <<= 1;
  93. a -= c;
  94. }
  95. else {
  96. c = a;
  97. while (b - c >= c)
  98. c <<= 1;
  99. b -= c;
  100. }
  101. return a + b;
  102. }
  103. /* rotate list[0..len-1] left by rot positions, in place */
  104. local void rotate(unsigned char *list, unsigned len, unsigned rot)
  105. {
  106. unsigned char tmp;
  107. unsigned cycles;
  108. unsigned char *start, *last, *to, *from;
  109. /* normalize rot and handle degenerate cases */
  110. if (len < 2) return;
  111. if (rot >= len) rot %= len;
  112. if (rot == 0) return;
  113. /* pointer to last entry in list */
  114. last = list + (len - 1);
  115. /* do simple left shift by one */
  116. if (rot == 1) {
  117. tmp = *list;
  118. memcpy(list, list + 1, len - 1);
  119. *last = tmp;
  120. return;
  121. }
  122. /* do simple right shift by one */
  123. if (rot == len - 1) {
  124. tmp = *last;
  125. memmove(list + 1, list, len - 1);
  126. *list = tmp;
  127. return;
  128. }
  129. /* otherwise do rotate as a set of cycles in place */
  130. cycles = gcd(len, rot); /* number of cycles */
  131. do {
  132. start = from = list + cycles; /* start index is arbitrary */
  133. tmp = *from; /* save entry to be overwritten */
  134. for (;;) {
  135. to = from; /* next step in cycle */
  136. from += rot; /* go right rot positions */
  137. if (from > last) from -= len; /* (pointer better not wrap) */
  138. if (from == start) break; /* all but one shifted */
  139. *to = *from; /* shift left */
  140. }
  141. *to = tmp; /* complete the circle */
  142. } while (--cycles);
  143. }
  144. /* structure for gzip file read operations */
  145. typedef struct {
  146. int fd; /* file descriptor */
  147. int size; /* 1 << size is bytes in buf */
  148. unsigned left; /* bytes available at next */
  149. unsigned char *buf; /* buffer */
  150. unsigned char *next; /* next byte in buffer */
  151. char *name; /* file name for error messages */
  152. } file;
  153. /* reload buffer */
  154. local int readin(file *in)
  155. {
  156. int len;
  157. len = read(in->fd, in->buf, 1 << in->size);
  158. if (len == -1) bye("error reading ", in->name);
  159. in->left = (unsigned)len;
  160. in->next = in->buf;
  161. return len;
  162. }
  163. /* read from file in, exit if end-of-file */
  164. local int readmore(file *in)
  165. {
  166. if (readin(in) == 0) bye("unexpected end of ", in->name);
  167. return 0;
  168. }
  169. #define read1(in) (in->left == 0 ? readmore(in) : 0, \
  170. in->left--, *(in->next)++)
  171. /* skip over n bytes of in */
  172. local void skip(file *in, unsigned n)
  173. {
  174. unsigned bypass;
  175. if (n > in->left) {
  176. n -= in->left;
  177. bypass = n & ~((1U << in->size) - 1);
  178. if (bypass) {
  179. if (lseek(in->fd, (off_t)bypass, SEEK_CUR) == -1)
  180. bye("seeking ", in->name);
  181. n -= bypass;
  182. }
  183. readmore(in);
  184. if (n > in->left)
  185. bye("unexpected end of ", in->name);
  186. }
  187. in->left -= n;
  188. in->next += n;
  189. }
  190. /* read a four-byte unsigned integer, little-endian, from in */
  191. unsigned long read4(file *in)
  192. {
  193. unsigned long val;
  194. val = read1(in);
  195. val += (unsigned)read1(in) << 8;
  196. val += (unsigned long)read1(in) << 16;
  197. val += (unsigned long)read1(in) << 24;
  198. return val;
  199. }
  200. /* skip over gzip header */
  201. local void gzheader(file *in)
  202. {
  203. int flags;
  204. unsigned n;
  205. if (read1(in) != 31 || read1(in) != 139) bye(in->name, " not a gzip file");
  206. if (read1(in) != 8) bye("unknown compression method in", in->name);
  207. flags = read1(in);
  208. if (flags & 0xe0) bye("unknown header flags set in", in->name);
  209. skip(in, 6);
  210. if (flags & 4) {
  211. n = read1(in);
  212. n += (unsigned)(read1(in)) << 8;
  213. skip(in, n);
  214. }
  215. if (flags & 8) while (read1(in) != 0) ;
  216. if (flags & 16) while (read1(in) != 0) ;
  217. if (flags & 2) skip(in, 2);
  218. }
  219. /* decompress gzip file "name", return strm with a deflate stream ready to
  220. continue compression of the data in the gzip file, and return a file
  221. descriptor pointing to where to write the compressed data -- the deflate
  222. stream is initialized to compress using level "level" */
  223. local int gzscan(char *name, z_stream *strm, int level)
  224. {
  225. int ret, lastbit, left, full;
  226. unsigned have;
  227. unsigned long crc, tot;
  228. unsigned char *window;
  229. off_t lastoff, end;
  230. file gz;
  231. /* open gzip file */
  232. gz.name = name;
  233. gz.fd = open(name, O_RDWR, 0);
  234. if (gz.fd == -1) bye("cannot open ", name);
  235. gz.buf = malloc(CHUNK);
  236. if (gz.buf == NULL) bye("out of memory", "");
  237. gz.size = LGCHUNK;
  238. gz.left = 0;
  239. /* skip gzip header */
  240. gzheader(&gz);
  241. /* prepare to decompress */
  242. window = malloc(DSIZE);
  243. if (window == NULL) bye("out of memory", "");
  244. strm->zalloc = Z_NULL;
  245. strm->zfree = Z_NULL;
  246. strm->opaque = Z_NULL;
  247. ret = inflateInit2(strm, -15);
  248. if (ret != Z_OK) bye("out of memory", " or library mismatch");
  249. /* decompress the deflate stream, saving append information */
  250. lastbit = 0;
  251. lastoff = lseek(gz.fd, 0L, SEEK_CUR) - gz.left;
  252. left = 0;
  253. strm->avail_in = gz.left;
  254. strm->next_in = gz.next;
  255. crc = crc32(0L, Z_NULL, 0);
  256. have = full = 0;
  257. do {
  258. /* if needed, get more input */
  259. if (strm->avail_in == 0) {
  260. readmore(&gz);
  261. strm->avail_in = gz.left;
  262. strm->next_in = gz.next;
  263. }
  264. /* set up output to next available section of sliding window */
  265. strm->avail_out = DSIZE - have;
  266. strm->next_out = window + have;
  267. /* inflate and check for errors */
  268. ret = inflate(strm, Z_BLOCK);
  269. if (ret == Z_STREAM_ERROR) bye("internal stream error!", "");
  270. if (ret == Z_MEM_ERROR) bye("out of memory", "");
  271. if (ret == Z_DATA_ERROR)
  272. bye("invalid compressed data--format violated in", name);
  273. /* update crc and sliding window pointer */
  274. crc = crc32(crc, window + have, DSIZE - have - strm->avail_out);
  275. if (strm->avail_out)
  276. have = DSIZE - strm->avail_out;
  277. else {
  278. have = 0;
  279. full = 1;
  280. }
  281. /* process end of block */
  282. if (strm->data_type & 128) {
  283. if (strm->data_type & 64)
  284. left = strm->data_type & 0x1f;
  285. else {
  286. lastbit = strm->data_type & 0x1f;
  287. lastoff = lseek(gz.fd, 0L, SEEK_CUR) - strm->avail_in;
  288. }
  289. }
  290. } while (ret != Z_STREAM_END);
  291. inflateEnd(strm);
  292. gz.left = strm->avail_in;
  293. gz.next = strm->next_in;
  294. /* save the location of the end of the compressed data */
  295. end = lseek(gz.fd, 0L, SEEK_CUR) - gz.left;
  296. /* check gzip trailer and save total for deflate */
  297. if (crc != read4(&gz))
  298. bye("invalid compressed data--crc mismatch in ", name);
  299. tot = strm->total_out;
  300. if ((tot & 0xffffffffUL) != read4(&gz))
  301. bye("invalid compressed data--length mismatch in", name);
  302. /* if not at end of file, warn */
  303. if (gz.left || readin(&gz))
  304. fprintf(stderr,
  305. "gzappend warning: junk at end of gzip file overwritten\n");
  306. /* clear last block bit */
  307. lseek(gz.fd, lastoff - (lastbit != 0), SEEK_SET);
  308. if (read(gz.fd, gz.buf, 1) != 1) bye("reading after seek on ", name);
  309. *gz.buf = (unsigned char)(*gz.buf ^ (1 << ((8 - lastbit) & 7)));
  310. lseek(gz.fd, -1L, SEEK_CUR);
  311. if (write(gz.fd, gz.buf, 1) != 1) bye("writing after seek to ", name);
  312. /* if window wrapped, build dictionary from window by rotating */
  313. if (full) {
  314. rotate(window, DSIZE, have);
  315. have = DSIZE;
  316. }
  317. /* set up deflate stream with window, crc, total_in, and leftover bits */
  318. ret = deflateInit2(strm, level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
  319. if (ret != Z_OK) bye("out of memory", "");
  320. deflateSetDictionary(strm, window, have);
  321. strm->adler = crc;
  322. strm->total_in = tot;
  323. if (left) {
  324. lseek(gz.fd, --end, SEEK_SET);
  325. if (read(gz.fd, gz.buf, 1) != 1) bye("reading after seek on ", name);
  326. deflatePrime(strm, 8 - left, *gz.buf);
  327. }
  328. lseek(gz.fd, end, SEEK_SET);
  329. /* clean up and return */
  330. free(window);
  331. free(gz.buf);
  332. return gz.fd;
  333. }
  334. /* append file "name" to gzip file gd using deflate stream strm -- if last
  335. is true, then finish off the deflate stream at the end */
  336. local void gztack(char *name, int gd, z_stream *strm, int last)
  337. {
  338. int fd, len, ret;
  339. unsigned left;
  340. unsigned char *in, *out;
  341. /* open file to compress and append */
  342. fd = 0;
  343. if (name != NULL) {
  344. fd = open(name, O_RDONLY, 0);
  345. if (fd == -1)
  346. fprintf(stderr, "gzappend warning: %s not found, skipping ...\n",
  347. name);
  348. }
  349. /* allocate buffers */
  350. in = fd == -1 ? NULL : malloc(CHUNK);
  351. out = malloc(CHUNK);
  352. if (out == NULL) bye("out of memory", "");
  353. /* compress input file and append to gzip file */
  354. do {
  355. /* get more input */
  356. len = fd == -1 ? 0 : read(fd, in, CHUNK);
  357. if (len == -1) {
  358. fprintf(stderr,
  359. "gzappend warning: error reading %s, skipping rest ...\n",
  360. name);
  361. len = 0;
  362. }
  363. strm->avail_in = (unsigned)len;
  364. strm->next_in = in;
  365. if (len) strm->adler = crc32(strm->adler, in, (unsigned)len);
  366. /* compress and write all available output */
  367. do {
  368. strm->avail_out = CHUNK;
  369. strm->next_out = out;
  370. ret = deflate(strm, last && len == 0 ? Z_FINISH : Z_NO_FLUSH);
  371. left = CHUNK - strm->avail_out;
  372. while (left) {
  373. len = write(gd, out + CHUNK - strm->avail_out - left, left);
  374. if (len == -1) bye("writing gzip file", "");
  375. left -= (unsigned)len;
  376. }
  377. } while (strm->avail_out == 0 && ret != Z_STREAM_END);
  378. } while (len != 0);
  379. /* write trailer after last entry */
  380. if (last) {
  381. deflateEnd(strm);
  382. out[0] = (unsigned char)(strm->adler);
  383. out[1] = (unsigned char)(strm->adler >> 8);
  384. out[2] = (unsigned char)(strm->adler >> 16);
  385. out[3] = (unsigned char)(strm->adler >> 24);
  386. out[4] = (unsigned char)(strm->total_in);
  387. out[5] = (unsigned char)(strm->total_in >> 8);
  388. out[6] = (unsigned char)(strm->total_in >> 16);
  389. out[7] = (unsigned char)(strm->total_in >> 24);
  390. len = 8;
  391. do {
  392. ret = write(gd, out + 8 - len, len);
  393. if (ret == -1) bye("writing gzip file", "");
  394. len -= ret;
  395. } while (len);
  396. close(gd);
  397. }
  398. /* clean up and return */
  399. free(out);
  400. if (in != NULL) free(in);
  401. if (fd > 0) close(fd);
  402. }
  403. /* process the compression level option if present, scan the gzip file, and
  404. append the specified files, or append the data from stdin if no other file
  405. names are provided on the command line -- the gzip file must be writable
  406. and seekable */
  407. int main(int argc, char **argv)
  408. {
  409. int gd, level;
  410. z_stream strm;
  411. /* ignore command name */
  412. argv++;
  413. /* provide usage if no arguments */
  414. if (*argv == NULL) {
  415. printf("gzappend 1.1 (4 Nov 2003) Copyright (C) 2003 Mark Adler\n");
  416. printf(
  417. "usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n");
  418. return 0;
  419. }
  420. /* set compression level */
  421. level = Z_DEFAULT_COMPRESSION;
  422. if (argv[0][0] == '-') {
  423. if (argv[0][1] < '0' || argv[0][1] > '9' || argv[0][2] != 0)
  424. bye("invalid compression level", "");
  425. level = argv[0][1] - '0';
  426. if (*++argv == NULL) bye("no gzip file name after options", "");
  427. }
  428. /* prepare to append to gzip file */
  429. gd = gzscan(*argv++, &strm, level);
  430. /* append files on command line, or from stdin if none */
  431. if (*argv == NULL)
  432. gztack(NULL, gd, &strm, 1);
  433. else
  434. do {
  435. gztack(*argv, gd, &strm, argv[1] == NULL);
  436. } while (*++argv != NULL);
  437. return 0;
  438. }