1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/* CFUnicodePrecomposition.c
Copyright (c) 1999-2019, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2019, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Responsibility: Foundation Team
*/
#include <string.h>
#include "CFBase.h"
#include "CFCharacterSet.h"
#include "CFUniChar.h"
#include "CFUnicodePrecomposition.h"
#include "CFInternal.h"
#include "CFUniCharPriv.h"
#include "CFUniCharPrecompositionData.inc.h"
#include "CFUniCharBitmapData.inc.h"
#include "CFUniCharPropertyDatabase.inc.h"
// Canonical Precomposition
static const uint8_t *__CFUniCharNonBaseBitmapForBMP_P = (const uint8_t *)__CFUniCharNonBaseCharacterSetBitmapPlane0; // Adding _P so the symbol name is different from the one in CFUnicodeDecomposition.c
static const uint8_t *__CFUniCharCombiningClassForBMP = (const uint8_t *)__CFUniCharCombiningPriorityTablePlane0;
// Adding _P so the symbol name is different from the one in CFUnicodeDecomposition.c
#define __CFUniCharIsNonBaseCharacter __CFUniCharIsNonBaseCharacter_P
CF_INLINE bool __CFUniCharIsNonBaseCharacter(UTF32Char character) {
return CFUniCharIsMemberOfBitmap(character, (character < 0x10000 ? __CFUniCharNonBaseBitmapForBMP_P : CFUniCharGetBitmapPtrForPlane(kCFUniCharNonBaseCharacterSet, ((character >> 16) & 0xFF))));
}
typedef struct {
UTF16Char _key;
UTF16Char _value;
} __CFUniCharPrecomposeBMPMappings;
static UTF16Char __CFUniCharGetMappedBMPValue(const __CFUniCharPrecomposeBMPMappings *theTable, uint32_t numElem, UTF16Char character) {
const __CFUniCharPrecomposeBMPMappings *p, *q, *divider;
if ((character < theTable[0]._key) || (character > theTable[numElem-1]._key)) {
return 0;
}
p = theTable;
q = p + (numElem-1);
while (p <= q) {
divider = p + ((q - p) >> 1); /* divide by 2 */
if (character < divider->_key) { q = divider - 1; }
else if (character > divider->_key) { p = divider + 1; }
else { return divider->_value; }
}
return 0;
}
typedef struct {
UTF32Char _key;
uint32_t _value;
} __CFUniCharPrecomposeMappings;
static uint32_t __CFUniCharGetMappedValue_P(const __CFUniCharPrecomposeMappings *theTable, uint32_t numElem, UTF32Char character) {
const __CFUniCharPrecomposeMappings *p, *q, *divider;
if ((character < theTable[0]._key) || (character > theTable[numElem-1]._key)) {
return 0;
}
p = theTable;
q = p + (numElem-1);
while (p <= q) {
divider = p + ((q - p) >> 1); /* divide by 2 */
if (character < divider->_key) { q = divider - 1; }
else if (character > divider->_key) { p = divider + 1; }
else { return divider->_value; }
}
return 0;
}
CF_PRIVATE
UTF32Char CFUniCharPrecomposeCharacter(UTF32Char base, UTF32Char combining) {
uint32_t value;
if (!(value = __CFUniCharGetMappedValue_P((const __CFUniCharPrecomposeMappings *)__CFUniCharPrecompSourceTable, __CFUniCharPrecompositionTableLength, combining))) return 0xFFFD;
// We don't have precomposition in non-BMP
if (value & kCFUniCharNonBmpFlag) {
value = __CFUniCharGetMappedValue_P((const __CFUniCharPrecomposeMappings *)((uint32_t *)__CFUniCharNonBMPPrecompDestinationTable + (value & 0xFFFF)), (value >> 16) & 0x7FFF, base);
} else {
value = __CFUniCharGetMappedBMPValue((const __CFUniCharPrecomposeBMPMappings *)((uint32_t *)__CFUniCharBMPPrecompDestinationTable + (value & 0xFFFF)), (value >> 16), base);
}
return (value ? value : 0xFFFD);
}
#define HANGUL_SBASE 0xAC00
#define HANGUL_LBASE 0x1100
#define HANGUL_VBASE 0x1161
#define HANGUL_TBASE 0x11A7
#define HANGUL_SCOUNT 11172
#define HANGUL_LCOUNT 19
#define HANGUL_VCOUNT 21
#define HANGUL_TCOUNT 28
#define HANGUL_NCOUNT (HANGUL_VCOUNT * HANGUL_TCOUNT)
CF_INLINE void __CFUniCharMoveBufferFromEnd0(UTF16Char *convertedChars, CFIndex length, CFIndex delta) {
const UTF16Char *limit = convertedChars;
UTF16Char *dstP;
convertedChars += length;
dstP = convertedChars + delta;
while (convertedChars > limit) *(--dstP) = *(--convertedChars);
}
bool CFUniCharPrecompose(const UTF16Char *characters, CFIndex length, CFIndex *consumedLength, UTF16Char *precomposed, CFIndex maxLength, CFIndex *filledLength) {
UTF32Char currentChar = 0, lastChar = 0, precomposedChar = 0xFFFD;
CFIndex originalLength = length, usedLength = 0;
UTF16Char *currentBase = precomposed;
uint8_t currentClass, lastClass = 0;
bool currentBaseIsBMP = true;
bool isPrecomposed;
while (length > 0) {
currentChar = *(characters++);
--length;
if (CFUniCharIsSurrogateHighCharacter(currentChar) && (length > 0) && CFUniCharIsSurrogateLowCharacter(*characters)) {
currentChar = CFUniCharGetLongCharacterForSurrogatePair(currentChar, *(characters++));
--length;
}
if (lastChar && __CFUniCharIsNonBaseCharacter(currentChar)) {
isPrecomposed = (precomposedChar == 0xFFFD ? false : true);
if (isPrecomposed) lastChar = precomposedChar;
currentClass = (currentChar > 0xFFFF ? CFUniCharGetUnicodeProperty(currentChar, kCFUniCharCombiningProperty) : CFUniCharGetCombiningPropertyForCharacter(currentChar, __CFUniCharCombiningClassForBMP));
if ((lastClass == 0) || (currentClass > lastClass)) {
if ((precomposedChar = CFUniCharPrecomposeCharacter(lastChar, currentChar)) == 0xFFFD) {
if (isPrecomposed) precomposedChar = lastChar;
lastClass = currentClass;
} else {
continue;
}
}
if (currentChar > 0xFFFF) { // Non-BMP
usedLength += 2;
if (usedLength > maxLength) break;
currentChar -= 0x10000;
*(precomposed++) = (UTF16Char)((currentChar >> 10) + 0xD800UL);
*(precomposed++) = (UTF16Char)((currentChar & 0x3FF) + 0xDC00UL);
} else {
++usedLength;
if (usedLength > maxLength) break;
*(precomposed++) = (UTF16Char)currentChar;
}
} else {
if ((currentChar >= HANGUL_LBASE) && (currentChar < (HANGUL_LBASE + 0xFF))) { // Hangul Jamo
int8_t lIndex = currentChar - HANGUL_LBASE;
if ((length > 0) && (0 <= lIndex) && (lIndex <= HANGUL_LCOUNT)) {
int16_t vIndex = *characters - HANGUL_VBASE;
if ((vIndex >= 0) && (vIndex <= HANGUL_VCOUNT)) {
int16_t tIndex = 0;
++characters; --length;
if (length > 0) {
tIndex = *characters - HANGUL_TBASE;
if ((tIndex < 0) || (tIndex > HANGUL_TCOUNT)) {
tIndex = 0;
} else {
++characters; --length;
}
}
currentChar = (lIndex * HANGUL_VCOUNT + vIndex) * HANGUL_TCOUNT + tIndex + HANGUL_SBASE;
}
}
}
if (precomposedChar != 0xFFFD) {
if (currentBaseIsBMP) { // Non-BMP
if (lastChar > 0xFFFF) { // Last char was Non-BMP
--usedLength;
memmove(currentBase + 1, currentBase + 2, (precomposed - (currentBase + 2)) * sizeof(UTF16Char));
}
*(currentBase) = (UTF16Char)precomposedChar;
} else {
if (lastChar < 0x10000) { // Last char was BMP
++usedLength;
if (usedLength > maxLength) break;
__CFUniCharMoveBufferFromEnd0(currentBase + 1, precomposed - (currentBase + 1), 1);
}
precomposedChar -= 0x10000;
*currentBase = (UTF16Char)((precomposedChar >> 10) + 0xD800UL);
*(currentBase + 1) = (UTF16Char)((precomposedChar & 0x3FF) + 0xDC00UL);
}
precomposedChar = 0xFFFD;
}
currentBase = precomposed;
lastChar = currentChar;
lastClass = 0;
if (currentChar > 0xFFFF) { // Non-BMP
usedLength += 2;
if (usedLength > maxLength) break;
currentChar -= 0x10000;
*(precomposed++) = (UTF16Char)((currentChar >> 10) + 0xD800UL);
*(precomposed++) = (UTF16Char)((currentChar & 0x3FF) + 0xDC00UL);
currentBaseIsBMP = false;
} else {
++usedLength;
if (usedLength > maxLength) break;
*(precomposed++) = (UTF16Char)currentChar;
currentBaseIsBMP = true;
}
}
}
if (precomposedChar != 0xFFFD) {
if (currentChar > 0xFFFF) { // Non-BMP
if (lastChar < 0x10000) { // Last char was BMP
++usedLength;
if (usedLength > maxLength) {
if (consumedLength) *consumedLength = originalLength - length;
if (filledLength) *filledLength = usedLength;
return false;
}
__CFUniCharMoveBufferFromEnd0(currentBase + 1, precomposed - (currentBase + 1), 1);
}
precomposedChar -= 0x10000;
*currentBase = (UTF16Char)((precomposedChar >> 10) + 0xD800UL);
*(currentBase + 1) = (UTF16Char)((precomposedChar & 0x3FF) + 0xDC00UL);
} else {
if (lastChar > 0xFFFF) { // Last char was Non-BMP
--usedLength;
memmove(currentBase + 1, currentBase + 2, (precomposed - (currentBase + 2)) * sizeof(UTF16Char));
}
*(currentBase) = (UTF16Char)precomposedChar;
}
}
if (consumedLength) *consumedLength = originalLength - length;
if (filledLength) *filledLength = usedLength;
return true;
}
#undef __CFUniCharIsNonBaseCharacter
#undef HANGUL_SBASE
#undef HANGUL_LBASE
#undef HANGUL_VBASE
#undef HANGUL_TBASE
#undef HANGUL_SCOUNT
#undef HANGUL_LCOUNT
#undef HANGUL_VCOUNT
#undef HANGUL_TCOUNT
#undef HANGUL_NCOUNT
|