summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurice <maurice.henke@protonmail.com>2021-04-25 23:34:56 +0200
committerMaurice <maurice.henke@protonmail.com>2021-04-25 23:34:56 +0200
commit1b78e18270c35849a624a77426d2920438eeab4b (patch)
tree11e441712c3ee31b0fbdbee9ccda3146660c2b53
parentc165bb407d08b9afee4aa75146ad0f3ab58b2793 (diff)
Added fletcher16
-rw-r--r--include/tools.h9
-rw-r--r--source/tools.cpp14
2 files changed, 23 insertions, 0 deletions
diff --git a/include/tools.h b/include/tools.h
index 00bb3c0..5855374 100644
--- a/include/tools.h
+++ b/include/tools.h
@@ -14,6 +14,15 @@
namespace libtp::tools
{
/**
+ * @brief Fletcher16 checksum
+ *
+ * @param data Data to calculate the checksum from
+ * @param length How many bytes of data should be considered
+ * @return Checksum in 2 bytes
+ */
+ uint16_t fletcher16( uint8_t* data, int32_t length );
+
+ /**
* @brief Savely loads a given stage-room-spawnpoint-state and event combination
*
* @param stage 8 char stage ID
diff --git a/source/tools.cpp b/source/tools.cpp
index c864a40..7237515 100644
--- a/source/tools.cpp
+++ b/source/tools.cpp
@@ -16,6 +16,20 @@
namespace libtp::tools
{
+ uint16_t fletcher16( uint8_t* data, int32_t length )
+ {
+ uint16_t sum1 = 0;
+ uint16_t sum2 = 0;
+
+ for ( int32_t index = 0; index < length; ++index )
+ {
+ sum1 = ( sum1 + data[index] ) % 0xFF;
+ sum2 = ( sum2 + sum1 ) % 0xFF;
+ }
+
+ return ( sum2 << 8 ) | sum1;
+ }
+
void TriggerSaveLoad( const char* stage, uint8_t room, uint8_t spawn, uint8_t state, uint8_t event )
{
using namespace libtp::tp::d_com_inf_game;