]> git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/zlib/compress.c
Security-29.tar.gz
[apple/security.git] / SecurityServer / MacYarrow / zlib / compress.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /* compress.c -- compress a memory buffer
20 * Copyright (C) 1995-1998 Jean-loup Gailly.
21 * For conditions of distribution and use, see copyright notice in zlib.h
22 */
23
24 /* @(#) $Id: compress.c,v 1.1.1.1 2001/05/18 23:14:03 mb Exp $ */
25
26 #include "zlib.h"
27
28 /* ===========================================================================
29 Compresses the source buffer into the destination buffer. The level
30 parameter has the same meaning as in deflateInit. sourceLen is the byte
31 length of the source buffer. Upon entry, destLen is the total size of the
32 destination buffer, which must be at least 0.1% larger than sourceLen plus
33 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
34
35 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
36 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
37 Z_STREAM_ERROR if the level parameter is invalid.
38 */
39 int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
40 Bytef *dest;
41 uLongf *destLen;
42 const Bytef *source;
43 uLong sourceLen;
44 int level;
45 {
46 z_stream stream;
47 int err;
48
49 stream.next_in = (Bytef*)source;
50 stream.avail_in = (uInt)sourceLen;
51 #ifdef MAXSEG_64K
52 /* Check for source > 64K on 16-bit machine: */
53 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
54 #endif
55 stream.next_out = dest;
56 stream.avail_out = (uInt)*destLen;
57 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
58
59 stream.zalloc = (alloc_func)0;
60 stream.zfree = (free_func)0;
61 stream.opaque = (voidpf)0;
62
63 err = deflateInit(&stream, level);
64 if (err != Z_OK) return err;
65
66 err = deflate(&stream, Z_FINISH);
67 if (err != Z_STREAM_END) {
68 deflateEnd(&stream);
69 return err == Z_OK ? Z_BUF_ERROR : err;
70 }
71 *destLen = stream.total_out;
72
73 err = deflateEnd(&stream);
74 return err;
75 }
76
77 /* ===========================================================================
78 */
79 int ZEXPORT compress (dest, destLen, source, sourceLen)
80 Bytef *dest;
81 uLongf *destLen;
82 const Bytef *source;
83 uLong sourceLen;
84 {
85 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
86 }