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#if defined(ZLIB_COMPAT)
9# include "zlib.h"
10#else
11# include "zlib-ng.h"
12#endif
13
14/* ===========================================================================
15 * Architecture-specific hooks.
16 */
17#ifdef S390_DFLTCC_DEFLATE
18# include "arch/s390/dfltcc_common.h"
19#else
20/* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings.
21 * Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */
22# define DEFLATE_BOUND_COMPLEN(source_len) 0
23#endif
24
25/* ===========================================================================
26 Compresses the source buffer into the destination buffer. The level
27 parameter has the same meaning as in deflateInit. sourceLen is the byte
28 length of the source buffer. Upon entry, destLen is the total size of the
29 destination buffer, which must be at least 0.1% larger than sourceLen plus
30 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
31
32 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
33 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
34 Z_STREAM_ERROR if the level parameter is invalid.
35*/
36int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_size_t *destLen, const unsigned char *source,
37 z_size_t sourceLen, int level) {
38 PREFIX3(stream) stream;
39 int err;
40 const unsigned int max = (unsigned int)-1;
41 z_size_t left;
42
43 left = *destLen;
44 *destLen = 0;
45
46 stream.zalloc = NULL;
47 stream.zfree = NULL;
48 stream.opaque = NULL;
49
50 err = PREFIX(deflateInit)(&stream, level);
51 if (err != Z_OK)
52 return err;
53
54 stream.next_out = dest;
55 stream.avail_out = 0;
56 stream.next_in = (z_const unsigned char *)source;
57 stream.avail_in = 0;
58
59 do {
60 if (stream.avail_out == 0) {
61 stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;
62 left -= stream.avail_out;
63 }
64 if (stream.avail_in == 0) {
65 stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen;
66 sourceLen -= stream.avail_in;
67 }
68 err = PREFIX(deflate)(strm: &stream, flush: sourceLen ? Z_NO_FLUSH : Z_FINISH);
69 } while (err == Z_OK);
70
71 *destLen = (z_size_t)stream.total_out;
72 PREFIX(deflateEnd)(strm: &stream);
73 return err == Z_STREAM_END ? Z_OK : err;
74}
75
76/* ===========================================================================
77 */
78int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t sourceLen) {
79 return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
80}
81
82/* ===========================================================================
83 If the default memLevel or windowBits for deflateInit() is changed, then
84 this function needs to be updated.
85 */
86z_size_t Z_EXPORT PREFIX(compressBound)(z_size_t sourceLen) {
87 z_size_t complen = DEFLATE_BOUND_COMPLEN(sourceLen);
88
89 if (complen > 0)
90 /* Architecture-specific code provided an upper bound. */
91 return complen + ZLIB_WRAPLEN;
92
93#ifndef NO_QUICK_STRATEGY
94 return sourceLen /* The source size itself */
95 + DEFLATE_QUICK_OVERHEAD(sourceLen) /* Source encoding overhead, padded to next full byte */
96 + DEFLATE_BLOCK_OVERHEAD /* Deflate block overhead bytes */
97 + ZLIB_WRAPLEN; /* zlib wrapper */
98#else
99 return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN;
100#endif
101}
102