1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

MD5Builder::addStream: fixed falsy calculated hash for len > filelength (#2126)

This commit is contained in:
Sven Eliasson
2016-06-13 04:37:05 +02:00
committed by Ivan Grokhotkov
parent 9dd7910aed
commit 3640757692
2 changed files with 22 additions and 27 deletions

View File

@ -23,40 +23,35 @@ void MD5Builder::addHexString(const char * data){
free(tmp); free(tmp);
} }
bool MD5Builder::addStream(Stream & stream, const size_t total_len) { bool MD5Builder::addStream(Stream & stream, const size_t maxLen) {
const int buf_size = 512; const int buf_size = 512;
int bytesleft = total_len; int maxLengthLeft = maxLen;
uint8_t * buf = (uint8_t*) malloc(buf_size); uint8_t * buf = (uint8_t*) malloc(buf_size);
if(buf) { if(buf) {
while((stream.available() > -1) && (bytesleft > 0)) { int bytesAvailable = stream.available();
// get available data size while((bytesAvailable > 0) && (maxLengthLeft > 0)) {
int sizeAvailable = stream.available();
if(sizeAvailable) {
int readBytes = sizeAvailable;
// read only the asked bytes // determine number of bytes to read
if(readBytes > bytesleft) { int readBytes = bytesAvailable;
readBytes = bytesleft ; if(readBytes > maxLengthLeft) readBytes = maxLengthLeft ; // read only until max_len
} if(readBytes > buf_size) readBytes = buf_size; // not read more the buffer can handle
// not read more the buffer can handle // read data and check if we got something
if(readBytes > buf_size) { int numBytesRead = stream.readBytes(buf, readBytes);
readBytes = buf_size; if(numBytesRead< 1) return false;
}
// read data // Update MD5 with buffer payload
int bytesread = stream.readBytes(buf, readBytes); MD5Update(&_ctx, buf, numBytesRead);
bytesleft -= bytesread;
if(bytesread > 0) { delay(0); // time for network streams
MD5Update(&_ctx, buf, bytesread);
} // update available number of bytes
} maxLengthLeft -= numBytesRead;
// time for network streams bytesAvailable = stream.available();
delay(0);
} }
// guaranteed not null
free(buf); free(buf);
return (bytesleft == 0); return true;
} else { } else {
return false; return false;
} }

View File

@ -37,7 +37,7 @@ class MD5Builder {
void addHexString(const char * data); void addHexString(const char * data);
void addHexString(char * data){ addHexString((const char*)data); } void addHexString(char * data){ addHexString((const char*)data); }
void addHexString(String data){ addHexString(data.c_str()); } void addHexString(String data){ addHexString(data.c_str()); }
bool addStream(Stream & stream, const size_t total_len); bool addStream(Stream & stream, const size_t maxLen);
void calculate(void); void calculate(void);
void getBytes(uint8_t * output); void getBytes(uint8_t * output);
void getChars(char * output); void getChars(char * output);