Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/externals/zlib/src/gzwrite.c

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/zlib/src/gzwrite.c (Version 11.3.0) and /externals/zlib/src/gzwrite.c (Version 10.5.p1)


  1 /* gzwrite.c -- zlib functions for writing gzi      1 /* gzwrite.c -- zlib functions for writing gzip files
  2  * Copyright (C) 2004-2019 Mark Adler          <<   2  * Copyright (C) 2004-2017 Mark Adler
  3  * For conditions of distribution and use, see      3  * For conditions of distribution and use, see copyright notice in zlib.h
  4  */                                                 4  */
  5                                                     5 
  6 #include "gzguts.h"                                 6 #include "gzguts.h"
  7                                                     7 
  8 /* Local functions */                               8 /* Local functions */
  9 local int gz_init OF((gz_statep));                  9 local int gz_init OF((gz_statep));
 10 local int gz_comp OF((gz_statep, int));            10 local int gz_comp OF((gz_statep, int));
 11 local int gz_zero OF((gz_statep, z_off64_t));      11 local int gz_zero OF((gz_statep, z_off64_t));
 12 local z_size_t gz_write OF((gz_statep, voidpc,     12 local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
 13                                                    13 
 14 /* Initialize state for writing a gzip file.       14 /* Initialize state for writing a gzip file.  Mark initialization by setting
 15    state->size to non-zero.  Return -1 on a me     15    state->size to non-zero.  Return -1 on a memory allocation failure, or 0 on
 16    success. */                                     16    success. */
 17 local int gz_init(state)                           17 local int gz_init(state)
 18     gz_statep state;                               18     gz_statep state;
 19 {                                                  19 {
 20     int ret;                                       20     int ret;
 21     z_streamp strm = &(state->strm);               21     z_streamp strm = &(state->strm);
 22                                                    22 
 23     /* allocate input buffer (double size for      23     /* allocate input buffer (double size for gzprintf) */
 24     state->in = (unsigned char *)malloc(state-     24     state->in = (unsigned char *)malloc(state->want << 1);
 25     if (state->in == NULL) {                       25     if (state->in == NULL) {
 26         gz_error(state, Z_MEM_ERROR, "out of m     26         gz_error(state, Z_MEM_ERROR, "out of memory");
 27         return -1;                                 27         return -1;
 28     }                                              28     }
 29                                                    29 
 30     /* only need output buffer and deflate sta     30     /* only need output buffer and deflate state if compressing */
 31     if (!state->direct) {                          31     if (!state->direct) {
 32         /* allocate output buffer */               32         /* allocate output buffer */
 33         state->out = (unsigned char *)malloc(s     33         state->out = (unsigned char *)malloc(state->want);
 34         if (state->out == NULL) {                  34         if (state->out == NULL) {
 35             free(state->in);                       35             free(state->in);
 36             gz_error(state, Z_MEM_ERROR, "out      36             gz_error(state, Z_MEM_ERROR, "out of memory");
 37             return -1;                             37             return -1;
 38         }                                          38         }
 39                                                    39 
 40         /* allocate deflate memory, set up for     40         /* allocate deflate memory, set up for gzip compression */
 41         strm->zalloc = Z_NULL;                     41         strm->zalloc = Z_NULL;
 42         strm->zfree = Z_NULL;                      42         strm->zfree = Z_NULL;
 43         strm->opaque = Z_NULL;                     43         strm->opaque = Z_NULL;
 44         ret = deflateInit2(strm, state->level,     44         ret = deflateInit2(strm, state->level, Z_DEFLATED,
 45                            MAX_WBITS + 16, DEF     45                            MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
 46         if (ret != Z_OK) {                         46         if (ret != Z_OK) {
 47             free(state->out);                      47             free(state->out);
 48             free(state->in);                       48             free(state->in);
 49             gz_error(state, Z_MEM_ERROR, "out      49             gz_error(state, Z_MEM_ERROR, "out of memory");
 50             return -1;                             50             return -1;
 51         }                                          51         }
 52         strm->next_in = NULL;                      52         strm->next_in = NULL;
 53     }                                              53     }
 54                                                    54 
 55     /* mark state as initialized */                55     /* mark state as initialized */
 56     state->size = state->want;                     56     state->size = state->want;
 57                                                    57 
 58     /* initialize write buffer if compressing      58     /* initialize write buffer if compressing */
 59     if (!state->direct) {                          59     if (!state->direct) {
 60         strm->avail_out = state->size;             60         strm->avail_out = state->size;
 61         strm->next_out = state->out;               61         strm->next_out = state->out;
 62         state->x.next = strm->next_out;            62         state->x.next = strm->next_out;
 63     }                                              63     }
 64     return 0;                                      64     return 0;
 65 }                                                  65 }
 66                                                    66 
 67 /* Compress whatever is at avail_in and next_i     67 /* Compress whatever is at avail_in and next_in and write to the output file.
 68    Return -1 if there is an error writing to t     68    Return -1 if there is an error writing to the output file or if gz_init()
 69    fails to allocate memory, otherwise 0.  flu     69    fails to allocate memory, otherwise 0.  flush is assumed to be a valid
 70    deflate() flush value.  If flush is Z_FINIS     70    deflate() flush value.  If flush is Z_FINISH, then the deflate() state is
 71    reset to start a new gzip stream.  If gz->d     71    reset to start a new gzip stream.  If gz->direct is true, then simply write
 72    to the output file without compressing, and     72    to the output file without compressing, and ignore flush. */
 73 local int gz_comp(state, flush)                    73 local int gz_comp(state, flush)
 74     gz_statep state;                               74     gz_statep state;
 75     int flush;                                     75     int flush;
 76 {                                                  76 {
 77     int ret, writ;                                 77     int ret, writ;
 78     unsigned have, put, max = ((unsigned)-1 >>     78     unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
 79     z_streamp strm = &(state->strm);               79     z_streamp strm = &(state->strm);
 80                                                    80 
 81     /* allocate memory if this is the first ti     81     /* allocate memory if this is the first time through */
 82     if (state->size == 0 && gz_init(state) ==      82     if (state->size == 0 && gz_init(state) == -1)
 83         return -1;                                 83         return -1;
 84                                                    84 
 85     /* write directly if requested */              85     /* write directly if requested */
 86     if (state->direct) {                           86     if (state->direct) {
 87         while (strm->avail_in) {                   87         while (strm->avail_in) {
 88             put = strm->avail_in > max ? max :     88             put = strm->avail_in > max ? max : strm->avail_in;
 89             writ = write(state->fd, strm->next     89             writ = write(state->fd, strm->next_in, put);
 90             if (writ < 0) {                        90             if (writ < 0) {
 91                 gz_error(state, Z_ERRNO, zstre     91                 gz_error(state, Z_ERRNO, zstrerror());
 92                 return -1;                         92                 return -1;
 93             }                                      93             }
 94             strm->avail_in -= (unsigned)writ;      94             strm->avail_in -= (unsigned)writ;
 95             strm->next_in += writ;                 95             strm->next_in += writ;
 96         }                                          96         }
 97         return 0;                                  97         return 0;
 98     }                                              98     }
 99                                                    99 
100     /* check for a pending reset */            << 
101     if (state->reset) {                        << 
102         /* don't start a new gzip member unles << 
103         if (strm->avail_in == 0)               << 
104             return 0;                          << 
105         deflateReset(strm);                    << 
106         state->reset = 0;                      << 
107     }                                          << 
108                                                << 
109     /* run deflate() on provided input until i    100     /* run deflate() on provided input until it produces no more output */
110     ret = Z_OK;                                   101     ret = Z_OK;
111     do {                                          102     do {
112         /* write out current buffer contents i    103         /* write out current buffer contents if full, or if flushing, but if
113            doing Z_FINISH then don't write unt    104            doing Z_FINISH then don't write until we get to Z_STREAM_END */
114         if (strm->avail_out == 0 || (flush !=     105         if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
115             (flush != Z_FINISH || ret == Z_STR    106             (flush != Z_FINISH || ret == Z_STREAM_END))) {
116             while (strm->next_out > state->x.n    107             while (strm->next_out > state->x.next) {
117                 put = strm->next_out - state->    108                 put = strm->next_out - state->x.next > (int)max ? max :
118                       (unsigned)(strm->next_ou    109                       (unsigned)(strm->next_out - state->x.next);
119                 writ = write(state->fd, state-    110                 writ = write(state->fd, state->x.next, put);
120                 if (writ < 0) {                   111                 if (writ < 0) {
121                     gz_error(state, Z_ERRNO, z    112                     gz_error(state, Z_ERRNO, zstrerror());
122                     return -1;                    113                     return -1;
123                 }                                 114                 }
124                 state->x.next += writ;            115                 state->x.next += writ;
125             }                                     116             }
126             if (strm->avail_out == 0) {           117             if (strm->avail_out == 0) {
127                 strm->avail_out = state->size;    118                 strm->avail_out = state->size;
128                 strm->next_out = state->out;      119                 strm->next_out = state->out;
129                 state->x.next = state->out;       120                 state->x.next = state->out;
130             }                                     121             }
131         }                                         122         }
132                                                   123 
133         /* compress */                            124         /* compress */
134         have = strm->avail_out;                   125         have = strm->avail_out;
135         ret = deflate(strm, flush);               126         ret = deflate(strm, flush);
136         if (ret == Z_STREAM_ERROR) {              127         if (ret == Z_STREAM_ERROR) {
137             gz_error(state, Z_STREAM_ERROR,       128             gz_error(state, Z_STREAM_ERROR,
138                       "internal error: deflate    129                       "internal error: deflate stream corrupt");
139             return -1;                            130             return -1;
140         }                                         131         }
141         have -= strm->avail_out;                  132         have -= strm->avail_out;
142     } while (have);                               133     } while (have);
143                                                   134 
144     /* if that completed a deflate stream, all    135     /* if that completed a deflate stream, allow another to start */
145     if (flush == Z_FINISH)                        136     if (flush == Z_FINISH)
146         state->reset = 1;                      << 137         deflateReset(strm);
147                                                   138 
148     /* all done, no errors */                     139     /* all done, no errors */
149     return 0;                                     140     return 0;
150 }                                                 141 }
151                                                   142 
152 /* Compress len zeros to output.  Return -1 on    143 /* Compress len zeros to output.  Return -1 on a write error or memory
153    allocation failure by gz_comp(), or 0 on su    144    allocation failure by gz_comp(), or 0 on success. */
154 local int gz_zero(state, len)                     145 local int gz_zero(state, len)
155     gz_statep state;                              146     gz_statep state;
156     z_off64_t len;                                147     z_off64_t len;
157 {                                                 148 {
158     int first;                                    149     int first;
159     unsigned n;                                   150     unsigned n;
160     z_streamp strm = &(state->strm);              151     z_streamp strm = &(state->strm);
161                                                   152 
162     /* consume whatever's left in the input bu    153     /* consume whatever's left in the input buffer */
163     if (strm->avail_in && gz_comp(state, Z_NO_    154     if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
164         return -1;                                155         return -1;
165                                                   156 
166     /* compress len zeros (len guaranteed > 0)    157     /* compress len zeros (len guaranteed > 0) */
167     first = 1;                                    158     first = 1;
168     while (len) {                                 159     while (len) {
169         n = GT_OFF(state->size) || (z_off64_t)    160         n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
170             (unsigned)len : state->size;          161             (unsigned)len : state->size;
171         if (first) {                              162         if (first) {
172             memset(state->in, 0, n);              163             memset(state->in, 0, n);
173             first = 0;                            164             first = 0;
174         }                                         165         }
175         strm->avail_in = n;                       166         strm->avail_in = n;
176         strm->next_in = state->in;                167         strm->next_in = state->in;
177         state->x.pos += n;                        168         state->x.pos += n;
178         if (gz_comp(state, Z_NO_FLUSH) == -1)     169         if (gz_comp(state, Z_NO_FLUSH) == -1)
179             return -1;                            170             return -1;
180         len -= n;                                 171         len -= n;
181     }                                             172     }
182     return 0;                                     173     return 0;
183 }                                                 174 }
184                                                   175 
185 /* Write len bytes from buf to file.  Return t    176 /* Write len bytes from buf to file.  Return the number of bytes written.  If
186    the returned value is less than len, then t    177    the returned value is less than len, then there was an error. */
187 local z_size_t gz_write(state, buf, len)          178 local z_size_t gz_write(state, buf, len)
188     gz_statep state;                              179     gz_statep state;
189     voidpc buf;                                   180     voidpc buf;
190     z_size_t len;                                 181     z_size_t len;
191 {                                                 182 {
192     z_size_t put = len;                           183     z_size_t put = len;
193                                                   184 
194     /* if len is zero, avoid unnecessary opera    185     /* if len is zero, avoid unnecessary operations */
195     if (len == 0)                                 186     if (len == 0)
196         return 0;                                 187         return 0;
197                                                   188 
198     /* allocate memory if this is the first ti    189     /* allocate memory if this is the first time through */
199     if (state->size == 0 && gz_init(state) ==     190     if (state->size == 0 && gz_init(state) == -1)
200         return 0;                                 191         return 0;
201                                                   192 
202     /* check for seek request */                  193     /* check for seek request */
203     if (state->seek) {                            194     if (state->seek) {
204         state->seek = 0;                          195         state->seek = 0;
205         if (gz_zero(state, state->skip) == -1)    196         if (gz_zero(state, state->skip) == -1)
206             return 0;                             197             return 0;
207     }                                             198     }
208                                                   199 
209     /* for small len, copy to input buffer, ot    200     /* for small len, copy to input buffer, otherwise compress directly */
210     if (len < state->size) {                      201     if (len < state->size) {
211         /* copy to input buffer, compress when    202         /* copy to input buffer, compress when full */
212         do {                                      203         do {
213             unsigned have, copy;                  204             unsigned have, copy;
214                                                   205 
215             if (state->strm.avail_in == 0)        206             if (state->strm.avail_in == 0)
216                 state->strm.next_in = state->i    207                 state->strm.next_in = state->in;
217             have = (unsigned)((state->strm.nex    208             have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
218                               state->in);         209                               state->in);
219             copy = state->size - have;            210             copy = state->size - have;
220             if (copy > len)                       211             if (copy > len)
221                 copy = (unsigned)len;             212                 copy = (unsigned)len;
222             memcpy(state->in + have, buf, copy    213             memcpy(state->in + have, buf, copy);
223             state->strm.avail_in += copy;         214             state->strm.avail_in += copy;
224             state->x.pos += copy;                 215             state->x.pos += copy;
225             buf = (const char *)buf + copy;       216             buf = (const char *)buf + copy;
226             len -= copy;                          217             len -= copy;
227             if (len && gz_comp(state, Z_NO_FLU    218             if (len && gz_comp(state, Z_NO_FLUSH) == -1)
228                 return 0;                         219                 return 0;
229         } while (len);                            220         } while (len);
230     }                                             221     }
231     else {                                        222     else {
232         /* consume whatever's left in the inpu    223         /* consume whatever's left in the input buffer */
233         if (state->strm.avail_in && gz_comp(st    224         if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
234             return 0;                             225             return 0;
235                                                   226 
236         /* directly compress user buffer to fi    227         /* directly compress user buffer to file */
237         state->strm.next_in = (z_const Bytef *    228         state->strm.next_in = (z_const Bytef *)buf;
238         do {                                      229         do {
239             unsigned n = (unsigned)-1;            230             unsigned n = (unsigned)-1;
240             if (n > len)                          231             if (n > len)
241                 n = (unsigned)len;                232                 n = (unsigned)len;
242             state->strm.avail_in = n;             233             state->strm.avail_in = n;
243             state->x.pos += n;                    234             state->x.pos += n;
244             if (gz_comp(state, Z_NO_FLUSH) ==     235             if (gz_comp(state, Z_NO_FLUSH) == -1)
245                 return 0;                         236                 return 0;
246             len -= n;                             237             len -= n;
247         } while (len);                            238         } while (len);
248     }                                             239     }
249                                                   240 
250     /* input was all buffered or compressed */    241     /* input was all buffered or compressed */
251     return put;                                   242     return put;
252 }                                                 243 }
253                                                   244 
254 /* -- see zlib.h -- */                            245 /* -- see zlib.h -- */
255 int ZEXPORT gzwrite(file, buf, len)               246 int ZEXPORT gzwrite(file, buf, len)
256     gzFile file;                                  247     gzFile file;
257     voidpc buf;                                   248     voidpc buf;
258     unsigned len;                                 249     unsigned len;
259 {                                                 250 {
260     gz_statep state;                              251     gz_statep state;
261                                                   252 
262     /* get internal structure */                  253     /* get internal structure */
263     if (file == NULL)                             254     if (file == NULL)
264         return 0;                                 255         return 0;
265     state = (gz_statep)file;                      256     state = (gz_statep)file;
266                                                   257 
267     /* check that we're writing and that there    258     /* check that we're writing and that there's no error */
268     if (state->mode != GZ_WRITE || state->err     259     if (state->mode != GZ_WRITE || state->err != Z_OK)
269         return 0;                                 260         return 0;
270                                                   261 
271     /* since an int is returned, make sure len    262     /* since an int is returned, make sure len fits in one, otherwise return
272        with an error (this avoids a flaw in th    263        with an error (this avoids a flaw in the interface) */
273     if ((int)len < 0) {                           264     if ((int)len < 0) {
274         gz_error(state, Z_DATA_ERROR, "request    265         gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
275         return 0;                                 266         return 0;
276     }                                             267     }
277                                                   268 
278     /* write len bytes from buf (the return va    269     /* write len bytes from buf (the return value will fit in an int) */
279     return (int)gz_write(state, buf, len);        270     return (int)gz_write(state, buf, len);
280 }                                                 271 }
281                                                   272 
282 /* -- see zlib.h -- */                            273 /* -- see zlib.h -- */
283 z_size_t ZEXPORT gzfwrite(buf, size, nitems, f    274 z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
284     voidpc buf;                                   275     voidpc buf;
285     z_size_t size;                                276     z_size_t size;
286     z_size_t nitems;                              277     z_size_t nitems;
287     gzFile file;                                  278     gzFile file;
288 {                                                 279 {
289     z_size_t len;                                 280     z_size_t len;
290     gz_statep state;                              281     gz_statep state;
291                                                   282 
292     /* get internal structure */                  283     /* get internal structure */
293     if (file == NULL)                             284     if (file == NULL)
294         return 0;                                 285         return 0;
295     state = (gz_statep)file;                      286     state = (gz_statep)file;
296                                                   287 
297     /* check that we're writing and that there    288     /* check that we're writing and that there's no error */
298     if (state->mode != GZ_WRITE || state->err     289     if (state->mode != GZ_WRITE || state->err != Z_OK)
299         return 0;                                 290         return 0;
300                                                   291 
301     /* compute bytes to read -- error on overf    292     /* compute bytes to read -- error on overflow */
302     len = nitems * size;                          293     len = nitems * size;
303     if (size && len / size != nitems) {           294     if (size && len / size != nitems) {
304         gz_error(state, Z_STREAM_ERROR, "reque    295         gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
305         return 0;                                 296         return 0;
306     }                                             297     }
307                                                   298 
308     /* write len bytes to buf, return the numb    299     /* write len bytes to buf, return the number of full items written */
309     return len ? gz_write(state, buf, len) / s    300     return len ? gz_write(state, buf, len) / size : 0;
310 }                                                 301 }
311                                                   302 
312 /* -- see zlib.h -- */                            303 /* -- see zlib.h -- */
313 int ZEXPORT gzputc(file, c)                       304 int ZEXPORT gzputc(file, c)
314     gzFile file;                                  305     gzFile file;
315     int c;                                        306     int c;
316 {                                                 307 {
317     unsigned have;                                308     unsigned have;
318     unsigned char buf[1];                         309     unsigned char buf[1];
319     gz_statep state;                              310     gz_statep state;
320     z_streamp strm;                               311     z_streamp strm;
321                                                   312 
322     /* get internal structure */                  313     /* get internal structure */
323     if (file == NULL)                             314     if (file == NULL)
324         return -1;                                315         return -1;
325     state = (gz_statep)file;                      316     state = (gz_statep)file;
326     strm = &(state->strm);                        317     strm = &(state->strm);
327                                                   318 
328     /* check that we're writing and that there    319     /* check that we're writing and that there's no error */
329     if (state->mode != GZ_WRITE || state->err     320     if (state->mode != GZ_WRITE || state->err != Z_OK)
330         return -1;                                321         return -1;
331                                                   322 
332     /* check for seek request */                  323     /* check for seek request */
333     if (state->seek) {                            324     if (state->seek) {
334         state->seek = 0;                          325         state->seek = 0;
335         if (gz_zero(state, state->skip) == -1)    326         if (gz_zero(state, state->skip) == -1)
336             return -1;                            327             return -1;
337     }                                             328     }
338                                                   329 
339     /* try writing to input buffer for speed (    330     /* try writing to input buffer for speed (state->size == 0 if buffer not
340        initialized) */                            331        initialized) */
341     if (state->size) {                            332     if (state->size) {
342         if (strm->avail_in == 0)                  333         if (strm->avail_in == 0)
343             strm->next_in = state->in;            334             strm->next_in = state->in;
344         have = (unsigned)((strm->next_in + str    335         have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
345         if (have < state->size) {                 336         if (have < state->size) {
346             state->in[have] = (unsigned char)c    337             state->in[have] = (unsigned char)c;
347             strm->avail_in++;                     338             strm->avail_in++;
348             state->x.pos++;                       339             state->x.pos++;
349             return c & 0xff;                      340             return c & 0xff;
350         }                                         341         }
351     }                                             342     }
352                                                   343 
353     /* no room in buffer or not initialized, u    344     /* no room in buffer or not initialized, use gz_write() */
354     buf[0] = (unsigned char)c;                    345     buf[0] = (unsigned char)c;
355     if (gz_write(state, buf, 1) != 1)             346     if (gz_write(state, buf, 1) != 1)
356         return -1;                                347         return -1;
357     return c & 0xff;                              348     return c & 0xff;
358 }                                                 349 }
359                                                   350 
360 /* -- see zlib.h -- */                            351 /* -- see zlib.h -- */
361 int ZEXPORT gzputs(file, s)                    << 352 int ZEXPORT gzputs(file, str)
362     gzFile file;                                  353     gzFile file;
363     const char *s;                             << 354     const char *str;
364 {                                                 355 {
365     z_size_t len, put;                         << 356     int ret;
                                                   >> 357     z_size_t len;
366     gz_statep state;                              358     gz_statep state;
367                                                   359 
368     /* get internal structure */                  360     /* get internal structure */
369     if (file == NULL)                             361     if (file == NULL)
370         return -1;                                362         return -1;
371     state = (gz_statep)file;                      363     state = (gz_statep)file;
372                                                   364 
373     /* check that we're writing and that there    365     /* check that we're writing and that there's no error */
374     if (state->mode != GZ_WRITE || state->err     366     if (state->mode != GZ_WRITE || state->err != Z_OK)
375         return -1;                                367         return -1;
376                                                   368 
377     /* write string */                            369     /* write string */
378     len = strlen(s);                           << 370     len = strlen(str);
379     if ((int)len < 0 || (unsigned)len != len)  << 371     if (len > INT_MAX) {
380         gz_error(state, Z_STREAM_ERROR, "strin << 372   gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
381         return -1;                             << 373   return -1;
382     }                                             374     }
383     put = gz_write(state, s, len);             << 375 
384     return put < len ? -1 : (int)len;          << 376     /* write len bytes from buf (the return value will fit in an int) */
                                                   >> 377     ret = (int)gz_write(state, str, len);
                                                   >> 378     return ret < (int)len ? -1 : ret;
385 }                                                 379 }
386                                                   380 
387 #if defined(STDC) || defined(Z_HAVE_STDARG_H)     381 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
388 #include <stdarg.h>                               382 #include <stdarg.h>
389                                                   383 
390 /* -- see zlib.h -- */                            384 /* -- see zlib.h -- */
391 int ZEXPORTVA gzvprintf(gzFile file, const cha    385 int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
392 {                                                 386 {
393     int len;                                      387     int len;
394     unsigned left;                                388     unsigned left;
395     char *next;                                   389     char *next;
396     gz_statep state;                              390     gz_statep state;
397     z_streamp strm;                               391     z_streamp strm;
398                                                   392 
399     /* get internal structure */                  393     /* get internal structure */
400     if (file == NULL)                             394     if (file == NULL)
401         return Z_STREAM_ERROR;                    395         return Z_STREAM_ERROR;
402     state = (gz_statep)file;                      396     state = (gz_statep)file;
403     strm = &(state->strm);                        397     strm = &(state->strm);
404                                                   398 
405     /* check that we're writing and that there    399     /* check that we're writing and that there's no error */
406     if (state->mode != GZ_WRITE || state->err     400     if (state->mode != GZ_WRITE || state->err != Z_OK)
407         return Z_STREAM_ERROR;                    401         return Z_STREAM_ERROR;
408                                                   402 
409     /* make sure we have some buffer space */     403     /* make sure we have some buffer space */
410     if (state->size == 0 && gz_init(state) ==     404     if (state->size == 0 && gz_init(state) == -1)
411         return state->err;                        405         return state->err;
412                                                   406 
413     /* check for seek request */                  407     /* check for seek request */
414     if (state->seek) {                            408     if (state->seek) {
415         state->seek = 0;                          409         state->seek = 0;
416         if (gz_zero(state, state->skip) == -1)    410         if (gz_zero(state, state->skip) == -1)
417             return state->err;                    411             return state->err;
418     }                                             412     }
419                                                   413 
420     /* do the printf() into the input buffer,     414     /* do the printf() into the input buffer, put length in len -- the input
421        buffer is double-sized just for this fu    415        buffer is double-sized just for this function, so there is guaranteed to
422        be state->size bytes available after th    416        be state->size bytes available after the current contents */
423     if (strm->avail_in == 0)                      417     if (strm->avail_in == 0)
424         strm->next_in = state->in;                418         strm->next_in = state->in;
425     next = (char *)(state->in + (strm->next_in    419     next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
426     next[state->size - 1] = 0;                    420     next[state->size - 1] = 0;
427 #ifdef NO_vsnprintf                               421 #ifdef NO_vsnprintf
428 #  ifdef HAS_vsprintf_void                        422 #  ifdef HAS_vsprintf_void
429     (void)vsprintf(next, format, va);             423     (void)vsprintf(next, format, va);
430     for (len = 0; len < state->size; len++)       424     for (len = 0; len < state->size; len++)
431         if (next[len] == 0) break;                425         if (next[len] == 0) break;
432 #  else                                           426 #  else
433     len = vsprintf(next, format, va);             427     len = vsprintf(next, format, va);
434 #  endif                                          428 #  endif
435 #else                                             429 #else
436 #  ifdef HAS_vsnprintf_void                       430 #  ifdef HAS_vsnprintf_void
437     (void)vsnprintf(next, state->size, format,    431     (void)vsnprintf(next, state->size, format, va);
438     len = strlen(next);                           432     len = strlen(next);
439 #  else                                           433 #  else
440     len = vsnprintf(next, state->size, format,    434     len = vsnprintf(next, state->size, format, va);
441 #  endif                                          435 #  endif
442 #endif                                            436 #endif
443                                                   437 
444     /* check that printf() results fit in buff    438     /* check that printf() results fit in buffer */
445     if (len == 0 || (unsigned)len >= state->si    439     if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
446         return 0;                                 440         return 0;
447                                                   441 
448     /* update buffer and position, compress fi    442     /* update buffer and position, compress first half if past that */
449     strm->avail_in += (unsigned)len;              443     strm->avail_in += (unsigned)len;
450     state->x.pos += len;                          444     state->x.pos += len;
451     if (strm->avail_in >= state->size) {          445     if (strm->avail_in >= state->size) {
452         left = strm->avail_in - state->size;      446         left = strm->avail_in - state->size;
453         strm->avail_in = state->size;             447         strm->avail_in = state->size;
454         if (gz_comp(state, Z_NO_FLUSH) == -1)     448         if (gz_comp(state, Z_NO_FLUSH) == -1)
455             return state->err;                    449             return state->err;
456         memmove(state->in, state->in + state-> << 450         memcpy(state->in, state->in + state->size, left);
457         strm->next_in = state->in;                451         strm->next_in = state->in;
458         strm->avail_in = left;                    452         strm->avail_in = left;
459     }                                             453     }
460     return len;                                   454     return len;
461 }                                                 455 }
462                                                   456 
463 int ZEXPORTVA gzprintf(gzFile file, const char    457 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
464 {                                                 458 {
465     va_list va;                                   459     va_list va;
466     int ret;                                      460     int ret;
467                                                   461 
468     va_start(va, format);                         462     va_start(va, format);
469     ret = gzvprintf(file, format, va);            463     ret = gzvprintf(file, format, va);
470     va_end(va);                                   464     va_end(va);
471     return ret;                                   465     return ret;
472 }                                                 466 }
473                                                   467 
474 #else /* !STDC && !Z_HAVE_STDARG_H */             468 #else /* !STDC && !Z_HAVE_STDARG_H */
475                                                   469 
476 /* -- see zlib.h -- */                            470 /* -- see zlib.h -- */
477 int ZEXPORTVA gzprintf(file, format, a1, a2, a << 471 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
478                        a11, a12, a13, a14, a15    472                        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
479     gzFile file;                                  473     gzFile file;
480     const char *format;                           474     const char *format;
481     int a1, a2, a3, a4, a5, a6, a7, a8, a9, a1    475     int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
482         a11, a12, a13, a14, a15, a16, a17, a18    476         a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
483 {                                                 477 {
484     unsigned len, left;                           478     unsigned len, left;
485     char *next;                                   479     char *next;
486     gz_statep state;                              480     gz_statep state;
487     z_streamp strm;                               481     z_streamp strm;
488                                                   482 
489     /* get internal structure */                  483     /* get internal structure */
490     if (file == NULL)                             484     if (file == NULL)
491         return Z_STREAM_ERROR;                    485         return Z_STREAM_ERROR;
492     state = (gz_statep)file;                      486     state = (gz_statep)file;
493     strm = &(state->strm);                        487     strm = &(state->strm);
494                                                   488 
495     /* check that can really pass pointer in i    489     /* check that can really pass pointer in ints */
496     if (sizeof(int) != sizeof(void *))            490     if (sizeof(int) != sizeof(void *))
497         return Z_STREAM_ERROR;                    491         return Z_STREAM_ERROR;
498                                                   492 
499     /* check that we're writing and that there    493     /* check that we're writing and that there's no error */
500     if (state->mode != GZ_WRITE || state->err     494     if (state->mode != GZ_WRITE || state->err != Z_OK)
501         return Z_STREAM_ERROR;                    495         return Z_STREAM_ERROR;
502                                                   496 
503     /* make sure we have some buffer space */     497     /* make sure we have some buffer space */
504     if (state->size == 0 && gz_init(state) ==     498     if (state->size == 0 && gz_init(state) == -1)
505         return state->error;                      499         return state->error;
506                                                   500 
507     /* check for seek request */                  501     /* check for seek request */
508     if (state->seek) {                            502     if (state->seek) {
509         state->seek = 0;                          503         state->seek = 0;
510         if (gz_zero(state, state->skip) == -1)    504         if (gz_zero(state, state->skip) == -1)
511             return state->error;                  505             return state->error;
512     }                                             506     }
513                                                   507 
514     /* do the printf() into the input buffer,     508     /* do the printf() into the input buffer, put length in len -- the input
515        buffer is double-sized just for this fu    509        buffer is double-sized just for this function, so there is guaranteed to
516        be state->size bytes available after th    510        be state->size bytes available after the current contents */
517     if (strm->avail_in == 0)                      511     if (strm->avail_in == 0)
518         strm->next_in = state->in;                512         strm->next_in = state->in;
519     next = (char *)(strm->next_in + strm->avai    513     next = (char *)(strm->next_in + strm->avail_in);
520     next[state->size - 1] = 0;                    514     next[state->size - 1] = 0;
521 #ifdef NO_snprintf                                515 #ifdef NO_snprintf
522 #  ifdef HAS_sprintf_void                         516 #  ifdef HAS_sprintf_void
523     sprintf(next, format, a1, a2, a3, a4, a5,     517     sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
524             a13, a14, a15, a16, a17, a18, a19,    518             a13, a14, a15, a16, a17, a18, a19, a20);
525     for (len = 0; len < size; len++)              519     for (len = 0; len < size; len++)
526         if (next[len] == 0)                       520         if (next[len] == 0)
527             break;                                521             break;
528 #  else                                           522 #  else
529     len = sprintf(next, format, a1, a2, a3, a4    523     len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
530                   a12, a13, a14, a15, a16, a17    524                   a12, a13, a14, a15, a16, a17, a18, a19, a20);
531 #  endif                                          525 #  endif
532 #else                                             526 #else
533 #  ifdef HAS_snprintf_void                        527 #  ifdef HAS_snprintf_void
534     snprintf(next, state->size, format, a1, a2    528     snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
535              a10, a11, a12, a13, a14, a15, a16    529              a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
536     len = strlen(next);                           530     len = strlen(next);
537 #  else                                           531 #  else
538     len = snprintf(next, state->size, format,     532     len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
539                    a9, a10, a11, a12, a13, a14    533                    a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
540 #  endif                                          534 #  endif
541 #endif                                            535 #endif
542                                                   536 
543     /* check that printf() results fit in buff    537     /* check that printf() results fit in buffer */
544     if (len == 0 || len >= state->size || next    538     if (len == 0 || len >= state->size || next[state->size - 1] != 0)
545         return 0;                                 539         return 0;
546                                                   540 
547     /* update buffer and position, compress fi    541     /* update buffer and position, compress first half if past that */
548     strm->avail_in += len;                        542     strm->avail_in += len;
549     state->x.pos += len;                          543     state->x.pos += len;
550     if (strm->avail_in >= state->size) {          544     if (strm->avail_in >= state->size) {
551         left = strm->avail_in - state->size;      545         left = strm->avail_in - state->size;
552         strm->avail_in = state->size;             546         strm->avail_in = state->size;
553         if (gz_comp(state, Z_NO_FLUSH) == -1)     547         if (gz_comp(state, Z_NO_FLUSH) == -1)
554             return state->err;                    548             return state->err;
555         memmove(state->in, state->in + state-> << 549         memcpy(state->in, state->in + state->size, left);
556         strm->next_in = state->in;                550         strm->next_in = state->in;
557         strm->avail_in = left;                    551         strm->avail_in = left;
558     }                                             552     }
559     return (int)len;                              553     return (int)len;
560 }                                                 554 }
561                                                   555 
562 #endif                                            556 #endif
563                                                   557 
564 /* -- see zlib.h -- */                            558 /* -- see zlib.h -- */
565 int ZEXPORT gzflush(file, flush)                  559 int ZEXPORT gzflush(file, flush)
566     gzFile file;                                  560     gzFile file;
567     int flush;                                    561     int flush;
568 {                                                 562 {
569     gz_statep state;                              563     gz_statep state;
570                                                   564 
571     /* get internal structure */                  565     /* get internal structure */
572     if (file == NULL)                             566     if (file == NULL)
573         return Z_STREAM_ERROR;                    567         return Z_STREAM_ERROR;
574     state = (gz_statep)file;                      568     state = (gz_statep)file;
575                                                   569 
576     /* check that we're writing and that there    570     /* check that we're writing and that there's no error */
577     if (state->mode != GZ_WRITE || state->err     571     if (state->mode != GZ_WRITE || state->err != Z_OK)
578         return Z_STREAM_ERROR;                    572         return Z_STREAM_ERROR;
579                                                   573 
580     /* check flush parameter */                   574     /* check flush parameter */
581     if (flush < 0 || flush > Z_FINISH)            575     if (flush < 0 || flush > Z_FINISH)
582         return Z_STREAM_ERROR;                    576         return Z_STREAM_ERROR;
583                                                   577 
584     /* check for seek request */                  578     /* check for seek request */
585     if (state->seek) {                            579     if (state->seek) {
586         state->seek = 0;                          580         state->seek = 0;
587         if (gz_zero(state, state->skip) == -1)    581         if (gz_zero(state, state->skip) == -1)
588             return state->err;                    582             return state->err;
589     }                                             583     }
590                                                   584 
591     /* compress remaining data with requested     585     /* compress remaining data with requested flush */
592     (void)gz_comp(state, flush);                  586     (void)gz_comp(state, flush);
593     return state->err;                            587     return state->err;
594 }                                                 588 }
595                                                   589 
596 /* -- see zlib.h -- */                            590 /* -- see zlib.h -- */
597 int ZEXPORT gzsetparams(file, level, strategy)    591 int ZEXPORT gzsetparams(file, level, strategy)
598     gzFile file;                                  592     gzFile file;
599     int level;                                    593     int level;
600     int strategy;                                 594     int strategy;
601 {                                                 595 {
602     gz_statep state;                              596     gz_statep state;
603     z_streamp strm;                               597     z_streamp strm;
604                                                   598 
605     /* get internal structure */                  599     /* get internal structure */
606     if (file == NULL)                             600     if (file == NULL)
607         return Z_STREAM_ERROR;                    601         return Z_STREAM_ERROR;
608     state = (gz_statep)file;                      602     state = (gz_statep)file;
609     strm = &(state->strm);                        603     strm = &(state->strm);
610                                                   604 
611     /* check that we're writing and that there    605     /* check that we're writing and that there's no error */
612     if (state->mode != GZ_WRITE || state->err     606     if (state->mode != GZ_WRITE || state->err != Z_OK)
613         return Z_STREAM_ERROR;                    607         return Z_STREAM_ERROR;
614                                                   608 
615     /* if no change is requested, then do noth    609     /* if no change is requested, then do nothing */
616     if (level == state->level && strategy == s    610     if (level == state->level && strategy == state->strategy)
617         return Z_OK;                              611         return Z_OK;
618                                                   612 
619     /* check for seek request */                  613     /* check for seek request */
620     if (state->seek) {                            614     if (state->seek) {
621         state->seek = 0;                          615         state->seek = 0;
622         if (gz_zero(state, state->skip) == -1)    616         if (gz_zero(state, state->skip) == -1)
623             return state->err;                    617             return state->err;
624     }                                             618     }
625                                                   619 
626     /* change compression parameters for subse    620     /* change compression parameters for subsequent input */
627     if (state->size) {                            621     if (state->size) {
628         /* flush previous input with previous     622         /* flush previous input with previous parameters before changing */
629         if (strm->avail_in && gz_comp(state, Z    623         if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
630             return state->err;                    624             return state->err;
631         deflateParams(strm, level, strategy);     625         deflateParams(strm, level, strategy);
632     }                                             626     }
633     state->level = level;                         627     state->level = level;
634     state->strategy = strategy;                   628     state->strategy = strategy;
635     return Z_OK;                                  629     return Z_OK;
636 }                                                 630 }
637                                                   631 
638 /* -- see zlib.h -- */                            632 /* -- see zlib.h -- */
639 int ZEXPORT gzclose_w(file)                       633 int ZEXPORT gzclose_w(file)
640     gzFile file;                                  634     gzFile file;
641 {                                                 635 {
642     int ret = Z_OK;                               636     int ret = Z_OK;
643     gz_statep state;                              637     gz_statep state;
644                                                   638 
645     /* get internal structure */                  639     /* get internal structure */
646     if (file == NULL)                             640     if (file == NULL)
647         return Z_STREAM_ERROR;                    641         return Z_STREAM_ERROR;
648     state = (gz_statep)file;                      642     state = (gz_statep)file;
649                                                   643 
650     /* check that we're writing */                644     /* check that we're writing */
651     if (state->mode != GZ_WRITE)                  645     if (state->mode != GZ_WRITE)
652         return Z_STREAM_ERROR;                    646         return Z_STREAM_ERROR;
653                                                   647 
654     /* check for seek request */                  648     /* check for seek request */
655     if (state->seek) {                            649     if (state->seek) {
656         state->seek = 0;                          650         state->seek = 0;
657         if (gz_zero(state, state->skip) == -1)    651         if (gz_zero(state, state->skip) == -1)
658             ret = state->err;                     652             ret = state->err;
659     }                                             653     }
660                                                   654 
661     /* flush, free memory, and close file */      655     /* flush, free memory, and close file */
662     if (gz_comp(state, Z_FINISH) == -1)           656     if (gz_comp(state, Z_FINISH) == -1)
663         ret = state->err;                         657         ret = state->err;
664     if (state->size) {                            658     if (state->size) {
665         if (!state->direct) {                     659         if (!state->direct) {
666             (void)deflateEnd(&(state->strm));     660             (void)deflateEnd(&(state->strm));
667             free(state->out);                     661             free(state->out);
668         }                                         662         }
669         free(state->in);                          663         free(state->in);
670     }                                             664     }
671     gz_error(state, Z_OK, NULL);                  665     gz_error(state, Z_OK, NULL);
672     free(state->path);                            666     free(state->path);
673     if (close(state->fd) == -1)                   667     if (close(state->fd) == -1)
674         ret = Z_ERRNO;                            668         ret = Z_ERRNO;
675     free(state);                                  669     free(state);
676     return ret;                                   670     return ret;
677 }                                                 671 }
678                                                   672