74 lines
1.1 KiB
C
74 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include "charset.h"
|
|
|
|
static int
|
|
add_unicode(char **d, size_t *len, uint16_t u)
|
|
{
|
|
if (u < 0x80)
|
|
{
|
|
// 1 byte
|
|
if (*len < 1) return 0;
|
|
(*d)[0] = u;
|
|
(*d)++, *len--;
|
|
return 1;
|
|
}
|
|
if (u < 0x800)
|
|
{
|
|
// 2 bytes
|
|
if (*len < 2) return 0;
|
|
|
|
(*d)[0] = 0xc0 | ((u >> 6) & 0x1f);
|
|
(*d)[1] = 0x80 | (u & 0x3f);
|
|
*d += 2, *len -= 2;
|
|
return 1;
|
|
}
|
|
if (u < 0x10000)
|
|
{
|
|
// 3 bytes
|
|
if (*len < 3) return 0;
|
|
|
|
(*d)[0] = 0xe0 | ((u >> 12) & 0xf);
|
|
(*d)[1] = 0x80 | ((u >> 6) & 0x3f);
|
|
(*d)[2] = 0x80 | (u & 0x3f);
|
|
*d += 3, *len -= 3;
|
|
return 1;
|
|
}
|
|
// 4 byte code point not supported.
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
xconv(char *src, char *dst, size_t dstlen)
|
|
{
|
|
size_t len = dstlen;
|
|
char *s, *d;
|
|
int i;
|
|
|
|
for (s = src, d = dst; *s && len > 0; s++)
|
|
{
|
|
// Check for combined character.
|
|
if ((*s & 0xf0) == 0xc0 && s[1])
|
|
{
|
|
icc_t *p;
|
|
int k = *s & 0xf;
|
|
p = iso6937_combined[k];
|
|
for (i = 0; p[i].c; i++)
|
|
{
|
|
if (p[i].c == (s[1] & 0xff))
|
|
{
|
|
add_unicode(&d, &len,
|
|
iso6937_combined[k][i].u);
|
|
break;
|
|
}
|
|
}
|
|
s++;
|
|
continue;
|
|
}
|
|
|
|
add_unicode(&d, &len, iso6937_map[*s & 0xff]);
|
|
}
|
|
*d = '\0';
|
|
return dstlen - len;
|
|
}
|
|
|