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)
103 struct inflator *inf = zm->aux;
105 if (inf->zss.avail_in == 0)
111 if ( inf->state == 0)
113 inf->ucomp[1] = inf->cmf_flg ;
114 inf->ucomp[0] = inf->cmf_flg >> 8 ;
120 bytes_to_read = zm->comp_size - inf->ucomp_bytes_read;
122 if (bytes_to_read == 0)
125 if (bytes_to_read > UCOMPSIZE)
126 bytes_to_read = UCOMPSIZE;
128 bytes_read = fread (inf->ucomp + pad, 1, bytes_to_read - pad, zm->fp);
130 inf->ucomp_bytes_read += bytes_read;
132 inf->zss.avail_in = bytes_read + pad;
133 inf->zss.next_in = inf->ucomp;
135 inf->zss.avail_out = n;
136 inf->zss.next_out = buf;
138 r = inflate (&inf->zss, Z_NO_FLUSH);
141 return n - inf->zss.avail_out;
144 ds_put_format (zm->errs, _("Error inflating: %s"), zError (r));