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

105 lines
2.3 KiB
C

/*
* Secure X Message Passing Library v2 implementation.
10 years ago
* (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>
10 years ago
#ifdef WIN32
#include <Winsock2.h>
#include <windows.h>
#include <rpc.h>
#else
#include <sys/select.h>
10 years ago
#include <netdb.h>
#include <unistd.h>
10 years ago
#include <uuid/uuid.h>
#endif
#include <fcntl.h>
10 years ago
#ifdef WIN32
#define UUID_T_LENGTH 16
10 years ago
#ifdef uuid_t
10 years ago
#undef uuid_t
10 years ago
#endif
10 years ago
typedef unsigned char uuid_t[16];
10 years ago
10 years ago
void uuid_generate_random(uuid_t out){
10 years ago
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);
10 years ago
QueryPerformanceCounter(&t1);
10 years ago
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)
{
10 years ago
#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++)
10 years ago
snprintf(uuidc+(2*i*sizeof(char)), len, "%02x", uuid[i]);
10 years ago
return uuidc;
#endif
}