4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU Public License.
7 * This file contains routines for converting between the Macintosh
8 * character set and various other encodings. This includes dealing
9 * with ':' vs. '/' as the path-element separator.
11 * Latin-1 translation based on code contributed by Holger Schemel
12 * (aeglos@valinor.owl.de).
14 * The '8-bit', '7-bit ASCII' and '7-bit alphanumeric' encodings are
15 * implementations of the three encodings recommended by Apple in the
16 * document "AppleSingle/AppleDouble Formats: Developer's Note
17 * (9/94)". This document is available from Apple's Technical
18 * Information Library from the World Wide Web server
21 * The 'CAP' encoding is an implementation of the naming scheme used
22 * by the Columbia AppleTalk Package, available for anonymous FTP from
25 * "XXX" in a comment is a note to myself to consider changing something.
27 * In function preconditions the term "valid" applied to a pointer to
28 * a structure means that the pointer is non-NULL and the structure it
29 * points to has all fields initialized to consistent values.
33 #include <linux/hfs_fs_sb.h>
34 #include <linux/hfs_fs_i.h>
35 #include <linux/hfs_fs.h>
37 /*================ File-local variables ================*/
39 /* int->ASCII map for a single hex digit */
40 static char hex
[16] = {'0','1','2','3','4','5','6','7',
41 '8','9','a','b','c','d','e','f'};
43 * Latin-1 to Mac character set map
45 * For the sake of consistency this map is generated from the Mac to
46 * Latin-1 map the first time it is needed. This means there is just
47 * one map to maintain.
49 static unsigned char latin2mac_map
[128]; /* initially all zero */
52 * Mac to Latin-1 map for the upper 128 characters (both have ASCII in
53 * the lower 128 positions)
55 static unsigned char mac2latin_map
[128] = {
56 0xC4, 0xC5, 0xC7, 0xC9, 0xD1, 0xD6, 0xDC, 0xE1,
57 0xE0, 0xE2, 0xE4, 0xE3, 0xE5, 0xE7, 0xE9, 0xE8,
58 0xEA, 0xEB, 0xED, 0xEC, 0xEE, 0xEF, 0xF1, 0xF3,
59 0xF2, 0xF4, 0xF6, 0xF5, 0xFA, 0xF9, 0xFB, 0xFC,
60 0x00, 0xB0, 0xA2, 0xA3, 0xA7, 0xB7, 0xB6, 0xDF,
61 0xAE, 0xA9, 0x00, 0xB4, 0xA8, 0x00, 0xC6, 0xD8,
62 0x00, 0xB1, 0x00, 0x00, 0xA5, 0xB5, 0xF0, 0x00,
63 0x00, 0x00, 0x00, 0xAA, 0xBA, 0x00, 0xE6, 0xF8,
64 0xBF, 0xA1, 0xAC, 0x00, 0x00, 0x00, 0x00, 0xAB,
65 0xBB, 0x00, 0xA0, 0xC0, 0xC3, 0xD5, 0x00, 0x00,
66 0xAD, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7, 0x00,
67 0xFF, 0x00, 0x00, 0xA4, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0xB8, 0x00, 0x00, 0xC2, 0xCA, 0xC1,
69 0xCB, 0xC8, 0xCD, 0xCE, 0xCF, 0xCC, 0xD3, 0xD4,
70 0x00, 0xD2, 0xDA, 0xDB, 0xD9, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
74 /*================ File-local functions ================*/
79 * Given a hexadecimal digit in ASCII, return the integer representation.
81 static inline const unsigned char dehex(char c
) {
82 if ((c
>='0')&&(c
<='9')) {
85 if ((c
>='a')&&(c
<='f')) {
88 if ((c
>='A')&&(c
<='F')) {
94 /*================ Global functions ================*/
99 * Given a 'Pascal String' (a string preceded by a length byte) in
100 * the Macintosh character set produce the corresponding filename using
101 * the Netatalk name-mangling scheme, returning the length of the
102 * mangled filename. Note that the output string is not NULL terminated.
104 * The name-mangling works as follows:
105 * Characters 32-126 (' '-'~') except '/' and any initial '.' are passed
106 * unchanged from input to output. The remaining characters are replaced
107 * by three characters: ':xx' where xx is the hexadecimal representation
108 * of the character, using lowercase 'a' through 'f'.
110 int hfs_mac2nat(char *out
, const struct hfs_name
*in
) {
112 const unsigned char *p
= in
->Name
;
116 /* Special case for .AppleDesktop which in the
117 distant future may be a pseudodirectory. */
118 if (strncmp(".AppleDesktop", p
, len
) == 0) {
125 if ((c
<32) || (c
=='/') || (c
>126) || (!count
&& (c
=='.'))) {
127 *out
++ = hex
[(c
>>4) & 0xf];
128 *out
++ = hex
[c
& 0xf];
141 * Given a 'Pascal String' (a string preceded by a length byte) in
142 * the Macintosh character set produce the corresponding filename using
143 * the CAP name-mangling scheme, returning the length of the mangled
144 * filename. Note that the output string is not NULL terminated.
146 * The name-mangling works as follows:
147 * Characters 32-126 (' '-'~') except '/' are passed unchanged from
148 * input to output. The remaining characters are replaced by three
149 * characters: ':xx' where xx is the hexadecimal representation of the
150 * character, using lowercase 'a' through 'f'.
152 int hfs_mac2cap(char *out
, const struct hfs_name
*in
) {
154 const unsigned char *p
= in
->Name
;
160 if ((c
<32) || (c
=='/') || (c
>126)) {
162 *out
++ = hex
[(c
>>4) & 0xf];
163 *out
++ = hex
[c
& 0xf];
176 * Given a 'Pascal String' (a string preceded by a length byte) in
177 * the Macintosh character set produce the corresponding filename using
178 * the '8-bit' name-mangling scheme, returning the length of the
179 * mangled filename. Note that the output string is not NULL
182 * This is one of the three recommended naming conventions described
183 * in Apple's document "AppleSingle/AppleDouble Formats: Developer's
186 * The name-mangling works as follows:
187 * Characters 0, '%' and '/' are replaced by three characters: '%xx'
188 * where xx is the hexadecimal representation of the character, using
189 * lowercase 'a' through 'f'. All other characters are passed
190 * unchanged from input to output. Note that this format is mainly
191 * implemented for completeness and is rather hard to read.
193 int hfs_mac2eight(char *out
, const struct hfs_name
*in
) {
195 const unsigned char *p
= in
->Name
;
201 if (!c
|| (c
=='/') || (c
=='%')) {
203 *out
++ = hex
[(c
>>4) & 0xf];
204 *out
++ = hex
[c
& 0xf];
217 * Given a 'Pascal String' (a string preceded by a length byte) in
218 * the Macintosh character set produce the corresponding filename using
219 * the '7-bit ASCII' name-mangling scheme, returning the length of the
220 * mangled filename. Note that the output string is not NULL
223 * This is one of the three recommended naming conventions described
224 * in Apple's document "AppleSingle/AppleDouble Formats: Developer's
227 * The name-mangling works as follows:
228 * Characters 0, '%', '/' and 128-255 are replaced by three
229 * characters: '%xx' where xx is the hexadecimal representation of the
230 * character, using lowercase 'a' through 'f'. All other characters
231 * are passed unchanged from input to output. Note that control
232 * characters (including newline) and space are unchanged make reading
233 * these filenames difficult.
235 int hfs_mac2seven(char *out
, const struct hfs_name
*in
) {
237 const unsigned char *p
= in
->Name
;
243 if (!c
|| (c
=='/') || (c
=='%') || (c
&0x80)) {
245 *out
++ = hex
[(c
>>4) & 0xf];
246 *out
++ = hex
[c
& 0xf];
259 * Given a 'Pascal String' (a string preceded by a length byte) in
260 * the Macintosh character set produce the corresponding filename using
261 * the '7-bit alphanumeric' name-mangling scheme, returning the length
262 * of the mangled filename. Note that the output string is not NULL
265 * This is one of the three recommended naming conventions described
266 * in Apple's document "AppleSingle/AppleDouble Formats: Developer's
269 * The name-mangling works as follows:
270 * The characters 'a'-'z', 'A'-'Z', '0'-'9', '_' and the last '.' in
271 * the filename are passed unchanged from input to output. All
272 * remaining characters (including any '.'s other than the last) are
273 * replaced by three characters: '%xx' where xx is the hexadecimal
274 * representation of the character, using lowercase 'a' through 'f'.
276 int hfs_mac2alpha(char *out
, const struct hfs_name
*in
) {
278 const unsigned char *p
= in
->Name
;
281 const unsigned char *lp
; /* last period */
283 /* strrchr() would be good here, but 'in' is not null-terminated */
284 for (lp
=p
+len
-1; (lp
>=p
)&&(*lp
!='.'); --lp
) {}
289 if ((p
==lp
) || ((c
>='0')&&(c
<='9')) || ((c
>='A')&&(c
<='Z')) ||
290 ((c
>='a')&&(c
<='z')) || (c
=='_')) {
295 *out
++ = hex
[(c
>>4) & 0xf];
296 *out
++ = hex
[c
& 0xf];
306 * Given a 'Pascal String' (a string preceded by a length byte) in
307 * the Macintosh character set produce the corresponding filename using
308 * the 'trivial' name-mangling scheme, returning the length of the
309 * mangled filename. Note that the output string is not NULL
312 * The name-mangling works as follows:
313 * The character '/', which is illegal in Linux filenames is replaced
314 * by ':' which never appears in HFS filenames. All other characters
315 * are passed unchanged from input to output.
317 int hfs_mac2triv(char *out
, const struct hfs_name
*in
) {
319 const unsigned char *p
= in
->Name
;
338 * Given a 'Pascal String' (a string preceded by a length byte) in
339 * the Macintosh character set produce the corresponding filename using
340 * the 'Latin-1' name-mangling scheme, returning the length of the
341 * mangled filename. Note that the output string is not NULL
344 * The Macintosh character set and Latin-1 are both extensions of the
345 * ASCII character set. Some, but certainly not all, of the characters
346 * in the Macintosh character set are also in Latin-1 but not with the
347 * same encoding. This name-mangling scheme replaces the characters in
348 * the Macintosh character set that have Latin-1 equivalents by those
349 * equivalents; the characters 32-126, excluding '/' and '%', are
350 * passed unchanged from input to output. The remaining characters
351 * are replaced by three characters: '%xx' where xx is the hexadecimal
352 * representation of the character, using lowercase 'a' through 'f'.
354 * The array mac2latin_map[] indicates the correspondence between the
355 * two character sets. The byte in element x-128 gives the Latin-1
356 * encoding of the character with encoding x in the Macintosh
357 * character set. A value of zero indicates Latin-1 has no
358 * corresponding character.
360 int hfs_mac2latin(char *out
, const struct hfs_name
*in
) {
362 const unsigned char *p
= in
->Name
;
369 if ((c
& 0x80) && mac2latin_map
[c
& 0x7f]) {
370 *out
++ = mac2latin_map
[c
& 0x7f];
372 } else if ((c
>=32) && (c
<=126) && (c
!='/') && (c
!='%')) {
377 *out
++ = hex
[(c
>>4) & 0xf];
378 *out
++ = hex
[c
& 0xf];
388 * Given an ASCII string (not null-terminated) and its length,
389 * generate the corresponding filename in the Macintosh character set
390 * using the 'CAP' name-mangling scheme, returning the length of the
391 * mangled filename. Note that the output string is not NULL
394 * This routine is a inverse to hfs_mac2cap() and hfs_mac2nat().
395 * A ':' not followed by a 2-digit hexadecimal number (or followed
396 * by the codes for NULL or ':') is replaced by a '|'.
398 void hfs_colon2mac(struct hfs_name
*out
, const char *in
, int len
) {
400 unsigned char code
, c
, *count
;
401 unsigned char *p
= out
->Name
;
405 while (len
-- && (*count
< HFS_NAMELEN
)) {
410 } else if ((len
<2) ||
411 ((hi
=dehex(in
[0])) & 0xf0) ||
412 ((lo
=dehex(in
[1])) & 0xf0) ||
413 !(code
= (hi
<< 4) | lo
) ||
427 * Given an ASCII string (not null-terminated) and its length,
428 * generate the corresponding filename in the Macintosh character set
429 * using Apple's three recommended name-mangling schemes, returning
430 * the length of the mangled filename. Note that the output string is
431 * not NULL terminated.
433 * This routine is a inverse to hfs_mac2alpha(), hfs_mac2seven() and
435 * A '%' not followed by a 2-digit hexadecimal number (or followed
436 * by the code for NULL or ':') is unchanged.
437 * A ':' is replaced by a '|'.
439 void hfs_prcnt2mac(struct hfs_name
*out
, const char *in
, int len
) {
441 unsigned char code
, c
, *count
;
442 unsigned char *p
= out
->Name
;
446 while (len
-- && (*count
< HFS_NAMELEN
)) {
453 } else if ((len
<2) ||
454 ((hi
=dehex(in
[0])) & 0xf0) ||
455 ((lo
=dehex(in
[1])) & 0xf0) ||
456 !(code
= (hi
<< 4) | lo
) ||
470 * Given an ASCII string (not null-terminated) and its length,
471 * generate the corresponding filename in the Macintosh character set
472 * using the 'trivial' name-mangling scheme, returning the length of
473 * the mangled filename. Note that the output string is not NULL
476 * This routine is a inverse to hfs_mac2triv().
477 * A ':' is replaced by a '/'.
479 void hfs_triv2mac(struct hfs_name
*out
, const char *in
, int len
) {
480 unsigned char c
, *count
;
481 unsigned char *p
= out
->Name
;
485 while (len
-- && (*count
< HFS_NAMELEN
)) {
499 * Given an Latin-1 string (not null-terminated) and its length,
500 * generate the corresponding filename in the Macintosh character set
501 * using the 'Latin-1' name-mangling scheme, returning the length of
502 * the mangled filename. Note that the output string is not NULL
505 * This routine is a inverse to hfs_latin2cap().
506 * A '%' not followed by a 2-digit hexadecimal number (or followed
507 * by the code for NULL or ':') is unchanged.
508 * A ':' is replaced by a '|'.
510 * Note that the character map is built the first time it is needed.
512 void hfs_latin2mac(struct hfs_name
*out
, const char *in
, int len
)
515 unsigned char code
, c
, *count
;
516 unsigned char *p
= out
->Name
;
517 static int map_initialized
= 0;
519 if (!map_initialized
) {
522 /* build the inverse mapping at run time */
523 for (i
= 0; i
< 128; i
++) {
524 if ((c
= mac2latin_map
[i
])) {
525 latin2mac_map
[(int)c
- 128] = i
+ 128;
533 while (len
-- && (*count
< HFS_NAMELEN
)) {
540 if (c
<128 || !(*p
= latin2mac_map
[c
-128])) {
544 } else if ((len
<2) ||
545 ((hi
=dehex(in
[0])) & 0xf0) ||
546 ((lo
=dehex(in
[1])) & 0xf0) ||
547 !(code
= (hi
<< 4) | lo
) ||