cast.c 12 KB

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