name.c 3.2 KB

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