@@ -444,22 +444,7 @@ uint32_t EspClass::getSketchSize() {
444444 DEBUG_SERIAL.printf (" section=%u size=%u pos=%u\r\n " , section_index, section_header.size , pos);
445445#endif
446446 }
447- uint8_t buff[16 ] = {0 };
448-
449- if (spi_flash_read (pos, (uint32_t *) buff, 16 )) {
450- return 0 ;
451- }
452- uint8_t index = 0 ;
453- for (index = 0 ; index < 16 ; index++) {
454- if (buff[index] == 255 ) { break ; }
455- }
456- #ifdef DEBUG_SERIAL
457- DEBUG_SERIAL.printf (" Last 16bytes: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\r\n " ,
458- buff[0 ],buff[1 ],buff[2 ],buff[3 ],buff[4 ],buff[5 ],buff[6 ],buff[7 ],
459- buff[8 ],buff[9 ],buff[10 ],buff[11 ],buff[12 ],buff[13 ],buff[14 ],buff[15 ]);
460- DEBUG_SERIAL.printf (" end offset index = %u\r\n " , index );
461- #endif
462- result = pos + index;
447+ result = (pos + 16 ) & ~15 ;
463448 return result;
464449}
465450
@@ -536,52 +521,32 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
536521 return rc == 0 ;
537522}
538523
539-
540-
541524String EspClass::getSketchMD5 ()
542525{
543-
544- const int buf_size = 512 ;
545- uint32_t offset = 0 ;
546- uint32_t maxLengthLeft = getSketchSize ();
547- uint8_t * buf = (uint8_t *) malloc (buf_size);
548- uint8_t remainder = 0 ;
549-
550- if (!buf) {
551- return " 0" ;
526+ static String result;
527+ if (result.length ()) {
528+ return result;
529+ }
530+ uint32_t lengthLeft = getSketchSize ();
531+ const size_t bufSize = 512 ;
532+ std::unique_ptr<uint8_t []> buf (new uint8_t [bufSize]);
533+ uint32_t offset = 0 ;
534+ if (!buf.get ()) {
535+ return String ();
552536 }
553-
554537 MD5Builder md5;
555538 md5.begin ();
556-
557- while ( maxLengthLeft > 0 ) {
558-
559- size_t readBytes = maxLengthLeft;
560-
561- if (readBytes > buf_size) {
562- readBytes = buf_size;
563- }
564-
565- if (readBytes < 4 ) {
566- remainder = readBytes;
567- readBytes = 4 ;
568- }
569-
570- if ( flashRead (offset, (uint32_t *)buf, readBytes) ) {
571- if (!remainder) {
572- md5.add (buf, readBytes);
573- } else {
574- md5.add (buf, remainder);
539+ while ( lengthLeft > 0 ) {
540+ size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
541+ if (!flashRead (offset, reinterpret_cast <uint32_t *>(buf.get ()), (readBytes + 3 ) & ~3 )) {
542+ return String ();
575543 }
576- offset += readBytes;
577- maxLengthLeft -= readBytes;
578-
579- }
580-
544+ md5.add (buf.get (), readBytes);
545+ lengthLeft -= readBytes;
546+ offset += readBytes;
581547 }
582-
583548 md5.calculate ();
584-
585- return md5. toString ();
549+ result = md5. toString ();
550+ return result;
586551}
587552
0 commit comments