#include "cups-private.h"
#include <limits.h>
#include <time.h>
#ifdef HAVE_ICONV_H
# include <iconv.h>
#endif
#ifdef HAVE_ICONV_H
static _cups_mutex_t map_mutex = _CUPS_MUTEX_INITIALIZER;
static iconv_t map_from_utf8 = (iconv_t)-1;
static iconv_t map_to_utf8 = (iconv_t)-1;
static cups_encoding_t map_encoding = CUPS_AUTO_ENCODING;
#endif
void
_cupsCharmapFlush(void)
{
#ifdef HAVE_ICONV_H
if (map_from_utf8 != (iconv_t)-1)
{
iconv_close(map_from_utf8);
map_from_utf8 = (iconv_t)-1;
}
if (map_to_utf8 != (iconv_t)-1)
{
iconv_close(map_to_utf8);
map_to_utf8 = (iconv_t)-1;
}
map_encoding = CUPS_AUTO_ENCODING;
#endif
}
int
cupsCharsetToUTF8(
cups_utf8_t *dest,
const char *src,
const int maxout,
const cups_encoding_t encoding)
{
cups_utf8_t *destptr;
#ifdef HAVE_ICONV_H
size_t srclen,
outBytesLeft;
#endif
DEBUG_printf(("2cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)",
dest, src, maxout, encoding));
if (!dest || !src || maxout < 1)
{
if (dest)
*dest = '\0';
DEBUG_puts("3cupsCharsetToUTF8: Bad arguments, returning -1");
return (-1);
}
if (encoding == CUPS_UTF8 || encoding <= CUPS_US_ASCII ||
encoding >= CUPS_ENCODING_VBCS_END)
{
strlcpy((char *)dest, src, maxout);
return ((int)strlen((char *)dest));
}
destptr = dest;
if (encoding == CUPS_ISO8859_1)
{
int ch;
cups_utf8_t *destend;
destend = dest + maxout - 2;
while (*src && destptr < destend)
{
ch = *src++ & 255;
if (ch & 128)
{
*destptr++ = 0xc0 | (ch >> 6);
*destptr++ = 0x80 | (ch & 0x3f);
}
else
*destptr++ = ch;
}
*destptr = '\0';
return ((int)(destptr - dest));
}
#ifdef HAVE_ICONV_H
_cupsMutexLock(&map_mutex);
if (map_encoding != encoding)
{
_cupsCharmapFlush();
map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding));
map_encoding = encoding;
}
if (map_to_utf8 != (iconv_t)-1)
{
char *altdestptr = (char *)dest;
srclen = strlen(src);
outBytesLeft = maxout - 1;
iconv(map_to_utf8, (char **)&src, &srclen, &altdestptr, &outBytesLeft);
*altdestptr = '\0';
_cupsMutexUnlock(&map_mutex);
return ((int)(altdestptr - (char *)dest));
}
_cupsMutexUnlock(&map_mutex);
#endif
*destptr = '\0';
return (-1);
}
int
cupsUTF8ToCharset(
char *dest,
const cups_utf8_t *src,
const int maxout,
const cups_encoding_t encoding)
{
char *destptr;
#ifdef HAVE_ICONV_H
size_t srclen,
outBytesLeft;
#endif
if (!dest || !src || maxout < 1)
{
if (dest)
*dest = '\0';
return (-1);
}
if (encoding == CUPS_UTF8 ||
encoding >= CUPS_ENCODING_VBCS_END)
{
strlcpy(dest, (char *)src, maxout);
return ((int)strlen(dest));
}
destptr = dest;
if (encoding == CUPS_ISO8859_1 || encoding <= CUPS_US_ASCII)
{
int ch,
maxch;
char *destend;
maxch = encoding == CUPS_ISO8859_1 ? 256 : 128;
destend = dest + maxout - 1;
while (*src && destptr < destend)
{
ch = *src++;
if ((ch & 0xe0) == 0xc0)
{
ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
if (ch < maxch)
*destptr++ = ch;
else
*destptr++ = '?';
}
else if ((ch & 0xf0) == 0xe0 ||
(ch & 0xf8) == 0xf0)
*destptr++ = '?';
else if (!(ch & 0x80))
*destptr++ = ch;
}
*destptr = '\0';
return ((int)(destptr - dest));
}
#ifdef HAVE_ICONV_H
_cupsMutexLock(&map_mutex);
if (map_encoding != encoding)
{
_cupsCharmapFlush();
map_from_utf8 = iconv_open(_cupsEncodingName(encoding), "UTF-8");
map_to_utf8 = iconv_open("UTF-8", _cupsEncodingName(encoding));
map_encoding = encoding;
}
if (map_from_utf8 != (iconv_t)-1)
{
char *altsrc = (char *)src;
srclen = strlen((char *)src);
outBytesLeft = maxout - 1;
iconv(map_from_utf8, &altsrc, &srclen, &destptr, &outBytesLeft);
*destptr = '\0';
_cupsMutexUnlock(&map_mutex);
return ((int)(destptr - dest));
}
_cupsMutexUnlock(&map_mutex);
#endif
*destptr = '\0';
return (-1);
}
int
cupsUTF8ToUTF32(
cups_utf32_t *dest,
const cups_utf8_t *src,
const int maxout)
{
int i;
cups_utf8_t ch;
cups_utf8_t next;
cups_utf32_t ch32;
DEBUG_printf(("2cupsUTF8ToUTF32(dest=%p, src=\"%s\", maxout=%d)", dest,
src, maxout));
if (dest)
*dest = 0;
if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad arguments)");
return (-1);
}
for (i = maxout - 1; *src && i > 0; i --)
{
ch = *src++;
if (!(ch & 0x80))
{
*dest++ = ch;
DEBUG_printf(("4cupsUTF8ToUTF32: %02x => %08X", src[-1], ch));
continue;
}
else if ((ch & 0xe0) == 0xc0)
{
next = *src++;
if ((next & 0xc0) != 0x80)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
if (ch32 < 0x80)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
*dest++ = ch32;
DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x => %08X",
src[-2], src[-1], (unsigned)ch32));
}
else if ((ch & 0xf0) == 0xe0)
{
next = *src++;
if ((next & 0xc0) != 0x80)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
next = *src++;
if ((next & 0xc0) != 0x80)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
ch32 = (ch32 << 6) | (next & 0x3f);
if (ch32 < 0x800)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
*dest++ = ch32;
DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x => %08X",
src[-3], src[-2], src[-1], (unsigned)ch32));
}
else if ((ch & 0xf8) == 0xf0)
{
next = *src++;
if ((next & 0xc0) != 0x80)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
next = *src++;
if ((next & 0xc0) != 0x80)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
ch32 = (ch32 << 6) | (next & 0x3f);
next = *src++;
if ((next & 0xc0) != 0x80)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
ch32 = (ch32 << 6) | (next & 0x3f);
if (ch32 < 0x10000)
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
*dest++ = ch32;
DEBUG_printf(("4cupsUTF8ToUTF32: %02x %02x %02x %02x => %08X",
src[-4], src[-3], src[-2], src[-1], (unsigned)ch32));
}
else
{
DEBUG_puts("3cupsUTF8ToUTF32: Returning -1 (bad UTF-8 sequence)");
return (-1);
}
if (ch32 >= 0xd800 && ch32 <= 0xdfff)
return (-1);
}
*dest = 0;
DEBUG_printf(("3cupsUTF8ToUTF32: Returning %d characters", maxout - 1 - i));
return (maxout - 1 - i);
}
int
cupsUTF32ToUTF8(
cups_utf8_t *dest,
const cups_utf32_t *src,
const int maxout)
{
cups_utf8_t *start;
int i;
int swap;
cups_utf32_t ch;
DEBUG_printf(("2cupsUTF32ToUTF8(dest=%p, src=%p, maxout=%d)", dest, src,
maxout));
if (dest)
*dest = '\0';
if (!dest || !src || maxout < 1)
{
DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (bad args)");
return (-1);
}
start = dest;
swap = *src == 0xfffe0000;
DEBUG_printf(("4cupsUTF32ToUTF8: swap=%d", swap));
if (*src == 0xfffe0000 || *src == 0xfeff)
src ++;
for (i = maxout - 1; *src && i > 0;)
{
ch = *src++;
if (swap)
ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
if (ch > 0x10ffff)
{
DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (character out of range)");
return (-1);
}
if (ch < 0x80)
{
*dest++ = (cups_utf8_t)ch;
i --;
DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x", (unsigned)ch, dest[-1]));
}
else if (ch < 0x800)
{
if (i < 2)
{
DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 2)");
return (-1);
}
*dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 2;
DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x", (unsigned)ch,
dest[-2], dest[-1]));
}
else if (ch < 0x10000)
{
if (i < 3)
{
DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 3)");
return (-1);
}
*dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f));
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 3;
DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x", (unsigned)ch,
dest[-3], dest[-2], dest[-1]));
}
else
{
if (i < 4)
{
DEBUG_puts("3cupsUTF32ToUTF8: Returning -1 (too long 4)");
return (-1);
}
*dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07));
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 4;
DEBUG_printf(("4cupsUTF32ToUTF8: %08x => %02x %02x %02x %02x",
(unsigned)ch, dest[-4], dest[-3], dest[-2], dest[-1]));
}
}
*dest = '\0';
DEBUG_printf(("3cupsUTF32ToUTF8: Returning %d", (int)(dest - start)));
return ((int)(dest - start));
}