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/sxmp/uuid.c

105 lines
2.3 KiB
C

/*
* Secure X Message Passing Library v2 implementation.
* (c) Askele Group 2013-2015 <http://askele.com>
* (c) Alexander Vdolainen 2013-2015 <avdolainen@gmail.com>
*
* libsxmp is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsxmp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*
*/
#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
}