fix #91: get rid of most of the yearly failing test
[m6w6/ext-http] / src / php_http_misc.h
index e8d900b5813848c48b95b57566c164dd8e7e689f..b91638e2098f27abb48c1abdd0bc53be3f58541a 100644 (file)
@@ -67,47 +67,7 @@ char *php_http_pretty_key(register char *key, size_t key_len, zend_bool uctitle,
 size_t php_http_boundary(char *buf, size_t len);
 int php_http_select_str(const char *cmp, int argc, ...);
 
-/* 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)
-{
-       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;
-}
+#define php_http_locate_str(h, h_len, n, n_len) zend_memnstr((h), (n), (n_len), (h)+(h_len))
 
 static inline const char *php_http_locate_eol(const char *line, int *eol_len)
 {
@@ -123,25 +83,61 @@ static inline const char *php_http_locate_bin_eol(const char *bin, size_t len, i
 {
        register const char *eol = bin;
 
-       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;
+       while (len--) {
+               if (UNEXPECTED(*eol == '\r' || *eol == '\n')) {
+                       if (EXPECTED(eol_len)) {
+                               *eol_len = (EXPECTED(eol[0] == '\r' && eol[1] == '\n') ? 2 : 1);
                        }
-                       ++eol;
-               );
+                       return eol;
+               }
+               ++eol;
        }
        return NULL;
 }
 
 /* ZEND */
 
-#ifdef PHP_DEBUG
+#if PHP_DEBUG
 #      undef  HASH_OF
-#      define HASH_OF(p) ((HashTable*)(Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties((p)) : NULL))))
+#      if PHP_VERSION_ID >= 70500
+#              define HASH_OF(p) ((HashTable*)(Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties(Z_OBJ_P(p)) : NULL))))
+#      else
+#              define HASH_OF(p) ((HashTable*)(Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties((p)) : NULL))))
+#      endif
+#endif
+
+#ifndef GC_SET_REFCOUNT
+#      define GC_SET_REFCOUNT(gc, rc) GC_REFCOUNT(gc) = rc
+#endif
+#ifndef GC_ADDREF
+#      define GC_ADDREF(gc) ++GC_REFCOUNT(gc)
+#endif
+#ifndef GC_DELREF
+#      define GC_DELREF(gc) --GC_REFCOUNT(gc)
+#endif
+
+#ifdef ZEND_HASH_GET_APPLY_COUNT
+#      define HT_IS_RECURSIVE(ht) (ZEND_HASH_GET_APPLY_COUNT(ht) > 0)
+#else
+#      define HT_IS_RECURSIVE(ht) GC_IS_RECURSIVE(ht)
+#endif
+#ifdef ZEND_HASH_INC_APPLY_COUNT
+#      define HT_PROTECT_RECURSION(ht) ZEND_HASH_INC_APPLY_COUNT(ht)
+#else
+#      define HT_PROTECT_RECURSION(ht) GC_PROTECT_RECURSION(ht)
+#endif
+#ifdef ZEND_HASH_DEC_APPLY_COUNT
+#      define HT_UNPROTECT_RECURSION(ht) ZEND_HASH_DEC_APPLY_COUNT(ht)
+#else
+#      define HT_UNPROTECT_RECURSION(ht) GC_UNPROTECT_RECURSION(ht)
+#endif
+
+#if PHP_VERSION_ID >= 70400
+#      define PHP_WRITE_PROP_HANDLER_TYPE zval *
+#      define PHP_WRITE_PROP_HANDLER_RETURN(v) return v
+#else
+#      define PHP_WRITE_PROP_HANDLER_TYPE void
+#      define PHP_WRITE_PROP_HANDLER_RETURN(v)
 #endif
 
 static inline void *PHP_HTTP_OBJ(zend_object *zo, zval *zv)
@@ -161,7 +157,7 @@ static inline zend_string *php_http_cs2zs(char *s, size_t l)
        str->len = l;
        str->h = 0;
 
-       GC_REFCOUNT(str) = 1;
+       GC_SET_REFCOUNT(str, 1);
        GC_TYPE_INFO(str) = IS_STRING;
 
        return str;