mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-10-31 22:02:17 +01:00
calcEscapedLength looped from strlen(raw) down to 1, counting the terminator instead of the first character. A value starting with a character that needs escaping was counted one byte short, so the fit check could let it be written one byte past the buffer. Control characters other than \b\f\n\r\t were copied raw, which isn't valid JSON. Both now go through one escapeChar helper, which also escapes the remaining control characters as \u00XX. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
5.5 KiB
C++
129 lines
5.5 KiB
C++
#include "JsonFormatter.h"
|
|
|
|
void BufferedJsonFormatter::setBuffer(char *b, size_t sz) {
|
|
this->buff = b;
|
|
this->buffSize = sz;
|
|
this->buff[0] = 0;
|
|
this->_nocomma = true;
|
|
this->_objects = 0;
|
|
this->_arrays = 0;
|
|
}
|
|
size_t BufferedJsonFormatter::length() const {
|
|
return strlen(this->buff);
|
|
}
|
|
void JsonFormatter::beginObject(const char *name) {
|
|
if(name && strlen(name) > 0) this->appendElem(name);
|
|
else if(!this->_nocomma) this->_safecat(",");
|
|
this->_safecat("{");
|
|
this->_objects++;
|
|
this->_nocomma = true;
|
|
}
|
|
void JsonFormatter::endObject() {
|
|
//if(strlen(this->buff) + 1 > this->buffSize - 1) this->send();
|
|
this->_safecat("}");
|
|
this->_objects--;
|
|
this->_nocomma = false;
|
|
}
|
|
void JsonFormatter::beginArray(const char *name) {
|
|
if(name && strlen(name) > 0) this->appendElem(name);
|
|
else if(!this->_nocomma) this->_safecat(",");
|
|
this->_safecat("[");
|
|
this->_arrays++;
|
|
this->_nocomma = true;
|
|
}
|
|
void JsonFormatter::endArray() {
|
|
//if(strlen(this->buff) + 1 > this->buffSize - 1) this->send();
|
|
this->_safecat("]");
|
|
this->_arrays--;
|
|
this->_nocomma = false;
|
|
}
|
|
|
|
void JsonFormatter::appendElem(const char *name) {
|
|
if(!this->_nocomma) this->_safecat(",");
|
|
if(name && strlen(name) > 0) {
|
|
this->_safecat(name, true);
|
|
this->_safecat(":");
|
|
}
|
|
this->_nocomma = false;
|
|
}
|
|
|
|
void JsonFormatter::addElem(const char *name, const char *val) {
|
|
if(!val) return;
|
|
this->appendElem(name);
|
|
this->_safecat(val, true);
|
|
}
|
|
void JsonFormatter::addElem(const char *val) { this->addElem(nullptr, val); }
|
|
void JsonFormatter::addElem(float fval) { sprintf(this->_numbuff, "%.4f", fval); this->_appendNumber(nullptr); }
|
|
void JsonFormatter::addElem(int8_t nval) { sprintf(this->_numbuff, "%d", nval); this->_appendNumber(nullptr); }
|
|
void JsonFormatter::addElem(uint8_t nval) { sprintf(this->_numbuff, "%u", nval); this->_appendNumber(nullptr); }
|
|
void JsonFormatter::addElem(int32_t nval) { sprintf(this->_numbuff, "%ld", (long)nval); this->_appendNumber(nullptr); }
|
|
void JsonFormatter::addElem(uint32_t nval) { sprintf(this->_numbuff, "%lu", (unsigned long)nval); this->_appendNumber(nullptr); }
|
|
|
|
/*
|
|
void JsonFormatter::addElem(int16_t nval) { sprintf(this->_numbuff, "%d", nval); this->_appendNumber(nullptr); }
|
|
void JsonFormatter::addElem(uint16_t nval) { sprintf(this->_numbuff, "%u", nval); this->_appendNumber(nullptr); }
|
|
void JsonFormatter::addElem(int64_t lval) { sprintf(this->_numbuff, "%lld", (long long)lval); this->_appendNumber(nullptr); }
|
|
void JsonFormatter::addElem(uint64_t lval) { sprintf(this->_numbuff, "%llu", (unsigned long long)lval); this->_appendNumber(nullptr); }
|
|
*/
|
|
void JsonFormatter::addElem(bool bval) { strcpy(this->_numbuff, bval ? "true" : "false"); this->_appendNumber(nullptr); }
|
|
|
|
void JsonFormatter::addElem(const char *name, float fval) { sprintf(this->_numbuff, "%.4f", fval); this->_appendNumber(name); }
|
|
void JsonFormatter::addElem(const char *name, int8_t nval) { sprintf(this->_numbuff, "%d", nval); this->_appendNumber(name); }
|
|
void JsonFormatter::addElem(const char *name, uint8_t nval) { sprintf(this->_numbuff, "%u", nval); this->_appendNumber(name); }
|
|
void JsonFormatter::addElem(const char *name, int32_t nval) { sprintf(this->_numbuff, "%ld", (long)nval); this->_appendNumber(name); }
|
|
void JsonFormatter::addElem(const char *name, uint32_t nval) { sprintf(this->_numbuff, "%lu", (unsigned long)nval); this->_appendNumber(name); }
|
|
|
|
/*
|
|
void JsonFormatter::addElem(const char *name, int16_t nval) { sprintf(this->_numbuff, "%d", nval); this->_appendNumber(name); }
|
|
void JsonFormatter::addElem(const char *name, uint16_t nval) { sprintf(this->_numbuff, "%u", nval); this->_appendNumber(name); }
|
|
void JsonFormatter::addElem(const char *name, int64_t lval) { sprintf(this->_numbuff, "%lld", (long long)lval); this->_appendNumber(name); }
|
|
*/
|
|
void JsonFormatter::addElem(const char *name, uint64_t lval) { sprintf(this->_numbuff, "%llu", (unsigned long long)lval); this->_appendNumber(name); }
|
|
|
|
void JsonFormatter::addElem(const char *name, bool bval) { strcpy(this->_numbuff, bval ? "true" : "false"); this->_appendNumber(name); }
|
|
|
|
void JsonFormatter::_safecat(const char *val, bool escape) {
|
|
size_t len = (escape ? this->calcEscapedLength(val) : strlen(val)) + strlen(this->buff);
|
|
if(escape) len += 2;
|
|
if(len >= this->buffSize) {
|
|
return;
|
|
}
|
|
if(escape) strcat(this->buff, "\"");
|
|
if(escape) this->escapeString(val, &this->buff[strlen(this->buff)]);
|
|
else strcat(this->buff, val);
|
|
if(escape) strcat(this->buff, "\"");
|
|
}
|
|
void JsonFormatter::_appendNumber(const char *name) { this->appendElem(name); this->_safecat(this->_numbuff); }
|
|
size_t JsonFormatter::escapeChar(char c, char *out) {
|
|
const char *esc = nullptr;
|
|
switch(c) {
|
|
case '"': esc = "\\\""; break;
|
|
case '/': esc = "\\/"; break;
|
|
case '\b': esc = "\\b"; break;
|
|
case '\f': esc = "\\f"; break;
|
|
case '\n': esc = "\\n"; break;
|
|
case '\r': esc = "\\r"; break;
|
|
case '\t': esc = "\\t"; break;
|
|
case '\\': esc = "\\\\"; break;
|
|
default: break;
|
|
}
|
|
if(esc) {
|
|
strcpy(out, esc);
|
|
return 2;
|
|
}
|
|
// Any other control character is not allowed raw in a JSON string.
|
|
if((unsigned char)c < 0x20) return snprintf(out, 7, "\\u%04x", (unsigned char)c);
|
|
out[0] = c;
|
|
out[1] = 0x00;
|
|
return 1;
|
|
}
|
|
uint32_t JsonFormatter::calcEscapedLength(const char *raw) {
|
|
char tmp[7];
|
|
uint32_t len = 0;
|
|
for(size_t i = 0; raw[i]; i++) len += escapeChar(raw[i], tmp);
|
|
return len;
|
|
}
|
|
void JsonFormatter::escapeString(const char *raw, char *escaped) {
|
|
char *out = escaped + strlen(escaped);
|
|
for(size_t i = 0; raw[i]; i++) out += escapeChar(raw[i], out);
|
|
}
|