5#ifndef MODES_HARDWARD_KEYSTORE_HPP
6#define MODES_HARDWARD_KEYSTORE_HPP
33static constexpr uint32_t storeUid =
utils::hash(
"StoreRev:v01");
36static constexpr uint32_t map32to31hash(uint32_t a)
38 uint32_t lsb = a & 0b1;
39 uint32_t val = (a ^ storeUid ^ (lsb << 14));
44template<
int16_t N>
static constexpr uint32_t LMBD_INLINE hash(
const char (&s)[N])
46 static_assert(N - 1 <= 14,
"Please use keys shorter than 14 bytes!");
55template<
int16_t N>
static inline void LMBD_INLINE
setValue(
const char (&key)[N], uint32_t value)
57 static_assert(N - 1 <= 14,
"Please use keys shorter than 14 bytes!");
67template<
int16_t N>
static inline bool LMBD_INLINE
getValue(
const char (&key)[N], uint32_t& out)
69 static_assert(N - 1 <= 14,
"Please use keys shorter than 14 bytes!");
88 uint32_t out = storeUid ^ 0xff;
116static constexpr uint32_t indexKeyFlag = (1 << 31);
134enum class PrefixValues : uint32_t
136 generalKeys = indexKeyFlag | (0x0 << 29),
137 managerKeys = indexKeyFlag | (0x1 << 29),
138 privateKeys = indexKeyFlag | (0x2 << 29),
139 invalidKeys = indexKeyFlag | (0x3 << 29),
143static constexpr uint32_t noGroupIndex = 15;
146static constexpr uint32_t noModeIndex = 31;
149static constexpr uint32_t LMBD_INLINE
150indexKeyFor(PrefixValues prefix, uint32_t groupId, uint32_t modeId, uint32_t offset, uint32_t index)
152 uint32_t rawPrefix = (uint32_t)prefix;
154 assert((rawPrefix >> 31) == 0b1);
155 assert((groupId >> 4) == 0);
156 assert((modeId >> 5) == 0);
157 assert((offset >> 4) == 0);
158 assert((index >> 16) == 0);
160 return rawPrefix | (groupId << 25) | (modeId << 20) | (offset << 16) | index;
164template<
typename _EnumTy,
int _groupId,
int _modeId, PrefixValues _prefix = PrefixValues::generalKeys>
struct KeyStore
166 using EnumTy = _EnumTy;
167 static constexpr uint32_t groupId = _groupId < 0 ? noGroupIndex : _groupId;
168 static constexpr uint32_t modeId = _modeId < 0 ? noModeIndex : _modeId;
169 static constexpr PrefixValues prefix = _prefix;
172 static constexpr uint16_t storeIdKey = 0xafff;
174 static_assert(
sizeof(EnumTy) ==
sizeof(uint16_t),
"uint16_t enum only");
175 static_assert((groupId >> 4) == 0,
"Group index must fit on 4 bits!");
176 static_assert((modeId >> 5) == 0,
"Mode index must fit on 5 bits!");
179 template<EnumTy key,
typename T>
static inline void LMBD_INLINE setValue(T value)
181 using valueTy = std::remove_cv_t<std::remove_reference_t<T>>;
182 constexpr uint16_t rawKey = (uint16_t)key;
184 static_assert(storeIdKey != rawKey,
"error: key can not match storeIdKey (0xafff)");
187 if constexpr (std::is_same_v<valueTy, uint32_t>)
189 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, rawKey);
194 else if constexpr (
sizeof(valueTy) <=
sizeof(uint32_t))
196 uint32_t storage = 0;
197 details::bit_cast<sizeof(value)>(storage, value);
198 setValue<key>(storage);
202 else if constexpr (
sizeof(valueTy) <= 16 *
sizeof(uint32_t))
204 constexpr uint8_t N = ((
sizeof(valueTy) - 1) /
sizeof(uint32_t)) + 1;
205 std::array<uint32_t, N> storage {};
206 details::bit_cast<sizeof(value)>(storage, value);
207 for (uint8_t I = 0; I < N; ++I)
209 uint32_t off = indexKeyFor(prefix, groupId, modeId, I, rawKey);
215 static_assert(
sizeof(valueTy) <= 64,
"KeyStore values limited to 64 bytes!");
220 template<EnumTy key,
typename T>
static inline bool LMBD_INLINE
getValue(T& output)
222 constexpr uint16_t rawKey = (uint16_t)key;
224 static_assert(storeIdKey != rawKey,
"error: key can not match storeIdKey (0xafff)");
227 if constexpr (std::is_same_v<T, uint32_t>)
229 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, rawKey);
234 else if constexpr (
sizeof(T) <=
sizeof(uint32_t))
236 uint32_t storage = 0;
237 if (not getValue<key, uint32_t>(storage))
242 details::bit_cast<sizeof(output)>(output, storage);
247 else if constexpr (
sizeof(T) <= 16 *
sizeof(uint32_t))
249 constexpr uint8_t N = ((
sizeof(T) - 1) /
sizeof(uint32_t)) + 1;
250 std::array<uint32_t, N> storage {};
251 for (uint8_t I = 0; I < N; ++I)
253 uint32_t off = indexKeyFor(prefix, groupId, modeId, I, rawKey);
260 details::bit_cast<sizeof(output)>(output, storage);
265 static_assert(
sizeof(T) <= 64,
"KeyStore values limited to 64 bytes!");
271 template<EnumTy key,
typename T>
static inline void LMBD_INLINE
getValue(T& output, T defValue)
273 if (not getValue<key, T>(output))
280 template<EnumTy key,
typename T = u
int32_t>
static inline bool LMBD_INLINE hasValue()
283 return getValue<key, T>(output);
287 template<u
int32_t storeId>
static void migrateStoreIfNeeded()
289 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, storeIdKey);
292 uint32_t otherId = 0;
300 if (otherId != storeId || otherId == 0)
302 constexpr uint32_t select = 0xfff00000;
303 constexpr uint32_t masked = idx & select;
311template<u
int32_t baseId,
typename AllModes>
static constexpr uint32_t derivateStoreIdImpl()
313 constexpr uint32_t MixCstA = 0x8af8901d, MixCstB = 0x2f9c7c90;
314 constexpr uint8_t NbModes = std::tuple_size_v<AllModes>;
316 uint32_t storeId = baseId;
317 details::unroll<NbModes>([&](
auto Idx) {
318 constexpr uint8_t I =
decltype(Idx)::value;
319 using ModeHere = std::tuple_element_t<I, AllModes>;
320 using StoreHere = StoreEnumOf<ModeHere>;
321 constexpr bool hasStore = not std::is_same_v<StoreHere, NoStoreHere>;
323 uint32_t storeIdHere = 0;
324 if constexpr (hasStore)
326 storeIdHere = ModeHere::storeId;
331 storeId = (storeId << 7) ^ (storeId >> (32 - 7));
334 storeId += storeIdHere;
341template<u
int32_t baseId,
typename AllModes>
static constexpr uint32_t derivateStoreId =
342 derivateStoreIdImpl<baseId, AllModes>();
Contains shorthand macro definitions.
Interface for the physical components of the file system.
Mode key store interface, see ContextTy::KeyProxy.
Definition: keystore.hpp:18
static void LMBD_INLINE setValue(const char(&key)[N], uint32_t value)
Set value for named key and read in in out.
Definition: keystore.hpp:55
static bool LMBD_INLINE getValue(const char(&key)[N], uint32_t &out)
Get value for named key and write it in out.
Definition: keystore.hpp:67
static void LMBD_INLINE migrateIfNeeded()
Check for migration and erase all values if needed IT WILL ERASE THE WHOLE MEMORY,...
Definition: keystore.hpp:81
static void clear_stored()
Force clear the stored parameters.
Definition: keystore.hpp:74
bool load_from_file()
Load system values from memory.
Definition: fileSystem.cpp:308
void write_to_file()
Write the system parameters to a file.
Definition: fileSystem.cpp:286
void write_to_file()
Write the user parameters to a file.
Definition: fileSystem.cpp:390
bool load_from_file()
Load user values from memory.
Definition: fileSystem.cpp:414
void set_value(const uint32_t key, const uint32_t value)
Store a key/value in the filesystem.
Definition: fileSystem.cpp:355
bool doKeyExists(const uint32_t key)
Return true if the given key exists in the filesystem.
Definition: fileSystem.cpp:328
uint32_t dropMatchingKeys(const uint32_t bitMatch, const uint32_t bitSelect)
Drop all keys using the given bit prefix.
Definition: fileSystem.cpp:364
bool get_value(const uint32_t key, uint32_t &value)
Check and return a value stored in a filesystem.
Definition: fileSystem.cpp:330
void clear()
clear the stored values in the currently loaded file system.
Definition: fileSystem.cpp:79
void clear_internal_fs()
hard clean of the whole filesystem, you will loose all stored data.
Definition: fileSystem.cpp:89
static constexpr uint32_t hash(const T s, const uint16_t maxSize=14, const uint16_t off=0)
Hash input string into a 32-bit unsigned integer.
Definition: utils.h:174