collate.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * collate.c - NTFS kernel collation handling. Part of the Linux-NTFS project.
  4. *
  5. * Copyright (c) 2004 Anton Altaparmakov
  6. */
  7. #include "collate.h"
  8. #include "debug.h"
  9. #include "ntfs.h"
  10. static int ntfs_collate_binary(ntfs_volume *vol,
  11. const void *data1, const int data1_len,
  12. const void *data2, const int data2_len)
  13. {
  14. int rc;
  15. ntfs_debug("Entering.");
  16. rc = memcmp(data1, data2, min(data1_len, data2_len));
  17. if (!rc && (data1_len != data2_len)) {
  18. if (data1_len < data2_len)
  19. rc = -1;
  20. else
  21. rc = 1;
  22. }
  23. ntfs_debug("Done, returning %i", rc);
  24. return rc;
  25. }
  26. static int ntfs_collate_ntofs_ulong(ntfs_volume *vol,
  27. const void *data1, const int data1_len,
  28. const void *data2, const int data2_len)
  29. {
  30. int rc;
  31. u32 d1, d2;
  32. ntfs_debug("Entering.");
  33. // FIXME: We don't really want to bug here.
  34. BUG_ON(data1_len != data2_len);
  35. BUG_ON(data1_len != 4);
  36. d1 = le32_to_cpup(data1);
  37. d2 = le32_to_cpup(data2);
  38. if (d1 < d2)
  39. rc = -1;
  40. else {
  41. if (d1 == d2)
  42. rc = 0;
  43. else
  44. rc = 1;
  45. }
  46. ntfs_debug("Done, returning %i", rc);
  47. return rc;
  48. }
  49. typedef int (*ntfs_collate_func_t)(ntfs_volume *, const void *, const int,
  50. const void *, const int);
  51. static ntfs_collate_func_t ntfs_do_collate0x0[3] = {
  52. ntfs_collate_binary,
  53. NULL/*ntfs_collate_file_name*/,
  54. NULL/*ntfs_collate_unicode_string*/,
  55. };
  56. static ntfs_collate_func_t ntfs_do_collate0x1[4] = {
  57. ntfs_collate_ntofs_ulong,
  58. NULL/*ntfs_collate_ntofs_sid*/,
  59. NULL/*ntfs_collate_ntofs_security_hash*/,
  60. NULL/*ntfs_collate_ntofs_ulongs*/,
  61. };
  62. /**
  63. * ntfs_collate - collate two data items using a specified collation rule
  64. * @vol: ntfs volume to which the data items belong
  65. * @cr: collation rule to use when comparing the items
  66. * @data1: first data item to collate
  67. * @data1_len: length in bytes of @data1
  68. * @data2: second data item to collate
  69. * @data2_len: length in bytes of @data2
  70. *
  71. * Collate the two data items @data1 and @data2 using the collation rule @cr
  72. * and return -1, 0, ir 1 if @data1 is found, respectively, to collate before,
  73. * to match, or to collate after @data2.
  74. *
  75. * For speed we use the collation rule @cr as an index into two tables of
  76. * function pointers to call the appropriate collation function.
  77. */
  78. int ntfs_collate(ntfs_volume *vol, COLLATION_RULE cr,
  79. const void *data1, const int data1_len,
  80. const void *data2, const int data2_len) {
  81. int i;
  82. ntfs_debug("Entering.");
  83. /*
  84. * FIXME: At the moment we only support COLLATION_BINARY and
  85. * COLLATION_NTOFS_ULONG, so we BUG() for everything else for now.
  86. */
  87. BUG_ON(cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG);
  88. i = le32_to_cpu(cr);
  89. BUG_ON(i < 0);
  90. if (i <= 0x02)
  91. return ntfs_do_collate0x0[i](vol, data1, data1_len,
  92. data2, data2_len);
  93. BUG_ON(i < 0x10);
  94. i -= 0x10;
  95. if (likely(i <= 3))
  96. return ntfs_do_collate0x1[i](vol, data1, data1_len,
  97. data2, data2_len);
  98. BUG();
  99. return 0;
  100. }