1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "precompile.h"
27 #include "hstream.hxx"
33 #define Z_BUFSIZE (1024 * 4)
35 #define ALLOC(size) malloc(size)
36 #define TRYFREE(p) {if (p) free(p);}
38 local
int get_byte(gz_stream
* s
);
39 local
int destroy(gz_stream
* s
);
40 local uLong
getLong(gz_stream
* s
);
42 /* ===========================================================================
43 Opens a gzip (.gz) file for reading or writing. The mode parameter
44 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
45 or path name (if fd == -1).
46 gz_open return NULL if the file could not be opened or if there was
47 insufficient memory to allocate the (de)compression state; errno
48 can be checked to distinguish the two cases (if errno is zero, the
49 zlib error is Z_MEM_ERROR).
51 gz_stream
*gz_open(HStream
& _stream
)
54 //int level = Z_DEFAULT_COMPRESSION; /* compression level */
56 // char *p = (char*)mode;
57 //char fmode[80]; /* copy of mode, without the compression level */
61 s
= static_cast<gz_stream
*>(ALLOC(sizeof(gz_stream
)));
64 s
->stream
.zalloc
= (alloc_func
) 0;
65 s
->stream
.zfree
= (free_func
) 0;
66 s
->stream
.opaque
= (voidpf
) 0;
67 s
->stream
.next_in
= s
->inbuf
= Z_NULL
;
68 s
->stream
.next_out
= s
->outbuf
= Z_NULL
;
69 s
->stream
.avail_in
= s
->stream
.avail_out
= 0;
70 //s->_inputstream = NULL;
73 s
->crc
= crc32(0L, Z_NULL
, 0);
80 err
= inflateInit2(&(s
->stream
), -MAX_WBITS
);
81 s
->stream
.next_in
= s
->inbuf
= static_cast<Byte
*>(ALLOC(Z_BUFSIZE
));
83 if (err
!= Z_OK
|| s
->inbuf
== Z_NULL
)
89 s
->stream
.avail_out
= Z_BUFSIZE
;
92 s
->_inputstream
= &_stream
;
98 /* ===========================================================================
99 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
101 IN assertion: the stream s has been successfully opened for reading.
103 local
int get_byte(gz_stream
* s
)
107 if (s
->stream
.avail_in
== 0)
111 s
->stream
.avail_in
= s
->_inputstream
->readBytes(s
->inbuf
, Z_BUFSIZE
);
112 if (s
->stream
.avail_in
== 0)
117 s
->stream
.next_in
= s
->inbuf
;
119 s
->stream
.avail_in
--;
120 return *(s
->stream
.next_in
)++;
124 /* ===========================================================================
125 * Cleanup then free the given gz_stream. Return a zlib error code.
126 * Try freeing in the reverse order of allocations.
128 local
int destroy(gz_stream
* s
)
133 return Z_STREAM_ERROR
;
137 if (s
->stream
.state
!= NULL
)
139 err
= inflateEnd(&(s
->stream
));
151 // typedef unsigned char Byte
152 // typedef Byte FAR Bytef;
153 /* ===========================================================================
154 Reads the given number of uncompressed bytes from the compressed file.
155 gz_read returns the number of bytes actually read (0 for end of file).
157 int gz_read(gz_stream
* file
, voidp buf
, unsigned len
)
159 //printf("@@ gz_read : len : %d\t",len);
161 Bytef
*start
= static_cast<Bytef
*>(buf
); /* starting point for crc computation */
162 Byte
*next_out
; /* == stream.next_out but not forced far (for MSDOS) */
164 return Z_STREAM_ERROR
;
166 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
)
168 if (s
->z_err
== Z_STREAM_END
)
171 s
->stream
.next_out
= next_out
= static_cast<Bytef
*>(buf
);
172 s
->stream
.avail_out
= len
;
174 while (s
->stream
.avail_out
!= 0)
178 /* Copy first the lookahead bytes: */
179 uInt n
= s
->stream
.avail_in
;
181 if (n
> s
->stream
.avail_out
)
182 n
= s
->stream
.avail_out
;
185 memcpy(s
->stream
.next_out
, s
->stream
.next_in
, n
);
187 s
->stream
.next_out
= next_out
;
188 s
->stream
.next_in
+= n
;
189 s
->stream
.avail_out
-= n
;
190 s
->stream
.avail_in
-= n
;
192 if (s
->stream
.avail_out
> 0)
194 s
->stream
.avail_out
-=
195 s
->_inputstream
->readBytes(next_out
, s
->stream
.avail_out
);
197 return (int) (len
- s
->stream
.avail_out
);
199 if (s
->stream
.avail_in
== 0 && !s
->z_eof
)
203 s
->stream
.avail_in
= s
->_inputstream
->readBytes(s
->inbuf
, Z_BUFSIZE
);
204 if (s
->stream
.avail_in
== 0)
209 s
->stream
.next_in
= s
->inbuf
;
211 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
213 if (s
->z_err
== Z_STREAM_END
)
215 /* Check CRC and original size */
216 s
->crc
= crc32(s
->crc
, start
, (uInt
) (s
->stream
.next_out
- start
));
217 start
= s
->stream
.next_out
;
219 if (getLong(s
) != s
->crc
|| getLong(s
) != s
->stream
.total_out
)
221 s
->z_err
= Z_DATA_ERROR
;
223 else if (s
->z_err
== Z_OK
)
225 inflateReset(&(s
->stream
));
226 s
->crc
= crc32(0L, Z_NULL
, 0);
229 if (s
->z_err
!= Z_OK
|| s
->z_eof
)
232 s
->crc
= crc32(s
->crc
, start
, (uInt
) (s
->stream
.next_out
- start
));
233 return (int) (len
- s
->stream
.avail_out
);
236 /* ===========================================================================
237 Flushes all pending output into the compressed file. The parameter
238 flush is as in the deflate() function.
239 gz_flush should be called only when strictly necessary because it can
242 int gz_flush(gz_stream
* file
, int flush
)
248 if (s
== NULL
|| s
->mode
!= 'w')
249 return Z_STREAM_ERROR
;
251 s
->stream
.avail_in
= 0; /* should be zero already anyway */
255 len
= Z_BUFSIZE
- s
->stream
.avail_out
;
259 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
264 s
->stream
.next_out
= s
->outbuf
;
265 s
->stream
.avail_out
= Z_BUFSIZE
;
269 s
->z_err
= deflate(&(s
->stream
), flush
);
271 /* deflate has finished flushing only when it hasn't used up
272 * all the available space in the output buffer:
274 done
= (s
->stream
.avail_out
!= 0 || s
->z_err
== Z_STREAM_END
);
276 if (s
->z_err
!= Z_OK
&& s
->z_err
!= Z_STREAM_END
)
279 return s
->z_err
== Z_STREAM_END
? Z_OK
: s
->z_err
;
283 /* ===========================================================================
284 Reads a long in LSB order from the given gz_stream. Sets
286 local uLong
getLong(gz_stream
* s
)
288 uLong x
= (unsigned char) get_byte(s
);
290 x
+= ((unsigned char) get_byte(s
)) << 8;
291 x
+= ((unsigned char) get_byte(s
)) << 16;
292 x
+= ((unsigned char) get_byte(s
)) << 24;
295 s
->z_err
= Z_DATA_ERROR
;
301 /* ===========================================================================
302 Flushes all pending output if necessary, closes the compressed file
303 and deallocates all the (de)compression state.
305 int gz_close(gz_stream
* file
)
311 return Z_STREAM_ERROR
;
315 err
= gz_flush(file
, Z_FINISH
);
318 putLong(s
->file
, s
->crc
);
319 putLong(s
->file
, s
->stream
.total_in
);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */