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