forked from kjdev/php-ext-lz4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlz4.c
203 lines (171 loc) · 5.35 KB
/
lz4.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
/*
Copyright (c) 2013 kjdev
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 "php_ini.h"
#include "ext/standard/info.h"
#include "php_verdep.h"
#include "php_lz4.h"
/* lz4 */
#include "lz4/lz4.h"
#include "lz4/lz4hc.h"
static ZEND_FUNCTION(lz4_compress);
static ZEND_FUNCTION(lz4_uncompress);
ZEND_BEGIN_ARG_INFO_EX(arginfo_lz4_compress, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, high)
ZEND_ARG_INFO(0, extra)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_lz4_uncompress, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, max)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
static zend_function_entry lz4_functions[] = {
ZEND_FE(lz4_compress, arginfo_lz4_compress)
ZEND_FE(lz4_uncompress, arginfo_lz4_uncompress)
ZEND_FE_END
};
ZEND_MINFO_FUNCTION(lz4)
{
char buffer[128];
php_info_print_table_start();
php_info_print_table_row(2, "LZ4 support", "enabled");
php_info_print_table_row(2, "Extension Version", LZ4_EXT_VERSION);
snprintf(buffer, 128, "%d.%d.%d",
LZ4_VERSION_MAJOR, LZ4_VERSION_MINOR, LZ4_VERSION_RELEASE);
php_info_print_table_row(2, "Interface Version", buffer);
php_info_print_table_end();
}
zend_module_entry lz4_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"lz4",
lz4_functions,
NULL,
NULL,
NULL,
NULL,
ZEND_MINFO(lz4),
#if ZEND_MODULE_API_NO >= 20010901
LZ4_EXT_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_LZ4
ZEND_GET_MODULE(lz4)
#endif
static ZEND_FUNCTION(lz4_compress)
{
zval *data;
char *output;
int output_len, data_len;
zend_bool high = 0;
char *extra = NULL;
int extra_len = -1;
int offset = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z|bs", &data, &high,
&extra, &extra_len) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(data) != IS_STRING) {
zend_error(E_WARNING,
"lz4_compress : expects parameter to be string.");
RETURN_FALSE;
}
if (extra && extra_len > 0) {
offset = extra_len;
} else {
offset = sizeof(int);
}
data_len = Z_STRLEN_P(data);
output = (char *)emalloc(LZ4_compressBound(data_len) + offset);
if (!output) {
zend_error(E_WARNING, "lz4_compress : memory error");
RETURN_FALSE;
}
if (extra && extra_len > 0) {
memcpy(output, extra, offset);
} else {
/* Set the data length */
memcpy(output, &data_len, offset);
}
if (high) {
output_len = LZ4_compressHC(Z_STRVAL_P(data), output + offset, data_len);
} else {
output_len = LZ4_compress(Z_STRVAL_P(data), output + offset, data_len);
}
if (output_len <= 0) {
RETVAL_FALSE;
} else {
RETVAL_STRINGL(output, output_len + offset, 1);
}
efree(output);
}
static ZEND_FUNCTION(lz4_uncompress)
{
zval *data;
int output_len, data_size;
char *output;
long max_size = -1, offset = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"z|ll", &data, &max_size, &offset) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(data) != IS_STRING) {
zend_error(E_WARNING,
"lz4_uncompress : expects parameter to be string.");
RETURN_FALSE;
}
if (max_size > 0) {
data_size = max_size;
if (!offset) {
offset = sizeof(int);
}
} else {
/* Get data length */
offset = sizeof(int);
memcpy(&data_size, Z_STRVAL_P(data), offset);
}
if (data_size < 0) {
zend_error(E_WARNING, "lz4_uncompress : allocate size error");
RETURN_FALSE;
}
output = (char *)emalloc(data_size + 1);
if (!output) {
zend_error(E_WARNING, "lz4_uncompress : memory error");
RETURN_FALSE;
}
output_len = LZ4_decompress_safe(Z_STRVAL_P(data) + offset,
output,
Z_STRLEN_P(data) - offset,
data_size);
if (output_len <= 0) {
zend_error(E_WARNING, "lz4_uncompress : data error");
RETVAL_FALSE;
} else {
RETVAL_STRINGL(output, output_len, 1);
}
efree(output);
}