aboutsummaryrefslogtreecommitdiffstats
path: root/libcmap_get_codepoint_data.c
blob: 553636de6d741648ea27a5b3b20d4b0e7c09c8e4 (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
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


static int
construct_codepoint_label(const char *metalabel, uint32_t codepoint, void *buffer, size_t *bufsizep)
{
	/* See Unicode Core Spec 4.8.2 Code Point Labels */

	size_t req = sizeof("-NNNN") + strlen(metalabel) - 2u * (size_t)(*metalabel == '<');
	char *p = buffer;

	if (codepoint >> 16)  req += 1u;
	if (codepoint >> 20)  req += 1u;

	if (*bufsizep < req) {
		*bufsizep = req;
		errno = ERANGE;
		return -1;
	}

	p = stpcpy(buffer, &metalabel[*metalabel == '<']);
	p -= *metalabel == '<';
	*p++ = '-';
	if (codepoint >> 20)  *p++ = "0123456789ABCDEF"[(codepoint >> 20) & 15u];
	if (codepoint >> 16)  *p++ = "0123456789ABCDEF"[(codepoint >> 16) & 15u];
	*p++ = "0123456789ABCDEF"[(codepoint >> 12) & 15u];
	*p++ = "0123456789ABCDEF"[(codepoint >> 8) & 15u];
	*p++ = "0123456789ABCDEF"[(codepoint >> 4) & 15u];
	*p++ = "0123456789ABCDEF"[(codepoint >> 0) & 15u];
	*p = '\0';

	return 0;
}


int
libcmap_get_codepoint_data(uint32_t codepoint, struct libcmap_codepoint_data *data_out, void *buffer, size_t *bufsizep)
{
	const struct libcmap_codepoint_data *data;
	size_t i, namelen;
	const char *metalabel;

	memset(data_out, 0, sizeof(*data_out));
	data_out->name = NULL;
	data_out->unicode_1_name = NULL;
	data_out->numeric_value = NULL;
	data_out->decomposition_mapping = NULL;

	if (codepoint > LIBCMAP_ULTIMATE_CODEPOINT)
		return 0;

	if ((0xFDD0u <= codepoint && codepoint <= 0xFDEFu) || (codepoint & 0xFFFEu) == 0xFFFEu) {
		if (data_out) {
			if (construct_codepoint_label("noncharacter", codepoint, buffer, bufsizep))
				return -1;
			data_out->name = buffer;
			data_out->general_category = LIBCMAP_GENERAL_CATEGORY_OTHER_NOT_ASSIGNED;
			data_out->noncharacter = 1u;
		}
		return 1;
	}

	for (i = 0u; i < libcmap_no_data_range_list_size; i++) {
		if (codepoint <= libcmap_no_data_range_list[i].last) {
			if (libcmap_no_data_range_list[i].first > codepoint)
				break;
			if (!data_out)
				return 1;
			data = libcmap_find_codepoint_data(libcmap_no_data_range_list[i].first);
			switch ((int)data->general_category) {
			case LIBCMAP_GENERAL_CATEGORY_OTHER_CONTROL:      metalabel = "control";      goto have_metalabel;
			case LIBCMAP_GENERAL_CATEGORY_OTHER_SURROGATE:    metalabel = "surrogate";    goto have_metalabel;
			case LIBCMAP_GENERAL_CATEGORY_OTHER_PRIVATE_USE:  metalabel = "private-use";  goto have_metalabel;
			default:
				namelen = strlen(data->name);
				namelen -= sizeof(", First") - 1u;
				if (namelen + 1u > *bufsizep) {
					*bufsizep = namelen + 1u;
					errno = ERANGE;
					return -1;
				}
				memcpy(buffer, data->name, namelen - 1u);
				((char *)buffer)[namelen - 1u] = '>';
				((char *)buffer)[namelen] = '\0';
				break;

			have_metalabel:
				if (construct_codepoint_label(metalabel, codepoint, buffer, bufsizep))
					return -1;
				break;
			}
			*data_out = *data;
			data_out->name = buffer;
			return 1;
		}
	}

	data = libcmap_find_codepoint_data(codepoint);
	if (!data) {
		if (data_out) {
			if (construct_codepoint_label("reserved", codepoint, buffer, bufsizep))
				return -1;
			data_out->name = buffer;
			data_out->general_category = LIBCMAP_GENERAL_CATEGORY_OTHER_NOT_ASSIGNED;
			data_out->noncharacter = 0u;
		}
		return 1;
	}

	if (data_out) {
		if (data->name[0u] == '<') {
			if (construct_codepoint_label(data->name, codepoint, buffer, bufsizep))
				return -1;
			*data_out = *data;
			data_out->name = buffer;
		} else {
			*data_out = *data;
		}
	}

	return 1;
}