]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 A |
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. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | ||
29 | #include <sys/param.h> | |
30 | #include <sys/systm.h> | |
31 | #include <sys/kernel.h> | |
1c79356b A |
32 | #include <sys/malloc.h> |
33 | #include <sys/queue.h> | |
34 | #include <sys/utfconv.h> | |
35 | ||
36 | #include "hfs.h" | |
37 | ||
38 | ||
91447636 A |
39 | lck_grp_t * encodinglst_lck_grp; |
40 | lck_grp_attr_t * encodinglst_lck_grp_attr; | |
41 | lck_attr_t * encodinglst_lck_attr; | |
42 | ||
43 | ||
1c79356b A |
44 | /* hfs encoding converter list */ |
45 | SLIST_HEAD(encodinglst, hfs_encoding) hfs_encoding_list = {0}; | |
91447636 A |
46 | |
47 | lck_mtx_t encodinglst_mutex; | |
48 | ||
1c79356b A |
49 | |
50 | ||
51 | /* hfs encoding converter entry */ | |
52 | struct hfs_encoding { | |
53 | SLIST_ENTRY(hfs_encoding) link; | |
54 | int refcount; | |
55 | int kmod_id; | |
56 | UInt32 encoding; | |
57 | hfs_to_unicode_func_t get_unicode_func; | |
58 | unicode_to_hfs_func_t get_hfsname_func; | |
59 | }; | |
60 | ||
61 | /* XXX We should use an "official" interface! */ | |
62 | extern kern_return_t kmod_destroy(host_priv_t host_priv, kmod_t id); | |
63 | extern struct host realhost; | |
64 | ||
65 | #define MAX_HFS_UNICODE_CHARS (15*5) | |
66 | ||
55e303ae | 67 | int mac_roman_to_unicode(const Str31 hfs_str, UniChar *uni_str, UInt32 maxCharLen, UInt32 *usedCharLen); |
1c79356b A |
68 | |
69 | static int unicode_to_mac_roman(UniChar *uni_str, UInt32 unicodeChars, Str31 hfs_str); | |
70 | ||
71 | ||
72 | void | |
73 | hfs_converterinit(void) | |
74 | { | |
75 | SLIST_INIT(&hfs_encoding_list); | |
91447636 A |
76 | |
77 | encodinglst_lck_grp_attr= lck_grp_attr_alloc_init(); | |
91447636 | 78 | encodinglst_lck_grp = lck_grp_alloc_init("cnode_hash", encodinglst_lck_grp_attr); |
91447636 | 79 | encodinglst_lck_attr = lck_attr_alloc_init(); |
91447636 A |
80 | |
81 | lck_mtx_init(&encodinglst_mutex, encodinglst_lck_grp, encodinglst_lck_attr); | |
1c79356b A |
82 | |
83 | /* | |
84 | * add resident MacRoman converter and take a reference | |
85 | * since its always "loaded". | |
86 | */ | |
87 | hfs_addconverter(0, kTextEncodingMacRoman, mac_roman_to_unicode, unicode_to_mac_roman); | |
88 | SLIST_FIRST(&hfs_encoding_list)->refcount++; | |
89 | } | |
90 | ||
91 | ||
92 | /* | |
93 | * hfs_addconverter - add an HFS encoding converter | |
94 | * | |
95 | * This is called exclusivly by kernel loadable modules | |
96 | * (like HFS_Japanese.kmod) to register hfs encoding | |
97 | * conversion routines. | |
98 | * | |
99 | */ | |
100 | int | |
101 | hfs_addconverter(int id, UInt32 encoding, hfs_to_unicode_func_t get_unicode, unicode_to_hfs_func_t get_hfsname) | |
102 | { | |
103 | struct hfs_encoding *encp; | |
104 | ||
105 | MALLOC(encp, struct hfs_encoding *, sizeof(struct hfs_encoding), M_TEMP, M_WAITOK); | |
106 | ||
91447636 | 107 | lck_mtx_lock(&encodinglst_mutex); |
1c79356b A |
108 | |
109 | encp->link.sle_next = NULL; | |
110 | encp->refcount = 0; | |
111 | encp->encoding = encoding; | |
112 | encp->get_unicode_func = get_unicode; | |
113 | encp->get_hfsname_func = get_hfsname; | |
114 | encp->kmod_id = id; | |
115 | SLIST_INSERT_HEAD(&hfs_encoding_list, encp, link); | |
116 | ||
91447636 | 117 | lck_mtx_unlock(&encodinglst_mutex); |
1c79356b A |
118 | return (0); |
119 | } | |
120 | ||
121 | ||
122 | /* | |
123 | * hfs_remconverter - remove an HFS encoding converter | |
124 | * | |
125 | * Can be called by a kernel loadable module's finalize | |
126 | * routine to remove an encoding converter so that the | |
127 | * module (i.e. the code) can be unloaded. | |
128 | * | |
129 | * However, in the normal case, the removing and unloading | |
130 | * of these converters is done in hfs_relconverter. | |
131 | * The call is initiated from within the kernel during the unmounting of an hfs voulume. | |
132 | */ | |
133 | int | |
134 | hfs_remconverter(int id, UInt32 encoding) | |
135 | { | |
136 | struct hfs_encoding *encp; | |
1c79356b | 137 | |
91447636 | 138 | lck_mtx_lock(&encodinglst_mutex); |
1c79356b A |
139 | SLIST_FOREACH(encp, &hfs_encoding_list, link) { |
140 | if (encp->encoding == encoding && encp->kmod_id == id) { | |
141 | encp->refcount--; | |
142 | ||
143 | /* if converter is no longer in use, release it */ | |
144 | if (encp->refcount <= 0 && encp->kmod_id != 0) { | |
145 | SLIST_REMOVE(&hfs_encoding_list, encp, hfs_encoding, link); | |
91447636 | 146 | lck_mtx_unlock(&encodinglst_mutex); |
1c79356b | 147 | FREE(encp, M_TEMP); |
91447636 | 148 | return (0); |
1c79356b | 149 | } else { |
91447636 A |
150 | lck_mtx_unlock(&encodinglst_mutex); |
151 | return (1); /* busy */ | |
1c79356b A |
152 | } |
153 | break; | |
154 | } | |
155 | } | |
91447636 | 156 | lck_mtx_unlock(&encodinglst_mutex); |
1c79356b | 157 | |
91447636 | 158 | return (0); |
1c79356b A |
159 | } |
160 | ||
161 | ||
162 | /* | |
163 | * hfs_getconverter - get HFS encoding converters | |
164 | * | |
165 | * Normally called during the mounting of an hfs voulume. | |
166 | */ | |
167 | int | |
168 | hfs_getconverter(UInt32 encoding, hfs_to_unicode_func_t *get_unicode, unicode_to_hfs_func_t *get_hfsname) | |
169 | { | |
170 | struct hfs_encoding *encp; | |
171 | int found = 0; | |
172 | ||
91447636 | 173 | lck_mtx_lock(&encodinglst_mutex); |
1c79356b A |
174 | SLIST_FOREACH(encp, &hfs_encoding_list, link) { |
175 | if (encp->encoding == encoding) { | |
176 | found = 1; | |
177 | *get_unicode = encp->get_unicode_func; | |
178 | *get_hfsname = encp->get_hfsname_func; | |
179 | ++encp->refcount; | |
180 | break; | |
181 | } | |
182 | } | |
91447636 | 183 | lck_mtx_unlock(&encodinglst_mutex); |
1c79356b A |
184 | |
185 | if (!found) { | |
186 | *get_unicode = NULL; | |
187 | *get_hfsname = NULL; | |
188 | return (EINVAL); | |
189 | } | |
190 | ||
191 | return (0); | |
192 | } | |
193 | ||
194 | ||
195 | /* | |
196 | * hfs_relconverter - release interest in an HFS encoding converter | |
197 | * | |
198 | * Normally called during the unmounting of an hfs voulume. | |
199 | */ | |
200 | int | |
201 | hfs_relconverter(UInt32 encoding) | |
202 | { | |
203 | struct hfs_encoding *encp; | |
1c79356b | 204 | |
91447636 | 205 | lck_mtx_lock(&encodinglst_mutex); |
1c79356b A |
206 | SLIST_FOREACH(encp, &hfs_encoding_list, link) { |
207 | if (encp->encoding == encoding) { | |
1c79356b A |
208 | encp->refcount--; |
209 | ||
210 | /* if converter is no longer in use, release it */ | |
211 | if (encp->refcount <= 0 && encp->kmod_id != 0) { | |
212 | int id = encp->kmod_id; | |
213 | ||
214 | SLIST_REMOVE(&hfs_encoding_list, encp, hfs_encoding, link); | |
91447636 A |
215 | lck_mtx_unlock(&encodinglst_mutex); |
216 | ||
217 | FREE(encp, M_TEMP); | |
218 | kmod_destroy((host_priv_t) host_priv_self(), id); | |
219 | return (0); | |
1c79356b | 220 | } |
91447636 A |
221 | lck_mtx_unlock(&encodinglst_mutex); |
222 | return (0); | |
1c79356b A |
223 | } |
224 | } | |
91447636 | 225 | lck_mtx_unlock(&encodinglst_mutex); |
1c79356b | 226 | |
91447636 | 227 | return (EINVAL); |
1c79356b A |
228 | } |
229 | ||
230 | ||
231 | /* | |
232 | * Convert HFS encoded string into UTF-8 | |
233 | * | |
234 | * Unicode output is fully decomposed | |
235 | * '/' chars are converted to ':' | |
236 | */ | |
237 | int | |
238 | hfs_to_utf8(ExtendedVCB *vcb, Str31 hfs_str, ByteCount maxDstLen, ByteCount *actualDstLen, unsigned char* dstStr) | |
239 | { | |
240 | int error; | |
241 | UniChar uniStr[MAX_HFS_UNICODE_CHARS]; | |
242 | ItemCount uniCount; | |
243 | size_t utf8len; | |
244 | hfs_to_unicode_func_t hfs_get_unicode = VCBTOHFS(vcb)->hfs_get_unicode; | |
245 | ||
246 | error = hfs_get_unicode(hfs_str, uniStr, MAX_HFS_UNICODE_CHARS, &uniCount); | |
247 | ||
248 | if (uniCount == 0) | |
249 | error = EINVAL; | |
250 | ||
251 | if (error == 0) { | |
252 | error = utf8_encodestr(uniStr, uniCount * sizeof(UniChar), dstStr, &utf8len, maxDstLen , ':', 0); | |
253 | if (error == ENAMETOOLONG) | |
254 | *actualDstLen = utf8_encodelen(uniStr, uniCount * sizeof(UniChar), ':', 0); | |
255 | else | |
256 | *actualDstLen = utf8len; | |
257 | } | |
258 | ||
259 | return error; | |
260 | } | |
261 | ||
262 | ||
263 | /* | |
264 | * When an HFS name cannot be encoded with the current | |
265 | * volume encoding then MacRoman is used as a fallback. | |
266 | */ | |
267 | int | |
268 | mac_roman_to_utf8(Str31 hfs_str, ByteCount maxDstLen, ByteCount *actualDstLen, unsigned char* dstStr) | |
269 | { | |
270 | int error; | |
271 | UniChar uniStr[MAX_HFS_UNICODE_CHARS]; | |
272 | ItemCount uniCount; | |
273 | size_t utf8len; | |
274 | ||
275 | error = mac_roman_to_unicode(hfs_str, uniStr, MAX_HFS_UNICODE_CHARS, &uniCount); | |
276 | ||
277 | if (uniCount == 0) | |
278 | error = EINVAL; | |
279 | ||
280 | if (error == 0) { | |
281 | error = utf8_encodestr(uniStr, uniCount * sizeof(UniChar), dstStr, &utf8len, maxDstLen , ':', 0); | |
282 | if (error == ENAMETOOLONG) | |
283 | *actualDstLen = utf8_encodelen(uniStr, uniCount * sizeof(UniChar), ':', 0); | |
284 | else | |
285 | *actualDstLen = utf8len; | |
286 | } | |
287 | ||
288 | return error; | |
289 | } | |
290 | ||
291 | ||
9bccf70c A |
292 | /* |
293 | * Convert Unicode string into HFS encoding | |
294 | * | |
295 | * ':' chars are converted to '/' | |
296 | * Assumes input represents fully decomposed Unicode | |
297 | */ | |
298 | int | |
299 | unicode_to_hfs(ExtendedVCB *vcb, ByteCount srcLen, u_int16_t* srcStr, Str31 dstStr, int retry) | |
300 | { | |
301 | int error; | |
302 | unicode_to_hfs_func_t hfs_get_hfsname = VCBTOHFS(vcb)->hfs_get_hfsname; | |
303 | ||
304 | error = hfs_get_hfsname(srcStr, srcLen/sizeof(UniChar), dstStr); | |
305 | if (error && retry) { | |
306 | error = unicode_to_mac_roman(srcStr, srcLen/sizeof(UniChar), dstStr); | |
307 | } | |
308 | return error; | |
309 | } | |
310 | ||
1c79356b A |
311 | /* |
312 | * Convert UTF-8 string into HFS encoding | |
313 | * | |
314 | * ':' chars are converted to '/' | |
315 | * Assumes input represents fully decomposed Unicode | |
316 | */ | |
317 | int | |
9bccf70c | 318 | utf8_to_hfs(ExtendedVCB *vcb, ByteCount srcLen, const unsigned char* srcStr, Str31 dstStr/*, int retry*/) |
1c79356b A |
319 | { |
320 | int error; | |
321 | UniChar uniStr[MAX_HFS_UNICODE_CHARS]; | |
322 | size_t ucslen; | |
1c79356b A |
323 | |
324 | error = utf8_decodestr(srcStr, srcLen, uniStr, &ucslen, sizeof(uniStr), ':', 0); | |
325 | if (error == 0) | |
9bccf70c | 326 | error = unicode_to_hfs(vcb, ucslen, uniStr, dstStr, 1); |
1c79356b A |
327 | |
328 | return error; | |
329 | } | |
330 | ||
331 | int | |
332 | utf8_to_mac_roman(ByteCount srcLen, const unsigned char* srcStr, Str31 dstStr) | |
333 | { | |
334 | int error; | |
335 | UniChar uniStr[MAX_HFS_UNICODE_CHARS]; | |
336 | size_t ucslen; | |
337 | ||
338 | error = utf8_decodestr(srcStr, srcLen, uniStr, &ucslen, sizeof(uniStr), ':', 0); | |
339 | if (error == 0) | |
340 | error = unicode_to_mac_roman(uniStr, ucslen/sizeof(UniChar), dstStr); | |
341 | ||
342 | return error; | |
343 | } | |
344 | ||
345 | /* | |
346 | * HFS MacRoman to/from Unicode conversions are built into the kernel | |
347 | * All others hfs encodings are loadable. | |
348 | */ | |
349 | ||
350 | /* 0x00A0 - 0x00FF = Latin 1 Supplement (30 total) */ | |
351 | static UInt8 gLatin1Table[] = { | |
352 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
353 | /* 0x00A0 */ 0xCA, 0xC1, 0xA2, 0xA3, 0xDB, 0xB4, '?', 0xA4, 0xAC, 0xA9, 0xBB, 0xC7, 0xC2, '?', 0xA8, 0xF8, | |
354 | /* 0x00B0 */ 0xA1, 0XB1, '?', '?', 0xAB, 0xB5, 0xA6, 0xe1, 0xFC, '?', 0xBC, 0xC8, '?', '?', '?', 0xC0, | |
355 | /* 0x00C0 */ '?', '?', '?', '?', '?', '?', 0xAE, '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
356 | /* 0x00D0 */ '?', '?', '?', '?', '?', '?', '?', '?', 0xAF, '?', '?', '?', '?', '?', '?', 0xA7, | |
357 | /* 0x00E0 */ '?', '?', '?', '?', '?', '?', 0xBE, '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
358 | /* 0x00F0 */ '?', '?', '?', '?', '?', '?', '?', 0xD6, 0xBF, '?', '?', '?', '?', '?', '?', '?' | |
359 | }; | |
360 | ||
361 | /* 0x02C0 - 0x02DF = Spacing Modifiers (8 total) */ | |
362 | static UInt8 gSpaceModsTable[] = { | |
363 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
364 | /* 0x02C0 */ '?', '?', '?', '?', '?', '?', 0xF6, 0xFF, '?', '?', '?', '?', '?', '?', '?', '?', | |
365 | /* 0x02D0 */ '?', '?', '?', '?', '?', '?', '?', '?', 0xF9, 0xFA, 0xFB, 0xFE, 0xF7, 0xFD, '?', '?' | |
366 | }; | |
367 | ||
368 | /* 0x2010 - 0x20AF = General Punctuation (17 total) */ | |
369 | static UInt8 gPunctTable[] = { | |
370 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
371 | /* 0x2010 */ '?', '?', '?', 0xd0, 0xd1, '?', '?', '?', 0xd4, 0xd5, 0xe2, '?', 0xd2, 0xd3, 0xe3, '?', | |
372 | /* 0x2020 */ 0xa0, 0xe0, 0xa5, '?', '?', '?', 0xc9, '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
373 | /* 0x2030 */ 0xe4, '?', '?', '?', '?', '?', '?', '?', '?', 0xdc, 0xdd, '?', '?', '?', '?', '?', | |
374 | /* 0x2040 */ '?', '?', '?', '?', 0xda, '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
375 | /* 0x2050 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
376 | /* 0x2060 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
377 | /* 0x2070 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
378 | /* 0x2080 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
379 | /* 0x2090 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
380 | /* 0x20A0 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 0xdb, '?', '?', '?' | |
381 | }; | |
382 | ||
383 | /* 0x22xx = Mathematical Operators (11 total) */ | |
384 | static UInt8 gMathTable[] = { | |
385 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
386 | /* 0x2200 */ '?', '?', 0xb6, '?', '?', '?', 0xc6, '?', '?', '?', '?', '?', '?', '?', '?', 0xb8, | |
387 | /* 0x2210 */ '?', 0xb7, '?', '?', '?', '?', '?', '?', '?', '?', 0xc3, '?', '?', '?', 0xb0, '?', | |
388 | /* 0x2220 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 0xba, '?', '?', '?', '?', | |
389 | /* 0x2230 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
390 | /* 0x2240 */ '?', '?', '?', '?', '?', '?', '?', '?', 0xc5, '?', '?', '?', '?', '?', '?', '?', | |
391 | /* 0x2250 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', | |
392 | /* 0x2260 */ 0xad, '?', '?', '?', 0xb2, 0xb3, '?', '?' | |
393 | }; | |
394 | ||
395 | /* */ | |
396 | static UInt8 gReverseCombTable[] = { | |
397 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
398 | /* 0x40 */ 0xDA, 0x40, 0xDA, 0xDA, 0xDA, 0x56, 0xDA, 0xDA, 0xDA, 0x6C, 0xDA, 0xDA, 0xDA, 0xDA, 0x82, 0x98, | |
399 | /* 0x50 */ 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xAE, 0xDA, 0xDA, 0xDA, 0xC4, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, | |
400 | /* 0x60 */ 0xDA, 0x4B, 0xDA, 0xDA, 0xDA, 0x61, 0xDA, 0xDA, 0xDA, 0x77, 0xDA, 0xDA, 0xDA, 0xDA, 0x8D, 0xA3, | |
401 | /* 0x70 */ 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xB9, 0xDA, 0xDA, 0xDA, 0xCF, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, | |
402 | ||
403 | /* Combining Diacritical Marks (0x0300 - 0x030A) */ | |
404 | /* 0 1 2 3 4 5 6 7 8 9 A */ | |
405 | /* 'A' */ | |
406 | /* 0x0300 */ 0xCB, 0xE7, 0xE5, 0xCC, '?', '?', '?', '?', 0x80, '?', 0x81, | |
407 | ||
408 | /* 'a' */ | |
409 | /* 0x0300 */ 0x88, 0x87, 0x89, 0x8B, '?', '?', '?', '?', 0x8A, '?', 0x8C, | |
410 | ||
411 | /* 'E' */ | |
412 | /* 0x0300 */ 0xE9, 0x83, 0xE6, '?', '?', '?', '?', '?', 0xE8, '?', '?', | |
413 | ||
414 | /* 'e' */ | |
415 | /* 0x0300 */ 0x8F, 0x8E, 0x90, '?', '?', '?', '?', '?', 0x91, '?', '?', | |
416 | ||
417 | /* 'I' */ | |
418 | /* 0x0300 */ 0xED, 0xEA, 0xEB, '?', '?', '?', '?', '?', 0xEC, '?', '?', | |
419 | ||
420 | /* 'i' */ | |
421 | /* 0x0300 */ 0x93, 0x92, 0x94, '?', '?', '?', '?', '?', 0x95, '?', '?', | |
422 | ||
423 | /* 'N' */ | |
424 | /* 0x0300 */ '?', '?', '?', 0x84, '?', '?', '?', '?', '?', '?', '?', | |
425 | ||
426 | /* 'n' */ | |
427 | /* 0x0300 */ '?', '?', '?', 0x96, '?', '?', '?', '?', '?', '?', '?', | |
428 | ||
429 | /* 'O' */ | |
430 | /* 0x0300 */ 0xF1, 0xEE, 0xEF, 0xCD, '?', '?', '?', '?', 0x85, '?', '?', | |
431 | ||
432 | /* 'o' */ | |
433 | /* 0x0300 */ 0x98, 0x97, 0x99, 0x9B, '?', '?', '?', '?', 0x9A, '?', '?', | |
434 | ||
435 | /* 'U' */ | |
436 | /* 0x0300 */ 0xF4, 0xF2, 0xF3, '?', '?', '?', '?', '?', 0x86, '?', '?', | |
437 | ||
438 | /* 'u' */ | |
439 | /* 0x0300 */ 0x9D, 0x9C, 0x9E, '?', '?', '?', '?', '?', 0x9F, '?', '?', | |
440 | ||
441 | /* 'Y' */ | |
442 | /* 0x0300 */ '?', '?', '?', '?', '?', '?', '?', '?', 0xD9, '?', '?', | |
443 | ||
444 | /* 'y' */ | |
445 | /* 0x0300 */ '?', '?', '?', '?', '?', '?', '?', '?', 0xD8, '?', '?', | |
446 | ||
447 | /* else */ | |
448 | /* 0x0300 */ '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?' | |
449 | }; | |
450 | ||
451 | ||
452 | /* | |
453 | * Convert Unicode string into HFS MacRoman encoding | |
454 | * | |
455 | * Assumes Unicode input is fully decomposed | |
456 | */ | |
457 | static int unicode_to_mac_roman(UniChar *uni_str, UInt32 unicodeChars, Str31 hfs_str) | |
458 | { | |
459 | UInt8 *p; | |
460 | const UniChar *u; | |
461 | UniChar c; | |
462 | UniChar mask; | |
463 | UInt16 inputChars; | |
464 | UInt16 pascalChars; | |
465 | OSErr result = noErr; | |
466 | UInt8 lsb; | |
467 | UInt8 prevChar; | |
468 | UInt8 mc; | |
469 | ||
470 | mask = (UniChar) 0xFF80; | |
471 | p = &hfs_str[1]; | |
472 | u = uni_str; | |
473 | inputChars = unicodeChars; | |
474 | pascalChars = prevChar = 0; | |
475 | ||
476 | while (inputChars) { | |
477 | c = *(u++); | |
478 | lsb = (UInt8) c; | |
479 | ||
480 | /* | |
481 | * If its not 7-bit ascii, then we need to map it | |
482 | */ | |
483 | if ( c & mask ) { | |
484 | mc = '?'; | |
485 | switch (c & 0xFF00) { | |
486 | case 0x0000: | |
487 | if (lsb >= 0xA0) | |
488 | mc = gLatin1Table[lsb - 0xA0]; | |
489 | break; | |
490 | ||
491 | case 0x0200: | |
492 | if (lsb >= 0xC0 && lsb <= 0xDF) | |
493 | mc = gSpaceModsTable[lsb - 0xC0]; | |
494 | break; | |
495 | ||
496 | case 0x2000: | |
497 | if (lsb >= 0x10 && lsb <= 0xAF) | |
498 | mc = gPunctTable[lsb- 0x10]; | |
499 | break; | |
500 | ||
501 | case 0x2200: | |
502 | if (lsb <= 0x68) | |
503 | mc = gMathTable[lsb]; | |
504 | break; | |
505 | ||
506 | case 0x0300: | |
507 | if (c <= 0x030A) { | |
508 | if (prevChar >= 'A' && prevChar < 'z') { | |
509 | mc = gReverseCombTable[gReverseCombTable[prevChar - 0x40] + lsb]; | |
510 | --p; /* backup over base char */ | |
511 | --pascalChars; | |
512 | } | |
513 | } else { | |
514 | switch (c) { | |
515 | case 0x0327: /* combining cedilla */ | |
516 | if (prevChar == 'C') | |
517 | mc = 0x82; | |
518 | else if (prevChar == 'c') | |
519 | mc = 0x8D; | |
520 | else | |
521 | break; | |
522 | --p; /* backup over base char */ | |
523 | --pascalChars; | |
524 | break; | |
525 | ||
526 | case 0x03A9: mc = 0xBD; break; /* omega */ | |
527 | ||
528 | case 0x03C0: mc = 0xB9; break; /* pi */ | |
529 | } | |
530 | } | |
531 | break; | |
532 | ||
533 | default: | |
534 | switch (c) { | |
535 | case 0x0131: mc = 0xf5; break; /* dotless i */ | |
536 | ||
537 | case 0x0152: mc = 0xce; break; /* OE */ | |
538 | ||
539 | case 0x0153: mc = 0xcf; break; /* oe */ | |
540 | ||
541 |