Debian Codegraph @

texk/web2c/luatexdir/utils/unistring.c

raw
1 /*
2
3 Copyright 2013 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under the terms
8 of the GNU General Public License as published by the Free Software Foundation;
9 either version 2 of the License, or (at your option) any later version.
10
11 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20 #include "ptexlib.h"
21 #include <string.h>
22
23 static void utf_error(void)
24 {
25 const char *hlp[] = {
26 "A funny symbol that I can't read has just been (re)read.",
27 "Just continue, I'll change it to 0xFFFD.",
28 NULL
29 };
30 deletions_allowed = false;
31 tex_error("String contains an invalid utf-8 sequence", hlp);
32 deletions_allowed = true;
33 }
34
35 /*
36 unsigned str2uni(const unsigned char *k)
37 {
38 register int ch;
39 int val = 0xFFFD;
40 const unsigned char *text = k;
41 if ((ch = *text++) < 0x80) {
42 val = (unsigned) ch;
43 } else if (ch <= 0xbf) {
44 //
45 } else if (ch <= 0xdf) {
46 if (*text >= 0x80 && *text < 0xc0)
47 val = (unsigned) (((ch & 0x1f) << 6) | (*text++ & 0x3f));
48 } else if (ch <= 0xef) {
49 if (*text >= 0x80 && *text < 0xc0 && text[1] >= 0x80 && text[1] < 0xc0) {
50 val = (unsigned)
51 (((ch & 0xf) << 12) | ((text[0] & 0x3f) << 6) |
52 (text[1] & 0x3f));
53 }
54 } else if (ch <= 0xf7) {
55 int w = (((ch & 0x7) << 2) | ((text[0] & 0x30) >> 4)) - 1, w2;
56 w = (w << 6) | ((text[0] & 0xf) << 2) | ((text[1] & 0x30) >> 4);
57 w2 = ((text[1] & 0xf) << 6) | (text[2] & 0x3f);
58 val = (unsigned) (w * 0x400 + w2 + 0x10000);
59 if (*text < 0x80 || text[1] < 0x80 || text[2] < 0x80 ||
60 *text >= 0xc0 || text[1] >= 0xc0 || text[2] >= 0xc0)
61 val = 0xFFFD;
62 }
63 if (val == 0xFFFD)
64 utf_error();
65 return (val);
66 }
67 */
68
69 /*
70 Per August 13 2022 we do the following. We still error on an invalid utf because we
71 have to remain compatible but accept 0xFFFD as valid now.
72 */
73
74 unsigned str2uni(const unsigned char *k)
75 {
76 int val = -1;
77 const unsigned char *text = k;
78 register int ch = *text++;
79 if (ch < 0x80) {
80 val = (unsigned) ch;
81 } else if (ch <= 0xbf) {
82 /*tex An error. */
83 } else if (ch <= 0xdf) {
84 if (*text >= 0x80 && *text < 0xc0) {
85 val = (unsigned) (((ch & 0x1f) << 6) | (*text++ & 0x3f));
86 }
87 } else if (ch <= 0xef) {
88 if (*text >= 0x80 && *text < 0xc0 && text[1] >= 0x80 && text[1] < 0xc0) {
89 val = (unsigned) (((ch & 0xf) << 12) | ((text[0] & 0x3f) << 6) | (text[1] & 0x3f));
90 }
91 } else if (ch <= 0xf7) {
92 int w = (((ch & 0x7) << 2) | ((text[0] & 0x30) >> 4)) - 1, w2;
93 w = (w << 6) | ((text[0] & 0xf) << 2) | ((text[1] & 0x30) >> 4);
94 w2 = ((text[1] & 0xf) << 6) | (text[2] & 0x3f);
95 val = (unsigned) (w * 0x400 + w2 + 0x10000);
96 if (*text < 0x80 || text[1] < 0x80 || text[2] < 0x80 || *text >= 0xc0 || text[1] >= 0xc0 || text[2] >= 0xc0) {
97 val = -1;
98 }
99 }
100 if (val < 0) {
101 utf_error();
102 return 0xFFFD;
103 } else {
104 return val;
105 }
106 }
107
108 /*tex
109
110 A real basic helper.
111 */
112
113 unsigned char *uni2str(unsigned unic)
114 {
115 unsigned char *buf = xmalloc(5);
116 unsigned char *pt = buf;
117 if (unic < 0x80)
118 *pt++ = (unsigned char) unic;
119 else if (unic < 0x800) {
120 *pt++ = (unsigned char) (0xc0 | (unic >> 6));
121 *pt++ = (unsigned char) (0x80 | (unic & 0x3f));
122 } else if (unic >= 0x110000) {
123 *pt++ = (unsigned char) (unic - 0x110000);
124 } else if (unic < 0x10000) {
125 *pt++ = (unsigned char) (0xe0 | (unic >> 12));
126 *pt++ = (unsigned char) (0x80 | ((unic >> 6) & 0x3f));
127 *pt++ = (unsigned char) (0x80 | (unic & 0x3f));
128 } else {
129 unsigned val = unic - 0x10000;
130 int u = (int) (((val & 0xf0000) >> 16) + 1);
131 int z = (int) ((val & 0x0f000) >> 12);
132 int y = (int) ((val & 0x00fc0) >> 6);
133 int x = (int) (val & 0x0003f);
134 *pt++ = (unsigned char) (0xf0 | (u >> 2));
135 *pt++ = (unsigned char) (0x80 | ((u & 3) << 4) | z);
136 *pt++ = (unsigned char) (0x80 | y);
137 *pt++ = (unsigned char) (0x80 | x);
138 }
139 *pt = '\0';
140 return buf;
141 }
142
143 /*tex
144
145 Function |buffer_to_unichar| converts a sequence of bytes in the |buffer|
146 into a unicode character value. It does not check for overflow of the
147 |buffer|, but it is careful to check the validity of the \UTF-8 encoding.
148
149 */
150
151 int buffer_to_unichar(int k)
152 {
153 return str2uni((const unsigned char *)(buffer+k));
154 }
155
156 /*tex
157
158 These came from texlang.c:
159
160 */
161
162 char *uni2string(char *utf8_text, unsigned ch)
163 {
164 /*tex Increment and deposit character: */
165 if (ch >= 17 * 65536)
166 return (utf8_text);
167 if (ch <= 127)
168 *utf8_text++ = (char) ch;
169 else if (ch <= 0x7ff) {
170 *utf8_text++ = (char) (0xc0 | (ch >> 6));
171 *utf8_text++ = (char) (0x80 | (ch & 0x3f));
172 } else if (ch <= 0xffff) {
173 *utf8_text++ = (char) (0xe0 | (ch >> 12));
174 *utf8_text++ = (char) (0x80 | ((ch >> 6) & 0x3f));
175 *utf8_text++ = (char) (0x80 | (ch & 0x3f));
176 } else {
177 unsigned val = ch - 0x10000;
178 unsigned u = ((val & 0xf0000) >> 16) + 1, z = (val & 0x0f000) >> 12, y =
179 (val & 0x00fc0) >> 6, x = val & 0x0003f;
180 *utf8_text++ = (char) (0xf0 | (u >> 2));
181 *utf8_text++ = (char) (0x80 | ((u & 3) << 4) | z);
182 *utf8_text++ = (char) (0x80 | y);
183 *utf8_text++ = (char) (0x80 | x);
184 }
185 return (utf8_text);
186 }
187
188 unsigned u_length(register unsigned int *str)
189 {
190 register unsigned len = 0;
191 while (*str++ != '\0')
192 ++len;
193 return (len);
194 }
195
196 void utf2uni_strcpy(unsigned int *ubuf, const char *utf8buf)
197 {
198 int len = (int) strlen(utf8buf) + 1;
199 unsigned int *upt = ubuf, *uend = ubuf + len - 1;
200 const unsigned char *pt = (const unsigned char *) utf8buf, *end = pt + strlen(utf8buf);
201 int w, w2;
202 while (pt < end && *pt != '\0' && upt < uend) {
203 if (*pt <= 127)
204 *upt = *pt++;
205 else if (*pt <= 0xdf) {
206 *upt = (unsigned int) (((*pt & 0x1f) << 6) | (pt[1] & 0x3f));
207 pt += 2;
208 } else if (*pt <= 0xef) {
209 *upt = (unsigned int) (((*pt & 0xf) << 12) | ((pt[1] & 0x3f) << 6) | (pt[2] & 0x3f));
210 pt += 3;
211 } else {
212 w = (((*pt & 0x7) << 2) | ((pt[1] & 0x30) >> 4)) - 1;
213 w = (w << 6) | ((pt[1] & 0xf) << 2) | ((pt[2] & 0x30) >> 4);
214 w2 = ((pt[2] & 0xf) << 6) | (pt[3] & 0x3f);
215 *upt = (unsigned int) (w * 0x400 + w2 + 0x10000);
216 pt += 4;
217 }
218 ++upt;
219 }
220 *upt = '\0';
221 }
222
223 char *utf16be_str(long code)
224 {
225 static char buf[SMALL_BUF_SIZE];
226 long v;
227 unsigned vh, vl;
228 if (code <= 0xFFFF)
229 sprintf(buf, "%04lX", code);
230 else {
231 v = code - 0x10000;
232 vh = (unsigned) (v / 0x400 + 0xD800);
233 vl = (unsigned) (v % 0x400 + 0xDC00);
234 sprintf(buf, "%04X%04X", vh, vl);
235 }
236 return buf;
237 }