This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / ntfs / collate.c
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
22 #include "ntfs.h"
23 #include "collate.h"
24
25 static int ntfs_collate_binary(ntfs_volume *vol,
26                 const void *data1, const int data1_len,
27                 const void *data2, const int data2_len)
28 {
29         int rc;
30
31         ntfs_debug("Entering.");
32         rc = memcmp(data1, data2, min(data1_len, data2_len));
33         if (!rc && (data1_len != data2_len)) {
34                 if (data1_len < data2_len)
35                         rc = -1;
36                 else
37                         rc = 1;
38         }
39         ntfs_debug("Done, returning %i", rc);
40         return rc;
41 }
42
43 static int ntfs_collate_ntofs_ulong(ntfs_volume *vol,
44                 const void *data1, const int data1_len,
45                 const void *data2, const int data2_len)
46 {
47         int rc;
48         u32 d1, d2;
49
50         ntfs_debug("Entering.");
51         // FIXME:  We don't really want to bug here.
52         BUG_ON(data1_len != data2_len);
53         BUG_ON(data1_len != 4);
54         d1 = le32_to_cpup(data1);
55         d2 = le32_to_cpup(data2);
56         if (d1 < d2)
57                 rc = -1;
58         else {
59                 if (d1 == d2)
60                         rc = 0;
61                 else
62                         rc = 1;
63         }
64         ntfs_debug("Done, returning %i", rc);
65         return rc;
66 }
67
68 typedef int (*ntfs_collate_func_t)(ntfs_volume *, const void *, const int,
69                 const void *, const int);
70
71 static ntfs_collate_func_t ntfs_do_collate0x0[3] = {
72         ntfs_collate_binary,
73         NULL/*ntfs_collate_file_name*/,
74         NULL/*ntfs_collate_unicode_string*/,
75 };
76
77 static ntfs_collate_func_t ntfs_do_collate0x1[4] = {
78         ntfs_collate_ntofs_ulong,
79         NULL/*ntfs_collate_ntofs_sid*/,
80         NULL/*ntfs_collate_ntofs_security_hash*/,
81         NULL/*ntfs_collate_ntofs_ulongs*/,
82 };
83
84 /**
85  * ntfs_collate - collate two data items using a specified collation rule
86  * @vol:        ntfs volume to which the data items belong
87  * @cr:         collation rule to use when comparing the items
88  * @data1:      first data item to collate
89  * @data1_len:  length in bytes of @data1
90  * @data2:      second data item to collate
91  * @data2_len:  length in bytes of @data2
92  *
93  * Collate the two data items @data1 and @data2 using the collation rule @cr
94  * and return -1, 0, ir 1 if @data1 is found, respectively, to collate before,
95  * to match, or to collate after @data2.
96  *
97  * For speed we use the collation rule @cr as an index into two tables of
98  * function pointers to call the appropriate collation function.
99  */
100 int ntfs_collate(ntfs_volume *vol, COLLATION_RULES cr,
101                 const void *data1, const int data1_len,
102                 const void *data2, const int data2_len) {
103         ntfs_debug("Entering.");
104         /*
105          * FIXME:  At the moment we only support COLLATION_BINARY and
106          * COLLATION_NTOFS_ULONG, so we BUG() for everything else for now.
107          */
108         BUG_ON(cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG);
109         cr = le32_to_cpu(cr);
110         BUG_ON(cr < 0);
111         if (cr <= 0x02)
112                 return ntfs_do_collate0x0[cr](vol, data1, data1_len,
113                                 data2, data2_len);
114         BUG_ON(cr < 0x10);
115         cr -= 0x10;
116         if (likely(cr <= 3))
117                 return ntfs_do_collate0x1[cr](vol, data1, data1_len,
118                                 data2, data2_len);
119         BUG();
120         return 0;
121 }