-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathxz_fopen_wrapper.c
387 lines (324 loc) · 11.1 KB
/
xz_fopen_wrapper.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Payden Sutherland <[email protected]> |
| Mostly looked at ext/zlib for guidance |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_xz.h"
#include "fopen_wrappers.h"
#include <lzma.h>
struct php_xz_stream_data_t {
lzma_stream strm;
size_t in_buf_sz;
size_t out_buf_sz;
uint8_t *in_buf;
uint8_t *out_buf;
uint8_t *out_buf_idx; //only used for decompressing
php_stream *stream;
int fd;
char mode[4];
unsigned long level;
};
static int php_xz_decompress(struct php_xz_stream_data_t *self TSRMLS_DC)
{
lzma_stream *strm = &self->strm;
lzma_action action = LZMA_RUN;
if (strm->avail_in == 0 && !php_stream_eof(self->stream)) {
strm->next_in = self->in_buf;
strm->avail_in = php_stream_read(self->stream, self->in_buf, self->in_buf_sz);
}
lzma_ret ret = lzma_code(strm, action);
if (strm->avail_out == 0 && self->out_buf_idx == strm->next_out) {
//have read all bytes in output buffer in php_xziop_read()
strm->next_out = self->out_buf_idx = self->out_buf;
strm->avail_out = self->out_buf_sz;
}
}
static int php_xz_compress(struct php_xz_stream_data_t *self TSRMLS_DC)
{
//this function will attempt to consume all bytes in lzma_stream->next_in
// and write to underlying file
lzma_stream *strm = &self->strm;
lzma_action action = LZMA_RUN;
int wrote = 0;
int to_write = strm->avail_in;
while (strm->avail_in > 0) {
lzma_ret ret = lzma_code(strm, action);
size_t write_size = self->out_buf_sz - strm->avail_out;
php_stream_write(self->stream, self->out_buf, write_size);
strm->next_out = self->out_buf;
strm->avail_out = self->out_buf_sz;
}
strm->next_in = self->in_buf;
return to_write;
}
static int php_xz_init_decoder(struct php_xz_stream_data_t *self)
{
lzma_stream *strm = &self->strm;
lzma_ret ret = lzma_stream_decoder(strm, UINT64_MAX, LZMA_CONCATENATED);
if (ret == LZMA_OK) {
self->in_buf_sz = XZ_INBUF_SIZE;
self->out_buf_sz = XZ_OUTBUF_SIZE;
self->in_buf = emalloc(self->in_buf_sz);
self->out_buf = emalloc(self->out_buf_sz);
self->out_buf_idx = self->out_buf;
strm->next_in = self->in_buf;
strm->avail_in = 0;
strm->next_out = self->out_buf;
strm->avail_out = self->out_buf_sz;
return 1;
}
/*const char *msg;
switch(ret) {
case LZMA_MEM_ERROR:
msg = "Memory allocation";
break;
case LZMA_OPTIONS_ERROR:
msg = "Unsupported decompress flags";
break;
default:
msg = "Unknown";
break;
}*/
return 0;
}
static int php_xz_init_encoder(struct php_xz_stream_data_t *self)
{
lzma_stream *strm = &self->strm;
lzma_options_lzma opt_lzma2;
if (lzma_lzma_preset(&opt_lzma2, self->level)) {
return 0;
}
lzma_filter filters[] = {
{ .id = LZMA_FILTER_LZMA2, .options = &opt_lzma2 },
{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
};
lzma_ret ret = lzma_stream_encoder(strm, filters, LZMA_CHECK_CRC64);
if (ret == LZMA_OK) {
self->in_buf_sz = XZ_INBUF_SIZE;
self->out_buf_sz = XZ_OUTBUF_SIZE;
self->in_buf = emalloc(self->in_buf_sz);
self->out_buf = emalloc(self->out_buf_sz);
strm->avail_in = 0;
strm->next_in = self->in_buf;
strm->avail_out = self->out_buf_sz;
strm->next_out = self->out_buf;
return 1;
}
/*const char *msg;
switch (ret) {
case LZMA_MEM_ERROR:
msg = "Memory allocation failed";
break;
case LZMA_OPTIONS_ERROR:
msg = "Specified filter chain is not supported";
break;
case LZMA_UNSUPPORTED_CHECK:
msg = "Specified integrity check is not supported";
break;
default:
msg = "Unknown error, possibly a bug";
break;
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, msg);
*/
return 0;
}
static size_t php_xziop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
lzma_stream *strm = &self->strm;
size_t to_read = count;
size_t have_read = 0;
while (to_read > 0) {
if (to_read < strm->next_out - self->out_buf_idx) {
memcpy(buf + have_read, self->out_buf_idx, to_read);
self->out_buf_idx += to_read;
have_read += to_read;
break;
} else if (strm->next_out - self->out_buf_idx > 0) {
memcpy(buf + have_read, self->out_buf_idx, strm->next_out - self->out_buf_idx);
have_read += strm->next_out - self->out_buf_idx;
to_read -= strm->next_out - self->out_buf_idx;
self->out_buf_idx = strm->next_out;
if (strm->next_out == self->out_buf_idx) {
self->out_buf_idx = strm->next_out = self->out_buf;
strm->avail_out = self->out_buf_sz;
}
}
if (self->out_buf_idx == strm->next_out && php_stream_eof(self->stream) && strm->avail_in == 0) {
stream->eof = 1;
return have_read;
}
php_xz_decompress(self TSRMLS_CC);
}
return have_read;
}
static size_t php_xziop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
int wrote = 0;
int bytes_consumed = 0;
lzma_stream *strm = &self->strm;
while (count - wrote > self->in_buf_sz - strm->avail_in) {
//copy as many bytes into next_in buffer as we can
memcpy((void *)self->in_buf + strm->avail_in, buf + wrote, self->in_buf_sz - strm->avail_in);
wrote += self->in_buf_sz - strm->avail_in;
strm->avail_in = self->in_buf_sz;
bytes_consumed = php_xz_compress(self TSRMLS_CC);
if (bytes_consumed < 0) {
break;
}
}
if (count - wrote > 0) {
memcpy((void *)self->in_buf + strm->avail_in, buf + wrote, count - wrote);
strm->avail_in += count - wrote;
}
return count;
}
static int php_xziop_close(php_stream *stream, int close_handle TSRMLS_DC)
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
int ret = EOF;
lzma_stream *strm = &self->strm;
//if write mode, finish writing out lzma stuff.
if (strcmp(self->mode, "w") == 0 || strcmp(self->mode, "wb") == 0) {
lzma_ret lz_ret;
do {
strm->next_out = self->out_buf;
strm->avail_out = self->out_buf_sz;
lzma_action action = LZMA_FINISH;
lz_ret = lzma_code(strm, action);
if (strm->avail_out < self->out_buf_sz) {
size_t write_size = self->out_buf_sz - strm->avail_out;
php_stream_write(self->stream, self->out_buf, write_size);
strm->next_out = self->out_buf;
strm->avail_out = self->out_buf_sz;
}
} while (lz_ret == LZMA_OK);
}
lzma_end(&self->strm);
if (self->stream) {
php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
}
efree(self->in_buf);
efree(self->out_buf);
efree(self);
return ret;
}
static int php_xziop_flush(php_stream *stream TSRMLS_DC)
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
if (strcmp(self->mode, "w") == 0 || strcmp(self->mode, "wb") == 0) {
php_xz_compress(self TSRMLS_CC);
}
php_stream_flush(self->stream);
return 0;
}
php_stream_ops php_stream_xzio_ops = {
php_xziop_write, php_xziop_read,
php_xziop_close, php_xziop_flush,
"XZ",
NULL, /* seek */
NULL, /* cast */
NULL, /* stat */
NULL /* set_option */
};
php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, char *path, char *mode_pass, int options,
char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
{
char mode[64];
unsigned long level = 6;
php_stream *stream = NULL, *innerstream = NULL;
struct php_xz_stream_data_t *self;
strncpy(mode, mode_pass, 63);
//we allocated this in PHP_FUNCTION manually, free it.
efree(mode_pass);
//split compression level out of mode
char *colonp = strchr(mode, ':');
if (colonp) {
level = strtoul(colonp+1, NULL, 10);
*colonp = '\0'; //terminate string early.
}
if (strchr(mode, '+') || strcmp(mode, "rw") == 0) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "cannot open xz stream for reading and writing at the same time.");
return NULL;
}
if (level > 9 || level < 0) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid compression level");
return NULL;
}
if (strncasecmp("compress.lzma://", path, 16) == 0) {
path += 16;
}
innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
if (innerstream) {
int fd;
if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **)&fd, REPORT_ERRORS)) {
self = ecalloc(1, sizeof(*self));
self->stream = innerstream;
self->fd = fd;
self->level = level;
strncpy(self->mode, mode, sizeof(self->mode));
stream = php_stream_alloc_rel(&php_stream_xzio_ops, self, 0, mode);
if (stream) {
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
if (strcmp(mode, "w") == 0 || strcmp(mode, "wb") == 0) {
if (!php_xz_init_encoder(self)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize xz encoder.");
efree(self);
php_stream_close(stream);
return NULL;
}
} else if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0) {
if (!php_xz_init_decoder(self)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize xz decoder");
efree(self);
php_stream_close(stream);
return NULL;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only open in read (r) or write (w) mode.");
efree(self);
php_stream_close(stream);
return NULL;
}
return stream;
}
efree(self);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed opening xz stream");
}
php_stream_close(innerstream);
}
return NULL;
}
static php_stream_wrapper_ops xz_stream_wops = {
php_stream_xzopen,
NULL, /* close */
NULL, /* stat */
NULL, /* stat_url */
NULL, /* opendir */
"XZ",
NULL, /* unlink */
NULL, /* rename */
NULL, /* mkdir */
NULL /* rmdir */
};
php_stream_wrapper php_stream_xz_wrapper = {
&xz_stream_wops,
NULL,
0 /* is_url */
};