4 typedef unsigned short smb_ucs2_t;
6 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
8 static int utf8_encode(unsigned char c[3], smb_ucs2_t uc)
10 unsigned char uc1, uc2;
14 if ((uc2 & 0xf8) == 0xd8) {
22 c[0] = 0xe0 | (uc2>>4);
23 c[1] = 0x80 | ((uc2&0xF)<<2) | (uc1>>6);
24 c[2] = 0x80 | (uc1&0x3f);
28 if (uc2 | (uc1 & 0x80)) {
29 c[0] = 0xc0 | (uc2<<2) | (uc1>>6);
30 c[1] = 0x80 | (uc1&0x3f);
38 static int utf8_decode(unsigned char c[3], smb_ucs2_t *uc)
42 if ((c[0] & 0xf0) == 0xe0) {
43 CVAL(uc, 1) = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
44 CVAL(uc, 0) = (c[1]<<6) | (c[2]&0x3f);
48 if ((c[0] & 0xe0) == 0xc0) {
49 CVAL(uc, 1) = (c[0]>>2) & 0x7;
50 CVAL(uc, 0) = (c[0]<<6) | (c[1]&0x3f);
59 int main(int argc, char *argv[])
63 unsigned char utf8[10];
64 unsigned char foo[10];
70 cd = iconv_open(argv[1], "UCS2");
72 for (i=1;i<0x10000;i++) {
82 iconv(cd, &p, &inlen, &q, &outlen);
84 len2 = utf8_encode(foo, i);
86 if (1 || len != len2 || strncmp(foo, utf8, len) != 0) {
89 printf("%02x ", utf8[j]);
94 for (j=0;j<len2;j++) {
95 printf("%02x ", foo[j]);