xref: /netbsd-src/external/gpl3/gdb/dist/zlib/compress.c (revision 924795e69c8bb3f17afd8fcbb799710cc1719dc4)
1212397c6Schristos /* compress.c -- compress a memory buffer
2796c32c9Schristos  * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3212397c6Schristos  * For conditions of distribution and use, see copyright notice in zlib.h
4212397c6Schristos  */
5212397c6Schristos 
6*924795e6Schristos /* @(#) Id */
7212397c6Schristos 
8212397c6Schristos #define ZLIB_INTERNAL
9212397c6Schristos #include "zlib.h"
10212397c6Schristos 
11212397c6Schristos /* ===========================================================================
12212397c6Schristos      Compresses the source buffer into the destination buffer. The level
13212397c6Schristos    parameter has the same meaning as in deflateInit.  sourceLen is the byte
14212397c6Schristos    length of the source buffer. Upon entry, destLen is the total size of the
15212397c6Schristos    destination buffer, which must be at least 0.1% larger than sourceLen plus
16212397c6Schristos    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17212397c6Schristos 
18212397c6Schristos      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19212397c6Schristos    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20212397c6Schristos    Z_STREAM_ERROR if the level parameter is invalid.
21212397c6Schristos */
compress2(dest,destLen,source,sourceLen,level)22212397c6Schristos int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
23212397c6Schristos     Bytef *dest;
24212397c6Schristos     uLongf *destLen;
25212397c6Schristos     const Bytef *source;
26212397c6Schristos     uLong sourceLen;
27212397c6Schristos     int level;
28212397c6Schristos {
29212397c6Schristos     z_stream stream;
30212397c6Schristos     int err;
31796c32c9Schristos     const uInt max = (uInt)-1;
32796c32c9Schristos     uLong left;
33212397c6Schristos 
34796c32c9Schristos     left = *destLen;
35796c32c9Schristos     *destLen = 0;
36212397c6Schristos 
37212397c6Schristos     stream.zalloc = (alloc_func)0;
38212397c6Schristos     stream.zfree = (free_func)0;
39212397c6Schristos     stream.opaque = (voidpf)0;
40212397c6Schristos 
41212397c6Schristos     err = deflateInit(&stream, level);
42212397c6Schristos     if (err != Z_OK) return err;
43212397c6Schristos 
44796c32c9Schristos     stream.next_out = dest;
45796c32c9Schristos     stream.avail_out = 0;
46796c32c9Schristos     stream.next_in = (z_const Bytef *)source;
47796c32c9Schristos     stream.avail_in = 0;
48212397c6Schristos 
49796c32c9Schristos     do {
50796c32c9Schristos         if (stream.avail_out == 0) {
51796c32c9Schristos             stream.avail_out = left > (uLong)max ? max : (uInt)left;
52796c32c9Schristos             left -= stream.avail_out;
53796c32c9Schristos         }
54796c32c9Schristos         if (stream.avail_in == 0) {
55796c32c9Schristos             stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
56796c32c9Schristos             sourceLen -= stream.avail_in;
57796c32c9Schristos         }
58796c32c9Schristos         err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
59796c32c9Schristos     } while (err == Z_OK);
60796c32c9Schristos 
61796c32c9Schristos     *destLen = stream.total_out;
62796c32c9Schristos     deflateEnd(&stream);
63796c32c9Schristos     return err == Z_STREAM_END ? Z_OK : err;
64212397c6Schristos }
65212397c6Schristos 
66212397c6Schristos /* ===========================================================================
67212397c6Schristos  */
compress(dest,destLen,source,sourceLen)68212397c6Schristos int ZEXPORT compress (dest, destLen, source, sourceLen)
69212397c6Schristos     Bytef *dest;
70212397c6Schristos     uLongf *destLen;
71212397c6Schristos     const Bytef *source;
72212397c6Schristos     uLong sourceLen;
73212397c6Schristos {
74212397c6Schristos     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
75212397c6Schristos }
76212397c6Schristos 
77212397c6Schristos /* ===========================================================================
78212397c6Schristos      If the default memLevel or windowBits for deflateInit() is changed, then
79212397c6Schristos    this function needs to be updated.
80212397c6Schristos  */
compressBound(sourceLen)81212397c6Schristos uLong ZEXPORT compressBound (sourceLen)
82212397c6Schristos     uLong sourceLen;
83212397c6Schristos {
84212397c6Schristos     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
85212397c6Schristos            (sourceLen >> 25) + 13;
86212397c6Schristos }
87