- Colin Finck: Replace broken memcmp comparison of wide strings by an own version...
[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 #include "mkhive.h"
11 #include <bitmap.c>
12
13 SIZE_T xwcslen( PCWSTR String ) {
14 SIZE_T i;
15
16 for( i = 0; String[i]; i++ );
17
18 return i;
19 }
20
21 PWSTR xwcschr( PWSTR String, WCHAR Char )
22 {
23 SIZE_T i;
24
25 for( i = 0; String[i] && String[i] != Char; i++ );
26
27 if( String[i] ) return &String[i];
28 else return NULL;
29 }
30
31 int xwcsncmp(PCWSTR s1, PCWSTR s2, size_t n)
32 {
33 while(n--)
34 {
35 if(*s1 != *s2)
36 return 1;
37
38 if(*s1 == 0)
39 return 0;
40
41 s1++;
42 s2++;
43 }
44
45 return 0;
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 RtlInitAnsiString(
56 IN OUT PANSI_STRING DestinationString,
57 IN PCSTR SourceString)
58 {
59 SIZE_T DestSize;
60
61 if(SourceString)
62 {
63 DestSize = strlen(SourceString);
64 DestinationString->Length = (USHORT)DestSize;
65 DestinationString->MaximumLength = (USHORT)DestSize + sizeof(CHAR);
66 }
67 else
68 {
69 DestinationString->Length = 0;
70 DestinationString->MaximumLength = 0;
71 }
72
73 DestinationString->Buffer = (PCHAR)SourceString;
74 }
75
76 /*
77 * @implemented
78 *
79 * NOTES
80 * If source is NULL the length of source is assumed to be 0.
81 */
82 VOID NTAPI
83 RtlInitUnicodeString(
84 IN OUT PUNICODE_STRING DestinationString,
85 IN PCWSTR SourceString)
86 {
87 SIZE_T DestSize;
88
89 if(SourceString)
90 {
91 DestSize = xwcslen(SourceString) * sizeof(WCHAR);
92 DestinationString->Length = (USHORT)DestSize;
93 DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR);
94 }
95 else
96 {
97 DestinationString->Length = 0;
98 DestinationString->MaximumLength = 0;
99 }
100
101 DestinationString->Buffer = (PWCHAR)SourceString;
102 }
103
104 NTSTATUS NTAPI
105 RtlAnsiStringToUnicodeString(
106 IN OUT PUNICODE_STRING UniDest,
107 IN PANSI_STRING AnsiSource,
108 IN BOOLEAN AllocateDestinationString)
109 {
110 ULONG Length;
111 PUCHAR WideString;
112 USHORT i;
113
114 Length = AnsiSource->Length * sizeof(WCHAR);
115 if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
116 UniDest->Length = (USHORT)Length;
117
118 if (AllocateDestinationString)
119 {
120 UniDest->MaximumLength = (USHORT)Length + sizeof(WCHAR);
121 UniDest->Buffer = (PWSTR) malloc(UniDest->MaximumLength);
122 if (!UniDest->Buffer)
123 return STATUS_NO_MEMORY;
124 }
125 else if (UniDest->Length >= UniDest->MaximumLength)
126 {
127 return STATUS_BUFFER_OVERFLOW;
128 }
129
130 WideString = (PUCHAR)UniDest->Buffer;
131 for (i = 0; i <= AnsiSource->Length; i++)
132 {
133 WideString[2 * i + 0] = AnsiSource->Buffer[i];
134 WideString[2 * i + 1] = 0;
135 }
136 return STATUS_SUCCESS;
137 }
138
139 WCHAR NTAPI
140 RtlUpcaseUnicodeChar(
141 IN WCHAR Source)
142 {
143 USHORT Offset;
144
145 if (Source < 'a')
146 return Source;
147
148 if (Source <= 'z')
149 return (Source - ('a' - 'A'));
150
151 Offset = 0;
152
153 return Source + (SHORT)Offset;
154 }
155
156 VOID NTAPI
157 KeQuerySystemTime(
158 OUT PLARGE_INTEGER CurrentTime)
159 {
160 CurrentTime->QuadPart = 0;
161 }
162
163 PVOID NTAPI
164 ExAllocatePool(
165 IN POOL_TYPE PoolType,
166 IN SIZE_T NumberOfBytes)
167 {
168 return (PVOID) malloc(NumberOfBytes);
169 }
170
171 VOID NTAPI
172 ExFreePool(
173 IN PVOID p)
174 {
175 free(p);
176 }
177
178 ULONG
179 __cdecl
180 DbgPrint(
181 IN CHAR *Format,
182 IN ...)
183 {
184 va_list ap;
185 va_start(ap, Format);
186 vprintf(Format, ap);
187 va_end(ap);
188
189 return 0;
190 }
191
192 VOID
193 NTAPI
194 RtlAssert(PVOID FailedAssertion,
195 PVOID FileName,
196 ULONG LineNumber,
197 PCHAR Message)
198 {
199 if (NULL != Message)
200 {
201 DbgPrint("Assertion \'%s\' failed at %s line %d: %s\n",
202 (PCHAR)FailedAssertion,
203 (PCHAR)FileName,
204 LineNumber,
205 Message);
206 }
207 else
208 {
209 DbgPrint("Assertion \'%s\' failed at %s line %d\n",
210 (PCHAR)FailedAssertion,
211 (PCHAR)FileName,
212 LineNumber);
213 }
214
215 //DbgBreakPoint();
216 }