jscript: Implement the String.fixed() method.
[wine/hacks.git] / dlls / rpcrt4 / rpcrt4_main.c
blob1f0b1bbd6156af2f2c0ab166b67d6c8c6598eecd
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 "iptypes.h"
47 #include "iphlpapi.h"
48 #include "wine/unicode.h"
49 #include "rpc.h"
51 #include "ole2.h"
52 #include "rpcndr.h"
53 #include "rpcproxy.h"
55 #include "rpc_binding.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
61 static UUID uuid_nil;
63 static CRITICAL_SECTION uuid_cs;
64 static CRITICAL_SECTION_DEBUG critsect_debug =
66 0, 0, &uuid_cs,
67 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
68 0, 0, { (DWORD_PTR)(__FILE__ ": uuid_cs") }
70 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
72 static CRITICAL_SECTION threaddata_cs;
73 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
75 0, 0, &threaddata_cs,
76 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
77 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
79 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
81 struct list threaddata_list = LIST_INIT(threaddata_list);
83 struct context_handle_list
85 struct context_handle_list *next;
86 NDR_SCONTEXT context_handle;
89 struct threaddata
91 struct list entry;
92 CRITICAL_SECTION cs;
93 DWORD thread_id;
94 RpcConnection *connection;
95 RpcBinding *server_binding;
96 struct context_handle_list *context_handle_list;
99 /***********************************************************************
100 * DllMain
102 * PARAMS
103 * hinstDLL [I] handle to the DLL's instance
104 * fdwReason [I]
105 * lpvReserved [I] reserved, must be NULL
107 * RETURNS
108 * Success: TRUE
109 * Failure: FALSE
112 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
114 struct threaddata *tdata;
116 switch (fdwReason) {
117 case DLL_PROCESS_ATTACH:
118 break;
120 case DLL_THREAD_DETACH:
121 tdata = NtCurrentTeb()->ReservedForNtRpc;
122 if (tdata)
124 EnterCriticalSection(&threaddata_cs);
125 list_remove(&tdata->entry);
126 LeaveCriticalSection(&threaddata_cs);
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 break;
141 return TRUE;
144 /*************************************************************************
145 * RpcStringFreeA [RPCRT4.@]
147 * Frees a character string allocated by the RPC run-time library.
149 * RETURNS
151 * S_OK if successful.
153 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
155 HeapFree( GetProcessHeap(), 0, *String);
157 return RPC_S_OK;
160 /*************************************************************************
161 * RpcStringFreeW [RPCRT4.@]
163 * Frees a character string allocated by the RPC run-time library.
165 * RETURNS
167 * S_OK if successful.
169 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
171 HeapFree( GetProcessHeap(), 0, *String);
173 return RPC_S_OK;
176 /*************************************************************************
177 * RpcRaiseException [RPCRT4.@]
179 * Raises an exception.
181 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
183 /* shouldn't return */
184 RaiseException(exception, 0, 0, NULL);
185 ERR("handler continued execution\n");
186 ExitProcess(1);
189 /*************************************************************************
190 * UuidCompare [RPCRT4.@]
192 * PARAMS
193 * UUID *Uuid1 [I] Uuid to compare
194 * UUID *Uuid2 [I] Uuid to compare
195 * RPC_STATUS *Status [O] returns RPC_S_OK
197 * RETURNS
198 * -1 if Uuid1 is less than Uuid2
199 * 0 if Uuid1 and Uuid2 are equal
200 * 1 if Uuid1 is greater than Uuid2
202 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
204 int i;
206 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
208 *Status = RPC_S_OK;
210 if (!Uuid1) Uuid1 = &uuid_nil;
211 if (!Uuid2) Uuid2 = &uuid_nil;
213 if (Uuid1 == Uuid2) return 0;
215 if (Uuid1->Data1 != Uuid2->Data1)
216 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
218 if (Uuid1->Data2 != Uuid2->Data2)
219 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
221 if (Uuid1->Data3 != Uuid2->Data3)
222 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
224 for (i = 0; i < 8; i++) {
225 if (Uuid1->Data4[i] < Uuid2->Data4[i])
226 return -1;
227 if (Uuid1->Data4[i] > Uuid2->Data4[i])
228 return 1;
231 return 0;
234 /*************************************************************************
235 * UuidEqual [RPCRT4.@]
237 * PARAMS
238 * UUID *Uuid1 [I] Uuid to compare
239 * UUID *Uuid2 [I] Uuid to compare
240 * RPC_STATUS *Status [O] returns RPC_S_OK
242 * RETURNS
243 * TRUE/FALSE
245 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
247 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
248 return !UuidCompare(Uuid1, Uuid2, Status);
251 /*************************************************************************
252 * UuidIsNil [RPCRT4.@]
254 * PARAMS
255 * UUID *Uuid [I] Uuid to compare
256 * RPC_STATUS *Status [O] returns RPC_S_OK
258 * RETURNS
259 * TRUE/FALSE
261 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
263 TRACE("(%s)\n", debugstr_guid(Uuid));
264 if (!Uuid) return TRUE;
265 return !UuidCompare(Uuid, &uuid_nil, Status);
268 /*************************************************************************
269 * UuidCreateNil [RPCRT4.@]
271 * PARAMS
272 * UUID *Uuid [O] returns a nil UUID
274 * RETURNS
275 * RPC_S_OK
277 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
279 *Uuid = uuid_nil;
280 return RPC_S_OK;
283 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
284 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
285 #define TICKS_PER_CLOCK_TICK 1000
286 #define SECSPERDAY 86400
287 #define TICKSPERSEC 10000000
288 /* UUID system time starts at October 15, 1582 */
289 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
290 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
292 static void RPC_UuidGetSystemTime(ULONGLONG *time)
294 FILETIME ft;
296 GetSystemTimeAsFileTime(&ft);
298 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
299 *time += TICKS_15_OCT_1582_TO_1601;
302 /* Assume that a hardware address is at least 6 bytes long */
303 #define ADDRESS_BYTES_NEEDED 6
305 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
307 int i;
308 DWORD status = RPC_S_OK;
310 ULONG buflen = sizeof(IP_ADAPTER_INFO);
311 PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
313 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
314 HeapFree(GetProcessHeap(), 0, adapter);
315 adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
318 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
319 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
320 address[i] = adapter->Address[i];
323 /* We can't get a hardware address, just use random numbers.
324 Set the multicast bit to prevent conflicts with real cards. */
325 else {
326 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
327 address[i] = rand() & 0xff;
330 address[0] |= 0x01;
331 status = RPC_S_UUID_LOCAL_ONLY;
334 HeapFree(GetProcessHeap(), 0, adapter);
335 return status;
338 /*************************************************************************
339 * UuidCreate [RPCRT4.@]
341 * Creates a 128bit UUID.
343 * RETURNS
345 * RPC_S_OK if successful.
346 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
348 * FIXME: No compensation for changes across reloading
349 * this dll or across reboots (e.g. clock going
350 * backwards and swapped network cards). The RFC
351 * suggests using NVRAM for storing persistent
352 * values.
354 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
356 static int initialised, count;
358 ULONGLONG time;
359 static ULONGLONG timelast;
360 static WORD sequence;
362 static DWORD status;
363 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
365 EnterCriticalSection(&uuid_cs);
367 if (!initialised) {
368 RPC_UuidGetSystemTime(&timelast);
369 count = TICKS_PER_CLOCK_TICK;
371 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
372 sequence &= 0x1fff;
374 status = RPC_UuidGetNodeAddress(address);
375 initialised = 1;
378 /* Generate time element of the UUID. Account for going faster
379 than our clock as well as the clock going backwards. */
380 while (1) {
381 RPC_UuidGetSystemTime(&time);
382 if (time > timelast) {
383 count = 0;
384 break;
386 if (time < timelast) {
387 sequence = (sequence + 1) & 0x1fff;
388 count = 0;
389 break;
391 if (count < TICKS_PER_CLOCK_TICK) {
392 count++;
393 break;
397 timelast = time;
398 time += count;
400 /* Pack the information into the UUID structure. */
402 Uuid->Data1 = (unsigned long)(time & 0xffffffff);
403 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
404 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
406 /* This is a version 1 UUID */
407 Uuid->Data3 |= (1 << 12);
409 Uuid->Data4[0] = sequence & 0xff;
410 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
411 Uuid->Data4[1] |= 0x80;
413 Uuid->Data4[2] = address[0];
414 Uuid->Data4[3] = address[1];
415 Uuid->Data4[4] = address[2];
416 Uuid->Data4[5] = address[3];
417 Uuid->Data4[6] = address[4];
418 Uuid->Data4[7] = address[5];
420 LeaveCriticalSection(&uuid_cs);
422 TRACE("%s\n", debugstr_guid(Uuid));
424 return status;
427 /*************************************************************************
428 * UuidCreateSequential [RPCRT4.@]
430 * Creates a 128bit UUID.
432 * RETURNS
434 * RPC_S_OK if successful.
435 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
438 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
440 return UuidCreate(Uuid);
444 /*************************************************************************
445 * UuidHash [RPCRT4.@]
447 * Generates a hash value for a given UUID
449 * Code based on FreeDCE implementation
452 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
454 BYTE *data = (BYTE*)uuid;
455 short c0 = 0, c1 = 0, x, y;
456 unsigned int i;
458 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
460 TRACE("(%s)\n", debugstr_guid(uuid));
462 for (i=0; i<sizeof(UUID); i++) {
463 c0 += data[i];
464 c1 += c0;
467 x = -c1 % 255;
468 if (x < 0) x += 255;
470 y = (c1 - c0) % 255;
471 if (y < 0) y += 255;
473 *Status = RPC_S_OK;
474 return y*256 + x;
477 /*************************************************************************
478 * UuidToStringA [RPCRT4.@]
480 * Converts a UUID to a string.
482 * UUID format is 8 hex digits, followed by a hyphen then three groups of
483 * 4 hex digits each followed by a hyphen and then 12 hex digits
485 * RETURNS
487 * S_OK if successful.
488 * S_OUT_OF_MEMORY if unsuccessful.
490 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
492 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
494 if(!(*StringUuid))
495 return RPC_S_OUT_OF_MEMORY;
497 if (!Uuid) Uuid = &uuid_nil;
499 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
500 Uuid->Data1, Uuid->Data2, Uuid->Data3,
501 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
502 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
503 Uuid->Data4[6], Uuid->Data4[7] );
505 return RPC_S_OK;
508 /*************************************************************************
509 * UuidToStringW [RPCRT4.@]
511 * Converts a UUID to a string.
513 * S_OK if successful.
514 * S_OUT_OF_MEMORY if unsuccessful.
516 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
518 char buf[37];
520 if (!Uuid) Uuid = &uuid_nil;
522 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
523 Uuid->Data1, Uuid->Data2, Uuid->Data3,
524 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
525 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
526 Uuid->Data4[6], Uuid->Data4[7] );
528 *StringUuid = RPCRT4_strdupAtoW(buf);
530 if(!(*StringUuid))
531 return RPC_S_OUT_OF_MEMORY;
533 return RPC_S_OK;
536 static const BYTE hex2bin[] =
538 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
539 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
540 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
541 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
542 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
543 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
544 0,10,11,12,13,14,15 /* 0x60 */
547 /***********************************************************************
548 * UuidFromStringA (RPCRT4.@)
550 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
552 int i;
554 if (!s) return UuidCreateNil( uuid );
556 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
558 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
559 return RPC_S_INVALID_STRING_UUID;
561 for (i=0; i<36; i++)
563 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
564 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
567 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
569 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
570 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
571 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
572 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
574 /* these are just sequential bytes */
575 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
576 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
577 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
578 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
579 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
580 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
581 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
582 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
583 return RPC_S_OK;
587 /***********************************************************************
588 * UuidFromStringW (RPCRT4.@)
590 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
592 int i;
594 if (!s) return UuidCreateNil( uuid );
596 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
598 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
599 return RPC_S_INVALID_STRING_UUID;
601 for (i=0; i<36; i++)
603 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
604 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
607 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
609 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
610 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
611 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
612 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
614 /* these are just sequential bytes */
615 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
616 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
617 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
618 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
619 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
620 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
621 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
622 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
623 return RPC_S_OK;
626 /***********************************************************************
627 * DllRegisterServer (RPCRT4.@)
630 HRESULT WINAPI DllRegisterServer( void )
632 FIXME( "(): stub\n" );
633 return S_OK;
636 #define MAX_RPC_ERROR_TEXT 256
638 /******************************************************************************
639 * DceErrorInqTextW (rpcrt4.@)
641 * Notes
642 * 1. On passing a NULL pointer the code does bomb out.
643 * 2. The size of the required buffer is not defined in the documentation.
644 * It appears to be 256.
645 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
646 * of any value for which it does.
647 * 4. The MSDN documentation currently declares that the second argument is
648 * unsigned char *, even for the W version. I don't believe it.
650 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
652 DWORD count;
653 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
654 FORMAT_MESSAGE_IGNORE_INSERTS,
655 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
656 if (!count)
658 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
659 FORMAT_MESSAGE_IGNORE_INSERTS,
660 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
661 if (!count)
663 ERR ("Failed to translate error\n");
664 return RPC_S_INVALID_ARG;
667 return RPC_S_OK;
670 /******************************************************************************
671 * DceErrorInqTextA (rpcrt4.@)
673 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
675 RPC_STATUS status;
676 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
677 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
679 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
680 NULL, NULL))
682 ERR ("Failed to translate error\n");
683 status = RPC_S_INVALID_ARG;
686 return status;
689 /******************************************************************************
690 * I_RpcAllocate (rpcrt4.@)
692 void * WINAPI I_RpcAllocate(unsigned int Size)
694 return HeapAlloc(GetProcessHeap(), 0, Size);
697 /******************************************************************************
698 * I_RpcFree (rpcrt4.@)
700 void WINAPI I_RpcFree(void *Object)
702 HeapFree(GetProcessHeap(), 0, Object);
705 /******************************************************************************
706 * I_RpcMapWin32Status (rpcrt4.@)
708 * Maps Win32 RPC error codes to NT statuses.
710 * PARAMS
711 * status [I] Win32 RPC error code.
713 * RETURNS
714 * Appropriate translation into an NT status code.
716 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
718 TRACE("(%ld)\n", status);
719 switch (status)
721 case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
722 case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
723 case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
724 case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
725 case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
726 case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
727 case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
728 case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
729 case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
730 case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
731 case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
732 case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
733 case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
734 case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
735 case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
736 case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
737 case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
738 case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
739 case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
740 case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
741 case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
742 case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
743 case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
744 case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
745 case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
746 case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
747 case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
748 case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
749 case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
750 case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
751 case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
752 case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
753 case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
754 case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
755 case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
756 case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
757 case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
758 case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
759 case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
760 case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
761 case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
762 case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
763 case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
764 case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
765 case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
766 case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
767 case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
768 case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
769 case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
770 case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
771 case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
772 case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
773 case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
774 case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
775 case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
776 case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
777 case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
778 case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
779 case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
780 case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
781 case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
782 case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
783 case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
784 case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
785 case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
786 case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
787 case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
788 case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
789 case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
790 case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
791 case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
792 case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
793 case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
794 case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
795 case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
796 case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
797 case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
798 case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
799 case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
800 case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
801 case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
802 case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
803 case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
804 case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
805 case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
806 case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
807 case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
808 case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
809 case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
810 case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
811 case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
812 case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
813 case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
814 case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
815 case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
816 case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
817 case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
818 case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
819 case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
820 case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
821 case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
822 case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
823 case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
824 case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
825 case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
826 case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
827 default: return status;
831 /******************************************************************************
832 * I_RpcExceptionFilter (rpcrt4.@)
834 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
836 TRACE("0x%x\n", ExceptionCode);
837 switch (ExceptionCode)
839 case STATUS_DATATYPE_MISALIGNMENT:
840 case STATUS_BREAKPOINT:
841 case STATUS_ACCESS_VIOLATION:
842 case STATUS_ILLEGAL_INSTRUCTION:
843 case STATUS_PRIVILEGED_INSTRUCTION:
844 case STATUS_INSTRUCTION_MISALIGNMENT:
845 case STATUS_STACK_OVERFLOW:
846 case STATUS_POSSIBLE_DEADLOCK:
847 return EXCEPTION_CONTINUE_SEARCH;
848 default:
849 return EXCEPTION_EXECUTE_HANDLER;
853 /******************************************************************************
854 * RpcErrorStartEnumeration (rpcrt4.@)
856 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
858 FIXME("(%p): stub\n", EnumHandle);
859 return RPC_S_ENTRY_NOT_FOUND;
862 /******************************************************************************
863 * RpcMgmtSetCancelTimeout (rpcrt4.@)
865 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
867 FIXME("(%d): stub\n", Timeout);
868 return RPC_S_OK;
871 static struct threaddata *get_or_create_threaddata(void)
873 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
874 if (!tdata)
876 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
877 if (!tdata) return NULL;
879 InitializeCriticalSection(&tdata->cs);
880 tdata->thread_id = GetCurrentThreadId();
882 EnterCriticalSection(&threaddata_cs);
883 list_add_tail(&threaddata_list, &tdata->entry);
884 LeaveCriticalSection(&threaddata_cs);
886 NtCurrentTeb()->ReservedForNtRpc = tdata;
887 return tdata;
889 return tdata;
892 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
894 struct threaddata *tdata = get_or_create_threaddata();
895 if (!tdata) return;
897 EnterCriticalSection(&tdata->cs);
898 tdata->connection = Connection;
899 LeaveCriticalSection(&tdata->cs);
902 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
904 struct threaddata *tdata = get_or_create_threaddata();
905 if (!tdata) return;
907 tdata->server_binding = Binding;
910 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
912 struct threaddata *tdata = get_or_create_threaddata();
913 if (!tdata) return NULL;
915 return tdata->server_binding;
918 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
920 struct threaddata *tdata = get_or_create_threaddata();
921 struct context_handle_list *context_handle_list;
923 if (!tdata) return;
925 context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
926 if (!context_handle_list) return;
928 context_handle_list->context_handle = SContext;
929 context_handle_list->next = tdata->context_handle_list;
930 tdata->context_handle_list = context_handle_list;
933 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
935 struct threaddata *tdata = get_or_create_threaddata();
936 struct context_handle_list *current, *prev;
938 if (!tdata) return;
940 for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
942 if (current->context_handle == SContext)
944 if (prev)
945 prev->next = current->next;
946 else
947 tdata->context_handle_list = current->next;
948 HeapFree(GetProcessHeap(), 0, current);
949 return;
954 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
956 struct threaddata *tdata = get_or_create_threaddata();
957 struct context_handle_list *context_handle_list;
958 NDR_SCONTEXT context_handle;
960 if (!tdata) return NULL;
962 context_handle_list = tdata->context_handle_list;
963 if (!context_handle_list) return NULL;
964 tdata->context_handle_list = context_handle_list->next;
966 context_handle = context_handle_list->context_handle;
967 HeapFree(GetProcessHeap(), 0, context_handle_list);
968 return context_handle;
971 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
973 struct threaddata *tdata;
975 EnterCriticalSection(&threaddata_cs);
976 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
977 if (tdata->thread_id == target_tid)
979 EnterCriticalSection(&tdata->cs);
980 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
981 LeaveCriticalSection(&tdata->cs);
982 break;
984 LeaveCriticalSection(&threaddata_cs);
986 return RPC_S_OK;
989 /******************************************************************************
990 * RpcCancelThread (rpcrt4.@)
992 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
994 TRACE("(%p)\n", ThreadHandle);
995 return RpcCancelThreadEx(ThreadHandle, 0);
998 /******************************************************************************
999 * RpcCancelThreadEx (rpcrt4.@)
1001 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
1003 DWORD target_tid;
1005 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1007 target_tid = GetThreadId(ThreadHandle);
1008 if (!target_tid)
1009 return RPC_S_INVALID_ARG;
1011 if (Timeout)
1013 FIXME("(%p, %d)\n", ThreadHandle, Timeout);
1014 return RPC_S_OK;
1016 else
1017 return rpc_cancel_thread(target_tid);