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