|
Packit Service |
50ad14 |
/*
|
|
Packit Service |
50ad14 |
#include <sys/types.h>
|
|
Packit Service |
50ad14 |
#include <stdio.h>
|
|
Packit Service |
50ad14 |
#include <stdlib.h>
|
|
Packit Service |
50ad14 |
#include <string.h>
|
|
Packit Service |
50ad14 |
#include <unistd.h>
|
|
Packit Service |
50ad14 |
#include <errno.h>
|
|
Packit Service |
50ad14 |
*/
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
/*
|
|
Packit Service |
50ad14 |
the our_* routines are implementations for the corresponding library
|
|
Packit Service |
50ad14 |
routines. for a while, i tried to actually name them wctomb etc
|
|
Packit Service |
50ad14 |
but stopped that after i found a system which made wchar_t an
|
|
Packit Service |
50ad14 |
unsigned char.
|
|
Packit Service |
50ad14 |
*/
|
|
Packit Service |
50ad14 |
enum {
|
|
Packit Service |
50ad14 |
T1 = 0x00,
|
|
Packit Service |
50ad14 |
Tx = 0x80,
|
|
Packit Service |
50ad14 |
T2 = 0xC0,
|
|
Packit Service |
50ad14 |
T3 = 0xE0,
|
|
Packit Service |
50ad14 |
T4 = 0xF0,
|
|
Packit Service |
50ad14 |
T5 = 0xF8,
|
|
Packit Service |
50ad14 |
T6 = 0xFC,
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
Bit1 = 7,
|
|
Packit Service |
50ad14 |
Bitx = 6,
|
|
Packit Service |
50ad14 |
Bit2 = 5,
|
|
Packit Service |
50ad14 |
Bit3 = 4,
|
|
Packit Service |
50ad14 |
Bit4 = 3,
|
|
Packit Service |
50ad14 |
Bit5 = 2,
|
|
Packit Service |
50ad14 |
Bit6 = 2,
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
Mask1 = (1 << Bit1) - 1,
|
|
Packit Service |
50ad14 |
Maskx = (1 << Bitx) - 1,
|
|
Packit Service |
50ad14 |
Mask2 = (1 << Bit2) - 1,
|
|
Packit Service |
50ad14 |
Mask3 = (1 << Bit3) - 1,
|
|
Packit Service |
50ad14 |
Mask4 = (1 << Bit4) - 1,
|
|
Packit Service |
50ad14 |
Mask5 = (1 << Bit5) - 1,
|
|
Packit Service |
50ad14 |
Mask6 = (1 << Bit6) - 1,
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
Wchar1 = (1UL << Bit1) - 1,
|
|
Packit Service |
50ad14 |
Wchar2 = (1UL << (Bit2 + Bitx)) - 1,
|
|
Packit Service |
50ad14 |
Wchar3 = (1UL << (Bit3 + 2 * Bitx)) - 1,
|
|
Packit Service |
50ad14 |
Wchar4 = (1UL << (Bit4 + 3 * Bitx)) - 1,
|
|
Packit Service |
50ad14 |
Wchar5 = (1UL << (Bit5 + 4 * Bitx)) - 1
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
#ifndef EILSEQ
|
|
Packit Service |
50ad14 |
, /* we hate ansi c's comma rules */
|
|
Packit Service |
50ad14 |
EILSEQ = 123
|
|
Packit Service |
50ad14 |
#endif /* PLAN9 */
|
|
Packit Service |
50ad14 |
};
|
|
Packit Service |
50ad14 |
|
|
Packit Service |
50ad14 |
static int
|
|
Packit Service |
50ad14 |
our_wctomb(char *s, unsigned long wc)
|
|
Packit Service |
50ad14 |
{
|
|
Packit Service |
50ad14 |
if (s == 0)
|
|
Packit Service |
50ad14 |
return 0; /* no shift states */
|
|
Packit Service |
50ad14 |
if (wc & ~Wchar2) {
|
|
Packit Service |
50ad14 |
if (wc & ~Wchar4) {
|
|
Packit Service |
50ad14 |
if (wc & ~Wchar5) {
|
|
Packit Service |
50ad14 |
/* 6 bytes */
|
|
Packit Service |
50ad14 |
s[0] = T6 | ((wc >> 5 * Bitx) & Mask6);
|
|
Packit Service |
50ad14 |
s[1] = Tx | ((wc >> 4 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[2] = Tx | ((wc >> 3 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[3] = Tx | ((wc >> 2 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[4] = Tx | ((wc >> 1 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[5] = Tx | (wc & Maskx);
|
|
Packit Service |
50ad14 |
return 6;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
/* 5 bytes */
|
|
Packit Service |
50ad14 |
s[0] = T5 | (wc >> 4 * Bitx);
|
|
Packit Service |
50ad14 |
s[1] = Tx | ((wc >> 3 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[2] = Tx | ((wc >> 2 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[3] = Tx | ((wc >> 1 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[4] = Tx | (wc & Maskx);
|
|
Packit Service |
50ad14 |
return 5;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
if (wc & ~Wchar3) {
|
|
Packit Service |
50ad14 |
/* 4 bytes */
|
|
Packit Service |
50ad14 |
s[0] = T4 | (wc >> 3 * Bitx);
|
|
Packit Service |
50ad14 |
s[1] = Tx | ((wc >> 2 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[2] = Tx | ((wc >> 1 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[3] = Tx | (wc & Maskx);
|
|
Packit Service |
50ad14 |
return 4;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
/* 3 bytes */
|
|
Packit Service |
50ad14 |
s[0] = T3 | (wc >> 2 * Bitx);
|
|
Packit Service |
50ad14 |
s[1] = Tx | ((wc >> 1 * Bitx) & Maskx);
|
|
Packit Service |
50ad14 |
s[2] = Tx | (wc & Maskx);
|
|
Packit Service |
50ad14 |
return 3;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
if (wc & ~Wchar1) {
|
|
Packit Service |
50ad14 |
/* 2 bytes */
|
|
Packit Service |
50ad14 |
s[0] = T2 | (wc >> 1 * Bitx);
|
|
Packit Service |
50ad14 |
s[1] = Tx | (wc & Maskx);
|
|
Packit Service |
50ad14 |
return 2;
|
|
Packit Service |
50ad14 |
}
|
|
Packit Service |
50ad14 |
/* 1 byte */
|
|
Packit Service |
50ad14 |
s[0] = T1 | wc;
|
|
Packit Service |
50ad14 |
return 1;
|
|
Packit Service |
50ad14 |
}
|