Annotation of src/usr.bin/file/compress.c, Revision 1.3
1.3 ! millert 1: /* $OpenBSD: compress.c,v 1.2 1996/06/26 05:32:55 deraadt Exp $ */
! 2:
1.1 deraadt 3: /*
4: * compress routines:
5: * zmagic() - returns 0 if not recognized, uncompresses and prints
6: * information if recognized
7: * uncompress(method, old, n, newch) - uncompress old into new,
8: * using method, return sizeof new
9: */
10: #include <stdio.h>
11: #include <stdlib.h>
12: #include <unistd.h>
13: #include <string.h>
14: #include <sys/wait.h>
15:
16: #include "file.h"
17:
18: static struct {
19: char *magic;
20: int maglen;
21: char *argv[3];
22: int silent;
23: } compr[] = {
24: { "\037\235", 2, { "uncompress", "-c", NULL }, 0 }, /* compressed */
25: { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */
26: { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */
27: { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */
28: /* the standard pack utilities do not accept standard input */
29: { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */
30: };
31:
32: static int ncompr = sizeof(compr) / sizeof(compr[0]);
33:
34:
35: static int uncompress __P((int, const unsigned char *, unsigned char **, int));
36:
37: int
38: zmagic(buf, nbytes)
39: unsigned char *buf;
40: int nbytes;
41: {
42: unsigned char *newbuf;
43: int newsize;
44: int i;
45:
46: for (i = 0; i < ncompr; i++) {
47: if (nbytes < compr[i].maglen)
48: continue;
49: if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
50: break;
51: }
52:
53: if (i == ncompr)
54: return 0;
55:
56: if ((newsize = uncompress(i, buf, &newbuf, nbytes)) != 0) {
57: tryit(newbuf, newsize, 1);
58: free(newbuf);
59: printf(" (");
60: tryit(buf, nbytes, 0);
61: printf(")");
62: }
63: return 1;
64: }
65:
66:
67: static int
68: uncompress(method, old, newch, n)
69: int method;
70: const unsigned char *old;
71: unsigned char **newch;
72: int n;
73: {
74: int fdin[2], fdout[2];
75:
76: if (pipe(fdin) == -1 || pipe(fdout) == -1) {
77: error("cannot create pipe (%s).\n", strerror(errno));
78: /*NOTREACHED*/
79: }
80: switch (fork()) {
81: case 0: /* child */
82: (void) close(0);
83: (void) dup(fdin[0]);
84: (void) close(fdin[0]);
85: (void) close(fdin[1]);
86:
87: (void) close(1);
88: (void) dup(fdout[1]);
89: (void) close(fdout[0]);
90: (void) close(fdout[1]);
91: if (compr[method].silent)
92: (void) close(2);
93:
94: execvp(compr[method].argv[0], compr[method].argv);
95: error("could not execute `%s' (%s).\n",
96: compr[method].argv[0], strerror(errno));
97: /*NOTREACHED*/
98: case -1:
99: error("could not fork (%s).\n", strerror(errno));
100: /*NOTREACHED*/
101:
102: default: /* parent */
103: (void) close(fdin[0]);
104: (void) close(fdout[1]);
105: if (write(fdin[1], old, n) != n) {
106: error("write failed (%s).\n", strerror(errno));
107: /*NOTREACHED*/
108: }
109: (void) close(fdin[1]);
110: if ((*newch = (unsigned char *) malloc(n)) == NULL) {
111: error("out of memory.\n");
112: /*NOTREACHED*/
113: }
114: if ((n = read(fdout[0], *newch, n)) <= 0) {
115: free(*newch);
116: error("read failed (%s).\n", strerror(errno));
117: /*NOTREACHED*/
118: }
119: (void) close(fdout[0]);
120: (void) wait(NULL);
121: return n;
122: }
123: }