1- #include " Arduino.h"
2- #include " md5.h"
3- #include " MD5Builder.h"
1+ #include < Arduino.h>
2+ #include < MD5Builder.h>
43
5- #define hex_char_to_byte (c ) (((c)>=' a' &&(c)<=' f' )?((c)-87 ):((c)>=' A' &&(c)<=' F' )?((c)-55 ):((c)>=' 0' &&(c)<=' 9' )?((c)-48 ):0 )
4+ uint8_t hex_char_to_byte (uint8_t c)
5+ {
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 ;
9+ }
610
7- void MD5Builder::begin (void ){
8- memset (_buf, 0x00 , 16 );
9- MD5Init (&_ctx);
11+ void MD5Builder::begin (void )
12+ {
13+ memset (_buf, 0x00 , 16 );
14+ MD5Init (&_ctx);
1015}
1116
12- void MD5Builder::add (uint8_t * data, uint16_t len){
13- MD5Update (&_ctx, data, len);
17+ void MD5Builder::add (uint8_t * data, uint16_t len)
18+ {
19+ MD5Update (&_ctx, data, len);
1420}
1521
16- void MD5Builder::addHexString (const char * data){
17- uint16_t i, len = strlen (data);
18- uint8_t * tmp = (uint8_t *)malloc (len/2 );
19- if (tmp == NULL )
20- return ;
21- for (i=0 ; i<len; i+=2 ) tmp[i/2 ] = (hex_char_to_byte (data[i]) & 0x0F ) << 4 | (hex_char_to_byte (data[i+1 ]) & 0x0F );
22- add (tmp, len/2 );
23- free (tmp);
22+ void MD5Builder::addHexString (const char * data)
23+ {
24+ uint16_t i, len = strlen (data);
25+ uint8_t * tmp = (uint8_t *)malloc (len/2 );
26+ if (tmp == NULL ) {
27+ return ;
28+ }
29+ for (i=0 ; i<len; i+=2 ) {
30+ uint8_t high = hex_char_to_byte (data[i]);
31+ uint8_t low = hex_char_to_byte (data[i+1 ]);
32+ tmp[i/2 ] = (high & 0x0F ) << 4 | (low & 0x0F );
33+ }
34+ add (tmp, len/2 );
35+ free (tmp);
2436}
2537
26- bool MD5Builder::addStream (Stream & stream, const size_t maxLen) {
27- const int buf_size = 512 ;
28- int maxLengthLeft = maxLen;
29- uint8_t * buf = (uint8_t *) malloc (buf_size);
38+ bool MD5Builder::addStream (Stream & stream, const size_t maxLen)
39+ {
40+ const int buf_size = 512 ;
41+ int maxLengthLeft = maxLen;
42+ uint8_t * buf = (uint8_t *) malloc (buf_size);
43+
44+ if (!buf) {
45+ return false ;
46+ }
3047
31- if (buf) {
3248 int bytesAvailable = stream.available ();
3349 while ((bytesAvailable > 0 ) && (maxLengthLeft > 0 )) {
3450
3551 // determine number of bytes to read
3652 int readBytes = bytesAvailable;
37- if (readBytes > maxLengthLeft) readBytes = maxLengthLeft ; // read only until max_len
38- if (readBytes > buf_size) readBytes = buf_size; // not read more the buffer can handle
53+ if (readBytes > maxLengthLeft) {
54+ readBytes = maxLengthLeft ; // read only until max_len
55+ }
56+ if (readBytes > buf_size) {
57+ readBytes = buf_size; // not read more the buffer can handle
58+ }
3959
4060 // read data and check if we got something
4161 int numBytesRead = stream.readBytes (buf, readBytes);
42- if (numBytesRead< 1 ) return false ;
62+ if (numBytesRead< 1 ) {
63+ return false ;
64+ }
4365
4466 // Update MD5 with buffer payload
4567 MD5Update (&_ctx, buf, numBytesRead);
4668
47- delay ( 0 ); // time for network streams
69+ yield ( ); // time for network streams
4870
4971 // update available number of bytes
5072 maxLengthLeft -= numBytesRead;
5173 bytesAvailable = stream.available ();
5274 }
75+ printf (" ba: %d mll: %d\n " , bytesAvailable, maxLengthLeft);
5376 free (buf);
5477 return true ;
55- } else {
56- return false ;
57- }
5878}
5979
60- void MD5Builder::calculate (void ){
61- MD5Final (_buf, &_ctx);
80+ void MD5Builder::calculate (void )
81+ {
82+ MD5Final (_buf, &_ctx);
6283}
6384
64- void MD5Builder::getBytes (uint8_t * output){
65- memcpy (output, _buf, 16 );
85+ void MD5Builder::getBytes (uint8_t * output)
86+ {
87+ memcpy (output, _buf, 16 );
6688}
6789
68- void MD5Builder::getChars (char * output){
69- for (uint8_t i = 0 ; i < 16 ; i++)
70- sprintf (output + (i * 2 ), " %02x" , _buf[i]);
90+ void MD5Builder::getChars (char * output)
91+ {
92+ for (uint8_t i = 0 ; i < 16 ; i++) {
93+ sprintf (output + (i * 2 ), " %02x" , _buf[i]);
94+ }
7195}
7296
73- String MD5Builder::toString (void ){
74- char out[33 ];
75- getChars (out);
76- return String (out);
77- }
97+ String MD5Builder::toString (void )
98+ {
99+ char out[33 ];
100+ getChars (out);
101+ return String (out);
102+ }
0 commit comments