diff --git a/apps/graph/cartesian_function_store.cpp b/apps/graph/cartesian_function_store.cpp index 087fcb3a8..7c74dacea 100644 --- a/apps/graph/cartesian_function_store.cpp +++ b/apps/graph/cartesian_function_store.cpp @@ -18,13 +18,13 @@ CartesianFunctionStore::CartesianFunctionStore() : } uint32_t CartesianFunctionStore::storeChecksum() { - size_t dataLengthInBytes = k_maxNumberOfFunctions*TextField::maxBufferSize()*sizeof(char); + size_t dataLengthInBytes = k_maxNumberOfFunctions*sizeof(uint32_t); assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4 - char data[k_maxNumberOfFunctions*TextField::maxBufferSize()] = {}; + uint32_t checksums[k_maxNumberOfFunctions]; for (int i = 0; i < k_maxNumberOfFunctions; i++) { - strlcpy(data+i*TextField::maxBufferSize(), m_functions[i].text(), TextField::maxBufferSize()); + checksums[i] = m_functions[i].checksum(); } - return Ion::crc32((uint32_t *)data, dataLengthInBytes>>2); + return Ion::crc32((uint32_t *)checksums, dataLengthInBytes>>2); } CartesianFunction * CartesianFunctionStore::functionAtIndex(int i) { diff --git a/apps/sequence/sequence.cpp b/apps/sequence/sequence.cpp index a8a7e348e..cb4470ea6 100644 --- a/apps/sequence/sequence.cpp +++ b/apps/sequence/sequence.cpp @@ -2,6 +2,7 @@ #include "local_context.h" #include "../../poincare/src/layout/string_layout.h" #include "../../poincare/src/layout/baseline_relative_layout.h" +#include #include using namespace Shared; @@ -81,6 +82,16 @@ Sequence& Sequence::operator=(const Sequence& other) { return *this; } +uint32_t Sequence::checksum() { + size_t dataLengthInBytes = 3*TextField::maxBufferSize()*sizeof(char); + assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4 + char data[3*TextField::maxBufferSize()] = {}; + strlcpy(data, text(), TextField::maxBufferSize()); + strlcpy(data+TextField::maxBufferSize(), firstInitialConditionText(), TextField::maxBufferSize()); + strlcpy(data+2*TextField::maxBufferSize(), secondInitialConditionText(), TextField::maxBufferSize()); + return Ion::crc32((uint32_t *)data, dataLengthInBytes>>2); +} + const char * Sequence::firstInitialConditionText() { return m_firstInitialConditionText; } diff --git a/apps/sequence/sequence.h b/apps/sequence/sequence.h index 09563df98..e794192b4 100644 --- a/apps/sequence/sequence.h +++ b/apps/sequence/sequence.h @@ -18,6 +18,7 @@ public: Sequence& operator=(Sequence&& other) = delete; Sequence(const Sequence& other) = delete; Sequence(Sequence&& other) = delete; + uint32_t checksum() override; Type type(); void setType(Type type); const char * firstInitialConditionText(); diff --git a/apps/sequence/sequence_store.cpp b/apps/sequence/sequence_store.cpp index 7a107baf7..991d30762 100644 --- a/apps/sequence/sequence_store.cpp +++ b/apps/sequence/sequence_store.cpp @@ -11,15 +11,13 @@ constexpr KDColor SequenceStore::k_defaultColors[k_maxNumberOfSequences]; constexpr const char * SequenceStore::k_sequenceNames[k_maxNumberOfSequences]; uint32_t SequenceStore::storeChecksum() { - size_t dataLengthInBytes = k_maxNumberOfSequences*3*TextField::maxBufferSize()*sizeof(char); + size_t dataLengthInBytes = k_maxNumberOfSequences*sizeof(uint32_t); assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4 - char data[3*k_maxNumberOfSequences*TextField::maxBufferSize()] = {}; + uint32_t checksums[k_maxNumberOfSequences]; for (int i = 0; i < k_maxNumberOfSequences; i++) { - strlcpy(data+i*3*TextField::maxBufferSize(), m_sequences[i].text(), TextField::maxBufferSize()); - strlcpy(data+i*3*TextField::maxBufferSize()+TextField::maxBufferSize(), m_sequences[i].firstInitialConditionText(), TextField::maxBufferSize()); - strlcpy(data+i*3*TextField::maxBufferSize()+2*TextField::maxBufferSize(), m_sequences[i].secondInitialConditionText(), TextField::maxBufferSize()); + checksums[i] = m_sequences[i].checksum(); } - return Ion::crc32((uint32_t *)data, dataLengthInBytes>>2); + return Ion::crc32((uint32_t *)checksums, dataLengthInBytes>>2); } Sequence * SequenceStore::functionAtIndex(int i) { diff --git a/apps/shared/function.cpp b/apps/shared/function.cpp index 354ba28f9..c53c70e88 100644 --- a/apps/shared/function.cpp +++ b/apps/shared/function.cpp @@ -1,6 +1,7 @@ #include "function.h" #include #include +#include using namespace Poincare; @@ -25,6 +26,14 @@ Function& Function::operator=(const Function& other) { return *this; } +uint32_t Function::checksum() { + size_t dataLengthInBytes = TextField::maxBufferSize()*sizeof(char); + assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4 + char data[TextField::maxBufferSize()] = {}; + strlcpy(data, m_text, TextField::maxBufferSize()); + return Ion::crc32((uint32_t *)data, dataLengthInBytes>>2); +} + void Function::setContent(const char * c) { strlcpy(m_text, c, sizeof(m_text)); if (m_layout != nullptr) { diff --git a/apps/shared/function.h b/apps/shared/function.h index cdf64bc4c..1483d28a0 100644 --- a/apps/shared/function.h +++ b/apps/shared/function.h @@ -15,6 +15,7 @@ public: Function& operator=(Function&& other) = delete; Function(const Function& other) = delete; Function(Function&& other) = delete; + virtual uint32_t checksum(); const char * text() const; const char * name() const; KDColor color() const { return m_color; }