2 * $Id: mangle.c,v 1.19 2006-09-19 01:35:45 didg Exp $
4 * Copyright (c) 2002. Joe Marcus Clarke (marcus@marcuscom.com)
5 * All Rights Reserved. See COPYRIGHT.
7 * mangle, demangle (filename):
8 * mangle or demangle filenames if they are greater than the max allowed
9 * characters for a given version of AFP.
14 #endif /* HAVE_CONFIG_H */
20 #include <atalk/util.h>
22 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'A' )
23 #define isuxdigit(x) (isdigit(x) || (isupper(x) && isxdigit(x)))
25 static size_t mangle_extension(const struct vol *vol, const char* uname,
26 char* extension, charset_t charset)
28 char *p = strrchr(uname, '.');
30 if (p && p != uname) {
31 u_int16_t flags = CONV_FORCE | CONV_UNESCAPEHEX;
32 size_t len = convert_charset(vol->v_volcharset, charset,
33 vol->v_maccharset, p, strlen(p),
34 extension, MAX_EXT_LENGTH, &flags);
36 if (len != (size_t)-1) return len;
41 static char *demangle_checks ( const struct vol *vol, char* uname, char * mfilename, size_t prefix, char * ext)
44 static char buffer[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
48 /* We need to check, whether we really need to demangle the filename */
49 /* i.e. it's not just a file with a valid #HEX in the name ... */
50 /* but we don't want to miss valid demangle as well. */
52 /* check whether file extensions match */
54 char buf[MAX_EXT_LENGTH + 2]; /* for convert_charset dest_len parameter +2 */
55 size_t ext_len = mangle_extension(vol, uname, buf, CH_UTF8_MAC);
59 if (strcmp(ext, buf)) return mfilename;
61 if (*ext) return mfilename;
65 /* First we convert the unix name to our volume maccharset */
66 /* This assumes, OSX will not send us a mangled name for *any* */
67 /* other reason than a hint/filename mismatch on the OSX side!! */
68 /* If the filename needed mangling, we'll get the mac filename */
69 /* till the first unconvertable char, so these have to match */
70 /* the mangled name we got .. */
72 flags = CONV_IGNORE | CONV_UNESCAPEHEX;
73 if ( (size_t) -1 == (len = convert_charset(vol->v_volcharset, vol->v_maccharset, 0,
74 uname, strlen(uname), buffer, MAXPATHLEN, &flags)) ) {
77 /* If the filename is too long we also needed to mangle */
78 mfilenamelen = strlen(mfilename);
79 if ( len >= vol->max_filename || mfilenamelen == MACFILELEN ) {
80 flags |= CONV_REQMANGLE;
84 /* Ok, mangling was needed, now we do some further checks */
85 /* this is still necessary, as we might have a file abcde:xx */
86 /* with id 12, mangled to abcde#12, and a passed filename */
88 /* if we only checked if "prefix" number of characters match */
89 /* we get a false postive in above case */
91 if ( (flags & CONV_REQMANGLE) ) {
93 /* convert the buffer to UTF8_MAC ... */
94 if ((size_t) -1 == (len = convert_charset(vol->v_maccharset, CH_UTF8_MAC, 0,
95 buffer, len, buffer, MAXPATHLEN, &flags)) ) {
98 /* Now compare the two names, they have to match the number of characters in buffer */
99 /* prefix can be longer than len, OSX might send us the first character(s) of a */
100 /* decomposed char as the *last* character(s) before the #, so our match below will */
101 /* still work, but leaves room for a race ... FIXME */
102 if ( (prefix >= len || mfilenamelen == MACFILELEN)
103 && !strncmp (mfilename, buffer, len)) {
108 /* We couldn't convert the name to maccharset at all, so we'd expect a name */
109 /* in the "???#ID" form ... */
110 if ( !strncmp("???", mfilename, prefix)) {
113 /* ..but OSX might send us only the first characters of a decomposed character. */
114 /* So convert to UTF8_MAC again, now at least the prefix number of */
115 /* characters have to match ... again a possible race FIXME */
117 if ( (size_t) -1 == (len = convert_charset(vol->v_volcharset, CH_UTF8_MAC, 0,
118 uname, strlen(uname), buffer, MAXPATHLEN, &flags)) ) {
122 if ( !strncmp (mfilename, buffer, prefix) ) {
130 /* -------------------------------------------------------
133 private_demangle(const struct vol *vol, char *mfilename, cnid_t did, cnid_t *osx)
137 u_int32_t id, file_id;
138 static char buffer[12 + MAXPATHLEN + 1];
139 int len = 12 + MAXPATHLEN + 1;
145 t = strchr(mfilename, MANGLE_CHAR);
149 prefix = t - mfilename;
151 * is prefix == 0 a valid mangled filename ?
153 /* may be a mangled filename */
155 if (*t == '0') { /* can't start with a 0 */
158 while(isuxdigit(*t)) {
159 id = (id *16) + hextoint(*t);
162 if ((*t != 0 && *t != '.') || strlen(t) > MAX_EXT_LENGTH || id < 17) {
166 file_id = id = htonl(id);
171 /* is it a dir?, there's a conflict with pre OSX 'trash #2' */
172 if ((dir = dirsearch(vol, id))) {
173 if (dir->d_parent && dir->d_parent->d_did != did) {
174 /* not in the same folder, there's a race with outdate cache
175 * but we have to live with it, hopefully client will recover
180 /* it's not from cname so mfilename and dir must be the same */
181 if (!strcmp(dir->d_m_name, mfilename)) {
182 return dir->d_u_name;
186 return demangle_checks (vol, dir->d_u_name, mfilename, prefix, t);
189 else if (NULL != (u_name = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
194 /* convert back to mac name and check it's the same */
195 t = utompath(vol, u_name, file_id, utf8_encoding());
196 if (!strcmp(t, mfilename)) {
201 return demangle_checks (vol, u_name, mfilename, prefix, t);
208 /* -------------------------------------------------------
211 demangle(const struct vol *vol, char *mfilename, cnid_t did)
213 return private_demangle(vol, mfilename, did, NULL);
216 /* -------------------------------------------------------
220 demangle_osx(const struct vol *vol, char *mfilename, cnid_t did, cnid_t *fileid)
222 return private_demangle(vol, mfilename, did, fileid);
226 /* -----------------------
227 with utf8 filename not always round trip
228 filename mac filename too long or first chars if unmatchable chars.
230 id file/folder ID or 0
234 mangle(const struct vol *vol, char *filename, size_t filenamelen, char *uname, cnid_t id, int flags) {
236 static char mfilename[MAXPATHLEN]; /* way > maxlen */
237 char mangle_suffix[MANGLE_LENGTH + 1];
238 char ext[MAX_EXT_LENGTH +2]; /* for convert_charset dest_len parameter +2 */
243 maxlen = (flags & 2)?255:MACFILELEN; /* was vol->max_filename */
244 /* Do we really need to mangle this filename? */
245 if (!(flags & 1) && filenamelen <= maxlen) {
250 /* we don't have the file id! only catsearch call mangle with id == 0 */
253 /* First, attempt to locate a file extension. */
254 ext_len = mangle_extension(vol, uname, ext, (flags & 2) ? CH_UTF8_MAC : vol->v_maccharset);
256 k = sprintf(mangle_suffix, "%c%X", MANGLE_CHAR, ntohl(id));
258 if (filenamelen + k + ext_len > maxlen) {
259 u_int16_t opt = CONV_FORCE | CONV_UNESCAPEHEX;
260 size_t n = convert_charset(vol->v_volcharset,
261 (flags & 2) ? CH_UTF8_MAC : vol->v_maccharset,
262 vol->v_maccharset, uname, strlen(uname),
263 m, maxlen - k - ext_len, &opt);
264 m[n != (size_t)-1 ? n : 0] = 0;
266 strlcpy(m, filename, filenamelen + 1);
271 strcat(m, mangle_suffix);
273 strncat(m, ext, ext_len);