static void *zend_file_cache_serialize_interned(zend_string *str, zend_file_cache_metainfo *info) { size_t len; void *ret; /* check if the same interned string was already stored */ ret = zend_shared_alloc_get_xlat_entry(str); if (ret) { return ret; } len = ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); ret = (void*)(info->str_size | Z_UL(1)); zend_shared_alloc_register_xlat_entry(str, ret); if (info->str_size + len > ZSTR_LEN((zend_string*)ZCG(mem))) { size_t new_len = info->str_size + len; ZCG(mem) = (void*)zend_string_realloc( (zend_string*)ZCG(mem), ((_ZSTR_HEADER_SIZE + 1 + new_len + 4095) & ~0xfff) - (_ZSTR_HEADER_SIZE + 1), 0); } memcpy(ZSTR_VAL((zend_string*)ZCG(mem)) + info->str_size, str, len); info->str_size += len; return ret; }
/* {{{ php_escape_shell_arg */ PHPAPI zend_string *php_escape_shell_arg(char *str) { int x, y = 0, l = (int)strlen(str); zend_string *cmd; size_t estimate = (4 * l) + 3; cmd = zend_string_alloc(4 * l + 2, 0); /* worst case */ #ifdef PHP_WIN32 cmd->val[y++] = '"'; #else cmd->val[y++] = '\''; #endif for (x = 0; x < l; x++) { int mb_len = php_mblen(str + x, (l - x)); /* skip non-valid multibyte characters */ if (mb_len < 0) { continue; } else if (mb_len > 1) { memcpy(cmd->val + y, str + x, mb_len); y += mb_len; x += mb_len - 1; continue; } switch (str[x]) { #ifdef PHP_WIN32 case '"': case '%': cmd->val[y++] = ' '; break; #else case '\'': cmd->val[y++] = '\''; cmd->val[y++] = '\\'; cmd->val[y++] = '\''; #endif /* fall-through */ default: cmd->val[y++] = str[x]; } } #ifdef PHP_WIN32 cmd->val[y++] = '"'; #else cmd->val[y++] = '\''; #endif cmd->val[y] = '\0'; if ((estimate - y) > 4096) { /* realloc if the estimate was way overill * Arbitrary cutoff point of 4096 */ cmd = zend_string_realloc(cmd, y, 0); } cmd->len = y; return cmd; }
/* {{{ php_zlib_encode() */ static zend_string *php_zlib_encode(const char *in_buf, size_t in_len, int encoding, int level) { int status; z_stream Z; zend_string *out; memset(&Z, 0, sizeof(z_stream)); Z.zalloc = php_zlib_alloc; Z.zfree = php_zlib_free; if (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) { out = zend_string_alloc(PHP_ZLIB_BUFFER_SIZE_GUESS(in_len), 0); Z.next_in = (Bytef *) in_buf; Z.next_out = (Bytef *) out->val; Z.avail_in = in_len; Z.avail_out = out->len; status = deflate(&Z, Z_FINISH); deflateEnd(&Z); if (Z_STREAM_END == status) { /* size buffer down to actual length */ out = zend_string_realloc(out, Z.total_out, 0); out->val[out->len] = '\0'; return out; } else { zend_string_free(out); } } php_error_docref(NULL, E_WARNING, "%s", zError(status)); return NULL; }
/* {{{ php_escape_shell_cmd Escape all chars that could possibly be used to break out of a shell command This function emalloc's a string and returns the pointer. Remember to efree it when done with it. *NOT* safe for binary strings */ PHPAPI zend_string *php_escape_shell_cmd(char *str) { register int x, y, l = (int)strlen(str); size_t estimate = (2 * l) + 1; zend_string *cmd; #ifndef PHP_WIN32 char *p = NULL; #endif cmd = zend_string_alloc(2 * l, 0); for (x = 0, y = 0; x < l; x++) { int mb_len = php_mblen(str + x, (l - x)); /* skip non-valid multibyte characters */ if (mb_len < 0) { continue; } else if (mb_len > 1) { memcpy(cmd->val + y, str + x, mb_len); y += mb_len; x += mb_len - 1; continue; } switch (str[x]) { #ifndef PHP_WIN32 case '"': case '\'': if (!p && (p = memchr(str + x + 1, str[x], l - x - 1))) { /* noop */ } else if (p && *p == str[x]) { p = NULL; } else { cmd->val[y++] = '\\'; } cmd->val[y++] = str[x]; break; #else /* % is Windows specific for environmental variables, ^%PATH% will output PATH whil ^%PATH^% not. escapeshellcmd->val will escape all %. */ case '%': case '"': case '\'': #endif case '#': /* This is character-set independent */ case '&': case ';': case '`': case '|': case '*': case '?': case '~': case '<': case '>': case '^': case '(': case ')': case '[': case ']': case '{': case '}': case '$': case '\\': case '\x0A': /* excluding these two */ case '\xFF': #ifdef PHP_WIN32 cmd->val[y++] = '^'; #else cmd->val[y++] = '\\'; #endif /* fall-through */ default: cmd->val[y++] = str[x]; } } cmd->val[y] = '\0'; if ((estimate - y) > 4096) { /* realloc if the estimate was way overill * Arbitrary cutoff point of 4096 */ cmd = zend_string_realloc(cmd, y, 0); } cmd->len = y; return cmd; }
/* {{{ * Gets the value from ICU * common code shared by get_primary_language,get_script or get_region or get_variant * result = 0 if error, 1 if successful , -1 if no value */ static zend_string* get_icu_value_internal( const char* loc_name , char* tag_name, int* result , int fromParseLocale) { zend_string* tag_value = NULL; int32_t tag_value_len = 512; zend_off_t singletonPos = 0; char* mod_loc_name = NULL; zend_off_t grOffset = 0; int32_t buflen = 512; UErrorCode status = U_ZERO_ERROR; if( strcmp(tag_name, LOC_CANONICALIZE_TAG) != 0 ){ /* Handle grandfathered languages */ grOffset = findOffset( LOC_GRANDFATHERED , loc_name ); if( grOffset >= 0 ){ if( strcmp(tag_name , LOC_LANG_TAG)==0 ){ return zend_string_init(loc_name, strlen(loc_name), 0); } else { /* Since Grandfathered , no value , do nothing , retutn NULL */ return NULL; } } if( fromParseLocale==1 ){ /* Handle singletons */ if( strcmp(tag_name , LOC_LANG_TAG)==0 ){ if( strlen(loc_name)>1 && (isIDPrefix(loc_name) == 1) ){ return zend_string_init(loc_name, strlen(loc_name), 0); } } singletonPos = getSingletonPos( loc_name ); if( singletonPos == 0){ /* singleton at start of script, region , variant etc. * or invalid singleton at start of language */ return NULL; } else if( singletonPos > 0 ){ /* singleton at some position except at start * strip off the singleton and rest of the loc_name */ mod_loc_name = estrndup ( loc_name , singletonPos-1); } } /* end of if fromParse */ } /* end of if != LOC_CANONICAL_TAG */ if( mod_loc_name == NULL){ mod_loc_name = estrdup(loc_name ); } /* Proceed to ICU */ do{ if (tag_value) { tag_value = zend_string_realloc( tag_value , buflen, 0); } else { tag_value = zend_string_alloc( buflen, 0); } tag_value_len = buflen; if( strcmp(tag_name , LOC_SCRIPT_TAG)==0 ){ buflen = uloc_getScript ( mod_loc_name , tag_value->val , tag_value_len , &status); } if( strcmp(tag_name , LOC_LANG_TAG )==0 ){ buflen = uloc_getLanguage ( mod_loc_name , tag_value->val , tag_value_len , &status); } if( strcmp(tag_name , LOC_REGION_TAG)==0 ){ buflen = uloc_getCountry ( mod_loc_name , tag_value->val , tag_value_len , &status); } if( strcmp(tag_name , LOC_VARIANT_TAG)==0 ){ buflen = uloc_getVariant ( mod_loc_name , tag_value->val , tag_value_len , &status); } if( strcmp(tag_name , LOC_CANONICALIZE_TAG)==0 ){ buflen = uloc_canonicalize ( mod_loc_name , tag_value->val , tag_value_len , &status); } if( U_FAILURE( status ) ) { if( status == U_BUFFER_OVERFLOW_ERROR ) { status = U_ZERO_ERROR; buflen++; /* add space for \0 */ continue; } /* Error in retriving data */ *result = 0; if( tag_value ){ zend_string_release( tag_value ); } if( mod_loc_name ){ efree( mod_loc_name); } return NULL; } } while( buflen > tag_value_len ); if( buflen ==0 ){ /* No value found */ *result = -1; if( tag_value ){ zend_string_release( tag_value ); } if( mod_loc_name ){ efree( mod_loc_name); } return NULL; } else { *result = 1; } if( mod_loc_name ){ efree( mod_loc_name); } tag_value->len = strlen(tag_value->val); return tag_value; }