Fossil SCM

fossil-scm / compat / zlib / uncompr.c
Source Blame History 101 lines
7ef7284… drh 1 /* uncompr.c -- decompress a memory buffer
6ea30fb… florian 2 * Copyright (C) 1995-2026 Jean-loup Gailly, Mark Adler
7ef7284… drh 3 * For conditions of distribution and use, see copyright notice in zlib.h
7ef7284… drh 4 */
7ef7284… drh 5
7ef7284… drh 6 /* @(#) $Id$ */
7ef7284… drh 7
7ef7284… drh 8 #define ZLIB_INTERNAL
7ef7284… drh 9 #include "zlib.h"
7ef7284… drh 10
7ef7284… drh 11 /* ===========================================================================
e38d5e1… jan.nijtmans 12 Decompresses the source buffer into the destination buffer. *sourceLen is
e38d5e1… jan.nijtmans 13 the byte length of the source buffer. Upon entry, *destLen is the total size
e38d5e1… jan.nijtmans 14 of the destination buffer, which must be large enough to hold the entire
e38d5e1… jan.nijtmans 15 uncompressed data. (The size of the uncompressed data must have been saved
e38d5e1… jan.nijtmans 16 previously by the compressor and transmitted to the decompressor by some
e38d5e1… jan.nijtmans 17 mechanism outside the scope of this compression library.) Upon exit,
e38d5e1… jan.nijtmans 18 *destLen is the size of the decompressed data and *sourceLen is the number
e38d5e1… jan.nijtmans 19 of source bytes consumed. Upon return, source + *sourceLen points to the
e38d5e1… jan.nijtmans 20 first unused input byte.
e38d5e1… jan.nijtmans 21
e38d5e1… jan.nijtmans 22 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
e38d5e1… jan.nijtmans 23 memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
e38d5e1… jan.nijtmans 24 Z_DATA_ERROR if the input data was corrupted, including if the input data is
e38d5e1… jan.nijtmans 25 an incomplete zlib stream.
6ea30fb… florian 26
6ea30fb… florian 27 The _z versions of the functions take size_t length arguments.
7ef7284… drh 28 */
6ea30fb… florian 29 int ZEXPORT uncompress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
6ea30fb… florian 30 z_size_t *sourceLen) {
7ef7284… drh 31 z_stream stream;
7ef7284… drh 32 int err;
e38d5e1… jan.nijtmans 33 const uInt max = (uInt)-1;
6ea30fb… florian 34 z_size_t len, left;
6ea30fb… florian 35
6ea30fb… florian 36 if (sourceLen == NULL || (*sourceLen > 0 && source == NULL) ||
6ea30fb… florian 37 destLen == NULL || (*destLen > 0 && dest == NULL))
6ea30fb… florian 38 return Z_STREAM_ERROR;
e38d5e1… jan.nijtmans 39
e38d5e1… jan.nijtmans 40 len = *sourceLen;
6ea30fb… florian 41 left = *destLen;
6ea30fb… florian 42 if (left == 0 && dest == Z_NULL)
6ea30fb… florian 43 dest = (Bytef *)&stream.reserved; /* next_out cannot be NULL */
bb4776e… jan.nijtmans 44
bb4776e… jan.nijtmans 45 stream.next_in = (z_const Bytef *)source;
e38d5e1… jan.nijtmans 46 stream.avail_in = 0;
7ef7284… drh 47 stream.zalloc = (alloc_func)0;
7ef7284… drh 48 stream.zfree = (free_func)0;
e38d5e1… jan.nijtmans 49 stream.opaque = (voidpf)0;
7ef7284… drh 50
7ef7284… drh 51 err = inflateInit(&stream);
7ef7284… drh 52 if (err != Z_OK) return err;
7ef7284… drh 53
e38d5e1… jan.nijtmans 54 stream.next_out = dest;
e38d5e1… jan.nijtmans 55 stream.avail_out = 0;
e38d5e1… jan.nijtmans 56
e38d5e1… jan.nijtmans 57 do {
e38d5e1… jan.nijtmans 58 if (stream.avail_out == 0) {
6ea30fb… florian 59 stream.avail_out = left > (z_size_t)max ? max : (uInt)left;
e38d5e1… jan.nijtmans 60 left -= stream.avail_out;
e38d5e1… jan.nijtmans 61 }
e38d5e1… jan.nijtmans 62 if (stream.avail_in == 0) {
6ea30fb… florian 63 stream.avail_in = len > (z_size_t)max ? max : (uInt)len;
e38d5e1… jan.nijtmans 64 len -= stream.avail_in;
e38d5e1… jan.nijtmans 65 }
e38d5e1… jan.nijtmans 66 err = inflate(&stream, Z_NO_FLUSH);
e38d5e1… jan.nijtmans 67 } while (err == Z_OK);
e38d5e1… jan.nijtmans 68
6ea30fb… florian 69 /* Set len and left to the unused input data and unused output space. Set
6ea30fb… florian 70 *sourceLen to the amount of input consumed. Set *destLen to the amount
6ea30fb… florian 71 of data produced. */
6ea30fb… florian 72 len += stream.avail_in;
6ea30fb… florian 73 left += stream.avail_out;
6ea30fb… florian 74 *sourceLen -= len;
6ea30fb… florian 75 *destLen -= left;
e38d5e1… jan.nijtmans 76
e38d5e1… jan.nijtmans 77 inflateEnd(&stream);
e38d5e1… jan.nijtmans 78 return err == Z_STREAM_END ? Z_OK :
e38d5e1… jan.nijtmans 79 err == Z_NEED_DICT ? Z_DATA_ERROR :
6ea30fb… florian 80 err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR :
e38d5e1… jan.nijtmans 81 err;
e38d5e1… jan.nijtmans 82 }
6ea30fb… florian 83 int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
6ea30fb… florian 84 uLong *sourceLen) {
6ea30fb… florian 85 int ret;
6ea30fb… florian 86 z_size_t got = *destLen, used = *sourceLen;
6ea30fb… florian 87 ret = uncompress2_z(dest, &got, source, &used);
6ea30fb… florian 88 *sourceLen = (uLong)used;
6ea30fb… florian 89 *destLen = (uLong)got;
6ea30fb… florian 90 return ret;
6ea30fb… florian 91 }
6ea30fb… florian 92 int ZEXPORT uncompress_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
6ea30fb… florian 93 z_size_t sourceLen) {
6ea30fb… florian 94 z_size_t used = sourceLen;
6ea30fb… florian 95 return uncompress2_z(dest, destLen, source, &used);
6ea30fb… florian 96 }
f1f1d6c… drh 97 int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
f1f1d6c… drh 98 uLong sourceLen) {
6ea30fb… florian 99 uLong used = sourceLen;
6ea30fb… florian 100 return uncompress2(dest, destLen, source, &used);
7ef7284… drh 101 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button