Changeset 3716

Show
Ignore:
Timestamp:
02/21/06 23:10:09 (3 years ago)
Author:
morris
Message:

Add wi_uuid_init_with_string()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/data/wi-uuid.c

    r3696 r3716  
    6868#include <netinet/in.h> 
    6969#include <net/if.h> 
     70#include <inttypes.h> 
    7071 
    7172#ifdef HAVE_NET_IF_DL_H 
     
    109110        wi_string_t                                                     *string; 
    110111 
    111         unsigned int                                          time_low; 
    112         unsigned short                                                time_mid; 
    113         unsigned short                                                time_hi_and_version; 
    114         unsigned short                                                clock_seq; 
    115         unsigned char                                         node[_WI_UUID_NODE_SIZE]; 
     112        uint32_t                                                      time_low; 
     113        uint16_t                                                      time_mid; 
     114        uint16_t                                                      time_hi_and_version; 
     115        uint16_t                                                      clock_seq; 
     116        uint8_t                                                               node[_WI_UUID_NODE_SIZE]; 
    116117}; 
    117118 
     
    127128static void                                                             _wi_uuid_get_random_buffer(void *, size_t); 
    128129static wi_boolean_t                                             _wi_uuid_get_node(unsigned char *); 
    129 static void                                                             _wi_uuid_get_clock(unsigned int *, unsigned int *, unsigned short *); 
     130static void                                                             _wi_uuid_get_clock(uint32_t *, uint32_t *, uint16_t *); 
    130131 
    131132 
     
    154155void wi_uuid_initialize(void) { 
    155156        struct timeval          tv; 
    156         unsigned int          i; 
     157        uint32_t                      i; 
    157158 
    158159        _wi_uuid_clock_lock = wi_lock_init(wi_lock_alloc()), 
     
    222223 
    223224wi_uuid_t * wi_uuid_init_from_time(wi_uuid_t *uuid) { 
    224         unsigned int   clock_mid, clock_low; 
    225         unsigned short        clock_seq; 
    226          
    227         _wi_uuid_get_clock(&clock_mid, &clock_low, &clock_seq); 
    228  
    229         uuid->time_low                          = clock_low; 
    230         uuid->time_mid                          = (unsigned short) clock_mid; 
    231         uuid->time_hi_and_version       = ((clock_mid >> 16) & 0x0FFFF) | 0x1000; 
     225        uint32_t               time_mid, time_low; 
     226        uint16_t              clock_seq; 
     227         
     228        _wi_uuid_get_clock(&time_mid, &time_low, &clock_seq); 
     229 
     230        uuid->time_low                          = time_low; 
     231        uuid->time_mid                          = (uint16_t) time_mid; 
     232        uuid->time_hi_and_version       = ((time_mid >> 16) & 0x0FFFF) | 0x1000; 
    232233        uuid->clock_seq                         = clock_seq | 0x8000; 
    233234         
     
    236237        _wi_uuid_pack_buffer(uuid); 
    237238 
     239        return uuid; 
     240} 
     241 
     242 
     243 
     244wi_uuid_t * wi_uuid_init_with_string(wi_uuid_t *uuid, wi_string_t *string) { 
     245        uint32_t                time_low, time_mid, time_hi_and_version, clock_seq_high, clock_seq_low; 
     246        uint32_t                node[6]; 
     247         
     248        if(sscanf(wi_string_cstring(string), "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", 
     249                          &time_low, 
     250                          &time_mid, 
     251                          &time_hi_and_version, 
     252                          &clock_seq_high, 
     253                          &clock_seq_low, 
     254                          &node[0], 
     255                          &node[1], 
     256                          &node[2], 
     257                          &node[3], 
     258                          &node[4], 
     259                          &node[5]) != 11) { 
     260                wi_release(uuid); 
     261                 
     262                return NULL; 
     263        } 
     264         
     265        uuid->time_low                          = time_low; 
     266        uuid->time_mid                          = (uint16_t) time_mid; 
     267        uuid->time_hi_and_version       = (uint16_t) time_hi_and_version; 
     268        uuid->clock_seq                         = (clock_seq_high << 8) + (clock_seq_low & 0xFF); 
     269        uuid->node[0]                           = (uint8_t) node[0]; 
     270        uuid->node[1]                           = (uint8_t) node[1]; 
     271        uuid->node[2]                           = (uint8_t) node[2]; 
     272        uuid->node[3]                           = (uint8_t) node[3]; 
     273        uuid->node[4]                           = (uint8_t) node[4]; 
     274        uuid->node[5]                           = (uint8_t) node[5]; 
     275         
     276        _wi_uuid_pack_buffer(uuid); 
     277         
    238278        return uuid; 
    239279} 
     
    243283wi_uuid_t * wi_uuid_init_with_buffer(wi_uuid_t *uuid, const void *buffer) { 
    244284        const unsigned char             *p; 
    245         unsigned int                  i; 
     285        uint32_t                              i; 
    246286         
    247287        memcpy(uuid->buffer, buffer, WI_UUID_BUFFER_SIZE); 
     
    343383static void _wi_uuid_pack_buffer(wi_uuid_t *uuid) { 
    344384        unsigned char           *p; 
    345         unsigned int          i; 
     385        uint32_t                      i; 
    346386         
    347387        p = uuid->buffer; 
     
    378418static void _wi_uuid_get_random_buffer(void *buffer, size_t size) { 
    379419        unsigned char           *p = buffer; 
    380         unsigned int          i; 
     420        uint32_t                      i; 
    381421         
    382422        if(_wi_uuid_random_fd >= 0) 
     
    459499 
    460500 
    461 static void _wi_uuid_get_clock(unsigned int *clock_high, unsigned int *clock_low, unsigned short *clock_seq) { 
     501static void _wi_uuid_get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *clock_seq) { 
    462502        static struct timeval   lasttv; 
    463         static unsigned int           adjustment; 
    464         static unsigned short sequence; 
    465         unsigned long long            clock_reg; 
     503        static uint32_t                       adjustment; 
     504        static uint16_t                       sequence; 
     505        uint64_t                              clock_reg; 
    466506        struct timeval                  tv; 
    467507        wi_boolean_t                    tryagain; 
     
    503543                 
    504544        clock_reg       = (tv.tv_usec * 10) + adjustment; 
    505         clock_reg       += ((unsigned long long) tv.tv_sec) * 10000000; 
    506         clock_reg       += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000; 
     545        clock_reg       += ((uint64_t) tv.tv_sec) * 10000000; 
     546        clock_reg       += (((uint64_t) 0x01B21DD2) << 32) + 0x13814000; 
    507547 
    508548        *clock_high     = clock_reg >> 32; 
  • libwired/trunk/libwired/data/wi-uuid.h

    r3696 r3716  
    4848WI_EXPORT wi_uuid_t *                           wi_uuid_init_from_random_data(wi_uuid_t *); 
    4949WI_EXPORT wi_uuid_t *                           wi_uuid_init_from_time(wi_uuid_t *); 
     50WI_EXPORT wi_uuid_t *                           wi_uuid_init_with_string(wi_uuid_t *, wi_string_t *); 
    5051WI_EXPORT wi_uuid_t *                           wi_uuid_init_with_buffer(wi_uuid_t *, const void *); 
    5152