compress.c revision 186675
1/* 2 * Copyright (c) Ian F. Darwin 1986-1995. 3 * Software written by Ian F. Darwin and others; 4 * maintained 1995-present by Christos Zoulas and others. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice immediately at the beginning of the file, without modification, 11 * this list of conditions, and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28/* 29 * compress routines: 30 * zmagic() - returns 0 if not recognized, uncompresses and prints 31 * information if recognized 32 * uncompress(method, old, n, newch) - uncompress old into new, 33 * using method, return sizeof new 34 */ 35#include "file.h" 36#include "magic.h" 37#include <stdio.h> 38#include <stdlib.h> 39#ifdef HAVE_UNISTD_H 40#include <unistd.h> 41#endif 42#include <string.h> 43#include <errno.h> 44#include <sys/types.h> 45#include <sys/ioctl.h> 46#ifdef HAVE_SYS_WAIT_H 47#include <sys/wait.h> 48#endif 49#if defined(HAVE_SYS_TIME_H) 50#include <sys/time.h> 51#endif 52#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ) 53#define BUILTIN_DECOMPRESS 54#include <zlib.h> 55#endif 56 57 58#ifndef lint 59FILE_RCSID("@(#)$File: compress.c,v 1.54 2007/12/02 00:28:10 christos Exp $") 60#endif 61 62private struct { 63 const char *magic; 64 size_t maglen; 65 const char *const argv[3]; 66 int silent; 67} compr[] = { 68 { "\037\235", 2, { "gzip", "-cdq", NULL }, 1 }, /* compressed */ 69 /* Uncompress can get stuck; so use gzip first if we have it 70 * Idea from Damien Clark, thanks! */ 71 { "\037\235", 2, { "uncompress", "-c", NULL }, 1 }, /* compressed */ 72 { "\037\213", 2, { "gzip", "-cdq", NULL }, 1 }, /* gzipped */ 73 { "\037\236", 2, { "gzip", "-cdq", NULL }, 1 }, /* frozen */ 74 { "\037\240", 2, { "gzip", "-cdq", NULL }, 1 }, /* SCO LZH */ 75 /* the standard pack utilities do not accept standard input */ 76 { "\037\036", 2, { "gzip", "-cdq", NULL }, 0 }, /* packed */ 77 { "PK\3\4", 4, { "gzip", "-cdq", NULL }, 1 }, /* pkzipped, */ 78 /* ...only first file examined */ 79 { "BZh", 3, { "bzip2", "-cd", NULL }, 1 }, /* bzip2-ed */ 80}; 81 82private size_t ncompr = sizeof(compr) / sizeof(compr[0]); 83 84#define NODATA ((size_t)~0) 85 86 87private ssize_t swrite(int, const void *, size_t); 88private size_t uncompressbuf(struct magic_set *, int, size_t, 89 const unsigned char *, unsigned char **, size_t); 90#ifdef BUILTIN_DECOMPRESS 91private size_t uncompressgzipped(struct magic_set *, const unsigned char *, 92 unsigned char **, size_t); 93#endif 94 95protected int 96file_zmagic(struct magic_set *ms, int fd, const char *name, 97 const unsigned char *buf, size_t nbytes) 98{ 99 unsigned char *newbuf = NULL; 100 size_t i, nsz; 101 int rv = 0; 102 int mime = ms->flags & MAGIC_MIME; 103 104 if ((ms->flags & MAGIC_COMPRESS) == 0) 105 return 0; 106 107 for (i = 0; i < ncompr; i++) { 108 if (nbytes < compr[i].maglen) 109 continue; 110 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 && 111 (nsz = uncompressbuf(ms, fd, i, buf, &newbuf, 112 nbytes)) != NODATA) { 113 ms->flags &= ~MAGIC_COMPRESS; 114 rv = -1; 115 if (file_buffer(ms, -1, name, newbuf, nsz) == -1) 116 goto error; 117 118 if (mime == MAGIC_MIME || mime == 0) { 119 if (file_printf(ms, mime ? 120 " compressed-encoding=" : " (") == -1) 121 goto error; 122 } 123 124 if ((mime == 0 || mime & MAGIC_MIME_ENCODING) && 125 file_buffer(ms, -1, NULL, buf, nbytes) == -1) 126 goto error; 127 128 if (!mime && file_printf(ms, ")") == -1) 129 goto error; 130 rv = 1; 131 break; 132 } 133 } 134error: 135 if (newbuf) 136 free(newbuf); 137 ms->flags |= MAGIC_COMPRESS; 138 return rv; 139} 140 141/* 142 * `safe' write for sockets and pipes. 143 */ 144private ssize_t 145swrite(int fd, const void *buf, size_t n) 146{ 147 int rv; 148 size_t rn = n; 149 150 do 151 switch (rv = write(fd, buf, n)) { 152 case -1: 153 if (errno == EINTR) 154 continue; 155 return -1; 156 default: 157 n -= rv; 158 buf = ((const char *)buf) + rv; 159 break; 160 } 161 while (n > 0); 162 return rn; 163} 164 165 166/* 167 * `safe' read for sockets and pipes. 168 */ 169protected ssize_t 170sread(int fd, void *buf, size_t n, int canbepipe) 171{ 172 int rv, cnt; 173#ifdef FIONREAD 174 int t = 0; 175#endif 176 size_t rn = n; 177 178 if (fd == STDIN_FILENO) 179 goto nocheck; 180 181#ifdef FIONREAD 182 if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) { 183#ifdef FD_ZERO 184 for (cnt = 0;; cnt++) { 185 fd_set check; 186 struct timeval tout = {0, 100 * 1000}; 187 int selrv; 188 189 FD_ZERO(&check); 190 FD_SET(fd, &check); 191 192 /* 193 * Avoid soft deadlock: do not read if there 194 * is nothing to read from sockets and pipes. 195 */ 196 selrv = select(fd + 1, &check, NULL, NULL, &tout); 197 if (selrv == -1) { 198 if (errno == EINTR || errno == EAGAIN) 199 continue; 200 } else if (selrv == 0 && cnt >= 5) { 201 return 0; 202 } else 203 break; 204 } 205#endif 206 (void)ioctl(fd, FIONREAD, &t); 207 } 208 209 if (t > 0 && (size_t)t < n) { 210 n = t; 211 rn = n; 212 } 213#endif 214 215nocheck: 216 do 217 switch ((rv = read(fd, buf, n))) { 218 case -1: 219 if (errno == EINTR) 220 continue; 221 return -1; 222 case 0: 223 return rn - n; 224 default: 225 n -= rv; 226 buf = ((char *)buf) + rv; 227 break; 228 } 229 while (n > 0); 230 return rn; 231} 232 233protected int 234file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, 235 size_t nbytes) 236{ 237 char buf[4096]; 238 int r, tfd; 239 240 (void)strcpy(buf, "/tmp/file.XXXXXX"); 241#ifndef HAVE_MKSTEMP 242 { 243 char *ptr = mktemp(buf); 244 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600); 245 r = errno; 246 (void)unlink(ptr); 247 errno = r; 248 } 249#else 250 tfd = mkstemp(buf); 251 r = errno; 252 (void)unlink(buf); 253 errno = r; 254#endif 255 if (tfd == -1) { 256 file_error(ms, errno, 257 "cannot create temporary file for pipe copy"); 258 return -1; 259 } 260 261 if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes) 262 r = 1; 263 else { 264 while ((r = sread(fd, buf, sizeof(buf), 1)) > 0) 265 if (swrite(tfd, buf, (size_t)r) != r) 266 break; 267 } 268 269 switch (r) { 270 case -1: 271 file_error(ms, errno, "error copying from pipe to temp file"); 272 return -1; 273 case 0: 274 break; 275 default: 276 file_error(ms, errno, "error while writing to temp file"); 277 return -1; 278 } 279 280 /* 281 * We duplicate the file descriptor, because fclose on a 282 * tmpfile will delete the file, but any open descriptors 283 * can still access the phantom inode. 284 */ 285 if ((fd = dup2(tfd, fd)) == -1) { 286 file_error(ms, errno, "could not dup descriptor for temp file"); 287 return -1; 288 } 289 (void)close(tfd); 290 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 291 file_badseek(ms); 292 return -1; 293 } 294 return fd; 295} 296 297#ifdef BUILTIN_DECOMPRESS 298 299#define FHCRC (1 << 1) 300#define FEXTRA (1 << 2) 301#define FNAME (1 << 3) 302#define FCOMMENT (1 << 4) 303 304private size_t 305uncompressgzipped(struct magic_set *ms, const unsigned char *old, 306 unsigned char **newch, size_t n) 307{ 308 unsigned char flg = old[3]; 309 size_t data_start = 10; 310 z_stream z; 311 int rc; 312 313 if (flg & FEXTRA) { 314 if (data_start+1 >= n) 315 return 0; 316 data_start += 2 + old[data_start] + old[data_start + 1] * 256; 317 } 318 if (flg & FNAME) { 319 while(data_start < n && old[data_start]) 320 data_start++; 321 data_start++; 322 } 323 if(flg & FCOMMENT) { 324 while(data_start < n && old[data_start]) 325 data_start++; 326 data_start++; 327 } 328 if(flg & FHCRC) 329 data_start += 2; 330 331 if (data_start >= n) 332 return 0; 333 if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) { 334 return 0; 335 } 336 337 /* XXX: const castaway, via strchr */ 338 z.next_in = (Bytef *)strchr((const char *)old + data_start, 339 old[data_start]); 340 z.avail_in = n - data_start; 341 z.next_out = *newch; 342 z.avail_out = HOWMANY; 343 z.zalloc = Z_NULL; 344 z.zfree = Z_NULL; 345 z.opaque = Z_NULL; 346 347 rc = inflateInit2(&z, -15); 348 if (rc != Z_OK) { 349 file_error(ms, 0, "zlib: %s", z.msg); 350 return 0; 351 } 352 353 rc = inflate(&z, Z_SYNC_FLUSH); 354 if (rc != Z_OK && rc != Z_STREAM_END) { 355 file_error(ms, 0, "zlib: %s", z.msg); 356 return 0; 357 } 358 359 n = (size_t)z.total_out; 360 (void)inflateEnd(&z); 361 362 /* let's keep the nul-terminate tradition */ 363 (*newch)[n] = '\0'; 364 365 return n; 366} 367#endif 368 369private size_t 370uncompressbuf(struct magic_set *ms, int fd, size_t method, 371 const unsigned char *old, unsigned char **newch, size_t n) 372{ 373 int fdin[2], fdout[2]; 374 int r; 375 376#ifdef BUILTIN_DECOMPRESS 377 if (method == 2) 378 return uncompressgzipped(ms, old, newch, n); 379#endif 380 (void)fflush(stdout); 381 (void)fflush(stderr); 382 383 if ((fd != -1 && pipe(fdin) == -1) || pipe(fdout) == -1) { 384 file_error(ms, errno, "cannot create pipe"); 385 return NODATA; 386 } 387 switch (fork()) { 388 case 0: /* child */ 389 (void) close(0); 390 if (fd != -1) { 391 (void) dup(fd); 392 (void) lseek(0, (off_t)0, SEEK_SET); 393 } else { 394 (void) dup(fdin[0]); 395 (void) close(fdin[0]); 396 (void) close(fdin[1]); 397 } 398 399 (void) close(1); 400 (void) dup(fdout[1]); 401 (void) close(fdout[0]); 402 (void) close(fdout[1]); 403#ifndef DEBUG 404 if (compr[method].silent) 405 (void)close(2); 406#endif 407 408 (void)execvp(compr[method].argv[0], 409 (char *const *)(intptr_t)compr[method].argv); 410#ifdef DEBUG 411 (void)fprintf(stderr, "exec `%s' failed (%s)\n", 412 compr[method].argv[0], strerror(errno)); 413#endif 414 exit(1); 415 /*NOTREACHED*/ 416 case -1: 417 file_error(ms, errno, "could not fork"); 418 return NODATA; 419 420 default: /* parent */ 421 (void) close(fdout[1]); 422 if (fd == -1) { 423 (void) close(fdin[0]); 424 /* 425 * fork again, to avoid blocking because both 426 * pipes filled 427 */ 428 switch (fork()) { 429 case 0: /* child */ 430 (void)close(fdout[0]); 431 if (swrite(fdin[1], old, n) != (ssize_t)n) { 432#ifdef DEBUG 433 (void)fprintf(stderr, 434 "Write failed (%s)\n", 435 strerror(errno)); 436#endif 437 exit(1); 438 } 439 exit(0); 440 /*NOTREACHED*/ 441 442 case -1: 443#ifdef DEBUG 444 (void)fprintf(stderr, "Fork failed (%s)\n", 445 strerror(errno)); 446#endif 447 exit(1); 448 /*NOTREACHED*/ 449 450 default: /* parent */ 451 break; 452 } 453 (void) close(fdin[1]); 454 fdin[1] = -1; 455 } 456 457 if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) { 458#ifdef DEBUG 459 (void)fprintf(stderr, "Malloc failed (%s)\n", 460 strerror(errno)); 461#endif 462 n = 0; 463 goto err; 464 } 465 if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) { 466#ifdef DEBUG 467 (void)fprintf(stderr, "Read failed (%s)\n", 468 strerror(errno)); 469#endif 470 free(*newch); 471 n = 0; 472 newch[0] = '\0'; 473 goto err; 474 } else { 475 n = r; 476 } 477 /* NUL terminate, as every buffer is handled here. */ 478 (*newch)[n] = '\0'; 479err: 480 if (fdin[1] != -1) 481 (void) close(fdin[1]); 482 (void) close(fdout[0]); 483#ifdef WNOHANG 484 while (waitpid(-1, NULL, WNOHANG) != -1) 485 continue; 486#else 487 (void)wait(NULL); 488#endif 489 return n; 490 } 491} 492