zlib_fopen_wrapper.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: Wez Furlong <wez@thebrainroom.com>, based on work by: |
  14. | Hartmut Holzgraefe <hholzgra@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #define _GNU_SOURCE
  18. #include "php.h"
  19. #include "php_zlib.h"
  20. #include "fopen_wrappers.h"
  21. #include "main/php_network.h"
  22. struct php_gz_stream_data_t {
  23. gzFile gz_file;
  24. php_stream *stream;
  25. };
  26. static ssize_t php_gziop_read(php_stream *stream, char *buf, size_t count)
  27. {
  28. struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
  29. int read;
  30. /* XXX this needs to be looped for the case count > UINT_MAX */
  31. read = gzread(self->gz_file, buf, count);
  32. if (gzeof(self->gz_file)) {
  33. stream->eof = 1;
  34. }
  35. return read;
  36. }
  37. static ssize_t php_gziop_write(php_stream *stream, const char *buf, size_t count)
  38. {
  39. struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
  40. /* XXX this needs to be looped for the case count > UINT_MAX */
  41. return gzwrite(self->gz_file, (char *) buf, count);
  42. }
  43. static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
  44. {
  45. struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
  46. assert(self != NULL);
  47. if (whence == SEEK_END) {
  48. php_error_docref(NULL, E_WARNING, "SEEK_END is not supported");
  49. return -1;
  50. }
  51. *newoffs = gzseek(self->gz_file, offset, whence);
  52. return (*newoffs < 0) ? -1 : 0;
  53. }
  54. static int php_gziop_close(php_stream *stream, int close_handle)
  55. {
  56. struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
  57. int ret = EOF;
  58. if (close_handle) {
  59. if (self->gz_file) {
  60. ret = gzclose(self->gz_file);
  61. self->gz_file = NULL;
  62. }
  63. if (self->stream) {
  64. php_stream_close(self->stream);
  65. self->stream = NULL;
  66. }
  67. }
  68. efree(self);
  69. return ret;
  70. }
  71. static int php_gziop_flush(php_stream *stream)
  72. {
  73. struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
  74. return gzflush(self->gz_file, Z_SYNC_FLUSH);
  75. }
  76. const php_stream_ops php_stream_gzio_ops = {
  77. php_gziop_write, php_gziop_read,
  78. php_gziop_close, php_gziop_flush,
  79. "ZLIB",
  80. php_gziop_seek,
  81. NULL, /* cast */
  82. NULL, /* stat */
  83. NULL /* set_option */
  84. };
  85. php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
  86. zend_string **opened_path, php_stream_context *context STREAMS_DC)
  87. {
  88. struct php_gz_stream_data_t *self;
  89. php_stream *stream = NULL, *innerstream = NULL;
  90. /* sanity check the stream: it can be either read-only or write-only */
  91. if (strchr(mode, '+')) {
  92. if (options & REPORT_ERRORS) {
  93. php_error_docref(NULL, E_WARNING, "Cannot open a zlib stream for reading and writing at the same time!");
  94. }
  95. return NULL;
  96. }
  97. if (strncasecmp("compress.zlib://", path, 16) == 0) {
  98. path += 16;
  99. } else if (strncasecmp("zlib:", path, 5) == 0) {
  100. path += 5;
  101. }
  102. innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
  103. if (innerstream) {
  104. php_socket_t fd;
  105. if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
  106. self = emalloc(sizeof(*self));
  107. self->stream = innerstream;
  108. self->gz_file = gzdopen(dup(fd), mode);
  109. if (self->gz_file) {
  110. zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
  111. if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
  112. php_error(E_WARNING, "failed setting compression level");
  113. }
  114. stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
  115. if (stream) {
  116. stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
  117. return stream;
  118. }
  119. gzclose(self->gz_file);
  120. }
  121. efree(self);
  122. if (options & REPORT_ERRORS) {
  123. php_error_docref(NULL, E_WARNING, "gzopen failed");
  124. }
  125. }
  126. php_stream_close(innerstream);
  127. }
  128. return NULL;
  129. }
  130. static const php_stream_wrapper_ops gzip_stream_wops = {
  131. php_stream_gzopen,
  132. NULL, /* close */
  133. NULL, /* stat */
  134. NULL, /* stat_url */
  135. NULL, /* opendir */
  136. "ZLIB",
  137. NULL, /* unlink */
  138. NULL, /* rename */
  139. NULL, /* mkdir */
  140. NULL, /* rmdir */
  141. NULL
  142. };
  143. const php_stream_wrapper php_stream_gzip_wrapper = {
  144. &gzip_stream_wops,
  145. NULL,
  146. 0, /* is_url */
  147. };