4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU General 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>
36 #include <linux/compiler.h>
38 /*================ File-local variables ================*/
40 /* int->ASCII map for a single hex digit */
41 static char hex[16] = {'0','1','2','3','4','5','6','7',
42 '8','9','a','b','c','d','e','f'};
44 * Latin-1 to Mac character set map
46 * For the sake of consistency this map is generated from the Mac to
47 * Latin-1 map the first time it is needed. This means there is just
48 * one map to maintain.
50 static unsigned char latin2mac_map[128]; /* initially all zero */
53 * Mac to Latin-1 map for the upper 128 characters (both have ASCII in
54 * the lower 128 positions)
56 static unsigned char mac2latin_map[128] = {
57 0xC4, 0xC5, 0xC7, 0xC9, 0xD1, 0xD6, 0xDC, 0xE1,
58 0xE0, 0xE2, 0xE4, 0xE3, 0xE5, 0xE7, 0xE9, 0xE8,
59 0xEA, 0xEB, 0xED, 0xEC, 0xEE, 0xEF, 0xF1, 0xF3,
60 0xF2, 0xF4, 0xF6, 0xF5, 0xFA, 0xF9, 0xFB, 0xFC,
61 0x00, 0xB0, 0xA2, 0xA3, 0xA7, 0xB7, 0xB6, 0xDF,
62 0xAE, 0xA9, 0x00, 0xB4, 0xA8, 0x00, 0xC6, 0xD8,
63 0x00, 0xB1, 0x00, 0x00, 0xA5, 0xB5, 0xF0, 0x00,
64 0x00, 0x00, 0x00, 0xAA, 0xBA, 0x00, 0xE6, 0xF8,
65 0xBF, 0xA1, 0xAC, 0x00, 0x00, 0x00, 0x00, 0xAB,
66 0xBB, 0x00, 0xA0, 0xC0, 0xC3, 0xD5, 0x00, 0x00,
67 0xAD, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7, 0x00,
68 0xFF, 0x00, 0x00, 0xA4, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0xB8, 0x00, 0x00, 0xC2, 0xCA, 0xC1,
70 0xCB, 0xC8, 0xCD, 0xCE, 0xCF, 0xCC, 0xD3, 0xD4,
71 0x00, 0xD2, 0xDA, 0xDB, 0xD9, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
75 /*================ File-local functions ================*/
80 * Given a hexadecimal digit in ASCII, return the integer representation.
82 static inline __attribute_const__ unsigned char dehex(char c) {
83 if ((c>='0')&&(c<='9')) {
86 if ((c>='a')&&(c<='f')) {
89 if ((c>='A')&&(c<='F')) {
95 /*================ Global functions ================*/
100 * Given a 'Pascal String' (a string preceded by a length byte) in
101 * the Macintosh character set produce the corresponding filename using
102 * the Netatalk name-mangling scheme, returning the length of the
103 * mangled filename. Note that the output string is not NULL terminated.
105 * The name-mangling works as follows:
106 * Characters 32-126 (' '-'~') except '/' and any initial '.' are passed
107 * unchanged from input to output. The remaining characters are replaced
108 * by three characters: ':xx' where xx is the hexadecimal representation
109 * of the character, using lowercase 'a' through 'f'.
111 int hfs_mac2nat(char *out, const struct hfs_name *in) {
113 const unsigned char *p = in->Name;
117 /* Special case for .AppleDesktop which in the
118 distant future may be a pseudodirectory. */
119 if (strncmp(".AppleDesktop", p, len) == 0) {
126 if ((c<32) || (c=='/') || (c>126) || (!count && (c=='.'))) {
128 *out++ = hex[(c>>4) & 0xf];
129 *out++ = hex[c & 0xf];
142 * Given a 'Pascal String' (a string preceded by a length byte) in
143 * the Macintosh character set produce the corresponding filename using
144 * the CAP name-mangling scheme, returning the length of the mangled
145 * filename. Note that the output string is not NULL terminated.
147 * The name-mangling works as follows:
148 * Characters 32-126 (' '-'~') except '/' are passed unchanged from
149 * input to output. The remaining characters are replaced by three
150 * characters: ':xx' where xx is the hexadecimal representation of the
151 * character, using lowercase 'a' through 'f'.
153 int hfs_mac2cap(char *out, const struct hfs_name *in) {
155 const unsigned char *p = in->Name;
161 if ((c<32) || (c=='/') || (c>126)) {
163 *out++ = hex[(c>>4) & 0xf];
164 *out++ = hex[c & 0xf];
177 * Given a 'Pascal String' (a string preceded by a length byte) in
178 * the Macintosh character set produce the corresponding filename using
179 * the '8-bit' name-mangling scheme, returning the length of the
180 * mangled filename. Note that the output string is not NULL
183 * This is one of the three recommended naming conventions described
184 * in Apple's document "AppleSingle/AppleDouble Formats: Developer's
187 * The name-mangling works as follows:
188 * Characters 0, '%' and '/' are replaced by three characters: '%xx'
189 * where xx is the hexadecimal representation of the character, using
190 * lowercase 'a' through 'f'. All other characters are passed
191 * unchanged from input to output. Note that this format is mainly
192 * implemented for completeness and is rather hard to read.
194 int hfs_mac2eight(char *out, const struct hfs_name *in) {
196 const unsigned char *p = in->Name;
202 if (!c || (c=='/') || (c=='%')) {
204 *out++ = hex[(c>>4) & 0xf];
205 *out++ = hex[c & 0xf];
218 * Given a 'Pascal String' (a string preceded by a length byte) in
219 * the Macintosh character set produce the corresponding filename using
220 * the '7-bit ASCII' name-mangling scheme, returning the length of the
221 * mangled filename. Note that the output string is not NULL
224 * This is one of the three recommended naming conventions described
225 * in Apple's document "AppleSingle/AppleDouble Formats: Developer's
228 * The name-mangling works as follows:
229 * Characters 0, '%', '/' and 128-255 are replaced by three
230 * characters: '%xx' where xx is the hexadecimal representation of the
231 * character, using lowercase 'a' through 'f'. All other characters
232 * are passed unchanged from input to output. Note that control
233 * characters (including newline) and space are unchanged make reading
234 * these filenames difficult.
236 int hfs_mac2seven(char *out, const struct hfs_name *in) {
238 const unsigned char *p = in->Name;
244 if (!c || (c=='/') || (c=='%') || (c&0x80)) {
246 *out++ = hex[(c>>4) & 0xf];
247 *out++ = hex[c & 0xf];
260 * Given a 'Pascal String' (a string preceded by a length byte) in
261 * the Macintosh character set produce the corresponding filename using
262 * the '7-bit alphanumeric' name-mangling scheme, returning the length
263 * of the mangled filename. Note that the output string is not NULL
266 * This is one of the three recommended naming conventions described
267 * in Apple's document "AppleSingle/AppleDouble Formats: Developer's
270 * The name-mangling works as follows:
271 * The characters 'a'-'z', 'A'-'Z', '0'-'9', '_' and the last '.' in
272 * the filename are passed unchanged from input to output. All
273 * remaining characters (including any '.'s other than the last) are
274 * replaced by three characters: '%xx' where xx is the hexadecimal
275 * representation of the character, using lowercase 'a' through 'f'.
277 int hfs_mac2alpha(char *out, const struct hfs_name *in) {
279 const unsigned char *p = in->Name;
282 const unsigned char *lp; /* last period */
284 /* strrchr() would be good here, but 'in' is not null-terminated */
285 for (lp=p+len-1; (lp>=p)&&(*lp!='.'); --lp) {}
290 if ((p==lp) || ((c>='0')&&(c<='9')) || ((c>='A')&&(c<='Z')) ||
291 ((c>='a')&&(c<='z')) || (c=='_')) {
296 *out++ = hex[(c>>4) & 0xf];
297 *out++ = hex[c & 0xf];
307 * Given a 'Pascal String' (a string preceded by a length byte) in
308 * the Macintosh character set produce the corresponding filename using
309 * the 'trivial' name-mangling scheme, returning the length of the
310 * mangled filename. Note that the output string is not NULL
313 * The name-mangling works as follows:
314 * The character '/', which is illegal in Linux filenames is replaced
315 * by ':' which never appears in HFS filenames. All other characters
316 * are passed unchanged from input to output.
318 int hfs_mac2triv(char *out, const struct hfs_name *in) {
320 const unsigned char *p = in->Name;
339 * Given a 'Pascal String' (a string preceded by a length byte) in
340 * the Macintosh character set produce the corresponding filename using
341 * the 'Latin-1' name-mangling scheme, returning the length of the
342 * mangled filename. Note that the output string is not NULL
345 * The Macintosh character set and Latin-1 are both extensions of the
346 * ASCII character set. Some, but certainly not all, of the characters
347 * in the Macintosh character set are also in Latin-1 but not with the
348 * same encoding. This name-mangling scheme replaces the characters in
349 * the Macintosh character set that have Latin-1 equivalents by those
350 * equivalents; the characters 32-126, excluding '/' and '%', are
351 * passed unchanged from input to output. The remaining characters
352 * are replaced by three characters: '%xx' where xx is the hexadecimal
353 * representation of the character, using lowercase 'a' through 'f'.
355 * The array mac2latin_map[] indicates the correspondence between the
356 * two character sets. The byte in element x-128 gives the Latin-1
357 * encoding of the character with encoding x in the Macintosh
358 * character set. A value of zero indicates Latin-1 has no
359 * corresponding character.
361 int hfs_mac2latin(char *out, const struct hfs_name *in) {
363 const unsigned char *p = in->Name;
370 if ((c & 0x80) && mac2latin_map[c & 0x7f]) {
371 *out++ = mac2latin_map[c & 0x7f];
373 } else if ((c>=32) && (c<=126) && (c!='/') && (c!='%')) {
378 *out++ = hex[(c>>4) & 0xf];
379 *out++ = hex[c & 0xf];
389 * Given an ASCII string (not null-terminated) and its length,
390 * generate the corresponding filename in the Macintosh character set
391 * using the 'CAP' name-mangling scheme, returning the length of the
392 * mangled filename. Note that the output string is not NULL
395 * This routine is a inverse to hfs_mac2cap() and hfs_mac2nat().
396 * A ':' not followed by a 2-digit hexadecimal number (or followed
397 * by the codes for NULL or ':') is replaced by a '|'.
399 void hfs_colon2mac(struct hfs_name *out, const char *in, int len) {
401 unsigned char code, c, *count;
402 unsigned char *p = out->Name;
406 while (len-- && (*count < HFS_NAMELEN)) {
411 } else if ((len<2) ||
412 ((hi=dehex(in[0])) & 0xf0) ||
413 ((lo=dehex(in[1])) & 0xf0) ||
414 !(code = (hi << 4) | lo) ||
428 * Given an ASCII string (not null-terminated) and its length,
429 * generate the corresponding filename in the Macintosh character set
430 * using Apple's three recommended name-mangling schemes, returning
431 * the length of the mangled filename. Note that the output string is
432 * not NULL terminated.
434 * This routine is a inverse to hfs_mac2alpha(), hfs_mac2seven() and
436 * A '%' not followed by a 2-digit hexadecimal number (or followed
437 * by the code for NULL or ':') is unchanged.
438 * A ':' is replaced by a '|'.
440 void hfs_prcnt2mac(struct hfs_name *out, const char *in, int len) {
442 unsigned char code, c, *count;
443 unsigned char *p = out->Name;
447 while (len-- && (*count < HFS_NAMELEN)) {
454 } else if ((len<2) ||
455 ((hi=dehex(in[0])) & 0xf0) ||
456 ((lo=dehex(in[1])) & 0xf0) ||
457 !(code = (hi << 4) | lo) ||
471 * Given an ASCII string (not null-terminated) and its length,
472 * generate the corresponding filename in the Macintosh character set
473 * using the 'trivial' name-mangling scheme, returning the length of
474 * the mangled filename. Note that the output string is not NULL
477 * This routine is a inverse to hfs_mac2triv().
478 * A ':' is replaced by a '/'.
480 void hfs_triv2mac(struct hfs_name *out, const char *in, int len) {
481 unsigned char c, *count;
482 unsigned char *p = out->Name;
486 while (len-- && (*count < HFS_NAMELEN)) {
500 * Given an Latin-1 string (not null-terminated) and its length,
501 * generate the corresponding filename in the Macintosh character set
502 * using the 'Latin-1' name-mangling scheme, returning the length of
503 * the mangled filename. Note that the output string is not NULL
506 * This routine is a inverse to hfs_latin2cap().
507 * A '%' not followed by a 2-digit hexadecimal number (or followed
508 * by the code for NULL or ':') is unchanged.
509 * A ':' is replaced by a '|'.
511 * Note that the character map is built the first time it is needed.
513 void hfs_latin2mac(struct hfs_name *out, const char *in, int len)
516 unsigned char code, c, *count;
517 unsigned char *p = out->Name;
518 static int map_initialized;
520 if (!map_initialized) {
523 /* build the inverse mapping at run time */
524 for (i = 0; i < 128; i++) {
525 if ((c = mac2latin_map[i])) {
526 latin2mac_map[(int)c - 128] = i + 128;
534 while (len-- && (*count < HFS_NAMELEN)) {
541 if (c<128 || !(*p = latin2mac_map[c-128])) {
545 } else if ((len<2) ||
546 ((hi=dehex(in[0])) & 0xf0) ||
547 ((lo=dehex(in[1])) & 0xf0) ||
548 !(code = (hi << 4) | lo) ||