7 the our_* routines are implementations for the corresponding library
8 routines. for a while, i tried to actually name them wctomb etc
9 but stopped that after i found a system which made wchar_t an
13 int our_wctomb(char *s, unsigned long wc);
14 int our_mbtowc(unsigned long *p, char *s, unsigned n);
15 int runetoisoutf(char *str, Rune *rune);
16 int fullisorune(char *str, int n);
17 int isochartorune(Rune *rune, char *str);
20 utf_in(int fd, long *, struct convert *out)
27 while((n = read(fd, buf+tot, N-tot)) >= 0){
29 for(i=j=0; i<=tot-UTFmax || (i<tot && (n==0 || fullrune(buf+i, tot-i))); ){
30 c = our_mbtowc(&l, buf+i, tot-i);
33 warn("bad UTF sequence near byte %ld in input", ninput+i);
49 memmove(buf, buf+i, tot);
57 utf_out(Rune *base, int n, long *)
63 for(r = base, p = obuf; n-- > 0; r++){
64 p += our_wctomb(p, *r);
68 write(1, obuf, p-obuf);
72 isoutf_in(int fd, long *, struct convert *out)
78 while((n = read(fd, buf+tot, N-tot)) >= 0){
81 if(!fullisorune(buf+i, tot-i))
83 c = isochartorune(&runes[j], buf+i);
84 if(runes[j] == Runeerror){
86 warn("bad UTF sequence near byte %ld in input", ninput+i);
100 memmove(buf, buf+i, tot);
108 isoutf_out(Rune *base, int n, long *)
114 for(r = base, p = obuf; n-- > 0; r++)
115 p += runetoisoutf(p, r);
118 write(1, obuf, p-obuf);
124 Char1 = Runeself, Rune1 = Runeself,
125 Char21 = 0xA1, Rune21 = 0x0100,
126 Char22 = 0xF6, Rune22 = 0x4016,
127 Char3 = 0xFC, Rune3 = 0x10000, /* really 0x38E2E */
128 Esc = 0xBE, Bad = Runeerror
140 for(i=0; i<256; i++) {
141 u = i + (0x5E - 0xA0);
143 u = i + (0xDF - 0x7F);
145 u = i + (0x00 - 0x21);
147 u = i + (0xBE - 0x00);
154 isochartorune(Rune *rune, char *str)
163 * one character sequence
164 * 00000-0009F => 00-9F
173 * two character sequence
174 * 000A0-000FF => A0; A0-FF
176 c1 = *(uchar*)(str+1);
178 if(c1 >= Rune1 && c1 < Rune21) {
186 * two character sequence
187 * 00100-04015 => A1-F5; 21-7E/A0-FF
193 *rune = (c-Char21)*Esc + c1 + Rune21;
198 * three character sequence
199 * 04016-38E2D => A6-FB; 21-7E/A0-FF
201 c2 = U[*(uchar*)(str+2)];
205 l = (c-Char22)*Esc*Esc + c1*Esc + c2 + Rune22;
221 runetoisoutf(char *str, Rune *rune)
229 * one character sequence
230 * 00000-0009F => 00-9F
239 * two character sequence
240 * 000A0-000FF => A0; A0-FF
249 * two character sequence
250 * 00100-04015 => A1-F5; 21-7E/A0-FF
254 str[0] = c/Esc + Char21;
260 * three character sequence
261 * 04016-38E2D => A6-FB; 21-7E/A0-FF
264 str[0] = c/(Esc*Esc) + Char22;
265 str[1] = T[c/Esc%Esc];
271 fullisorune(char *str, int n)
280 if(c < Char22 || n > 2)
312 Wchar1 = (1UL<<Bit1)-1,
313 Wchar2 = (1UL<<(Bit2+Bitx))-1,
314 Wchar3 = (1UL<<(Bit3+2*Bitx))-1,
315 Wchar4 = (1UL<<(Bit4+3*Bitx))-1,
316 Wchar5 = (1UL<<(Bit5+4*Bitx))-1,
320 our_wctomb(char *s, unsigned long wc)
323 return 0; /* no shift states */
328 s[0] = T6 | ((wc >> 5*Bitx) & Mask6);
329 s[1] = Tx | ((wc >> 4*Bitx) & Maskx);
330 s[2] = Tx | ((wc >> 3*Bitx) & Maskx);
331 s[3] = Tx | ((wc >> 2*Bitx) & Maskx);
332 s[4] = Tx | ((wc >> 1*Bitx) & Maskx);
333 s[5] = Tx | (wc & Maskx);
337 s[0] = T5 | (wc >> 4*Bitx);
338 s[1] = Tx | ((wc >> 3*Bitx) & Maskx);
339 s[2] = Tx | ((wc >> 2*Bitx) & Maskx);
340 s[3] = Tx | ((wc >> 1*Bitx) & Maskx);
341 s[4] = Tx | (wc & Maskx);
346 s[0] = T4 | (wc >> 3*Bitx);
347 s[1] = Tx | ((wc >> 2*Bitx) & Maskx);
348 s[2] = Tx | ((wc >> 1*Bitx) & Maskx);
349 s[3] = Tx | (wc & Maskx);
353 s[0] = T3 | (wc >> 2*Bitx);
354 s[1] = Tx | ((wc >> 1*Bitx) & Maskx);
355 s[2] = Tx | (wc & Maskx);
360 s[0] = T2 | (wc >> 1*Bitx);
361 s[1] = Tx | (wc & Maskx);
370 our_mbtowc(unsigned long *p, char *s, unsigned n)
373 int c0, c1, c2, c3, c4, c5;
377 return 0; /* no shift states */
404 wc = ((((((((((c0 & Mask6) << Bitx) |
405 c1) << Bitx) | c2) << Bitx) |
406 c3) << Bitx) | c4) << Bitx) | c5;
413 wc = ((((((((c0 & Mask5) << Bitx) |
414 c1) << Bitx) | c2) << Bitx) |
428 wc = ((((((c0 & Mask4) << Bitx) |
429 c1) << Bitx) | c2) << Bitx) |
437 wc = ((((c0 & Mask3) << Bitx) |
451 wc = ((c0 & Mask2) << Bitx) |