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