xref: /freebsd/sys/dev/vt/vt_font.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/refcount.h>
36 #include <sys/systm.h>
37 
38 #include <dev/vt/vt.h>
39 
40 static MALLOC_DEFINE(M_VTFONT, "vtfont", "vt font");
41 
42 /* Some limits to prevent abnormal fonts from being loaded. */
43 #define	VTFONT_MAXMAPPINGS	65536
44 #define	VTFONT_MAXGLYPHS	131072
45 #define	VTFONT_MAXGLYPHSIZE	2097152
46 #define	VTFONT_MAXDIMENSION	128
47 
48 static uint16_t
49 vtfont_bisearch(const vfnt_map_t *map, unsigned int len, uint32_t src)
50 {
51 	int min, mid, max;
52 
53 	min = 0;
54 	max = len - 1;
55 
56 	/* Empty font map. */
57 	if (len == 0)
58 		return (0);
59 	/* Character below minimal entry. */
60 	if (src < map[0].vfm_src)
61 		return (0);
62 	/* Optimization: ASCII characters occur very often. */
63 	if (src <= map[0].vfm_src + map[0].vfm_len)
64 		return (src - map[0].vfm_src + map[0].vfm_dst);
65 	/* Character above maximum entry. */
66 	if (src > map[max].vfm_src + map[max].vfm_len)
67 		return (0);
68 
69 	/* Binary search. */
70 	while (max >= min) {
71 		mid = (min + max) / 2;
72 		if (src < map[mid].vfm_src)
73 			max = mid - 1;
74 		else if (src > map[mid].vfm_src + map[mid].vfm_len)
75 			min = mid + 1;
76 		else
77 			return (src - map[mid].vfm_src + map[mid].vfm_dst);
78 	}
79 
80 	return (0);
81 }
82 
83 const uint8_t *
84 vtfont_lookup(const struct vt_font *vf, term_char_t c)
85 {
86 	uint32_t src;
87 	uint16_t dst;
88 	size_t stride;
89 	unsigned int normal_map;
90 	unsigned int bold_map;
91 
92 	src = TCHAR_CHARACTER(c);
93 	if (TCHAR_FORMAT(c) & TF_CJK_RIGHT) {
94 		normal_map = VFNT_MAP_NORMAL_RIGHT;
95 		bold_map = VFNT_MAP_BOLD_RIGHT;
96 	} else {
97 		normal_map = VFNT_MAP_NORMAL;
98 		bold_map = VFNT_MAP_BOLD;
99 	}
100 
101 	if (TCHAR_FORMAT(c) & TF_BOLD) {
102 		dst = vtfont_bisearch(vf->vf_map[bold_map],
103 		    vf->vf_map_count[bold_map], src);
104 		if (dst != 0)
105 			goto found;
106 	}
107 	dst = vtfont_bisearch(vf->vf_map[normal_map],
108 	    vf->vf_map_count[normal_map], src);
109 
110 found:
111 	stride = howmany(vf->vf_width, 8) * vf->vf_height;
112 	return (&vf->vf_bytes[dst * stride]);
113 }
114 
115 struct vt_font *
116 vtfont_ref(struct vt_font *vf)
117 {
118 
119 	refcount_acquire(&vf->vf_refcount);
120 	return (vf);
121 }
122 
123 void
124 vtfont_unref(struct vt_font *vf)
125 {
126 	unsigned int i;
127 
128 	if (refcount_release(&vf->vf_refcount)) {
129 		for (i = 0; i < VFNT_MAPS; i++)
130 			free(vf->vf_map[i], M_VTFONT);
131 		free(vf->vf_bytes, M_VTFONT);
132 		free(vf, M_VTFONT);
133 	}
134 }
135 
136 static int
137 vtfont_validate_map(vfnt_map_t *vfm, unsigned int length,
138     unsigned int glyph_count)
139 {
140 	unsigned int i, last = 0;
141 
142 	for (i = 0; i < length; i++) {
143 		/* Not ordered. */
144 		if (i > 0 && vfm[i].vfm_src <= last)
145 			return (EINVAL);
146 		/*
147 		 * Destination extends amount of glyphs.
148 		 */
149 		if (vfm[i].vfm_dst >= glyph_count ||
150 		    vfm[i].vfm_dst + vfm[i].vfm_len >= glyph_count)
151 			return (EINVAL);
152 		last = vfm[i].vfm_src + vfm[i].vfm_len;
153 	}
154 
155 	return (0);
156 }
157 
158 int
159 vtfont_load(vfnt_t *f, struct vt_font **ret)
160 {
161 	size_t glyphsize, mapsize;
162 	struct vt_font *vf;
163 	int error;
164 	unsigned int i;
165 
166 	/* Make sure the dimensions are valid. */
167 	if (f->width < 1 || f->height < 1)
168 		return (EINVAL);
169 	if (f->width > VTFONT_MAXDIMENSION || f->height > VTFONT_MAXDIMENSION ||
170 	    f->glyph_count > VTFONT_MAXGLYPHS)
171 		return (E2BIG);
172 
173 	/* Not too many mappings. */
174 	for (i = 0; i < VFNT_MAPS; i++)
175 		if (f->map_count[i] > VTFONT_MAXMAPPINGS)
176 			return (E2BIG);
177 
178 	/* Character 0 must always be present. */
179 	if (f->glyph_count < 1)
180 		return (EINVAL);
181 
182 	glyphsize = howmany(f->width, 8) * f->height * f->glyph_count;
183 	if (glyphsize > VTFONT_MAXGLYPHSIZE)
184 		return (E2BIG);
185 
186 	/* Allocate new font structure. */
187 	vf = malloc(sizeof *vf, M_VTFONT, M_WAITOK | M_ZERO);
188 	vf->vf_bytes = malloc(glyphsize, M_VTFONT, M_WAITOK);
189 	vf->vf_height = f->height;
190 	vf->vf_width = f->width;
191 	vf->vf_refcount = 1;
192 
193 	/* Allocate, copy in, and validate mappings. */
194 	for (i = 0; i < VFNT_MAPS; i++) {
195 		vf->vf_map_count[i] = f->map_count[i];
196 		if (f->map_count[i] == 0)
197 			continue;
198 		mapsize = f->map_count[i] * sizeof(vfnt_map_t);
199 		vf->vf_map[i] = malloc(mapsize, M_VTFONT, M_WAITOK);
200 		error = copyin(f->map[i], vf->vf_map[i], mapsize);
201 		if (error)
202 			goto bad;
203 		error = vtfont_validate_map(vf->vf_map[i], vf->vf_map_count[i],
204 		    f->glyph_count);
205 		if (error)
206 			goto bad;
207 	}
208 
209 	/* Copy in glyph data. */
210 	error = copyin(f->glyphs, vf->vf_bytes, glyphsize);
211 	if (error)
212 		goto bad;
213 
214 	/* Success. */
215 	*ret = vf;
216 	return (0);
217 
218 bad:	vtfont_unref(vf);
219 	return (error);
220 }
221