Blob


1 /* THIS FILE HAS BEEN MODIFIED -- rsc split bzlib.c into bzlib.c,
2 bzlibcompress.c, bzlibdecompress.c, bzlibread.c, bzlibwrite.c
3 */
4 /*-------------------------------------------------------------*/
5 /*--- Library top-level functions. ---*/
6 /*--- bzlib.c ---*/
7 /*-------------------------------------------------------------*/
9 /*--
10 This file is a part of bzip2 and/or libbzip2, a program and
11 library for lossless, block-sorting data compression.
13 Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 2. The origin of this software must not be misrepresented; you must
23 not claim that you wrote the original software. If you use this
24 software in a product, an acknowledgment in the product
25 documentation would be appreciated but is not required.
27 3. Altered source versions must be plainly marked as such, and must
28 not be misrepresented as being the original software.
30 4. The name of the author may not be used to endorse or promote
31 products derived from this software without specific prior written
32 permission.
34 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
35 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
38 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 Julian Seward, Cambridge, UK.
47 jseward@acm.org
48 bzip2/libbzip2 version 1.0 of 21 March 2000
50 This program is based on (at least) the work of:
51 Mike Burrows
52 David Wheeler
53 Peter Fenwick
54 Alistair Moffat
55 Radford Neal
56 Ian H. Witten
57 Robert Sedgewick
58 Jon L. Bentley
60 For more information on these sources, see the manual.
61 --*/
63 /*--
64 CHANGES
65 ~~~~~~~
66 0.9.0 -- original version.
68 0.9.0a/b -- no changes in this file.
70 0.9.0c
71 * made zero-length BZ_FLUSH work correctly in bzCompress().
72 * fixed bzWrite/bzRead to ignore zero-length requests.
73 * fixed bzread to correctly handle read requests after EOF.
74 * wrong parameter order in call to bzDecompressInit in
75 bzBuffToBuffDecompress. Fixed.
76 --*/
78 #include "os.h"
79 #include "bzlib.h"
80 #include "bzlib_private.h"
81 #include "bzlib_stdio.h"
82 #include "bzlib_stdio_private.h"
84 /*---------------------------------------------------*/
85 BZFILE* BZ_API(BZ2_bzReadOpen)
86 ( int* bzerror,
87 FILE* f,
88 int verbosity,
89 int small,
90 void* unused,
91 int nUnused )
92 {
93 bzFile* bzf = NULL;
94 int ret;
96 BZ_SETERR(BZ_OK);
98 if (f == NULL ||
99 (small != 0 && small != 1) ||
100 (verbosity < 0 || verbosity > 4) ||
101 (unused == NULL && nUnused != 0) ||
102 (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
103 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
105 if (ferror(f))
106 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
108 bzf = malloc ( sizeof(bzFile) );
109 if (bzf == NULL)
110 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
112 BZ_SETERR(BZ_OK);
114 bzf->initialisedOk = False;
115 bzf->handle = f;
116 bzf->bufN = 0;
117 bzf->writing = False;
118 bzf->strm.bzalloc = NULL;
119 bzf->strm.bzfree = NULL;
120 bzf->strm.opaque = NULL;
122 while (nUnused > 0) {
123 bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
124 unused = ((void*)( 1 + ((UChar*)(unused)) ));
125 nUnused--;
128 ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
129 if (ret != BZ_OK)
130 { BZ_SETERR(ret); free(bzf); return NULL; };
132 bzf->strm.avail_in = bzf->bufN;
133 bzf->strm.next_in = bzf->buf;
135 bzf->initialisedOk = True;
136 return bzf;
140 /*---------------------------------------------------*/
141 void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
143 bzFile* bzf = (bzFile*)b;
145 BZ_SETERR(BZ_OK);
146 if (bzf == NULL)
147 { BZ_SETERR(BZ_OK); return; };
149 if (bzf->writing)
150 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
152 if (bzf->initialisedOk)
153 (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
154 free ( bzf );
158 /*---------------------------------------------------*/
159 int BZ_API(BZ2_bzRead)
160 ( int* bzerror,
161 BZFILE* b,
162 void* buf,
163 int len )
165 Int32 n, ret;
166 bzFile* bzf = (bzFile*)b;
168 BZ_SETERR(BZ_OK);
170 if (bzf == NULL || buf == NULL || len < 0)
171 { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
173 if (bzf->writing)
174 { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
176 if (len == 0)
177 { BZ_SETERR(BZ_OK); return 0; };
179 bzf->strm.avail_out = len;
180 bzf->strm.next_out = buf;
182 while (True) {
184 if (ferror(bzf->handle))
185 { BZ_SETERR(BZ_IO_ERROR); return 0; };
187 if (bzf->strm.avail_in == 0 && !bz_feof(bzf->handle)) {
188 n = fread ( bzf->buf, sizeof(UChar),
189 BZ_MAX_UNUSED, bzf->handle );
190 if (ferror(bzf->handle))
191 { BZ_SETERR(BZ_IO_ERROR); return 0; };
192 bzf->bufN = n;
193 bzf->strm.avail_in = bzf->bufN;
194 bzf->strm.next_in = bzf->buf;
197 ret = BZ2_bzDecompress ( &(bzf->strm) );
199 if (ret != BZ_OK && ret != BZ_STREAM_END)
200 { BZ_SETERR(ret); return 0; };
202 if (ret == BZ_OK && bz_feof(bzf->handle) &&
203 bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
204 { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
206 if (ret == BZ_STREAM_END)
207 { BZ_SETERR(BZ_STREAM_END);
208 return len - bzf->strm.avail_out; };
209 if (bzf->strm.avail_out == 0)
210 { BZ_SETERR(BZ_OK); return len; };
214 /* return 0; not reached*/
218 /*---------------------------------------------------*/
219 void BZ_API(BZ2_bzReadGetUnused)
220 ( int* bzerror,
221 BZFILE* b,
222 void** unused,
223 int* nUnused )
225 bzFile* bzf = (bzFile*)b;
226 if (bzf == NULL)
227 { BZ_SETERR(BZ_PARAM_ERROR); return; };
228 if (bzf->lastErr != BZ_STREAM_END)
229 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
230 if (unused == NULL || nUnused == NULL)
231 { BZ_SETERR(BZ_PARAM_ERROR); return; };
233 BZ_SETERR(BZ_OK);
234 *nUnused = bzf->strm.avail_in;
235 *unused = bzf->strm.next_in;