1 /* $NetBSD: unbzip2.c,v 1.10 2006/10/03 08:20:03 simonb Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Simon Burge. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 * 38 * $FreeBSD$ 39 */ 40 41 /* This file is #included by gzip.c */ 42 43 static off_t 44 unbzip2(int in, int out, char *pre, size_t prelen, off_t *bytes_in) 45 { 46 int ret, end_of_file; 47 off_t bytes_out = 0; 48 bz_stream bzs; 49 static char *inbuf, *outbuf; 50 51 if (inbuf == NULL) 52 inbuf = malloc(BUFLEN); 53 if (outbuf == NULL) 54 outbuf = malloc(BUFLEN); 55 if (inbuf == NULL || outbuf == NULL) 56 maybe_err("malloc"); 57 58 bzs.bzalloc = NULL; 59 bzs.bzfree = NULL; 60 bzs.opaque = NULL; 61 62 end_of_file = 0; 63 ret = BZ2_bzDecompressInit(&bzs, 0, 0); 64 if (ret != BZ_OK) 65 maybe_errx("bzip2 init"); 66 67 /* Prepend. */ 68 bzs.avail_in = prelen; 69 bzs.next_in = pre; 70 71 if (bytes_in) 72 *bytes_in = prelen; 73 74 while (ret >= BZ_OK && ret != BZ_STREAM_END) { 75 if (bzs.avail_in == 0 && !end_of_file) { 76 ssize_t n; 77 78 n = read(in, inbuf, BUFLEN); 79 if (n < 0) 80 maybe_err("read"); 81 if (n == 0) 82 end_of_file = 1; 83 bzs.next_in = inbuf; 84 bzs.avail_in = n; 85 if (bytes_in) 86 *bytes_in += n; 87 } 88 89 bzs.next_out = outbuf; 90 bzs.avail_out = BUFLEN; 91 ret = BZ2_bzDecompress(&bzs); 92 93 switch (ret) { 94 case BZ_STREAM_END: 95 case BZ_OK: 96 if (ret == BZ_OK && end_of_file) 97 maybe_err("read"); 98 if (!tflag) { 99 ssize_t n; 100 101 n = write(out, outbuf, BUFLEN - bzs.avail_out); 102 if (n < 0) 103 maybe_err("write"); 104 bytes_out += n; 105 } 106 break; 107 108 case BZ_DATA_ERROR: 109 maybe_warnx("bzip2 data integrity error"); 110 break; 111 112 case BZ_DATA_ERROR_MAGIC: 113 maybe_warnx("bzip2 magic number error"); 114 break; 115 116 case BZ_MEM_ERROR: 117 maybe_warnx("bzip2 out of memory"); 118 break; 119 120 } 121 } 122 123 if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK) 124 return (-1); 125 126 return (bytes_out); 127 } 128 129