C language Implementation UUID generation algorithm (WIN32 version)

Source: Internet
Author: User
Tags string format time interval uuid win32

By definition, the UUID (Universally unique IDentifier, also known as the GUID) is unique in both time and space. To ensure the uniqueness of the space, each UUID uses a 48-bit value to record, typically the computer's network card address. To ensure the uniqueness of the time, each UUID has a 60-bit timestamp (timestamp). This timestamp indicates that since 1582 A.D. (definitely not 1852, this is the COM technology Insider, an error in the 1th edition 89th page of March 1999), the time since October 15 00:00:00:00, is in the 100 nanosecond time interval. 1 nanoseconds (ns) = 10-9 seconds (s). The UUID algorithm is guaranteed to be the only one in approximately A.D. 3400. The C language structure of the UUID is defined as follows:

typedef struct _uuid_t
     unsigned long      data1;
     unsigned short     data2;
     unsigned short     data3;
     unsigned char      data4[8];
} uuid_t;

It has a structure size of 16 bytes. That is, sizeof (uuid_t) ==16 is true. Written as a 16-string format, typically:


The string form above takes up 36 characters, excluding the trailing null character ' ". So, to hold a UUID string, you must declare a char[36+1] character array.

It is very practical to implement UUID with software algorithm. Referring to the RFC4122 documentation and some other open source code, I wrote a WIN32 under the UUID to implement the C language program--uuid32.c. The procedure conforms to the RFC4122 standard. The program not only implements the creation of UUID and UUID String, but also makes character and time comparisons to the UUID. You can also extract the timestamp from the UUID (precision to seconds). The header file uuid32.h is defined as follows:

/* Uuid32.h
2007-09-15 last created by Cheungmine.
Partly rights reserved by Cheungmine.
#ifndef uuid32_h_included
#define Uuid32_h_included

#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <memory.h>

#include "cdatatype.h"

typedef struct _TIMESTAMP_T
BYTE tm_sec; /* Seconds after minute (0–59). */
BYTE tm_min; /* Minutes after hour (0–59). */
BYTE Tm_hour; /* Hours after Midnight (0–23). */
BYTE Tm_mday; /* Day of Month (1–31). */
BYTE Tm_mon; /* Month (0–11; January = 0). */
BYTE Tm_wday; /* Day of week (0–6; Sunday = 0). */
Short tm_year; /* Year (minus 1900). */
Short Tm_yday; /* Day of year (0–365; January 1 = 0). */
Long tm_fraction; /* fraction little than 1 second * *
} timestamp_t;

typedef struct _UUID_T
unsigned long data1;
unsigned short data2;
unsigned short data3;
unsigned char data4[8];
} uuid_t;

* Checks whether the given string matches the UUID format.
* Params:
* [in] uuid-the potential UUID string
* Return
* TRUE If the given string is a UUID, FALSE otherwise
BOOL is_uuid_string (const char *uuid);

* Generates a new UUID. The UUID is a time-based time 1 uuid.
* A Random per-process node identifier is used to avoid keeping global
* State and maintaining inter-process synchronization.
void Uuid_create (uuid_t* uuid);

* Generates a new UUID string. The returned UUID is a time-based time 1 uuid.
* A Random per-process node identifier is used to avoid keeping global
* State and maintaining inter-process synchronization.
* Return UUID string (newly allocated)
Char *uuid_create_string (void);

* Generates a name-based (Type 3) UUID string from the given external
* identifier. The special namespace UUID is used as the namespace of
* The generated UUID.
* Params
* [in] external-the external identifier
* Return
* UUID string (Newly allocated)
void uuid_create_external (const char *external, uuid_t* uuid);

* Translate a uuid_t to a UUID string
* Return UUID string
Char *uuid_to_string (const uuid_t* UUID);

* Get timestamp from a UUID
void Uuid_to_timestamp (const uuid_t* UUID, timestamp_t* time);

* Resurn a description of timestamp not including fraction
char* timestamp_to_string (const timestamp_t* time);

* Compare two UUID ' s lexically
* Return
*-1 U1 is lexically before U2
* 0 U1 is equal to U2
* 1 U1 is lexically after U2
int Uuid_compare (const uuid_t *U1, const uuid_t *U2);

* Compare two UUID ' s temporally
* Return
*-1 U1 is temporally before U2
* 0 U1 is equal to U2
* 1 U1 is temporally after U2
int uuid_compare_time (const uuid_t *U1, const uuid_t *U2);

#endif/* uuid32_h_included * *

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.