[ATL] Add CString.AllocSysString
[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
383 PAGED_CODE();
384
385 /* Get the data and the size of the source cell */
386 SourceData = HvGetCell(SourceHive, SourceCell);
387 DataSize = HvGetCellSize(SourceHive, SourceData);
388
389 /* Allocate a new cell in the destination hive */
390 DestinationCell = HvAllocateCell(DestinationHive,
391 DataSize,
392 StorageType,
393 HCELL_NIL);
394 if (DestinationCell == HCELL_NIL) goto Cleanup;
395
396 /* Get the data of the destination cell */
397 DestinationData = HvGetCell(DestinationHive, DestinationCell);
398
399 /* Copy the data from the source cell to the destination cell */
400 RtlMoveMemory(DestinationData, SourceData, DataSize);
401
402 Cleanup:
403
404 /* Release the cells */
405 if (DestinationData) HvReleaseCell(DestinationHive, DestinationCell);
406 if (SourceData) HvReleaseCell(SourceHive, SourceCell);
407
408 /* Return the destination cell index */
409 return DestinationCell;
410 }
411
412 HCELL_INDEX
413 NTAPI
414 CmpCopyValue(IN PHHIVE SourceHive,
415 IN HCELL_INDEX SourceValueCell,
416 IN PHHIVE DestinationHive,
417 IN HSTORAGE_TYPE StorageType)
418 {
419 PCM_KEY_VALUE Value, NewValue;
420 HCELL_INDEX NewValueCell, NewDataCell;
421 PCELL_DATA CellData;
422 ULONG SmallData;
423 ULONG DataSize;
424 BOOLEAN IsSmall;
425
426 PAGED_CODE();
427
428 /* Get the actual source data */
429 Value = (PCM_KEY_VALUE)HvGetCell(SourceHive, SourceValueCell);
430 if (!Value) ASSERT(FALSE);
431
432 /* Copy the value cell body */
433 NewValueCell = CmpCopyCell(SourceHive,
434 SourceValueCell,
435 DestinationHive,
436 StorageType);
437 if (NewValueCell == HCELL_NIL)
438 {
439 /* Not enough storage space */
440 goto Quit;
441 }
442
443 /* Copy the value data */
444 IsSmall = CmpIsKeyValueSmall(&DataSize, Value->DataLength);
445 if (DataSize == 0)
446 {
447 /* Nothing to copy */
448
449 NewValue = (PCM_KEY_VALUE)HvGetCell(DestinationHive, NewValueCell);
450 ASSERT(NewValue);
451 NewValue->DataLength = 0;
452 NewValue->Data = HCELL_NIL;
453 HvReleaseCell(DestinationHive, NewValueCell);
454
455 goto Quit;
456 }
457
458 if (DataSize <= CM_KEY_VALUE_SMALL)
459 {
460 if (IsSmall)
461 {
462 /* Small value, copy directly */
463 SmallData = Value->Data;
464 }
465 else
466 {
467 /* The value is small, but was stored in a regular cell. Get the data from it. */
468 CellData = HvGetCell(SourceHive, Value->Data);
469 ASSERT(CellData);
470 SmallData = *(PULONG)CellData;
471 HvReleaseCell(SourceHive, Value->Data);
472 }
473
474 /* This is a small key, set the data directly inside */
475 NewValue = (PCM_KEY_VALUE)HvGetCell(DestinationHive, NewValueCell);
476 ASSERT(NewValue);
477 NewValue->DataLength = DataSize + CM_KEY_VALUE_SPECIAL_SIZE;
478 NewValue->Data = SmallData;
479 HvReleaseCell(DestinationHive, NewValueCell);
480 }
481 else
482 {
483 /* Big keys are currently unsupported */
484 ASSERT_VALUE_BIG(SourceHive, DataSize);
485 // Should use CmpGetValueData and CmpSetValueDataNew for big values!
486
487 /* Regular value */
488
489 /* Copy the data cell */
490 NewDataCell = CmpCopyCell(SourceHive,
491 Value->Data,
492 DestinationHive,
493 StorageType);
494 if (NewDataCell == HCELL_NIL)
495 {
496 /* Not enough storage space */
497 HvFreeCell(DestinationHive, NewValueCell);
498 NewValueCell = HCELL_NIL;
499 goto Quit;
500 }
501
502 NewValue = (PCM_KEY_VALUE)HvGetCell(DestinationHive, NewValueCell);
503 ASSERT(NewValue);
504 NewValue->DataLength = DataSize;
505 NewValue->Data = NewDataCell;
506 HvReleaseCell(DestinationHive, NewValueCell);
507 }
508
509 Quit:
510 HvReleaseCell(SourceHive, SourceValueCell);
511
512 /* Return the copied value body cell index */
513 return NewValueCell;
514 }
515
516 NTSTATUS
517 NTAPI
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)
523 {
524 NTSTATUS Status = STATUS_SUCCESS;
525 PCELL_DATA SrcListData = NULL, DestListData = NULL;
526 HCELL_INDEX NewValue;
527 ULONG Index;
528
529 PAGED_CODE();
530
531 /* Reset the destination value list */
532 DestValueList->Count = 0;
533 DestValueList->List = HCELL_NIL;
534
535 /* Check if the list is empty */
536 if (!SrcValueList->Count)
537 return STATUS_SUCCESS;
538
539 /* Get the source value list */
540 SrcListData = HvGetCell(SourceHive, SrcValueList->List);
541 ASSERT(SrcListData);
542
543 /* Copy the actual values */
544 for (Index = 0; Index < SrcValueList->Count; Index++)
545 {
546 NewValue = CmpCopyValue(SourceHive,
547 SrcListData->u.KeyList[Index],
548 DestinationHive,
549 StorageType);
550 if (NewValue == HCELL_NIL)
551 {
552 /* Not enough storage space, stop there and cleanup afterwards */
553 Status = STATUS_INSUFFICIENT_RESOURCES;
554 break;
555 }
556
557 /* Add this value cell to the child list */
558 Status = CmpAddValueToList(DestinationHive,
559 NewValue,
560 Index,
561 StorageType,
562 DestValueList);
563 if (!NT_SUCCESS(Status))
564 {
565 /* Not enough storage space, stop there */
566
567 /* Cleanup the newly-created value here, the other ones will be cleaned up afterwards */
568 if (!CmpFreeValue(DestinationHive, NewValue))
569 HvFreeCell(DestinationHive, NewValue);
570 break;
571 }
572 }
573
574 /* Revert-cleanup if failure */
575 if (!NT_SUCCESS(Status) && (DestValueList->List != HCELL_NIL))
576 {
577 /* Do not use CmpRemoveValueFromList but directly delete the data */
578
579 /* Get the destination value list */
580 DestListData = HvGetCell(DestinationHive, DestValueList->List);
581 ASSERT(DestListData);
582
583 /* Delete each copied value */
584 while (Index--)
585 {
586 NewValue = DestListData->u.KeyList[Index];
587 if (!CmpFreeValue(DestinationHive, NewValue))
588 HvFreeCell(DestinationHive, NewValue);
589 }
590
591 /* Release and free the list */
592 HvReleaseCell(DestinationHive, DestValueList->List);
593 HvFreeCell(DestinationHive, DestValueList->List);
594
595 DestValueList->Count = 0;
596 DestValueList->List = HCELL_NIL;
597 }
598
599 /* Release the cells */
600 HvReleaseCell(SourceHive, SrcValueList->List);
601
602 return Status;
603 }