Compare commits

...

2 Commits

2 changed files with 26 additions and 30 deletions

52
xconv.c
View File

@ -1,4 +1,3 @@
#include <stdio.h>
#include "charset.h" #include "charset.h"
static int static int
@ -8,8 +7,8 @@ add_unicode(char **d, size_t *len, uint16_t u)
{ {
// 1 byte // 1 byte
if (*len < 1) return 0; if (*len < 1) return 0;
(*d)[0] = u; (*d)[0] = (char)u;
(*d)++, (*len)--; ++(*d), --(*len);
return 1; return 1;
} }
if (u < 0x800) if (u < 0x800)
@ -42,8 +41,7 @@ add_unicode(char **d, size_t *len, uint16_t u)
static int static int
is_utf_sequence(char *s) is_utf_sequence(char *s)
{ {
int utf8_len; size_t utf8_len, i;
int i;
if ((*s & 0xe0) == 0xc0) // U+0080+ 110xxxxx if ((*s & 0xe0) == 0xc0) // U+0080+ 110xxxxx
utf8_len = 1; utf8_len = 1;
@ -53,7 +51,7 @@ is_utf_sequence(char *s)
utf8_len = 3; utf8_len = 3;
else return 0; else return 0;
for (i = 1; i <= utf8_len; i++) for (i = 1; i <= utf8_len; ++i)
{ {
// UTF-8 continuation character 10xxxxxx // UTF-8 continuation character 10xxxxxx
if ((s[i] & 0xc0) != 0x80) if ((s[i] & 0xc0) != 0x80)
@ -66,17 +64,16 @@ is_utf_sequence(char *s)
size_t size_t
xconv(char *src, char *dst, size_t dstlen) xconv(char *src, char *dst, size_t dstlen)
{ {
size_t len = dstlen - 1; size_t i, len = dstlen - 1;
char *s, *d; char *s, *d;
int changed = 0; int changed = 0;
int i;
for (s = src, d = dst; *s && len > 0; s++) for (s = src, d = dst; *s && len > 0; ++s)
{ {
if (!(*s & 0x80)) if (!(*s & 0x80))
{ {
*d++ = *s; *(d++) = *s;
len--; --len;
continue; continue;
} }
@ -84,31 +81,30 @@ xconv(char *src, char *dst, size_t dstlen)
return 0; return 0;
changed = 1; changed = 1;
// Check for combined character. // Check for combined character.
if ((*s & 0xf0) == 0xc0 && s[1]) if ((*s & 0xf0) == 0xc0)
{ {
int k = *s & 0xf; if (s[1])
icc_t *p = iso6937_combined[k];
for (i = 0; p[i].c; i++)
{ {
if (p[i].c == (s[1] & 0xff)) icc_t *p = iso6937_combined[*s & 0xf];
{
add_unicode(&d, &len, p[i].u);
break;
}
}
s++;
continue;
}
add_unicode(&d, &len, iso6937_map[*s & 0xff]); for (i = 0; p[i].c; ++i)
if (p[i].c == (s[1] & 0xff))
{
add_unicode(&d, &len, p[i].u);
break;
}
++s;
}
}
else
add_unicode(&d, &len, iso6937_map[*s & 0xff]);
} }
if (!changed) if (!changed)
return 0; return 0;
*d = '\0'; *d = '\0';
return dstlen - len - 1; return dstlen - len - 1;
} }

View File

@ -1,7 +1,7 @@
#ifndef _XCONV_H #ifndef _XCONV_H
#define _XCONV_H
size_t xconv(char *, char *, size_t); size_t xconv(char *, char *, size_t);
#endif #endif