summaryrefslogtreecommitdiff
path: root/src/lib/ubjson/ubjw.c
blob: 9fce397eb153e6e18a6c60d832476eca0581fa5c (plain)
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include "ubj.h"
#include "ubj_internal.h"

#ifdef MULTIPASS_TRACE_MALLOC
#include "lib/mpmalloc.h"
#else
#define mpmalloc malloc
#define mpfree free
#endif

#define CONTAINER_IS_SIZED		0x1
#define CONTAINER_IS_TYPED		0x2
#define CONTAINER_IS_UBJ_ARRAY		0x4
#define CONTAINER_IS_UBJ_OBJECT		0x8

#define CONTAINER_EXPECTS_KEY	0x10

#define CONTAINER_STACK_MAX		16
#define BUFFER_OUT_SIZE			1024

#define MAX_DIMS	8


struct priv_ubjw_container_t
{
	uint8_t flags;
	UBJ_TYPE type;
	size_t elements_remaining;
};

struct ubjw_context_t_s
{
	size_t(*write_cb)(const void* data, size_t size, size_t count, void* userdata);
	int(*close_cb)(void* userdata);
	void (*error_cb)(const char* error_msg);
	
	void* userdata;

	struct priv_ubjw_container_t container_stack[CONTAINER_STACK_MAX];
	struct priv_ubjw_container_t* head;

	uint8_t ignore_container_flags;

	uint16_t last_error_code;

	size_t total_written;
};



ubjw_context_t* ubjw_open_callback(void* userdata,
	size_t(*write_cb)(const void* data, size_t size, size_t count, void* userdata),
	int(*close_cb)(void* userdata),
	void (*error_cb)(const char* error_msg)
 					)
{
	ubjw_context_t* ctx = (ubjw_context_t*)mpmalloc(sizeof(ubjw_context_t));
	ctx->userdata = userdata;
	ctx->write_cb = write_cb;
	ctx->close_cb = close_cb;
	ctx->error_cb = error_cb;
	
	ctx->head = ctx->container_stack;
	ctx->head->flags = 0;
	ctx->head->type = UBJ_MIXED;
	ctx->head->elements_remaining = 0;
	//ctx->head->num_dims=1;

	ctx->ignore_container_flags = 0;

	ctx->last_error_code = 0;

	ctx->total_written = 0;
	return ctx;
}
ubjw_context_t* ubjw_open_file(FILE* fd)
{
	return ubjw_open_callback(fd, (void*)fwrite,(void*)fclose,NULL);
}

struct mem_w_fd
{
	uint8_t *begin,*current, *end;
};

static int memclose(void* mfd)
{
	mpfree(mfd);
	return 0;
}
static size_t memwrite(const void* data, size_t size, size_t count, struct mem_w_fd* fp)
{
	size_t n = size*count;
	size_t lim = fp->end - fp->current;
	if (lim < n)
	{
		n = lim;
	}
	memcpy(fp->current, data, n);
	fp->current += n;
	return n;
}

ubjw_context_t* ubjw_open_memory(uint8_t* be, uint8_t* en)
{
	struct mem_w_fd* mfd = (struct mem_w_fd*)mpmalloc(sizeof(struct mem_w_fd));
	mfd->current = be;
	mfd->begin = be;
	mfd->end = en;
	return ubjw_open_callback(mfd, (void*)memwrite, (void*)memclose,NULL);
}

static inline void priv_ubjw_context_append(ubjw_context_t* ctx, uint8_t a)
{
	ctx->total_written += 1;
	ctx->write_cb(&a, 1, 1, ctx->userdata);
}

static inline void priv_disassembly_begin(ubjw_context_t* ctx)
{
#ifdef UBJW_DISASSEMBLY_MODE
	priv_ubjw_context_append(ctx, (uint8_t)'[');
#endif
}
static inline void priv_disassembly_end(ubjw_context_t* ctx)
{
#ifdef UBJW_DISASSEMBLY_MODE
	priv_ubjw_context_append(ctx, (uint8_t)']');
#endif
}
static inline void priv_disassembly_indent(ubjw_context_t* ctx)
{
#ifdef UBJW_DISASSEMBLY_MODE
	int n = ctx->head - ctx->container_stack;
	int i;
	priv_ubjw_context_append(ctx, (uint8_t)'\n');
	for (i = 0; i < n; i++)
	{
		priv_ubjw_context_append(ctx, (uint8_t)'\t');
	}
#endif
}

static inline void priv_ubjw_context_finish_container(ubjw_context_t* ctx, struct priv_ubjw_container_t* head)
{
	if (head->flags & CONTAINER_IS_SIZED)
	{
		if (head->elements_remaining > 0)
		{
			//error not all elements written
		}
	}
	else
	{
		priv_disassembly_begin(ctx);
		if (head->flags & CONTAINER_IS_UBJ_ARRAY)
		{
			priv_ubjw_context_append(ctx, (uint8_t)']');
		}
		else if (head->flags & CONTAINER_IS_UBJ_OBJECT)
		{
			priv_ubjw_context_append(ctx, (uint8_t)'}');
		}
		priv_disassembly_end(ctx);
	}
}

static inline priv_ubjw_container_stack_push(ubjw_context_t* ctx, const struct priv_ubjw_container_t* cnt)
{
	size_t height = ctx->head-ctx->container_stack+1;
	if(height < CONTAINER_STACK_MAX)
	{
		*(++(ctx->head))=*cnt;
	}
	else
	{
		//todo::error
	}
}
static inline struct priv_ubjw_container_t priv_ubjw_container_stack_pop(ubjw_context_t* ctx)
{
	return *ctx->head--;
}

size_t ubjw_close_context(ubjw_context_t* ctx)
{
	while (ctx->head > ctx->container_stack)
	{
		struct priv_ubjw_container_t cnt = priv_ubjw_container_stack_pop(ctx);
		priv_ubjw_context_finish_container(ctx, &cnt);
	};
	size_t n = ctx->total_written;
	if (ctx->close_cb)
		ctx->close_cb(ctx->userdata);
	mpfree(ctx);
	return n;
}


static inline size_t priv_ubjw_context_write(ubjw_context_t* ctx, const uint8_t* data, size_t sz)
{
	ctx->total_written += sz;
	return ctx->write_cb(data, 1, sz, ctx->userdata);
}

static inline void priv_ubjw_tag_public(ubjw_context_t* ctx, UBJ_TYPE tid)
{
	struct priv_ubjw_container_t* ch = ctx->head;
	if (!ctx->ignore_container_flags)
	{

		/*if (
			(!(ch->flags & (CONTAINER_IS_UBJ_ARRAY | CONTAINER_IS_UBJ_OBJECT))) &&
			(tid != UBJ_ARRAY && tid !=UBJ_OBJECT))
		{
			//error, only array and object can be first written
		}*/

		if (ch->flags & CONTAINER_IS_UBJ_OBJECT)
		{
			if (ch->flags & CONTAINER_EXPECTS_KEY)
			{
				//error,a key expected
				return;
			}
			ch->flags |= CONTAINER_EXPECTS_KEY; //set key expected next time in this context
		}
		else
		{
			priv_disassembly_indent(ctx);
		}

		if (ch->flags & CONTAINER_IS_SIZED)
		{
			ch->elements_remaining--; //todo: error if elements remaining is 0;
		}

		if ((ch->flags & CONTAINER_IS_TYPED) && ch->type == tid)
		{
			return;
		}
	}
	priv_disassembly_begin(ctx);
	priv_ubjw_context_append(ctx, UBJI_TYPEC_convert[tid]);
	priv_disassembly_end(ctx);
}

static inline void priv_ubjw_write_raw_string(ubjw_context_t* ctx, const char* out)//TODO: possibly use a safe string
{
	size_t n = strlen(out);
	ctx->ignore_container_flags = 1; 
	ubjw_write_integer(ctx, (int64_t)n);
	ctx->ignore_container_flags = 0;
	priv_disassembly_begin(ctx);
	priv_ubjw_context_write(ctx, (const uint8_t*)out, n);
	priv_disassembly_end(ctx);
}
void ubjw_write_string(ubjw_context_t* ctx, const char* out)
{
	priv_ubjw_tag_public(ctx,UBJ_STRING);
	priv_ubjw_write_raw_string(ctx, out);
}

static inline void priv_ubjw_write_raw_char(ubjw_context_t* ctx, char out)
{
	priv_disassembly_begin(ctx);
	priv_ubjw_context_append(ctx, (uint8_t)out);
	priv_disassembly_end(ctx);
}
void ubjw_write_char(ubjw_context_t* ctx, char out)
{
	priv_ubjw_tag_public(ctx,UBJ_CHAR);
	priv_ubjw_write_raw_char(ctx, out);
}

#ifndef min
static inline size_t min(size_t x,size_t y)
{
	return x < y ? x : y;
}
#endif

#ifdef UBJW_DISASSEMBLY_MODE
#include <stdarg.h>  
#define DISASSEMBLY_PRINT_BUFFER_SIZE 1024

static inline priv_disassembly_print(ubjw_context_t* ctx, const char* format,...)
{
	char buffer[DISASSEMBLY_PRINT_BUFFER_SIZE];
	va_list args; 
	va_start(args, format);
	int n=vsnprintf(buffer, DISASSEMBLY_PRINT_BUFFER_SIZE, format, args);
	n = min(n, DISASSEMBLY_PRINT_BUFFER_SIZE);
	priv_ubjw_context_write(ctx, buffer,n);
	va_end(args);
}
#endif

static inline void priv_ubjw_write_raw_uint8(ubjw_context_t* ctx, uint8_t out)
{
	priv_disassembly_begin(ctx);
#ifndef UBJW_DISASSEMBLY_MODE
	priv_ubjw_context_append(ctx, out);
#else
	priv_disassembly_print(ctx, "%hhu", out);
#endif
	priv_disassembly_end(ctx);
}
void ubjw_write_uint8(ubjw_context_t* ctx, uint8_t out)
{
	priv_ubjw_tag_public(ctx,UBJ_UINT8);
	priv_ubjw_write_raw_uint8(ctx, out);
}

static inline void priv_ubjw_write_raw_int8(ubjw_context_t* ctx, int8_t out)
{
	priv_disassembly_begin(ctx);
#ifndef UBJW_DISASSEMBLY_MODE
	priv_ubjw_context_append(ctx, *(uint8_t*)&out);
#else
	priv_disassembly_print(ctx, "%hhd", out);
#endif
	priv_disassembly_end(ctx);
}
void ubjw_write_int8(ubjw_context_t* ctx, int8_t out)
{
	priv_ubjw_tag_public(ctx,UBJ_INT8);
	priv_ubjw_write_raw_int8(ctx, out);
}

static inline void priv_ubjw_write_raw_int16(ubjw_context_t* ctx, int16_t out)
{
	priv_disassembly_begin(ctx);
#ifndef UBJW_DISASSEMBLY_MODE
	uint8_t buf[2];
	_to_bigendian16(buf, *(uint16_t*)&out);
	priv_ubjw_context_write(ctx, buf, 2);
#else
	priv_disassembly_print(ctx, "%hd", out);
#endif
	priv_disassembly_end(ctx);
}
void ubjw_write_int16(ubjw_context_t* ctx, int16_t out)
{
	priv_ubjw_tag_public(ctx,UBJ_INT16);
	priv_ubjw_write_raw_int16(ctx, out);
}
static inline void priv_ubjw_write_raw_int32(ubjw_context_t* ctx, int32_t out)
{
	priv_disassembly_begin(ctx);
#ifndef UBJW_DISASSEMBLY_MODE
	uint8_t buf[4];
	_to_bigendian32(buf, *(uint32_t*)&out);
	priv_ubjw_context_write(ctx, buf, 4);
#else
	priv_disassembly_print(ctx, "%ld", out);
#endif
	priv_disassembly_end(ctx);
}
void ubjw_write_int32(ubjw_context_t* ctx, int32_t out)
{
	priv_ubjw_tag_public(ctx,UBJ_INT32);
	priv_ubjw_write_raw_int32(ctx, out);
}
static inline void priv_ubjw_write_raw_int64(ubjw_context_t* ctx, int64_t out)
{
	priv_disassembly_begin(ctx);
#ifndef UBJW_DISASSEMBLY_MODE
	uint8_t buf[8];
	_to_bigendian64(buf, *(uint64_t*)&out);
	priv_ubjw_context_write(ctx, buf, 8);
#else
	priv_disassembly_print(ctx, "%lld", out);
#endif
	priv_disassembly_end(ctx);
}
void ubjw_write_int64(ubjw_context_t* ctx, int64_t out)
{
	priv_ubjw_tag_public(ctx,UBJ_INT64);
	priv_ubjw_write_raw_int64(ctx, out);
}

void ubjw_write_high_precision(ubjw_context_t* ctx, const char* hp)
{
	priv_ubjw_tag_public(ctx,UBJ_HIGH_PRECISION);
	priv_ubjw_write_raw_string(ctx, hp);
}
UBJ_TYPE ubjw_min_integer_type(int64_t in)
{
	uint64_t mc = llabs(in);
	if (mc < 0x80)
	{
		return UBJ_INT8;
	}
	else if (in > 0 && mc < 0x100)
	{
		return UBJ_UINT8;
	}
	else if (mc < 0x8000)
	{
		return UBJ_INT16;
	}
	else if (mc < 0x80000000)
	{
		return UBJ_INT32;
	}
	else
	{
		return UBJ_INT64;
	}
}

void ubjw_write_integer(ubjw_context_t* ctx, int64_t out)
{
	switch (ubjw_min_integer_type(out))
	{
	case UBJ_INT8:
		ubjw_write_int8(ctx, (int8_t)out);
		break;
	case UBJ_UINT8:
		ubjw_write_uint8(ctx, (uint8_t)out);
		break;
	case UBJ_INT16:
		ubjw_write_int16(ctx, (int16_t)out);
		break;
	case UBJ_INT32:
		ubjw_write_int32(ctx, (int32_t)out);
		break;
	default:
		ubjw_write_int64(ctx, out);
		break;
	};
}

static inline void priv_ubjw_write_raw_float32(ubjw_context_t* ctx, float out)
{
	priv_disassembly_begin(ctx);
#ifndef UBJW_DISASSEMBLY_MODE
	uint32_t fout = *(uint32_t*)&out;
	uint8_t outbuf[4];
	_to_bigendian32(outbuf, fout);
	priv_ubjw_context_write(ctx, outbuf, 4);
#else
	priv_disassembly_print(ctx, "%g", out);
#endif
	priv_disassembly_end(ctx);

}
void ubjw_write_float32(ubjw_context_t* ctx, float out)
{
	priv_ubjw_tag_public(ctx,UBJ_FLOAT32);
	priv_ubjw_write_raw_float32(ctx, out);
}
static inline void priv_ubjw_write_raw_float64(ubjw_context_t* ctx, double out)
{
	priv_disassembly_begin(ctx);
#ifndef UBJW_DISASSEMBLY_MODE
	uint64_t fout = *(uint64_t*)&out;
	uint8_t outbuf[8];
	_to_bigendian64(outbuf, fout);
	priv_ubjw_context_write(ctx, outbuf, 8);
#else
	priv_disassembly_print(ctx, "%g", out);
#endif
	priv_disassembly_end(ctx);
}
void ubjw_write_float64(ubjw_context_t* ctx, double out)
{
	priv_ubjw_tag_public(ctx,UBJ_FLOAT64);
	priv_ubjw_write_raw_float64(ctx, out);
}

void ubjw_write_floating_point(ubjw_context_t* ctx, double out)
{
	//this may not be possible to implement correctly...for now we just write it as a float64'
	ubjw_write_float64(ctx,out);
}

void ubjw_write_noop(ubjw_context_t* ctx)
{
	priv_ubjw_tag_public(ctx,UBJ_NOOP);
}
void ubjw_write_null(ubjw_context_t* ctx)
{
	priv_ubjw_tag_public(ctx,UBJ_NULLTYPE);
}
void ubjw_write_bool(ubjw_context_t* ctx, uint8_t out)
{
	priv_ubjw_tag_public(ctx,(out ? UBJ_BOOL_TRUE : UBJ_BOOL_FALSE));
}

void priv_ubjw_begin_container(struct priv_ubjw_container_t* cnt, ubjw_context_t* ctx, UBJ_TYPE typ, size_t count)
{
	cnt->flags=0;
	cnt->elements_remaining = count;
	cnt->type = typ;

	if (typ != UBJ_MIXED)
	{
		if (count == 0)
		{
			//error and return;
		}
		priv_disassembly_begin(ctx);
		priv_ubjw_context_append(ctx, '$');
		priv_disassembly_end(ctx);

		priv_disassembly_begin(ctx);
		priv_ubjw_context_append(ctx, UBJI_TYPEC_convert[typ]);
		priv_disassembly_end(ctx);

		cnt->flags |= CONTAINER_IS_TYPED;
	}
	if (count != 0)
	{
		priv_disassembly_begin(ctx);
		priv_ubjw_context_append(ctx, '#');
		priv_disassembly_end(ctx);

		ctx->ignore_container_flags = 1;
		ubjw_write_integer(ctx, (int64_t)count);
		ctx->ignore_container_flags = 0;
		
		cnt->flags |= CONTAINER_IS_SIZED;
		cnt->elements_remaining = count;
	}
}
void ubjw_begin_array(ubjw_context_t* ctx, UBJ_TYPE type, size_t count)
{
	priv_ubjw_tag_public(ctx, UBJ_ARRAY); //todo: should this happen before any erro potential?
	struct priv_ubjw_container_t ch;
	priv_ubjw_begin_container(&ch, ctx, type, count);
	ch.flags |= CONTAINER_IS_UBJ_ARRAY;
	priv_ubjw_container_stack_push(ctx, &ch);
}
void ubjw_begin_ndarray(ubjw_context_t* dst, UBJ_TYPE type, const size_t* dims, uint8_t ndims);
void ubjw_write_ndbuffer(ubjw_context_t* dst, const uint8_t* data, UBJ_TYPE type, const size_t* dims, uint8_t ndims);

void ubjw_begin_object(ubjw_context_t* ctx, UBJ_TYPE type, size_t count)
{
	priv_ubjw_tag_public(ctx, UBJ_OBJECT);
	struct priv_ubjw_container_t ch;
	priv_ubjw_begin_container(&ch, ctx, type, count);
	ch.flags |= CONTAINER_EXPECTS_KEY | CONTAINER_IS_UBJ_OBJECT;
	priv_ubjw_container_stack_push(ctx, &ch);
}
void ubjw_write_key(ubjw_context_t* ctx, const char* key)
{
	if (ctx->head->flags & CONTAINER_EXPECTS_KEY && ctx->head->flags & CONTAINER_IS_UBJ_OBJECT)
	{
		priv_disassembly_indent(ctx);
		priv_ubjw_write_raw_string(ctx, key);
		ctx->head->flags ^= CONTAINER_EXPECTS_KEY; //turn off container 
	}
	else
	{
		//error unexpected key
	}
}
void ubjw_end(ubjw_context_t* ctx)
{
	struct priv_ubjw_container_t ch = priv_ubjw_container_stack_pop(ctx);
	if ((ch.flags & CONTAINER_IS_UBJ_OBJECT) && !(ch.flags & CONTAINER_EXPECTS_KEY))
	{
		//error expected value
	}
	priv_disassembly_indent(ctx);
	priv_ubjw_context_finish_container(ctx, &ch);
}


// Not used by benchmark.py -> high BUFFER_OUT_SIZE does not matter
static inline void priv_ubjw_write_byteswap(ubjw_context_t* ctx, const uint8_t* data, int sz, size_t count)
{
	uint8_t buf[BUFFER_OUT_SIZE];

	size_t i;
	size_t nbytes = sz*count;
	for (i = 0; i < nbytes; i+=BUFFER_OUT_SIZE)
	{
		size_t npass = min(nbytes - i, BUFFER_OUT_SIZE);
		memcpy(buf, data + i, npass);
		buf_endian_swap(buf, sz, npass/sz);
		priv_ubjw_context_write(ctx, buf, npass);
	}
}
void ubjw_write_buffer(ubjw_context_t* ctx, const uint8_t* data, UBJ_TYPE type, size_t count)
{
	int typesz = UBJI_TYPE_size[type];
	if (typesz < 0)
	{
		//error cannot write an STC buffer of this type.
	}
	ubjw_begin_array(ctx, type, count);
	if (type == UBJ_STRING || type == UBJ_HIGH_PRECISION)
	{
		const char** databufs = (const char**)data;
		size_t i;
		for (i = 0; i < count; i++)
		{
			priv_ubjw_write_raw_string(ctx, databufs[i]);
		}
	}
#ifndef UBJW_DISASSEMBLY_MODE
	else if (typesz == 1 || _is_bigendian())
	{
		size_t n = typesz*count;
		priv_ubjw_context_write(ctx, data, typesz*count);
	}
	else if (typesz > 1) //and not big-endian
	{
		priv_ubjw_write_byteswap(ctx, data,typesz,count);
	}
#else
	else
	{
		size_t i;
		for (i = 0; i < count; i++)
		{
			ubjr_dynamic_t dyn = priv_ubjr_pointer_to_dynamic(type, data + i*typesz);
			ubjrw_write_dynamic(ctx, dyn, 0);
		}
	}
#endif
	ubjw_end(ctx);
}