1275793eaSopenharmony_ci/* compress.c -- compress a memory buffer
2275793eaSopenharmony_ci * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3275793eaSopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
4275793eaSopenharmony_ci */
5275793eaSopenharmony_ci
6275793eaSopenharmony_ci/* @(#) $Id$ */
7275793eaSopenharmony_ci
8275793eaSopenharmony_ci#define ZLIB_INTERNAL
9275793eaSopenharmony_ci#include "zlib.h"
10275793eaSopenharmony_ci
11275793eaSopenharmony_ci/* ===========================================================================
12275793eaSopenharmony_ci     Compresses the source buffer into the destination buffer. The level
13275793eaSopenharmony_ci   parameter has the same meaning as in deflateInit.  sourceLen is the byte
14275793eaSopenharmony_ci   length of the source buffer. Upon entry, destLen is the total size of the
15275793eaSopenharmony_ci   destination buffer, which must be at least 0.1% larger than sourceLen plus
16275793eaSopenharmony_ci   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17275793eaSopenharmony_ci
18275793eaSopenharmony_ci     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19275793eaSopenharmony_ci   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20275793eaSopenharmony_ci   Z_STREAM_ERROR if the level parameter is invalid.
21275793eaSopenharmony_ci*/
22275793eaSopenharmony_ciint ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
23275793eaSopenharmony_ci                      uLong sourceLen, int level) {
24275793eaSopenharmony_ci    z_stream stream;
25275793eaSopenharmony_ci    int err;
26275793eaSopenharmony_ci    const uInt max = (uInt)-1;
27275793eaSopenharmony_ci    uLong left;
28275793eaSopenharmony_ci
29275793eaSopenharmony_ci    left = *destLen;
30275793eaSopenharmony_ci    *destLen = 0;
31275793eaSopenharmony_ci
32275793eaSopenharmony_ci    stream.zalloc = (alloc_func)0;
33275793eaSopenharmony_ci    stream.zfree = (free_func)0;
34275793eaSopenharmony_ci    stream.opaque = (voidpf)0;
35275793eaSopenharmony_ci
36275793eaSopenharmony_ci    err = deflateInit(&stream, level);
37275793eaSopenharmony_ci    if (err != Z_OK) return err;
38275793eaSopenharmony_ci
39275793eaSopenharmony_ci    stream.next_out = dest;
40275793eaSopenharmony_ci    stream.avail_out = 0;
41275793eaSopenharmony_ci    stream.next_in = (z_const Bytef *)source;
42275793eaSopenharmony_ci    stream.avail_in = 0;
43275793eaSopenharmony_ci
44275793eaSopenharmony_ci    do {
45275793eaSopenharmony_ci        if (stream.avail_out == 0) {
46275793eaSopenharmony_ci            stream.avail_out = left > (uLong)max ? max : (uInt)left;
47275793eaSopenharmony_ci            left -= stream.avail_out;
48275793eaSopenharmony_ci        }
49275793eaSopenharmony_ci        if (stream.avail_in == 0) {
50275793eaSopenharmony_ci            stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
51275793eaSopenharmony_ci            sourceLen -= stream.avail_in;
52275793eaSopenharmony_ci        }
53275793eaSopenharmony_ci        err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
54275793eaSopenharmony_ci    } while (err == Z_OK);
55275793eaSopenharmony_ci
56275793eaSopenharmony_ci    *destLen = stream.total_out;
57275793eaSopenharmony_ci    deflateEnd(&stream);
58275793eaSopenharmony_ci    return err == Z_STREAM_END ? Z_OK : err;
59275793eaSopenharmony_ci}
60275793eaSopenharmony_ci
61275793eaSopenharmony_ci/* ===========================================================================
62275793eaSopenharmony_ci */
63275793eaSopenharmony_ciint ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
64275793eaSopenharmony_ci                     uLong sourceLen) {
65275793eaSopenharmony_ci    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
66275793eaSopenharmony_ci}
67275793eaSopenharmony_ci
68275793eaSopenharmony_ci/* ===========================================================================
69275793eaSopenharmony_ci     If the default memLevel or windowBits for deflateInit() is changed, then
70275793eaSopenharmony_ci   this function needs to be updated.
71275793eaSopenharmony_ci */
72275793eaSopenharmony_ciuLong ZEXPORT compressBound(uLong sourceLen)
73275793eaSopenharmony_ci{
74275793eaSopenharmony_ci    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
75275793eaSopenharmony_ci           (sourceLen >> 25) + 13;
76275793eaSopenharmony_ci}
77