11/* compress.c -- compress a memory buffer
2- * Copyright (C) 1995-2005 Jean-loup Gailly.
2+ * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
33 * For conditions of distribution and use, see copyright notice in zlib.h
44 */
55
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.
2121*/
22- int ZEXPORT compress2 (dest , destLen , source , sourceLen , level )
23- Bytef * dest ;
24- uLongf * destLen ;
25- const Bytef * source ;
26- uLong sourceLen ;
27- int level ;
28- {
22+ int ZEXPORT compress2 (Bytef * dest , uLongf * destLen , const Bytef * source ,
23+ uLong sourceLen , int level ) {
2924 z_stream stream ;
3025 int err ;
26+ const uInt max = (uInt )- 1 ;
27+ uLong left ;
3128
32- stream .next_in = (z_const Bytef * )source ;
33- stream .avail_in = (uInt )sourceLen ;
34- #ifdef MAXSEG_64K
35- /* Check for source > 64K on 16-bit machine: */
36- if ((uLong )stream .avail_in != sourceLen ) return Z_BUF_ERROR ;
37- #endif
38- stream .next_out = dest ;
39- stream .avail_out = (uInt )* destLen ;
40- if ((uLong )stream .avail_out != * destLen ) return Z_BUF_ERROR ;
29+ left = * destLen ;
30+ * destLen = 0 ;
4131
4232 stream .zalloc = (alloc_func )0 ;
4333 stream .zfree = (free_func )0 ;
@@ -46,35 +36,40 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
4636 err = deflateInit (& stream , level );
4737 if (err != Z_OK ) return err ;
4838
49- err = deflate (& stream , Z_FINISH );
50- if (err != Z_STREAM_END ) {
51- deflateEnd (& stream );
52- return err == Z_OK ? Z_BUF_ERROR : err ;
53- }
54- * destLen = stream .total_out ;
39+ stream .next_out = dest ;
40+ stream .avail_out = 0 ;
41+ stream .next_in = (z_const Bytef * )source ;
42+ stream .avail_in = 0 ;
43+
44+ do {
45+ if (stream .avail_out == 0 ) {
46+ stream .avail_out = left > (uLong )max ? max : (uInt )left ;
47+ left -= stream .avail_out ;
48+ }
49+ if (stream .avail_in == 0 ) {
50+ stream .avail_in = sourceLen > (uLong )max ? max : (uInt )sourceLen ;
51+ sourceLen -= stream .avail_in ;
52+ }
53+ err = deflate (& stream , sourceLen ? Z_NO_FLUSH : Z_FINISH );
54+ } while (err == Z_OK );
5555
56- err = deflateEnd (& stream );
57- return err ;
56+ * destLen = stream .total_out ;
57+ deflateEnd (& stream );
58+ return err == Z_STREAM_END ? Z_OK : err ;
5859}
5960
6061/* ===========================================================================
6162 */
62- int ZEXPORT compress (dest , destLen , source , sourceLen )
63- Bytef * dest ;
64- uLongf * destLen ;
65- const Bytef * source ;
66- uLong sourceLen ;
67- {
63+ int ZEXPORT compress (Bytef * dest , uLongf * destLen , const Bytef * source ,
64+ uLong sourceLen ) {
6865 return compress2 (dest , destLen , source , sourceLen , Z_DEFAULT_COMPRESSION );
6966}
7067
7168/* ===========================================================================
7269 If the default memLevel or windowBits for deflateInit() is changed, then
7370 this function needs to be updated.
7471 */
75- uLong ZEXPORT compressBound (sourceLen )
76- uLong sourceLen ;
77- {
72+ uLong ZEXPORT compressBound (uLong sourceLen ) {
7873 return sourceLen + (sourceLen >> 12 ) + (sourceLen >> 14 ) +
7974 (sourceLen >> 25 ) + 13 ;
8075}
0 commit comments