1
1
#include < Arduino.h>
2
2
#include < MD5Builder.h>
3
+ #include < memory>
3
4
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 ;
8
9
}
9
10
10
11
void MD5Builder::begin (void ){
@@ -18,25 +19,27 @@ void MD5Builder::add(const uint8_t * data, const uint16_t len){
18
19
19
20
void MD5Builder::addHexString (const char * data){
20
21
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) {
23
25
return ;
24
26
}
27
+
25
28
for (i=0 ; i<len; i+=2 ) {
26
29
uint8_t high = hex_char_to_byte (data[i]);
27
30
uint8_t low = hex_char_to_byte (data[i+1 ]);
28
31
tmp[i/2 ] = (high & 0x0F ) << 4 | (low & 0x0F );
29
32
}
30
- add (tmp, len/2 );
31
- free (tmp);
33
+ add (tmp.get (), len/2 );
32
34
}
33
35
34
- bool MD5Builder::addStream (Stream & stream, const size_t maxLen){
36
+ bool MD5Builder::addStream (Stream &stream, const size_t maxLen) {
35
37
const int buf_size = 512 ;
36
38
int maxLengthLeft = maxLen;
37
- uint8_t * buf = (uint8_t *) malloc (buf_size);
38
39
39
- if (!buf) {
40
+ auto buf = std::unique_ptr<uint8_t []>{new (std::nothrow) uint8_t [buf_size]};
41
+
42
+ if (!buf) {
40
43
return false ;
41
44
}
42
45
@@ -45,48 +48,47 @@ bool MD5Builder::addStream(Stream & stream, const size_t maxLen){
45
48
46
49
// determine number of bytes to read
47
50
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
50
53
}
51
- if (readBytes > buf_size) {
54
+ if (readBytes > buf_size){
52
55
readBytes = buf_size; // not read more the buffer can handle
53
56
}
54
57
55
58
// 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 ) {
59
61
return false ;
60
62
}
61
63
62
64
// Update MD5 with buffer payload
63
- MD5Update (&_ctx, buf, numBytesRead);
65
+ MD5Update (&_ctx, buf. get () , numBytesRead);
64
66
65
67
yield (); // time for network streams
66
68
67
69
// update available number of bytes
68
70
maxLengthLeft -= numBytesRead;
69
71
bytesAvailable = stream.available ();
70
72
}
71
- free (buf);
73
+
72
74
return true ;
73
75
}
74
76
75
77
void MD5Builder::calculate (void ){
76
78
MD5Final (_buf, &_ctx);
77
79
}
78
80
79
- void MD5Builder::getBytes (uint8_t * output){
81
+ void MD5Builder::getBytes (uint8_t * output) const {
80
82
memcpy (output, _buf, 16 );
81
83
}
82
84
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++){
85
87
sprintf (output + (i * 2 ), " %02x" , _buf[i]);
86
88
}
87
89
}
88
90
89
- String MD5Builder::toString (void ){
91
+ String MD5Builder::toString (void ) const {
90
92
char out[33 ];
91
93
getChars (out);
92
94
return String (out);
0 commit comments