memory.c 20 KB

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