summaryrefslogtreecommitdiff
path: root/core/zlib.c
blob: 2ea5d9db52b3f6508345110a38b3fa423f68ec94 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "uwsgi.h"

char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };

extern struct uwsgi_server uwsgi;

char *uwsgi_gzip_chunk(z_stream *z, uint32_t *crc32, char *buf, size_t len, size_t *dlen) {
	uwsgi_crc32(crc32, buf, len);
	return uwsgi_deflate(z, buf, len, dlen);
}

struct uwsgi_buffer *uwsgi_gzip(char *buf, size_t len) {
	z_stream z;
	struct uwsgi_buffer *ub = NULL;
	uint32_t gzip_crc32 = 0;
	char *gzipped = NULL;
	char *gzipped0 = NULL;
	size_t dlen = 0;
	size_t dlen0 = 0;

	uwsgi_crc32(&gzip_crc32, NULL, 0);
	if (uwsgi_deflate_init(&z, NULL, 0)) return NULL; 
	uwsgi_crc32(&gzip_crc32, buf, len);

	gzipped = uwsgi_deflate(&z, buf, len, &dlen);
	if (!gzipped) goto end;
	gzipped0 = uwsgi_deflate(&z, NULL, 0, &dlen0);
	if (!gzipped0) goto end;

	ub = uwsgi_buffer_new(10 + dlen + dlen0 + 8);
	if (uwsgi_buffer_append(ub, gzheader, 10)) goto end;
	if (uwsgi_buffer_append(ub, gzipped, dlen)) goto end;
	if (uwsgi_buffer_append(ub, gzipped0, dlen0)) goto end;
	if (uwsgi_buffer_u32le(ub, gzip_crc32)) goto end;
        if (uwsgi_buffer_u32le(ub, len)) goto end;
end:
	if (gzipped) free(gzipped);
	if (gzipped0) free(gzipped0);
	deflateEnd(&z);
	return ub;
}

struct uwsgi_buffer *uwsgi_zlib_decompress(char *buf, size_t len) {
        z_stream z;

	z.zalloc = Z_NULL;
        z.zfree = Z_NULL;
        z.opaque = Z_NULL;
        if (inflateInit(&z) != Z_OK) {
		return NULL;
        }

        struct uwsgi_buffer *ub = uwsgi_buffer_new(uwsgi.page_size);
	unsigned char out[8192];

	z.next_in = (unsigned char *)buf;
	z.avail_in = len;
	z.next_out = out;

	do {
		z.avail_out = 8192;
		z.next_out = out;
		int ret = inflate(&z, Z_NO_FLUSH);
		if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
			uwsgi_buffer_destroy(ub);
			ub = NULL;
			goto end;			
		}
		if (uwsgi_buffer_append(ub, (char *)out, 8192 - z.avail_out)) {
			uwsgi_buffer_destroy(ub);
			ub = NULL;
			goto end;			
		}
	}
	while (z.avail_out == 0);
end:
        inflateEnd(&z);
        return ub;
}


int uwsgi_deflate_init(z_stream *z, char *dict, size_t dict_len) {
        z->zalloc = Z_NULL;
        z->zfree = Z_NULL;
        z->opaque = Z_NULL;
        if (deflateInit2(z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY) != Z_OK) {
	//if (deflateInit(z, Z_DEFAULT_COMPRESSION)) {
		return -1;
	}
	if (dict && dict_len) {
                if (deflateSetDictionary(z, (Bytef *) dict, dict_len) != Z_OK) {
                        return -1;
                }
	}
	return 0;
}

int uwsgi_inflate_init(z_stream *z, char *dict, size_t dict_len) {
        z->zalloc = Z_NULL;
        z->zfree = Z_NULL;
        z->opaque = Z_NULL;
        if (inflateInit2(z, 16+MAX_WBITS) != Z_OK) {
                return -1;
        }
        if (dict && dict_len) {
                if (inflateSetDictionary(z, (Bytef *) dict, dict_len) != Z_OK) {
                        return -1;
                }
        }
        return 0;
}

int uwsgi_gzip_prepare(z_stream *z, char *dict, size_t dict_len, uint32_t *crc32) {
	uwsgi_crc32(crc32, NULL, 0);
	if (uwsgi_deflate_init(z, NULL, 0)) return -1;
	return 0;
}

// fix and free a gzip stream
int uwsgi_gzip_fix(z_stream *z, uint32_t crc32, struct uwsgi_buffer *ub, size_t len) {
	size_t dlen0 = 0;
	char *gzipped0 = uwsgi_deflate(z, NULL, 0, &dlen0);
        if (!gzipped0) goto end;
	if (uwsgi_buffer_append(ub, gzipped0, dlen0)) goto end;
	free(gzipped0);
	if (uwsgi_buffer_u32le(ub, crc32)) goto end2;
        if (uwsgi_buffer_u32le(ub, len)) goto end2;
	deflateEnd(z);
	return 0;
end:
	if (gzipped0) free(gzipped0);
end2:
	deflateEnd(z);
	return -1;
}


char *uwsgi_deflate(z_stream *z, char *buf, size_t len, size_t *dlen) {

	// calculate the amount of bytes needed for output (+30 should be enough)
        Bytef *dbuf = uwsgi_malloc(len+30);
	z->avail_in = len;
	z->next_in = (Bytef *) buf;
	z->avail_out = len+30;
	z->next_out = dbuf;

	if (len > 0) {
		if (deflate(z, Z_SYNC_FLUSH) != Z_OK) {
			free(dbuf);
			return NULL;
		}
	}
	else {
        	if (deflate(z, Z_FINISH) != Z_STREAM_END) {
                	free(dbuf);
                	return NULL;
		}
		deflateEnd(z);
        }

        *dlen = (z->next_out - dbuf);
        return (char *) dbuf;
}

void uwsgi_crc32(uint32_t *ctx, char *buf, size_t len) {
	if (!buf) {
		*ctx = crc32(*ctx, Z_NULL, 0);
	}
	else {
		*ctx = crc32(*ctx, (const Bytef *) buf, len);
	}
}