blob: 0832b8c58c542250bf7da1b03f7539cfdbc9595c [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
Benny Prijonodd859a62005-11-01 16:42:51 +00002 */
3#include <pj/guid.h>
4#include <pj/string.h>
5#include <pj/sock.h>
6#include <windows.h>
7#include <objbase.h>
8#include <pj/os.h>
9
10
11const unsigned PJ_GUID_STRING_LENGTH=32;
12
13PJ_INLINE(void) hex2digit(unsigned value, char *p)
14{
15 static char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
16 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
17 *p++ = hex[ (value & 0xF0) >> 4 ];
18 *p++ = hex[ (value & 0x0F) ];
19}
20
21static void guid_to_str( const GUID *guid, pj_str_t *str )
22{
23 unsigned i;
24 GUID guid_copy;
25 const unsigned char *src = (const unsigned char*)&guid_copy;
26 char *dst = str->ptr;
27
28 pj_memcpy(&guid_copy, guid, sizeof(*guid));
29 guid_copy.Data1 = pj_ntohl(guid_copy.Data1);
30 guid_copy.Data2 = pj_ntohs(guid_copy.Data2);
31 guid_copy.Data3 = pj_ntohs(guid_copy.Data3);
32
33 for (i=0; i<16; ++i) {
34 hex2digit( *src, dst );
35 dst += 2;
36 ++src;
37 }
38 str->slen = 32;
39}
40
41
42PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str)
43{
44 GUID guid;
45
46 PJ_CHECK_STACK();
47
48 CoCreateGuid(&guid);
49 guid_to_str( &guid, str );
50 return str;
51}
52