Changeset 1815 for pjproject/trunk/pjlib/src/pj/guid_simple.c
- Timestamp:
- Feb 21, 2008 9:36:34 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/guid_simple.c
r1417 r1815 18 18 */ 19 19 #include <pj/guid.h> 20 #include <pj/assert.h> 21 #include <pj/rand.h> 20 22 #include <pj/os.h> 21 #include <pj/rand.h>22 23 #include <pj/string.h> 23 24 24 PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=20; 25 PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=32; 26 27 static char guid_chars[64]; 25 28 26 29 PJ_DEF(unsigned) pj_GUID_STRING_LENGTH() … … 29 32 } 30 33 31 static void init_ mac_address(unsigned char mac_addr[16])34 static void init_guid_chars(void) 32 35 { 33 unsigned long *ulval1 = (unsigned long*) &mac_addr[0];34 unsigned short *usval1 = (unsigned short*) &mac_addr[4];36 char *p = guid_chars; 37 unsigned i; 35 38 36 *ulval1 = pj_rand(); 37 *usval1 = (unsigned short) pj_rand(); 39 for (i=0; i<10; ++i) 40 *p++ = '0'+i; 41 42 for (i=0; i<26; ++i) { 43 *p++ = 'a'+i; 44 *p++ = 'A'+i; 45 } 46 47 *p++ = '-'; 48 *p++ = '.'; 38 49 } 39 50 40 51 PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) 41 52 { 42 static int guid_initialized; 43 static unsigned pid; 44 static char str_pid[32]; 45 static unsigned char mac_addr[32]; 46 static char str_mac_addr[32]; 47 static unsigned clock_seq; 53 char *p, *end; 48 54 49 55 PJ_CHECK_STACK(); 50 56 51 if (guid_initialized == 0) { 52 pid = pj_getpid(); 53 init_mac_address(mac_addr); 54 clock_seq = 0; 55 56 sprintf(str_pid, "%04x", pid); 57 sprintf(str_mac_addr, "%02x%02x%02x%02x%02x%02x", 58 mac_addr[0], mac_addr[1], mac_addr[2], 59 mac_addr[3], mac_addr[4], mac_addr[5]); 60 61 guid_initialized = 1; 57 if (guid_chars[0] == '\0') { 58 pj_enter_critical_section(); 59 if (guid_chars[0] == '\0') { 60 init_guid_chars(); 61 } 62 pj_enter_critical_section(); 62 63 } 63 64 64 strcpy(str->ptr, str_pid); 65 sprintf(str->ptr+4, "%08x", clock_seq++); 66 pj_memcpy(str->ptr+12, str_mac_addr, 8); 67 str->slen = 20; 65 /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */ 66 pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0); 68 67 68 for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) { 69 /* Assumes rand() only has 16bit randomness */ 70 unsigned short val = pj_rand(); 71 *p++ = guid_chars[(val >> 8) & 63]; 72 *p++ = guid_chars[(val & 0xFF) & 63]; 73 } 74 75 str->slen = PJ_GUID_STRING_LENGTH; 69 76 return str; 70 77 }
Note: See TracChangeset
for help on using the changeset viewer.