From bf44849ecfe57397a74b9172846bb5678a4dcd40 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Fri, 24 Jan 2020 14:52:14 +0100 Subject: [PATCH 1/3] Added base64 lib directly into this project to fix build problem with esp32 --- src/Base64.cpp | 142 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Base64.h | 26 +++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/Base64.cpp create mode 100644 src/Base64.h diff --git a/src/Base64.cpp b/src/Base64.cpp new file mode 100644 index 00000000..5cf68987 --- /dev/null +++ b/src/Base64.cpp @@ -0,0 +1,142 @@ +/* +Copyright (C) 2016 Arturo Guadalupi. All right reserved. + +This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +*/ + +#include "Base64.h" +#include +#if (defined(__AVR__)) +#include +#else +#include +#endif +const char PROGMEM _Base64AlphabetTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +int Base64Class::encode(char *output, char *input, int inputLength) { + int i = 0, j = 0; + int encodedLength = 0; + unsigned char A3[3]; + unsigned char A4[4]; + + while(inputLength--) { + A3[i++] = *(input++); + if(i == 3) { + fromA3ToA4(A4, A3); + + for(i = 0; i < 4; i++) { + output[encodedLength++] = pgm_read_byte(&_Base64AlphabetTable[A4[i]]); + } + + i = 0; + } + } + + if(i) { + for(j = i; j < 3; j++) { + A3[j] = '\0'; + } + + fromA3ToA4(A4, A3); + + for(j = 0; j < i + 1; j++) { + output[encodedLength++] = pgm_read_byte(&_Base64AlphabetTable[A4[j]]); + } + + while((i++ < 3)) { + output[encodedLength++] = '='; + } + } + output[encodedLength] = '\0'; + return encodedLength; +} + +int Base64Class::decode(char * output, char * input, int inputLength) { + int i = 0, j = 0; + int decodedLength = 0; + unsigned char A3[3]; + unsigned char A4[4]; + + + while (inputLength--) { + if(*input == '=') { + break; + } + + A4[i++] = *(input++); + if (i == 4) { + for (i = 0; i <4; i++) { + A4[i] = lookupTable(A4[i]); + } + + fromA4ToA3(A3,A4); + + for (i = 0; i < 3; i++) { + output[decodedLength++] = A3[i]; + } + i = 0; + } + } + + if (i) { + for (j = i; j < 4; j++) { + A4[j] = '\0'; + } + + for (j = 0; j <4; j++) { + A4[j] = lookupTable(A4[j]); + } + + fromA4ToA3(A3,A4); + + for (j = 0; j < i - 1; j++) { + output[decodedLength++] = A3[j]; + } + } + output[decodedLength] = '\0'; + return decodedLength; +} + +int Base64Class::encodedLength(int plainLength) { + int n = plainLength; + return (n + 2 - ((n + 2) % 3)) / 3 * 4; +} + +int Base64Class::decodedLength(char * input, int inputLength) { + int i = 0; + int numEq = 0; + for(i = inputLength - 1; input[i] == '='; i--) { + numEq++; + } + + return ((6 * inputLength) / 8) - numEq; +} + +//Private utility functions +inline void Base64Class::fromA3ToA4(unsigned char * A4, unsigned char * A3) { + A4[0] = (A3[0] & 0xfc) >> 2; + A4[1] = ((A3[0] & 0x03) << 4) + ((A3[1] & 0xf0) >> 4); + A4[2] = ((A3[1] & 0x0f) << 2) + ((A3[2] & 0xc0) >> 6); + A4[3] = (A3[2] & 0x3f); +} + +inline void Base64Class::fromA4ToA3(unsigned char * A3, unsigned char * A4) { + A3[0] = (A4[0] << 2) + ((A4[1] & 0x30) >> 4); + A3[1] = ((A4[1] & 0xf) << 4) + ((A4[2] & 0x3c) >> 2); + A3[2] = ((A4[2] & 0x3) << 6) + A4[3]; +} + +inline unsigned char Base64Class::lookupTable(char c) { + if(c >='A' && c <='Z') return c - 'A'; + if(c >='a' && c <='z') return c - 71; + if(c >='0' && c <='9') return c + 4; + if(c == '+') return 62; + if(c == '/') return 63; + return -1; +} + +Base64Class Base64; diff --git a/src/Base64.h b/src/Base64.h new file mode 100644 index 00000000..7330225e --- /dev/null +++ b/src/Base64.h @@ -0,0 +1,26 @@ +/* +Copyright (C) 2016 Arturo Guadalupi. All right reserved. + +This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +*/ + +#ifndef _BASE64_H +#define _BASE64_H + +class Base64Class{ + public: + int encode(char *output, char *input, int inputLength); + int decode(char * output, char * input, int inputLength); + int encodedLength(int plainLength); + int decodedLength(char * input, int inputLength); + + private: + inline void fromA3ToA4(unsigned char * A4, unsigned char * A3); + inline void fromA4ToA3(unsigned char * A3, unsigned char * A4); + inline unsigned char lookupTable(char c); +}; +extern Base64Class Base64; + +#endif // _BASE64_H From 50faaca5591e6d8988574a6699188ecab3d93ecd Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Fri, 24 Jan 2020 14:54:42 +0100 Subject: [PATCH 2/3] Moved Base64 lib --- {src => lib/HanConfigAp/src}/Base64.cpp | 0 {src => lib/HanConfigAp/src}/Base64.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {src => lib/HanConfigAp/src}/Base64.cpp (100%) rename {src => lib/HanConfigAp/src}/Base64.h (100%) diff --git a/src/Base64.cpp b/lib/HanConfigAp/src/Base64.cpp similarity index 100% rename from src/Base64.cpp rename to lib/HanConfigAp/src/Base64.cpp diff --git a/src/Base64.h b/lib/HanConfigAp/src/Base64.h similarity index 100% rename from src/Base64.h rename to lib/HanConfigAp/src/Base64.h From 3312f88804701b82d9283509aa43d30bf2697268 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Tue, 28 Jan 2020 18:17:20 +0100 Subject: [PATCH 3/3] Some changes to make it compile for both ESP8266 and ESP32 --- .gitignore | 3 ++- lib/HanConfigAp/src/HanConfigAp.cpp | 6 ++++-- platformio.ini | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 97877088..e86f0cf3 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ *.sw[op] .vscode .pio -platformio-user.ini \ No newline at end of file +platformio-user.ini +src/version.h \ No newline at end of file diff --git a/lib/HanConfigAp/src/HanConfigAp.cpp b/lib/HanConfigAp/src/HanConfigAp.cpp index 1c901e49..b43364cf 100644 --- a/lib/HanConfigAp/src/HanConfigAp.cpp +++ b/lib/HanConfigAp/src/HanConfigAp.cpp @@ -107,7 +107,7 @@ void HanConfigAp::handleRoot() { configuration *config = new configuration(); config->load(); - String html = CONFIG_HTML; + String html = String((const __FlashStringHelper*) CONFIG_HTML); if(config->hasConfig()) { bool access = !config->isAuth(); @@ -201,10 +201,12 @@ void HanConfigAp::handleRoot() { void HanConfigAp::handleStyle() { println("Serving /style.css over http..."); + String css = String((const __FlashStringHelper*) STYLE_CSS); + server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); - server.send(200, "text/html", STYLE_CSS, sizeof(STYLE_CSS)); + server.send(200, "text/css", css); } void HanConfigAp::handleSave() { diff --git a/platformio.ini b/platformio.ini index 84250766..091c3d93 100755 --- a/platformio.ini +++ b/platformio.ini @@ -3,7 +3,7 @@ extra_configs = platformio-user.ini [common] framework = arduino -lib_deps = HanConfigAp@1.0.0, HanReader@1.0.0, HanToJson@1.0.0, ArduinoJson@^6.0.0, MQTT@^2.4.0, DallasTemperature@^3.8.0, Base64@0.0.1 +lib_deps = HanConfigAp@1.0.0, HanReader@1.0.0, HanToJson@1.0.0, ArduinoJson@^6.0.0, MQTT@^2.4.0, DallasTemperature@^3.8.0 [env:esp12e] platform = espressif8266