This source file includes following definitions.
- ZEND_GET_MODULE
- php_bz2iop_read
- php_bz2iop_write
- php_bz2iop_close
- php_bz2iop_flush
- _php_stream_bz2open_from_BZFILE
- _php_stream_bz2open
- PHP_MINIT_FUNCTION
- PHP_MSHUTDOWN_FUNCTION
- PHP_MINFO_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- php_bz2_error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_bz2.h"
27
28 #if HAVE_BZ2
29
30
31 #include "ext/standard/file.h"
32 #include "ext/standard/info.h"
33 #include "ext/standard/php_string.h"
34 #include "main/php_network.h"
35
36
37 #include <stdio.h>
38
39
40 #define PHP_BZ_ERRNO 0
41 #define PHP_BZ_ERRSTR 1
42 #define PHP_BZ_ERRBOTH 2
43
44 static PHP_MINIT_FUNCTION(bz2);
45 static PHP_MSHUTDOWN_FUNCTION(bz2);
46 static PHP_MINFO_FUNCTION(bz2);
47 static PHP_FUNCTION(bzopen);
48 static PHP_FUNCTION(bzread);
49 static PHP_FUNCTION(bzerrno);
50 static PHP_FUNCTION(bzerrstr);
51 static PHP_FUNCTION(bzerror);
52 static PHP_FUNCTION(bzcompress);
53 static PHP_FUNCTION(bzdecompress);
54
55
56 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzread, 0, 0, 1)
57 ZEND_ARG_INFO(0, bz)
58 ZEND_ARG_INFO(0, length)
59 ZEND_END_ARG_INFO()
60
61 ZEND_BEGIN_ARG_INFO(arginfo_bzopen, 0)
62 ZEND_ARG_INFO(0, file)
63 ZEND_ARG_INFO(0, mode)
64 ZEND_END_ARG_INFO()
65
66 ZEND_BEGIN_ARG_INFO(arginfo_bzerrno, 0)
67 ZEND_ARG_INFO(0, bz)
68 ZEND_END_ARG_INFO()
69
70 ZEND_BEGIN_ARG_INFO(arginfo_bzerrstr, 0)
71 ZEND_ARG_INFO(0, bz)
72 ZEND_END_ARG_INFO()
73
74 ZEND_BEGIN_ARG_INFO(arginfo_bzerror, 0)
75 ZEND_ARG_INFO(0, bz)
76 ZEND_END_ARG_INFO()
77
78 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzcompress, 0, 0, 2)
79 ZEND_ARG_INFO(0, source)
80 ZEND_ARG_INFO(0, blocksize)
81 ZEND_ARG_INFO(0, workfactor)
82 ZEND_END_ARG_INFO()
83
84 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzdecompress, 0, 0, 1)
85 ZEND_ARG_INFO(0, source)
86 ZEND_ARG_INFO(0, small)
87 ZEND_END_ARG_INFO()
88
89 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzwrite, 0, 0, 2)
90 ZEND_ARG_INFO(0, fp)
91 ZEND_ARG_INFO(0, str)
92 ZEND_ARG_INFO(0, length)
93 ZEND_END_ARG_INFO()
94
95 ZEND_BEGIN_ARG_INFO(arginfo_bzflush, 0)
96 ZEND_ARG_INFO(0, fp)
97 ZEND_END_ARG_INFO()
98
99
100 static const zend_function_entry bz2_functions[] = {
101 PHP_FE(bzopen, arginfo_bzopen)
102 PHP_FE(bzread, arginfo_bzread)
103 PHP_FALIAS(bzwrite, fwrite, arginfo_bzwrite)
104 PHP_FALIAS(bzflush, fflush, arginfo_bzflush)
105 PHP_FALIAS(bzclose, fclose, arginfo_bzflush)
106 PHP_FE(bzerrno, arginfo_bzerrno)
107 PHP_FE(bzerrstr, arginfo_bzerrstr)
108 PHP_FE(bzerror, arginfo_bzerror)
109 PHP_FE(bzcompress, arginfo_bzcompress)
110 PHP_FE(bzdecompress, arginfo_bzdecompress)
111 PHP_FE_END
112 };
113
114 zend_module_entry bz2_module_entry = {
115 STANDARD_MODULE_HEADER,
116 "bz2",
117 bz2_functions,
118 PHP_MINIT(bz2),
119 PHP_MSHUTDOWN(bz2),
120 NULL,
121 NULL,
122 PHP_MINFO(bz2),
123 PHP_BZ2_VERSION,
124 STANDARD_MODULE_PROPERTIES
125 };
126
127 #ifdef COMPILE_DL_BZ2
128 ZEND_GET_MODULE(bz2)
129 #endif
130
131 struct php_bz2_stream_data_t {
132 BZFILE *bz_file;
133 php_stream *stream;
134 };
135
136
137
138 static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count)
139 {
140 struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
141 size_t ret = 0;
142
143 do {
144 int just_read;
145 size_t remain = count - ret;
146 int to_read = (int)(remain <= INT_MAX ? remain : INT_MAX);
147
148 just_read = BZ2_bzread(self->bz_file, buf, to_read);
149
150 if (just_read < 1) {
151 stream->eof = 0 == just_read;
152 break;
153 }
154
155 ret += just_read;
156 } while (ret < count);
157
158 return ret;
159 }
160
161 static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count)
162 {
163 size_t wrote = 0;
164 struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
165
166
167 do {
168 int just_wrote;
169 size_t remain = count - wrote;
170 int to_write = (int)(remain <= INT_MAX ? remain : INT_MAX);
171
172 just_wrote = BZ2_bzwrite(self->bz_file, (char*)buf, to_write);
173
174 if (just_wrote < 1) {
175 break;
176 }
177
178 wrote += just_wrote;
179
180 } while (wrote < count);
181
182 return wrote;
183 }
184
185 static int php_bz2iop_close(php_stream *stream, int close_handle)
186 {
187 struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
188 int ret = EOF;
189
190 if (close_handle) {
191 BZ2_bzclose(self->bz_file);
192 }
193
194 if (self->stream) {
195 php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
196 }
197
198 efree(self);
199
200 return ret;
201 }
202
203 static int php_bz2iop_flush(php_stream *stream)
204 {
205 struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
206 return BZ2_bzflush(self->bz_file);
207 }
208
209
210 php_stream_ops php_stream_bz2io_ops = {
211 php_bz2iop_write, php_bz2iop_read,
212 php_bz2iop_close, php_bz2iop_flush,
213 "BZip2",
214 NULL,
215 NULL,
216 NULL,
217 NULL
218 };
219
220
221 PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz,
222 const char *mode, php_stream *innerstream STREAMS_DC)
223 {
224 struct php_bz2_stream_data_t *self;
225
226 self = emalloc(sizeof(*self));
227
228 self->stream = innerstream;
229 if (innerstream) {
230 GC_REFCOUNT(innerstream->res)++;
231 }
232 self->bz_file = bz;
233
234 return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode);
235 }
236
237 PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
238 const char *path,
239 const char *mode,
240 int options,
241 zend_string **opened_path,
242 php_stream_context *context STREAMS_DC)
243 {
244 php_stream *retstream = NULL, *stream = NULL;
245 char *path_copy = NULL;
246 BZFILE *bz_file = NULL;
247
248 if (strncasecmp("compress.bzip2://", path, 17) == 0) {
249 path += 17;
250 }
251 if (mode[0] == '\0' || (mode[0] != 'w' && mode[0] != 'r' && mode[1] != '\0')) {
252 return NULL;
253 }
254
255 #ifdef VIRTUAL_DIR
256 virtual_filepath_ex(path, &path_copy, NULL);
257 #else
258 path_copy = (char *)path;
259 #endif
260
261 if (php_check_open_basedir(path_copy)) {
262 #ifdef VIRTUAL_DIR
263 efree(path_copy);
264 #endif
265 return NULL;
266 }
267
268
269 bz_file = BZ2_bzopen(path_copy, mode);
270
271 if (opened_path && bz_file) {
272 *opened_path = zend_string_init(path_copy, strlen(path_copy), 0);
273 }
274
275 #ifdef VIRTUAL_DIR
276 efree(path_copy);
277 #endif
278
279 if (bz_file == NULL) {
280
281 stream = php_stream_open_wrapper(path, mode, options | STREAM_WILL_CAST, opened_path);
282
283 if (stream) {
284 php_socket_t fd;
285 if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
286 bz_file = BZ2_bzdopen((int)fd, mode);
287 }
288 }
289
290
291
292
293 if (opened_path && !bz_file && mode[0] == 'w') {
294 VCWD_UNLINK(ZSTR_VAL(*opened_path));
295 }
296 }
297
298 if (bz_file) {
299 retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC);
300 if (retstream) {
301 return retstream;
302 }
303
304 BZ2_bzclose(bz_file);
305 }
306
307 if (stream) {
308 php_stream_close(stream);
309 }
310
311 return NULL;
312 }
313
314
315
316 static php_stream_wrapper_ops bzip2_stream_wops = {
317 _php_stream_bz2open,
318 NULL,
319 NULL,
320 NULL,
321 NULL,
322 "BZip2",
323 NULL,
324 NULL,
325 NULL,
326 NULL
327 };
328
329 static php_stream_wrapper php_stream_bzip2_wrapper = {
330 &bzip2_stream_wops,
331 NULL,
332 0
333 };
334
335 static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int);
336
337 static PHP_MINIT_FUNCTION(bz2)
338 {
339 php_register_url_stream_wrapper("compress.bzip2", &php_stream_bzip2_wrapper);
340 php_stream_filter_register_factory("bzip2.*", &php_bz2_filter_factory);
341 return SUCCESS;
342 }
343
344 static PHP_MSHUTDOWN_FUNCTION(bz2)
345 {
346 php_unregister_url_stream_wrapper("compress.bzip2");
347 php_stream_filter_unregister_factory("bzip2.*");
348
349 return SUCCESS;
350 }
351
352 static PHP_MINFO_FUNCTION(bz2)
353 {
354 php_info_print_table_start();
355 php_info_print_table_row(2, "BZip2 Support", "Enabled");
356 php_info_print_table_row(2, "Stream Wrapper support", "compress.bzip2://");
357 php_info_print_table_row(2, "Stream Filter support", "bzip2.decompress, bzip2.compress");
358 php_info_print_table_row(2, "BZip2 Version", (char *) BZ2_bzlibVersion());
359 php_info_print_table_end();
360 }
361
362
363
364 static PHP_FUNCTION(bzread)
365 {
366 zval *bz;
367 zend_long len = 1024;
368 php_stream *stream;
369 zend_string *data;
370
371 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &bz, &len)) {
372 RETURN_FALSE;
373 }
374
375 php_stream_from_zval(stream, bz);
376
377 if ((len + 1) < 1) {
378 php_error_docref(NULL, E_WARNING, "length may not be negative");
379 RETURN_FALSE;
380 }
381 data = zend_string_alloc(len, 0);
382 ZSTR_LEN(data) = php_stream_read(stream, ZSTR_VAL(data), ZSTR_LEN(data));
383 ZSTR_VAL(data)[ZSTR_LEN(data)] = '\0';
384
385 RETURN_NEW_STR(data);
386 }
387
388
389
390
391 static PHP_FUNCTION(bzopen)
392 {
393 zval *file;
394 char *mode;
395 size_t mode_len;
396
397 BZFILE *bz;
398 php_stream *stream = NULL;
399
400 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &file, &mode, &mode_len) == FAILURE) {
401 return;
402 }
403
404 if (mode_len != 1 || (mode[0] != 'r' && mode[0] != 'w')) {
405 php_error_docref(NULL, E_WARNING, "'%s' is not a valid mode for bzopen(). Only 'w' and 'r' are supported.", mode);
406 RETURN_FALSE;
407 }
408
409
410 if (Z_TYPE_P(file) == IS_STRING) {
411 if (Z_STRLEN_P(file) == 0) {
412 php_error_docref(NULL, E_WARNING, "filename cannot be empty");
413 RETURN_FALSE;
414 }
415
416 if (CHECK_ZVAL_NULL_PATH(file)) {
417 RETURN_FALSE;
418 }
419
420 stream = php_stream_bz2open(NULL, Z_STRVAL_P(file), mode, REPORT_ERRORS, NULL);
421 } else if (Z_TYPE_P(file) == IS_RESOURCE) {
422
423 php_socket_t fd;
424 size_t stream_mode_len;
425
426 php_stream_from_zval(stream, file);
427 stream_mode_len = strlen(stream->mode);
428
429 if (stream_mode_len != 1 && !(stream_mode_len == 2 && memchr(stream->mode, 'b', 2))) {
430 php_error_docref(NULL, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
431 RETURN_FALSE;
432 } else if (stream_mode_len == 1 && stream->mode[0] != 'r' && stream->mode[0] != 'w' && stream->mode[0] != 'a' && stream->mode[0] != 'x') {
433 php_error_docref(NULL, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
434 RETURN_FALSE;
435 }
436
437 switch(mode[0]) {
438 case 'r':
439
440 if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])) {
441 php_error_docref(NULL, E_WARNING, "cannot read from a stream opened in write only mode");
442 RETURN_FALSE;
443 }
444 break;
445 case 'w':
446
447 if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])
448 && stream->mode[0] != 'a' && !(stream_mode_len == 2 && stream->mode[1] != 'a')
449 && stream->mode[0] != 'x' && !(stream_mode_len == 2 && stream->mode[1] != 'x')) {
450 php_error_docref(NULL, E_WARNING, "cannot write to a stream opened in read only mode");
451 RETURN_FALSE;
452 }
453 break;
454 default:
455
456 break;
457 }
458
459 if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) {
460 RETURN_FALSE;
461 }
462
463 bz = BZ2_bzdopen((int)fd, mode);
464
465 stream = php_stream_bz2open_from_BZFILE(bz, mode, stream);
466 } else {
467 php_error_docref(NULL, E_WARNING, "first parameter has to be string or file-resource");
468 RETURN_FALSE;
469 }
470
471 if (stream) {
472 php_stream_to_zval(stream, return_value);
473 } else {
474 RETURN_FALSE;
475 }
476 }
477
478
479
480
481 static PHP_FUNCTION(bzerrno)
482 {
483 php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRNO);
484 }
485
486
487
488
489 static PHP_FUNCTION(bzerrstr)
490 {
491 php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRSTR);
492 }
493
494
495
496
497 static PHP_FUNCTION(bzerror)
498 {
499 php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRBOTH);
500 }
501
502
503
504
505 static PHP_FUNCTION(bzcompress)
506 {
507 char *source;
508 zend_long zblock_size = 0;
509 zend_long zwork_factor = 0;
510 zend_string *dest = NULL;
511 int error,
512 block_size = 4,
513 work_factor = 0,
514 argc;
515 size_t source_len;
516 unsigned int dest_len;
517
518 argc = ZEND_NUM_ARGS();
519
520 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &source, &source_len, &zblock_size, &zwork_factor) == FAILURE) {
521 return;
522 }
523
524
525
526
527
528 dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
529
530
531 dest = zend_string_alloc(dest_len, 0);
532
533
534 if (argc > 1) {
535 block_size = zblock_size;
536 }
537
538 if (argc > 2) {
539 work_factor = zwork_factor;
540 }
541
542 error = BZ2_bzBuffToBuffCompress(ZSTR_VAL(dest), &dest_len, source, source_len, block_size, 0, work_factor);
543 if (error != BZ_OK) {
544 zend_string_free(dest);
545 RETURN_LONG(error);
546 } else {
547
548
549 ZSTR_LEN(dest) = dest_len;
550 ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
551 RETURN_NEW_STR(dest);
552 }
553 }
554
555
556
557
558 static PHP_FUNCTION(bzdecompress)
559 {
560 char *source, *dest;
561 size_t source_len;
562 int error;
563 zend_long small = 0;
564 #if defined(PHP_WIN32)
565 unsigned __int64 size = 0;
566 #else
567 unsigned long long size = 0;
568 #endif
569 bz_stream bzs;
570
571 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &source, &source_len, &small)) {
572 RETURN_FALSE;
573 }
574
575 bzs.bzalloc = NULL;
576 bzs.bzfree = NULL;
577
578 if (BZ2_bzDecompressInit(&bzs, 0, (int)small) != BZ_OK) {
579 RETURN_FALSE;
580 }
581
582 bzs.next_in = source;
583 bzs.avail_in = source_len;
584
585
586 bzs.avail_out = source_len * 2;
587 bzs.next_out = dest = emalloc(bzs.avail_out + 1);
588
589 while ((error = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) {
590
591 bzs.avail_out = source_len;
592 size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
593 dest = safe_erealloc(dest, 1, bzs.avail_out+1, (size_t) size );
594 bzs.next_out = dest + size;
595 }
596
597 if (error == BZ_STREAM_END || error == BZ_OK) {
598 size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
599 dest = safe_erealloc(dest, 1, (size_t) size, 1);
600 dest[size] = '\0';
601 RETVAL_STRINGL(dest, (int) size);
602 efree(dest);
603 } else {
604 efree(dest);
605 RETVAL_LONG(error);
606 }
607
608 BZ2_bzDecompressEnd(&bzs);
609 }
610
611
612
613
614 static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt)
615 {
616 zval *bzp;
617 php_stream *stream;
618 const char *errstr;
619 int errnum;
620 struct php_bz2_stream_data_t *self;
621
622 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &bzp) == FAILURE) {
623 return;
624 }
625
626 php_stream_from_zval(stream, bzp);
627
628 if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) {
629 RETURN_FALSE;
630 }
631
632 self = (struct php_bz2_stream_data_t *) stream->abstract;
633
634
635 errstr = BZ2_bzerror(self->bz_file, &errnum);
636
637
638 switch (opt) {
639 case PHP_BZ_ERRNO:
640 RETURN_LONG(errnum);
641 break;
642 case PHP_BZ_ERRSTR:
643 RETURN_STRING((char*)errstr);
644 break;
645 case PHP_BZ_ERRBOTH:
646 array_init(return_value);
647
648 add_assoc_long (return_value, "errno", errnum);
649 add_assoc_string(return_value, "errstr", (char*)errstr);
650 break;
651 }
652 }
653
654
655 #endif
656
657
658
659
660
661
662
663
664