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

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