X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_misc.h;h=8e901da3c135e546ebb8f7b6bb239be6756c8a92;hp=3759a1aa969f821c7334b92b1b93f6de3214d539;hb=046973e332c1475f9bf7717af9d33a75283deb37;hpb=4850e3ba188215f9d0cda9e7c0e7e011b340b7c7 diff --git a/php_http_misc.h b/php_http_misc.h index 3759a1a..8e901da 100644 --- a/php_http_misc.h +++ b/php_http_misc.h @@ -41,10 +41,10 @@ PHP_HTTP_API void php_http_sleep(double s); /* STRING UTILITIES */ -#ifndef STR_SET -# define STR_SET(STR, SET) \ +#ifndef PTR_SET +# define PTR_SET(STR, SET) \ { \ - STR_FREE(STR); \ + PTR_FREE(STR); \ STR = SET; \ } #endif @@ -60,27 +60,49 @@ PHP_HTTP_API void php_http_sleep(double s); #define PHP_HTTP_MATCH_STRICT (PHP_HTTP_MATCH_CASE|PHP_HTTP_MATCH_FULL) int php_http_match(const char *haystack, const char *needle, int flags); -char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen); +char *php_http_pretty_key(register char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen); size_t php_http_boundary(char *buf, size_t len TSRMLS_DC); int php_http_select_str(const char *cmp, int argc, ...); -static inline const char *php_http_locate_str(const char *h, size_t h_len, const char *n, size_t n_len) +/* See "A Reusable Duff Device" By Ralf Holly, August 01, 2005 */ +#define PHP_HTTP_DUFF_BREAK() times_=1 +#define PHP_HTTP_DUFF(c, a) do { \ + size_t count_ = (c); \ + size_t times_ = (count_ + 7) >> 3; \ + switch (count_ & 7){ \ + case 0: do { \ + a; \ + case 7: \ + a; \ + case 6: \ + a; \ + case 5: \ + a; \ + case 4: \ + a; \ + case 3: \ + a; \ + case 2: \ + a; \ + case 1: \ + a; \ + } while (--times_ > 0); \ + } \ +} while (0) + +static inline const char *php_http_locate_str(register const char *h, size_t h_len, const char *n, size_t n_len) { - const char *p, *e; - - if (n_len && h_len) { - e = h + h_len; - do { - if (*h == *n) { - for (p = n; *p == h[p-n]; ++p) { - if (p == n+n_len-1) { - return h; - } - } - } - } while (h++ != e); + if (!n_len || !h_len || h_len < n_len) { + return NULL; } + PHP_HTTP_DUFF(h_len - n_len + 1, + if (*h == *n && !strncmp(h + 1, n + 1, n_len - 1)) { + return h; + } + ++h; + ); + return NULL; } @@ -96,17 +118,19 @@ static inline const char *php_http_locate_eol(const char *line, int *eol_len) static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, int *eol_len) { - const char *eol; + register const char *eol = bin; - for (eol = bin; eol - bin < len; ++eol) { - if (*eol == '\r' || *eol == '\n') { - if (eol_len) { - *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1); + if (len > 0) { + PHP_HTTP_DUFF(len, + if (*eol == '\r' || *eol == '\n') { + if (eol_len) { + *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1); + } + return eol; } - return eol; - } + ++eol; + ); } - return NULL; } @@ -140,42 +164,37 @@ static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, i Z_ARRVAL_P(zv) = (ht); \ } -static inline zval *php_http_ztyp(int type, zval *z) +static inline zval *php_http_zconv(int type, zval *z) { - SEPARATE_ARG_IF_REF(z); - if (Z_TYPE_P(z) != type) { - switch (type) { - case IS_NULL: convert_to_null_ex(&z); break; - case IS_BOOL: convert_to_boolean_ex(&z); break; - case IS_LONG: convert_to_long_ex(&z); break; - case IS_DOUBLE: convert_to_double_ex(&z); break; - case IS_STRING: convert_to_string_ex(&z); break; - case IS_ARRAY: convert_to_array_ex(&z); break; - case IS_OBJECT: convert_to_object_ex(&z); break; - } + switch (type) { + case IS_NULL: convert_to_null_ex(&z); break; + case IS_BOOL: convert_to_boolean_ex(&z); break; + case IS_LONG: convert_to_long_ex(&z); break; + case IS_DOUBLE: convert_to_double_ex(&z); break; + case IS_STRING: convert_to_string_ex(&z); break; + case IS_ARRAY: convert_to_array_ex(&z); break; + case IS_OBJECT: convert_to_object_ex(&z); break; } return z; } +static inline zval *php_http_ztyp(int type, zval *z) +{ + SEPARATE_ARG_IF_REF(z); + return (Z_TYPE_P(z) == type) ? z : php_http_zconv(type, z); +} + static inline zval *php_http_zsep(zend_bool add_ref, int type, zval *z) { if (add_ref) { Z_ADDREF_P(z); } if (Z_TYPE_P(z) != type) { - switch (type) { - case IS_NULL: convert_to_null_ex(&z); break; - case IS_BOOL: convert_to_boolean_ex(&z); break; - case IS_LONG: convert_to_long_ex(&z); break; - case IS_DOUBLE: convert_to_double_ex(&z); break; - case IS_STRING: convert_to_string_ex(&z); break; - case IS_ARRAY: convert_to_array_ex(&z); break; - case IS_OBJECT: convert_to_object_ex(&z); break; - } + return php_http_zconv(type, z); } else { SEPARATE_ZVAL_IF_NOT_REF(&z); + return z; } - return z; } static inline STATUS php_http_ini_entry(const char *name_str, size_t name_len, const char **value_str, size_t *value_len, zend_bool orig TSRMLS_DC) @@ -246,6 +265,11 @@ static inline STATUS php_http_ini_entry(const char *name_str, size_t name_len, c #define PHP_RSHUTDOWN_CALL(func) PHP_RSHUTDOWN(func)(SHUTDOWN_FUNC_ARGS_PASSTHRU) /* ARRAYS */ + +#ifndef HASH_KEY_NON_EXISTENT +# define HASH_KEY_NON_EXISTENT HASH_KEY_NON_EXISTANT +#endif + PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...); typedef struct php_http_array_hashkey { @@ -267,7 +291,7 @@ static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t *ke static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t *key) { if (key->type != HASH_KEY_IS_STRING || key->dup) { - STR_FREE(key->str); + PTR_FREE(key->str); } }