[HEADERS] Use the new header with SPDX license identifier for host headers I've contr...
[reactos.git] / reactos / sdk / lib / 3rdparty / fullfat / ff_hash.c
1 /*****************************************************************************
2 * FullFAT - High Performance, Thread-Safe Embedded FAT File-System *
3 * Copyright (C) 2009 James Walmsley (james@worm.me.uk) *
4 * *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 * *
18 * IMPORTANT NOTICE: *
19 * ================= *
20 * Alternative Licensing is available directly from the Copyright holder, *
21 * (James Walmsley). For more information consult LICENSING.TXT to obtain *
22 * a Commercial license. *
23 * *
24 * See RESTRICTIONS.TXT for extra restrictions on the use of FullFAT. *
25 * *
26 * Removing the above notice is illegal and will invalidate this license. *
27 *****************************************************************************
28 * See http://worm.me.uk/fullfat for more information. *
29 * Or http://fullfat.googlecode.com/ for latest releases and the wiki. *
30 *****************************************************************************/
31
32 /**
33 * @file ff_hash.c
34 * @author James Walmsley
35 * @ingroup HASH
36 *
37 * @defgroup HASH HASH Table
38 * @brief Provides a simple HASH lookup table.
39 *
40 **/
41
42 #include "ff_hash.h"
43 #include <stdlib.h>
44 #include <string.h>
45
46 #ifdef FF_HASH_CACHE
47 struct _FF_HASH_TABLE {
48 FF_T_UINT8 bitTable[FF_HASH_TABLE_SIZE];
49 };
50
51 /**
52 *
53 *
54 **/
55 FF_HASH_TABLE FF_CreateHashTable() {
56 FF_HASH_TABLE pHash = (FF_HASH_TABLE) FF_MALLOC(sizeof(struct _FF_HASH_TABLE));
57
58 if(pHash) {
59 FF_ClearHashTable(pHash);
60 return pHash;
61 }
62
63 return NULL;
64 }
65
66 FF_ERROR FF_ClearHashTable(FF_HASH_TABLE pHash) {
67 if(pHash) {
68 memset(pHash->bitTable, 0, FF_HASH_TABLE_SIZE);
69 return FF_ERR_NONE;
70 }
71
72 return FF_ERR_NULL_POINTER;
73 }
74
75 FF_ERROR FF_SetHash(FF_HASH_TABLE pHash, FF_T_UINT32 nHash) {
76 FF_T_UINT32 tblIndex = ((nHash / 8) % FF_HASH_TABLE_SIZE);
77 FF_T_UINT32 tblBit = nHash % 8;
78
79 if(pHash) {
80 pHash->bitTable[tblIndex] |= (0x80 >> tblBit);
81 return FF_ERR_NONE;
82 }
83
84 return FF_ERR_NULL_POINTER;
85 }
86
87 FF_ERROR FF_ClearHash(FF_HASH_TABLE pHash, FF_T_UINT32 nHash) {
88 FF_T_UINT32 tblIndex = ((nHash / 8) % FF_HASH_TABLE_SIZE);
89 FF_T_UINT32 tblBit = nHash % 8;
90
91 if(pHash) {
92 pHash->bitTable[tblIndex] &= ~(0x80 >> tblBit);
93 return FF_ERR_NONE;
94 }
95
96 return FF_ERR_NULL_POINTER;
97 }
98
99 FF_T_BOOL FF_isHashSet(FF_HASH_TABLE pHash, FF_T_UINT32 nHash) {
100 FF_T_UINT32 tblIndex = ((nHash / 8) % FF_HASH_TABLE_SIZE);
101 FF_T_UINT32 tblBit = nHash % 8;
102
103 if(pHash) {
104 if(pHash->bitTable[tblIndex] & (0x80 >> tblBit)) {
105 return FF_TRUE;
106 }
107 }
108 return FF_FALSE;
109 }
110
111 FF_ERROR FF_DestroyHashTable(FF_HASH_TABLE pHash) {
112 if(pHash) {
113 FF_FREE(pHash);
114 return FF_ERR_NONE;
115 }
116 return FF_ERR_NULL_POINTER;
117 }
118
119 #endif