-
Notifications
You must be signed in to change notification settings - Fork 1
/
archiver.cc
592 lines (538 loc) · 11.2 KB
/
archiver.cc
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#define ICI_CORE
/*
* Object Serialization
*
* The ICI 'archiver' serializes ICI object graphs, reading and
* writing a binary encoded representation of the ICI objects using a
* format known as the ICI object serialization protocol.
*
* All builtin object types, apart from files, may be saved and
* subsequently restored, this includes function objects.
*
* The two functions save and restore write and read objects. Both
* functions may be passed an ICI file object used as the source or
* destination of object data. The functions default to using the
* standard input and output files as per other ICI I/O functions.
*
* Serialization Protocol
*
* The ICI object serialization protocol is a tagged binary protocol.
* The protocol is platform independent in that it defines the size
* and format of all exchanged data and conforming implementations
* must adhere to those sizes and formats.
*
* All integral types are represented in network byte order, floating
* pointed is defined to be 64-bit IEEE-754 double precision values,
* byte swapped in the same manner as 64-bit integers. Object sizes
* and lengths are sent as 32-bit unsigned values.
*
* The exact protocol follows.
*
* tcode byte, top-bit set if object is an ICI atom
*
* A tcode encodes two values. A type code in the range
* [0, 127] and a flag indicating the value is an ICI
* atom, an atomic, read-only value.
*
*
* object ::- tcode <tcode-specific-data> ;
*
* An object is sent as a _tcode_ (type code) followed by zero
* or more bytes of data specific to that type.
*
* null ::- tcode
* int ::- tcode <int64>
* float ::- tcode <float64>
* string ::- tcode <length> [<byte>...]
* regexp ::- tcode <options> <length> [<byte>...]
* array ::- tcode <length> [<object>...]
* set ::- tcode <length> [<object>...]
* map ::- tcode <length> [ <key> <value> ]...
* mem ::- tcode <accessz> <length> <byte>...
*
* op ::- tcode
* func ::- tcode
* cfunc ::- tcode
*
* This --intro-- and --synopsis-- are part of --ici-serialisation-- documentation.
*/
#include "archiver.h"
#include "cfunc.h"
#include "file.h"
#include "fwd.h"
#include "int.h"
#include "map.h"
#include "null.h"
#include "op.h"
#include <netinet/in.h>
namespace ici
{
static void swapout(void *ptr, int sz)
{
#if defined(ICI_ARCHIVE_LITTLE_ENDIAN_HOST)
switch (sz)
{
case 2: {
int16_t *v = (int16_t *)ptr;
auto tmp = htons(*v);
*v = tmp;
}
break;
case 4: {
int32_t *v = (int32_t *)ptr;
auto tmp = htonl(*v);
*v = tmp;
}
break;
case 8: {
int64_t *v = (int64_t *)ptr;
auto tmp = htonll(*v);
*v = tmp;
}
break;
default:
abort();
}
#else
(void)ptr;
(void)sz;
#endif
}
static void swapin(void *ptr, int sz)
{
#if defined(ICI_ARCHIVE_LITTLE_ENDIAN_HOST)
switch (sz)
{
case 2: {
int16_t *v = (int16_t *)ptr;
auto tmp = ntohs(*v);
*v = tmp;
}
break;
case 4: {
int32_t *v = (int32_t *)ptr;
auto tmp = ntohl(*v);
*v = tmp;
}
break;
case 8: {
int64_t *v = (int64_t *)ptr;
auto tmp = ntohll(*v);
*v = tmp;
}
break;
default:
abort();
}
#else
(void)ptr;
(void)sz;
#endif
}
#ifdef BINOPFUNC
constexpr auto num_op_funcs = 13;
#else
constexpr auto num_op_funcs = 12;
#endif
typedef int int_func();
static int_func *op_funcs[num_op_funcs];
int archive_init()
{
op_funcs[0] = nullptr;
op_funcs[1] = o_mklvalue.op_func;
op_funcs[2] = o_onerror.op_func;
op_funcs[3] = o_return.op_func;
op_funcs[4] = o_mkptr.op_func;
op_funcs[5] = o_openptr.op_func;
op_funcs[6] = o_fetch.op_func;
op_funcs[7] = op_unary;
op_funcs[8] = op_forall;
op_funcs[9] = op_for;
op_funcs[10] = op_onerror;
op_funcs[11] = op_return;
#ifdef BINOPFUNC
op_funcs[12] = op_binop;
#endif
return 0;
}
void archive_uninit()
{
}
inline ref<integer> make_key(object *obj)
{
return make_ref(new_int((int64_t)obj));
}
archiver::archiver(file *f, objwsup *scope)
: a_file(f)
, a_scope(scope)
, a_sent(new_map())
, a_names(new_array())
{
}
archiver::operator bool() const
{
return a_sent != nullptr && a_names != nullptr;
}
int archiver::push_name(str *name)
{
return a_names->push_back(name);
}
int archiver::pop_name()
{
a_names->pop_back();
return 0;
}
int archiver::record(object *obj, object *ref)
{
if (auto key = make_key(obj))
{
return a_sent->assign(key, ref);
}
return 1;
}
void archiver::remove(object *obj)
{
if (auto k = make_key(obj))
{
unassign(a_sent, k);
}
}
int archiver::save_name(object *o)
{
const int64_t value = (int64_t)o;
if (record(o, o))
{
return 1;
}
if (write(value))
{
return 1;
}
return 0;
}
int archiver::restore_name(object **name)
{
int64_t value;
if (read(&value))
{
return 1;
}
*name = (object *)value;
return 0;
}
int archiver::save_ref(object *o)
{
const uint8_t tcode = TC_REF;
const int64_t ref = (int64_t)o;
if (write(tcode))
{
return 1;
}
if (write(ref))
{
return 1;
}
return 0;
}
object *archiver::restore_ref()
{
object *name;
if (restore_name(&name))
{
return nullptr;
}
if (auto o = lookup(name))
{
incref(o);
return o;
}
return nullptr;
}
object *archiver::lookup(object *obj)
{
object *v = nullptr;
if (auto k = make_key(obj))
{
v = a_sent->fetch(k);
}
return v == null ? nullptr : v;
}
int archiver::op_func_code(int_func *fn)
{
for (size_t i = 0; i < num_op_funcs; ++i)
{
if (fn == op_funcs[i])
{
return int(i);
}
}
return -1;
}
int_func *archiver::op_func(int code)
{
if (code < 0 || size_t(code) >= num_op_funcs)
{
return nullptr;
}
return op_funcs[code];
}
int archiver::read(int16_t *hword)
{
if (read(hword, sizeof *hword))
{
return 1;
}
swapin(hword, sizeof *hword);
return 0;
}
int archiver::read(int32_t *aword)
{
if (read(aword, sizeof *aword))
{
return 1;
}
swapin(aword, sizeof *aword);
return 0;
}
int archiver::read(int64_t *dword)
{
if (read(dword, sizeof *dword))
{
return 1;
}
swapin(dword, sizeof *dword);
return 0;
}
int archiver::read(float *flt)
{
if (read(flt, sizeof *flt))
{
return 1;
}
swapin(flt, sizeof *flt);
return 0;
}
int archiver::read(double *dbl)
{
if (read(dbl, sizeof *dbl))
{
return 1;
}
swapin(dbl, sizeof *dbl);
return 0;
}
int archiver::write(int16_t hword)
{
swapout(&hword, sizeof hword);
return write(&hword, sizeof hword);
}
int archiver::write(int32_t aword)
{
swapout(&aword, sizeof aword);
return write(&aword, sizeof aword);
}
int archiver::write(int64_t dword)
{
swapout(&dword, sizeof dword);
return write(&dword, sizeof dword);
}
int archiver::write(float v)
{
swapout(&v, sizeof v);
return write(&v, sizeof v);
}
int archiver::write(double v)
{
swapout(&v, sizeof v);
return write(&v, sizeof v);
}
int archiver::save(object *o)
{
if (auto p = lookup(o))
{ // if already sent in this session
return save_ref(p); // save a reference to the object
}
uint8_t tcode = o->o_tcode & object::O_ICIBITS; // mask out user-bits
if (o->isatom())
{
tcode |= O_ARCHIVE_ATOMIC;
}
if (write(tcode))
{
return 1;
}
if (o->icitype()->save(this, o))
{
return 1;
}
return 0;
}
object *archiver::restore()
{
uint8_t tcode;
uint8_t flags = 0;
if (read(&tcode))
{
return nullptr;
}
if (tcode == TC_REF)
{
return restore_ref();
}
if ((tcode & O_ARCHIVE_ATOMIC) != 0)
{
flags |= object::O_ATOM;
tcode &= ~O_ARCHIVE_ATOMIC;
}
auto no_type = [&]() {
set_error("no type with code %02X", tcode);
return nullptr;
};
if (tcode >= num_types)
{
return no_type();
}
auto t = types[tcode];
if (!t)
{
return no_type();
}
auto o = t->restore(this);
if (!o)
{
return nullptr;
}
if (flags & object::O_ATOM)
{
o = atom(o, 1);
}
return o;
}
/*
* save(any, [file [, map]])
*
* Save an object to a file by writing a serialized object graph
* using the given object as the root of the graph.
*
* If file is not given the object is written to the standard output.
*
* If map is supplied it is used as the 'scope' during the
* save operation.
*
* This --topic-- forms part of the --ici-serialisation-- documentation.
*/
int f_archive_save(...)
{
objwsup *scp = mapof(vs.a_top[-1])->o_super;
file *file;
object *obj;
int failed = 1;
switch (NARGS())
{
case 3:
if (typecheck("oud", &obj, &file, &scp))
{
return 1;
}
break;
case 2:
if (typecheck("ou", &obj, &file))
{
return 1;
}
break;
case 1:
if (typecheck("o", &obj))
{
return 1;
}
if ((file = need_stdout()) == nullptr)
{
return 1;
}
break;
default:
return argerror(2);
}
archiver ar(file, scp);
if (ar)
{
failed = ar.save(obj);
}
return failed ? failed : null_ret();
}
int f_archive_restore(...)
{
file *file;
objwsup *scp;
object *obj = nullptr;
scp = mapof(vs.a_top[-1])->o_super;
switch (NARGS())
{
case 0:
if ((file = need_stdin()) == nullptr)
{
return 1;
}
break;
case 1:
if (typecheck("u", &file))
{
if (typecheck("d", &scp))
{
return 1;
}
if ((file = need_stdin()) == nullptr)
{
return 1;
}
}
break;
default:
if (typecheck("ud", &file, &scp))
{
return 1;
}
break;
}
archiver ar(file, scp);
if (ar)
{
obj = ar.restore();
}
return obj == nullptr ? 1 : ret_with_decref(obj);
}
ICI_DEFINE_CFUNCS(save_restore)
{
ICI_DEFINE_CFUNC(save, f_archive_save),
ICI_DEFINE_CFUNC(restore, f_archive_restore),
ICI_CFUNCS_END()
};
str *archiver::name_qualifier()
{
if (a_names->len() == 0)
{
return stringof(SS(empty_string));
}
auto e = a_names->astart();
int len = stringof(*e)->s_nchars;
for (e = a_names->anext(e); e != a_names->alimit(); e = a_names->anext(e))
{
len = len + 1 /* '.' (dot) */ + stringof(*e)->s_nchars;
}
auto s = str_alloc(len);
if (!s)
{
return nullptr;
}
auto p = s->s_chars;
for (e = a_names->astart(); e != a_names->alimit(); e = a_names->anext(e))
{
auto s = stringof(*e);
memcpy(p, s->s_chars, s->s_nchars);
p += s->s_nchars;
*p++ = '.';
}
return str_intern(s);
}
} // namespace ici