#ifndef lint
static char *rcsid = "$Id: utf8.c,v 1.1 2003/06/04 00:26:44 marka Exp $";
#endif
#include <config.h>
#include <stddef.h>
#include <idn/assert.h>
#include <idn/logmacro.h>
#include <idn/utf8.h>
#include <idn/debug.h>
#define UTF8_WIDTH(c) \
(((c) < 0x80) ? 1 : \
((c) < 0xc0) ? 0 : \
((c) < 0xe0) ? 2 : \
((c) < 0xf0) ? 3 : \
((c) < 0xf8) ? 4 : \
((c) < 0xfc) ? 5 : \
((c) < 0xfe) ? 6 : 0)
#define VALID_CONT_BYTE(c) (0x80 <= (c) && (c) < 0xc0)
int
idn_utf8_mblen(const char *s) {
int c = *(unsigned char *)s;
assert(s != NULL);
#if 0
TRACE(("idn_utf8_mblen(s=<%s>)\n", idn__debug_hexstring(s, 6)));
#endif
return UTF8_WIDTH(c);
}
int
idn_utf8_getmb(const char *s, size_t len, char *buf) {
const unsigned char *p = (const unsigned char *)s;
unsigned char *q = (unsigned char *)buf;
int width = UTF8_WIDTH(*p);
int w;
assert(s != NULL);
#if 0
TRACE(("idn_utf8_getmb(s=<%s>,len=%d)\n",
idn__debug_hexstring(s, 6), len));
#endif
if (width == 0 || len < width)
return (0);
*q++ = *p++;
w = width;
while (--w > 0) {
if (!VALID_CONT_BYTE(*p))
return (0);
*q++ = *p++;
}
return (width);
}
int
idn_utf8_getwc(const char *s, size_t len, unsigned long *vp) {
unsigned long v;
unsigned long min;
const unsigned char *p = (const unsigned char *)s;
int c;
int width;
int rest;
assert(s != NULL);
#if 0
TRACE(("idn_utf8_getwc(s=<%s>,len=%d)\n",
idn__debug_hexstring(s, 10), len));
#endif
c = *p++;
width = UTF8_WIDTH(c);
switch (width) {
case 0:
return (0);
case 1:
v = c;
min = 0;
break;
case 2:
v = c & 0x1f;
min = 0x80;
break;
case 3:
v = c & 0xf;
min = 0x800;
break;
case 4:
v = c & 0x7;
min = 0x10000;
break;
case 5:
v = c & 3;
min = 0x200000;
break;
case 6:
v = c & 1;
min = 0x4000000;
break;
default:
FATAL(("idn_utf8_getint: internal error\n"));
return (0);
}
if (len < width)
return (0);
rest = width - 1;
while (rest-- > 0) {
if (!VALID_CONT_BYTE(*p))
return (0);
v = (v << 6) | (*p & 0x3f);
p++;
}
if (v < min)
return (0);
*vp = v;
return (width);
}
int
idn_utf8_putwc(char *s, size_t len, unsigned long v) {
unsigned char *p = (unsigned char *)s;
int mask;
int off;
int l;
assert(s != NULL);
#if 0
TRACE(("idn_utf8_putwc(v=%lx)\n", v));
#endif
if (v < 0x80) {
mask = 0;
l = 1;
} else if (v < 0x800) {
mask = 0xc0;
l = 2;
} else if (v < 0x10000) {
mask = 0xe0;
l = 3;
} else if (v < 0x200000) {
mask = 0xf0;
l = 4;
} else if (v < 0x4000000) {
mask = 0xf8;
l = 5;
} else if (v < 0x80000000) {
mask = 0xfc;
l = 6;
} else {
return (0);
}
if (len < l)
return (0);
off = 6 * (l - 1);
*p++ = (v >> off) | mask;
mask = 0x80;
while (off > 0) {
off -= 6;
*p++ = ((v >> off) & 0x3f) | mask;
}
return l;
}
int
idn_utf8_isvalidchar(const char *s) {
unsigned long dummy;
TRACE(("idn_utf8_isvalidchar(s=<%s>)\n",
idn__debug_hexstring(s, 6)));
return (idn_utf8_getwc(s, 6, &dummy) > 0);
}
int
idn_utf8_isvalidstring(const char *s) {
unsigned long dummy;
int width;
assert(s != NULL);
TRACE(("idn_utf8_isvalidstring(s=<%s>)\n",
idn__debug_hexstring(s, 20)));
while (*s != '\0') {
width = idn_utf8_getwc(s, 6, &dummy);
if (width == 0)
return (0);
s += width;
}
return (1);
}
char *
idn_utf8_findfirstbyte(const char *s, const char *known_top) {
const unsigned char *p = (const unsigned char *)s;
const unsigned char *t = (const unsigned char *)known_top;
assert(s != NULL && known_top != NULL && known_top <= s);
TRACE(("idn_utf8_findfirstbyte(s=<%s>)\n",
idn__debug_hexstring(s, 8)));
while (p >= t) {
if (!VALID_CONT_BYTE(*p))
break;
p--;
}
if (p < t || UTF8_WIDTH(*p) == 0)
return (NULL);
return ((char *)p);
}