fyaml-sys 0.1.0+fy0.9.3

Rust FFI bindings for libfyaml (maintained fork of libfyaml-sys)
Documentation
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
 * fy-utf8.h - UTF-8 methods
 *
 * Copyright (c) 2019 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
 *
 * SPDX-License-Identifier: MIT
 */
#ifndef FY_UTF8_H
#define FY_UTF8_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdint.h>
#include <stdlib.h>
#include <assert.h>

#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif


#include "fy-utils.h"

#define FY_UTF8_MIN_WIDTH 1
#define FY_UTF8_MAX_WIDTH 4

extern const int8_t fy_utf8_width_table[32];

static inline int
fy_utf8_width_by_first_octet_no_table(const uint8_t c)
{
	return (c & 0x80) == 0x00 ? 1 :
	       (c & 0xe0) == 0xc0 ? 2 :
	       (c & 0xf0) == 0xe0 ? 3 :
	       (c & 0xf8) == 0xf0 ? 4 : 0;
}

static inline FY_ALWAYS_INLINE int
fy_utf8_width_by_first_octet(uint8_t c)
{
	return fy_utf8_width_table[c >> 3];
}

/* assumes valid utf8 character */
static inline size_t
fy_utf8_width(const int c)
{
	return 1 + (c >= 0x80) + (c >= 0x800) + (c >= 0x10000);
}

static inline bool
fy_utf8_is_valid(const int c)
{
	return c >= 0 && !((c >= 0xd800 && c <= 0xdfff) || c >= 0x110000);
}

/* generic utf8 decoder (not inlined) */
int fy_utf8_get_generic(const void *ptr, size_t left, int *widthp);

/* -1 for end of input, -2 for invalid character, -3 for partial */
#define FYUG_EOF	-1
#define FYUG_INV	-2
#define FYUG_PARTIAL	-3

#define FY_UTF8_64_C(_x)	((int)(int32_t)(_x))
#define FY_UTF8_64_W(_x)	(((int)((_x) >> 32)))

#define FY_UTF8_64_MAKE(_w, _c)	((int64_t)(((int64_t)((_w)) << 32) | (int64_t)(_c)))

static inline FY_ALWAYS_INLINE int64_t
fy_utf8_get_branch_64(const void *ptr, size_t left)
{
	const uint8_t *s = ptr;
	unsigned int a, b, c, d;
	uint32_t code;

	if (!left)
		return FYUG_EOF;

	a = s[0];
	if (a < 0x80) 				// 1 byte: 0xxxxxxx
		return FY_UTF8_64_MAKE(1, a);

	if (left < 2)
		return FYUG_PARTIAL;

	b = s[1];
	if ((a & 0xe0) == 0xc0) {		// 2 bytes: 110xxxxx 10xxxxxx
		if ((b & 0xc0) != 0x80)
			return FYUG_INV;

		code = (int)(((a & 0x1f) << 6) | (b & 0x3f));
		if (code < 0x80)
			return FYUG_INV;	// overlong

		return FY_UTF8_64_MAKE(2, code);
	}

	if (left < 3)
		return FYUG_PARTIAL;

	c = s[2];
	if ((a & 0xf0) == 0xe0) {		// 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx

		if (((b | c) & 0xc0) != 0x80)
			return FYUG_INV;

		code = (int)(((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f));
		if (code < 0x800 || (code >= 0xd800 && code <= 0xdfff))
			return FYUG_INV;	// overlong or surrogate

		return FY_UTF8_64_MAKE(3, code);
	}

	if (left < 4)
		return FYUG_PARTIAL;

	d = s[3];
	if ((a & 0xf8) == 0xf0) {		// 4 bytes: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

		if (((b | c | d) & 0xc0) != 0x80)
			return FYUG_INV;

		code = ((a & 0x07) << 18) | ((b & 0x3f) << 12) |
		       ((c & 0x3f) <<  6)  | (d & 0x3f);

		if (code < 0x10000 || code > 0x10ffff)
			return FYUG_INV;	// overlong or out of range

		return FY_UTF8_64_MAKE(4, code);
	}

	return FYUG_INV;
}

static inline FY_ALWAYS_INLINE int
fy_utf8_get_branch(const void *ptr, size_t left, int *widthp)
{
	const uint8_t *s = ptr;
	unsigned int a, b, c, d;
	int code;

	if (!left)
		goto err_eof;

	a = s[0];
	if (a < 0x80) {				// 1 byte: 0xxxxxxx
		code = (int)a;
		*widthp = 1;
		return code;
	}

	if (left < 2)
		goto err_partial;

	b = s[1];
	if ((a & 0xe0) == 0xc0) {		// 2 bytes: 110xxxxx 10xxxxxx
		if ((b & 0xc0) != 0x80)
			goto err_inv;

		code = (int)(((a & 0x1f) << 6) | (b & 0x3f));
		if (code < 0x80)
			goto err_inv;		// overlong

		*widthp = 2;
		return code;
	}

	if (left < 3)
		goto err_partial;

	c = s[2];
	if ((a & 0xf0) == 0xe0) {		// 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx

		if (((b | c) & 0xc0) != 0x80)
			goto err_inv;

		code = (int)(((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f));
		if (code < 0x800 || (code >= 0xd800 && code <= 0xdfff))
			goto err_inv;		// overlong or surrogate

		*widthp = 3;
		return code;
	}

	if (left < 4)
		goto err_partial;

	d = s[3];
	if ((a & 0xf8) == 0xf0) {		// 4 bytes: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

		if (((b | c | d) & 0xc0) != 0x80)
			goto err_inv;

		code = ((a & 0x07) << 18) | ((b & 0x3f) << 12) |
		       ((c & 0x3f) <<  6)  | (d & 0x3f);

		if (code < 0x10000 || code > 0x10ffff)
			goto err_inv;		// overlong or out of range

		*widthp = 4;
		return code;
	}

err_inv:
	*widthp = 0;
	return FYUG_INV;

err_partial:
	*widthp = 0;
	return FYUG_PARTIAL;

err_eof:
	*widthp = 0;
	return FYUG_EOF;
}

static inline FY_ALWAYS_INLINE int
fy_utf8_get_table(const void *ptr, size_t left, int *widthp)
{
	const uint8_t *s = ptr;
	unsigned int a, b, c, d;
	int code, w;

	if (!left)
		goto err_eof;

	w = fy_utf8_width_by_first_octet(s[0]);
	if ((size_t)w > left)
		goto err_partial;

	*widthp = w;

	switch (w) {
	case 1:
		a = s[0];
		code = (int)a;
		return code;
	case 2:
		a = s[0];
		b = s[1];
		if ((b & 0xc0) != 0x80)
			goto err_inv;

		code = (int)(((a & 0x1f) << 6) | (b & 0x3f));
		if (code < 0x80)
			goto err_inv;		// overlong

		return code;
	case 3:
		a = s[0];
		b = s[1];
		c = s[2];

		if (((b | c) & 0xc0) != 0x80)
			goto err_inv;

		code = (int)(((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f));
		if (code < 0x800 || (code >= 0xd800 && code <= 0xdfff))
			goto err_inv;		// overlong or surrogate

		return code;
	case 4:
		a = s[0];
		b = s[1];
		c = s[2];
		d = s[3];

		if (((b | c | d) & 0xc0) != 0x80)
			goto err_inv;

		code = ((a & 0x07) << 18) | ((b & 0x3f) << 12) |
		       ((c & 0x3f) <<  6) | (d & 0x3f);

		if (code < 0x10000 || code > 0x10ffff)
			goto err_inv;		// overlong or out of range

		return code;

	default:
		break;
	}

err_inv:
	*widthp = 0;
	return FYUG_INV;

err_partial:
	*widthp = 0;
	return FYUG_PARTIAL;

err_eof:
	*widthp = 0;
	return FYUG_EOF;
}

static inline FY_ALWAYS_INLINE int
fy_utf8_get(const void *ptr, size_t left, int *widthp)
{
	return fy_utf8_get_branch(ptr, left, widthp);
}

static inline FY_ALWAYS_INLINE int64_t
fy_utf8_get_64(const void *ptr, size_t left)
{
	return fy_utf8_get_branch_64(ptr, left);
}

static inline FY_ALWAYS_INLINE int
fy_utf8_get_no_width(const void *ptr, size_t left)
{
	int w;

	return fy_utf8_get(ptr, left, &w);
}

static inline FY_ALWAYS_INLINE int
fy_utf8_get_end(const void *ptr, const void *ptr_end, int *widthp)
{
	return fy_utf8_get(ptr, (size_t)(ptr_end - ptr), widthp);
}

static inline FY_ALWAYS_INLINE int
fy_utf8_get_end_no_width(const void *ptr, const void *ptr_end)
{
	int w;

	return fy_utf8_get(ptr, (size_t)(ptr_end - ptr), &w);
}

int fy_utf8_get_right_generic(const void *ptr, size_t left, int *widthp);

static inline int fy_utf8_get_right(const void *ptr, size_t left, int *widthp)
{
	const uint8_t *p = ptr + left;

	/* single byte (hot path) */
	if (left > 0 && !(p[-1] & 0x80)) {
		if (widthp)
			*widthp = 1;
		return p[-1] & 0x7f;
	}
	return fy_utf8_get_right_generic(ptr, left, widthp);
}


/* for when you _know_ that there's enough room and c is valid */
static inline void *fy_utf8_put_unchecked(void *ptr, int c)
{
	uint8_t *s = ptr;

	assert(c >= 0);
	if (c < 0x80)
		*s++ = c;
	else if (c < 0x800) {
		*s++ = (c >> 6) | 0xc0;
		*s++ = (c & 0x3f) | 0x80;
	} else if (c < 0x10000) {
		*s++ = (c >> 12) | 0xe0;
		*s++ = ((c >> 6) & 0x3f) | 0x80;
		*s++ = (c & 0x3f) | 0x80;
	} else {
		*s++ = (c >> 18) | 0xf0;
		*s++ = ((c >> 12) & 0x3f) | 0x80;
		*s++ = ((c >> 6) & 0x3f) | 0x80;
		*s++ = (c & 0x3f) | 0x80;
	}
	return s;
}

static inline void *fy_utf8_put(void *ptr, size_t left, int c)
{
	if (!fy_utf8_is_valid(c) || fy_utf8_width(c) > left)
		return NULL;

	return fy_utf8_put_unchecked(ptr, c);
}

/* buffer must contain at least 5 characters */
#define FY_UTF8_FORMAT_BUFMIN	5
enum fy_utf8_escape {
	fyue_none,
	fyue_singlequote,
	fyue_doublequote,
	fyue_doublequote_json,
	fyue_doublequote_yaml_1_1,
};

static inline bool fy_utf8_escape_is_any_doublequote(const enum fy_utf8_escape esc)
{
	return esc >= fyue_doublequote && esc <= fyue_doublequote_yaml_1_1;
}

char *fy_utf8_format(int c, char *buf, const enum fy_utf8_escape esc);

#define fy_utf8_format_a(_c, _esc) \
	({ \
	 	char *_buf = alloca(FY_UTF8_FORMAT_BUFMIN); \
	 	fy_utf8_format((_c), _buf, _esc); \
	})

size_t fy_utf8_format_text_length(const char *buf, size_t len,
			          enum fy_utf8_escape esc);
char *fy_utf8_format_text(const char *buf, size_t len,
			  char *out, size_t maxsz,
			  enum fy_utf8_escape esc);

#define fy_utf8_format_text_a(_buf, _len, _esc) \
	({ \
		const char *__buf = (_buf); \
		size_t __len = (_len); \
		enum fy_utf8_escape __esc = (_esc); \
		size_t _outsz = fy_utf8_format_text_length(__buf, __len, __esc); \
		char *_out = alloca(_outsz + 1); \
		fy_utf8_format_text(__buf, __len, _out, _outsz, __esc); \
	})

char *fy_utf8_format_text_alloc(const char *buf, size_t len, const enum fy_utf8_escape esc);

const void *fy_utf8_memchr_generic(const void *s, int c, size_t n);

static inline const void *fy_utf8_memchr(const void *s, int c, size_t n)
{
	if (c < 0 || !n)
		return NULL;
	if (c < 0x80)
		return memchr(s, c, n);
	return fy_utf8_memchr_generic(s, c, n);
}

static inline const void *fy_utf8_strchr(const void *s, int c)
{
	if (c < 0)
		return NULL;
	if (c < 0x80)
		return strchr(s, c);
	return fy_utf8_memchr_generic(s, c, strlen(s));
}

static inline int fy_utf8_count(const void *ptr, size_t len)
{
	const uint8_t *s = ptr, *e = ptr + len;
	int w, count;

	count = 0;
	while (s < e) {
		w = fy_utf8_width_by_first_octet(*s);

		/* malformed? */
		if (!w || s + w > e)
			break;
		s += w;

		count++;
	}

	return count;
}

int fy_utf8_parse_escape(const char **strp, size_t len, const enum fy_utf8_escape esc);

#define F_NONE			0
#define F_SIMPLE_SCALAR		FY_BIT(0)	/* part of simple scalar 0-9a-zA-Z_ */
#define F_DIRECT_PRINT		FY_BIT(1)	/* 0x20..0x7e */
#define F_LB			FY_BIT(2)	/* is a linebreak */
#define F_WS			FY_BIT(3)	/* is a whitespace */
#define F_LETTER		FY_BIT(4)	/* is a letter a..z A..Z */
#define F_DIGIT			FY_BIT(5)	/* is a digit 0..9 */
#define F_XDIGIT		FY_BIT(6)	/* is a hex digit 0..9 a-f A-F */
#define F_FLOW_INDICATOR	FY_BIT(7)	/* is ,[]{} */

extern const uint8_t fy_utf8_low_ascii_flags[256];

void *fy_utf8_split_posix(const char *str, int *argcp, const char * const *argvp[]);

int fy_utf8_get_generic_s(const void *ptr, const void *ptr_end, int *widthp);
int fy_utf8_get_generic_s_nocheck(const void *ptr, int *widthp);

static inline int fy_utf8_get_s(const void *ptr, const void *ptr_end, int *widthp)
{
	const uint8_t *p = ptr;

	/* single byte (hot path) */
	if (ptr >= ptr_end) {
		*widthp = 0;
		return FYUG_EOF;
	}

	if (!(p[0] & 0x80)) {
		*widthp = 1;
		return p[0];
	}
	return fy_utf8_get_generic_s(ptr, ptr_end, widthp);
}

static inline int fy_utf8_get_s_nocheck(const void *ptr, int *widthp)
{
	const uint8_t *p = ptr;

	if (!(p[0] & 0x80)) {
		*widthp = 1;
		return p[0];
	}
	return fy_utf8_get_generic_s_nocheck(ptr, widthp);
}

/* for most 64 bit arches this will fit in a single register */
struct fy_utf8_result {
	int c;
	int w;
};

/* probably the most performant version */
static inline struct fy_utf8_result fy_utf8_get_s_res(const void *ptr, const void *ptr_end)
{
	const uint8_t *p = ptr;
	int c, width;

	/* single byte (hot path) */
	if (ptr >= ptr_end)
		return (struct fy_utf8_result){ FYUG_EOF, 0 };

	if (!(p[0] & 0x80))
		return (struct fy_utf8_result){ p[0], 1 };

	c = fy_utf8_get_generic_s(ptr, ptr_end, &width);
	if (c < 0)
		return (struct fy_utf8_result){ c, 0 };
	return (struct fy_utf8_result){ c, width };
}

static inline bool fy_utf8_is_space(const int c)
{
	return c == ' ';
}

static inline bool fy_utf8_is_tab(const int c)
{
	return c == '\t';
}

static inline bool fy_utf8_is_simple_scalar_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_SIMPLE_SCALAR;
}

static inline bool
fy_utf8_is_printable_ascii_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_DIRECT_PRINT;
}

static inline bool fy_utf8_is_lb_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_LB;
}

static inline bool fy_utf8_is_ws_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_WS;
}

static inline bool fy_utf8_is_ws_lb_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & (F_WS | F_LB);
}

static inline bool fy_utf8_is_letter_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_LETTER;
}

static inline bool fy_utf8_is_digit_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_DIGIT;
}

static inline bool fy_utf8_is_hex_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_XDIGIT;
}

static inline bool fy_utf8_is_flow_indicator_no_check(const int c)
{
	return fy_utf8_low_ascii_flags[(uint8_t)c] & F_FLOW_INDICATOR;
}

static inline bool fy_utf8_is_low_ascii(const int c)
{
	return (unsigned int)c < 128;
}

static inline bool fy_utf8_is_simple_scalar(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_simple_scalar_no_check(c);
}

static inline bool
fy_utf8_is_printable_ascii_x(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_printable_ascii_no_check(c);
}

static inline bool
fy_utf8_is_printable_ascii(const int c)
{
	return c >= 0x20 && c <= 0x7e;
}

static inline bool fy_utf8_is_lb(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_lb_no_check(c);
}

static inline bool fy_utf8_is_ws(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_ws_no_check(c);
}

static inline bool fy_utf8_is_ws_lb(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_ws_lb_no_check(c);
}

static inline bool fy_utf8_is_letter(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_letter_no_check(c);
}

static inline bool fy_utf8_is_digit(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_digit_no_check(c);
}

static inline bool fy_utf8_is_hex(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_hex_no_check(c);
}

static inline bool fy_utf8_is_flow_indicator(const int c)
{
	return fy_utf8_is_low_ascii(c) && fy_utf8_is_flow_indicator_no_check(c);
}

#endif