Coverage Report

Created: 2025-06-24 06:45

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