]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /* |
2 | * Copyright (c) 2008 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 | /* uncompr.c -- decompress 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 | Decompresses the source buffer into the destination buffer. sourceLen is | |
44 | the byte length of the source buffer. Upon entry, destLen is the total | |
45 | size of the destination buffer, which must be large enough to hold the | |
46 | entire uncompressed data. (The size of the uncompressed data must have | |
47 | been saved previously by the compressor and transmitted to the decompressor | |
48 | by some mechanism outside the scope of this compression library.) | |
49 | Upon exit, destLen is the actual size of the compressed buffer. | |
50 | This function can be used to decompress a whole file at once if the | |
51 | input file is mmap'ed. | |
52 | ||
53 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not | |
54 | enough memory, Z_BUF_ERROR if there was not enough room in the output | |
55 | buffer, or Z_DATA_ERROR if the input data was corrupted. | |
56 | */ | |
57 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) | |
58 | Bytef *dest; | |
59 | uLongf *destLen; | |
60 | const Bytef *source; | |
61 | uLong sourceLen; | |
62 | { | |
63 | z_stream stream; | |
64 | int err; | |
65 | ||
66 | stream.next_in = (Bytef*)source; | |
67 | stream.avail_in = (uInt)sourceLen; | |
68 | /* Check for source > 64K on 16-bit machine: */ | |
69 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; | |
70 | ||
71 | stream.next_out = dest; | |
72 | stream.avail_out = (uInt)*destLen; | |
73 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; | |
74 | ||
75 | stream.zalloc = (alloc_func)0; | |
76 | stream.zfree = (free_func)0; | |
77 | ||
78 | err = inflateInit(&stream); | |
79 | if (err != Z_OK) return err; | |
80 | ||
81 | err = inflate(&stream, Z_FINISH); | |
82 | if (err != Z_STREAM_END) { | |
83 | inflateEnd(&stream); | |
84 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) | |
85 | return Z_DATA_ERROR; | |
86 | return err; | |
87 | } | |
88 | *destLen = stream.total_out; | |
89 | ||
90 | err = inflateEnd(&stream); | |
91 | return err; | |
92 | } |