root/fs/umsdos/mangle.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. umsdos_manglename
  2. umsdos_evalrecsize
  3. umsdos_evalrecsize_old
  4. umsdos_parse
  5. main

   1 /*
   2  *  linux/fs/umsdos/mangle.c
   3  *
   4  *      Written 1993 by Jacques Gelinas 
   5  *
   6  * Control the mangling of file name to fit msdos name space.
   7  * Many optimisation by GLU == dglaude@is1.vub.ac.be (GLAUDE DAVID)
   8 */
   9 #ifdef MODULE
  10 #include <linux/module.h>
  11 #endif
  12 
  13 #include <linux/errno.h>
  14 #include <linux/ctype.h>
  15 #include <linux/string.h>
  16 #include <linux/kernel.h>
  17 #include <linux/umsdos_fs.h>
  18 
  19 /*
  20         Complete the mangling of the MSDOS fake name
  21         based on the position of the entry in the EMD file.
  22 
  23         Simply complete the job of umsdos_parse; fill the extension.
  24 
  25         Beware that info->f_pos must be set.
  26 */
  27 void umsdos_manglename (struct umsdos_info *info)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29         if (info->msdos_reject){
  30                 /* #Specification: file name / non MSDOS conforming / mangling
  31                         Each non MSDOS conforming file has a special extension
  32                         build from the entry position in the EMD file.
  33 
  34                         This number is then transform in a base 32 number, where
  35                         each digit is expressed like hexadecimal number, using
  36                         digit and letter, except it uses 22 letters from 'a' to 'v'.
  37                         The number 32 comes from 2**5. It is faster to split a binary
  38                         number using a base which is a power of two. And I was 32
  39                         when I started this project. Pick your answer :-) .
  40 
  41                         If the result is '0', it is replace with '_', simply
  42                         to make it odd.
  43 
  44                         This is true for the first two character of the extension.
  45                         The last one is taken from a list of odd character, which
  46                         are:
  47 
  48                                 { } ( ) ! ` ^ & @
  49 
  50                         With this scheme, we can produce 9216 ( 9* 32 * 32)
  51                         different extensions which should not clash with any useful
  52                         extension already popular or meaningful. Since most directory
  53                         have much less than 32 * 32 files in it, the first character
  54                         of the extension of any mangle name will be {.
  55 
  56                         Here are the reason to do this (this kind of mangling).
  57 
  58                         -The mangling is deterministic. Just by the extension, we
  59                          are able to locate the entry in the EMD file.
  60 
  61                         -By keeping to beginning of the file name almost unchanged,
  62                          we are helping the MSDOS user.
  63 
  64                         -The mangling produces names not too ugly, so an msdos user
  65                          may live with it (remember it, type it, etc...).
  66 
  67                         -The mangling produces names ugly enough so no one will
  68                          ever think of using such a name in real life. This is not
  69                          fool proof. I don't think there is a total solution to this.
  70                 */
  71                 union {
  72                         int entry_num;
  73                         struct {
  74                                 unsigned num1:5,num2:5,num3:5;
  75                         }num;
  76                 } u;
  77                 char *pt = info->fake.fname + info->fake.len;
  78                 /* lookup for encoding the last character of the extension */
  79                 /* It contain valid character after the ugly one to make sure */
  80                 /* even if someone overflow the 32 * 32 * 9 limit, it still do */
  81                 /* something */
  82                 #define SPECIAL_MANGLING '{','}','(',')','!','`','^','&','@'
  83                 static char lookup3[]={
  84                         SPECIAL_MANGLING,
  85                         /* This is the start of lookup12 */
  86                         '_','1','2','3','4','5','6','7','8','9',
  87                         'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
  88                         'p','q','r','s','t','u','v' 
  89                 };
  90                 #define lookup12 (lookup3+9)
  91                 u.entry_num = info->f_pos / UMSDOS_REC_SIZE;
  92                 if (u.entry_num > (9* 32 * 32)){
  93                         printk ("UMSDOS: More than 9216 file in a directory.\n"
  94                                 "This may break the mangling strategy.\n"
  95                                 "Not a killer problem. See doc.\n");
  96                 }
  97                 *pt++ = '.';
  98                 *pt++ = lookup3 [u.num.num3];
  99                 *pt++ = lookup12[u.num.num2];
 100                 *pt++ = lookup12[u.num.num1];
 101                 *pt = '\0';             /* help doing printk */ 
 102                 info->fake.len += 4;
 103                 info->msdos_reject = 0;         /* Avoid mangling twice */
 104         }
 105 }
 106 
 107 /*
 108         Evaluate the record size needed to store of name of len character.
 109         The value returned is a multiple of UMSDOS_REC_SIZE.
 110 */
 111 int umsdos_evalrecsize (int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113         struct umsdos_dirent dirent;
 114         int nbrec = 1+((len-1+(dirent.name-(char*)&dirent))
 115                    / UMSDOS_REC_SIZE);
 116         return nbrec * UMSDOS_REC_SIZE;
 117         /*
 118         GLU     This should be inlined or something to speed it up to the max.
 119         GLU     nbrec is absolutely not needed to return the value.
 120         */
 121 }
 122 #ifdef TEST
 123 int umsdos_evalrecsize_old (int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         struct umsdos_dirent dirent;
 126         int size = len + (dirent.name-(char*)&dirent);
 127         int nbrec = size / UMSDOS_REC_SIZE;
 128         int extra = size % UMSDOS_REC_SIZE;
 129         if (extra > 0) nbrec++;
 130         return nbrec * UMSDOS_REC_SIZE;
 131 }
 132 #endif
 133 /*
 134         Fill the struct info with the full and msdos name of a file
 135         Return 0 if all is ok, a negative error code otherwise.
 136 */
 137 int umsdos_parse (
     /* [previous][next][first][last][top][bottom][index][help] */
 138         const char *fname,
 139         int len,
 140         struct umsdos_info *info)
 141 {
 142         int ret = -ENAMETOOLONG;
 143         /* #Specification: file name / too long
 144                 If a file name exceed UMSDOS maxima, the file name is silently
 145                 truncated. This makes it conformant with the other file system
 146                 of Linux (minix and ext2 at least).
 147         */
 148         if (len > UMSDOS_MAXNAME) len = UMSDOS_MAXNAME;
 149         {
 150                 const char *firstpt=NULL;       /* First place we saw a . in fname */
 151                 /* #Specification: file name / non MSDOS conforming / base length 0
 152                         file name beginning with a period '.' are invalid for MsDOS.
 153                         It needs absolutely a base name. So the file name is mangled
 154                 */
 155                 int ivldchar = fname[0] == '.';/* At least one invalid character */
 156                 int msdos_len = len;
 157                 int base_len;
 158                 /*
 159                         cardinal_per_size tells if there exist at least one
 160                         DOS pseudo devices on length n. See the test below.
 161                 */
 162                 static const char cardinal_per_size[9]={
 163                         0, 0, 0, 1, 1, 0, 1, 0, 1
 164                 };
 165                 /*
 166                         lkp translate all character to acceptable character (for DOS).
 167                         When lkp[n] == n, it means also it is an acceptable one.
 168                         So it serve both as a flag and as a translator.
 169                 */
 170                 static char lkp[256];
 171                 static char is_init=0;
 172                 if (!is_init){
 173                         /*
 174                                 Initialisation of the array is easier and less error prone
 175                                 like this.
 176                         */
 177                         int i;
 178                         static char *spc = "\"*+,/:;<=>?[\\]|~";
 179                         is_init = 1;
 180                         for (i=0; i<=32; i++) lkp[i] = '#';
 181                         for (i=33; i<'A'; i++) lkp[i] = (char)i;
 182                         for (i='A'; i<='Z'; i++) lkp[i] = (char)(i+('a'-'A'));
 183                         for (i='Z'+1; i<127; i++) lkp[i] = (char)i;
 184                         for (i=128; i<256; i++) lkp[i] = '#';
 185 
 186                         lkp['.'] = '_';
 187                         while (*spc != '\0') lkp[(unsigned char)(*spc++)] = '#';
 188                 }
 189                 /*      GLU
 190                         file name which are longer than 8+'.'+3 are invalid for MsDOS.
 191                         So the file name is to be mangled no more test needed.
 192                         This Speed Up for long and very long name.
 193                         The position of the last point is no more necessary anyway.
 194                 */
 195                 if (len<=(8+1+3)){
 196                         const char *pt = fname;
 197                         const char *endpt = fname + len;
 198                         while (pt < endpt){
 199                                 if (*pt == '.'){
 200                                         if (firstpt != NULL){
 201                                                 /* 2 . in a file name. Reject */
 202                                                 ivldchar = 1;
 203                                                 break;
 204                                         }else{
 205                                                 int extlen = (int)(endpt - pt);
 206                                                 firstpt = pt;
 207                                                 if (firstpt - fname > 8){
 208                                                         /* base name longer than 8: reject */
 209                                                         ivldchar = 1;
 210                                                         break;
 211                                                 }else if (extlen > 4){
 212                                                         /* Extension longer than 4 (including .): reject */
 213                                                         ivldchar = 1;
 214                                                         break;
 215                                                 }else if (extlen == 1){
 216                                                         /* #Specification: file name / non MSDOS conforming / last char == .
 217                                                                 If the last character of a file name is
 218                                                                 a period, mangling is applied. MsDOS do
 219                                                                 not support those file name.
 220                                                         */
 221                                                         ivldchar = 1;
 222                                                         break;
 223                                                 }else if (extlen == 4){
 224                                                         /* #Specification: file name / non MSDOS conforming / mangling clash
 225                                                                 To avoid clash with     the umsdos mangling, any file
 226                                                                 with a special character as the first character
 227                                                                 of the extension will be mangled. This solve the
 228                                                                 following problem:
 229 
 230                                                                 #
 231                                                                 touch FILE
 232                                                                 # FILE is invalid for DOS, so mangling is applied
 233                                                                 # file.{_1 is created in the DOS directory
 234                                                                 touch file.{_1
 235                                                                 # To UMSDOS file point to a single DOS entry.
 236                                                                 # So file.{_1 has to be mangled.
 237                                                                 #
 238                                                         */      
 239                                                         static char special[]={
 240                                                                 SPECIAL_MANGLING,'\0'
 241                                                         };
 242                                                         if (strchr(special,firstpt[1])!= NULL){
 243                                                                 ivldchar = 1;
 244                                                                 break;
 245                                                         }
 246                                                 }
 247                                         }
 248                                 }else if (lkp[(unsigned char)(*pt)] != *pt){
 249                                         ivldchar = 1;
 250                                         break;
 251                                 }
 252                                 pt++;
 253                         }
 254                 }else{
 255                         ivldchar = 1;
 256                 }
 257                 if (ivldchar
 258                         || (firstpt == NULL && len > 8)
 259                         || (len == UMSDOS_EMD_NAMELEN
 260                                 && memcmp(fname,UMSDOS_EMD_FILE,UMSDOS_EMD_NAMELEN)==0)){
 261                         /* #Specification: file name / --linux-.---
 262                                 The name of the EMD file --linux-.--- is map to a mangled
 263                                 name. So UMSDOS does not restrict its use.
 264                         */
 265                         /* #Specification: file name / non MSDOS conforming / mangling
 266                                 Non MSDOS conforming file name must use some alias to fit
 267                                 in the MSDOS name space.
 268 
 269                                 The strategy is simple. The name is simply truncated to
 270                                 8 char. points are replace with underscore and a
 271                                 number is given as an extension. This number correspond
 272                                 to the entry number in the EMD file. The EMD file
 273                                 only need to carry the real name.
 274 
 275                                 Upper case is also convert to lower case.
 276                                 Control character are converted to #.
 277                                 Space are converted to #.
 278                                 The following character are also converted to #.
 279                                 #
 280                                         " * + , / : ; < = > ? [ \ ] | ~
 281                                 #
 282 
 283                                 Sometime, the problem is not in MsDOS itself but in
 284                                 command.com.
 285                         */
 286                         int i;
 287                         char *pt = info->fake.fname;
 288                         base_len = msdos_len = (msdos_len>8) ? 8 : msdos_len;
 289                         /*
 290                                 There is no '.' any more so we know for a fact that
 291                                 the base length is the length.
 292                         */
 293                         memcpy (info->fake.fname,fname,msdos_len);
 294                         for (i=0; i<msdos_len; i++, pt++) *pt = lkp[(unsigned char)(*pt)];
 295                         *pt = '\0';     /* GLU  C'est sur on a un 0 a la fin */
 296                         info->msdos_reject = 1;
 297                         /*
 298                                 The numeric extension is added only when we know
 299                                 the position in the EMD file, in umsdos_newentry(),
 300                                 umsdos_delentry(), and umsdos_findentry().
 301                                 See umsdos_manglename().
 302                         */
 303                 }else{
 304                         /* Conforming MSDOS file name */
 305                         strncpy (info->fake.fname,fname,len);
 306                         info->msdos_reject = 0;
 307                         base_len = firstpt != NULL ? (int)(firstpt - fname) : len;
 308                 }
 309                 if (cardinal_per_size[base_len]){
 310                         /* #Specification: file name / MSDOS devices / mangling
 311                                 To avoid unreachable file from MsDOS, any MsDOS conforming
 312                                 file with a basename equal to one of the MsDOS pseudo
 313                                 devices will be mangled.
 314 
 315                                 If a file such as "prn" was created, it would be unreachable
 316                                 under MsDOS because prn is assumed to be the printer, even
 317                                 if the file does have an extension.
 318 
 319                                 Since the extension is unimportant to MsDOS, we must patch
 320                                 the basename also. We simply insert a minus '-'. To avoid
 321                                 conflict with valid file with a minus in front (such as
 322                                 "-prn"), we add an mangled extension like any other
 323                                 mangled file name.
 324 
 325                                 Here is the list of DOS pseudo devices:
 326 
 327                                 #
 328                                         "prn","con","aux","nul",
 329                                         "lpt1","lpt2","lpt3","lpt4",
 330                                         "com1","com2","com3","com4",
 331                                         "clock$"
 332                                 #
 333 
 334                                 and some standard ones for common DOS programs
 335 
 336                                         "emmxxxx0","xmsxxxx0","setverxx"
 337 
 338                                 (Thanks to Chris Hall <CAH17@PHOENIX.CAMBRIDGE.AC.UK>
 339                                  for pointing these to me).
 340 
 341                                 Is there one missing ?
 342                         */
 343                         /* This table must be ordered by length */
 344                         static const char *tbdev[]={
 345                                 "prn","con","aux","nul",
 346                                 "lpt1","lpt2","lpt3","lpt4",
 347                                 "com1","com2","com3","com4",
 348                                 "clock$",
 349                                 "emmxxxx0","xmsxxxx0","setverxx"
 350                         };
 351                         /* Tell where to find in tbdev[], the first name of */
 352                         /* a certain length */
 353                         static const char start_ind_dev[9]={
 354                                 0, 0, 0, 4, 12, 12, 13, 13, 16 
 355                         };
 356                         char basen[9];
 357                         int i;
 358                         for (i=start_ind_dev[base_len-1]; i<start_ind_dev[base_len]; i++){
 359                                 if (memcmp(info->fake.fname,tbdev[i],base_len)==0){
 360                                         memcpy (basen,info->fake.fname,base_len);
 361                                         basen[base_len] = '\0';         /* GLU  C'est sur on a un 0 a la fin */
 362                                         /*
 363                                         GLU     On ne fait cela que si necessaire, on essaye d'etre le
 364                                         GLU     simple dans le cas general (le plus frequent).
 365                                         */
 366                                         info->fake.fname[0] = '-';
 367                                         strcpy (info->fake.fname+1,basen);      /* GLU  C'est sur on a un 0 a la fin */
 368                                         msdos_len = (base_len==8) ? 8 : base_len + 1;
 369                                         info->msdos_reject = 1;
 370                                         break;
 371                                 }
 372                         }
 373                 }
 374                 info->fake.fname[msdos_len] = '\0';     /* Help doing printk */
 375                 /* GLU  Ce zero devrais deja y etre ! (invariant ?) */
 376                 info->fake.len = msdos_len;
 377                 /* Pourquoi ne pas utiliser info->fake.len partout ??? plus long ?*/
 378                 memcpy (info->entry.name,fname,len);
 379                 info->entry.name_len = len;
 380                 ret = 0;
 381         }
 382         /*
 383                 Evaluate how many record are needed to store this entry.
 384         */
 385         info->recsize = umsdos_evalrecsize (len);
 386         return ret;
 387 }
 388 
 389 #ifdef TEST
 390 
 391 struct MANG_TEST{
 392         char *fname;            /* Name to validate */
 393         int msdos_reject;       /* Expected msdos_reject flag */
 394         char *msname;           /* Expected msdos name */
 395 };
 396 
 397 struct MANG_TEST tb[]={
 398         "hello",                0,      "hello",
 399         "hello.1",              0,      "hello.1",
 400         "hello.1_",             0,      "hello.1_",
 401         "prm",                  0,      "prm",
 402 
 403 #ifdef PROPOSITION
 404         "HELLO",                1,      "hello",
 405         "Hello.1",              1,      "hello.1",
 406         "Hello.c",              1,      "hello.c",
 407 #elseif
 408 /*
 409         Je trouve les trois exemples ci-dessous tres "malheureux".
 410         Je propose de mettre en minuscule dans un passe preliminaire,
 411         et de tester apres si il y a d'autres caracters "mechants".
 412         Bon, je ne l'ai pas fait, parceque ce n'est pas si facilement
 413         modifiable que ca. Mais c'est pour le principe.
 414         Evidemment cela augmente les chances de "Collision",
 415         par exemple: entre "HELLO" et "Hello", mais ces problemes
 416         peuvent etre traiter ailleur avec les autres collisions.
 417 */
 418         "HELLO",                1,      "hello",
 419         "Hello.1",              1,      "hello_1",
 420         "Hello.c",              1,      "hello_c",
 421 #endif
 422 
 423         "hello.{_1",            1,      "hello_{_",
 424         "hello\t",              1,      "hello#",
 425         "hello.1.1",            1,      "hello_1_",
 426         "hel,lo",               1,      "hel#lo",
 427         "Salut.Tu.vas.bien?",   1,      "salut_tu",
 428         ".profile",             1,      "_profile",
 429         ".xv",                  1,      "_xv",
 430         "toto.",                1,      "toto_",
 431         "clock$.x",             1,      "-clock$",
 432         "emmxxxx0",             1,      "-emmxxxx",
 433         "emmxxxx0.abcd",        1,      "-emmxxxx",
 434         "aux",                  1,      "-aux",
 435         "prn",                  1,      "-prn",
 436         "prn.abc",              1,      "-prn",
 437         "PRN",                  1,      "-prn",
 438 /* 
 439 GLU     ATTENTION : Le resultat de ceux-ci sont differents avec ma version
 440 GLU     du mangle par rapport au mangle originale.
 441 GLU     CAUSE: La maniere de calculer la variable baselen. 
 442 GLU             Pour toi c'est toujours 3
 443 GLU             Pour moi c'est respectivement 7, 8 et 8
 444 */
 445         "PRN.abc",              1,      "prn_abc",
 446         "Prn.abcd",             1,      "prn_abcd",
 447         "prn.abcd",             1,      "prn_abcd",
 448         "Prn.abcdefghij",       1,      "prn_abcd"
 449 };
 450 
 451 int main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
 452 {
 453         int i,rold,rnew;
 454         printf ("Testing the umsdos_parse.\n");
 455         for (i=0; i<sizeof(tb)/sizeof(tb[0]); i++){
 456                 struct MANG_TEST *pttb = tb+i;
 457                 struct umsdos_info info;
 458                 int ok = umsdos_parse (pttb->fname,strlen(pttb->fname),&info);
 459                 if (strcmp(info.fake.fname,pttb->msname)!=0){
 460                         printf ("**** %s -> ",pttb->fname);
 461                         printf ("%s <> %s\n",info.fake.fname,pttb->msname);
 462                 }else if (info.msdos_reject != pttb->msdos_reject){
 463                         printf ("**** %s -> %s ",pttb->fname,pttb->msname);
 464                         printf ("%d <> %d\n",info.msdos_reject,pttb->msdos_reject);
 465                 }else{
 466                         printf ("     %s -> %s %d\n",pttb->fname,pttb->msname
 467                                 ,pttb->msdos_reject);
 468                 }
 469         }
 470         printf ("Testing the new umsdos_evalrecsize.");
 471         for (i=0; i<UMSDOS_MAXNAME ; i++){
 472                 rnew=umsdos_evalrecsize (i);
 473                 rold=umsdos_evalrecsize_old (i);
 474                 if (!(i%UMSDOS_REC_SIZE)){
 475                         printf ("\n%d:\t",i);
 476                 }
 477                 if (rnew!=rold){
 478                         printf ("**** %d newres: %d != %d \n", i, rnew, rold);
 479                 }else{
 480                         printf(".");
 481                 }
 482         }
 483         printf ("\nEnd of Testing.\n");
 484 
 485         return 0;
 486 }
 487 
 488 #endif

/* [previous][next][first][last][top][bottom][index][help] */