This repository was archived by the owner on Aug 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlibdeflate.c
More file actions
175 lines (148 loc) · 5.05 KB
/
Copy pathlibdeflate.c
File metadata and controls
175 lines (148 loc) · 5.05 KB
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/spl/spl_exceptions.h"
#include "ext/standard/info.h"
#include "Zend/zend_exceptions.h"
#include "php_libdeflate.h"
#include "libdeflate.h"
#define MAX_COMPRESSION_LEVEL 12
#define COMPRESSOR_CACHE_SIZE (MAX_COMPRESSION_LEVEL + 1) //extra slot for level 0
ZEND_BEGIN_MODULE_GLOBALS(libdeflate)
struct libdeflate_compressor* compressor_cache[COMPRESSOR_CACHE_SIZE];
ZEND_END_MODULE_GLOBALS(libdeflate);
#define LIBDEFLATE_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(libdeflate, v)
ZEND_DECLARE_MODULE_GLOBALS(libdeflate);
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(libdeflate)
{
#if defined(ZTS) && defined(COMPILE_DL_LIBDEFLATE)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
memset(LIBDEFLATE_G(compressor_cache), 0, sizeof(struct libdeflate_compressor*) * COMPRESSOR_CACHE_SIZE);
return SUCCESS;
}
/* }}} */
/* {{{ */
PHP_RSHUTDOWN_FUNCTION(libdeflate) {
for (int i = 0; i < COMPRESSOR_CACHE_SIZE; i++) {
struct libdeflate_compressor* compressor = LIBDEFLATE_G(compressor_cache)[i];
if (compressor != NULL) {
libdeflate_free_compressor(compressor);
}
}
return SUCCESS;
} /* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(libdeflate)
{
php_info_print_table_start();
php_info_print_table_header(2, "libdeflate support", "enabled");
php_info_print_table_header(2, "libdeflate library version", LIBDEFLATE_VERSION_STRING);
php_info_print_table_end();
}
/* }}} */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_libdeflate_compress, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, level, IS_LONG, 0)
ZEND_END_ARG_INFO()
typedef size_t (*php_libdeflate_compress_bound_func)(struct libdeflate_compressor*, size_t);
typedef size_t (*php_libdeflate_compress_func)(struct libdeflate_compressor*, const void*, size_t, void*, size_t);
static inline zend_string* php_libdeflate_compress(zend_string *data, zend_long level, php_libdeflate_compress_bound_func compressBoundFunc, php_libdeflate_compress_func compressFunc) {
if (level < 0 || level > MAX_COMPRESSION_LEVEL) {
zend_throw_exception_ex(
spl_ce_InvalidArgumentException,
0,
"Invalid compression level: %zi (accepted levels: %u...%u)",
level,
0,
MAX_COMPRESSION_LEVEL
);
return NULL;
}
struct libdeflate_compressor* compressor = LIBDEFLATE_G(compressor_cache)[level];
if (compressor == NULL) {
compressor = libdeflate_alloc_compressor(level);
if (compressor == NULL) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Unable to allocate libdeflate compressor (this is a bug)");
return NULL;
}
LIBDEFLATE_G(compressor_cache)[level] = compressor;
}
size_t compressBound = compressBoundFunc(compressor, ZSTR_LEN(data));
void* output = emalloc(compressBound);
size_t actualSize = compressFunc(compressor, ZSTR_VAL(data), ZSTR_LEN(data), output, compressBound);
if (actualSize == 0){
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Too small buffer provided (this is a bug)");
return NULL;
}
zend_string* result = zend_string_init(output, actualSize, 0);
efree(output);
return result;
}
// php7 warns rather that throws with ZEND_PARSE_PARAMETERS_START
#if PHP_VERSION_ID >= 80000
#define LIBDEEFLATE_PARSE_PARAMS() ZEND_PARSE_PARAMETERS_START(1, 2)
#else
#define LIBDEEFLATE_PARSE_PARAMS() ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 2)
#endif
#define PHP_LIBDEFLATE_FUNC(compressFunc, compressBoundFunc) \
PHP_FUNCTION(compressFunc) { \
zend_string *data; \
zend_long level = 6; \
\
LIBDEEFLATE_PARSE_PARAMS() \
Z_PARAM_STR(data) \
Z_PARAM_OPTIONAL \
Z_PARAM_LONG(level) \
ZEND_PARSE_PARAMETERS_END(); \
\
zend_string *result = php_libdeflate_compress(data, level, compressBoundFunc, compressFunc); \
if (result == NULL) { \
return; \
} \
RETURN_STR(result); \
}
/* {{{ proto string libdeflate_deflate_compress(string $data [, int $level = 6]) */
PHP_LIBDEFLATE_FUNC(libdeflate_deflate_compress, libdeflate_deflate_compress_bound) /* }}} */
/* {{{ proto string libdeflate_zlib_compress(string $data [, int $level = 6]) */
PHP_LIBDEFLATE_FUNC(libdeflate_zlib_compress, libdeflate_zlib_compress_bound) /* }}} */
/* {{{ proto string libdeflate_gzip_compress(string $data [, int $level = 6]) */
PHP_LIBDEFLATE_FUNC(libdeflate_gzip_compress, libdeflate_gzip_compress_bound) /* }}} */
/* {{{ libdeflate_functions[]
*/
static const zend_function_entry libdeflate_functions[] = {
PHP_FE(libdeflate_deflate_compress, arginfo_libdeflate_compress)
PHP_FE(libdeflate_zlib_compress, arginfo_libdeflate_compress)
PHP_FE(libdeflate_gzip_compress, arginfo_libdeflate_compress)
PHP_FE_END
};
/* }}} */
/* {{{ libdeflate_module_entry
*/
zend_module_entry libdeflate_module_entry = {
STANDARD_MODULE_HEADER,
"libdeflate",
libdeflate_functions,
NULL,
NULL, /* PHP_MSHUTDOWN */
PHP_RINIT(libdeflate),
PHP_RSHUTDOWN(libdeflate),
PHP_MINFO(libdeflate),
PHP_LIBDEFLATE_VERSION,
PHP_MODULE_GLOBALS(libdeflate),
NULL,
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_LIBDEFLATE
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(libdeflate)
#endif