gdiplus: Correctly forward to GdipAddPathPie.
[wine] / dlls / rpcrt4 / rpcrt4_main.c
1 /*
2  *  RPCRT4
3  *
4  * Copyright 2000 Huw D M Davies for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  * 
20  * WINE RPC TODO's (and a few TODONT's)
21  *
22  * - Statistics: we are supposed to be keeping various counters.  we aren't.
23  *
24  * - Async RPC: Unimplemented.
25  *
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.
29  */
30
31 #include "config.h"
32
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
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 "wine/unicode.h"
48 #include "rpc.h"
49
50 #include "ole2.h"
51 #include "rpcndr.h"
52 #include "rpcproxy.h"
53
54 #include "rpc_binding.h"
55 #include "rpc_server.h"
56
57 #include "wine/debug.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
60
61 static UUID uuid_nil;
62
63 static CRITICAL_SECTION threaddata_cs;
64 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
65 {
66     0, 0, &threaddata_cs,
67     { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
68       0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
69 };
70 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
71
72 static struct list threaddata_list = LIST_INIT(threaddata_list);
73
74 struct context_handle_list
75 {
76     struct context_handle_list *next;
77     NDR_SCONTEXT context_handle;
78 };
79
80 struct threaddata
81 {
82     struct list entry;
83     CRITICAL_SECTION cs;
84     DWORD thread_id;
85     RpcConnection *connection;
86     RpcBinding *server_binding;
87     struct context_handle_list *context_handle_list;
88 };
89
90 /***********************************************************************
91  * DllMain
92  *
93  * PARAMS
94  *     hinstDLL    [I] handle to the DLL's instance
95  *     fdwReason   [I]
96  *     lpvReserved [I] reserved, must be NULL
97  *
98  * RETURNS
99  *     Success: TRUE
100  *     Failure: FALSE
101  */
102
103 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
104 {
105     struct threaddata *tdata;
106
107     switch (fdwReason) {
108     case DLL_PROCESS_ATTACH:
109         break;
110
111     case DLL_THREAD_DETACH:
112         tdata = NtCurrentTeb()->ReservedForNtRpc;
113         if (tdata)
114         {
115             EnterCriticalSection(&threaddata_cs);
116             list_remove(&tdata->entry);
117             LeaveCriticalSection(&threaddata_cs);
118
119             DeleteCriticalSection(&tdata->cs);
120             if (tdata->connection)
121                 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
122             if (tdata->server_binding)
123                 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
124             HeapFree(GetProcessHeap(), 0, tdata);
125         }
126         break;
127
128     case DLL_PROCESS_DETACH:
129         RPCRT4_destroy_all_protseqs();
130         break;
131     }
132
133     return TRUE;
134 }
135
136 /*************************************************************************
137  *           RpcStringFreeA   [RPCRT4.@]
138  *
139  * Frees a character string allocated by the RPC run-time library.
140  *
141  * RETURNS
142  *
143  *  S_OK if successful.
144  */
145 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
146 {
147   HeapFree( GetProcessHeap(), 0, *String);
148
149   return RPC_S_OK;
150 }
151
152 /*************************************************************************
153  *           RpcStringFreeW   [RPCRT4.@]
154  *
155  * Frees a character string allocated by the RPC run-time library.
156  *
157  * RETURNS
158  *
159  *  S_OK if successful.
160  */
161 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
162 {
163   HeapFree( GetProcessHeap(), 0, *String);
164
165   return RPC_S_OK;
166 }
167
168 /*************************************************************************
169  *           RpcRaiseException   [RPCRT4.@]
170  *
171  * Raises an exception.
172  */
173 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
174 {
175   /* shouldn't return */
176   RaiseException(exception, 0, 0, NULL);
177   ERR("handler continued execution\n");
178   ExitProcess(1);
179 }
180
181 /*************************************************************************
182  * UuidCompare [RPCRT4.@]
183  *
184  * PARAMS
185  *     UUID *Uuid1        [I] Uuid to compare
186  *     UUID *Uuid2        [I] Uuid to compare
187  *     RPC_STATUS *Status [O] returns RPC_S_OK
188  * 
189  * RETURNS
190  *    -1  if Uuid1 is less than Uuid2
191  *     0  if Uuid1 and Uuid2 are equal
192  *     1  if Uuid1 is greater than Uuid2
193  */
194 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
195 {
196   int i;
197
198   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
199
200   *Status = RPC_S_OK;
201
202   if (!Uuid1) Uuid1 = &uuid_nil;
203   if (!Uuid2) Uuid2 = &uuid_nil;
204
205   if (Uuid1 == Uuid2) return 0;
206
207   if (Uuid1->Data1 != Uuid2->Data1)
208     return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
209
210   if (Uuid1->Data2 != Uuid2->Data2)
211     return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
212
213   if (Uuid1->Data3 != Uuid2->Data3)
214     return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
215
216   for (i = 0; i < 8; i++) {
217     if (Uuid1->Data4[i] < Uuid2->Data4[i])
218       return -1;
219     if (Uuid1->Data4[i] > Uuid2->Data4[i])
220       return 1;
221   }
222
223   return 0;
224 }
225
226 /*************************************************************************
227  * UuidEqual [RPCRT4.@]
228  *
229  * PARAMS
230  *     UUID *Uuid1        [I] Uuid to compare
231  *     UUID *Uuid2        [I] Uuid to compare
232  *     RPC_STATUS *Status [O] returns RPC_S_OK
233  *
234  * RETURNS
235  *     TRUE/FALSE
236  */
237 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
238 {
239   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
240   return !UuidCompare(Uuid1, Uuid2, Status);
241 }
242
243 /*************************************************************************
244  * UuidIsNil [RPCRT4.@]
245  *
246  * PARAMS
247  *     UUID *Uuid         [I] Uuid to compare
248  *     RPC_STATUS *Status [O] returns RPC_S_OK
249  *
250  * RETURNS
251  *     TRUE/FALSE
252  */
253 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
254 {
255   TRACE("(%s)\n", debugstr_guid(Uuid));
256   if (!Uuid) return TRUE;
257   return !UuidCompare(Uuid, &uuid_nil, Status);
258 }
259
260  /*************************************************************************
261  * UuidCreateNil [RPCRT4.@]
262  *
263  * PARAMS
264  *     UUID *Uuid [O] returns a nil UUID
265  *
266  * RETURNS
267  *     RPC_S_OK
268  */
269 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
270 {
271   *Uuid = uuid_nil;
272   return RPC_S_OK;
273 }
274
275 /*************************************************************************
276  *           UuidCreate   [RPCRT4.@]
277  *
278  * Creates a 128bit UUID.
279  *
280  * RETURNS
281  *
282  *  RPC_S_OK if successful.
283  *  RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
284  *
285  * NOTES
286  *
287  *  Follows RFC 4122, section 4.4 (Algorithms for Creating a UUID from
288  *  Truly Random or Pseudo-Random Numbers)
289  */
290 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
291 {
292     RtlGenRandom(Uuid, sizeof(*Uuid));
293     /* Clear the version bits and set the version (4) */
294     Uuid->Data3 &= 0x0fff;
295     Uuid->Data3 |= (4 << 12);
296     /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
297      * specified in RFC 4122, section 4.4.
298      */
299     Uuid->Data4[0] &= 0x3f;
300     Uuid->Data4[0] |= 0x80;
301
302     TRACE("%s\n", debugstr_guid(Uuid));
303
304     return RPC_S_OK;
305 }
306
307 /*************************************************************************
308  *           UuidCreateSequential   [RPCRT4.@]
309  *
310  * Creates a 128bit UUID.
311  *
312  * RETURNS
313  *
314  *  RPC_S_OK if successful.
315  *  RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
316  *
317  */
318 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
319 {
320    return UuidCreate(Uuid);
321 }
322
323
324 /*************************************************************************
325  *           UuidHash   [RPCRT4.@]
326  *
327  * Generates a hash value for a given UUID
328  *
329  * Code based on FreeDCE implementation
330  *
331  */
332 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
333 {
334   BYTE *data = (BYTE*)uuid;
335   short c0 = 0, c1 = 0, x, y;
336   unsigned int i;
337
338   if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
339
340   TRACE("(%s)\n", debugstr_guid(uuid));
341
342   for (i=0; i<sizeof(UUID); i++) {
343     c0 += data[i];
344     c1 += c0;
345   }
346
347   x = -c1 % 255;
348   if (x < 0) x += 255;
349
350   y = (c1 - c0) % 255;
351   if (y < 0) y += 255;
352
353   *Status = RPC_S_OK;
354   return y*256 + x;
355 }
356
357 /*************************************************************************
358  *           UuidToStringA   [RPCRT4.@]
359  *
360  * Converts a UUID to a string.
361  *
362  * UUID format is 8 hex digits, followed by a hyphen then three groups of
363  * 4 hex digits each followed by a hyphen and then 12 hex digits
364  *
365  * RETURNS
366  *
367  *  S_OK if successful.
368  *  S_OUT_OF_MEMORY if unsuccessful.
369  */
370 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
371 {
372   *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
373
374   if(!(*StringUuid))
375     return RPC_S_OUT_OF_MEMORY;
376
377   if (!Uuid) Uuid = &uuid_nil;
378
379   sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
380                  Uuid->Data1, Uuid->Data2, Uuid->Data3,
381                  Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
382                  Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
383                  Uuid->Data4[6], Uuid->Data4[7] );
384
385   return RPC_S_OK;
386 }
387
388 /*************************************************************************
389  *           UuidToStringW   [RPCRT4.@]
390  *
391  * Converts a UUID to a string.
392  *
393  *  S_OK if successful.
394  *  S_OUT_OF_MEMORY if unsuccessful.
395  */
396 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
397 {
398   char buf[37];
399
400   if (!Uuid) Uuid = &uuid_nil;
401
402   sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
403                Uuid->Data1, Uuid->Data2, Uuid->Data3,
404                Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
405                Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
406                Uuid->Data4[6], Uuid->Data4[7] );
407
408   *StringUuid = RPCRT4_strdupAtoW(buf);
409
410   if(!(*StringUuid))
411     return RPC_S_OUT_OF_MEMORY;
412
413   return RPC_S_OK;
414 }
415
416 static const BYTE hex2bin[] =
417 {
418     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x00 */
419     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x10 */
420     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x20 */
421     0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,        /* 0x30 */
422     0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,  /* 0x40 */
423     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x50 */
424     0,10,11,12,13,14,15                     /* 0x60 */
425 };
426
427 /***********************************************************************
428  *              UuidFromStringA (RPCRT4.@)
429  */
430 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
431 {
432     int i;
433
434     if (!s) return UuidCreateNil( uuid );
435
436     if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
437
438     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
439         return RPC_S_INVALID_STRING_UUID;
440
441     for (i=0; i<36; i++)
442     {
443         if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
444         if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
445     }
446
447     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
448
449     uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
450                    hex2bin[s[4]] << 12 | hex2bin[s[5]]  << 8 | hex2bin[s[6]]  << 4 | hex2bin[s[7]]);
451     uuid->Data2 =  hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
452     uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
453
454     /* these are just sequential bytes */
455     uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
456     uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
457     uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
458     uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
459     uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
460     uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
461     uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
462     uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
463     return RPC_S_OK;
464 }
465
466
467 /***********************************************************************
468  *              UuidFromStringW (RPCRT4.@)
469  */
470 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
471 {
472     int i;
473
474     if (!s) return UuidCreateNil( uuid );
475
476     if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
477
478     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
479         return RPC_S_INVALID_STRING_UUID;
480
481     for (i=0; i<36; i++)
482     {
483         if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
484         if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
485     }
486
487     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
488
489     uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
490                    hex2bin[s[4]] << 12 | hex2bin[s[5]]  << 8 | hex2bin[s[6]]  << 4 | hex2bin[s[7]]);
491     uuid->Data2 =  hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
492     uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
493
494     /* these are just sequential bytes */
495     uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
496     uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
497     uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
498     uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
499     uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
500     uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
501     uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
502     uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
503     return RPC_S_OK;
504 }
505
506 /***********************************************************************
507  *              DllRegisterServer (RPCRT4.@)
508  */
509
510 HRESULT WINAPI DllRegisterServer( void )
511 {
512     FIXME( "(): stub\n" );
513     return S_OK;
514 }
515
516 #define MAX_RPC_ERROR_TEXT 256
517
518 /******************************************************************************
519  * DceErrorInqTextW   (rpcrt4.@)
520  *
521  * Notes
522  * 1. On passing a NULL pointer the code does bomb out.
523  * 2. The size of the required buffer is not defined in the documentation.
524  *    It appears to be 256.
525  * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
526  *    of any value for which it does.
527  * 4. The MSDN documentation currently declares that the second argument is
528  *    unsigned char *, even for the W version.  I don't believe it.
529  */
530 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
531 {
532     DWORD count;
533     count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
534                 FORMAT_MESSAGE_IGNORE_INSERTS,
535                 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
536     if (!count)
537     {
538         count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
539                 FORMAT_MESSAGE_IGNORE_INSERTS,
540                 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
541         if (!count)
542         {
543             ERR ("Failed to translate error\n");
544             return RPC_S_INVALID_ARG;
545         }
546     }
547     return RPC_S_OK;
548 }
549
550 /******************************************************************************
551  * DceErrorInqTextA   (rpcrt4.@)
552  */
553 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
554 {
555     RPC_STATUS status;
556     WCHAR bufferW [MAX_RPC_ERROR_TEXT];
557     if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
558     {
559         if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
560                 NULL, NULL))
561         {
562             ERR ("Failed to translate error\n");
563             status = RPC_S_INVALID_ARG;
564         }
565     }
566     return status;
567 }
568
569 /******************************************************************************
570  * I_RpcAllocate   (rpcrt4.@)
571  */
572 void * WINAPI I_RpcAllocate(unsigned int Size)
573 {
574     return HeapAlloc(GetProcessHeap(), 0, Size);
575 }
576
577 /******************************************************************************
578  * I_RpcFree   (rpcrt4.@)
579  */
580 void WINAPI I_RpcFree(void *Object)
581 {
582     HeapFree(GetProcessHeap(), 0, Object);
583 }
584
585 /******************************************************************************
586  * I_RpcMapWin32Status   (rpcrt4.@)
587  *
588  * Maps Win32 RPC error codes to NT statuses.
589  *
590  * PARAMS
591  *  status [I] Win32 RPC error code.
592  *
593  * RETURNS
594  *  Appropriate translation into an NT status code.
595  */
596 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
597 {
598     TRACE("(%d)\n", status);
599     switch (status)
600     {
601     case ERROR_ACCESS_DENIED: return STATUS_ACCESS_DENIED;
602     case ERROR_INVALID_HANDLE: return RPC_NT_SS_CONTEXT_MISMATCH;
603     case ERROR_OUTOFMEMORY: return STATUS_NO_MEMORY;
604     case ERROR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
605     case ERROR_INSUFFICIENT_BUFFER: return STATUS_BUFFER_TOO_SMALL;
606     case ERROR_MAX_THRDS_REACHED: return STATUS_NO_MEMORY;
607     case ERROR_NOACCESS: return STATUS_ACCESS_VIOLATION;
608     case ERROR_NOT_ENOUGH_SERVER_MEMORY: return STATUS_INSUFF_SERVER_RESOURCES;
609     case ERROR_WRONG_PASSWORD: return STATUS_WRONG_PASSWORD;
610     case ERROR_INVALID_LOGON_HOURS: return STATUS_INVALID_LOGON_HOURS;
611     case ERROR_PASSWORD_EXPIRED: return STATUS_PASSWORD_EXPIRED;
612     case ERROR_ACCOUNT_DISABLED: return STATUS_ACCOUNT_DISABLED;
613     case ERROR_INVALID_SECURITY_DESCR: return STATUS_INVALID_SECURITY_DESCR;
614     case RPC_S_INVALID_STRING_BINDING: return RPC_NT_INVALID_STRING_BINDING;
615     case RPC_S_WRONG_KIND_OF_BINDING: return RPC_NT_WRONG_KIND_OF_BINDING;
616     case RPC_S_INVALID_BINDING: return RPC_NT_INVALID_BINDING;
617     case RPC_S_PROTSEQ_NOT_SUPPORTED: return RPC_NT_PROTSEQ_NOT_SUPPORTED;
618     case RPC_S_INVALID_RPC_PROTSEQ: return RPC_NT_INVALID_RPC_PROTSEQ;
619     case RPC_S_INVALID_STRING_UUID: return RPC_NT_INVALID_STRING_UUID;
620     case RPC_S_INVALID_ENDPOINT_FORMAT: return RPC_NT_INVALID_ENDPOINT_FORMAT;
621     case RPC_S_INVALID_NET_ADDR: return RPC_NT_INVALID_NET_ADDR;
622     case RPC_S_NO_ENDPOINT_FOUND: return RPC_NT_NO_ENDPOINT_FOUND;
623     case RPC_S_INVALID_TIMEOUT: return RPC_NT_INVALID_TIMEOUT;
624     case RPC_S_OBJECT_NOT_FOUND: return RPC_NT_OBJECT_NOT_FOUND;
625     case RPC_S_ALREADY_REGISTERED: return RPC_NT_ALREADY_REGISTERED;
626     case RPC_S_TYPE_ALREADY_REGISTERED: return RPC_NT_TYPE_ALREADY_REGISTERED;
627     case RPC_S_ALREADY_LISTENING: return RPC_NT_ALREADY_LISTENING;
628     case RPC_S_NO_PROTSEQS_REGISTERED: return RPC_NT_NO_PROTSEQS_REGISTERED;
629     case RPC_S_NOT_LISTENING: return RPC_NT_NOT_LISTENING;
630     case RPC_S_UNKNOWN_MGR_TYPE: return RPC_NT_UNKNOWN_MGR_TYPE;
631     case RPC_S_UNKNOWN_IF: return RPC_NT_UNKNOWN_IF;
632     case RPC_S_NO_BINDINGS: return RPC_NT_NO_BINDINGS;
633     case RPC_S_NO_PROTSEQS: return RPC_NT_NO_PROTSEQS;
634     case RPC_S_CANT_CREATE_ENDPOINT: return RPC_NT_CANT_CREATE_ENDPOINT;
635     case RPC_S_OUT_OF_RESOURCES: return RPC_NT_OUT_OF_RESOURCES;
636     case RPC_S_SERVER_UNAVAILABLE: return RPC_NT_SERVER_UNAVAILABLE;
637     case RPC_S_SERVER_TOO_BUSY: return RPC_NT_SERVER_TOO_BUSY;
638     case RPC_S_INVALID_NETWORK_OPTIONS: return RPC_NT_INVALID_NETWORK_OPTIONS;
639     case RPC_S_NO_CALL_ACTIVE: return RPC_NT_NO_CALL_ACTIVE;
640     case RPC_S_CALL_FAILED: return RPC_NT_CALL_FAILED;
641     case RPC_S_CALL_FAILED_DNE: return RPC_NT_CALL_FAILED_DNE;
642     case RPC_S_PROTOCOL_ERROR: return RPC_NT_PROTOCOL_ERROR;
643     case RPC_S_UNSUPPORTED_TRANS_SYN: return RPC_NT_UNSUPPORTED_TRANS_SYN;
644     case RPC_S_UNSUPPORTED_TYPE: return RPC_NT_UNSUPPORTED_TYPE;
645     case RPC_S_INVALID_TAG: return RPC_NT_INVALID_TAG;
646     case RPC_S_INVALID_BOUND: return RPC_NT_INVALID_BOUND;
647     case RPC_S_NO_ENTRY_NAME: return RPC_NT_NO_ENTRY_NAME;
648     case RPC_S_INVALID_NAME_SYNTAX: return RPC_NT_INVALID_NAME_SYNTAX;
649     case RPC_S_UNSUPPORTED_NAME_SYNTAX: return RPC_NT_UNSUPPORTED_NAME_SYNTAX;
650     case RPC_S_UUID_NO_ADDRESS: return RPC_NT_UUID_NO_ADDRESS;
651     case RPC_S_DUPLICATE_ENDPOINT: return RPC_NT_DUPLICATE_ENDPOINT;
652     case RPC_S_UNKNOWN_AUTHN_TYPE: return RPC_NT_UNKNOWN_AUTHN_TYPE;
653     case RPC_S_MAX_CALLS_TOO_SMALL: return RPC_NT_MAX_CALLS_TOO_SMALL;
654     case RPC_S_STRING_TOO_LONG: return RPC_NT_STRING_TOO_LONG;
655     case RPC_S_PROTSEQ_NOT_FOUND: return RPC_NT_PROTSEQ_NOT_FOUND;
656     case RPC_S_PROCNUM_OUT_OF_RANGE: return RPC_NT_PROCNUM_OUT_OF_RANGE;
657     case RPC_S_BINDING_HAS_NO_AUTH: return RPC_NT_BINDING_HAS_NO_AUTH;
658     case RPC_S_UNKNOWN_AUTHN_SERVICE: return RPC_NT_UNKNOWN_AUTHN_SERVICE;
659     case RPC_S_UNKNOWN_AUTHN_LEVEL: return RPC_NT_UNKNOWN_AUTHN_LEVEL;
660     case RPC_S_INVALID_AUTH_IDENTITY: return RPC_NT_INVALID_AUTH_IDENTITY;
661     case RPC_S_UNKNOWN_AUTHZ_SERVICE: return RPC_NT_UNKNOWN_AUTHZ_SERVICE;
662     case EPT_S_INVALID_ENTRY: return EPT_NT_INVALID_ENTRY;
663     case EPT_S_CANT_PERFORM_OP: return EPT_NT_CANT_PERFORM_OP;
664     case EPT_S_NOT_REGISTERED: return EPT_NT_NOT_REGISTERED;
665     case EPT_S_CANT_CREATE: return EPT_NT_CANT_CREATE;
666     case RPC_S_NOTHING_TO_EXPORT: return RPC_NT_NOTHING_TO_EXPORT;
667     case RPC_S_INCOMPLETE_NAME: return RPC_NT_INCOMPLETE_NAME;
668     case RPC_S_INVALID_VERS_OPTION: return RPC_NT_INVALID_VERS_OPTION;
669     case RPC_S_NO_MORE_MEMBERS: return RPC_NT_NO_MORE_MEMBERS;
670     case RPC_S_NOT_ALL_OBJS_UNEXPORTED: return RPC_NT_NOT_ALL_OBJS_UNEXPORTED;
671     case RPC_S_INTERFACE_NOT_FOUND: return RPC_NT_INTERFACE_NOT_FOUND;
672     case RPC_S_ENTRY_ALREADY_EXISTS: return RPC_NT_ENTRY_ALREADY_EXISTS;
673     case RPC_S_ENTRY_NOT_FOUND: return RPC_NT_ENTRY_NOT_FOUND;
674     case RPC_S_NAME_SERVICE_UNAVAILABLE: return RPC_NT_NAME_SERVICE_UNAVAILABLE;
675     case RPC_S_INVALID_NAF_ID: return RPC_NT_INVALID_NAF_ID;
676     case RPC_S_CANNOT_SUPPORT: return RPC_NT_CANNOT_SUPPORT;
677     case RPC_S_NO_CONTEXT_AVAILABLE: return RPC_NT_NO_CONTEXT_AVAILABLE;
678     case RPC_S_INTERNAL_ERROR: return RPC_NT_INTERNAL_ERROR;
679     case RPC_S_ZERO_DIVIDE: return RPC_NT_ZERO_DIVIDE;
680     case RPC_S_ADDRESS_ERROR: return RPC_NT_ADDRESS_ERROR;
681     case RPC_S_FP_DIV_ZERO: return RPC_NT_FP_DIV_ZERO;
682     case RPC_S_FP_UNDERFLOW: return RPC_NT_FP_UNDERFLOW;
683     case RPC_S_FP_OVERFLOW: return RPC_NT_FP_OVERFLOW;
684     case RPC_S_CALL_IN_PROGRESS: return RPC_NT_CALL_IN_PROGRESS;
685     case RPC_S_NO_MORE_BINDINGS: return RPC_NT_NO_MORE_BINDINGS;
686     case RPC_S_CALL_CANCELLED: return RPC_NT_CALL_CANCELLED;
687     case RPC_S_INVALID_OBJECT: return RPC_NT_INVALID_OBJECT;
688     case RPC_S_INVALID_ASYNC_HANDLE: return RPC_NT_INVALID_ASYNC_HANDLE;
689     case RPC_S_INVALID_ASYNC_CALL: return RPC_NT_INVALID_ASYNC_CALL;
690     case RPC_S_GROUP_MEMBER_NOT_FOUND: return RPC_NT_GROUP_MEMBER_NOT_FOUND;
691     case RPC_X_NO_MORE_ENTRIES: return RPC_NT_NO_MORE_ENTRIES;
692     case RPC_X_SS_CHAR_TRANS_OPEN_FAIL: return RPC_NT_SS_CHAR_TRANS_OPEN_FAIL;
693     case RPC_X_SS_CHAR_TRANS_SHORT_FILE: return RPC_NT_SS_CHAR_TRANS_SHORT_FILE;
694     case RPC_X_SS_IN_NULL_CONTEXT: return RPC_NT_SS_IN_NULL_CONTEXT;
695     case RPC_X_SS_CONTEXT_DAMAGED: return RPC_NT_SS_CONTEXT_DAMAGED;
696     case RPC_X_SS_HANDLES_MISMATCH: return RPC_NT_SS_HANDLES_MISMATCH;
697     case RPC_X_SS_CANNOT_GET_CALL_HANDLE: return RPC_NT_SS_CANNOT_GET_CALL_HANDLE;
698     case RPC_X_NULL_REF_POINTER: return RPC_NT_NULL_REF_POINTER;
699     case RPC_X_ENUM_VALUE_OUT_OF_RANGE: return RPC_NT_ENUM_VALUE_OUT_OF_RANGE;
700     case RPC_X_BYTE_COUNT_TOO_SMALL: return RPC_NT_BYTE_COUNT_TOO_SMALL;
701     case RPC_X_BAD_STUB_DATA: return RPC_NT_BAD_STUB_DATA;
702     case RPC_X_PIPE_CLOSED: return RPC_NT_PIPE_CLOSED;
703     case RPC_X_PIPE_DISCIPLINE_ERROR: return RPC_NT_PIPE_DISCIPLINE_ERROR;
704     case RPC_X_PIPE_EMPTY: return RPC_NT_PIPE_EMPTY;
705     case ERROR_PASSWORD_MUST_CHANGE: return STATUS_PASSWORD_MUST_CHANGE;
706     case ERROR_ACCOUNT_LOCKED_OUT: return STATUS_ACCOUNT_LOCKED_OUT;
707     default: return status;
708     }
709 }
710
711 /******************************************************************************
712  * I_RpcExceptionFilter   (rpcrt4.@)
713  */
714 int WINAPI I_RpcExceptionFilter(ULONG ExceptionCode)
715 {
716     TRACE("0x%x\n", ExceptionCode);
717     switch (ExceptionCode)
718     {
719     case STATUS_DATATYPE_MISALIGNMENT:
720     case STATUS_BREAKPOINT:
721     case STATUS_ACCESS_VIOLATION:
722     case STATUS_ILLEGAL_INSTRUCTION:
723     case STATUS_PRIVILEGED_INSTRUCTION:
724     case STATUS_INSTRUCTION_MISALIGNMENT:
725     case STATUS_STACK_OVERFLOW:
726     case STATUS_POSSIBLE_DEADLOCK:
727         return EXCEPTION_CONTINUE_SEARCH;
728     default:
729         return EXCEPTION_EXECUTE_HANDLER;
730     }
731 }
732
733 /******************************************************************************
734  * RpcErrorStartEnumeration   (rpcrt4.@)
735  */
736 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
737 {
738     FIXME("(%p): stub\n", EnumHandle);
739     return RPC_S_ENTRY_NOT_FOUND;
740 }
741
742 /******************************************************************************
743  * RpcMgmtSetCancelTimeout   (rpcrt4.@)
744  */
745 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
746 {
747     FIXME("(%d): stub\n", Timeout);
748     return RPC_S_OK;
749 }
750
751 static struct threaddata *get_or_create_threaddata(void)
752 {
753     struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
754     if (!tdata)
755     {
756         tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
757         if (!tdata) return NULL;
758
759         InitializeCriticalSection(&tdata->cs);
760         tdata->thread_id = GetCurrentThreadId();
761
762         EnterCriticalSection(&threaddata_cs);
763         list_add_tail(&threaddata_list, &tdata->entry);
764         LeaveCriticalSection(&threaddata_cs);
765
766         NtCurrentTeb()->ReservedForNtRpc = tdata;
767         return tdata;
768     }
769     return tdata;
770 }
771
772 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
773 {
774     struct threaddata *tdata = get_or_create_threaddata();
775     if (!tdata) return;
776
777     EnterCriticalSection(&tdata->cs);
778     tdata->connection = Connection;
779     LeaveCriticalSection(&tdata->cs);
780 }
781
782 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
783 {
784     struct threaddata *tdata = get_or_create_threaddata();
785     if (!tdata) return;
786
787     tdata->server_binding = Binding;
788 }
789
790 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
791 {
792     struct threaddata *tdata = get_or_create_threaddata();
793     if (!tdata) return NULL;
794
795     return tdata->server_binding;
796 }
797
798 void RPCRT4_PushThreadContextHandle(NDR_SCONTEXT SContext)
799 {
800     struct threaddata *tdata = get_or_create_threaddata();
801     struct context_handle_list *context_handle_list;
802
803     if (!tdata) return;
804
805     context_handle_list = HeapAlloc(GetProcessHeap(), 0, sizeof(*context_handle_list));
806     if (!context_handle_list) return;
807
808     context_handle_list->context_handle = SContext;
809     context_handle_list->next = tdata->context_handle_list;
810     tdata->context_handle_list = context_handle_list;
811 }
812
813 void RPCRT4_RemoveThreadContextHandle(NDR_SCONTEXT SContext)
814 {
815     struct threaddata *tdata = get_or_create_threaddata();
816     struct context_handle_list *current, *prev;
817
818     if (!tdata) return;
819
820     for (current = tdata->context_handle_list, prev = NULL; current; prev = current, current = current->next)
821     {
822         if (current->context_handle == SContext)
823         {
824             if (prev)
825                 prev->next = current->next;
826             else
827                 tdata->context_handle_list = current->next;
828             HeapFree(GetProcessHeap(), 0, current);
829             return;
830         }
831     }
832 }
833
834 NDR_SCONTEXT RPCRT4_PopThreadContextHandle(void)
835 {
836     struct threaddata *tdata = get_or_create_threaddata();
837     struct context_handle_list *context_handle_list;
838     NDR_SCONTEXT context_handle;
839
840     if (!tdata) return NULL;
841
842     context_handle_list = tdata->context_handle_list;
843     if (!context_handle_list) return NULL;
844     tdata->context_handle_list = context_handle_list->next;
845
846     context_handle = context_handle_list->context_handle;
847     HeapFree(GetProcessHeap(), 0, context_handle_list);
848     return context_handle;
849 }
850
851 static RPC_STATUS rpc_cancel_thread(DWORD target_tid)
852 {
853     struct threaddata *tdata;
854
855     EnterCriticalSection(&threaddata_cs);
856     LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
857         if (tdata->thread_id == target_tid)
858         {
859             EnterCriticalSection(&tdata->cs);
860             if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
861             LeaveCriticalSection(&tdata->cs);
862             break;
863         }
864     LeaveCriticalSection(&threaddata_cs);
865
866     return RPC_S_OK;
867 }
868
869 /******************************************************************************
870  * RpcCancelThread   (rpcrt4.@)
871  */
872 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
873 {
874     TRACE("(%p)\n", ThreadHandle);
875     return RpcCancelThreadEx(ThreadHandle, 0);
876 }
877
878 /******************************************************************************
879  * RpcCancelThreadEx   (rpcrt4.@)
880  */
881 RPC_STATUS RPC_ENTRY RpcCancelThreadEx(void* ThreadHandle, LONG Timeout)
882 {
883     DWORD target_tid;
884
885     FIXME("(%p, %d)\n", ThreadHandle, Timeout);
886
887     target_tid = GetThreadId(ThreadHandle);
888     if (!target_tid)
889         return RPC_S_INVALID_ARG;
890
891     if (Timeout)
892     {
893         FIXME("(%p, %d)\n", ThreadHandle, Timeout);
894         return RPC_S_OK;
895     }
896     else
897         return rpc_cancel_thread(target_tid);
898 }