-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuncompress.c
57 lines (48 loc) · 1.42 KB
/
uncompress.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdbool.h>
#include <stdio.h>
#include <zlib.h>
#include "chaosvpn.h"
bool
uncompress_inflate(struct string *compressed, struct string *uncompressed)
{
int retval;
z_stream strm;
unsigned char outbuf[1024];
int have;
/* allocate inflate state */
memset(&strm, 0, sizeof(strm)); /* paranoia */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
if (inflateInit(&strm) != Z_OK)
return false;
strm.avail_in = string_length(compressed);
strm.next_in = (unsigned char*)string_get(compressed);
do {
strm.avail_out = sizeof(outbuf);
strm.next_out = outbuf;
retval = inflate(&strm, Z_FINISH);
switch (retval) {
case Z_STREAM_ERROR:
/* state clobbered */
return false;
break;
case Z_NEED_DICT:
retval = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return false;
}
have = sizeof(outbuf) - strm.avail_out;
string_concatb(uncompressed, (char *)outbuf, have);
} while (strm.avail_out == 0);
(void)inflateEnd(&strm);
if (retval != Z_STREAM_END) {
fprintf(stderr, "decompression failed\n");
return false;
}
return true;
}