X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/0b4e3aa066abc0728aacb4bbeb86f53f9737156e..143464d58d2bd6378e74eec636961ceb0d32fb91:/bsd/hfs/hfs_encodinghint.c?ds=sidebyside diff --git a/bsd/hfs/hfs_encodinghint.c b/bsd/hfs/hfs_encodinghint.c index 776f2b361..232944bd9 100644 --- a/bsd/hfs/hfs_encodinghint.c +++ b/bsd/hfs/hfs_encodinghint.c @@ -1,27 +1,35 @@ /* - * Copyright (c) 2001 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2001-2013 Apple Computer, Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. * - * @APPLE_LICENSE_HEADER_END@ + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ +#if HFS #include #include +#include /* CJK Mac Encoding Bits */ @@ -31,13 +39,19 @@ #define CJK_CHINESE_SIMP 0x8 #define CJK_ALL 0xF -#define CJK_CHINESE (CJK_CHINESE_TRAD | CJK_CHINESE_SIMP) -#define CJK_KATAKANA (CJK_JAPAN | CJK_CHINESE_SIMP | CJK_KOREAN) +#define CJK_CHINESE (CJK_CHINESE_TRAD | CJK_CHINESE_SIMP) +#define CJK_KATAKANA (CJK_JAPAN) -/* Remeber the las unique CJK bit */ +/* Remember the last unique CJK bit */ u_int8_t cjk_lastunique = 0; +/* Encoding bias */ +u_int32_t hfs_encodingbias = 0; +int hfs_islatinbias = 0; + +extern lck_mtx_t encodinglst_mutex; + /* Map CJK bits to Mac encoding */ u_int8_t cjk_encoding[] = { @@ -59,6 +73,9 @@ u_int8_t cjk_encoding[] = { /* 1111 */ kTextEncodingMacJapanese }; + +#if CONFIG_HFS_STD + /* * CJK Mac Encoding Bitmap * @@ -722,6 +739,7 @@ static u_int8_t cjk_bitmap[] = { 0x0D,0x04,0x04,0x00,0x00,0x00,0x00,0x00, }; + /* * Pick a suitable Mac encoding value for a Unicode string. * @@ -787,6 +805,14 @@ hfs_pickencoding(const u_int16_t *src, int len) cjkstate = CJK_ALL; continue; } + if (hfs_islatinbias && ch >= 0x0300 && ch <= 0x0329) { + guess = hfs_encodingbias; + continue; + } + if (ch <= 0x03CE && ch >= 0x0384) { + guess = kTextEncodingMacGreek; + continue; + } if (ch <= 0x0491 && ch >= 0x0401) { guess = kTextEncodingMacCyrillic; continue; @@ -800,6 +826,35 @@ hfs_pickencoding(const u_int16_t *src, int len) if (ch >= 0x0E00 && ch <= 0x0E5B) { return kTextEncodingMacThai; } + /* Catch a few Shift-JIS strays */ + if (guess == 0 || guess == kTextEncodingMacUnicode) { + if (ch == 0x2010 || ch == 0x2014 || ch == 0x2015 || ch == 0x2016) { + guess = kTextEncodingMacJapanese; + if ((cjkstate == 0) || (cjkstate & CJK_JAPAN)) + cjkstate = CJK_JAPAN; + else + cjkstate |= CJK_JAPAN; + continue; + } + if ((hfs_encodingbias == kTextEncodingMacJapanese) && + (ch == 0x00A2 || ch == 0x00A3 || ch == 0x00AC)) { + guess = kTextEncodingMacJapanese; + continue; + } + /* TM char depends on the Mac encoding used. */ + if (ch == 0x2122) { + switch(hfs_encodingbias) { + case kTextEncodingMacJapanese: + case kTextEncodingMacChineseTrad: + case kTextEncodingMacKorean: + case kTextEncodingMacGreek: + case kTextEncodingMacThai: + case kTextEncodingMacChineseSimp: + guess = hfs_encodingbias; + break; + } + } + } if (guess == 0 && ch > 0x2122) { guess = kTextEncodingMacUnicode; } @@ -808,7 +863,33 @@ hfs_pickencoding(const u_int16_t *src, int len) if (cjkstate) { if (powerof2(cjkstate)) { cjk_lastunique = cjkstate; - } else if (cjk_lastunique) { + return ((u_int32_t)cjk_encoding[cjkstate]); + } + if (hfs_encodingbias != 0) { + switch(hfs_encodingbias) { + case kTextEncodingMacJapanese: + if (cjkstate & CJK_JAPAN) + return (kTextEncodingMacJapanese); + break; + case kTextEncodingMacKorean: + if (cjkstate & CJK_KOREAN) + return (kTextEncodingMacKorean); + break; + case kTextEncodingMacChineseTrad: + if (cjkstate & CJK_CHINESE_TRAD) + return (kTextEncodingMacChineseTrad); + if (cjkstate & CJK_CHINESE_SIMP) + return (kTextEncodingMacChineseSimp); + break; + case kTextEncodingMacChineseSimp: + if (cjkstate & CJK_CHINESE_SIMP) + return (kTextEncodingMacChineseSimp); + if (cjkstate & CJK_CHINESE_TRAD) + return (kTextEncodingMacChineseTrad); + break; + } + } + if (cjk_lastunique) { if (cjkstate & cjk_lastunique) cjkstate = cjk_lastunique; else @@ -820,4 +901,61 @@ hfs_pickencoding(const u_int16_t *src, int len) return guess; } +#else /* HFS standard *NOT* supported */ + +u_int32_t +hfs_pickencoding(__unused const u_int16_t *src, __unused int len) { + /* Just return kTextEncodingMacRoman if HFS standard is not supported. */ + return kTextEncodingMacRoman; +} + +#endif /* CONFIG_HFS_STD */ + + +__private_extern__ +u_int32_t +hfs_getencodingbias(void) +{ + return (hfs_encodingbias); +} + + +__private_extern__ +void +hfs_setencodingbias(u_int32_t bias) +{ + lck_mtx_lock(&encodinglst_mutex); + + hfs_encodingbias = bias; + + switch (bias) { + case kTextEncodingMacRoman: + case kTextEncodingMacCentralEurRoman: + case kTextEncodingMacTurkish: + case kTextEncodingMacCroatian: + case kTextEncodingMacIcelandic: + case kTextEncodingMacRomanian: + hfs_islatinbias = 1; + break; + default: + hfs_islatinbias = 0; + break; + } + + lck_mtx_unlock(&encodinglst_mutex); +} + +#else /* not HFS - temp workaround until 4277828 is fixed */ +/* stubs for exported routines that aren't present when we build kernel without HFS */ + +#include + +u_int32_t hfs_pickencoding(u_int16_t *src, int len); + +u_int32_t hfs_pickencoding(__unused u_int16_t *src, __unused int len) +{ + return(0); +} + +#endif /* HFS */