From b47a52f0a3db907095882f8ed9015a1bfc76537e Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 26 Aug 2014 17:51:14 +0200 Subject: [PATCH] simplify duff device usage --- php_http_misc.c | 2 +- php_http_misc.h | 68 +++++++++++++++++++++++-------------------------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/php_http_misc.c b/php_http_misc.c index 7afa006..8fcb82e 100644 --- a/php_http_misc.c +++ b/php_http_misc.c @@ -96,7 +96,7 @@ char *php_http_pretty_key(register char *key, size_t key_len, zend_bool uctitle, if ((wasalpha = PHP_HTTP_IS_CTYPE(alpha, key[0]))) { key[0] = (char) (uctitle ? PHP_HTTP_TO_CTYPE(upper, key[0]) : PHP_HTTP_TO_CTYPE(lower, key[0])); } - PHP_HTTP_DUFF(1, key_len, + PHP_HTTP_DUFF(key_len, if (PHP_HTTP_IS_CTYPE(alpha, key[i])) { key[i] = (char) (((!wasalpha) && uctitle) ? PHP_HTTP_TO_CTYPE(upper, key[i]) : PHP_HTTP_TO_CTYPE(lower, key[i])); wasalpha = 1; diff --git a/php_http_misc.h b/php_http_misc.h index d47477c..3bd77d8 100644 --- a/php_http_misc.h +++ b/php_http_misc.h @@ -65,48 +65,44 @@ size_t php_http_boundary(char *buf, size_t len TSRMLS_DC); 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(i) do { \ - times_##i = 1; \ -} while (0) - -#define PHP_HTTP_DUFF(i, c, a) do { \ - size_t count_##i = (c); \ - size_t times_##i = (count_##i + 7) >> 3; \ - switch (count_##i & 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_##i > 0); \ +#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) { - register const char *p1, *p2; - - if (n_len && h_len && h_len >= n_len) { - PHP_HTTP_DUFF(1, h_len - n_len + 1, - if (*h == *n) { - p1 = h; - p2 = n; - PHP_HTTP_DUFF(2, n_len, - if (*p1++ != *p2++) { - PHP_HTTP_DUFF_BREAK(2); - } else if (p2 == n + n_len - 1) { - return h; - } - ); - } - ++h; - ); + 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; } @@ -125,7 +121,7 @@ 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(1, len, + PHP_HTTP_DUFF(len, if (*eol == '\r' || *eol == '\n') { if (eol_len) { *eol_len = ((eol[0] == '\r' && eol[1] == '\n') ? 2 : 1); -- 2.30.2