Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * collate.c - NTFS kernel collation handling.  Part of the Linux-NTFS project.
0004  *
0005  * Copyright (c) 2004 Anton Altaparmakov
0006  */
0007 
0008 #include "collate.h"
0009 #include "debug.h"
0010 #include "ntfs.h"
0011 
0012 static int ntfs_collate_binary(ntfs_volume *vol,
0013         const void *data1, const int data1_len,
0014         const void *data2, const int data2_len)
0015 {
0016     int rc;
0017 
0018     ntfs_debug("Entering.");
0019     rc = memcmp(data1, data2, min(data1_len, data2_len));
0020     if (!rc && (data1_len != data2_len)) {
0021         if (data1_len < data2_len)
0022             rc = -1;
0023         else
0024             rc = 1;
0025     }
0026     ntfs_debug("Done, returning %i", rc);
0027     return rc;
0028 }
0029 
0030 static int ntfs_collate_ntofs_ulong(ntfs_volume *vol,
0031         const void *data1, const int data1_len,
0032         const void *data2, const int data2_len)
0033 {
0034     int rc;
0035     u32 d1, d2;
0036 
0037     ntfs_debug("Entering.");
0038     // FIXME:  We don't really want to bug here.
0039     BUG_ON(data1_len != data2_len);
0040     BUG_ON(data1_len != 4);
0041     d1 = le32_to_cpup(data1);
0042     d2 = le32_to_cpup(data2);
0043     if (d1 < d2)
0044         rc = -1;
0045     else {
0046         if (d1 == d2)
0047             rc = 0;
0048         else
0049             rc = 1;
0050     }
0051     ntfs_debug("Done, returning %i", rc);
0052     return rc;
0053 }
0054 
0055 typedef int (*ntfs_collate_func_t)(ntfs_volume *, const void *, const int,
0056         const void *, const int);
0057 
0058 static ntfs_collate_func_t ntfs_do_collate0x0[3] = {
0059     ntfs_collate_binary,
0060     NULL/*ntfs_collate_file_name*/,
0061     NULL/*ntfs_collate_unicode_string*/,
0062 };
0063 
0064 static ntfs_collate_func_t ntfs_do_collate0x1[4] = {
0065     ntfs_collate_ntofs_ulong,
0066     NULL/*ntfs_collate_ntofs_sid*/,
0067     NULL/*ntfs_collate_ntofs_security_hash*/,
0068     NULL/*ntfs_collate_ntofs_ulongs*/,
0069 };
0070 
0071 /**
0072  * ntfs_collate - collate two data items using a specified collation rule
0073  * @vol:    ntfs volume to which the data items belong
0074  * @cr:     collation rule to use when comparing the items
0075  * @data1:  first data item to collate
0076  * @data1_len:  length in bytes of @data1
0077  * @data2:  second data item to collate
0078  * @data2_len:  length in bytes of @data2
0079  *
0080  * Collate the two data items @data1 and @data2 using the collation rule @cr
0081  * and return -1, 0, ir 1 if @data1 is found, respectively, to collate before,
0082  * to match, or to collate after @data2.
0083  *
0084  * For speed we use the collation rule @cr as an index into two tables of
0085  * function pointers to call the appropriate collation function.
0086  */
0087 int ntfs_collate(ntfs_volume *vol, COLLATION_RULE cr,
0088         const void *data1, const int data1_len,
0089         const void *data2, const int data2_len) {
0090     int i;
0091 
0092     ntfs_debug("Entering.");
0093     /*
0094      * FIXME:  At the moment we only support COLLATION_BINARY and
0095      * COLLATION_NTOFS_ULONG, so we BUG() for everything else for now.
0096      */
0097     BUG_ON(cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG);
0098     i = le32_to_cpu(cr);
0099     BUG_ON(i < 0);
0100     if (i <= 0x02)
0101         return ntfs_do_collate0x0[i](vol, data1, data1_len,
0102                 data2, data2_len);
0103     BUG_ON(i < 0x10);
0104     i -= 0x10;
0105     if (likely(i <= 3))
0106         return ntfs_do_collate0x1[i](vol, data1, data1_len,
0107                 data2, data2_len);
0108     BUG();
0109     return 0;
0110 }