Commit 81d0ef16 authored by Chen Bill's avatar Chen Bill

EncodeUTF8: check array size

parent d58bb28a
...@@ -58,40 +58,56 @@ public: ...@@ -58,40 +58,56 @@ public:
return l; return l;
} }
// UTF-16/UTF-32 to UTF-8 // UTF-16/UTF-32 to UTF-8
static int EncodeUTF8(const wchar_t * wsrc, char * str) { template<size_t N>
static int EncodeUTF8(const wchar_t* wsrc, char(&str)[N]) {
char* pstr = str; char* pstr = str;
while(*wsrc != 0) { while (*wsrc != 0) {
unsigned cur = *wsrc; unsigned cur = *wsrc;
if(cur < 0x80) { int codepoint_size = 0;
*str = (char)cur; if (cur < 0x80U)
++str; codepoint_size = 1;
} else if(cur < 0x800) { else if (cur < 0x800U)
str[0] = ((cur >> 6) & 0x1f) | 0xc0; codepoint_size = 2;
str[1] = (cur & 0x3f) | 0x80; else if (cur < 0x10000U && (cur < 0xd800U || cur > 0xdfffU))
str += 2; codepoint_size = 3;
} else if(cur < 0x10000 && (cur < 0xd800 || cur > 0xdfff)) { else
str[0] = ((cur >> 12) & 0xf) | 0xe0; codepoint_size = 4;
str[1] = ((cur >> 6) & 0x3f) | 0x80; if (pstr - str + codepoint_size > N - 1)
str[2] = (cur & 0x3f) | 0x80; break;
str += 3; switch (codepoint_size) {
} else { case 1:
if (sizeof(wchar_t) == 2) { *pstr = (char)cur;
cur = 0; break;
cur |= ((unsigned)*wsrc & 0x3ff) << 10; case 2:
++wsrc; pstr[0] = ((cur >> 6) & 0x1f) | 0xc0;
cur |= (unsigned)*wsrc & 0x3ff; pstr[1] = (cur & 0x3f) | 0x80;
cur += 0x10000; break;
} case 3:
str[0] = ((cur >> 18) & 0x7) | 0xf0; pstr[0] = ((cur >> 12) & 0xf) | 0xe0;
str[1] = ((cur >> 12) & 0x3f) | 0x80; pstr[1] = ((cur >> 6) & 0x3f) | 0x80;
str[2] = ((cur >> 6) & 0x3f) | 0x80; pstr[2] = (cur & 0x3f) | 0x80;
str[3] = (cur & 0x3f) | 0x80; break;
str += 4; case 4:
if (sizeof(wchar_t) == 2) {
cur = 0;
cur |= ((unsigned)*wsrc & 0x3ff) << 10;
++wsrc;
cur |= (unsigned)*wsrc & 0x3ff;
cur += 0x10000;
}
pstr[0] = ((cur >> 18) & 0x7) | 0xf0;
pstr[1] = ((cur >> 12) & 0x3f) | 0x80;
pstr[2] = ((cur >> 6) & 0x3f) | 0x80;
pstr[3] = (cur & 0x3f) | 0x80;
break;
default:
break;
} }
pstr += codepoint_size;
wsrc++; wsrc++;
} }
*str = 0; *pstr = 0;
return str - pstr; 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) { static int DecodeUTF8(const char * src, wchar_t * wstr) {
......
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