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