From fdd80761ff68fdb71bcb27bad08daef637f0e8ca Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 14 Nov 2025 16:37:47 +0100 Subject: [PATCH 1/3] File system for persistent data implemented. Basically operating. --- Generic_Algorithms/CRC.h | 44 ++++ Generic_Algorithms/CRCtable.cpp | 40 ++++ NAV_Algorithms/persistent_data_file.h | 298 ++++++++++++++++++++++++++ 3 files changed, 382 insertions(+) create mode 100644 Generic_Algorithms/CRC.h create mode 100644 Generic_Algorithms/CRCtable.cpp create mode 100644 NAV_Algorithms/persistent_data_file.h diff --git a/Generic_Algorithms/CRC.h b/Generic_Algorithms/CRC.h new file mode 100644 index 0000000..f252c00 --- /dev/null +++ b/Generic_Algorithms/CRC.h @@ -0,0 +1,44 @@ +/* + * CRC.h + * + * Created on: Jun 27, 2013 + * Author: schaefer + */ + +#ifndef CRC_H_ +#define CRC_H_ + +#include +#include "CRC.h" +#include "embedded_memory.h" + +extern ROM uint16_t CRCtable[]; + +static inline uint16_t CRC16( const uint16_t input, uint16_t crc) +{ + uint8_t carry=(crc >> 8) ^ input; + crc = (crc << 8) ^ CRCtable[carry]; + return crc; +} + +static inline uint16_t CRC16_blockcheck_bytes( const uint8_t *input, uint16_t length) +{ + uint16_t crc=0; + while( length--) + { + crc = CRC16( (const uint16_t)(*input++), crc); + } + return crc; +} + +static inline uint16_t CRC16_blockcheck( const uint16_t *input, uint16_t length) +{ + uint16_t crc=0; + while( length--) + { + crc = CRC16( *input++, crc); + } + return crc; +} + +#endif /* CRC_H_ */ diff --git a/Generic_Algorithms/CRCtable.cpp b/Generic_Algorithms/CRCtable.cpp new file mode 100644 index 0000000..4065379 --- /dev/null +++ b/Generic_Algorithms/CRCtable.cpp @@ -0,0 +1,40 @@ +#include +#include "CRC.h" +#include "embedded_memory.h" + +ROM uint16_t CRCtable[]= +{ +0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, +0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, +0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, +0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, +0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, +0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, +0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, +0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, +0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, +0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, +0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, +0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, +0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, +0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, +0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, +0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, +0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, +0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, +0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, +0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, +0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, +0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, +0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, +0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, +0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, +0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, +0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, +0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, +0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, +0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, +0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, +0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 +}; + diff --git a/NAV_Algorithms/persistent_data_file.h b/NAV_Algorithms/persistent_data_file.h new file mode 100644 index 0000000..ca28885 --- /dev/null +++ b/NAV_Algorithms/persistent_data_file.h @@ -0,0 +1,298 @@ +#ifndef NAV_ALGORITHMS_PERSISTENT_DATA_FILE_H_ +#define NAV_ALGORITHMS_PERSISTENT_DATA_FILE_H_ + +#define DEBUG + +#include "stdint.h" +#include "assert.h" +#include "CRC.h" + +class node +{ +public: + enum{ DIRECT_8_BIT=0}; + + typedef uint8_t ID_t; + + node * next( void) + { + return this + size; + } + + ID_t id; + uint8_t size; // size in 32-bit units of entry including sizeof( node) + uint16_t data; // direct 8bit + checksum OR crc of 32bit data file +}; + +#ifdef DEBUG + +void FLASH_write( uint32_t * dest, uint32_t * source, unsigned n_words) +{ + memcpy( dest, source, n_words * sizeof( uint32_t)); +} + +#endif + +class file_system +{ +public: + file_system( void * begin, void * behind_end) + : head( (node *)begin), + free_space( begin), + end( behind_end) + { + free_space = find_end(); + } + + node * find_datum( node::ID_t id) + { + return find_last_datum( head, id); + } + + bool store_data( node::ID_t id, unsigned data_size_words, const void * data) + { + node * next_entry = (node *)free_space; + + if( next_entry + data_size_words + 1 >= end) + return false; // no more room ! + + node temp_node; + temp_node.id = id; + temp_node.size = data_size_words + 1; // size including node itself + + if( data_size_words == node::DIRECT_8_BIT) // if direct 16bit data + { + uint16_t checked_datum = *(uint8_t *)data; // take only 8 bits + checked_datum |= ( (~checked_datum) << 8); + temp_node.data = checked_datum; + + FLASH_write( (uint32_t *)next_entry, (uint32_t *)&temp_node, 1); + } + else // : bunch of 32bit data + { + temp_node.data = CRC16_blockcheck( (uint16_t *)data, data_size_words * sizeof( uint16_t)); + + FLASH_write( (uint32_t *)next_entry, (uint32_t *)&temp_node, 1); + FLASH_write( (uint32_t *)(next_entry + 1), (uint32_t *)data, data_size_words * sizeof( uint32_t)); + } + + free_space = (uint32_t *)free_space + data_size_words + 1; + return true; + } + + bool store_data( node::ID_t id, const uint8_t data) + { + return store_data( id, node::DIRECT_8_BIT, &data); + } + + bool retrieve_data( node::ID_t id, uint32_t * target, unsigned data_size) + { + unsigned size_including_node = data_size +1; // ... including node itself + node * candidate = find_last_datum( head, id); + if( candidate == 0 || size_including_node != candidate->size) + return false; + + uint32_t *from = (uint32_t *)candidate + 1; + uint16_t crc = CRC16_blockcheck( (uint16_t *)from, data_size * sizeof( uint16_t)); + + if( candidate->data != crc) + return false; + + do + { + *target++ = *from++; + --data_size; + } + while( data_size != 0); + + return true; + } + + bool retrieve_data( node::ID_t id, uint16_t & target) + { + node * my_node = find_last_datum( head, id); + if( my_node == 0) + return false; + + if( my_node->size != 1) + return false; + + target = my_node->data; + return true; + } + +#ifdef DEBUG + + void dump_all_entries( void) + { + node * the_node; + for( node::ID_t id=1; id < 255; ++id) + { + the_node = find_last_datum( head, id); + if( the_node != 0) + { + printf( "ID: %d ", the_node->id); + + if( the_node->size == 1) + printf( "val = %02x\n", the_node->data & 0xff); + else + { + printf( "val ="); + for( unsigned i = 0; i < the_node->size - 1; ++i) + printf( " %08x", * ((unsigned *) the_node + 1 + i)); + printf( "\n"); + } + } + } +#endif + } + + bool is_consistent(void) + { + //check all nodes for consistency + for( node * work = head; *(uint32_t *)work != 0xffffffff && work < free_space; work = work->next()) + { + if( work->size == 1) // we have found a dircect-data-entry + { + if( ((work->data & 0xff) ^ (work->data >> 8)) != 0xff) + return false; // invalid data pattern + } + else + { + uint16_t crc = CRC16_blockcheck( (uint16_t *)work + 2, (work->size - 1) * 2); + if( work->data != crc) + return false; // found bad entry + } + } + return true; + } + +private: + + node * find_last_datum( node * start, node::ID_t id=0xff) + { + node * thisone = find_first_datum( start, id); + node * candidate=0; + while( thisone != 0) + { + candidate = thisone; + thisone = find_first_datum( candidate->next(), id); + } + return candidate; + } + + node * find_end( void) + { + node * work = head; + + while( (work < free_space) && (work->size != 0xff)) + { + + work = work->next(); + if( work >= end) + { + work = 0; + break; + } + } + + if( work >= end) + work = 0; + + return work; + } + + node * find_first_datum( node * start, node::ID_t id) + { + for( node * work = start; (work < free_space) && (work->size != 0) && (work->size != 0xff); work = work->next()) + { + if( work->id == id) + return work; + } + return 0; + } + + node * head; + void * free_space; + void * end; +}; + +#ifdef DEBUG + +#define STORAGE_SIZE 1024 +uint32_t storage[STORAGE_SIZE]; + +void test_storage( void) +{ + bool success; + memset( storage, 0xff, STORAGE_SIZE * sizeof(uint32_t)); + file_system file( storage, storage+STORAGE_SIZE); + + uint8_t datum1 = 0x34; + file.store_data(1, node::DIRECT_8_BIT, &datum1); + + success = file.is_consistent(); + + file.dump_all_entries(); + + unsigned datum2[10]={ 1, 2, 3, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}; + file.store_data(2, 3, datum2); + + success = file.is_consistent(); + file.dump_all_entries(); + + datum1 = 0x21; + file.store_data(1, datum1); + + success = file.is_consistent(); + file.dump_all_entries(); + + uint16_t test = 0xff; + success = file.retrieve_data( 1, test); + + datum2[0]=4; + datum2[1]=5; + datum2[2]=6; + success = file.retrieve_data( 2, datum2, 3); + + success = file.is_consistent(); + file.dump_all_entries(); + + do + { + ++datum2[0]; + success = file.store_data(2, 3, datum2); + } + while( success == true); + + success = file.is_consistent(); + file.dump_all_entries(); + + datum2[0]=0; + datum2[1]=0; + datum2[2]=0; + success = file.retrieve_data( 2, datum2, 3); + + success = file.is_consistent(); + file.dump_all_entries(); + + do + { + ++datum1; + success = file.store_data(1, 0, &datum1); + } + while( success); + + success = file.is_consistent(); + file.dump_all_entries(); + + success = file.retrieve_data( 1, test); + file.dump_all_entries(); + + success = file.retrieve_data( 13, test); + success = file.retrieve_data( 0xff, test); +} + +#endif + +#endif /* NAV_ALGORITHMS_PERSISTENT_DATA_FILE_H_ */ From fc480d62a116354f8feb723119b751713dbd7f34 Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Fri, 14 Nov 2025 17:36:31 +0100 Subject: [PATCH 2/3] Split C and H files. --- NAV_Algorithms/persistent_data_file.cpp | 113 ++++++++++++++++++++ NAV_Algorithms/persistent_data_file.h | 131 ++++++++---------------- 2 files changed, 155 insertions(+), 89 deletions(-) create mode 100644 NAV_Algorithms/persistent_data_file.cpp diff --git a/NAV_Algorithms/persistent_data_file.cpp b/NAV_Algorithms/persistent_data_file.cpp new file mode 100644 index 0000000..59d1193 --- /dev/null +++ b/NAV_Algorithms/persistent_data_file.cpp @@ -0,0 +1,113 @@ +/***********************************************************************//** + * @file persistent_data_file.cpp + * @brief tiny file system for configuration data + * @author Dr. Klaus Schaefer + * @copyright Copyright 2021 Dr. Klaus Schaefer. All rights reserved. + * @license This project is released under the GNU Public License GPL-3.0 + + + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + **************************************************************************/ +#include "persistent_data_file.h" + +#ifdef DEBUG + +void FLASH_write( uint32_t * dest, uint32_t * source, unsigned n_words) +{ + memcpy( dest, source, n_words * sizeof( uint32_t)); +} + +#define STORAGE_SIZE 1024 +uint32_t storage[STORAGE_SIZE]; + +void test_storage( void) +{ + bool success; + memset( storage, 0xff, STORAGE_SIZE * sizeof(uint32_t)); + file_system file( storage, storage+STORAGE_SIZE); + + uint8_t datum1 = 0x34; + file.store_data(1, node::DIRECT_8_BIT, &datum1); + + success = file.is_consistent(); + + file.dump_all_entries(); + + unsigned datum2[10]={ 1, 2, 3, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}; + file.store_data(2, 3, datum2); + + success = file.is_consistent(); + file.dump_all_entries(); + + datum1 = 0x21; + file.store_data(1, datum1); + + success = file.is_consistent(); + file.dump_all_entries(); + + uint16_t test = 0xff; + success = file.retrieve_data( 1, test); + + datum2[0]=4; + datum2[1]=5; + datum2[2]=6; + success = file.retrieve_data( 2, datum2, 3); + + success = file.is_consistent(); + file.dump_all_entries(); + + do + { + ++datum2[0]; + success = file.store_data(2, 3, datum2); + } + while( success == true); + + success = file.is_consistent(); + file.dump_all_entries(); + + datum2[0]=0; + datum2[1]=0; + datum2[2]=0; + success = file.retrieve_data( 2, datum2, 3); + + success = file.is_consistent(); + file.dump_all_entries(); + + do + { + ++datum1; + success = file.store_data(1, 0, &datum1); + } + while( success); + + success = file.is_consistent(); + file.dump_all_entries(); + + success = file.retrieve_data( 1, test); + file.dump_all_entries(); + + success = file.retrieve_data( 13, test); + success = file.retrieve_data( 0xff, test); +} + +#endif + + + + + + diff --git a/NAV_Algorithms/persistent_data_file.h b/NAV_Algorithms/persistent_data_file.h index ca28885..2c5ea56 100644 --- a/NAV_Algorithms/persistent_data_file.h +++ b/NAV_Algorithms/persistent_data_file.h @@ -1,12 +1,52 @@ +/***********************************************************************//** + * @file persistent_data_file.h + * @brief tiny file system for configuration data + * @author Dr. Klaus Schaefer + * @copyright Copyright 2021 Dr. Klaus Schaefer. All rights reserved. + * @license This project is released under the GNU Public License GPL-3.0 + + + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + **************************************************************************/ + #ifndef NAV_ALGORITHMS_PERSISTENT_DATA_FILE_H_ #define NAV_ALGORITHMS_PERSISTENT_DATA_FILE_H_ #define DEBUG -#include "stdint.h" -#include "assert.h" +#include "embedded_math.h" #include "CRC.h" +#ifdef DEBUG +#include "stdio.h" +#include "stdint.h" +#include "string.h" +#endif + +void FLASH_write( uint32_t * dest, uint32_t * source, unsigned n_words); + +enum EEPROM_PARAMETER_FILE_ID +{ + SENS_TILT_RPY=50, // roll pitch yaw angle + MAG_SENSOR_CALIBRATION, // xoff xscale yoff yscale zoff zscale std-deviation : 7 floats + SOFT_IRON_PARAMETERS, // 10 float values for x,y,z each : 30 floats + EXT_MAG_SENSOR_XFER_MATRIX // 4 input 3 output channels : 12 floats +} ; + + class node { public: @@ -24,15 +64,6 @@ class node uint16_t data; // direct 8bit + checksum OR crc of 32bit data file }; -#ifdef DEBUG - -void FLASH_write( uint32_t * dest, uint32_t * source, unsigned n_words) -{ - memcpy( dest, source, n_words * sizeof( uint32_t)); -} - -#endif - class file_system { public: @@ -217,82 +248,4 @@ class file_system void * end; }; -#ifdef DEBUG - -#define STORAGE_SIZE 1024 -uint32_t storage[STORAGE_SIZE]; - -void test_storage( void) -{ - bool success; - memset( storage, 0xff, STORAGE_SIZE * sizeof(uint32_t)); - file_system file( storage, storage+STORAGE_SIZE); - - uint8_t datum1 = 0x34; - file.store_data(1, node::DIRECT_8_BIT, &datum1); - - success = file.is_consistent(); - - file.dump_all_entries(); - - unsigned datum2[10]={ 1, 2, 3, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}; - file.store_data(2, 3, datum2); - - success = file.is_consistent(); - file.dump_all_entries(); - - datum1 = 0x21; - file.store_data(1, datum1); - - success = file.is_consistent(); - file.dump_all_entries(); - - uint16_t test = 0xff; - success = file.retrieve_data( 1, test); - - datum2[0]=4; - datum2[1]=5; - datum2[2]=6; - success = file.retrieve_data( 2, datum2, 3); - - success = file.is_consistent(); - file.dump_all_entries(); - - do - { - ++datum2[0]; - success = file.store_data(2, 3, datum2); - } - while( success == true); - - success = file.is_consistent(); - file.dump_all_entries(); - - datum2[0]=0; - datum2[1]=0; - datum2[2]=0; - success = file.retrieve_data( 2, datum2, 3); - - success = file.is_consistent(); - file.dump_all_entries(); - - do - { - ++datum1; - success = file.store_data(1, 0, &datum1); - } - while( success); - - success = file.is_consistent(); - file.dump_all_entries(); - - success = file.retrieve_data( 1, test); - file.dump_all_entries(); - - success = file.retrieve_data( 13, test); - success = file.retrieve_data( 0xff, test); -} - -#endif - #endif /* NAV_ALGORITHMS_PERSISTENT_DATA_FILE_H_ */ From 89eefbfed8d4bf1132508a0a41e5a550da64f19a Mon Sep 17 00:00:00 2001 From: realtimepeople Date: Sat, 15 Nov 2025 19:07:41 +0100 Subject: [PATCH 3/3] CRC checksum includes node itself. --- NAV_Algorithms/persistent_data_file.cpp | 6 ++- NAV_Algorithms/persistent_data_file.h | 52 ++++++++++++++++++++----- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/NAV_Algorithms/persistent_data_file.cpp b/NAV_Algorithms/persistent_data_file.cpp index 59d1193..64597f1 100644 --- a/NAV_Algorithms/persistent_data_file.cpp +++ b/NAV_Algorithms/persistent_data_file.cpp @@ -69,10 +69,12 @@ void test_storage( void) success = file.is_consistent(); file.dump_all_entries(); + unsigned id=5; do { ++datum2[0]; - success = file.store_data(2, 3, datum2); + success = file.store_data( id, 3, datum2); + ++id; } while( success == true); @@ -100,7 +102,7 @@ void test_storage( void) success = file.retrieve_data( 1, test); file.dump_all_entries(); - success = file.retrieve_data( 13, test); + success = file.retrieve_data( 13, test); // test non existing nodes success = file.retrieve_data( 0xff, test); } diff --git a/NAV_Algorithms/persistent_data_file.h b/NAV_Algorithms/persistent_data_file.h index 2c5ea56..d517065 100644 --- a/NAV_Algorithms/persistent_data_file.h +++ b/NAV_Algorithms/persistent_data_file.h @@ -93,16 +93,17 @@ class file_system if( data_size_words == node::DIRECT_8_BIT) // if direct 16bit data { - uint16_t checked_datum = *(uint8_t *)data; // take only 8 bits - checked_datum |= ( (~checked_datum) << 8); - temp_node.data = checked_datum; + + uint16_t checked_datum = *(uint8_t *)data; // take only 8 bits + temp_node.data = check_and_pack_id_len_and_data( temp_node, checked_datum); FLASH_write( (uint32_t *)next_entry, (uint32_t *)&temp_node, 1); } else // : bunch of 32bit data { - temp_node.data = CRC16_blockcheck( (uint16_t *)data, data_size_words * sizeof( uint16_t)); - + uint16_t crc = CRC16_blockcheck( (uint16_t *)data, data_size_words * sizeof( uint16_t)); + uint16_t protected_id_and_size = temp_node.id + ((temp_node.size) << 8); + temp_node.data = CRC16( protected_id_and_size, crc); FLASH_write( (uint32_t *)next_entry, (uint32_t *)&temp_node, 1); FLASH_write( (uint32_t *)(next_entry + 1), (uint32_t *)data, data_size_words * sizeof( uint32_t)); } @@ -125,6 +126,8 @@ class file_system uint32_t *from = (uint32_t *)candidate + 1; uint16_t crc = CRC16_blockcheck( (uint16_t *)from, data_size * sizeof( uint16_t)); + uint16_t protected_id_and_size = candidate->id + ((candidate->size) << 8); + crc = CRC16( protected_id_and_size, crc); if( candidate->data != crc) return false; @@ -148,6 +151,9 @@ class file_system if( my_node->size != 1) return false; + if( not short_node_is_consistent(*my_node)) + return false; + target = my_node->data; return true; } @@ -185,14 +191,14 @@ class file_system { if( work->size == 1) // we have found a dircect-data-entry { - if( ((work->data & 0xff) ^ (work->data >> 8)) != 0xff) + + if( not short_node_is_consistent( *work)) return false; // invalid data pattern } else { - uint16_t crc = CRC16_blockcheck( (uint16_t *)work + 2, (work->size - 1) * 2); - if( work->data != crc) - return false; // found bad entry + if( not long_node_is_consistent( work)) + return false; // invalid data pattern } } return true; @@ -200,6 +206,34 @@ class file_system private: + uint16_t check_and_pack_id_len_and_data( node the_node, uint8_t datum) + { + uint16_t info = datum; // need 16bit data + uint16_t crc = CRC16( info, 0); // data crc + info = the_node.id + (the_node.size << 8); + crc = CRC16( info, crc); // plus node crc + crc = (crc ^ (crc >> 8)) & 0xff; // fold crc into 8 bits + return datum + (crc << 8); + } + + bool short_node_is_consistent( node the_node) + { + uint16_t crc = CRC16( the_node.data & 0xff, 0); // data crc + uint16_t info = the_node.id + (the_node.size << 8); + crc = CRC16( info, crc); // plus node crc + crc = (crc ^ (crc >> 8)) & 0xff; // fold crc into 8 bits + return (the_node.data >> 8) == crc; + } + + bool long_node_is_consistent( node * work) + { + uint16_t crc = CRC16_blockcheck( (uint16_t *)work + 2, (work->size - 1) * 2); + uint16_t protected_id_and_size = work->id + ((work->size) << 8); + crc = CRC16( protected_id_and_size, crc); + + return work->data == crc; + } + node * find_last_datum( node * start, node::ID_t id=0xff) { node * thisone = find_first_datum( start, id);