collate.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * collate.c - NTFS kernel collation handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2004 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "collate.h"
  22. #include "debug.h"
  23. #include "ntfs.h"
  24. static int ntfs_collate_binary(ntfs_volume *vol,
  25. const void *data1, const int data1_len,
  26. const void *data2, const int data2_len)
  27. {
  28. int rc;
  29. ntfs_debug("Entering.");
  30. rc = memcmp(data1, data2, min(data1_len, data2_len));
  31. if (!rc && (data1_len != data2_len)) {
  32. if (data1_len < data2_len)
  33. rc = -1;
  34. else
  35. rc = 1;
  36. }
  37. ntfs_debug("Done, returning %i", rc);
  38. return rc;
  39. }
  40. static int ntfs_collate_ntofs_ulong(ntfs_volume *vol,
  41. const void *data1, const int data1_len,
  42. const void *data2, const int data2_len)
  43. {
  44. int rc;
  45. u32 d1, d2;
  46. ntfs_debug("Entering.");
  47. // FIXME: We don't really want to bug here.
  48. BUG_ON(data1_len != data2_len);
  49. BUG_ON(data1_len != 4);
  50. d1 = le32_to_cpup(data1);
  51. d2 = le32_to_cpup(data2);
  52. if (d1 < d2)
  53. rc = -1;
  54. else {
  55. if (d1 == d2)
  56. rc = 0;
  57. else
  58. rc = 1;
  59. }
  60. ntfs_debug("Done, returning %i", rc);
  61. return rc;
  62. }
  63. typedef int (*ntfs_collate_func_t)(ntfs_volume *, const void *, const int,
  64. const void *, const int);
  65. static ntfs_collate_func_t ntfs_do_collate0x0[3] = {
  66. ntfs_collate_binary,
  67. NULL/*ntfs_collate_file_name*/,
  68. NULL/*ntfs_collate_unicode_string*/,
  69. };
  70. static ntfs_collate_func_t ntfs_do_collate0x1[4] = {
  71. ntfs_collate_ntofs_ulong,
  72. NULL/*ntfs_collate_ntofs_sid*/,
  73. NULL/*ntfs_collate_ntofs_security_hash*/,
  74. NULL/*ntfs_collate_ntofs_ulongs*/,
  75. };
  76. /**
  77. * ntfs_collate - collate two data items using a specified collation rule
  78. * @vol: ntfs volume to which the data items belong
  79. * @cr: collation rule to use when comparing the items
  80. * @data1: first data item to collate
  81. * @data1_len: length in bytes of @data1
  82. * @data2: second data item to collate
  83. * @data2_len: length in bytes of @data2
  84. *
  85. * Collate the two data items @data1 and @data2 using the collation rule @cr
  86. * and return -1, 0, ir 1 if @data1 is found, respectively, to collate before,
  87. * to match, or to collate after @data2.
  88. *
  89. * For speed we use the collation rule @cr as an index into two tables of
  90. * function pointers to call the appropriate collation function.
  91. */
  92. int ntfs_collate(ntfs_volume *vol, COLLATION_RULE cr,
  93. const void *data1, const int data1_len,
  94. const void *data2, const int data2_len) {
  95. int i;
  96. ntfs_debug("Entering.");
  97. /*
  98. * FIXME: At the moment we only support COLLATION_BINARY and
  99. * COLLATION_NTOFS_ULONG, so we BUG() for everything else for now.
  100. */
  101. BUG_ON(cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG);
  102. i = le32_to_cpu(cr);
  103. BUG_ON(i < 0);
  104. if (i <= 0x02)
  105. return ntfs_do_collate0x0[i](vol, data1, data1_len,
  106. data2, data2_len);
  107. BUG_ON(i < 0x10);
  108. i -= 0x10;
  109. if (likely(i <= 3))
  110. return ntfs_do_collate0x1[i](vol, data1, data1_len,
  111. data2, data2_len);
  112. BUG();
  113. return 0;
  114. }