You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libsxmp/lib/uuid.c

92 lines
1.7 KiB
C

/*
* Secure X Message Passing Library v2 implementation.
* This is a proprietary software. See COPYING for further details.
*
* (c) Askele Group 2013-2015 <http://askele.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef WIN32
#include <Winsock2.h>
#include <windows.h>
#include <rpc.h>
#else
#include <sys/select.h>
#include <netdb.h>
#include <unistd.h>
#include <uuid/uuid.h>
#endif
#include <fcntl.h>
#ifdef WIN32
#define UUID_T_LENGTH 16
#ifdef uuid_t
#undef uuid_t
#endif
typedef unsigned char uuid_t[16];
void uuid_generate_random(uuid_t out){
int i,a=0;
LARGE_INTEGER frequency,t1;
long int d;
for(i=0;i<UUID_T_LENGTH;i++){
a=rand();
*(out+i)=a;
}
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&t1);
d=(long int)t1.QuadPart*1000/frequency.QuadPart;
memcpy(out,&d,sizeof(d));
}
#endif
/* this function is an ugly implementation to get C string with uuid */
char *__generate_uuid(void)
{
#ifdef WIN32
char *uuidc = NULL;
uuid_t uuid_t_m;
int len, i = 0,r=0;
len = 33;
if(!(uuidc = malloc(len))) return NULL;
uuid_generate_random(uuid_t_m);
for(i = 0; i < 16; i++)
snprintf(uuidc+(2*i*sizeof(char)), len, "%02x", uuid_t_m[i]);
return uuidc;
#else
char *uuidc = NULL;
uuid_t uuid;
int len, i = 0;
len = sizeof(char)*(sizeof(uuid_t)*2) + sizeof(char);
if(!(uuidc = malloc(len))) return NULL;
uuid_generate_time_safe(uuid);
for(i = 0; i < sizeof(uuid_t); i++)
snprintf(uuidc+(2*i*sizeof(char)), len, "%02x", uuid[i]);
return uuidc;
#endif
}