From 7d54f5211c47dd200ccafb80f3eb5970d2e7dc62 Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Thu, 31 Dec 2015 15:04:26 +0000 Subject: [PATCH 1/3] Improvements for MD5Builder with Stream --- cores/esp8266/FS.h | 4 +++- cores/esp8266/MD5Builder.cpp | 40 ++++++++++++++++++++++++++++++++++++ cores/esp8266/MD5Builder.h | 1 + cores/esp8266/Stream.h | 4 ++-- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 07b58c040f..e66fcd7726 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -59,7 +59,9 @@ class File : public Stream int read() override; int peek() override; void flush() override; - + size_t readBytes(char *buffer, size_t length) override { + return read((uint8_t*)buffer, length); + } size_t read(uint8_t* buf, size_t size); bool seek(uint32_t pos, SeekMode mode); size_t position() const; diff --git a/cores/esp8266/MD5Builder.cpp b/cores/esp8266/MD5Builder.cpp index 0957785ce6..6539d1ae61 100644 --- a/cores/esp8266/MD5Builder.cpp +++ b/cores/esp8266/MD5Builder.cpp @@ -23,6 +23,46 @@ void MD5Builder::addHexString(const char * data){ free(tmp); } +bool MD5Builder::addStream(Stream & stream, const int total_len) { + const int buf_size = 512; + int bytesleft = total_len; + uint8_t * buf = (uint8_t*) malloc(buf_size); + if(buf) { + while((stream.available() > -1) && (bytesleft > 0)) { + + // get available data size + int sizeAvailable = stream.available(); + if(sizeAvailable) { + int readBytes = sizeAvailable; + + // read only the asked bytes + if(readBytes > bytesleft) { + readBytes = bytesleft ; + } + + // not read more the buffer can handle + if(readBytes > buf_size) { + readBytes = buf_size; + } + + // read data + int bytesread = stream.readBytes(buf, readBytes); + bytesleft -= bytesread; + if(bytesread > 0) { + MD5Update(&_ctx, buf, bytesread); + } + } + // time for network streams + delay(0); + } + // not free null ptr + free(buf); + return (bytesleft == 0); + } else { + return false; + } +} + void MD5Builder::calculate(void){ MD5Final(_buf, &_ctx); } diff --git a/cores/esp8266/MD5Builder.h b/cores/esp8266/MD5Builder.h index 9c345de99c..aa6df45844 100644 --- a/cores/esp8266/MD5Builder.h +++ b/cores/esp8266/MD5Builder.h @@ -37,6 +37,7 @@ class MD5Builder { void addHexString(const char * data); void addHexString(char * data){ addHexString((const char*)data); } void addHexString(String data){ addHexString(data.c_str()); } + void addStream(Stream & stream, size_t len); void calculate(void); void getBytes(uint8_t * output); void getChars(char * output); diff --git a/cores/esp8266/Stream.h b/cores/esp8266/Stream.h index 14eacbf6c3..6d2646d77c 100644 --- a/cores/esp8266/Stream.h +++ b/cores/esp8266/Stream.h @@ -87,8 +87,8 @@ class Stream: public Print { float parseFloat(); // float version of parseInt - size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer - size_t readBytes(uint8_t *buffer, size_t length) { + virtual size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer + virtual size_t readBytes(uint8_t *buffer, size_t length) { return readBytes((char *) buffer, length); } // terminates if length characters have been read or timeout (see setTimeout) From 34db483bb5e0e48df22b38a65947c3eb066cba60 Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Fri, 1 Jan 2016 13:18:24 +0000 Subject: [PATCH 2/3] fix wrong return type --- cores/esp8266/MD5Builder.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/MD5Builder.h b/cores/esp8266/MD5Builder.h index aa6df45844..274aa0a553 100644 --- a/cores/esp8266/MD5Builder.h +++ b/cores/esp8266/MD5Builder.h @@ -37,7 +37,7 @@ class MD5Builder { void addHexString(const char * data); void addHexString(char * data){ addHexString((const char*)data); } void addHexString(String data){ addHexString(data.c_str()); } - void addStream(Stream & stream, size_t len); + bool addStream(Stream & stream, int len); void calculate(void); void getBytes(uint8_t * output); void getChars(char * output); From 2cf902cb7655bb821b202aab4ef628e48582bb24 Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Sat, 9 Jan 2016 10:30:28 +0200 Subject: [PATCH 3/3] use size_t in md5builder stream --- cores/esp8266/MD5Builder.cpp | 2 +- cores/esp8266/MD5Builder.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/MD5Builder.cpp b/cores/esp8266/MD5Builder.cpp index 6539d1ae61..a382093a4a 100644 --- a/cores/esp8266/MD5Builder.cpp +++ b/cores/esp8266/MD5Builder.cpp @@ -23,7 +23,7 @@ void MD5Builder::addHexString(const char * data){ free(tmp); } -bool MD5Builder::addStream(Stream & stream, const int total_len) { +bool MD5Builder::addStream(Stream & stream, const size_t total_len) { const int buf_size = 512; int bytesleft = total_len; uint8_t * buf = (uint8_t*) malloc(buf_size); diff --git a/cores/esp8266/MD5Builder.h b/cores/esp8266/MD5Builder.h index 274aa0a553..ec1ac83114 100644 --- a/cores/esp8266/MD5Builder.h +++ b/cores/esp8266/MD5Builder.h @@ -37,7 +37,7 @@ class MD5Builder { void addHexString(const char * data); void addHexString(char * data){ addHexString((const char*)data); } void addHexString(String data){ addHexString(data.c_str()); } - bool addStream(Stream & stream, int len); + bool addStream(Stream & stream, const size_t total_len); void calculate(void); void getBytes(uint8_t * output); void getChars(char * output);