4275cf358ba8918268a92b9f9ced5e5dad8c39f9
[reactos.git] / reactos / tools / mkhive / rtl.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS hive maker
4 * FILE: tools/mkhive/rtl.c
5 * PURPOSE: Runtime Library
6 */
7
8 #include <stdlib.h>
9 #include <stdarg.h>
10
11 /* gcc defaults to cdecl */
12 #if defined(__GNUC__)
13 #undef __cdecl
14 #define __cdecl
15 #endif
16
17 #include "mkhive.h"
18 #include <bitmap.c>
19
20 /*
21 * @implemented
22 *
23 * NOTES
24 * If source is NULL the length of source is assumed to be 0.
25 */
26 VOID NTAPI
27 RtlInitAnsiString(
28 IN OUT PANSI_STRING DestinationString,
29 IN PCSTR SourceString)
30 {
31 SIZE_T DestSize;
32
33 if(SourceString)
34 {
35 DestSize = strlen(SourceString);
36 DestinationString->Length = (USHORT)DestSize;
37 DestinationString->MaximumLength = (USHORT)DestSize + sizeof(CHAR);
38 }
39 else
40 {
41 DestinationString->Length = 0;
42 DestinationString->MaximumLength = 0;
43 }
44
45 DestinationString->Buffer = (PCHAR)SourceString;
46 }
47
48 /*
49 * @implemented
50 *
51 * NOTES
52 * If source is NULL the length of source is assumed to be 0.
53 */
54 VOID NTAPI
55 RtlInitUnicodeString(
56 IN OUT PUNICODE_STRING DestinationString,
57 IN PCWSTR SourceString)
58 {
59 SIZE_T DestSize;
60
61 if(SourceString)
62 {
63 DestSize = strlenW(SourceString) * sizeof(WCHAR);
64 DestinationString->Length = (USHORT)DestSize;
65 DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR);
66 }
67 else
68 {
69 DestinationString->Length = 0;
70 DestinationString->MaximumLength = 0;
71 }
72
73 DestinationString->Buffer = (PWCHAR)SourceString;
74 }
75
76 NTSTATUS NTAPI
77 RtlAnsiStringToUnicodeString(
78 IN OUT PUNICODE_STRING UniDest,
79 IN PANSI_STRING AnsiSource,
80 IN BOOLEAN AllocateDestinationString)
81 {
82 ULONG Length;
83 PUCHAR WideString;
84 USHORT i;
85
86 Length = AnsiSource->Length * sizeof(WCHAR);
87 if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
88 UniDest->Length = (USHORT)Length;
89
90 if (AllocateDestinationString)
91 {
92 UniDest->MaximumLength = (USHORT)Length + sizeof(WCHAR);
93 UniDest->Buffer = (PWSTR) malloc(UniDest->MaximumLength);
94 if (!UniDest->Buffer)
95 return STATUS_NO_MEMORY;
96 }
97 else if (UniDest->Length >= UniDest->MaximumLength)
98 {
99 return STATUS_BUFFER_OVERFLOW;
100 }
101
102 WideString = (PUCHAR)UniDest->Buffer;
103 for (i = 0; i <= AnsiSource->Length; i++)
104 {
105 WideString[2 * i + 0] = AnsiSource->Buffer[i];
106 WideString[2 * i + 1] = 0;
107 }
108 return STATUS_SUCCESS;
109 }
110
111 LONG NTAPI
112 RtlCompareUnicodeString(
113 IN PCUNICODE_STRING String1,
114 IN PCUNICODE_STRING String2,
115 IN BOOLEAN CaseInSensitive)
116 {
117 USHORT i;
118 WCHAR c1, c2;
119
120 for (i = 0; i <= String1->Length / sizeof(WCHAR) && i <= String2->Length / sizeof(WCHAR); i++)
121 {
122 if (CaseInSensitive)
123 {
124 c1 = RtlUpcaseUnicodeChar(String1->Buffer[i]);
125 c2 = RtlUpcaseUnicodeChar(String2->Buffer[i]);
126 }
127 else
128 {
129 c1 = String1->Buffer[i];
130 c2 = String2->Buffer[i];
131 }
132
133 if (c1 < c2)
134 return -1;
135 else if (c1 > c2)
136 return 1;
137 }
138
139 return 0;
140 }
141
142 WCHAR NTAPI
143 RtlUpcaseUnicodeChar(
144 IN WCHAR Source)
145 {
146 USHORT Offset;
147
148 if (Source < 'a')
149 return Source;
150
151 if (Source <= 'z')
152 return (Source - ('a' - 'A'));
153
154 Offset = 0;
155
156 return Source + (SHORT)Offset;
157 }
158
159 VOID NTAPI
160 KeQuerySystemTime(
161 OUT PLARGE_INTEGER CurrentTime)
162 {
163 CurrentTime->QuadPart = 0;
164 }
165
166 PVOID NTAPI
167 ExAllocatePool(
168 IN POOL_TYPE PoolType,
169 IN SIZE_T NumberOfBytes)
170 {
171 return (PVOID) malloc(NumberOfBytes);
172 }
173
174 VOID NTAPI
175 ExFreePool(
176 IN PVOID p)
177 {
178 free(p);
179 }
180
181 ULONG
182 __cdecl
183 DbgPrint(
184 IN CHAR *Format,
185 IN ...)
186 {
187 va_list ap;
188 va_start(ap, Format);
189 vprintf(Format, ap);
190 va_end(ap);
191
192 return 0;
193 }
194
195 VOID
196 NTAPI
197 RtlAssert(PVOID FailedAssertion,
198 PVOID FileName,
199 ULONG LineNumber,
200 PCHAR Message)
201 {
202 if (NULL != Message)
203 {
204 DbgPrint("Assertion \'%s\' failed at %s line %d: %s\n",
205 (PCHAR)FailedAssertion,
206 (PCHAR)FileName,
207 LineNumber,
208 Message);
209 }
210 else
211 {
212 DbgPrint("Assertion \'%s\' failed at %s line %d\n",
213 (PCHAR)FailedAssertion,
214 (PCHAR)FileName,
215 LineNumber);
216 }
217
218 //DbgBreakPoint();
219 }
220
221 // DECLSPEC_NORETURN
222 VOID
223 NTAPI
224 KeBugCheckEx(
225 IN ULONG BugCheckCode,
226 IN ULONG_PTR BugCheckParameter1,
227 IN ULONG_PTR BugCheckParameter2,
228 IN ULONG_PTR BugCheckParameter3,
229 IN ULONG_PTR BugCheckParameter4)
230 {
231 char Buffer[70];
232 printf("*** STOP: 0x%08lX (0x%08lX, 0x%08lX, 0x%08lX, 0x%08lX)",
233 BugCheckCode, BugCheckParameter1, BugCheckParameter2,
234 BugCheckParameter3, BugCheckParameter4);
235 ASSERT(FALSE);
236 }
237
238 unsigned char BitScanForward(ULONG * Index, unsigned long Mask)
239 {
240 *Index = 0;
241 while (Mask && ((Mask & 1) == 0))
242 {
243 Mask >>= 1;
244 ++(*Index);
245 }
246 return Mask ? 1 : 0;
247 }
248
249 unsigned char BitScanReverse(ULONG * const Index, unsigned long Mask)
250 {
251 *Index = 0;
252 while (Mask && ((Mask & (1 << 31)) == 0))
253 {
254 Mask <<= 1;
255 ++(*Index);
256 }
257 return Mask ? 1 : 0;
258 }