Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* compress.c -- compress a memory buffer |
michael@0 | 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. |
michael@0 | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | /* @(#) $Id$ */ |
michael@0 | 7 | |
michael@0 | 8 | #define ZLIB_INTERNAL |
michael@0 | 9 | #include "zlib.h" |
michael@0 | 10 | |
michael@0 | 11 | /* =========================================================================== |
michael@0 | 12 | Compresses the source buffer into the destination buffer. The level |
michael@0 | 13 | parameter has the same meaning as in deflateInit. sourceLen is the byte |
michael@0 | 14 | length of the source buffer. Upon entry, destLen is the total size of the |
michael@0 | 15 | destination buffer, which must be at least 0.1% larger than sourceLen plus |
michael@0 | 16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. |
michael@0 | 17 | |
michael@0 | 18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough |
michael@0 | 19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, |
michael@0 | 20 | Z_STREAM_ERROR if the level parameter is invalid. |
michael@0 | 21 | */ |
michael@0 | 22 | int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) |
michael@0 | 23 | Bytef *dest; |
michael@0 | 24 | uLongf *destLen; |
michael@0 | 25 | const Bytef *source; |
michael@0 | 26 | uLong sourceLen; |
michael@0 | 27 | int level; |
michael@0 | 28 | { |
michael@0 | 29 | z_stream stream; |
michael@0 | 30 | int err; |
michael@0 | 31 | |
michael@0 | 32 | stream.next_in = (Bytef*)source; |
michael@0 | 33 | stream.avail_in = (uInt)sourceLen; |
michael@0 | 34 | #ifdef MAXSEG_64K |
michael@0 | 35 | /* Check for source > 64K on 16-bit machine: */ |
michael@0 | 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; |
michael@0 | 37 | #endif |
michael@0 | 38 | stream.next_out = dest; |
michael@0 | 39 | stream.avail_out = (uInt)*destLen; |
michael@0 | 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; |
michael@0 | 41 | |
michael@0 | 42 | stream.zalloc = (alloc_func)0; |
michael@0 | 43 | stream.zfree = (free_func)0; |
michael@0 | 44 | stream.opaque = (voidpf)0; |
michael@0 | 45 | |
michael@0 | 46 | err = deflateInit(&stream, level); |
michael@0 | 47 | if (err != Z_OK) return err; |
michael@0 | 48 | |
michael@0 | 49 | err = deflate(&stream, Z_FINISH); |
michael@0 | 50 | if (err != Z_STREAM_END) { |
michael@0 | 51 | deflateEnd(&stream); |
michael@0 | 52 | return err == Z_OK ? Z_BUF_ERROR : err; |
michael@0 | 53 | } |
michael@0 | 54 | *destLen = stream.total_out; |
michael@0 | 55 | |
michael@0 | 56 | err = deflateEnd(&stream); |
michael@0 | 57 | return err; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | /* =========================================================================== |
michael@0 | 61 | */ |
michael@0 | 62 | int ZEXPORT compress (dest, destLen, source, sourceLen) |
michael@0 | 63 | Bytef *dest; |
michael@0 | 64 | uLongf *destLen; |
michael@0 | 65 | const Bytef *source; |
michael@0 | 66 | uLong sourceLen; |
michael@0 | 67 | { |
michael@0 | 68 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | /* =========================================================================== |
michael@0 | 72 | If the default memLevel or windowBits for deflateInit() is changed, then |
michael@0 | 73 | this function needs to be updated. |
michael@0 | 74 | */ |
michael@0 | 75 | uLong ZEXPORT compressBound (sourceLen) |
michael@0 | 76 | uLong sourceLen; |
michael@0 | 77 | { |
michael@0 | 78 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + |
michael@0 | 79 | (sourceLen >> 25) + 13; |
michael@0 | 80 | } |