Annotation of parser3/src/main/pa_random.C, revision 1.11

1.1       misha       1: /** @file
                      2:        Parser: random related functions.
                      3: 
1.10      moko        4:        Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       misha       5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
                      8: // includes
                      9: 
1.8       moko       10: #include "pa_common.h"
1.1       misha      11: #include "pa_random.h"
                     12: #include "pa_exception.h"
                     13: #include "pa_threads.h"
                     14: 
1.11    ! moko       15: 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;
1.3       moko       16: 
1.5       moko       17: #ifdef _MSC_VER
1.4       moko       18: #include <windows.h>
1.1       misha      19: 
                     20: class Random_provider {
                     21:        HCRYPTPROV fhProv;
                     22:        
                     23:        void acquire() {
                     24:                SYNCHRONIZED;
                     25: 
                     26:                if(fhProv)
                     27:                        return;
                     28: 
                     29:                if(!CryptAcquireContext(&fhProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
1.7       moko       30:                        throw Exception(0, 0, "CryptAcquireContext failed");
1.1       misha      31:        }
                     32:        void release() {
                     33:                if(fhProv)
                     34:                        CryptReleaseContext(fhProv, 0);
                     35:        }
                     36:        
                     37: public:
                     38:        Random_provider(): fhProv(0) {}
                     39:        ~Random_provider() { release(); }
                     40:        void generate(void *buffer, size_t size) {
                     41:                acquire();
                     42: 
                     43:                if(!CryptGenRandom(fhProv, size, (BYTE*)buffer))
1.7       moko       44:                        throw Exception(0, 0, "CryptGenRandom failed");
1.1       misha      45:        }
                     46: }
                     47:        random_provider;
                     48: 
                     49: #else
                     50: 
                     51: /// from gen_uuid.c
                     52: static int get_random_fd(void)
                     53: {
                     54:         struct timeval  tv;
                     55:         static int      fd = -2;
                     56:         int             i;
                     57: 
                     58:         if (fd == -2) {
                     59:                 gettimeofday(&tv, 0);
                     60:                 fd = open("/dev/urandom", O_RDONLY);
                     61:                 if (fd == -1)
                     62:                         fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
                     63:                 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
                     64:         }
                     65:         /* Crank the random number generator a few times */
                     66:         gettimeofday(&tv, 0);
                     67:         for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
                     68:                 rand();
                     69:         return fd;
                     70: }
                     71: 
                     72: 
                     73: /*
                     74:  * Generate a series of random bytes.  Use /dev/urandom if possible,
                     75:  * and if not, use srandom/random.
                     76:  */
                     77: static void get_random_bytes(void *buf, int nbytes)
                     78: {
                     79:         int i, fd = get_random_fd();
                     80:         int lose_counter = 0;
                     81:         char *cp = (char *) buf;
                     82: 
                     83:         if (fd >= 0) {
                     84:                 while (nbytes > 0) {
                     85:                         i = read(fd, cp, nbytes);
                     86:                         if (i <= 0) {
                     87:                                 if (lose_counter++ > 16)
                     88:                                         break;
                     89:                                 continue;
                     90:                         }
                     91:                         nbytes -= i;
                     92:                         cp += i;
                     93:                         lose_counter = 0;
                     94:                 }
                     95:         }
                     96: 
                     97:         /* XXX put something better here if no /dev/random! */
                     98:         for (i = 0; i < nbytes; i++)
                     99:                 *cp++ = rand() & 0xFF;
                    100:         return;
                    101: }
                    102: 
                    103: 
                    104: #endif
                    105: 
                    106: void random(void *buffer, size_t size) {
1.5       moko      107: #ifdef _MSC_VER
1.1       misha     108:        random_provider.generate(buffer, size);
                    109: #else
                    110:        get_random_bytes(buffer, size);
                    111: #endif
                    112: }
                    113: 
1.7       moko      114: /// to hell with extra bytes on 64bit platforms
                    115: struct uuid {
                    116:        unsigned int    time_low;
                    117:        unsigned short  time_mid;
                    118:        unsigned short  time_hi_and_version;
                    119:        unsigned short  clock_seq;
                    120:        unsigned char   node[6];
                    121: };
                    122: 
                    123: static uuid get_uuid() {
1.1       misha     124:        // random
                    125:        uuid uuid;
                    126:        random(&uuid, sizeof(uuid));
                    127: 
                    128:        // http://www.opengroup.org/onlinepubs/9629399/apdxa.htm#tagtcjh_35
                    129:        // ~
                    130:        // version = DCE Security version, with embedded POSIX UIDs.  
                    131:        // variant = DCE
                    132:        //
                    133:        // DCE=Distributed Computing Environment
                    134:        // http://www.opengroup.org/dce/
                    135:        //
                    136:        // they say this influences comparison&such,
                    137:        // but could not figure out how, hence structure layout specified strictly
                    138:        // anyhow, uuidgen on Win32 yield those values
                    139:        // 
                    140:        // xxxxxxxx-xxxx-4xxx-{8,9,A,B}xxx-xxxxxxxxxxxx
                    141:        uuid.clock_seq = (uuid.clock_seq & 0x3FFF) | 0x8000;
                    142:         uuid.time_hi_and_version = (uuid.time_hi_and_version & 0x0FFF) | 0x4000;
                    143: 
                    144:        return uuid;
                    145: }
                    146: 
1.11    ! moko      147: char *get_uuid_cstr(bool lower, bool solid) {
1.7       moko      148:        uuid uuid=get_uuid();
                    149: 
                    150:        const size_t bufsize=36+1/*zero-teminator*/+1/*for faulty snprintfs*/;
                    151:        char* cstr=new(PointerFreeGC) char[bufsize];
                    152: 
1.11    ! moko      153:        const char *format[] = {
        !           154:                "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
        !           155:                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
        !           156:                "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X",
        !           157:                "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x"
        !           158:        };
        !           159: 
1.7       moko      160:        snprintf(cstr, bufsize,
1.11    ! moko      161:                format[(lower ? 1:0) + (solid ? 2:0)],
1.7       moko      162:                uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
                    163:                uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
                    164:                uuid.node[0], uuid.node[1], uuid.node[2],
                    165:                uuid.node[3], uuid.node[4], uuid.node[5]);
                    166:        return cstr;
                    167: }
                    168: 
                    169: char *get_uuid_boundary() {
                    170:        uuid uuid=get_uuid();
                    171: 
                    172:        const int boundary_bufsize=10+32+1/*for zero-teminator*/+1/*for faulty snprintfs*/;
                    173:        char* boundary=new(PointerFreeGC) char[boundary_bufsize];
                    174: 
                    175:        snprintf(boundary, boundary_bufsize,
                    176:                "----------%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X",
                    177:                uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
                    178:                uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
                    179:                uuid.node[0], uuid.node[1], uuid.node[2],
                    180:                uuid.node[3], uuid.node[4], uuid.node[5]);
                    181:        return boundary;
                    182: }

E-mail: