gunzip.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. return 0;
  189. }
  190. static void
  191. header(Biobuf *bin, GZHead *h)
  192. {
  193. char *s;
  194. int i, c, flag, ns, nsa;
  195. if(get1(bin) != GZMAGIC1 || get1(bin) != GZMAGIC2)
  196. error("bad gzip file magic");
  197. if(get1(bin) != GZDEFLATE)
  198. error("unknown compression type");
  199. flag = get1(bin);
  200. if(flag & ~(GZFTEXT|GZFEXTRA|GZFNAME|GZFCOMMENT|GZFHCRC))
  201. fprint(2, "gunzip: reserved flags set, data may not be decompressed correctly\n");
  202. /* mod time */
  203. h->mtime = get4(bin);
  204. /* extra flags */
  205. get1(bin);
  206. /* OS type */
  207. get1(bin);
  208. if(flag & GZFEXTRA)
  209. for(i=get1(bin); i>0; i--)
  210. get1(bin);
  211. /* name */
  212. if(flag & GZFNAME){
  213. nsa = 32;
  214. ns = 0;
  215. s = malloc(nsa);
  216. if(s == nil)
  217. error("out of memory");
  218. while((c = get1(bin)) != 0){
  219. s[ns++] = c;
  220. if(ns >= nsa){
  221. nsa += 32;
  222. s = realloc(s, nsa);
  223. if(s == nil)
  224. error("out of memory");
  225. }
  226. }
  227. s[ns] = '\0';
  228. h->file = s;
  229. }else
  230. h->file = strdup("<unnamed file>");
  231. /* comment */
  232. if(flag & GZFCOMMENT)
  233. while(get1(bin) != 0)
  234. ;
  235. /* crc16 */
  236. if(flag & GZFHCRC){
  237. get1(bin);
  238. get1(bin);
  239. }
  240. }
  241. static void
  242. trailer(Biobuf *bin, long wlen)
  243. {
  244. ulong tcrc;
  245. long len;
  246. tcrc = get4(bin);
  247. if(tcrc != crc)
  248. error("crc mismatch");
  249. len = get4(bin);
  250. if(len != wlen)
  251. error("bad output length: expected %lud got %lud", wlen, len);
  252. }
  253. static ulong
  254. get4(Biobuf *b)
  255. {
  256. ulong v;
  257. int i, c;
  258. v = 0;
  259. for(i = 0; i < 4; i++){
  260. c = Bgetc(b);
  261. if(c < 0)
  262. error("unexpected eof reading file information");
  263. v |= c << (i * 8);
  264. }
  265. return v;
  266. }
  267. static int
  268. get1(Biobuf *b)
  269. {
  270. int c;
  271. c = Bgetc(b);
  272. if(c < 0)
  273. error("unexpected eof reading file information");
  274. return c;
  275. }
  276. static int
  277. crcwrite(void *out, void *buf, int n)
  278. {
  279. int fd, nw;
  280. wlen += n;
  281. crc = blockcrc(crctab, crc, buf, n);
  282. fd = (int)(uintptr)out;
  283. if(fd < 0)
  284. return n;
  285. nw = write(fd, buf, n);
  286. if(nw != n)
  287. wbad = 1;
  288. return nw;
  289. }
  290. static void
  291. error(char *fmt, ...)
  292. {
  293. va_list arg;
  294. if(gzok)
  295. fprint(2, "gunzip: %s: corrupted data after byte %lld ignored\n", infile, gzok);
  296. else{
  297. fprint(2, "gunzip: ");
  298. if(infile)
  299. fprint(2, "%s: ", infile);
  300. va_start(arg, fmt);
  301. vfprint(2, fmt, arg);
  302. va_end(arg);
  303. fprint(2, "\n");
  304. if(delfile != nil){
  305. fprint(2, "gunzip: removing output file %s\n", delfile);
  306. remove(delfile);
  307. delfile = nil;
  308. }
  309. }
  310. longjmp(zjmp, 1);
  311. }