4 * Copyright 2000 Huw D M Davies for Codeweavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/config.h"
23 #include "wine/port.h"
24 #include "wine/debug.h"
26 #define _TIMEVAL_DEFINED // ReactOS
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
43 #include "wine/unicode.h"
50 #ifdef HAVE_SYS_FILE_H
51 # include <sys/file.h>
53 #ifdef HAVE_SYS_IOCTL_H
54 # include <sys/ioctl.h>
56 #ifdef HAVE_SYS_SOCKET_H
57 # include <sys/socket.h>
59 #ifdef HAVE_SYS_SOCKIO_H
60 # include <sys/sockio.h>
65 #ifdef HAVE_NETINET_IN_H
66 # include <netinet/in.h>
69 WINE_DEFAULT_DEBUG_CHANNEL(ole);
72 static HANDLE master_mutex;
74 HANDLE RPCRT4_GetMasterMutex(void)
79 /***********************************************************************
83 * hinstDLL [I] handle to the DLL's instance
85 * lpvReserved [I] reserved, must be NULL
92 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
95 case DLL_PROCESS_ATTACH:
98 case DLL_PROCESS_DETACH:
105 /* From WINE RPC_Binding.c */
106 LPWSTR RPCRT4_strdupAtoW(LPSTR src)
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);
117 /*************************************************************************
118 * RpcStringFreeA [RPCRT4.@]
120 * Frees a character string allocated by the RPC run-time library.
124 * S_OK if successful.
126 RPC_STATUS WINAPI RpcStringFreeA(unsigned char** String)
128 HeapFree( GetProcessHeap(), 0, *String);
133 /*************************************************************************
134 * RpcStringFreeW [RPCRT4.@]
136 * Frees a character string allocated by the RPC run-time library.
140 * S_OK if successful.
142 RPC_STATUS WINAPI RpcStringFreeW(unsigned short** String)
144 HeapFree( GetProcessHeap(), 0, *String);
149 /*************************************************************************
150 * RpcRaiseException [RPCRT4.@]
152 * Raises an exception.
154 void WINAPI RpcRaiseException(RPC_STATUS exception)
156 /* FIXME: translate exception? */
157 RaiseException(exception, 0, 0, NULL);
160 /*************************************************************************
161 * UuidCompare [RPCRT4.@]
163 * (an educated-guess implementation)
166 * UUID *Uuid1 [I] Uuid to compare
167 * UUID *Uuid2 [I] Uuid to compare
168 * RPC_STATUS *Status [O] returns RPC_S_OK
171 * -1 if Uuid1 is less than Uuid2
172 * 0 if Uuid1 and Uuid2 are equal
173 * 1 if Uuid1 is greater than Uuid2
175 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
177 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
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));
185 /*************************************************************************
186 * UuidEqual [RPCRT4.@]
189 * UUID *Uuid1 [I] Uuid to compare
190 * UUID *Uuid2 [I] Uuid to compare
191 * RPC_STATUS *Status [O] returns RPC_S_OK
196 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
198 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
199 return !UuidCompare(Uuid1, Uuid2, Status);
202 /*************************************************************************
203 * UuidIsNil [RPCRT4.@]
206 * UUID *Uuid [I] Uuid to compare
207 * RPC_STATUS *Status [O] retuns RPC_S_OK
212 int WINAPI UuidIsNil(UUID *uuid, RPC_STATUS *Status)
214 TRACE("(%s)\n", debugstr_guid(uuid));
216 if (!uuid) return TRUE;
217 return !memcmp(uuid, &uuid_nil, sizeof(UUID));
220 /*************************************************************************
221 * UuidCreateNil [RPCRT4.@]
224 * UUID *Uuid [O] returns a nil UUID
229 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
235 /*************************************************************************
236 * gettimeofday for systems that lack it
242 #define EPOCHFILETIME (116444736000000000i64)
244 #define EPOCHFILETIME (116444736000000000LL)
248 int tz_minuteswest; /* minutes W of Greenwich */
249 int tz_dsttime; /* type of dst correction */
252 int gettimeofday(struct timeval *tv, struct timezone *tz)
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);
278 tz->tz_minuteswest = _timezone / 60;
279 tz->tz_dsttime = _daylight;
285 /*************************************************************************
286 * UuidCreate [RPCRT4.@]
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.
295 * S_OK if successful.
297 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
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;
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;
310 struct ifreq ifr, *ifrp;
316 /* Have we already tried to get the MAC address? */
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)
324 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
326 # define max(a,b) ((a) > (b) ? (a) : (b))
328 # define ifreq_size(i) max(sizeof(struct ifreq),\
329 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
331 # define ifreq_size(i) sizeof(struct ifreq)
332 # endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
334 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
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;
345 memset(buf, 0, sizeof(buf));
346 ifc.ifc_len = sizeof(buf);
348 /* get the ifconf interface */
349 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
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;
360 /* loop through the interfaces, looking for a valid one */
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)
369 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
372 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
374 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
376 /* XXX we don't have a way of getting the hardware address */
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])
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;
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 */
413 /* generate time element of GUID */
415 /* Assume that the gettimeofday() has microsecond granularity */
416 #define MAX_ADJUSTMENT 10
419 gettimeofday(&tv, 0);
420 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
421 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
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;
431 } else if ((tv.tv_sec == last.tv_sec) &&
432 (tv.tv_usec == last.tv_usec)) {
433 if (adjustment >= MAX_ADJUSTMENT)
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;
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;
449 /* pack the information into the GUID structure */
451 ((unsigned char*)&Uuid->Data1)[3] = (unsigned char)clock_low;
453 ((unsigned char*)&Uuid->Data1)[2] = (unsigned char)clock_low;
455 ((unsigned char*)&Uuid->Data1)[1] = (unsigned char)clock_low;
457 ((unsigned char*)&Uuid->Data1)[0] = (unsigned char)clock_low;
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;
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;
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;
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];
478 TRACE("%s\n", debugstr_guid(Uuid));
484 /*************************************************************************
485 * UuidCreateSequential [RPCRT4.@]
487 * Creates a 128bit UUID by calling UuidCreate.
488 * New API in Win 2000
490 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
492 return UuidCreate (Uuid);
496 /*************************************************************************
497 * UuidHash [RPCRT4.@]
499 * Generates a hash value for a given UUID
501 * Code based on FreeDCE implementation
504 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
506 BYTE *data = (BYTE*)uuid;
507 short c0 = 0, c1 = 0, x, y;
510 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
512 TRACE("(%s)\n", debugstr_guid(uuid));
514 for (i=0; i<sizeof(UUID); i++) {
529 /*************************************************************************
530 * UuidToStringA [RPCRT4.@]
532 * Converts a UUID to a string.
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
539 * S_OK if successful.
540 * S_OUT_OF_MEMORY if unsucessful.
542 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, unsigned char** StringUuid)
544 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
547 return RPC_S_OUT_OF_MEMORY;
549 if (!Uuid) Uuid = &uuid_nil;
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] );
560 /*************************************************************************
561 * UuidToStringW [RPCRT4.@]
563 * Converts a UUID to a string.
565 * S_OK if successful.
566 * S_OUT_OF_MEMORY if unsucessful.
568 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, unsigned short** StringUuid)
572 if (!Uuid) Uuid = &uuid_nil;
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] );
580 *StringUuid = RPCRT4_strdupAtoW(buf);
583 return RPC_S_OUT_OF_MEMORY;
588 static const BYTE hex2bin[] =
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 */
599 /***********************************************************************
600 * UuidFromStringA (RPCRT4.@)
602 RPC_STATUS WINAPI UuidFromStringA(unsigned char* str, UUID *uuid)
604 BYTE *s = (BYTE *)str;
607 if (!s) return UuidCreateNil( uuid );
609 if (strlen(s) != 36) return RPC_S_INVALID_STRING_UUID;
611 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
612 return RPC_S_INVALID_STRING_UUID;
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;
620 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
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]];
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]];
640 /***********************************************************************
641 * UuidFromStringW (RPCRT4.@)
643 RPC_STATUS WINAPI UuidFromStringW(unsigned short* s, UUID *uuid)
647 if (!s) return UuidCreateNil( uuid );
649 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
651 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
652 return RPC_S_INVALID_STRING_UUID;
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;
660 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
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]];
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]];
679 /***********************************************************************
680 * DllRegisterServer (RPCRT4.@)
683 HRESULT WINAPI RPCRT4_DllRegisterServer( void )
685 FIXME( "(): stub\n" );
689 BOOL RPCRT4_StartRPCSS(void)
691 PROCESS_INFORMATION pi;
696 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
697 ZeroMemory(&si, sizeof(STARTUPINFOA));
698 si.cb = sizeof(STARTUPINFOA);
700 /* apparently it's not OK to use a constant string below */
701 CopyMemory(cmd, "rpcss", 6);
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 */
718 CloseHandle(pi.hProcess);
719 CloseHandle(pi.hThread);