xref: /reactos/sdk/tools/mkhive/rtl.c (revision 8a978a17)
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 RtlInitUnicodeString(
28     IN OUT PUNICODE_STRING DestinationString,
29     IN PCWSTR SourceString)
30 {
31     SIZE_T DestSize;
32 
33     if(SourceString)
34     {
35         DestSize = strlenW(SourceString) * sizeof(WCHAR);
36         DestinationString->Length = (USHORT)DestSize;
37         DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR);
38     }
39     else
40     {
41         DestinationString->Length = 0;
42         DestinationString->MaximumLength = 0;
43     }
44 
45     DestinationString->Buffer = (PWCHAR)SourceString;
46 }
47 
48 LONG NTAPI
49 RtlCompareUnicodeString(
50     IN PCUNICODE_STRING String1,
51     IN PCUNICODE_STRING String2,
52     IN BOOLEAN CaseInSensitive)
53 {
54     USHORT i;
55     WCHAR c1, c2;
56 
57     for (i = 0; i <= String1->Length / sizeof(WCHAR) && i <= String2->Length / sizeof(WCHAR); i++)
58     {
59         if (CaseInSensitive)
60         {
61             c1 = RtlUpcaseUnicodeChar(String1->Buffer[i]);
62             c2 = RtlUpcaseUnicodeChar(String2->Buffer[i]);
63         }
64         else
65         {
66             c1 = String1->Buffer[i];
67             c2 = String2->Buffer[i];
68         }
69 
70         if (c1 < c2)
71             return -1;
72         else if (c1 > c2)
73             return 1;
74     }
75 
76     return 0;
77 }
78 
79 WCHAR NTAPI
80 RtlUpcaseUnicodeChar(
81     IN WCHAR Source)
82 {
83     USHORT Offset;
84 
85     if (Source < 'a')
86         return Source;
87 
88     if (Source <= 'z')
89         return (Source - ('a' - 'A'));
90 
91     Offset = 0;
92 
93     return Source + (SHORT)Offset;
94 }
95 
96 VOID NTAPI
97 KeQuerySystemTime(
98     OUT PLARGE_INTEGER CurrentTime)
99 {
100     CurrentTime->QuadPart = 0;
101 }
102 
103 PVOID NTAPI
104 ExAllocatePool(
105     IN POOL_TYPE PoolType,
106     IN SIZE_T NumberOfBytes)
107 {
108     return (PVOID) malloc(NumberOfBytes);
109 }
110 
111 VOID NTAPI
112 ExFreePool(
113     IN PVOID p)
114 {
115     free(p);
116 }
117 
118 ULONG
119 __cdecl
120 DbgPrint(
121   IN CHAR *Format,
122   IN ...)
123 {
124     va_list ap;
125     va_start(ap, Format);
126     vprintf(Format, ap);
127     va_end(ap);
128 
129     return 0;
130 }
131 
132 VOID
133 NTAPI
134 RtlAssert(IN PVOID FailedAssertion,
135           IN PVOID FileName,
136           IN ULONG LineNumber,
137           IN PCHAR Message OPTIONAL)
138 {
139     if (Message != NULL)
140     {
141         DbgPrint("Assertion \'%s\' failed at %s line %u: %s\n",
142                  (PCHAR)FailedAssertion,
143                  (PCHAR)FileName,
144                  LineNumber,
145                  Message);
146     }
147     else
148     {
149         DbgPrint("Assertion \'%s\' failed at %s line %u\n",
150                  (PCHAR)FailedAssertion,
151                  (PCHAR)FileName,
152                  LineNumber);
153     }
154 
155     //DbgBreakPoint();
156 }
157 
158 // FIXME: DECLSPEC_NORETURN
159 VOID
160 NTAPI
161 KeBugCheckEx(
162     IN ULONG BugCheckCode,
163     IN ULONG_PTR BugCheckParameter1,
164     IN ULONG_PTR BugCheckParameter2,
165     IN ULONG_PTR BugCheckParameter3,
166     IN ULONG_PTR BugCheckParameter4)
167 {
168     printf("*** STOP: 0x%08X (0x%p,0x%p,0x%p,0x%p)",
169            BugCheckCode,
170            (PVOID)BugCheckParameter1,
171            (PVOID)BugCheckParameter2,
172            (PVOID)BugCheckParameter3,
173            (PVOID)BugCheckParameter4);
174     ASSERT(FALSE);
175 }
176 
177 unsigned char BitScanForward(ULONG * Index, unsigned long Mask)
178 {
179     *Index = 0;
180     while (Mask && ((Mask & 1) == 0))
181     {
182         Mask >>= 1;
183         ++(*Index);
184     }
185     return Mask ? 1 : 0;
186 }
187 
188 unsigned char BitScanReverse(ULONG * const Index, unsigned long Mask)
189 {
190     *Index = 0;
191     while (Mask && ((Mask & (1 << 31)) == 0))
192     {
193         Mask <<= 1;
194         ++(*Index);
195     }
196     return Mask ? 1 : 0;
197 }
198