1 /* THIS FILE HAS BEEN MODIFIED -- rsc split bzlib.c into bzlib.c,
2 bzlibcompress.c, bzlibdecompress.c, bzlibread.c, bzlibwrite.c
4 /*-------------------------------------------------------------*/
5 /*--- Library top-level functions. ---*/
7 /*-------------------------------------------------------------*/
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
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
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.
48 bzip2/libbzip2 version 1.0 of 21 March 2000
50 This program is based on (at least) the work of:
60 For more information on these sources, see the manual.
66 0.9.0 -- original version.
68 0.9.0a/b -- no changes in this file.
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.
80 #include "bzlib_private.h"
81 #include "bzlib_stdio.h"
82 #include "bzlib_stdio_private.h"
84 /*---------------------------------------------------*/
85 BZFILE* BZ_API(BZ2_bzReadOpen)
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; };
106 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
108 bzf = malloc ( sizeof(bzFile) );
110 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
114 bzf->initialisedOk = False;
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)) ));
128 ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
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;
140 /*---------------------------------------------------*/
141 void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
143 bzFile* bzf = (bzFile*)b;
147 { BZ_SETERR(BZ_OK); return; };
150 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
152 if (bzf->initialisedOk)
153 (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
158 /*---------------------------------------------------*/
159 int BZ_API(BZ2_bzRead)
166 bzFile* bzf = (bzFile*)b;
170 if (bzf == NULL || buf == NULL || len < 0)
171 { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
174 { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
177 { BZ_SETERR(BZ_OK); return 0; };
179 bzf->strm.avail_out = len;
180 bzf->strm.next_out = buf;
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; };
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)
225 bzFile* bzf = (bzFile*)b;
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; };
234 *nUnused = bzf->strm.avail_in;
235 *unused = bzf->strm.next_in;