windowscodecs: Silence fixme for IID_CMetaBitmapRenderTarget.
[wine.git] / dlls / rpcrt4 / rpcrt4_main.c
blob37d82f330dc3255ed6aad4dd9f4d9694f8c2f888
1 /*
2 * RPCRT4
4 * Copyright 2000 Huw D M Davies for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * WINE RPC TODO's (and a few TODONT's)
22 * - Statistics: we are supposed to be keeping various counters. we aren't.
24 * - Async RPC: Unimplemented.
26 * - The NT "ports" API, aka LPC. Greg claims this is on his radar. Might (or
27 * might not) enable users to get some kind of meaningful result out of
28 * NT-based native rpcrt4's. Commonly-used transport for self-to-self RPC's.
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "windef.h"
39 #include "winerror.h"
40 #include "winbase.h"
41 #include "winuser.h"
42 #include "winnt.h"
43 #include "winternl.h"
44 #include "ntsecapi.h"
45 #include "iptypes.h"
46 #include "iphlpapi.h"
47 #include "rpc.h"
49 #include "ole2.h"
50 #include "rpcndr.h"
51 #include "rpcproxy.h"
53 #include "rpc_binding.h"
54 #include "rpc_server.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
60 static UUID uuid_nil;
62 static CRITICAL_SECTION uuid_cs;
63 static CRITICAL_SECTION_DEBUG critsect_debug =
65 0, 0, &uuid_cs,
66 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
67 0, 0, { (DWORD_PTR)(__FILE__ ": uuid_cs") }
69 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
71 static CRITICAL_SECTION threaddata_cs;
72 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
74 0, 0, &threaddata_cs,
75 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
76 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
78 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
80 static struct list threaddata_list = LIST_INIT(threaddata_list);
82 struct context_handle_list
84 struct context_handle_list *next;
85 NDR_SCONTEXT context_handle;
88 struct threaddata
90 struct list entry;
91 CRITICAL_SECTION cs;
92 DWORD thread_id;
93 RpcConnection *connection;
94 RpcBinding *server_binding;
95 struct context_handle_list *context_handle_list;
98 struct interface_header
100 unsigned int length;
101 RPC_SYNTAX_IDENTIFIER id;
104 /***********************************************************************
105 * DllMain
107 * PARAMS
108 * hinstDLL [I] handle to the DLL's instance
109 * fdwReason [I]
110 * lpvReserved [I] reserved, must be NULL
112 * RETURNS
113 * Success: TRUE
114 * Failure: FALSE
117 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
119 struct threaddata *tdata;
121 switch (fdwReason) {
122 case DLL_PROCESS_ATTACH:
123 break;
125 case DLL_THREAD_DETACH:
126 tdata = NtCurrentTeb()->ReservedForNtRpc;
127 if (tdata)
129 EnterCriticalSection(&threaddata_cs);
130 list_remove(&tdata->entry);
131 LeaveCriticalSection(&threaddata_cs);
133 tdata->cs.DebugInfo->Spare[0] = 0;
134 DeleteCriticalSection(&tdata->cs);
135 if (tdata->connection)
136 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
137 if (tdata->server_binding)
138 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
139 free(tdata);
141 break;
143 case DLL_PROCESS_DETACH:
144 if (lpvReserved) break; /* do nothing if process is shutting down */
145 RPCRT4_destroy_all_protseqs();
146 RPCRT4_ServerFreeAllRegisteredAuthInfo();
147 DeleteCriticalSection(&uuid_cs);
148 DeleteCriticalSection(&threaddata_cs);
149 break;
152 return TRUE;
155 /*************************************************************************
156 * RpcStringFreeA [RPCRT4.@]
158 * Frees a character string allocated by the RPC run-time library.
160 * RETURNS
162 * S_OK if successful.
164 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
166 free(*String);
168 return RPC_S_OK;
171 /*************************************************************************
172 * RpcStringFreeW [RPCRT4.@]
174 * Frees a character string allocated by the RPC run-time library.
176 * RETURNS
178 * S_OK if successful.
180 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
182 free(*String);
184 return RPC_S_OK;
187 /*************************************************************************
188 * RpcIfInqId [RPCRT4.@]
190 * Get interface UUID and version.
192 RPC_STATUS WINAPI RpcIfInqId(RPC_IF_HANDLE if_handle, RPC_IF_ID *if_id)
194 struct interface_header *header = (struct interface_header *)if_handle;
196 TRACE("(%p,%p)\n", if_handle, if_id);
198 if_id->Uuid = header->id.SyntaxGUID;
199 if_id->VersMajor = header->id.SyntaxVersion.MajorVersion;
200 if_id->VersMinor = header->id.SyntaxVersion.MinorVersion;
201 TRACE("UUID:%s VersMajor:%hu VersMinor:%hu.\n", debugstr_guid(&if_id->Uuid), if_id->VersMajor,
202 if_id->VersMinor);
203 return RPC_S_OK;
206 /*************************************************************************
207 * RpcRaiseException [RPCRT4.@]
209 * Raises an exception.
211 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
213 /* shouldn't return */
214 RaiseException(exception, 0, 0, NULL);
215 ERR("handler continued execution\n");
216 ExitProcess(1);
219 /*************************************************************************
220 * UuidCompare [RPCRT4.@]
222 * PARAMS
223 * UUID *Uuid1 [I] Uuid to compare
224 * UUID *Uuid2 [I] Uuid to compare
225 * RPC_STATUS *Status [O] returns RPC_S_OK
227 * RETURNS
228 * -1 if Uuid1 is less than Uuid2
229 * 0 if Uuid1 and Uuid2 are equal
230 * 1 if Uuid1 is greater than Uuid2
232 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
234 int i;
236 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
238 *Status = RPC_S_OK;
240 if (!Uuid1) Uuid1 = &uuid_nil;
241 if (!Uuid2) Uuid2 = &uuid_nil;
243 if (Uuid1 == Uuid2) return 0;
245 if (Uuid1->Data1 != Uuid2->Data1)
246 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
248 if (Uuid1->Data2 != Uuid2->Data2)
249 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
251 if (Uuid1->Data3 != Uuid2->Data3)
252 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
254 for (i = 0; i < 8; i++) {
255 if (Uuid1->Data4[i] < Uuid2->Data4[i])
256 return -1;
257 if (Uuid1->Data4[i] > Uuid2->Data4[i])
258 return 1;
261 return 0;
264 /*************************************************************************
265 * UuidEqual [RPCRT4.@]
267 * PARAMS
268 * UUID *Uuid1 [I] Uuid to compare
269 * UUID *Uuid2 [I] Uuid to compare
270 * RPC_STATUS *Status [O] returns RPC_S_OK
272 * RETURNS
273 * TRUE/FALSE
275 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
277 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
278 return !UuidCompare(Uuid1, Uuid2, Status);
281 /*************************************************************************
282 * UuidIsNil [RPCRT4.@]
284 * PARAMS
285 * UUID *Uuid [I] Uuid to compare
286 * RPC_STATUS *Status [O] returns RPC_S_OK
288 * RETURNS
289 * TRUE/FALSE
291 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
293 TRACE("(%s)\n", debugstr_guid(Uuid));
294 if (!Uuid) return TRUE;
295 return !UuidCompare(Uuid, &uuid_nil, Status);
298 /*************************************************************************
299 * UuidCreateNil [RPCRT4.@]
301 * PARAMS
302 * UUID *Uuid [O] returns a nil UUID
304 * RETURNS
305 * RPC_S_OK
307 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
309 *Uuid = uuid_nil;
310 return RPC_S_OK;
313 /*************************************************************************
314 * UuidCreate [RPCRT4.@]
316 * Creates a 128bit UUID.
318 * RETURNS
320 * RPC_S_OK if successful.
321 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
323 * NOTES
325 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
326 * Truly Random or Pseudo-Random Numbers)
328 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
330 RtlGenRandom(Uuid, sizeof(*Uuid));
331 /* Clear the version bits and set the version (4) */
332 Uuid->Data3 &= 0x0fff;
333 Uuid->Data3 |= (4 << 12);
334 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
335 * specified in RFC 4122, section 4.4.
337 Uuid->Data4[0] &= 0x3f;
338 Uuid->Data4[0] |= 0x80;
340 TRACE("%s\n", debugstr_guid(Uuid));
342 return RPC_S_OK;
345 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
346 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
347 #define TICKS_PER_CLOCK_TICK 1000
348 #define SECSPERDAY 86400
349 #define TICKSPERSEC 10000000
350 /* UUID system time starts at October 15, 1582 */
351 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
352 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
354 static void RPC_UuidGetSystemTime(ULONGLONG *time)
356 FILETIME ft;
358 GetSystemTimeAsFileTime(&ft);
360 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
361 *time += TICKS_15_OCT_1582_TO_1601;
364 /* Assume that a hardware address is at least 6 bytes long */
365 #define ADDRESS_BYTES_NEEDED 6
367 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
369 int i;
370 DWORD status = RPC_S_OK;
372 ULONG buflen = sizeof(IP_ADAPTER_INFO);
373 PIP_ADAPTER_INFO adapter = malloc(buflen);
375 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
376 free(adapter);
377 adapter = malloc(buflen);
380 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
381 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
382 address[i] = adapter->Address[i];
385 /* We can't get a hardware address, just use random numbers.
386 Set the multicast bit to prevent conflicts with real cards. */
387 else {
388 RtlGenRandom(address, ADDRESS_BYTES_NEEDED);
389 address[0] |= 0x01;
390 status = RPC_S_UUID_LOCAL_ONLY;
393 free(adapter);
394 return status;
397 /*************************************************************************
398 * UuidCreateSequential [RPCRT4.@]
400 * Creates a 128bit UUID.
402 * RETURNS
404 * RPC_S_OK if successful.
405 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
407 * FIXME: No compensation for changes across reloading
408 * this dll or across reboots (e.g. clock going
409 * backwards and swapped network cards). The RFC
410 * suggests using NVRAM for storing persistent
411 * values.
413 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
415 static BOOL initialised;
416 static int count;
418 ULONGLONG time;
419 static ULONGLONG timelast;
420 static WORD sequence;
422 static DWORD status;
423 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
425 EnterCriticalSection(&uuid_cs);
427 if (!initialised) {
428 RPC_UuidGetSystemTime(&timelast);
429 count = TICKS_PER_CLOCK_TICK;
431 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
432 sequence &= 0x1fff;
434 status = RPC_UuidGetNodeAddress(address);
435 initialised = TRUE;
438 /* Generate time element of the UUID. Account for going faster
439 than our clock as well as the clock going backwards. */
440 while (1) {
441 RPC_UuidGetSystemTime(&time);
442 if (time > timelast) {
443 count = 0;
444 break;
446 if (time < timelast) {
447 sequence = (sequence + 1) & 0x1fff;
448 count = 0;
449 break;
451 if (count < TICKS_PER_CLOCK_TICK) {
452 count++;
453 break;
457 timelast = time;
458 time += count;
460 /* Pack the information into the UUID structure. */
462 Uuid->Data1 = (ULONG)(time & 0xffffffff);
463 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
464 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
466 /* This is a version 1 UUID */
467 Uuid->Data3 |= (1 << 12);
469 Uuid->Data4[0] = sequence & 0xff;
470 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
471 Uuid->Data4[1] |= 0x80;
472 memcpy(&Uuid->Data4[2], address, ADDRESS_BYTES_NEEDED);
474 LeaveCriticalSection(&uuid_cs);
476 TRACE("%s\n", debugstr_guid(Uuid));
478 return status;
481 /*************************************************************************
482 * I_UuidCreate [RPCRT4.@]
484 * See UuidCreateSequential()
486 RPC_STATUS WINAPI I_UuidCreate(UUID *Uuid)
488 return UuidCreateSequential(Uuid);
491 /*************************************************************************
492 * UuidHash [RPCRT4.@]
494 * Generates a hash value for a given UUID
496 * Code based on FreeDCE implementation
499 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
501 BYTE *data = (BYTE*)uuid;
502 short c0 = 0, c1 = 0, x, y;
503 unsigned int i;
505 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
507 TRACE("(%s)\n", debugstr_guid(uuid));
509 for (i=0; i<sizeof(UUID); i++) {
510 c0 += data[i];
511 c1 += c0;
514 x = -c1 % 255;
515 if (x < 0) x += 255;
517 y = (c1 - c0) % 255;
518 if (y < 0) y += 255;
520 *Status = RPC_S_OK;
521 return y*256 + x;
524 /*************************************************************************
525 * UuidToStringA [RPCRT4.@]
527 * Converts a UUID to a string.
529 * UUID format is 8 hex digits, followed by a hyphen then three groups of
530 * 4 hex digits each followed by a hyphen and then 12 hex digits
532 * RETURNS
534 * S_OK if successful.
535 * S_OUT_OF_MEMORY if unsuccessful.
537 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
539 *StringUuid = malloc(37);
541 if(!(*StringUuid))
542 return RPC_S_OUT_OF_MEMORY;
544 if (!Uuid) Uuid = &uuid_nil;
546 sprintf( (char*)*StringUuid, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
547 Uuid->Data1, Uuid->Data2, Uuid->Data3,
548 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
549 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
550 Uuid->Data4[6], Uuid->Data4[7] );
552 return RPC_S_OK;
555 /*************************************************************************
556 * UuidToStringW [RPCRT4.@]
558 * Converts a UUID to a string.
560 * S_OK if successful.
561 * S_OUT_OF_MEMORY if unsuccessful.
563 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
565 char buf[37];
567 if (!Uuid) Uuid = &uuid_nil;
569 sprintf(buf, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
570 Uuid->Data1, Uuid->Data2, Uuid->Data3,
571 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
572 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
573 Uuid->Data4[6], Uuid->Data4[7] );
575 *StringUuid = RPCRT4_strdupAtoW(buf);
577 if(!(*StringUuid))
578 return RPC_S_OUT_OF_MEMORY;
580 return RPC_S_OK;
583 static const BYTE hex2bin[] =
585 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
586 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
587 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
588 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
589 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
590 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
591 0,10,11,12,13,14,15 /* 0x60 */
594 /***********************************************************************
595 * UuidFromStringA (RPCRT4.@)
597 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
599 int i;
601 if (!s) return UuidCreateNil( uuid );
603 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
605 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
606 return RPC_S_INVALID_STRING_UUID;
608 for (i=0; i<36; i++)
610 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
611 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
614 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
616 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
617 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
618 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
619 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
621 /* these are just sequential bytes */
622 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
623 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
624 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
625 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
626 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
627 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
628 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
629 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
630 return RPC_S_OK;
634 /***********************************************************************
635 * UuidFromStringW (RPCRT4.@)
637 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
639 int i;
641 if (!s) return UuidCreateNil( uuid );
643 if (lstrlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
645 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
646 return RPC_S_INVALID_STRING_UUID;
648 for (i=0; i<36; i++)
650 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
651 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
654 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
656 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
657 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
658 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
659 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
661 /* these are just sequential bytes */
662 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
663 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
664 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
665 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
666 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
667 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
668 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
669 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
670 return RPC_S_OK;
673 #define MAX_RPC_ERROR_TEXT 256
675 /******************************************************************************
676 * DceErrorInqTextW (rpcrt4.@)
678 * Notes
679 * 1. On passing a NULL pointer the code does bomb out.
680 * 2. The size of the required buffer is not defined in the documentation.
681 * It appears to be 256.
682 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
683 * of any value for which it does.
684 * 4. The MSDN documentation currently declares that the second argument is
685 * unsigned char *, even for the W version. I don't believe it.
687 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
689 DWORD count;
690 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
691 FORMAT_MESSAGE_IGNORE_INSERTS,
692 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
693 if (!count)
695 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
696 FORMAT_MESSAGE_IGNORE_INSERTS,
697 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
698 if (!count)
700 ERR ("Failed to translate error\n");
701 return RPC_S_INVALID_ARG;
704 return RPC_S_OK;
707 /******************************************************************************
708 * DceErrorInqTextA (rpcrt4.@)
710 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
712 RPC_STATUS status;
713 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
714 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
716 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
717 NULL, NULL))
719 ERR ("Failed to translate error\n");
720 status = RPC_S_INVALID_ARG;
723 return status;
726 /******************************************************************************
727 * I_RpcAllocate (rpcrt4.@)
729 void * WINAPI I_RpcAllocate(unsigned int Size)
731 return malloc(Size);
734 /******************************************************************************
735 * I_RpcFree (rpcrt4.@)
737 void WINAPI I_RpcFree(void *Object)
739 free(Object);
742 /******************************************************************************
743 * I_RpcMapWin32Status (rpcrt4.@)
745 * Maps Win32 RPC error codes to NT statuses.
747 * PARAMS
748 * status [I] Win32 RPC error code.
750 * RETURNS
751 * Appropriate translation into an NT status code.
753 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
755 TRACE("(%ld)\n", status);
756 switch (status)
758 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
759 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
760 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
761 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
762 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
763 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
764 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
765 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
766 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
767 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
768 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
769 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
770 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
771 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
772 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
773 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
774 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
775 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
776 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
777 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
778 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
779 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
780 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
781 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
782 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
783 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
784 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
785 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
786 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
787 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
788 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
789 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
790 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
791 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
792 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
793 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
794 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
795 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
796 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
797 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
798 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
799 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
800 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
801 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
802 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
803 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
804 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
805 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
806 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
807 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
808 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
809 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
810 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
811 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
812 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
813 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
814 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
815 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
816 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
817 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
818 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
819 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
820 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
821 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
822 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
823 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
824 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
825 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
826 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
827 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
828 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
829 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
830 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
831 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
832 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
833 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
834 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
835 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
836 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
837 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
838 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
839 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
840 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
841 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
842 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
843 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
844 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
845 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
846 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
847 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
848 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
849 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
850 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
851 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
852 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
853 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
854 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
855 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
856 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
857 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
858 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
859 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
860 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
861 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
862 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
863 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
864 default: return status;
868 /******************************************************************************
869 * RpcExceptionFilter (rpcrt4.@)
870 * I_RpcExceptionFilter (rpcrt4.@)
872 int WINAPI RpcExceptionFilter(ULONG ExceptionCode)
874 TRACE("0x%lx\n", ExceptionCode);
875 switch (ExceptionCode)
877 case STATUS_DATATYPE_MISALIGNMENT:
878 case STATUS_BREAKPOINT:
879 case STATUS_ACCESS_VIOLATION:
880 case STATUS_ILLEGAL_INSTRUCTION:
881 case STATUS_PRIVILEGED_INSTRUCTION:
882 case STATUS_INSTRUCTION_MISALIGNMENT:
883 case STATUS_STACK_OVERFLOW:
884 case STATUS_POSSIBLE_DEADLOCK:
885 return EXCEPTION_CONTINUE_SEARCH;
886 default:
887 return EXCEPTION_EXECUTE_HANDLER;
891 /******************************************************************************
892 * RpcErrorStartEnumeration (rpcrt4.@)
894 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
896 FIXME("(%p): stub\n", EnumHandle);
897 return RPC_S_ENTRY_NOT_FOUND;
900 /******************************************************************************
901 * RpcErrorEndEnumeration (rpcrt4.@)
903 RPC_STATUS RPC_ENTRY RpcErrorEndEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
905 FIXME("(%p): stub\n", EnumHandle);
906 return RPC_S_OK;
909 /******************************************************************************
910 * RpcErrorSaveErrorInfo (rpcrt4.@)
912 RPC_STATUS RPC_ENTRY RpcErrorSaveErrorInfo(RPC_ERROR_ENUM_HANDLE *EnumHandle, void **ErrorBlob, SIZE_T *BlobSize)
914 FIXME("(%p %p %p): stub\n", EnumHandle, ErrorBlob, BlobSize);
915 return ERROR_CALL_NOT_IMPLEMENTED;
918 /******************************************************************************
919 * RpcErrorLoadErrorInfo (rpcrt4.@)
921 RPC_STATUS RPC_ENTRY RpcErrorLoadErrorInfo(void *ErrorBlob, SIZE_T BlobSize, RPC_ERROR_ENUM_HANDLE *EnumHandle)
923 FIXME("(%p %Iu %p): stub\n", ErrorBlob, BlobSize, EnumHandle);
924 return ERROR_CALL_NOT_IMPLEMENTED;
927 /******************************************************************************
928 * RpcErrorGetNextRecord (rpcrt4.@)
930 RPC_STATUS RPC_ENTRY RpcErrorGetNextRecord(RPC_ERROR_ENUM_HANDLE *EnumHandle, BOOL CopyStrings, RPC_EXTENDED_ERROR_INFO *ErrorInfo)
932 FIXME("(%p %x %p): stub\n", EnumHandle, CopyStrings, ErrorInfo);
933 return RPC_S_ENTRY_NOT_FOUND;
936 /******************************************************************************
937 * RpcMgmtSetCancelTimeout (rpcrt4.@)
939 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
941 FIXME("(%ld): stub\n", Timeout);
942 return RPC_S_OK;
945 static struct threaddata *get_or_create_threaddata(void)
947 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
948 if (!tdata)
950 tdata = calloc(1, sizeof(*tdata));
951 if (!tdata) return NULL;
953 InitializeCriticalSectionEx(&tdata->cs, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO);
954 tdata->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": threaddata.cs");
955 tdata->thread_id = GetCurrentThreadId();
957 EnterCriticalSection(&threaddata_cs);
958 list_add_tail(&threaddata_list, &tdata->entry);
959 LeaveCriticalSection(&threaddata_cs);
961 NtCurrentTeb()->ReservedForNtRpc = tdata;
962 return tdata;
964 return tdata;
967 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
969 struct threaddata *tdata = get_or_create_threaddata();
970 if (!tdata) return;
972 EnterCriticalSection(&tdata->cs);
973 tdata->connection = Connection;
974 LeaveCriticalSection(&tdata->cs);
977 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
979 struct threaddata *tdata = get_or_create_threaddata();
980 if (!tdata) return;
982 tdata->server_binding = Binding;
985 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
987 struct threaddata *tdata = get_or_create_threaddata();
988 if (!tdata) return NULL;
990 return tdata->server_binding;
993 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
995 struct threaddata *tdata = get_or_create_threaddata();
996 struct context_handle_list *context_handle_list;
998 if (!tdata) return;
1000 context_handle_list = malloc(sizeof(*context_handle_list));
1001 if (!context_handle_list) return;
1003 context_handle_list->context_handle = SContext;
1004 context_handle_list->next = tdata->context_handle_list;
1005 tdata->context_handle_list = context_handle_list;
1008 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
1010 struct threaddata *tdata = get_or_create_threaddata();
1011 struct context_handle_list *current, *prev;
1013 if (!tdata) return;
1015 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
1017 if (current->context_handle == SContext)
1019 if (prev)
1020 prev->next = current->next;
1021 else
1022 tdata->context_handle_list = current->next;
1023 free(current);
1024 return;
1029 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
1031 struct threaddata *tdata = get_or_create_threaddata();
1032 struct context_handle_list *context_handle_list;
1033 NDR_SCONTEXT context_handle;
1035 if (!tdata) return NULL;
1037 context_handle_list = tdata->context_handle_list;
1038 if (!context_handle_list) return NULL;
1039 tdata->context_handle_list = context_handle_list->next;
1041 context_handle = context_handle_list->context_handle;
1042 free(context_handle_list);
1043 return context_handle;
1046 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
1048 struct threaddata *tdata;
1050 EnterCriticalSection(&threaddata_cs);
1051 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
1052 if (tdata->thread_id == target_tid)
1054 EnterCriticalSection(&tdata->cs);
1055 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
1056 LeaveCriticalSection(&tdata->cs);
1057 break;
1059 LeaveCriticalSection(&threaddata_cs);
1061 return RPC_S_OK;
1064 /******************************************************************************
1065 * RpcCancelThread (rpcrt4.@)
1067 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
1069 TRACE("(%p)\n", ThreadHandle);
1070 return RpcCancelThreadEx(ThreadHandle, 0);
1073 /******************************************************************************
1074 * RpcCancelThreadEx (rpcrt4.@)
1076 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
1078 DWORD target_tid;
1080 FIXME("(%p, %ld)\n", ThreadHandle, Timeout);
1082 target_tid = GetThreadId(ThreadHandle);
1083 if (!target_tid)
1084 return RPC_S_INVALID_ARG;
1086 if (Timeout)
1088 FIXME("(%p, %ld)\n", ThreadHandle, Timeout);
1089 return RPC_S_OK;
1091 else
1092 return rpc_cancel_thread(target_tid);