xref: /reactos/ntoskrnl/config/cmwraprs.c (revision 80774a2f)
1 /*
2  * PROJECT:         ReactOS Kernel
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            ntoskrnl/config/cmwraprs.c
5  * PURPOSE:         Configuration Manager - Wrappers for Hive Operations
6  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include "ntoskrnl.h"
12 #define NDEBUG
13 #include "debug.h"
14 
15 /* FUNCTIONS *****************************************************************/
16 
17 NTSTATUS
18 NTAPI
19 CmpCreateEvent(IN EVENT_TYPE EventType,
20                OUT PHANDLE EventHandle,
21                OUT PKEVENT *Event)
22 {
23     NTSTATUS Status;
24     OBJECT_ATTRIBUTES ObjectAttributes;
25 
26     /* Create the event */
27     InitializeObjectAttributes(&ObjectAttributes,
28                                NULL,
29                                OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
30                                NULL,
31                                NULL);
32     Status = ZwCreateEvent(EventHandle,
33                            EVENT_ALL_ACCESS,
34                            &ObjectAttributes,
35                            EventType,
36                            FALSE);
37     if (!NT_SUCCESS(Status)) return Status;
38 
39     /* Get a pointer to the object itself */
40     Status = ObReferenceObjectByHandle(*EventHandle,
41                                        EVENT_ALL_ACCESS,
42                                        NULL,
43                                        KernelMode,
44                                        (PVOID*)Event,
45                                        NULL);
46     if (!NT_SUCCESS(Status)) ZwClose(*EventHandle);
47 
48     /* Return status */
49     return Status;
50 }
51 
52 PVOID
53 NTAPI
54 CmpAllocate(IN SIZE_T Size,
55             IN BOOLEAN Paged,
56             IN ULONG Tag)
57 {
58     return ExAllocatePoolWithTag(Paged ? PagedPool : NonPagedPool,
59                                  Size,
60                                  Tag);
61 }
62 
63 VOID
64 NTAPI
65 CmpFree(IN PVOID Ptr,
66         IN ULONG Quota)
67 {
68     ExFreePool(Ptr);
69 }
70 
71 BOOLEAN
72 NTAPI
73 CmpFileRead(IN PHHIVE RegistryHive,
74             IN ULONG FileType,
75             IN PULONG FileOffset,
76             OUT PVOID Buffer,
77             IN SIZE_T BufferLength)
78 {
79     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
80     HANDLE HiveHandle = CmHive->FileHandles[FileType];
81     LARGE_INTEGER _FileOffset;
82     IO_STATUS_BLOCK IoStatusBlock;
83     NTSTATUS Status;
84 
85     _FileOffset.QuadPart = *FileOffset;
86     Status = ZwReadFile(HiveHandle, NULL, NULL, NULL, &IoStatusBlock,
87                         Buffer, (ULONG)BufferLength, &_FileOffset, NULL);
88     return NT_SUCCESS(Status) ? TRUE : FALSE;
89 }
90 
91 BOOLEAN
92 NTAPI
93 CmpFileWrite(IN PHHIVE RegistryHive,
94              IN ULONG FileType,
95              IN PULONG FileOffset,
96              IN PVOID Buffer,
97              IN SIZE_T BufferLength)
98 {
99     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
100     HANDLE HiveHandle = CmHive->FileHandles[FileType];
101     LARGE_INTEGER _FileOffset;
102     IO_STATUS_BLOCK IoStatusBlock;
103     NTSTATUS Status;
104 
105     _FileOffset.QuadPart = *FileOffset;
106     Status = ZwWriteFile(HiveHandle, NULL, NULL, NULL, &IoStatusBlock,
107                          Buffer, (ULONG)BufferLength, &_FileOffset, NULL);
108     return NT_SUCCESS(Status) ? TRUE : FALSE;
109 }
110 
111 BOOLEAN
112 NTAPI
113 CmpFileSetSize(IN PHHIVE RegistryHive,
114                IN ULONG FileType,
115                IN ULONG FileSize,
116                IN ULONG OldFileSize)
117 {
118     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
119     HANDLE HiveHandle = CmHive->FileHandles[FileType];
120     FILE_END_OF_FILE_INFORMATION EndOfFileInfo;
121     FILE_ALLOCATION_INFORMATION FileAllocationInfo;
122     IO_STATUS_BLOCK IoStatusBlock;
123     NTSTATUS Status;
124 
125     EndOfFileInfo.EndOfFile.QuadPart = FileSize;
126     Status = ZwSetInformationFile(HiveHandle,
127                                   &IoStatusBlock,
128                                   &EndOfFileInfo,
129                                   sizeof(FILE_END_OF_FILE_INFORMATION),
130                                   FileEndOfFileInformation);
131     if (!NT_SUCCESS(Status)) return FALSE;
132 
133     FileAllocationInfo.AllocationSize.QuadPart = FileSize;
134     Status = ZwSetInformationFile(HiveHandle,
135                                   &IoStatusBlock,
136                                   &FileAllocationInfo,
137                                   sizeof(FILE_ALLOCATION_INFORMATION),
138                                   FileAllocationInformation);
139     if (!NT_SUCCESS(Status)) return FALSE;
140 
141     return TRUE;
142 }
143 
144 BOOLEAN
145 NTAPI
146 CmpFileFlush(IN PHHIVE RegistryHive,
147              IN ULONG FileType,
148              IN OUT PLARGE_INTEGER FileOffset,
149              IN ULONG Length)
150 {
151     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
152     HANDLE HiveHandle = CmHive->FileHandles[FileType];
153     IO_STATUS_BLOCK IoStatusBlock;
154     NTSTATUS Status;
155 
156     Status = ZwFlushBuffersFile(HiveHandle, &IoStatusBlock);
157     return NT_SUCCESS(Status) ? TRUE : FALSE;
158 }
159