bz2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sterling Hughes <sterling@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "php_bz2.h"
  21. #include "bz2_arginfo.h"
  22. #ifdef HAVE_BZ2
  23. /* PHP Includes */
  24. #include "ext/standard/info.h"
  25. #include "ext/standard/php_string.h"
  26. #include "main/php_network.h"
  27. /* for fileno() */
  28. #include <stdio.h>
  29. /* Internal error constants */
  30. #define PHP_BZ_ERRNO 0
  31. #define PHP_BZ_ERRSTR 1
  32. #define PHP_BZ_ERRBOTH 2
  33. static PHP_MINIT_FUNCTION(bz2);
  34. static PHP_MSHUTDOWN_FUNCTION(bz2);
  35. static PHP_MINFO_FUNCTION(bz2);
  36. zend_module_entry bz2_module_entry = {
  37. STANDARD_MODULE_HEADER,
  38. "bz2",
  39. ext_functions,
  40. PHP_MINIT(bz2),
  41. PHP_MSHUTDOWN(bz2),
  42. NULL,
  43. NULL,
  44. PHP_MINFO(bz2),
  45. PHP_BZ2_VERSION,
  46. STANDARD_MODULE_PROPERTIES
  47. };
  48. #ifdef COMPILE_DL_BZ2
  49. ZEND_GET_MODULE(bz2)
  50. #endif
  51. struct php_bz2_stream_data_t {
  52. BZFILE *bz_file;
  53. php_stream *stream;
  54. };
  55. /* {{{ BZip2 stream implementation */
  56. static ssize_t php_bz2iop_read(php_stream *stream, char *buf, size_t count)
  57. {
  58. struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
  59. size_t ret = 0;
  60. do {
  61. int just_read;
  62. size_t remain = count - ret;
  63. int to_read = (int)(remain <= INT_MAX ? remain : INT_MAX);
  64. just_read = BZ2_bzread(self->bz_file, buf, to_read);
  65. if (just_read < 1) {
  66. /* it is not safe to keep reading after an error, see #72613 */
  67. stream->eof = 1;
  68. if (just_read < 0) {
  69. if (ret) {
  70. return ret;
  71. }
  72. return -1;
  73. }
  74. break;
  75. }
  76. ret += just_read;
  77. } while (ret < count);
  78. return ret;
  79. }
  80. static ssize_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count)
  81. {
  82. ssize_t wrote = 0;
  83. struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
  84. do {
  85. int just_wrote;
  86. size_t remain = count - wrote;
  87. int to_write = (int)(remain <= INT_MAX ? remain : INT_MAX);
  88. just_wrote = BZ2_bzwrite(self->bz_file, (char*)buf, to_write);
  89. if (just_wrote < 0) {
  90. if (wrote == 0) {
  91. return just_wrote;
  92. }
  93. return wrote;
  94. }
  95. if (just_wrote == 0) {
  96. break;
  97. }
  98. wrote += just_wrote;
  99. } while (wrote < count);
  100. return wrote;
  101. }
  102. static int php_bz2iop_close(php_stream *stream, int close_handle)
  103. {
  104. struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
  105. int ret = EOF;
  106. if (close_handle) {
  107. BZ2_bzclose(self->bz_file);
  108. }
  109. if (self->stream) {
  110. php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
  111. }
  112. efree(self);
  113. return ret;
  114. }
  115. static int php_bz2iop_flush(php_stream *stream)
  116. {
  117. struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
  118. return BZ2_bzflush(self->bz_file);
  119. }
  120. /* }}} */
  121. const php_stream_ops php_stream_bz2io_ops = {
  122. php_bz2iop_write, php_bz2iop_read,
  123. php_bz2iop_close, php_bz2iop_flush,
  124. "BZip2",
  125. NULL, /* seek */
  126. NULL, /* cast */
  127. NULL, /* stat */
  128. NULL /* set_option */
  129. };
  130. /* {{{ Bzip2 stream openers */
  131. PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz,
  132. const char *mode, php_stream *innerstream STREAMS_DC)
  133. {
  134. struct php_bz2_stream_data_t *self;
  135. self = emalloc(sizeof(*self));
  136. self->stream = innerstream;
  137. if (innerstream) {
  138. GC_ADDREF(innerstream->res);
  139. }
  140. self->bz_file = bz;
  141. return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode);
  142. }
  143. PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
  144. const char *path,
  145. const char *mode,
  146. int options,
  147. zend_string **opened_path,
  148. php_stream_context *context STREAMS_DC)
  149. {
  150. php_stream *retstream = NULL, *stream = NULL;
  151. char *path_copy = NULL;
  152. BZFILE *bz_file = NULL;
  153. if (strncasecmp("compress.bzip2://", path, 17) == 0) {
  154. path += 17;
  155. }
  156. if (mode[0] == '\0' || (mode[0] != 'w' && mode[0] != 'r' && mode[1] != '\0')) {
  157. return NULL;
  158. }
  159. #ifdef VIRTUAL_DIR
  160. virtual_filepath_ex(path, &path_copy, NULL);
  161. #else
  162. path_copy = (char *)path;
  163. #endif
  164. if (php_check_open_basedir(path_copy)) {
  165. #ifdef VIRTUAL_DIR
  166. efree(path_copy);
  167. #endif
  168. return NULL;
  169. }
  170. /* try and open it directly first */
  171. bz_file = BZ2_bzopen(path_copy, mode);
  172. if (opened_path && bz_file) {
  173. *opened_path = zend_string_init(path_copy, strlen(path_copy), 0);
  174. }
  175. #ifdef VIRTUAL_DIR
  176. efree(path_copy);
  177. #endif
  178. if (bz_file == NULL) {
  179. /* that didn't work, so try and get something from the network/wrapper */
  180. stream = php_stream_open_wrapper(path, mode, options | STREAM_WILL_CAST, opened_path);
  181. if (stream) {
  182. php_socket_t fd;
  183. if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
  184. bz_file = BZ2_bzdopen((int)fd, mode);
  185. }
  186. }
  187. /* remove the file created by php_stream_open_wrapper(), it is not needed since BZ2 functions
  188. * failed.
  189. */
  190. if (opened_path && !bz_file && mode[0] == 'w') {
  191. VCWD_UNLINK(ZSTR_VAL(*opened_path));
  192. }
  193. }
  194. if (bz_file) {
  195. retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC);
  196. if (retstream) {
  197. return retstream;
  198. }
  199. BZ2_bzclose(bz_file);
  200. }
  201. if (stream) {
  202. php_stream_close(stream);
  203. }
  204. return NULL;
  205. }
  206. /* }}} */
  207. static const php_stream_wrapper_ops bzip2_stream_wops = {
  208. _php_stream_bz2open,
  209. NULL, /* close */
  210. NULL, /* fstat */
  211. NULL, /* stat */
  212. NULL, /* opendir */
  213. "BZip2",
  214. NULL, /* unlink */
  215. NULL, /* rename */
  216. NULL, /* mkdir */
  217. NULL, /* rmdir */
  218. NULL
  219. };
  220. static const php_stream_wrapper php_stream_bzip2_wrapper = {
  221. &bzip2_stream_wops,
  222. NULL,
  223. 0 /* is_url */
  224. };
  225. static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int);
  226. static PHP_MINIT_FUNCTION(bz2)
  227. {
  228. php_register_url_stream_wrapper("compress.bzip2", &php_stream_bzip2_wrapper);
  229. php_stream_filter_register_factory("bzip2.*", &php_bz2_filter_factory);
  230. return SUCCESS;
  231. }
  232. static PHP_MSHUTDOWN_FUNCTION(bz2)
  233. {
  234. php_unregister_url_stream_wrapper("compress.bzip2");
  235. php_stream_filter_unregister_factory("bzip2.*");
  236. return SUCCESS;
  237. }
  238. static PHP_MINFO_FUNCTION(bz2)
  239. {
  240. php_info_print_table_start();
  241. php_info_print_table_row(2, "BZip2 Support", "Enabled");
  242. php_info_print_table_row(2, "Stream Wrapper support", "compress.bzip2://");
  243. php_info_print_table_row(2, "Stream Filter support", "bzip2.decompress, bzip2.compress");
  244. php_info_print_table_row(2, "BZip2 Version", (char *) BZ2_bzlibVersion());
  245. php_info_print_table_end();
  246. }
  247. /* {{{ Reads up to length bytes from a BZip2 stream, or 1024 bytes if length is not specified */
  248. PHP_FUNCTION(bzread)
  249. {
  250. zval *bz;
  251. zend_long len = 1024;
  252. php_stream *stream;
  253. zend_string *data;
  254. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &bz, &len)) {
  255. RETURN_THROWS();
  256. }
  257. php_stream_from_zval(stream, bz);
  258. if (len < 0) {
  259. zend_argument_value_error(2, "must be greater than or equal to 0");
  260. RETURN_THROWS();
  261. }
  262. data = php_stream_read_to_str(stream, len);
  263. if (!data) {
  264. RETURN_FALSE;
  265. }
  266. RETURN_STR(data);
  267. }
  268. /* }}} */
  269. /* {{{ Opens a new BZip2 stream */
  270. PHP_FUNCTION(bzopen)
  271. {
  272. zval *file; /* The file to open */
  273. char *mode; /* The mode to open the stream with */
  274. size_t mode_len;
  275. BZFILE *bz; /* The compressed file stream */
  276. php_stream *stream = NULL;
  277. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &file, &mode, &mode_len) == FAILURE) {
  278. RETURN_THROWS();
  279. }
  280. if (mode_len != 1 || (mode[0] != 'r' && mode[0] != 'w')) {
  281. zend_argument_value_error(2, "must be either \"r\" or \"w\"");
  282. RETURN_THROWS();
  283. }
  284. /* If it's not a resource its a string containing the filename to open */
  285. if (Z_TYPE_P(file) == IS_STRING) {
  286. if (Z_STRLEN_P(file) == 0) {
  287. zend_argument_value_error(1, "cannot be empty");
  288. RETURN_THROWS();
  289. }
  290. if (CHECK_ZVAL_NULL_PATH(file)) {
  291. zend_argument_type_error(1, "must not contain null bytes");
  292. RETURN_THROWS();
  293. }
  294. stream = php_stream_bz2open(NULL, Z_STRVAL_P(file), mode, REPORT_ERRORS, NULL);
  295. } else if (Z_TYPE_P(file) == IS_RESOURCE) {
  296. /* If it is a resource, than its a stream resource */
  297. php_socket_t fd;
  298. size_t stream_mode_len;
  299. php_stream_from_zval(stream, file);
  300. stream_mode_len = strlen(stream->mode);
  301. if (stream_mode_len != 1 && !(stream_mode_len == 2 && memchr(stream->mode, 'b', 2))) {
  302. php_error_docref(NULL, E_WARNING, "Cannot use stream opened in mode '%s'", stream->mode);
  303. RETURN_FALSE;
  304. } else if (stream_mode_len == 1 && stream->mode[0] != 'r' && stream->mode[0] != 'w' && stream->mode[0] != 'a' && stream->mode[0] != 'x') {
  305. php_error_docref(NULL, E_WARNING, "Cannot use stream opened in mode '%s'", stream->mode);
  306. RETURN_FALSE;
  307. }
  308. switch(mode[0]) {
  309. case 'r':
  310. /* only "r" and "rb" are supported */
  311. if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])) {
  312. php_error_docref(NULL, E_WARNING, "Cannot read from a stream opened in write only mode");
  313. RETURN_FALSE;
  314. }
  315. break;
  316. case 'w':
  317. /* support only "w"(b), "a"(b), "x"(b) */
  318. if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])
  319. && stream->mode[0] != 'a' && !(stream_mode_len == 2 && stream->mode[1] != 'a')
  320. && stream->mode[0] != 'x' && !(stream_mode_len == 2 && stream->mode[1] != 'x')) {
  321. php_error_docref(NULL, E_WARNING, "cannot write to a stream opened in read only mode");
  322. RETURN_FALSE;
  323. }
  324. break;
  325. default:
  326. /* not reachable */
  327. break;
  328. }
  329. if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) {
  330. RETURN_FALSE;
  331. }
  332. bz = BZ2_bzdopen((int)fd, mode);
  333. stream = php_stream_bz2open_from_BZFILE(bz, mode, stream);
  334. } else {
  335. zend_argument_type_error(1, "must be of type string or file-resource, %s given", zend_zval_type_name(file));
  336. RETURN_THROWS();
  337. }
  338. if (stream) {
  339. php_stream_to_zval(stream, return_value);
  340. } else {
  341. RETURN_FALSE;
  342. }
  343. }
  344. /* }}} */
  345. /* {{{ Returns the error number */
  346. PHP_FUNCTION(bzerrno)
  347. {
  348. php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRNO);
  349. }
  350. /* }}} */
  351. /* {{{ Returns the error string */
  352. PHP_FUNCTION(bzerrstr)
  353. {
  354. php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRSTR);
  355. }
  356. /* }}} */
  357. /* {{{ Returns the error number and error string in an associative array */
  358. PHP_FUNCTION(bzerror)
  359. {
  360. php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRBOTH);
  361. }
  362. /* }}} */
  363. /* {{{ Compresses a string into BZip2 encoded data */
  364. PHP_FUNCTION(bzcompress)
  365. {
  366. char *source; /* Source data to compress */
  367. zend_long zblock_size = 0; /* Optional block size to use */
  368. zend_long zwork_factor = 0;/* Optional work factor to use */
  369. zend_string *dest = NULL; /* Destination to place the compressed data into */
  370. int error, /* Error Container */
  371. block_size = 4, /* Block size for compression algorithm */
  372. work_factor = 0, /* Work factor for compression algorithm */
  373. argc = ZEND_NUM_ARGS(); /* Argument count */
  374. size_t source_len; /* Length of the source data */
  375. unsigned int dest_len; /* Length of the destination buffer */
  376. if (zend_parse_parameters(argc, "s|ll", &source, &source_len, &zblock_size, &zwork_factor) == FAILURE) {
  377. RETURN_THROWS();
  378. }
  379. /* Assign them to easy to use variables, dest_len is initially the length of the data
  380. + .01 x length of data + 600 which is the largest size the results of the compression
  381. could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net
  382. for pointing this out). */
  383. dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
  384. /* Allocate the destination buffer */
  385. dest = zend_string_alloc(dest_len, 0);
  386. /* Handle the optional arguments */
  387. if (argc > 1) {
  388. block_size = zblock_size;
  389. }
  390. if (argc > 2) {
  391. work_factor = zwork_factor;
  392. }
  393. error = BZ2_bzBuffToBuffCompress(ZSTR_VAL(dest), &dest_len, source, source_len, block_size, 0, work_factor);
  394. if (error != BZ_OK) {
  395. zend_string_efree(dest);
  396. RETURN_LONG(error);
  397. } else {
  398. /* Copy the buffer, we have perhaps allocate a lot more than we need,
  399. so we erealloc() the buffer to the proper size */
  400. ZSTR_LEN(dest) = dest_len;
  401. ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
  402. RETURN_NEW_STR(dest);
  403. }
  404. }
  405. /* }}} */
  406. /* {{{ Decompresses BZip2 compressed data */
  407. PHP_FUNCTION(bzdecompress)
  408. {
  409. char *source;
  410. zend_string *dest;
  411. size_t source_len;
  412. int error;
  413. bool small = 0;
  414. #ifdef PHP_WIN32
  415. unsigned __int64 size = 0;
  416. #else
  417. unsigned long long size = 0;
  418. #endif
  419. bz_stream bzs;
  420. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &source, &source_len, &small)) {
  421. RETURN_THROWS();
  422. }
  423. bzs.bzalloc = NULL;
  424. bzs.bzfree = NULL;
  425. if (BZ2_bzDecompressInit(&bzs, 0, (int)small) != BZ_OK) {
  426. RETURN_FALSE;
  427. }
  428. bzs.next_in = source;
  429. bzs.avail_in = source_len;
  430. /* in most cases bz2 offers at least 2:1 compression, so we use that as our base */
  431. dest = zend_string_safe_alloc(source_len, 2, 1, 0);
  432. bzs.avail_out = source_len * 2;
  433. bzs.next_out = ZSTR_VAL(dest);
  434. while ((error = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) {
  435. /* compression is better then 2:1, need to allocate more memory */
  436. bzs.avail_out = source_len;
  437. size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
  438. #ifndef ZEND_ENABLE_ZVAL_LONG64
  439. if (size > SIZE_MAX) {
  440. /* no reason to continue if we're going to drop it anyway */
  441. break;
  442. }
  443. #endif
  444. dest = zend_string_safe_realloc(dest, 1, bzs.avail_out+1, (size_t) size, 0);
  445. bzs.next_out = ZSTR_VAL(dest) + size;
  446. }
  447. if (error == BZ_STREAM_END || error == BZ_OK) {
  448. size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
  449. #ifndef ZEND_ENABLE_ZVAL_LONG64
  450. if (UNEXPECTED(size > SIZE_MAX)) {
  451. php_error_docref(NULL, E_WARNING, "Decompressed size too big, max is %zd", SIZE_MAX);
  452. zend_string_efree(dest);
  453. RETVAL_LONG(BZ_MEM_ERROR);
  454. } else
  455. #endif
  456. {
  457. dest = zend_string_safe_realloc(dest, 1, (size_t)size, 1, 0);
  458. ZSTR_LEN(dest) = (size_t)size;
  459. ZSTR_VAL(dest)[(size_t)size] = '\0';
  460. RETVAL_STR(dest);
  461. }
  462. } else { /* real error */
  463. zend_string_efree(dest);
  464. RETVAL_LONG(error);
  465. }
  466. BZ2_bzDecompressEnd(&bzs);
  467. }
  468. /* }}} */
  469. /* {{{ php_bz2_error()
  470. The central error handling interface, does the work for bzerrno, bzerrstr and bzerror */
  471. static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt)
  472. {
  473. zval *bzp; /* BZip2 Resource Pointer */
  474. php_stream *stream;
  475. const char *errstr; /* Error string */
  476. int errnum; /* Error number */
  477. struct php_bz2_stream_data_t *self;
  478. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &bzp) == FAILURE) {
  479. RETURN_THROWS();
  480. }
  481. php_stream_from_zval(stream, bzp);
  482. if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) {
  483. zend_argument_type_error(1, "must be a bz2 stream");
  484. RETURN_THROWS();
  485. }
  486. self = (struct php_bz2_stream_data_t *) stream->abstract;
  487. /* Fetch the error information */
  488. errstr = BZ2_bzerror(self->bz_file, &errnum);
  489. /* Determine what to return */
  490. switch (opt) {
  491. case PHP_BZ_ERRNO:
  492. RETURN_LONG(errnum);
  493. break;
  494. case PHP_BZ_ERRSTR:
  495. RETURN_STRING((char*)errstr);
  496. break;
  497. case PHP_BZ_ERRBOTH:
  498. array_init(return_value);
  499. add_assoc_long (return_value, "errno", errnum);
  500. add_assoc_string(return_value, "errstr", (char*)errstr);
  501. break;
  502. }
  503. }
  504. /* }}} */
  505. #endif