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