comparison mupdf-source/thirdparty/mujs/utf.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 * The authors of this software are Rob Pike and Ken Thompson.
3 * Copyright (c) 2002 by Lucent Technologies.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose without fee is hereby granted, provided that this entire notice
6 * is included in all copies of any software which is or includes a copy
7 * or modification of this software and in all copies of the supporting
8 * documentation for such software.
9 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE
11 * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13 */
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "utf.h"
18 #include "utfdata.h"
19
20 #define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
21
22 typedef unsigned char uchar;
23
24 enum
25 {
26 Bit1 = 7,
27 Bitx = 6,
28 Bit2 = 5,
29 Bit3 = 4,
30 Bit4 = 3,
31 Bit5 = 2,
32
33 T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
34 Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
35 T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
36 T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
37 T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
38 T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */
39
40 Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0000 0000 0111 1111 */
41 Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0000 0000 0111 1111 1111 */
42 Rune3 = (1<<(Bit3+2*Bitx))-1, /* 0000 0000 1111 1111 1111 1111 */
43 Rune4 = (1<<(Bit4+3*Bitx))-1, /* 0001 1111 1111 1111 1111 1111 */
44
45 Maskx = (1<<Bitx)-1, /* 0011 1111 */
46 Testx = Maskx ^ 0xFF, /* 1100 0000 */
47
48 Bad = Runeerror
49 };
50
51 int
52 chartorune(Rune *rune, const char *str)
53 {
54 int c, c1, c2, c3;
55 int l;
56
57 /* overlong null character */
58 if((uchar)str[0] == 0xc0 && (uchar)str[1] == 0x80) {
59 *rune = 0;
60 return 2;
61 }
62
63 /*
64 * one character sequence
65 * 00000-0007F => T1
66 */
67 c = *(uchar*)str;
68 if(c < Tx) {
69 *rune = c;
70 return 1;
71 }
72
73 /*
74 * two character sequence
75 * 0080-07FF => T2 Tx
76 */
77 c1 = *(uchar*)(str+1) ^ Tx;
78 if(c1 & Testx)
79 goto bad;
80 if(c < T3) {
81 if(c < T2)
82 goto bad;
83 l = ((c << Bitx) | c1) & Rune2;
84 if(l <= Rune1)
85 goto bad;
86 *rune = l;
87 return 2;
88 }
89
90 /*
91 * three character sequence
92 * 0800-FFFF => T3 Tx Tx
93 */
94 c2 = *(uchar*)(str+2) ^ Tx;
95 if(c2 & Testx)
96 goto bad;
97 if(c < T4) {
98 l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
99 if(l <= Rune2)
100 goto bad;
101 *rune = l;
102 return 3;
103 }
104
105 /*
106 * four character sequence
107 * 10000-10FFFF => T4 Tx Tx Tx
108 */
109 if(UTFmax >= 4) {
110 c3 = *(uchar*)(str+3) ^ Tx;
111 if(c3 & Testx)
112 goto bad;
113 if(c < T5) {
114 l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
115 if(l <= Rune3)
116 goto bad;
117 if(l > Runemax)
118 goto bad;
119 *rune = l;
120 return 4;
121 }
122 }
123
124 /*
125 * bad decoding
126 */
127 bad:
128 *rune = Bad;
129 return 1;
130 }
131
132 int
133 runetochar(char *str, const Rune *rune)
134 {
135 int c = *rune;
136
137 /* overlong null character */
138 if (c == 0) {
139 str[0] = (char)0xc0;
140 str[1] = (char)0x80;
141 return 2;
142 }
143
144 /*
145 * one character sequence
146 * 00000-0007F => 00-7F
147 */
148 if(c <= Rune1) {
149 str[0] = c;
150 return 1;
151 }
152
153 /*
154 * two character sequence
155 * 00080-007FF => T2 Tx
156 */
157 if(c <= Rune2) {
158 str[0] = T2 | (c >> 1*Bitx);
159 str[1] = Tx | (c & Maskx);
160 return 2;
161 }
162
163 /*
164 * three character sequence
165 * 00800-0FFFF => T3 Tx Tx
166 */
167 if(c > Runemax)
168 c = Runeerror;
169 if(c <= Rune3) {
170 str[0] = T3 | (c >> 2*Bitx);
171 str[1] = Tx | ((c >> 1*Bitx) & Maskx);
172 str[2] = Tx | (c & Maskx);
173 return 3;
174 }
175
176 /*
177 * four character sequence
178 * 010000-1FFFFF => T4 Tx Tx Tx
179 */
180 str[0] = T4 | (c >> 3*Bitx);
181 str[1] = Tx | ((c >> 2*Bitx) & Maskx);
182 str[2] = Tx | ((c >> 1*Bitx) & Maskx);
183 str[3] = Tx | (c & Maskx);
184 return 4;
185 }
186
187 int
188 runelen(int c)
189 {
190 Rune rune;
191 char str[10];
192
193 rune = c;
194 return runetochar(str, &rune);
195 }
196
197 static const Rune *
198 ucd_bsearch(Rune c, const Rune *t, int n, int ne)
199 {
200 const Rune *p;
201 int m;
202
203 while(n > 1) {
204 m = n/2;
205 p = t + m*ne;
206 if(c >= p[0]) {
207 t = p;
208 n = n-m;
209 } else
210 n = m;
211 }
212 if(n && c >= t[0])
213 return t;
214 return 0;
215 }
216
217 Rune
218 tolowerrune(Rune c)
219 {
220 const Rune *p;
221
222 p = ucd_bsearch(c, ucd_tolower2, nelem(ucd_tolower2)/3, 3);
223 if(p && c >= p[0] && c <= p[1])
224 return c + p[2];
225 p = ucd_bsearch(c, ucd_tolower1, nelem(ucd_tolower1)/2, 2);
226 if(p && c == p[0])
227 return c + p[1];
228 return c;
229 }
230
231 Rune
232 toupperrune(Rune c)
233 {
234 const Rune *p;
235
236 p = ucd_bsearch(c, ucd_toupper2, nelem(ucd_toupper2)/3, 3);
237 if(p && c >= p[0] && c <= p[1])
238 return c + p[2];
239 p = ucd_bsearch(c, ucd_toupper1, nelem(ucd_toupper1)/2, 2);
240 if(p && c == p[0])
241 return c + p[1];
242 return c;
243 }
244
245 int
246 islowerrune(Rune c)
247 {
248 const Rune *p;
249
250 p = ucd_bsearch(c, ucd_toupper2, nelem(ucd_toupper2)/3, 3);
251 if(p && c >= p[0] && c <= p[1])
252 return 1;
253 p = ucd_bsearch(c, ucd_toupper1, nelem(ucd_toupper1)/2, 2);
254 if(p && c == p[0])
255 return 1;
256 return 0;
257 }
258
259 int
260 isupperrune(Rune c)
261 {
262 const Rune *p;
263
264 p = ucd_bsearch(c, ucd_tolower2, nelem(ucd_tolower2)/3, 3);
265 if(p && c >= p[0] && c <= p[1])
266 return 1;
267 p = ucd_bsearch(c, ucd_tolower1, nelem(ucd_tolower1)/2, 2);
268 if(p && c == p[0])
269 return 1;
270 return 0;
271 }
272
273 int
274 isalpharune(Rune c)
275 {
276 const Rune *p;
277
278 p = ucd_bsearch(c, ucd_alpha2, nelem(ucd_alpha2)/2, 2);
279 if(p && c >= p[0] && c <= p[1])
280 return 1;
281 p = ucd_bsearch(c, ucd_alpha1, nelem(ucd_alpha1), 1);
282 if(p && c == p[0])
283 return 1;
284 return 0;
285 }