Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_fixcpp/sys/src/cmd/bzip2/lib/bzzlib.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/*
2
 * THIS FILE IS NOT IDENTICAL TO THE ORIGINAL
3
 * FROM THE BZIP2 DISTRIBUTION.
4
 *
5
 * It has been modified, mainly to break the library
6
 * into smaller pieces.
7
 *
8
 * Russ Cox
9
 * rsc@plan9.bell-labs.com
10
 * July 2000
11
 */
12
 
13
/*-------------------------------------------------------------*/
14
/*--- Library top-level functions.                          ---*/
15
/*---                                               bzlib.c ---*/
16
/*-------------------------------------------------------------*/
17
 
18
/*--
19
  This file is a part of bzip2 and/or libbzip2, a program and
20
  library for lossless, block-sorting data compression.
21
 
22
  Copyright (C) 1996-2000 Julian R Seward.  All rights reserved.
23
 
24
  Redistribution and use in source and binary forms, with or without
25
  modification, are permitted provided that the following conditions
26
  are met:
27
 
28
  1. Redistributions of source code must retain the above copyright
29
     notice, this list of conditions and the following disclaimer.
30
 
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.
35
 
36
  3. Altered source versions must be plainly marked as such, and must
37
     not be misrepresented as being the original software.
38
 
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 
41
     permission.
42
 
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.
54
 
55
  Julian Seward, Cambridge, UK.
56
  jseward@acm.org
57
  bzip2/libbzip2 version 1.0 of 21 March 2000
58
 
59
  This program is based on (at least) the work of:
60
     Mike Burrows
61
     David Wheeler
62
     Peter Fenwick
63
     Alistair Moffat
64
     Radford Neal
65
     Ian H. Witten
66
     Robert Sedgewick
67
     Jon L. Bentley
68
 
69
  For more information on these sources, see the manual.
70
--*/
71
 
72
/*--
73
   CHANGES
74
   ~~~~~~~
75
   0.9.0 -- original version.
76
 
77
   0.9.0a/b -- no changes in this file.
78
 
79
   0.9.0c
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.
85
--*/
86
 
87
#include "os.h"
88
#include "bzlib.h"
89
#include "bzlib_private.h"
90
#include "bzlib_stdio.h"
91
#include "bzlib_stdio_private.h"
92
 
93
/*---------------------------------------------------*/
94
/*--
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.
102
--*/
103
/*---------------------------------------------------*/
104
 
105
/*---------------------------------------------------*/
106
 
107
#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
108
#   include <fcntl.h>
109
#   include <io.h>
110
#   define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
111
#else
112
#   define SET_BINARY_MODE(file)
113
#endif
114
static
115
BZFILE * bzopen_or_bzdopen
116
               ( const char *path,   /* no use when bzdopen */
117
                 int fd,             /* no use when bzdopen */
118
                 const char *mode,
119
                 int open_mode)      /* bzopen: 0, bzdopen:1 */
120
{
121
   int    bzerr;
122
   char   unused[BZ_MAX_UNUSED];
123
   int    blockSize100k = 9;
124
   int    writing       = 0;
125
   char   mode2[10]     = "";
126
   FILE   *fp           = NULL;
127
   BZFILE *bzfp         = NULL;
128
   int    verbosity     = 0;
129
   int    workFactor    = 30;
130
   int    smallMode     = 0;
131
   int    nUnused       = 0; 
132
 
133
   if (mode == NULL) return NULL;
134
   while (*mode) {
135
      switch (*mode) {
136
      case 'r':
137
         writing = 0; break;
138
      case 'w':
139
         writing = 1; break;
140
      case 's':
141
         smallMode = 1; break;
142
      default:
143
         if (isdigit((int)(*mode))) {
144
            blockSize100k = *mode-'0';
145
         }
146
      }
147
      mode++;
148
   }
149
   strcat(mode2, writing ? "w" : "r" );
150
   strcat(mode2,"b");   /* binary mode */
151
 
152
   if (open_mode==0) {
153
      if (path==NULL || strcmp(path,"")==0) {
154
        fp = (writing ? stdout : stdin);
155
        SET_BINARY_MODE(fp);
156
      } else {
157
        fp = fopen(path,mode2);
158
      }
159
   } else {
160
#ifdef BZ_STRICT_ANSI
161
      fp = NULL;
162
#else
163
      fp = fdopen(fd,mode2);
164
#endif
165
   }
166
   if (fp == NULL) return NULL;
167
 
168
   if (writing) {
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);
174
   } else {
175
      bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
176
                            unused,nUnused);
177
   }
178
   if (bzfp == NULL) {
179
      if (fp != stdin && fp != stdout) fclose(fp);
180
      return NULL;
181
   }
182
   return bzfp;
183
}
184
 
185
 
186
/*---------------------------------------------------*/
187
/*--
188
   open file for read or write.
189
      ex) bzopen("file","w9")
190
      case path="" or NULL => use stdin or stdout.
191
--*/
192
BZFILE * BZ_API(BZ2_bzopen)
193
               ( const char *path,
194
                 const char *mode )
195
{
196
   return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
197
}
198
 
199
 
200
/*---------------------------------------------------*/
201
BZFILE * BZ_API(BZ2_bzdopen)
202
               ( int fd,
203
                 const char *mode )
204
{
205
   return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
206
}
207
 
208
 
209
/*---------------------------------------------------*/
210
int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
211
{
212
   int bzerr, nread;
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) {
216
      return nread;
217
   } else {
218
      return -1;
219
   }
220
}
221
 
222
 
223
/*---------------------------------------------------*/
224
int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
225
{
226
   int bzerr;
227
 
228
   BZ2_bzWrite(&bzerr,b,buf,len);
229
   if(bzerr == BZ_OK){
230
      return len;
231
   }else{
232
      return -1;
233
   }
234
}
235
 
236
 
237
/*---------------------------------------------------*/
238
int BZ_API(BZ2_bzflush) (BZFILE *b)
239
{
240
   /* do nothing now... */
241
   return 0;
242
}
243
 
244
 
245
/*---------------------------------------------------*/
246
void BZ_API(BZ2_bzclose) (BZFILE* b)
247
{
248
   int bzerr;
249
   FILE *fp = ((bzFile *)b)->handle;
250
 
251
   if (b==NULL) {return;}
252
   if(((bzFile*)b)->writing){
253
      BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
254
      if(bzerr != BZ_OK){
255
         BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
256
      }
257
   }else{
258
      BZ2_bzReadClose(&bzerr,b);
259
   }
260
   if(fp!=stdin && fp!=stdout){
261
      fclose(fp);
262
   }
263
}
264
 
265
 
266
/*---------------------------------------------------*/
267
/*--
268
   return last error code 
269
--*/
270
static char *bzerrorstrings[] = {
271
       "OK"
272
      ,"SEQUENCE_ERROR"
273
      ,"PARAM_ERROR"
274
      ,"MEM_ERROR"
275
      ,"DATA_ERROR"
276
      ,"DATA_ERROR_MAGIC"
277
      ,"IO_ERROR"
278
      ,"UNEXPECTED_EOF"
279
      ,"OUTBUFF_FULL"
280
      ,"CONFIG_ERROR"
281
      ,"???"   /* for future */
282
      ,"???"   /* for future */
283
      ,"???"   /* for future */
284
      ,"???"   /* for future */
285
      ,"???"   /* for future */
286
      ,"???"   /* for future */
287
};
288
 
289
 
290
const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
291
{
292
   int err = ((bzFile *)b)->lastErr;
293
 
294
   if(err>0) err = 0;
295
   *errnum = err;
296
   return bzerrorstrings[err*-1];
297
}