zlib_fopen_wrapper.c 5.4 KB

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