+#include "desktop.h"
+
+
+#define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'A' )
+#define isuxdigit(x) (isdigit(x) || (isupper(x) && isxdigit(x)))
+
+static size_t mangle_extension(const struct vol *vol, const char* uname,
+ char* extension, charset_t charset)
+{
+ char *p = strrchr(uname, '.');
+
+ if (p && p != uname) {
+ u_int16_t flags = CONV_FORCE | CONV_UNESCAPEHEX;
+ size_t len = convert_charset(vol->v_volcharset, charset,
+ vol->v_maccharset, p, strlen(p),
+ extension, MAX_EXT_LENGTH, &flags);
+
+ if (len != (size_t)-1) return len;
+ }
+ return 0;
+}
+
+static char *demangle_checks(const struct vol *vol, char* uname, char * mfilename, size_t prefix, char * ext)
+{
+ u_int16_t flags;
+ static char buffer[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
+ size_t len;
+ size_t mfilenamelen;
+
+ /* We need to check, whether we really need to demangle the filename */
+ /* i.e. it's not just a file with a valid #HEX in the name ... */
+ /* but we don't want to miss valid demangle as well. */
+ /* check whether file extensions match */
+
+ char buf[MAX_EXT_LENGTH + 2]; /* for convert_charset dest_len parameter +2 */
+ size_t ext_len = mangle_extension(vol, uname, buf, CH_UTF8_MAC);
+
+ if (ext_len) {
+ buf[ext_len] = '\0';
+ if (strcmp(ext, buf))
+ return mfilename;
+ } else {
+ if (*ext)
+ return mfilename;
+ }
+
+ /* First we convert the unix name to our volume maccharset */
+ /* This assumes, OSX will not send us a mangled name for *any* */
+ /* other reason than a hint/filename mismatch on the OSX side!! */
+ /* If the filename needed mangling, we'll get the mac filename */
+ /* till the first unconvertable char, so these have to match */
+ /* the mangled name we got .. */
+
+ flags = CONV_IGNORE | CONV_UNESCAPEHEX;
+ if ( (size_t) -1 == (len = convert_charset(vol->v_volcharset, vol->v_maccharset, 0,
+ uname, strlen(uname), buffer, MAXPATHLEN, &flags)) ) {
+ return mfilename;
+ }
+ /* If the filename is too long we also needed to mangle */
+ mfilenamelen = strlen(mfilename);
+ if ( len >= vol->max_filename || mfilenamelen == MACFILELEN ) {
+ flags |= CONV_REQMANGLE;
+ len = prefix;
+ }
+
+ /* Ok, mangling was needed, now we do some further checks */
+ /* this is still necessary, as we might have a file abcde:xx */
+ /* with id 12, mangled to abcde#12, and a passed filename */
+ /* abcd#12 */
+ /* if we only checked if "prefix" number of characters match */
+ /* we get a false postive in above case */
+
+ if ( (flags & CONV_REQMANGLE) ) {
+ if (len) {
+ /* convert the buffer to UTF8_MAC ... */
+ if ((size_t) -1 == (len = convert_charset(vol->v_maccharset, CH_UTF8_MAC, 0,
+ buffer, len, buffer, MAXPATHLEN, &flags)) ) {
+ return mfilename;
+ }
+ /* Now compare the two names, they have to match the number of characters in buffer */
+ /* prefix can be longer than len, OSX might send us the first character(s) of a */
+ /* decomposed char as the *last* character(s) before the #, so our match below will */
+ /* still work, but leaves room for a race ... FIXME */
+ if ( (prefix >= len || mfilenamelen == MACFILELEN)
+ && !strncmp (mfilename, buffer, len)) {
+ return uname;
+ }
+ }
+ else {
+ /* We couldn't convert the name to maccharset at all, so we'd expect a name */
+ /* in the "???#ID" form ... */
+ if ( !strncmp("???", mfilename, prefix)) {
+ return uname;
+ }
+ /* ..but OSX might send us only the first characters of a decomposed character. */
+ /* So convert to UTF8_MAC again, now at least the prefix number of */
+ /* characters have to match ... again a possible race FIXME */
+
+ if ( (size_t) -1 == (len = convert_charset(vol->v_volcharset, CH_UTF8_MAC, 0,
+ uname, strlen(uname), buffer, MAXPATHLEN, &flags)) ) {
+ return mfilename;
+ }
+
+ if ( !strncmp (mfilename, buffer, prefix) ) {
+ return uname;
+ }
+ }
+ }
+ return mfilename;
+}
+
+/* -------------------------------------------------------
+*/
+static char *
+private_demangle(const struct vol *vol, char *mfilename, cnid_t did, cnid_t *osx)
+{
+ char *t;
+ char *u_name;
+ u_int32_t id, file_id;
+ static char buffer[12 + MAXPATHLEN + 1];
+ int len = 12 + MAXPATHLEN + 1;
+ struct dir *dir;
+ size_t prefix;