The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/msdosfs/msdosfs_conv.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $FreeBSD$ */
    2 /*      $NetBSD: msdosfs_conv.c,v 1.25 1997/11/17 15:36:40 ws Exp $     */
    3 
    4 /*-
    5  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
    6  * Copyright (C) 1995, 1997 TooLs GmbH.
    7  * All rights reserved.
    8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. All advertising materials mentioning features or use of this software
   19  *    must display the following acknowledgement:
   20  *      This product includes software developed by TooLs GmbH.
   21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
   22  *    derived from this software without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
   25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   34  */
   35 /*-
   36  * Written by Paul Popelka (paulp@uts.amdahl.com)
   37  *
   38  * You can do anything you want with this software, just don't say you wrote
   39  * it, and don't remove this notice.
   40  *
   41  * This software is provided "as is".
   42  *
   43  * The author supplies this software to be publicly redistributed on the
   44  * understanding that the author is not responsible for the correct
   45  * functioning of this software in any circumstances and is not liable for
   46  * any damages caused by this software.
   47  *
   48  * October 1992
   49  */
   50 
   51 /*
   52  * System include files.
   53  */
   54 #include <sys/param.h>
   55 #include <sys/time.h>
   56 #include <sys/kernel.h>         /* defines tz */
   57 #include <sys/systm.h>
   58 #include <machine/clock.h>
   59 #include <sys/dirent.h>
   60 #include <sys/iconv.h>
   61 #include <sys/mount.h>
   62 #include <sys/malloc.h>
   63 
   64 extern struct iconv_functions *msdosfs_iconv;
   65 
   66 /*
   67  * MSDOSFS include files.
   68  */
   69 #include <fs/msdosfs/bpb.h>
   70 #include <fs/msdosfs/msdosfsmount.h>
   71 #include <fs/msdosfs/direntry.h>
   72 
   73 /*
   74  * Total number of days that have passed for each month in a regular year.
   75  */
   76 static u_short regyear[] = {
   77         31, 59, 90, 120, 151, 181,
   78         212, 243, 273, 304, 334, 365
   79 };
   80 
   81 /*
   82  * Total number of days that have passed for each month in a leap year.
   83  */
   84 static u_short leapyear[] = {
   85         31, 60, 91, 121, 152, 182,
   86         213, 244, 274, 305, 335, 366
   87 };
   88 
   89 /*
   90  * Variables used to remember parts of the last time conversion.  Maybe we
   91  * can avoid a full conversion.
   92  */
   93 static u_long  lasttime;
   94 static u_long  lastday;
   95 static u_short lastddate;
   96 static u_short lastdtime;
   97 
   98 static int mbsadjpos(const char **, size_t, size_t, int, int, void *handle);
   99 static u_int16_t dos2unixchr(const u_char **, size_t *, int, struct msdosfsmount *);
  100 static u_int16_t unix2doschr(const u_char **, size_t *, struct msdosfsmount *);
  101 static u_int16_t win2unixchr(u_int16_t, struct msdosfsmount *);
  102 static u_int16_t unix2winchr(const u_char **, size_t *, int, struct msdosfsmount *);
  103 
  104 static char     *nambuf_ptr;
  105 static size_t   nambuf_len;
  106 static int      nambuf_last_id;
  107 
  108 /*
  109  * Convert the unix version of time to dos's idea of time to be used in
  110  * file timestamps. The passed in unix time is assumed to be in GMT.
  111  */
  112 void
  113 unix2dostime(tsp, ddp, dtp, dhp)
  114         struct timespec *tsp;
  115         u_int16_t *ddp;
  116         u_int16_t *dtp;
  117         u_int8_t *dhp;
  118 {
  119         u_long t;
  120         u_long days;
  121         u_long inc;
  122         u_long year;
  123         u_long month;
  124         u_short *months;
  125 
  126         /*
  127          * If the time from the last conversion is the same as now, then
  128          * skip the computations and use the saved result.
  129          */
  130         t = tsp->tv_sec - (tz_minuteswest * 60)
  131             - (wall_cmos_clock ? adjkerntz : 0);
  132             /* - daylight savings time correction */
  133         t &= ~1;
  134         if (lasttime != t) {
  135                 lasttime = t;
  136                 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
  137                     + (((t / 60) % 60) << DT_MINUTES_SHIFT)
  138                     + (((t / 3600) % 24) << DT_HOURS_SHIFT);
  139 
  140                 /*
  141                  * If the number of days since 1970 is the same as the last
  142                  * time we did the computation then skip all this leap year
  143                  * and month stuff.
  144                  */
  145                 days = t / (24 * 60 * 60);
  146                 if (days != lastday) {
  147                         lastday = days;
  148                         for (year = 1970;; year++) {
  149                                 inc = year & 0x03 ? 365 : 366;
  150                                 if (days < inc)
  151                                         break;
  152                                 days -= inc;
  153                         }
  154                         months = year & 0x03 ? regyear : leapyear;
  155                         for (month = 0; days >= months[month]; month++)
  156                                 ;
  157                         if (month > 0)
  158                                 days -= months[month - 1];
  159                         lastddate = ((days + 1) << DD_DAY_SHIFT)
  160                             + ((month + 1) << DD_MONTH_SHIFT);
  161                         /*
  162                          * Remember dos's idea of time is relative to 1980.
  163                          * unix's is relative to 1970.  If somehow we get a
  164                          * time before 1980 then don't give totally crazy
  165                          * results.
  166                          */
  167                         if (year > 1980)
  168                                 lastddate += (year - 1980) << DD_YEAR_SHIFT;
  169                 }
  170         }
  171         if (dtp)
  172                 *dtp = lastdtime;
  173         if (dhp)
  174                 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
  175 
  176         *ddp = lastddate;
  177 }
  178 
  179 /*
  180  * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
  181  * interval there were 8 regular years and 2 leap years.
  182  */
  183 #define SECONDSTO1980   (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
  184 
  185 static u_short lastdosdate;
  186 static u_long  lastseconds;
  187 
  188 /*
  189  * Convert from dos' idea of time to unix'. This will probably only be
  190  * called from the stat(), and fstat() system calls and so probably need
  191  * not be too efficient.
  192  */
  193 void
  194 dos2unixtime(dd, dt, dh, tsp)
  195         u_int dd;
  196         u_int dt;
  197         u_int dh;
  198         struct timespec *tsp;
  199 {
  200         u_long seconds;
  201         u_long month;
  202         u_long year;
  203         u_long days;
  204         u_short *months;
  205 
  206         if (dd == 0) {
  207                 /*
  208                  * Uninitialized field, return the epoch.
  209                  */
  210                 tsp->tv_sec = 0;
  211                 tsp->tv_nsec = 0;
  212                 return;
  213         }
  214         seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1)
  215             + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
  216             + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
  217             + dh / 100;
  218         /*
  219          * If the year, month, and day from the last conversion are the
  220          * same then use the saved value.
  221          */
  222         if (lastdosdate != dd) {
  223                 lastdosdate = dd;
  224                 days = 0;
  225                 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
  226                 days = year * 365;
  227                 days += year / 4 + 1;   /* add in leap days */
  228                 if ((year & 0x03) == 0)
  229                         days--;         /* if year is a leap year */
  230                 months = year & 0x03 ? regyear : leapyear;
  231                 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
  232                 if (month < 1 || month > 12) {
  233                         printf("dos2unixtime(): month value out of range (%ld)\n",
  234                             month);
  235                         month = 1;
  236                 }
  237                 if (month > 1)
  238                         days += months[month - 2];
  239                 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
  240                 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
  241         }
  242         tsp->tv_sec = seconds + lastseconds + (tz_minuteswest * 60)
  243              + adjkerntz;
  244              /* + daylight savings time correction */
  245         tsp->tv_nsec = (dh % 100) * 10000000;
  246 }
  247 
  248 /*
  249  * 0 - character disallowed in long file name.
  250  * 1 - character should be replaced by '_' in DOS file name, 
  251  *     and generation number inserted.
  252  * 2 - character ('.' and ' ') should be skipped in DOS file name,
  253  *     and generation number inserted.
  254  */
  255 static u_char
  256 unix2dos[256] = {
  257 /* iso8859-1 -> cp850 */
  258         0,    0,    0,    0,    0,    0,    0,    0,    /* 00-07 */
  259         0,    0,    0,    0,    0,    0,    0,    0,    /* 08-0f */
  260         0,    0,    0,    0,    0,    0,    0,    0,    /* 10-17 */
  261         0,    0,    0,    0,    0,    0,    0,    0,    /* 18-1f */
  262         2,    0x21, 0,    0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
  263         0x28, 0x29, 0,    1,    1,    0x2d, 2,    0,    /* 28-2f */
  264         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
  265         0x38, 0x39, 0,    1,    0,    1,    0,    0,    /* 38-3f */
  266         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
  267         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
  268         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
  269         0x58, 0x59, 0x5a, 1,    0,    1,    0x5e, 0x5f, /* 58-5f */
  270         0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 60-67 */
  271         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 68-6f */
  272         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 70-77 */
  273         0x58, 0x59, 0x5a, 0x7b, 0,    0x7d, 0x7e, 0,    /* 78-7f */
  274         0,    0,    0,    0,    0,    0,    0,    0,    /* 80-87 */
  275         0,    0,    0,    0,    0,    0,    0,    0,    /* 88-8f */
  276         0,    0,    0,    0,    0,    0,    0,    0,    /* 90-97 */
  277         0,    0,    0,    0,    0,    0,    0,    0,    /* 98-9f */
  278         0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
  279         0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
  280         0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
  281         0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
  282         0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
  283         0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
  284         0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
  285         0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
  286         0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
  287         0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
  288         0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
  289         0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
  290 };
  291 
  292 static u_char
  293 dos2unix[256] = {
  294 /* cp850 -> iso8859-1 */
  295         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 00-07 */
  296         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 08-0f */
  297         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 10-17 */
  298         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, /* 18-1f */
  299         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
  300         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
  301         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
  302         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
  303         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 40-47 */
  304         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 48-4f */
  305         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 50-57 */
  306         0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
  307         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
  308         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
  309         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
  310         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
  311         0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
  312         0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
  313         0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
  314         0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7, 0x3f, /* 98-9f */
  315         0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
  316         0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
  317         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xc1, 0xc2, 0xc0, /* b0-b7 */
  318         0xa9, 0x3f, 0x3f, 0x3f, 0x3f, 0xa2, 0xa5, 0x3f, /* b8-bf */
  319         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xe3, 0xc3, /* c0-c7 */
  320         0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0xa4, /* c8-cf */
  321         0xf0, 0xd0, 0xca, 0xcb, 0xc8, 0x3f, 0xcd, 0xce, /* d0-d7 */
  322         0xcf, 0x3f, 0x3f, 0x3f, 0x3f, 0xa6, 0xcc, 0x3f, /* d8-df */
  323         0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
  324         0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
  325         0xad, 0xb1, 0x3f, 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
  326         0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2, 0x3f, 0x3f, /* f8-ff */
  327 };
  328 
  329 static u_char
  330 u2l[256] = {
  331 /* tolower */
  332         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
  333         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
  334         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
  335         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
  336         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
  337         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
  338         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
  339         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
  340         0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
  341         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
  342         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
  343         0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
  344         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
  345         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
  346         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
  347         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
  348         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
  349         0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
  350         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
  351         0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
  352         0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
  353         0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
  354         0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
  355         0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
  356         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
  357         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
  358         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
  359         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
  360         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
  361         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
  362         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
  363         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
  364 };
  365 
  366 static u_char
  367 l2u[256] = {
  368 /* toupper */
  369         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
  370         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
  371         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
  372         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
  373         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 20-27 */
  374         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 28-2f */
  375         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 30-37 */
  376         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 38-3f */
  377         0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 40-47 */
  378         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 48-4f */
  379         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 50-57 */
  380         0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 58-5f */
  381         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 60-67 */
  382         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 68-6f */
  383         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 70-77 */
  384         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 78-7f */
  385         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
  386         0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
  387         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
  388         0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
  389         0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
  390         0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
  391         0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
  392         0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
  393         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
  394         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
  395         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
  396         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
  397         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
  398         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
  399         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
  400         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
  401 };
  402 
  403 /*
  404  * DOS filenames are made of 2 parts, the name part and the extension part.
  405  * The name part is 8 characters long and the extension part is 3
  406  * characters long.  They may contain trailing blanks if the name or
  407  * extension are not long enough to fill their respective fields.
  408  */
  409 
  410 /*
  411  * Convert a DOS filename to a unix filename. And, return the number of
  412  * characters in the resulting unix filename excluding the terminating
  413  * null.
  414  */
  415 int
  416 dos2unixfn(dn, un, lower, pmp)
  417         u_char dn[11];
  418         u_char *un;
  419         int lower;
  420         struct msdosfsmount *pmp;
  421 {
  422         size_t i;
  423         int thislong = 0;
  424         u_int16_t c;
  425 
  426         /*
  427          * If first char of the filename is SLOT_E5 (0x05), then the real
  428          * first char of the filename should be 0xe5. But, they couldn't
  429          * just have a 0xe5 mean 0xe5 because that is used to mean a freed
  430          * directory slot. Another dos quirk.
  431          */
  432         if (*dn == SLOT_E5)
  433                 *dn = 0xe5;
  434 
  435         /*
  436          * Copy the name portion into the unix filename string.
  437          */
  438         for (i = 8; i > 0 && *dn != ' ';) {
  439                 c = dos2unixchr((const u_char **)&dn, &i, lower & LCASE_BASE,
  440                     pmp);
  441                 if (c & 0xff00) {
  442                         *un++ = c >> 8;
  443                         thislong++;
  444                 }
  445                 *un++ = c;
  446                 thislong++;
  447         }
  448         dn += i;
  449 
  450         /*
  451          * Now, if there is an extension then put in a period and copy in
  452          * the extension.
  453          */
  454         if (*dn != ' ') {
  455                 *un++ = '.';
  456                 thislong++;
  457                 for (i = 3; i > 0 && *dn != ' ';) {
  458                         c = dos2unixchr((const u_char **)&dn, &i,
  459                             lower & LCASE_EXT, pmp);
  460                         if (c & 0xff00) {
  461                                 *un++ = c >> 8;
  462                                 thislong++;
  463                         }
  464                         *un++ = c;
  465                         thislong++;
  466                 }
  467         }
  468         *un++ = 0;
  469 
  470         return (thislong);
  471 }
  472 
  473 /*
  474  * Convert a unix filename to a DOS filename according to Win95 rules.
  475  * If applicable and gen is not 0, it is inserted into the converted
  476  * filename as a generation number.
  477  * Returns
  478  *      0 if name couldn't be converted
  479  *      1 if the converted name is the same as the original
  480  *        (no long filename entry necessary for Win95)
  481  *      2 if conversion was successful
  482  *      3 if conversion was successful and generation number was inserted
  483  */
  484 int
  485 unix2dosfn(un, dn, unlen, gen, pmp)
  486         const u_char *un;
  487         u_char dn[12];
  488         size_t unlen;
  489         u_int gen;
  490         struct msdosfsmount *pmp;
  491 {
  492         ssize_t i, j;
  493         int l;
  494         int conv = 1;
  495         const u_char *cp, *dp, *dp1;
  496         u_char gentext[6], *wcp;
  497         u_int16_t c;
  498 
  499         /*
  500          * Fill the dos filename string with blanks. These are DOS's pad
  501          * characters.
  502          */
  503         for (i = 0; i < 11; i++)
  504                 dn[i] = ' ';
  505         dn[11] = 0;
  506 
  507         /*
  508          * The filenames "." and ".." are handled specially, since they
  509          * don't follow dos filename rules.
  510          */
  511         if (un[0] == '.' && unlen == 1) {
  512                 dn[0] = '.';
  513                 return gen <= 1;
  514         }
  515         if (un[0] == '.' && un[1] == '.' && unlen == 2) {
  516                 dn[0] = '.';
  517                 dn[1] = '.';
  518                 return gen <= 1;
  519         }
  520 
  521         /*
  522          * Filenames with only blanks and dots are not allowed!
  523          */
  524         for (cp = un, i = unlen; --i >= 0; cp++)
  525                 if (*cp != ' ' && *cp != '.')
  526                         break;
  527         if (i < 0)
  528                 return 0;
  529 
  530 
  531         /*
  532          * Filenames with some characters are not allowed!
  533          */
  534         for (cp = un, i = unlen; i > 0;)
  535                 if (unix2doschr(&cp, (size_t *)&i, pmp) == 0)
  536                         return 0;
  537 
  538         /*
  539          * Now find the extension
  540          * Note: dot as first char doesn't start extension
  541          *       and trailing dots and blanks are ignored
  542          * Note(2003/7): It seems recent Windows has
  543          *       defferent rule than this code, that Windows
  544          *       ignores all dots before extension, and use all
  545          *       chars as filename except for dots.
  546          */
  547         dp = dp1 = 0;
  548         for (cp = un + 1, i = unlen - 1; --i >= 0;) {
  549                 switch (*cp++) {
  550                 case '.':
  551                         if (!dp1)
  552                                 dp1 = cp;
  553                         break;
  554                 case ' ':
  555                         break;
  556                 default:
  557                         if (dp1)
  558                                 dp = dp1;
  559                         dp1 = 0;
  560                         break;
  561                 }
  562         }
  563 
  564         /*
  565          * Now convert it (this part is for extension).
  566          * As Windows XP do, if it's not ascii char,
  567          * this function should return 2 or 3, so that checkng out Unicode name.
  568          */
  569         if (dp) {
  570                 if (dp1)
  571                         l = dp1 - dp;
  572                 else
  573                         l = unlen - (dp - un);
  574                 for (cp = dp, i = l, j = 8; i > 0 && j < 11; j++) {
  575                         c = unix2doschr(&cp, (size_t *)&i, pmp);
  576                         if (c & 0xff00) {
  577                                 dn[j] = c >> 8;
  578                                 if (++j < 11) {
  579                                         dn[j] = c;
  580                                         if (conv != 3)
  581                                                 conv = 2;
  582                                         continue;
  583                                 } else {
  584                                         conv = 3;
  585                                         dn[j-1] = ' ';
  586                                         break;
  587                                 }
  588                         } else {
  589                                 dn[j] = c;
  590                         }
  591                         if (((dn[j] & 0x80) || *(cp - 1) != dn[j]) && conv != 3)
  592                                 conv = 2;
  593                         if (dn[j] == 1) {
  594                                 conv = 3;
  595                                 dn[j] = '_';
  596                         }
  597                         if (dn[j] == 2) {
  598                                 conv = 3;
  599                                 dn[j--] = ' ';
  600                         }
  601                 }
  602                 if (i > 0)
  603                         conv = 3;
  604                 dp--;
  605         } else {
  606                 for (dp = cp; *--dp == ' ' || *dp == '.';);
  607                 dp++;
  608         }
  609 
  610         /*
  611          * Now convert the rest of the name
  612          */
  613         for (i = dp - un, j = 0; un < dp && j < 8; j++) {
  614                 c = unix2doschr(&un, &i, pmp);
  615                 if (c & 0xff00) {
  616                         dn[j] = c >> 8;
  617                         if (++j < 8) {
  618                                 dn[j] = c;
  619                                 if (conv != 3)
  620                                         conv = 2;
  621                                 continue;
  622                         } else {
  623                                 conv = 3;
  624                                 dn[j-1] = ' ';
  625                                 break;
  626                         }
  627                 } else {
  628                         dn[j] = c;
  629                 }
  630                 if (((dn[j] & 0x80) || *(un - 1) != dn[j]) && conv != 3)
  631                         conv = 2;
  632                 if (dn[j] == 1) {
  633                         conv = 3;
  634                         dn[j] = '_';
  635                 }
  636                 if (dn[j] == 2) {
  637                         conv = 3;
  638                         dn[j--] = ' ';
  639                 }
  640         }
  641         if (un < dp)
  642                 conv = 3;
  643         /*
  644          * If we didn't have any chars in filename,
  645          * generate a default
  646          */
  647         if (!j)
  648                 dn[0] = '_';
  649 
  650         /*
  651          * If there wasn't any char dropped,
  652          * there is no place for generation numbers
  653          */
  654         if (conv != 3) {
  655                 if (gen > 1)
  656                         conv = 0;
  657                 goto done;
  658         }
  659 
  660         /*
  661          * Now insert the generation number into the filename part
  662          */
  663         if (gen == 0)
  664                 goto done;
  665         for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
  666                 *--wcp = gen % 10 + '';
  667         if (gen) {
  668                 conv = 0;
  669                 goto done;
  670         }
  671         for (i = 8; dn[--i] == ' ';);
  672         i++;
  673         if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
  674                 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
  675         /*
  676          * Correct posision to where insert the generation number
  677          */
  678         cp = dn;
  679         i -= mbsadjpos((const char**)&cp, i, unlen, 1, pmp->pm_flags, pmp->pm_d2u);
  680 
  681         dn[i++] = '~';
  682         while (wcp < gentext + sizeof(gentext))
  683                 dn[i++] = *wcp++;
  684 
  685         /*
  686          * Tail of the filename should be space
  687          */
  688         while (i < 8)
  689                 dn[i++] = ' ';
  690         conv = 3;
  691 
  692 done:
  693         /*
  694          * The first character cannot be E5,
  695          * because that means a deleted entry
  696          */
  697         if (dn[0] == 0xe5)
  698                 dn[0] = SLOT_E5;
  699 
  700         return conv;
  701 }
  702 
  703 /*
  704  * Create a Win95 long name directory entry
  705  * Note: assumes that the filename is valid,
  706  *       i.e. doesn't consist solely of blanks and dots
  707  */
  708 int
  709 unix2winfn(un, unlen, wep, cnt, chksum, pmp)
  710         const u_char *un;
  711         size_t unlen;
  712         struct winentry *wep;
  713         int cnt;
  714         int chksum;
  715         struct msdosfsmount *pmp;
  716 {
  717         u_int8_t *wcp;
  718         int i, end;
  719         u_int16_t code;
  720 
  721         /*
  722          * Drop trailing blanks and dots
  723          */
  724         unlen = winLenFixup(un, unlen);
  725 
  726         /*
  727          * Cut *un for this slot
  728          */
  729         unlen = mbsadjpos((const char **)&un, unlen, (cnt - 1) * WIN_CHARS, 2,
  730                           pmp->pm_flags, pmp->pm_u2w);
  731 
  732         /*
  733          * Initialize winentry to some useful default
  734          */
  735         for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
  736         wep->weCnt = cnt;
  737         wep->weAttributes = ATTR_WIN95;
  738         wep->weReserved1 = 0;
  739         wep->weChksum = chksum;
  740         wep->weReserved2 = 0;
  741 
  742         /*
  743          * Now convert the filename parts
  744          */
  745         end = 0;
  746         for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0 && !end;) {
  747                 code = unix2winchr(&un, &unlen, 0, pmp);
  748                 *wcp++ = code;
  749                 *wcp++ = code >> 8;
  750                 if (!code)
  751                         end = WIN_LAST;
  752         }
  753         for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0 && !end;) {
  754                 code = unix2winchr(&un, &unlen, 0, pmp);
  755                 *wcp++ = code;
  756                 *wcp++ = code >> 8;
  757                 if (!code)
  758                         end = WIN_LAST;
  759         }
  760         for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0 && !end;) {
  761                 code = unix2winchr(&un, &unlen, 0, pmp);
  762                 *wcp++ = code;
  763                 *wcp++ = code >> 8;
  764                 if (!code)
  765                         end = WIN_LAST;
  766         }
  767         if (*un == '\0')
  768                 end = WIN_LAST;
  769         wep->weCnt |= end;
  770         return !end;
  771 }
  772 
  773 /*
  774  * Compare our filename to the one in the Win95 entry
  775  * Returns the checksum or -1 if no match
  776  */
  777 int
  778 winChkName(un, unlen, chksum, pmp)
  779         const u_char *un;
  780         size_t unlen;
  781         int chksum;
  782         struct msdosfsmount *pmp;
  783 {
  784         size_t len;
  785         u_int16_t c1, c2;
  786         u_char *np;
  787         struct dirent dirbuf;
  788 
  789         /*
  790          * We alread have winentry in mbnambuf
  791          */
  792         if (!mbnambuf_flush(&dirbuf) || !dirbuf.d_namlen)
  793                 return -1;
  794 
  795 #ifdef MSDOSFS_DEBUG
  796         printf("winChkName(): un=%s:%d,d_name=%s:%d\n", un, unlen,
  797                                                         dirbuf.d_name,
  798                                                         dirbuf.d_namlen);
  799 #endif
  800 
  801         /*
  802          * Compare the name parts
  803          */
  804         len = dirbuf.d_namlen;
  805         if (unlen != len)
  806                 return -2;
  807 
  808         for (np = dirbuf.d_name; unlen > 0 && len > 0;) {
  809                 /*
  810                  * Comparison must be case insensitive, because FAT disallows
  811                  * to look up or create files in case sensitive even when
  812                  * it's a long file name.
  813                  */
  814                 c1 = unix2winchr((const u_char **)&np, &len, LCASE_BASE, pmp);
  815                 c2 = unix2winchr(&un, &unlen, LCASE_BASE, pmp);
  816                 if (c1 != c2)
  817                         return -2;
  818         }
  819         return chksum;
  820 }
  821 
  822 /*
  823  * Convert Win95 filename to dirbuf.
  824  * Returns the checksum or -1 if impossible
  825  */
  826 int
  827 win2unixfn(wep, chksum, pmp)
  828         struct winentry *wep;
  829         int chksum;
  830         struct msdosfsmount *pmp;
  831 {
  832         u_int8_t *cp;
  833         u_int8_t *np, name[WIN_CHARS * 2 + 1];
  834         u_int16_t code;
  835         int i;
  836 
  837         if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
  838             || !(wep->weCnt&WIN_CNT))
  839                 return -1;
  840 
  841         /*
  842          * First compare checksums
  843          */
  844         if (wep->weCnt&WIN_LAST) {
  845                 chksum = wep->weChksum;
  846         } else if (chksum != wep->weChksum)
  847                 chksum = -1;
  848         if (chksum == -1)
  849                 return -1;
  850 
  851         /*
  852          * Convert the name parts
  853          */
  854         np = name;
  855         for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
  856                 code = (cp[1] << 8) | cp[0];
  857                 switch (code) {
  858                 case 0:
  859                         *np = '\0';
  860                         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
  861                         return chksum;
  862                 case '/':
  863                         *np = '\0';
  864                         return -1;
  865                 default:
  866                         code = win2unixchr(code, pmp);
  867                         if (code & 0xff00)
  868                                 *np++ = code >> 8;
  869                         *np++ = code;
  870                         break;
  871                 }
  872                 cp += 2;
  873         }
  874         for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
  875                 code = (cp[1] << 8) | cp[0];
  876                 switch (code) {
  877                 case 0:
  878                         *np = '\0';
  879                         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
  880                         return chksum;
  881                 case '/':
  882                         *np = '\0';
  883                         return -1;
  884                 default:
  885                         code = win2unixchr(code, pmp);
  886                         if (code & 0xff00)
  887                                 *np++ = code >> 8;
  888                         *np++ = code;
  889                         break;
  890                 }
  891                 cp += 2;
  892         }
  893         for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
  894                 code = (cp[1] << 8) | cp[0];
  895                 switch (code) {
  896                 case 0:
  897                         *np = '\0';
  898                         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
  899                         return chksum;
  900                 case '/':
  901                         *np = '\0';
  902                         return -1;
  903                 default:
  904                         code = win2unixchr(code, pmp);
  905                         if (code & 0xff00)
  906                                 *np++ = code >> 8;
  907                         *np++ = code;
  908                         break;
  909                 }
  910                 cp += 2;
  911         }
  912         *np = '\0';
  913         mbnambuf_write(name, (wep->weCnt & WIN_CNT) - 1);
  914         return chksum;
  915 }
  916 
  917 /*
  918  * Compute the unrolled checksum of a DOS filename for Win95 LFN use.
  919  */
  920 u_int8_t
  921 winChksum(name)
  922         u_int8_t *name;
  923 {
  924         u_int8_t s;
  925 
  926         s = name[0];
  927         s = ((s << 7) | (s >> 1)) + name[1];
  928         s = ((s << 7) | (s >> 1)) + name[2];
  929         s = ((s << 7) | (s >> 1)) + name[3];
  930         s = ((s << 7) | (s >> 1)) + name[4];
  931         s = ((s << 7) | (s >> 1)) + name[5];
  932         s = ((s << 7) | (s >> 1)) + name[6];
  933         s = ((s << 7) | (s >> 1)) + name[7];
  934         s = ((s << 7) | (s >> 1)) + name[8];
  935         s = ((s << 7) | (s >> 1)) + name[9];
  936         s = ((s << 7) | (s >> 1)) + name[10];
  937 
  938         return (s);
  939 }
  940 
  941 /*
  942  * Determine the number of slots necessary for Win95 names
  943  */
  944 int
  945 winSlotCnt(un, unlen, pmp)
  946         const u_char *un;
  947         size_t unlen;
  948         struct msdosfsmount *pmp;
  949 {
  950         size_t wlen;
  951         char wn[WIN_MAXLEN * 2 + 1], *wnp;
  952 
  953         unlen = winLenFixup(un, unlen);
  954 
  955         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
  956                 wlen = WIN_MAXLEN * 2;
  957                 wnp = wn;
  958                 msdosfs_iconv->conv(pmp->pm_u2w, (const char **)&un, &unlen, &wnp, &wlen);
  959                 if (unlen > 0)
  960                         return 0;
  961                 return howmany(WIN_MAXLEN - wlen/2, WIN_CHARS);
  962         }
  963 
  964         if (unlen > WIN_MAXLEN)
  965                 return 0;
  966         return howmany(unlen, WIN_CHARS);
  967 }
  968 
  969 /*
  970  * Determine the number of bytes neccesary for Win95 names
  971  */
  972 size_t
  973 winLenFixup(un, unlen)
  974         const u_char* un;
  975         size_t unlen;
  976 {
  977         for (un += unlen; unlen > 0; unlen--)
  978                 if (*--un != ' ' && *un != '.')
  979                         break;
  980         return unlen;
  981 }
  982 
  983 /*
  984  * Store an area with multi byte string instr, and reterns left
  985  * byte of instr and moves pointer forward. The area's size is
  986  * inlen or outlen.
  987  */
  988 static int
  989 mbsadjpos(const char **instr, size_t inlen, size_t outlen, int weight, int flag, void *handle)
  990 {
  991         char *outp, outstr[outlen * weight + 1];
  992 
  993         if (flag & MSDOSFSMNT_KICONV && msdosfs_iconv) {
  994                 outp = outstr;
  995                 outlen *= weight;
  996                 msdosfs_iconv->conv(handle, instr, &inlen, &outp, &outlen);
  997                 return (inlen);
  998         }
  999 
 1000         (*instr) += min(inlen, outlen);
 1001         return (inlen - min(inlen, outlen));
 1002 }
 1003 
 1004 /*
 1005  * Convert DOS char to Local char
 1006  */
 1007 static u_int16_t
 1008 dos2unixchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
 1009 {
 1010         u_char c;
 1011         char *outp, outbuf[3];
 1012         u_int16_t wc;
 1013         size_t len, olen;
 1014 
 1015         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
 1016                 olen = len = 2;
 1017                 outp = outbuf;
 1018 
 1019                 if (lower & (LCASE_BASE | LCASE_EXT))
 1020                         msdosfs_iconv->convchr_case(pmp->pm_d2u, (const char **)instr,
 1021                                                   ilen, &outp, &olen, KICONV_LOWER);
 1022                 else
 1023                         msdosfs_iconv->convchr(pmp->pm_d2u, (const char **)instr,
 1024                                              ilen, &outp, &olen);
 1025                 len -= olen;
 1026 
 1027                 /*
 1028                  * return '?' if failed to convert
 1029                  */
 1030                 if (len == 0) {
 1031                         (*ilen)--;
 1032                         (*instr)++;
 1033                         return ('?');
 1034                 }
 1035 
 1036                 wc = 0;
 1037                 while(len--)
 1038                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
 1039                 return (wc);
 1040         }
 1041 
 1042         (*ilen)--;
 1043         c = *(*instr)++;
 1044         c = dos2unix[c];
 1045         if (lower & (LCASE_BASE | LCASE_EXT))
 1046                 c = u2l[c];
 1047         return ((u_int16_t)c);
 1048 }
 1049 
 1050 /*
 1051  * Convert Local char to DOS char
 1052  */
 1053 static u_int16_t
 1054 unix2doschr(const u_char **instr, size_t *ilen, struct msdosfsmount *pmp)
 1055 {
 1056         u_char c;
 1057         char *up, *outp, unicode[3], outbuf[3];
 1058         u_int16_t wc;
 1059         size_t len, ucslen, unixlen, olen;
 1060 
 1061         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
 1062                 /*
 1063                  * to hide an invisible character, using a unicode filter
 1064                  */
 1065                 ucslen = 2;
 1066                 len = *ilen;
 1067                 up = unicode;
 1068                 msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
 1069                                      ilen, &up, &ucslen);
 1070                 unixlen = len - *ilen;
 1071 
 1072                 /*
 1073                  * cannot be converted
 1074                  */
 1075                 if (unixlen == 0) {
 1076                         (*ilen)--;
 1077                         (*instr)++;
 1078                         return (0);
 1079                 }
 1080 
 1081                 /*
 1082                  * return magic number for ascii char
 1083                  */
 1084                 if (unixlen == 1) {
 1085                         c = *(*instr -1);
 1086                         if (! (c & 0x80)) {
 1087                                 c = unix2dos[c];
 1088                                 if (c <= 2)
 1089                                         return (c);
 1090                         }
 1091                 }
 1092 
 1093                 /*
 1094                  * now convert using libiconv
 1095                  */
 1096                 *instr -= unixlen;
 1097                 *ilen = len;
 1098 
 1099                 olen = len = 2;
 1100                 outp = outbuf;
 1101                 msdosfs_iconv->convchr_case(pmp->pm_u2d, (const char **)instr,
 1102                                           ilen, &outp, &olen, KICONV_FROM_UPPER);
 1103                 len -= olen;
 1104 
 1105                 /*
 1106                  * cannot be converted, but has unicode char, should return magic number
 1107                  */
 1108                 if (len == 0) {
 1109                         (*ilen) -= unixlen;
 1110                         (*instr) += unixlen;
 1111                         return (1);
 1112                 }
 1113 
 1114                 wc = 0;
 1115                 while(len--)
 1116                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
 1117                 return (wc);
 1118         }
 1119 
 1120         (*ilen)--;
 1121         c = *(*instr)++;
 1122         c = l2u[c];
 1123         c = unix2dos[c];
 1124         return ((u_int16_t)c);
 1125 }
 1126 
 1127 /*
 1128  * Convert Windows char to Local char
 1129  */
 1130 static u_int16_t
 1131 win2unixchr(u_int16_t wc, struct msdosfsmount *pmp)
 1132 {
 1133         u_char *inp, *outp, inbuf[3], outbuf[3];
 1134         size_t ilen, olen, len;
 1135 
 1136         if (wc == 0)
 1137                 return (0);
 1138 
 1139         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
 1140                 inbuf[0] = (u_char)(wc>>8);
 1141                 inbuf[1] = (u_char)wc;
 1142                 inbuf[2] = '\0';
 1143 
 1144                 ilen = olen = len = 2;
 1145                 inp = inbuf;
 1146                 outp = outbuf;
 1147                 msdosfs_iconv->convchr(pmp->pm_w2u, (const char **)&inp, &ilen,
 1148                                      (char **)&outp, &olen);
 1149                 len -= olen;
 1150 
 1151                 /*
 1152                  * return '?' if failed to convert
 1153                  */
 1154                 if (len == 0) {
 1155                         wc = '?';
 1156                         return (wc);
 1157                 }
 1158 
 1159                 wc = 0;
 1160                 while(len--)
 1161                         wc |= (*(outp - len - 1) & 0xff) << (len << 3);
 1162                 return (wc);
 1163         }
 1164 
 1165         if (wc & 0xff00)
 1166                 wc = '?';
 1167 
 1168         return (wc);
 1169 }
 1170 
 1171 /*
 1172  * Convert Local char to Windows char
 1173  */
 1174 static u_int16_t
 1175 unix2winchr(const u_char **instr, size_t *ilen, int lower, struct msdosfsmount *pmp)
 1176 {
 1177         u_char *outp, outbuf[3];
 1178         u_int16_t wc;
 1179         size_t olen;
 1180 
 1181         if (*ilen == 0)
 1182                 return (0);
 1183 
 1184         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
 1185                 outp = outbuf;
 1186                 olen = 2;
 1187                 if (lower & (LCASE_BASE | LCASE_EXT))
 1188                         msdosfs_iconv->convchr_case(pmp->pm_u2w, (const char **)instr,
 1189                                                   ilen, (char **)&outp, &olen,
 1190                                                   KICONV_FROM_LOWER);
 1191                 else
 1192                         msdosfs_iconv->convchr(pmp->pm_u2w, (const char **)instr,
 1193                                              ilen, (char **)&outp, &olen);
 1194 
 1195                 /*
 1196                  * return '' if end of filename
 1197                  */
 1198                 if (olen == 2)
 1199                         return (0);
 1200 
 1201                 wc = (outbuf[0]<<8) | outbuf[1];
 1202 
 1203                 return (wc);
 1204         }
 1205 
 1206         (*ilen)--;
 1207         wc = (*instr)[0];
 1208         if (lower & (LCASE_BASE | LCASE_EXT))
 1209                 wc = u2l[wc];
 1210         (*instr)++;
 1211         return (wc);
 1212 }
 1213 
 1214 /*
 1215  * Initialize the temporary concatenation buffer (once) and mark it as
 1216  * empty on subsequent calls.
 1217  */
 1218 void
 1219 mbnambuf_init(void)
 1220 {
 1221 
 1222         if (nambuf_ptr == NULL) { 
 1223                 nambuf_ptr = malloc(MAXNAMLEN + 1, M_MSDOSFSMNT, M_WAITOK);
 1224                 nambuf_ptr[MAXNAMLEN] = '\0';
 1225         }
 1226         nambuf_len = 0;
 1227         nambuf_last_id = -1;
 1228 }
 1229 
 1230 /*
 1231  * Fill out our concatenation buffer with the given substring, at the offset
 1232  * specified by its id.  Since this function must be called with ids in
 1233  * descending order, we take advantage of the fact that ASCII substrings are
 1234  * exactly WIN_CHARS in length.  For non-ASCII substrings, we shift all
 1235  * previous (i.e. higher id) substrings upwards to make room for this one.
 1236  * This only penalizes portions of substrings that contain more than
 1237  * WIN_CHARS bytes when they are first encountered.
 1238  */
 1239 void
 1240 mbnambuf_write(char *name, int id)
 1241 {
 1242         size_t count;
 1243         char *slot;
 1244 
 1245         KASSERT(nambuf_len == 0 || id == nambuf_last_id - 1,
 1246             ("non-decreasing id, id %d last id %d", id, nambuf_last_id));
 1247 
 1248         /* Store this substring in a WIN_CHAR-aligned slot. */
 1249         slot = nambuf_ptr + (id * WIN_CHARS);
 1250         count = strlen(name);
 1251         if (nambuf_len + count > MAXNAMLEN) {
 1252                 printf("msdosfs: file name %zu too long\n", nambuf_len + count);
 1253                 return;
 1254         }
 1255 
 1256         /* Shift suffix upwards by the amount length exceeds WIN_CHARS. */
 1257         if (count > WIN_CHARS && nambuf_len != 0)
 1258                 bcopy(slot + WIN_CHARS, slot + count, nambuf_len);
 1259 
 1260         /* Copy in the substring to its slot and update length so far. */
 1261         bcopy(name, slot, count);
 1262         nambuf_len += count;
 1263         nambuf_last_id = id;
 1264 }
 1265 
 1266 /*
 1267  * Take the completed string and use it to setup the struct dirent.
 1268  * Be sure to always nul-terminate the d_name and then copy the string
 1269  * from our buffer.  Note that this function assumes the full string has
 1270  * been reassembled in the buffer.  If it's called before all substrings
 1271  * have been written via mbnambuf_write(), the result will be incorrect.
 1272  */
 1273 char *
 1274 mbnambuf_flush(struct dirent *dp)
 1275 {
 1276 
 1277         if (nambuf_len > sizeof(dp->d_name) - 1) {
 1278                 mbnambuf_init();
 1279                 return (NULL);
 1280         }
 1281         bcopy(nambuf_ptr, dp->d_name, nambuf_len);
 1282         dp->d_name[nambuf_len] = '\0';
 1283         dp->d_namlen = nambuf_len;
 1284 
 1285         mbnambuf_init();
 1286         return (dp->d_name);
 1287 }

Cache object: 6b02658f71e730be2c3a67447fc4b4e6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.