1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009 The FreeBSD Foundation
5 *
6 * This software was developed by Ed Schouten under sponsorship from the
7 * FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/malloc.h>
34#include <sys/refcount.h>
35#include <sys/systm.h>
36
37#include <dev/vt/vt.h>
38
39static MALLOC_DEFINE(M_VTFONT, "vtfont", "vt font");
40
41/* Some limits to prevent abnormal fonts from being loaded. */
42#define	VTFONT_MAXMAPPINGS	65536
43#define	VTFONT_MAXGLYPHS	131072
44#define	VTFONT_MAXGLYPHSIZE	2097152
45#define	VTFONT_MAXDIMENSION	128
46
47static uint16_t
48vtfont_bisearch(const vfnt_map_t *map, unsigned int len, uint32_t src)
49{
50	int min, mid, max;
51
52	min = 0;
53	max = len - 1;
54
55	/* Empty font map. */
56	if (len == 0)
57		return (0);
58	/* Character below minimal entry. */
59	if (src < map[0].vfm_src)
60		return (0);
61	/* Optimization: ASCII characters occur very often. */
62	if (src <= map[0].vfm_src + map[0].vfm_len)
63		return (src - map[0].vfm_src + map[0].vfm_dst);
64	/* Character above maximum entry. */
65	if (src > map[max].vfm_src + map[max].vfm_len)
66		return (0);
67
68	/* Binary search. */
69	while (max >= min) {
70		mid = (min + max) / 2;
71		if (src < map[mid].vfm_src)
72			max = mid - 1;
73		else if (src > map[mid].vfm_src + map[mid].vfm_len)
74			min = mid + 1;
75		else
76			return (src - map[mid].vfm_src + map[mid].vfm_dst);
77	}
78
79	return (0);
80}
81
82const uint8_t *
83vtfont_lookup(const struct vt_font *vf, term_char_t c)
84{
85	uint32_t src;
86	uint16_t dst;
87	size_t stride;
88	unsigned int normal_map;
89	unsigned int bold_map;
90
91	src = TCHAR_CHARACTER(c);
92	if (TCHAR_FORMAT(c) & TF_CJK_RIGHT) {
93		normal_map = VFNT_MAP_NORMAL_RIGHT;
94		bold_map = VFNT_MAP_BOLD_RIGHT;
95	} else {
96		normal_map = VFNT_MAP_NORMAL;
97		bold_map = VFNT_MAP_BOLD;
98	}
99
100	if (TCHAR_FORMAT(c) & TF_BOLD) {
101		dst = vtfont_bisearch(vf->vf_map[bold_map],
102		    vf->vf_map_count[bold_map], src);
103		if (dst != 0)
104			goto found;
105	}
106	dst = vtfont_bisearch(vf->vf_map[normal_map],
107	    vf->vf_map_count[normal_map], src);
108
109found:
110	stride = howmany(vf->vf_width, 8) * vf->vf_height;
111	return (&vf->vf_bytes[dst * stride]);
112}
113
114struct vt_font *
115vtfont_ref(struct vt_font *vf)
116{
117
118	refcount_acquire(&vf->vf_refcount);
119	return (vf);
120}
121
122void
123vtfont_unref(struct vt_font *vf)
124{
125	unsigned int i;
126
127	if (refcount_release(&vf->vf_refcount)) {
128		for (i = 0; i < VFNT_MAPS; i++)
129			free(vf->vf_map[i], M_VTFONT);
130		free(vf->vf_bytes, M_VTFONT);
131		free(vf, M_VTFONT);
132	}
133}
134
135static int
136vtfont_validate_map(vfnt_map_t *vfm, unsigned int length,
137    unsigned int glyph_count)
138{
139	unsigned int i, last = 0;
140
141	for (i = 0; i < length; i++) {
142		/* Not ordered. */
143		if (i > 0 && vfm[i].vfm_src <= last)
144			return (EINVAL);
145		/*
146		 * Destination extends amount of glyphs.
147		 */
148		if (vfm[i].vfm_dst >= glyph_count ||
149		    vfm[i].vfm_dst + vfm[i].vfm_len >= glyph_count)
150			return (EINVAL);
151		last = vfm[i].vfm_src + vfm[i].vfm_len;
152	}
153
154	return (0);
155}
156
157int
158vtfont_load(vfnt_t *f, struct vt_font **ret)
159{
160	size_t glyphsize, mapsize;
161	struct vt_font *vf;
162	int error;
163	unsigned int i;
164
165	/* Make sure the dimensions are valid. */
166	if (f->width < 1 || f->height < 1)
167		return (EINVAL);
168	if (f->width > VTFONT_MAXDIMENSION || f->height > VTFONT_MAXDIMENSION ||
169	    f->glyph_count > VTFONT_MAXGLYPHS)
170		return (E2BIG);
171
172	/* Not too many mappings. */
173	for (i = 0; i < VFNT_MAPS; i++)
174		if (f->map_count[i] > VTFONT_MAXMAPPINGS)
175			return (E2BIG);
176
177	/* Character 0 must always be present. */
178	if (f->glyph_count < 1)
179		return (EINVAL);
180
181	glyphsize = howmany(f->width, 8) * f->height * f->glyph_count;
182	if (glyphsize > VTFONT_MAXGLYPHSIZE)
183		return (E2BIG);
184
185	/* Allocate new font structure. */
186	vf = malloc(sizeof *vf, M_VTFONT, M_WAITOK | M_ZERO);
187	vf->vf_bytes = malloc(glyphsize, M_VTFONT, M_WAITOK);
188	vf->vf_height = f->height;
189	vf->vf_width = f->width;
190	vf->vf_refcount = 1;
191
192	/* Allocate, copy in, and validate mappings. */
193	for (i = 0; i < VFNT_MAPS; i++) {
194		vf->vf_map_count[i] = f->map_count[i];
195		if (f->map_count[i] == 0)
196			continue;
197		mapsize = f->map_count[i] * sizeof(vfnt_map_t);
198		vf->vf_map[i] = malloc(mapsize, M_VTFONT, M_WAITOK);
199		error = copyin(f->map[i], vf->vf_map[i], mapsize);
200		if (error)
201			goto bad;
202		error = vtfont_validate_map(vf->vf_map[i], vf->vf_map_count[i],
203		    f->glyph_count);
204		if (error)
205			goto bad;
206	}
207
208	/* Copy in glyph data. */
209	error = copyin(f->glyphs, vf->vf_bytes, glyphsize);
210	if (error)
211		goto bad;
212
213	/* Success. */
214	*ret = vf;
215	return (0);
216
217bad:	vtfont_unref(vf);
218	return (error);
219}
220