memory.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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: Marcus Boerger <helly@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #define _GNU_SOURCE
  17. #include "php.h"
  18. #include "ext/standard/base64.h"
  19. PHPAPI size_t php_url_decode(char *str, size_t len);
  20. /* Memory streams use a dynamic memory buffer to emulate a stream.
  21. * You can use php_stream_memory_open to create a readonly stream
  22. * from an existing memory buffer.
  23. */
  24. /* Temp streams are streams that uses memory streams as long their
  25. * size is less than a given memory amount. When a write operation
  26. * exceeds that limit the content is written to a temporary file.
  27. */
  28. /* {{{ ------- MEMORY stream implementation -------*/
  29. typedef struct {
  30. zend_string *data;
  31. size_t fpos;
  32. int mode;
  33. } php_stream_memory_data;
  34. /* {{{ */
  35. static ssize_t php_stream_memory_write(php_stream *stream, const char *buf, size_t count)
  36. {
  37. php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
  38. assert(ms != NULL);
  39. if (ms->mode & TEMP_STREAM_READONLY) {
  40. return (ssize_t) -1;
  41. } else if (ms->mode & TEMP_STREAM_APPEND) {
  42. ms->fpos = ZSTR_LEN(ms->data);
  43. }
  44. if (ms->fpos + count > ZSTR_LEN(ms->data)) {
  45. ms->data = zend_string_realloc(ms->data, ms->fpos + count, 0);
  46. } else {
  47. ms->data = zend_string_separate(ms->data, 0);
  48. }
  49. if (count) {
  50. ZEND_ASSERT(buf != NULL);
  51. memcpy(ZSTR_VAL(ms->data) + ms->fpos, (char*) buf, count);
  52. ms->fpos += count;
  53. }
  54. return count;
  55. }
  56. /* }}} */
  57. /* {{{ */
  58. static ssize_t php_stream_memory_read(php_stream *stream, char *buf, size_t count)
  59. {
  60. php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
  61. assert(ms != NULL);
  62. if (ms->fpos == ZSTR_LEN(ms->data)) {
  63. stream->eof = 1;
  64. count = 0;
  65. } else {
  66. if (ms->fpos + count > ZSTR_LEN(ms->data)) {
  67. count = ZSTR_LEN(ms->data) - ms->fpos;
  68. }
  69. if (count) {
  70. ZEND_ASSERT(buf != NULL);
  71. memcpy(buf, ZSTR_VAL(ms->data) + ms->fpos, count);
  72. ms->fpos += count;
  73. }
  74. }
  75. return count;
  76. }
  77. /* }}} */
  78. /* {{{ */
  79. static int php_stream_memory_close(php_stream *stream, int close_handle)
  80. {
  81. php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
  82. ZEND_ASSERT(ms != NULL);
  83. zend_string_release(ms->data);
  84. efree(ms);
  85. return 0;
  86. }
  87. /* }}} */
  88. /* {{{ */
  89. static int php_stream_memory_flush(php_stream *stream)
  90. {
  91. /* nothing to do here */
  92. return 0;
  93. }
  94. /* }}} */
  95. /* {{{ */
  96. static int php_stream_memory_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
  97. {
  98. php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
  99. assert(ms != NULL);
  100. switch(whence) {
  101. case SEEK_CUR:
  102. if (offset < 0) {
  103. if (ms->fpos < (size_t)(-offset)) {
  104. ms->fpos = 0;
  105. *newoffs = -1;
  106. return -1;
  107. } else {
  108. ms->fpos = ms->fpos + offset;
  109. *newoffs = ms->fpos;
  110. stream->eof = 0;
  111. return 0;
  112. }
  113. } else {
  114. if (ms->fpos + (size_t)(offset) > ZSTR_LEN(ms->data)) {
  115. ms->fpos = ZSTR_LEN(ms->data);
  116. *newoffs = -1;
  117. return -1;
  118. } else {
  119. ms->fpos = ms->fpos + offset;
  120. *newoffs = ms->fpos;
  121. stream->eof = 0;
  122. return 0;
  123. }
  124. }
  125. case SEEK_SET:
  126. if (ZSTR_LEN(ms->data) < (size_t)(offset)) {
  127. ms->fpos = ZSTR_LEN(ms->data);
  128. *newoffs = -1;
  129. return -1;
  130. } else {
  131. ms->fpos = offset;
  132. *newoffs = ms->fpos;
  133. stream->eof = 0;
  134. return 0;
  135. }
  136. case SEEK_END:
  137. if (offset > 0) {
  138. ms->fpos = ZSTR_LEN(ms->data);
  139. *newoffs = -1;
  140. return -1;
  141. } else if (ZSTR_LEN(ms->data) < (size_t)(-offset)) {
  142. ms->fpos = 0;
  143. *newoffs = -1;
  144. return -1;
  145. } else {
  146. ms->fpos = ZSTR_LEN(ms->data) + offset;
  147. *newoffs = ms->fpos;
  148. stream->eof = 0;
  149. return 0;
  150. }
  151. default:
  152. *newoffs = ms->fpos;
  153. return -1;
  154. }
  155. }
  156. /* }}} */
  157. /* {{{ */
  158. static int php_stream_memory_cast(php_stream *stream, int castas, void **ret)
  159. {
  160. return FAILURE;
  161. }
  162. /* }}} */
  163. static int php_stream_memory_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
  164. {
  165. time_t timestamp = 0;
  166. php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
  167. assert(ms != NULL);
  168. memset(ssb, 0, sizeof(php_stream_statbuf));
  169. /* read-only across the board */
  170. ssb->sb.st_mode = ms->mode & TEMP_STREAM_READONLY ? 0444 : 0666;
  171. ssb->sb.st_size = ZSTR_LEN(ms->data);
  172. ssb->sb.st_mode |= S_IFREG; /* regular file */
  173. ssb->sb.st_mtime = timestamp;
  174. ssb->sb.st_atime = timestamp;
  175. ssb->sb.st_ctime = timestamp;
  176. ssb->sb.st_nlink = 1;
  177. ssb->sb.st_rdev = -1;
  178. /* this is only for APC, so use /dev/null device - no chance of conflict there! */
  179. ssb->sb.st_dev = 0xC;
  180. /* generate unique inode number for alias/filename, so no phars will conflict */
  181. ssb->sb.st_ino = 0;
  182. #ifndef PHP_WIN32
  183. ssb->sb.st_blksize = -1;
  184. ssb->sb.st_blocks = -1;
  185. #endif
  186. return 0;
  187. }
  188. /* }}} */
  189. static int php_stream_memory_set_option(php_stream *stream, int option, int value, void *ptrparam) /* {{{ */
  190. {
  191. php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
  192. size_t newsize;
  193. switch(option) {
  194. case PHP_STREAM_OPTION_TRUNCATE_API:
  195. switch (value) {
  196. case PHP_STREAM_TRUNCATE_SUPPORTED:
  197. return PHP_STREAM_OPTION_RETURN_OK;
  198. case PHP_STREAM_TRUNCATE_SET_SIZE:
  199. if (ms->mode & TEMP_STREAM_READONLY) {
  200. return PHP_STREAM_OPTION_RETURN_ERR;
  201. }
  202. newsize = *(size_t*)ptrparam;
  203. if (newsize <= ZSTR_LEN(ms->data)) {
  204. ms->data = zend_string_truncate(ms->data, newsize, 0);
  205. if (newsize < ms->fpos) {
  206. ms->fpos = newsize;
  207. }
  208. } else {
  209. size_t old_size = ZSTR_LEN(ms->data);
  210. ms->data = zend_string_realloc(ms->data, newsize, 0);
  211. memset(ZSTR_VAL(ms->data) + old_size, 0, newsize - old_size);
  212. }
  213. return PHP_STREAM_OPTION_RETURN_OK;
  214. }
  215. }
  216. return PHP_STREAM_OPTION_RETURN_NOTIMPL;
  217. }
  218. /* }}} */
  219. PHPAPI const php_stream_ops php_stream_memory_ops = {
  220. php_stream_memory_write, php_stream_memory_read,
  221. php_stream_memory_close, php_stream_memory_flush,
  222. "MEMORY",
  223. php_stream_memory_seek,
  224. php_stream_memory_cast,
  225. php_stream_memory_stat,
  226. php_stream_memory_set_option
  227. };
  228. /* {{{ */
  229. PHPAPI int php_stream_mode_from_str(const char *mode)
  230. {
  231. if (strpbrk(mode, "a")) {
  232. return TEMP_STREAM_APPEND;
  233. } else if (strpbrk(mode, "w+")) {
  234. return TEMP_STREAM_DEFAULT;
  235. }
  236. return TEMP_STREAM_READONLY;
  237. }
  238. /* }}} */
  239. /* {{{ */
  240. PHPAPI const char *_php_stream_mode_to_str(int mode)
  241. {
  242. if (mode == TEMP_STREAM_READONLY) {
  243. return "rb";
  244. } else if (mode == TEMP_STREAM_APPEND) {
  245. return "a+b";
  246. }
  247. return "w+b";
  248. }
  249. /* }}} */
  250. /* {{{ */
  251. PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC)
  252. {
  253. php_stream_memory_data *self;
  254. php_stream *stream;
  255. self = emalloc(sizeof(*self));
  256. self->data = ZSTR_EMPTY_ALLOC();
  257. self->fpos = 0;
  258. self->mode = mode;
  259. stream = php_stream_alloc_rel(&php_stream_memory_ops, self, 0, _php_stream_mode_to_str(mode));
  260. stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
  261. return stream;
  262. }
  263. /* }}} */
  264. /* {{{ */
  265. PHPAPI php_stream *_php_stream_memory_open(int mode, zend_string *buf STREAMS_DC)
  266. {
  267. php_stream *stream;
  268. php_stream_memory_data *ms;
  269. if ((stream = php_stream_memory_create_rel(mode)) != NULL) {
  270. ms = (php_stream_memory_data*)stream->abstract;
  271. ms->data = zend_string_copy(buf);
  272. }
  273. return stream;
  274. }
  275. /* }}} */
  276. /* {{{ */
  277. PHPAPI zend_string *_php_stream_memory_get_buffer(php_stream *stream STREAMS_DC)
  278. {
  279. php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
  280. ZEND_ASSERT(ms != NULL);
  281. return ms->data;
  282. }
  283. /* }}} */
  284. /* }}} */
  285. /* {{{ ------- TEMP stream implementation -------*/
  286. typedef struct {
  287. php_stream *innerstream;
  288. size_t smax;
  289. int mode;
  290. zval meta;
  291. char* tmpdir;
  292. } php_stream_temp_data;
  293. /* {{{ */
  294. static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count)
  295. {
  296. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  297. assert(ts != NULL);
  298. if (!ts->innerstream) {
  299. return -1;
  300. }
  301. if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
  302. zend_off_t pos = php_stream_tell(ts->innerstream);
  303. if (pos + count >= ts->smax) {
  304. zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream);
  305. php_stream *file = php_stream_fopen_temporary_file(ts->tmpdir, "php", NULL);
  306. if (file == NULL) {
  307. php_error_docref(NULL, E_WARNING, "Unable to create temporary file, Check permissions in temporary files directory.");
  308. return 0;
  309. }
  310. php_stream_write(file, ZSTR_VAL(membuf), ZSTR_LEN(membuf));
  311. php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
  312. ts->innerstream = file;
  313. php_stream_encloses(stream, ts->innerstream);
  314. php_stream_seek(ts->innerstream, pos, SEEK_SET);
  315. }
  316. }
  317. return php_stream_write(ts->innerstream, buf, count);
  318. }
  319. /* }}} */
  320. /* {{{ */
  321. static ssize_t php_stream_temp_read(php_stream *stream, char *buf, size_t count)
  322. {
  323. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  324. size_t got;
  325. assert(ts != NULL);
  326. if (!ts->innerstream) {
  327. return -1;
  328. }
  329. got = php_stream_read(ts->innerstream, buf, count);
  330. stream->eof = ts->innerstream->eof;
  331. return got;
  332. }
  333. /* }}} */
  334. /* {{{ */
  335. static int php_stream_temp_close(php_stream *stream, int close_handle)
  336. {
  337. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  338. int ret;
  339. assert(ts != NULL);
  340. if (ts->innerstream) {
  341. ret = php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE | (close_handle ? 0 : PHP_STREAM_FREE_PRESERVE_HANDLE));
  342. } else {
  343. ret = 0;
  344. }
  345. zval_ptr_dtor(&ts->meta);
  346. if (ts->tmpdir) {
  347. efree(ts->tmpdir);
  348. }
  349. efree(ts);
  350. return ret;
  351. }
  352. /* }}} */
  353. /* {{{ */
  354. static int php_stream_temp_flush(php_stream *stream)
  355. {
  356. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  357. assert(ts != NULL);
  358. return ts->innerstream ? php_stream_flush(ts->innerstream) : -1;
  359. }
  360. /* }}} */
  361. /* {{{ */
  362. static int php_stream_temp_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
  363. {
  364. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  365. int ret;
  366. assert(ts != NULL);
  367. if (!ts->innerstream) {
  368. *newoffs = -1;
  369. return -1;
  370. }
  371. ret = php_stream_seek(ts->innerstream, offset, whence);
  372. *newoffs = php_stream_tell(ts->innerstream);
  373. stream->eof = ts->innerstream->eof;
  374. return ret;
  375. }
  376. /* }}} */
  377. /* {{{ */
  378. static int php_stream_temp_cast(php_stream *stream, int castas, void **ret)
  379. {
  380. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  381. php_stream *file;
  382. zend_string *membuf;
  383. zend_off_t pos;
  384. assert(ts != NULL);
  385. if (!ts->innerstream) {
  386. return FAILURE;
  387. }
  388. if (php_stream_is(ts->innerstream, PHP_STREAM_IS_STDIO)) {
  389. return php_stream_cast(ts->innerstream, castas, ret, 0);
  390. }
  391. /* we are still using a memory based backing. If they are if we can be
  392. * a FILE*, say yes because we can perform the conversion.
  393. * If they actually want to perform the conversion, we need to switch
  394. * the memory stream to a tmpfile stream */
  395. if (ret == NULL && castas == PHP_STREAM_AS_STDIO) {
  396. return SUCCESS;
  397. }
  398. /* say "no" to other stream forms */
  399. if (ret == NULL) {
  400. return FAILURE;
  401. }
  402. file = php_stream_fopen_tmpfile();
  403. if (file == NULL) {
  404. php_error_docref(NULL, E_WARNING, "Unable to create temporary file.");
  405. return FAILURE;
  406. }
  407. /* perform the conversion and then pass the request on to the innerstream */
  408. membuf = php_stream_memory_get_buffer(ts->innerstream);
  409. php_stream_write(file, ZSTR_VAL(membuf), ZSTR_LEN(membuf));
  410. pos = php_stream_tell(ts->innerstream);
  411. php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
  412. ts->innerstream = file;
  413. php_stream_encloses(stream, ts->innerstream);
  414. php_stream_seek(ts->innerstream, pos, SEEK_SET);
  415. return php_stream_cast(ts->innerstream, castas, ret, 1);
  416. }
  417. /* }}} */
  418. static int php_stream_temp_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
  419. {
  420. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  421. if (!ts || !ts->innerstream) {
  422. return -1;
  423. }
  424. return php_stream_stat(ts->innerstream, ssb);
  425. }
  426. /* }}} */
  427. static int php_stream_temp_set_option(php_stream *stream, int option, int value, void *ptrparam) /* {{{ */
  428. {
  429. php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
  430. switch(option) {
  431. case PHP_STREAM_OPTION_META_DATA_API:
  432. if (Z_TYPE(ts->meta) != IS_UNDEF) {
  433. zend_hash_copy(Z_ARRVAL_P((zval*)ptrparam), Z_ARRVAL(ts->meta), zval_add_ref);
  434. }
  435. return PHP_STREAM_OPTION_RETURN_OK;
  436. default:
  437. if (ts->innerstream) {
  438. return php_stream_set_option(ts->innerstream, option, value, ptrparam);
  439. }
  440. return PHP_STREAM_OPTION_RETURN_NOTIMPL;
  441. }
  442. }
  443. /* }}} */
  444. PHPAPI const php_stream_ops php_stream_temp_ops = {
  445. php_stream_temp_write, php_stream_temp_read,
  446. php_stream_temp_close, php_stream_temp_flush,
  447. "TEMP",
  448. php_stream_temp_seek,
  449. php_stream_temp_cast,
  450. php_stream_temp_stat,
  451. php_stream_temp_set_option
  452. };
  453. /* }}} */
  454. /* {{{ _php_stream_temp_create_ex */
  455. PHPAPI php_stream *_php_stream_temp_create_ex(int mode, size_t max_memory_usage, const char *tmpdir STREAMS_DC)
  456. {
  457. php_stream_temp_data *self;
  458. php_stream *stream;
  459. self = ecalloc(1, sizeof(*self));
  460. self->smax = max_memory_usage;
  461. self->mode = mode;
  462. ZVAL_UNDEF(&self->meta);
  463. if (tmpdir) {
  464. self->tmpdir = estrdup(tmpdir);
  465. }
  466. stream = php_stream_alloc_rel(&php_stream_temp_ops, self, 0, _php_stream_mode_to_str(mode));
  467. stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
  468. self->innerstream = php_stream_memory_create_rel(mode);
  469. php_stream_encloses(stream, self->innerstream);
  470. return stream;
  471. }
  472. /* }}} */
  473. /* {{{ _php_stream_temp_create */
  474. PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STREAMS_DC)
  475. {
  476. return php_stream_temp_create_ex(mode, max_memory_usage, NULL);
  477. }
  478. /* }}} */
  479. /* {{{ _php_stream_temp_open */
  480. PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, const char *buf, size_t length STREAMS_DC)
  481. {
  482. php_stream *stream;
  483. php_stream_temp_data *ts;
  484. zend_off_t newoffs;
  485. if ((stream = php_stream_temp_create_rel(mode, max_memory_usage)) != NULL) {
  486. if (length) {
  487. assert(buf != NULL);
  488. php_stream_temp_write(stream, buf, length);
  489. php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs);
  490. }
  491. ts = (php_stream_temp_data*)stream->abstract;
  492. assert(ts != NULL);
  493. ts->mode = mode;
  494. }
  495. return stream;
  496. }
  497. /* }}} */
  498. PHPAPI const php_stream_ops php_stream_rfc2397_ops = {
  499. NULL, php_stream_temp_read,
  500. php_stream_temp_close, php_stream_temp_flush,
  501. "RFC2397",
  502. php_stream_temp_seek,
  503. php_stream_temp_cast,
  504. php_stream_temp_stat,
  505. php_stream_temp_set_option
  506. };
  507. static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper, const char *path,
  508. const char *mode, int options, zend_string **opened_path,
  509. php_stream_context *context STREAMS_DC) /* {{{ */
  510. {
  511. php_stream *stream;
  512. php_stream_temp_data *ts;
  513. char *comma, *semi, *sep;
  514. size_t mlen, dlen, plen, vlen, ilen;
  515. zend_off_t newoffs;
  516. zval meta;
  517. int base64 = 0;
  518. zend_string *base64_comma = NULL;
  519. ZVAL_NULL(&meta);
  520. if (memcmp(path, "data:", 5)) {
  521. return NULL;
  522. }
  523. path += 5;
  524. dlen = strlen(path);
  525. if (dlen >= 2 && path[0] == '/' && path[1] == '/') {
  526. dlen -= 2;
  527. path += 2;
  528. }
  529. if ((comma = memchr(path, ',', dlen)) == NULL) {
  530. php_stream_wrapper_log_error(wrapper, options, "rfc2397: no comma in URL");
  531. return NULL;
  532. }
  533. if (comma != path) {
  534. /* meta info */
  535. mlen = comma - path;
  536. dlen -= mlen;
  537. semi = memchr(path, ';', mlen);
  538. sep = memchr(path, '/', mlen);
  539. if (!semi && !sep) {
  540. php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal media type");
  541. return NULL;
  542. }
  543. array_init(&meta);
  544. if (!semi) { /* there is only a mime type */
  545. add_assoc_stringl(&meta, "mediatype", (char *) path, mlen);
  546. mlen = 0;
  547. } else if (sep && sep < semi) { /* there is a mime type */
  548. plen = semi - path;
  549. add_assoc_stringl(&meta, "mediatype", (char *) path, plen);
  550. mlen -= plen;
  551. path += plen;
  552. } else if (semi != path || mlen != sizeof(";base64")-1 || memcmp(path, ";base64", sizeof(";base64")-1)) { /* must be error since parameters are only allowed after mediatype */
  553. zval_ptr_dtor(&meta);
  554. php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal media type");
  555. return NULL;
  556. }
  557. /* get parameters and potentially ';base64' */
  558. while(semi && (semi == path)) {
  559. path++;
  560. mlen--;
  561. sep = memchr(path, '=', mlen);
  562. semi = memchr(path, ';', mlen);
  563. if (!sep || (semi && semi < sep)) { /* must be ';base64' or failure */
  564. if (mlen != sizeof("base64")-1 || memcmp(path, "base64", sizeof("base64")-1)) {
  565. /* must be error since parameters are only allowed after mediatype and we have no '=' sign */
  566. zval_ptr_dtor(&meta);
  567. php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal parameter");
  568. return NULL;
  569. }
  570. base64 = 1;
  571. mlen -= sizeof("base64") - 1;
  572. path += sizeof("base64") - 1;
  573. break;
  574. }
  575. /* found parameter ... the heart of cs ppl lies in +1/-1 or was it +2 this time? */
  576. plen = sep - path;
  577. vlen = (semi ? (size_t)(semi - sep) : (mlen - plen)) - 1 /* '=' */;
  578. if (plen != sizeof("mediatype")-1 || memcmp(path, "mediatype", sizeof("mediatype")-1)) {
  579. add_assoc_stringl_ex(&meta, path, plen, sep + 1, vlen);
  580. }
  581. plen += vlen + 1;
  582. mlen -= plen;
  583. path += plen;
  584. }
  585. if (mlen) {
  586. zval_ptr_dtor(&meta);
  587. php_stream_wrapper_log_error(wrapper, options, "rfc2397: illegal URL");
  588. return NULL;
  589. }
  590. } else {
  591. array_init(&meta);
  592. }
  593. add_assoc_bool(&meta, "base64", base64);
  594. /* skip ',' */
  595. comma++;
  596. dlen--;
  597. if (base64) {
  598. base64_comma = php_base64_decode_ex((const unsigned char *)comma, dlen, 1);
  599. if (!base64_comma) {
  600. zval_ptr_dtor(&meta);
  601. php_stream_wrapper_log_error(wrapper, options, "rfc2397: unable to decode");
  602. return NULL;
  603. }
  604. comma = ZSTR_VAL(base64_comma);
  605. ilen = ZSTR_LEN(base64_comma);
  606. } else {
  607. comma = estrndup(comma, dlen);
  608. dlen = php_url_decode(comma, dlen);
  609. ilen = dlen;
  610. }
  611. if ((stream = php_stream_temp_create_rel(0, ~0u)) != NULL) {
  612. /* store data */
  613. php_stream_temp_write(stream, comma, ilen);
  614. php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs);
  615. /* set special stream stuff (enforce exact mode) */
  616. vlen = strlen(mode);
  617. if (vlen >= sizeof(stream->mode)) {
  618. vlen = sizeof(stream->mode) - 1;
  619. }
  620. memcpy(stream->mode, mode, vlen);
  621. stream->mode[vlen] = '\0';
  622. stream->ops = &php_stream_rfc2397_ops;
  623. ts = (php_stream_temp_data*)stream->abstract;
  624. assert(ts != NULL);
  625. ts->mode = mode && mode[0] == 'r' && mode[1] != '+' ? TEMP_STREAM_READONLY : 0;
  626. ZVAL_COPY_VALUE(&ts->meta, &meta);
  627. }
  628. if (base64_comma) {
  629. zend_string_free(base64_comma);
  630. } else {
  631. efree(comma);
  632. }
  633. return stream;
  634. }
  635. PHPAPI const php_stream_wrapper_ops php_stream_rfc2397_wops = {
  636. php_stream_url_wrap_rfc2397,
  637. NULL, /* close */
  638. NULL, /* fstat */
  639. NULL, /* stat */
  640. NULL, /* opendir */
  641. "RFC2397",
  642. NULL, /* unlink */
  643. NULL, /* rename */
  644. NULL, /* mkdir */
  645. NULL, /* rmdir */
  646. NULL, /* stream_metadata */
  647. };
  648. PHPAPI const php_stream_wrapper php_stream_rfc2397_wrapper = {
  649. &php_stream_rfc2397_wops,
  650. NULL,
  651. 1, /* is_url */
  652. };