11cb0ef41Sopenharmony_ci/* compress.c -- compress a memory buffer
21cb0ef41Sopenharmony_ci * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
31cb0ef41Sopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
41cb0ef41Sopenharmony_ci */
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci/* @(#) $Id$ */
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#define ZLIB_INTERNAL
91cb0ef41Sopenharmony_ci#include "zlib.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci/* ===========================================================================
121cb0ef41Sopenharmony_ci     Compresses the source buffer into the destination buffer. The level
131cb0ef41Sopenharmony_ci   parameter has the same meaning as in deflateInit.  sourceLen is the byte
141cb0ef41Sopenharmony_ci   length of the source buffer. Upon entry, destLen is the total size of the
151cb0ef41Sopenharmony_ci   destination buffer, which must be at least 0.1% larger than sourceLen plus
161cb0ef41Sopenharmony_ci   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
191cb0ef41Sopenharmony_ci   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
201cb0ef41Sopenharmony_ci   Z_STREAM_ERROR if the level parameter is invalid.
211cb0ef41Sopenharmony_ci*/
221cb0ef41Sopenharmony_ciint ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
231cb0ef41Sopenharmony_ci                      uLong sourceLen, int level) {
241cb0ef41Sopenharmony_ci    z_stream stream;
251cb0ef41Sopenharmony_ci    int err;
261cb0ef41Sopenharmony_ci    const uInt max = (uInt)-1;
271cb0ef41Sopenharmony_ci    uLong left;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    left = *destLen;
301cb0ef41Sopenharmony_ci    *destLen = 0;
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    stream.zalloc = (alloc_func)0;
331cb0ef41Sopenharmony_ci    stream.zfree = (free_func)0;
341cb0ef41Sopenharmony_ci    stream.opaque = (voidpf)0;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    err = deflateInit(&stream, level);
371cb0ef41Sopenharmony_ci    if (err != Z_OK) return err;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    stream.next_out = dest;
401cb0ef41Sopenharmony_ci    stream.avail_out = 0;
411cb0ef41Sopenharmony_ci    stream.next_in = (z_const Bytef *)source;
421cb0ef41Sopenharmony_ci    stream.avail_in = 0;
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    do {
451cb0ef41Sopenharmony_ci        if (stream.avail_out == 0) {
461cb0ef41Sopenharmony_ci            stream.avail_out = left > (uLong)max ? max : (uInt)left;
471cb0ef41Sopenharmony_ci            left -= stream.avail_out;
481cb0ef41Sopenharmony_ci        }
491cb0ef41Sopenharmony_ci        if (stream.avail_in == 0) {
501cb0ef41Sopenharmony_ci            stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
511cb0ef41Sopenharmony_ci            sourceLen -= stream.avail_in;
521cb0ef41Sopenharmony_ci        }
531cb0ef41Sopenharmony_ci        err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
541cb0ef41Sopenharmony_ci    } while (err == Z_OK);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    *destLen = stream.total_out;
571cb0ef41Sopenharmony_ci    deflateEnd(&stream);
581cb0ef41Sopenharmony_ci    return err == Z_STREAM_END ? Z_OK : err;
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci/* ===========================================================================
621cb0ef41Sopenharmony_ci */
631cb0ef41Sopenharmony_ciint ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
641cb0ef41Sopenharmony_ci                     uLong sourceLen) {
651cb0ef41Sopenharmony_ci    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci/* ===========================================================================
691cb0ef41Sopenharmony_ci     If the default memLevel or windowBits for deflateInit() is changed, then
701cb0ef41Sopenharmony_ci   this function needs to be updated.
711cb0ef41Sopenharmony_ci */
721cb0ef41Sopenharmony_ciuLong ZEXPORT compressBound(uLong sourceLen) {
731cb0ef41Sopenharmony_ci    sourceLen = sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
741cb0ef41Sopenharmony_ci                (sourceLen >> 25) + 13;
751cb0ef41Sopenharmony_ci    /* FIXME(cavalcantii): usage of CRC32 Castagnoli as a hash function
761cb0ef41Sopenharmony_ci     * for the hash table of symbols used for compression has a side effect
771cb0ef41Sopenharmony_ci     * where for compression level [4, 5] it will increase the output buffer size
781cb0ef41Sopenharmony_ci     * by 0.1% (i.e. less than 1%) for a high entropy input (i.e. random data).
791cb0ef41Sopenharmony_ci     * To avoid a scenario where client code would fail, for safety we increase
801cb0ef41Sopenharmony_ci     * the expected output size by 0.8% (i.e. 8x more than the worst scenario).
811cb0ef41Sopenharmony_ci     * See: http://crbug.com/990489
821cb0ef41Sopenharmony_ci     */
831cb0ef41Sopenharmony_ci    sourceLen += sourceLen >> 7; // Equivalent to 1.0078125
841cb0ef41Sopenharmony_ci    return sourceLen;
851cb0ef41Sopenharmony_ci}
86