X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_utf8.h;h=f9008032dff0eea98abc8c59cb1134b35ab3b7c3;hp=c7bcb49428c53a79234593390c3705d9ca791af5;hb=refs%2Fheads%2Fv2.4.x;hpb=caddd8ffdc0b23544671c64df711c04d3da1b4b4 diff --git a/php_http_utf8.h b/php_http_utf8.h index c7bcb49..f900803 100644 --- a/php_http_utf8.h +++ b/php_http_utf8.h @@ -555,7 +555,7 @@ static inline size_t utf8towc(unsigned *wc, const unsigned char *uc, size_t len) { unsigned char ub = utf8_mblen[*uc]; - if (!ub || ub > len || ub > 3) { + if (!ub || ub > len || ub > 4) { return 0; } @@ -595,9 +595,9 @@ static inline size_t utf8towc(unsigned *wc, const unsigned char *uc, size_t len) static inline zend_bool isualpha(unsigned ch) { - unsigned i, j; + unsigned i = 0, j; - for (i = 0; i < sizeof(utf8_ranges)/sizeof(utf8_range_t); ++i) { + PHP_HTTP_DUFF(sizeof(utf8_ranges)/sizeof(utf8_range_t), if (utf8_ranges[i].start == ch) { return 1; } else if (utf8_ranges[i].start <= ch && utf8_ranges[i].end >= ch) { @@ -611,7 +611,8 @@ static inline zend_bool isualpha(unsigned ch) } return 0; } - } + ++i; + ); return 0; } @@ -624,6 +625,39 @@ static inline zend_bool isualnum(unsigned ch) return isualpha(ch); } +static inline size_t wctoutf16(unsigned short u16[2], unsigned wc) +{ + if (wc > 0x10ffff || (wc >= 0xd800 && wc <= 0xdfff)) { + return 0; + } + + if (wc <= 0xffff) { + u16[0] = (unsigned short) wc; + return 1; + } + + wc -= 0x10000; + u16[0] = (unsigned short) ((wc >> 10) + 0xd800); + u16[1] = (unsigned short) ((wc & 0x3ff) + 0xdc00); + return 2; +} + +static inline size_t utf16towc(unsigned *wc, unsigned short *u16_str, size_t u16_len) +{ + if (u16_len < 1) { + return 0; + } + if (u16_str[0] - 0xd800 >= 0x800) { + *wc = u16_str[0]; + return 1; + } + if (u16_len < 2 || (u16_str[0] & 0xfffffc00) != 0xd800 || (u16_str[1] & 0xfffffc00) != 0xdc00) { + return 0; + } + *wc = (u16_str[0] << 10) + u16_str[1] - 0x35fdc00; + return 2; +} + #endif /* PHP_HTTP_UTF8_H */ /*