Skip to content

Commit e581717

Browse files
dirkxSuGliderlucasssvaz
authoredJan 16, 2024
Web server simplifications and handers (espressif#7429)
* First stab ad simplyfing webserver auth and adding a handler. * Tweaks after testing against docs and latest Library tree * Add documentatin for callback handler * Bodge to allow things to compile without the dependencies * Remove dependency on sodium to make it compile with 4.4 * Fix hex conversion * Move some common HEX functions into a static HEX class, remove those from MD5 and add some examples. This allows for the cleanup of various to/from HEX routines elsewhere. * Remove some duplicated code * Add simplfiied HEXBuilder under MD5Bulder to CMakefile. * Update for 3.0.0 and QoL improvements * Remove examples that depend on external libraries * Skip H2 testing * Formatting improvements * Move builders examples to Utilities folder * Fix indentation * Add HashBuilder abstract class * Add SHA1Builder * Fix comment * Fix whitespace * Fix crashes and improve log messages * Fix indentation for webserver --------- Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com> Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>
1 parent a114af0 commit e581717

File tree

21 files changed

+1417
-146
lines changed

21 files changed

+1417
-146
lines changed
 

‎CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ set(CORE_SRCS
5050
cores/esp32/Esp.cpp
5151
cores/esp32/FunctionalInterrupt.cpp
5252
cores/esp32/HardwareSerial.cpp
53+
cores/esp32/HEXBuilder.cpp
5354
cores/esp32/IPAddress.cpp
5455
cores/esp32/libb64/cdecode.c
5556
cores/esp32/libb64/cencode.c
5657
cores/esp32/main.cpp
5758
cores/esp32/MD5Builder.cpp
5859
cores/esp32/Print.cpp
60+
cores/esp32/SHA1Builder.cpp
5961
cores/esp32/stdlib_noniso.c
6062
cores/esp32/Stream.cpp
6163
cores/esp32/StreamString.cpp

‎cores/esp32/HEXBuilder.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
3+
This file is part of the esp32 core for Arduino environment.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <Arduino.h>
21+
#include <HEXBuilder.h>
22+
23+
static uint8_t hex_char_to_byte(uint8_t c)
24+
{
25+
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
26+
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
27+
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0x10; // unknown char is 16
28+
}
29+
30+
size_t HEXBuilder::hex2bytes(unsigned char * out, size_t maxlen, String &in) {
31+
return hex2bytes(out, maxlen, in.c_str());
32+
}
33+
34+
size_t HEXBuilder::hex2bytes(unsigned char * out, size_t maxlen, const char * in) {
35+
size_t len = 0;
36+
for(;*in;in++) {
37+
uint8_t c = hex_char_to_byte(*in);
38+
// Silently skip anything unknown.
39+
if (c > 15)
40+
continue;
41+
42+
if (len & 1) {
43+
if (len/2 < maxlen)
44+
out[len/2] |= c;
45+
} else {
46+
if (len/2 < maxlen)
47+
out[len/2] = c<<4;
48+
}
49+
len++;
50+
}
51+
return (len + 1)/2;
52+
}
53+
54+
size_t HEXBuilder::bytes2hex(char * out, size_t maxlen, const unsigned char * in, size_t len) {
55+
for(size_t i = 0; i < len; i++) {
56+
if (i*2 + 1 < maxlen) {
57+
sprintf(out + (i * 2), "%02x", in[i]);
58+
}
59+
}
60+
return len * 2 + 1;
61+
}
62+
63+
String HEXBuilder::bytes2hex(const unsigned char * in, size_t len) {
64+
size_t maxlen = len * 2 + 1;
65+
char * out = (char *) malloc(maxlen);
66+
if (!out) return String();
67+
bytes2hex(out, maxlen, in, len);
68+
String ret = String(out);
69+
free(out);
70+
return ret;
71+
}

‎cores/esp32/HEXBuilder.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
3+
This file is part of the esp32 core for Arduino environment.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef HEXBuilder_h
21+
#define HEXBuilder_h
22+
23+
#include <WString.h>
24+
#include <Stream.h>
25+
26+
class HEXBuilder {
27+
public:
28+
static size_t hex2bytes(unsigned char * out, size_t maxlen, String & in);
29+
static size_t hex2bytes(unsigned char * out, size_t maxlen, const char * in);
30+
31+
static String bytes2hex(const unsigned char * in, size_t len);
32+
static size_t bytes2hex(char * out, size_t maxlen, const unsigned char * in, size_t len);
33+
};
34+
#endif

‎cores/esp32/HashBuilder.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef HashBuilder_h
16+
#define HashBuilder_h
17+
18+
#include <WString.h>
19+
#include <Stream.h>
20+
21+
#include "HEXBuilder.h"
22+
23+
class HashBuilder : public HEXBuilder
24+
{
25+
public:
26+
virtual ~HashBuilder() {}
27+
virtual void begin() = 0;
28+
29+
virtual void add(uint8_t* data, size_t len) = 0;
30+
virtual void add(const char* data)
31+
{
32+
add((uint8_t*)data, strlen(data));
33+
}
34+
virtual void add(char* data)
35+
{
36+
add((const char*)data);
37+
}
38+
virtual void add(String data)
39+
{
40+
add(data.c_str());
41+
}
42+
43+
virtual void addHexString(const char* data) = 0;
44+
virtual void addHexString(char* data)
45+
{
46+
addHexString((const char*)data);
47+
}
48+
virtual void addHexString(String data)
49+
{
50+
addHexString(data.c_str());
51+
}
52+
53+
virtual bool addStream(Stream& stream, const size_t maxLen) = 0;
54+
virtual void calculate() = 0;
55+
virtual void getBytes(uint8_t* output) = 0;
56+
virtual void getChars(char* output) = 0;
57+
virtual String toString() = 0;
58+
};
59+
60+
#endif

‎cores/esp32/MD5Builder.cpp

+6-17
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,30 @@
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
19+
1920
#include <Arduino.h>
21+
#include <HEXBuilder.h>
2022
#include <MD5Builder.h>
2123

22-
static uint8_t hex_char_to_byte(uint8_t c)
23-
{
24-
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
25-
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
26-
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0;
27-
}
28-
2924
void MD5Builder::begin(void)
3025
{
3126
memset(_buf, 0x00, ESP_ROM_MD5_DIGEST_LEN);
3227
esp_rom_md5_init(&_ctx);
3328
}
3429

35-
void MD5Builder::add(uint8_t * data, uint16_t len)
30+
void MD5Builder::add(uint8_t * data, size_t len)
3631
{
3732
esp_rom_md5_update(&_ctx, data, len);
3833
}
3934

4035
void MD5Builder::addHexString(const char * data)
4136
{
42-
uint16_t i, len = strlen(data);
37+
size_t len = strlen(data);
4338
uint8_t * tmp = (uint8_t*)malloc(len/2);
4439
if(tmp == NULL) {
4540
return;
4641
}
47-
for(i=0; i<len; i+=2) {
48-
uint8_t high = hex_char_to_byte(data[i]);
49-
uint8_t low = hex_char_to_byte(data[i+1]);
50-
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
51-
}
42+
hex2bytes(tmp, len/2, data);
5243
add(tmp, len/2);
5344
free(tmp);
5445
}
@@ -105,9 +96,7 @@ void MD5Builder::getBytes(uint8_t * output)
10596

10697
void MD5Builder::getChars(char * output)
10798
{
108-
for(uint8_t i = 0; i < ESP_ROM_MD5_DIGEST_LEN; i++) {
109-
sprintf(output + (i * 2), "%02x", _buf[i]);
110-
}
99+
bytes2hex(output, ESP_ROM_MD5_DIGEST_LEN*2+1, _buf, ESP_ROM_MD5_DIGEST_LEN);
111100
}
112101

113102
String MD5Builder::toString(void)

‎cores/esp32/MD5Builder.h

+23-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/*
1+
/*
22
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
3-
This file is part of the esp8266 core for Arduino environment.
4-
3+
This file is part of the esp32 core for Arduino environment.
4+
55
This library is free software; you can redistribute it and/or
66
modify it under the terms of the GNU Lesser General Public
77
License as published by the Free Software Foundation; either
@@ -15,51 +15,39 @@
1515
You should have received a copy of the GNU Lesser General Public
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
Modified 10 Jan 2024 by Lucas Saavedra Vaz (Use abstract class HashBuilder)
1820
*/
19-
#ifndef __ESP8266_MD5_BUILDER__
20-
#define __ESP8266_MD5_BUILDER__
21+
#ifndef MD5Builder_h
22+
#define MD5Builder_h
2123

2224
#include <WString.h>
2325
#include <Stream.h>
2426

2527
#include "esp_system.h"
2628
#include "esp_rom_md5.h"
2729

28-
class MD5Builder
30+
#include "HashBuilder.h"
31+
32+
class MD5Builder : public HashBuilder
2933
{
3034
private:
3135
md5_context_t _ctx;
3236
uint8_t _buf[ESP_ROM_MD5_DIGEST_LEN];
3337
public:
34-
void begin(void);
35-
void add(uint8_t * data, uint16_t len);
36-
void add(const char * data)
37-
{
38-
add((uint8_t*)data, strlen(data));
39-
}
40-
void add(char * data)
41-
{
42-
add((const char*)data);
43-
}
44-
void add(String data)
45-
{
46-
add(data.c_str());
47-
}
48-
void addHexString(const char * data);
49-
void addHexString(char * data)
50-
{
51-
addHexString((const char*)data);
52-
}
53-
void addHexString(String data)
54-
{
55-
addHexString(data.c_str());
56-
}
57-
bool addStream(Stream & stream, const size_t maxLen);
58-
void calculate(void);
59-
void getBytes(uint8_t * output);
60-
void getChars(char * output);
61-
String toString(void);
62-
};
38+
void begin(void) override;
6339

40+
using HashBuilder::add;
41+
void add(uint8_t * data, size_t len) override;
42+
43+
using HashBuilder::addHexString;
44+
void addHexString(const char * data) override;
45+
46+
bool addStream(Stream & stream, const size_t maxLen) override;
47+
void calculate(void) override;
48+
void getBytes(uint8_t * output) override;
49+
void getChars(char * output) override;
50+
String toString(void) override;
51+
};
6452

6553
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.