1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 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/>. */
28 #include "zip-reader.h"
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) (msgid)
36 #define UCOMPSIZE 4096
42 unsigned char ucomp[UCOMPSIZE];
44 size_t ucomp_bytes_read;
46 /* Two bitfields as defined by RFC1950 */
51 inflate_finish (struct zip_member *zm)
53 struct inflator *inf = zm->aux;
55 inflateEnd (&inf->zss);
61 inflate_init (struct zip_member *zm)
64 struct inflator *inf = xzalloc (sizeof *inf);
67 uint16_t cmf = 0x8; /* Always 8 for inflate */
69 const uint16_t cinfo = 7; /* log_2(Window size) - 8 */
71 cmf |= cinfo << 4; /* Put cinfo into the high nibble */
73 /* make these into a 16 bit word */
74 inf->cmf_flg = (cmf << 8 ) | flg;
76 /* Set the check bits */
77 inf->cmf_flg += 31 - (inf->cmf_flg % 31);
78 assert (inf->cmf_flg % 31 == 0);
80 inf->zss.next_in = Z_NULL;
81 inf->zss.avail_in = 0;
82 inf->zss.zalloc = Z_NULL;
83 inf->zss.zfree = Z_NULL;
84 inf->zss.opaque = Z_NULL;
85 r = inflateInit (&inf->zss);
89 ds_put_format (zm->errs, _("Cannot initialize inflator: %s"), zError (r));
100 inflate_read (struct zip_member *zm, void *buf, size_t n)
102 struct inflator *inf = zm->aux;
104 if (inf->zss.avail_in == 0)
110 if ( inf->state == 0)
112 inf->ucomp[1] = inf->cmf_flg ;
113 inf->ucomp[0] = inf->cmf_flg >> 8 ;
119 bytes_to_read = zm->comp_size - inf->ucomp_bytes_read;
121 if (bytes_to_read == 0)
124 if (bytes_to_read > UCOMPSIZE)
125 bytes_to_read = UCOMPSIZE;
127 bytes_read = fread (inf->ucomp + pad, 1, bytes_to_read - pad, zm->fp);
129 inf->ucomp_bytes_read += bytes_read;
131 inf->zss.avail_in = bytes_read + pad;
132 inf->zss.next_in = inf->ucomp;
134 inf->zss.avail_out = n;
135 inf->zss.next_out = buf;
137 int r = inflate (&inf->zss, Z_NO_FLUSH);
140 return n - inf->zss.avail_out;
143 ds_put_format (zm->errs, _("Error inflating: %s"), zError (r));