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