libsxmp/lib/uuid.c

96 lines
1.7 KiB
C
Raw Normal View History

2015-02-02 14:10:17 +02:00
/*
2015-10-22 05:15:52 +03:00
* Secure X Message Passing Library v2 implementation.
2015-02-02 14:10:17 +02:00
* This is a proprietary software. See COPYING for further details.
*
2015-05-14 02:20:25 +03:00
* (c) Askele Group 2013-2015 <http://askele.com>
2015-02-02 14:10:17 +02:00
*/
#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>
2015-03-03 04:38:20 +04:00
#ifdef WIN32
#include <Winsock2.h>
#include <windows.h>
#include <rpc.h>
#else
2015-02-02 14:10:17 +02:00
#include <sys/select.h>
2015-03-03 04:38:20 +04:00
#include <netdb.h>
2015-02-02 14:10:17 +02:00
#include <unistd.h>
2015-03-03 04:38:20 +04:00
#include <uuid/uuid.h>
#endif
2015-02-02 14:10:17 +02:00
#include <fcntl.h>
2015-03-03 04:38:20 +04:00
#ifdef WIN32
#define UUID_T_LENGTH 16
2015-05-14 02:20:25 +03:00
#ifdef uuid_t
2015-03-03 04:38:20 +04:00
#undef uuid_t
2015-05-14 02:20:25 +03:00
#endif
2015-05-14 02:20:25 +03:00
typedef unsigned char uuid_t[16];
2015-03-03 04:38:20 +04:00
2015-05-14 02:20:25 +03:00
void uuid_generate_random(uuid_t out){
2015-03-03 04:38:20 +04:00
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);
2015-05-14 02:20:25 +03:00
QueryPerformanceCounter(&t1);
2015-03-03 04:38:20 +04:00
d=(long int)t1.QuadPart*1000/frequency.QuadPart;
memcpy(out,&d,sizeof(d));
}
#endif
2015-02-02 14:10:17 +02:00
/* this function is an ugly implementation to get C string with uuid */
char *__generate_uuid(void)
{
2015-03-03 04:38:20 +04:00
#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
2015-02-02 14:10:17 +02:00
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++)
2015-03-03 04:38:20 +04:00
snprintf(uuidc+(2*i*sizeof(char)), len, "%02x", uuid[i]);
2015-02-02 14:10:17 +02:00
2015-05-14 02:20:25 +03:00
return uuidc;
#endif
2015-02-02 14:10:17 +02:00
}