libwine: Remove __wine_main_arg* from the public header.
[wine/zf.git] / dlls / rpcrt4 / rpcrt4_main.c
blobe716f7cfff2bf519f5250d58c05624566da3233d
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 /***********************************************************************
99 * DllMain
101 * PARAMS
102 * hinstDLL [I] handle to the DLL's instance
103 * fdwReason [I]
104 * lpvReserved [I] reserved, must be NULL
106 * RETURNS
107 * Success: TRUE
108 * Failure: FALSE
111 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
113 struct threaddata *tdata;
115 switch (fdwReason) {
116 case DLL_PROCESS_ATTACH:
117 break;
119 case DLL_THREAD_DETACH:
120 tdata = NtCurrentTeb()->ReservedForNtRpc;
121 if (tdata)
123 EnterCriticalSection(&threaddata_cs);
124 list_remove(&tdata->entry);
125 LeaveCriticalSection(&threaddata_cs);
127 tdata->cs.DebugInfo->Spare[0] = 0;
128 DeleteCriticalSection(&tdata->cs);
129 if (tdata->connection)
130 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
131 if (tdata->server_binding)
132 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
133 HeapFree(GetProcessHeap(), 0, tdata);
135 break;
137 case DLL_PROCESS_DETACH:
138 if (lpvReserved) break; /* do nothing if process is shutting down */
139 RPCRT4_destroy_all_protseqs();
140 RPCRT4_ServerFreeAllRegisteredAuthInfo();
141 DeleteCriticalSection(&uuid_cs);
142 DeleteCriticalSection(&threaddata_cs);
143 break;
146 return TRUE;
149 /*************************************************************************
150 * RpcStringFreeA [RPCRT4.@]
152 * Frees a character string allocated by the RPC run-time library.
154 * RETURNS
156 * S_OK if successful.
158 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
160 HeapFree( GetProcessHeap(), 0, *String);
162 return RPC_S_OK;
165 /*************************************************************************
166 * RpcStringFreeW [RPCRT4.@]
168 * Frees a character string allocated by the RPC run-time library.
170 * RETURNS
172 * S_OK if successful.
174 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
176 HeapFree( GetProcessHeap(), 0, *String);
178 return RPC_S_OK;
181 /*************************************************************************
182 * RpcRaiseException [RPCRT4.@]
184 * Raises an exception.
186 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
188 /* shouldn't return */
189 RaiseException(exception, 0, 0, NULL);
190 ERR("handler continued execution\n");
191 ExitProcess(1);
194 /*************************************************************************
195 * UuidCompare [RPCRT4.@]
197 * PARAMS
198 * UUID *Uuid1 [I] Uuid to compare
199 * UUID *Uuid2 [I] Uuid to compare
200 * RPC_STATUS *Status [O] returns RPC_S_OK
202 * RETURNS
203 * -1 if Uuid1 is less than Uuid2
204 * 0 if Uuid1 and Uuid2 are equal
205 * 1 if Uuid1 is greater than Uuid2
207 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
209 int i;
211 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
213 *Status = RPC_S_OK;
215 if (!Uuid1) Uuid1 = &uuid_nil;
216 if (!Uuid2) Uuid2 = &uuid_nil;
218 if (Uuid1 == Uuid2) return 0;
220 if (Uuid1->Data1 != Uuid2->Data1)
221 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
223 if (Uuid1->Data2 != Uuid2->Data2)
224 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
226 if (Uuid1->Data3 != Uuid2->Data3)
227 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
229 for (i = 0; i < 8; i++) {
230 if (Uuid1->Data4[i] < Uuid2->Data4[i])
231 return -1;
232 if (Uuid1->Data4[i] > Uuid2->Data4[i])
233 return 1;
236 return 0;
239 /*************************************************************************
240 * UuidEqual [RPCRT4.@]
242 * PARAMS
243 * UUID *Uuid1 [I] Uuid to compare
244 * UUID *Uuid2 [I] Uuid to compare
245 * RPC_STATUS *Status [O] returns RPC_S_OK
247 * RETURNS
248 * TRUE/FALSE
250 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
252 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
253 return !UuidCompare(Uuid1, Uuid2, Status);
256 /*************************************************************************
257 * UuidIsNil [RPCRT4.@]
259 * PARAMS
260 * UUID *Uuid [I] Uuid to compare
261 * RPC_STATUS *Status [O] returns RPC_S_OK
263 * RETURNS
264 * TRUE/FALSE
266 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
268 TRACE("(%s)\n", debugstr_guid(Uuid));
269 if (!Uuid) return TRUE;
270 return !UuidCompare(Uuid, &uuid_nil, Status);
273 /*************************************************************************
274 * UuidCreateNil [RPCRT4.@]
276 * PARAMS
277 * UUID *Uuid [O] returns a nil UUID
279 * RETURNS
280 * RPC_S_OK
282 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
284 *Uuid = uuid_nil;
285 return RPC_S_OK;
288 /*************************************************************************
289 * UuidCreate [RPCRT4.@]
291 * Creates a 128bit UUID.
293 * RETURNS
295 * RPC_S_OK if successful.
296 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
298 * NOTES
300 * Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
301 * Truly Random or Pseudo-Random Numbers)
303 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
305 RtlGenRandom(Uuid, sizeof(*Uuid));
306 /* Clear the version bits and set the version (4) */
307 Uuid->Data3 &= 0x0fff;
308 Uuid->Data3 |= (4 << 12);
309 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
310 * specified in RFC 4122, section 4.4.
312 Uuid->Data4[0] &= 0x3f;
313 Uuid->Data4[0] |= 0x80;
315 TRACE("%s\n", debugstr_guid(Uuid));
317 return RPC_S_OK;
320 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
321 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
322 #define TICKS_PER_CLOCK_TICK 1000
323 #define SECSPERDAY 86400
324 #define TICKSPERSEC 10000000
325 /* UUID system time starts at October 15, 1582 */
326 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
327 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
329 static void RPC_UuidGetSystemTime(ULONGLONG *time)
331 FILETIME ft;
333 GetSystemTimeAsFileTime(&ft);
335 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
336 *time += TICKS_15_OCT_1582_TO_1601;
339 /* Assume that a hardware address is at least 6 bytes long */
340 #define ADDRESS_BYTES_NEEDED 6
342 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
344 int i;
345 DWORD status = RPC_S_OK;
347 ULONG buflen = sizeof(IP_ADAPTER_INFO);
348 PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
350 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
351 HeapFree(GetProcessHeap(), 0, adapter);
352 adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
355 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
356 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
357 address[i] = adapter->Address[i];
360 /* We can't get a hardware address, just use random numbers.
361 Set the multicast bit to prevent conflicts with real cards. */
362 else {
363 RtlGenRandom(address, ADDRESS_BYTES_NEEDED);
364 address[0] |= 0x01;
365 status = RPC_S_UUID_LOCAL_ONLY;
368 HeapFree(GetProcessHeap(), 0, adapter);
369 return status;
372 /*************************************************************************
373 * UuidCreateSequential [RPCRT4.@]
375 * Creates a 128bit UUID.
377 * RETURNS
379 * RPC_S_OK if successful.
380 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
382 * FIXME: No compensation for changes across reloading
383 * this dll or across reboots (e.g. clock going
384 * backwards and swapped network cards). The RFC
385 * suggests using NVRAM for storing persistent
386 * values.
388 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
390 static BOOL initialised;
391 static int count;
393 ULONGLONG time;
394 static ULONGLONG timelast;
395 static WORD sequence;
397 static DWORD status;
398 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
400 EnterCriticalSection(&uuid_cs);
402 if (!initialised) {
403 RPC_UuidGetSystemTime(&timelast);
404 count = TICKS_PER_CLOCK_TICK;
406 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
407 sequence &= 0x1fff;
409 status = RPC_UuidGetNodeAddress(address);
410 initialised = TRUE;
413 /* Generate time element of the UUID. Account for going faster
414 than our clock as well as the clock going backwards. */
415 while (1) {
416 RPC_UuidGetSystemTime(&time);
417 if (time > timelast) {
418 count = 0;
419 break;
421 if (time < timelast) {
422 sequence = (sequence + 1) & 0x1fff;
423 count = 0;
424 break;
426 if (count < TICKS_PER_CLOCK_TICK) {
427 count++;
428 break;
432 timelast = time;
433 time += count;
435 /* Pack the information into the UUID structure. */
437 Uuid->Data1 = (ULONG)(time & 0xffffffff);
438 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
439 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
441 /* This is a version 1 UUID */
442 Uuid->Data3 |= (1 << 12);
444 Uuid->Data4[0] = sequence & 0xff;
445 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
446 Uuid->Data4[1] |= 0x80;
447 memcpy(&Uuid->Data4[2], address, ADDRESS_BYTES_NEEDED);
449 LeaveCriticalSection(&uuid_cs);
451 TRACE("%s\n", debugstr_guid(Uuid));
453 return status;
456 /*************************************************************************
457 * I_UuidCreate [RPCRT4.@]
459 * See UuidCreateSequential()
461 RPC_STATUS WINAPI I_UuidCreate(UUID *Uuid)
463 return UuidCreateSequential(Uuid);
466 /*************************************************************************
467 * UuidHash [RPCRT4.@]
469 * Generates a hash value for a given UUID
471 * Code based on FreeDCE implementation
474 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
476 BYTE *data = (BYTE*)uuid;
477 short c0 = 0, c1 = 0, x, y;
478 unsigned int i;
480 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
482 TRACE("(%s)\n", debugstr_guid(uuid));
484 for (i=0; i<sizeof(UUID); i++) {
485 c0 += data[i];
486 c1 += c0;
489 x = -c1 % 255;
490 if (x < 0) x += 255;
492 y = (c1 - c0) % 255;
493 if (y < 0) y += 255;
495 *Status = RPC_S_OK;
496 return y*256 + x;
499 /*************************************************************************
500 * UuidToStringA [RPCRT4.@]
502 * Converts a UUID to a string.
504 * UUID format is 8 hex digits, followed by a hyphen then three groups of
505 * 4 hex digits each followed by a hyphen and then 12 hex digits
507 * RETURNS
509 * S_OK if successful.
510 * S_OUT_OF_MEMORY if unsuccessful.
512 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
514 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
516 if(!(*StringUuid))
517 return RPC_S_OUT_OF_MEMORY;
519 if (!Uuid) Uuid = &uuid_nil;
521 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
522 Uuid->Data1, Uuid->Data2, Uuid->Data3,
523 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
524 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
525 Uuid->Data4[6], Uuid->Data4[7] );
527 return RPC_S_OK;
530 /*************************************************************************
531 * UuidToStringW [RPCRT4.@]
533 * Converts a UUID to a string.
535 * S_OK if successful.
536 * S_OUT_OF_MEMORY if unsuccessful.
538 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
540 char buf[37];
542 if (!Uuid) Uuid = &uuid_nil;
544 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
545 Uuid->Data1, Uuid->Data2, Uuid->Data3,
546 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
547 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
548 Uuid->Data4[6], Uuid->Data4[7] );
550 *StringUuid = RPCRT4_strdupAtoW(buf);
552 if(!(*StringUuid))
553 return RPC_S_OUT_OF_MEMORY;
555 return RPC_S_OK;
558 static const BYTE hex2bin[] =
560 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
561 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
562 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
563 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
564 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
565 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
566 0,10,11,12,13,14,15 /* 0x60 */
569 /***********************************************************************
570 * UuidFromStringA (RPCRT4.@)
572 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
574 int i;
576 if (!s) return UuidCreateNil( uuid );
578 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
580 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
581 return RPC_S_INVALID_STRING_UUID;
583 for (i=0; i<36; i++)
585 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
586 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
589 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
591 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
592 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
593 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
594 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
596 /* these are just sequential bytes */
597 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
598 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
599 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
600 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
601 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
602 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
603 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
604 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
605 return RPC_S_OK;
609 /***********************************************************************
610 * UuidFromStringW (RPCRT4.@)
612 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
614 int i;
616 if (!s) return UuidCreateNil( uuid );
618 if (lstrlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
620 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
621 return RPC_S_INVALID_STRING_UUID;
623 for (i=0; i<36; i++)
625 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
626 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
629 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
631 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
632 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
633 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
634 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
636 /* these are just sequential bytes */
637 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
638 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
639 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
640 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
641 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
642 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
643 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
644 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
645 return RPC_S_OK;
648 /***********************************************************************
649 * DllRegisterServer (RPCRT4.@)
652 HRESULT WINAPI DllRegisterServer( void )
654 FIXME( "(): stub\n" );
655 return S_OK;
658 #define MAX_RPC_ERROR_TEXT 256
660 /******************************************************************************
661 * DceErrorInqTextW (rpcrt4.@)
663 * Notes
664 * 1. On passing a NULL pointer the code does bomb out.
665 * 2. The size of the required buffer is not defined in the documentation.
666 * It appears to be 256.
667 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
668 * of any value for which it does.
669 * 4. The MSDN documentation currently declares that the second argument is
670 * unsigned char *, even for the W version. I don't believe it.
672 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
674 DWORD count;
675 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
676 FORMAT_MESSAGE_IGNORE_INSERTS,
677 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
678 if (!count)
680 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
681 FORMAT_MESSAGE_IGNORE_INSERTS,
682 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
683 if (!count)
685 ERR ("Failed to translate error\n");
686 return RPC_S_INVALID_ARG;
689 return RPC_S_OK;
692 /******************************************************************************
693 * DceErrorInqTextA (rpcrt4.@)
695 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
697 RPC_STATUS status;
698 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
699 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
701 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
702 NULL, NULL))
704 ERR ("Failed to translate error\n");
705 status = RPC_S_INVALID_ARG;
708 return status;
711 /******************************************************************************
712 * I_RpcAllocate (rpcrt4.@)
714 void * WINAPI I_RpcAllocate(unsigned int Size)
716 return HeapAlloc(GetProcessHeap(), 0, Size);
719 /******************************************************************************
720 * I_RpcFree (rpcrt4.@)
722 void WINAPI I_RpcFree(void *Object)
724 HeapFree(GetProcessHeap(), 0, Object);
727 /******************************************************************************
728 * I_RpcMapWin32Status (rpcrt4.@)
730 * Maps Win32 RPC error codes to NT statuses.
732 * PARAMS
733 * status [I] Win32 RPC error code.
735 * RETURNS
736 * Appropriate translation into an NT status code.
738 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
740 TRACE("(%d)\n", status);
741 switch (status)
743 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
744 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
745 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
746 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
747 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
748 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
749 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
750 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
751 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
752 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
753 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
754 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
755 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
756 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
757 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
758 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
759 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
760 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
761 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
762 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
763 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
764 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
765 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
766 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
767 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
768 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
769 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
770 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
771 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
772 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
773 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
774 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
775 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
776 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
777 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
778 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
779 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
780 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
781 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
782 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
783 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
784 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
785 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
786 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
787 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
788 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
789 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
790 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
791 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
792 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
793 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
794 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
795 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
796 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
797 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
798 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
799 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
800 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
801 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
802 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
803 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
804 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
805 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
806 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
807 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
808 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
809 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
810 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
811 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
812 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
813 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
814 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
815 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
816 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
817 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
818 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
819 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
820 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
821 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
822 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
823 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
824 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
825 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
826 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
827 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
828 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
829 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
830 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
831 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
832 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
833 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
834 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
835 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
836 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
837 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
838 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
839 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
840 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
841 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
842 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
843 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
844 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
845 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
846 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
847 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
848 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
849 default: return status;
853 /******************************************************************************
854 * RpcExceptionFilter (rpcrt4.@)
855 * I_RpcExceptionFilter (rpcrt4.@)
857 int WINAPI RpcExceptionFilter(ULONG ExceptionCode)
859 TRACE("0x%x\n", ExceptionCode);
860 switch (ExceptionCode)
862 case STATUS_DATATYPE_MISALIGNMENT:
863 case STATUS_BREAKPOINT:
864 case STATUS_ACCESS_VIOLATION:
865 case STATUS_ILLEGAL_INSTRUCTION:
866 case STATUS_PRIVILEGED_INSTRUCTION:
867 case STATUS_INSTRUCTION_MISALIGNMENT:
868 case STATUS_STACK_OVERFLOW:
869 case STATUS_POSSIBLE_DEADLOCK:
870 return EXCEPTION_CONTINUE_SEARCH;
871 default:
872 return EXCEPTION_EXECUTE_HANDLER;
876 /******************************************************************************
877 * RpcErrorStartEnumeration (rpcrt4.@)
879 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
881 FIXME("(%p): stub\n", EnumHandle);
882 return RPC_S_ENTRY_NOT_FOUND;
885 /******************************************************************************
886 * RpcErrorEndEnumeration (rpcrt4.@)
888 RPC_STATUS RPC_ENTRY RpcErrorEndEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
890 FIXME("(%p): stub\n", EnumHandle);
891 return RPC_S_OK;
894 /******************************************************************************
895 * RpcErrorSaveErrorInfo (rpcrt4.@)
897 RPC_STATUS RPC_ENTRY RpcErrorSaveErrorInfo(RPC_ERROR_ENUM_HANDLE *EnumHandle, void **ErrorBlob, SIZE_T *BlobSize)
899 FIXME("(%p %p %p): stub\n", EnumHandle, ErrorBlob, BlobSize);
900 return ERROR_CALL_NOT_IMPLEMENTED;
903 /******************************************************************************
904 * RpcErrorLoadErrorInfo (rpcrt4.@)
906 RPC_STATUS RPC_ENTRY RpcErrorLoadErrorInfo(void *ErrorBlob, SIZE_T BlobSize, RPC_ERROR_ENUM_HANDLE *EnumHandle)
908 FIXME("(%p %lu %p): stub\n", ErrorBlob, BlobSize, EnumHandle);
909 return ERROR_CALL_NOT_IMPLEMENTED;
912 /******************************************************************************
913 * RpcErrorGetNextRecord (rpcrt4.@)
915 RPC_STATUS RPC_ENTRY RpcErrorGetNextRecord(RPC_ERROR_ENUM_HANDLE *EnumHandle, BOOL CopyStrings, RPC_EXTENDED_ERROR_INFO *ErrorInfo)
917 FIXME("(%p %x %p): stub\n", EnumHandle, CopyStrings, ErrorInfo);
918 return RPC_S_ENTRY_NOT_FOUND;
921 /******************************************************************************
922 * RpcMgmtSetCancelTimeout (rpcrt4.@)
924 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
926 FIXME("(%d): stub\n", Timeout);
927 return RPC_S_OK;
930 static struct threaddata *get_or_create_threaddata(void)
932 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
933 if (!tdata)
935 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
936 if (!tdata) return NULL;
938 InitializeCriticalSection(&tdata->cs);
939 tdata->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": threaddata.cs");
940 tdata->thread_id = GetCurrentThreadId();
942 EnterCriticalSection(&threaddata_cs);
943 list_add_tail(&threaddata_list, &tdata->entry);
944 LeaveCriticalSection(&threaddata_cs);
946 NtCurrentTeb()->ReservedForNtRpc = tdata;
947 return tdata;
949 return tdata;
952 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
954 struct threaddata *tdata = get_or_create_threaddata();
955 if (!tdata) return;
957 EnterCriticalSection(&tdata->cs);
958 tdata->connection = Connection;
959 LeaveCriticalSection(&tdata->cs);
962 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
964 struct threaddata *tdata = get_or_create_threaddata();
965 if (!tdata) return;
967 tdata->server_binding = Binding;
970 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
972 struct threaddata *tdata = get_or_create_threaddata();
973 if (!tdata) return NULL;
975 return tdata->server_binding;
978 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
980 struct threaddata *tdata = get_or_create_threaddata();
981 struct context_handle_list *context_handle_list;
983 if (!tdata) return;
985 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
986 if (!context_handle_list) return;
988 context_handle_list->context_handle = SContext;
989 context_handle_list->next = tdata->context_handle_list;
990 tdata->context_handle_list = context_handle_list;
993 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
995 struct threaddata *tdata = get_or_create_threaddata();
996 struct context_handle_list *current, *prev;
998 if (!tdata) return;
1000 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
1002 if (current->context_handle == SContext)
1004 if (prev)
1005 prev->next = current->next;
1006 else
1007 tdata->context_handle_list = current->next;
1008 HeapFree(GetProcessHeap(), 0, current);
1009 return;
1014 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
1016 struct threaddata *tdata = get_or_create_threaddata();
1017 struct context_handle_list *context_handle_list;
1018 NDR_SCONTEXT context_handle;
1020 if (!tdata) return NULL;
1022 context_handle_list = tdata->context_handle_list;
1023 if (!context_handle_list) return NULL;
1024 tdata->context_handle_list = context_handle_list->next;
1026 context_handle = context_handle_list->context_handle;
1027 HeapFree(GetProcessHeap(), 0, context_handle_list);
1028 return context_handle;
1031 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
1033 struct threaddata *tdata;
1035 EnterCriticalSection(&threaddata_cs);
1036 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
1037 if (tdata->thread_id == target_tid)
1039 EnterCriticalSection(&tdata->cs);
1040 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
1041 LeaveCriticalSection(&tdata->cs);
1042 break;
1044 LeaveCriticalSection(&threaddata_cs);
1046 return RPC_S_OK;
1049 /******************************************************************************
1050 * RpcCancelThread (rpcrt4.@)
1052 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
1054 TRACE("(%p)\n", ThreadHandle);
1055 return RpcCancelThreadEx(ThreadHandle, 0);
1058 /******************************************************************************
1059 * RpcCancelThreadEx (rpcrt4.@)
1061 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
1063 DWORD target_tid;
1065 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1067 target_tid = GetThreadId(ThreadHandle);
1068 if (!target_tid)
1069 return RPC_S_INVALID_ARG;
1071 if (Timeout)
1073 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1074 return RPC_S_OK;
1076 else
1077 return rpc_cancel_thread(target_tid);