Blame


1 a31db67d 2004-04-21 devnull #ifdef PLAN9
2 a31db67d 2004-04-21 devnull #include <u.h>
3 a31db67d 2004-04-21 devnull #include <libc.h>
4 a31db67d 2004-04-21 devnull #include <bio.h>
5 fa059a4e 2005-12-17 devnull #ifdef PLAN9PORT
6 fa059a4e 2005-12-17 devnull #include <errno.h>
7 a31db67d 2004-04-21 devnull #else
8 fa059a4e 2005-12-17 devnull extern int errno;
9 fa059a4e 2005-12-17 devnull #endif
10 fa059a4e 2005-12-17 devnull #else
11 a31db67d 2004-04-21 devnull #include <sys/types.h>
12 a31db67d 2004-04-21 devnull #include <stdio.h>
13 a31db67d 2004-04-21 devnull #include <stdlib.h>
14 a31db67d 2004-04-21 devnull #include <string.h>
15 a31db67d 2004-04-21 devnull #include <unistd.h>
16 a31db67d 2004-04-21 devnull #include <errno.h>
17 a31db67d 2004-04-21 devnull #include "plan9.h"
18 a31db67d 2004-04-21 devnull #endif
19 a31db67d 2004-04-21 devnull #include "hdr.h"
20 35d26aa3 2005-12-26 devnull #ifndef EILSEQ
21 35d26aa3 2005-12-26 devnull #define EILSEQ 9998
22 35d26aa3 2005-12-26 devnull #endif
23 a31db67d 2004-04-21 devnull
24 a31db67d 2004-04-21 devnull /*
25 a31db67d 2004-04-21 devnull the our_* routines are implementations for the corresponding library
26 a31db67d 2004-04-21 devnull routines. for a while, i tried to actually name them wctomb etc
27 a31db67d 2004-04-21 devnull but stopped that after i found a system which made wchar_t an
28 a31db67d 2004-04-21 devnull unsigned char.
29 a31db67d 2004-04-21 devnull */
30 a31db67d 2004-04-21 devnull
31 a31db67d 2004-04-21 devnull int our_wctomb(char *s, unsigned long wc);
32 a31db67d 2004-04-21 devnull int our_mbtowc(unsigned long *p, char *s, unsigned n);
33 a31db67d 2004-04-21 devnull int runetoisoutf(char *str, Rune *rune);
34 a31db67d 2004-04-21 devnull int fullisorune(char *str, int n);
35 a31db67d 2004-04-21 devnull int isochartorune(Rune *rune, char *str);
36 a31db67d 2004-04-21 devnull
37 a31db67d 2004-04-21 devnull void
38 a31db67d 2004-04-21 devnull utf_in(int fd, long *notused, struct convert *out)
39 a31db67d 2004-04-21 devnull {
40 7551b2ec 2005-03-09 devnull char buf[N];
41 7551b2ec 2005-03-09 devnull int i, j, c, n, tot;
42 7551b2ec 2005-03-09 devnull ulong l;
43 a31db67d 2004-04-21 devnull
44 a31db67d 2004-04-21 devnull USED(notused);
45 7551b2ec 2005-03-09 devnull tot = 0;
46 7551b2ec 2005-03-09 devnull while((n = read(fd, buf+tot, N-tot)) >= 0){
47 7551b2ec 2005-03-09 devnull tot += n;
48 2b03bf69 2006-10-12 devnull for(i=j=0; i<=tot-UTFmax || (i<tot && (n==0 || fullrune(buf+i, tot-i))); ){
49 7551b2ec 2005-03-09 devnull c = our_mbtowc(&l, buf+i, tot-i);
50 7f0d675d 2005-09-13 devnull if(c == -1){
51 7551b2ec 2005-03-09 devnull if(squawk)
52 7551b2ec 2005-03-09 devnull EPR "%s: bad UTF sequence near byte %ld in input\n", argv0, ninput+i);
53 536f9b83 2006-05-21 devnull if(clean){
54 536f9b83 2006-05-21 devnull i++;
55 7551b2ec 2005-03-09 devnull continue;
56 536f9b83 2006-05-21 devnull }
57 7551b2ec 2005-03-09 devnull nerrors++;
58 7551b2ec 2005-03-09 devnull l = Runeerror;
59 7f0d675d 2005-09-13 devnull c = 1;
60 a31db67d 2004-04-21 devnull }
61 7551b2ec 2005-03-09 devnull runes[j++] = l;
62 7551b2ec 2005-03-09 devnull i += c;
63 a31db67d 2004-04-21 devnull }
64 7551b2ec 2005-03-09 devnull OUT(out, runes, j);
65 7551b2ec 2005-03-09 devnull tot -= i;
66 7551b2ec 2005-03-09 devnull ninput += i;
67 7551b2ec 2005-03-09 devnull if(tot)
68 7551b2ec 2005-03-09 devnull memmove(buf, buf+i, tot);
69 7551b2ec 2005-03-09 devnull if(n == 0)
70 7551b2ec 2005-03-09 devnull break;
71 7551b2ec 2005-03-09 devnull }
72 536f9b83 2006-05-21 devnull OUT(out, runes, 0);
73 a31db67d 2004-04-21 devnull }
74 a31db67d 2004-04-21 devnull
75 a31db67d 2004-04-21 devnull void
76 a31db67d 2004-04-21 devnull utf_out(Rune *base, int n, long *notused)
77 a31db67d 2004-04-21 devnull {
78 a31db67d 2004-04-21 devnull char *p;
79 a31db67d 2004-04-21 devnull Rune *r;
80 a31db67d 2004-04-21 devnull
81 a31db67d 2004-04-21 devnull USED(notused);
82 a31db67d 2004-04-21 devnull nrunes += n;
83 a31db67d 2004-04-21 devnull for(r = base, p = obuf; n-- > 0; r++){
84 a31db67d 2004-04-21 devnull p += our_wctomb(p, *r);
85 a31db67d 2004-04-21 devnull }
86 a31db67d 2004-04-21 devnull noutput += p-obuf;
87 a31db67d 2004-04-21 devnull write(1, obuf, p-obuf);
88 a31db67d 2004-04-21 devnull }
89 a31db67d 2004-04-21 devnull
90 a31db67d 2004-04-21 devnull void
91 a31db67d 2004-04-21 devnull isoutf_in(int fd, long *notused, struct convert *out)
92 a31db67d 2004-04-21 devnull {
93 7551b2ec 2005-03-09 devnull char buf[N];
94 7551b2ec 2005-03-09 devnull int i, j, c, n, tot;
95 a31db67d 2004-04-21 devnull
96 a31db67d 2004-04-21 devnull USED(notused);
97 7551b2ec 2005-03-09 devnull tot = 0;
98 7551b2ec 2005-03-09 devnull while((n = read(fd, buf+tot, N-tot)) >= 0){
99 7551b2ec 2005-03-09 devnull tot += n;
100 7551b2ec 2005-03-09 devnull for(i=j=0; i<tot; ){
101 7551b2ec 2005-03-09 devnull if(!fullisorune(buf+i, tot-i))
102 7551b2ec 2005-03-09 devnull break;
103 7551b2ec 2005-03-09 devnull c = isochartorune(&runes[j], buf+i);
104 536f9b83 2006-05-21 devnull if(runes[j] == Runeerror && c == 1){
105 7551b2ec 2005-03-09 devnull if(squawk)
106 7551b2ec 2005-03-09 devnull EPR "%s: bad UTF sequence near byte %ld in input\n", argv0, ninput+i);
107 536f9b83 2006-05-21 devnull if(clean){
108 536f9b83 2006-05-21 devnull i++;
109 7551b2ec 2005-03-09 devnull continue;
110 536f9b83 2006-05-21 devnull }
111 7551b2ec 2005-03-09 devnull nerrors++;
112 a31db67d 2004-04-21 devnull }
113 7551b2ec 2005-03-09 devnull j++;
114 7551b2ec 2005-03-09 devnull i += c;
115 a31db67d 2004-04-21 devnull }
116 7551b2ec 2005-03-09 devnull OUT(out, runes, j);
117 7551b2ec 2005-03-09 devnull tot -= i;
118 7551b2ec 2005-03-09 devnull ninput += i;
119 7551b2ec 2005-03-09 devnull if(tot)
120 7551b2ec 2005-03-09 devnull memmove(buf, buf+i, tot);
121 7551b2ec 2005-03-09 devnull if(n == 0)
122 7551b2ec 2005-03-09 devnull break;
123 7551b2ec 2005-03-09 devnull }
124 536f9b83 2006-05-21 devnull OUT(out, runes, 0);
125 a31db67d 2004-04-21 devnull }
126 a31db67d 2004-04-21 devnull
127 a31db67d 2004-04-21 devnull void
128 a31db67d 2004-04-21 devnull isoutf_out(Rune *base, int n, long *notused)
129 a31db67d 2004-04-21 devnull {
130 a31db67d 2004-04-21 devnull char *p;
131 a31db67d 2004-04-21 devnull Rune *r;
132 a31db67d 2004-04-21 devnull
133 a31db67d 2004-04-21 devnull USED(notused);
134 a31db67d 2004-04-21 devnull nrunes += n;
135 a31db67d 2004-04-21 devnull for(r = base, p = obuf; n-- > 0; r++)
136 a31db67d 2004-04-21 devnull p += runetoisoutf(p, r);
137 a31db67d 2004-04-21 devnull noutput += p-obuf;
138 a31db67d 2004-04-21 devnull write(1, obuf, p-obuf);
139 a31db67d 2004-04-21 devnull }
140 a31db67d 2004-04-21 devnull
141 a31db67d 2004-04-21 devnull
142 a31db67d 2004-04-21 devnull enum
143 a31db67d 2004-04-21 devnull {
144 a31db67d 2004-04-21 devnull Char1 = Runeself, Rune1 = Runeself,
145 a31db67d 2004-04-21 devnull Char21 = 0xA1, Rune21 = 0x0100,
146 a31db67d 2004-04-21 devnull Char22 = 0xF6, Rune22 = 0x4016,
147 a31db67d 2004-04-21 devnull Char3 = 0xFC, Rune3 = 0x10000, /* really 0x38E2E */
148 a31db67d 2004-04-21 devnull Esc = 0xBE, Bad = Runeerror
149 a31db67d 2004-04-21 devnull };
150 a31db67d 2004-04-21 devnull
151 a31db67d 2004-04-21 devnull static uchar U[256];
152 a31db67d 2004-04-21 devnull static uchar T[256];
153 a31db67d 2004-04-21 devnull
154 a31db67d 2004-04-21 devnull static
155 a31db67d 2004-04-21 devnull void
156 a31db67d 2004-04-21 devnull mktable(void)
157 a31db67d 2004-04-21 devnull {
158 a31db67d 2004-04-21 devnull int i, u;
159 a31db67d 2004-04-21 devnull
160 a31db67d 2004-04-21 devnull for(i=0; i<256; i++) {
161 a31db67d 2004-04-21 devnull u = i + (0x5E - 0xA0);
162 a31db67d 2004-04-21 devnull if(i < 0xA0)
163 a31db67d 2004-04-21 devnull u = i + (0xDF - 0x7F);
164 a31db67d 2004-04-21 devnull if(i < 0x7F)
165 a31db67d 2004-04-21 devnull u = i + (0x00 - 0x21);
166 a31db67d 2004-04-21 devnull if(i < 0x21)
167 a31db67d 2004-04-21 devnull u = i + (0xBE - 0x00);
168 a31db67d 2004-04-21 devnull U[i] = u;
169 a31db67d 2004-04-21 devnull T[u] = i;
170 a31db67d 2004-04-21 devnull }
171 a31db67d 2004-04-21 devnull }
172 a31db67d 2004-04-21 devnull
173 a31db67d 2004-04-21 devnull int
174 a31db67d 2004-04-21 devnull isochartorune(Rune *rune, char *str)
175 a31db67d 2004-04-21 devnull {
176 a31db67d 2004-04-21 devnull int c, c1, c2;
177 a31db67d 2004-04-21 devnull long l;
178 a31db67d 2004-04-21 devnull
179 a31db67d 2004-04-21 devnull if(U[0] == 0)
180 a31db67d 2004-04-21 devnull mktable();
181 a31db67d 2004-04-21 devnull
182 a31db67d 2004-04-21 devnull /*
183 a31db67d 2004-04-21 devnull * one character sequence
184 a31db67d 2004-04-21 devnull * 00000-0009F => 00-9F
185 a31db67d 2004-04-21 devnull */
186 a31db67d 2004-04-21 devnull c = *(uchar*)str;
187 a31db67d 2004-04-21 devnull if(c < Char1) {
188 a31db67d 2004-04-21 devnull *rune = c;
189 a31db67d 2004-04-21 devnull return 1;
190 a31db67d 2004-04-21 devnull }
191 a31db67d 2004-04-21 devnull
192 a31db67d 2004-04-21 devnull /*
193 a31db67d 2004-04-21 devnull * two character sequence
194 a31db67d 2004-04-21 devnull * 000A0-000FF => A0; A0-FF
195 a31db67d 2004-04-21 devnull */
196 a31db67d 2004-04-21 devnull c1 = *(uchar*)(str+1);
197 a31db67d 2004-04-21 devnull if(c < Char21) {
198 a31db67d 2004-04-21 devnull if(c1 >= Rune1 && c1 < Rune21) {
199 a31db67d 2004-04-21 devnull *rune = c1;
200 a31db67d 2004-04-21 devnull return 2;
201 a31db67d 2004-04-21 devnull }
202 a31db67d 2004-04-21 devnull goto bad;
203 a31db67d 2004-04-21 devnull }
204 a31db67d 2004-04-21 devnull
205 a31db67d 2004-04-21 devnull /*
206 a31db67d 2004-04-21 devnull * two character sequence
207 a31db67d 2004-04-21 devnull * 00100-04015 => A1-F5; 21-7E/A0-FF
208 a31db67d 2004-04-21 devnull */
209 a31db67d 2004-04-21 devnull c1 = U[c1];
210 a31db67d 2004-04-21 devnull if(c1 >= Esc)
211 a31db67d 2004-04-21 devnull goto bad;
212 a31db67d 2004-04-21 devnull if(c < Char22) {
213 a31db67d 2004-04-21 devnull *rune = (c-Char21)*Esc + c1 + Rune21;
214 a31db67d 2004-04-21 devnull return 2;
215 a31db67d 2004-04-21 devnull }
216 a31db67d 2004-04-21 devnull
217 a31db67d 2004-04-21 devnull /*
218 a31db67d 2004-04-21 devnull * three character sequence
219 a31db67d 2004-04-21 devnull * 04016-38E2D => A6-FB; 21-7E/A0-FF
220 a31db67d 2004-04-21 devnull */
221 a31db67d 2004-04-21 devnull c2 = U[*(uchar*)(str+2)];
222 a31db67d 2004-04-21 devnull if(c2 >= Esc)
223 a31db67d 2004-04-21 devnull goto bad;
224 a31db67d 2004-04-21 devnull if(c < Char3) {
225 a31db67d 2004-04-21 devnull l = (c-Char22)*Esc*Esc + c1*Esc + c2 + Rune22;
226 a31db67d 2004-04-21 devnull if(l >= Rune3)
227 a31db67d 2004-04-21 devnull goto bad;
228 a31db67d 2004-04-21 devnull *rune = l;
229 a31db67d 2004-04-21 devnull return 3;
230 a31db67d 2004-04-21 devnull }
231 a31db67d 2004-04-21 devnull
232 a31db67d 2004-04-21 devnull /*
233 a31db67d 2004-04-21 devnull * bad decoding
234 a31db67d 2004-04-21 devnull */
235 a31db67d 2004-04-21 devnull bad:
236 a31db67d 2004-04-21 devnull *rune = Bad;
237 a31db67d 2004-04-21 devnull return 1;
238 a31db67d 2004-04-21 devnull }
239 a31db67d 2004-04-21 devnull
240 a31db67d 2004-04-21 devnull int
241 a31db67d 2004-04-21 devnull runetoisoutf(char *str, Rune *rune)
242 a31db67d 2004-04-21 devnull {
243 a31db67d 2004-04-21 devnull long c;
244 a31db67d 2004-04-21 devnull
245 a31db67d 2004-04-21 devnull if(T[0] == 0)
246 a31db67d 2004-04-21 devnull mktable();
247 a31db67d 2004-04-21 devnull
248 a31db67d 2004-04-21 devnull /*
249 a31db67d 2004-04-21 devnull * one character sequence
250 a31db67d 2004-04-21 devnull * 00000-0009F => 00-9F
251 a31db67d 2004-04-21 devnull */
252 a31db67d 2004-04-21 devnull c = *rune;
253 a31db67d 2004-04-21 devnull if(c < Rune1) {
254 a31db67d 2004-04-21 devnull str[0] = c;
255 a31db67d 2004-04-21 devnull return 1;
256 a31db67d 2004-04-21 devnull }
257 a31db67d 2004-04-21 devnull
258 a31db67d 2004-04-21 devnull /*
259 a31db67d 2004-04-21 devnull * two character sequence
260 a31db67d 2004-04-21 devnull * 000A0-000FF => A0; A0-FF
261 a31db67d 2004-04-21 devnull */
262 a31db67d 2004-04-21 devnull if(c < Rune21) {
263 b5932d3d 2005-03-18 devnull str[0] = (char)Char1;
264 a31db67d 2004-04-21 devnull str[1] = c;
265 a31db67d 2004-04-21 devnull return 2;
266 a31db67d 2004-04-21 devnull }
267 a31db67d 2004-04-21 devnull
268 a31db67d 2004-04-21 devnull /*
269 a31db67d 2004-04-21 devnull * two character sequence
270 a31db67d 2004-04-21 devnull * 00100-04015 => A1-F5; 21-7E/A0-FF
271 a31db67d 2004-04-21 devnull */
272 a31db67d 2004-04-21 devnull if(c < Rune22) {
273 a31db67d 2004-04-21 devnull c -= Rune21;
274 a31db67d 2004-04-21 devnull str[0] = c/Esc + Char21;
275 a31db67d 2004-04-21 devnull str[1] = T[c%Esc];
276 a31db67d 2004-04-21 devnull return 2;
277 a31db67d 2004-04-21 devnull }
278 a31db67d 2004-04-21 devnull
279 a31db67d 2004-04-21 devnull /*
280 a31db67d 2004-04-21 devnull * three character sequence
281 a31db67d 2004-04-21 devnull * 04016-38E2D => A6-FB; 21-7E/A0-FF
282 a31db67d 2004-04-21 devnull */
283 a31db67d 2004-04-21 devnull c -= Rune22;
284 a31db67d 2004-04-21 devnull str[0] = c/(Esc*Esc) + Char22;
285 a31db67d 2004-04-21 devnull str[1] = T[c/Esc%Esc];
286 a31db67d 2004-04-21 devnull str[2] = T[c%Esc];
287 a31db67d 2004-04-21 devnull return 3;
288 a31db67d 2004-04-21 devnull }
289 a31db67d 2004-04-21 devnull
290 a31db67d 2004-04-21 devnull int
291 a31db67d 2004-04-21 devnull fullisorune(char *str, int n)
292 a31db67d 2004-04-21 devnull {
293 a31db67d 2004-04-21 devnull int c;
294 a31db67d 2004-04-21 devnull
295 a31db67d 2004-04-21 devnull if(n > 0) {
296 a31db67d 2004-04-21 devnull c = *(uchar*)str;
297 a31db67d 2004-04-21 devnull if(c < Char1)
298 a31db67d 2004-04-21 devnull return 1;
299 a31db67d 2004-04-21 devnull if(n > 1)
300 a31db67d 2004-04-21 devnull if(c < Char22 || n > 2)
301 a31db67d 2004-04-21 devnull return 1;
302 a31db67d 2004-04-21 devnull }
303 a31db67d 2004-04-21 devnull return 0;
304 a31db67d 2004-04-21 devnull }
305 a31db67d 2004-04-21 devnull
306 a31db67d 2004-04-21 devnull enum
307 a31db67d 2004-04-21 devnull {
308 a31db67d 2004-04-21 devnull T1 = 0x00,
309 a31db67d 2004-04-21 devnull Tx = 0x80,
310 a31db67d 2004-04-21 devnull T2 = 0xC0,
311 a31db67d 2004-04-21 devnull T3 = 0xE0,
312 a31db67d 2004-04-21 devnull T4 = 0xF0,
313 a31db67d 2004-04-21 devnull T5 = 0xF8,
314 a31db67d 2004-04-21 devnull T6 = 0xFC,
315 a31db67d 2004-04-21 devnull
316 a31db67d 2004-04-21 devnull Bit1 = 7,
317 a31db67d 2004-04-21 devnull Bitx = 6,
318 a31db67d 2004-04-21 devnull Bit2 = 5,
319 a31db67d 2004-04-21 devnull Bit3 = 4,
320 a31db67d 2004-04-21 devnull Bit4 = 3,
321 a31db67d 2004-04-21 devnull Bit5 = 2,
322 a31db67d 2004-04-21 devnull Bit6 = 2,
323 a31db67d 2004-04-21 devnull
324 a31db67d 2004-04-21 devnull Mask1 = (1<<Bit1)-1,
325 a31db67d 2004-04-21 devnull Maskx = (1<<Bitx)-1,
326 a31db67d 2004-04-21 devnull Mask2 = (1<<Bit2)-1,
327 a31db67d 2004-04-21 devnull Mask3 = (1<<Bit3)-1,
328 a31db67d 2004-04-21 devnull Mask4 = (1<<Bit4)-1,
329 a31db67d 2004-04-21 devnull Mask5 = (1<<Bit5)-1,
330 a31db67d 2004-04-21 devnull Mask6 = (1<<Bit6)-1,
331 a31db67d 2004-04-21 devnull
332 a31db67d 2004-04-21 devnull Wchar1 = (1UL<<Bit1)-1,
333 a31db67d 2004-04-21 devnull Wchar2 = (1UL<<(Bit2+Bitx))-1,
334 a31db67d 2004-04-21 devnull Wchar3 = (1UL<<(Bit3+2*Bitx))-1,
335 a31db67d 2004-04-21 devnull Wchar4 = (1UL<<(Bit4+3*Bitx))-1,
336 a31db67d 2004-04-21 devnull Wchar5 = (1UL<<(Bit5+4*Bitx))-1
337 a31db67d 2004-04-21 devnull };
338 a31db67d 2004-04-21 devnull
339 a31db67d 2004-04-21 devnull int
340 a31db67d 2004-04-21 devnull our_wctomb(char *s, unsigned long wc)
341 a31db67d 2004-04-21 devnull {
342 a31db67d 2004-04-21 devnull if(s == 0)
343 a31db67d 2004-04-21 devnull return 0; /* no shift states */
344 a31db67d 2004-04-21 devnull if(wc & ~Wchar2) {
345 a31db67d 2004-04-21 devnull if(wc & ~Wchar4) {
346 a31db67d 2004-04-21 devnull if(wc & ~Wchar5) {
347 a31db67d 2004-04-21 devnull /* 6 bytes */
348 a31db67d 2004-04-21 devnull s[0] = T6 | ((wc >> 5*Bitx) & Mask6);
349 a31db67d 2004-04-21 devnull s[1] = Tx | ((wc >> 4*Bitx) & Maskx);
350 a31db67d 2004-04-21 devnull s[2] = Tx | ((wc >> 3*Bitx) & Maskx);
351 a31db67d 2004-04-21 devnull s[3] = Tx | ((wc >> 2*Bitx) & Maskx);
352 a31db67d 2004-04-21 devnull s[4] = Tx | ((wc >> 1*Bitx) & Maskx);
353 a31db67d 2004-04-21 devnull s[5] = Tx | (wc & Maskx);
354 a31db67d 2004-04-21 devnull return 6;
355 a31db67d 2004-04-21 devnull }
356 a31db67d 2004-04-21 devnull /* 5 bytes */
357 a31db67d 2004-04-21 devnull s[0] = T5 | (wc >> 4*Bitx);
358 a31db67d 2004-04-21 devnull s[1] = Tx | ((wc >> 3*Bitx) & Maskx);
359 a31db67d 2004-04-21 devnull s[2] = Tx | ((wc >> 2*Bitx) & Maskx);
360 a31db67d 2004-04-21 devnull s[3] = Tx | ((wc >> 1*Bitx) & Maskx);
361 a31db67d 2004-04-21 devnull s[4] = Tx | (wc & Maskx);
362 a31db67d 2004-04-21 devnull return 5;
363 a31db67d 2004-04-21 devnull }
364 a31db67d 2004-04-21 devnull if(wc & ~Wchar3) {
365 a31db67d 2004-04-21 devnull /* 4 bytes */
366 a31db67d 2004-04-21 devnull s[0] = T4 | (wc >> 3*Bitx);
367 a31db67d 2004-04-21 devnull s[1] = Tx | ((wc >> 2*Bitx) & Maskx);
368 a31db67d 2004-04-21 devnull s[2] = Tx | ((wc >> 1*Bitx) & Maskx);
369 a31db67d 2004-04-21 devnull s[3] = Tx | (wc & Maskx);
370 a31db67d 2004-04-21 devnull return 4;
371 a31db67d 2004-04-21 devnull }
372 a31db67d 2004-04-21 devnull /* 3 bytes */
373 a31db67d 2004-04-21 devnull s[0] = T3 | (wc >> 2*Bitx);
374 a31db67d 2004-04-21 devnull s[1] = Tx | ((wc >> 1*Bitx) & Maskx);
375 a31db67d 2004-04-21 devnull s[2] = Tx | (wc & Maskx);
376 a31db67d 2004-04-21 devnull return 3;
377 a31db67d 2004-04-21 devnull }
378 a31db67d 2004-04-21 devnull if(wc & ~Wchar1) {
379 a31db67d 2004-04-21 devnull /* 2 bytes */
380 a31db67d 2004-04-21 devnull s[0] = T2 | (wc >> 1*Bitx);
381 a31db67d 2004-04-21 devnull s[1] = Tx | (wc & Maskx);
382 a31db67d 2004-04-21 devnull return 2;
383 a31db67d 2004-04-21 devnull }
384 a31db67d 2004-04-21 devnull /* 1 byte */
385 a31db67d 2004-04-21 devnull s[0] = T1 | wc;
386 a31db67d 2004-04-21 devnull return 1;
387 a31db67d 2004-04-21 devnull }
388 a31db67d 2004-04-21 devnull
389 a31db67d 2004-04-21 devnull int
390 a31db67d 2004-04-21 devnull our_mbtowc(unsigned long *p, char *s, unsigned n)
391 a31db67d 2004-04-21 devnull {
392 a31db67d 2004-04-21 devnull uchar *us;
393 a31db67d 2004-04-21 devnull int c0, c1, c2, c3, c4, c5;
394 a31db67d 2004-04-21 devnull unsigned long wc;
395 a31db67d 2004-04-21 devnull
396 a31db67d 2004-04-21 devnull if(s == 0)
397 a31db67d 2004-04-21 devnull return 0; /* no shift states */
398 a31db67d 2004-04-21 devnull
399 a31db67d 2004-04-21 devnull if(n < 1)
400 536f9b83 2006-05-21 devnull goto bad;
401 a31db67d 2004-04-21 devnull us = (uchar*)s;
402 a31db67d 2004-04-21 devnull c0 = us[0];
403 a31db67d 2004-04-21 devnull if(c0 >= T3) {
404 a31db67d 2004-04-21 devnull if(n < 3)
405 536f9b83 2006-05-21 devnull goto bad;
406 a31db67d 2004-04-21 devnull c1 = us[1] ^ Tx;
407 a31db67d 2004-04-21 devnull c2 = us[2] ^ Tx;
408 a31db67d 2004-04-21 devnull if((c1|c2) & T2)
409 a31db67d 2004-04-21 devnull goto bad;
410 a31db67d 2004-04-21 devnull if(c0 >= T5) {
411 a31db67d 2004-04-21 devnull if(n < 5)
412 536f9b83 2006-05-21 devnull goto bad;
413 a31db67d 2004-04-21 devnull c3 = us[3] ^ Tx;
414 a31db67d 2004-04-21 devnull c4 = us[4] ^ Tx;
415 a31db67d 2004-04-21 devnull if((c3|c4) & T2)
416 a31db67d 2004-04-21 devnull goto bad;
417 a31db67d 2004-04-21 devnull if(c0 >= T6) {
418 a31db67d 2004-04-21 devnull /* 6 bytes */
419 a31db67d 2004-04-21 devnull if(n < 6)
420 536f9b83 2006-05-21 devnull goto bad;
421 a31db67d 2004-04-21 devnull c5 = us[5] ^ Tx;
422 a31db67d 2004-04-21 devnull if(c5 & T2)
423 a31db67d 2004-04-21 devnull goto bad;
424 a31db67d 2004-04-21 devnull wc = ((((((((((c0 & Mask6) << Bitx) |
425 a31db67d 2004-04-21 devnull c1) << Bitx) | c2) << Bitx) |
426 a31db67d 2004-04-21 devnull c3) << Bitx) | c4) << Bitx) | c5;
427 a31db67d 2004-04-21 devnull if(wc <= Wchar5)
428 a31db67d 2004-04-21 devnull goto bad;
429 a31db67d 2004-04-21 devnull *p = wc;
430 a31db67d 2004-04-21 devnull return 6;
431 a31db67d 2004-04-21 devnull }
432 a31db67d 2004-04-21 devnull /* 5 bytes */
433 a31db67d 2004-04-21 devnull wc = ((((((((c0 & Mask5) << Bitx) |
434 a31db67d 2004-04-21 devnull c1) << Bitx) | c2) << Bitx) |
435 a31db67d 2004-04-21 devnull c3) << Bitx) | c4;
436 a31db67d 2004-04-21 devnull if(wc <= Wchar4)
437 a31db67d 2004-04-21 devnull goto bad;
438 a31db67d 2004-04-21 devnull *p = wc;
439 a31db67d 2004-04-21 devnull return 5;
440 a31db67d 2004-04-21 devnull }
441 a31db67d 2004-04-21 devnull if(c0 >= T4) {
442 a31db67d 2004-04-21 devnull /* 4 bytes */
443 a31db67d 2004-04-21 devnull if(n < 4)
444 536f9b83 2006-05-21 devnull goto bad;
445 a31db67d 2004-04-21 devnull c3 = us[3] ^ Tx;
446 a31db67d 2004-04-21 devnull if(c3 & T2)
447 a31db67d 2004-04-21 devnull goto bad;
448 a31db67d 2004-04-21 devnull wc = ((((((c0 & Mask4) << Bitx) |
449 a31db67d 2004-04-21 devnull c1) << Bitx) | c2) << Bitx) |
450 a31db67d 2004-04-21 devnull c3;
451 a31db67d 2004-04-21 devnull if(wc <= Wchar3)
452 a31db67d 2004-04-21 devnull goto bad;
453 a31db67d 2004-04-21 devnull *p = wc;
454 a31db67d 2004-04-21 devnull return 4;
455 a31db67d 2004-04-21 devnull }
456 a31db67d 2004-04-21 devnull /* 3 bytes */
457 a31db67d 2004-04-21 devnull wc = ((((c0 & Mask3) << Bitx) |
458 a31db67d 2004-04-21 devnull c1) << Bitx) | c2;
459 a31db67d 2004-04-21 devnull if(wc <= Wchar2)
460 a31db67d 2004-04-21 devnull goto bad;
461 a31db67d 2004-04-21 devnull *p = wc;
462 a31db67d 2004-04-21 devnull return 3;
463 a31db67d 2004-04-21 devnull }
464 a31db67d 2004-04-21 devnull if(c0 >= T2) {
465 a31db67d 2004-04-21 devnull /* 2 bytes */
466 a31db67d 2004-04-21 devnull if(n < 2)
467 536f9b83 2006-05-21 devnull goto bad;
468 a31db67d 2004-04-21 devnull c1 = us[1] ^ Tx;
469 a31db67d 2004-04-21 devnull if(c1 & T2)
470 a31db67d 2004-04-21 devnull goto bad;
471 a31db67d 2004-04-21 devnull wc = ((c0 & Mask2) << Bitx) |
472 a31db67d 2004-04-21 devnull c1;
473 a31db67d 2004-04-21 devnull if(wc <= Wchar1)
474 a31db67d 2004-04-21 devnull goto bad;
475 a31db67d 2004-04-21 devnull *p = wc;
476 a31db67d 2004-04-21 devnull return 2;
477 a31db67d 2004-04-21 devnull }
478 a31db67d 2004-04-21 devnull /* 1 byte */
479 a31db67d 2004-04-21 devnull if(c0 >= Tx)
480 a31db67d 2004-04-21 devnull goto bad;
481 a31db67d 2004-04-21 devnull *p = c0;
482 a31db67d 2004-04-21 devnull return 1;
483 a31db67d 2004-04-21 devnull
484 a31db67d 2004-04-21 devnull bad:
485 a31db67d 2004-04-21 devnull errno = EILSEQ;
486 a31db67d 2004-04-21 devnull return -1;
487 a31db67d 2004-04-21 devnull }