]> git.saurik.com Git - apple/cf.git/blob - CFStringEncodingConverterExt.h
CF-550.tar.gz
[apple/cf.git] / CFStringEncodingConverterExt.h
1 /*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* CFStringEncodingConverterExt.h
24 Copyright (c) 1998-2009, Apple Inc. All rights reserved.
25 */
26
27 #if !defined(__COREFOUNDATION_CFSTRINGENCODINGCONVERETEREXT__)
28 #define __COREFOUNDATION_CFSTRINGENCODINGCONVERETEREXT__ 1
29
30 #include <CoreFoundation/CFStringEncodingConverter.h>
31
32 CF_EXTERN_C_BEGIN
33
34 #define MAX_DECOMPOSED_LENGTH (10)
35
36 enum {
37 kCFStringEncodingConverterStandard = 0,
38 kCFStringEncodingConverterCheapEightBit = 1,
39 kCFStringEncodingConverterStandardEightBit = 2,
40 kCFStringEncodingConverterCheapMultiByte = 3,
41 kCFStringEncodingConverterPlatformSpecific = 4, // Other fields are ignored
42 kCFStringEncodingConverterICU = 5 // Other fields are ignored
43 };
44
45 /* kCFStringEncodingConverterStandard */
46 typedef CFIndex (*CFStringEncodingToBytesProc)(uint32_t flags, const UniChar *characters, CFIndex numChars, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen);
47 typedef CFIndex (*CFStringEncodingToUnicodeProc)(uint32_t flags, const uint8_t *bytes, CFIndex numBytes, UniChar *characters, CFIndex maxCharLen, CFIndex *usedCharLen);
48 /* kCFStringEncodingConverterCheapEightBit */
49 typedef bool (*CFStringEncodingCheapEightBitToBytesProc)(uint32_t flags, UniChar character, uint8_t *byte);
50 typedef bool (*CFStringEncodingCheapEightBitToUnicodeProc)(uint32_t flags, uint8_t byte, UniChar *character);
51 /* kCFStringEncodingConverterStandardEightBit */
52 typedef uint16_t (*CFStringEncodingStandardEightBitToBytesProc)(uint32_t flags, const UniChar *characters, CFIndex numChars, uint8_t *byte);
53 typedef uint16_t (*CFStringEncodingStandardEightBitToUnicodeProc)(uint32_t flags, uint8_t byte, UniChar *characters);
54 /* kCFStringEncodingConverterCheapMultiByte */
55 typedef uint16_t (*CFStringEncodingCheapMultiByteToBytesProc)(uint32_t flags, UniChar character, uint8_t *bytes);
56 typedef uint16_t (*CFStringEncodingCheapMultiByteToUnicodeProc)(uint32_t flags, const uint8_t *bytes, CFIndex numBytes, UniChar *character);
57
58 typedef CFIndex (*CFStringEncodingToBytesLenProc)(uint32_t flags, const UniChar *characters, CFIndex numChars);
59 typedef CFIndex (*CFStringEncodingToUnicodeLenProc)(uint32_t flags, const uint8_t *bytes, CFIndex numBytes);
60
61 typedef CFIndex (*CFStringEncodingToBytesPrecomposeProc)(uint32_t flags, const UniChar *character, CFIndex numChars, uint8_t *bytes, CFIndex maxByteLen, CFIndex *usedByteLen);
62 typedef bool (*CFStringEncodingIsValidCombiningCharacterProc)(UniChar character);
63
64 typedef struct {
65 void *toBytes;
66 void *toUnicode;
67 uint16_t maxBytesPerChar;
68 uint16_t maxDecomposedCharLen;
69 uint8_t encodingClass;
70 uint32_t :24;
71 CFStringEncodingToBytesLenProc toBytesLen;
72 CFStringEncodingToUnicodeLenProc toUnicodeLen;
73 CFStringEncodingToBytesFallbackProc toBytesFallback;
74 CFStringEncodingToUnicodeFallbackProc toUnicodeFallback;
75 CFStringEncodingToBytesPrecomposeProc toBytesPrecompose;
76 CFStringEncodingIsValidCombiningCharacterProc isValidCombiningChar;
77 } CFStringEncodingConverter;
78
79 extern const CFStringEncodingConverter *CFStringEncodingGetConverter(uint32_t encoding);
80
81 enum {
82 kCFStringEncodingGetConverterSelector = 0,
83 kCFStringEncodingIsDecomposableCharacterSelector = 1,
84 kCFStringEncodingDecomposeCharacterSelector = 2,
85 kCFStringEncodingIsValidLatin1CombiningCharacterSelector = 3,
86 kCFStringEncodingPrecomposeLatin1CharacterSelector = 4
87 };
88
89 extern const void *CFStringEncodingGetAddressForSelector(uint32_t selector);
90
91 #define BOOTSTRAPFUNC_NAME CFStringEncodingBootstrap
92 typedef const CFStringEncodingConverter* (*CFStringEncodingBootstrapProc)(uint32_t encoding, const void *getSelector);
93
94 /* Latin precomposition */
95 /* This function does not precompose recursively nor to U+01E0 and U+01E1.
96 */
97 extern bool CFStringEncodingIsValidCombiningCharacterForLatin1(UniChar character);
98 extern UniChar CFStringEncodingPrecomposeLatinCharacter(const UniChar *character, CFIndex numChars, CFIndex *usedChars);
99
100 /* Convenience functions for converter development */
101 typedef struct _CFStringEncodingUnicodeTo8BitCharMap {
102 UniChar _u;
103 uint8_t _c;
104 uint8_t :8;
105 } CFStringEncodingUnicodeTo8BitCharMap;
106
107 /* Binary searches CFStringEncodingUnicodeTo8BitCharMap */
108 CF_INLINE bool CFStringEncodingUnicodeTo8BitEncoding(const CFStringEncodingUnicodeTo8BitCharMap *theTable, CFIndex numElem, UniChar character, uint8_t *ch) {
109 const CFStringEncodingUnicodeTo8BitCharMap *p, *q, *divider;
110
111 if ((character < theTable[0]._u) || (character > theTable[numElem-1]._u)) {
112 return 0;
113 }
114 p = theTable;
115 q = p + (numElem-1);
116 while (p <= q) {
117 divider = p + ((q - p) >> 1); /* divide by 2 */
118 if (character < divider->_u) { q = divider - 1; }
119 else if (character > divider->_u) { p = divider + 1; }
120 else { *ch = divider->_c; return 1; }
121 }
122 return 0;
123 }
124
125
126 CF_EXTERN_C_END
127
128 #endif /* ! __COREFOUNDATION_CFSTRINGENCODINGCONVERETEREXT__ */
129