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_bzWriteOpen)
98 (blockSize100k < 1 || blockSize100k > 9) ||
99 (workFactor < 0 || workFactor > 250) ||
100 (verbosity < 0 || verbosity > 4))
101 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
104 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
106 bzf = malloc ( sizeof(bzFile) );
108 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
111 bzf->initialisedOk = False;
115 bzf->strm.bzalloc = NULL;
116 bzf->strm.bzfree = NULL;
117 bzf->strm.opaque = NULL;
119 if (workFactor == 0) workFactor = 30;
120 ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k,
121 verbosity, workFactor );
123 { BZ_SETERR(ret); free(bzf); return NULL; };
125 bzf->strm.avail_in = 0;
126 bzf->initialisedOk = True;
132 /*---------------------------------------------------*/
133 void BZ_API(BZ2_bzWrite)
140 bzFile* bzf = (bzFile*)b;
143 if (bzf == NULL || buf == NULL || len < 0)
144 { BZ_SETERR(BZ_PARAM_ERROR); return; };
146 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
147 if (ferror(bzf->handle))
148 { BZ_SETERR(BZ_IO_ERROR); return; };
151 { BZ_SETERR(BZ_OK); return; };
153 bzf->strm.avail_in = len;
154 bzf->strm.next_in = buf;
157 bzf->strm.avail_out = BZ_MAX_UNUSED;
158 bzf->strm.next_out = bzf->buf;
159 ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN );
160 if (ret != BZ_RUN_OK)
161 { BZ_SETERR(ret); return; };
163 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
164 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
165 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
167 if (n != n2 || ferror(bzf->handle))
168 { BZ_SETERR(BZ_IO_ERROR); return; };
171 if (bzf->strm.avail_in == 0)
172 { BZ_SETERR(BZ_OK); return; };
177 /*---------------------------------------------------*/
178 void BZ_API(BZ2_bzWriteClose)
182 unsigned int* nbytes_in,
183 unsigned int* nbytes_out )
185 BZ2_bzWriteClose64 ( bzerror, b, abandon,
186 nbytes_in, NULL, nbytes_out, NULL );
190 void BZ_API(BZ2_bzWriteClose64)
194 unsigned int* nbytes_in_lo32,
195 unsigned int* nbytes_in_hi32,
196 unsigned int* nbytes_out_lo32,
197 unsigned int* nbytes_out_hi32 )
200 bzFile* bzf = (bzFile*)b;
203 { BZ_SETERR(BZ_OK); return; };
205 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
206 if (ferror(bzf->handle))
207 { BZ_SETERR(BZ_IO_ERROR); return; };
209 if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0;
210 if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0;
211 if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0;
212 if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0;
214 if ((!abandon) && bzf->lastErr == BZ_OK) {
216 bzf->strm.avail_out = BZ_MAX_UNUSED;
217 bzf->strm.next_out = bzf->buf;
218 ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH );
219 if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
220 { BZ_SETERR(ret); return; };
222 if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
223 n = BZ_MAX_UNUSED - bzf->strm.avail_out;
224 n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
226 if (n != n2 || ferror(bzf->handle))
227 { BZ_SETERR(BZ_IO_ERROR); return; };
230 if (ret == BZ_STREAM_END) break;
234 if ( !abandon && !ferror ( bzf->handle ) ) {
235 fflush ( bzf->handle );
236 if (ferror(bzf->handle))
237 { BZ_SETERR(BZ_IO_ERROR); return; };
240 if (nbytes_in_lo32 != NULL)
241 *nbytes_in_lo32 = bzf->strm.total_in_lo32;
242 if (nbytes_in_hi32 != NULL)
243 *nbytes_in_hi32 = bzf->strm.total_in_hi32;
244 if (nbytes_out_lo32 != NULL)
245 *nbytes_out_lo32 = bzf->strm.total_out_lo32;
246 if (nbytes_out_hi32 != NULL)
247 *nbytes_out_hi32 = bzf->strm.total_out_hi32;
250 BZ2_bzCompressEnd ( &(bzf->strm) );