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 /*      $NetBSD: msdosfs_conv.c,v 1.2 2003/09/07 22:09:11 itojun Exp $  */
    2 
    3 /*-
    4  * Copyright (C) 1995, 1997 Wolfgang Solfrank.
    5  * Copyright (C) 1995, 1997 TooLs GmbH.
    6  * All rights reserved.
    7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. All advertising materials mentioning features or use of this software
   18  *    must display the following acknowledgement:
   19  *      This product includes software developed by TooLs GmbH.
   20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
   21  *    derived from this software without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
   24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 /*
   35  * Written by Paul Popelka (paulp@uts.amdahl.com)
   36  *
   37  * You can do anything you want with this software, just don't say you wrote
   38  * it, and don't remove this notice.
   39  *
   40  * This software is provided "as is".
   41  *
   42  * The author supplies this software to be publicly redistributed on the
   43  * understanding that the author is not responsible for the correct
   44  * functioning of this software in any circumstances and is not liable for
   45  * any damages caused by this software.
   46  *
   47  * October 1992
   48  */
   49 
   50 #include <sys/cdefs.h>
   51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_conv.c,v 1.2 2003/09/07 22:09:11 itojun Exp $");
   52 
   53 /*
   54  * System include files.
   55  */
   56 #include <sys/param.h>
   57 #include <sys/systm.h>
   58 #include <sys/time.h>
   59 #include <sys/kernel.h>
   60 #include <sys/dirent.h>
   61 #include <sys/vnode.h>
   62 
   63 /*
   64  * MSDOSFS include files.
   65  */
   66 #include <fs/msdosfs/direntry.h>
   67 #include <fs/msdosfs/denode.h>
   68 
   69 /*
   70  * Days in each month in a regular year.
   71  */
   72 u_short const regyear[] = {
   73         31, 28, 31, 30, 31, 30,
   74         31, 31, 30, 31, 30, 31
   75 };
   76 
   77 /*
   78  * Days in each month in a leap year.
   79  */
   80 u_short const leapyear[] = {
   81         31, 29, 31, 30, 31, 30,
   82         31, 31, 30, 31, 30, 31
   83 };
   84 
   85 /*
   86  * Variables used to remember parts of the last time conversion.  Maybe we
   87  * can avoid a full conversion.
   88  */
   89 u_long lasttime;
   90 u_long lastday;
   91 u_short lastddate;
   92 u_short lastdtime;
   93 
   94 /*
   95  * Convert the unix version of time to dos's idea of time to be used in
   96  * file timestamps. The passed in unix time is assumed to be in GMT.
   97  */
   98 void
   99 unix2dostime(tsp, gmtoff, ddp, dtp, dhp)
  100         struct timespec *tsp;
  101         int gmtoff;
  102         u_int16_t *ddp;
  103         u_int16_t *dtp;
  104         u_int8_t *dhp;
  105 {
  106         u_long t;
  107         u_long days;
  108         u_long inc;
  109         u_long year;
  110         u_long month;
  111         const u_short *months;
  112 
  113         /*
  114          * If the time from the last conversion is the same as now, then
  115          * skip the computations and use the saved result.
  116          */
  117         t = tsp->tv_sec + gmtoff; /* time zone correction */
  118         t &= ~1;
  119         if (lasttime != t) {
  120                 lasttime = t;
  121                 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT)
  122                     + (((t / 60) % 60) << DT_MINUTES_SHIFT)
  123                     + (((t / 3600) % 24) << DT_HOURS_SHIFT);
  124 
  125                 /*
  126                  * If the number of days since 1970 is the same as the last
  127                  * time we did the computation then skip all this leap year
  128                  * and month stuff.
  129                  */
  130                 days = t / (24 * 60 * 60);
  131                 if (days != lastday) {
  132                         lastday = days;
  133                         for (year = 1970;; year++) {
  134                                 inc = year & 0x03 ? 365 : 366;
  135                                 if (days < inc)
  136                                         break;
  137                                 days -= inc;
  138                         }
  139                         months = year & 0x03 ? regyear : leapyear;
  140                         for (month = 0; month < 12; month++) {
  141                                 if (days < months[month])
  142                                         break;
  143                                 days -= months[month];
  144                         }
  145                         lastddate = ((days + 1) << DD_DAY_SHIFT)
  146                             + ((month + 1) << DD_MONTH_SHIFT);
  147                         /*
  148                          * Remember dos's idea of time is relative to 1980.
  149                          * unix's is relative to 1970.  If somehow we get a
  150                          * time before 1980 then don't give totally crazy
  151                          * results.
  152                          */
  153                         if (year > 1980)
  154                                 lastddate += (year - 1980) << DD_YEAR_SHIFT;
  155                 }
  156         }
  157         if (dtp)
  158                 *dtp = lastdtime;
  159         if (dhp)
  160                 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000;
  161 
  162         *ddp = lastddate;
  163 }
  164 
  165 /*
  166  * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that
  167  * interval there were 8 regular years and 2 leap years.
  168  */
  169 #define SECONDSTO1980   (((8 * 365) + (2 * 366)) * (24 * 60 * 60))
  170 
  171 u_short lastdosdate;
  172 u_long lastseconds;
  173 
  174 /*
  175  * Convert from dos' idea of time to unix'. This will probably only be
  176  * called from the stat(), and fstat() system calls and so probably need
  177  * not be too efficient.
  178  */
  179 void
  180 dos2unixtime(dd, dt, dh, gmtoff, tsp)
  181         u_int dd;
  182         u_int dt;
  183         u_int dh;
  184         int gmtoff;
  185         struct timespec *tsp;
  186 {
  187         u_long seconds;
  188         u_long m, month;
  189         u_long y, year;
  190         u_long days;
  191         const u_short *months;
  192 
  193         if (dd == 0) {
  194                 /*
  195                  * Uninitialized field, return the epoch.
  196                  */
  197                 tsp->tv_sec = 0;
  198                 tsp->tv_nsec = 0;
  199                 return;
  200         }
  201         seconds = ((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) * 2
  202             + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60
  203             + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600
  204             + dh / 100;
  205         /*
  206          * If the year, month, and day from the last conversion are the
  207          * same then use the saved value.
  208          */
  209         if (lastdosdate != dd) {
  210                 lastdosdate = dd;
  211                 days = 0;
  212                 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT;
  213                 for (y = 0; y < year; y++)
  214                         days += y & 0x03 ? 365 : 366;
  215                 months = year & 0x03 ? regyear : leapyear;
  216                 /*
  217                  * Prevent going from 0 to 0xffffffff in the following
  218                  * loop.
  219                  */
  220                 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT;
  221                 if (month == 0) {
  222                         printf("dos2unixtime(): month value out of range (%ld)\n",
  223                             month);
  224                         month = 1;
  225                 }
  226                 for (m = 0; m < month - 1; m++)
  227                         days += months[m];
  228                 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1;
  229                 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980;
  230         }
  231         tsp->tv_sec = seconds + lastseconds;
  232         tsp->tv_sec -= gmtoff;  /* time zone correction */
  233         tsp->tv_nsec = (dh % 100) * 10000000;
  234 }
  235 
  236 static const u_char
  237 unix2dos[256] = {
  238         0,    0,    0,    0,    0,    0,    0,    0,    /* 00-07 */
  239         0,    0,    0,    0,    0,    0,    0,    0,    /* 08-0f */
  240         0,    0,    0,    0,    0,    0,    0,    0,    /* 10-17 */
  241         0,    0,    0,    0,    0,    0,    0,    0,    /* 18-1f */
  242         0,    '!',  0,    '#',  '$',  '%',  '&',  '\'', /* 20-27 */
  243         '(',  ')',  0,    '+',  0,    '-',  0,    0,    /* 28-2f */
  244         '',  '1',  '2',  '3',  '4',  '5',  '6',  '7',  /* 30-37 */
  245         '8',  '9',  0,    0,    0,    0,    0,    0,    /* 38-3f */
  246         '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',  /* 40-47 */
  247         'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',  /* 48-4f */
  248         'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',  /* 50-57 */
  249         'X',  'Y',  'Z',  0,    0,    0,    '^',  '_',  /* 58-5f */
  250         '`',  'A',  'B',  'C',  'D',  'E',  'F',  'G',  /* 60-67 */
  251         'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',  /* 68-6f */
  252         'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',  /* 70-77 */
  253         'X',  'Y',  'Z',  '{',  0,    '}',  '~',  0,    /* 78-7f */
  254         0,    0,    0,    0,    0,    0,    0,    0,    /* 80-87 */
  255         0,    0,    0,    0,    0,    0,    0,    0,    /* 88-8f */
  256         0,    0,    0,    0,    0,    0,    0,    0,    /* 90-97 */
  257         0,    0,    0,    0,    0,    0,    0,    0,    /* 98-9f */
  258         0,    0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, /* a0-a7 */
  259         0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, /* a8-af */
  260         0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, /* b0-b7 */
  261         0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, /* b8-bf */
  262         0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* c0-c7 */
  263         0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* c8-cf */
  264         0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, /* d0-d7 */
  265         0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, /* d8-df */
  266         0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, /* e0-e7 */
  267         0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, /* e8-ef */
  268         0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0xf6, /* f0-f7 */
  269         0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0x98, /* f8-ff */
  270 };
  271 
  272 static const u_char
  273 dos2unix[256] = {
  274          '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?', /* 00-07 */
  275          '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?', /* 08-0f */
  276          '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?', /* 10-17 */
  277          '?',  '?',  '?',  '?',  '?',  '?',  '?',  '?', /* 18-1f */
  278          ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'', /* 20-27 */
  279          '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/', /* 28-2f */
  280          '',  '1',  '2',  '3',  '4',  '5',  '6',  '7', /* 30-37 */
  281          '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?', /* 38-3f */
  282          '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G', /* 40-47 */
  283          'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O', /* 48-4f */
  284          'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W', /* 50-57 */
  285          'X',  'Y',  'Z',  '[', '\\',  ']',  '^',  '_', /* 58-5f */
  286          '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 60-67 */
  287          'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 68-6f */
  288          'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 70-77 */
  289          'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f, /* 78-7f */
  290         0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7, /* 80-87 */
  291         0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5, /* 88-8f */
  292         0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9, /* 90-97 */
  293         0xff, 0xd6, 0xdc, 0xf8, 0xa3, 0xd8, 0xd7,  '?', /* 98-9f */
  294         0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba, /* a0-a7 */
  295         0xbf, 0xae, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb, /* a8-af */
  296          '?',  '?',  '?',  '?',  '?', 0xc1, 0xc2, 0xc0, /* b0-b7 */
  297         0xa9,  '?',  '?',  '?',  '?', 0xa2, 0xa5,  '?', /* b8-bf */
  298          '?',  '?',  '?',  '?',  '?',  '?', 0xe3, 0xc3, /* c0-c7 */
  299          '?',  '?',  '?',  '?',  '?',  '?',  '?', 0xa4, /* c8-cf */
  300         0xf0, 0xd0, 0xca, 0xcb, 0xc8,  '?', 0xcd, 0xce, /* d0-d7 */
  301         0xcf,  '?',  '?',  '?',  '?', 0xa6, 0xcc,  '?', /* d8-df */
  302         0xd3, 0xdf, 0xd4, 0xd2, 0xf5, 0xd5, 0xb5, 0xfe, /* e0-e7 */
  303         0xde, 0xda, 0xdb, 0xd9, 0xfd, 0xdd, 0xaf, 0x3f, /* e8-ef */
  304         0xad, 0xb1,  '?', 0xbe, 0xb6, 0xa7, 0xf7, 0xb8, /* f0-f7 */
  305         0xb0, 0xa8, 0xb7, 0xb9, 0xb3, 0xb2,  '?',  '?', /* f8-ff */
  306 };
  307 
  308 static const u_char
  309 u2l[256] = {
  310         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 00-07 */
  311         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 08-0f */
  312         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 10-17 */
  313         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 18-1f */
  314          ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'', /* 20-27 */
  315          '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/', /* 28-2f */
  316          '',  '1',  '2',  '3',  '4',  '5',  '6',  '7', /* 30-37 */
  317          '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?', /* 38-3f */
  318          '@',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 40-47 */
  319          'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 48-4f */
  320          'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 50-57 */
  321          'x',  'y',  'z',  '[', '\\',  ']',  '^',  '_', /* 58-5f */
  322          '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g', /* 60-67 */
  323          'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', /* 68-6f */
  324          'p',  'q',  'r',  's',  't',  'u',  'v',  'w', /* 70-77 */
  325          'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f, /* 78-7f */
  326         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 80-87 */
  327         0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 88-8f */
  328         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 90-97 */
  329         0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 98-9f */
  330         0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* a0-a7 */
  331         0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* a8-af */
  332         0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* b0-b7 */
  333         0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* b8-bf */
  334         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* c0-c7 */
  335         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* c8-cf */
  336         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xd7, /* d0-d7 */
  337         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xdf, /* d8-df */
  338         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* e0-e7 */
  339         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* e8-ef */
  340         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* f0-f7 */
  341         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* f8-ff */
  342 };
  343 
  344 /*
  345  * DOS filenames are made of 2 parts, the name part and the extension part.
  346  * The name part is 8 characters long and the extension part is 3
  347  * characters long.  They may contain trailing blanks if the name or
  348  * extension are not long enough to fill their respective fields.
  349  */
  350 
  351 /*
  352  * Convert a DOS filename to a unix filename. And, return the number of
  353  * characters in the resulting unix filename excluding the terminating
  354  * null.
  355  */
  356 int
  357 dos2unixfn(dn, un, lower)
  358         u_char dn[11];
  359         u_char *un;
  360         int lower;
  361 {
  362         int i, j;
  363         int thislong = 1;
  364         u_char c;
  365 
  366         /*
  367          * If first char of the filename is SLOT_E5 (0x05), then the real
  368          * first char of the filename should be 0xe5. But, they couldn't
  369          * just have a 0xe5 mean 0xe5 because that is used to mean a freed
  370          * directory slot. Another dos quirk.
  371          */
  372         if (*dn == SLOT_E5)
  373                 c = dos2unix[0xe5];
  374         else
  375                 c = dos2unix[*dn];
  376         *un++ = lower ? u2l[c] : c;
  377 
  378         /*
  379          * Copy the rest into the unix filename string, ignoring
  380          * trailing blanks.
  381          */
  382 
  383         for (j=7; (j >= 0) && (dn[j] == ' '); j--)
  384                 ;
  385 
  386         for (i = 1; i <= j; i++) {
  387                 c = dos2unix[dn[i]];
  388                 *un++ = lower ? u2l[c] : c;
  389                 thislong++;
  390         }
  391         dn += 8;
  392         
  393         /*
  394          * Now, if there is an extension then put in a period and copy in
  395          * the extension.
  396          */
  397         if (*dn != ' ') {
  398                 *un++ = '.';
  399                 thislong++;
  400                 for (i = 0; i < 3 && *dn != ' '; i++) {
  401                         c = dos2unix[*dn++];
  402                         *un++ = lower ? u2l[c] : c;
  403                         thislong++;
  404                 }
  405         }
  406         *un++ = 0;
  407 
  408         return (thislong);
  409 }
  410 
  411 /*
  412  * Convert a unix filename to a DOS filename according to Win95 rules.
  413  * If applicable and gen is not 0, it is inserted into the converted
  414  * filename as a generation number.
  415  * Returns
  416  *      0 if name couldn't be converted
  417  *      1 if the converted name is the same as the original
  418  *        (no long filename entry necessary for Win95)
  419  *      2 if conversion was successful
  420  *      3 if conversion was successful and generation number was inserted
  421  */
  422 int
  423 unix2dosfn(un, dn, unlen, gen)
  424         const u_char *un;
  425         u_char dn[12];
  426         int unlen;
  427         u_int gen;
  428 {
  429         int i, j, l;
  430         int conv = 1;
  431         const u_char *cp, *dp, *dp1;
  432         u_char gentext[6], *wcp;
  433         int shortlen;
  434 
  435         /*
  436          * Fill the dos filename string with blanks. These are DOS's pad
  437          * characters.
  438          */
  439         for (i = 0; i < 11; i++)
  440                 dn[i] = ' ';
  441         dn[11] = 0;
  442 
  443         /*
  444          * The filenames "." and ".." are handled specially, since they
  445          * don't follow dos filename rules.
  446          */
  447         if (un[0] == '.' && unlen == 1) {
  448                 dn[0] = '.';
  449                 return gen <= 1;
  450         }
  451         if (un[0] == '.' && un[1] == '.' && unlen == 2) {
  452                 dn[0] = '.';
  453                 dn[1] = '.';
  454                 return gen <= 1;
  455         }
  456 
  457         /*
  458          * Filenames with only blanks and dots are not allowed!
  459          */
  460         for (cp = un, i = unlen; --i >= 0; cp++)
  461                 if (*cp != ' ' && *cp != '.')
  462                         break;
  463         if (i < 0)
  464                 return 0;
  465 
  466         /*
  467          * Now find the extension
  468          * Note: dot as first char doesn't start extension
  469          *       and trailing dots and blanks are ignored
  470          */
  471         dp = dp1 = 0;
  472         for (cp = un + 1, i = unlen - 1; --i >= 0;) {
  473                 switch (*cp++) {
  474                 case '.':
  475                         if (!dp1)
  476                                 dp1 = cp;
  477                         break;
  478                 case ' ':
  479                         break;
  480                 default:
  481                         if (dp1)
  482                                 dp = dp1;
  483                         dp1 = 0;
  484                         break;
  485                 }
  486         }
  487 
  488         /*
  489          * Now convert it
  490          */
  491         if (dp) {
  492                 if (dp1)
  493                         l = dp1 - dp;
  494                 else
  495                         l = unlen - (dp - un);
  496                 for (i = 0, j = 8; i < l && j < 11; i++, j++) {
  497                         if (dp[i] != (dn[j] = unix2dos[dp[i]])
  498                             && conv != 3)
  499                                 conv = 2;
  500                         if (!dn[j]) {
  501                                 conv = 3;
  502                                 dn[j--] = ' ';
  503                         }
  504                 }
  505                 if (i < l)
  506                         conv = 3;
  507                 dp--;
  508         } else {
  509                 for (dp = cp; *--dp == ' ' || *dp == '.';);
  510                 dp++;
  511         }
  512 
  513         shortlen = (dp - un) <= 8;
  514 
  515         /*
  516          * Now convert the rest of the name
  517          */
  518         for (i = j = 0; un < dp && j < 8; i++, j++, un++) {
  519                 if ((*un == ' ') && shortlen)
  520                         dn[j] = ' ';
  521                 else
  522                         dn[j] = unix2dos[*un];
  523                 if ((*un != dn[j])
  524                     && conv != 3)
  525                         conv = 2;
  526                 if (!dn[j]) {
  527                         conv = 3;
  528                         dn[j--] = ' ';
  529                 }
  530         }
  531         if (un < dp)
  532                 conv = 3;
  533         /*
  534          * If we didn't have any chars in filename,
  535          * generate a default
  536          */
  537         if (!j)
  538                 dn[0] = '_';
  539 
  540         /*
  541          * The first character cannot be E5,
  542          * because that means a deleted entry
  543          */
  544         if (dn[0] == 0xe5)
  545                 dn[0] = SLOT_E5;
  546 
  547         /*
  548          * If there wasn't any char dropped,
  549          * there is no place for generation numbers
  550          */
  551         if (conv != 3) {
  552                 if (gen > 1)
  553                         return 0;
  554                 return conv;
  555         }
  556 
  557         /*
  558          * Now insert the generation number into the filename part
  559          */
  560         for (wcp = gentext + sizeof(gentext); wcp > gentext && gen; gen /= 10)
  561                 *--wcp = gen % 10 + '';
  562         if (gen)
  563                 return 0;
  564         for (i = 8; dn[--i] == ' ';);
  565         i++;
  566         if (gentext + sizeof(gentext) - wcp + 1 > 8 - i)
  567                 i = 8 - (gentext + sizeof(gentext) - wcp + 1);
  568         dn[i++] = '~';
  569         while (wcp < gentext + sizeof(gentext))
  570                 dn[i++] = *wcp++;
  571         return 3;
  572 }
  573 
  574 /*
  575  * Create a Win95 long name directory entry
  576  * Note: assumes that the filename is valid,
  577  *       i.e. doesn't consist solely of blanks and dots
  578  */
  579 int
  580 unix2winfn(un, unlen, wep, cnt, chksum)
  581         const u_char *un;
  582         int unlen;
  583         struct winentry *wep;
  584         int cnt;
  585         int chksum;
  586 {
  587         const u_int8_t *cp;
  588         u_int8_t *wcp;
  589         int i;
  590 
  591         /*
  592          * Drop trailing blanks and dots
  593          */
  594         for (cp = un + unlen; *--cp == ' ' || *cp == '.'; unlen--);
  595 
  596         un += (cnt - 1) * WIN_CHARS;
  597         unlen -= (cnt - 1) * WIN_CHARS;
  598 
  599         /*
  600          * Initialize winentry to some useful default
  601          */
  602         for (wcp = (u_int8_t *)wep, i = sizeof(*wep); --i >= 0; *wcp++ = 0xff);
  603         wep->weCnt = cnt;
  604         wep->weAttributes = ATTR_WIN95;
  605         wep->weReserved1 = 0;
  606         wep->weChksum = chksum;
  607         wep->weReserved2 = 0;
  608 
  609         /*
  610          * Now convert the filename parts
  611          */
  612         for (wcp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
  613                 if (--unlen < 0)
  614                         goto done;
  615                 *wcp++ = *un++;
  616                 *wcp++ = 0;
  617         }
  618         for (wcp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
  619                 if (--unlen < 0)
  620                         goto done;
  621                 *wcp++ = *un++;
  622                 *wcp++ = 0;
  623         }
  624         for (wcp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
  625                 if (--unlen < 0)
  626                         goto done;
  627                 *wcp++ = *un++;
  628                 *wcp++ = 0;
  629         }
  630         if (!unlen)
  631                 wep->weCnt |= WIN_LAST;
  632         return unlen;
  633 
  634 done:
  635         *wcp++ = 0;
  636         *wcp++ = 0;
  637         wep->weCnt |= WIN_LAST;
  638         return 0;
  639 }
  640 
  641 /*
  642  * Compare our filename to the one in the Win95 entry
  643  * Returns the checksum or -1 if no match
  644  */
  645 int
  646 winChkName(un, unlen, wep, chksum)
  647         const u_char *un;
  648         int unlen;
  649         struct winentry *wep;
  650         int chksum;
  651 {
  652         u_int8_t *cp;
  653         int i;
  654 
  655         /*
  656          * First compare checksums
  657          */
  658         if (wep->weCnt&WIN_LAST)
  659                 chksum = wep->weChksum;
  660         else if (chksum != wep->weChksum)
  661                 chksum = -1;
  662         if (chksum == -1)
  663                 return -1;
  664 
  665         /*
  666          * Offset of this entry
  667          */
  668         i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
  669         un += i;
  670         if ((unlen -= i) < 0)
  671                 return -1;
  672 
  673         /*
  674          * Ignore redundant winentries (those with only \0\0 on start in them).
  675          * An appearance of such entry is a bug; unknown if in NetBSD msdosfs
  676          * or MS Windows.
  677          */
  678         if (unlen == 0) {
  679                 if (wep->wePart1[0] == '\0' && wep->wePart1[1] == '\0')
  680                         return chksum;
  681                 else
  682                         return -1;
  683         }
  684 
  685         if ((wep->weCnt&WIN_LAST) && unlen > WIN_CHARS)
  686                 return -1;
  687 
  688         /*
  689          * Compare the name parts
  690          */
  691         for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
  692                 if (--unlen < 0) {
  693                         if (!*cp++ && !*cp)
  694                                 return chksum;
  695                         return -1;
  696                 }
  697                 if (u2l[*cp++] != u2l[*un++] || *cp++)
  698                         return -1;
  699         }
  700         for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
  701                 if (--unlen < 0) {
  702                         if (!*cp++ && !*cp)
  703                                 return chksum;
  704                         return -1;
  705                 }
  706                 if (u2l[*cp++] != u2l[*un++] || *cp++)
  707                         return -1;
  708         }
  709         for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
  710                 if (--unlen < 0) {
  711                         if (!*cp++ && !*cp)
  712                                 return chksum;
  713                         return -1;
  714                 }
  715                 if (u2l[*cp++] != u2l[*un++] || *cp++)
  716                         return -1;
  717         }
  718         return chksum;
  719 }
  720 
  721 /*
  722  * Convert Win95 filename to dirbuf.
  723  * Returns the checksum or -1 if impossible
  724  */
  725 int
  726 win2unixfn(wep, dp, chksum)
  727         struct winentry *wep;
  728         struct dirent *dp;
  729         int chksum;
  730 {
  731         u_int8_t *cp;
  732         u_int8_t *np, *ep = dp->d_name + WIN_MAXLEN;
  733         int i;
  734 
  735         if ((wep->weCnt&WIN_CNT) > howmany(WIN_MAXLEN, WIN_CHARS)
  736             || !(wep->weCnt&WIN_CNT))
  737                 return -1;
  738 
  739         /*
  740          * First compare checksums
  741          */
  742         if (wep->weCnt&WIN_LAST) {
  743                 chksum = wep->weChksum;
  744                 /*
  745                  * This works even though d_namlen is one byte!
  746                  */
  747                 dp->d_namlen = (wep->weCnt&WIN_CNT) * WIN_CHARS;
  748         } else if (chksum != wep->weChksum)
  749                 chksum = -1;
  750         if (chksum == -1)
  751                 return -1;
  752 
  753         /*
  754          * Offset of this entry
  755          */
  756         i = ((wep->weCnt&WIN_CNT) - 1) * WIN_CHARS;
  757         np = (u_int8_t *)dp->d_name + i;
  758 
  759         /*
  760          * Convert the name parts
  761          */
  762         for (cp = wep->wePart1, i = sizeof(wep->wePart1)/2; --i >= 0;) {
  763                 switch (*np++ = *cp++) {
  764                 case 0:
  765                         dp->d_namlen -= sizeof(wep->wePart2)/2
  766                             + sizeof(wep->wePart3)/2 + i + 1;
  767                         return chksum;
  768                 case '/':
  769                         np[-1] = 0;
  770                         return -1;
  771                 }
  772                 /*
  773                  * The size comparison should result in the compiler
  774                  * optimizing the whole if away
  775                  */
  776                 if (WIN_MAXLEN % WIN_CHARS < sizeof(wep->wePart1) / 2
  777                     && np > ep) {
  778                         np[-1] = 0;
  779                         return -1;
  780                 }
  781                 if (*cp++)
  782                         return -1;
  783         }
  784         for (cp = wep->wePart2, i = sizeof(wep->wePart2)/2; --i >= 0;) {
  785                 switch (*np++ = *cp++) {
  786                 case 0:
  787                         dp->d_namlen -= sizeof(wep->wePart3)/2 + i + 1;
  788                         return chksum;
  789                 case '/':
  790                         np[-1] = 0;
  791                         return -1;
  792                 }
  793                 /*
  794                  * The size comparisons should be optimized away
  795                  */
  796                 if (WIN_MAXLEN % WIN_CHARS >= sizeof(wep->wePart1) / 2
  797                     && WIN_MAXLEN % WIN_CHARS < (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
  798                     && np > ep) {
  799                         np[-1] = 0;
  800                         return -1;
  801                 }
  802                 if (*cp++)
  803                         return -1;
  804         }
  805         for (cp = wep->wePart3, i = sizeof(wep->wePart3)/2; --i >= 0;) {
  806                 switch (*np++ = *cp++) {
  807                 case 0:
  808                         dp->d_namlen -= i + 1;
  809                         return chksum;
  810                 case '/':
  811                         np[-1] = 0;
  812                         return -1;
  813                 }
  814                 /*
  815                  * See above
  816                  */
  817                 if (WIN_MAXLEN % WIN_CHARS >= (sizeof(wep->wePart1) + sizeof(wep->wePart2)) / 2
  818                     && np > ep) {
  819                         np[-1] = 0;
  820                         return -1;
  821                 }
  822                 if (*cp++)
  823                         return -1;
  824         }
  825         return chksum;
  826 }
  827 
  828 /*
  829  * Compute the checksum of a DOS filename for Win95 use
  830  */
  831 u_int8_t
  832 winChksum(name)
  833         u_int8_t *name;
  834 {
  835         int i;
  836         u_int8_t s;
  837 
  838         for (s = 0, i = 11; --i >= 0; s += *name++)
  839                 s = (s << 7)|(s >> 1);
  840         return s;
  841 }
  842 
  843 /*
  844  * Determine the number of slots necessary for Win95 names
  845  */
  846 int
  847 winSlotCnt(un, unlen)
  848         const u_char *un;
  849         int unlen;
  850 {
  851         for (un += unlen; unlen > 0; unlen--)
  852                 if (*--un != ' ' && *un != '.')
  853                         break;
  854         if (unlen > WIN_MAXLEN)
  855                 return 0;
  856         return howmany(unlen, WIN_CHARS);
  857 }

Cache object: ab94818352f9f6ccffcd1078883dc18e


[ 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.