Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
keystore.hpp
Go to the documentation of this file.
1
5#ifndef MODES_HARDWARD_KEYSTORE_HPP
6#define MODES_HARDWARD_KEYSTORE_HPP
7
10
12
14
15#include <cstring>
16#include <array>
17
20
34static constexpr uint32_t storeUid = utils::hash("StoreRev:v01");
35
37static constexpr uint32_t map32to31hash(uint32_t a)
38{
39 uint32_t lsb = a & 0b1; // mix the lost LSB info into the 13th bit
40 uint32_t val = (a ^ storeUid ^ (lsb << 14));
41 return val >> 1;
42}
43
45template<int16_t N> static constexpr uint32_t LMBD_INLINE hash(const char (&s)[N])
46{
47 static_assert(N - 1 <= 14, "Please use keys shorter than 14 bytes!");
48 return map32to31hash(utils::hash(s, N));
49}
50
56template<int16_t N> static inline void LMBD_INLINE setValue(const char (&key)[N], uint32_t value)
57{
58 static_assert(N - 1 <= 14, "Please use keys shorter than 14 bytes!");
60}
61
68template<int16_t N> static inline bool LMBD_INLINE getValue(const char (&key)[N], uint32_t& out)
69{
70 static_assert(N - 1 <= 14, "Please use keys shorter than 14 bytes!");
71 return component::fileSystem::user::get_value(hash(key), out);
72}
73
75static inline void clear_stored() { component::fileSystem::clear(); }
76
82static inline void LMBD_INLINE migrateIfNeeded()
83{
85 {
87 }
88
89 uint32_t out = storeUid ^ 0xff;
90 if (not component::fileSystem::user::get_value(storeUid, out))
91 {
94 }
95
96 if (out != storeUid)
97 {
100 component::fileSystem::user::set_value(storeUid, storeUid);
103
106 }
107}
108
117static constexpr uint32_t indexKeyFlag = (1 << 31);
118
135enum class PrefixValues : uint32_t
136{
137 generalKeys = indexKeyFlag | (0x0 << 29),
138 managerKeys = indexKeyFlag | (0x1 << 29),
139 privateKeys = indexKeyFlag | (0x2 << 29),
140 invalidKeys = indexKeyFlag | (0x3 << 29),
141};
142
144static constexpr uint32_t noGroupIndex = 15;
145
147static constexpr uint32_t noModeIndex = 31;
148
150static constexpr uint32_t LMBD_INLINE
151indexKeyFor(PrefixValues prefix, uint32_t groupId, uint32_t modeId, uint32_t offset, uint32_t index)
152{
153 uint32_t rawPrefix = (uint32_t)prefix;
154
155 assert((rawPrefix >> 31) == 0b1); // prefix on 2 of the MSBs +indexKeyFlag
156 assert((groupId >> 4) == 0); // groupId on 4 bits (0-14)
157 assert((modeId >> 5) == 0); // modeId on 5 bits (0-30)
158 assert((offset >> 4) == 0); // offset on 4 bits (0-15)
159 assert((index >> 16) == 0); // index on 16 bits (0-65535)
160
161 return rawPrefix | (groupId << 25) | (modeId << 20) | (offset << 16) | index;
162}
163
165template<typename _EnumTy, int _groupId, int _modeId, PrefixValues _prefix = PrefixValues::generalKeys> struct KeyStore
166{
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;
171
172 // it's magic!
173 static constexpr uint16_t storeIdKey = 0xafff;
174
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!");
178
179 // \private maps to ContextTy::KeyProxy::setValue()
180 template<EnumTy key, typename T> static inline void LMBD_INLINE setValue(T value)
181 {
182 using valueTy = std::remove_cv_t<std::remove_reference_t<T>>;
183 constexpr uint16_t rawKey = (uint16_t)key;
184
185 static_assert(storeIdKey != rawKey, "error: key can not match storeIdKey (0xafff)");
186
187 // simple case: value is uint32_t, store it as-is
188 if constexpr (std::is_same_v<valueTy, uint32_t>)
189 {
190 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, rawKey);
192
193 // small case: value is smaller than uint32_t, store it on a uint32_t
194 }
195 else if constexpr (sizeof(valueTy) <= sizeof(uint32_t))
196 {
197 uint32_t storage = 0;
198 details::bit_cast<sizeof(value)>(storage, value);
199 setValue<key>(storage);
200
201 // large case: value is stored on up to 16 uint32_t
202 }
203 else if constexpr (sizeof(valueTy) <= 16 * sizeof(uint32_t))
204 {
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)
209 {
210 uint32_t off = indexKeyFor(prefix, groupId, modeId, I, rawKey);
212 }
213 }
214 else
215 {
216 static_assert(sizeof(valueTy) <= 64, "KeyStore values limited to 64 bytes!");
217 }
218 }
219
220 // \private maps to ContextTy::KeyProxy::getValue()
221 template<EnumTy key, typename T> static inline bool LMBD_INLINE getValue(T& output)
222 {
223 constexpr uint16_t rawKey = (uint16_t)key;
224
225 static_assert(storeIdKey != rawKey, "error: key can not match storeIdKey (0xafff)");
226
227 // simple case: value is uint32_t, get it as-is
228 if constexpr (std::is_same_v<T, uint32_t>)
229 {
230 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, rawKey);
231 return component::fileSystem::user::get_value(idx, output);
232
233 // small case: value is smaller than uint32_t, get it on a uint32_t
234 }
235 else if constexpr (sizeof(T) <= sizeof(uint32_t))
236 {
237 uint32_t storage = 0;
238 if (not getValue<key, uint32_t>(storage))
239 {
240 return false;
241 }
242
243 details::bit_cast<sizeof(output)>(output, storage);
244 return true;
245
246 // large case: value is stored on up to 16 uint32_t
247 }
248 else if constexpr (sizeof(T) <= 16 * sizeof(uint32_t))
249 {
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)
253 {
254 uint32_t off = indexKeyFor(prefix, groupId, modeId, I, rawKey);
255 if (not component::fileSystem::user::get_value(off, storage[I]))
256 {
257 return false;
258 }
259 }
260
261 details::bit_cast<sizeof(output)>(output, storage);
262 return true;
263 }
264 else
265 {
266 static_assert(sizeof(T) <= 64, "KeyStore values limited to 64 bytes!");
267 }
268 return false;
269 }
270
271 // \private maps to ContextTy::KeyProxy::getValue(defaultValue)
272 template<EnumTy key, typename T> static inline void LMBD_INLINE getValue(T& output, T defValue)
273 {
274 if (not getValue<key, T>(output))
275 {
276 output = defValue;
277 }
278 }
279
280 // \private maps to ContextTy::KeyProxy::hasValue
281 template<EnumTy key, typename T = uint32_t> static inline bool LMBD_INLINE hasValue()
282 {
283 T output;
284 return getValue<key, T>(output);
285 }
286
287 // \private empty store if \p storeId mismatch with value at \p storeIdKey
288 template<uint32_t storeId> static void migrateStoreIfNeeded()
289 {
290 constexpr auto idx = indexKeyFor(prefix, groupId, modeId, 0, storeIdKey);
291
292 // retrieve storeId from storage
293 uint32_t otherId = 0;
294 if (not component::fileSystem::user::get_value(idx, otherId))
295 {
296 otherId = storeId;
298 }
299
300 // if it don't match our storeId, clean storage from our old keys
301 if (otherId != storeId || otherId == 0)
302 {
303 constexpr uint32_t select = 0xfff00000; // all but offset
304 constexpr uint32_t masked = idx & select;
307 }
308 }
309};
310
312template<uint32_t baseId, typename AllModes> static constexpr uint32_t derivateStoreIdImpl()
313{
314 constexpr uint32_t MixCstA = 0x8af8901d, MixCstB = 0x2f9c7c90;
315 constexpr uint8_t NbModes = std::tuple_size_v<AllModes>;
316
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>;
323
324 uint32_t storeIdHere = 0;
325 if constexpr (hasStore)
326 {
327 storeIdHere = ModeHere::storeId;
328 }
329
330 // straightforward mix function
331 storeId += MixCstA;
332 storeId = (storeId << 7) ^ (storeId >> (32 - 7));
333 storeId ^= MixCstB;
334
335 storeId += storeIdHere;
336 });
337
338 return storeId;
339}
340
342template<uint32_t baseId, typename AllModes> static constexpr uint32_t derivateStoreId =
343 derivateStoreIdImpl<baseId, AllModes>();
344
345} // namespace lampda::modes::store
346
347#endif
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
Define templated tools to analyze the manager objects.
Define useful functions.