123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319 |
- #define _GNU_SOURCE
- #include "php.h"
- #include "php_globals.h"
- #include "php_memory_streams.h"
- #include "php_network.h"
- #include "php_open_temporary_file.h"
- #include "ext/standard/file.h"
- #include "ext/standard/basic_functions.h"
- #include "ext/standard/php_string.h"
- #include <stddef.h>
- #include <fcntl.h>
- #include "php_streams_int.h"
- static HashTable url_stream_wrappers_hash;
- static int le_stream = FAILURE;
- static int le_pstream = FAILURE;
- static int le_stream_filter = FAILURE;
- PHPAPI int php_file_le_stream(void)
- {
- return le_stream;
- }
- PHPAPI int php_file_le_pstream(void)
- {
- return le_pstream;
- }
- PHPAPI int php_file_le_stream_filter(void)
- {
- return le_stream_filter;
- }
- PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(void)
- {
- return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
- }
- PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void)
- {
- return &url_stream_wrappers_hash;
- }
- static int forget_persistent_resource_id_numbers(zval *el)
- {
- php_stream *stream;
- zend_resource *rsrc = Z_RES_P(el);
- if (rsrc->type != le_pstream) {
- return 0;
- }
- stream = (php_stream*)rsrc->ptr;
- #if STREAM_DEBUG
- fprintf(stderr, "forget_persistent: %s:%p\n", stream->ops->label, stream);
- #endif
- stream->res = NULL;
- if (stream->ctx) {
- zend_list_delete(stream->ctx);
- stream->ctx = NULL;
- }
- return 0;
- }
- PHP_RSHUTDOWN_FUNCTION(streams)
- {
- zend_hash_apply(&EG(persistent_list), forget_persistent_resource_id_numbers);
- return SUCCESS;
- }
- PHPAPI php_stream *php_stream_encloses(php_stream *enclosing, php_stream *enclosed)
- {
- php_stream *orig = enclosed->enclosing_stream;
- php_stream_auto_cleanup(enclosed);
- enclosed->enclosing_stream = enclosing;
- return orig;
- }
- PHPAPI int php_stream_from_persistent_id(const char *persistent_id, php_stream **stream)
- {
- zend_resource *le;
- if ((le = zend_hash_str_find_ptr(&EG(persistent_list), persistent_id, strlen(persistent_id))) != NULL) {
- if (le->type == le_pstream) {
- if (stream) {
- zend_resource *regentry = NULL;
-
- *stream = (php_stream*)le->ptr;
- ZEND_HASH_FOREACH_PTR(&EG(regular_list), regentry) {
- if (regentry->ptr == le->ptr) {
- GC_ADDREF(regentry);
- (*stream)->res = regentry;
- return PHP_STREAM_PERSISTENT_SUCCESS;
- }
- } ZEND_HASH_FOREACH_END();
- GC_ADDREF(le);
- (*stream)->res = zend_register_resource(*stream, le_pstream);
- }
- return PHP_STREAM_PERSISTENT_SUCCESS;
- }
- return PHP_STREAM_PERSISTENT_FAILURE;
- }
- return PHP_STREAM_PERSISTENT_NOT_EXIST;
- }
- static zend_llist *php_get_wrapper_errors_list(php_stream_wrapper *wrapper)
- {
- if (!FG(wrapper_errors)) {
- return NULL;
- } else {
- return (zend_llist*) zend_hash_str_find_ptr(FG(wrapper_errors), (const char*)&wrapper, sizeof(wrapper));
- }
- }
- void php_stream_display_wrapper_errors(php_stream_wrapper *wrapper, const char *path, const char *caption)
- {
- char *tmp;
- char *msg;
- int free_msg = 0;
- if (EG(exception)) {
-
- return;
- }
- tmp = estrdup(path);
- if (wrapper) {
- zend_llist *err_list = php_get_wrapper_errors_list(wrapper);
- if (err_list) {
- size_t l = 0;
- int brlen;
- int i;
- int count = (int)zend_llist_count(err_list);
- const char *br;
- const char **err_buf_p;
- zend_llist_position pos;
- if (PG(html_errors)) {
- brlen = 7;
- br = "<br />\n";
- } else {
- brlen = 1;
- br = "\n";
- }
- for (err_buf_p = zend_llist_get_first_ex(err_list, &pos), i = 0;
- err_buf_p;
- err_buf_p = zend_llist_get_next_ex(err_list, &pos), i++) {
- l += strlen(*err_buf_p);
- if (i < count - 1) {
- l += brlen;
- }
- }
- msg = emalloc(l + 1);
- msg[0] = '\0';
- for (err_buf_p = zend_llist_get_first_ex(err_list, &pos), i = 0;
- err_buf_p;
- err_buf_p = zend_llist_get_next_ex(err_list, &pos), i++) {
- strcat(msg, *err_buf_p);
- if (i < count - 1) {
- strcat(msg, br);
- }
- }
- free_msg = 1;
- } else {
- if (wrapper == &php_plain_files_wrapper) {
- msg = strerror(errno);
- } else {
- msg = "operation failed";
- }
- }
- } else {
- msg = "no suitable wrapper could be found";
- }
- php_strip_url_passwd(tmp);
- php_error_docref1(NULL, tmp, E_WARNING, "%s: %s", caption, msg);
- efree(tmp);
- if (free_msg) {
- efree(msg);
- }
- }
- void php_stream_tidy_wrapper_error_log(php_stream_wrapper *wrapper)
- {
- if (wrapper && FG(wrapper_errors)) {
- zend_hash_str_del(FG(wrapper_errors), (const char*)&wrapper, sizeof(wrapper));
- }
- }
- static void wrapper_error_dtor(void *error)
- {
- efree(*(char**)error);
- }
- static void wrapper_list_dtor(zval *item) {
- zend_llist *list = (zend_llist*)Z_PTR_P(item);
- zend_llist_destroy(list);
- efree(list);
- }
- PHPAPI void php_stream_wrapper_log_error(const php_stream_wrapper *wrapper, int options, const char *fmt, ...)
- {
- va_list args;
- char *buffer = NULL;
- va_start(args, fmt);
- vspprintf(&buffer, 0, fmt, args);
- va_end(args);
- if (options & REPORT_ERRORS || wrapper == NULL) {
- php_error_docref(NULL, E_WARNING, "%s", buffer);
- efree(buffer);
- } else {
- zend_llist *list = NULL;
- if (!FG(wrapper_errors)) {
- ALLOC_HASHTABLE(FG(wrapper_errors));
- zend_hash_init(FG(wrapper_errors), 8, NULL, wrapper_list_dtor, 0);
- } else {
- list = zend_hash_str_find_ptr(FG(wrapper_errors), (const char*)&wrapper, sizeof(wrapper));
- }
- if (!list) {
- zend_llist new_list;
- zend_llist_init(&new_list, sizeof(buffer), wrapper_error_dtor, 0);
- list = zend_hash_str_update_mem(FG(wrapper_errors), (const char*)&wrapper,
- sizeof(wrapper), &new_list, sizeof(new_list));
- }
-
- zend_llist_add_element(list, &buffer);
- }
- }
- PHPAPI php_stream *_php_stream_alloc(const php_stream_ops *ops, void *abstract, const char *persistent_id, const char *mode STREAMS_DC)
- {
- php_stream *ret;
- ret = (php_stream*) pemalloc_rel_orig(sizeof(php_stream), persistent_id ? 1 : 0);
- memset(ret, 0, sizeof(php_stream));
- ret->readfilters.stream = ret;
- ret->writefilters.stream = ret;
- #if STREAM_DEBUG
- fprintf(stderr, "stream_alloc: %s:%p persistent=%s\n", ops->label, ret, persistent_id);
- #endif
- ret->ops = ops;
- ret->abstract = abstract;
- ret->is_persistent = persistent_id ? 1 : 0;
- ret->chunk_size = FG(def_chunk_size);
- #if ZEND_DEBUG
- ret->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename;
- ret->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno;
- #endif
- if (FG(auto_detect_line_endings)) {
- ret->flags |= PHP_STREAM_FLAG_DETECT_EOL;
- }
- if (persistent_id) {
- if (NULL == zend_register_persistent_resource(persistent_id, strlen(persistent_id), ret, le_pstream)) {
- pefree(ret, 1);
- return NULL;
- }
- }
- ret->res = zend_register_resource(ret, persistent_id ? le_pstream : le_stream);
- strlcpy(ret->mode, mode, sizeof(ret->mode));
- ret->wrapper = NULL;
- ret->wrapperthis = NULL;
- ZVAL_UNDEF(&ret->wrapperdata);
- ret->stdiocast = NULL;
- ret->orig_path = NULL;
- ret->ctx = NULL;
- ret->readbuf = NULL;
- ret->enclosing_stream = NULL;
- return ret;
- }
- PHPAPI int _php_stream_free_enclosed(php_stream *stream_enclosed, int close_options)
- {
- return php_stream_free(stream_enclosed,
- close_options | PHP_STREAM_FREE_IGNORE_ENCLOSING);
- }
- #if STREAM_DEBUG
- static const char *_php_stream_pretty_free_options(int close_options, char *out)
- {
- if (close_options & PHP_STREAM_FREE_CALL_DTOR)
- strcat(out, "CALL_DTOR, ");
- if (close_options & PHP_STREAM_FREE_RELEASE_STREAM)
- strcat(out, "RELEASE_STREAM, ");
- if (close_options & PHP_STREAM_FREE_PRESERVE_HANDLE)
- strcat(out, "PREVERSE_HANDLE, ");
- if (close_options & PHP_STREAM_FREE_RSRC_DTOR)
- strcat(out, "RSRC_DTOR, ");
- if (close_options & PHP_STREAM_FREE_PERSISTENT)
- strcat(out, "PERSISTENT, ");
- if (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING)
- strcat(out, "IGNORE_ENCLOSING, ");
- if (out[0] != '\0')
- out[strlen(out) - 2] = '\0';
- return out;
- }
- #endif
- static int _php_stream_free_persistent(zval *zv, void *pStream)
- {
- zend_resource *le = Z_RES_P(zv);
- return le->ptr == pStream;
- }
- PHPAPI int _php_stream_free(php_stream *stream, int close_options)
- {
- int ret = 1;
- int preserve_handle = close_options & PHP_STREAM_FREE_PRESERVE_HANDLE ? 1 : 0;
- int release_cast = 1;
- php_stream_context *context = NULL;
-
- if (EG(active)) {
- context = PHP_STREAM_CONTEXT(stream);
- }
- if (stream->flags & PHP_STREAM_FLAG_NO_CLOSE) {
- preserve_handle = 1;
- }
- #if STREAM_DEBUG
- {
- char out[200] = "";
- fprintf(stderr, "stream_free: %s:%p[%s] in_free=%d opts=%s\n",
- stream->ops->label, stream, stream->orig_path, stream->in_free, _php_stream_pretty_free_options(close_options, out));
- }
- #endif
- if (stream->in_free) {
-
- if ((stream->in_free == 1) && (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) && (stream->enclosing_stream == NULL)) {
- close_options |= PHP_STREAM_FREE_RSRC_DTOR;
- } else {
- return 1;
- }
- }
- stream->in_free++;
-
- if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) &&
- !(close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) &&
- (close_options & (PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_RELEASE_STREAM)) &&
- (stream->enclosing_stream != NULL)) {
- php_stream *enclosing_stream = stream->enclosing_stream;
- stream->enclosing_stream = NULL;
-
- return php_stream_free(enclosing_stream,
- (close_options | PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_KEEP_RSRC) & ~PHP_STREAM_FREE_RSRC_DTOR);
- }
-
- if (preserve_handle) {
- if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
-
- php_stream_auto_cleanup(stream);
- stream->in_free--;
- return 0;
- }
-
- release_cast = 0;
- }
- #if STREAM_DEBUG
- fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remove_rsrc=%d\n",
- stream->ops->label, stream, stream->orig_path, preserve_handle, release_cast,
- (close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0);
- #endif
- if (stream->flags & PHP_STREAM_FLAG_WAS_WRITTEN) {
-
- _php_stream_flush(stream, 1);
- }
-
- if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0 && stream->res) {
-
- zend_list_close(stream->res);
- if ((close_options & PHP_STREAM_FREE_KEEP_RSRC) == 0) {
-
- zend_list_delete(stream->res);
- stream->res = NULL;
- }
- }
- if (close_options & PHP_STREAM_FREE_CALL_DTOR) {
- if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
-
- stream->in_free = 0;
- return fclose(stream->stdiocast);
- }
- ret = stream->ops->close(stream, preserve_handle ? 0 : 1);
- stream->abstract = NULL;
-
- if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) {
- fclose(stream->stdiocast);
- stream->stdiocast = NULL;
- stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
- }
- }
- if (close_options & PHP_STREAM_FREE_RELEASE_STREAM) {
- while (stream->readfilters.head) {
- if (stream->readfilters.head->res != NULL) {
- zend_list_close(stream->readfilters.head->res);
- }
- php_stream_filter_remove(stream->readfilters.head, 1);
- }
- while (stream->writefilters.head) {
- if (stream->writefilters.head->res != NULL) {
- zend_list_close(stream->writefilters.head->res);
- }
- php_stream_filter_remove(stream->writefilters.head, 1);
- }
- if (stream->wrapper && stream->wrapper->wops && stream->wrapper->wops->stream_closer) {
- stream->wrapper->wops->stream_closer(stream->wrapper, stream);
- stream->wrapper = NULL;
- }
- if (Z_TYPE(stream->wrapperdata) != IS_UNDEF) {
- zval_ptr_dtor(&stream->wrapperdata);
- ZVAL_UNDEF(&stream->wrapperdata);
- }
- if (stream->readbuf) {
- pefree(stream->readbuf, stream->is_persistent);
- stream->readbuf = NULL;
- }
- if (stream->is_persistent && (close_options & PHP_STREAM_FREE_PERSISTENT)) {
-
- zend_hash_apply_with_argument(&EG(persistent_list), _php_stream_free_persistent, stream);
- }
- if (stream->orig_path) {
- pefree(stream->orig_path, stream->is_persistent);
- stream->orig_path = NULL;
- }
- pefree(stream, stream->is_persistent);
- }
- if (context) {
- zend_list_delete(context->res);
- }
- return ret;
- }
- PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size)
- {
-
- if (stream->readfilters.head) {
- char *chunk_buf;
- int err_flag = 0;
- php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL };
- php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out, *brig_swap;
-
- chunk_buf = emalloc(stream->chunk_size);
- while (!stream->eof && !err_flag && (stream->writepos - stream->readpos < (zend_off_t)size)) {
- size_t justread = 0;
- int flags;
- php_stream_bucket *bucket;
- php_stream_filter_status_t status = PSFS_ERR_FATAL;
- php_stream_filter *filter;
-
- justread = stream->ops->read(stream, chunk_buf, stream->chunk_size);
- if (justread && justread != (size_t)-1) {
- bucket = php_stream_bucket_new(stream, chunk_buf, justread, 0, 0);
-
- php_stream_bucket_append(brig_inp, bucket);
- flags = PSFS_FLAG_NORMAL;
- } else {
- flags = stream->eof ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC;
- }
-
- for (filter = stream->readfilters.head; filter; filter = filter->next) {
- status = filter->fops->filter(stream, filter, brig_inp, brig_outp, NULL, flags);
- if (status != PSFS_PASS_ON) {
- break;
- }
-
- brig_swap = brig_inp;
- brig_inp = brig_outp;
- brig_outp = brig_swap;
- memset(brig_outp, 0, sizeof(*brig_outp));
- }
- switch (status) {
- case PSFS_PASS_ON:
-
- while (brig_inp->head) {
- bucket = brig_inp->head;
-
- if (stream->readbuf && stream->readbuflen - stream->writepos < bucket->buflen) {
- if (stream->writepos > stream->readpos) {
- memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos);
- }
- stream->writepos -= stream->readpos;
- stream->readpos = 0;
- }
-
- if (stream->readbuflen - stream->writepos < bucket->buflen) {
- stream->readbuflen += bucket->buflen;
- stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,
- stream->is_persistent);
- }
- memcpy(stream->readbuf + stream->writepos, bucket->buf, bucket->buflen);
- stream->writepos += bucket->buflen;
- php_stream_bucket_unlink(bucket);
- php_stream_bucket_delref(bucket);
- }
- break;
- case PSFS_FEED_ME:
-
- if (justread == 0) {
-
- err_flag = 1;
- break;
- }
- continue;
- case PSFS_ERR_FATAL:
-
- err_flag = 1;
- break;
- }
- if (justread == 0 || justread == (size_t)-1) {
- break;
- }
- }
- efree(chunk_buf);
- } else {
-
- if (stream->writepos - stream->readpos < (zend_off_t)size) {
- size_t justread = 0;
-
- if (stream->readbuf && stream->readbuflen - stream->writepos < stream->chunk_size) {
- if (stream->writepos > stream->readpos) {
- memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos);
- }
- stream->writepos -= stream->readpos;
- stream->readpos = 0;
- }
-
- if (stream->readbuflen - stream->writepos < stream->chunk_size) {
- stream->readbuflen += stream->chunk_size;
- stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,
- stream->is_persistent);
- }
- justread = stream->ops->read(stream, (char*)stream->readbuf + stream->writepos,
- stream->readbuflen - stream->writepos
- );
- if (justread != (size_t)-1) {
- stream->writepos += justread;
- }
- }
- }
- }
- PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t size)
- {
- size_t toread = 0, didread = 0;
- while (size > 0) {
-
- if (stream->writepos > stream->readpos) {
- toread = stream->writepos - stream->readpos;
- if (toread > size) {
- toread = size;
- }
- memcpy(buf, stream->readbuf + stream->readpos, toread);
- stream->readpos += toread;
- size -= toread;
- buf += toread;
- didread += toread;
- }
-
- if (size == 0) {
- break;
- }
- if (!stream->readfilters.head && (stream->flags & PHP_STREAM_FLAG_NO_BUFFER || stream->chunk_size == 1)) {
- toread = stream->ops->read(stream, buf, size);
- if (toread == (size_t) -1) {
-
- break;
- }
- } else {
- php_stream_fill_read_buffer(stream, size);
- toread = stream->writepos - stream->readpos;
- if (toread > size) {
- toread = size;
- }
- if (toread > 0) {
- memcpy(buf, stream->readbuf + stream->readpos, toread);
- stream->readpos += toread;
- }
- }
- if (toread > 0) {
- didread += toread;
- buf += toread;
- size -= toread;
- } else {
-
- break;
- }
-
- if ((stream->wrapper != &php_plain_files_wrapper) &&
- (stream->ops != &php_stream_memory_ops) &&
- (stream->ops != &php_stream_temp_ops)) {
- break;
- }
- }
- if (didread > 0) {
- stream->position += didread;
- }
- return didread;
- }
- PHPAPI int _php_stream_eof(php_stream *stream)
- {
-
- if (stream->writepos - stream->readpos > 0) {
- return 0;
- }
-
- if (!stream->eof && PHP_STREAM_OPTION_RETURN_ERR ==
- php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS,
- 0, NULL)) {
- stream->eof = 1;
- }
- return stream->eof;
- }
- PHPAPI int _php_stream_putc(php_stream *stream, int c)
- {
- unsigned char buf = c;
- if (php_stream_write(stream, (char*)&buf, 1) > 0) {
- return 1;
- }
- return EOF;
- }
- PHPAPI int _php_stream_getc(php_stream *stream)
- {
- char buf;
- if (php_stream_read(stream, &buf, 1) > 0) {
- return buf & 0xff;
- }
- return EOF;
- }
- PHPAPI int _php_stream_puts(php_stream *stream, const char *buf)
- {
- size_t len;
- char newline[2] = "\n";
- len = strlen(buf);
- if (len > 0 && php_stream_write(stream, buf, len) && php_stream_write(stream, newline, 1)) {
- return 1;
- }
- return 0;
- }
- PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb)
- {
- memset(ssb, 0, sizeof(*ssb));
-
- if (stream->wrapper && stream->wrapper->wops->stream_stat != NULL) {
- return stream->wrapper->wops->stream_stat(stream->wrapper, stream, ssb);
- }
-
- if (stream->ops->stat == NULL) {
- return -1;
- }
- return (stream->ops->stat)(stream, ssb);
- }
- PHPAPI const char *php_stream_locate_eol(php_stream *stream, zend_string *buf)
- {
- size_t avail;
- const char *cr, *lf, *eol = NULL;
- const char *readptr;
- if (!buf) {
- readptr = (char*)stream->readbuf + stream->readpos;
- avail = stream->writepos - stream->readpos;
- } else {
- readptr = ZSTR_VAL(buf);
- avail = ZSTR_LEN(buf);
- }
-
- if (stream->flags & PHP_STREAM_FLAG_DETECT_EOL) {
- cr = memchr(readptr, '\r', avail);
- lf = memchr(readptr, '\n', avail);
- if (cr && lf != cr + 1 && !(lf && lf < cr)) {
-
- stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL;
- stream->flags |= PHP_STREAM_FLAG_EOL_MAC;
- eol = cr;
- } else if ((cr && lf && cr == lf - 1) || (lf)) {
-
- stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL;
- eol = lf;
- }
- } else if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
- eol = memchr(readptr, '\r', avail);
- } else {
-
- eol = memchr(readptr, '\n', avail);
- }
- return eol;
- }
- PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
- size_t *returned_len)
- {
- size_t avail = 0;
- size_t current_buf_size = 0;
- size_t total_copied = 0;
- int grow_mode = 0;
- char *bufstart = buf;
- if (buf == NULL) {
- grow_mode = 1;
- } else if (maxlen == 0) {
- return NULL;
- }
-
- for (;;) {
- avail = stream->writepos - stream->readpos;
- if (avail > 0) {
- size_t cpysz = 0;
- char *readptr;
- const char *eol;
- int done = 0;
- readptr = (char*)stream->readbuf + stream->readpos;
- eol = php_stream_locate_eol(stream, NULL);
- if (eol) {
- cpysz = eol - readptr + 1;
- done = 1;
- } else {
- cpysz = avail;
- }
- if (grow_mode) {
-
- bufstart = erealloc(bufstart, current_buf_size + cpysz + 1);
- current_buf_size += cpysz + 1;
- buf = bufstart + total_copied;
- } else {
- if (cpysz >= maxlen - 1) {
- cpysz = maxlen - 1;
- done = 1;
- }
- }
- memcpy(buf, readptr, cpysz);
- stream->position += cpysz;
- stream->readpos += cpysz;
- buf += cpysz;
- maxlen -= cpysz;
- total_copied += cpysz;
- if (done) {
- break;
- }
- } else if (stream->eof) {
- break;
- } else {
-
- size_t toread;
- if (grow_mode) {
- toread = stream->chunk_size;
- } else {
- toread = maxlen - 1;
- if (toread > stream->chunk_size) {
- toread = stream->chunk_size;
- }
- }
- php_stream_fill_read_buffer(stream, toread);
- if (stream->writepos - stream->readpos == 0) {
- break;
- }
- }
- }
- if (total_copied == 0) {
- if (grow_mode) {
- assert(bufstart == NULL);
- }
- return NULL;
- }
- buf[0] = '\0';
- if (returned_len) {
- *returned_len = total_copied;
- }
- return bufstart;
- }
- #define STREAM_BUFFERED_AMOUNT(stream) \
- ((size_t)(((stream)->writepos) - (stream)->readpos))
- static const char *_php_stream_search_delim(php_stream *stream,
- size_t maxlen,
- size_t skiplen,
- const char *delim,
- size_t delim_len)
- {
- size_t seek_len;
-
- seek_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen);
- if (seek_len <= skiplen) {
- return NULL;
- }
- if (delim_len == 1) {
- return memchr(&stream->readbuf[stream->readpos + skiplen],
- delim[0], seek_len - skiplen);
- } else {
- return php_memnstr((char*)&stream->readbuf[stream->readpos + skiplen],
- delim, delim_len,
- (char*)&stream->readbuf[stream->readpos + seek_len]);
- }
- }
- PHPAPI zend_string *php_stream_get_record(php_stream *stream, size_t maxlen, const char *delim, size_t delim_len)
- {
- zend_string *ret_buf;
- const char *found_delim = NULL;
- size_t buffered_len,
- tent_ret_len;
- int has_delim = delim_len > 0;
- if (maxlen == 0) {
- return NULL;
- }
- if (has_delim) {
- found_delim = _php_stream_search_delim(
- stream, maxlen, 0, delim, delim_len);
- }
- buffered_len = STREAM_BUFFERED_AMOUNT(stream);
-
- while (!found_delim && buffered_len < maxlen) {
- size_t just_read,
- to_read_now;
- to_read_now = MIN(maxlen - buffered_len, stream->chunk_size);
- php_stream_fill_read_buffer(stream, buffered_len + to_read_now);
- just_read = STREAM_BUFFERED_AMOUNT(stream) - buffered_len;
-
- if (just_read == 0) {
- break;
- }
- if (has_delim) {
-
- found_delim = _php_stream_search_delim(
- stream, maxlen,
- buffered_len >= (delim_len - 1)
- ? buffered_len - (delim_len - 1)
- : 0,
- delim, delim_len);
- if (found_delim) {
- break;
- }
- }
- buffered_len += just_read;
- }
- if (has_delim && found_delim) {
- tent_ret_len = found_delim - (char*)&stream->readbuf[stream->readpos];
- } else if (!has_delim && STREAM_BUFFERED_AMOUNT(stream) >= maxlen) {
- tent_ret_len = maxlen;
- } else {
-
- if (STREAM_BUFFERED_AMOUNT(stream) < maxlen && !stream->eof) {
- return NULL;
- } else if (STREAM_BUFFERED_AMOUNT(stream) == 0 && stream->eof) {
-
- return NULL;
- } else {
- tent_ret_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen);
- }
- }
- ret_buf = zend_string_alloc(tent_ret_len, 0);
-
- ZSTR_LEN(ret_buf) = php_stream_read(stream, ZSTR_VAL(ret_buf), tent_ret_len);
- if (found_delim) {
- stream->readpos += delim_len;
- stream->position += delim_len;
- }
- ZSTR_VAL(ret_buf)[ZSTR_LEN(ret_buf)] = '\0';
- return ret_buf;
- }
- static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size_t count)
- {
- size_t didwrite = 0, towrite, justwrote;
-
- if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && stream->readpos != stream->writepos) {
- stream->readpos = stream->writepos = 0;
- stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position);
- }
- while (count > 0) {
- towrite = count;
- if (towrite > stream->chunk_size)
- towrite = stream->chunk_size;
- justwrote = stream->ops->write(stream, buf, towrite);
-
- if ((int)justwrote > 0) {
- buf += justwrote;
- count -= justwrote;
- didwrite += justwrote;
-
- if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
- stream->position += justwrote;
- }
- } else {
- break;
- }
- }
- return didwrite;
- }
- static size_t _php_stream_write_filtered(php_stream *stream, const char *buf, size_t count, int flags)
- {
- size_t consumed = 0;
- php_stream_bucket *bucket;
- php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL };
- php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out, *brig_swap;
- php_stream_filter_status_t status = PSFS_ERR_FATAL;
- php_stream_filter *filter;
- if (buf) {
- bucket = php_stream_bucket_new(stream, (char *)buf, count, 0, 0);
- php_stream_bucket_append(&brig_in, bucket);
- }
- for (filter = stream->writefilters.head; filter; filter = filter->next) {
-
- status = filter->fops->filter(stream, filter, brig_inp, brig_outp,
- filter == stream->writefilters.head ? &consumed : NULL, flags);
- if (status != PSFS_PASS_ON) {
- break;
- }
-
- brig_swap = brig_inp;
- brig_inp = brig_outp;
- brig_outp = brig_swap;
- memset(brig_outp, 0, sizeof(*brig_outp));
- }
- switch (status) {
- case PSFS_PASS_ON:
-
- while (brig_inp->head) {
- bucket = brig_inp->head;
- _php_stream_write_buffer(stream, bucket->buf, bucket->buflen);
-
- php_stream_bucket_unlink(bucket);
- php_stream_bucket_delref(bucket);
- }
- break;
- case PSFS_FEED_ME:
-
- break;
- case PSFS_ERR_FATAL:
-
- break;
- }
- return consumed;
- }
- PHPAPI int _php_stream_flush(php_stream *stream, int closing)
- {
- int ret = 0;
- if (stream->writefilters.head) {
- _php_stream_write_filtered(stream, NULL, 0, closing ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC );
- }
- stream->flags &= ~PHP_STREAM_FLAG_WAS_WRITTEN;
- if (stream->ops->flush) {
- ret = stream->ops->flush(stream);
- }
- return ret;
- }
- PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t count)
- {
- size_t bytes;
- if (buf == NULL || count == 0 || stream->ops->write == NULL) {
- return 0;
- }
- if (stream->writefilters.head) {
- bytes = _php_stream_write_filtered(stream, buf, count, PSFS_FLAG_NORMAL);
- } else {
- bytes = _php_stream_write_buffer(stream, buf, count);
- }
- if (bytes) {
- stream->flags |= PHP_STREAM_FLAG_WAS_WRITTEN;
- }
- return bytes;
- }
- PHPAPI size_t _php_stream_printf(php_stream *stream, const char *fmt, ...)
- {
- size_t count;
- char *buf;
- va_list ap;
- va_start(ap, fmt);
- count = vspprintf(&buf, 0, fmt, ap);
- va_end(ap);
- if (!buf) {
- return 0;
- }
- count = php_stream_write(stream, buf, count);
- efree(buf);
- return count;
- }
- PHPAPI zend_off_t _php_stream_tell(php_stream *stream)
- {
- return stream->position;
- }
- PHPAPI int _php_stream_seek(php_stream *stream, zend_off_t offset, int whence)
- {
- if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
-
- fflush(stream->stdiocast);
- }
-
- if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) {
- switch(whence) {
- case SEEK_CUR:
- if (offset > 0 && offset <= stream->writepos - stream->readpos) {
- stream->readpos += offset;
- stream->position += offset;
- stream->eof = 0;
- return 0;
- }
- break;
- case SEEK_SET:
- if (offset > stream->position &&
- offset <= stream->position + stream->writepos - stream->readpos) {
- stream->readpos += offset - stream->position;
- stream->position = offset;
- stream->eof = 0;
- return 0;
- }
- break;
- }
- }
- if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
- int ret;
- if (stream->writefilters.head) {
- _php_stream_flush(stream, 0);
- }
- switch(whence) {
- case SEEK_CUR:
- offset = stream->position + offset;
- whence = SEEK_SET;
- break;
- }
- ret = stream->ops->seek(stream, offset, whence, &stream->position);
- if (((stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) || ret == 0) {
- if (ret == 0) {
- stream->eof = 0;
- }
-
- stream->readpos = stream->writepos = 0;
- return ret;
- }
-
- }
-
- if (whence == SEEK_CUR && offset >= 0) {
- char tmp[1024];
- size_t didread;
- while(offset > 0) {
- if ((didread = php_stream_read(stream, tmp, MIN(offset, sizeof(tmp)))) == 0) {
- return -1;
- }
- offset -= didread;
- }
- stream->eof = 0;
- return 0;
- }
- php_error_docref(NULL, E_WARNING, "stream does not support seeking");
- return -1;
- }
- PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, void *ptrparam)
- {
- int ret = PHP_STREAM_OPTION_RETURN_NOTIMPL;
- if (stream->ops->set_option) {
- ret = stream->ops->set_option(stream, option, value, ptrparam);
- }
- if (ret == PHP_STREAM_OPTION_RETURN_NOTIMPL) {
- switch(option) {
- case PHP_STREAM_OPTION_SET_CHUNK_SIZE:
-
- ret = stream->chunk_size > INT_MAX ? INT_MAX : (int)stream->chunk_size;
- stream->chunk_size = value;
- return ret;
- case PHP_STREAM_OPTION_READ_BUFFER:
-
- if (value == PHP_STREAM_BUFFER_NONE) {
- stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
- } else if (stream->flags & PHP_STREAM_FLAG_NO_BUFFER) {
- stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER;
- }
- ret = PHP_STREAM_OPTION_RETURN_OK;
- break;
- default:
- ;
- }
- }
- return ret;
- }
- PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize)
- {
- return php_stream_set_option(stream, PHP_STREAM_OPTION_TRUNCATE_API, PHP_STREAM_TRUNCATE_SET_SIZE, &newsize);
- }
- PHPAPI size_t _php_stream_passthru(php_stream * stream STREAMS_DC)
- {
- size_t bcount = 0;
- char buf[8192];
- size_t b;
- if (php_stream_mmap_possible(stream)) {
- char *p;
- size_t mapped;
- p = php_stream_mmap_range(stream, php_stream_tell(stream), PHP_STREAM_MMAP_ALL, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);
- if (p) {
- do {
-
- if (0 < (b = PHPWRITE(p + bcount, MIN(mapped - bcount, INT_MAX)))) {
- bcount += b;
- }
- } while (b > 0 && mapped > bcount);
- php_stream_mmap_unmap_ex(stream, mapped);
- return bcount;
- }
- }
- while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) {
- PHPWRITE(buf, b);
- bcount += b;
- }
- return bcount;
- }
- PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int persistent STREAMS_DC)
- {
- size_t ret = 0;
- char *ptr;
- size_t len = 0, max_len;
- int step = CHUNK_SIZE;
- int min_room = CHUNK_SIZE / 4;
- php_stream_statbuf ssbuf;
- zend_string *result;
- if (maxlen == 0) {
- return ZSTR_EMPTY_ALLOC();
- }
- if (maxlen == PHP_STREAM_COPY_ALL) {
- maxlen = 0;
- }
- if (maxlen > 0) {
- result = zend_string_alloc(maxlen, persistent);
- ptr = ZSTR_VAL(result);
- while ((len < maxlen) && !php_stream_eof(src)) {
- ret = php_stream_read(src, ptr, maxlen - len);
- if (!ret) {
- break;
- }
- len += ret;
- ptr += ret;
- }
- if (len) {
- ZSTR_LEN(result) = len;
- ZSTR_VAL(result)[len] = '\0';
-
- if (len < maxlen / 2) {
- result = zend_string_truncate(result, len, persistent);
- }
- } else {
- zend_string_free(result);
- result = NULL;
- }
- return result;
- }
-
- if (php_stream_stat(src, &ssbuf) == 0 && ssbuf.sb.st_size > 0) {
- max_len = ssbuf.sb.st_size + step;
- } else {
- max_len = step;
- }
- result = zend_string_alloc(max_len, persistent);
- ptr = ZSTR_VAL(result);
- while ((ret = php_stream_read(src, ptr, max_len - len))) {
- len += ret;
- if (len + min_room >= max_len) {
- result = zend_string_extend(result, max_len + step, persistent);
- max_len += step;
- ptr = ZSTR_VAL(result) + len;
- } else {
- ptr += ret;
- }
- }
- if (len) {
- result = zend_string_truncate(result, len, persistent);
- ZSTR_VAL(result)[len] = '\0';
- } else {
- zend_string_free(result);
- result = NULL;
- }
- return result;
- }
- PHPAPI int _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t *len STREAMS_DC)
- {
- char buf[CHUNK_SIZE];
- size_t readchunk;
- size_t haveread = 0;
- size_t didread, didwrite, towrite;
- size_t dummy;
- php_stream_statbuf ssbuf;
- if (!len) {
- len = &dummy;
- }
- if (maxlen == 0) {
- *len = 0;
- return SUCCESS;
- }
- if (maxlen == PHP_STREAM_COPY_ALL) {
- maxlen = 0;
- }
- if (php_stream_stat(src, &ssbuf) == 0) {
- if (ssbuf.sb.st_size == 0
- #ifdef S_ISREG
- && S_ISREG(ssbuf.sb.st_mode)
- #endif
- ) {
- *len = 0;
- return SUCCESS;
- }
- }
- if (php_stream_mmap_possible(src)) {
- char *p;
- size_t mapped;
- p = php_stream_mmap_range(src, php_stream_tell(src), maxlen, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);
- if (p) {
- didwrite = php_stream_write(dest, p, mapped);
- php_stream_mmap_unmap_ex(src, mapped);
- *len = didwrite;
-
- if (mapped > 0 && mapped == didwrite) {
- return SUCCESS;
- }
- return FAILURE;
- }
- }
- while(1) {
- readchunk = sizeof(buf);
- if (maxlen && (maxlen - haveread) < readchunk) {
- readchunk = maxlen - haveread;
- }
- didread = php_stream_read(src, buf, readchunk);
- if (didread) {
-
- char *writeptr;
- towrite = didread;
- writeptr = buf;
- haveread += didread;
- while(towrite) {
- didwrite = php_stream_write(dest, writeptr, towrite);
- if (didwrite == 0) {
- *len = haveread - (didread - towrite);
- return FAILURE;
- }
- towrite -= didwrite;
- writeptr += didwrite;
- }
- } else {
- break;
- }
- if (maxlen - haveread == 0) {
- break;
- }
- }
- *len = haveread;
-
- if (haveread > 0 || src->eof) {
- return SUCCESS;
- }
- return FAILURE;
- }
- ZEND_ATTRIBUTE_DEPRECATED
- PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC)
- {
- size_t len;
- int ret = _php_stream_copy_to_stream_ex(src, dest, maxlen, &len STREAMS_REL_CC);
- if (ret == SUCCESS && len == 0 && maxlen != 0) {
- return 1;
- }
- return len;
- }
- static void stream_resource_regular_dtor(zend_resource *rsrc)
- {
- php_stream *stream = (php_stream*)rsrc->ptr;
-
- FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
- }
- static void stream_resource_persistent_dtor(zend_resource *rsrc)
- {
- php_stream *stream = (php_stream*)rsrc->ptr;
- FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
- }
- void php_shutdown_stream_hashes(void)
- {
- if (FG(stream_wrappers)) {
- zend_hash_destroy(FG(stream_wrappers));
- efree(FG(stream_wrappers));
- FG(stream_wrappers) = NULL;
- }
- if (FG(stream_filters)) {
- zend_hash_destroy(FG(stream_filters));
- efree(FG(stream_filters));
- FG(stream_filters) = NULL;
- }
- if (FG(wrapper_errors)) {
- zend_hash_destroy(FG(wrapper_errors));
- efree(FG(wrapper_errors));
- FG(wrapper_errors) = NULL;
- }
- }
- int php_init_stream_wrappers(int module_number)
- {
- le_stream = zend_register_list_destructors_ex(stream_resource_regular_dtor, NULL, "stream", module_number);
- le_pstream = zend_register_list_destructors_ex(NULL, stream_resource_persistent_dtor, "persistent stream", module_number);
-
- le_stream_filter = zend_register_list_destructors_ex(NULL, NULL, "stream filter", module_number);
- zend_hash_init(&url_stream_wrappers_hash, 8, NULL, NULL, 1);
- zend_hash_init(php_get_stream_filters_hash_global(), 8, NULL, NULL, 1);
- zend_hash_init(php_stream_xport_get_hash(), 8, NULL, NULL, 1);
- return (php_stream_xport_register("tcp", php_stream_generic_socket_factory) == SUCCESS
- &&
- php_stream_xport_register("udp", php_stream_generic_socket_factory) == SUCCESS
- #if defined(AF_UNIX) && !(defined(PHP_WIN32) || defined(__riscos__))
- &&
- php_stream_xport_register("unix", php_stream_generic_socket_factory) == SUCCESS
- &&
- php_stream_xport_register("udg", php_stream_generic_socket_factory) == SUCCESS
- #endif
- ) ? SUCCESS : FAILURE;
- }
- int php_shutdown_stream_wrappers(int module_number)
- {
- zend_hash_destroy(&url_stream_wrappers_hash);
- zend_hash_destroy(php_get_stream_filters_hash_global());
- zend_hash_destroy(php_stream_xport_get_hash());
- return SUCCESS;
- }
- static inline int php_stream_wrapper_scheme_validate(const char *protocol, unsigned int protocol_len)
- {
- unsigned int i;
- for(i = 0; i < protocol_len; i++) {
- if (!isalnum((int)protocol[i]) &&
- protocol[i] != '+' &&
- protocol[i] != '-' &&
- protocol[i] != '.') {
- return FAILURE;
- }
- }
- return SUCCESS;
- }
- PHPAPI int php_register_url_stream_wrapper(const char *protocol, const php_stream_wrapper *wrapper)
- {
- unsigned int protocol_len = (unsigned int)strlen(protocol);
- int ret;
- zend_string *str;
- if (php_stream_wrapper_scheme_validate(protocol, protocol_len) == FAILURE) {
- return FAILURE;
- }
- str = zend_string_init_interned(protocol, protocol_len, 1);
- ret = zend_hash_add_ptr(&url_stream_wrappers_hash, str, (void*)wrapper) ? SUCCESS : FAILURE;
- zend_string_release_ex(str, 1);
- return ret;
- }
- PHPAPI int php_unregister_url_stream_wrapper(const char *protocol)
- {
- return zend_hash_str_del(&url_stream_wrappers_hash, protocol, strlen(protocol));
- }
- static void clone_wrapper_hash(void)
- {
- ALLOC_HASHTABLE(FG(stream_wrappers));
- zend_hash_init(FG(stream_wrappers), zend_hash_num_elements(&url_stream_wrappers_hash), NULL, NULL, 0);
- zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL);
- }
- PHPAPI int php_register_url_stream_wrapper_volatile(zend_string *protocol, php_stream_wrapper *wrapper)
- {
- if (php_stream_wrapper_scheme_validate(ZSTR_VAL(protocol), ZSTR_LEN(protocol)) == FAILURE) {
- return FAILURE;
- }
- if (!FG(stream_wrappers)) {
- clone_wrapper_hash();
- }
- return zend_hash_add_ptr(FG(stream_wrappers), protocol, wrapper) ? SUCCESS : FAILURE;
- }
- PHPAPI int php_unregister_url_stream_wrapper_volatile(zend_string *protocol)
- {
- if (!FG(stream_wrappers)) {
- clone_wrapper_hash();
- }
- return zend_hash_del(FG(stream_wrappers), protocol);
- }
- PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options)
- {
- HashTable *wrapper_hash = (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
- php_stream_wrapper *wrapper = NULL;
- const char *p, *protocol = NULL;
- size_t n = 0;
- if (path_for_open) {
- *path_for_open = (char*)path;
- }
- if (options & IGNORE_URL) {
- return (php_stream_wrapper*)((options & STREAM_LOCATE_WRAPPERS_ONLY) ? NULL : &php_plain_files_wrapper);
- }
- for (p = path; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++) {
- n++;
- }
- if ((*p == ':') && (n > 1) && (!strncmp("//", p+1, 2) || (n == 4 && !memcmp("data:", path, 5)))) {
- protocol = path;
- }
- if (protocol) {
- if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, protocol, n))) {
- char *tmp = estrndup(protocol, n);
- php_strtolower(tmp, n);
- if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, tmp, n))) {
- char wrapper_name[32];
- if (n >= sizeof(wrapper_name)) {
- n = sizeof(wrapper_name) - 1;
- }
- PHP_STRLCPY(wrapper_name, protocol, sizeof(wrapper_name), n);
- php_error_docref(NULL, E_WARNING, "Unable to find the wrapper \"%s\" - did you forget to enable it when you configured PHP?", wrapper_name);
- wrapper = NULL;
- protocol = NULL;
- }
- efree(tmp);
- }
- }
-
- if (!protocol || !strncasecmp(protocol, "file", n)) {
-
- php_stream_wrapper *plain_files_wrapper = (php_stream_wrapper*)&php_plain_files_wrapper;
- if (protocol) {
- int localhost = 0;
- if (!strncasecmp(path, "file://localhost/", 17)) {
- localhost = 1;
- }
- #ifdef PHP_WIN32
- if (localhost == 0 && path[n+3] != '\0' && path[n+3] != '/' && path[n+4] != ':') {
- #else
- if (localhost == 0 && path[n+3] != '\0' && path[n+3] != '/') {
- #endif
- if (options & REPORT_ERRORS) {
- php_error_docref(NULL, E_WARNING, "remote host file access not supported, %s", path);
- }
- return NULL;
- }
- if (path_for_open) {
-
- *path_for_open = (char*)path + n + 1;
- if (localhost == 1) {
- (*path_for_open) += 11;
- }
- while (*(++*path_for_open)=='/') {
-
- }
- #ifdef PHP_WIN32
- if (*(*path_for_open + 1) != ':')
- #endif
- (*path_for_open)--;
- }
- }
- if (options & STREAM_LOCATE_WRAPPERS_ONLY) {
- return NULL;
- }
- if (FG(stream_wrappers)) {
-
- if (wrapper) {
-
- return wrapper;
- }
-
- if ((wrapper = zend_hash_find_ex_ptr(wrapper_hash, ZSTR_KNOWN(ZEND_STR_FILE), 1)) != NULL) {
- return wrapper;
- }
- if (options & REPORT_ERRORS) {
- php_error_docref(NULL, E_WARNING, "file:// wrapper is disabled in the server configuration");
- }
- return NULL;
- }
- return plain_files_wrapper;
- }
- if (wrapper && wrapper->is_url &&
- (options & STREAM_DISABLE_URL_PROTECTION) == 0 &&
- (!PG(allow_url_fopen) ||
- (((options & STREAM_OPEN_FOR_INCLUDE) ||
- PG(in_user_include)) && !PG(allow_url_include)))) {
- if (options & REPORT_ERRORS) {
-
- if (!PG(allow_url_fopen)) {
- php_error_docref(NULL, E_WARNING, "%.*s:// wrapper is disabled in the server configuration by allow_url_fopen=0", (int)n, protocol);
- } else {
- php_error_docref(NULL, E_WARNING, "%.*s:// wrapper is disabled in the server configuration by allow_url_include=0", (int)n, protocol);
- }
- }
- return NULL;
- }
- return wrapper;
- }
- PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream_context *context)
- {
- php_stream_wrapper *wrapper = NULL;
- wrapper = php_stream_locate_url_wrapper(path, NULL, 0);
- if (!wrapper || !wrapper->wops || !wrapper->wops->stream_mkdir) {
- return 0;
- }
- return wrapper->wops->stream_mkdir(wrapper, path, mode, options, context);
- }
- PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context)
- {
- php_stream_wrapper *wrapper = NULL;
- wrapper = php_stream_locate_url_wrapper(path, NULL, 0);
- if (!wrapper || !wrapper->wops || !wrapper->wops->stream_rmdir) {
- return 0;
- }
- return wrapper->wops->stream_rmdir(wrapper, path, options, context);
- }
- PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf *ssb, php_stream_context *context)
- {
- php_stream_wrapper *wrapper = NULL;
- const char *path_to_open = path;
- int ret;
- memset(ssb, 0, sizeof(*ssb));
- if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
-
- if (flags & PHP_STREAM_URL_STAT_LINK) {
- if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
- memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
- return 0;
- }
- } else {
- if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
- memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
- return 0;
- }
- }
- }
- wrapper = php_stream_locate_url_wrapper(path, &path_to_open, 0);
- if (wrapper && wrapper->wops->url_stat) {
- ret = wrapper->wops->url_stat(wrapper, path_to_open, flags, ssb, context);
- if (ret == 0) {
- if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
-
- if (flags & PHP_STREAM_URL_STAT_LINK) {
- if (BG(CurrentLStatFile)) {
- efree(BG(CurrentLStatFile));
- }
- BG(CurrentLStatFile) = estrdup(path);
- memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
- } else {
- if (BG(CurrentStatFile)) {
- efree(BG(CurrentStatFile));
- }
- BG(CurrentStatFile) = estrdup(path);
- memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
- }
- }
- }
- return ret;
- }
- return -1;
- }
- PHPAPI php_stream *_php_stream_opendir(const char *path, int options,
- php_stream_context *context STREAMS_DC)
- {
- php_stream *stream = NULL;
- php_stream_wrapper *wrapper = NULL;
- const char *path_to_open;
- if (!path || !*path) {
- return NULL;
- }
- path_to_open = path;
- wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options);
- if (wrapper && wrapper->wops->dir_opener) {
- stream = wrapper->wops->dir_opener(wrapper,
- path_to_open, "r", options ^ REPORT_ERRORS, NULL,
- context STREAMS_REL_CC);
- if (stream) {
- stream->wrapper = wrapper;
- stream->flags |= PHP_STREAM_FLAG_NO_BUFFER | PHP_STREAM_FLAG_IS_DIR;
- }
- } else if (wrapper) {
- php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS, "not implemented");
- }
- if (stream == NULL && (options & REPORT_ERRORS)) {
- php_stream_display_wrapper_errors(wrapper, path, "failed to open dir");
- }
- php_stream_tidy_wrapper_error_log(wrapper);
- return stream;
- }
- PHPAPI php_stream_dirent *_php_stream_readdir(php_stream *dirstream, php_stream_dirent *ent)
- {
- if (sizeof(php_stream_dirent) == php_stream_read(dirstream, (char*)ent, sizeof(php_stream_dirent))) {
- return ent;
- }
- return NULL;
- }
- PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options,
- zend_string **opened_path, php_stream_context *context STREAMS_DC)
- {
- php_stream *stream = NULL;
- php_stream_wrapper *wrapper = NULL;
- const char *path_to_open;
- int persistent = options & STREAM_OPEN_PERSISTENT;
- zend_string *resolved_path = NULL;
- char *copy_of_path = NULL;
- if (opened_path) {
- *opened_path = NULL;
- }
- if (!path || !*path) {
- php_error_docref(NULL, E_WARNING, "Filename cannot be empty");
- return NULL;
- }
- if (options & USE_PATH) {
- resolved_path = zend_resolve_path(path, strlen(path));
- if (resolved_path) {
- path = ZSTR_VAL(resolved_path);
-
- options |= STREAM_ASSUME_REALPATH;
- options &= ~USE_PATH;
- }
- }
- path_to_open = path;
- wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options);
- if (options & STREAM_USE_URL && (!wrapper || !wrapper->is_url)) {
- php_error_docref(NULL, E_WARNING, "This function may only be used against URLs");
- if (resolved_path) {
- zend_string_release_ex(resolved_path, 0);
- }
- return NULL;
- }
- if (wrapper) {
- if (!wrapper->wops->stream_opener) {
- php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS,
- "wrapper does not support stream open");
- } else {
- stream = wrapper->wops->stream_opener(wrapper,
- path_to_open, mode, options ^ REPORT_ERRORS,
- opened_path, context STREAMS_REL_CC);
- }
-
- if (stream && (options & STREAM_OPEN_PERSISTENT) && !stream->is_persistent) {
- php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS,
- "wrapper does not support persistent streams");
- php_stream_close(stream);
- stream = NULL;
- }
- if (stream) {
- stream->wrapper = wrapper;
- }
- }
- if (stream) {
- if (opened_path && !*opened_path && resolved_path) {
- *opened_path = resolved_path;
- resolved_path = NULL;
- }
- if (stream->orig_path) {
- pefree(stream->orig_path, persistent);
- }
- copy_of_path = pestrdup(path, persistent);
- stream->orig_path = copy_of_path;
- #if ZEND_DEBUG
- stream->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename;
- stream->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno;
- #endif
- }
- if (stream != NULL && (options & STREAM_MUST_SEEK)) {
- php_stream *newstream;
- switch(php_stream_make_seekable_rel(stream, &newstream,
- (options & STREAM_WILL_CAST)
- ? PHP_STREAM_PREFER_STDIO : PHP_STREAM_NO_PREFERENCE)) {
- case PHP_STREAM_UNCHANGED:
- if (resolved_path) {
- zend_string_release_ex(resolved_path, 0);
- }
- return stream;
- case PHP_STREAM_RELEASED:
- if (newstream->orig_path) {
- pefree(newstream->orig_path, persistent);
- }
- newstream->orig_path = pestrdup(path, persistent);
- if (resolved_path) {
- zend_string_release_ex(resolved_path, 0);
- }
- return newstream;
- default:
- php_stream_close(stream);
- stream = NULL;
- if (options & REPORT_ERRORS) {
- char *tmp = estrdup(path);
- php_strip_url_passwd(tmp);
- php_error_docref1(NULL, tmp, E_WARNING, "could not make seekable - %s",
- tmp);
- efree(tmp);
- options ^= REPORT_ERRORS;
- }
- }
- }
- if (stream && stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && strchr(mode, 'a') && stream->position == 0) {
- zend_off_t newpos = 0;
-
- if (0 == stream->ops->seek(stream, 0, SEEK_CUR, &newpos)) {
- stream->position = newpos;
- }
- }
- if (stream == NULL && (options & REPORT_ERRORS)) {
- php_stream_display_wrapper_errors(wrapper, path, "failed to open stream");
- if (opened_path && *opened_path) {
- zend_string_release_ex(*opened_path, 0);
- *opened_path = NULL;
- }
- }
- php_stream_tidy_wrapper_error_log(wrapper);
- #if ZEND_DEBUG
- if (stream == NULL && copy_of_path != NULL) {
- pefree(copy_of_path, persistent);
- }
- #endif
- if (resolved_path) {
- zend_string_release_ex(resolved_path, 0);
- }
- return stream;
- }
- PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context)
- {
- php_stream_context *oldcontext = PHP_STREAM_CONTEXT(stream);
- if (context) {
- stream->ctx = context->res;
- GC_ADDREF(context->res);
- } else {
- stream->ctx = NULL;
- }
- if (oldcontext) {
- zend_list_delete(oldcontext->res);
- }
- return oldcontext;
- }
- PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity,
- char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr)
- {
- if (context && context->notifier)
- context->notifier->func(context, notifycode, severity, xmsg, xcode, bytes_sofar, bytes_max, ptr);
- }
- PHPAPI void php_stream_context_free(php_stream_context *context)
- {
- if (Z_TYPE(context->options) != IS_UNDEF) {
- zval_ptr_dtor(&context->options);
- ZVAL_UNDEF(&context->options);
- }
- if (context->notifier) {
- php_stream_notification_free(context->notifier);
- context->notifier = NULL;
- }
- efree(context);
- }
- PHPAPI php_stream_context *php_stream_context_alloc(void)
- {
- php_stream_context *context;
- context = ecalloc(1, sizeof(php_stream_context));
- context->notifier = NULL;
- array_init(&context->options);
- context->res = zend_register_resource(context, php_le_stream_context());
- return context;
- }
- PHPAPI php_stream_notifier *php_stream_notification_alloc(void)
- {
- return ecalloc(1, sizeof(php_stream_notifier));
- }
- PHPAPI void php_stream_notification_free(php_stream_notifier *notifier)
- {
- if (notifier->dtor) {
- notifier->dtor(notifier);
- }
- efree(notifier);
- }
- PHPAPI zval *php_stream_context_get_option(php_stream_context *context,
- const char *wrappername, const char *optionname)
- {
- zval *wrapperhash;
- if (NULL == (wrapperhash = zend_hash_str_find(Z_ARRVAL(context->options), wrappername, strlen(wrappername)))) {
- return NULL;
- }
- return zend_hash_str_find(Z_ARRVAL_P(wrapperhash), optionname, strlen(optionname));
- }
- PHPAPI int php_stream_context_set_option(php_stream_context *context,
- const char *wrappername, const char *optionname, zval *optionvalue)
- {
- zval *wrapperhash;
- zval category;
- SEPARATE_ARRAY(&context->options);
- wrapperhash = zend_hash_str_find(Z_ARRVAL(context->options), wrappername, strlen(wrappername));
- if (NULL == wrapperhash) {
- array_init(&category);
- wrapperhash = zend_hash_str_update(Z_ARRVAL(context->options), (char*)wrappername, strlen(wrappername), &category);
- }
- ZVAL_DEREF(optionvalue);
- Z_TRY_ADDREF_P(optionvalue);
- SEPARATE_ARRAY(wrapperhash);
- zend_hash_str_update(Z_ARRVAL_P(wrapperhash), optionname, strlen(optionname), optionvalue);
- return SUCCESS;
- }
- PHPAPI int php_stream_dirent_alphasort(const zend_string **a, const zend_string **b)
- {
- return strcoll(ZSTR_VAL(*a), ZSTR_VAL(*b));
- }
- PHPAPI int php_stream_dirent_alphasortr(const zend_string **a, const zend_string **b)
- {
- return strcoll(ZSTR_VAL(*b), ZSTR_VAL(*a));
- }
- PHPAPI int _php_stream_scandir(const char *dirname, zend_string **namelist[], int flags, php_stream_context *context,
- int (*compare) (const zend_string **a, const zend_string **b))
- {
- php_stream *stream;
- php_stream_dirent sdp;
- zend_string **vector = NULL;
- unsigned int vector_size = 0;
- unsigned int nfiles = 0;
- if (!namelist) {
- return FAILURE;
- }
- stream = php_stream_opendir(dirname, REPORT_ERRORS, context);
- if (!stream) {
- return FAILURE;
- }
- while (php_stream_readdir(stream, &sdp)) {
- if (nfiles == vector_size) {
- if (vector_size == 0) {
- vector_size = 10;
- } else {
- if(vector_size*2 < vector_size) {
-
- php_stream_closedir(stream);
- efree(vector);
- return FAILURE;
- }
- vector_size *= 2;
- }
- vector = (zend_string **) safe_erealloc(vector, vector_size, sizeof(char *), 0);
- }
- vector[nfiles] = zend_string_init(sdp.d_name, strlen(sdp.d_name), 0);
- nfiles++;
- if(vector_size < 10 || nfiles == 0) {
-
- php_stream_closedir(stream);
- efree(vector);
- return FAILURE;
- }
- }
- php_stream_closedir(stream);
- *namelist = vector;
- if (nfiles > 0 && compare) {
- qsort(*namelist, nfiles, sizeof(zend_string *), (int(*)(const void *, const void *))compare);
- }
- return nfiles;
- }
|