Fix compiler warnings
[pspp] / src / libpspp / inflate.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include "inflate.h"
21
22 #if HAVE_ZLIB_H
23
24 #include <xalloc.h>
25 #include <zlib.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "zip-reader.h"
29
30 #include "str.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) (msgid)
35
36 #define UCOMPSIZE 4096
37
38 struct inflator
39 {
40   z_stream zss;
41   int state;
42   unsigned char ucomp[UCOMPSIZE];
43   size_t bytes_uncomp;
44   size_t ucomp_bytes_read;
45
46   /* Two bitfields as defined by RFC1950 */
47   uint16_t cmf_flg ;
48 };
49
50 void
51 inflate_finish (struct zip_member *zm)
52 {
53   struct inflator *inf = zm->aux;
54
55   inflateEnd (&inf->zss);
56
57   free (inf);
58 }
59
60 bool
61 inflate_init (struct zip_member *zm)
62 {
63   int r;
64   struct inflator *inf = xzalloc (sizeof *inf);
65   
66   uint16_t flg = 0 ; 
67   uint16_t cmf = 0x8; /* Always 8 for inflate */
68
69   const uint16_t cinfo = 7;  /* log_2(Window size) - 8 */
70
71   cmf |= cinfo << 4;     /* Put cinfo into the high nibble */
72
73   /* make these into a 16 bit word */
74   inf->cmf_flg = (cmf << 8 ) | flg;
75
76   /* Set the check bits */
77   inf->cmf_flg += 31 - (inf->cmf_flg % 31);
78   assert (inf->cmf_flg % 31 == 0);
79
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);
86
87   if ( Z_OK != r)
88     {
89       ds_put_format (zm->errs, _("Cannot initialize inflator: %s"), zError (r));
90       return false;
91     }
92
93   zm->aux = inf;
94
95   return true;
96 }
97
98
99 int
100 inflate_read (struct zip_member *zm, void *buf, size_t n)
101 {
102   int r;
103   struct inflator *inf = zm->aux;
104
105   if (inf->zss.avail_in == 0)
106     {
107       int bytes_read;
108       int bytes_to_read;
109       int pad = 0;
110
111       if ( inf->state == 0)
112         {
113           inf->ucomp[1] = inf->cmf_flg ;
114           inf->ucomp[0] = inf->cmf_flg >> 8 ;
115
116           pad = 2;
117           inf->state++;
118         }
119
120       bytes_to_read = zm->comp_size - inf->ucomp_bytes_read;
121       
122       if (bytes_to_read == 0)
123         return 0;
124
125       if (bytes_to_read > UCOMPSIZE)
126         bytes_to_read = UCOMPSIZE;
127
128       bytes_read = fread (inf->ucomp + pad, 1, bytes_to_read - pad, zm->fp);
129
130       inf->ucomp_bytes_read += bytes_read;
131
132       inf->zss.avail_in = bytes_read + pad;
133       inf->zss.next_in = inf->ucomp;
134     }
135   inf->zss.avail_out = n;
136   inf->zss.next_out = buf;
137
138   r = inflate (&inf->zss, Z_NO_FLUSH);
139   if ( Z_OK == r)
140     {
141       return n - inf->zss.avail_out;
142     }
143   
144   ds_put_format (zm->errs, _("Error inflating: %s"), zError (r));
145
146   return -1;
147 }
148
149 #endif