fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / hwpfilter / source / hgzip.cxx
blobbaf3b20e43f55d3d761060f0280bb4daf454fec3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include "hgzip.h"
27 #include "hstream.hxx"
29 #ifndef local
30 # define local static
31 #endif
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)
53 int err;
54 //int level = Z_DEFAULT_COMPRESSION; /* compression level */
56 // char *p = (char*)mode;
57 //char fmode[80]; /* copy of mode, without the compression level */
58 //char *m = fmode;
59 gz_stream *s;
61 s = static_cast<gz_stream *>(ALLOC(sizeof(gz_stream)));
62 if (!s)
63 return Z_NULL;
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;
71 s->z_err = Z_OK;
72 s->z_eof = 0;
73 s->crc = crc32(0L, Z_NULL, 0);
74 s->msg = NULL;
75 s->transparent = 0;
77 s->mode = 'r';
79 //realking
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)
85 destroy(s);
86 return Z_NULL;
89 s->stream.avail_out = Z_BUFSIZE;
91 errno = 0;
92 s->_inputstream = &_stream;
94 return s;
98 /* ===========================================================================
99 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
100 for end of file.
101 IN assertion: the stream s has been successfully opened for reading.
103 local int get_byte(gz_stream * s)
105 if (s->z_eof)
106 return EOF;
107 if (s->stream.avail_in == 0)
109 errno = 0;
111 s->stream.avail_in = s->_inputstream->readBytes(s->inbuf, Z_BUFSIZE);
112 if (s->stream.avail_in == 0)
114 s->z_eof = 1;
115 return EOF;
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)
130 int err = Z_OK;
132 if (!s)
133 return Z_STREAM_ERROR;
135 TRYFREE(s->msg);
137 if (s->stream.state != NULL)
139 err = inflateEnd(&(s->stream));
141 if (s->z_err < 0)
142 err = s->z_err;
144 TRYFREE(s->inbuf);
145 TRYFREE(s->outbuf);
146 TRYFREE(s);
147 return err;
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);
160 gz_stream *s = file;
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) */
163 if (s == NULL)
164 return Z_STREAM_ERROR;
166 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
167 return -1;
168 if (s->z_err == Z_STREAM_END)
169 return 0; /* EOF */
171 s->stream.next_out = next_out = static_cast<Bytef *>(buf);
172 s->stream.avail_out = len;
174 while (s->stream.avail_out != 0)
176 if (s->transparent)
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;
183 if (n > 0)
185 memcpy(s->stream.next_out, s->stream.next_in, n);
186 next_out += 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)
202 errno = 0;
203 s->stream.avail_in = s->_inputstream->readBytes(s->inbuf, Z_BUFSIZE);
204 if (s->stream.avail_in == 0)
206 s->z_eof = 1;
207 break;
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)
230 break;
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
240 degrade compression.
242 int gz_flush(gz_stream * file, int flush)
244 uInt len;
245 bool done = false;
246 gz_stream *s = file;
248 if (s == NULL || s->mode != 'w')
249 return Z_STREAM_ERROR;
251 s->stream.avail_in = 0; /* should be zero already anyway */
253 for (;;)
255 len = Z_BUFSIZE - s->stream.avail_out;
256 if (len != 0)
259 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
260 s->z_err = Z_ERRNO;
261 return Z_ERRNO;
264 s->stream.next_out = s->outbuf;
265 s->stream.avail_out = Z_BUFSIZE;
267 if (done)
268 break;
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)
277 break;
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;
293 if (s->z_eof)
295 s->z_err = Z_DATA_ERROR;
297 return x;
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)
307 // int err;
308 gz_stream *s = file;
310 if (s == NULL)
311 return Z_STREAM_ERROR;
312 #if 0
313 if (s->mode == 'w')
315 err = gz_flush(file, Z_FINISH);
316 if (err != Z_OK)
317 return destroy(s);
318 putLong(s->file, s->crc);
319 putLong(s->file, s->stream.total_in);
321 #endif
322 return destroy(s);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */