#include <IOKit/IOLib.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/utfconv.h>
#include <kern/host.h>
#include <mach/host_priv.h>
#include <libkern/OSKextLib.h>
#include <libkern/OSKextLibPrivate.h>
#include "hfs_encodings.h"
#include "../core/hfs_macos_defs.h"
lck_grp_t * encodinglst_lck_grp;
lck_grp_attr_t * encodinglst_lck_grp_attr;
lck_attr_t * encodinglst_lck_attr;
SLIST_HEAD(encodinglst, hfs_encoding) hfs_encoding_list = {0};
lck_mtx_t encodinglst_mutex;
struct hfs_encoding {
SLIST_ENTRY(hfs_encoding) link;
int refcount;
int kmod_id;
u_int32_t encoding;
hfs_to_unicode_func_t get_unicode_func;
unicode_to_hfs_func_t get_hfsname_func;
};
void
hfs_converterinit(void)
{
SLIST_INIT(&hfs_encoding_list);
encodinglst_lck_grp_attr= lck_grp_attr_alloc_init();
encodinglst_lck_grp = lck_grp_alloc_init("cnode_hash", encodinglst_lck_grp_attr);
encodinglst_lck_attr = lck_attr_alloc_init();
lck_mtx_init(&encodinglst_mutex, encodinglst_lck_grp, encodinglst_lck_attr);
hfs_addconverter(0, kTextEncodingMacRoman, mac_roman_to_unicode, unicode_to_mac_roman);
SLIST_FIRST(&hfs_encoding_list)->refcount++;
}
void hfs_converterdone(void)
{
if (encodinglst_lck_grp_attr)
lck_grp_attr_free(encodinglst_lck_grp_attr);
if (encodinglst_lck_grp)
lck_grp_free(encodinglst_lck_grp);
if (encodinglst_lck_attr)
lck_attr_free(encodinglst_lck_attr);
}
int
hfs_addconverter(int id, u_int32_t encoding, hfs_to_unicode_func_t get_unicode, unicode_to_hfs_func_t get_hfsname)
{
struct hfs_encoding *encp = (struct hfs_encoding *)IOMalloc(sizeof(*encp));
lck_mtx_lock(&encodinglst_mutex);
encp->link.sle_next = NULL;
encp->refcount = 0;
encp->encoding = encoding;
encp->get_unicode_func = get_unicode;
encp->get_hfsname_func = get_hfsname;
encp->kmod_id = id;
SLIST_INSERT_HEAD(&hfs_encoding_list, encp, link);
lck_mtx_unlock(&encodinglst_mutex);
if (id)
OSKextRetainKextWithLoadTag(id);
return (0);
}
int
hfs_remconverter(int id, u_int32_t encoding)
{
struct hfs_encoding *encp;
lck_mtx_lock(&encodinglst_mutex);
SLIST_FOREACH(encp, &hfs_encoding_list, link) {
if (encp->encoding == encoding && encp->kmod_id == id) {
encp->refcount--;
if (encp->refcount <= 0 && encp->kmod_id != 0) {
SLIST_REMOVE(&hfs_encoding_list, encp, hfs_encoding, link);
lck_mtx_unlock(&encodinglst_mutex);
OSKextReleaseKextWithLoadTag(encp->kmod_id);
IOFree(encp, sizeof(*encp));
return (0);
} else {
lck_mtx_unlock(&encodinglst_mutex);
return (1);
}
break;
}
}
lck_mtx_unlock(&encodinglst_mutex);
return (0);
}
int
hfs_getconverter(u_int32_t encoding, hfs_to_unicode_func_t *get_unicode, unicode_to_hfs_func_t *get_hfsname)
{
struct hfs_encoding *encp;
int found = 0;
lck_mtx_lock(&encodinglst_mutex);
SLIST_FOREACH(encp, &hfs_encoding_list, link) {
if (encp->encoding == encoding) {
found = 1;
*get_unicode = encp->get_unicode_func;
*get_hfsname = encp->get_hfsname_func;
++encp->refcount;
break;
}
}
lck_mtx_unlock(&encodinglst_mutex);
if (!found) {
*get_unicode = NULL;
*get_hfsname = NULL;
return (EINVAL);
}
return (0);
}
int
hfs_relconverter(u_int32_t encoding)
{
struct hfs_encoding *encp;
lck_mtx_lock(&encodinglst_mutex);
SLIST_FOREACH(encp, &hfs_encoding_list, link) {
if (encp->encoding == encoding) {
encp->refcount--;
if (encp->refcount <= 0 && encp->kmod_id != 0) {
SLIST_REMOVE(&hfs_encoding_list, encp, hfs_encoding, link);
lck_mtx_unlock(&encodinglst_mutex);
OSKextReleaseKextWithLoadTag(encp->kmod_id);
IOFree(encp, sizeof(*encp));
return (0);
}
lck_mtx_unlock(&encodinglst_mutex);
return (0);
}
}
lck_mtx_unlock(&encodinglst_mutex);
return (EINVAL);
}
int
mac_roman_to_utf8(const Str31 hfs_str, ByteCount maxDstLen, ByteCount *actualDstLen, unsigned char* dstStr)
{
int error;
UniChar uniStr[MAX_HFS_UNICODE_CHARS];
ItemCount uniCount;
size_t utf8len;
u_int8_t pascal_length = 0;
pascal_length = *((const u_int8_t*)(hfs_str));
if (pascal_length > 31) {
error = EINVAL;
return error;
}
error = mac_roman_to_unicode(hfs_str, uniStr, MAX_HFS_UNICODE_CHARS, &uniCount);
if (uniCount == 0)
error = EINVAL;
if (error == 0) {
error = utf8_encodestr(uniStr, uniCount * sizeof(UniChar), dstStr, &utf8len, maxDstLen , ':', 0);
if (error == ENAMETOOLONG)
*actualDstLen = utf8_encodelen(uniStr, uniCount * sizeof(UniChar), ':', 0);
else
*actualDstLen = utf8len;
}
return error;
}
int
utf8_to_mac_roman(ByteCount srcLen, const unsigned char* srcStr, Str31 dstStr)
{
int error;
UniChar uniStr[MAX_HFS_UNICODE_CHARS];
size_t ucslen;
error = utf8_decodestr(srcStr, srcLen, uniStr, &ucslen, sizeof(uniStr), ':', 0);
if (error == 0)
error = unicode_to_mac_roman(uniStr, ucslen/sizeof(UniChar), dstStr);
return error;
}
static u_int8_t gLatin1Table[] = {
0xCA, 0xC1, 0xA2, 0xA3, 0xDB, 0xB4, '?', 0xA4, 0xAC, 0xA9, 0xBB, 0xC7, 0xC2, '?', 0xA8, 0xF8,
0xA1, 0XB1, '?', '?', 0xAB, 0xB5, 0xA6, 0xe1, 0xFC, '?', 0xBC, 0xC8, '?', '?', '?', 0xC0,
'?', '?', '?', '?', '?', '?', 0xAE, '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', 0xAF, '?', '?', '?', '?', '?', '?', 0xA7,
'?', '?', '?', '?', '?', '?', 0xBE, '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', 0xD6, 0xBF, '?', '?', '?', '?', '?', '?', '?'
};
static u_int8_t gSpaceModsTable[] = {
'?', '?', '?', '?', '?', '?', 0xF6, 0xFF, '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', 0xF9, 0xFA, 0xFB, 0xFE, 0xF7, 0xFD, '?', '?'
};
static u_int8_t gPunctTable[] = {
'?', '?', '?', 0xd0, 0xd1, '?', '?', '?', 0xd4, 0xd5, 0xe2, '?', 0xd2, 0xd3, 0xe3, '?',
0xa0, 0xe0, 0xa5, '?', '?', '?', 0xc9, '?', '?', '?', '?', '?', '?', '?', '?', '?',
0xe4, '?', '?', '?', '?', '?', '?', '?', '?', 0xdc, 0xdd, '?', '?', '?', '?', '?',
'?', '?', '?', '?', 0xda, '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 0xdb, '?', '?', '?'
};
static u_int8_t gMathTable[] = {
'?', '?', 0xb6, '?', '?', '?', 0xc6, '?', '?', '?', '?', '?', '?', '?', '?', 0xb8,
'?', 0xb7, '?', '?', '?', '?', '?', '?', '?', '?', 0xc3, '?', '?', '?', 0xb0, '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 0xba, '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', 0xc5, '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
0xad, '?', '?', '?', 0xb2, 0xb3, '?', '?'
};
static u_int8_t gReverseCombTable[] = {
0xDA, 0x40, 0xDA, 0xDA, 0xDA, 0x56, 0xDA, 0xDA, 0xDA, 0x6C, 0xDA, 0xDA, 0xDA, 0xDA, 0x82, 0x98,
0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xAE, 0xDA, 0xDA, 0xDA, 0xC4, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA,
0xDA, 0x4B, 0xDA, 0xDA, 0xDA, 0x61, 0xDA, 0xDA, 0xDA, 0x77, 0xDA, 0xDA, 0xDA, 0xDA, 0x8D, 0xA3,
0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xB9, 0xDA, 0xDA, 0xDA, 0xCF, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA,
0xCB, 0xE7, 0xE5, 0xCC, '?', '?', '?', '?', 0x80, '?', 0x81,
0x88, 0x87, 0x89, 0x8B, '?', '?', '?', '?', 0x8A, '?', 0x8C,
0xE9, 0x83, 0xE6, '?', '?', '?', '?', '?', 0xE8, '?', '?',
0x8F, 0x8E, 0x90, '?', '?', '?', '?', '?', 0x91, '?', '?',
0xED, 0xEA, 0xEB, '?', '?', '?', '?', '?', 0xEC, '?', '?',
0x93, 0x92, 0x94, '?', '?', '?', '?', '?', 0x95, '?', '?',
'?', '?', '?', 0x84, '?', '?', '?', '?', '?', '?', '?',
'?', '?', '?', 0x96, '?', '?', '?', '?', '?', '?', '?',
0xF1, 0xEE, 0xEF, 0xCD, '?', '?', '?', '?', 0x85, '?', '?',
0x98, 0x97, 0x99, 0x9B, '?', '?', '?', '?', 0x9A, '?', '?',
0xF4, 0xF2, 0xF3, '?', '?', '?', '?', '?', 0x86, '?', '?',
0x9D, 0x9C, 0x9E, '?', '?', '?', '?', '?', 0x9F, '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', 0xD9, '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', 0xD8, '?', '?',
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?'
};
int unicode_to_mac_roman(UniChar *uni_str, u_int32_t unicodeChars, Str31 hfs_str)
{
u_int8_t *p;
const UniChar *u;
UniChar c;
UniChar mask;
u_int16_t inputChars;
u_int16_t pascalChars;
OSErr result = noErr;
u_int8_t lsb;
u_int8_t prevChar;
u_int8_t mc;
mask = (UniChar) 0xFF80;
p = &hfs_str[1];
u = uni_str;
inputChars = unicodeChars;
pascalChars = prevChar = 0;
while (inputChars) {
c = *(u++);
lsb = (u_int8_t) c;
if ( c & mask ) {
mc = '?';
switch (c & 0xFF00) {
case 0x0000:
if (lsb >= 0xA0)
mc = gLatin1Table[lsb - 0xA0];
break;
case 0x0200:
if (lsb >= 0xC0 && lsb <= 0xDF)
mc = gSpaceModsTable[lsb - 0xC0];
break;
case 0x2000:
if (lsb >= 0x10 && lsb <= 0xAF)
mc = gPunctTable[lsb- 0x10];
break;
case 0x2200:
if (lsb < 0x68)
mc = gMathTable[lsb];
break;
case 0x0300:
if (c <= 0x030A) {
if (prevChar >= 'A' && prevChar < 'z') {
mc = gReverseCombTable[gReverseCombTable[prevChar - 0x40] + lsb];
--p;
--pascalChars;
}
} else {
switch (c) {
case 0x0327:
if (prevChar == 'C')
mc = 0x82;
else if (prevChar == 'c')
mc = 0x8D;
else
break;
--p;
--pascalChars;
break;
case 0x03A9: mc = 0xBD; break;
case 0x03C0: mc = 0xB9; break;
}
}
break;
default:
switch (c) {
case 0x0131: mc = 0xf5; break;
case 0x0152: mc = 0xce; break;
case 0x0153: mc = 0xcf; break;
case 0x0192: mc = 0xc4; break;
case 0x2122: mc = 0xaa; break;
case 0x25ca: mc = 0xd7; break;
case 0xf8ff: mc = 0xf0; break;
case 0xfb01: mc = 0xde; break;
case 0xfb02: mc = 0xdf; break;
}
}
if (mc == '?')
result = kTECUsedFallbacksStatus;
prevChar = 0;
lsb = mc;
} else {
prevChar = lsb;
}
if (pascalChars >= 31)
break;
*(p++) = lsb;
++pascalChars;
--inputChars;
}
hfs_str[0] = pascalChars;
if (inputChars > 0)
result = ENAMETOOLONG;
return result;
}
static UniChar gHiBitBaseUnicode[128] = {
0x0041, 0x0041, 0x0043, 0x0045, 0x004e, 0x004f, 0x0055, 0x0061,
0x0061, 0x0061, 0x0061, 0x0061, 0x0061, 0x0063, 0x0065, 0x0065,
0x0065, 0x0065, 0x0069, 0x0069, 0x0069, 0x0069, 0x006e, 0x006f,
0x006f, 0x006f, 0x006f, 0x006f, 0x0075, 0x0075, 0x0075, 0x0075,
0x2020, 0x00b0, 0x00a2, 0x00a3, 0x00a7, 0x2022, 0x00b6, 0x00df,
0x00ae, 0x00a9, 0x2122, 0x00b4, 0x00a8, 0x2260, 0x00c6, 0x00d8,
0x221e, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x00b5, 0x2202, 0x2211,
0x220f, 0x03c0, 0x222b, 0x00aa, 0x00ba, 0x03a9, 0x00e6, 0x00f8,
0x00bf, 0x00a1, 0x00ac, 0x221a, 0x0192, 0x2248, 0x2206, 0x00ab,
0x00bb, 0x2026, 0x00a0, 0x0041, 0x0041, 0x004f, 0x0152, 0x0153,
0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x25ca,
0x0079, 0x0059, 0x2044, 0x20ac, 0x2039, 0x203a, 0xfb01, 0xfb02,
0x2021, 0x00b7, 0x201a, 0x201e, 0x2030, 0x0041, 0x0045, 0x0041,
0x0045, 0x0045, 0x0049, 0x0049, 0x0049, 0x0049, 0x004f, 0x004f,
0xf8ff, 0x004f, 0x0055, 0x0055, 0x0055, 0x0131, 0x02c6, 0x02dc,
0x00af, 0x02d8, 0x02d9, 0x02da, 0x00b8, 0x02dd, 0x02db, 0x02c7
};
static UniChar gHiBitCombUnicode[128] = {
0x0308, 0x030a, 0x0327, 0x0301, 0x0303, 0x0308, 0x0308, 0x0301,
0x0300, 0x0302, 0x0308, 0x0303, 0x030a, 0x0327, 0x0301, 0x0300,
0x0302, 0x0308, 0x0301, 0x0300, 0x0302, 0x0308, 0x0303, 0x0301,
0x0300, 0x0302, 0x0308, 0x0303, 0x0301, 0x0300, 0x0302, 0x0308,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0300, 0x0303, 0x0303, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0308, 0x0308, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0302, 0x0302, 0x0301,
0x0308, 0x0300, 0x0301, 0x0302, 0x0308, 0x0300, 0x0301, 0x0302,
0x0000, 0x0300, 0x0301, 0x0302, 0x0300, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
};
int
mac_roman_to_unicode(const Str31 hfs_str, UniChar *uni_str,
__unused u_int32_t maxCharLen, u_int32_t *unicodeChars)
{
const u_int8_t *p;
UniChar *u;
u_int16_t pascalChars;
u_int8_t c;
p = hfs_str;
u = uni_str;
*unicodeChars = pascalChars = *(p++);
while (pascalChars--) {
c = *(p++);
if ( (int8_t) c >= 0 ) {
*(u++) = (UniChar) c;
} else {
UniChar uc;
c &= 0x7F;
*(u++) = uc = gHiBitBaseUnicode[c];
if ((uc <= (UniChar) 'z') && (uc >= (UniChar) 'A')) {
*(u++) = gHiBitCombUnicode[c];
++(*unicodeChars);
}
}
}
return noErr;
}