xref: /openbsd-src/gnu/usr.bin/perl/cpan/Compress-Raw-Zlib/zlib-src/compress.c (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b39c5158Smillert /* compress.c -- compress a memory buffer
29f11ffb7Safresh1  * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3b39c5158Smillert  * For conditions of distribution and use, see copyright notice in zlib.h
4b39c5158Smillert  */
5b39c5158Smillert 
6b39c5158Smillert /* @(#) $Id$ */
7b39c5158Smillert 
8b39c5158Smillert #define ZLIB_INTERNAL
9b39c5158Smillert #include "zlib.h"
10b39c5158Smillert 
11b39c5158Smillert /* ===========================================================================
12b39c5158Smillert      Compresses the source buffer into the destination buffer. The level
13b39c5158Smillert    parameter has the same meaning as in deflateInit.  sourceLen is the byte
14b39c5158Smillert    length of the source buffer. Upon entry, destLen is the total size of the
15b39c5158Smillert    destination buffer, which must be at least 0.1% larger than sourceLen plus
16b39c5158Smillert    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17b39c5158Smillert 
18b39c5158Smillert      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19b39c5158Smillert    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20b39c5158Smillert    Z_STREAM_ERROR if the level parameter is invalid.
21b39c5158Smillert */
22*3d61058aSafresh1 int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
23*3d61058aSafresh1                       uLong sourceLen, int level) {
24b39c5158Smillert     z_stream stream;
25b39c5158Smillert     int err;
269f11ffb7Safresh1     const uInt max = (uInt)-1;
279f11ffb7Safresh1     uLong left;
28b39c5158Smillert 
299f11ffb7Safresh1     left = *destLen;
309f11ffb7Safresh1     *destLen = 0;
31b39c5158Smillert 
32b39c5158Smillert     stream.zalloc = (alloc_func)0;
33b39c5158Smillert     stream.zfree = (free_func)0;
34b39c5158Smillert     stream.opaque = (voidpf)0;
35b39c5158Smillert 
36b39c5158Smillert     err = deflateInit(&stream, level);
37b39c5158Smillert     if (err != Z_OK) return err;
38b39c5158Smillert 
399f11ffb7Safresh1     stream.next_out = dest;
409f11ffb7Safresh1     stream.avail_out = 0;
419f11ffb7Safresh1     stream.next_in = (z_const Bytef *)source;
429f11ffb7Safresh1     stream.avail_in = 0;
43b39c5158Smillert 
449f11ffb7Safresh1     do {
459f11ffb7Safresh1         if (stream.avail_out == 0) {
469f11ffb7Safresh1             stream.avail_out = left > (uLong)max ? max : (uInt)left;
479f11ffb7Safresh1             left -= stream.avail_out;
489f11ffb7Safresh1         }
499f11ffb7Safresh1         if (stream.avail_in == 0) {
509f11ffb7Safresh1             stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
519f11ffb7Safresh1             sourceLen -= stream.avail_in;
529f11ffb7Safresh1         }
539f11ffb7Safresh1         err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
549f11ffb7Safresh1     } while (err == Z_OK);
559f11ffb7Safresh1 
569f11ffb7Safresh1     *destLen = stream.total_out;
579f11ffb7Safresh1     deflateEnd(&stream);
589f11ffb7Safresh1     return err == Z_STREAM_END ? Z_OK : err;
59b39c5158Smillert }
60b39c5158Smillert 
61b39c5158Smillert /* ===========================================================================
62b39c5158Smillert  */
63*3d61058aSafresh1 int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
64*3d61058aSafresh1                      uLong sourceLen) {
65b39c5158Smillert     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
66b39c5158Smillert }
67b39c5158Smillert 
68b39c5158Smillert /* ===========================================================================
69b39c5158Smillert      If the default memLevel or windowBits for deflateInit() is changed, then
70b39c5158Smillert    this function needs to be updated.
71b39c5158Smillert  */
72*3d61058aSafresh1 uLong ZEXPORT compressBound(uLong sourceLen) {
7348950c12Ssthen     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
7448950c12Ssthen            (sourceLen >> 25) + 13;
75b39c5158Smillert }
76