--- embedaddon/php/ext/mbstring/libmbfl/filters/mbfilter_utf8.c 2012/02/21 23:47:57 1.1.1.1 +++ embedaddon/php/ext/mbstring/libmbfl/filters/mbfilter_utf8.c 2013/10/14 08:02:22 1.1.1.3 @@ -34,25 +34,25 @@ #include "mbfilter.h" #include "mbfilter_utf8.h" -static int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter); +int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter); -static const unsigned char mblen_table_utf8[] = { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 +const unsigned char mblen_table_utf8[] = { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; static const char *mbfl_encoding_utf8_aliases[] = {"utf8", NULL}; @@ -79,7 +79,7 @@ const struct mbfl_convert_vtbl vtbl_utf8_wchar = { mbfl_filt_conv_common_ctor, mbfl_filt_conv_common_dtor, mbfl_filt_conv_utf8_wchar, - mbfl_filt_conv_common_flush + mbfl_filt_conv_utf8_wchar_flush }; const struct mbfl_convert_vtbl vtbl_wchar_utf8 = { @@ -93,91 +93,128 @@ const struct mbfl_convert_vtbl vtbl_wchar_utf8 = { #define CK(statement) do { if ((statement) < 0) return (-1); } while (0) +int mbfl_filt_put_invalid_char(int c, mbfl_convert_filter *filter) +{ + int w; + w = c & MBFL_WCSGROUP_MASK; + w |= MBFL_WCSGROUP_THROUGH; + filter->status = 0; + filter->cache = 0; + CK((*filter->output_function)(w, filter->data)); +} + + /* * UTF-8 => wchar */ int mbfl_filt_conv_utf8_wchar(int c, mbfl_convert_filter *filter) { - int s; + int s, c1, w = 0, flag = 0; - if (c < 0x80) { - if (c >= 0) { +retry: + switch (filter->status & 0xff) { + case 0x00: + if (c < 0x80) { CK((*filter->output_function)(c, filter->data)); + } else if (c >= 0xc2 && c <= 0xdf) { /* 2byte code first char: 0xc2-0xdf */ + filter->status = 0x10; + filter->cache = c & 0x1f; + } else if (c >= 0xe0 && c <= 0xef) { /* 3byte code first char: 0xe0-0xef */ + filter->status = 0x20; + filter->cache = c & 0xf; + } else if (c >= 0xf0 && c <= 0xf4) { /* 3byte code first char: 0xf0-0xf4 */ + filter->status = 0x30; + filter->cache = c & 0x7; + } else { + mbfl_filt_put_invalid_char(c, filter); } + break; + case 0x10: /* 2byte code 2nd char: 0x80-0xbf */ + case 0x21: /* 3byte code 3rd char: 0x80-0xbf */ + case 0x32: /* 4byte code 4th char: 0x80-0xbf */ filter->status = 0; - } else if (c < 0xc0) { - int status = filter->status & 0xff; - switch (status) { - case 0x10: /* 2byte code 2nd char */ - case 0x21: /* 3byte code 3rd char */ - case 0x32: /* 4byte code 4th char */ - case 0x43: /* 5byte code 5th char */ - case 0x54: /* 6byte code 6th char */ - filter->status = 0; - s = filter->cache | (c & 0x3f); - if ((status == 0x10 && s >= 0x80) || - (status == 0x21 && s >= 0x800 && (s < 0xd800 || s > 0xdfff)) || - (status == 0x32 && s >= 0x10000) || - (status == 0x43 && s >= 0x200000) || - (status == 0x54 && s >= 0x4000000 && s < MBFL_WCSGROUP_UCS4MAX)) { - CK((*filter->output_function)(s, filter->data)); - } - break; - case 0x20: /* 3byte code 2nd char */ - case 0x31: /* 4byte code 3rd char */ - case 0x42: /* 5byte code 4th char */ - case 0x53: /* 6byte code 5th char */ - filter->cache |= ((c & 0x3f) << 6); + if (c >= 0x80 && c <= 0xbf) { + s = (filter->cache<<6) | (c & 0x3f); + filter->cache = 0; + CK((*filter->output_function)(s, filter->data)); + } else { + mbfl_filt_put_invalid_char(filter->cache, filter); + goto retry; + } + break; + case 0x20: /* 3byte code 2nd char: 0:0xa0-0xbf,D:0x80-9F,1-C,E-F:0x80-0x9f */ + s = (filter->cache<<6) | (c & 0x3f); + c1 = filter->cache & 0xf; + + if ((c >= 0x80 && c <= 0xbf) && + ((c1 == 0x0 && c >= 0xa0) || + (c1 == 0xd && c < 0xa0) || + (c1 > 0x0 && c1 != 0xd))) { + filter->cache = s; filter->status++; - break; - case 0x30: /* 4byte code 2nd char */ - case 0x41: /* 5byte code 3rd char */ - case 0x52: /* 6byte code 4th char */ - filter->cache |= ((c & 0x3f) << 12); + } else { + mbfl_filt_put_invalid_char(filter->cache, filter); + goto retry; + } + break; + case 0x30: /* 4byte code 2nd char: 0:0x90-0xbf,1-3:0x80-0xbf,4:0x80-0x8f */ + s = (filter->cache<<6) | (c & 0x3f); + c1 = filter->cache & 0x7; + + if ((c >= 0x80 && c <= 0xbf) && + ((c1 == 0x0 && c >= 0x90) || + (c1 == 0x4 && c < 0x90) || + (c1 > 0x0 && c1 != 0x4))) { + filter->cache = s; filter->status++; - break; - case 0x40: /* 5byte code 2nd char */ - case 0x51: /* 6byte code 3rd char */ - filter->cache |= ((c & 0x3f) << 18); + } else { + mbfl_filt_put_invalid_char(filter->cache, filter); + goto retry; + } + break; + case 0x31: /* 4byte code 3rd char: 0x80-0xbf */ + if (c >= 0x80 && c <= 0xbf) { + filter->cache = (filter->cache<<6) | (c & 0x3f); filter->status++; - break; - case 0x50: /* 6byte code 2nd char */ - filter->cache |= ((c & 0x3f) << 24); - filter->status++; - break; - default: - filter->status = 0; - break; + } else { + mbfl_filt_put_invalid_char(filter->cache, filter); + goto retry; } - } else if (c < 0xe0) { /* 2byte code first char */ - filter->status = 0x10; - filter->cache = (c & 0x1f) << 6; - } else if (c < 0xf0) { /* 3byte code first char */ - filter->status = 0x20; - filter->cache = (c & 0xf) << 12; - } else if (c < 0xf8) { /* 4byte code first char */ - filter->status = 0x30; - filter->cache = (c & 0x7) << 18; - } else if (c < 0xfc) { /* 5byte code first char */ - filter->status = 0x40; - filter->cache = (c & 0x3) << 24; - } else if (c < 0xfe) { /* 6 byte code first char */ - filter->status = 0x50; - filter->cache = (c & 0x1) << 30; - } else { + break; + default: filter->status = 0; - filter->cache = 0; + break; } return c; } +int mbfl_filt_conv_utf8_wchar_flush(mbfl_convert_filter *filter) +{ + int status, cache; + + status = filter->status; + cache = filter->cache; + + filter->status = 0; + filter->cache = 0; + + if (status != 0) { + mbfl_filt_put_invalid_char(cache, filter); + } + + if (filter->flush_function != NULL) { + (*filter->flush_function)(filter->data); + } + return 0; +} + /* * wchar => UTF-8 */ int mbfl_filt_conv_wchar_utf8(int c, mbfl_convert_filter *filter) { - if (c >= 0 && c < MBFL_WCSGROUP_UCS4MAX) { + if (c >= 0 && c < 0x110000) { if (c < 0x80) { CK((*filter->output_function)(c, filter->data)); } else if (c < 0x800) { @@ -187,24 +224,11 @@ int mbfl_filt_conv_wchar_utf8(int c, mbfl_convert_filt CK((*filter->output_function)(((c >> 12) & 0x0f) | 0xe0, filter->data)); CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data)); CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data)); - } else if (c < 0x200000) { + } else { CK((*filter->output_function)(((c >> 18) & 0x07) | 0xf0, filter->data)); CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data)); CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data)); CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data)); - } else if (c < 0x4000000) { - CK((*filter->output_function)(((c >> 24) & 0x03) | 0xf8, filter->data)); - CK((*filter->output_function)(((c >> 18) & 0x3f) | 0x80, filter->data)); - CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data)); - CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data)); - CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data)); - } else { - CK((*filter->output_function)(((c >> 30) & 0x01) | 0xfc, filter->data)); - CK((*filter->output_function)(((c >> 24) & 0x3f) | 0x80, filter->data)); - CK((*filter->output_function)(((c >> 18) & 0x3f) | 0x80, filter->data)); - CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data)); - CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data)); - CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data)); } } else { if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) { @@ -215,8 +239,13 @@ int mbfl_filt_conv_wchar_utf8(int c, mbfl_convert_filt return c; } -static int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter) +int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter) { + int c1; + + c1 = (filter->status >> 8) & 0xff; + filter->status &= 0xff; + if (c < 0x80) { if (c < 0) { filter->flag = 1; /* bad */ @@ -227,22 +256,31 @@ static int mbfl_filt_ident_utf8(int c, mbfl_identify_f } else if (c < 0xc0) { switch (filter->status) { case 0x20: /* 3 byte code 2nd char */ + if ((c1 == 0x0 && c >= 0xa0) || + (c1 == 0xd && c < 0xa0) || + (c1 > 0x0 && c1 != 0xd)) { + filter->status++; + } else { + filter->flag = 1; /* bad */ + filter->status = 0; + } + break; case 0x30: /* 4 byte code 2nd char */ + if ((c1 == 0x0 && c >= 0x90) || + (c1 > 0x0 && c1 < 0x4) || + (c1 == 0x4 && c < 0x90)) { + filter->status++; + } else { + filter->flag = 1; /* bad */ + filter->status = 0; + } + break; case 0x31: /* 4 byte code 3rd char */ - case 0x40: /* 5 byte code 2nd char */ - case 0x41: /* 5 byte code 3rd char */ - case 0x42: /* 5 byte code 4th char */ - case 0x50: /* 6 byte code 2nd char */ - case 0x51: /* 6 byte code 3rd char */ - case 0x52: /* 6 byte code 4th char */ - case 0x53: /* 6 byte code 5th char */ filter->status++; break; case 0x10: /* 2 byte code 2nd char */ case 0x21: /* 3 byte code 3rd char */ case 0x32: /* 4 byte code 4th char */ - case 0x43: /* 5 byte code 5th char */ - case 0x54: /* 6 byte code 6th char */ filter->status = 0; break; default: @@ -250,6 +288,9 @@ static int mbfl_filt_ident_utf8(int c, mbfl_identify_f filter->status = 0; break; } + } else if (c < 0xc2) { /* 0xc0,0xc1 */ + filter->flag = 1; /* bad */ + filter->status = 0; } else { if (filter->status) { filter->flag = 1; /* bad */ @@ -259,12 +300,10 @@ static int mbfl_filt_ident_utf8(int c, mbfl_identify_f filter->status = 0x10; } else if (c < 0xf0) { /* 3 byte code 1st char */ filter->status = 0x20; - } else if (c < 0xf8) { /* 4 byte code 1st char */ + filter->status |= (c & 0xf) << 8; + } else if (c < 0xf5) { /* 4 byte code 1st char */ filter->status = 0x30; - } else if (c < 0xfc) { /* 5 byte code 1st char */ - filter->status = 0x40; - } else if (c < 0xfe) { /* 6 byte code 1st char */ - filter->status = 0x50; + filter->status |= (c & 0x7) << 8; } else { filter->flag = 1; /* bad */ }