Linux Audio

Check our new training course

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