1 /* $NetBSD: compress.c,v 1.2 2006/01/27 00:45:27 christos Exp $ */
3 /* compress.c -- compress a memory buffer
4 * Copyright (C) 1995-2003 Jean-loup Gailly.
5 * For conditions of distribution and use, see copyright notice in zlib.h
13 /* ===========================================================================
14 Compresses the source buffer into the destination buffer. The level
15 parameter has the same meaning as in deflateInit. sourceLen is the byte
16 length of the source buffer. Upon entry, destLen is the total size of the
17 destination buffer, which must be at least 0.1% larger than sourceLen plus
18 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
20 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
21 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
22 Z_STREAM_ERROR if the level parameter is invalid.
24 int ZEXPORT
compress2 (dest
, destLen
, source
, sourceLen
, level
)
34 stream
.next_in
= __UNCONST(source
);
35 stream
.avail_in
= (uInt
)sourceLen
;
37 /* Check for source > 64K on 16-bit machine: */
38 if ((uLong
)stream
.avail_in
!= sourceLen
) return Z_BUF_ERROR
;
40 stream
.next_out
= dest
;
41 stream
.avail_out
= (uInt
)*destLen
;
42 if ((uLong
)stream
.avail_out
!= *destLen
) return Z_BUF_ERROR
;
44 stream
.zalloc
= (alloc_func
)0;
45 stream
.zfree
= (free_func
)0;
46 stream
.opaque
= (voidpf
)0;
48 err
= deflateInit(&stream
, level
);
49 if (err
!= Z_OK
) return err
;
51 err
= deflate(&stream
, Z_FINISH
);
52 if (err
!= Z_STREAM_END
) {
54 return err
== Z_OK
? Z_BUF_ERROR
: err
;
56 *destLen
= stream
.total_out
;
58 err
= deflateEnd(&stream
);
62 /* ===========================================================================
64 int ZEXPORT
compress (dest
, destLen
, source
, sourceLen
)
70 return compress2(dest
, destLen
, source
, sourceLen
, Z_DEFAULT_COMPRESSION
);
73 /* ===========================================================================
74 If the default memLevel or windowBits for deflateInit() is changed, then
75 this function needs to be updated.
77 uLong ZEXPORT
compressBound (sourceLen
)
80 return sourceLen
+ (sourceLen
>> 12) + (sourceLen
>> 14) + 11;