Subversion Repositories Kolibri OS

Rev

Rev 1896 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1896 Rev 3926
Line 1... Line 1...
1
/* gzwrite.c -- zlib functions for writing gzip files
1
/* gzwrite.c -- zlib functions for writing gzip files
2
 * Copyright (C) 2004, 2005, 2010 Mark Adler
2
 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
4
 */
Line 5... Line 5...
5
 
5
 
Line 16... Line 16...
16
    gz_statep state;
16
    gz_statep state;
17
{
17
{
18
    int ret;
18
    int ret;
19
    z_streamp strm = &(state->strm);
19
    z_streamp strm = &(state->strm);
Line 20... Line 20...
20
 
20
 
21
    /* allocate input and output buffers */
21
    /* allocate input buffer */
22
    state->in = malloc(state->want);
22
    state->in = (unsigned char *)malloc(state->want);
23
    state->out = malloc(state->want);
23
    if (state->in == NULL) {
-
 
24
        gz_error(state, Z_MEM_ERROR, "out of memory");
-
 
25
        return -1;
-
 
26
    }
-
 
27
 
24
    if (state->in == NULL || state->out == NULL) {
28
    /* only need output buffer and deflate state if compressing */
25
        if (state->out != NULL)
29
    if (!state->direct) {
-
 
30
        /* allocate output buffer */
26
            free(state->out);
31
        state->out = (unsigned char *)malloc(state->want);
27
        if (state->in != NULL)
32
        if (state->out == NULL) {
28
            free(state->in);
33
            free(state->in);
29
        gz_error(state, Z_MEM_ERROR, "out of memory");
34
            gz_error(state, Z_MEM_ERROR, "out of memory");
30
        return -1;
35
            return -1;
Line 31... Line 36...
31
    }
36
        }
32
 
37
 
33
    /* allocate deflate memory, set up for gzip compression */
38
        /* allocate deflate memory, set up for gzip compression */
34
    strm->zalloc = Z_NULL;
39
        strm->zalloc = Z_NULL;
35
    strm->zfree = Z_NULL;
40
        strm->zfree = Z_NULL;
36
    strm->opaque = Z_NULL;
41
        strm->opaque = Z_NULL;
37
    ret = deflateInit2(strm, state->level, Z_DEFLATED,
42
        ret = deflateInit2(strm, state->level, Z_DEFLATED,
-
 
43
                           MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
38
                       15 + 16, 8, state->strategy);
44
        if (ret != Z_OK) {
39
    if (ret != Z_OK) {
45
            free(state->out);
40
        free(state->in);
46
            free(state->in);
41
        gz_error(state, Z_MEM_ERROR, "out of memory");
47
            gz_error(state, Z_MEM_ERROR, "out of memory");
-
 
48
            return -1;
Line 42... Line 49...
42
        return -1;
49
        }
43
    }
50
    }
Line 44... Line 51...
44
 
51
 
-
 
52
    /* mark state as initialized */
45
    /* mark state as initialized */
53
    state->size = state->want;
46
    state->size = state->want;
54
 
47
 
55
    /* initialize write buffer if compressing */
-
 
56
    if (!state->direct) {
48
    /* initialize write buffer */
57
        strm->avail_out = state->size;
49
    strm->avail_out = state->size;
58
        strm->next_out = state->out;
Line 50... Line 59...
50
    strm->next_out = state->out;
59
        state->x.next = strm->next_out;
51
    state->next = strm->next_out;
60
    }
52
    return 0;
61
    return 0;
53
}
62
}
-
 
63
 
-
 
64
/* Compress whatever is at avail_in and next_in and write to the output file.
54
 
65
   Return -1 if there is an error writing to the output file, otherwise 0.
55
/* Compress whatever is at avail_in and next_in and write to the output file.
66
   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
56
   Return -1 if there is an error writing to the output file, otherwise 0.
67
   then the deflate() state is reset to start a new gzip stream.  If gz->direct
57
   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
68
   is true, then simply write to the output file without compressing, and
58
   then the deflate() state is reset to start a new gzip stream. */
69
   ignore flush. */
Line 66... Line 77...
66
 
77
 
67
    /* allocate memory if this is the first time through */
78
    /* allocate memory if this is the first time through */
68
    if (state->size == 0 && gz_init(state) == -1)
79
    if (state->size == 0 && gz_init(state) == -1)
Line -... Line 80...
-
 
80
        return -1;
-
 
81
 
-
 
82
    /* write directly if requested */
-
 
83
    if (state->direct) {
-
 
84
        got = write(state->fd, strm->next_in, strm->avail_in);
-
 
85
        if (got < 0 || (unsigned)got != strm->avail_in) {
-
 
86
            gz_error(state, Z_ERRNO, zstrerror());
-
 
87
            return -1;
-
 
88
        }
-
 
89
        strm->avail_in = 0;
-
 
90
        return 0;
69
        return -1;
91
    }
70
 
92
 
71
    /* run deflate() on provided input until it produces no more output */
93
    /* run deflate() on provided input until it produces no more output */
72
    ret = Z_OK;
94
    ret = Z_OK;
73
    do {
95
    do {
74
        /* write out current buffer contents if full, or if flushing, but if
96
        /* write out current buffer contents if full, or if flushing, but if
75
           doing Z_FINISH then don't write until we get to Z_STREAM_END */
97
           doing Z_FINISH then don't write until we get to Z_STREAM_END */
76
        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
98
        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
77
            (flush != Z_FINISH || ret == Z_STREAM_END))) {
99
            (flush != Z_FINISH || ret == Z_STREAM_END))) {
78
            have = (unsigned)(strm->next_out - state->next);
100
            have = (unsigned)(strm->next_out - state->x.next);
79
            if (have && ((got = write(state->fd, state->next, have)) < 0 ||
101
            if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
80
                         (unsigned)got != have)) {
102
                         (unsigned)got != have)) {
81
                gz_error(state, Z_ERRNO, zstrerror());
103
                gz_error(state, Z_ERRNO, zstrerror());
82
                return -1;
104
                return -1;
83
            }
105
            }
84
            if (strm->avail_out == 0) {
106
            if (strm->avail_out == 0) {
85
                strm->avail_out = state->size;
107
                strm->avail_out = state->size;
86
                strm->next_out = state->out;
108
                strm->next_out = state->out;
87
            }
109
            }
Line 88... Line 110...
88
            state->next = strm->next_out;
110
            state->x.next = strm->next_out;
89
        }
111
        }
90
 
112
 
Line 129... Line 151...
129
            memset(state->in, 0, n);
151
            memset(state->in, 0, n);
130
            first = 0;
152
            first = 0;
131
        }
153
        }
132
        strm->avail_in = n;
154
        strm->avail_in = n;
133
        strm->next_in = state->in;
155
        strm->next_in = state->in;
134
        state->pos += n;
156
        state->x.pos += n;
135
        if (gz_comp(state, Z_NO_FLUSH) == -1)
157
        if (gz_comp(state, Z_NO_FLUSH) == -1)
136
            return -1;
158
            return -1;
137
        len -= n;
159
        len -= n;
138
    }
160
    }
139
    return 0;
161
    return 0;
Line 144... Line 166...
144
    gzFile file;
166
    gzFile file;
145
    voidpc buf;
167
    voidpc buf;
146
    unsigned len;
168
    unsigned len;
147
{
169
{
148
    unsigned put = len;
170
    unsigned put = len;
149
    unsigned n;
-
 
150
    gz_statep state;
171
    gz_statep state;
151
    z_streamp strm;
172
    z_streamp strm;
Line 152... Line 173...
152
 
173
 
153
    /* get internal structure */
174
    /* get internal structure */
Line 161... Line 182...
161
        return 0;
182
        return 0;
Line 162... Line 183...
162
 
183
 
163
    /* since an int is returned, make sure len fits in one, otherwise return
184
    /* since an int is returned, make sure len fits in one, otherwise return
164
       with an error (this avoids the flaw in the interface) */
185
       with an error (this avoids the flaw in the interface) */
165
    if ((int)len < 0) {
186
    if ((int)len < 0) {
166
        gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
187
        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
167
        return 0;
188
        return 0;
Line 168... Line 189...
168
    }
189
    }
169
 
190
 
Line 184... Line 205...
184
 
205
 
185
    /* for small len, copy to input buffer, otherwise compress directly */
206
    /* for small len, copy to input buffer, otherwise compress directly */
186
    if (len < state->size) {
207
    if (len < state->size) {
187
        /* copy to input buffer, compress when full */
208
        /* copy to input buffer, compress when full */
-
 
209
        do {
-
 
210
            unsigned have, copy;
188
        do {
211
 
189
            if (strm->avail_in == 0)
212
            if (strm->avail_in == 0)
-
 
213
                strm->next_in = state->in;
190
                strm->next_in = state->in;
214
            have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
191
            n = state->size - strm->avail_in;
215
            copy = state->size - have;
192
            if (n > len)
216
            if (copy > len)
193
                n = len;
217
                copy = len;
194
            memcpy(strm->next_in + strm->avail_in, buf, n);
218
            memcpy(state->in + have, buf, copy);
195
            strm->avail_in += n;
219
            strm->avail_in += copy;
196
            state->pos += n;
220
            state->x.pos += copy;
197
            buf = (char *)buf + n;
221
            buf = (const char *)buf + copy;
198
            len -= n;
222
            len -= copy;
199
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
223
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
200
                return 0;
224
                return 0;
201
        } while (len);
225
        } while (len);
202
    }
226
    }
Line 205... Line 229...
205
        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
229
        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
206
            return 0;
230
            return 0;
Line 207... Line 231...
207
 
231
 
208
        /* directly compress user buffer to file */
232
        /* directly compress user buffer to file */
209
        strm->avail_in = len;
233
        strm->avail_in = len;
210
        strm->next_in = (voidp)buf;
234
        strm->next_in = (z_const Bytef *)buf;
211
        state->pos += len;
235
        state->x.pos += len;
212
        if (gz_comp(state, Z_NO_FLUSH) == -1)
236
        if (gz_comp(state, Z_NO_FLUSH) == -1)
213
            return 0;
237
            return 0;
Line 214... Line 238...
214
    }
238
    }
Line 220... Line 244...
220
/* -- see zlib.h -- */
244
/* -- see zlib.h -- */
221
int ZEXPORT gzputc(file, c)
245
int ZEXPORT gzputc(file, c)
222
    gzFile file;
246
    gzFile file;
223
    int c;
247
    int c;
224
{
248
{
-
 
249
    unsigned have;
225
    unsigned char buf[1];
250
    unsigned char buf[1];
226
    gz_statep state;
251
    gz_statep state;
227
    z_streamp strm;
252
    z_streamp strm;
Line 228... Line 253...
228
 
253
 
Line 243... Line 268...
243
            return -1;
268
            return -1;
244
    }
269
    }
Line 245... Line 270...
245
 
270
 
246
    /* try writing to input buffer for speed (state->size == 0 if buffer not
271
    /* try writing to input buffer for speed (state->size == 0 if buffer not
247
       initialized) */
272
       initialized) */
248
    if (strm->avail_in < state->size) {
273
    if (state->size) {
249
        if (strm->avail_in == 0)
274
        if (strm->avail_in == 0)
-
 
275
            strm->next_in = state->in;
-
 
276
        have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
-
 
277
        if (have < state->size) {
250
            strm->next_in = state->in;
278
            state->in[have] = c;
251
        strm->next_in[strm->avail_in++] = c;
279
            strm->avail_in++;
252
        state->pos++;
280
            state->x.pos++;
-
 
281
            return c & 0xff;
253
        return c;
282
        }
Line 254... Line 283...
254
    }
283
    }
255
 
284
 
256
    /* no room in buffer or not initialized, use gz_write() */
285
    /* no room in buffer or not initialized, use gz_write() */
257
    buf[0] = c;
286
    buf[0] = c;
258
    if (gzwrite(file, buf, 1) != 1)
287
    if (gzwrite(file, buf, 1) != 1)
259
        return -1;
288
        return -1;
Line 260... Line 289...
260
    return c;
289
    return c & 0xff;
261
}
290
}
262
 
291
 
Line 272... Line 301...
272
    len = (unsigned)strlen(str);
301
    len = (unsigned)strlen(str);
273
    ret = gzwrite(file, str, len);
302
    ret = gzwrite(file, str, len);
274
    return ret == 0 && len != 0 ? -1 : ret;
303
    return ret == 0 && len != 0 ? -1 : ret;
275
}
304
}
Line 276... Line 305...
276
 
305
 
277
#ifdef STDC
306
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
Line 278... Line 307...
278
#include 
307
#include 
279
 
308
 
280
/* -- see zlib.h -- */
309
/* -- see zlib.h -- */
281
int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
310
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
282
{
311
{
283
    int size, len;
312
    int size, len;
284
    gz_statep state;
-
 
Line 285... Line 313...
285
    z_streamp strm;
313
    gz_statep state;
286
    va_list va;
314
    z_streamp strm;
287
 
315
 
288
    /* get internal structure */
316
    /* get internal structure */
Line 311... Line 339...
311
        return 0;
339
        return 0;
Line 312... Line 340...
312
 
340
 
313
    /* do the printf() into the input buffer, put length in len */
341
    /* do the printf() into the input buffer, put length in len */
314
    size = (int)(state->size);
342
    size = (int)(state->size);
315
    state->in[size - 1] = 0;
-
 
316
    va_start(va, format);
343
    state->in[size - 1] = 0;
317
#ifdef NO_vsnprintf
344
#ifdef NO_vsnprintf
318
#  ifdef HAS_vsprintf_void
345
#  ifdef HAS_vsprintf_void
319
    (void)vsprintf(state->in, format, va);
-
 
320
    va_end(va);
346
    (void)vsprintf((char *)(state->in), format, va);
321
    for (len = 0; len < size; len++)
347
    for (len = 0; len < size; len++)
322
        if (state->in[len] == 0) break;
348
        if (state->in[len] == 0) break;
323
#  else
349
#  else
324
    len = vsprintf(state->in, format, va);
-
 
325
    va_end(va);
350
    len = vsprintf((char *)(state->in), format, va);
326
#  endif
351
#  endif
327
#else
352
#else
328
#  ifdef HAS_vsnprintf_void
353
#  ifdef HAS_vsnprintf_void
329
    (void)vsnprintf(state->in, size, format, va);
-
 
330
    va_end(va);
354
    (void)vsnprintf((char *)(state->in), size, format, va);
331
    len = strlen(state->in);
355
    len = strlen((char *)(state->in));
332
#  else
356
#  else
333
    len = vsnprintf((char *)(state->in), size, format, va);
-
 
334
    va_end(va);
357
    len = vsnprintf((char *)(state->in), size, format, va);
335
#  endif
358
#  endif
Line 336... Line 359...
336
#endif
359
#endif
337
 
360
 
338
    /* check that printf() results fit in buffer */
361
    /* check that printf() results fit in buffer */
Line 339... Line 362...
339
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
362
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
340
        return 0;
363
        return 0;
341
 
364
 
342
    /* update buffer and position, defer compression until needed */
365
    /* update buffer and position, defer compression until needed */
343
    strm->avail_in = (unsigned)len;
366
    strm->avail_in = (unsigned)len;
344
    strm->next_in = state->in;
367
    strm->next_in = state->in;
Line -... Line 368...
-
 
368
    state->x.pos += len;
-
 
369
    return len;
-
 
370
}
-
 
371
 
-
 
372
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
-
 
373
{
-
 
374
    va_list va;
-
 
375
    int ret;
-
 
376
 
-
 
377
    va_start(va, format);
-
 
378
    ret = gzvprintf(file, format, va);
345
    state->pos += len;
379
    va_end(va);
Line 346... Line 380...
346
    return len;
380
    return ret;
347
}
381
}
348
 
382
 
349
#else /* !STDC */
383
#else /* !STDC && !Z_HAVE_STDARG_H */
Line 364... Line 398...
364
    if (file == NULL)
398
    if (file == NULL)
365
        return -1;
399
        return -1;
366
    state = (gz_statep)file;
400
    state = (gz_statep)file;
367
    strm = &(state->strm);
401
    strm = &(state->strm);
Line -... Line 402...
-
 
402
 
-
 
403
    /* check that can really pass pointer in ints */
-
 
404
    if (sizeof(int) != sizeof(void *))
-
 
405
        return 0;
368
 
406
 
369
    /* check that we're writing and that there's no error */
407
    /* check that we're writing and that there's no error */
370
    if (state->mode != GZ_WRITE || state->err != Z_OK)
408
    if (state->mode != GZ_WRITE || state->err != Z_OK)
Line 371... Line 409...
371
        return 0;
409
        return 0;
Line 388... Line 426...
388
    /* do the printf() into the input buffer, put length in len */
426
    /* do the printf() into the input buffer, put length in len */
389
    size = (int)(state->size);
427
    size = (int)(state->size);
390
    state->in[size - 1] = 0;
428
    state->in[size - 1] = 0;
391
#ifdef NO_snprintf
429
#ifdef NO_snprintf
392
#  ifdef HAS_sprintf_void
430
#  ifdef HAS_sprintf_void
393
    sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
431
    sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
394
            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
432
            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
395
    for (len = 0; len < size; len++)
433
    for (len = 0; len < size; len++)
396
        if (state->in[len] == 0) break;
434
        if (state->in[len] == 0) break;
397
#  else
435
#  else
398
    len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
436
    len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
399
                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
437
                  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
400
#  endif
438
#  endif
401
#else
439
#else
402
#  ifdef HAS_snprintf_void
440
#  ifdef HAS_snprintf_void
403
    snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
441
    snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
404
             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
442
             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
405
    len = strlen(state->in);
443
    len = strlen((char *)(state->in));
406
#  else
444
#  else
407
    len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
445
    len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
408
                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
446
                   a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
-
 
447
                   a19, a20);
409
#  endif
448
#  endif
410
#endif
449
#endif
Line 411... Line 450...
411
 
450
 
412
    /* check that printf() results fit in buffer */
451
    /* check that printf() results fit in buffer */
413
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
452
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
Line 414... Line 453...
414
        return 0;
453
        return 0;
415
 
454
 
416
    /* update buffer and position, defer compression until needed */
455
    /* update buffer and position, defer compression until needed */
417
    strm->avail_in = (unsigned)len;
456
    strm->avail_in = (unsigned)len;
418
    strm->next_in = state->in;
457
    strm->next_in = state->in;
419
    state->pos += len;
458
    state->x.pos += len;
Line 420... Line 459...
420
    return len;
459
    return len;
Line 498... Line 537...
498
 
537
 
499
/* -- see zlib.h -- */
538
/* -- see zlib.h -- */
500
int ZEXPORT gzclose_w(file)
539
int ZEXPORT gzclose_w(file)
501
    gzFile file;
540
    gzFile file;
502
{
541
{
503
    int ret = 0;
542
    int ret = Z_OK;
Line 504... Line 543...
504
    gz_statep state;
543
    gz_statep state;
505
 
544
 
506
    /* get internal structure */
545
    /* get internal structure */
Line 513... Line 552...
513
        return Z_STREAM_ERROR;
552
        return Z_STREAM_ERROR;
Line 514... Line 553...
514
 
553
 
515
    /* check for seek request */
554
    /* check for seek request */
516
    if (state->seek) {
555
    if (state->seek) {
517
        state->seek = 0;
556
        state->seek = 0;
-
 
557
        if (gz_zero(state, state->skip) == -1)
518
        ret += gz_zero(state, state->skip);
558
            ret = state->err;
Line 519... Line 559...
519
    }
559
    }
520
 
560
 
-
 
561
    /* flush, free memory, and close file */
-
 
562
    if (gz_comp(state, Z_FINISH) == -1)
-
 
563
        ret = state->err;
521
    /* flush, free memory, and close file */
564
    if (state->size) {
522
    ret += gz_comp(state, Z_FINISH);
565
        if (!state->direct) {
-
 
566
            (void)deflateEnd(&(state->strm));
523
    (void)deflateEnd(&(state->strm));
567
            free(state->out);
-
 
568
        }
524
    free(state->out);
569
        free(state->in);
525
    free(state->in);
570
    }
526
    gz_error(state, Z_OK, NULL);
571
    gz_error(state, Z_OK, NULL);
-
 
572
    free(state->path);
527
    free(state->path);
573
    if (close(state->fd) == -1)
528
    ret += close(state->fd);
574
        ret = Z_ERRNO;
529
    free(state);
575
    free(state);