Blob


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