--- parser3/src/main/pa_random.C 2012/03/16 09:24:14 1.3 +++ parser3/src/main/pa_random.C 2020/12/15 17:10:36 1.10 @@ -1,23 +1,22 @@ /** @file Parser: random related functions. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ // includes +#include "pa_common.h" #include "pa_random.h" #include "pa_exception.h" #include "pa_threads.h" -volatile const char * IDENT_PA_RANDOM_C="$Id: pa_random.C,v 1.3 2012/03/16 09:24:14 moko Exp $" IDENT_PA_RANDOM_H; +volatile const char * IDENT_PA_RANDOM_C="$Id: pa_random.C,v 1.10 2020/12/15 17:10:36 moko Exp $" IDENT_PA_RANDOM_H; -#ifdef WIN32 -# include -#endif +#ifdef _MSC_VER +#include -#ifdef WIN32 class Random_provider { HCRYPTPROV fhProv; @@ -28,9 +27,7 @@ class Random_provider { return; if(!CryptAcquireContext(&fhProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) - throw Exception(0, - 0, - "CryptAcquireContext failed"); + throw Exception(0, 0, "CryptAcquireContext failed"); } void release() { if(fhProv) @@ -44,9 +41,7 @@ public: acquire(); if(!CryptGenRandom(fhProv, size, (BYTE*)buffer)) - throw Exception(0, - 0, - "CryptGenRandom failed"); + throw Exception(0, 0, "CryptGenRandom failed"); } } random_provider; @@ -109,14 +104,23 @@ static void get_random_bytes(void *buf, #endif void random(void *buffer, size_t size) { -#ifdef WIN32 +#ifdef _MSC_VER random_provider.generate(buffer, size); #else get_random_bytes(buffer, size); #endif } -uuid get_uuid() { +/// to hell with extra bytes on 64bit platforms +struct uuid { + unsigned int time_low; + unsigned short time_mid; + unsigned short time_hi_and_version; + unsigned short clock_seq; + unsigned char node[6]; +}; + +static uuid get_uuid() { // random uuid uuid; random(&uuid, sizeof(uuid)); @@ -140,3 +144,32 @@ uuid get_uuid() { return uuid; } +char *get_uuid_cstr() { + uuid uuid=get_uuid(); + + const size_t bufsize=36+1/*zero-teminator*/+1/*for faulty snprintfs*/; + char* cstr=new(PointerFreeGC) char[bufsize]; + + snprintf(cstr, bufsize, + "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", + uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, + uuid.clock_seq >> 8, uuid.clock_seq & 0xFF, + uuid.node[0], uuid.node[1], uuid.node[2], + uuid.node[3], uuid.node[4], uuid.node[5]); + return cstr; +} + +char *get_uuid_boundary() { + uuid uuid=get_uuid(); + + const int boundary_bufsize=10+32+1/*for zero-teminator*/+1/*for faulty snprintfs*/; + char* boundary=new(PointerFreeGC) char[boundary_bufsize]; + + snprintf(boundary, boundary_bufsize, + "----------%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X", + uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, + uuid.clock_seq >> 8, uuid.clock_seq & 0xFF, + uuid.node[0], uuid.node[1], uuid.node[2], + uuid.node[3], uuid.node[4], uuid.node[5]); + return boundary; +}