[BACK]Return to unbzip2.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / gzip

Annotation of src/usr.bin/gzip/unbzip2.c, Revision 1.11

1.11    ! martin      1: /*     $NetBSD: unbzip2.c,v 1.10 2006/10/03 08:20:03 simonb Exp $      */
1.10      simonb      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:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
1.1       mrg        31:
                     32: /* This file is #included by gzip.c */
                     33:
                     34: static off_t
1.4       mrg        35: unbzip2(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
1.1       mrg        36: {
1.4       mrg        37:        int             ret, end_of_file;
1.1       mrg        38:        off_t           bytes_out = 0;
1.3       mrg        39:        bz_stream       bzs;
                     40:        static char     *inbuf, *outbuf;
1.1       mrg        41:
1.6       dsl        42:        if (inbuf == NULL)
                     43:                inbuf = malloc(BUFLEN);
                     44:        if (outbuf == NULL)
                     45:                outbuf = malloc(BUFLEN);
                     46:        if (inbuf == NULL || outbuf == NULL)
1.5       mrg        47:                maybe_err("malloc");
1.3       mrg        48:
                     49:        bzs.bzalloc = NULL;
                     50:        bzs.bzfree = NULL;
                     51:        bzs.opaque = NULL;
                     52:
                     53:        end_of_file = 0;
                     54:        ret = BZ2_bzDecompressInit(&bzs, 0, 0);
                     55:        if (ret != BZ_OK)
1.5       mrg        56:                maybe_errx("bzip2 init");
1.3       mrg        57:
1.4       mrg        58:        /* Prepend. */
                     59:        bzs.avail_in = prelen;
                     60:        bzs.next_in = pre;
                     61:
                     62:        if (bytes_in)
                     63:                *bytes_in = prelen;
1.3       mrg        64:
1.9       mrg        65:        while (ret >= BZ_OK && ret != BZ_STREAM_END) {
1.3       mrg        66:                if (bzs.avail_in == 0 && !end_of_file) {
1.8       mrg        67:                        ssize_t n;
                     68:
1.4       mrg        69:                        n = read(in, inbuf, BUFLEN);
1.3       mrg        70:                        if (n < 0)
1.5       mrg        71:                                maybe_err("read");
1.3       mrg        72:                        if (n == 0)
                     73:                                end_of_file = 1;
                     74:                        bzs.next_in = inbuf;
                     75:                        bzs.avail_in = n;
1.4       mrg        76:                        if (bytes_in)
                     77:                                *bytes_in += n;
                     78:                }
1.3       mrg        79:
                     80:                bzs.next_out = outbuf;
1.4       mrg        81:                bzs.avail_out = BUFLEN;
1.3       mrg        82:                ret = BZ2_bzDecompress(&bzs);
                     83:
                     84:                switch (ret) {
                     85:                case BZ_STREAM_END:
                     86:                case BZ_OK:
                     87:                        if (ret == BZ_OK && end_of_file)
1.5       mrg        88:                                maybe_err("read");
1.3       mrg        89:                        if (!tflag) {
1.8       mrg        90:                                ssize_t n;
                     91:
1.4       mrg        92:                                n = write(out, outbuf, BUFLEN - bzs.avail_out);
1.3       mrg        93:                                if (n < 0)
1.5       mrg        94:                                        maybe_err("write");
1.8       mrg        95:                                bytes_out += n;
1.3       mrg        96:                        }
                     97:                        break;
                     98:
                     99:                case BZ_DATA_ERROR:
1.6       dsl       100:                        maybe_warnx("bzip2 data integrity error");
1.5       mrg       101:                        break;
                    102:
1.3       mrg       103:                case BZ_DATA_ERROR_MAGIC:
1.6       dsl       104:                        maybe_warnx("bzip2 magic number error");
1.5       mrg       105:                        break;
                    106:
1.3       mrg       107:                case BZ_MEM_ERROR:
1.6       dsl       108:                        maybe_warnx("bzip2 out of memory");
1.5       mrg       109:                        break;
                    110:
1.3       mrg       111:                }
                    112:        }
1.1       mrg       113:
1.5       mrg       114:        if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK)
                    115:                return (-1);
1.1       mrg       116:
                    117:        return (bytes_out);
                    118: }

CVSweb <webmaster@jp.NetBSD.org>