cast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. | Authors: Wez Furlong <wez@thebrainroom.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #define _GNU_SOURCE
  17. #include "php.h"
  18. #include "php_globals.h"
  19. #include "php_network.h"
  20. #include "php_open_temporary_file.h"
  21. #include "ext/standard/file.h"
  22. #include <stddef.h>
  23. #include <fcntl.h>
  24. #include "php_streams_int.h"
  25. /* Under BSD, emulate fopencookie using funopen */
  26. #if defined(HAVE_FUNOPEN) && !defined(HAVE_FOPENCOOKIE)
  27. /* NetBSD 6.0+ uses off_t instead of fpos_t in funopen */
  28. # if defined(__NetBSD__) && (__NetBSD_Version__ >= 600000000)
  29. # define PHP_FPOS_T off_t
  30. # else
  31. # define PHP_FPOS_T fpos_t
  32. # endif
  33. typedef struct {
  34. int (*reader)(void *, char *, int);
  35. int (*writer)(void *, const char *, int);
  36. PHP_FPOS_T (*seeker)(void *, PHP_FPOS_T, int);
  37. int (*closer)(void *);
  38. } COOKIE_IO_FUNCTIONS_T;
  39. FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
  40. {
  41. return funopen(cookie, funcs->reader, funcs->writer, funcs->seeker, funcs->closer);
  42. }
  43. # define HAVE_FOPENCOOKIE 1
  44. # define PHP_EMULATE_FOPENCOOKIE 1
  45. # define PHP_STREAM_COOKIE_FUNCTIONS &stream_cookie_functions
  46. #elif defined(HAVE_FOPENCOOKIE)
  47. # define PHP_STREAM_COOKIE_FUNCTIONS stream_cookie_functions
  48. #endif
  49. /* {{{ STDIO with fopencookie */
  50. #if defined(PHP_EMULATE_FOPENCOOKIE)
  51. /* use our fopencookie emulation */
  52. static int stream_cookie_reader(void *cookie, char *buffer, int size)
  53. {
  54. int ret;
  55. ret = php_stream_read((php_stream*)cookie, buffer, size);
  56. return ret;
  57. }
  58. static int stream_cookie_writer(void *cookie, const char *buffer, int size)
  59. {
  60. return php_stream_write((php_stream *)cookie, (char *)buffer, size);
  61. }
  62. static PHP_FPOS_T stream_cookie_seeker(void *cookie, zend_off_t position, int whence)
  63. {
  64. return (PHP_FPOS_T)php_stream_seek((php_stream *)cookie, position, whence);
  65. }
  66. static int stream_cookie_closer(void *cookie)
  67. {
  68. php_stream *stream = (php_stream*)cookie;
  69. /* prevent recursion */
  70. stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
  71. return php_stream_free(stream,
  72. PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_RSRC_DTOR);
  73. }
  74. #elif defined(HAVE_FOPENCOOKIE)
  75. static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
  76. {
  77. ssize_t ret;
  78. ret = php_stream_read(((php_stream *)cookie), buffer, size);
  79. return ret;
  80. }
  81. static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size)
  82. {
  83. return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
  84. }
  85. # ifdef COOKIE_SEEKER_USES_OFF64_T
  86. static int stream_cookie_seeker(void *cookie, off64_t *position, int whence)
  87. {
  88. *position = php_stream_seek((php_stream *)cookie, (zend_off_t)*position, whence);
  89. if (*position == -1) {
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. # else
  95. static int stream_cookie_seeker(void *cookie, zend_off_t position, int whence)
  96. {
  97. return php_stream_seek((php_stream *)cookie, position, whence);
  98. }
  99. # endif
  100. static int stream_cookie_closer(void *cookie)
  101. {
  102. php_stream *stream = (php_stream*)cookie;
  103. /* prevent recursion */
  104. stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
  105. return php_stream_free(stream,
  106. PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_KEEP_RSRC | PHP_STREAM_FREE_RSRC_DTOR);
  107. }
  108. #endif /* elif defined(HAVE_FOPENCOOKIE) */
  109. #if HAVE_FOPENCOOKIE
  110. static COOKIE_IO_FUNCTIONS_T stream_cookie_functions =
  111. {
  112. stream_cookie_reader, stream_cookie_writer,
  113. stream_cookie_seeker, stream_cookie_closer
  114. };
  115. #else
  116. /* TODO: use socketpair() to emulate fopencookie, as suggested by Hartmut ? */
  117. #endif
  118. /* }}} */
  119. /* {{{ php_stream_mode_sanitize_fdopen_fopencookie
  120. * Result should have at least size 5, e.g. to write wbx+\0 */
  121. void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *result)
  122. {
  123. /* replace modes not supported by fdopen and fopencookie, but supported
  124. * by PHP's fread(), so that their calls won't fail */
  125. const char *cur_mode = stream->mode;
  126. int has_plus = 0,
  127. has_bin = 0,
  128. i,
  129. res_curs = 0;
  130. if (cur_mode[0] == 'r' || cur_mode[0] == 'w' || cur_mode[0] == 'a') {
  131. result[res_curs++] = cur_mode[0];
  132. } else {
  133. /* assume cur_mode[0] is 'c' or 'x'; substitute by 'w', which should not
  134. * truncate anything in fdopen/fopencookie */
  135. result[res_curs++] = 'w';
  136. /* x is allowed (at least by glibc & compat), but not as the 1st mode
  137. * as in PHP and in any case is (at best) ignored by fdopen and fopencookie */
  138. }
  139. /* assume current mode has at most length 4 (e.g. wbn+) */
  140. for (i = 1; i < 4 && cur_mode[i] != '\0'; i++) {
  141. if (cur_mode[i] == 'b') {
  142. has_bin = 1;
  143. } else if (cur_mode[i] == '+') {
  144. has_plus = 1;
  145. }
  146. /* ignore 'n', 't' or other stuff */
  147. }
  148. if (has_bin) {
  149. result[res_curs++] = 'b';
  150. }
  151. if (has_plus) {
  152. result[res_curs++] = '+';
  153. }
  154. result[res_curs] = '\0';
  155. }
  156. /* }}} */
  157. /* {{{ php_stream_cast */
  158. PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show_err)
  159. {
  160. int flags = castas & PHP_STREAM_CAST_MASK;
  161. castas &= ~PHP_STREAM_CAST_MASK;
  162. /* synchronize our buffer (if possible) */
  163. if (ret && castas != PHP_STREAM_AS_FD_FOR_SELECT) {
  164. php_stream_flush(stream);
  165. if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
  166. zend_off_t dummy;
  167. stream->ops->seek(stream, stream->position, SEEK_SET, &dummy);
  168. stream->readpos = stream->writepos = 0;
  169. }
  170. }
  171. /* filtered streams can only be cast as stdio, and only when fopencookie is present */
  172. if (castas == PHP_STREAM_AS_STDIO) {
  173. if (stream->stdiocast) {
  174. if (ret) {
  175. *(FILE**)ret = stream->stdiocast;
  176. }
  177. goto exit_success;
  178. }
  179. /* if the stream is a stdio stream let's give it a chance to respond
  180. * first, to avoid doubling up the layers of stdio with an fopencookie */
  181. if (php_stream_is(stream, PHP_STREAM_IS_STDIO) &&
  182. stream->ops->cast &&
  183. !php_stream_is_filtered(stream) &&
  184. stream->ops->cast(stream, castas, ret) == SUCCESS
  185. ) {
  186. goto exit_success;
  187. }
  188. #if HAVE_FOPENCOOKIE
  189. /* if just checking, say yes we can be a FILE*, but don't actually create it yet */
  190. if (ret == NULL) {
  191. goto exit_success;
  192. }
  193. {
  194. char fixed_mode[5];
  195. php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
  196. *(FILE**)ret = fopencookie(stream, fixed_mode, PHP_STREAM_COOKIE_FUNCTIONS);
  197. }
  198. if (*ret != NULL) {
  199. zend_off_t pos;
  200. stream->fclose_stdiocast = PHP_STREAM_FCLOSE_FOPENCOOKIE;
  201. /* If the stream position is not at the start, we need to force
  202. * the stdio layer to believe it's real location. */
  203. pos = php_stream_tell(stream);
  204. if (pos > 0) {
  205. zend_fseek(*ret, pos, SEEK_SET);
  206. }
  207. goto exit_success;
  208. }
  209. /* must be either:
  210. a) programmer error
  211. b) no memory
  212. -> lets bail
  213. */
  214. php_error_docref(NULL, E_ERROR, "fopencookie failed");
  215. return FAILURE;
  216. #endif
  217. if (!php_stream_is_filtered(stream) && stream->ops->cast && stream->ops->cast(stream, castas, NULL) == SUCCESS) {
  218. if (FAILURE == stream->ops->cast(stream, castas, ret)) {
  219. return FAILURE;
  220. }
  221. goto exit_success;
  222. } else if (flags & PHP_STREAM_CAST_TRY_HARD) {
  223. php_stream *newstream;
  224. newstream = php_stream_fopen_tmpfile();
  225. if (newstream) {
  226. int retcopy = php_stream_copy_to_stream_ex(stream, newstream, PHP_STREAM_COPY_ALL, NULL);
  227. if (retcopy != SUCCESS) {
  228. php_stream_close(newstream);
  229. } else {
  230. int retcast = php_stream_cast(newstream, castas | flags, (void **)ret, show_err);
  231. if (retcast == SUCCESS) {
  232. rewind(*(FILE**)ret);
  233. }
  234. /* do some specialized cleanup */
  235. if ((flags & PHP_STREAM_CAST_RELEASE)) {
  236. php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
  237. }
  238. /* TODO: we probably should be setting .stdiocast and .fclose_stdiocast or
  239. * we may be leaking the FILE*. Needs investigation, though. */
  240. return retcast;
  241. }
  242. }
  243. }
  244. }
  245. if (php_stream_is_filtered(stream)) {
  246. if (show_err) {
  247. php_error_docref(NULL, E_WARNING, "Cannot cast a filtered stream on this system");
  248. }
  249. return FAILURE;
  250. } else if (stream->ops->cast && stream->ops->cast(stream, castas, ret) == SUCCESS) {
  251. goto exit_success;
  252. }
  253. if (show_err) {
  254. /* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
  255. static const char *cast_names[4] = {
  256. "STDIO FILE*",
  257. "File Descriptor",
  258. "Socket Descriptor",
  259. "select()able descriptor"
  260. };
  261. php_error_docref(NULL, E_WARNING, "Cannot represent a stream of type %s as a %s", stream->ops->label, cast_names[castas]);
  262. }
  263. return FAILURE;
  264. exit_success:
  265. if ((stream->writepos - stream->readpos) > 0 &&
  266. stream->fclose_stdiocast != PHP_STREAM_FCLOSE_FOPENCOOKIE &&
  267. (flags & PHP_STREAM_CAST_INTERNAL) == 0
  268. ) {
  269. /* the data we have buffered will be lost to the third party library that
  270. * will be accessing the stream. Emit a warning so that the end-user will
  271. * know that they should try something else */
  272. php_error_docref(NULL, E_WARNING, ZEND_LONG_FMT " bytes of buffered data lost during stream conversion!", (zend_long)(stream->writepos - stream->readpos));
  273. }
  274. if (castas == PHP_STREAM_AS_STDIO && ret) {
  275. stream->stdiocast = *(FILE**)ret;
  276. }
  277. if (flags & PHP_STREAM_CAST_RELEASE) {
  278. php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
  279. }
  280. return SUCCESS;
  281. }
  282. /* }}} */
  283. /* {{{ php_stream_open_wrapper_as_file */
  284. PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, zend_string **opened_path STREAMS_DC)
  285. {
  286. FILE *fp = NULL;
  287. php_stream *stream = NULL;
  288. stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, opened_path);
  289. if (stream == NULL) {
  290. return NULL;
  291. }
  292. if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) {
  293. php_stream_close(stream);
  294. if (opened_path && *opened_path) {
  295. zend_string_release_ex(*opened_path, 0);
  296. }
  297. return NULL;
  298. }
  299. return fp;
  300. }
  301. /* }}} */
  302. /* {{{ php_stream_make_seekable */
  303. PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC)
  304. {
  305. if (newstream == NULL) {
  306. return PHP_STREAM_FAILED;
  307. }
  308. *newstream = NULL;
  309. if (((flags & PHP_STREAM_FORCE_CONVERSION) == 0) && origstream->ops->seek != NULL) {
  310. *newstream = origstream;
  311. return PHP_STREAM_UNCHANGED;
  312. }
  313. /* Use a tmpfile and copy the old streams contents into it */
  314. if (flags & PHP_STREAM_PREFER_STDIO) {
  315. *newstream = php_stream_fopen_tmpfile();
  316. } else {
  317. *newstream = php_stream_temp_new();
  318. }
  319. if (*newstream == NULL) {
  320. return PHP_STREAM_FAILED;
  321. }
  322. #if ZEND_DEBUG
  323. (*newstream)->open_filename = origstream->open_filename;
  324. (*newstream)->open_lineno = origstream->open_lineno;
  325. #endif
  326. if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL, NULL) != SUCCESS) {
  327. php_stream_close(*newstream);
  328. *newstream = NULL;
  329. return PHP_STREAM_CRITICAL;
  330. }
  331. php_stream_close(origstream);
  332. php_stream_seek(*newstream, 0, SEEK_SET);
  333. return PHP_STREAM_RELEASED;
  334. }
  335. /* }}} */