Hash :
dc899182
Author :
Thomas de Grivel
Date :
2023-12-21T01:03:28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "list.h"
#include "str.h"
#include "tag.h"
#include "tag_type.h"
#define HASH_UPDATE_DEF(type) \
void hash_update_##type (t_hash *hash, const type *x) \
{ \
const s8 t[] = #type; \
assert(hash); \
assert(x); \
hash_update(hash, t, sizeof(t)); \
hash_update(hash, x, sizeof(type)); \
}
void hash_clean (t_hash *hash)
{
*hash = (t_hash) {0};
}
void hash_init (t_hash *hash)
{
assert(hash);
SHA1Init(hash);
}
uw hash_to_uw (t_hash *hash)
{
u8 digest[SHA1_DIGEST_LENGTH];
SHA1Final(digest, hash);
return *((uw *) digest);
}
u64 hash_to_u64 (t_hash *hash)
{
u8 digest[SHA1_DIGEST_LENGTH];
SHA1Final(digest, hash);
return *((u64 *) digest);
}
void hash_update (t_hash *hash, const void *data, uw size)
{
assert(hash);
assert(data);
SHA1Update(hash, data, size);
}
void hash_update_1 (t_hash *hash, const s8 *p)
{
uw len;
const s8 type[] = "s8*";
assert(hash);
assert(p);
hash_update(hash, type, sizeof(type));
len = strlen(p);
hash_update(hash, &len, sizeof(len));
hash_update(hash, p, strlen(p));
}
void hash_update_array (t_hash *hash, const s_array *a)
{
uw i = 0;
const s8 type[] = "array";
assert(hash);
assert(a);
hash_update(hash, type, sizeof(type));
hash_update(hash, &a->dimension, sizeof(a->dimension));
hash_update(hash, &a->type, sizeof(a->type));
while (i < a->dimension) {
hash_update(hash, &a->dimensions[i].count,
sizeof(a->dimensions[i].count));
i++;
}
if (a->data)
hash_update(hash, a->data, a->size);
else if (a->tags) {
i = 0;
while (i < a->count) {
hash_update_tag(hash, a->tags + i);
i++;
}
}
}
void hash_update_bool (t_hash *hash, const bool *x)
{
bool b;
const s8 type[] = "bool";
assert(hash);
assert(x);
hash_update(hash, type, sizeof(type));
b = x ? 1 : 0;
hash_update(hash, &b, sizeof(b));
}
void hash_update_call (t_hash *hash, const s_call *call)
{
const s8 type[] = "call";
assert(hash);
assert(call);
hash_update(hash, type, sizeof(type));
hash_update_ident(hash, &call->ident);
hash_update_list(hash, (const s_list * const *) &call->arguments);
}
void hash_update_cfn (t_hash *hash, const s_cfn *cfn)
{
const s8 type[] = "cfn";
assert(hash);
assert(cfn);
hash_update(hash, type, sizeof(type));
hash_update_sym(hash, &cfn->name);
hash_update_list(hash, (const s_list * const *) &cfn->arg_types);
}
HASH_UPDATE_DEF(character)
HASH_UPDATE_DEF(f32)
HASH_UPDATE_DEF(f64)
void hash_update_fact (t_hash *hash, const s_fact *fact)
{
const s8 type[] = "fact";
assert(hash);
assert(fact);
assert(fact->subject);
assert(fact->predicate);
assert(fact->object);
hash_update(hash, type, sizeof(type));
hash_update_tag(hash, fact->subject);
hash_update_tag(hash, fact->predicate);
hash_update_tag(hash, fact->object);
}
void hash_update_fn (t_hash *hash, const s_fn *fn)
{
const s8 type[] = "fn";
assert(hash);
assert(fn);
hash_update(hash, type, sizeof(type));
hash_update_bool(hash, &fn->macro);
hash_update_bool(hash, &fn->special_operator);
hash_update_fn_clauses(hash, fn->clauses);
}
void hash_update_fn_clauses (t_hash *hash, const s_fn_clause *clauses)
{
uw count = 0;
const s_fn_clause *f;
const s8 type[] = "fn_clauses";
assert(hash);
assert(clauses);
hash_update(hash, type, sizeof(type));
f = clauses;
while (f) {
count++;
f = f->next_clause;
}
hash_update_uw(hash, &count);
f = clauses;
while (f) {
hash_update_list(hash, (const s_list * const *) &f->pattern);
hash_update_list(hash, (const s_list * const *) &f->algo);
f = f->next_clause;
}
}
void hash_update_ident (t_hash *hash, const s_ident *ident)
{
assert(hash);
assert(ident);
hash_update_s8(hash, "_");
if (ident->module) {
hash_update_sym(hash, &ident->module);
hash_update_s8(hash, ".");
}
hash_update_sym(hash, &ident->sym);
}
void hash_update_integer (t_hash *hash, const s_integer *i)
{
int j = 0;
mp_digit *digit;
const s8 type[] = "integer";
assert(hash);
assert(i);
digit = i->mp_int.dp;
hash_update(hash, type, 7);
hash_update(hash, &i->mp_int.used, sizeof(i->mp_int.used));
while (j < i->mp_int.used) {
hash_update(hash, digit, sizeof(*digit));
digit++;
j++;
}
}
/* FIXME: circular lists */
void hash_update_list (t_hash *hash, const s_list * const *list)
{
uw count;
const s_list *l;
const s_list *last;
const s8 type[] = "list";
assert(hash);
assert(list);
l = *list;
hash_update(hash, type, sizeof(type));
count = list_length(l);
hash_update_uw(hash, &count);
if (l) {
while (l) {
hash_update_tag(hash, &l->tag);
last = l;
l = list_next(l);
}
hash_update_tag(hash, &last->next);
}
}
void hash_update_map (t_hash *hash, const s_map *map)
{
uw i = 0;
const s8 type[] = "map";
assert(hash);
assert(map);
hash_update(hash, type, strlen(type));
hash_update(hash, &map->count, sizeof(map->count));
while (i < map->count) {
hash_update_tag(hash, map->key + i);
hash_update_tag(hash, map->value + i);
i++;
}
}
void hash_update_ptag (t_hash *hash, const p_tag *ptag)
{
const s8 type[] = "ptag";
assert(hash);
assert(ptag);
hash_update(hash, type, sizeof(type));
hash_update(hash, ptag, sizeof(ptag));
}
void hash_update_ptr (t_hash *hash, const u_ptr_w *ptr)
{
const s8 type[] = "ptr";
hash_update(hash, type, strlen(type));
hash_update(hash, &ptr->p, sizeof(void *));
}
void hash_update_ptr_free (t_hash *hash, const u_ptr_w *ptr_free)
{
const s8 type[] = "ptr_free";
hash_update(hash, type, strlen(type));
hash_update(hash, &ptr_free->p, sizeof(void *));
}
void hash_update_quote (t_hash *hash, const s_quote *x)
{
const s8 type[] = "quote";
hash_update(hash, type, strlen(type));
hash_update_tag(hash, x->tag);
}
HASH_UPDATE_DEF(s8)
HASH_UPDATE_DEF(s16)
HASH_UPDATE_DEF(s32)
HASH_UPDATE_DEF(s64)
HASH_UPDATE_DEF(sw)
void hash_update_str (t_hash *hash, const s_str *str)
{
s8 type[] = "str";
assert(hash);
assert(str);
hash_update(hash, type, strlen(type));
hash_update(hash, &str->size, sizeof(str->size));
hash_update(hash, str->ptr.p, str->size);
}
void hash_update_struct (t_hash *hash, const s_struct *s)
{
const void *data;
f_hash_update hash_update_value;
uw i = 0;
const s_sym *sym;
s8 type[] = "struct";
assert(hash);
assert(s);
hash_update(hash, type, sizeof(type));
hash_update_sym(hash, &s->type.module);
hash_update(hash, &s->type.map.count, sizeof(s->type.map.count));
while (i < s->type.map.count) {
hash_update_tag(hash, s->type.map.key + i);
if (! tag_type_to_hash_update(s->type.map.value[i].type,
&hash_update_value))
exit(1);
if (s->data)
data = (s8 *) s->data + s->type.offset[i];
else {
if (! tag_type(s->type.map.value + i, &sym))
exit(1);
if (s->tag) {
if (! tag_to_const_pointer(s->tag + i, sym, &data))
exit(1);
}
else {
if (! tag_to_const_pointer(s->type.map.value + i, sym, &data))
exit(1);
}
}
hash_update_value(hash, data);
i++;
}
}
void hash_update_sym (t_hash *hash, const s_sym * const *sym)
{
s8 type[] = "sym";
assert(hash);
assert(sym);
hash_update(hash, type, sizeof(type));
hash_update_str(hash, &(*sym)->str);
}
void hash_update_tag (t_hash *hash, const s_tag *tag)
{
u8 tag_type;
s8 type[] = "tag";
assert(hash);
assert(tag);
hash_update(hash, type, strlen(type));
tag_type = tag->type;
hash_update_u8(hash, &tag_type);
switch (tag->type) {
case TAG_ARRAY: hash_update_array(hash, &tag->data.array); break;
case TAG_BOOL: hash_update_bool(hash, &tag->data.bool); break;
case TAG_CALL: hash_update_call(hash, &tag->data.call); break;
case TAG_CFN: hash_update_cfn(hash, &tag->data.cfn); break;
case TAG_CHARACTER:
hash_update_character(hash, &tag->data.character); break;
case TAG_F32: hash_update_f32(hash, &tag->data.f32); break;
case TAG_F64: hash_update_f64(hash, &tag->data.f64); break;
case TAG_FACT: hash_update_fact(hash, &tag->data.fact); break;
case TAG_FN: hash_update_fn(hash, &tag->data.fn); break;
case TAG_IDENT: hash_update_ident(hash, &tag->data.ident); break;
case TAG_INTEGER:
hash_update_integer(hash, &tag->data.integer); break;
case TAG_LIST:
hash_update_list(hash, (const s_list * const *) &tag->data.list);
break;
case TAG_MAP: hash_update_map(hash, &tag->data.map); break;
case TAG_PTAG: hash_update_ptag(hash, &tag->data.ptag); break;
case TAG_PTR: hash_update_ptr(hash, &tag->data.ptr); break;
case TAG_PTR_FREE:
hash_update_ptr_free(hash, &tag->data.ptr_free); break;
case TAG_QUOTE: hash_update_quote(hash, &tag->data.quote); break;
case TAG_S8: hash_update_s8(hash, &tag->data.s8); break;
case TAG_S16: hash_update_s16(hash, &tag->data.s16); break;
case TAG_S32: hash_update_s32(hash, &tag->data.s32); break;
case TAG_S64: hash_update_s64(hash, &tag->data.s64); break;
case TAG_SW: hash_update_sw(hash, &tag->data.sw); break;
case TAG_STR: hash_update_str(hash, &tag->data.str); break;
case TAG_STRUCT: hash_update_struct(hash, &tag->data.struct_); break;
case TAG_SYM: hash_update_sym(hash, &tag->data.sym); break;
case TAG_TUPLE: hash_update_tuple(hash, &tag->data.tuple); break;
case TAG_U8: hash_update_u8(hash, &tag->data.u8); break;
case TAG_U16: hash_update_u16(hash, &tag->data.u16); break;
case TAG_U32: hash_update_u32(hash, &tag->data.u32); break;
case TAG_U64: hash_update_u64(hash, &tag->data.u64); break;
case TAG_UW: hash_update_uw(hash, &tag->data.uw); break;
case TAG_VAR: hash_update_var(hash, NULL); break;
case TAG_VOID: hash_update_void(hash, NULL); break;
}
}
void hash_update_tuple (t_hash *hash, const s_tuple *tuple)
{
uw i = 0;
assert(tuple);
hash_update(hash, &tuple->count, sizeof(tuple->count));
while (i < tuple->count) {
hash_update_tag(hash, tuple->tag + i);
i++;
}
}
HASH_UPDATE_DEF(u8)
HASH_UPDATE_DEF(u16)
HASH_UPDATE_DEF(u32)
HASH_UPDATE_DEF(u64)
HASH_UPDATE_DEF(uw)
void hash_update_var (t_hash *hash, const void *x)
{
s8 type[] = "var";
(void) x;
assert(hash);
hash_update(hash, type, strlen(type));
}
void hash_update_void (t_hash *hash, const void *x)
{
s8 type[] = "void";
(void) x;
assert(hash);
hash_update(hash, type, strlen(type));
}