A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
add json methods to parse file entries
Geo0a2e324e61eb
diff --git a/src/Transformers/JsonTransformer.cpp b/src/Transformers/JsonTransformer.cppindex 05dc2805..b22ae5b9 100644--- a/src/Transformers/JsonTransformer.cpp+++ b/src/Transformers/JsonTransformer.cpp@@ -52,3 +52,82 @@ std::string JsonTransformer::dechunk(const std::string& chunked)}return out;}++std::string JsonTransformer::escape(const std::string& s) {+ std::string out;+ out.reserve(s.size() + 8);+ for (unsigned char c : s) {+ switch (c) {+ case '\"': out += "\\\""; break;+ case '\\': out += "\\\\"; break;+ case '\b': out += "\\b"; break;+ case '\f': out += "\\f"; break;+ case '\n': out += "\\n"; break;+ case '\r': out += "\\r"; break;+ case '\t': out += "\\t"; break;+ default:+ if (c < 0x20) {+ char buf[7];+ std::snprintf(buf, sizeof(buf), "\\u%04X", (unsigned)c);+ out += buf;+ } else {+ out.push_back((char)c);+ }+ }+ }+ return out;+}++std::string JsonTransformer::makeEntryJson(const std::string& name, size_t size, bool isDir) {+ std::string out;+ out.reserve(name.size() + 48);+ out += "{\"name\":\"";+ out += escape(name);+ out += "\",\"size\":";+ out += std::to_string(size);+ out += ",\"isDir\":";+ out += (isDir ? "true" : "false");+ out += "}";+ return out;+}++std::string JsonTransformer::makeLsJson(const std::string& dir,+ size_t total,+ size_t used,+ const std::vector<std::string>& names,+ const std::vector<size_t>& sizes,+ const std::vector<uint8_t>& isDirs)+{+ const size_t n = names.size();+ if (sizes.size() != n || isDirs.size() != n) {+ std::string err = "{\"error\":\"mismatched vector sizes\"}";+ return err;+ }++ size_t approx = 64 + dir.size() + n * 48;+ for (auto &nm : names) approx += nm.size();+ std::string out;+ out.reserve(approx);++ out += "{\"dir\":\"";+ out += escape(dir);+ out += "\",\"total\":";+ out += std::to_string(total);+ out += ",\"used\":";+ out += std::to_string(used);+ out += ",\"entries\":[";++ for (size_t i = 0; i < n; ++i) {+ if (i) out += ",";+ out += "{\"name\":\"";+ out += escape(names[i]);+ out += "\",\"size\":";+ out += std::to_string(sizes[i]);+ out += ",\"isDir\":";+ out += (isDirs[i] ? "true" : "false");+ out += "}";+ }++ out += "]}";+ return out;+}\ No newline at end of filediff --git a/src/Transformers/JsonTransformer.h b/src/Transformers/JsonTransformer.hindex f6d0a3f3..a7d46f95 100644--- a/src/Transformers/JsonTransformer.h+++ b/src/Transformers/JsonTransformer.h@@ -12,5 +12,20 @@ public:// Remove chunked transfer encodingstd::string dechunk(const std::string& chunked);++ // Escape a string for JSON+ static std::string escape(const std::string& s);++ // Create a JSON entry for a file or directory+ static std::string makeEntryJson(const std::string& name, size_t size, bool isDir);++ // Create a JSON listing for a directory+ static std::string makeLsJson(const std::string& dir,+ size_t total,+ size_t used,+ const std::vector<std::string>& names,+ const std::vector<size_t>& sizes,+ const std::vector<uint8_t>& isDirs);+private:};