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


int
libparsepcf_get_tables(const void *file, size_t size,
                       struct libparsepcf_table *tables,
                       size_t first, size_t count)
{
	const char *text = file;
	size_t pos = 8 + first * 16;
	size_t i;

	for (i = 0; i < count; i++, pos += 16) {
		tables[i].type   = libparsepcf_parse_lsb_uint32__(&text[pos +  0]);
		tables[i].format = libparsepcf_parse_lsb_uint32__(&text[pos +  4]);
		tables[i].size   = libparsepcf_parse_lsb_uint32__(&text[pos +  8]);
		tables[i].offset = libparsepcf_parse_lsb_uint32__(&text[pos + 12]);

		if ((size_t)tables[i].offset > size)
			goto ebfont;

		/* For some reason files specify table sizes such that they
		 * actually exceed the boundary of the file, despite not using
		 * that much data. Don't including this fix, but instead check
		 * that the table size is not too large, will break your
		 * favourite fonts. */
		if ((size_t)tables[i].size > size - (size_t)tables[i].offset) {
#if SIZE_MAX > UINT32_MAX
			if (size - (size_t)tables[i].offset > (size_t)UINT32_MAX)
				goto ebfont;
#endif
			tables[i].size = (uint32_t)(size - (size_t)tables[i].offset);
		}
	}

	return 0;

ebfont:
	errno = EBFONT;
	return -1;
}