2008-12-04 22:00:12 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004,2005 by Marcoen Hirschberg
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-12-06 13:27:15 +00:00
|
|
|
*
|
2008-12-04 22:00:12 +00:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
/* Some conversion functions for handling UTF-8
|
2005-12-06 13:27:15 +00:00
|
|
|
*
|
|
|
|
* I got all the info from:
|
|
|
|
* http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
|
|
|
* and
|
|
|
|
* http://en.wikipedia.org/wiki/Unicode
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "file.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "rbunicode.h"
|
2007-04-01 13:31:09 +00:00
|
|
|
#include "config.h"
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2008-11-24 22:16:07 +00:00
|
|
|
#define CODEPAGE_DIR ROCKBOX_DIR"/codepages"
|
2005-12-06 13:27:15 +00:00
|
|
|
static int default_codepage = 0;
|
|
|
|
static int loaded_cp_table = 0;
|
|
|
|
|
2007-04-01 13:09:22 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2007-04-01 13:09:22 +00:00
|
|
|
#define MAX_CP_TABLE_SIZE 32768
|
|
|
|
#define NUM_TABLES 5
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2009-10-25 10:42:21 +00:00
|
|
|
static const char * const filename[NUM_TABLES] =
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
CODEPAGE_DIR"/iso.cp",
|
|
|
|
CODEPAGE_DIR"/932.cp", /* SJIS */
|
|
|
|
CODEPAGE_DIR"/936.cp", /* GB2312 */
|
|
|
|
CODEPAGE_DIR"/949.cp", /* KSX1001 */
|
|
|
|
CODEPAGE_DIR"/950.cp" /* BIG5 */
|
|
|
|
};
|
2008-12-04 22:00:12 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
static const char cp_2_table[NUM_CODEPAGES] =
|
|
|
|
{
|
2007-10-01 17:16:31 +00:00
|
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 0
|
2005-12-06 13:27:15 +00:00
|
|
|
};
|
|
|
|
|
2009-10-25 10:42:21 +00:00
|
|
|
static const char * const name_codepages[NUM_CODEPAGES+1] =
|
2008-12-04 22:00:12 +00:00
|
|
|
{
|
|
|
|
"ISO-8859-1",
|
|
|
|
"ISO-8859-7",
|
|
|
|
"ISO-8859-8",
|
|
|
|
"CP1251",
|
|
|
|
"ISO-8859-11",
|
|
|
|
"CP1256",
|
|
|
|
"ISO-8859-9",
|
|
|
|
"ISO-8859-2",
|
|
|
|
"CP1250",
|
|
|
|
"SJIS",
|
|
|
|
"GB-2312",
|
|
|
|
"KSX-1001",
|
|
|
|
"BIG5",
|
|
|
|
"UTF-8",
|
|
|
|
"unknown"
|
|
|
|
};
|
|
|
|
|
2007-04-01 13:09:22 +00:00
|
|
|
#else /* !HAVE_LCD_BITMAP, reduced support */
|
|
|
|
|
2007-10-02 07:25:24 +00:00
|
|
|
#define MAX_CP_TABLE_SIZE 640
|
2007-04-01 13:09:22 +00:00
|
|
|
#define NUM_TABLES 1
|
|
|
|
|
2009-10-25 10:42:21 +00:00
|
|
|
static const char * const filename[NUM_TABLES] = {
|
2008-12-15 23:42:19 +00:00
|
|
|
CODEPAGE_DIR"/isomini.cp"
|
2007-04-01 13:09:22 +00:00
|
|
|
};
|
2008-12-04 22:00:12 +00:00
|
|
|
|
2007-04-01 13:09:22 +00:00
|
|
|
static const char cp_2_table[NUM_CODEPAGES] =
|
|
|
|
{
|
2007-10-01 17:16:31 +00:00
|
|
|
0, 1, 1, 1, 1, 1, 0
|
2007-04-01 13:09:22 +00:00
|
|
|
};
|
|
|
|
|
2009-10-25 10:42:21 +00:00
|
|
|
static const char * const name_codepages[NUM_CODEPAGES+1] =
|
2008-12-04 22:00:12 +00:00
|
|
|
{
|
|
|
|
"ISO-8859-1",
|
|
|
|
"ISO-8859-7",
|
|
|
|
"CP1251",
|
|
|
|
"ISO-8859-9",
|
|
|
|
"ISO-8859-2",
|
|
|
|
"CP1250",
|
|
|
|
"UTF-8",
|
|
|
|
"unknown"
|
|
|
|
};
|
|
|
|
|
2007-04-01 13:09:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static unsigned short codepage_table[MAX_CP_TABLE_SIZE];
|
|
|
|
|
|
|
|
static const unsigned char utf8comp[6] =
|
|
|
|
{
|
|
|
|
0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
|
|
|
|
};
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Load codepage file into memory */
|
2007-01-19 15:55:11 +00:00
|
|
|
static int load_cp_table(int cp)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2009-10-25 10:42:21 +00:00
|
|
|
int i = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
int table = cp_2_table[cp];
|
|
|
|
int file, tablesize;
|
|
|
|
unsigned char tmp[2];
|
|
|
|
|
2006-03-10 11:13:21 +00:00
|
|
|
if (table == 0 || table == loaded_cp_table)
|
2005-12-06 13:27:15 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
file = open(filename[table-1], O_RDONLY|O_BINARY);
|
|
|
|
|
|
|
|
if (file < 0) {
|
|
|
|
DEBUGF("Can't open codepage file: %s.cp\n", filename[table-1]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-24 16:02:44 +00:00
|
|
|
tablesize = filesize(file) / 2;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
if (tablesize > MAX_CP_TABLE_SIZE) {
|
|
|
|
DEBUGF("Invalid codepage file: %s.cp\n", filename[table-1]);
|
|
|
|
close(file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i < tablesize) {
|
|
|
|
if (!read(file, tmp, 2)) {
|
2005-12-06 15:04:48 +00:00
|
|
|
DEBUGF("Can't read from codepage file: %s.cp\n",
|
|
|
|
filename[table-1]);
|
2005-12-06 13:27:15 +00:00
|
|
|
loaded_cp_table = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
codepage_table[i++] = (tmp[1] << 8) | tmp[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded_cp_table = table;
|
|
|
|
close(file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode a UCS value as UTF-8 and return a pointer after this UTF-8 char. */
|
|
|
|
unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8)
|
|
|
|
{
|
|
|
|
int tail = 0;
|
|
|
|
|
|
|
|
if (ucs > 0x7F)
|
2005-12-07 08:37:14 +00:00
|
|
|
while (ucs >> (5*tail + 6))
|
2005-12-06 13:27:15 +00:00
|
|
|
tail++;
|
|
|
|
|
|
|
|
*utf8++ = (ucs >> (6*tail)) | utf8comp[tail];
|
|
|
|
while (tail--)
|
|
|
|
*utf8++ = ((ucs >> (6*tail)) & (MASK ^ 0xFF)) | COMP;
|
|
|
|
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recode an iso encoded string to UTF-8 */
|
|
|
|
unsigned char* iso_decode(const unsigned char *iso, unsigned char *utf8,
|
|
|
|
int cp, int count)
|
|
|
|
{
|
|
|
|
unsigned short ucs, tmp;
|
|
|
|
|
|
|
|
if (cp == -1) /* use default codepage */
|
2006-03-10 11:13:21 +00:00
|
|
|
cp = default_codepage;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
if (!load_cp_table(cp)) cp = 0;
|
|
|
|
|
|
|
|
while (count--) {
|
2007-04-01 13:09:22 +00:00
|
|
|
if (*iso < 128 || cp == UTF_8) /* Already UTF-8 */
|
2005-12-06 13:27:15 +00:00
|
|
|
*utf8++ = *iso++;
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* cp tells us which codepage to convert from */
|
|
|
|
switch (cp) {
|
2007-04-01 13:09:22 +00:00
|
|
|
case ISO_8859_7: /* Greek */
|
|
|
|
case WIN_1251: /* Cyrillic */
|
|
|
|
case ISO_8859_9: /* Turkish */
|
|
|
|
case ISO_8859_2: /* Latin Extended */
|
2007-10-01 17:16:31 +00:00
|
|
|
case WIN_1250: /* Central European */
|
2007-04-01 13:09:22 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
case ISO_8859_8: /* Hebrew */
|
|
|
|
case ISO_8859_11: /* Thai */
|
|
|
|
case WIN_1256: /* Arabic */
|
|
|
|
#endif
|
2005-12-06 13:27:15 +00:00
|
|
|
tmp = ((cp-1)*128) + (*iso++ - 128);
|
|
|
|
ucs = codepage_table[tmp];
|
|
|
|
break;
|
|
|
|
|
2007-04-01 13:09:22 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
case SJIS: /* Japanese */
|
2005-12-06 13:27:15 +00:00
|
|
|
if (*iso > 0xA0 && *iso < 0xE0) {
|
2006-03-10 11:13:21 +00:00
|
|
|
tmp = *iso++ | (0xA100 - 0x8000);
|
2005-12-06 13:27:15 +00:00
|
|
|
ucs = codepage_table[tmp];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-01 13:09:22 +00:00
|
|
|
case GB_2312: /* Simplified Chinese */
|
|
|
|
case KSX_1001: /* Korean */
|
2007-04-01 13:31:09 +00:00
|
|
|
case BIG_5: /* Traditional Chinese */
|
2005-12-06 13:27:15 +00:00
|
|
|
if (count < 1 || !iso[1]) {
|
|
|
|
ucs = *iso++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we assume all cjk strings are written
|
|
|
|
in big endian order */
|
|
|
|
tmp = *iso++ << 8;
|
|
|
|
tmp |= *iso++;
|
|
|
|
tmp -= 0x8000;
|
|
|
|
ucs = codepage_table[tmp];
|
|
|
|
count--;
|
|
|
|
break;
|
2007-04-01 13:09:22 +00:00
|
|
|
#endif /* HAVE_LCD_BITMAP */
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
ucs = *iso++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-03-10 11:13:21 +00:00
|
|
|
if (ucs == 0) /* unknown char, use replacement char */
|
|
|
|
ucs = 0xfffd;
|
2005-12-06 13:27:15 +00:00
|
|
|
utf8 = utf8encode(ucs, utf8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recode a UTF-16 string with little-endian byte ordering to UTF-8 */
|
2005-12-06 15:04:48 +00:00
|
|
|
unsigned char* utf16LEdecode(const unsigned char *utf16, unsigned char *utf8,
|
2006-07-25 18:12:57 +00:00
|
|
|
int count)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
unsigned long ucs;
|
|
|
|
|
2006-07-25 18:12:57 +00:00
|
|
|
while (count > 0) {
|
2005-12-06 15:04:48 +00:00
|
|
|
/* Check for a surrogate pair */
|
|
|
|
if (utf16[1] >= 0xD8 && utf16[1] < 0xE0) {
|
|
|
|
ucs = 0x10000 + ((utf16[0] << 10) | ((utf16[1] - 0xD8) << 18)
|
|
|
|
| utf16[2] | ((utf16[3] - 0xDC) << 8));
|
2005-12-06 13:27:15 +00:00
|
|
|
utf16 += 4;
|
|
|
|
count -= 2;
|
|
|
|
} else {
|
|
|
|
ucs = (utf16[0] | (utf16[1] << 8));
|
|
|
|
utf16 += 2;
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
utf8 = utf8encode(ucs, utf8);
|
|
|
|
}
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recode a UTF-16 string with big-endian byte ordering to UTF-8 */
|
2005-12-06 15:04:48 +00:00
|
|
|
unsigned char* utf16BEdecode(const unsigned char *utf16, unsigned char *utf8,
|
2006-07-25 18:12:57 +00:00
|
|
|
int count)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
unsigned long ucs;
|
|
|
|
|
2006-07-25 18:12:57 +00:00
|
|
|
while (count > 0) {
|
2005-12-06 13:27:15 +00:00
|
|
|
if (*utf16 >= 0xD8 && *utf16 < 0xE0) { /* Check for a surrogate pair */
|
2005-12-06 15:04:48 +00:00
|
|
|
ucs = 0x10000 + (((utf16[0] - 0xD8) << 18) | (utf16[1] << 10)
|
|
|
|
| ((utf16[2] - 0xDC) << 8) | utf16[3]);
|
2005-12-06 13:27:15 +00:00
|
|
|
utf16 += 4;
|
|
|
|
count -= 2;
|
|
|
|
} else {
|
|
|
|
ucs = (utf16[0] << 8) | utf16[1];
|
|
|
|
utf16 += 2;
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
utf8 = utf8encode(ucs, utf8);
|
|
|
|
}
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
2007-01-19 15:55:11 +00:00
|
|
|
#if 0 /* currently unused */
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Recode any UTF-16 string to UTF-8 */
|
2005-12-06 15:04:48 +00:00
|
|
|
unsigned char* utf16decode(const unsigned char *utf16, unsigned char *utf8,
|
|
|
|
unsigned int count)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
unsigned long ucs;
|
|
|
|
|
|
|
|
ucs = *(utf16++) << 8;
|
|
|
|
ucs |= *(utf16++);
|
|
|
|
|
|
|
|
if (ucs == 0xFEFF) /* Check for BOM */
|
|
|
|
return utf16BEdecode(utf16, utf8, count-1);
|
|
|
|
else if (ucs == 0xFFFE)
|
|
|
|
return utf16LEdecode(utf16, utf8, count-1);
|
|
|
|
else { /* ADDME: Should default be LE or BE? */
|
|
|
|
utf16 -= 2;
|
|
|
|
return utf16BEdecode(utf16, utf8, count);
|
|
|
|
}
|
|
|
|
}
|
2007-01-19 15:55:11 +00:00
|
|
|
#endif
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* Return the number of UTF-8 chars in a string */
|
|
|
|
unsigned long utf8length(const unsigned char *utf8)
|
|
|
|
{
|
|
|
|
unsigned long l = 0;
|
|
|
|
|
|
|
|
while (*utf8 != 0)
|
|
|
|
if ((*utf8++ & MASK) != COMP)
|
|
|
|
l++;
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decode 1 UTF-8 char and return a pointer to the next char. */
|
|
|
|
const unsigned char* utf8decode(const unsigned char *utf8, unsigned short *ucs)
|
|
|
|
{
|
|
|
|
unsigned char c = *utf8++;
|
|
|
|
unsigned long code;
|
|
|
|
int tail = 0;
|
|
|
|
|
|
|
|
if ((c <= 0x7f) || (c >= 0xc2)) {
|
|
|
|
/* Start of new character. */
|
|
|
|
if (c < 0x80) { /* U-00000000 - U-0000007F, 1 byte */
|
|
|
|
code = c;
|
|
|
|
} else if (c < 0xe0) { /* U-00000080 - U-000007FF, 2 bytes */
|
|
|
|
tail = 1;
|
|
|
|
code = c & 0x1f;
|
|
|
|
} else if (c < 0xf0) { /* U-00000800 - U-0000FFFF, 3 bytes */
|
|
|
|
tail = 2;
|
|
|
|
code = c & 0x0f;
|
|
|
|
} else if (c < 0xf5) { /* U-00010000 - U-001FFFFF, 4 bytes */
|
|
|
|
tail = 3;
|
|
|
|
code = c & 0x07;
|
|
|
|
} else {
|
|
|
|
/* Invalid size. */
|
2006-03-10 11:13:21 +00:00
|
|
|
code = 0xfffd;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (tail-- && ((c = *utf8++) != 0)) {
|
|
|
|
if ((c & 0xc0) == 0x80) {
|
|
|
|
/* Valid continuation character. */
|
|
|
|
code = (code << 6) | (c & 0x3f);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Invalid continuation char */
|
2006-03-10 11:13:21 +00:00
|
|
|
code = 0xfffd;
|
2005-12-06 13:27:15 +00:00
|
|
|
utf8--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Invalid UTF-8 char */
|
2006-03-10 11:13:21 +00:00
|
|
|
code = 0xfffd;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
/* currently we don't support chars above U-FFFF */
|
2006-03-10 11:13:21 +00:00
|
|
|
*ucs = (code < 0x10000) ? code : 0xfffd;
|
2005-12-06 13:27:15 +00:00
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_codepage(int cp)
|
|
|
|
{
|
|
|
|
default_codepage = cp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* seek to a given char in a utf8 string and
|
|
|
|
return its start position in the string */
|
|
|
|
int utf8seek(const unsigned char* utf8, int offset)
|
|
|
|
{
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
while (offset--) {
|
|
|
|
pos++;
|
|
|
|
while ((utf8[pos] & MASK) == COMP)
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
2008-12-04 22:00:12 +00:00
|
|
|
|
|
|
|
const char* get_codepage_name(int cp)
|
|
|
|
{
|
|
|
|
if (cp < 0 || cp>= NUM_CODEPAGES)
|
|
|
|
return name_codepages[NUM_CODEPAGES];
|
|
|
|
return name_codepages[cp];
|
|
|
|
}
|