Release 970215
[wine/multimedia.git] / misc / ntdll.c
blobf9d89856d58c6219ed0120b0d2dcbde3ac9a63b8
1 /*
2 * NT basis DLL
3 *
4 * Copyright 1996 Marcus Meissner
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 #include <ctype.h>
12 #include <math.h>
13 #include "win.h"
14 #include "windows.h"
15 #include "ntdll.h"
16 #include "heap.h"
17 #include "stddebug.h"
18 #include "debug.h"
19 #include "module.h"
20 #include "heap.h"
22 /**************************************************************************
23 * RtlLengthRequiredSid [NTDLL]
25 DWORD
26 RtlLengthRequiredSid(DWORD nrofsubauths) {
27 return sizeof(DWORD)*nrofsubauths+sizeof(SID);
30 /**************************************************************************
31 * RtlLengthSid [NTDLL]
33 DWORD
34 RtlLengthSid(LPSID sid) {
35 return sizeof(DWORD)*sid->SubAuthorityCount+sizeof(SID);
38 /**************************************************************************
39 * RtlCreateAcl [NTDLL]
41 DWORD /* NTSTATUS */
42 RtlCreateAcl(LPACL acl,DWORD size,DWORD rev) {
43 if (rev!=ACL_REVISION)
44 return STATUS_INVALID_PARAMETER;
45 if (size<sizeof(ACL))
46 return STATUS_BUFFER_TOO_SMALL;
47 if (size>0xFFFF)
48 return STATUS_INVALID_PARAMETER;
50 memset(acl,'\0',sizeof(ACL));
51 acl->AclRevision = rev;
52 acl->AclSize = size;
53 acl->AceCount = 0;
54 return 0;
57 /**************************************************************************
58 * RtlFirstFreeAce [NTDLL]
59 * looks for the AceCount+1 ACE, and if it is still within the alloced
60 * ACL, return a pointer to it
62 BOOL32
63 RtlFirstFreeAce(LPACL acl,LPACE_HEADER *x) {
64 LPACE_HEADER ace;
65 int i;
67 *x = 0;
68 ace = (LPACE_HEADER)(acl+1);
69 for (i=0;i<acl->AceCount;i++) {
70 if ((DWORD)ace>=(((DWORD)acl)+acl->AclSize))
71 return 0;
72 ace = (LPACE_HEADER)(((BYTE*)ace)+ace->AceSize);
74 if ((DWORD)ace>=(((DWORD)acl)+acl->AclSize))
75 return 0;
76 *x = ace;
77 return 1;
80 /**************************************************************************
81 * RtlAddAce [NTDLL]
83 DWORD /* NTSTATUS */
84 RtlAddAce(LPACL acl,DWORD rev,DWORD xnrofaces,LPACE_HEADER acestart,DWORD acelen){
85 LPACE_HEADER ace,targetace;
86 int nrofaces;
88 if (acl->AclRevision != ACL_REVISION)
89 return STATUS_INVALID_PARAMETER;
90 if (!RtlFirstFreeAce(acl,&targetace))
91 return STATUS_INVALID_PARAMETER;
92 nrofaces=0;ace=acestart;
93 while (((DWORD)ace-(DWORD)acestart)<acelen) {
94 nrofaces++;
95 ace = (LPACE_HEADER)(((BYTE*)ace)+ace->AceSize);
97 if ((DWORD)targetace+acelen>(DWORD)acl+acl->AclSize) /* too much aces */
98 return STATUS_INVALID_PARAMETER;
99 memcpy((LPBYTE)targetace,acestart,acelen);
100 acl->AceCount+=nrofaces;
101 return 0;
104 /**************************************************************************
105 * RtlCreateSecurityDescriptor [NTDLL]
107 DWORD /* NTSTATUS */
108 RtlCreateSecurityDescriptor(LPSECURITY_DESCRIPTOR lpsd,DWORD rev) {
109 if (rev!=SECURITY_DESCRIPTOR_REVISION)
110 return STATUS_UNKNOWN_REVISION;
111 memset(lpsd,'\0',sizeof(*lpsd));
112 lpsd->Revision = SECURITY_DESCRIPTOR_REVISION;
113 return 0;
116 /**************************************************************************
117 * RtlSetDaclSecurityDescriptor [NTDLL]
119 DWORD /* NTSTATUS */
120 RtlSetDaclSecurityDescriptor (
121 LPSECURITY_DESCRIPTOR lpsd,BOOL32 daclpresent,LPACL dacl,BOOL32 dacldefaulted
123 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
124 return STATUS_UNKNOWN_REVISION;
125 if (lpsd->Control & SE_SELF_RELATIVE)
126 return STATUS_INVALID_SECURITY_DESCR;
127 if (!daclpresent) {
128 lpsd->Control &= ~SE_DACL_PRESENT;
129 return 0;
131 lpsd->Control |= SE_DACL_PRESENT;
132 lpsd->Dacl = dacl;
133 if (dacldefaulted)
134 lpsd->Control |= SE_DACL_DEFAULTED;
135 else
136 lpsd->Control &= ~SE_DACL_DEFAULTED;
137 return 0;
140 /**************************************************************************
141 * RtlSetSaclSecurityDescriptor [NTDLL]
143 DWORD /* NTSTATUS */
144 RtlSetSaclSecurityDescriptor (
145 LPSECURITY_DESCRIPTOR lpsd,BOOL32 saclpresent,LPACL sacl,BOOL32 sacldefaulted
147 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
148 return STATUS_UNKNOWN_REVISION;
149 if (lpsd->Control & SE_SELF_RELATIVE)
150 return STATUS_INVALID_SECURITY_DESCR;
151 if (!saclpresent) {
152 lpsd->Control &= ~SE_SACL_PRESENT;
153 return 0;
155 lpsd->Control |= SE_SACL_PRESENT;
156 lpsd->Sacl = sacl;
157 if (sacldefaulted)
158 lpsd->Control |= SE_SACL_DEFAULTED;
159 else
160 lpsd->Control &= ~SE_SACL_DEFAULTED;
161 return 0;
164 /**************************************************************************
165 * RtlSetOwnerSecurityDescriptor [NTDLL]
167 DWORD /* NTSTATUS */
168 RtlSetOwnerSecurityDescriptor (LPSECURITY_DESCRIPTOR lpsd,LPSID owner,BOOL32 ownerdefaulted) {
169 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
170 return STATUS_UNKNOWN_REVISION;
171 if (lpsd->Control & SE_SELF_RELATIVE)
172 return STATUS_INVALID_SECURITY_DESCR;
174 lpsd->Owner = owner;
175 if (ownerdefaulted)
176 lpsd->Control |= SE_OWNER_DEFAULTED;
177 else
178 lpsd->Control &= ~SE_OWNER_DEFAULTED;
179 return 0;
182 /**************************************************************************
183 * RtlSetOwnerSecurityDescriptor [NTDLL]
185 DWORD /* NTSTATUS */
186 RtlSetGroupSecurityDescriptor (LPSECURITY_DESCRIPTOR lpsd,LPSID group,BOOL32 groupdefaulted) {
187 if (lpsd->Revision!=SECURITY_DESCRIPTOR_REVISION)
188 return STATUS_UNKNOWN_REVISION;
189 if (lpsd->Control & SE_SELF_RELATIVE)
190 return STATUS_INVALID_SECURITY_DESCR;
192 lpsd->Group = group;
193 if (groupdefaulted)
194 lpsd->Control |= SE_GROUP_DEFAULTED;
195 else
196 lpsd->Control &= ~SE_GROUP_DEFAULTED;
197 return 0;
201 /**************************************************************************
202 * RtlNormalizeProcessParams [NTDLL]
204 LPVOID
205 RtlNormalizeProcessParams(LPVOID x)
207 fprintf(stdnimp,"RtlNormalizeProcessParams(%p), stub.\n",x);
208 return x;
211 /**************************************************************************
212 * RtlInitializeSid [NTDLL]
214 DWORD
215 RtlInitializeSid(LPSID lpsid,LPSID_IDENTIFIER_AUTHORITY lpsidauth,DWORD c) {
216 BYTE a = c&0xff;
218 if (a>=SID_MAX_SUB_AUTHORITIES)
219 return a;
220 lpsid->SubAuthorityCount = a;
221 lpsid->Revision = SID_REVISION;
222 memcpy(&(lpsid->IdentifierAuthority),lpsidauth,sizeof(SID_IDENTIFIER_AUTHORITY));
223 return 0;
226 /**************************************************************************
227 * RtlSubAuthoritySid [NTDLL]
229 LPDWORD
230 RtlSubAuthoritySid(LPSID lpsid,DWORD nr) {
231 return &(lpsid->SubAuthority[nr]);
234 /**************************************************************************
235 * RtlSubAuthorityCountSid [NTDLL]
237 LPBYTE
238 RtlSubAuthorityCountSid(LPSID lpsid) {
239 return ((LPBYTE)lpsid)+1;
242 /**************************************************************************
243 * RtlCopySid [NTDLL]
245 DWORD
246 RtlCopySid(DWORD len,LPSID to,LPSID from) {
247 if (len<(from->SubAuthorityCount*4+8))
248 return STATUS_BUFFER_TOO_SMALL;
249 memmove(to,from,from->SubAuthorityCount*4+8);
250 return STATUS_SUCCESS;
253 /**************************************************************************
254 * RtlAnsiStringToUnicodeString [NTDLL]
256 DWORD /* NTSTATUS */
257 RtlAnsiStringToUnicodeString(LPUNICODE_STRING uni,LPANSI_STRING ansi,BOOL32 doalloc) {
258 DWORD unilen = (ansi->Length+1)*sizeof(WCHAR);
260 if (unilen>0xFFFF)
261 return STATUS_INVALID_PARAMETER_2;
262 uni->Length = unilen;
263 if (doalloc) {
264 uni->MaximumLength = unilen;
265 uni->Buffer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,unilen);
266 if (!uni->Buffer)
267 return STATUS_NO_MEMORY;
269 if (unilen>uni->MaximumLength)
270 return STATUS_BUFFER_OVERFLOW;
271 lstrcpynAtoW(uni->Buffer,ansi->Buffer,unilen/2);
272 return STATUS_SUCCESS;
275 /**************************************************************************
276 * RtlOemStringToUnicodeString [NTDLL]
278 DWORD /* NTSTATUS */
279 RtlOemStringToUnicodeString(LPUNICODE_STRING uni,LPSTRING ansi,BOOL32 doalloc) {
280 DWORD unilen = (ansi->Length+1)*sizeof(WCHAR);
282 if (unilen>0xFFFF)
283 return STATUS_INVALID_PARAMETER_2;
284 uni->Length = unilen;
285 if (doalloc) {
286 uni->MaximumLength = unilen;
287 uni->Buffer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,unilen);
288 if (!uni->Buffer)
289 return STATUS_NO_MEMORY;
291 if (unilen>uni->MaximumLength)
292 return STATUS_BUFFER_OVERFLOW;
293 lstrcpynAtoW(uni->Buffer,ansi->Buffer,unilen/2);
294 return STATUS_SUCCESS;
296 /**************************************************************************
297 * RtlMultiByteToUnicodeN [NTDLL]
298 * FIXME: multibyte support
300 DWORD /* NTSTATUS */
301 RtlMultiByteToUnicodeN(LPWSTR unistr,DWORD unilen,LPDWORD reslen,LPSTR oemstr,DWORD oemlen) {
302 DWORD len;
303 LPWSTR x;
305 len = oemlen;
306 if (unilen/2 < len)
307 len = unilen/2;
308 x=(LPWSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
309 lstrcpynAtoW(x,oemstr,len+1);
310 memcpy(unistr,x,len*2);
311 if (reslen) *reslen = len*2;
312 return 0;
315 /**************************************************************************
316 * RtlOemToUnicodeN [NTDLL]
318 DWORD /* NTSTATUS */
319 RtlOemToUnicodeN(LPWSTR unistr,DWORD unilen,LPDWORD reslen,LPSTR oemstr,DWORD oemlen) {
320 DWORD len;
321 LPWSTR x;
323 len = oemlen;
324 if (unilen/2 < len)
325 len = unilen/2;
326 x=(LPWSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
327 lstrcpynAtoW(x,oemstr,len+1);
328 memcpy(unistr,x,len*2);
329 if (reslen) *reslen = len*2;
330 return 0;
333 /**************************************************************************
334 * RtlInitString [NTDLL]
336 VOID
337 RtlInitAnsiString(LPANSI_STRING target,LPCSTR source) {
338 target->Length = target->MaximumLength = 0;
339 target->Buffer = (LPSTR)source;
340 if (!source)
341 return;
342 target->Length = lstrlen32A(target->Buffer);
343 target->MaximumLength = target->Length+1;
345 /**************************************************************************
346 * RtlInitString [NTDLL]
348 VOID
349 RtlInitString(LPSTRING target,LPCSTR source) {
350 target->Length = target->MaximumLength = 0;
351 target->Buffer = (LPSTR)source;
352 if (!source)
353 return;
354 target->Length = lstrlen32A(target->Buffer);
355 target->MaximumLength = target->Length+1;
358 /**************************************************************************
359 * RtlInitUnicodeString [NTDLL]
361 VOID
362 RtlInitUnicodeString(LPUNICODE_STRING target,LPCWSTR source) {
363 target->Length = target->MaximumLength = 0;
364 target->Buffer = (LPWSTR)source;
365 if (!source)
366 return;
367 target->Length = lstrlen32W(target->Buffer)*2;
368 target->MaximumLength = target->Length+2;
371 /**************************************************************************
372 * RtlUnicodeToOemN [NTDLL]
374 DWORD /* NTSTATUS */
375 RtlUnicodeToOemN(LPSTR oemstr,DWORD oemlen,LPDWORD reslen,LPWSTR unistr,DWORD unilen) {
376 DWORD len;
377 LPSTR x;
379 len = oemlen;
380 if (unilen/2 < len)
381 len = unilen/2;
382 x=(LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,len+1);
383 lstrcpynWtoA(x,unistr,len+1);
384 memcpy(oemstr,x,len);
385 if (reslen) *reslen = len;
386 return 0;
389 /**************************************************************************
390 * RtlUnicodeStringToOemString [NTDLL]
392 DWORD /* NTSTATUS */
393 RtlUnicodeStringToOemString(LPUNICODE_STRING uni,LPANSI_STRING oem,BOOL32 alloc)
395 if (alloc) {
396 oem->Buffer = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,uni->Length/2)+1;
397 oem->MaximumLength = uni->Length/2+1;
399 oem->Length = uni->Length/2;
400 lstrcpynWtoA(oem->Buffer,uni->Buffer,uni->Length/2+1);
401 return 0;
404 /**************************************************************************
405 * RtlNtStatusToDosErro [NTDLL]
407 DWORD
408 RtlNtStatusToDosError(DWORD error) {
409 /* FIXME: map STATUS_ to ERROR_ */
410 return error;
413 /**************************************************************************
414 * RtlGetNtProductType [NTDLL]
416 DWORD
417 RtlGetNtProductType(LPVOID x) {
418 /* FIXME : find documentation for this one */
419 return 0;
422 /**************************************************************************
423 * RtlUpcaseUnicodeString [NTDLL]
425 DWORD
426 RtlUpcaseUnicodeString(LPUNICODE_STRING dest,LPUNICODE_STRING src,BOOL32 doalloc) {
427 LPWSTR s,t;
428 DWORD i,len;
430 len = src->Length;
431 if (doalloc) {
432 dest->MaximumLength = len;
433 dest->Buffer = (LPWSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,len);
434 if (!dest->Buffer)
435 return STATUS_NO_MEMORY;
438 if (dest->MaximumLength < len)
439 return STATUS_BUFFER_OVERFLOW;
440 s=dest->Buffer;t=src->Buffer;
441 for (i=0;i<len;i++) {
442 s[i]=toupper(t[i]);
443 s++;
444 t++;
446 return STATUS_SUCCESS;
449 /**************************************************************************
450 * RtlxOemStringToUnicodeSize [NTDLL]
452 UINT32
453 RtlxOemStringToUnicodeSize(LPSTRING str) {
454 return str->Length*2+2;
457 /**************************************************************************
458 * RtlxAnsiStringToUnicodeSize [NTDLL]
460 UINT32
461 RtlxAnsiStringToUnicodeSize(LPANSI_STRING str) {
462 return str->Length*2+2;
465 /**************************************************************************
466 * RtlDosPathNameToNtPathName_U [NTDLL]
468 * FIXME: convert to UNC or whatever is expected here
470 BOOL32
471 RtlDosPathNameToNtPathName_U(
472 LPWSTR from,LPUNICODE_STRING us,DWORD x2,DWORD x3
474 LPSTR fromA = HEAP_strdupWtoA(GetProcessHeap(),0,from);
476 fprintf(stderr,"RtlDosPathNameToNtPathName_U(%s,%p,%08lx,%08lx)\n",
477 fromA,us,x2,x3
479 if (us)
480 RtlInitUnicodeString(us,HEAP_strdupW(GetProcessHeap(),0,from));
481 return TRUE;
484 /**************************************************************************
485 * NtOpenFile [NTDLL]
487 DWORD
488 NtOpenFile(DWORD x1,DWORD flags,DWORD x3,DWORD x4,DWORD alignment,DWORD x6) {
489 fprintf(stderr,"NtOpenFile(%08lx,%08lx,%08lx,%08lx,%08lx,%08lx)\n",
490 x1,flags,x3,x4,alignment,x6
492 /* returns file io completion status */
493 return 0;