2 * THIS FILE IS NOT IDENTICAL TO THE ORIGINAL
3 * FROM THE BZIP2 DISTRIBUTION.
5 * It has been modified, mainly to break the library
9 * rsc@plan9.bell-labs.com
13 /*-------------------------------------------------------------*/
14 /*--- Library top-level functions. ---*/
16 /*-------------------------------------------------------------*/
19 This file is a part of bzip2 and/or libbzip2, a program and
20 library for lossless, block-sorting data compression.
22 Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions
28 1. Redistributions of source code must retain the above copyright
29 notice, this list of conditions and the following disclaimer.
31 2. The origin of this software must not be misrepresented; you must
32 not claim that you wrote the original software. If you use this
33 software in a product, an acknowledgment in the product
34 documentation would be appreciated but is not required.
36 3. Altered source versions must be plainly marked as such, and must
37 not be misrepresented as being the original software.
39 4. The name of the author may not be used to endorse or promote
40 products derived from this software without specific prior written
43 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
49 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 Julian Seward, Cambridge, UK.
57 bzip2/libbzip2 version 1.0 of 21 March 2000
59 This program is based on (at least) the work of:
69 For more information on these sources, see the manual.
75 0.9.0 -- original version.
77 0.9.0a/b -- no changes in this file.
80 * made zero-length BZ_FLUSH work correctly in bzCompress().
81 * fixed bzWrite/bzRead to ignore zero-length requests.
82 * fixed bzread to correctly handle read requests after EOF.
83 * wrong parameter order in call to bzDecompressInit in
84 bzBuffToBuffDecompress. Fixed.
89 #include "bzlib_private.h"
90 #include "bzlib_stdio.h"
91 #include "bzlib_stdio_private.h"
93 /*---------------------------------------------------*/
95 Code contributed by Yoshioka Tsuneo
96 (QWF00133@niftyserve.or.jp/tsuneo-y@is.aist-nara.ac.jp),
97 to support better zlib compatibility.
98 This code is not _officially_ part of libbzip2 (yet);
99 I haven't tested it, documented it, or considered the
100 threading-safeness of it.
101 If this code breaks, please contact both Yoshioka and me.
103 /*---------------------------------------------------*/
105 /*---------------------------------------------------*/
107 #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
110 # define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
112 # define SET_BINARY_MODE(file)
115 BZFILE * bzopen_or_bzdopen
116 ( const char *path, /* no use when bzdopen */
117 int fd, /* no use when bzdopen */
119 int open_mode) /* bzopen: 0, bzdopen:1 */
122 char unused[BZ_MAX_UNUSED];
123 int blockSize100k = 9;
133 if (mode == NULL) return NULL;
141 smallMode = 1; break;
143 if (isdigit((int)(*mode))) {
144 blockSize100k = *mode-'0';
149 strcat(mode2, writing ? "w" : "r" );
150 strcat(mode2,"b"); /* binary mode */
153 if (path==NULL || strcmp(path,"")==0) {
154 fp = (writing ? stdout : stdin);
157 fp = fopen(path,mode2);
160 #ifdef BZ_STRICT_ANSI
163 fp = fdopen(fd,mode2);
166 if (fp == NULL) return NULL;
169 /* Guard against total chaos and anarchy -- JRS */
170 if (blockSize100k < 1) blockSize100k = 1;
171 if (blockSize100k > 9) blockSize100k = 9;
172 bzfp = BZ2_bzWriteOpen(&bzerr,fp,blockSize100k,
173 verbosity,workFactor);
175 bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
179 if (fp != stdin && fp != stdout) fclose(fp);
186 /*---------------------------------------------------*/
188 open file for read or write.
189 ex) bzopen("file","w9")
190 case path="" or NULL => use stdin or stdout.
192 BZFILE * BZ_API(BZ2_bzopen)
196 return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
200 /*---------------------------------------------------*/
201 BZFILE * BZ_API(BZ2_bzdopen)
205 return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
209 /*---------------------------------------------------*/
210 int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
213 if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0;
214 nread = BZ2_bzRead(&bzerr,b,buf,len);
215 if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {
223 /*---------------------------------------------------*/
224 int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
228 BZ2_bzWrite(&bzerr,b,buf,len);
237 /*---------------------------------------------------*/
238 int BZ_API(BZ2_bzflush) (BZFILE *b)
240 /* do nothing now... */
245 /*---------------------------------------------------*/
246 void BZ_API(BZ2_bzclose) (BZFILE* b)
249 FILE *fp = ((bzFile *)b)->handle;
251 if (b==NULL) {return;}
252 if(((bzFile*)b)->writing){
253 BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
255 BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
258 BZ2_bzReadClose(&bzerr,b);
260 if(fp!=stdin && fp!=stdout){
266 /*---------------------------------------------------*/
268 return last error code
270 static char *bzerrorstrings[] = {
281 ,"???" /* for future */
282 ,"???" /* for future */
283 ,"???" /* for future */
284 ,"???" /* for future */
285 ,"???" /* for future */
286 ,"???" /* for future */
290 const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
292 int err = ((bzFile *)b)->lastErr;
296 return bzerrorstrings[err*-1];