pike.git / src / builtin.cmod

version» Context lines:

pike.git/src/builtin.cmod:2155:    THIS->buffer_bits = 64;    pop_stack();    }       /* Generates a number 0<=c<limit from random bits taken from the    int_buffer. Follows the NIST SP800-90A method for converting bit    sequences into bound numbers, described in section B.5.1.1, and    summarized as "throw away attempts that are too large". */    static INT_TYPE read_int(INT_TYPE limit)    { -  if(limit <= 1) return 0; -  int bits = my_log2(limit-1)+1; -  UINT64 mask = (((UINT64)1)<<bits)-1; +  int bits; +  UINT64 mask;    int i; -  +  +  if(limit <= 1) return 0; +  +  bits = my_log2(limit-1)+1; +  mask = (((UINT64)1)<<bits)-1;    for(i=0; i<1000; i++)    { -  +  INT_TYPE ret;    if(THIS->buffer_bits < bits)    fill_int_buffer(); -  INT_TYPE ret = THIS->int_buffer&mask; +  ret = THIS->int_buffer & mask;    THIS->int_buffer >>= bits;    THIS->buffer_bits -= bits;    if( ret < limit )    return ret;    }    Pike_error("Failed to generate random data.\n");    }       /*! @decl int random(int max)    *!
pike.git/src/builtin.cmod:2279:    *!    *! @throws    *! Throws an exception if the object doesn't have a _random method.    *!    *! @seealso    *! @[lfun::_random()]    */    PIKEFUN mixed random(object o)    {    int f = low_find_lfun(o->prog, LFUN__RANDOM); +  struct object *co; +     if (f < 0)    Pike_error("Calling undefined lfun::%s.\n", lfun_names[LFUN__RANDOM]); -  struct object *co = Pike_fp->current_object; +  +  co = Pike_fp->current_object;    ref_push_function(co, f_RandomInterface_random_string_fun_num);    /* FIXME: precompiler doesn't generate usable fun_nums for variant    symbols. */    ref_push_function(co, find_shared_string_identifier(MK_STRING("random"),    co->prog));    apply_low(o, f, 2);    }   }   /*! @endclass    */
pike.git/src/builtin.cmod:2336:    *! Return a string of random data from the system randomness pool.    *!    *! On POSIX platforms this reads random data from @tt{/dev/urandom@}    *! on other platforms it may use other methods.    *!    *! @throws    *! May throw errors on unexpected state.    */    PIKEFUN string(8bit) random_string(int(0..) len)    { +  struct pike_string *ret; +  char *str; +     if( !len )    RETURN empty_pike_string;    if( len<0 )    Pike_error("Bad argument 1 to random_string(). Expected int(0..).\n");      #ifdef __NT__    if(!crypto_handle)    {    if( !CryptAcquireContext(&crypto_handle, 0, 0, PROV_RSA_FULL,    CRYPT_VERIFYCONTEXT|CRYPT_SILENT) )    Pike_error("Failed to set up Crypto Service.\n");    }    -  struct pike_string *ret = begin_shared_string(len); -  if( !CryptGenRandom(crypto_handle, len, (BYTE*)ret->str) ) +  ret = begin_shared_string(len); +  str = ret->str; +  if( !CryptGenRandom(crypto_handle, len, (BYTE*)str) )    {    do_free_unlinked_pike_string (ret);    Pike_error("Failed to create random data.\n");    }   #else /* !__NT__ */    while( random_fd == -1 )    {    random_fd = open("/dev/urandom", O_RDONLY);    if( random_fd==-1 ) {    if (errno == EINTR) continue; /* Retry */    Pike_error("Failed to open /dev/urandom.\n");    }    }    -  struct pike_string *ret = begin_shared_string(len); -  char* str = ret->str; +  ret = begin_shared_string(len); +  str = ret->str;    while( len )    {    int sz = read(random_fd, str, len);    if (sz < 0) {    if (errno == EINTR) continue; /* Retry */       free_string(ret);       /* Attempt to recover on next call. */    close(random_fd);