2 * collate.c - NTFS collation handling. Originated from the Linux-NTFS project.
4 * Copyright (c) 2004 Anton Altaparmakov
5 * Copyright (c) 2005 Yura Pakhuchiy
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the NTFS-3G
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
44 * ntfs_collate_binary - Which of two binary objects should be listed first
55 static int ntfs_collate_binary(ntfs_volume
*vol
__attribute__((unused
)),
56 const void *data1
, const int data1_len
,
57 const void *data2
, const int data2_len
)
61 ntfs_log_trace("Entering.\n");
62 rc
= memcmp(data1
, data2
, min(data1_len
, data2_len
));
63 if (!rc
&& (data1_len
!= data2_len
)) {
64 if (data1_len
< data2_len
)
69 ntfs_log_trace("Done, returning %i.\n", rc
);
74 * ntfs_collate_ntofs_ulong - Which of two long ints should be listed first
85 static int ntfs_collate_ntofs_ulong(ntfs_volume
*vol
__attribute__((unused
)),
86 const void *data1
, const int data1_len
,
87 const void *data2
, const int data2_len
)
92 ntfs_log_trace("Entering.\n");
93 if (data1_len
!= data2_len
|| data1_len
!= 4) {
94 ntfs_log_error("data1_len or/and data2_len not equal to 4.\n");
95 return NTFS_COLLATION_ERROR
;
97 d1
= le32_to_cpup(data1
);
98 d2
= le32_to_cpup(data2
);
107 ntfs_log_trace("Done, returning %i.\n", rc
);
112 * ntfs_collate_ntofs_ulongs - Which of two le32 arrays should be listed first
114 * Returns: -1, 0 or 1 depending of how the arrays compare
117 static int ntfs_collate_ntofs_ulongs(ntfs_volume
*vol
__attribute__((unused
)),
118 const void *data1
, const int data1_len
,
119 const void *data2
, const int data2_len
)
126 ntfs_log_trace("Entering.\n");
127 if ((data1_len
!= data2_len
) || (data1_len
<= 0) || (data1_len
& 3)) {
128 ntfs_log_error("data1_len or data2_len not valid\n");
129 return NTFS_COLLATION_ERROR
;
131 p1
= (const le32
*)data1
;
132 p2
= (const le32
*)data2
;
135 d1
= le32_to_cpup(p1
);
137 d2
= le32_to_cpup(p2
);
139 } while ((d1
== d2
) && ((len
-= 4) > 0));
148 ntfs_log_trace("Done, returning %i.\n", rc
);
153 * ntfs_collate_ntofs_security_hash - Which of two security descriptors
154 * should be listed first
161 * JPA compare two security hash keys made of two unsigned le32
163 * Returns: -1, 0 or 1 depending of how the keys compare
165 static int ntfs_collate_ntofs_security_hash(ntfs_volume
*vol
__attribute__((unused
)),
166 const void *data1
, const int data1_len
,
167 const void *data2
, const int data2_len
)
173 ntfs_log_trace("Entering.\n");
174 if (data1_len
!= data2_len
|| data1_len
!= 8) {
175 ntfs_log_error("data1_len or/and data2_len not equal to 8.\n");
176 return NTFS_COLLATION_ERROR
;
178 p1
= (const le32
*)data1
;
179 p2
= (const le32
*)data2
;
180 d1
= le32_to_cpup(p1
);
181 d2
= le32_to_cpup(p2
);
190 d1
= le32_to_cpup(p1
);
191 d2
= le32_to_cpup(p2
);
202 ntfs_log_trace("Done, returning %i.\n", rc
);
207 * ntfs_collate_file_name - Which of two filenames should be listed first
218 static int ntfs_collate_file_name(ntfs_volume
*vol
,
219 const void *data1
, const int data1_len
__attribute__((unused
)),
220 const void *data2
, const int data2_len
__attribute__((unused
)))
222 const FILE_NAME_ATTR
*file_name_attr1
;
223 const FILE_NAME_ATTR
*file_name_attr2
;
226 ntfs_log_trace("Entering.\n");
227 file_name_attr1
= (const FILE_NAME_ATTR
*)data1
;
228 file_name_attr2
= (const FILE_NAME_ATTR
*)data2
;
229 rc
= ntfs_names_full_collate(
230 (ntfschar
*)&file_name_attr1
->file_name
,
231 file_name_attr1
->file_name_length
,
232 (ntfschar
*)&file_name_attr2
->file_name
,
233 file_name_attr2
->file_name_length
,
234 CASE_SENSITIVE
, vol
->upcase
, vol
->upcase_len
);
235 ntfs_log_trace("Done, returning %i.\n", rc
);
240 * Get a pointer to appropriate collation function.
242 * Returns NULL if the needed function is not implemented
245 COLLATE
ntfs_get_collate_function(COLLATION_RULES cr
)
250 case COLLATION_BINARY
:
251 collate
= ntfs_collate_binary
;
253 case COLLATION_FILE_NAME
:
254 collate
= ntfs_collate_file_name
;
256 case COLLATION_NTOFS_SECURITY_HASH
:
257 collate
= ntfs_collate_ntofs_security_hash
;
259 case COLLATION_NTOFS_ULONG
:
260 collate
= ntfs_collate_ntofs_ulong
;
262 case COLLATION_NTOFS_ULONGS
:
263 collate
= ntfs_collate_ntofs_ulongs
;
267 collate
= (COLLATE
)NULL
;