refactor: move html_util into legacy core library
This commit is contained in:
4
lib/dd3_legacy_core/include/dd3_legacy_core.h
Normal file
4
lib/dd3_legacy_core/include/dd3_legacy_core.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
// Include this header in legacy Unity tests to force-link dd3_legacy_core.
|
||||
void dd3_legacy_core_force_link();
|
||||
7
lib/dd3_legacy_core/include/html_util.h
Normal file
7
lib/dd3_legacy_core/include/html_util.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
String html_escape(const String &input);
|
||||
String url_encode_component(const String &input);
|
||||
bool sanitize_device_id(const String &input, String &out_device_id);
|
||||
3
lib/dd3_legacy_core/src/dd3_legacy_core.cpp
Normal file
3
lib/dd3_legacy_core/src/dd3_legacy_core.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "dd3_legacy_core.h"
|
||||
|
||||
void dd3_legacy_core_force_link() {}
|
||||
98
lib/dd3_legacy_core/src/html_util.cpp
Normal file
98
lib/dd3_legacy_core/src/html_util.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "html_util.h"
|
||||
|
||||
String html_escape(const String &input) {
|
||||
String out;
|
||||
out.reserve(input.length() + 8);
|
||||
for (size_t i = 0; i < input.length(); ++i) {
|
||||
char c = input[i];
|
||||
switch (c) {
|
||||
case '&':
|
||||
out += "&";
|
||||
break;
|
||||
case '<':
|
||||
out += "<";
|
||||
break;
|
||||
case '>':
|
||||
out += ">";
|
||||
break;
|
||||
case '"':
|
||||
out += """;
|
||||
break;
|
||||
case '\'':
|
||||
out += "'";
|
||||
break;
|
||||
default:
|
||||
out += c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
String url_encode_component(const String &input) {
|
||||
String out;
|
||||
out.reserve(input.length() * 3);
|
||||
const char *hex = "0123456789ABCDEF";
|
||||
for (size_t i = 0; i < input.length(); ++i) {
|
||||
unsigned char c = static_cast<unsigned char>(input[i]);
|
||||
bool safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~';
|
||||
if (safe) {
|
||||
out += static_cast<char>(c);
|
||||
} else {
|
||||
out += '%';
|
||||
out += hex[(c >> 4) & 0x0F];
|
||||
out += hex[c & 0x0F];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static bool is_hex_char(char c) {
|
||||
return (c >= '0' && c <= '9') ||
|
||||
(c >= 'a' && c <= 'f') ||
|
||||
(c >= 'A' && c <= 'F');
|
||||
}
|
||||
|
||||
static String to_upper_hex4(const String &input) {
|
||||
String out = input;
|
||||
out.toUpperCase();
|
||||
return out;
|
||||
}
|
||||
|
||||
bool sanitize_device_id(const String &input, String &out_device_id) {
|
||||
String trimmed = input;
|
||||
trimmed.trim();
|
||||
if (trimmed.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
if (trimmed.indexOf('/') >= 0 || trimmed.indexOf('\\') >= 0 || trimmed.indexOf("..") >= 0) {
|
||||
return false;
|
||||
}
|
||||
if (trimmed.indexOf('%') >= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (trimmed.length() == 4) {
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
if (!is_hex_char(trimmed[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out_device_id = String("dd3-") + to_upper_hex4(trimmed);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (trimmed.length() == 8 && trimmed.startsWith("dd3-")) {
|
||||
String hex = trimmed.substring(4);
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
if (!is_hex_char(hex[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out_device_id = String("dd3-") + to_upper_hex4(hex);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user