name.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * linux/fs/hpfs/name.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * operations with filenames
  7. */
  8. #include "hpfs_fn.h"
  9. static inline int not_allowed_char(unsigned char c)
  10. {
  11. return c<' ' || c=='"' || c=='*' || c=='/' || c==':' || c=='<' ||
  12. c=='>' || c=='?' || c=='\\' || c=='|';
  13. }
  14. static inline int no_dos_char(unsigned char c)
  15. { /* Characters that are allowed in HPFS but not in DOS */
  16. return c=='+' || c==',' || c==';' || c=='=' || c=='[' || c==']';
  17. }
  18. static inline unsigned char upcase(unsigned char *dir, unsigned char a)
  19. {
  20. if (a<128 || a==255) return a>='a' && a<='z' ? a - 0x20 : a;
  21. if (!dir) return a;
  22. return dir[a-128];
  23. }
  24. unsigned char hpfs_upcase(unsigned char *dir, unsigned char a)
  25. {
  26. return upcase(dir, a);
  27. }
  28. static inline unsigned char locase(unsigned char *dir, unsigned char a)
  29. {
  30. if (a<128 || a==255) return a>='A' && a<='Z' ? a + 0x20 : a;
  31. if (!dir) return a;
  32. return dir[a];
  33. }
  34. int hpfs_chk_name(const unsigned char *name, unsigned *len)
  35. {
  36. int i;
  37. if (*len > 254) return -ENAMETOOLONG;
  38. hpfs_adjust_length(name, len);
  39. if (!*len) return -EINVAL;
  40. for (i = 0; i < *len; i++) if (not_allowed_char(name[i])) return -EINVAL;
  41. if (*len == 1) if (name[0] == '.') return -EINVAL;
  42. if (*len == 2) if (name[0] == '.' && name[1] == '.') return -EINVAL;
  43. return 0;
  44. }
  45. unsigned char *hpfs_translate_name(struct super_block *s, unsigned char *from,
  46. unsigned len, int lc, int lng)
  47. {
  48. unsigned char *to;
  49. int i;
  50. if (hpfs_sb(s)->sb_chk >= 2) if (hpfs_is_name_long(from, len) != lng) {
  51. printk("HPFS: Long name flag mismatch - name ");
  52. for (i=0; i<len; i++) printk("%c", from[i]);
  53. printk(" misidentified as %s.\n", lng ? "short" : "long");
  54. printk("HPFS: It's nothing serious. It could happen because of bug in OS/2.\nHPFS: Set checks=normal to disable this message.\n");
  55. }
  56. if (!lc) return from;
  57. if (!(to = kmalloc(len, GFP_KERNEL))) {
  58. printk("HPFS: can't allocate memory for name conversion buffer\n");
  59. return from;
  60. }
  61. for (i = 0; i < len; i++) to[i] = locase(hpfs_sb(s)->sb_cp_table,from[i]);
  62. return to;
  63. }
  64. int hpfs_compare_names(struct super_block *s,
  65. const unsigned char *n1, unsigned l1,
  66. const unsigned char *n2, unsigned l2, int last)
  67. {
  68. unsigned l = l1 < l2 ? l1 : l2;
  69. unsigned i;
  70. if (last) return -1;
  71. for (i = 0; i < l; i++) {
  72. unsigned char c1 = upcase(hpfs_sb(s)->sb_cp_table,n1[i]);
  73. unsigned char c2 = upcase(hpfs_sb(s)->sb_cp_table,n2[i]);
  74. if (c1 < c2) return -1;
  75. if (c1 > c2) return 1;
  76. }
  77. if (l1 < l2) return -1;
  78. if (l1 > l2) return 1;
  79. return 0;
  80. }
  81. int hpfs_is_name_long(const unsigned char *name, unsigned len)
  82. {
  83. int i,j;
  84. for (i = 0; i < len && name[i] != '.'; i++)
  85. if (no_dos_char(name[i])) return 1;
  86. if (!i || i > 8) return 1;
  87. if (i == len) return 0;
  88. for (j = i + 1; j < len; j++)
  89. if (name[j] == '.' || no_dos_char(name[i])) return 1;
  90. return j - i > 4;
  91. }
  92. /* OS/2 clears dots and spaces at the end of file name, so we have to */
  93. void hpfs_adjust_length(const unsigned char *name, unsigned *len)
  94. {
  95. if (!*len) return;
  96. if (*len == 1 && name[0] == '.') return;
  97. if (*len == 2 && name[0] == '.' && name[1] == '.') return;
  98. while (*len && (name[*len - 1] == '.' || name[*len - 1] == ' '))
  99. (*len)--;
  100. }