-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlib_utils.c
86 lines (65 loc) · 2.01 KB
/
zlib_utils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* This file is part of hwzip from https://www.hanshq.net/zip.html
It is put in the public domain; see the LICENSE file for details. */
#include "zlib_utils.h"
#include <assert.h>
size_t zlib_deflate(const uint8_t *src, size_t src_len, int level,
uint8_t *dst, size_t dst_cap)
{
z_stream strm;
int ret;
size_t dst_size;
assert(level >= 0 && level <= 9);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit2(&strm, level, Z_DEFLATED, -15, 9,
level == 1 ? Z_FIXED : Z_DEFAULT_STRATEGY);
assert(ret == Z_OK);
(void)ret;
strm.avail_in = (unsigned)src_len;
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-qual"
#endif
strm.next_in = (uint8_t*)src;
#ifdef __clang__
#pragma clang diagnostic pop
#endif
strm.avail_out = (unsigned)dst_cap;
strm.next_out = dst;
ret = deflate(&strm, Z_FINISH);
assert(ret != Z_STREAM_ERROR);
assert(strm.avail_in == 0);
dst_size = dst_cap - strm.avail_out;
deflateEnd(&strm);
return dst_size;
}
size_t zlib_inflate(const uint8_t *src, size_t src_len,
uint8_t *dst, size_t dst_cap)
{
z_stream strm;
int ret;
size_t dst_size;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = inflateInit2(&strm, -15);
assert(ret == Z_OK);
(void)ret;
strm.avail_in = (unsigned)src_len;
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-qual"
#endif
strm.next_in = (uint8_t*)src;
#ifdef __clang__
#pragma clang diagnostic pop
#endif
strm.avail_out = (unsigned)dst_cap;
strm.next_out = dst;
ret = inflate(&strm, Z_FINISH);
assert(ret == Z_STREAM_END);
dst_size = dst_cap - strm.avail_out;
inflateEnd(&strm);
return dst_size;
}