+ return( utom_buf );
+}
+
+static void euc2sjis( int *p1, int *p2) /* agrees w/ Samba on valid codes */
+{
+ int row_offset, cell_offset;
+ unsigned char c1, c2;
+
+ /* first convert EUC to ISO-2022 */
+ c1 = *p1 & 0x7F;
+ c2 = *p2 & 0x7F;
+
+ /* now convert ISO-2022 to Shift-JIS */
+ row_offset = c1 < 95 ? 112 : 176;
+ cell_offset = c1 % 2 ? (c2 > 95 ? 32 : 31) : 126;
+
+ *p1 = ((c1 + 1) >> 1) + row_offset;
+ *p2 = c2 + cell_offset;
+}
+
+static void sjis2euc( int *p1, int *p2) /* agrees w/ Samba on valid codes */
+{
+ int row_offset, cell_offset, adjust;
+ unsigned char c1, c2;
+
+ c1 = *p1;
+ c2 = *p2;
+
+ /* first convert Shift-JIS to ISO-2022 */
+ adjust = c2 < 159;
+ row_offset = c1 < 160 ? 112 : 176;
+ cell_offset = adjust ? (c2 > 127 ? 32 : 31) : 126;
+
+ c1 = ((c1 - row_offset) << 1) - adjust;
+ c2 -= cell_offset;
+
+ /* now convert ISO-2022 to EUC */
+ *p1 = c1 | 0x80;
+ *p2 = c2 | 0x80;
+}
+
+static char *mtoupatheuc( char *from)
+{
+ unsigned char *in, *out, *maxout;
+ int p, p2, i = 0;
+
+ in = (unsigned char *) from;
+ out = (unsigned char *) mtou_buf;
+
+ if( *in ) {
+ maxout = out + sizeof( mtou_buf) - 3;
+
+ while( out < maxout ) {
+ p = *in++;
+
+ if( ((0x81 <= p) && (p <= 0x9F))
+ || ((0xE0 <= p) && (p <= 0xEF)) ) {
+ /* JIS X 0208 */
+ p2 = *in++;
+ if( ((0x40 <= p2) && (p2 <= 0x7E))
+ || ((0x80 <= p2) && (p2 <= 0xFC)) )
+ sjis2euc( &p, &p2);
+ *out++ = p;
+ p = p2;
+
+ } else if( (0xA1 <= p) && (p <= 0xDF) ) {
+ *out++ = 0x8E; /* halfwidth katakana */
+ } else if( p < 0x80 ) {
+#ifdef DOWNCASE
+ p = ( isupper( p )) ? tolower( p ) : p;
+#endif /* DOWNCASE */
+ }
+ if( ( p == '/') || ( i == 0 && p == '.' ) ) {
+ *out++ = ':';
+ *out++ = hexdig[ ( p & 0xf0 ) >> 4 ];
+ p = hexdig[ p & 0x0f ];
+ }
+ i++;
+ *out++ = p;
+ if( p )
+ continue;
+ break;
+ }
+ } else {
+ *out++ = '.';
+ *out = 0;
+ }
+
+ return mtou_buf;
+}
+
+static char *utompatheuc( char *from)
+{
+ unsigned char *in, *out, *maxout;
+ int p, p2;
+
+ in = (unsigned char *) from;
+ out = (unsigned char *) utom_buf;
+ maxout = out + sizeof( utom_buf) - 3;
+
+ while( out < maxout ) {
+ p = *in++;
+
+ if( (0xA1 <= p) && (p <= 0xFE) ) { /* JIS X 0208 */
+ p2 = *in++;
+ if( (0xA1 <= p2) && (p2 <= 0xFE) )
+ euc2sjis( &p, &p2);
+ *out++ = p;
+ p = p2;
+ } else if( p == 0x8E ) { /* halfwidth katakana */
+ p = *in++;
+ } else if( p < 0x80 ) {
+#ifdef DOWNCASE
+ p = ( isupper( p )) ? tolower( p ) : p;
+#endif /* DOWNCASE */
+ }
+ if ( p == ':' && *(in) != '\0' && islxdigit( *(in)) &&
+ *(in+1) != '\0' && islxdigit( *(in+1))) {
+ p = hextoint( *in ) << 4;
+ in++;
+ p |= hextoint( *in );
+ in++;
+ }
+ *out++ = p;
+ if( p )
+ continue;
+ break;
+ }
+
+ return utom_buf;
+}
+
+static char *mtoupathsjis( char *from)
+{
+ unsigned char *in, *out, *maxout;
+ int p, p2, i = 0;
+
+ in = (unsigned char *) from;
+ out = (unsigned char *) mtou_buf;
+
+ if( *in ) {
+ maxout = out + sizeof( mtou_buf) - 3;
+
+ while( out < maxout ) {
+ p = *in++;
+
+ if( ((0x81 <= p) && (p <= 0x9F))
+ || ((0xE0 <= p) && (p <= 0xEF)) ) {
+ /* JIS X 0208 */
+ p2 = *in++;
+ *out++ = p;
+ p = p2;
+
+ } else if( (0xA1 <= p) && (p <= 0xDF) ) {
+ ; /* halfwidth katakana */
+ } else if(p < 0x80 ) {
+#ifdef DOWNCASE
+ p = ( isupper( p )) ? tolower( p ) : p;
+#endif /* DOWNCASE */
+ }
+ if( ( p == '/') || ( i == 0 && p == '.' ) ) {
+ *out++ = ':';
+ *out++ = hexdig[ ( p & 0xf0 ) >> 4 ];
+ p = hexdig[ p & 0x0f ];
+ }
+ i++;
+ *out++ = p;
+ if( p )
+ continue;
+ break;
+ }
+ } else {
+ *out++ = '.';
+ *out = 0;
+ }
+
+ return mtou_buf;
+}
+
+static char *utompathsjis( char *from)
+{
+ unsigned char *in, *out, *maxout;
+ int p, p2;
+
+ in = (unsigned char *) from;
+ out = (unsigned char *) utom_buf;
+ maxout = out + sizeof( utom_buf) - 3;
+
+ while( out < maxout ) {
+ p = *in++;
+
+ if( (0xA1 <= p) && (p <= 0xFE) ) { /* JIS X 0208 */
+ p2 = *in++;
+ *out++ = p;
+ p = p2;
+ } else if( p == 0x8E ) { /* do nothing */
+ ;
+ } else if( p < 0x80 ) {
+#ifdef DOWNCASE
+ p = ( isupper( p )) ? tolower( p ) : p;
+#endif /* DOWNCASE */
+ }
+ if ( p == ':' && *(in) != '\0' && islxdigit( *(in)) &&
+ *(in+1) != '\0' && islxdigit( *(in+1))) {
+ p = hextoint( *in ) << 4;
+ in++;
+ p |= hextoint( *in );
+ in++;
+ }
+ *out++ = p;
+ if( p )
+ continue;
+ break;
+ }
+
+ return utom_buf;
+ }
+
+static char *utompathiconv(char *upath)
+{
+ char *m, *u;
+ u_int16_t flags = CONV_IGNORE | CONV_UNESCAPEHEX;
+ size_t outlen;
+ static char mpath[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
+
+ m = mpath;
+ outlen = strlen(upath);
+
+#if 0
+ if (vol->v_casefold & AFPVOL_UTOMUPPER)
+ flags |= CONV_TOUPPER;
+ else if (vol->v_casefold & AFPVOL_UTOMLOWER)
+ flags |= CONV_TOLOWER;
+#endif
+
+ u = upath;
+
+ /* convert charsets */
+ if ((size_t)-1 == ( outlen = convert_charset ( vol.v_volcharset, vol.v_maccharset, vol.v_maccharset, u, outlen, mpath, MAXPATHLEN, &flags)) ) {
+ fprintf( stderr, "Conversion from %s to %s for %s failed.", vol.v_volcodepage, vol.v_maccodepage, u);
+ goto utompath_error;
+ }
+
+ if (flags & CONV_REQMANGLE)
+ goto utompath_error;
+
+ return(m);
+
+utompath_error:
+ return(utompathcap( upath ));
+}
+
+static char *mtoupathiconv(char *mpath)
+{
+ char *m, *u;
+ size_t inplen;
+ size_t outlen;
+ u_int16_t flags = 0;
+ static char upath[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
+
+ if ( *mpath == '\0' ) {
+ return( "." );
+ }
+
+ /* set conversion flags */
+ if (!(vol.v_flags & AFPVOL_NOHEX))
+ flags |= CONV_ESCAPEHEX;
+ if (!(vol.v_flags & AFPVOL_USEDOTS))
+ flags |= CONV_ESCAPEDOTS;
+
+#if 0
+ if ((vol->v_casefold & AFPVOL_MTOUUPPER))
+ flags |= CONV_TOUPPER;
+ else if ((vol->v_casefold & AFPVOL_MTOULOWER))
+ flags |= CONV_TOLOWER;
+#endif
+
+ m = mpath;
+ u = upath;
+
+ inplen = strlen(m);
+ outlen = MAXPATHLEN;
+
+ if ((size_t)-1 == (outlen = convert_charset ( vol.v_maccharset, vol.v_volcharset, vol.v_maccharset, m, inplen, u, outlen, &flags)) ) {
+ fprintf (stderr, "conversion from %s to %s for %s failed.", vol.v_maccodepage, vol.v_volcodepage, mpath);
+ return(mtoupathcap( upath ));
+ }
+
+ return( upath );