mie: id: add an interface for building a mie_id value across multiple steps
This commit is contained in:
58
mie/id.c
58
mie/id.c
@@ -99,19 +99,10 @@ void mie_id_init_random(mie_id *out)
|
||||
|
||||
void mie_id_init_ns(mie_id *out, const mie_id *ns, const b_rope *name)
|
||||
{
|
||||
b_hash_ctx hash;
|
||||
b_hash_ctx_init(&hash, B_HASH_SHA1);
|
||||
|
||||
b_hash_ctx_update(&hash, ns->id_bytes, sizeof ns->id_bytes);
|
||||
b_hash_ctx_update_rope(&hash, name);
|
||||
|
||||
b_hash_ctx_finish(&hash, out->id_bytes, sizeof out->id_bytes);
|
||||
|
||||
out->id_bytes[6] &= 0x0F;
|
||||
out->id_bytes[6] |= 0x50;
|
||||
|
||||
out->id_bytes[8] &= 0x3F;
|
||||
out->id_bytes[8] |= 0x80;
|
||||
struct mie_id_builder ctx;
|
||||
mie_id_builder_begin(&ctx, ns);
|
||||
mie_id_builder_add_rope(&ctx, name);
|
||||
mie_id_builder_end(&ctx, out);
|
||||
}
|
||||
|
||||
void mie_id_to_string(const mie_id *id, char *out, size_t max)
|
||||
@@ -145,7 +136,48 @@ void mie_id_map_put(struct mie_id_map *map, mie_id *id, const b_rope *name)
|
||||
put_id(&map->map_entries, id);
|
||||
}
|
||||
|
||||
void mie_id_map_put_id(struct mie_id_map *map, mie_id *node)
|
||||
{
|
||||
put_id(&map->map_entries, node);
|
||||
}
|
||||
|
||||
mie_id *mie_id_map_get(const struct mie_id_map *map, mie_id *id)
|
||||
{
|
||||
return get_id(&map->map_entries, id);
|
||||
}
|
||||
|
||||
void mie_id_builder_begin(struct mie_id_builder *builder, const mie_id *ns)
|
||||
{
|
||||
memset(builder, 0x0, sizeof *builder);
|
||||
b_hash_ctx_init(&builder->b_hash, B_HASH_SHA1);
|
||||
b_hash_ctx_update(&builder->b_hash, ns->id_bytes, sizeof ns->id_bytes);
|
||||
}
|
||||
|
||||
void mie_id_builder_add(struct mie_id_builder *builder, const void *p, size_t len)
|
||||
{
|
||||
b_hash_ctx_update(&builder->b_hash, p, len);
|
||||
}
|
||||
|
||||
void mie_id_builder_add_rope(struct mie_id_builder *builder, const b_rope *rope)
|
||||
{
|
||||
b_hash_ctx_update_rope(&builder->b_hash, rope);
|
||||
}
|
||||
|
||||
void mie_id_builder_add_marker(
|
||||
struct mie_id_builder *builder, enum mie_id_builder_marker marker)
|
||||
{
|
||||
unsigned char c = marker;
|
||||
b_hash_ctx_update(&builder->b_hash, &c, sizeof c);
|
||||
}
|
||||
|
||||
void mie_id_builder_end(struct mie_id_builder *builder, mie_id *out)
|
||||
{
|
||||
memset(out, 0x0, sizeof *out);
|
||||
b_hash_ctx_finish(&builder->b_hash, out->id_bytes, sizeof out->id_bytes);
|
||||
|
||||
out->id_bytes[6] &= 0x0F;
|
||||
out->id_bytes[6] |= 0x50;
|
||||
|
||||
out->id_bytes[8] &= 0x3F;
|
||||
out->id_bytes[8] |= 0x80;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,15 @@
|
||||
#define MIE_ID_NR_WORDS_32 (MIE_ID_NR_BYTES / sizeof(uint32_t))
|
||||
#define MIE_ID_NR_WORDS_64 (MIE_ID_NR_BYTES / sizeof(uint64_t))
|
||||
|
||||
enum mie_id_builder_marker {
|
||||
MIE_ID_BUILDER_STORAGE_START = 0x32,
|
||||
MIE_ID_BUILDER_STORAGE_END = 0x8a,
|
||||
MIE_ID_BUILDER_FUNCTION_IN_START = 0x78,
|
||||
MIE_ID_BUILDER_FUNCTION_IN_END = 0x55,
|
||||
MIE_ID_BUILDER_FUNCTION_OUT_START = 0xa2,
|
||||
MIE_ID_BUILDER_FUNCTION_OUT_END = 0xbc,
|
||||
};
|
||||
|
||||
typedef struct mie_id {
|
||||
union {
|
||||
uint8_t id_bytes[MIE_ID_NR_BYTES];
|
||||
@@ -31,9 +40,16 @@ typedef struct mie_id {
|
||||
uint64_t id_words_64[MIE_ID_NR_WORDS_64];
|
||||
};
|
||||
|
||||
b_btree_node e_node;
|
||||
union {
|
||||
b_btree_node e_node;
|
||||
b_queue_entry e_entry;
|
||||
};
|
||||
} mie_id;
|
||||
|
||||
struct mie_id_builder {
|
||||
b_hash_ctx b_hash;
|
||||
};
|
||||
|
||||
struct mie_id_map {
|
||||
mie_id map_ns_id;
|
||||
b_btree map_entries;
|
||||
@@ -54,7 +70,27 @@ MIE_API void mie_id_to_string(const mie_id *id, char *out, size_t max);
|
||||
|
||||
MIE_API void mie_id_map_init(struct mie_id_map *map, const mie_id *ns);
|
||||
MIE_API const mie_id *mie_id_map_get_ns(const struct mie_id_map *map);
|
||||
MIE_API void mie_id_map_put(struct mie_id_map *map, mie_id *id, const b_rope *name);
|
||||
MIE_API void mie_id_map_put(
|
||||
struct mie_id_map *map, mie_id *node, const b_rope *name);
|
||||
MIE_API void mie_id_map_put_id(struct mie_id_map *map, mie_id *node);
|
||||
MIE_API mie_id *mie_id_map_get(const struct mie_id_map *map, mie_id *id);
|
||||
|
||||
MIE_API void mie_id_builder_begin(struct mie_id_builder *builder, const mie_id *ns);
|
||||
MIE_API void mie_id_builder_add(
|
||||
struct mie_id_builder *builder, const void *p, size_t len);
|
||||
static inline void mie_id_builder_add_char(struct mie_id_builder *builder, char c)
|
||||
{
|
||||
mie_id_builder_add(builder, &c, 1);
|
||||
}
|
||||
static inline void mie_id_builder_add_cstr(
|
||||
struct mie_id_builder *builder, const char *s)
|
||||
{
|
||||
mie_id_builder_add(builder, s, strlen(s));
|
||||
}
|
||||
MIE_API void mie_id_builder_add_rope(
|
||||
struct mie_id_builder *builder, const b_rope *rope);
|
||||
MIE_API void mie_id_builder_add_marker(
|
||||
struct mie_id_builder *builder, enum mie_id_builder_marker marker);
|
||||
MIE_API void mie_id_builder_end(struct mie_id_builder *builder, mie_id *out);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user