Skip to content

Commit e1789dd

Browse files
MD5 Builder: Rework for unqiue_ptr instead of malloc, get only member functions marked as const (#7208)
Co-authored-by: Luiss <luiss@mind.cc>
1 parent 4f27ce1 commit e1789dd

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

cores/esp8266/MD5Builder.cpp

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include <Arduino.h>
22
#include <MD5Builder.h>
3+
#include <memory>
34

4-
uint8_t hex_char_to_byte(uint8_t c){
5-
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
6-
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
7-
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0;
5+
uint8_t hex_char_to_byte(uint8_t c) {
6+
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
7+
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
8+
(c >= '0' && c <= '9') ? (c - (uint8_t)'0') : 0;
89
}
910

1011
void MD5Builder::begin(void){
@@ -18,25 +19,27 @@ void MD5Builder::add(const uint8_t * data, const uint16_t len){
1819

1920
void MD5Builder::addHexString(const char * data){
2021
uint16_t i, len = strlen(data);
21-
uint8_t * tmp = (uint8_t*)malloc(len/2);
22-
if(tmp == NULL) {
22+
auto tmp = std::unique_ptr<uint8_t[]>{new(std::nothrow) uint8_t[len / 2]};
23+
24+
if (!tmp) {
2325
return;
2426
}
27+
2528
for(i=0; i<len; i+=2) {
2629
uint8_t high = hex_char_to_byte(data[i]);
2730
uint8_t low = hex_char_to_byte(data[i+1]);
2831
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
2932
}
30-
add(tmp, len/2);
31-
free(tmp);
33+
add(tmp.get(), len/2);
3234
}
3335

34-
bool MD5Builder::addStream(Stream & stream, const size_t maxLen){
36+
bool MD5Builder::addStream(Stream &stream, const size_t maxLen) {
3537
const int buf_size = 512;
3638
int maxLengthLeft = maxLen;
37-
uint8_t * buf = (uint8_t*) malloc(buf_size);
3839

39-
if(!buf) {
40+
auto buf = std::unique_ptr<uint8_t[]>{new(std::nothrow) uint8_t[buf_size]};
41+
42+
if (!buf) {
4043
return false;
4144
}
4245

@@ -45,48 +48,47 @@ bool MD5Builder::addStream(Stream & stream, const size_t maxLen){
4548

4649
// determine number of bytes to read
4750
int readBytes = bytesAvailable;
48-
if(readBytes > maxLengthLeft) {
49-
readBytes = maxLengthLeft ; // read only until max_len
51+
if (readBytes > maxLengthLeft){
52+
readBytes = maxLengthLeft; // read only until max_len
5053
}
51-
if(readBytes > buf_size) {
54+
if (readBytes > buf_size){
5255
readBytes = buf_size; // not read more the buffer can handle
5356
}
5457

5558
// read data and check if we got something
56-
int numBytesRead = stream.readBytes(buf, readBytes);
57-
if(numBytesRead< 1) {
58-
free(buf); // release the buffer
59+
int numBytesRead = stream.readBytes(buf.get(), readBytes);
60+
if (numBytesRead < 1) {
5961
return false;
6062
}
6163

6264
// Update MD5 with buffer payload
63-
MD5Update(&_ctx, buf, numBytesRead);
65+
MD5Update(&_ctx, buf.get(), numBytesRead);
6466

6567
yield(); // time for network streams
6668

6769
// update available number of bytes
6870
maxLengthLeft -= numBytesRead;
6971
bytesAvailable = stream.available();
7072
}
71-
free(buf);
73+
7274
return true;
7375
}
7476

7577
void MD5Builder::calculate(void){
7678
MD5Final(_buf, &_ctx);
7779
}
7880

79-
void MD5Builder::getBytes(uint8_t * output){
81+
void MD5Builder::getBytes(uint8_t * output) const {
8082
memcpy(output, _buf, 16);
8183
}
8284

83-
void MD5Builder::getChars(char * output){
84-
for(uint8_t i = 0; i < 16; i++) {
85+
void MD5Builder::getChars(char * output) const {
86+
for (uint8_t i=0; i<16; i++){
8587
sprintf(output + (i * 2), "%02x", _buf[i]);
8688
}
8789
}
8890

89-
String MD5Builder::toString(void){
91+
String MD5Builder::toString(void) const {
9092
char out[33];
9193
getChars(out);
9294
return String(out);

cores/esp8266/MD5Builder.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class MD5Builder {
4040
void addHexString(const String& data){ addHexString(data.c_str()); }
4141
bool addStream(Stream & stream, const size_t maxLen);
4242
void calculate(void);
43-
void getBytes(uint8_t * output);
44-
void getChars(char * output);
45-
String toString(void);
43+
void getBytes(uint8_t * output) const;
44+
void getChars(char * output) const;
45+
String toString(void) const;
4646
};
4747

4848

0 commit comments

Comments
 (0)