-
Notifications
You must be signed in to change notification settings - Fork 1
/
hs.c
275 lines (239 loc) · 8.94 KB
/
hs.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
Copyright (c) 2014 Alex Stanev <alex@stanev.org>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_hs.h"
/* heatshrink */
#include "heatshrink/heatshrink_encoder.h"
#include "heatshrink/heatshrink_decoder.h"
#define DEF_WINDOW_SZ2 8
#define DEF_LOOKAHEAD_SZ2 4
#define DEF_DECODER_INPUT_BUFFER_SIZE 256
#define DEF_BUFFER_SIZE (64 * 1024)
static ZEND_FUNCTION(hs_compress);
static ZEND_FUNCTION(hs_decompress);
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_compress, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, window)
ZEND_ARG_INFO(0, lookahead)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_decompress, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, window)
ZEND_ARG_INFO(0, lookahead)
ZEND_END_ARG_INFO()
static zend_function_entry hs_functions[] = {
ZEND_FE(hs_compress, arginfo_hs_compress)
ZEND_FE(hs_decompress, arginfo_hs_decompress)
ZEND_FE_END
};
ZEND_MINFO_FUNCTION(hs)
{
char buffer[128];
php_info_print_table_start();
php_info_print_table_row(2, "heatshrink support", "enabled");
php_info_print_table_row(2, "Extension Version", HS_EXT_VERSION);
snprintf(buffer, 128, "%d.%d.%d",
HEATSHRINK_VERSION_MAJOR, HEATSHRINK_VERSION_MINOR, HEATSHRINK_VERSION_PATCH);
php_info_print_table_row(2, "Interface Version", buffer);
php_info_print_table_end();
}
zend_module_entry hs_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"hs",
hs_functions,
NULL,
NULL,
NULL,
NULL,
ZEND_MINFO(hs),
#if ZEND_MODULE_API_NO >= 20010901
HS_EXT_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_HS
ZEND_GET_MODULE(hs)
#endif
/* PHP */
static ZEND_FUNCTION(hs_compress)
{
zval *data;
size_t data_len;
size_t sink_sz = 0;
size_t poll_sz = 0;
HSE_sink_res sres;
HSE_poll_res pres;
HSE_finish_res fres;
//Default window_sz2 and lookahead_sz2 values
uint8_t window = DEF_WINDOW_SZ2;
uint8_t lookahead = DEF_LOOKAHEAD_SZ2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z|ll", &data, &window,
&lookahead) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(data) != IS_STRING) {
zend_error(E_WARNING, "hs_compress : expects parameter to be string.");
RETURN_FALSE;
}
//Allocate encoder
heatshrink_encoder *hse = heatshrink_encoder_alloc(window, lookahead);
if (hse == NULL) {
zend_error(E_WARNING, "hs_compress : heatshrink_encoder_alloc error. Check window and lookahead values.");
RETURN_FALSE;
}
data_len = Z_STRLEN_P(data);
//TODO: think of better way to compute outbuff size
size_t outbuff_len = data_len+1;
unsigned char *outbuff = (unsigned char *) emalloc(outbuff_len);
size_t sunk = 0;
do {
sres = heatshrink_encoder_sink(hse, (unsigned char *) &(Z_STRVAL_P(data)[sunk]), data_len - sunk, &sink_sz);
if (sres < 0) {
zend_error(E_WARNING, "hs_compress : heatshrink_encoder_sink error");
efree(outbuff);
heatshrink_encoder_free(hse);
RETURN_FALSE;
}
sunk += sink_sz;
do {
do {
pres = heatshrink_encoder_poll(hse, &outbuff[poll_sz], outbuff_len - poll_sz, &sink_sz);
if (pres < 0) {
zend_error(E_WARNING, "hs_compress : heatshrink_encoder_poll error");
efree(outbuff);
heatshrink_encoder_free(hse);
RETURN_FALSE;
}
poll_sz += sink_sz;
if (poll_sz == outbuff_len && pres == HSER_POLL_MORE) {
//TODO: think of bettery way to compute buffer reallocation size
outbuff_len += data_len/2;
outbuff = erealloc(outbuff, outbuff_len);
}
} while (pres == HSER_POLL_MORE);
if (sunk == data_len) {
fres = heatshrink_encoder_finish(hse);
if (fres < 0) {
zend_error(E_WARNING, "hs_compress : heatshrink_encoder_finish error");
efree(outbuff);
heatshrink_encoder_free(hse);
RETURN_FALSE;
}
if (fres == HSER_FINISH_DONE) {
RETVAL_STRINGL((char *) outbuff, poll_sz, 1);
efree(outbuff);
heatshrink_encoder_free(hse);
return;
}
}
} while (fres == HSER_FINISH_MORE);
} while (sunk < data_len);
zend_error(E_WARNING, "hs_compress : general error");
efree(outbuff);
heatshrink_encoder_free(hse);
RETURN_FALSE;
}
static ZEND_FUNCTION(hs_decompress)
{
zval *data;
size_t data_len;
size_t sink_sz = 0;
size_t poll_sz = 0;
HSD_sink_res sres;
HSD_poll_res pres;
HSD_finish_res fres;
//Default window_sz2 and lookahead_sz2 values
uint8_t window = DEF_WINDOW_SZ2;
uint8_t lookahead = DEF_LOOKAHEAD_SZ2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z|ll", &data, &window,
&lookahead) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(data) != IS_STRING) {
zend_error(E_WARNING, "hs_decompress : expects parameter to be string.");
RETURN_FALSE;
}
//Allocate decoder
//TODO: How we calculate input buffer size?
heatshrink_decoder *hsd = heatshrink_decoder_alloc(DEF_DECODER_INPUT_BUFFER_SIZE, window, lookahead);
if (hsd == NULL) {
zend_error(E_WARNING, "hs_decompress : heatshrink_decoder_alloc error. Check window and lookahead values.");
RETURN_FALSE;
}
data_len = Z_STRLEN_P(data);
//TODO: think of better way to compute outbuff size
size_t outbuff_len = data_len*2;
unsigned char *outbuff = (unsigned char *) emalloc(outbuff_len);
size_t sunk = 0;
do {
sres = heatshrink_decoder_sink(hsd, (unsigned char *) &(Z_STRVAL_P(data)[sunk]), data_len - sunk, &sink_sz);
if (sres < 0) {
zend_error(E_WARNING, "hs_decompress : heatshrink_decoder_sink error");
efree(outbuff);
heatshrink_decoder_free(hsd);
RETURN_FALSE;
}
sunk += sink_sz;
do {
do {
pres = heatshrink_decoder_poll(hsd, &outbuff[poll_sz], outbuff_len - poll_sz, &sink_sz);
if (pres < 0) {
zend_error(E_WARNING, "hs_compress : heatshrink_decoder_poll error");
efree(outbuff);
heatshrink_decoder_free(hsd);
RETURN_FALSE;
}
poll_sz += sink_sz;
if (poll_sz == outbuff_len && pres == HSDR_POLL_MORE) {
//TODO: think of bettery way to compute buffer reallocation size
outbuff_len += data_len/2;
outbuff = erealloc(outbuff, outbuff_len);
}
} while (pres == HSDR_POLL_MORE);
if (sunk == data_len) {
fres = heatshrink_decoder_finish(hsd);
if (fres < 0) {
zend_error(E_WARNING, "hs_decompress : heatshrink_decoder_finish error");
efree(outbuff);
heatshrink_decoder_free(hsd);
RETURN_FALSE;
}
if (fres == HSDR_FINISH_DONE) {
RETVAL_STRINGL((char *) outbuff, poll_sz, 1);
efree(outbuff);
heatshrink_decoder_free(hsd);
return;
}
}
} while (fres == HSDR_FINISH_MORE);
} while (sunk < data_len);
zend_error(E_WARNING, "hs_decompress : general error");
efree(outbuff);
heatshrink_decoder_free(hsd);
RETURN_FALSE;
}