Create the AHCI branch for Aman's work
[reactos.git] / sdk / lib / cmlib / cmvalue.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/cmlib/cmvalue.c
5 * PURPOSE: Configuration Manager Library - Cell Values
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "cmlib.h"
12 #define NDEBUG
13 #include <debug.h>
14
15 /* FUNCTIONS *****************************************************************/
16
17 BOOLEAN
18 NTAPI
19 CmpMarkValueDataDirty(IN PHHIVE Hive,
20 IN PCM_KEY_VALUE Value)
21 {
22 ULONG KeySize;
23 PAGED_CODE();
24
25 /* Make sure there's actually any data */
26 if (Value->Data != HCELL_NIL)
27 {
28 /* If this is a small key, there's no need to have it dirty */
29 if (CmpIsKeyValueSmall(&KeySize, Value->DataLength)) return TRUE;
30
31 /* Check if this is a big key */
32 ASSERT_VALUE_BIG(Hive, KeySize);
33
34 /* Normal value, just mark it dirty */
35 HvMarkCellDirty(Hive, Value->Data, FALSE);
36 }
37
38 /* Operation complete */
39 return TRUE;
40 }
41
42 BOOLEAN
43 NTAPI
44 CmpFreeValueData(IN PHHIVE Hive,
45 IN HCELL_INDEX DataCell,
46 IN ULONG DataLength)
47 {
48 ULONG KeySize;
49 PAGED_CODE();
50
51 /* If this is a small key, the data is built-in */
52 if (!CmpIsKeyValueSmall(&KeySize, DataLength))
53 {
54 /* If there's no data cell, there's nothing to do */
55 if (DataCell == HCELL_NIL) return TRUE;
56
57 /* Make sure the data cell is allocated */
58 //ASSERT(HvIsCellAllocated(Hive, DataCell));
59
60 /* Unsupported value type */
61 ASSERT_VALUE_BIG(Hive, KeySize);
62
63 /* Normal value, just free the data cell */
64 HvFreeCell(Hive, DataCell);
65 }
66
67 /* Operation complete */
68 return TRUE;
69 }
70
71 BOOLEAN
72 NTAPI
73 CmpFreeValue(IN PHHIVE Hive,
74 IN HCELL_INDEX Cell)
75 {
76 PCM_KEY_VALUE Value;
77 PAGED_CODE();
78
79 /* Get the cell data */
80 Value = (PCM_KEY_VALUE)HvGetCell(Hive, Cell);
81 if (!Value) ASSERT(FALSE);
82
83 /* Free it */
84 if (!CmpFreeValueData(Hive, Value->Data, Value->DataLength))
85 {
86 /* We failed to free the data, return failure */
87 HvReleaseCell(Hive, Cell);
88 return FALSE;
89 }
90
91 /* Release the cell and free it */
92 HvReleaseCell(Hive, Cell);
93 HvFreeCell(Hive, Cell);
94 return TRUE;
95 }
96
97 HCELL_INDEX
98 NTAPI
99 CmpFindValueByName(IN PHHIVE Hive,
100 IN PCM_KEY_NODE KeyNode,
101 IN PUNICODE_STRING Name)
102 {
103 HCELL_INDEX CellIndex;
104
105 /* Call the main function */
106 if (!CmpFindNameInList(Hive,
107 &KeyNode->ValueList,
108 Name,
109 NULL,
110 &CellIndex))
111 {
112 /* Sanity check */
113 ASSERT(CellIndex == HCELL_NIL);
114 }
115
116 /* Return the index */
117 return CellIndex;
118 }
119
120 /*
121 * NOTE: This function should support big values, contrary to CmpValueToData.
122 */
123 BOOLEAN
124 NTAPI
125 CmpGetValueData(IN PHHIVE Hive,
126 IN PCM_KEY_VALUE Value,
127 OUT PULONG Length,
128 OUT PVOID *Buffer,
129 OUT PBOOLEAN BufferAllocated,
130 OUT PHCELL_INDEX CellToRelease)
131 {
132 PAGED_CODE();
133
134 /* Sanity check */
135 ASSERT(Value->Signature == CM_KEY_VALUE_SIGNATURE);
136
137 /* Set failure defaults */
138 *BufferAllocated = FALSE;
139 *Buffer = NULL;
140 *CellToRelease = HCELL_NIL;
141
142 /* Check if this is a small key */
143 if (CmpIsKeyValueSmall(Length, Value->DataLength))
144 {
145 /* Return the data immediately */
146 *Buffer = &Value->Data;
147 return TRUE;
148 }
149
150 /* Unsupported at the moment */
151 ASSERT_VALUE_BIG(Hive, *Length);
152
153 /* Get the data from the cell */
154 *Buffer = HvGetCell(Hive, Value->Data);
155 if (!(*Buffer)) return FALSE;
156
157 /* Return success and the cell to be released */
158 *CellToRelease = Value->Data;
159 return TRUE;
160 }
161
162 /*
163 * NOTE: This function doesn't support big values, contrary to CmpGetValueData.
164 */
165 PCELL_DATA
166 NTAPI
167 CmpValueToData(IN PHHIVE Hive,
168 IN PCM_KEY_VALUE Value,
169 OUT PULONG Length)
170 {
171 PCELL_DATA Buffer;
172 BOOLEAN BufferAllocated;
173 HCELL_INDEX CellToRelease;
174 PAGED_CODE();
175
176 /* Sanity check */
177 ASSERT(Hive->ReleaseCellRoutine == NULL);
178
179 /* Get the actual data */
180 if (!CmpGetValueData(Hive,
181 Value,
182 Length,
183 (PVOID)&Buffer,
184 &BufferAllocated,
185 &CellToRelease))
186 {
187 /* We failed */
188 ASSERT(BufferAllocated == FALSE);
189 ASSERT(Buffer == NULL);
190 return NULL;
191 }
192
193 /* This should never happen!*/
194 if (BufferAllocated)
195 {
196 /* Free the buffer and bugcheck */
197 CmpFree(Buffer, 0);
198 KeBugCheckEx(REGISTRY_ERROR, 8, 0, (ULONG_PTR)Hive, (ULONG_PTR)Value);
199 }
200
201 /* Otherwise, return the cell data */
202 return Buffer;
203 }
204
205 NTSTATUS
206 NTAPI
207 CmpAddValueToList(IN PHHIVE Hive,
208 IN HCELL_INDEX ValueCell,
209 IN ULONG Index,
210 IN ULONG Type,
211 IN OUT PCHILD_LIST ChildList)
212 {
213 HCELL_INDEX ListCell;
214 ULONG ChildCount, Length, i;
215 PCELL_DATA CellData;
216 PAGED_CODE();
217
218 /* Sanity check */
219 ASSERT((((LONG)Index) >= 0) && (Index <= ChildList->Count));
220
221 /* Get the number of entries in the child list */
222 ChildCount = ChildList->Count;
223 ChildCount++;
224 if (ChildCount > 1)
225 {
226 /* The cell should be dirty at this point */
227 ASSERT(HvIsCellDirty(Hive, ChildList->List));
228
229 /* Check if we have less then 100 children */
230 if (ChildCount < 100)
231 {
232 /* Allocate just enough as requested */
233 Length = ChildCount * sizeof(HCELL_INDEX);
234 }
235 else
236 {
237 /* Otherwise, we have quite a few, so allocate a batch */
238 Length = ROUND_UP(ChildCount, 100) * sizeof(HCELL_INDEX);
239 if (Length > HBLOCK_SIZE)
240 {
241 /* But make sure we don't allocate beyond our block size */
242 Length = ROUND_UP(Length, HBLOCK_SIZE);
243 }
244 }
245
246 /* Perform the allocation */
247 ListCell = HvReallocateCell(Hive, ChildList->List, Length);
248 }
249 else
250 {
251 /* This is our first child, so allocate a single cell */
252 ListCell = HvAllocateCell(Hive, sizeof(HCELL_INDEX), Type, HCELL_NIL);
253 }
254
255 /* Fail if we couldn't get a cell */
256 if (ListCell == HCELL_NIL) return STATUS_INSUFFICIENT_RESOURCES;
257
258 /* Set this cell as the child list's list cell */
259 ChildList->List = ListCell;
260
261 /* Get the actual key list memory */
262 CellData = HvGetCell(Hive, ListCell);
263 ASSERT(CellData != NULL);
264
265 /* Loop all the children */
266 for (i = ChildCount - 1; i > Index; i--)
267 {
268 /* Move them all down */
269 CellData->u.KeyList[i] = CellData->u.KeyList[i - 1];
270 }
271
272 /* Insert us on top now */
273 CellData->u.KeyList[Index] = ValueCell;
274 ChildList->Count = ChildCount;
275
276 /* Release the list cell and make sure the value cell is dirty */
277 HvReleaseCell(Hive, ListCell);
278 ASSERT(HvIsCellDirty(Hive, ValueCell));
279
280 /* We're done here */
281 return STATUS_SUCCESS;
282 }
283
284 NTSTATUS
285 NTAPI
286 CmpSetValueDataNew(IN PHHIVE Hive,
287 IN PVOID Data,
288 IN ULONG DataSize,
289 IN ULONG StorageType,
290 IN HCELL_INDEX ValueCell,
291 OUT PHCELL_INDEX DataCell)
292 {
293 PCELL_DATA CellData;
294 PAGED_CODE();
295 ASSERT(DataSize > CM_KEY_VALUE_SMALL);
296
297 /* Check if this is a big key */
298 ASSERT_VALUE_BIG(Hive, DataSize);
299
300 /* Allocate a data cell */
301 *DataCell = HvAllocateCell(Hive, DataSize, StorageType, HCELL_NIL);
302 if (*DataCell == HCELL_NIL) return STATUS_INSUFFICIENT_RESOURCES;
303
304 /* Get the actual data */
305 CellData = HvGetCell(Hive, *DataCell);
306 if (!CellData) ASSERT(FALSE);
307
308 /* Copy our buffer into it */
309 RtlCopyMemory(CellData, Data, DataSize);
310
311 /* All done */
312 return STATUS_SUCCESS;
313 }
314
315 NTSTATUS
316 NTAPI
317 CmpRemoveValueFromList(IN PHHIVE Hive,
318 IN ULONG Index,
319 IN OUT PCHILD_LIST ChildList)
320 {
321 ULONG Count;
322 PCELL_DATA CellData;
323 HCELL_INDEX NewCell;
324 PAGED_CODE();
325
326 /* Sanity check */
327 ASSERT((((LONG)Index) >= 0) && (Index <= ChildList->Count));
328
329 /* Get the new count after removal */
330 Count = ChildList->Count - 1;
331 if (Count > 0)
332 {
333 /* Get the actual list array */
334 CellData = HvGetCell(Hive, ChildList->List);
335 if (!CellData) return STATUS_INSUFFICIENT_RESOURCES;
336
337 /* Make sure cells data have been made dirty */
338 ASSERT(HvIsCellDirty(Hive, ChildList->List));
339 ASSERT(HvIsCellDirty(Hive, CellData->u.KeyList[Index]));
340
341 /* Loop the list */
342 while (Index < Count)
343 {
344 /* Move everything up */
345 CellData->u.KeyList[Index] = CellData->u.KeyList[Index + 1];
346 Index++;
347 }
348
349 /* Re-allocate the cell for the list by decreasing the count */
350 NewCell = HvReallocateCell(Hive,
351 ChildList->List,
352 Count * sizeof(HCELL_INDEX));
353 ASSERT(NewCell != HCELL_NIL);
354 HvReleaseCell(Hive,ChildList->List);
355
356 /* Update the list cell */
357 ChildList->List = NewCell;
358 }
359 else
360 {
361 /* Otherwise, we were the last entry, so free the list entirely */
362 HvFreeCell(Hive, ChildList->List);
363 ChildList->List = HCELL_NIL;
364 }
365
366 /* Update the child list with the new count */
367 ChildList->Count = Count;
368 return STATUS_SUCCESS;
369 }
370
371 HCELL_INDEX
372 NTAPI
373 CmpCopyCell(IN PHHIVE SourceHive,
374 IN HCELL_INDEX SourceCell,
375 IN PHHIVE DestinationHive,
376 IN HSTORAGE_TYPE StorageType)
377 {
378 PCELL_DATA SourceData;
379 PCELL_DATA DestinationData = NULL;
380 HCELL_INDEX DestinationCell = HCELL_NIL;
381 LONG DataSize;
382 PAGED_CODE();
383
384 /* Get the data and the size of the source cell */
385 SourceData = HvGetCell(SourceHive, SourceCell);
386 DataSize = HvGetCellSize(SourceHive, SourceData);
387
388 /* Allocate a new cell in the destination hive */
389 DestinationCell = HvAllocateCell(DestinationHive,
390 DataSize,
391 StorageType,
392 HCELL_NIL);
393 if (DestinationCell == HCELL_NIL) goto Cleanup;
394
395 /* Get the data of the destination cell */
396 DestinationData = HvGetCell(DestinationHive, DestinationCell);
397
398 /* Copy the data from the source cell to the destination cell */
399 RtlMoveMemory(DestinationData, SourceData, DataSize);
400
401 Cleanup:
402
403 /* Release the cells */
404 if (SourceData) HvReleaseCell(SourceHive, SourceCell);
405 if (DestinationData) HvReleaseCell(DestinationHive, DestinationCell);
406
407 /* Return the destination cell index */
408 return DestinationCell;
409 }
410
411 NTSTATUS
412 NTAPI
413 CmpCopyKeyValueList(IN PHHIVE SourceHive,
414 IN PCHILD_LIST SrcValueList,
415 IN PHHIVE DestinationHive,
416 IN OUT PCHILD_LIST DestValueList,
417 IN HSTORAGE_TYPE StorageType)
418 {
419 NTSTATUS Status = STATUS_SUCCESS;
420 HCELL_INDEX CellIndex = HCELL_NIL;
421 ULONG Index;
422 PCELL_DATA SrcListData = NULL;
423 PCELL_DATA DestListData = NULL;
424
425 PAGED_CODE();
426
427 /* Set the count */
428 DestValueList->Count = SrcValueList->Count;
429
430 /* Check if the list is empty */
431 if (!DestValueList->Count)
432 {
433 DestValueList->List = HCELL_NIL;
434 return STATUS_SUCCESS;
435 }
436
437 /* Create a simple copy of the list */
438 CellIndex = CmpCopyCell(SourceHive,
439 SrcValueList->List,
440 DestinationHive,
441 StorageType);
442 if (CellIndex == HCELL_NIL) return STATUS_INSUFFICIENT_RESOURCES;
443
444 /* Get the source and the destination value list */
445 SrcListData = HvGetCell(SourceHive, SrcValueList->List);
446 DestListData = HvGetCell(DestinationHive, CellIndex);
447
448 /* Copy the actual values */
449 for (Index = 0; Index < SrcValueList->Count; Index++)
450 {
451 DestListData->u.KeyList[Index] = CmpCopyCell(SourceHive,
452 SrcListData->u.KeyList[Index],
453 DestinationHive,
454 StorageType);
455 if (DestListData->u.KeyList[Index] == HCELL_NIL)
456 {
457 Status = STATUS_INSUFFICIENT_RESOURCES;
458 break;
459 }
460 }
461
462 /* Release the cells */
463 if (SrcListData) HvReleaseCell(SourceHive, SrcValueList->List);
464 if (DestListData) HvReleaseCell(DestinationHive, CellIndex);
465
466 return Status;
467 }