Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/fs/hpfs/name.c
0004  *
0005  *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
0006  *
0007  *  operations with filenames
0008  */
0009 
0010 #include "hpfs_fn.h"
0011 
0012 static inline int not_allowed_char(unsigned char c)
0013 {
0014     return c<' ' || c=='"' || c=='*' || c=='/' || c==':' || c=='<' ||
0015           c=='>' || c=='?' || c=='\\' || c=='|';
0016 }
0017 
0018 static inline int no_dos_char(unsigned char c)
0019 {   /* Characters that are allowed in HPFS but not in DOS */
0020     return c=='+' || c==',' || c==';' || c=='=' || c=='[' || c==']';
0021 }
0022 
0023 static inline unsigned char upcase(unsigned char *dir, unsigned char a)
0024 {
0025     if (a<128 || a==255) return a>='a' && a<='z' ? a - 0x20 : a;
0026     if (!dir) return a;
0027     return dir[a-128];
0028 }
0029 
0030 unsigned char hpfs_upcase(unsigned char *dir, unsigned char a)
0031 {
0032     return upcase(dir, a);
0033 }
0034 
0035 static inline unsigned char locase(unsigned char *dir, unsigned char a)
0036 {
0037     if (a<128 || a==255) return a>='A' && a<='Z' ? a + 0x20 : a;
0038     if (!dir) return a;
0039     return dir[a];
0040 }
0041 
0042 int hpfs_chk_name(const unsigned char *name, unsigned *len)
0043 {
0044     int i;
0045     if (*len > 254) return -ENAMETOOLONG;
0046     hpfs_adjust_length(name, len);
0047     if (!*len) return -EINVAL;
0048     for (i = 0; i < *len; i++) if (not_allowed_char(name[i])) return -EINVAL;
0049     if (*len == 1) if (name[0] == '.') return -EINVAL;
0050     if (*len == 2) if (name[0] == '.' && name[1] == '.') return -EINVAL;
0051     return 0;
0052 }
0053 
0054 unsigned char *hpfs_translate_name(struct super_block *s, unsigned char *from,
0055               unsigned len, int lc, int lng)
0056 {
0057     unsigned char *to;
0058     int i;
0059     if (hpfs_sb(s)->sb_chk >= 2) if (hpfs_is_name_long(from, len) != lng) {
0060         pr_err("Long name flag mismatch - name ");
0061         for (i = 0; i < len; i++)
0062             pr_cont("%c", from[i]);
0063         pr_cont(" misidentified as %s.\n", lng ? "short" : "long");
0064         pr_err("It's nothing serious. It could happen because of bug in OS/2.\nSet checks=normal to disable this message.\n");
0065     }
0066     if (!lc) return from;
0067     if (!(to = kmalloc(len, GFP_KERNEL))) {
0068         pr_err("can't allocate memory for name conversion buffer\n");
0069         return from;
0070     }
0071     for (i = 0; i < len; i++) to[i] = locase(hpfs_sb(s)->sb_cp_table,from[i]);
0072     return to;
0073 }
0074 
0075 int hpfs_compare_names(struct super_block *s,
0076                const unsigned char *n1, unsigned l1,
0077                const unsigned char *n2, unsigned l2, int last)
0078 {
0079     unsigned l = l1 < l2 ? l1 : l2;
0080     unsigned i;
0081     if (last) return -1;
0082     for (i = 0; i < l; i++) {
0083         unsigned char c1 = upcase(hpfs_sb(s)->sb_cp_table,n1[i]);
0084         unsigned char c2 = upcase(hpfs_sb(s)->sb_cp_table,n2[i]);
0085         if (c1 < c2) return -1;
0086         if (c1 > c2) return 1;
0087     }
0088     if (l1 < l2) return -1;
0089     if (l1 > l2) return 1;
0090     return 0;
0091 }
0092 
0093 int hpfs_is_name_long(const unsigned char *name, unsigned len)
0094 {
0095     int i,j;
0096     for (i = 0; i < len && name[i] != '.'; i++)
0097         if (no_dos_char(name[i])) return 1;
0098     if (!i || i > 8) return 1;
0099     if (i == len) return 0;
0100     for (j = i + 1; j < len; j++)
0101         if (name[j] == '.' || no_dos_char(name[i])) return 1;
0102     return j - i > 4;
0103 }
0104 
0105 /* OS/2 clears dots and spaces at the end of file name, so we have to */
0106 
0107 void hpfs_adjust_length(const unsigned char *name, unsigned *len)
0108 {
0109     if (!*len) return;
0110     if (*len == 1 && name[0] == '.') return;
0111     if (*len == 2 && name[0] == '.' && name[1] == '.') return;
0112     while (*len && (name[*len - 1] == '.' || name[*len - 1] == ' '))
0113         (*len)--;
0114 }