[FREELDR/CMLIB]
[reactos.git] / reactos / lib / cmlib / cmtools.c
1 /*
2 * PROJECT: registry manipulation library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * COPYRIGHT: Copyright 2005 Filip Navara <navaraf@reactos.org>
5 * Copyright 2001 - 2005 Eric Kohl
6 */
7
8 #include "cmlib.h"
9 #define NDEBUG
10 #include <debug.h>
11
12 BOOLEAN
13 NTAPI
14 CmCompareHash(
15 IN PCUNICODE_STRING KeyName,
16 IN PCHAR HashString,
17 IN BOOLEAN CaseInsensitive)
18 {
19 CHAR Buffer[4];
20
21 Buffer[0] = (KeyName->Length >= 2) ? (CHAR)KeyName->Buffer[0] : 0;
22 Buffer[1] = (KeyName->Length >= 4) ? (CHAR)KeyName->Buffer[1] : 0;
23 Buffer[2] = (KeyName->Length >= 6) ? (CHAR)KeyName->Buffer[2] : 0;
24 Buffer[3] = (KeyName->Length >= 8) ? (CHAR)KeyName->Buffer[3] : 0;
25
26 if (CaseInsensitive)
27 {
28 return (strncasecmp(Buffer, HashString, 4) == 0);
29 }
30 else
31 {
32 return (strncmp(Buffer, HashString, 4) == 0);
33 }
34 }
35
36 BOOLEAN
37 NTAPI
38 CmComparePackedNames(
39 IN PCUNICODE_STRING CompareName,
40 IN PVOID Name,
41 IN USHORT NameLength,
42 IN BOOLEAN NamePacked,
43 IN BOOLEAN CaseInsensitive)
44 {
45 ULONG i;
46
47 if (NamePacked == TRUE)
48 {
49 PUCHAR PackedName = (PUCHAR)Name;
50
51 if (CompareName->Length != NameLength * sizeof(WCHAR))
52 {
53 //DPRINT1("Length doesn'T match %lu / %lu\n", CompareName->Length, NameLength);
54 return FALSE;
55 }
56
57 if (CaseInsensitive)
58 {
59 for (i = 0; i < CompareName->Length / sizeof(WCHAR); i++)
60 {
61 //DbgPrint("%c/%c,",
62 // RtlUpcaseUnicodeChar(CompareName->Buffer[i]),
63 // RtlUpcaseUnicodeChar(PackedName[i]));
64 if (RtlUpcaseUnicodeChar(CompareName->Buffer[i]) !=
65 RtlUpcaseUnicodeChar(PackedName[i]))
66 {
67 //DbgPrint("\nFailed!\n");
68 return FALSE;
69 }
70 }
71 //DbgPrint("\nSuccess!\n");
72 }
73 else
74 {
75 for (i = 0; i < CompareName->Length / sizeof(WCHAR); i++)
76 {
77 if (CompareName->Buffer[i] != PackedName[i])
78 return FALSE;
79 }
80 }
81
82 }
83 else
84 {
85 PWCHAR UnicodeName = (PWCHAR)Name;
86
87 if (CompareName->Length != NameLength)
88 return FALSE;
89
90 if (CaseInsensitive)
91 {
92 for (i = 0; i < CompareName->Length / sizeof(WCHAR); i++)
93 {
94 if (RtlUpcaseUnicodeChar(CompareName->Buffer[i]) !=
95 RtlUpcaseUnicodeChar(UnicodeName[i]))
96 return FALSE;
97 }
98 }
99 else
100 {
101 for (i = 0; i < CompareName->Length / sizeof(WCHAR); i++)
102 {
103 if (CompareName->Buffer[i] != UnicodeName[i])
104 return FALSE;
105 }
106 }
107 }
108
109 return TRUE;
110 }
111
112
113 BOOLEAN
114 NTAPI
115 CmCompareKeyName(
116 IN PCM_KEY_NODE KeyNode,
117 IN PCUNICODE_STRING KeyName,
118 IN BOOLEAN CaseInsensitive)
119 {
120 ASSERT(KeyNode->Signature == CM_KEY_NODE_SIGNATURE);
121 return CmComparePackedNames(KeyName,
122 KeyNode->Name,
123 KeyNode->NameLength,
124 (KeyNode->Flags & KEY_COMP_NAME) ? TRUE : FALSE,
125 CaseInsensitive);
126 }
127
128 BOOLEAN
129 NTAPI
130 CmCompareKeyValueName(
131 IN PCM_KEY_VALUE ValueCell,
132 IN PCUNICODE_STRING ValueName,
133 IN BOOLEAN CaseInsensitive)
134 {
135 ASSERT(ValueCell->Signature == CM_KEY_VALUE_SIGNATURE);
136 return CmComparePackedNames(ValueName,
137 ValueCell->Name,
138 ValueCell->NameLength,
139 (ValueCell->Flags & VALUE_COMP_NAME) ? TRUE : FALSE,
140 CaseInsensitive);
141 }
142
143 ULONG
144 NTAPI
145 CmCopyPackedName(
146 _Out_ PWCHAR Buffer,
147 _In_ ULONG BufferLength,
148 _In_ PVOID Name,
149 _In_ USHORT NameLength,
150 _In_ BOOLEAN NamePacked)
151 {
152 ULONG CharCount, i;
153 ASSERT(Name != 0);
154 ASSERT(NameLength != 0);
155
156 if (NamePacked == TRUE)
157 {
158 NameLength *= sizeof(WCHAR);
159 CharCount = min(BufferLength, NameLength) / sizeof(WCHAR);
160
161 if (Buffer != NULL)
162 {
163 PUCHAR PackedName = (PUCHAR)Name;
164
165 for (i = 0; i < CharCount; i++)
166 {
167 Buffer[i] = PackedName[i];
168 }
169 }
170 }
171 else
172 {
173 CharCount = min(BufferLength, NameLength) / sizeof(WCHAR);
174
175 if (Buffer != NULL)
176 {
177 PWCHAR UnicodeName = (PWCHAR)Name;
178
179 for (i = 0; i < CharCount; i++)
180 {
181 Buffer[i] = UnicodeName[i];
182 }
183 }
184 }
185
186 if (BufferLength >= NameLength + sizeof(WCHAR))
187 {
188 Buffer[NameLength / sizeof(WCHAR)] = '\0';
189 }
190
191 return NameLength + sizeof(WCHAR);
192 }
193
194 ULONG
195 NTAPI
196 CmCopyKeyName(
197 _In_ PCM_KEY_NODE KeyNode,
198 _Out_ PWCHAR KeyNameBuffer,
199 _Inout_ ULONG BufferLength)
200 {
201 ASSERT(KeyNode->Signature == CM_KEY_NODE_SIGNATURE);
202 return CmCopyPackedName(KeyNameBuffer,
203 BufferLength,
204 KeyNode->Name,
205 KeyNode->NameLength,
206 (KeyNode->Flags & KEY_COMP_NAME) ? TRUE : FALSE);
207 }
208
209 ULONG
210 NTAPI
211 CmCopyKeyValueName(
212 _In_ PCM_KEY_VALUE ValueCell,
213 _Out_ PWCHAR ValueNameBuffer,
214 _Inout_ ULONG BufferLength)
215 {
216 ASSERT(ValueCell->Signature == CM_KEY_VALUE_SIGNATURE);
217 return CmCopyPackedName(ValueNameBuffer,
218 BufferLength,
219 ValueCell->Name,
220 ValueCell->NameLength,
221 (ValueCell->Flags & VALUE_COMP_NAME) ? TRUE : FALSE);
222 }