summaryrefslogtreecommitdiffstats
path: root/post.c
blob: 18cafab2ff3bfbda90c607732dd1b4736be33d80 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* See LICENSE file for copyright and license details. */
#include "common.h"


static uint16_t
post_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);
}

static void
post_sign16(union {uint16_t u; int16_t s;} *info)
{
	info->s = ((info->u >> 15) ? -(int16_t)(~info->u + 1) : (int16_t)info->u);
}


int
libparsesfnt_parse_post(
			const char *data, size_t size,
			struct libparsesfnt_post *infop,
			const struct libparsesfnt_tabdir_entry *tag)
{
	int ret;
	ret = PARSE(LIBPARSESFNT_POST__, tag->offset, 0, 0, 0, 1, tag);
	if (!ret) {
		if (infop->format >> 16 == 2) {
			if (2 > tag->length - tag->offset)
				goto ebfont;
			infop->subtable.v2.number_of_glyphs = post_parse16(&data[tag->offset + 2]);
		}
	}
	return ret;

ebfont:
	errno = EBFONT;
	return -1;
}


static int
post_indices(
			const char *data, size_t size,
			uint16_t *indexp,
			const struct libparsesfnt_tabdir_entry *tag,
			size_t first, size_t count,
			size_t offset)
{
	if (offset > tag->length || first > (tag->length - offset) / 2)
		goto ebfont;
	offset += first * 2;
	if (count > (tag->length - offset) / 2)
		goto ebfont;
	if (tag->offset > size || offset > size - tag->offset)
		goto ebfont;
	offset += tag->offset;

	for (; count--; indexp++, offset += 2)
		*indexp = post_parse16(&data[offset]);

	return 0;

ebfont:
	errno = EBFONT;
	return -1;
}


int
libparsesfnt_parse_post_format_2_0_indices(
			const char *data, size_t size,
			uint16_t *indexp,
			const struct libparsesfnt_tabdir_entry *tag,
			size_t first, size_t count)
{
	return post_indices(data, size, indexp, tag, first, count, 34);
}


int
libparsesfnt_parse_post_format_2_0_name(
	const char *data, size_t size,
	char namep[256],
	const struct libparsesfnt_tabdir_entry *tag, const struct libparsesfnt_post *post,
	size_t *offsetp /* start at 0 */)
{
	size_t len;

	if (!*offsetp) {
		if ((size_t)post->subtable.v2.number_of_glyphs > ((size_t)tag->length - 34) / 2)
			goto ebfont;
		*offsetp = 34 + (size_t)post->subtable.v2.number_of_glyphs / 2;
		if ((size_t)tag->length > size - (size_t)tag->offset)
			goto ebfont;
	}

	if (*offsetp >= (size_t)tag->length)
		goto ebfont;
	len = (size_t)*(const uint8_t *)&data[*offsetp];
	*offsetp += 1;

	if (len > (size_t)tag->length - *offsetp)
		goto ebfont;
	memcpy(namep, &data[*offsetp], len);
	namep[len] = 0;
	*offsetp += len;

	return 0;

ebfont:
	errno = EBFONT;
	return -1;
}


int
libparsesfnt_parse_post_format_2_5_offsets(
			const char *data, size_t size,
			int16_t *offsetp,
			const struct libparsesfnt_tabdir_entry *tag,
			size_t first, size_t count)
{
	if (post_indices(data, size, (uint16_t *)offsetp, tag, first, count, 34))
		return -1;
	for (; count--; offsetp++)
		post_sign16((void *)offsetp);
	return 0;
}


int
libparsesfnt_parse_post_format_4_0_indices(
			const char *data, size_t size,
			uint16_t *indexp,
			const struct libparsesfnt_tabdir_entry *tag,
			size_t first, size_t count)
{
	return post_indices(data, size, indexp, tag, first, count, 32);
}