5#ifndef MODES_HARDWARD_KEYSTORE_HPP
6#define MODES_HARDWARD_KEYSTORE_HPP
34static constexpr uint32_t storeUid =
utils::hash(
"StoreRev:v01");
37static constexpr uint32_t map32to31hash(uint32_t a)
39 uint32_t lsb = a & 0b1;
40 uint32_t val = (a ^ storeUid ^ (lsb << 14));
45template<
int16_t N>
static constexpr uint32_t LMBD_INLINE hash(
const char (&s)[N])
47 static_assert(N - 1 <= 14,
"Please use keys shorter than 14 bytes!");
56template<
int16_t N>
static inline void LMBD_INLINE
setValue(
const char (&key)[N], uint32_t value)
58 static_assert(N - 1 <= 14,
"Please use keys shorter than 14 bytes!");
68template<
int16_t N>
static inline bool LMBD_INLINE
getValue(
const char (&key)[N], uint32_t& out)
70 static_assert(N - 1 <= 14,
"Please use keys shorter than 14 bytes!");
89 uint32_t out = storeUid ^ 0xff;
117static constexpr uint32_t indexKeyFlag = (1 << 31);
135enum class PrefixValues : uint32_t
137 generalKeys = indexKeyFlag | (0x0 << 29),
138 managerKeys = indexKeyFlag | (0x1 << 29),
139 privateKeys = indexKeyFlag | (0x2 << 29),
140 invalidKeys = indexKeyFlag | (0x3 << 29),
144static constexpr uint32_t noGroupIndex = 15;
147static constexpr uint32_t noModeIndex = 31;
150static constexpr uint32_t LMBD_INLINE
151indexKeyFor(PrefixValues prefix, uint32_t groupId, uint32_t modeId, uint32_t offset, uint32_t index)
153 uint32_t rawPrefix = (uint32_t)prefix;
155 assert((rawPrefix >> 31) == 0b1);
156 assert((groupId >> 4) == 0);
157 assert((modeId >> 5) == 0);
158 assert((offset >> 4) == 0);
159 assert((index >> 16) == 0);
161 return rawPrefix | (groupId << 25) | (modeId << 20) | (offset << 16) | index;
165template<
typename _EnumTy,
int _groupId,
int _modeId, PrefixValues _prefix = PrefixValues::generalKeys>
struct KeyStore
167 using EnumTy = _EnumTy;
168 static constexpr uint32_t groupId = _groupId < 0 ? noGroupIndex : _groupId;
169 static constexpr uint32_t modeId = _modeId < 0 ? noModeIndex : _modeId;
170 static constexpr PrefixValues prefix = _prefix;
173 static constexpr uint16_t storeIdKey = 0xafff;
175 static_assert(
sizeof(EnumTy) ==
sizeof(uint16_t),
"uint16_t enum only");
176 static_assert((groupId >> 4) == 0,
"Group index must fit on 4 bits!");
177 static_assert((modeId >> 5) == 0,
"Mode index must fit on 5 bits!");
180 template<EnumTy key,
typename T>
static inline void LMBD_INLINE setValue(T value)
182 using valueTy = std::remove_cv_t<std::remove_reference_t<T>>;
183 constexpr uint16_t rawKey = (uint16_t)key;
185 static_assert(storeIdKey != rawKey,
"error: key can not match storeIdKey (0xafff)");
188 if constexpr (std::is_same_v<valueTy, uint32_t>)
190 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, rawKey);
195 else if constexpr (
sizeof(valueTy) <=
sizeof(uint32_t))
197 uint32_t storage = 0;
198 details::bit_cast<sizeof(value)>(storage, value);
199 setValue<key>(storage);
203 else if constexpr (
sizeof(valueTy) <= 16 *
sizeof(uint32_t))
205 constexpr uint8_t N = ((
sizeof(valueTy) - 1) /
sizeof(uint32_t)) + 1;
206 std::array<uint32_t, N> storage {};
207 details::bit_cast<sizeof(value)>(storage, value);
208 for (uint8_t I = 0; I < N; ++I)
210 uint32_t off = indexKeyFor(prefix, groupId, modeId, I, rawKey);
216 static_assert(
sizeof(valueTy) <= 64,
"KeyStore values limited to 64 bytes!");
221 template<EnumTy key,
typename T>
static inline bool LMBD_INLINE
getValue(T& output)
223 constexpr uint16_t rawKey = (uint16_t)key;
225 static_assert(storeIdKey != rawKey,
"error: key can not match storeIdKey (0xafff)");
228 if constexpr (std::is_same_v<T, uint32_t>)
230 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, rawKey);
235 else if constexpr (
sizeof(T) <=
sizeof(uint32_t))
237 uint32_t storage = 0;
238 if (not getValue<key, uint32_t>(storage))
243 details::bit_cast<sizeof(output)>(output, storage);
248 else if constexpr (
sizeof(T) <= 16 *
sizeof(uint32_t))
250 constexpr uint8_t N = ((
sizeof(T) - 1) /
sizeof(uint32_t)) + 1;
251 std::array<uint32_t, N> storage {};
252 for (uint8_t I = 0; I < N; ++I)
254 uint32_t off = indexKeyFor(prefix, groupId, modeId, I, rawKey);
261 details::bit_cast<sizeof(output)>(output, storage);
266 static_assert(
sizeof(T) <= 64,
"KeyStore values limited to 64 bytes!");
272 template<EnumTy key,
typename T>
static inline void LMBD_INLINE
getValue(T& output, T defValue)
274 if (not getValue<key, T>(output))
281 template<EnumTy key,
typename T = u
int32_t>
static inline bool LMBD_INLINE hasValue()
284 return getValue<key, T>(output);
288 template<u
int32_t storeId>
static void migrateStoreIfNeeded()
290 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, storeIdKey);
293 uint32_t otherId = 0;
301 if (otherId != storeId || otherId == 0)
303 constexpr uint32_t select = 0xfff00000;
304 constexpr uint32_t masked = idx & select;
312template<u
int32_t baseId,
typename AllModes>
static constexpr uint32_t derivateStoreIdImpl()
314 constexpr uint32_t MixCstA = 0x8af8901d, MixCstB = 0x2f9c7c90;
315 constexpr uint8_t NbModes = std::tuple_size_v<AllModes>;
317 uint32_t storeId = baseId;
318 details::unroll<NbModes>([&](
auto Idx) {
319 constexpr uint8_t I =
decltype(Idx)::value;
320 using ModeHere = std::tuple_element_t<I, AllModes>;
321 using StoreHere = StoreEnumOf<ModeHere>;
322 constexpr bool hasStore = not std::is_same_v<StoreHere, NoStoreHere>;
324 uint32_t storeIdHere = 0;
325 if constexpr (hasStore)
327 storeIdHere = ModeHere::storeId;
332 storeId = (storeId << 7) ^ (storeId >> (32 - 7));
335 storeId += storeIdHere;
342template<u
int32_t baseId,
typename AllModes>
static constexpr uint32_t derivateStoreId =
343 derivateStoreIdImpl<baseId, AllModes>();
Contains shorthand macro definitions.
Interface for the physical components of the file system.
bool load_from_file()
Load system values from memory.
Definition: fileSystem.cpp:305
void write_to_file()
Write the system parameters to a file.
Definition: fileSystem.cpp:283
bool doKeyExists(const uint32_t key)
Return true if the given key exists in the filesystem.
Definition: fileSystem.cpp:325
void write_to_file()
Write the user parameters to a file.
Definition: fileSystem.cpp:383
bool get_value(const uint32_t key, uint32_t &value)
Check and return a value stored in a filesystem.
Definition: fileSystem.cpp:327
bool load_from_file()
Load user values from memory.
Definition: fileSystem.cpp:407
uint32_t dropMatchingKeys(const uint32_t bitMatch, const uint32_t bitSelect)
Drop all keys using the given bit prefix.
Definition: fileSystem.cpp:357
void set_value(const uint32_t key, const uint32_t value)
Store a key/value in the filesystem.
Definition: fileSystem.cpp:348
void clear_internal_fs()
hard clean of the whole filesystem, you will loose all stored data.
Definition: fileSystem.cpp:90
void clear()
clear the stored values in the currently loaded file system.
Definition: fileSystem.cpp:80
Mode key store interface, see ContextTy::KeyProxy.
Definition: keystore.hpp:19
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:56
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:68
static void LMBD_INLINE migrateIfNeeded()
Check for migration and erase all values if needed IT WILL ERASE THE WHOLE MEMORY,...
Definition: keystore.hpp:82
static void clear_stored()
Force clear the stored parameters.
Definition: keystore.hpp:75
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