Hash :
b5ec33b5
Author :
Thomas de Grivel
Date :
2025-07-28T16:54:06
add missing functions for serialization of libtommath big integers
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
/* kc3
* Copyright from 2022 to 2025 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 <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "assert.h"
#include "alloc.h"
#include "buf.h"
#include "buf_file.h"
#include "endian.h"
#include "file.h"
#include "ht.h"
#include "sym.h"
#include "list.h"
#include "str.h"
#include "tag.h"
#include "tag_init.h"
#include "types.h"
#include "marshall.h"
#define DEF_MARSHALL(type) \
s_marshall * marshall_ ## type (s_marshall *m, bool heap, type src) \
{ \
s_buf *buf; \
type le; \
sw r; \
assert(m); \
le = _Generic(src, \
s16: htole16(src), \
u16: htole16(src), \
s32: htole32(src), \
u32: htole32(src), \
s64: htole64(src), \
u64: htole64(src), \
default: src); \
buf = heap ? &m->heap : &m->buf; \
if ((r = buf_write_ ## type(buf, le)) <= 0) \
return NULL; \
if (heap) \
m->heap_pos += r; \
else \
m->buf_pos += r; \
return m; \
}
DEF_MARSHALL(bool)
s_marshall * marshall_heap_pointer (s_marshall *m, bool heap, void *p,
bool *present)
{
s_buf *buf;
s_tag key = {0};
sw r;
s_tag tag = {0};
u64 offset;
assert(m);
buf = heap ? &m->heap : &m->buf;
tag_init_tuple(&key, 2);
key.data.tuple.tag[0].data.u64 = (u64) p;
if (ht_get(&m->ht, &key, &tag)) {
*present = true;
goto ok;
}
*present = false;
tag_init_tuple(&tag, 2);
tag.data.tuple.tag[0].data.u64 = (u64) p;
tag.data.tuple.tag[0].type = TAG_U64;
tag.data.tuple.tag[1].data.u64 =
(u64) m->heap_pos + sizeof(s_marshall_header);
tag.data.tuple.tag[1].type = TAG_U64;
if (! ht_add(&m->ht, &tag))
goto ko;
ok:
if (tag.type != TAG_TUPLE ||
tag.data.tuple.count != 2 ||
tag.data.tuple.tag[1].type != TAG_U64) {
err_puts("marshall_heap_pointer: invalid offset in hash table");
err_inspect_tag(&tag);
err_write_1("\n");
assert(! "marshall_heap_pointer: invalid offset in hash table");
goto ko;
}
offset = tag.data.tuple.tag[1].data.u64;
if ((r = buf_write_u64(buf, offset)) <= 0)
goto ko;
tag_clean(&key);
tag_clean(&tag);
if (heap) {
m->heap_pos += r;
m->heap_count++;
}
else
m->buf_pos += r;
return m;
ko:
tag_clean(&key);
tag_clean(&tag);
return NULL;
}
s_marshall * marshall_character (s_marshall *m, bool heap,
character src)
{
sw r;
s_buf *buf;
assert(m);
buf = heap ? &m->heap : &m->buf;
if ((r = buf_write_character_utf8(buf, src)) <= 0)
return NULL;
if (heap)
m->heap_pos += r;
else
m->buf_pos += r;
return m;
}
void marshall_clean (s_marshall *m)
{
assert(m);
buf_clean(&m->buf);
buf_clean(&m->heap);
ht_clean(&m->ht);
}
void marshall_delete (s_marshall *m)
{
marshall_clean(m);
free(m);
}
s_marshall * marshall_init (s_marshall *m)
{
s_marshall tmp = {0};
if (! ht_init(&tmp.ht, &g_sym_Tag, 1024) ||
! buf_init_alloc(&tmp.heap, 1024024))
return NULL;
if (! buf_init_alloc(&tmp.buf, 1024024)) {
buf_delete(&tmp.heap);
return NULL;
}
*m = tmp;
return m;
}
s_marshall * marshall_integer (s_marshall *m, bool heap,
const s_integer *i)
{
s_buf *buf;
sw r;
assert(m);
assert(i);
buf = heap ? &m->heap : &m->buf;
if ((r = buf_write_integer(buf, i)) <= 0)
return NULL;
if (heap)
m->heap_pos += r;
else
m->buf_pos += r;
return m;
}
s_marshall * marshall_list (s_marshall *m, bool heap,
const s_list *list)
{
assert(m);
assert(list);
if (! marshall_tag(m, heap, &list->tag) ||
! marshall_tag(m, heap, &list->next))
return NULL;
return m;
}
s_marshall * marshall_plist (s_marshall *m, bool heap,
const p_list plist)
{
assert(m);
bool present = false;
if (! m)
return NULL;
if (! marshall_heap_pointer(m, heap, plist, &present))
return NULL;
if (! present && plist)
return marshall_list(m, true, plist);
return m;
}
s_marshall * marshall_new (void)
{
s_marshall *m;
if (! (m = alloc(sizeof(s_marshall))))
return NULL;
if (marshall_init(m) == NULL) {
free(m);
return NULL;
}
return m;
}
DEF_MARSHALL(s8)
DEF_MARSHALL(s16)
DEF_MARSHALL(s32)
DEF_MARSHALL(s64)
s_marshall * marshall_str (s_marshall *m, bool heap, const s_str *src)
{
sw r;
s_buf *buf;
assert(m);
assert(src);
if (! marshall_u32(m, heap, src->size))
return NULL;
if (! src->size)
return m;
buf = heap ? &m->heap : &m->buf;
if ((r = buf_write(buf, src->ptr.pchar, src->size)) <= 0)
return NULL;
if (heap)
m->heap_pos += r;
else
m->buf_pos += r;
return m;
}
DEF_MARSHALL(sw)
s_marshall * marshall_tag (s_marshall *m, bool heap, const s_tag *tag)
{
u8 type;
assert(m);
assert(tag);
type = tag->type;
marshall_u8(m, heap, type);
switch (tag->type) {
case TAG_VOID: return m;
case TAG_ARRAY: return marshall_array(m, heap, &tag->data.array);
case TAG_BOOL: return marshall_bool(m, heap, tag->data.bool_);
case TAG_CALL: return marshall_call(m, heap, &tag->data.call);
case TAG_CHARACTER:
return marshall_character(m, heap, tag->data.character);
case TAG_DO_BLOCK:
return marshall_do_block(m, heap, &tag->data.do_block);
case TAG_F32: return marshall_f32(m, heap, tag->data.f32);
case TAG_F64: return marshall_f64(m, heap, tag->data.f64);
case TAG_F128: return marshall_f128(m, heap, tag->data.f128);
case TAG_FACT: return marshall_fact(m, heap, &tag->data.fact);
case TAG_IDENT: return marshall_ident(m, heap, &tag->data.ident);
case TAG_INTEGER:
return marshall_integer(m, heap, &tag->data.integer);
case TAG_MAP: return marshall_map(m, heap, &tag->data.map);
case TAG_PCALLABLE:
return marshall_pcallable(m, heap, tag->data.pcallable);
case TAG_PCOMPLEX:
return marshall_pcomplex(m, heap, tag->data.pcomplex);
case TAG_PCOW: return marshall_pcow(m, heap, tag->data.pcow);
case TAG_PLIST: return marshall_plist(m, heap, tag->data.plist);
case TAG_PSTRUCT:
return marshall_pstruct(m, heap, tag->data.pstruct);
case TAG_PSTRUCT_TYPE:
return marshall_pstruct_type(m, heap, tag->data.pstruct_type);
case TAG_PSYM: return marshall_psym(m, heap, tag->data.psym);
case TAG_PTAG:
return marshall_ptag(m, heap, tag->data.ptag);
case TAG_PTR: return marshall_ptr(m, heap, tag->data.ptr);
case TAG_PTR_FREE:
return marshall_ptr_free(m, heap, tag->data.ptr_free);
case TAG_PVAR: return marshall_pvar(m, heap, tag->data.pvar);
case TAG_QUOTE: return marshall_quote(m, heap, &tag->data.quote);
case TAG_RATIO: return marshall_ratio(m, heap, &tag->data.ratio);
case TAG_S8: return marshall_s8(m, heap, tag->data.s8);
case TAG_S16: return marshall_s16(m, heap, tag->data.s16);
case TAG_S32: return marshall_s32(m, heap, tag->data.s32);
case TAG_S64: return marshall_s64(m, heap, tag->data.s64);
case TAG_STR: return marshall_str(m, heap, &tag->data.str);
case TAG_SW: return marshall_sw(m, heap, tag->data.sw);
case TAG_TIME: return marshall_time(m, heap, &tag->data.time);
case TAG_TUPLE: return marshall_tuple(m, heap, &tag->data.tuple);
case TAG_U8: return marshall_u8(m, heap, tag->data.u8);
case TAG_U16: return marshall_u16(m, heap, tag->data.u16);
case TAG_U32: return marshall_u32(m, heap, tag->data.u32);
case TAG_U64: return marshall_u64(m, heap, tag->data.u64);
case TAG_UNQUOTE:
return marshall_unquote(m, heap, &tag->data.unquote);
case TAG_UW: return marshall_uw(m, heap, tag->data.uw);
}
err_write_1("marshall_tag: unknown tag type : ");
err_inspect_u8_decimal(&type);
err_write_1("\n");
assert(! "marshall_tag: unknown tag type");
return NULL;
}
sw marshall_to_buf (s_marshall *m, s_buf *out)
{
s_marshall_header mh = {0};
sw r;
sw result = 0;
assert(m);
assert(out);
mh.le_magic = htole64(MARSHALL_MAGIC);
mh.le_heap_count = htole64(m->heap_count);
mh.le_heap_size = htole64(m->heap_pos);
mh.le_buf_size = htole64(m->buf_pos);
if ((r = buf_write(out, &mh, sizeof(mh))) != sizeof(mh))
return -1;
result += r;
if ((r = buf_xfer(out, &m->heap, m->heap_pos)) != m->heap_pos)
return -1;
result += r;
if ((r = buf_xfer(out, &m->buf, m->buf_pos)) != m->buf_pos)
return -1;
result += r;
return result;
}
sw marshall_to_file (s_marshall *m, const char *path)
{
FILE *fp;
s_buf out;
sw r;
sw result = 0;
assert(m);
assert(path);
if (! buf_init_alloc(&out, 1024 * 1024))
return -1;
if (! (fp = file_open(path, "wb")) ||
! buf_file_open_w(&out, fp) ||
(r = marshall_to_buf(m, &out)) <= 0) {
buf_clean(&out);
return -1;
}
result = r;
buf_file_close(&out);
buf_clean(&out);
return result;
}
s_str * marshall_to_str (s_marshall *m, s_str *dest)
{
s_buf out;
sw r;
assert(m);
assert(dest);
if (! buf_init_alloc(&out, 1024 * 1024))
return NULL;
if ((r = marshall_to_buf(m, &out)) <= 0) {
buf_clean(&out);
return NULL;
}
if (! buf_read_to_str(&out, dest)) {
buf_clean(&out);
return NULL;
}
buf_clean(&out);
return dest;
}
s_marshall * marshall_tuple (s_marshall *m, bool heap,
const s_tuple *tuple)
{
uw i;
assert(m);
assert(tuple);
if (! marshall_uw(m, heap, tuple->count))
return NULL;
i = 0;
while (i < tuple->count) {
if (! marshall_tag(m, heap, tuple->tag + i))
return NULL;
i++;
}
return m;
}
DEF_MARSHALL(u8)
DEF_MARSHALL(u16)
DEF_MARSHALL(u32)
DEF_MARSHALL(u64)
DEF_MARSHALL(uw)
#define DEF_MARSHALL_STUB(name, type) \
PROTO_MARSHALL(name, type) \
{ \
(void) m; \
(void) heap; \
(void) src; \
err_puts("marshall_" # name ": not implemented"); \
return NULL; \
}
DEF_MARSHALL_STUB(array, const s_array *)
DEF_MARSHALL_STUB(call, const s_call *)
DEF_MARSHALL_STUB(do_block, const s_do_block *)
DEF_MARSHALL_STUB(f32, f32)
DEF_MARSHALL_STUB(f64, f64)
DEF_MARSHALL_STUB(f128, f128)
DEF_MARSHALL_STUB(fact, const s_fact *)
DEF_MARSHALL_STUB(ident, const s_ident *)
DEF_MARSHALL_STUB(map, const s_map *)
DEF_MARSHALL_STUB(pcallable, p_callable)
DEF_MARSHALL_STUB(pcomplex, p_complex)
DEF_MARSHALL_STUB(pcow, p_cow)
DEF_MARSHALL_STUB(pstruct, p_struct)
DEF_MARSHALL_STUB(pstruct_type, p_struct_type)
DEF_MARSHALL_STUB(ptag, p_tag)
DEF_MARSHALL_STUB(ptr, u_ptr_w)
DEF_MARSHALL_STUB(ptr_free, u_ptr_w)
DEF_MARSHALL_STUB(psym, p_sym)
DEF_MARSHALL_STUB(pvar, p_var)
DEF_MARSHALL_STUB(quote, const s_quote *)
DEF_MARSHALL_STUB(ratio, const s_ratio *)
DEF_MARSHALL_STUB(struct, const s_struct *)
DEF_MARSHALL_STUB(struct_type, const s_struct_type *)
DEF_MARSHALL_STUB(sym, const s_sym *)
DEF_MARSHALL_STUB(time, const s_time *)
DEF_MARSHALL_STUB(unquote, const s_unquote *)
DEF_MARSHALL_STUB(var, const s_var *)