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
;
385 /* Get the data and the size of the source cell */
386 SourceData
= HvGetCell(SourceHive
, SourceCell
);
387 DataSize
= HvGetCellSize(SourceHive
, SourceData
);
389 /* Allocate a new cell in the destination hive */
390 DestinationCell
= HvAllocateCell(DestinationHive
,
394 if (DestinationCell
== HCELL_NIL
) goto Cleanup
;
396 /* Get the data of the destination cell */
397 DestinationData
= HvGetCell(DestinationHive
, DestinationCell
);
399 /* Copy the data from the source cell to the destination cell */
400 RtlMoveMemory(DestinationData
, SourceData
, DataSize
);
404 /* Release the cells */
405 if (DestinationData
) HvReleaseCell(DestinationHive
, DestinationCell
);
406 if (SourceData
) HvReleaseCell(SourceHive
, SourceCell
);
408 /* Return the destination cell index */
409 return DestinationCell
;
414 CmpCopyValue(IN PHHIVE SourceHive
,
415 IN HCELL_INDEX SourceValueCell
,
416 IN PHHIVE DestinationHive
,
417 IN HSTORAGE_TYPE StorageType
)
419 PCM_KEY_VALUE Value
, NewValue
;
420 HCELL_INDEX NewValueCell
, NewDataCell
;
428 /* Get the actual source data */
429 Value
= (PCM_KEY_VALUE
)HvGetCell(SourceHive
, SourceValueCell
);
430 if (!Value
) ASSERT(FALSE
);
432 /* Copy the value cell body */
433 NewValueCell
= CmpCopyCell(SourceHive
,
437 if (NewValueCell
== HCELL_NIL
)
439 /* Not enough storage space */
443 /* Copy the value data */
444 IsSmall
= CmpIsKeyValueSmall(&DataSize
, Value
->DataLength
);
447 /* Nothing to copy */
449 NewValue
= (PCM_KEY_VALUE
)HvGetCell(DestinationHive
, NewValueCell
);
451 NewValue
->DataLength
= 0;
452 NewValue
->Data
= HCELL_NIL
;
453 HvReleaseCell(DestinationHive
, NewValueCell
);
458 if (DataSize
<= CM_KEY_VALUE_SMALL
)
462 /* Small value, copy directly */
463 SmallData
= Value
->Data
;
467 /* The value is small, but was stored in a regular cell. Get the data from it. */
468 CellData
= HvGetCell(SourceHive
, Value
->Data
);
470 SmallData
= *(PULONG
)CellData
;
471 HvReleaseCell(SourceHive
, Value
->Data
);
474 /* This is a small key, set the data directly inside */
475 NewValue
= (PCM_KEY_VALUE
)HvGetCell(DestinationHive
, NewValueCell
);
477 NewValue
->DataLength
= DataSize
+ CM_KEY_VALUE_SPECIAL_SIZE
;
478 NewValue
->Data
= SmallData
;
479 HvReleaseCell(DestinationHive
, NewValueCell
);
483 /* Big keys are currently unsupported */
484 ASSERT_VALUE_BIG(SourceHive
, DataSize
);
485 // Should use CmpGetValueData and CmpSetValueDataNew for big values!
489 /* Copy the data cell */
490 NewDataCell
= CmpCopyCell(SourceHive
,
494 if (NewDataCell
== HCELL_NIL
)
496 /* Not enough storage space */
497 HvFreeCell(DestinationHive
, NewValueCell
);
498 NewValueCell
= HCELL_NIL
;
502 NewValue
= (PCM_KEY_VALUE
)HvGetCell(DestinationHive
, NewValueCell
);
504 NewValue
->DataLength
= DataSize
;
505 NewValue
->Data
= NewDataCell
;
506 HvReleaseCell(DestinationHive
, NewValueCell
);
510 HvReleaseCell(SourceHive
, SourceValueCell
);
512 /* Return the copied value body cell index */
518 CmpCopyKeyValueList(IN PHHIVE SourceHive
,
519 IN PCHILD_LIST SrcValueList
,
520 IN PHHIVE DestinationHive
,
521 IN OUT PCHILD_LIST DestValueList
,
522 IN HSTORAGE_TYPE StorageType
)
524 NTSTATUS Status
= STATUS_SUCCESS
;
525 PCELL_DATA SrcListData
= NULL
, DestListData
= NULL
;
526 HCELL_INDEX NewValue
;
531 /* Reset the destination value list */
532 DestValueList
->Count
= 0;
533 DestValueList
->List
= HCELL_NIL
;
535 /* Check if the list is empty */
536 if (!SrcValueList
->Count
)
537 return STATUS_SUCCESS
;
539 /* Get the source value list */
540 SrcListData
= HvGetCell(SourceHive
, SrcValueList
->List
);
543 /* Copy the actual values */
544 for (Index
= 0; Index
< SrcValueList
->Count
; Index
++)
546 NewValue
= CmpCopyValue(SourceHive
,
547 SrcListData
->u
.KeyList
[Index
],
550 if (NewValue
== HCELL_NIL
)
552 /* Not enough storage space, stop there and cleanup afterwards */
553 Status
= STATUS_INSUFFICIENT_RESOURCES
;
557 /* Add this value cell to the child list */
558 Status
= CmpAddValueToList(DestinationHive
,
563 if (!NT_SUCCESS(Status
))
565 /* Not enough storage space, stop there */
567 /* Cleanup the newly-created value here, the other ones will be cleaned up afterwards */
568 if (!CmpFreeValue(DestinationHive
, NewValue
))
569 HvFreeCell(DestinationHive
, NewValue
);
574 /* Revert-cleanup if failure */
575 if (!NT_SUCCESS(Status
) && (DestValueList
->List
!= HCELL_NIL
))
577 /* Do not use CmpRemoveValueFromList but directly delete the data */
579 /* Get the destination value list */
580 DestListData
= HvGetCell(DestinationHive
, DestValueList
->List
);
581 ASSERT(DestListData
);
583 /* Delete each copied value */
586 NewValue
= DestListData
->u
.KeyList
[Index
];
587 if (!CmpFreeValue(DestinationHive
, NewValue
))
588 HvFreeCell(DestinationHive
, NewValue
);
591 /* Release and free the list */
592 HvReleaseCell(DestinationHive
, DestValueList
->List
);
593 HvFreeCell(DestinationHive
, DestValueList
->List
);
595 DestValueList
->Count
= 0;
596 DestValueList
->List
= HCELL_NIL
;
599 /* Release the cells */
600 HvReleaseCell(SourceHive
, SrcValueList
->List
);