opencv
1/* compress.c -- compress a memory buffer
2* Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3* For conditions of distribution and use, see copyright notice in zlib.h
4*/
5
6#include "zbuild.h"7#include "zutil.h"8
9/* ===========================================================================
10* Architecture-specific hooks.
11*/
12#ifdef S390_DFLTCC_DEFLATE13# include "arch/s390/dfltcc_common.h"14#else15/* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings.
16* Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */
17# define DEFLATE_BOUND_COMPLEN(source_len) 018#endif19
20/* ===========================================================================
21Compresses the source buffer into the destination buffer. The level
22parameter has the same meaning as in deflateInit. sourceLen is the byte
23length of the source buffer. Upon entry, destLen is the total size of the
24destination buffer, which must be at least 0.1% larger than sourceLen plus
2512 bytes. Upon exit, destLen is the actual size of the compressed buffer.
26
27compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
28memory, Z_BUF_ERROR if there was not enough room in the output buffer,
29Z_STREAM_ERROR if the level parameter is invalid.
30*/
31int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source,32z_uintmax_t sourceLen, int level) {33PREFIX3(stream) stream;34int err;35const unsigned int max = (unsigned int)-1;36z_size_t left;37
38left = *destLen;39*destLen = 0;40
41stream.zalloc = NULL;42stream.zfree = NULL;43stream.opaque = NULL;44
45err = PREFIX(deflateInit)(&stream, level);46if (err != Z_OK)47return err;48
49stream.next_out = dest;50stream.avail_out = 0;51stream.next_in = (z_const unsigned char *)source;52stream.avail_in = 0;53
54do {55if (stream.avail_out == 0) {56stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;57left -= stream.avail_out;58}59if (stream.avail_in == 0) {60stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen;61sourceLen -= stream.avail_in;62}63err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);64} while (err == Z_OK);65
66*destLen = stream.total_out;67PREFIX(deflateEnd)(&stream);68return err == Z_STREAM_END ? Z_OK : err;69}
70
71/* ===========================================================================
72*/
73int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {74return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);75}
76
77/* ===========================================================================
78If the default memLevel or windowBits for deflateInit() is changed, then
79this function needs to be updated.
80*/
81z_uintmax_t Z_EXPORT PREFIX(compressBound)(z_uintmax_t sourceLen) {82z_uintmax_t complen = DEFLATE_BOUND_COMPLEN(sourceLen);83
84if (complen > 0)85/* Architecture-specific code provided an upper bound. */86return complen + ZLIB_WRAPLEN;87
88#ifndef NO_QUICK_STRATEGY89return sourceLen /* The source size itself */90+ (sourceLen == 0 ? 1 : 0) /* Always at least one byte for any input */91+ (sourceLen < 9 ? 1 : 0) /* One extra byte for lengths less than 9 */92+ DEFLATE_QUICK_OVERHEAD(sourceLen) /* Source encoding overhead, padded to next full byte */93+ DEFLATE_BLOCK_OVERHEAD /* Deflate block overhead bytes */94+ ZLIB_WRAPLEN; /* zlib wrapper */95#else96return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN;97#endif98}
99