rpcrt4: Constify NDR function tables.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  * WINE RPC TODO's (and a few TODONT's)
21  *
22  * - Ove's decreasingly incomplete widl is an IDL compiler for wine.  For widl
23  *   to be wine's only IDL compiler, a fair bit of work remains to be done.
24  *   until then we have used some midl-generated stuff.  (What?)
25  *   widl currently doesn't generate stub/proxy files required by wine's (O)RPC
26  *   capabilities -- nor does it make those lovely format strings :(
27  *   The MS MIDL compiler does some really esoteric stuff.  Of course Ove has
28  *   started with the less esoteric stuff.  There are also lots of nice
29  *   comments in there if you want to flex your bison and help build this monster.
30  *
31  * - RPC has a quite featureful error handling mechanism; basically none of this is
32  *   implemented right now.  We also have deficiencies on the compiler side, where
33  *   wine's __TRY / __EXCEPT / __FINALLY macros are not even used for RpcTryExcept & co,
34  *   due to syntactic differences! (we can fix it with widl by using __TRY)
35  *
36  * - There are several different memory allocation schemes for MSRPC.
37  *   I don't even understand what they all are yet, much less have them
38  *   properly implemented.  Surely we are supposed to be doing something with
39  *   the user-provided allocation/deallocation functions, but so far,
40  *   I don't think we are doing this...
41  *
42  * - MSRPC provides impersonation capabilities which currently are not possible
43  *   to implement in wine.  At the very least we should implement the authorization
44  *   API's & gracefully ignore the irrelevant stuff (to an extent we already do).
45  *
46  * - Some transports are not yet implemented.  The existing transport implementations
47  *   are incomplete and may be bug-infested.
48  * 
49  * - The various transports that we do support ought to be supported in a more
50  *   object-oriented manner, as in DCE's RPC implementation, instead of cluttering
51  *   up the code with conditionals like we do now.
52  * 
53  * - Data marshalling: So far, only the beginnings of a full implementation
54  *   exist in wine.  NDR protocol itself is documented, but the MS API's to
55  *   convert data-types in memory into NDR are not.  This is challenging work,
56  *   and has supposedly been "at the top of Greg's queue" for several months now.
57  *
58  * - ORPC is RPC for OLE; once we have a working RPC framework, we can
59  *   use it to implement out-of-process OLE client/server communications.
60  *   ATM there is maybe a disconnect between the marshalling in the OLE DLLs
61  *   and the marshalling going on here [TODO: well, is there or not?]
62  * 
63  * - In-source API Documentation, at least for those functions which we have
64  *   implemented, but preferably for everything we can document, would be nice,
65  *   since some of this stuff is quite obscure.
66  *
67  * - Name services... [TODO: what about them]
68  *
69  * - Protocol Towers: Totally unimplemented.... I think.
70  *
71  * - Context Handle Rundown: whatever that is.
72  *
73  * - Nested RPC's: Totally unimplemented.
74  *
75  * - Statistics: we are supposed to be keeping various counters.  we aren't.
76  *
77  * - Async RPC: Unimplemented.
78  *
79  * - XML/http RPC: Somewhere there's an XML fiend that wants to do this! Betcha
80  *   we could use these as a transport for RPC's across computers without a
81  *   permissions and/or licensing crisis.
82  *
83  * - The NT "ports" API, aka LPC.  Greg claims this is on his radar.  Might (or
84  *   might not) enable users to get some kind of meaningful result out of
85  *   NT-based native rpcrt4's.  Commonly-used transport for self-to-self RPC's.
86  *
87  * - ...?  More stuff I haven't thought of.  If you think of more RPC todo's
88  *   drop me an e-mail <gmturner007@ameritech.net> or send a patch to the
89  *   wine-patches mailing list.
90  */
91
92 #include "config.h"
93
94 #include <stdarg.h>
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98
99 #include "windef.h"
100 #include "winerror.h"
101 #include "winbase.h"
102 #include "winuser.h"
103 #include "iptypes.h"
104 #include "iphlpapi.h"
105 #include "wine/unicode.h"
106 #include "rpc.h"
107
108 #include "ole2.h"
109 #include "rpcndr.h"
110 #include "rpcproxy.h"
111
112 #include "rpc_binding.h"
113 #include "rpcss_np_client.h"
114
115 #include "wine/debug.h"
116
117 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
118
119 static UUID uuid_nil;
120 static HANDLE master_mutex;
121
122 HANDLE RPCRT4_GetMasterMutex(void)
123 {
124     return master_mutex;
125 }
126
127 static CRITICAL_SECTION uuid_cs;
128 static CRITICAL_SECTION_DEBUG critsect_debug =
129 {
130     0, 0, &uuid_cs,
131     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
132       0, 0, { (DWORD_PTR)(__FILE__ ": uuid_cs") }
133 };
134 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
135
136 /***********************************************************************
137  * DllMain
138  *
139  * PARAMS
140  *     hinstDLL    [I] handle to the DLL's instance
141  *     fdwReason   [I]
142  *     lpvReserved [I] reserved, must be NULL
143  *
144  * RETURNS
145  *     Success: TRUE
146  *     Failure: FALSE
147  */
148
149 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
150 {
151     switch (fdwReason) {
152     case DLL_PROCESS_ATTACH:
153         DisableThreadLibraryCalls(hinstDLL);
154         master_mutex = CreateMutexA( NULL, FALSE, RPCSS_MASTER_MUTEX_NAME);
155         if (!master_mutex)
156           ERR("Failed to create master mutex\n");
157         break;
158
159     case DLL_PROCESS_DETACH:
160         CloseHandle(master_mutex);
161         master_mutex = NULL;
162         break;
163     }
164
165     return TRUE;
166 }
167
168 /*************************************************************************
169  *           RpcStringFreeA   [RPCRT4.@]
170  *
171  * Frees a character string allocated by the RPC run-time library.
172  *
173  * RETURNS
174  *
175  *  S_OK if successful.
176  */
177 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
178 {
179   HeapFree( GetProcessHeap(), 0, *String);
180
181   return RPC_S_OK;
182 }
183
184 /*************************************************************************
185  *           RpcStringFreeW   [RPCRT4.@]
186  *
187  * Frees a character string allocated by the RPC run-time library.
188  *
189  * RETURNS
190  *
191  *  S_OK if successful.
192  */
193 RPC_STATUS WINAPI RpcStringFreeW(unsigned short** String)
194 {
195   HeapFree( GetProcessHeap(), 0, *String);
196
197   return RPC_S_OK;
198 }
199
200 /*************************************************************************
201  *           RpcRaiseException   [RPCRT4.@]
202  *
203  * Raises an exception.
204  */
205 void WINAPI RpcRaiseException(RPC_STATUS exception)
206 {
207   /* FIXME: translate exception? */
208   RaiseException(exception, 0, 0, NULL);
209 }
210
211 /*************************************************************************
212  * UuidCompare [RPCRT4.@]
213  *
214  * PARAMS
215  *     UUID *Uuid1        [I] Uuid to compare
216  *     UUID *Uuid2        [I] Uuid to compare
217  *     RPC_STATUS *Status [O] returns RPC_S_OK
218  * 
219  * RETURNS
220  *    -1  if Uuid1 is less than Uuid2
221  *     0  if Uuid1 and Uuid2 are equal
222  *     1  if Uuid1 is greater than Uuid2
223  */
224 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
225 {
226   int i;
227
228   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
229
230   *Status = RPC_S_OK;
231
232   if (!Uuid1) Uuid1 = &uuid_nil;
233   if (!Uuid2) Uuid2 = &uuid_nil;
234
235   if (Uuid1 == Uuid2) return 0;
236
237   if (Uuid1->Data1 != Uuid2->Data1)
238     return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
239
240   if (Uuid1->Data2 != Uuid2->Data2)
241     return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
242
243   if (Uuid1->Data3 != Uuid2->Data3)
244     return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
245
246   for (i = 0; i < 8; i++) {
247     if (Uuid1->Data4[i] < Uuid2->Data4[i])
248       return -1;
249     if (Uuid1->Data4[i] > Uuid2->Data4[i])
250       return 1;
251   }
252
253   return 0;
254 }
255
256 /*************************************************************************
257  * UuidEqual [RPCRT4.@]
258  *
259  * PARAMS
260  *     UUID *Uuid1        [I] Uuid to compare
261  *     UUID *Uuid2        [I] Uuid to compare
262  *     RPC_STATUS *Status [O] returns RPC_S_OK
263  *
264  * RETURNS
265  *     TRUE/FALSE
266  */
267 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
268 {
269   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
270   return !UuidCompare(Uuid1, Uuid2, Status);
271 }
272
273 /*************************************************************************
274  * UuidIsNil [RPCRT4.@]
275  *
276  * PARAMS
277  *     UUID *Uuid         [I] Uuid to compare
278  *     RPC_STATUS *Status [O] retuns RPC_S_OK
279  *
280  * RETURNS
281  *     TRUE/FALSE
282  */
283 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
284 {
285   TRACE("(%s)\n", debugstr_guid(Uuid));
286   if (!Uuid) return TRUE;
287   return !UuidCompare(Uuid, &uuid_nil, Status);
288 }
289
290  /*************************************************************************
291  * UuidCreateNil [RPCRT4.@]
292  *
293  * PARAMS
294  *     UUID *Uuid [O] returns a nil UUID
295  *
296  * RETURNS
297  *     RPC_S_OK
298  */
299 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
300 {
301   *Uuid = uuid_nil;
302   return RPC_S_OK;
303 }
304
305 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
306    resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
307 #define TICKS_PER_CLOCK_TICK 1000
308 #define SECSPERDAY  86400
309 #define TICKSPERSEC 10000000
310 /* UUID system time starts at October 15, 1582 */
311 #define SECS_15_OCT_1582_TO_1601  ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
312 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
313
314 static void RPC_UuidGetSystemTime(ULONGLONG *time)
315 {
316     FILETIME ft;
317
318     GetSystemTimeAsFileTime(&ft);
319
320     *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
321     *time += TICKS_15_OCT_1582_TO_1601;
322 }
323
324 /* Assume that a hardware address is at least 6 bytes long */ 
325 #define ADDRESS_BYTES_NEEDED 6
326
327 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
328 {
329     int i;
330     DWORD status = RPC_S_OK;
331
332     ULONG buflen = sizeof(IP_ADAPTER_INFO);
333     PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
334
335     if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
336         HeapFree(GetProcessHeap(), 0, adapter);
337         adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
338     }
339
340     if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
341         for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
342             address[i] = adapter->Address[i];
343         }
344     }
345     /* We can't get a hardware address, just use random numbers.
346        Set the multicast bit to prevent conflicts with real cards. */
347     else {
348         for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
349             address[i] = rand() & 0xff;
350         }
351
352         address[0] |= 0x01;
353         status = RPC_S_UUID_LOCAL_ONLY;
354     }
355
356     HeapFree(GetProcessHeap(), 0, adapter);
357     return status;
358 }
359
360 /*************************************************************************
361  *           UuidCreate   [RPCRT4.@]
362  *
363  * Creates a 128bit UUID.
364  *
365  * RETURNS
366  *
367  *  RPC_S_OK if successful.
368  *  RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
369  *
370  *  FIXME: No compensation for changes across reloading
371  *         this dll or across reboots (e.g. clock going 
372  *         backwards and swapped network cards). The RFC
373  *         suggests using NVRAM for storing persistent 
374  *         values.
375  */
376 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
377 {
378     static int initialised, count;
379
380     ULONGLONG time;
381     static ULONGLONG timelast;
382     static WORD sequence;
383
384     static DWORD status;
385     static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
386
387     EnterCriticalSection(&uuid_cs);
388
389     if (!initialised) {
390         RPC_UuidGetSystemTime(&timelast);
391         count = TICKS_PER_CLOCK_TICK;
392
393         sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
394         sequence &= 0x1fff;
395
396         status = RPC_UuidGetNodeAddress(address);
397         initialised = 1;
398     }
399
400     /* Generate time element of the UUID. Account for going faster
401        than our clock as well as the clock going backwards. */
402     while (1) {
403         RPC_UuidGetSystemTime(&time);
404         if (time > timelast) {
405             count = 0;
406             break;
407         }
408         if (time < timelast) {
409             sequence = (sequence + 1) & 0x1fff;
410             count = 0;
411             break;
412         }
413         if (count < TICKS_PER_CLOCK_TICK) {
414             count++;
415             break;
416         }
417     }
418
419     timelast = time;
420     time += count;
421
422     /* Pack the information into the UUID structure. */
423
424     Uuid->Data1  = (unsigned long)(time & 0xffffffff);
425     Uuid->Data2  = (unsigned short)((time >> 32) & 0xffff);
426     Uuid->Data3  = (unsigned short)((time >> 48) & 0x0fff);
427
428     /* This is a version 1 UUID */
429     Uuid->Data3 |= (1 << 12);
430
431     Uuid->Data4[0]  = sequence & 0xff;
432     Uuid->Data4[1]  = (sequence & 0x3f00) >> 8;
433     Uuid->Data4[1] |= 0x80;
434
435     Uuid->Data4[2] = address[0];
436     Uuid->Data4[3] = address[1];
437     Uuid->Data4[4] = address[2];
438     Uuid->Data4[5] = address[3];
439     Uuid->Data4[6] = address[4];
440     Uuid->Data4[7] = address[5];
441
442     LeaveCriticalSection(&uuid_cs);
443
444     TRACE("%s\n", debugstr_guid(Uuid));
445
446     return status;
447 }
448
449 /*************************************************************************
450  *           UuidCreateSequential   [RPCRT4.@]
451  *
452  * Creates a 128bit UUID.
453  *
454  * RETURNS
455  *
456  *  RPC_S_OK if successful.
457  *  RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
458  *
459  */
460 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
461 {
462    return UuidCreate(Uuid);
463 }
464
465
466 /*************************************************************************
467  *           UuidHash   [RPCRT4.@]
468  *
469  * Generates a hash value for a given UUID
470  *
471  * Code based on FreeDCE implementation
472  *
473  */
474 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
475 {
476   BYTE *data = (BYTE*)uuid;
477   short c0 = 0, c1 = 0, x, y;
478   unsigned int i;
479
480   if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
481
482   TRACE("(%s)\n", debugstr_guid(uuid));
483
484   for (i=0; i<sizeof(UUID); i++) {
485     c0 += data[i];
486     c1 += c0;
487   }
488
489   x = -c1 % 255;
490   if (x < 0) x += 255;
491
492   y = (c1 - c0) % 255;
493   if (y < 0) y += 255;
494
495   *Status = RPC_S_OK;
496   return y*256 + x;
497 }
498
499 /*************************************************************************
500  *           UuidToStringA   [RPCRT4.@]
501  *
502  * Converts a UUID to a string.
503  *
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
506  *
507  * RETURNS
508  *
509  *  S_OK if successful.
510  *  S_OUT_OF_MEMORY if unsucessful.
511  */
512 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
513 {
514   *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
515
516   if(!(*StringUuid))
517     return RPC_S_OUT_OF_MEMORY;
518
519   if (!Uuid) Uuid = &uuid_nil;
520
521   sprintf( (char*)*StringUuid, "%08lx-%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] );
526
527   return RPC_S_OK;
528 }
529
530 /*************************************************************************
531  *           UuidToStringW   [RPCRT4.@]
532  *
533  * Converts a UUID to a string.
534  *
535  *  S_OK if successful.
536  *  S_OUT_OF_MEMORY if unsucessful.
537  */
538 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, unsigned short** StringUuid)
539 {
540   char buf[37];
541
542   if (!Uuid) Uuid = &uuid_nil;
543
544   sprintf(buf, "%08lx-%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] );
549
550   *StringUuid = RPCRT4_strdupAtoW(buf);
551
552   if(!(*StringUuid))
553     return RPC_S_OUT_OF_MEMORY;
554
555   return RPC_S_OK;
556 }
557
558 static const BYTE hex2bin[] =
559 {
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 */
567 };
568
569 /***********************************************************************
570  *              UuidFromStringA (RPCRT4.@)
571  */
572 RPC_STATUS WINAPI UuidFromStringA(unsigned char* s, UUID *uuid)
573 {
574     int i;
575
576     if (!s) return UuidCreateNil( uuid );
577
578     if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
579
580     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
581         return RPC_S_INVALID_STRING_UUID;
582
583     for (i=0; i<36; i++)
584     {
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;
587     }
588
589     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
590
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]];
595
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;
606 }
607
608
609 /***********************************************************************
610  *              UuidFromStringW (RPCRT4.@)
611  */
612 RPC_STATUS WINAPI UuidFromStringW(unsigned short* s, UUID *uuid)
613 {
614     int i;
615
616     if (!s) return UuidCreateNil( uuid );
617
618     if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
619
620     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
621         return RPC_S_INVALID_STRING_UUID;
622
623     for (i=0; i<36; i++)
624     {
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;
627     }
628
629     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
630
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]];
635
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;
646 }
647
648 /***********************************************************************
649  *              DllRegisterServer (RPCRT4.@)
650  */
651
652 HRESULT WINAPI DllRegisterServer( void )
653 {
654     FIXME( "(): stub\n" );
655     return S_OK;
656 }
657
658 BOOL RPCRT4_StartRPCSS(void)
659
660     PROCESS_INFORMATION pi;
661     STARTUPINFOA si;
662     static char cmd[6];
663     BOOL rslt;
664
665     ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
666     ZeroMemory(&si, sizeof(STARTUPINFOA));
667     si.cb = sizeof(STARTUPINFOA);
668
669     /* apparently it's not OK to use a constant string below */
670     CopyMemory(cmd, "rpcss", 6);
671
672     /* FIXME: will this do the right thing when run as a test? */
673     rslt = CreateProcessA(
674         NULL,           /* executable */
675         cmd,            /* command line */
676         NULL,           /* process security attributes */
677         NULL,           /* primary thread security attributes */
678         FALSE,          /* inherit handles */
679         0,              /* creation flags */
680         NULL,           /* use parent's environment */
681         NULL,           /* use parent's current directory */
682         &si,            /* STARTUPINFO pointer */
683         &pi             /* PROCESS_INFORMATION */
684     );
685
686     if (rslt) {
687       CloseHandle(pi.hProcess);
688       CloseHandle(pi.hThread);
689     }
690
691     return rslt;
692 }
693
694 /***********************************************************************
695  *           RPCRT4_RPCSSOnDemandCall (internal)
696  * 
697  * Attempts to send a message to the RPCSS process
698  * on the local machine, invoking it if necessary.
699  * For remote RPCSS calls, use.... your imagination.
700  * 
701  * PARAMS
702  *     msg             [I] pointer to the RPCSS message
703  *     vardata_payload [I] pointer vardata portion of the RPCSS message
704  *     reply           [O] pointer to reply structure
705  *
706  * RETURNS
707  *     TRUE if successful
708  *     FALSE otherwise
709  */
710 BOOL RPCRT4_RPCSSOnDemandCall(PRPCSS_NP_MESSAGE msg, char *vardata_payload, PRPCSS_NP_REPLY reply)
711 {
712     HANDLE client_handle;
713     int i, j = 0;
714
715     TRACE("(msg == %p, vardata_payload == %p, reply == %p)\n", msg, vardata_payload, reply);
716
717     client_handle = RPCRT4_RpcssNPConnect();
718
719     while (!client_handle) {
720         /* start the RPCSS process */
721         if (!RPCRT4_StartRPCSS()) {
722             ERR("Unable to start RPCSS process.\n");
723             return FALSE;
724         }
725         /* wait for a connection (w/ periodic polling) */
726         for (i = 0; i < 60; i++) {
727             Sleep(200);
728             client_handle = RPCRT4_RpcssNPConnect();
729             if (client_handle) break;
730         } 
731         /* we are only willing to try twice */
732         if (j++ >= 1) break;
733     }
734
735     if (!client_handle) {
736         /* no dice! */
737         ERR("Unable to connect to RPCSS process!\n");
738         SetLastError(RPC_E_SERVER_DIED_DNE);
739         return FALSE;
740     }
741
742     /* great, we're connected.  now send the message */
743     if (!RPCRT4_SendReceiveNPMsg(client_handle, msg, vardata_payload, reply)) {
744         ERR("Something is amiss: RPC_SendReceive failed.\n");
745         return FALSE;
746     }
747
748     return TRUE;
749 }
750
751 #define MAX_RPC_ERROR_TEXT 256
752
753 /******************************************************************************
754  * DceErrorInqTextW   (rpcrt4.@)
755  *
756  * Notes
757  * 1. On passing a NULL pointer the code does bomb out.
758  * 2. The size of the required buffer is not defined in the documentation.
759  *    It appears to be 256.
760  * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
761  *    of any value for which it does.
762  * 4. The MSDN documentation currently declares that the second argument is
763  *    unsigned char *, even for the W version.  I don't believe it.
764  */
765 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, unsigned short *buffer)
766 {
767     DWORD count;
768     count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
769                 FORMAT_MESSAGE_IGNORE_INSERTS,
770                 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
771     if (!count)
772     {
773         count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
774                 FORMAT_MESSAGE_IGNORE_INSERTS,
775                 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
776         if (!count)
777         {
778             ERR ("Failed to translate error");
779             return RPC_S_INVALID_ARG;
780         }
781     }
782     return RPC_S_OK;
783 }
784
785 /******************************************************************************
786  * DceErrorInqTextA   (rpcrt4.@)
787  */
788 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, unsigned char *buffer)
789 {
790     RPC_STATUS status;
791     WCHAR bufferW [MAX_RPC_ERROR_TEXT];
792     if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
793     {
794         if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
795                 NULL, NULL))
796         {
797             ERR ("Failed to translate error");
798             status = RPC_S_INVALID_ARG;
799         }
800     }
801     return status;
802 }