1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "zip-reader.h"
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) (msgid)
34 #define UCOMPSIZE 4096
40 unsigned char ucomp[UCOMPSIZE];
42 size_t ucomp_bytes_read;
44 /* Two bitfields as defined by RFC1950 */
49 inflate_finish (struct zip_member *zm)
51 struct inflator *inf = zm->aux;
53 inflateEnd (&inf->zss);
59 inflate_init (struct zip_member *zm)
62 struct inflator *inf = xzalloc (sizeof *inf);
65 uint16_t cmf = 0x8; /* Always 8 for inflate */
67 const uint16_t cinfo = 7; /* log_2(Window size) - 8 */
69 cmf |= cinfo << 4; /* Put cinfo into the high nibble */
71 /* make these into a 16 bit word */
72 inf->cmf_flg = (cmf << 8 ) | flg;
74 /* Set the check bits */
75 inf->cmf_flg += 31 - (inf->cmf_flg % 31);
76 assert (inf->cmf_flg % 31 == 0);
78 inf->zss.next_in = Z_NULL;
79 inf->zss.avail_in = 0;
80 inf->zss.zalloc = Z_NULL;
81 inf->zss.zfree = Z_NULL;
82 inf->zss.opaque = Z_NULL;
83 r = inflateInit (&inf->zss);
87 ds_put_format (zm->errs, _("Cannot initialize inflator: %s"), zError (r));
98 inflate_read (struct zip_member *zm, void *buf, size_t n)
101 struct inflator *inf = zm->aux;
103 if (inf->zss.avail_in == 0)
109 if ( inf->state == 0)
111 inf->ucomp[1] = inf->cmf_flg ;
112 inf->ucomp[0] = inf->cmf_flg >> 8 ;
118 bytes_to_read = zm->comp_size - inf->ucomp_bytes_read;
120 if (bytes_to_read == 0)
123 if (bytes_to_read > UCOMPSIZE)
124 bytes_to_read = UCOMPSIZE;
126 bytes_read = fread (inf->ucomp + pad, 1, bytes_to_read - pad, zm->fp);
128 inf->ucomp_bytes_read += bytes_read;
130 inf->zss.avail_in = bytes_read + pad;
131 inf->zss.next_in = inf->ucomp;
133 inf->zss.avail_out = n;
134 inf->zss.next_out = buf;
136 r = inflate (&inf->zss, Z_NO_FLUSH);
139 return n - inf->zss.avail_out;
142 ds_put_format (zm->errs, _("Error inflating: %s"), zError (r));