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