1 | /* |
2 | * Copyright (c) 2008-2016 Apple Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License |
8 | * Version 2.0 (the 'License'). You may not use this file except in |
9 | * compliance with the License. The rights granted to you under the License |
10 | * may not be used to create, or enable the creation or redistribution of, |
11 | * unlawful or unlicensed copies of an Apple operating system, or to |
12 | * circumvent, violate, or enable the circumvention or violation of, any |
13 | * terms of an Apple operating system software license agreement. |
14 | * |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | * |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and |
24 | * limitations under the License. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | /* compress.c -- compress a memory buffer |
29 | * Copyright (C) 1995-2003 Jean-loup Gailly. |
30 | * For conditions of distribution and use, see copyright notice in zlib.h |
31 | */ |
32 | |
33 | /* @(#) $Id$ */ |
34 | |
35 | #define ZLIB_INTERNAL |
36 | #if KERNEL |
37 | #include <libkern/zlib.h> |
38 | #else |
39 | #include "zlib.h" |
40 | #endif /* KERNEL */ |
41 | |
42 | /* =========================================================================== |
43 | Compresses the source buffer into the destination buffer. The level |
44 | parameter has the same meaning as in deflateInit. sourceLen is the byte |
45 | length of the source buffer. Upon entry, destLen is the total size of the |
46 | destination buffer, which must be at least 0.1% larger than sourceLen plus |
47 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. |
48 | |
49 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough |
50 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, |
51 | Z_STREAM_ERROR if the level parameter is invalid. |
52 | */ |
53 | int ZEXPORT |
54 | compress2(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, |
55 | int level) |
56 | { |
57 | z_stream stream; |
58 | int err; |
59 | |
60 | stream.next_in = (Bytef*)source; |
61 | stream.avail_in = (uInt)sourceLen; |
62 | #ifdef MAXSEG_64K |
63 | /* Check for source > 64K on 16-bit machine: */ |
64 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; |
65 | #endif |
66 | stream.next_out = dest; |
67 | stream.avail_out = (uInt)*destLen; |
68 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; |
69 | |
70 | stream.zalloc = (alloc_func)0; |
71 | stream.zfree = (free_func)0; |
72 | stream.opaque = (voidpf)0; |
73 | |
74 | err = deflateInit(&stream, level); |
75 | if (err != Z_OK) return err; |
76 | |
77 | err = deflate(strm: &stream, Z_FINISH); |
78 | if (err != Z_STREAM_END) { |
79 | deflateEnd(strm: &stream); |
80 | return err == Z_OK ? Z_BUF_ERROR : err; |
81 | } |
82 | *destLen = stream.total_out; |
83 | |
84 | err = deflateEnd(strm: &stream); |
85 | return err; |
86 | } |
87 | |
88 | /* =========================================================================== |
89 | */ |
90 | int ZEXPORT |
91 | compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) |
92 | { |
93 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); |
94 | } |
95 | |
96 | /* =========================================================================== |
97 | If the default memLevel or windowBits for deflateInit() is changed, then |
98 | this function needs to be updated. |
99 | */ |
100 | uLong ZEXPORT |
101 | compressBound(uLong sourceLen) |
102 | { |
103 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; |
104 | } |
105 | |