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