name.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. pr_err("Long name flag mismatch - name ");
  52. for (i = 0; i < len; i++)
  53. pr_cont("%c", from[i]);
  54. pr_cont(" misidentified as %s.\n", lng ? "short" : "long");
  55. pr_err("It's nothing serious. It could happen because of bug in OS/2.\nSet checks=normal to disable this message.\n");
  56. }
  57. if (!lc) return from;
  58. if (!(to = kmalloc(len, GFP_KERNEL))) {
  59. pr_err("can't allocate memory for name conversion buffer\n");
  60. return from;
  61. }
  62. for (i = 0; i < len; i++) to[i] = locase(hpfs_sb(s)->sb_cp_table,from[i]);
  63. return to;
  64. }
  65. int hpfs_compare_names(struct super_block *s,
  66. const unsigned char *n1, unsigned l1,
  67. const unsigned char *n2, unsigned l2, int last)
  68. {
  69. unsigned l = l1 < l2 ? l1 : l2;
  70. unsigned i;
  71. if (last) return -1;
  72. for (i = 0; i < l; i++) {
  73. unsigned char c1 = upcase(hpfs_sb(s)->sb_cp_table,n1[i]);
  74. unsigned char c2 = upcase(hpfs_sb(s)->sb_cp_table,n2[i]);
  75. if (c1 < c2) return -1;
  76. if (c1 > c2) return 1;
  77. }
  78. if (l1 < l2) return -1;
  79. if (l1 > l2) return 1;
  80. return 0;
  81. }
  82. int hpfs_is_name_long(const unsigned char *name, unsigned len)
  83. {
  84. int i,j;
  85. for (i = 0; i < len && name[i] != '.'; i++)
  86. if (no_dos_char(name[i])) return 1;
  87. if (!i || i > 8) return 1;
  88. if (i == len) return 0;
  89. for (j = i + 1; j < len; j++)
  90. if (name[j] == '.' || no_dos_char(name[i])) return 1;
  91. return j - i > 4;
  92. }
  93. /* OS/2 clears dots and spaces at the end of file name, so we have to */
  94. void hpfs_adjust_length(const unsigned char *name, unsigned *len)
  95. {
  96. if (!*len) return;
  97. if (*len == 1 && name[0] == '.') return;
  98. if (*len == 2 && name[0] == '.' && name[1] == '.') return;
  99. while (*len && (name[*len - 1] == '.' || name[*len - 1] == ' '))
  100. (*len)--;
  101. }