Commit 753a372b authored by Chen Bill's avatar Chen Bill

DecodeUTF8: check array size

parent 81d0ef16
...@@ -110,20 +110,33 @@ public: ...@@ -110,20 +110,33 @@ public:
return pstr - str; return pstr - str;
} }
// UTF-8 to UTF-16/UTF-32 // UTF-8 to UTF-16/UTF-32
static int DecodeUTF8(const char * src, wchar_t * wstr) { template<size_t N>
static int DecodeUTF8(const char* src, wchar_t(&wstr)[N]) {
const char* p = src; const char* p = src;
wchar_t* wp = wstr; wchar_t* wp = wstr;
while(*p != 0) { while(*p != 0) {
if(((unsigned)*p & 0x80) == 0) { const unsigned cur = (unsigned)*p & 0xff;
int codepoint_size = 0;
if ((cur & 0xf8) == 0xf0) {
if (sizeof(wchar_t) == 2)
codepoint_size = 2;
else
codepoint_size = 1;
}
else
codepoint_size = 1;
if (wp - wstr + codepoint_size > N - 1)
break;
if((cur & 0x80) == 0) {
*wp = *p; *wp = *p;
p++; p++;
} else if(((unsigned)*p & 0xe0) == 0xc0) { } else if((cur & 0xe0) == 0xc0) {
*wp = (((unsigned)p[0] & 0x1f) << 6) | ((unsigned)p[1] & 0x3f); *wp = (((unsigned)p[0] & 0x1f) << 6) | ((unsigned)p[1] & 0x3f);
p += 2; p += 2;
} else if(((unsigned)*p & 0xf0) == 0xe0) { } else if((cur & 0xf0) == 0xe0) {
*wp = (((unsigned)p[0] & 0xf) << 12) | (((unsigned)p[1] & 0x3f) << 6) | ((unsigned)p[2] & 0x3f); *wp = (((unsigned)p[0] & 0xf) << 12) | (((unsigned)p[1] & 0x3f) << 6) | ((unsigned)p[2] & 0x3f);
p += 3; p += 3;
} else if(((unsigned)*p & 0xf8) == 0xf0) { } else if((cur & 0xf8) == 0xf0) {
if (sizeof(wchar_t) == 2) { if (sizeof(wchar_t) == 2) {
unsigned unicode = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f); unsigned unicode = (((unsigned)p[0] & 0x7) << 18) | (((unsigned)p[1] & 0x3f) << 12) | (((unsigned)p[2] & 0x3f) << 6) | ((unsigned)p[3] & 0x3f);
unicode -= 0x10000; unicode -= 0x10000;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment