PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/bz2/bz2_filter.c

http://php52-backports.googlecode.com/
C | 416 lines | 304 code | 64 blank | 48 comment | 80 complexity | 8e8ad86eb110f540ab00e6955fc403c7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2010 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Sara Golemon (pollita@php.net) |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: bz2_filter.c 293036 2010-01-03 09:23:27Z sebastian $ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_bz2.h"
  24. /* {{{ data structure */
  25. typedef struct _php_bz2_filter_data {
  26. int persistent;
  27. bz_stream strm;
  28. char *inbuf;
  29. size_t inbuf_len;
  30. char *outbuf;
  31. size_t outbuf_len;
  32. zend_bool finished;
  33. } php_bz2_filter_data;
  34. /* }}} */
  35. /* {{{ Memory management wrappers */
  36. static void *php_bz2_alloc(void *opaque, int items, int size)
  37. {
  38. return (void *)safe_pemalloc(items, size, 0, ((php_bz2_filter_data*)opaque)->persistent);
  39. }
  40. static void php_bz2_free(void *opaque, void *address)
  41. {
  42. pefree((void *)address, ((php_bz2_filter_data*)opaque)->persistent);
  43. }
  44. /* }}} */
  45. /* {{{ bzip2.decompress filter implementation */
  46. static php_stream_filter_status_t php_bz2_decompress_filter(
  47. php_stream *stream,
  48. php_stream_filter *thisfilter,
  49. php_stream_bucket_brigade *buckets_in,
  50. php_stream_bucket_brigade *buckets_out,
  51. size_t *bytes_consumed,
  52. int flags
  53. TSRMLS_DC)
  54. {
  55. php_bz2_filter_data *data;
  56. php_stream_bucket *bucket;
  57. size_t consumed = 0;
  58. int status;
  59. php_stream_filter_status_t exit_status = PSFS_FEED_ME;
  60. bz_stream *streamp;
  61. if (!thisfilter || !thisfilter->abstract) {
  62. /* Should never happen */
  63. return PSFS_ERR_FATAL;
  64. }
  65. data = (php_bz2_filter_data *)(thisfilter->abstract);
  66. streamp = &(data->strm);
  67. while (buckets_in->head) {
  68. size_t bin = 0, desired;
  69. bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
  70. while (bin < bucket->buflen) {
  71. if (data->finished) {
  72. consumed += bucket->buflen;
  73. break;
  74. }
  75. desired = bucket->buflen - bin;
  76. if (desired > data->inbuf_len) {
  77. desired = data->inbuf_len;
  78. }
  79. memcpy(data->strm.next_in, bucket->buf + bin, desired);
  80. data->strm.avail_in = desired;
  81. status = BZ2_bzDecompress(&(data->strm));
  82. if (status == BZ_STREAM_END) {
  83. BZ2_bzDecompressEnd(&(data->strm));
  84. data->finished = '\1';
  85. } else if (status != BZ_OK) {
  86. /* Something bad happened */
  87. php_stream_bucket_delref(bucket TSRMLS_CC);
  88. return PSFS_ERR_FATAL;
  89. }
  90. desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
  91. data->strm.next_in = data->inbuf;
  92. data->strm.avail_in = 0;
  93. consumed += desired;
  94. bin += desired;
  95. if (data->strm.avail_out < data->outbuf_len) {
  96. php_stream_bucket *out_bucket;
  97. size_t bucketlen = data->outbuf_len - data->strm.avail_out;
  98. out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
  99. php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
  100. data->strm.avail_out = data->outbuf_len;
  101. data->strm.next_out = data->outbuf;
  102. exit_status = PSFS_PASS_ON;
  103. } else if (status == BZ_STREAM_END && data->strm.avail_out >= data->outbuf_len) {
  104. /* no more data to decompress, and nothing was spat out */
  105. php_stream_bucket_delref(bucket TSRMLS_CC);
  106. return PSFS_PASS_ON;
  107. }
  108. }
  109. php_stream_bucket_delref(bucket TSRMLS_CC);
  110. }
  111. if (!data->finished && (flags & PSFS_FLAG_FLUSH_CLOSE)) {
  112. /* Spit it out! */
  113. status = BZ_OK;
  114. while (status == BZ_OK) {
  115. status = BZ2_bzDecompress(&(data->strm));
  116. if (data->strm.avail_out < data->outbuf_len) {
  117. size_t bucketlen = data->outbuf_len - data->strm.avail_out;
  118. bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
  119. php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
  120. data->strm.avail_out = data->outbuf_len;
  121. data->strm.next_out = data->outbuf;
  122. exit_status = PSFS_PASS_ON;
  123. } else if (status == BZ_OK) {
  124. break;
  125. }
  126. }
  127. }
  128. if (bytes_consumed) {
  129. *bytes_consumed = consumed;
  130. }
  131. return exit_status;
  132. }
  133. static void php_bz2_decompress_dtor(php_stream_filter *thisfilter TSRMLS_DC)
  134. {
  135. if (thisfilter && thisfilter->abstract) {
  136. php_bz2_filter_data *data = thisfilter->abstract;
  137. if (!data->finished) {
  138. BZ2_bzDecompressEnd(&(data->strm));
  139. }
  140. pefree(data->inbuf, data->persistent);
  141. pefree(data->outbuf, data->persistent);
  142. pefree(data, data->persistent);
  143. }
  144. }
  145. static php_stream_filter_ops php_bz2_decompress_ops = {
  146. php_bz2_decompress_filter,
  147. php_bz2_decompress_dtor,
  148. "bzip2.decompress"
  149. };
  150. /* }}} */
  151. /* {{{ bzip2.compress filter implementation */
  152. static php_stream_filter_status_t php_bz2_compress_filter(
  153. php_stream *stream,
  154. php_stream_filter *thisfilter,
  155. php_stream_bucket_brigade *buckets_in,
  156. php_stream_bucket_brigade *buckets_out,
  157. size_t *bytes_consumed,
  158. int flags
  159. TSRMLS_DC)
  160. {
  161. php_bz2_filter_data *data;
  162. php_stream_bucket *bucket;
  163. size_t consumed = 0;
  164. int status;
  165. php_stream_filter_status_t exit_status = PSFS_FEED_ME;
  166. bz_stream *streamp;
  167. if (!thisfilter || !thisfilter->abstract) {
  168. /* Should never happen */
  169. return PSFS_ERR_FATAL;
  170. }
  171. data = (php_bz2_filter_data *)(thisfilter->abstract);
  172. streamp = &(data->strm);
  173. while (buckets_in->head) {
  174. size_t bin = 0, desired;
  175. bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
  176. while (bin < bucket->buflen) {
  177. desired = bucket->buflen - bin;
  178. if (desired > data->inbuf_len) {
  179. desired = data->inbuf_len;
  180. }
  181. memcpy(data->strm.next_in, bucket->buf + bin, desired);
  182. data->strm.avail_in = desired;
  183. status = BZ2_bzCompress(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH : (flags & PSFS_FLAG_FLUSH_INC ? BZ_FLUSH : BZ_RUN));
  184. if (status != BZ_RUN_OK && status != BZ_FLUSH_OK && status != BZ_FINISH_OK) {
  185. /* Something bad happened */
  186. php_stream_bucket_delref(bucket TSRMLS_CC);
  187. return PSFS_ERR_FATAL;
  188. }
  189. desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
  190. data->strm.next_in = data->inbuf;
  191. data->strm.avail_in = 0;
  192. consumed += desired;
  193. bin += desired;
  194. if (data->strm.avail_out < data->outbuf_len) {
  195. php_stream_bucket *out_bucket;
  196. size_t bucketlen = data->outbuf_len - data->strm.avail_out;
  197. out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
  198. php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
  199. data->strm.avail_out = data->outbuf_len;
  200. data->strm.next_out = data->outbuf;
  201. exit_status = PSFS_PASS_ON;
  202. }
  203. }
  204. php_stream_bucket_delref(bucket TSRMLS_CC);
  205. }
  206. if (flags & PSFS_FLAG_FLUSH_CLOSE) {
  207. /* Spit it out! */
  208. status = BZ_FINISH_OK;
  209. while (status == BZ_FINISH_OK) {
  210. status = BZ2_bzCompress(&(data->strm), BZ_FINISH);
  211. if (data->strm.avail_out < data->outbuf_len) {
  212. size_t bucketlen = data->outbuf_len - data->strm.avail_out;
  213. bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
  214. php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
  215. data->strm.avail_out = data->outbuf_len;
  216. data->strm.next_out = data->outbuf;
  217. exit_status = PSFS_PASS_ON;
  218. }
  219. }
  220. }
  221. if (bytes_consumed) {
  222. *bytes_consumed = consumed;
  223. }
  224. return exit_status;
  225. }
  226. static void php_bz2_compress_dtor(php_stream_filter *thisfilter TSRMLS_DC)
  227. {
  228. if (thisfilter && thisfilter->abstract) {
  229. php_bz2_filter_data *data = thisfilter->abstract;
  230. BZ2_bzCompressEnd(&(data->strm));
  231. pefree(data->inbuf, data->persistent);
  232. pefree(data->outbuf, data->persistent);
  233. pefree(data, data->persistent);
  234. }
  235. }
  236. static php_stream_filter_ops php_bz2_compress_ops = {
  237. php_bz2_compress_filter,
  238. php_bz2_compress_dtor,
  239. "bzip2.compress"
  240. };
  241. /* }}} */
  242. /* {{{ bzip2.* common factory */
  243. static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *filterparams, int persistent TSRMLS_DC)
  244. {
  245. php_stream_filter_ops *fops = NULL;
  246. php_bz2_filter_data *data;
  247. int status;
  248. /* Create this filter */
  249. data = pecalloc(1, sizeof(php_bz2_filter_data), persistent);
  250. if (!data) {
  251. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zu bytes.", sizeof(php_bz2_filter_data));
  252. return NULL;
  253. }
  254. /* Circular reference */
  255. data->strm.opaque = (void *) data;
  256. data->strm.bzalloc = php_bz2_alloc;
  257. data->strm.bzfree = php_bz2_free;
  258. data->persistent = persistent;
  259. data->strm.avail_out = data->outbuf_len = data->inbuf_len = 2048;
  260. data->strm.next_in = data->inbuf = (char *) pemalloc(data->inbuf_len, persistent);
  261. if (!data->inbuf) {
  262. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zu bytes.", data->inbuf_len);
  263. pefree(data, persistent);
  264. return NULL;
  265. }
  266. data->strm.avail_in = 0;
  267. data->strm.next_out = data->outbuf = (char *) pemalloc(data->outbuf_len, persistent);
  268. if (!data->outbuf) {
  269. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zu bytes.", data->outbuf_len);
  270. pefree(data->inbuf, persistent);
  271. pefree(data, persistent);
  272. return NULL;
  273. }
  274. if (strcasecmp(filtername, "bzip2.decompress") == 0) {
  275. int smallFootprint = 0;
  276. if (filterparams) {
  277. zval **tmpzval = NULL;
  278. if (Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) {
  279. zend_hash_find(HASH_OF(filterparams), "small", sizeof("small"), (void **) &tmpzval);
  280. } else {
  281. tmpzval = &filterparams;
  282. }
  283. if (tmpzval) {
  284. zval tmp, *tmp2;
  285. tmp = **tmpzval;
  286. zval_copy_ctor(&tmp);
  287. tmp2 = &tmp;
  288. convert_to_boolean_ex(&tmp2);
  289. smallFootprint = Z_LVAL(tmp);
  290. }
  291. }
  292. status = BZ2_bzDecompressInit(&(data->strm), 0, smallFootprint);
  293. data->finished = '\0';
  294. fops = &php_bz2_decompress_ops;
  295. } else if (strcasecmp(filtername, "bzip2.compress") == 0) {
  296. int blockSize100k = PHP_BZ2_FILTER_DEFAULT_BLOCKSIZE;
  297. int workFactor = PHP_BZ2_FILTER_DEFAULT_WORKFACTOR;
  298. if (filterparams) {
  299. zval **tmpzval;
  300. if (Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) {
  301. if (zend_hash_find(HASH_OF(filterparams), "blocks", sizeof("blocks"), (void**) &tmpzval) == SUCCESS) {
  302. /* How much memory to allocate (1 - 9) x 100kb */
  303. zval tmp;
  304. tmp = **tmpzval;
  305. zval_copy_ctor(&tmp);
  306. convert_to_long(&tmp);
  307. if (Z_LVAL(tmp) < 1 || Z_LVAL(tmp) > 9) {
  308. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter given for number of blocks to allocate. (%ld)", Z_LVAL_PP(tmpzval));
  309. } else {
  310. blockSize100k = Z_LVAL(tmp);
  311. }
  312. }
  313. if (zend_hash_find(HASH_OF(filterparams), "work", sizeof("work"), (void**) &tmpzval) == SUCCESS) {
  314. /* Work Factor (0 - 250) */
  315. zval tmp;
  316. tmp = **tmpzval;
  317. zval_copy_ctor(&tmp);
  318. convert_to_long(&tmp);
  319. if (Z_LVAL(tmp) < 0 || Z_LVAL(tmp) > 250) {
  320. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter given for work factor. (%ld)", Z_LVAL(tmp));
  321. } else {
  322. workFactor = Z_LVAL(tmp);
  323. }
  324. }
  325. }
  326. }
  327. status = BZ2_bzCompressInit(&(data->strm), blockSize100k, 0, workFactor);
  328. fops = &php_bz2_compress_ops;
  329. } else {
  330. status = BZ_DATA_ERROR;
  331. }
  332. if (status != BZ_OK) {
  333. /* Unspecified (probably strm) error, let stream-filter error do its own whining */
  334. pefree(data->strm.next_in, persistent);
  335. pefree(data->strm.next_out, persistent);
  336. pefree(data, persistent);
  337. return NULL;
  338. }
  339. return php_stream_filter_alloc(fops, data, persistent);
  340. }
  341. php_stream_filter_factory php_bz2_filter_factory = {
  342. php_bz2_filter_create
  343. };
  344. /* }}} */
  345. /*
  346. * Local variables:
  347. * tab-width: 4
  348. * c-basic-offset: 4
  349. * End:
  350. * vim600: sw=4 ts=4 fdm=marker
  351. * vim<600: sw=4 ts=4
  352. */