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_bzWriteOpen) 
86
                    ( int*  bzerror,      
87
                      FILE* f, 
88
                      int   blockSize100k, 
89
                      int   verbosity,
90
                      int   workFactor )
91
{
92
   Int32   ret;
93
   bzFile* bzf = NULL;
94
 
95
   BZ_SETERR(BZ_OK);
96
 
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; };
102
 
103
   if (ferror(f))
104
      { BZ_SETERR(BZ_IO_ERROR); return NULL; };
105
 
106
   bzf = malloc ( sizeof(bzFile) );
107
   if (bzf == NULL)
108
      { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
109
 
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;
118
 
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; };
124
 
125
   bzf->strm.avail_in = 0;
126
   bzf->initialisedOk = True;
127
   return bzf;   
128
}
129
 
130
 
131
 
132
/*---------------------------------------------------*/
133
void BZ_API(BZ2_bzWrite)
134
             ( int*    bzerror, 
135
               BZFILE* b, 
136
               void*   buf, 
137
               int     len )
138
{
139
   Int32 n, n2, ret;
140
   bzFile* bzf = (bzFile*)b;
141
 
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; };
149
 
150
   if (len == 0)
151
      { BZ_SETERR(BZ_OK); return; };
152
 
153
   bzf->strm.avail_in = len;
154
   bzf->strm.next_in  = buf;
155
 
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; };
162
 
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; };
169
      }
170
 
171
      if (bzf->strm.avail_in == 0)
172
         { BZ_SETERR(BZ_OK); return; };
173
   }
174
}
175
 
176
 
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 )
184
{
185
   BZ2_bzWriteClose64 ( bzerror, b, abandon, 
186
                        nbytes_in, NULL, nbytes_out, NULL );
187
}
188
 
189
 
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 )
198
{
199
   Int32   n, n2, ret;
200
   bzFile* bzf = (bzFile*)b;
201
 
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; };
208
 
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;
213
 
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; };
221
 
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; };
228
         }
229
 
230
         if (ret == BZ_STREAM_END) break;
231
      }
232
   }
233
 
234
   if ( !abandon && !ferror ( bzf->handle ) ) {
235
      fflush ( bzf->handle );
236
      if (ferror(bzf->handle))
237
         { BZ_SETERR(BZ_IO_ERROR); return; };
238
   }
239
 
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;
248
 
249
   BZ_SETERR(BZ_OK);
250
   BZ2_bzCompressEnd ( &(bzf->strm) );
251
   free ( bzf );
252
}
253