|
@@ -67,6 +67,10 @@ public:
|
|
|
{
|
|
{
|
|
|
appendBytes(number, static_cast<size_t>(length));
|
|
appendBytes(number, static_cast<size_t>(length));
|
|
|
}
|
|
}
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ error = ESP_FAIL;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void appendNullField(const char *key, bool &firstField)
|
|
void appendNullField(const char *key, bool &firstField)
|
|
@@ -136,81 +140,63 @@ private:
|
|
|
|
|
|
|
|
void appendEscapedString(const char *value)
|
|
void appendEscapedString(const char *value)
|
|
|
{
|
|
{
|
|
|
- if (error != ESP_OK)
|
|
|
|
|
- {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (error != ESP_OK) return;
|
|
|
|
|
+ if (value == nullptr) value = "";
|
|
|
|
|
|
|
|
- if (value == nullptr)
|
|
|
|
|
- {
|
|
|
|
|
- value = "";
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for (const unsigned char *cursor =
|
|
|
|
|
- reinterpret_cast<const unsigned char *>(value);
|
|
|
|
|
- *cursor != '\0' && error == ESP_OK;
|
|
|
|
|
- ++cursor)
|
|
|
|
|
|
|
+ const unsigned char *cursor =
|
|
|
|
|
+ reinterpret_cast<const unsigned char *>(value);
|
|
|
|
|
+ while (*cursor != '\0' && error == ESP_OK)
|
|
|
{
|
|
{
|
|
|
char escaped[7];
|
|
char escaped[7];
|
|
|
size_t escapedLength = 1;
|
|
size_t escapedLength = 1;
|
|
|
-
|
|
|
|
|
switch (*cursor)
|
|
switch (*cursor)
|
|
|
{
|
|
{
|
|
|
- case '"':
|
|
|
|
|
- escaped[0] = '\\';
|
|
|
|
|
- escaped[1] = '"';
|
|
|
|
|
- escapedLength = 2;
|
|
|
|
|
- break;
|
|
|
|
|
- case '\\':
|
|
|
|
|
- escaped[0] = '\\';
|
|
|
|
|
- escaped[1] = '\\';
|
|
|
|
|
- escapedLength = 2;
|
|
|
|
|
- break;
|
|
|
|
|
- case '\b':
|
|
|
|
|
- escaped[0] = '\\';
|
|
|
|
|
- escaped[1] = 'b';
|
|
|
|
|
- escapedLength = 2;
|
|
|
|
|
- break;
|
|
|
|
|
- case '\f':
|
|
|
|
|
- escaped[0] = '\\';
|
|
|
|
|
- escaped[1] = 'f';
|
|
|
|
|
- escapedLength = 2;
|
|
|
|
|
- break;
|
|
|
|
|
- case '\n':
|
|
|
|
|
- escaped[0] = '\\';
|
|
|
|
|
- escaped[1] = 'n';
|
|
|
|
|
- escapedLength = 2;
|
|
|
|
|
- break;
|
|
|
|
|
- case '\r':
|
|
|
|
|
- escaped[0] = '\\';
|
|
|
|
|
- escaped[1] = 'r';
|
|
|
|
|
- escapedLength = 2;
|
|
|
|
|
- break;
|
|
|
|
|
- case '\t':
|
|
|
|
|
- escaped[0] = '\\';
|
|
|
|
|
- escaped[1] = 't';
|
|
|
|
|
- escapedLength = 2;
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ case '"': escaped[0] = '\\'; escaped[1] = '"'; escapedLength = 2; break;
|
|
|
|
|
+ case '\\': escaped[0] = '\\'; escaped[1] = '\\'; escapedLength = 2; break;
|
|
|
|
|
+ case '\b': escaped[0] = '\\'; escaped[1] = 'b'; escapedLength = 2; break;
|
|
|
|
|
+ case '\f': escaped[0] = '\\'; escaped[1] = 'f'; escapedLength = 2; break;
|
|
|
|
|
+ case '\n': escaped[0] = '\\'; escaped[1] = 'n'; escapedLength = 2; break;
|
|
|
|
|
+ case '\r': escaped[0] = '\\'; escaped[1] = 'r'; escapedLength = 2; break;
|
|
|
|
|
+ case '\t': escaped[0] = '\\'; escaped[1] = 't'; escapedLength = 2; break;
|
|
|
default:
|
|
default:
|
|
|
if (*cursor < 0x20)
|
|
if (*cursor < 0x20)
|
|
|
{
|
|
{
|
|
|
- const int length = snprintf(
|
|
|
|
|
- escaped, sizeof(escaped), "\\u%04x", *cursor);
|
|
|
|
|
- if (length != 6)
|
|
|
|
|
- {
|
|
|
|
|
- error = ESP_FAIL;
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const int length = snprintf(escaped, sizeof(escaped), "\\u%04x", *cursor);
|
|
|
|
|
+ if (length != 6) { error = ESP_FAIL; return; }
|
|
|
escapedLength = 6;
|
|
escapedLength = 6;
|
|
|
}
|
|
}
|
|
|
- else
|
|
|
|
|
|
|
+ else if (*cursor < 0x80)
|
|
|
{
|
|
{
|
|
|
escaped[0] = static_cast<char>(*cursor);
|
|
escaped[0] = static_cast<char>(*cursor);
|
|
|
}
|
|
}
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ // Preserve valid UTF-8; replace malformed bytes so the JSON remains parseable.
|
|
|
|
|
+ const unsigned char b1 = cursor[1];
|
|
|
|
|
+ const unsigned char b2 = cursor[2];
|
|
|
|
|
+ const unsigned char b3 = cursor[3];
|
|
|
|
|
+ size_t n = 0;
|
|
|
|
|
+ if (*cursor >= 0xC2 && *cursor <= 0xDF && b1 >= 0x80 && b1 <= 0xBF) n = 2;
|
|
|
|
|
+ else if (*cursor == 0xE0 && b1 >= 0xA0 && b1 <= 0xBF && b2 >= 0x80 && b2 <= 0xBF) n = 3;
|
|
|
|
|
+ else if (((*cursor >= 0xE1 && *cursor <= 0xEC) || (*cursor >= 0xEE && *cursor <= 0xEF)) && b1 >= 0x80 && b1 <= 0xBF && b2 >= 0x80 && b2 <= 0xBF) n = 3;
|
|
|
|
|
+ else if (*cursor == 0xED && b1 >= 0x80 && b1 <= 0x9F && b2 >= 0x80 && b2 <= 0xBF) n = 3;
|
|
|
|
|
+ else if (*cursor == 0xF0 && b1 >= 0x90 && b1 <= 0xBF && b2 >= 0x80 && b2 <= 0xBF && b3 >= 0x80 && b3 <= 0xBF) n = 4;
|
|
|
|
|
+ else if (*cursor >= 0xF1 && *cursor <= 0xF3 && b1 >= 0x80 && b1 <= 0xBF && b2 >= 0x80 && b2 <= 0xBF && b3 >= 0x80 && b3 <= 0xBF) n = 4;
|
|
|
|
|
+ else if (*cursor == 0xF4 && b1 >= 0x80 && b1 <= 0x8F && b2 >= 0x80 && b2 <= 0xBF && b3 >= 0x80 && b3 <= 0xBF) n = 4;
|
|
|
|
|
+ if (n != 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ appendBytes(reinterpret_cast<const char *>(cursor), n);
|
|
|
|
|
+ cursor += n;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ appendLiteral("\\ufffd");
|
|
|
|
|
+ ++cursor;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
appendBytes(escaped, escapedLength);
|
|
appendBytes(escaped, escapedLength);
|
|
|
|
|
+ ++cursor;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|