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


int
libparsesfnt_parse_avar(
			const char *data, size_t size,
			struct libparsesfnt_avar *infop,
			const struct libparsesfnt_tabdir_entry *tag)
{
	return PARSE(LIBPARSESFNT_AVAR__, tag->offset, 0, 0, 0, 1, tag);
}


static uint16_t
avar_parse16(const char *data)
{
	uint16_t b1 = (uint16_t)((const uint8_t *)data)[0];
	uint16_t b2 = (uint16_t)((const uint8_t *)data)[1];
	b1 = (uint16_t)(b1 << 8);
	b2 = (uint16_t)(b2 << 0);
	return (uint16_t)(b1 | b2);
}

int
libparsesfnt_parse_avar_entries(
			const char *data, size_t size,
			struct libparsesfnt_avar_entry *infop,
			const struct libparsesfnt_tabdir_entry *tag,
			size_t *offsetp, size_t count)
{
	size_t offset = offsetp ? *offsetp : 0;
	if (offset < 8) {
		if (tag->length < 8) {
			errno = EBFONT;
			return -1;
		}
		offset = 8;
	}
	if ((size_t)tag->length < offset || size < (size_t)tag->length - offset) {
		errno = EBFONT;
		return -1;
	}
	while (count--) {
		if (tag->length - offset < 2) {
			errno = EBFONT;
			return -1;
		}
		infop->pair_count = avar_parse16(&data[offset]);
		offset += 2;
		infop->libparsesfnt_subentry_location = offset;
		memset(infop->__padding1, 0, sizeof(infop->__padding1));
		if ((size_t)infop->pair_count > ((size_t)tag->length - offset) / 4) {
			errno = EBFONT;
			return -1;
		}
		infop++;
	}
	if (offsetp)
		*offsetp = offset;
	return 0;
}


int
libparsesfnt_parse_avar_subentries(
			const char *data, size_t size,
			struct libparsesfnt_avar_subentry *infop,
			const struct libparsesfnt_avar_entry *entry,
			size_t first, size_t count)
{
	return PARSE(LIBPARSESFNT_AVAR_SUBENTRY__, entry->libparsesfnt_subentry_location, 0, 0, first, count, NULL);
}