From e31fb1c6296ec281460c16902d0dd2309711903d Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 20 Nov 2024 12:14:14 +0100 Subject: [PATCH] Use Arduino_CloudUtils --- src/ota/implementation/OTAUnoR4.cpp | 1 - src/ota/interface/OTAInterface.h | 2 +- src/ota/interface/OTAInterfaceDefault.cpp | 56 +------- src/ota/interface/OTAInterfaceDefault.h | 4 +- src/tls/utility/SHA256.cpp | 47 ------- src/tls/utility/SHA256.h | 48 ------- src/utility/lzss/lzss.cpp | 163 ---------------------- src/utility/lzss/lzss.h | 108 -------------- 8 files changed, 8 insertions(+), 421 deletions(-) delete mode 100644 src/tls/utility/SHA256.cpp delete mode 100644 src/tls/utility/SHA256.h delete mode 100644 src/utility/lzss/lzss.cpp delete mode 100644 src/utility/lzss/lzss.h diff --git a/src/ota/implementation/OTAUnoR4.cpp b/src/ota/implementation/OTAUnoR4.cpp index 8119c243..f2883b30 100644 --- a/src/ota/implementation/OTAUnoR4.cpp +++ b/src/ota/implementation/OTAUnoR4.cpp @@ -14,7 +14,6 @@ #include "OTAUnoR4.h" #include -#include "tls/utility/SHA256.h" #include "fsp_common_api.h" #include "r_flash_lp.h" #include "WiFi.h" diff --git a/src/ota/interface/OTAInterface.h b/src/ota/interface/OTAInterface.h index a62b7cb2..d2d54f38 100644 --- a/src/ota/interface/OTAInterface.h +++ b/src/ota/interface/OTAInterface.h @@ -18,7 +18,7 @@ #if OTA_ENABLED #include "../OTATypes.h" -#include "tls/utility/SHA256.h" +#include #include #include diff --git a/src/ota/interface/OTAInterfaceDefault.cpp b/src/ota/interface/OTAInterfaceDefault.cpp index 82bfd9e8..0fe8e2eb 100644 --- a/src/ota/interface/OTAInterfaceDefault.cpp +++ b/src/ota/interface/OTAInterfaceDefault.cpp @@ -10,6 +10,7 @@ #include #if OTA_ENABLED && ! defined(OFFLOADED_DOWNLOAD) +#include #include "OTAInterfaceDefault.h" #include "../OTA.h" @@ -128,7 +129,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() { // this could distinguish between consistency of the downloaded bytes and filesize // validate CRC - context->calculatedCrc32 ^= 0xFFFFFFFF; // finalize CRC + arduino::crc32::finalize(context->calculatedCrc32); if(context->header.header.crc32 == context->calculatedCrc32) { DEBUG_VERBOSE("Ota download completed successfully"); res = FlashOTA; @@ -168,7 +169,7 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len) if(sizeof(context->header.buf) == context->headerCopiedBytes) { context->downloadState = OtaDownloadFile; - context->calculatedCrc32 = crc_update( + context->calculatedCrc32 = arduino::crc32::update( context->calculatedCrc32, &(context->header.header.magic_number), sizeof(context->header) - offsetof(ota::OTAHeader, header.magic_number) @@ -188,7 +189,7 @@ void OTADefaultCloudProcessInterface::parseOta(uint8_t* buffer, size_t buf_len) const uint32_t dataLeft = buf_len - (cursor-buffer); context->decoder.decompress(cursor, dataLeft); // TODO verify return value - context->calculatedCrc32 = crc_update( + context->calculatedCrc32 = arduino::crc32::update( context->calculatedCrc32, cursor, dataLeft @@ -246,58 +247,11 @@ OTADefaultCloudProcessInterface::Context::Context( const char* url, std::function putc) : parsed_url(url) , downloadState(OtaDownloadHeader) - , calculatedCrc32(0xFFFFFFFF) + , calculatedCrc32(arduino::crc32::begin()) , headerCopiedBytes(0) , downloadedSize(0) , lastReportTime(0) , writeError(false) , decoder(putc) { } -static const uint32_t crc_table[256] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, - 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, - 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, - 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, - 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, - 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, - 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, - 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, - 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, - 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, - 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, - 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, - 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, - 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, - 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, - 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, - 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, - 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, - 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, - 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, - 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, - 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d -}; - -uint32_t crc_update(uint32_t crc, const void * data, size_t data_len) { - const unsigned char *d = (const unsigned char *)data; - unsigned int tbl_idx; - - while (data_len--) { - tbl_idx = (crc ^ *d) & 0xff; - crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff; - d++; - } - - return crc & 0xffffffff; -} #endif /* OTA_ENABLED && ! defined(OFFLOADED_DOWNLOAD) */ diff --git a/src/ota/interface/OTAInterfaceDefault.h b/src/ota/interface/OTAInterfaceDefault.h index 95384817..b3c86988 100644 --- a/src/ota/interface/OTAInterfaceDefault.h +++ b/src/ota/interface/OTAInterfaceDefault.h @@ -16,7 +16,7 @@ #include #include -#include "utility/lzss/lzss.h" +#include #include "OTAInterface.h" /** @@ -77,7 +77,7 @@ class OTADefaultCloudProcessInterface: public OTACloudProcessInterface { bool writeError; // LZSS decoder - LZSSDecoder decoder; + arduino::lzss::Decoder decoder; const size_t buf_len = 64; uint8_t buffer[64]; diff --git a/src/tls/utility/SHA256.cpp b/src/tls/utility/SHA256.cpp deleted file mode 100644 index ffed6e58..00000000 --- a/src/tls/utility/SHA256.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - This file is part of ArduinoIoTCloud. - - Copyright 2020 ARDUINO SA (http://www.arduino.cc/) - - This software is released under the GNU General Public License version 3, - which covers the main part of arduino-cli. - The terms of this license can be found at: - https://www.gnu.org/licenses/gpl-3.0.en.html - - You can be released from the requirements of the above licenses by purchasing - a commercial license. Buying such a license is mandatory if you want to modify or - otherwise use the software for commercial activities involving the Arduino - software without disclosing the source code of your own applications. To purchase - a commercial license, send an email to license@arduino.cc. -*/ - -/****************************************************************************** - * INCLUDE - ******************************************************************************/ - -#include "SHA256.h" - -/****************************************************************************** - * STATIC MEMBER DECLARATION - ******************************************************************************/ - -constexpr size_t SHA256::HASH_SIZE; - -/****************************************************************************** - * PUBLIC MEMBER FUNCTIONS - ******************************************************************************/ - -void SHA256::begin() -{ - br_sha256_init(&_ctx); -} - -void SHA256::update(uint8_t const * data, size_t const len) -{ - br_sha256_update(&_ctx, data, len); -} - -void SHA256::finalize(uint8_t * hash) -{ - br_sha256_out(&_ctx, hash); -} diff --git a/src/tls/utility/SHA256.h b/src/tls/utility/SHA256.h deleted file mode 100644 index ed0ce1d5..00000000 --- a/src/tls/utility/SHA256.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - This file is part of ArduinoIoTCloud. - - Copyright 2020 ARDUINO SA (http://www.arduino.cc/) - - This software is released under the GNU General Public License version 3, - which covers the main part of arduino-cli. - The terms of this license can be found at: - https://www.gnu.org/licenses/gpl-3.0.en.html - - You can be released from the requirements of the above licenses by purchasing - a commercial license. Buying such a license is mandatory if you want to modify or - otherwise use the software for commercial activities involving the Arduino - software without disclosing the source code of your own applications. To purchase - a commercial license, send an email to license@arduino.cc. -*/ - -#ifndef ARDUINO_TLS_UTILITY_SHA256_H_ -#define ARDUINO_TLS_UTILITY_SHA256_H_ - -/****************************************************************************** - * INCLUDE - ******************************************************************************/ - -#include "../bearssl/bearssl_hash.h" - -/****************************************************************************** - * CLASS DECLARATION - ******************************************************************************/ - -class SHA256 -{ - -public: - - static constexpr size_t HASH_SIZE = 32; - - void begin (); - void update (uint8_t const * data, size_t const len); - void finalize(uint8_t * hash); - -private: - - br_sha256_context _ctx; - -}; - -#endif /* ARDUINO_TLS_UTILITY_SHA256_H_ */ diff --git a/src/utility/lzss/lzss.cpp b/src/utility/lzss/lzss.cpp deleted file mode 100644 index 1a11399f..00000000 --- a/src/utility/lzss/lzss.cpp +++ /dev/null @@ -1,163 +0,0 @@ -/* - This file is part of the ArduinoIoTCloud library. - - Copyright (c) 2024 Arduino SA - - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. - - This implementation took inspiration from https://okumuralab.org/~okumura/compression/lzss.c source code -*/ - -/************************************************************************************** - INCLUDE - **************************************************************************************/ -#include "lzss.h" - -#include - -/************************************************************************************** - LZSS DECODER CLASS IMPLEMENTATION - **************************************************************************************/ - -// get the number of bits the algorithm will try to get given the state -uint8_t LZSSDecoder::bits_required(LZSSDecoder::FSM_STATES s) { - switch(s) { - case FSM_0: - return 1; - case FSM_1: - return 8; - case FSM_2: - return EI; - case FSM_3: - return EJ; - default: - return 0; - } -} - -LZSSDecoder::LZSSDecoder(std::function getc_cbk, std::function putc_cbk) -: available(0), state(FSM_0), put_char_cbk(putc_cbk), get_char_cbk(getc_cbk) { - for (int i = 0; i < N - F; i++) buffer[i] = ' '; - r = N - F; -} - - -LZSSDecoder::LZSSDecoder(std::function putc_cbk) -: available(0), state(FSM_0), put_char_cbk(putc_cbk), get_char_cbk(nullptr) { - for (int i = 0; i < N - F; i++) buffer[i] = ' '; - r = N - F; -} - -LZSSDecoder::status LZSSDecoder::handle_state() { - LZSSDecoder::status res = IN_PROGRESS; - - int c = getbit(bits_required(this->state)); - - if(c == LZSS_BUFFER_EMPTY) { - res = NOT_COMPLETED; - } else if (c == LZSS_EOF) { - res = DONE; - this->state = FSM_EOF; - } else { - switch(this->state) { - case FSM_0: - if(c) { - this->state = FSM_1; - } else { - this->state = FSM_2; - } - break; - case FSM_1: - putc(c); - buffer[r++] = c; - r &= (N - 1); // equivalent to r = r % N when N is a power of 2 - - this->state = FSM_0; - break; - case FSM_2: - this->i = c; - this->state = FSM_3; - break; - case FSM_3: { - int j = c; - - // This is where the actual decompression takes place: we look into the local buffer for reuse - // of byte chunks. This can be improved by means of memcpy and by changing the putc function - // into a put_buf function in order to avoid buffering on the other end. - // TODO improve this section of code - for (int k = 0; k <= j + 1; k++) { - c = buffer[(this->i + k) & (N - 1)]; // equivalent to buffer[(i+k) % N] when N is a power of 2 - putc(c); - buffer[r++] = c; - r &= (N - 1); // equivalent to r = r % N - } - this->state = FSM_0; - - break; - } - case FSM_EOF: - break; - } - } - - return res; -} - -LZSSDecoder::status LZSSDecoder::decompress(uint8_t* const buffer, uint32_t size) { - if(!get_char_cbk) { - this->in_buffer = buffer; - this->available += size; - } - - status res = IN_PROGRESS; - - while((res = handle_state()) == IN_PROGRESS); - - this->in_buffer = nullptr; - - return res; -} - -int LZSSDecoder::getbit(uint8_t n) { // get n bits from buffer - int x=0, c; - - // if the local bit buffer doesn't have enough bit get them - while(buf_size < n) { - switch(c=getc()) { - case LZSS_EOF: - case LZSS_BUFFER_EMPTY: - return c; - } - buf <<= 8; - - buf |= (uint8_t)c; - buf_size += sizeof(uint8_t)*8; - } - - // the result is the content of the buffer starting from msb to n successive bits - x = buf >> (buf_size-n); - - // remove from the buffer the read bits with a mask - buf &= (1<<(buf_size-n))-1; - - buf_size-=n; - - return x; -} - -int LZSSDecoder::getc() { - int c; - - if(get_char_cbk) { - c = get_char_cbk(); - } else if(in_buffer == nullptr || available == 0) { - c = LZSS_BUFFER_EMPTY; - } else { - c = *in_buffer; - in_buffer++; - available--; - } - return c; -} \ No newline at end of file diff --git a/src/utility/lzss/lzss.h b/src/utility/lzss/lzss.h deleted file mode 100644 index bf652026..00000000 --- a/src/utility/lzss/lzss.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - This file is part of the ArduinoIoTCloud library. - - Copyright (c) 2024 Arduino SA - - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. -*/ - -#pragma once - -/************************************************************************************** - INCLUDE - **************************************************************************************/ -#include -#include -#include - -/************************************************************************************** - FUNCTION DEFINITION - **************************************************************************************/ - -/************************************************************************************** - LZSS DECODER CLASS - **************************************************************************************/ - - -class LZSSDecoder { -public: - - /** - * Build an LZSS decoder by providing a callback for storing the decoded bytes - * @param putc_cbk: a callback that takes a char and stores it e.g. a callback to fwrite - */ - LZSSDecoder(std::function putc_cbk); - - /** - * Build an LZSS decoder providing a callback for getting a char and putting a char - * in this way you need to call decompress with no parameters - * @param putc_cbk: a callback that takes a char and stores it e.g. a callback to fwrite - * @param getc_cbk: a callback that returns the next char to consume - * -1 means EOF, -2 means buffer is temporairly finished - */ - LZSSDecoder(std::function getc_cbk, std::function putc_cbk); - - /** - * this enum describes the result of the computation of a single FSM computation - * DONE: the decompression is completed - * IN_PROGRESS: the decompression cycle completed successfully, ready to compute next - * NOT_COMPLETED: the current cycle didn't complete because the available data is not enough - */ - enum status: uint8_t { - DONE, - IN_PROGRESS, - NOT_COMPLETED - }; - - /** - * decode the provided buffer until buffer ends, then pause the process - * @return DONE if the decompression is completed, NOT_COMPLETED if not - */ - status decompress(uint8_t* const buffer=nullptr, uint32_t size=0); - - static const int LZSS_EOF = -1; - static const int LZSS_BUFFER_EMPTY = -2; -private: - // TODO provide a way for the user to set these parameters - static const int EI = 11; /* typically 10..13 */ - static const int EJ = 4; /* typically 4..5 */ - static const int N = (1 << EI); /* buffer size */ - static const int F = ((1 << EJ) + 1); /* lookahead buffer size */ - - // algorithm specific buffer used to store text that could be later referenced and copied - uint8_t buffer[N * 2]; - - // this function gets 1 single char from the input buffer - int getc(); - uint8_t* in_buffer = nullptr; - uint32_t available = 0; - - status handle_state(); - - // get 1 bit from the available input buffer - int getbit(uint8_t n); - // the following 2 are variables used by getbits - uint32_t buf, buf_size=0; - - enum FSM_STATES: uint8_t { - FSM_0 = 0, - FSM_1 = 1, - FSM_2 = 2, - FSM_3 = 3, - FSM_EOF - } state; - - // these variable are used in a decode session and specific to the old C implementation - // there is no documentation about their meaning - int i, r; - - std::function put_char_cbk; - std::function get_char_cbk; - - inline void putc(const uint8_t c) { if(put_char_cbk) { put_char_cbk(c); } } - - // get the number of bits the FSM will require given its state - uint8_t bits_required(FSM_STATES s); -}; \ No newline at end of file