compress.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. #ifndef lint
  37. FILE_RCSID("@(#)$File: compress.c,v 1.113 2018/10/15 16:29:16 christos Exp $")
  38. #endif
  39. #include "magic.h"
  40. #include <stdlib.h>
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h>
  43. #endif
  44. #include <string.h>
  45. #include <errno.h>
  46. #include <ctype.h>
  47. #include <stdarg.h>
  48. #include <signal.h>
  49. #ifndef HAVE_SIG_T
  50. typedef void (*sig_t)(int);
  51. #endif /* HAVE_SIG_T */
  52. #if !defined(__MINGW32__) && !defined(WIN32)
  53. #include <sys/ioctl.h>
  54. #endif
  55. #ifdef HAVE_SYS_WAIT_H
  56. #include <sys/wait.h>
  57. #endif
  58. #if defined(HAVE_SYS_TIME_H)
  59. #include <sys/time.h>
  60. #endif
  61. #if defined(HAVE_ZLIB_H) && defined(ZLIBSUPPORT)
  62. #define BUILTIN_DECOMPRESS
  63. #include <zlib.h>
  64. #endif
  65. #if defined(HAVE_BZLIB_H)
  66. #define BUILTIN_BZLIB
  67. #include <bzlib.h>
  68. #endif
  69. #ifdef DEBUG
  70. int tty = -1;
  71. #define DPRINTF(...) do { \
  72. if (tty == -1) \
  73. tty = open("/dev/tty", O_RDWR); \
  74. if (tty == -1) \
  75. abort(); \
  76. dprintf(tty, __VA_ARGS__); \
  77. } while (/*CONSTCOND*/0)
  78. #else
  79. #define DPRINTF(...)
  80. #endif
  81. #ifdef ZLIBSUPPORT
  82. /*
  83. * The following python code is not really used because ZLIBSUPPORT is only
  84. * defined if we have a built-in zlib, and the built-in zlib handles that.
  85. * That is not true for android where we have zlib.h and not -lz.
  86. */
  87. static const char zlibcode[] =
  88. "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))";
  89. static const char *zlib_args[] = { "python", "-c", zlibcode, NULL };
  90. static int
  91. zlibcmp(const unsigned char *buf)
  92. {
  93. unsigned short x = 1;
  94. unsigned char *s = CAST(unsigned char *, CAST(void *, &x));
  95. if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0)
  96. return 0;
  97. if (s[0] != 1) /* endianness test */
  98. x = buf[0] | (buf[1] << 8);
  99. else
  100. x = buf[1] | (buf[0] << 8);
  101. if (x % 31)
  102. return 0;
  103. return 1;
  104. }
  105. #endif
  106. #define gzip_flags "-cd"
  107. #define lrzip_flags "-do"
  108. #define lzip_flags gzip_flags
  109. static const char *gzip_args[] = {
  110. "gzip", gzip_flags, NULL
  111. };
  112. static const char *uncompress_args[] = {
  113. "uncompress", "-c", NULL
  114. };
  115. static const char *bzip2_args[] = {
  116. "bzip2", "-cd", NULL
  117. };
  118. static const char *lzip_args[] = {
  119. "lzip", lzip_flags, NULL
  120. };
  121. static const char *xz_args[] = {
  122. "xz", "-cd", NULL
  123. };
  124. static const char *lrzip_args[] = {
  125. "lrzip", lrzip_flags, NULL
  126. };
  127. static const char *lz4_args[] = {
  128. "lz4", "-cd", NULL
  129. };
  130. static const char *zstd_args[] = {
  131. "zstd", "-cd", NULL
  132. };
  133. #define do_zlib NULL
  134. #define do_bzlib NULL
  135. private const struct {
  136. const void *magic;
  137. size_t maglen;
  138. const char **argv;
  139. void *unused;
  140. } compr[] = {
  141. { "\037\235", 2, gzip_args, NULL }, /* compressed */
  142. /* Uncompress can get stuck; so use gzip first if we have it
  143. * Idea from Damien Clark, thanks! */
  144. { "\037\235", 2, uncompress_args, NULL }, /* compressed */
  145. { "\037\213", 2, gzip_args, do_zlib }, /* gzipped */
  146. { "\037\236", 2, gzip_args, NULL }, /* frozen */
  147. { "\037\240", 2, gzip_args, NULL }, /* SCO LZH */
  148. /* the standard pack utilities do not accept standard input */
  149. { "\037\036", 2, gzip_args, NULL }, /* packed */
  150. { "PK\3\4", 4, gzip_args, NULL }, /* pkzipped, */
  151. /* ...only first file examined */
  152. { "BZh", 3, bzip2_args, do_bzlib }, /* bzip2-ed */
  153. { "LZIP", 4, lzip_args, NULL }, /* lzip-ed */
  154. { "\3757zXZ\0", 6, xz_args, NULL }, /* XZ Utils */
  155. { "LRZI", 4, lrzip_args, NULL }, /* LRZIP */
  156. { "\004\"M\030",4, lz4_args, NULL }, /* LZ4 */
  157. { "\x28\xB5\x2F\xFD", 4, zstd_args, NULL }, /* zstd */
  158. #ifdef ZLIBSUPPORT
  159. { RCAST(const void *, zlibcmp), 0, zlib_args, NULL }, /* zlib */
  160. #endif
  161. };
  162. #define OKDATA 0
  163. #define NODATA 1
  164. #define ERRDATA 2
  165. private ssize_t swrite(int, const void *, size_t);
  166. #if HAVE_FORK
  167. private size_t ncompr = sizeof(compr) / sizeof(compr[0]);
  168. private int uncompressbuf(int, size_t, size_t, const unsigned char *,
  169. unsigned char **, size_t *);
  170. #ifdef BUILTIN_DECOMPRESS
  171. private int uncompresszlib(const unsigned char *, unsigned char **, size_t,
  172. size_t *, int);
  173. private int uncompressgzipped(const unsigned char *, unsigned char **, size_t,
  174. size_t *);
  175. #endif
  176. #ifdef BUILTIN_BZLIB
  177. private int uncompressbzlib(const unsigned char *, unsigned char **, size_t,
  178. size_t *, int);
  179. #endif
  180. static int makeerror(unsigned char **, size_t *, const char *, ...)
  181. __attribute__((__format__(__printf__, 3, 4)));
  182. private const char *methodname(size_t);
  183. private int
  184. format_decompression_error(struct magic_set *ms, size_t i, unsigned char *buf)
  185. {
  186. unsigned char *p;
  187. int mime = ms->flags & MAGIC_MIME;
  188. if (!mime)
  189. return file_printf(ms, "ERROR:[%s: %s]", methodname(i), buf);
  190. for (p = buf; *p; p++)
  191. if (!isalnum(*p))
  192. *p = '-';
  193. return file_printf(ms, "application/x-decompression-error-%s-%s",
  194. methodname(i), buf);
  195. }
  196. protected int
  197. file_zmagic(struct magic_set *ms, const struct buffer *b, const char *name)
  198. {
  199. unsigned char *newbuf = NULL;
  200. size_t i, nsz;
  201. char *rbuf;
  202. file_pushbuf_t *pb;
  203. int urv, prv, rv = 0;
  204. int mime = ms->flags & MAGIC_MIME;
  205. int fd = b->fd;
  206. const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
  207. size_t nbytes = b->flen;
  208. sig_t osigpipe;
  209. if ((ms->flags & MAGIC_COMPRESS) == 0)
  210. return 0;
  211. osigpipe = signal(SIGPIPE, SIG_IGN);
  212. for (i = 0; i < ncompr; i++) {
  213. int zm;
  214. if (nbytes < compr[i].maglen)
  215. continue;
  216. #ifdef ZLIBSUPPORT
  217. if (compr[i].maglen == 0)
  218. zm = (RCAST(int (*)(const unsigned char *),
  219. CCAST(void *, compr[i].magic)))(buf);
  220. else
  221. #endif
  222. zm = memcmp(buf, compr[i].magic, compr[i].maglen) == 0;
  223. if (!zm)
  224. continue;
  225. nsz = nbytes;
  226. urv = uncompressbuf(fd, ms->bytes_max, i, buf, &newbuf, &nsz);
  227. DPRINTF("uncompressbuf = %d, %s, %" SIZE_T_FORMAT "u\n", urv,
  228. (char *)newbuf, nsz);
  229. switch (urv) {
  230. case OKDATA:
  231. case ERRDATA:
  232. ms->flags &= ~MAGIC_COMPRESS;
  233. if (urv == ERRDATA)
  234. prv = format_decompression_error(ms, i, newbuf);
  235. else
  236. prv = file_buffer(ms, -1, name, newbuf, nsz);
  237. if (prv == -1)
  238. goto error;
  239. rv = 1;
  240. if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0)
  241. goto out;
  242. if (mime != MAGIC_MIME && mime != 0)
  243. goto out;
  244. if ((file_printf(ms,
  245. mime ? " compressed-encoding=" : " (")) == -1)
  246. goto error;
  247. if ((pb = file_push_buffer(ms)) == NULL)
  248. goto error;
  249. /*
  250. * XXX: If file_buffer fails here, we overwrite
  251. * the compressed text. FIXME.
  252. */
  253. if (file_buffer(ms, -1, NULL, buf, nbytes) == -1) {
  254. if (file_pop_buffer(ms, pb) != NULL)
  255. abort();
  256. goto error;
  257. }
  258. if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
  259. if (file_printf(ms, "%s", rbuf) == -1) {
  260. free(rbuf);
  261. goto error;
  262. }
  263. free(rbuf);
  264. }
  265. if (!mime && file_printf(ms, ")") == -1)
  266. goto error;
  267. /*FALLTHROUGH*/
  268. case NODATA:
  269. break;
  270. default:
  271. abort();
  272. /*NOTREACHED*/
  273. error:
  274. rv = -1;
  275. break;
  276. }
  277. }
  278. out:
  279. DPRINTF("rv = %d\n", rv);
  280. (void)signal(SIGPIPE, osigpipe);
  281. free(newbuf);
  282. ms->flags |= MAGIC_COMPRESS;
  283. DPRINTF("Zmagic returns %d\n", rv);
  284. return rv;
  285. }
  286. #endif
  287. /*
  288. * `safe' write for sockets and pipes.
  289. */
  290. private ssize_t
  291. swrite(int fd, const void *buf, size_t n)
  292. {
  293. ssize_t rv;
  294. size_t rn = n;
  295. do
  296. switch (rv = write(fd, buf, n)) {
  297. case -1:
  298. if (errno == EINTR)
  299. continue;
  300. return -1;
  301. default:
  302. n -= rv;
  303. buf = CAST(const char *, buf) + rv;
  304. break;
  305. }
  306. while (n > 0);
  307. return rn;
  308. }
  309. /*
  310. * `safe' read for sockets and pipes.
  311. */
  312. protected ssize_t
  313. sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__)))
  314. {
  315. ssize_t rv;
  316. #ifdef FIONREAD
  317. int t = 0;
  318. #endif
  319. size_t rn = n;
  320. if (fd == STDIN_FILENO)
  321. goto nocheck;
  322. #ifdef FIONREAD
  323. if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1 || t == 0)) {
  324. #ifdef FD_ZERO
  325. ssize_t cnt;
  326. for (cnt = 0;; cnt++) {
  327. fd_set check;
  328. struct timeval tout = {0, 100 * 1000};
  329. int selrv;
  330. FD_ZERO(&check);
  331. FD_SET(fd, &check);
  332. /*
  333. * Avoid soft deadlock: do not read if there
  334. * is nothing to read from sockets and pipes.
  335. */
  336. selrv = select(fd + 1, &check, NULL, NULL, &tout);
  337. if (selrv == -1) {
  338. if (errno == EINTR || errno == EAGAIN)
  339. continue;
  340. } else if (selrv == 0 && cnt >= 5) {
  341. return 0;
  342. } else
  343. break;
  344. }
  345. #endif
  346. (void)ioctl(fd, FIONREAD, &t);
  347. }
  348. if (t > 0 && (size_t)t < n) {
  349. n = t;
  350. rn = n;
  351. }
  352. #endif
  353. nocheck:
  354. do
  355. switch ((rv = read(fd, buf, n))) {
  356. case -1:
  357. if (errno == EINTR)
  358. continue;
  359. return -1;
  360. case 0:
  361. return rn - n;
  362. default:
  363. n -= rv;
  364. buf = CAST(char *, CCAST(void *, buf)) + rv;
  365. break;
  366. }
  367. while (n > 0);
  368. return rn;
  369. }
  370. protected int
  371. file_pipe2file(struct magic_set *ms, int fd, const void *startbuf,
  372. size_t nbytes)
  373. {
  374. char buf[4096];
  375. ssize_t r;
  376. int tfd;
  377. (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
  378. #ifndef HAVE_MKSTEMP
  379. {
  380. char *ptr = mktemp(buf);
  381. tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
  382. r = errno;
  383. (void)unlink(ptr);
  384. errno = r;
  385. }
  386. #else
  387. {
  388. int te;
  389. int ou = umask(0);
  390. tfd = mkstemp(buf);
  391. (void)umask(ou);
  392. te = errno;
  393. (void)unlink(buf);
  394. errno = te;
  395. }
  396. #endif
  397. if (tfd == -1) {
  398. file_error(ms, errno,
  399. "cannot create temporary file for pipe copy");
  400. return -1;
  401. }
  402. if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
  403. r = 1;
  404. else {
  405. while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
  406. if (swrite(tfd, buf, (size_t)r) != r)
  407. break;
  408. }
  409. switch (r) {
  410. case -1:
  411. file_error(ms, errno, "error copying from pipe to temp file");
  412. return -1;
  413. case 0:
  414. break;
  415. default:
  416. file_error(ms, errno, "error while writing to temp file");
  417. return -1;
  418. }
  419. /*
  420. * We duplicate the file descriptor, because fclose on a
  421. * tmpfile will delete the file, but any open descriptors
  422. * can still access the phantom inode.
  423. */
  424. if ((fd = dup2(tfd, fd)) == -1) {
  425. file_error(ms, errno, "could not dup descriptor for temp file");
  426. return -1;
  427. }
  428. (void)close(tfd);
  429. if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
  430. file_badseek(ms);
  431. return -1;
  432. }
  433. return fd;
  434. }
  435. #if HAVE_FORK
  436. #ifdef BUILTIN_DECOMPRESS
  437. #define FHCRC (1 << 1)
  438. #define FEXTRA (1 << 2)
  439. #define FNAME (1 << 3)
  440. #define FCOMMENT (1 << 4)
  441. private int
  442. uncompressgzipped(const unsigned char *old, unsigned char **newch,
  443. size_t bytes_max, size_t *n)
  444. {
  445. unsigned char flg = old[3];
  446. size_t data_start = 10;
  447. if (flg & FEXTRA) {
  448. if (data_start + 1 >= *n)
  449. goto err;
  450. data_start += 2 + old[data_start] + old[data_start + 1] * 256;
  451. }
  452. if (flg & FNAME) {
  453. while(data_start < *n && old[data_start])
  454. data_start++;
  455. data_start++;
  456. }
  457. if (flg & FCOMMENT) {
  458. while(data_start < *n && old[data_start])
  459. data_start++;
  460. data_start++;
  461. }
  462. if (flg & FHCRC)
  463. data_start += 2;
  464. if (data_start >= *n)
  465. goto err;
  466. *n -= data_start;
  467. old += data_start;
  468. return uncompresszlib(old, newch, bytes_max, n, 0);
  469. err:
  470. return makeerror(newch, n, "File too short");
  471. }
  472. private int
  473. uncompresszlib(const unsigned char *old, unsigned char **newch,
  474. size_t bytes_max, size_t *n, int zlib)
  475. {
  476. int rc;
  477. z_stream z;
  478. if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL)
  479. return makeerror(newch, n, "No buffer, %s", strerror(errno));
  480. z.next_in = CCAST(Bytef *, old);
  481. z.avail_in = CAST(uint32_t, *n);
  482. z.next_out = *newch;
  483. z.avail_out = CAST(unsigned int, bytes_max);
  484. z.zalloc = Z_NULL;
  485. z.zfree = Z_NULL;
  486. z.opaque = Z_NULL;
  487. /* LINTED bug in header macro */
  488. rc = zlib ? inflateInit(&z) : inflateInit2(&z, -15);
  489. if (rc != Z_OK)
  490. goto err;
  491. rc = inflate(&z, Z_SYNC_FLUSH);
  492. if (rc != Z_OK && rc != Z_STREAM_END)
  493. goto err;
  494. *n = (size_t)z.total_out;
  495. rc = inflateEnd(&z);
  496. if (rc != Z_OK)
  497. goto err;
  498. /* let's keep the nul-terminate tradition */
  499. (*newch)[*n] = '\0';
  500. return OKDATA;
  501. err:
  502. strlcpy((char *)*newch, z.msg ? z.msg : zError(rc), bytes_max);
  503. *n = strlen((char *)*newch);
  504. return ERRDATA;
  505. }
  506. #endif
  507. static int
  508. makeerror(unsigned char **buf, size_t *len, const char *fmt, ...)
  509. {
  510. char *msg;
  511. va_list ap;
  512. int rv;
  513. va_start(ap, fmt);
  514. rv = vasprintf(&msg, fmt, ap);
  515. va_end(ap);
  516. if (rv < 0) {
  517. *buf = NULL;
  518. *len = 0;
  519. return NODATA;
  520. }
  521. *buf = (unsigned char *)msg;
  522. *len = strlen(msg);
  523. return ERRDATA;
  524. }
  525. static void
  526. closefd(int *fd, size_t i)
  527. {
  528. if (fd[i] == -1)
  529. return;
  530. (void) close(fd[i]);
  531. fd[i] = -1;
  532. }
  533. static void
  534. closep(int *fd)
  535. {
  536. size_t i;
  537. for (i = 0; i < 2; i++)
  538. closefd(fd, i);
  539. }
  540. static void
  541. copydesc(int i, int *fd)
  542. {
  543. int j = fd[i == STDIN_FILENO ? 0 : 1];
  544. if (j == i)
  545. return;
  546. if (dup2(j, i) == -1) {
  547. DPRINTF("dup(%d, %d) failed (%s)\n", j, i, strerror(errno));
  548. exit(1);
  549. }
  550. closep(fd);
  551. }
  552. static void
  553. writechild(int fdp[3][2], const void *old, size_t n)
  554. {
  555. int status;
  556. closefd(fdp[STDIN_FILENO], 0);
  557. /*
  558. * fork again, to avoid blocking because both
  559. * pipes filled
  560. */
  561. switch (fork()) {
  562. case 0: /* child */
  563. closefd(fdp[STDOUT_FILENO], 0);
  564. if (swrite(fdp[STDIN_FILENO][1], old, n) != (ssize_t)n) {
  565. DPRINTF("Write failed (%s)\n", strerror(errno));
  566. exit(1);
  567. }
  568. exit(0);
  569. /*NOTREACHED*/
  570. case -1:
  571. DPRINTF("Fork failed (%s)\n", strerror(errno));
  572. exit(1);
  573. /*NOTREACHED*/
  574. default: /* parent */
  575. if (wait(&status) == -1) {
  576. DPRINTF("Wait failed (%s)\n", strerror(errno));
  577. exit(1);
  578. }
  579. DPRINTF("Grandchild wait return %#x\n", status);
  580. }
  581. closefd(fdp[STDIN_FILENO], 1);
  582. }
  583. static ssize_t
  584. filter_error(unsigned char *ubuf, ssize_t n)
  585. {
  586. char *p;
  587. char *buf;
  588. ubuf[n] = '\0';
  589. buf = (char *)ubuf;
  590. while (isspace((unsigned char)*buf))
  591. buf++;
  592. DPRINTF("Filter error[[[%s]]]\n", buf);
  593. if ((p = strchr((char *)buf, '\n')) != NULL)
  594. *p = '\0';
  595. if ((p = strchr((char *)buf, ';')) != NULL)
  596. *p = '\0';
  597. if ((p = strrchr((char *)buf, ':')) != NULL) {
  598. ++p;
  599. while (isspace((unsigned char)*p))
  600. p++;
  601. n = strlen(p);
  602. memmove(ubuf, p, CAST(size_t, n + 1));
  603. }
  604. DPRINTF("Filter error after[[[%s]]]\n", (char *)ubuf);
  605. if (islower(*ubuf))
  606. *ubuf = toupper(*ubuf);
  607. return n;
  608. }
  609. private const char *
  610. methodname(size_t method)
  611. {
  612. #ifdef BUILTIN_DECOMPRESS
  613. /* FIXME: This doesn't cope with bzip2 */
  614. if (method == 2 || compr[method].maglen == 0)
  615. return "zlib";
  616. #endif
  617. return compr[method].argv[0];
  618. }
  619. private int
  620. uncompressbuf(int fd, size_t bytes_max, size_t method, const unsigned char *old,
  621. unsigned char **newch, size_t* n)
  622. {
  623. int fdp[3][2];
  624. int status, rv;
  625. size_t i;
  626. ssize_t r;
  627. #ifdef BUILTIN_DECOMPRESS
  628. /* FIXME: This doesn't cope with bzip2 */
  629. if (method == 2)
  630. return uncompressgzipped(old, newch, bytes_max, n);
  631. if (compr[method].maglen == 0)
  632. return uncompresszlib(old, newch, bytes_max, n, 1);
  633. #endif
  634. (void)fflush(stdout);
  635. (void)fflush(stderr);
  636. for (i = 0; i < __arraycount(fdp); i++)
  637. fdp[i][0] = fdp[i][1] = -1;
  638. if ((fd == -1 && pipe(fdp[STDIN_FILENO]) == -1) ||
  639. pipe(fdp[STDOUT_FILENO]) == -1 || pipe(fdp[STDERR_FILENO]) == -1) {
  640. closep(fdp[STDIN_FILENO]);
  641. closep(fdp[STDOUT_FILENO]);
  642. return makeerror(newch, n, "Cannot create pipe, %s",
  643. strerror(errno));
  644. }
  645. switch (fork()) {
  646. case 0: /* child */
  647. if (fd != -1) {
  648. fdp[STDIN_FILENO][0] = fd;
  649. (void) lseek(fd, (off_t)0, SEEK_SET);
  650. }
  651. for (i = 0; i < __arraycount(fdp); i++)
  652. copydesc(CAST(int, i), fdp[i]);
  653. (void)execvp(compr[method].argv[0],
  654. (char *const *)(intptr_t)compr[method].argv);
  655. dprintf(STDERR_FILENO, "exec `%s' failed, %s",
  656. compr[method].argv[0], strerror(errno));
  657. exit(1);
  658. /*NOTREACHED*/
  659. case -1:
  660. return makeerror(newch, n, "Cannot fork, %s",
  661. strerror(errno));
  662. default: /* parent */
  663. for (i = 1; i < __arraycount(fdp); i++)
  664. closefd(fdp[i], 1);
  665. /* Write the buffer data to the child, if we don't have fd */
  666. if (fd == -1)
  667. writechild(fdp, old, *n);
  668. *newch = CAST(unsigned char *, malloc(bytes_max + 1));
  669. if (*newch == NULL) {
  670. rv = makeerror(newch, n, "No buffer, %s",
  671. strerror(errno));
  672. goto err;
  673. }
  674. rv = OKDATA;
  675. if ((r = sread(fdp[STDOUT_FILENO][0], *newch, bytes_max, 0)) > 0)
  676. break;
  677. DPRINTF("Read stdout failed %d (%s)\n", fdp[STDOUT_FILENO][0],
  678. r != -1 ? strerror(errno) : "no data");
  679. rv = ERRDATA;
  680. if (r == 0 &&
  681. (r = sread(fdp[STDERR_FILENO][0], *newch, bytes_max, 0)) > 0)
  682. {
  683. r = filter_error(*newch, r);
  684. break;
  685. }
  686. free(*newch);
  687. if (r == 0)
  688. rv = makeerror(newch, n, "Read failed, %s",
  689. strerror(errno));
  690. else
  691. rv = makeerror(newch, n, "No data");
  692. goto err;
  693. }
  694. *n = r;
  695. /* NUL terminate, as every buffer is handled here. */
  696. (*newch)[*n] = '\0';
  697. err:
  698. closefd(fdp[STDIN_FILENO], 1);
  699. closefd(fdp[STDOUT_FILENO], 0);
  700. closefd(fdp[STDERR_FILENO], 0);
  701. if (wait(&status) == -1) {
  702. free(*newch);
  703. rv = makeerror(newch, n, "Wait failed, %s", strerror(errno));
  704. DPRINTF("Child wait return %#x\n", status);
  705. } else if (!WIFEXITED(status)) {
  706. DPRINTF("Child not exited (%#x)\n", status);
  707. } else if (WEXITSTATUS(status) != 0) {
  708. DPRINTF("Child exited (%#x)\n", WEXITSTATUS(status));
  709. }
  710. closefd(fdp[STDIN_FILENO], 0);
  711. DPRINTF("Returning %p n=%" SIZE_T_FORMAT "u rv=%d\n", *newch, *n, rv);
  712. return rv;
  713. }
  714. #endif