zlib_fopen_wrapper.c 5.1 KB

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