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