From 98a78a708a970bcab41ce2e8e8a3e9549d4fcb03 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Thu, 15 Jul 2021 17:04:27 +0200 Subject: [PATCH] added urldecode() --- src/espstrutils.cpp | 34 ++++++++++++++++++++++++++++++++++ src/espstrutils.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/src/espstrutils.cpp b/src/espstrutils.cpp index 2980659..95c9cf5 100644 --- a/src/espstrutils.cpp +++ b/src/espstrutils.cpp @@ -1,5 +1,8 @@ #include "espstrutils.h" +// system includes +#include + namespace espcpputils { std::string toString(sntp_sync_mode_t val) @@ -13,4 +16,35 @@ std::string toString(sntp_sync_mode_t val) return std::string{"Unknown sntp_sync_mode_t("} + std::to_string(int(val)) + ')'; } +void urldecode(char *dst, const char *src) +{ + while (*src) { + char a, b; + if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (std::isxdigit(a) && std::isxdigit(b))) { + if (a >= 'a') + a -= 'a'-'A'; + if (a >= 'A') + a -= ('A' - 10); + else + a -= '0'; + + if (b >= 'a') + b -= 'a'-'A'; + if (b >= 'A') + b -= ('A' - 10); + else + b -= '0'; + + *dst++ = 16*a+b; + src+=3; + } else if (*src == '+') { + *dst++ = ' '; + src++; + } else + *dst++ = *src++; + } + + *dst++ = '\0'; +} + } // namespace espcpputils diff --git a/src/espstrutils.h b/src/espstrutils.h index 9c4ca5a..8694162 100644 --- a/src/espstrutils.h +++ b/src/espstrutils.h @@ -10,4 +10,6 @@ namespace espcpputils { std::string toString(sntp_sync_mode_t val); +void urldecode(char *dst, const char *src); + } // namespace espcpputils