]>
git.saurik.com Git - apple/file_cmds.git/blob - file/compress.c
4088f2d5d21b628fab3e6fb8b4bbde827aa6e2fb
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: compress.c,v 1.3 1997/02/09 23:58:19 millert Exp $ */
28 * zmagic() - returns 0 if not recognized, uncompresses and prints
29 * information if recognized
30 * uncompress(method, old, n, newch) - uncompress old into new,
31 * using method, return sizeof new
47 { "\037\235", 2, { "uncompress", "-c", NULL
}, 0 }, /* compressed */
48 { "\037\213", 2, { "gzip", "-cdq", NULL
}, 1 }, /* gzipped */
49 { "\037\236", 2, { "gzip", "-cdq", NULL
}, 1 }, /* frozen */
50 { "\037\240", 2, { "gzip", "-cdq", NULL
}, 1 }, /* SCO LZH */
51 /* the standard pack utilities do not accept standard input */
52 { "\037\036", 2, { "gzip", "-cdq", NULL
}, 0 }, /* packed */
55 static int ncompr
= sizeof(compr
) / sizeof(compr
[0]);
58 static int uncompress
__P((int, const unsigned char *, unsigned char **, int));
65 unsigned char *newbuf
;
69 for (i
= 0; i
< ncompr
; i
++) {
70 if (nbytes
< compr
[i
].maglen
)
72 if (memcmp(buf
, compr
[i
].magic
, compr
[i
].maglen
) == 0)
79 if ((newsize
= uncompress(i
, buf
, &newbuf
, nbytes
)) != 0) {
80 tryit(newbuf
, newsize
, 1);
83 tryit(buf
, nbytes
, 0);
91 uncompress(method
, old
, newch
, n
)
93 const unsigned char *old
;
94 unsigned char **newch
;
97 int fdin
[2], fdout
[2];
99 if (pipe(fdin
) == -1 || pipe(fdout
) == -1) {
100 error("cannot create pipe (%s).\n", strerror(errno
));
107 (void) close(fdin
[0]);
108 (void) close(fdin
[1]);
111 (void) dup(fdout
[1]);
112 (void) close(fdout
[0]);
113 (void) close(fdout
[1]);
114 if (compr
[method
].silent
)
117 execvp(compr
[method
].argv
[0], compr
[method
].argv
);
118 error("could not execute `%s' (%s).\n",
119 compr
[method
].argv
[0], strerror(errno
));
122 error("could not fork (%s).\n", strerror(errno
));
125 default: /* parent */
126 (void) close(fdin
[0]);
127 (void) close(fdout
[1]);
128 if (write(fdin
[1], old
, n
) != n
) {
129 error("write failed (%s).\n", strerror(errno
));
132 (void) close(fdin
[1]);
133 if ((*newch
= (unsigned char *) malloc(n
)) == NULL
) {
134 error("out of memory.\n");
137 if ((n
= read(fdout
[0], *newch
, n
)) <= 0) {
139 error("read failed (%s).\n", strerror(errno
));
142 (void) close(fdout
[0]);