Hash :
a7ae2f44
Author :
Thomas de Grivel
Date :
2023-04-07T23:18:15
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
/* 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"
#define HASH_UPDATE_DEF(type) \
void hash_update_##type (t_hash *hash, type x) \
{ \
const s8 t[] = #type; \
assert(hash); \
hash_update(hash, t, sizeof(t)); \
hash_update(hash, &x, sizeof(x)); \
}
void hash_clean (t_hash *hash)
{
bzero(hash, sizeof(t_hash));
}
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++;
}
hash_update(hash, a->data, a->size);
}
void hash_update_bool (t_hash *hash, e_bool x)
{
const s8 type[] = "bool";
assert(hash);
hash_update(hash, type, sizeof(type));
bool 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, 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, 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);
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)
{
uw count = 0;
const s_fn *f;
const s8 type[] = "fn";
assert(hash);
assert(fn);
hash_update(hash, type, sizeof(type));
f = fn;
while (f) {
count++;
f = f->next_clause;
}
hash_update_uw(hash, count);
while (fn) {
hash_update_list(hash, fn->pattern);
hash_update_list(hash, fn->algo);
fn = fn->next_clause;
}
}
void hash_update_ident (t_hash *hash, const s_ident *ident)
{
assert(hash);
assert(ident);
hash_update_u8(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 *list)
{
uw count;
const s_list *last;
const s8 type[] = "list";
hash_update(hash, type, sizeof(type));
count = list_length(list);
hash_update_uw(hash, count);
if (list) {
while (list) {
hash_update_tag(hash, &list->tag);
last = list;
list = list_next(list);
}
hash_update_tag(hash, &last->next);
}
}
void hash_update_ptag (t_hash *hash, const p_tag ptag)
{
const s8 type[] = "ptag";
hash_update(hash, type, strlen(type));
hash_update(hash, &ptag, sizeof(ptag));
}
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)
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_sym (t_hash *hash, const s_sym *sym)
{
assert(hash);
assert(sym);
hash_update_u8(hash, ':');
hash_update_str(hash, &sym->str);
}
void hash_update_tag (t_hash *hash, const s_tag *tag)
{
assert(hash);
assert(tag);
hash_update_u64(hash, tag->type.type);
switch (tag->type.type) {
case TAG_VOID: break;
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:
case TAG_CALL_FN:
case TAG_CALL_MACRO:
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_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, tag->data.list); break;
case TAG_PTAG: hash_update_ptag(hash, tag->data.ptag); 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_STR: hash_update_str(hash, &tag->data.str); 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_VAR:
assert(! "var hash update");
errx(1, "var hash update");
}
}
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)