This repository was archived by the owner on Apr 20, 2022. It is now read-only.
forked from couchbaselabs/php-ext-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcompress.c
238 lines (200 loc) · 6.42 KB
/
compress.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright 2012 Couchbase, Inc. |
+----------------------------------------------------------------------+
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| http://www.apache.org/licenses/LICENSE-2.0 |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
+----------------------------------------------------------------------+
*/
/**
* Compression stuff.
* First, I'd like to note that I painfully had to go through and poke around
* the php-memcached extension code from whence the compression headers and
* semantics are derived
*
* The compression scheme is fairly simple, and is compatible with the older
* memcached php clients.
*
* The first sizeof(uint32_t) bytes contain the uncompressed length, and the
* compressed data follows.
*
* To make the rest of the code easier to follow, I've divided the compression
* and decompression functions into these steps:
*
* 1) Allocate a buffer. This is the upper bound for allocation. This maintains
* a private pointer to the base allocation, and exposes a 'data' field
* which is an offset of sizeof(uint32_t) bytes into the allocated buffer.
*
* The 'space' is subsequently sizeof(uint32_t) bytes less than the actual
* allocated length (but we don't care about this..)
*
* 2) Compress the input.. this is simple.. and involves populating the ->len
* field of the structure with the compressed length
*
* 3) 'Deploy' the buffer. Encode the length as a size_t into the 'header'.
*
*
*/
#include "internal.h"
#define CBCOMPHDR_SANITY_MAX 0x40000000
static char *cbcomp_new(php_couchbase_comp *str,
size_t cmpbuf_len, size_t origlen)
{
str->alloc = cmpbuf_len;
str->_base = emalloc(str->alloc + sizeof(pcbc_payload_len_t));
if (!str->_base) {
return NULL; /* enomem */
}
str->data = str->_base + sizeof(pcbc_payload_len_t);
*(pcbc_payload_len_t *)str->_base = origlen;
return str->data;
}
PHP_COUCHBASE_LOCAL
void cbcomp_free(php_couchbase_comp *str)
{
free(str->_base);
str->data = NULL;
str->_base = NULL;
}
/**
* Called to 'deploy' the compressed buffer. This adjusts the base and the
* length so that they will include the "header". Do not call this function
* twice on the same comp
*/
PHP_COUCHBASE_LOCAL
void cbcomp_deploy(php_couchbase_comp *str)
{
/* Expose the rest, now that we're done with compression */
str->data = str->_base;
str->compressed_len += sizeof(pcbc_payload_len_t);
}
/* headers which claim an uncompressed size above this figure are bad */
#define DECOMP_SANITY_LIMIT 0x40000000
/* make sure to initialize info to zero before passing here */
PHP_COUCHBASE_LOCAL
int cbcomp_dcmp_init(const char *data, size_t len,
php_couchbase_decomp *info)
{
/* make a sane value size, say, 1GB? */
if (len < sizeof(pcbc_payload_len_t)) {
return 0;
}
info->orig = data;
info->orig_len = len;
info->compressed = data + sizeof(pcbc_payload_len_t);
info->compressed_len = len - sizeof(pcbc_payload_len_t);
info->expanded_len = *(pcbc_payload_len_t *)data;
info->expanded = NULL;
return 1;
}
PHP_COUCHBASE_LOCAL
void cbcomp_dcmp_free(php_couchbase_decomp *info)
{
if (info->expanded) {
efree(info->expanded);
info->expanded = NULL;
}
}
#ifdef HAVE_COMPRESSION_ZLIB
PHP_COUCHBASE_LOCAL
int php_couchbase_compress_zlib(const smart_str *input,
php_couchbase_comp *output)
{
/* compressBound tells us the maximum size of the buffer we'll need.. */
cbcomp_new(output, compressBound(input->len), input->len);
uLongf tmp_ulen = output->alloc;
int status = compress(
(Bytef *)output->data, &tmp_ulen,
(Bytef *)input->c, input->len);
if (status == Z_OK) {
output->compressed_len = tmp_ulen;
return 1;
}
return 0;
}
PHP_COUCHBASE_LOCAL
int php_couchbase_decompress_zlib(php_couchbase_decomp *info)
{
int status;
uLongf dlen;
size_t n_alloc;
/**
* in the event that the header is zero, make it default to 4096.
* This will automatically fall back to 'old-style' decompression.
*/
if (!info->expanded_len) {
info->expanded_len = 4096;
}
info->expanded = emalloc(info->expanded_len);
n_alloc = info->expanded_len;
dlen = n_alloc;
if (!info->expanded_len) {
return 0;
}
status = Z_BUF_ERROR;
/**
* sanity check, don't allocate over a GB, we should make this number
* smaller though
*/
if (info->expanded_len < CBCOMPHDR_SANITY_MAX) {
status = uncompress((Bytef *)info->expanded, &dlen,
(Bytef *)info->compressed, info->orig_len);
}
/* fall back to 'old-style' decompression */
while (status == Z_BUF_ERROR) {
n_alloc *= 2;
info->expanded = erealloc(info->expanded, n_alloc);
dlen = n_alloc;
status = uncompress((Bytef *)info->expanded, &dlen,
(Bytef *)info->orig, info->orig_len);
}
if (status != Z_OK) {
efree(info->expanded);
info->expanded = NULL;
} else {
info->expanded_len = dlen;
}
return status == Z_OK;
}
#endif /* HAVE_COMPRESSION_ZLIB */
PHP_COUCHBASE_LOCAL
int php_couchbase_compress_fastlz(const smart_str *input,
php_couchbase_comp *output)
{
cbcomp_new(output,
(size_t)((input->len * 1.05) + 1),
input->len);
return (output->compressed_len =
(fastlz_compress(input->c, input->len, output->data)));
}
PHP_COUCHBASE_LOCAL
int php_couchbase_decompress_fastlz(php_couchbase_decomp *info)
{
if (info->expanded_len == 0 || info->expanded_len > CBCOMPHDR_SANITY_MAX) {
return 0;
}
info->expanded = emalloc(info->expanded_len);
if (!info->expanded) {
return 0;
}
info->expanded_len = fastlz_decompress(info->compressed,
info->compressed_len, info->expanded, info->expanded_len);
return info->expanded_len;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/