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_bzWriteOpen)
86 ( int* bzerror,
87 FILE* f,
88 int blockSize100k,
89 int verbosity,
90 int workFactor )
91 {
92 Int32 ret;
93 bzFile* bzf = NULL;
95 BZ_SETERR(BZ_OK);
97 if (f == NULL ||
98 (blockSize100k < 1 || blockSize100k > 9) ||
99 (workFactor < 0 || workFactor > 250) ||
100 (verbosity < 0 || verbosity > 4))
101 { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
103 if (ferror(f))
104 { BZ_SETERR(BZ_IO_ERROR); return NULL; };
106 bzf = malloc ( sizeof(bzFile) );
107 if (bzf == NULL)
108 { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
110 BZ_SETERR(BZ_OK);
111 bzf->initialisedOk = False;
112 bzf->bufN = 0;
113 bzf->handle = f;
114 bzf->writing = True;
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 );
122 if (ret != BZ_OK)
123 { BZ_SETERR(ret); free(bzf); return NULL; };
125 bzf->strm.avail_in = 0;
126 bzf->initialisedOk = True;
127 return bzf;
132 /*---------------------------------------------------*/
133 void BZ_API(BZ2_bzWrite)
134 ( int* bzerror,
135 BZFILE* b,
136 void* buf,
137 int len )
139 Int32 n, n2, ret;
140 bzFile* bzf = (bzFile*)b;
142 BZ_SETERR(BZ_OK);
143 if (bzf == NULL || buf == NULL || len < 0)
144 { BZ_SETERR(BZ_PARAM_ERROR); return; };
145 if (!(bzf->writing))
146 { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
147 if (ferror(bzf->handle))
148 { BZ_SETERR(BZ_IO_ERROR); return; };
150 if (len == 0)
151 { BZ_SETERR(BZ_OK); return; };
153 bzf->strm.avail_in = len;
154 bzf->strm.next_in = buf;
156 while (True) {
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),
166 n, bzf->handle );
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)
179 ( int* bzerror,
180 BZFILE* b,
181 int abandon,
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)
191 ( int* bzerror,
192 BZFILE* b,
193 int abandon,
194 unsigned int* nbytes_in_lo32,
195 unsigned int* nbytes_in_hi32,
196 unsigned int* nbytes_out_lo32,
197 unsigned int* nbytes_out_hi32 )
199 Int32 n, n2, ret;
200 bzFile* bzf = (bzFile*)b;
202 if (bzf == NULL)
203 { BZ_SETERR(BZ_OK); return; };
204 if (!(bzf->writing))
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) {
215 while (True) {
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),
225 n, bzf->handle );
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;
249 BZ_SETERR(BZ_OK);
250 BZ2_bzCompressEnd ( &(bzf->strm) );
251 free ( bzf );