diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 43d6f1153c..6880f84d9d 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -373,10 +373,11 @@ int8_t step_to_accuracy_decimals(float step) { return str.length() - dot_pos - 1; } -// Use C-style string constant to store in ROM instead of RAM (saves 24 bytes) -static constexpr const char *BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; +// Store BASE64 characters as array - automatically placed in flash/ROM on embedded platforms +static const char BASE64_CHARS[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; // Helper function to find the index of a base64 character in the lookup table. // Returns the character's position (0-63) if found, or 0 if not found. @@ -386,8 +387,8 @@ static constexpr const char *BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // stops processing at the first invalid character due to the is_base64() check in its // while loop condition, making this edge case harmless in practice. static inline uint8_t base64_find_char(char c) { - const char *pos = strchr(BASE64_CHARS, c); - return pos ? (pos - BASE64_CHARS) : 0; + const void *ptr = memchr(BASE64_CHARS, c, sizeof(BASE64_CHARS)); + return ptr ? (static_cast(ptr) - BASE64_CHARS) : 0; } static inline bool is_base64(char c) { return (isalnum(c) || (c == '+') || (c == '/')); }