F.3. Sample Implementations

Below are sample implementations of the C standard wctomb() and mbtowc() functions that demonstrate the algorithms for converting from Unicode to the transformation format and converting from the transformation format to Unicode. The sample implementations include error checks, some of which may not be necessary for conformance:

typedef struct {

int cmask;

int cval;

int shift;

long lmask;

long lval;

} Tab;

static Tab tab[] = {

0x80, 0x00, 0*6, 0x7F, 0, /* 1 byte sequence */

0xE0, 0xC0, 1*6, 0x7FF, 0x80, /* 2 byte sequence */

0xF0, 0xE0, 2*6, 0xFFFF, 0x800, /* 3 byte sequence */

0xF8, 0xF0, 3*6, 0x1FFFFF, 0x10000, /* 4 byte sequence */

0xFC, 0xF8, 4*6, 0x3FFFFFF, 0x200000, /* 5 byte sequence */

0xFE, 0xFC, 5*6, 0x7FFFFFFF, 0x4000000, /* 6 byte sequence */

0, /* end of table */

};

int mbtowc ( wchar_t *p, char *s, size_t n ) {

long l;

int c0, c, nc;

Tab *t;

if ( s == 0 ) return 0;

nc = 0;

if ( n <= nc ) return -1;

c0 = *s & 0xff;

l = c0;

for ( t = tab; t->cmask; t++) {

nc++;

if ( ( c0 & t->cmask ) == t->cval ) {

l &= t->lmask;

if ( l < t->lval ) return -1;

*p = l;

return nc;

}

if ( n <= nc )

return -1;

s++;

c = ( *s ^ 0x80 ) & 0xFF;

if ( c & 0xC0 ) return -1;

l = ( l << 6 ) | c;

}

return -1;

}

int wctomb ( char *s, wchar_t wc ) {

long l;

int c, nc;

Tab *t;

if (s == 0 ) return 0;

l = wc;

nc = 0;

for ( t=tab; t->cmask; t++ ) {

nc++;

if ( l <= t->lmask ) {

c = t->shift;

*s = t->cval | ( l >> c );

while ( c > 0 ) {

c -= 6;

s++;

*s = 0x80 | ( ( l >> c ) & 0x3F );

}

return nc;

}

}

return -1;

}