📄 trans.c
字号:
int count = 0; const unsigned char *lp; /* last period */ /* strrchr() would be good here, but 'in' is not null-terminated */ for (lp=p+len-1; (lp>=p)&&(*lp!='.'); --lp) {} ++lp; while (len--) { c = *p++; if ((p==lp) || ((c>='0')&&(c<='9')) || ((c>='A')&&(c<='Z')) || ((c>='a')&&(c<='z')) || (c=='_')) { *out++ = c; count++; } else { *out++ = '%'; *out++ = hex[(c>>4) & 0xf]; *out++ = hex[c & 0xf]; count += 3; } } return count;}/* * hfs_mac2triv() * * Given a 'Pascal String' (a string preceded by a length byte) in * the Macintosh character set produce the corresponding filename using * the 'trivial' name-mangling scheme, returning the length of the * mangled filename. Note that the output string is not NULL * terminated. * * The name-mangling works as follows: * The character '/', which is illegal in Linux filenames is replaced * by ':' which never appears in HFS filenames. All other characters * are passed unchanged from input to output. */int hfs_mac2triv(char *out, const struct hfs_name *in) { unsigned char c; const unsigned char *p = in->Name; int len = in->Len; int count = 0; while (len--) { c = *p++; if (c=='/') { *out++ = ':'; } else { *out++ = c; } count++; } return count;}/* * hfs_mac2latin() * * Given a 'Pascal String' (a string preceded by a length byte) in * the Macintosh character set produce the corresponding filename using * the 'Latin-1' name-mangling scheme, returning the length of the * mangled filename. Note that the output string is not NULL * terminated. * * The Macintosh character set and Latin-1 are both extensions of the * ASCII character set. Some, but certainly not all, of the characters * in the Macintosh character set are also in Latin-1 but not with the * same encoding. This name-mangling scheme replaces the characters in * the Macintosh character set that have Latin-1 equivalents by those * equivalents; the characters 32-126, excluding '/' and '%', are * passed unchanged from input to output. The remaining characters * are replaced by three characters: '%xx' where xx is the hexadecimal * representation of the character, using lowercase 'a' through 'f'. * * The array mac2latin_map[] indicates the correspondence between the * two character sets. The byte in element x-128 gives the Latin-1 * encoding of the character with encoding x in the Macintosh * character set. A value of zero indicates Latin-1 has no * corresponding character. */int hfs_mac2latin(char *out, const struct hfs_name *in) { unsigned char c; const unsigned char *p = in->Name; int len = in->Len; int count = 0; while (len--) { c = *p++; if ((c & 0x80) && mac2latin_map[c & 0x7f]) { *out++ = mac2latin_map[c & 0x7f]; count++; } else if ((c>=32) && (c<=126) && (c!='/') && (c!='%')) { *out++ = c; count++; } else { *out++ = '%'; *out++ = hex[(c>>4) & 0xf]; *out++ = hex[c & 0xf]; count += 3; } } return count;}/* * hfs_colon2mac() * * Given an ASCII string (not null-terminated) and its length, * generate the corresponding filename in the Macintosh character set * using the 'CAP' name-mangling scheme, returning the length of the * mangled filename. Note that the output string is not NULL * terminated. * * This routine is a inverse to hfs_mac2cap() and hfs_mac2nat(). * A ':' not followed by a 2-digit hexadecimal number (or followed * by the codes for NULL or ':') is replaced by a '|'. */void hfs_colon2mac(struct hfs_name *out, const char *in, int len) { int hi, lo; unsigned char code, c, *count; unsigned char *p = out->Name; out->Len = 0; count = &out->Len; while (len-- && (*count < HFS_NAMELEN)) { c = *in++; (*count)++; if (c!=':') { *p++ = c; } else if ((len<2) || ((hi=dehex(in[0])) & 0xf0) || ((lo=dehex(in[1])) & 0xf0) || !(code = (hi << 4) | lo) || (code == ':')) { *p++ = '|'; } else { *p++ = code; len -= 2; in += 2; } }}/* * hfs_prcnt2mac() * * Given an ASCII string (not null-terminated) and its length, * generate the corresponding filename in the Macintosh character set * using Apple's three recommended name-mangling schemes, returning * the length of the mangled filename. Note that the output string is * not NULL terminated. * * This routine is a inverse to hfs_mac2alpha(), hfs_mac2seven() and * hfs_mac2eight(). * A '%' not followed by a 2-digit hexadecimal number (or followed * by the code for NULL or ':') is unchanged. * A ':' is replaced by a '|'. */void hfs_prcnt2mac(struct hfs_name *out, const char *in, int len) { int hi, lo; unsigned char code, c, *count; unsigned char *p = out->Name; out->Len = 0; count = &out->Len; while (len-- && (*count < HFS_NAMELEN)) { c = *in++; (*count)++; if (c==':') { *p++ = '|'; } else if (c!='%') { *p++ = c; } else if ((len<2) || ((hi=dehex(in[0])) & 0xf0) || ((lo=dehex(in[1])) & 0xf0) || !(code = (hi << 4) | lo) || (code == ':')) { *p++ = '%'; } else { *p++ = code; len -= 2; in += 2; } }}/* * hfs_triv2mac() * * Given an ASCII string (not null-terminated) and its length, * generate the corresponding filename in the Macintosh character set * using the 'trivial' name-mangling scheme, returning the length of * the mangled filename. Note that the output string is not NULL * terminated. * * This routine is a inverse to hfs_mac2triv(). * A ':' is replaced by a '/'. */void hfs_triv2mac(struct hfs_name *out, const char *in, int len) { unsigned char c, *count; unsigned char *p = out->Name; out->Len = 0; count = &out->Len; while (len-- && (*count < HFS_NAMELEN)) { c = *in++; (*count)++; if (c==':') { *p++ = '/'; } else { *p++ = c; } }}/* * hfs_latin2mac() * * Given an Latin-1 string (not null-terminated) and its length, * generate the corresponding filename in the Macintosh character set * using the 'Latin-1' name-mangling scheme, returning the length of * the mangled filename. Note that the output string is not NULL * terminated. * * This routine is a inverse to hfs_latin2cap(). * A '%' not followed by a 2-digit hexadecimal number (or followed * by the code for NULL or ':') is unchanged. * A ':' is replaced by a '|'. * * Note that the character map is built the first time it is needed. */void hfs_latin2mac(struct hfs_name *out, const char *in, int len){ int hi, lo; unsigned char code, c, *count; unsigned char *p = out->Name; static int map_initialized; if (!map_initialized) { int i; /* build the inverse mapping at run time */ for (i = 0; i < 128; i++) { if ((c = mac2latin_map[i])) { latin2mac_map[(int)c - 128] = i + 128; } } map_initialized = 1; } out->Len = 0; count = &out->Len; while (len-- && (*count < HFS_NAMELEN)) { c = *in++; (*count)++; if (c==':') { *p++ = '|'; } else if (c!='%') { if (c<128 || !(*p = latin2mac_map[c-128])) { *p = c; } p++; } else if ((len<2) || ((hi=dehex(in[0])) & 0xf0) || ((lo=dehex(in[1])) & 0xf0) || !(code = (hi << 4) | lo) || (code == ':')) { *p++ = '%'; } else { *p++ = code; len -= 2; in += 2; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -