1172302Spjd/*	$NetBSD: compress.c,v 1.4 2022/10/15 19:49:32 christos Exp $	*/
2172302Spjd
3172302Spjd/* compress.c -- compress a memory buffer
4172302Spjd * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
5172302Spjd * For conditions of distribution and use, see copyright notice in zlib.h
6172302Spjd */
7172302Spjd
8172302Spjd/* @(#) Id */
9172302Spjd
10172302Spjd#define ZLIB_INTERNAL
11172302Spjd#include "zlib.h"
12172302Spjd
13172302Spjd/* ===========================================================================
14172302Spjd     Compresses the source buffer into the destination buffer. The level
15172302Spjd   parameter has the same meaning as in deflateInit.  sourceLen is the byte
16172302Spjd   length of the source buffer. Upon entry, destLen is the total size of the
17172302Spjd   destination buffer, which must be at least 0.1% larger than sourceLen plus
18172302Spjd   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
19172302Spjd
20172302Spjd     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
21172302Spjd   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
22172302Spjd   Z_STREAM_ERROR if the level parameter is invalid.
23172302Spjd*/
24172302Spjdint ZEXPORT compress2(dest, destLen, source, sourceLen, level)
25172302Spjd    Bytef *dest;
26172302Spjd    uLongf *destLen;
27172302Spjd    const Bytef *source;
28172302Spjd    uLong sourceLen;
29172302Spjd    int level;
30172302Spjd{
31172302Spjd    z_stream stream;
32172302Spjd    int err;
33172302Spjd    const uInt max = (uInt)-1;
34172302Spjd    uLong left;
35172302Spjd
36172302Spjd    left = *destLen;
37172302Spjd    *destLen = 0;
38172302Spjd
39172302Spjd    stream.zalloc = (alloc_func)0;
40172302Spjd    stream.zfree = (free_func)0;
41172302Spjd    stream.opaque = (voidpf)0;
42172302Spjd
43172302Spjd    err = deflateInit(&stream, level);
44172302Spjd    if (err != Z_OK) return err;
45172302Spjd
46172302Spjd    stream.next_out = dest;
47172302Spjd    stream.avail_out = 0;
48172302Spjd    stream.next_in = __UNCONST(source);
49172302Spjd    stream.avail_in = 0;
50172302Spjd
51172302Spjd    do {
52212554Spjd        if (stream.avail_out == 0) {
53212554Spjd            stream.avail_out = left > (uLong)max ? max : (uInt)left;
54212554Spjd            left -= stream.avail_out;
55172302Spjd        }
56172302Spjd        if (stream.avail_in == 0) {
57172302Spjd            stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
58172302Spjd            sourceLen -= stream.avail_in;
59172302Spjd        }
60172302Spjd        err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
61172302Spjd    } while (err == Z_OK);
62172302Spjd
63172302Spjd    *destLen = stream.total_out;
64172302Spjd    deflateEnd(&stream);
65172302Spjd    return err == Z_STREAM_END ? Z_OK : err;
66212554Spjd}
67212554Spjd
68172302Spjd/* ===========================================================================
69212554Spjd */
70212554Spjdint ZEXPORT compress(dest, destLen, source, sourceLen)
71172302Spjd    Bytef *dest;
72212554Spjd    uLongf *destLen;
73212554Spjd    const Bytef *source;
74212554Spjd    uLong sourceLen;
75212554Spjd{
76212554Spjd    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
77212554Spjd}
78212554Spjd
79212554Spjd/* ===========================================================================
80172302Spjd     If the default memLevel or windowBits for deflateInit() is changed, then
81212554Spjd   this function needs to be updated.
82212554Spjd */
83212554SpjduLong ZEXPORT compressBound(sourceLen)
84212554Spjd    uLong sourceLen;
85212554Spjd{
86212554Spjd    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
87172302Spjd           (sourceLen >> 25) + 13;
88212554Spjd}
89212554Spjd