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
1011void MD5Builder::begin (void ){
@@ -18,25 +19,27 @@ void MD5Builder::add(const uint8_t * data, const uint16_t len){
1819
1920void 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
7577void 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);
0 commit comments