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