d3drm/tests: Properly initialise context.obj in test_Viewport().
[wine.git] / dlls / rpcrt4 / rpcrt4_main.c
blobd5d349dc58f8a45b9328f5a369ee3fe89746168f
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 "config.h"
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "ntstatus.h"
39 #define WIN32_NO_STATUS
40 #include "windef.h"
41 #include "winerror.h"
42 #include "winbase.h"
43 #include "winuser.h"
44 #include "winnt.h"
45 #include "winternl.h"
46 #include "ntsecapi.h"
47 #include "iptypes.h"
48 #include "iphlpapi.h"
49 #include "wine/unicode.h"
50 #include "rpc.h"
52 #include "ole2.h"
53 #include "rpcndr.h"
54 #include "rpcproxy.h"
56 #include "rpc_binding.h"
57 #include "rpc_server.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
63 static UUID uuid_nil;
65 static CRITICAL_SECTION uuid_cs;
66 static CRITICAL_SECTION_DEBUG critsect_debug =
68 0, 0, &uuid_cs,
69 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": uuid_cs") }
72 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
74 static CRITICAL_SECTION threaddata_cs;
75 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
77 0, 0, &threaddata_cs,
78 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
79 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
81 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
83 static struct list threaddata_list = LIST_INIT(threaddata_list);
85 struct context_handle_list
87 struct context_handle_list *next;
88 NDR_SCONTEXT context_handle;
91 struct threaddata
93 struct list entry;
94 CRITICAL_SECTION cs;
95 DWORD thread_id;
96 RpcConnection *connection;
97 RpcBinding *server_binding;
98 struct context_handle_list *context_handle_list;
101 /***********************************************************************
102 * DllMain
104 * PARAMS
105 * hinstDLL [I] handle to the DLL's instance
106 * fdwReason [I]
107 * lpvReserved [I] reserved, must be NULL
109 * RETURNS
110 * Success: TRUE
111 * Failure: FALSE
114 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
116 struct threaddata *tdata;
118 switch (fdwReason) {
119 case DLL_PROCESS_ATTACH:
120 break;
122 case DLL_THREAD_DETACH:
123 tdata = NtCurrentTeb()->ReservedForNtRpc;
124 if (tdata)
126 EnterCriticalSection(&threaddata_cs);
127 list_remove(&tdata->entry);
128 LeaveCriticalSection(&threaddata_cs);
130 tdata->cs.DebugInfo->Spare[0] = 0;
131 DeleteCriticalSection(&tdata->cs);
132 if (tdata->connection)
133 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
134 if (tdata->server_binding)
135 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
136 HeapFree(GetProcessHeap(), 0, tdata);
138 break;
140 case DLL_PROCESS_DETACH:
141 if (lpvReserved) break; /* do nothing if process is shutting down */
142 RPCRT4_destroy_all_protseqs();
143 RPCRT4_ServerFreeAllRegisteredAuthInfo();
144 DeleteCriticalSection(&uuid_cs);
145 DeleteCriticalSection(&threaddata_cs);
146 break;
149 return TRUE;
152 /*************************************************************************
153 * RpcStringFreeA [RPCRT4.@]
155 * Frees a character string allocated by the RPC run-time library.
157 * RETURNS
159 * S_OK if successful.
161 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
163 HeapFree( GetProcessHeap(), 0, *String);
165 return RPC_S_OK;
168 /*************************************************************************
169 * RpcStringFreeW [RPCRT4.@]
171 * Frees a character string allocated by the RPC run-time library.
173 * RETURNS
175 * S_OK if successful.
177 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
179 HeapFree( GetProcessHeap(), 0, *String);
181 return RPC_S_OK;
184 /*************************************************************************
185 * RpcRaiseException [RPCRT4.@]
187 * Raises an exception.
189 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
191 /* shouldn't return */
192 RaiseException(exception, 0, 0, NULL);
193 ERR("handler continued execution\n");
194 ExitProcess(1);
197 /*************************************************************************
198 * UuidCompare [RPCRT4.@]
200 * PARAMS
201 * UUID *Uuid1 [I] Uuid to compare
202 * UUID *Uuid2 [I] Uuid to compare
203 * RPC_STATUS *Status [O] returns RPC_S_OK
205 * RETURNS
206 * -1 if Uuid1 is less than Uuid2
207 * 0 if Uuid1 and Uuid2 are equal
208 * 1 if Uuid1 is greater than Uuid2
210 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
212 int i;
214 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
216 *Status = RPC_S_OK;
218 if (!Uuid1) Uuid1 = &uuid_nil;
219 if (!Uuid2) Uuid2 = &uuid_nil;
221 if (Uuid1 == Uuid2) return 0;
223 if (Uuid1->Data1 != Uuid2->Data1)
224 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
226 if (Uuid1->Data2 != Uuid2->Data2)
227 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
229 if (Uuid1->Data3 != Uuid2->Data3)
230 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
232 for (i = 0; i < 8; i++) {
233 if (Uuid1->Data4[i] < Uuid2->Data4[i])
234 return -1;
235 if (Uuid1->Data4[i] > Uuid2->Data4[i])
236 return 1;
239 return 0;
242 /*************************************************************************
243 * UuidEqual [RPCRT4.@]
245 * PARAMS
246 * UUID *Uuid1 [I] Uuid to compare
247 * UUID *Uuid2 [I] Uuid to compare
248 * RPC_STATUS *Status [O] returns RPC_S_OK
250 * RETURNS
251 * TRUE/FALSE
253 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
255 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
256 return !UuidCompare(Uuid1, Uuid2, Status);
259 /*************************************************************************
260 * UuidIsNil [RPCRT4.@]
262 * PARAMS
263 * UUID *Uuid [I] Uuid to compare
264 * RPC_STATUS *Status [O] returns RPC_S_OK
266 * RETURNS
267 * TRUE/FALSE
269 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
271 TRACE("(%s)\n", debugstr_guid(Uuid));
272 if (!Uuid) return TRUE;
273 return !UuidCompare(Uuid, &uuid_nil, Status);
276 /*************************************************************************
277 * UuidCreateNil [RPCRT4.@]
279 * PARAMS
280 * UUID *Uuid [O] returns a nil UUID
282 * RETURNS
283 * RPC_S_OK
285 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
287 *Uuid = uuid_nil;
288 return RPC_S_OK;
291 /*************************************************************************
292 * UuidCreate [RPCRT4.@]
294 * Creates a 128bit UUID.
296 * RETURNS
298 * RPC_S_OK if successful.
299 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
301 * NOTES
303 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
304 * Truly Random or Pseudo-Random Numbers)
306 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
308 RtlGenRandom(Uuid, sizeof(*Uuid));
309 /* Clear the version bits and set the version (4) */
310 Uuid->Data3 &= 0x0fff;
311 Uuid->Data3 |= (4 << 12);
312 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
313 * specified in RFC 4122, section 4.4.
315 Uuid->Data4[0] &= 0x3f;
316 Uuid->Data4[0] |= 0x80;
318 TRACE("%s\n", debugstr_guid(Uuid));
320 return RPC_S_OK;
323 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
324 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
325 #define TICKS_PER_CLOCK_TICK 1000
326 #define SECSPERDAY 86400
327 #define TICKSPERSEC 10000000
328 /* UUID system time starts at October 15, 1582 */
329 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
330 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
332 static void RPC_UuidGetSystemTime(ULONGLONG *time)
334 FILETIME ft;
336 GetSystemTimeAsFileTime(&ft);
338 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
339 *time += TICKS_15_OCT_1582_TO_1601;
342 /* Assume that a hardware address is at least 6 bytes long */
343 #define ADDRESS_BYTES_NEEDED 6
345 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
347 int i;
348 DWORD status = RPC_S_OK;
350 ULONG buflen = sizeof(IP_ADAPTER_INFO);
351 PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
353 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
354 HeapFree(GetProcessHeap(), 0, adapter);
355 adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
358 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
359 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
360 address[i] = adapter->Address[i];
363 /* We can't get a hardware address, just use random numbers.
364 Set the multicast bit to prevent conflicts with real cards. */
365 else {
366 RtlGenRandom(address, ADDRESS_BYTES_NEEDED);
367 address[0] |= 0x01;
368 status = RPC_S_UUID_LOCAL_ONLY;
371 HeapFree(GetProcessHeap(), 0, adapter);
372 return status;
375 /*************************************************************************
376 * UuidCreateSequential [RPCRT4.@]
378 * Creates a 128bit UUID.
380 * RETURNS
382 * RPC_S_OK if successful.
383 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
385 * FIXME: No compensation for changes across reloading
386 * this dll or across reboots (e.g. clock going
387 * backwards and swapped network cards). The RFC
388 * suggests using NVRAM for storing persistent
389 * values.
391 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
393 static BOOL initialised;
394 static int count;
396 ULONGLONG time;
397 static ULONGLONG timelast;
398 static WORD sequence;
400 static DWORD status;
401 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
403 EnterCriticalSection(&uuid_cs);
405 if (!initialised) {
406 RPC_UuidGetSystemTime(&timelast);
407 count = TICKS_PER_CLOCK_TICK;
409 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
410 sequence &= 0x1fff;
412 status = RPC_UuidGetNodeAddress(address);
413 initialised = TRUE;
416 /* Generate time element of the UUID. Account for going faster
417 than our clock as well as the clock going backwards. */
418 while (1) {
419 RPC_UuidGetSystemTime(&time);
420 if (time > timelast) {
421 count = 0;
422 break;
424 if (time < timelast) {
425 sequence = (sequence + 1) & 0x1fff;
426 count = 0;
427 break;
429 if (count < TICKS_PER_CLOCK_TICK) {
430 count++;
431 break;
435 timelast = time;
436 time += count;
438 /* Pack the information into the UUID structure. */
440 Uuid->Data1 = (ULONG)(time & 0xffffffff);
441 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
442 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
444 /* This is a version 1 UUID */
445 Uuid->Data3 |= (1 << 12);
447 Uuid->Data4[0] = sequence & 0xff;
448 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
449 Uuid->Data4[1] |= 0x80;
450 memcpy(&Uuid->Data4[2], address, ADDRESS_BYTES_NEEDED);
452 LeaveCriticalSection(&uuid_cs);
454 TRACE("%s\n", debugstr_guid(Uuid));
456 return status;
459 /*************************************************************************
460 * I_UuidCreate [RPCRT4.@]
462 * See UuidCreateSequential()
464 RPC_STATUS WINAPI I_UuidCreate(UUID *Uuid)
466 return UuidCreateSequential(Uuid);
469 /*************************************************************************
470 * UuidHash [RPCRT4.@]
472 * Generates a hash value for a given UUID
474 * Code based on FreeDCE implementation
477 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
479 BYTE *data = (BYTE*)uuid;
480 short c0 = 0, c1 = 0, x, y;
481 unsigned int i;
483 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
485 TRACE("(%s)\n", debugstr_guid(uuid));
487 for (i=0; i<sizeof(UUID); i++) {
488 c0 += data[i];
489 c1 += c0;
492 x = -c1 % 255;
493 if (x < 0) x += 255;
495 y = (c1 - c0) % 255;
496 if (y < 0) y += 255;
498 *Status = RPC_S_OK;
499 return y*256 + x;
502 /*************************************************************************
503 * UuidToStringA [RPCRT4.@]
505 * Converts a UUID to a string.
507 * UUID format is 8 hex digits, followed by a hyphen then three groups of
508 * 4 hex digits each followed by a hyphen and then 12 hex digits
510 * RETURNS
512 * S_OK if successful.
513 * S_OUT_OF_MEMORY if unsuccessful.
515 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
517 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
519 if(!(*StringUuid))
520 return RPC_S_OUT_OF_MEMORY;
522 if (!Uuid) Uuid = &uuid_nil;
524 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
525 Uuid->Data1, Uuid->Data2, Uuid->Data3,
526 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
527 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
528 Uuid->Data4[6], Uuid->Data4[7] );
530 return RPC_S_OK;
533 /*************************************************************************
534 * UuidToStringW [RPCRT4.@]
536 * Converts a UUID to a string.
538 * S_OK if successful.
539 * S_OUT_OF_MEMORY if unsuccessful.
541 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
543 char buf[37];
545 if (!Uuid) Uuid = &uuid_nil;
547 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
548 Uuid->Data1, Uuid->Data2, Uuid->Data3,
549 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
550 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
551 Uuid->Data4[6], Uuid->Data4[7] );
553 *StringUuid = RPCRT4_strdupAtoW(buf);
555 if(!(*StringUuid))
556 return RPC_S_OUT_OF_MEMORY;
558 return RPC_S_OK;
561 static const BYTE hex2bin[] =
563 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
564 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
565 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
566 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
567 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
568 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
569 0,10,11,12,13,14,15 /* 0x60 */
572 /***********************************************************************
573 * UuidFromStringA (RPCRT4.@)
575 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
577 int i;
579 if (!s) return UuidCreateNil( uuid );
581 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
583 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
584 return RPC_S_INVALID_STRING_UUID;
586 for (i=0; i<36; i++)
588 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
589 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
592 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
594 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
595 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
596 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
597 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
599 /* these are just sequential bytes */
600 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
601 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
602 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
603 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
604 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
605 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
606 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
607 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
608 return RPC_S_OK;
612 /***********************************************************************
613 * UuidFromStringW (RPCRT4.@)
615 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
617 int i;
619 if (!s) return UuidCreateNil( uuid );
621 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
623 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
624 return RPC_S_INVALID_STRING_UUID;
626 for (i=0; i<36; i++)
628 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
629 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
632 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
634 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
635 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
636 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
637 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
639 /* these are just sequential bytes */
640 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
641 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
642 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
643 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
644 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
645 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
646 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
647 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
648 return RPC_S_OK;
651 /***********************************************************************
652 * DllRegisterServer (RPCRT4.@)
655 HRESULT WINAPI DllRegisterServer( void )
657 FIXME( "(): stub\n" );
658 return S_OK;
661 #define MAX_RPC_ERROR_TEXT 256
663 /******************************************************************************
664 * DceErrorInqTextW (rpcrt4.@)
666 * Notes
667 * 1. On passing a NULL pointer the code does bomb out.
668 * 2. The size of the required buffer is not defined in the documentation.
669 * It appears to be 256.
670 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
671 * of any value for which it does.
672 * 4. The MSDN documentation currently declares that the second argument is
673 * unsigned char *, even for the W version. I don't believe it.
675 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
677 DWORD count;
678 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
679 FORMAT_MESSAGE_IGNORE_INSERTS,
680 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
681 if (!count)
683 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
684 FORMAT_MESSAGE_IGNORE_INSERTS,
685 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
686 if (!count)
688 ERR ("Failed to translate error\n");
689 return RPC_S_INVALID_ARG;
692 return RPC_S_OK;
695 /******************************************************************************
696 * DceErrorInqTextA (rpcrt4.@)
698 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
700 RPC_STATUS status;
701 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
702 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
704 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
705 NULL, NULL))
707 ERR ("Failed to translate error\n");
708 status = RPC_S_INVALID_ARG;
711 return status;
714 /******************************************************************************
715 * I_RpcAllocate (rpcrt4.@)
717 void * WINAPI I_RpcAllocate(unsigned int Size)
719 return HeapAlloc(GetProcessHeap(), 0, Size);
722 /******************************************************************************
723 * I_RpcFree (rpcrt4.@)
725 void WINAPI I_RpcFree(void *Object)
727 HeapFree(GetProcessHeap(), 0, Object);
730 /******************************************************************************
731 * I_RpcMapWin32Status (rpcrt4.@)
733 * Maps Win32 RPC error codes to NT statuses.
735 * PARAMS
736 * status [I] Win32 RPC error code.
738 * RETURNS
739 * Appropriate translation into an NT status code.
741 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
743 TRACE("(%d)\n", status);
744 switch (status)
746 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
747 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
748 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
749 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
750 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
751 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
752 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
753 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
754 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
755 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
756 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
757 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
758 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
759 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
760 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
761 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
762 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
763 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
764 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
765 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
766 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
767 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
768 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
769 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
770 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
771 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
772 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
773 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
774 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
775 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
776 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
777 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
778 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
779 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
780 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
781 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
782 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
783 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
784 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
785 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
786 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
787 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
788 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
789 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
790 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
791 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
792 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
793 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
794 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
795 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
796 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
797 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
798 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
799 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
800 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
801 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
802 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
803 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
804 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
805 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
806 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
807 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
808 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
809 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
810 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
811 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
812 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
813 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
814 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
815 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
816 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
817 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
818 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
819 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
820 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
821 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
822 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
823 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
824 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
825 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
826 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
827 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
828 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
829 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
830 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
831 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
832 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
833 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
834 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
835 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
836 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
837 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
838 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
839 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
840 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
841 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
842 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
843 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
844 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
845 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
846 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
847 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
848 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
849 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
850 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
851 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
852 default: return status;
856 /******************************************************************************
857 * I_RpcExceptionFilter (rpcrt4.@)
859 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
861 TRACE("0x%x\n", ExceptionCode);
862 switch (ExceptionCode)
864 case STATUS_DATATYPE_MISALIGNMENT:
865 case STATUS_BREAKPOINT:
866 case STATUS_ACCESS_VIOLATION:
867 case STATUS_ILLEGAL_INSTRUCTION:
868 case STATUS_PRIVILEGED_INSTRUCTION:
869 case STATUS_INSTRUCTION_MISALIGNMENT:
870 case STATUS_STACK_OVERFLOW:
871 case STATUS_POSSIBLE_DEADLOCK:
872 return EXCEPTION_CONTINUE_SEARCH;
873 default:
874 return EXCEPTION_EXECUTE_HANDLER;
878 /******************************************************************************
879 * RpcErrorStartEnumeration (rpcrt4.@)
881 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
883 FIXME("(%p): stub\n", EnumHandle);
884 return RPC_S_ENTRY_NOT_FOUND;
887 /******************************************************************************
888 * RpcErrorEndEnumeration (rpcrt4.@)
890 RPC_STATUS RPC_ENTRY RpcErrorEndEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
892 FIXME("(%p): stub\n", EnumHandle);
893 return RPC_S_OK;
896 /******************************************************************************
897 * RpcErrorSaveErrorInfo (rpcrt4.@)
899 RPC_STATUS RPC_ENTRY RpcErrorSaveErrorInfo(RPC_ERROR_ENUM_HANDLE *EnumHandle, void **ErrorBlob, SIZE_T *BlobSize)
901 FIXME("(%p %p %p): stub\n", EnumHandle, ErrorBlob, BlobSize);
902 return ERROR_CALL_NOT_IMPLEMENTED;
905 /******************************************************************************
906 * RpcErrorLoadErrorInfo (rpcrt4.@)
908 RPC_STATUS RPC_ENTRY RpcErrorLoadErrorInfo(void *ErrorBlob, SIZE_T BlobSize, RPC_ERROR_ENUM_HANDLE *EnumHandle)
910 FIXME("(%p %lu %p): stub\n", ErrorBlob, BlobSize, EnumHandle);
911 return ERROR_CALL_NOT_IMPLEMENTED;
914 /******************************************************************************
915 * RpcErrorGetNextRecord (rpcrt4.@)
917 RPC_STATUS RPC_ENTRY RpcErrorGetNextRecord(RPC_ERROR_ENUM_HANDLE *EnumHandle, BOOL CopyStrings, RPC_EXTENDED_ERROR_INFO *ErrorInfo)
919 FIXME("(%p %x %p): stub\n", EnumHandle, CopyStrings, ErrorInfo);
920 return RPC_S_ENTRY_NOT_FOUND;
923 /******************************************************************************
924 * RpcMgmtSetCancelTimeout (rpcrt4.@)
926 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
928 FIXME("(%d): stub\n", Timeout);
929 return RPC_S_OK;
932 static struct threaddata *get_or_create_threaddata(void)
934 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
935 if (!tdata)
937 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
938 if (!tdata) return NULL;
940 InitializeCriticalSection(&tdata->cs);
941 tdata->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": threaddata.cs");
942 tdata->thread_id = GetCurrentThreadId();
944 EnterCriticalSection(&threaddata_cs);
945 list_add_tail(&threaddata_list, &tdata->entry);
946 LeaveCriticalSection(&threaddata_cs);
948 NtCurrentTeb()->ReservedForNtRpc = tdata;
949 return tdata;
951 return tdata;
954 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
956 struct threaddata *tdata = get_or_create_threaddata();
957 if (!tdata) return;
959 EnterCriticalSection(&tdata->cs);
960 tdata->connection = Connection;
961 LeaveCriticalSection(&tdata->cs);
964 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
966 struct threaddata *tdata = get_or_create_threaddata();
967 if (!tdata) return;
969 tdata->server_binding = Binding;
972 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
974 struct threaddata *tdata = get_or_create_threaddata();
975 if (!tdata) return NULL;
977 return tdata->server_binding;
980 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
982 struct threaddata *tdata = get_or_create_threaddata();
983 struct context_handle_list *context_handle_list;
985 if (!tdata) return;
987 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
988 if (!context_handle_list) return;
990 context_handle_list->context_handle = SContext;
991 context_handle_list->next = tdata->context_handle_list;
992 tdata->context_handle_list = context_handle_list;
995 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
997 struct threaddata *tdata = get_or_create_threaddata();
998 struct context_handle_list *current, *prev;
1000 if (!tdata) return;
1002 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
1004 if (current->context_handle == SContext)
1006 if (prev)
1007 prev->next = current->next;
1008 else
1009 tdata->context_handle_list = current->next;
1010 HeapFree(GetProcessHeap(), 0, current);
1011 return;
1016 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
1018 struct threaddata *tdata = get_or_create_threaddata();
1019 struct context_handle_list *context_handle_list;
1020 NDR_SCONTEXT context_handle;
1022 if (!tdata) return NULL;
1024 context_handle_list = tdata->context_handle_list;
1025 if (!context_handle_list) return NULL;
1026 tdata->context_handle_list = context_handle_list->next;
1028 context_handle = context_handle_list->context_handle;
1029 HeapFree(GetProcessHeap(), 0, context_handle_list);
1030 return context_handle;
1033 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
1035 struct threaddata *tdata;
1037 EnterCriticalSection(&threaddata_cs);
1038 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
1039 if (tdata->thread_id == target_tid)
1041 EnterCriticalSection(&tdata->cs);
1042 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
1043 LeaveCriticalSection(&tdata->cs);
1044 break;
1046 LeaveCriticalSection(&threaddata_cs);
1048 return RPC_S_OK;
1051 /******************************************************************************
1052 * RpcCancelThread (rpcrt4.@)
1054 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
1056 TRACE("(%p)\n", ThreadHandle);
1057 return RpcCancelThreadEx(ThreadHandle, 0);
1060 /******************************************************************************
1061 * RpcCancelThreadEx (rpcrt4.@)
1063 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
1065 DWORD target_tid;
1067 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1069 target_tid = GetThreadId(ThreadHandle);
1070 if (!target_tid)
1071 return RPC_S_INVALID_ARG;
1073 if (Timeout)
1075 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1076 return RPC_S_OK;
1078 else
1079 return rpc_cancel_thread(target_tid);