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