+ m = mpath;
+
+#ifdef FILE_MANGLING
+ m = mangle(vol, mpath, upath, 0);
+ if (m != mpath) {
+ changed = 1;
+ }
+#endif /* FILE_MANGLING */
+
+#ifdef DEBUG
+ LOG(log_debug, logtype_afpd, "utompath: '%s':'%s'", upath, mpath);
+#endif /* DEBUG */
+
+ return((changed)? m:upath );
+}
+
+/* --------------- */
+#ifdef AFP3x
+extern unsigned int do_precomposition(unsigned int base, unsigned int comb);
+
+static char comp[MAXPATHLEN +1];
+
+static char *precompose(u_int16_t *name, size_t inplen, size_t *outlen)
+{
+size_t i;
+u_int16_t base, comb;
+u_int16_t *in, *out;
+u_int16_t result;
+
+ if (!inplen || (inplen & 1) || inplen > sizeof(comp)/sizeof(u_int16_t))
+ return NULL;
+ i = 0;
+ in = name;
+ out = (u_int16_t *)comp;
+ *outlen = 0;
+
+ base = *in;
+ while (1) {
+ i += 2;
+ in++;
+ if (i == inplen) {
+ *out = base;
+ *outlen += 2;
+ return comp;
+ }
+ comb = *in;
+ if (comb >= 0x300 && (result = do_precomposition(base, comb))) {
+ *out = result;
+ out++;
+ *outlen += 2;
+ i += 2;
+ in++;
+ if (i == inplen)
+ return comp;
+ base = *in;
+ }
+ else {
+ *out = base;
+ out++;
+ *outlen += 2;
+ base = comb;
+ }
+ }
+}
+
+/* --------------- */
+extern unsigned int do_decomposition(unsigned int base);
+
+static char *decompose(u_int16_t *name, size_t inplen, size_t *outlen)
+{
+size_t i;
+u_int16_t base;
+u_int16_t *in, *out;
+unsigned int result;
+
+ if (!inplen || (inplen & 1))
+ return NULL;
+ i = 0;
+ in = name;
+ out = (u_int16_t *)comp;
+ *outlen = 0;
+
+ while (i < inplen) {
+ if (*outlen >= sizeof(comp)/sizeof(u_int16_t) +2) {
+ return NULL;
+ }
+ base = *in;
+ if ((result = do_decomposition(base))) {
+ *out = result >> 16;
+ out++;
+ *outlen += 2;
+ *out = result & 0xffff;
+ out++;
+ *outlen += 2;
+ }
+ else {
+ *out = base;
+ out++;
+ *outlen += 2;
+ }
+ i += 2;
+ in++;
+ }
+ return comp;
+}
+#endif
+
+/* --------------------------- */
+char *mtoupath(const struct vol *vol, char *mpath, int utf8)
+{
+ int i = 0;
+ char *m, *u;
+#ifdef AFP3x
+ char *r;
+ size_t inplen;
+ size_t outlen;
+#endif
+
+ if ( *mpath == '\0' ) {
+ return( "." );
+ }
+
+#ifdef FILE_MANGLING
+ m = demangle(vol, mpath);
+ if (m != mpath) {
+ return m;
+ }
+#endif /* FILE_MANGLING */
+
+ if (!vol_utf8(vol))
+ return old_mtoupath(vol, mpath);
+
+ m = mpath;
+ u = upath;
+ while ( *m != '\0' ) {
+ if ( (!(vol->v_flags & AFPVOL_NOHEX) && *m == '/') ||
+ (!(vol->v_flags & AFPVOL_USEDOTS) && i == 0 && *m == '.') ||
+ (!utf8 && (unsigned char)*m == 0xf0) /* Apple logo */
+ ) {
+ /* do hex conversion. */
+ *u++ = ':';
+ *u++ = hexdig[ ( *m & 0xf0 ) >> 4 ];
+ *u = hexdig[ *m & 0x0f ];
+ } else
+ *u = *m;
+ u++;
+ i++;
+ m++;
+ }
+ *u = '\0';
+ u = upath;
+#ifdef AFP3x
+ inplen = strlen(u);
+ outlen = MAXPATHLEN;
+ r = ucs2;
+ if (!utf8) {
+ if ((size_t)(-1) == iconv(vol->v_mactoutf8, 0,0,0,0) )
+ return NULL;
+ /* assume precompose */
+ if ((size_t)(-1) == iconv(vol->v_mactoutf8, &u, &inplen, &r, &outlen))
+ return NULL;
+ u = ucs2;
+ }
+ else {
+ if ((size_t)(-1) == iconv(vol->v_utf8toucs2, 0,0,0,0) )
+ return NULL;
+
+ if ((size_t)(-1) == iconv(vol->v_utf8toucs2, &u, &inplen, &r, &outlen))
+ return NULL;
+
+ u = precompose((u_int16_t *)ucs2, MAXPATHLEN -outlen, &inplen);
+
+ if ((size_t)(-1) == iconv(vol->v_ucs2toutf8, 0,0,0,0))
+ return NULL;
+
+ outlen = MAXPATHLEN;
+ r = upath;
+ if ((size_t)(-1) == iconv(vol->v_ucs2toutf8, &u, &inplen, &r, &outlen))
+ return NULL;
+ u = upath;
+ }
+ u[MAXPATHLEN -outlen] = 0;
+#endif
+#ifdef DEBUG
+ LOG(log_debug, logtype_afpd, "mtoupath: '%s':'%s'", mpath, upath);
+#endif /* DEBUG */
+ return( u );
+}
+
+/* --------------- */
+char *utompath(const struct vol *vol, char *upath, int utf8)
+{
+ int h;
+ int mangleflag = 0;
+ char *m, *u;
+#ifdef AFP3x
+ char *r;
+ size_t inplen;
+ size_t outlen;
+#endif
+
+ if (!vol_utf8(vol))
+ return old_utompath(vol, upath);
+ /* do the hex conversion */
+ u = upath;
+ m = mpath;
+ while ( *u != '\0' ) {
+ if ( *u == ':' && islxdigit( *(u+1)) && islxdigit( *(u+2))) {
+ ++u;
+ h = hextoint( *u ) << 4;
+ ++u;
+ h |= hextoint( *u );
+ *m = h;
+ } else
+ *m = *u;
+ u++;
+ m++;
+ }
+ *m = '\0';
+ m = mpath;
+#ifdef AFP3x
+ if ((size_t)(-1) == iconv(vol->v_utf8toucs2, 0,0,0,0) )
+ return NULL;
+ inplen = strlen(mpath);
+ outlen = MAXPATHLEN;
+ r = ucs2;
+ if ((size_t)(-1) == iconv(vol->v_utf8toucs2, &m, &inplen, &r, &outlen))
+ return NULL;
+
+ if (utf8) {
+ if ( NULL == (m = decompose((u_int16_t *)ucs2, MAXPATHLEN -outlen, &inplen)))
+ return NULL;
+
+ if ((size_t)(-1) == iconv(vol->v_ucs2toutf8, 0,0,0,0))
+ return NULL;
+
+ outlen = MAXPATHLEN;
+ r = mpath;
+ if ((size_t)(-1) == iconv(vol->v_ucs2toutf8, &m, &inplen, &r, &outlen))
+ return NULL;
+ }
+ else {
+ m = precompose((u_int16_t *)ucs2, MAXPATHLEN -outlen, &inplen);
+
+ if ((size_t)(-1) == iconv(vol->v_ucs2tomac, 0,0,0,0))
+ return NULL;
+
+ outlen = MAXPATHLEN;
+ r = mpath;
+ if ((size_t)(-1) == iconv(vol->v_ucs2tomac, &m, &inplen, &r, &outlen)) {
+ switch (errno) {
+ case EILSEQ:
+ if (outlen != MAXPATHLEN) {
+ mangleflag = 1;
+ }
+ default:
+ return NULL;
+ }
+ }
+ }
+ mpath[MAXPATHLEN -outlen] = 0;
+#endif
+#ifdef FILE_MANGLING
+ m = mangle(vol, mpath, upath, mangleflag);
+#else
+ if (mangleflag)
+ return NULL;
+ m = mpath;
+#endif /* FILE_MANGLING */
+
+ return(m);