#include "config_store.h" #include #define CFG_FILE "/cfg.dat" #define SEP '\x01' AutoRunConfig g_autorun = {false, false, 3500, "elevated", ""}; static bool is_allowed_shell(const char *shell) { return strcmp(shell, "elevated") == 0 || strcmp(shell, "cmd") == 0 || strcmp(shell, "powershell") == 0 || strcmp(shell, "run") == 0; } static void sanitize_stored_field(char *s, size_t maxlen) { size_t out = 0; for (size_t i = 0; s[i] && out < maxlen - 1; i++) { unsigned char c = (unsigned char)s[i]; if (c == SEP || c < 0x20) continue; s[out++] = s[i]; } s[out] = '\0'; } void config_load() { File f = LittleFS.open(CFG_FILE, "r"); if (!f) return; String line; while (f.available()) { char c = (char)f.read(); if (c == '\n' || c == '\r') break; line += c; } f.close(); if (line.length() == 0) return; int p0 = line.indexOf(SEP); if (p0 < 0) return; int p1 = line.indexOf(SEP, p0+1); if (p1 < 0) return; int p2 = line.indexOf(SEP, p1+1); if (p2 < 0) return; int p3 = line.indexOf(SEP, p2+1); g_autorun.enabled = (line.substring(0, p0) == "1"); int d = line.substring(p0+1, p1).toInt(); g_autorun.delay_ms = (d < 500 || d > 15000) ? 3500 : d; strncpy(g_autorun.shell, line.substring(p1+1, p2).c_str(), sizeof(g_autorun.shell)-1); g_autorun.shell[sizeof(g_autorun.shell)-1] = '\0'; if (!is_allowed_shell(g_autorun.shell)) strncpy(g_autorun.shell, "run", sizeof(g_autorun.shell)); if (p3 >= 0) { strncpy(g_autorun.cmd, line.substring(p2 + 1, p3).c_str(), sizeof(g_autorun.cmd) - 1); g_autorun.cmd[sizeof(g_autorun.cmd) - 1] = '\0'; g_autorun.lab_scan_on_plug = (line.substring(p3 + 1) == "1"); } else { strncpy(g_autorun.cmd, line.substring(p2 + 1).c_str(), sizeof(g_autorun.cmd) - 1); g_autorun.cmd[sizeof(g_autorun.cmd) - 1] = '\0'; g_autorun.lab_scan_on_plug = false; } sanitize_stored_field(g_autorun.cmd, sizeof(g_autorun.cmd)); } bool config_save() { sanitize_stored_field(g_autorun.shell, sizeof(g_autorun.shell)); sanitize_stored_field(g_autorun.cmd, sizeof(g_autorun.cmd)); if (!is_allowed_shell(g_autorun.shell)) strncpy(g_autorun.shell, "run", sizeof(g_autorun.shell)); LittleFS.remove(CFG_FILE); File f = LittleFS.open(CFG_FILE, "w"); if (!f) return false; f.print(g_autorun.enabled ? "1" : "0"); f.print((char)SEP); f.print(g_autorun.delay_ms); f.print((char)SEP); f.print(g_autorun.shell); f.print((char)SEP); f.print(g_autorun.cmd); f.print((char)SEP); f.print(g_autorun.lab_scan_on_plug ? "1" : "0"); f.print('\n'); f.close(); return true; }