2323 */
2424
2525/* compress.c -- compress a memory buffer
26- * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
26+ * Copyright (C) 1995-2026 Jean-loup Gailly, Mark Adler
2727 * For conditions of distribution and use, see copyright notice in zlib.h
2828 */
2929
4242 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
4343 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
4444 Z_STREAM_ERROR if the level parameter is invalid.
45+
46+ The _z versions of the functions take size_t length arguments.
4547*/
46- int ZEXPORT compress2 (Bytef * dest , uLongf * destLen , const Bytef * source ,
47- uLong sourceLen , int level ) {
48+ int ZEXPORT compress2_z (Bytef * dest , z_size_t * destLen , const Bytef * source ,
49+ z_size_t sourceLen , int level ) {
4850 z_stream stream ;
4951 int err ;
5052 const uInt max = (uInt )- 1 ;
51- uLong left ;
53+ z_size_t left ;
54+
55+ if ((sourceLen > 0 && source == NULL ) ||
56+ destLen == NULL || (* destLen > 0 && dest == NULL ))
57+ return Z_STREAM_ERROR ;
5258
5359 left = * destLen ;
5460 * destLen = 0 ;
@@ -67,23 +73,36 @@ int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
6773
6874 do {
6975 if (stream .avail_out == 0 ) {
70- stream .avail_out = left > (uLong )max ? max : (uInt )left ;
76+ stream .avail_out = left > (z_size_t )max ? max : (uInt )left ;
7177 left -= stream .avail_out ;
7278 }
7379 if (stream .avail_in == 0 ) {
74- stream .avail_in = sourceLen > (uLong )max ? max : (uInt )sourceLen ;
80+ stream .avail_in = sourceLen > (z_size_t )max ? max :
81+ (uInt )sourceLen ;
7582 sourceLen -= stream .avail_in ;
7683 }
7784 err = deflate (& stream , sourceLen ? Z_NO_FLUSH : Z_FINISH );
7885 } while (err == Z_OK );
7986
80- * destLen = stream .total_out ;
87+ * destLen = ( z_size_t )( stream .next_out - dest ) ;
8188 deflateEnd (& stream );
8289 return err == Z_STREAM_END ? Z_OK : err ;
8390}
84-
91+ int ZEXPORT compress2 (Bytef * dest , uLongf * destLen , const Bytef * source ,
92+ uLong sourceLen , int level ) {
93+ int ret ;
94+ z_size_t got = * destLen ;
95+ ret = compress2_z (dest , & got , source , sourceLen , level );
96+ * destLen = (uLong )got ;
97+ return ret ;
98+ }
8599/* ===========================================================================
86100 */
101+ int ZEXPORT compress_z (Bytef * dest , z_size_t * destLen , const Bytef * source ,
102+ z_size_t sourceLen ) {
103+ return compress2_z (dest , destLen , source , sourceLen ,
104+ Z_DEFAULT_COMPRESSION );
105+ }
87106int ZEXPORT compress (Bytef * dest , uLongf * destLen , const Bytef * source ,
88107 uLong sourceLen ) {
89108 return compress2 (dest , destLen , source , sourceLen , Z_DEFAULT_COMPRESSION );
@@ -93,7 +112,12 @@ int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
93112 If the default memLevel or windowBits for deflateInit() is changed, then
94113 this function needs to be updated.
95114 */
115+ z_size_t ZEXPORT compressBound_z (z_size_t sourceLen ) {
116+ z_size_t bound = sourceLen + (sourceLen >> 12 ) + (sourceLen >> 14 ) +
117+ (sourceLen >> 25 ) + 13 ;
118+ return bound < sourceLen ? (z_size_t )- 1 : bound ;
119+ }
96120uLong ZEXPORT compressBound (uLong sourceLen ) {
97- return sourceLen + (sourceLen >> 12 ) + ( sourceLen >> 14 ) +
98- ( sourceLen >> 25 ) + 13 ;
121+ z_size_t bound = compressBound_z (sourceLen );
122+ return ( uLong ) bound != bound ? ( uLong ) - 1 : ( uLong ) bound ;
99123}
0 commit comments