abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
uncompr.c
Go to the documentation of this file.
1 /* uncompr.c -- decompress a memory buffer
2  * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 /* @(#) $Id$ */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "misc/util/abc_global.h"
12 
13 #define ZLIB_INTERNAL
14 #include "zlib.h"
15 
17 
18 /* ===========================================================================
19  Decompresses the source buffer into the destination buffer. sourceLen is
20  the byte length of the source buffer. Upon entry, destLen is the total
21  size of the destination buffer, which must be large enough to hold the
22  entire uncompressed data. (The size of the uncompressed data must have
23  been saved previously by the compressor and transmitted to the decompressor
24  by some mechanism outside the scope of this compression library.)
25  Upon exit, destLen is the actual size of the compressed buffer.
26 
27  uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
28  enough memory, Z_BUF_ERROR if there was not enough room in the output
29  buffer, or Z_DATA_ERROR if the input data was corrupted.
30 */
31 int ZEXPORT uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
32 {
33  z_stream stream;
34  int err;
35 
36  stream.next_in = (Bytef*)source;
37  stream.avail_in = (uInt)sourceLen;
38  /* Check for source > 64K on 16-bit machine: */
39  if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
40 
41  stream.next_out = dest;
42  stream.avail_out = (uInt)*destLen;
43  if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
44 
45  stream.zalloc = (alloc_func)0;
46  stream.zfree = (free_func)0;
47 
48  err = inflateInit(&stream);
49  if (err != Z_OK) return err;
50 
51  err = inflate(&stream, Z_FINISH);
52  if (err != Z_STREAM_END) {
53  inflateEnd(&stream);
54  if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
55  return Z_DATA_ERROR;
56  return err;
57  }
58  *destLen = stream.total_out;
59 
60  err = inflateEnd(&stream);
61  return err;
62 }
63 
64 
65 
67 
Bytef * next_in
Definition: zlib.h:94
uInt avail_in
Definition: zlib.h:95
#define Z_NEED_DICT
Definition: zlib.h:183
unsigned long uLong
Definition: zconf.h:336
Byte FAR Bytef
Definition: zconf.h:342
free_func zfree
Definition: zlib.h:106
#define Z_FINISH
Definition: zlib.h:176
#define Z_DATA_ERROR
Definition: zlib.h:186
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define Z_STREAM_END
Definition: zlib.h:182
alloc_func zalloc
Definition: zlib.h:105
Bytef * next_out
Definition: zlib.h:98
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
uLong total_out
Definition: zlib.h:100
uLong FAR uLongf
Definition: zconf.h:347
#define Z_BUF_ERROR
Definition: zlib.h:188
#define inflateInit(strm)
Definition: zlib.h:1556
uInt avail_out
Definition: zlib.h:99
#define Z_OK
Definition: zlib.h:181
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:580
ABC_NAMESPACE_IMPL_START int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: uncompr.c:31
#define ZEXPORT
Definition: zconf.h:322
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.c:1227
unsigned int uInt
Definition: zconf.h:335