aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libfyaml/src/lib/fy-ctype.c
blob: 560fec63fa3b62ffba9dbda440f2f7b162dfd0b2 (plain) (blame)
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
/*
 * fy-ctype.c - ctype utilities
 *
 * Copyright (c) 2019 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
 *
 * SPDX-License-Identifier: MIT
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <libfyaml.h>

#include "fy-ctype.h"

const char *fy_uri_esc(const char *s, size_t len, uint8_t *code, int *code_len)
{
	const char *e = s + len;
	int j, k, width;
	uint8_t octet;
	char c;

	width = 0;
	k = 0;
	do {
		/* check for enough space for %XX */
		if ((e - s) < 3)
			return NULL;

		/* if more than one run, expect '%' */
		if (s[0] != '%')
			return NULL;

		octet = 0;
		for (j = 0; j < 2; j++) {
			c = s[1 + j];
			octet <<= 4;
			if (c >= '0' && c <= '9')
				octet |= c - '0';
			else if (c >= 'a' && c <= 'f')
				octet |= 10 + c - 'a';
			else
				octet |= 10 + c - 'A';
		}
		if (!width) {
			width = fy_utf8_width_by_first_octet(octet);
			if (!width)
				return NULL;
			k = 0;
		}
		if (k >= *code_len)
			return NULL;

		code[k++] = octet;

		/* skip over the 3 character escape */
		s += 3;

	} while (--width > 0);

	*code_len = k;

	return s;
}