29#define Z_BUFSIZE (1024 * 4)
31#define ALLOC(size) malloc(size)
32#define TRYFREE(p) {if (p) free(p);}
60 s->
stream.zalloc =
nullptr;
62 s->
stream.opaque =
nullptr;
64 s->
stream.next_out = Z_NULL;
69 s->
crc = crc32(0, Z_NULL, 0);
75 err = inflateInit2(&(s->
stream), -MAX_WBITS);
78 if (
err != Z_OK || s->
inbuf == Z_NULL)
102 if (s->
stream.avail_in == 0)
107 if (s->
stream.avail_in == 0)
115 return *(s->
stream.next_in)++;
128 return Z_STREAM_ERROR;
132 if (s->
stream.state !=
nullptr)
155 Bytef *
start =
static_cast<Bytef *
>(buf);
162 if (s->
z_err == Z_DATA_ERROR || s->
z_err == Z_ERRNO)
164 if (s->
z_err == Z_STREAM_END)
167 s->
stream.next_out =
static_cast<Bytef *
>(buf);
168 s->
stream.avail_out = len;
170 while (s->
stream.avail_out != 0)
177 if (s->
stream.avail_in == 0)
186 if (s->
z_err == Z_STREAM_END)
194 s->
z_err = Z_DATA_ERROR;
196 else if (s->
z_err == Z_OK)
198 inflateReset(&(s->
stream));
199 s->
crc = crc32(0, Z_NULL, 0);
206 return len - s->
stream.avail_out;
221 if (s ==
nullptr || s->
mode !=
'w')
222 return Z_STREAM_ERROR;
237 s->
stream.next_out =
nullptr;
247 done = (s->
stream.avail_out != 0 || s->
z_err == Z_STREAM_END);
249 if (s->
z_err != Z_OK && s->
z_err != Z_STREAM_END)
252 return s->
z_err == Z_STREAM_END ? Z_OK : s->
z_err;
261 uLong
x =
static_cast<unsigned char>(
get_byte(s));
263 x +=
static_cast<unsigned char>(
get_byte(s)) << 8;
264 x +=
static_cast<unsigned char>(
get_byte(s)) << 16;
265 x +=
static_cast<unsigned char>(
get_byte(s)) << 24;
268 s->
z_err = Z_DATA_ERROR;
284 return Z_STREAM_ERROR;
291 putLong(s->file, s->
crc);
292 putLong(s->file, s->
stream.total_in);
size_t readBytes(byte *buf, size_t aToRead)
Read some byte to buf as given size.
gz_stream * gz_open(HStream &_stream)
Opens a gzipped stream for reading.
int gz_close(gz_stream *file)
Flushes all pending output if necessary, closes the compressed stream and deallocates all the (de)com...
int gz_flush(gz_stream *file, int flush)
Flushes all pending output into the compressed file gz_flush should be called only when strictly nece...
static int get_byte(gz_stream *s)
static int destroy(gz_stream *s)
static uLong getLong(gz_stream *s)
size_t gz_read(gz_stream *file, voidp buf, unsigned len)
Reads the given number of uncompressed bytes from the compressed stream.
Structure for using z_stream.
int z_eof
EOF of the input file.
Byte * inbuf
Input buffer.
uLong crc
Crc32 of uncompressed data.
HStream * _inputstream
Stream.
int z_err
The error code of z_stream operation.