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