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