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