dbf-halloween2015

annotate libs/zlib/compress.c @ 1:c3f5c32cb210

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