Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 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
/*-------------------------------------------------------------*/
8
 
9
/*--
10
  This file is a part of bzip2 and/or libbzip2, a program and
11
  library for lossless, block-sorting data compression.
12
 
13
  Copyright (C) 1996-2000 Julian R Seward.  All rights reserved.
14
 
15
  Redistribution and use in source and binary forms, with or without
16
  modification, are permitted provided that the following conditions
17
  are met:
18
 
19
  1. Redistributions of source code must retain the above copyright
20
     notice, this list of conditions and the following disclaimer.
21
 
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.
26
 
27
  3. Altered source versions must be plainly marked as such, and must
28
     not be misrepresented as being the original software.
29
 
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.
33
 
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.
45
 
46
  Julian Seward, Cambridge, UK.
47
  jseward@acm.org
48
  bzip2/libbzip2 version 1.0 of 21 March 2000
49
 
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
59
 
60
  For more information on these sources, see the manual.
61
--*/
62
 
63
/*--
64
   CHANGES
65
   ~~~~~~~
66
   0.9.0 -- original version.
67
 
68
   0.9.0a/b -- no changes in this file.
69
 
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
--*/
77
 
78
#include "os.h"
79
#include "bzlib.h"
80
#include "bzlib_private.h"
81
#include "bzlib_stdio.h"
82
#include "bzlib_stdio_private.h"
83
 
84
/*---------------------------------------------------*/
85
BZFILE* BZ_API(BZ2_bzReadOpen) 
86
                   ( int*  bzerror, 
87
                     FILE* f, 
88
                     int   verbosity,
89
                     int   small,
90
                     void* unused,
91
                     int   nUnused )
92
{
93
   bzFile* bzf = NULL;
94
   int     ret;
95
 
96
   BZ_SETERR(BZ_OK);
97
 
98
   if (f == NULL || 
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; };
104
 
105
   if (ferror(f))
106
      { BZ_SETERR(BZ_IO_ERROR); return NULL; };
107
 
108
   bzf = malloc ( sizeof(bzFile) );
109
   if (bzf == NULL) 
110
      { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
111
 
112
   BZ_SETERR(BZ_OK);
113
 
114
   bzf->initialisedOk = False;
115
   bzf->handle        = f;
116
   bzf->bufN          = 0;
117
   bzf->writing       = False;
118
   bzf->strm.bzalloc  = NULL;
119
   bzf->strm.bzfree   = NULL;
120
   bzf->strm.opaque   = NULL;
121
 
122
   while (nUnused > 0) {
123
      bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
124
      unused = ((void*)( 1 + ((UChar*)(unused))  ));
125
      nUnused--;
126
   }
127
 
128
   ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
129
   if (ret != BZ_OK)
130
      { BZ_SETERR(ret); free(bzf); return NULL; };
131
 
132
   bzf->strm.avail_in = bzf->bufN;
133
   bzf->strm.next_in  = bzf->buf;
134
 
135
   bzf->initialisedOk = True;
136
   return bzf;   
137
}
138
 
139
 
140
/*---------------------------------------------------*/
141
void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
142
{
143
   bzFile* bzf = (bzFile*)b;
144
 
145
   BZ_SETERR(BZ_OK);
146
   if (bzf == NULL)
147
      { BZ_SETERR(BZ_OK); return; };
148
 
149
   if (bzf->writing)
150
      { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
151
 
152
   if (bzf->initialisedOk)
153
      (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
154
   free ( bzf );
155
}
156
 
157
 
158
/*---------------------------------------------------*/
159
int BZ_API(BZ2_bzRead) 
160
           ( int*    bzerror, 
161
             BZFILE* b, 
162
             void*   buf, 
163
             int     len )
164
{
165
   Int32   n, ret;
166
   bzFile* bzf = (bzFile*)b;
167
 
168
   BZ_SETERR(BZ_OK);
169
 
170
   if (bzf == NULL || buf == NULL || len < 0)
171
      { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
172
 
173
   if (bzf->writing)
174
      { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
175
 
176
   if (len == 0)
177
      { BZ_SETERR(BZ_OK); return 0; };
178
 
179
   bzf->strm.avail_out = len;
180
   bzf->strm.next_out = buf;
181
 
182
   while (True) {
183
 
184
      if (ferror(bzf->handle)) 
185
         { BZ_SETERR(BZ_IO_ERROR); return 0; };
186
 
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; };
192
         bzf->bufN = n;
193
         bzf->strm.avail_in = bzf->bufN;
194
         bzf->strm.next_in = bzf->buf;
195
      }
196
 
197
      ret = BZ2_bzDecompress ( &(bzf->strm) );
198
 
199
      if (ret != BZ_OK && ret != BZ_STREAM_END)
200
         { BZ_SETERR(ret); return 0; };
201
 
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; };
205
 
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; };
211
 
212
   }
213
 
214
}
215
 
216
 
217
/*---------------------------------------------------*/
218
void BZ_API(BZ2_bzReadGetUnused) 
219
                     ( int*    bzerror, 
220
                       BZFILE* b, 
221
                       void**  unused, 
222
                       int*    nUnused )
223
{
224
   bzFile* bzf = (bzFile*)b;
225
   if (bzf == NULL)
226
      { BZ_SETERR(BZ_PARAM_ERROR); return; };
227
   if (bzf->lastErr != BZ_STREAM_END)
228
      { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
229
   if (unused == NULL || nUnused == NULL)
230
      { BZ_SETERR(BZ_PARAM_ERROR); return; };
231
 
232
   BZ_SETERR(BZ_OK);
233
   *nUnused = bzf->strm.avail_in;
234
   *unused = bzf->strm.next_in;
235
}