gunzip.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <flate.h>
  5. #include "gzip.h"
  6. typedef struct GZHead GZHead;
  7. struct GZHead
  8. {
  9. ulong mtime;
  10. char *file;
  11. };
  12. static int crcwrite(void *bout, void *buf, int n);
  13. static int get1(Biobuf *b);
  14. static ulong get4(Biobuf *b);
  15. static int gunzipf(char *file, int stdout);
  16. static int gunzip(int ofd, char *ofile, Biobuf *bin);
  17. static void header(Biobuf *bin, GZHead *h);
  18. static void trailer(Biobuf *bin, long wlen);
  19. static void error(char*, ...);
  20. #pragma varargck argpos error 1
  21. static Biobuf bin;
  22. static ulong crc;
  23. static ulong *crctab;
  24. static int debug;
  25. static char *delfile;
  26. static vlong gzok;
  27. static char *infile;
  28. static int settimes;
  29. static int table;
  30. static int verbose;
  31. static int wbad;
  32. static ulong wlen;
  33. static jmp_buf zjmp;
  34. void
  35. usage(void)
  36. {
  37. fprint(2, "usage: gunzip [-ctvTD] [file ....]\n");
  38. exits("usage");
  39. }
  40. void
  41. main(int argc, char *argv[])
  42. {
  43. int i, ok, stdout;
  44. stdout = 0;
  45. ARGBEGIN{
  46. case 'D':
  47. debug++;
  48. break;
  49. case 'c':
  50. stdout++;
  51. break;
  52. case 't':
  53. table++;
  54. break;
  55. case 'T':
  56. settimes++;
  57. break;
  58. case 'v':
  59. verbose++;
  60. break;
  61. default:
  62. usage();
  63. break;
  64. }ARGEND
  65. crctab = mkcrctab(GZCRCPOLY);
  66. ok = inflateinit();
  67. if(ok != FlateOk)
  68. sysfatal("inflateinit failed: %s\n", flateerr(ok));
  69. if(argc == 0){
  70. Binit(&bin, 0, OREAD);
  71. settimes = 0;
  72. infile = "<stdin>";
  73. ok = gunzip(1, "<stdout>", &bin);
  74. }else{
  75. ok = 1;
  76. if(stdout)
  77. settimes = 0;
  78. for(i = 0; i < argc; i++)
  79. ok &= gunzipf(argv[i], stdout);
  80. }
  81. exits(ok ? nil: "errors");
  82. }
  83. static int
  84. gunzipf(char *file, int stdout)
  85. {
  86. char ofile[256], *s;
  87. int ofd, ifd, ok;
  88. infile = file;
  89. ifd = open(file, OREAD);
  90. if(ifd < 0){
  91. fprint(2, "gunzip: can't open %s: %r\n", file);
  92. return 0;
  93. }
  94. Binit(&bin, ifd, OREAD);
  95. if(Bgetc(&bin) != GZMAGIC1 || Bgetc(&bin) != GZMAGIC2 || Bgetc(&bin) != GZDEFLATE){
  96. fprint(2, "gunzip: %s is not a gzip deflate file\n", file);
  97. Bterm(&bin);
  98. close(ifd);
  99. return 0;
  100. }
  101. Bungetc(&bin);
  102. Bungetc(&bin);
  103. Bungetc(&bin);
  104. if(table)
  105. ofd = -1;
  106. else if(stdout){
  107. ofd = 1;
  108. strcpy(ofile, "<stdout>");
  109. }else{
  110. s = strrchr(file, '/');
  111. if(s != nil)
  112. s++;
  113. else
  114. s = file;
  115. strecpy(ofile, ofile+sizeof ofile, s);
  116. s = strrchr(ofile, '.');
  117. if(s != nil && s != ofile && strcmp(s, ".gz") == 0)
  118. *s = '\0';
  119. else if(s != nil && strcmp(s, ".tgz") == 0)
  120. strcpy(s, ".tar");
  121. else if(strcmp(file, ofile) == 0){
  122. fprint(2, "gunzip: can't overwrite %s\n", file);
  123. Bterm(&bin);
  124. close(ifd);
  125. return 0;
  126. }
  127. ofd = create(ofile, OWRITE, 0666);
  128. if(ofd < 0){
  129. fprint(2, "gunzip: can't create %s: %r\n", ofile);
  130. Bterm(&bin);
  131. close(ifd);
  132. return 0;
  133. }
  134. delfile = ofile;
  135. }
  136. wbad = 0;
  137. ok = gunzip(ofd, ofile, &bin);
  138. Bterm(&bin);
  139. close(ifd);
  140. if(wbad){
  141. fprint(2, "gunzip: can't write %s: %r\n", ofile);
  142. if(delfile)
  143. remove(delfile);
  144. }
  145. delfile = nil;
  146. if(!stdout && ofd >= 0)
  147. close(ofd);
  148. return ok;
  149. }
  150. static int
  151. gunzip(int ofd, char *ofile, Biobuf *bin)
  152. {
  153. Dir *d;
  154. GZHead h;
  155. int err;
  156. h.file = nil;
  157. gzok = 0;
  158. for(;;){
  159. if(Bgetc(bin) < 0)
  160. return 1;
  161. Bungetc(bin);
  162. if(setjmp(zjmp))
  163. return 0;
  164. header(bin, &h);
  165. gzok = 0;
  166. wlen = 0;
  167. crc = 0;
  168. if(!table && verbose)
  169. fprint(2, "extracting %s to %s\n", h.file, ofile);
  170. err = inflate((void*)ofd, crcwrite, bin, (int(*)(void*))Bgetc);
  171. if(err != FlateOk)
  172. error("inflate failed: %s", flateerr(err));
  173. trailer(bin, wlen);
  174. if(table){
  175. if(verbose)
  176. print("%-32s %10ld %s", h.file, wlen, ctime(h.mtime));
  177. else
  178. print("%s\n", h.file);
  179. }else if(settimes && h.mtime && (d=dirfstat(ofd)) != nil){
  180. d->mtime = h.mtime;
  181. dirfwstat(ofd, d);
  182. free(d);
  183. }
  184. free(h.file);
  185. h.file = nil;
  186. gzok = Boffset(bin);
  187. }
  188. }
  189. static void
  190. header(Biobuf *bin, GZHead *h)
  191. {
  192. char *s;
  193. int i, c, flag, ns, nsa;
  194. if(get1(bin) != GZMAGIC1 || get1(bin) != GZMAGIC2)
  195. error("bad gzip file magic");
  196. if(get1(bin) != GZDEFLATE)
  197. error("unknown compression type");
  198. flag = get1(bin);
  199. if(flag & ~(GZFTEXT|GZFEXTRA|GZFNAME|GZFCOMMENT|GZFHCRC))
  200. fprint(2, "gunzip: reserved flags set, data may not be decompressed correctly\n");
  201. /* mod time */
  202. h->mtime = get4(bin);
  203. /* extra flags */
  204. get1(bin);
  205. /* OS type */
  206. get1(bin);
  207. if(flag & GZFEXTRA)
  208. for(i=get1(bin); i>0; i--)
  209. get1(bin);
  210. /* name */
  211. if(flag & GZFNAME){
  212. nsa = 32;
  213. ns = 0;
  214. s = malloc(nsa);
  215. if(s == nil)
  216. error("out of memory");
  217. while((c = get1(bin)) != 0){
  218. s[ns++] = c;
  219. if(ns >= nsa){
  220. nsa += 32;
  221. s = realloc(s, nsa);
  222. if(s == nil)
  223. error("out of memory");
  224. }
  225. }
  226. s[ns] = '\0';
  227. h->file = s;
  228. }else
  229. h->file = strdup("<unnamed file>");
  230. /* comment */
  231. if(flag & GZFCOMMENT)
  232. while(get1(bin) != 0)
  233. ;
  234. /* crc16 */
  235. if(flag & GZFHCRC){
  236. get1(bin);
  237. get1(bin);
  238. }
  239. }
  240. static void
  241. trailer(Biobuf *bin, long wlen)
  242. {
  243. ulong tcrc;
  244. long len;
  245. tcrc = get4(bin);
  246. if(tcrc != crc)
  247. error("crc mismatch");
  248. len = get4(bin);
  249. if(len != wlen)
  250. error("bad output length: expected %lud got %lud", wlen, len);
  251. }
  252. static ulong
  253. get4(Biobuf *b)
  254. {
  255. ulong v;
  256. int i, c;
  257. v = 0;
  258. for(i = 0; i < 4; i++){
  259. c = Bgetc(b);
  260. if(c < 0)
  261. error("unexpected eof reading file information");
  262. v |= c << (i * 8);
  263. }
  264. return v;
  265. }
  266. static int
  267. get1(Biobuf *b)
  268. {
  269. int c;
  270. c = Bgetc(b);
  271. if(c < 0)
  272. error("unexpected eof reading file information");
  273. return c;
  274. }
  275. static int
  276. crcwrite(void *out, void *buf, int n)
  277. {
  278. int fd, nw;
  279. wlen += n;
  280. crc = blockcrc(crctab, crc, buf, n);
  281. fd = (int)(uintptr)out;
  282. if(fd < 0)
  283. return n;
  284. nw = write(fd, buf, n);
  285. if(nw != n)
  286. wbad = 1;
  287. return nw;
  288. }
  289. static void
  290. error(char *fmt, ...)
  291. {
  292. va_list arg;
  293. if(gzok)
  294. fprint(2, "gunzip: %s: corrupted data after byte %lld ignored\n", infile, gzok);
  295. else{
  296. fprint(2, "gunzip: ");
  297. if(infile)
  298. fprint(2, "%s: ", infile);
  299. va_start(arg, fmt);
  300. vfprint(2, fmt, arg);
  301. va_end(arg);
  302. fprint(2, "\n");
  303. if(delfile != nil){
  304. fprint(2, "gunzip: removing output file %s\n", delfile);
  305. remove(delfile);
  306. delfile = nil;
  307. }
  308. }
  309. longjmp(zjmp, 1);
  310. }