|
@@ -1474,15 +1474,7 @@ int utf8CharLen(unsigned char ch)
|
|
|
return len;
|
|
|
}
|
|
|
|
|
|
-bool checkValidUtf8Char(unsigned size, const unsigned char *ch)
|
|
|
-{
|
|
|
- for (unsigned pos = 1; pos < size; pos++)
|
|
|
- if ((ch[pos] < 128) || (ch[pos] >= 192))
|
|
|
- return false; //its not a valid utf-8 character
|
|
|
- return true;
|
|
|
-}
|
|
|
-
|
|
|
-int utf8CharLen(const unsigned char *ch)
|
|
|
+int utf8CharLen(const unsigned char *ch, unsigned maxsize)
|
|
|
{
|
|
|
//return 1 if this is an ascii character,
|
|
|
//or 0 if its not a valid utf-8 character
|
|
@@ -1490,8 +1482,11 @@ int utf8CharLen(const unsigned char *ch)
|
|
|
return 1;
|
|
|
|
|
|
unsigned char len = utf8CharLen(*ch);
|
|
|
- if (len && !checkValidUtf8Char(len, ch))
|
|
|
+ if (maxsize!=(unsigned)-1 && len>maxsize)
|
|
|
return 0;
|
|
|
+ for (unsigned pos = 1; pos < len; pos++)
|
|
|
+ if ((ch[pos] < 128) || (ch[pos] >= 192))
|
|
|
+ return 0; //its not a valid utf-8 character after all
|
|
|
|
|
|
return len;
|
|
|
}
|
|
@@ -1984,7 +1979,8 @@ StringBuffer &appendJSONRealValue(StringBuffer& s, const char *name, double valu
|
|
|
|
|
|
inline StringBuffer &encodeJSONChar(StringBuffer &s, const char *&ch, unsigned &remaining)
|
|
|
{
|
|
|
- switch (*ch)
|
|
|
+ byte next = *ch;
|
|
|
+ switch (next)
|
|
|
{
|
|
|
case '\b':
|
|
|
s.append("\\b");
|
|
@@ -2005,26 +2001,23 @@ inline StringBuffer &encodeJSONChar(StringBuffer &s, const char *&ch, unsigned &
|
|
|
case '\\':
|
|
|
case '/':
|
|
|
s.append('\\');
|
|
|
- s.append(*ch);
|
|
|
+ s.append(next);
|
|
|
break;
|
|
|
default:
|
|
|
- if (*ch >= ' ' && ((byte)*ch) < 128)
|
|
|
- s.append(*ch);
|
|
|
- else if (*ch < ' ' && *ch > 0)
|
|
|
- s.append("\\u00").appendhex(*ch, true);
|
|
|
+ if (next >= ' ' && next < 128)
|
|
|
+ s.append(next);
|
|
|
+ else if (next < ' ' && next > 0)
|
|
|
+ s.append("\\u00").appendhex(next, true);
|
|
|
else //json is always supposed to be utf8 (or other unicode formats)
|
|
|
{
|
|
|
- unsigned chlen = utf8CharLen((unsigned char)*ch); //check first byte
|
|
|
- if (chlen==0 || chlen > remaining || !checkValidUtf8Char(chlen, (const unsigned char *) ch))
|
|
|
- s.append("\\u00").appendhex(*ch, true);
|
|
|
+ unsigned chlen = utf8CharLen((const unsigned char *)ch, remaining);
|
|
|
+ if (chlen==0)
|
|
|
+ s.append("\\u00").appendhex(next, true);
|
|
|
else
|
|
|
{
|
|
|
- s.append(*ch);
|
|
|
- while(--chlen)
|
|
|
- {
|
|
|
- s.append(*(++ch));
|
|
|
- remaining--;
|
|
|
- }
|
|
|
+ s.append(chlen, ch);
|
|
|
+ ch += (chlen-1);
|
|
|
+ remaining -= (chlen-1);
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
@@ -2038,7 +2031,7 @@ StringBuffer &encodeJSON(StringBuffer &s, unsigned size, const char *value)
|
|
|
{
|
|
|
if (!value)
|
|
|
return s;
|
|
|
- while (size && *value)
|
|
|
+ while (size)
|
|
|
encodeJSONChar(s, value, size);
|
|
|
return s;
|
|
|
}
|