@@ -98,7 +98,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
9898 goto exit;
9999 }
100100
101- if (http_client->available () == 0 ) {
101+ if (! http_client->available ()) {
102102 /* Avoid tight loop and allow yield */
103103 delay (1 );
104104 continue ;
@@ -159,10 +159,10 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
159159 for (uint8_t * cursor=(uint8_t *)buffer; cursor<buffer+buf_len; ) {
160160 switch (context->downloadState ) {
161161 case OtaDownloadHeader: {
162- uint32_t copied = buf_len < sizeof (context->header .buf ) ? buf_len : sizeof (context->header .buf );
163- memcpy (context->header .buf +context->headerCopiedBytes , buffer, copied );
164- cursor += copied ;
165- context->headerCopiedBytes += copied ;
162+ const uint32_t headerLeft = context-> headerCopiedBytes + buf_len <= sizeof (context->header .buf ) ? buf_len : sizeof (context->header .buf ) - context-> headerCopiedBytes ;
163+ memcpy (context->header .buf +context->headerCopiedBytes , buffer, headerLeft );
164+ cursor += headerLeft ;
165+ context->headerCopiedBytes += headerLeft ;
166166
167167 // when finished go to next state
168168 if (sizeof (context->header .buf ) == context->headerCopiedBytes ) {
@@ -178,22 +178,25 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len)
178178 context->downloadState = OtaDownloadMagicNumberMismatch;
179179 return ;
180180 }
181+ context->downloadedSize += sizeof (context->header .buf );
181182 }
182183
183184 break ;
184185 }
185186 case OtaDownloadFile: {
186- uint32_t contentLength = http_client->contentLength ();
187- context->decoder .decompress (cursor, buf_len - (cursor-buffer)); // TODO verify return value
187+ const uint32_t contentLength = http_client->contentLength ();
188+ const uint32_t dataLeft = buf_len - (cursor-buffer);
189+ context->decoder .decompress (cursor, dataLeft); // TODO verify return value
188190
189191 context->calculatedCrc32 = crc_update (
190192 context->calculatedCrc32 ,
191193 cursor,
192- buf_len - (cursor-buffer)
194+ dataLeft
193195 );
194196
195- cursor += buf_len - (cursor-buffer);
196- context->downloadedSize += (cursor-buffer);
197+ cursor += dataLeft;
198+ context->downloadedSize += dataLeft;
199+
197200
198201 if ((millis () - context->lastReportTime ) > 10000 ) { // Report the download progress each X millisecond
199202 DEBUG_VERBOSE (" OTA Download Progress %d/%d" , context->downloadedSize , contentLength);
0 commit comments