11/* compress.c -- compress a memory buffer
2- * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
2+ * Copyright (C) 1995-2026 Jean-loup Gailly, Mark Adler
33 * For conditions of distribution and use, see copyright notice in zlib.h
44 */
55
1818 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
1919 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
2020 Z_STREAM_ERROR if the level parameter is invalid.
21+
22+ The _z versions of the functions take size_t length arguments.
2123*/
22- int ZEXPORT compress2 (Bytef * dest , uLongf * destLen , const Bytef * source ,
23- uLong sourceLen , int level ) {
24+ int ZEXPORT compress2_z (Bytef * dest , z_size_t * destLen , const Bytef * source ,
25+ z_size_t sourceLen , int level ) {
2426 z_stream stream ;
2527 int err ;
2628 const uInt max = (uInt )- 1 ;
27- uLong left ;
29+ z_size_t left ;
30+
31+ if ((sourceLen > 0 && source == NULL ) ||
32+ destLen == NULL || (* destLen > 0 && dest == NULL ))
33+ return Z_STREAM_ERROR ;
2834
2935 left = * destLen ;
3036 * destLen = 0 ;
@@ -43,23 +49,36 @@ int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
4349
4450 do {
4551 if (stream .avail_out == 0 ) {
46- stream .avail_out = left > (uLong )max ? max : (uInt )left ;
52+ stream .avail_out = left > (z_size_t )max ? max : (uInt )left ;
4753 left -= stream .avail_out ;
4854 }
4955 if (stream .avail_in == 0 ) {
50- stream .avail_in = sourceLen > (uLong )max ? max : (uInt )sourceLen ;
56+ stream .avail_in = sourceLen > (z_size_t )max ? max :
57+ (uInt )sourceLen ;
5158 sourceLen -= stream .avail_in ;
5259 }
5360 err = deflate (& stream , sourceLen ? Z_NO_FLUSH : Z_FINISH );
5461 } while (err == Z_OK );
5562
56- * destLen = stream .total_out ;
63+ * destLen = ( z_size_t )( stream .next_out - dest ) ;
5764 deflateEnd (& stream );
5865 return err == Z_STREAM_END ? Z_OK : err ;
5966}
60-
67+ int ZEXPORT compress2 (Bytef * dest , uLongf * destLen , const Bytef * source ,
68+ uLong sourceLen , int level ) {
69+ int ret ;
70+ z_size_t got = * destLen ;
71+ ret = compress2_z (dest , & got , source , sourceLen , level );
72+ * destLen = (uLong )got ;
73+ return ret ;
74+ }
6175/* ===========================================================================
6276 */
77+ int ZEXPORT compress_z (Bytef * dest , z_size_t * destLen , const Bytef * source ,
78+ z_size_t sourceLen ) {
79+ return compress2_z (dest , destLen , source , sourceLen ,
80+ Z_DEFAULT_COMPRESSION );
81+ }
6382int ZEXPORT compress (Bytef * dest , uLongf * destLen , const Bytef * source ,
6483 uLong sourceLen ) {
6584 return compress2 (dest , destLen , source , sourceLen , Z_DEFAULT_COMPRESSION );
@@ -69,7 +88,12 @@ int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
6988 If the default memLevel or windowBits for deflateInit() is changed, then
7089 this function needs to be updated.
7190 */
91+ z_size_t ZEXPORT compressBound_z (z_size_t sourceLen ) {
92+ z_size_t bound = sourceLen + (sourceLen >> 12 ) + (sourceLen >> 14 ) +
93+ (sourceLen >> 25 ) + 13 ;
94+ return bound < sourceLen ? (z_size_t )- 1 : bound ;
95+ }
7296uLong ZEXPORT compressBound (uLong sourceLen ) {
73- return sourceLen + (sourceLen >> 12 ) + ( sourceLen >> 14 ) +
74- ( sourceLen >> 25 ) + 13 ;
97+ z_size_t bound = compressBound_z (sourceLen );
98+ return ( uLong ) bound != bound ? ( uLong ) - 1 : ( uLong ) bound ;
7599}
0 commit comments