update for HEAD-2003091401
[reactos.git] / lib / 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  */
21
22 #include "wine/config.h"
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #define _TIMEVAL_DEFINED // ReactOS
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 #include <stdarg.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39
40 #include "windef.h"
41 #include "winerror.h"
42 #include "winbase.h"
43 #include "wine/unicode.h"
44 #include "rpc.h"
45
46 #include "ole2.h"
47 #include "rpcndr.h"
48 #include "rpcproxy.h"
49
50 #ifdef HAVE_SYS_FILE_H
51 # include <sys/file.h>
52 #endif
53 #ifdef HAVE_SYS_IOCTL_H
54 # include <sys/ioctl.h>
55 #endif
56 #ifdef HAVE_SYS_SOCKET_H
57 # include <sys/socket.h>
58 #endif
59 #ifdef HAVE_SYS_SOCKIO_H
60 # include <sys/sockio.h>
61 #endif
62 #ifdef HAVE_NET_IF_H
63 # include <net/if.h>
64 #endif
65 #ifdef HAVE_NETINET_IN_H
66 # include <netinet/in.h>
67 #endif
68
69 WINE_DEFAULT_DEBUG_CHANNEL(ole);
70
71 static UUID uuid_nil;
72 static HANDLE master_mutex;
73
74 HANDLE RPCRT4_GetMasterMutex(void)
75 {
76     return master_mutex;
77 }
78
79 /***********************************************************************
80  * DllMain
81  *
82  * PARAMS
83  *     hinstDLL    [I] handle to the DLL's instance
84  *     fdwReason   [I]
85  *     lpvReserved [I] reserved, must be NULL
86  *
87  * RETURNS
88  *     Success: TRUE
89  *     Failure: FALSE
90  */
91
92 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
93 {
94     switch (fdwReason) {
95     case DLL_PROCESS_ATTACH:
96     break;
97
98     case DLL_PROCESS_DETACH:
99     break;
100     }
101
102     return TRUE;
103 }
104
105 /* From WINE RPC_Binding.c */
106 LPWSTR RPCRT4_strdupAtoW(LPSTR src)
107 {
108   DWORD len;
109   LPWSTR s;
110   if (!src) return NULL;
111   len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
112   s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
113   MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
114   return s;
115 }
116
117 /*************************************************************************
118  *           RpcStringFreeA   [RPCRT4.@]
119  *
120  * Frees a character string allocated by the RPC run-time library.
121  *
122  * RETURNS
123  *
124  *  S_OK if successful.
125  */
126 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
127 {
128   HeapFree( GetProcessHeap(), 0, *String);
129
130   return RPC_S_OK;
131 }
132
133 /*************************************************************************
134  *           RpcStringFreeW   [RPCRT4.@]
135  *
136  * Frees a character string allocated by the RPC run-time library.
137  *
138  * RETURNS
139  *
140  *  S_OK if successful.
141  */
142 RPC_STATUS WINAPI RpcStringFreeW(unsigned short** String)
143 {
144   HeapFree( GetProcessHeap(), 0, *String);
145
146   return RPC_S_OK;
147 }
148
149 /*************************************************************************
150  *           RpcRaiseException   [RPCRT4.@]
151  *
152  * Raises an exception.
153  */
154 void WINAPI RpcRaiseException(RPC_STATUS exception)
155 {
156   /* FIXME: translate exception? */
157   RaiseException(exception, 0, 0, NULL);
158 }
159
160 /*************************************************************************
161  * UuidCompare [RPCRT4.@]
162  *
163  * (an educated-guess implementation)
164  *
165  * PARAMS
166  *     UUID *Uuid1        [I] Uuid to compare
167  *     UUID *Uuid2        [I] Uuid to compare
168  *     RPC_STATUS *Status [O] returns RPC_S_OK
169  * 
170  * RETURNS
171  *     -1 if Uuid1 is less than Uuid2
172  *     0  if Uuid1 and Uuid2 are equal
173  *     1  if Uuid1 is greater than Uuid2
174  */
175 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
176 {
177   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
178   *Status = RPC_S_OK;
179   if (!Uuid1) Uuid1 = &uuid_nil;
180   if (!Uuid2) Uuid2 = &uuid_nil;
181   if (Uuid1 == Uuid2) return 0;
182   return memcmp(Uuid1, Uuid2, sizeof(UUID));
183 }
184
185 /*************************************************************************
186  * UuidEqual [RPCRT4.@]
187  *
188  * PARAMS
189  *     UUID *Uuid1        [I] Uuid to compare
190  *     UUID *Uuid2        [I] Uuid to compare
191  *     RPC_STATUS *Status [O] returns RPC_S_OK
192  *
193  * RETURNS
194  *     TRUE/FALSE
195  */
196 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
197 {
198   TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
199   return !UuidCompare(Uuid1, Uuid2, Status);
200 }
201
202 /*************************************************************************
203  * UuidIsNil [RPCRT4.@]
204  *
205  * PARAMS
206  *     UUID *Uuid         [I] Uuid to compare
207  *     RPC_STATUS *Status [O] retuns RPC_S_OK
208  *
209  * RETURNS
210  *     TRUE/FALSE
211  */
212 int WINAPI UuidIsNil(UUID *uuid, RPC_STATUS *Status)
213 {
214   TRACE("(%s)\n", debugstr_guid(uuid));
215   *Status = RPC_S_OK;
216   if (!uuid) return TRUE;
217   return !memcmp(uuid, &uuid_nil, sizeof(UUID));
218 }
219
220  /*************************************************************************
221  * UuidCreateNil [RPCRT4.@]
222  *
223  * PARAMS
224  *     UUID *Uuid [O] returns a nil UUID
225  *
226  * RETURNS
227  *     RPC_S_OK
228  */
229 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
230 {
231   *Uuid = uuid_nil;
232   return RPC_S_OK;
233 }
234
235  /*************************************************************************
236  * gettimeofday for systems that lack it
237  *
238  * By Wu Yongwei
239  */
240
241 #ifndef __GNUC__
242 #define EPOCHFILETIME (116444736000000000i64)
243 #else
244 #define EPOCHFILETIME (116444736000000000LL)
245 #endif
246
247 struct timezone {
248     int tz_minuteswest; /* minutes W of Greenwich */
249     int tz_dsttime;     /* type of dst correction */
250 };
251
252 int gettimeofday(struct timeval *tv, struct timezone *tz)
253 {
254     FILETIME        ft;
255     LARGE_INTEGER   li;
256     __int64         t;
257     static int      tzflag;
258
259     if (tv)
260     {
261         GetSystemTimeAsFileTime(&ft);
262         li.LowPart  = ft.dwLowDateTime;
263         li.HighPart = ft.dwHighDateTime;
264         t  = li.QuadPart;       /* In 100-nanosecond intervals */
265         t -= EPOCHFILETIME;     /* Offset to the Epoch time */
266         t /= 10;                /* In microseconds */
267         tv->tv_sec  = (long)(t / 1000000);
268         tv->tv_usec = (long)(t % 1000000);
269     }
270
271     if (tz)
272     {
273         if (!tzflag)
274         {
275             _tzset();
276             tzflag++;
277         }
278         tz->tz_minuteswest = _timezone / 60;
279         tz->tz_dsttime = _daylight;
280     }
281
282     return 0;
283 }
284
285 /*************************************************************************
286  *           UuidCreate   [RPCRT4.@]
287  *
288  * Creates a 128bit UUID.
289  * Implemented according the DCE specification for UUID generation.
290  * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
291  * Copyright (C) 1996, 1997 Theodore Ts'o.
292  *
293  * RETURNS
294  *
295  *  S_OK if successful.
296  */
297 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
298 {
299    static char has_init = 0;
300    static unsigned char a[6];
301    static int                      adjustment = 0;
302    static struct timeval           last = {0, 0};
303    static WORD                     clock_seq;
304    struct timeval                  tv;
305    unsigned long long              clock_reg;
306    DWORD clock_high, clock_low;
307    WORD temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
308 #ifdef HAVE_NET_IF_H
309    int             sd;
310    struct ifreq    ifr, *ifrp;
311    struct ifconf   ifc;
312    char buf[1024];
313    int             n, i;
314 #endif
315
316    /* Have we already tried to get the MAC address? */
317    if (!has_init) {
318 #ifdef HAVE_NET_IF_H
319       /* BSD 4.4 defines the size of an ifreq to be
320        * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
321        * However, under earlier systems, sa_len isn't present, so
322        *  the size is just sizeof(struct ifreq)
323        */
324 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
325 #  ifndef max
326 #   define max(a,b) ((a) > (b) ? (a) : (b))
327 #  endif
328 #  define ifreq_size(i) max(sizeof(struct ifreq),\
329 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
330 # else
331 #  define ifreq_size(i) sizeof(struct ifreq)
332 # endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
333
334       sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
335       if (sd < 0) {
336          /* if we can't open a socket, just use random numbers */
337          /* set the multicast bit to prevent conflicts with real cards */
338          a[0] = (rand() & 0xff) | 0x80;
339          a[1] = rand() & 0xff;
340          a[2] = rand() & 0xff;
341          a[3] = rand() & 0xff;
342          a[4] = rand() & 0xff;
343          a[5] = rand() & 0xff;
344       } else {
345          memset(buf, 0, sizeof(buf));
346          ifc.ifc_len = sizeof(buf);
347          ifc.ifc_buf = buf;
348          /* get the ifconf interface */
349          if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
350             close(sd);
351             /* no ifconf, so just use random numbers */
352             /* set the multicast bit to prevent conflicts with real cards */
353             a[0] = (rand() & 0xff) | 0x80;
354             a[1] = rand() & 0xff;
355             a[2] = rand() & 0xff;
356             a[3] = rand() & 0xff;
357             a[4] = rand() & 0xff;
358             a[5] = rand() & 0xff;
359          } else {
360             /* loop through the interfaces, looking for a valid one */
361             n = ifc.ifc_len;
362             for (i = 0; i < n; i+= ifreq_size(ifr) ) {
363                ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
364                strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
365                /* try to get the address for this interface */
366 # ifdef SIOCGIFHWADDR
367                if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
368                    continue;
369                memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
370 # else
371 #  ifdef SIOCGENADDR
372                if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
373                    continue;
374                memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
375 #  else
376                /* XXX we don't have a way of getting the hardware address */
377                close(sd);
378                a[0] = 0;
379                break;
380 #  endif /* SIOCGENADDR */
381 # endif /* SIOCGIFHWADDR */
382                /* make sure it's not blank */
383                if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
384                    continue;
385
386                goto valid_address;
387             }
388             /* if we didn't find a valid address, make a random one */
389             /* once again, set multicast bit to avoid conflicts */
390             a[0] = (rand() & 0xff) | 0x80;
391             a[1] = rand() & 0xff;
392             a[2] = rand() & 0xff;
393             a[3] = rand() & 0xff;
394             a[4] = rand() & 0xff;
395             a[5] = rand() & 0xff;
396
397             valid_address:
398             close(sd);
399          }
400       }
401 #else
402       /* no networking info, so generate a random address */
403       a[0] = (rand() & 0xff) | 0x80;
404       a[1] = rand() & 0xff;
405       a[2] = rand() & 0xff;
406       a[3] = rand() & 0xff;
407       a[4] = rand() & 0xff;
408       a[5] = rand() & 0xff;
409 #endif /* HAVE_NET_IF_H */
410       has_init = 1;
411    }
412
413    /* generate time element of GUID */
414
415    /* Assume that the gettimeofday() has microsecond granularity */
416 #define MAX_ADJUSTMENT 10
417
418    try_again:
419    gettimeofday(&tv, 0);
420    if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
421       clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
422       clock_seq &= 0x1FFF;
423       last = tv;
424       last.tv_sec--;
425    }
426    if ((tv.tv_sec < last.tv_sec) ||
427        ((tv.tv_sec == last.tv_sec) &&
428         (tv.tv_usec < last.tv_usec))) {
429       clock_seq = (clock_seq+1) & 0x1FFF;
430       adjustment = 0;
431    } else if ((tv.tv_sec == last.tv_sec) &&
432               (tv.tv_usec == last.tv_usec)) {
433       if (adjustment >= MAX_ADJUSTMENT)
434           goto try_again;
435       adjustment++;
436    } else
437        adjustment = 0;
438
439    clock_reg = tv.tv_usec*10 + adjustment;
440    clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
441    clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
442
443    clock_high = clock_reg >> 32;
444    clock_low = clock_reg;
445    temp_clock_seq = clock_seq | 0x8000;
446    temp_clock_mid = (WORD)clock_high;
447    temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
448
449    /* pack the information into the GUID structure */
450
451    ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
452    clock_low >>= 8;
453    ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
454    clock_low >>= 8;
455    ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
456    clock_low >>= 8;
457    ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
458
459    ((unsigned char*)&Uuid->Data2)[1] = (unsigned char)temp_clock_mid;
460    temp_clock_mid >>= 8;
461    ((unsigned char*)&Uuid->Data2)[0] = (unsigned char)temp_clock_mid;
462
463    ((unsigned char*)&Uuid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
464    temp_clock_hi_and_version >>= 8;
465    ((unsigned char*)&Uuid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
466
467    ((unsigned char*)Uuid->Data4)[1] = (unsigned char)temp_clock_seq;
468    temp_clock_seq >>= 8;
469    ((unsigned char*)Uuid->Data4)[0] = (unsigned char)temp_clock_seq;
470
471    ((unsigned char*)Uuid->Data4)[2] = a[0];
472    ((unsigned char*)Uuid->Data4)[3] = a[1];
473    ((unsigned char*)Uuid->Data4)[4] = a[2];
474    ((unsigned char*)Uuid->Data4)[5] = a[3];
475    ((unsigned char*)Uuid->Data4)[6] = a[4];
476    ((unsigned char*)Uuid->Data4)[7] = a[5];
477
478    TRACE("%s\n", debugstr_guid(Uuid));
479
480    return RPC_S_OK;
481 }
482
483
484 /*************************************************************************
485  *           UuidCreateSequential   [RPCRT4.@]
486  *
487  * Creates a 128bit UUID by calling UuidCreate.
488  * New API in Win 2000
489  */
490 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
491 {
492    return UuidCreate (Uuid);
493 }
494
495
496 /*************************************************************************
497  *           UuidHash   [RPCRT4.@]
498  *
499  * Generates a hash value for a given UUID
500  *
501  * Code based on FreeDCE implementation
502  *
503  */
504 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
505 {
506   BYTE *data = (BYTE*)uuid;
507   short c0 = 0, c1 = 0, x, y;
508   int i;
509
510   if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
511
512   TRACE("(%s)\n", debugstr_guid(uuid));
513
514   for (i=0; i<sizeof(UUID); i++) {
515     c0 += data[i];
516     c1 += c0;
517   }
518
519   x = -c1 % 255;
520   if (x < 0) x += 255;
521
522   y = (c1 - c0) % 255;
523   if (y < 0) y += 255;
524
525   *Status = RPC_S_OK;
526   return y*256 + x;
527 }
528
529 /*************************************************************************
530  *           UuidToStringA   [RPCRT4.@]
531  *
532  * Converts a UUID to a string.
533  *
534  * UUID format is 8 hex digits, followed by a hyphen then three groups of
535  * 4 hex digits each followed by a hyphen and then 12 hex digits
536  *
537  * RETURNS
538  *
539  *  S_OK if successful.
540  *  S_OUT_OF_MEMORY if unsucessful.
541  */
542 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
543 {
544   *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
545
546   if(!(*StringUuid))
547     return RPC_S_OUT_OF_MEMORY;
548
549   if (!Uuid) Uuid = &uuid_nil;
550
551   sprintf(*StringUuid, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
552                  Uuid->Data1, Uuid->Data2, Uuid->Data3,
553                  Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
554                  Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
555                  Uuid->Data4[6], Uuid->Data4[7] );
556
557   return RPC_S_OK;
558 }
559
560 /*************************************************************************
561  *           UuidToStringW   [RPCRT4.@]
562  *
563  * Converts a UUID to a string.
564  *
565  *  S_OK if successful.
566  *  S_OUT_OF_MEMORY if unsucessful.
567  */
568 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, unsigned short** StringUuid)
569 {
570   char buf[37];
571
572   if (!Uuid) Uuid = &uuid_nil;
573
574   sprintf(buf, "%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
575                Uuid->Data1, Uuid->Data2, Uuid->Data3,
576                Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
577                Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
578                Uuid->Data4[6], Uuid->Data4[7] );
579
580   *StringUuid = RPCRT4_strdupAtoW(buf);
581
582   if(!(*StringUuid))
583     return RPC_S_OUT_OF_MEMORY;
584
585   return RPC_S_OK;
586 }
587
588 static const BYTE hex2bin[] =
589 {
590     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x00 */
591     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x10 */
592     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x20 */
593     0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,        /* 0x30 */
594     0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,  /* 0x40 */
595     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,        /* 0x50 */
596     0,10,11,12,13,14,15                     /* 0x60 */
597 };
598
599 /***********************************************************************
600  *              UuidFromStringA (RPCRT4.@)
601  */
602 RPC_STATUS WINAPI UuidFromStringA(unsigned char* str, UUID *uuid)
603 {
604     BYTE *s = (BYTE *)str;
605     int i;
606
607     if (!s) return UuidCreateNil( uuid );
608
609     if (strlen(s) != 36) return RPC_S_INVALID_STRING_UUID;
610
611     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
612         return RPC_S_INVALID_STRING_UUID;
613
614     for (i=0; i<36; i++)
615     {
616         if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
617         if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
618     }
619
620     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
621
622     uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
623                    hex2bin[s[4]] << 12 | hex2bin[s[5]]  << 8 | hex2bin[s[6]]  << 4 | hex2bin[s[7]]);
624     uuid->Data2 =  hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
625     uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
626
627     /* these are just sequential bytes */
628     uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
629     uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
630     uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
631     uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
632     uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
633     uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
634     uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
635     uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
636     return RPC_S_OK;
637 }
638
639
640 /***********************************************************************
641  *              UuidFromStringW (RPCRT4.@)
642  */
643 RPC_STATUS WINAPI UuidFromStringW(unsigned short* s, UUID *uuid)
644 {
645     int i;
646
647     if (!s) return UuidCreateNil( uuid );
648
649     if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
650
651     if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
652         return RPC_S_INVALID_STRING_UUID;
653
654     for (i=0; i<36; i++)
655     {
656         if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
657         if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
658     }
659
660     /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
661
662     uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
663                    hex2bin[s[4]] << 12 | hex2bin[s[5]]  << 8 | hex2bin[s[6]]  << 4 | hex2bin[s[7]]);
664     uuid->Data2 =  hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
665     uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
666
667     /* these are just sequential bytes */
668     uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
669     uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
670     uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
671     uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
672     uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
673     uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
674     uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
675     uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
676     return RPC_S_OK;
677 }
678
679 /***********************************************************************
680  *              DllRegisterServer (RPCRT4.@)
681  */
682
683 HRESULT WINAPI RPCRT4_DllRegisterServer( void )
684 {
685     FIXME( "(): stub\n" );
686     return S_OK;
687 }
688
689 BOOL RPCRT4_StartRPCSS(void)
690
691     PROCESS_INFORMATION pi;
692     STARTUPINFOA si;
693     static char cmd[6];
694     BOOL rslt;
695
696     ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
697     ZeroMemory(&si, sizeof(STARTUPINFOA));
698     si.cb = sizeof(STARTUPINFOA);
699
700     /* apparently it's not OK to use a constant string below */
701     CopyMemory(cmd, "rpcss", 6);
702
703     /* FIXME: will this do the right thing when run as a test? */
704     rslt = CreateProcessA(
705         NULL,           /* executable */
706         cmd,            /* command line */
707         NULL,           /* process security attributes */
708         NULL,           /* primary thread security attributes */
709         FALSE,          /* inherit handles */
710         0,              /* creation flags */
711         NULL,           /* use parent's environment */
712         NULL,           /* use parent's current directory */
713         &si,            /* STARTUPINFO pointer */
714         &pi             /* PROCESS_INFORMATION */
715     );
716
717     if (rslt) {
718       CloseHandle(pi.hProcess);
719       CloseHandle(pi.hThread);
720     }
721
722     return rslt;
723 }