Changeset 3174

Show
Ignore:
Timestamp:
2007-07-30 02:49:21 (1 year ago)
Author:
conrad
Message:

Refactor oggz_get_serialno_new() to avoid using library rand()/random().
Instead, it generates a pseudorandom serialno on request, ensuring that the
number selected is not -1 or the serialno of an existing logical bitstream.
NB. This inlines a simple linear congruential generator to avoid problems
of portability of rand() vs. the POSIX random()/initstate()/getstate(), and
in the case of rand() in order to avoid interfering with the random number
sequence. Adapated from a patch by Erik de Castro Lopo, July 2007.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • liboggz/trunk/configure.ac

    r3173 r3174  
    3737# Checks for library functions. 
    3838AC_FUNC_REALLOC 
    39 AC_CHECK_FUNCS([memmove random]) 
     39AC_CHECK_FUNCS([memmove]) 
    4040 
    4141# Check for pkg-config 
  • liboggz/trunk/src/liboggz/oggz.c

    r3171 r3174  
    440440} 
    441441 
     442/* Generate a pseudorandom serialno on request, ensuring that the number 
     443 * selected is not -1 or the serialno of an existing logical bitstream. 
     444 * NB. This inlines a simple linear congruential generator to avoid problems 
     445 * of portability of rand() vs. the POSIX random()/initstate()/getstate(), and 
     446 * in the case of rand() in order to avoid interfering with the random number 
     447 * sequence. 
     448 * Adapated from a patch by Erik de Castro Lopo, July 2007. 
     449 */ 
    442450long 
    443451oggz_serialno_new (OGGZ * oggz) 
    444452{ 
    445   long serialno; 
     453  static long serialno = 0; 
     454  int k; 
     455 
     456  if (serialno == 0) serialno = time(NULL); 
    446457 
    447458  do { 
    448     serialno = oggz_random(); 
    449   } while (oggz_get_stream (oggz, serialno) != NULL); 
     459    for (k = 0; k < 3 || serialno == 0; k++) 
     460      serialno = 11117 * serialno + 211231; 
     461  } while (serialno == -1 || oggz_get_stream (oggz, serialno) != NULL); 
    450462 
    451463  return serialno; 
  • liboggz/trunk/src/liboggz/oggz_compat.h

    r357 r3174  
    3333#include "config.h" 
    3434 
    35 #ifdef HAVE_RANDOM 
    36 #  define oggz_random random 
    37 #else 
    38 #  define oggz_random rand 
    39 #endif 
    40  
    4135#ifndef WIN32 
    4236#  define oggz_stat_regular(mode) (S_ISREG((mode)) || S_ISLNK((mode)))