diff --git a/src/encString.cpp b/src/encString.cpp index 3fd97c7..3f629c7 100644 --- a/src/encString.cpp +++ b/src/encString.cpp @@ -3,6 +3,23 @@ #include #include +bool isBase64(const char *p) +{ + char c; + + if (strlen(p) % 4 != 0) + return false; + do { + c = *p; + if (isalnum(c)) // alphanumberics are OK + continue; + if (c == '/' || c == '+' || c == '=') // + or / or = is OK. + continue; + return false; + } while (*++p != 0); + return true; +} + void strEncode(String &str) { @@ -41,17 +58,20 @@ void strDecode(String &str) uint32_t cid = ESP_getChipId(); uint32_t *pkey = (uint32_t *)&key[0]; uint8_t data[str.length()]; - unsigned int clen = base64_decode_chars(src, str.length(), (char *)data); String result = F(""); - if (0 != clen) { - *pkey = cid; - for (i = 0; i < clen; i++) { - b = data[i]; - b ^= key[enciphidx]; - ++enciphidx; - enciphidx %= sizeof(key); - result.concat(char(b)); + if (isBase64(str.c_str())) { + unsigned int clen = base64_decode_chars(src, str.length(), (char *)data); + + if (0 != clen) { + *pkey = cid; + for (i = 0; i < clen; i++) { + b = data[i]; + b ^= key[enciphidx]; + ++enciphidx; + enciphidx %= sizeof(key); + result.concat(char(b)); + } } } str = result; diff --git a/src/encString.hpp b/src/encString.hpp index bd1fbc9..f1835fa 100644 --- a/src/encString.hpp +++ b/src/encString.hpp @@ -16,4 +16,13 @@ void strEncode(String &str); */ void strDecode(String &str); +/** + * @brief Overeni, ze je retezec Base64 + * + * @param p Ukazatel na retezec + * @return true Retezec je kodovany pomoci Base64 + * @return false Retezec neni kodovany pomoci Base64 + */ +bool isBase64(const char *p); + #endif