1
- #include " Arduino.h"
2
- #include " md5.h"
3
- #include " MD5Builder.h"
1
+ #include < Arduino.h>
2
+ #include < MD5Builder.h>
4
3
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
+ }
6
10
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);
10
15
}
11
16
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);
14
20
}
15
21
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);
24
36
}
25
37
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
+ }
30
47
31
- if (buf) {
32
48
int bytesAvailable = stream.available ();
33
49
while ((bytesAvailable > 0 ) && (maxLengthLeft > 0 )) {
34
50
35
51
// determine number of bytes to read
36
52
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
+ }
39
59
40
60
// read data and check if we got something
41
61
int numBytesRead = stream.readBytes (buf, readBytes);
42
- if (numBytesRead< 1 ) return false ;
62
+ if (numBytesRead< 1 ) {
63
+ return false ;
64
+ }
43
65
44
66
// Update MD5 with buffer payload
45
67
MD5Update (&_ctx, buf, numBytesRead);
46
68
47
- delay ( 0 ); // time for network streams
69
+ yield ( ); // time for network streams
48
70
49
71
// update available number of bytes
50
72
maxLengthLeft -= numBytesRead;
51
73
bytesAvailable = stream.available ();
52
74
}
75
+ printf (" ba: %d mll: %d\n " , bytesAvailable, maxLengthLeft);
53
76
free (buf);
54
77
return true ;
55
- } else {
56
- return false ;
57
- }
58
78
}
59
79
60
- void MD5Builder::calculate (void ){
61
- MD5Final (_buf, &_ctx);
80
+ void MD5Builder::calculate (void )
81
+ {
82
+ MD5Final (_buf, &_ctx);
62
83
}
63
84
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 );
66
88
}
67
89
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
+ }
71
95
}
72
96
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