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)
9 /* INCLUDES ******************************************************************/
15 /* FUNCTIONS *****************************************************************/
19 CmpMarkValueDataDirty(IN PHHIVE Hive
,
20 IN PCM_KEY_VALUE Value
)
25 /* Make sure there's actually any data */
26 if (Value
->Data
!= HCELL_NIL
)
28 /* If this is a small key, there's no need to have it dirty */
29 if (CmpIsKeyValueSmall(&KeySize
, Value
->DataLength
)) return TRUE
;
31 /* Check if this is a big key */
32 ASSERT_VALUE_BIG(Hive
, KeySize
);
34 /* Normal value, just mark it dirty */
35 HvMarkCellDirty(Hive
, Value
->Data
, FALSE
);
38 /* Operation complete */
44 CmpFreeValueData(IN PHHIVE Hive
,
45 IN HCELL_INDEX DataCell
,
51 /* If this is a small key, the data is built-in */
52 if (!CmpIsKeyValueSmall(&KeySize
, DataLength
))
54 /* If there's no data cell, there's nothing to do */
55 if (DataCell
== HCELL_NIL
) return TRUE
;
57 /* Make sure the data cell is allocated */
58 //ASSERT(HvIsCellAllocated(Hive, DataCell));
60 /* Unsupported value type */
61 ASSERT_VALUE_BIG(Hive
, KeySize
);
63 /* Normal value, just free the data cell */
64 HvFreeCell(Hive
, DataCell
);
67 /* Operation complete */
73 CmpFreeValue(IN PHHIVE Hive
,
79 /* Get the cell data */
80 Value
= (PCM_KEY_VALUE
)HvGetCell(Hive
, Cell
);
81 if (!Value
) ASSERT(FALSE
);
84 if (!CmpFreeValueData(Hive
, Value
->Data
, Value
->DataLength
))
86 /* We failed to free the data, return failure */
87 HvReleaseCell(Hive
, Cell
);
91 /* Release the cell and free it */
92 HvReleaseCell(Hive
, Cell
);
93 HvFreeCell(Hive
, Cell
);
99 CmpFindValueByName(IN PHHIVE Hive
,
100 IN PCM_KEY_NODE KeyNode
,
101 IN PUNICODE_STRING Name
)
103 HCELL_INDEX CellIndex
;
105 /* Call the main function */
106 if (!CmpFindNameInList(Hive
,
113 ASSERT(CellIndex
== HCELL_NIL
);
116 /* Return the index */
121 * NOTE: This function should support big values, contrary to CmpValueToData.
125 CmpGetValueData(IN PHHIVE Hive
,
126 IN PCM_KEY_VALUE Value
,
129 OUT PBOOLEAN BufferAllocated
,
130 OUT PHCELL_INDEX CellToRelease
)
135 ASSERT(Value
->Signature
== CM_KEY_VALUE_SIGNATURE
);
137 /* Set failure defaults */
138 *BufferAllocated
= FALSE
;
140 *CellToRelease
= HCELL_NIL
;
142 /* Check if this is a small key */
143 if (CmpIsKeyValueSmall(Length
, Value
->DataLength
))
145 /* Return the data immediately */
146 *Buffer
= &Value
->Data
;
150 /* Unsupported at the moment */
151 ASSERT_VALUE_BIG(Hive
, *Length
);
153 /* Get the data from the cell */
154 *Buffer
= HvGetCell(Hive
, Value
->Data
);
155 if (!(*Buffer
)) return FALSE
;
157 /* Return success and the cell to be released */
158 *CellToRelease
= Value
->Data
;
163 * NOTE: This function doesn't support big values, contrary to CmpGetValueData.
167 CmpValueToData(IN PHHIVE Hive
,
168 IN PCM_KEY_VALUE Value
,
172 BOOLEAN BufferAllocated
;
173 HCELL_INDEX CellToRelease
;
177 ASSERT(Hive
->ReleaseCellRoutine
== NULL
);
179 /* Get the actual data */
180 if (!CmpGetValueData(Hive
,
188 ASSERT(BufferAllocated
== FALSE
);
189 ASSERT(Buffer
== NULL
);
193 /* This should never happen!*/
196 /* Free the buffer and bugcheck */
198 KeBugCheckEx(REGISTRY_ERROR
, 8, 0, (ULONG_PTR
)Hive
, (ULONG_PTR
)Value
);
201 /* Otherwise, return the cell data */
207 CmpAddValueToList(IN PHHIVE Hive
,
208 IN HCELL_INDEX ValueCell
,
211 IN OUT PCHILD_LIST ChildList
)
213 HCELL_INDEX ListCell
;
214 ULONG ChildCount
, Length
, i
;
219 ASSERT((((LONG
)Index
) >= 0) && (Index
<= ChildList
->Count
));
221 /* Get the number of entries in the child list */
222 ChildCount
= ChildList
->Count
;
226 /* The cell should be dirty at this point */
227 ASSERT(HvIsCellDirty(Hive
, ChildList
->List
));
229 /* Check if we have less then 100 children */
230 if (ChildCount
< 100)
232 /* Allocate just enough as requested */
233 Length
= ChildCount
* sizeof(HCELL_INDEX
);
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
)
241 /* But make sure we don't allocate beyond our block size */
242 Length
= ROUND_UP(Length
, HBLOCK_SIZE
);
246 /* Perform the allocation */
247 ListCell
= HvReallocateCell(Hive
, ChildList
->List
, Length
);
251 /* This is our first child, so allocate a single cell */
252 ListCell
= HvAllocateCell(Hive
, sizeof(HCELL_INDEX
), Type
, HCELL_NIL
);
255 /* Fail if we couldn't get a cell */
256 if (ListCell
== HCELL_NIL
) return STATUS_INSUFFICIENT_RESOURCES
;
258 /* Set this cell as the child list's list cell */
259 ChildList
->List
= ListCell
;
261 /* Get the actual key list memory */
262 CellData
= HvGetCell(Hive
, ListCell
);
263 ASSERT(CellData
!= NULL
);
265 /* Loop all the children */
266 for (i
= ChildCount
- 1; i
> Index
; i
--)
268 /* Move them all down */
269 CellData
->u
.KeyList
[i
] = CellData
->u
.KeyList
[i
- 1];
272 /* Insert us on top now */
273 CellData
->u
.KeyList
[Index
] = ValueCell
;
274 ChildList
->Count
= ChildCount
;
276 /* Release the list cell and make sure the value cell is dirty */
277 HvReleaseCell(Hive
, ListCell
);
278 ASSERT(HvIsCellDirty(Hive
, ValueCell
));
280 /* We're done here */
281 return STATUS_SUCCESS
;
286 CmpSetValueDataNew(IN PHHIVE Hive
,
289 IN ULONG StorageType
,
290 IN HCELL_INDEX ValueCell
,
291 OUT PHCELL_INDEX DataCell
)
295 ASSERT(DataSize
> CM_KEY_VALUE_SMALL
);
297 /* Check if this is a big key */
298 ASSERT_VALUE_BIG(Hive
, DataSize
);
300 /* Allocate a data cell */
301 *DataCell
= HvAllocateCell(Hive
, DataSize
, StorageType
, HCELL_NIL
);
302 if (*DataCell
== HCELL_NIL
) return STATUS_INSUFFICIENT_RESOURCES
;
304 /* Get the actual data */
305 CellData
= HvGetCell(Hive
, *DataCell
);
306 if (!CellData
) ASSERT(FALSE
);
308 /* Copy our buffer into it */
309 RtlCopyMemory(CellData
, Data
, DataSize
);
312 return STATUS_SUCCESS
;
317 CmpRemoveValueFromList(IN PHHIVE Hive
,
319 IN OUT PCHILD_LIST ChildList
)
327 ASSERT((((LONG
)Index
) >= 0) && (Index
<= ChildList
->Count
));
329 /* Get the new count after removal */
330 Count
= ChildList
->Count
- 1;
333 /* Get the actual list array */
334 CellData
= HvGetCell(Hive
, ChildList
->List
);
335 if (!CellData
) return STATUS_INSUFFICIENT_RESOURCES
;
337 /* Make sure cells data have been made dirty */
338 ASSERT(HvIsCellDirty(Hive
, ChildList
->List
));
339 ASSERT(HvIsCellDirty(Hive
, CellData
->u
.KeyList
[Index
]));
342 while (Index
< Count
)
344 /* Move everything up */
345 CellData
->u
.KeyList
[Index
] = CellData
->u
.KeyList
[Index
+ 1];
349 /* Re-allocate the cell for the list by decreasing the count */
350 NewCell
= HvReallocateCell(Hive
,
352 Count
* sizeof(HCELL_INDEX
));
353 ASSERT(NewCell
!= HCELL_NIL
);
354 HvReleaseCell(Hive
,ChildList
->List
);
356 /* Update the list cell */
357 ChildList
->List
= NewCell
;
361 /* Otherwise, we were the last entry, so free the list entirely */
362 HvFreeCell(Hive
, ChildList
->List
);
363 ChildList
->List
= HCELL_NIL
;
366 /* Update the child list with the new count */
367 ChildList
->Count
= Count
;
368 return STATUS_SUCCESS
;
373 CmpCopyCell(IN PHHIVE SourceHive
,
374 IN HCELL_INDEX SourceCell
,
375 IN PHHIVE DestinationHive
,
376 IN HSTORAGE_TYPE StorageType
)
378 PCELL_DATA SourceData
;
379 PCELL_DATA DestinationData
= NULL
;
380 HCELL_INDEX DestinationCell
= HCELL_NIL
;
384 /* Get the data and the size of the source cell */
385 SourceData
= HvGetCell(SourceHive
, SourceCell
);
386 DataSize
= HvGetCellSize(SourceHive
, SourceData
);
388 /* Allocate a new cell in the destination hive */
389 DestinationCell
= HvAllocateCell(DestinationHive
,
393 if (DestinationCell
== HCELL_NIL
) goto Cleanup
;
395 /* Get the data of the destination cell */
396 DestinationData
= HvGetCell(DestinationHive
, DestinationCell
);
398 /* Copy the data from the source cell to the destination cell */
399 RtlMoveMemory(DestinationData
, SourceData
, DataSize
);
403 /* Release the cells */
404 if (SourceData
) HvReleaseCell(SourceHive
, SourceCell
);
405 if (DestinationData
) HvReleaseCell(DestinationHive
, DestinationCell
);
407 /* Return the destination cell index */
408 return DestinationCell
;
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
)
419 NTSTATUS Status
= STATUS_SUCCESS
;
420 HCELL_INDEX CellIndex
= HCELL_NIL
;
422 PCELL_DATA SrcListData
= NULL
;
423 PCELL_DATA DestListData
= NULL
;
428 DestValueList
->Count
= SrcValueList
->Count
;
430 /* Check if the list is empty */
431 if (!DestValueList
->Count
)
433 DestValueList
->List
= HCELL_NIL
;
434 return STATUS_SUCCESS
;
437 /* Create a simple copy of the list */
438 CellIndex
= CmpCopyCell(SourceHive
,
442 if (CellIndex
== HCELL_NIL
) return STATUS_INSUFFICIENT_RESOURCES
;
444 /* Get the source and the destination value list */
445 SrcListData
= HvGetCell(SourceHive
, SrcValueList
->List
);
446 DestListData
= HvGetCell(DestinationHive
, CellIndex
);
448 /* Copy the actual values */
449 for (Index
= 0; Index
< SrcValueList
->Count
; Index
++)
451 DestListData
->u
.KeyList
[Index
] = CmpCopyCell(SourceHive
,
452 SrcListData
->u
.KeyList
[Index
],
455 if (DestListData
->u
.KeyList
[Index
] == HCELL_NIL
)
457 Status
= STATUS_INSUFFICIENT_RESOURCES
;
462 /* Release the cells */
463 if (SrcListData
) HvReleaseCell(SourceHive
, SrcValueList
->List
);
464 if (DestListData
) HvReleaseCell(DestinationHive
, CellIndex
);