/* uuid32.c
2007-09-15 Last created by cheungmine.
Partly rights reserved by cheungmine.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include "uuid32.h"
#include "md5.h"
#define MD5_LEN 16
#define UUID_LEN 36
/* microsecond per second. 1s=1000000us=1000000000ns*/
#define NSec100_Per_Sec 10000000
#define USec_Per_Sec 1000000
#define USec_Per_MSec 1000
#define NSec_Since_1582 ((uint64)(0x01B21DD213814000))
/*========================================================================================
Private Functions
========================================================================================*/
static BOOL isbigendian()
{
int c = 1;
return ( *((unsigned char *) &c) == 1 )? FALSE: TRUE;
};
static void swap_word( int size_bytes, void * ptr_word )
{
int i;
unsigned char temp;
for( i=0; i < size_bytes/2; i++ )
{
temp = ((unsigned char *) ptr_word)[i];
((unsigned char *) ptr_word)[i] = ((unsigned char *) ptr_word)[size_bytes-i-1];
((unsigned char *) ptr_word)[size_bytes-i-1] = temp;
}
};
static void write_word( unsigned char* stream, word_t val )
{
memcpy(stream, &val, 2);
if( isbigendian() ) swap_word( 2, stream );
};
static void write_dword( unsigned char* stream, dword_t val )
{
memcpy(stream, &val, 4);
if( isbigendian() ) swap_word( 4, stream );
};
static void read_word( const unsigned char* stream, word_t* val )
{
memcpy( val, stream, 2 );
if( isbigendian() ) swap_word( 2, val );
};
static void read_dword( const unsigned char* stream, dword_t* val )
{
memcpy( val, stream, 4 );
if( isbigendian() ) swap_word( 4, val );
};
static BOOL is_xdigit(char c)
{
/* isxdigit returns a non-zero value if c is a hexadecimal digit (A 每 F, a 每 f, or 0 每 9). */
return ((c>='A'&&c<='F')||(c>='a'&&c<='f')||(c>='0'&&c<='9'))? TRUE : FALSE;
};
/* make a pseudorandom numbel based on current time*/
static int pseudo_rand()
{
#ifdef _USE_32BIT_TIME_T
assert(0);
#endif
struct _timeb timebuf;
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
_ftime(&timebuf);
#pragma warning(pop) /* C4996 */
srand((uint32) ((((uint32)timebuf.time&0xFFFF)+(uint32)timebuf.millitm)^(uint32)timebuf.millitm));
return rand();
};
/*========================================================================================
Public Functions
========================================================================================*/
BOOL is_uuid_string(const char *uuid)
{
static const char fmt[] = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
int i;
assert(uuid != NULL);
for (i = 0; i < sizeof(fmt); i++)
if (fmt[i] == 'x')
if (!is_xdigit(uuid[i]))
return FALSE;
else if (uuid[i] != fmt[i])
return FALSE;
return TRUE;
}
/**
* internal
* ingroup uuid
* The thread synchronization lock used to guarantee UUID uniqueness
* for all the threads running within a process.
*/
void uuid_create(uuid_t* u)
{
static BOOL initialized = FALSE;
static int64 timestamp;
static uint32 advance;
static uint16 clockseq;
static uint16 node_high;
static uint32 node_low;
int64 time; /* unit of 100ns */
uint16 nowseq;
int r;
#ifdef _USE_32BIT_TIME_T
assert(0);
#endif
struct _timeb tv;
assert(u);
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
_ftime(&tv);
#pragma warning(pop) /* C4996 */
/* time is counter of 100ns time interval since Oct.15, 1582 (NOT 1852) */
time = ((uint64) tv.time) * USec_Per_Sec + ((uint64) tv.millitm*USec_Per_MSec);
time = time * 10 + NSec_Since_1582;
if (!initialized)
{
timestamp = time;
advance = 0;
r = pseudo_rand();
clockseq = r >> 16;
node_high = r | 0x0100;
node_low = pseudo_rand();
initialized = TRUE;
}
else if (time < timestamp)
{
timestamp = time;
advance = 0;
clockseq++;
}
else if (time == timestamp)
{
advance++;
time += advance;
}
else
{
timestamp = time;
advance = 0;
}
nowseq = clockseq;
assert(u);
u->data1 = (dword_t) time;
u->data2 = (word_t) ((time >> 32) & 0xffff);
u->data3 = (word_t) (((time >> 48) & 0x0ffff) | 0x1000);
write_word(&(u->data4[6]), (word_t) ((nowseq & 0x3fff) | 0x8000));
write_word(&(u->data4[4]), (word_t) (node_high));
write_dword(&(u->data4[0]), (dword_t) (node_low));
}
/**
* internal
* ingroup uuid
* The thread synchronization lock used to guarantee UUID uniqueness
* for all the threads running within a process.
*/
char *uuid_create_string(void)
{
uuid_t u;
uuid_create(&u);
return uuid_to_string(&u);
}
char *uuid_to_string(const uuid_t* u)
{
static char uuid_str[UUID_LEN+1];
ushort a,b;
uint32 c;
read_word(&(u->data4[6]), &a);
read_word(&(u->data4[4]), &b);
read_dword(&(u->data4[0]), &c);
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
sprintf(uuid_str, "%08lx-%04x-%04x-%04x-%04x%08lx",
u->data1,
u->data2,
u->data3,
a, b, c);
#pragma warning(pop) /* C4996 */
return uuid_str;
}
/**
* internal
* ingroup uuid
* The predefined namespace UUID. Expressed in binary format
* to avoid unnecessary conversion when generating name based UUIDs.
*/
static const unsigned char namespace_uuid[] = {
0x9c, 0xfb, 0xd9, 0x1f, 0x11, 0x72, 0x4a, 0xf6,
0xbd, 0xcb, 0x9f, 0x34, 0xe4, 0x6f, 0xa0, 0xfb
};
void uuid_create_external(const char *external, uuid_t* u)
{
md5_context ctx;
unsigned char uuid[16];
//unsigned char md5sum[16];
assert(external != NULL);
md5_starts( &ctx );
md5_update( &ctx, (uint8 *) namespace_uuid, sizeof(namespace_uuid));
md5_update(&ctx, (unsigned char *) external, (unsigned int) strlen(external));
md5_finish( &ctx, uuid);
//MD5_init(&md5);
//MD5_update(&md5, namespace_uuid, sizeof(namespace_uuid));
//MD5_update(&md5, (unsigned char *) external, (unsigned int) strlen(external));
//MD5_fini(uuid, &md5);
u->data1 = (dword_t) (uuid[0] << 24 | uuid[1] << 16 | uuid[2] << 8 | uuid[3]);
u->data2 = (word_t) (uuid[4] << 8 | uuid[5]);
u->data3 = (word_t) (((uuid[6] & 0x0f) | 0x30) << 8 | uuid[7]);
/* BYTE 6-7 */
write_word(&(u->data4[6]), (word_t) (((uuid[8] & 0x3f) | 0x80) << 8 | uuid[9]));
/* BYTE 4-5 */
write_word(&(u->data4[4]), (word_t) (uuid[10] << 8 | uuid[11]));
/* BYTE 0-3 */
write_dword(&(u->data4[0]), (dword_t) (uuid[12] << 24 | uuid[13] << 16 | uuid[14] << 8 | uuid[15]));
}
/**
* Get timestamp from a UUID
**/
void uuid_to_timestamp(const uuid_t* u, timestamp_t* t)
{
int64 time, t2, t3;
struct tm* p;
assert(u);
t2 = u->data2;
t3 = u->data3;
time = u->data1 + (t2<<32) + ((t3&0x0fff)<<48); /* 100ns */
time -= NSec_Since_1582;
t->tm_fraction = (long)(time%NSec100_Per_Sec);
time /= 10;
time /= USec_Per_Sec;
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
p = localtime(&time);
#pragma warning(pop) /* C4996 */
t->tm_hour = p->tm_hour;
t->
没有合适的资源?快使用搜索试试~ 我知道了~
uuid.rar_C获得UUID_UUID C_c生成uuid_c语言生成uuid_uuid
共20个文件
h:3个
obj:3个
c:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 114 浏览量
2022-09-24
07:58:14
上传
评论
收藏 468KB RAR 举报
温馨提示
采用纯C的方式生成全球唯一ID,guid,采用linux下的代码移植过来的,软件采用vc6编译
资源推荐
资源详情
资源评论
收起资源包目录
uuid.rar (20个子文件)
uuid
md5.h 426B
main.opt 821KB
uuid32.h 3KB
main.dsw 533B
main.c 1KB
cdatatype.h 1KB
main.ncb 41KB
main.dsp 4KB
main.plg 1KB
uuid32.c 9KB
md5.c 9KB
Debug
main.ilk 201KB
vc60.idb 33KB
main.obj 4KB
main.exe 196KB
uuid32.obj 18KB
vc60.pdb 52KB
main.pdb 401KB
main.pch 248KB
md5.obj 9KB
共 20 条
- 1
资源评论
APei
- 粉丝: 63
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功