Commit 4df668bb authored by conrad's avatar conrad

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.


git-svn-id: http://svn.annodex.net/liboggz/trunk@3174 8158c8cd-e7e1-0310-9fa4-c5954c97daef
parent fce17f8f
......@@ -36,7 +36,7 @@ AC_TYPE_SIZE_T
# Checks for library functions.
AC_FUNC_REALLOC
AC_CHECK_FUNCS([memmove random])
AC_CHECK_FUNCS([memmove])
# Check for pkg-config
AC_CHECK_PROG(HAVE_PKG_CONFIG, pkg-config, yes)
......
......@@ -439,14 +439,26 @@ oggz_get_numtracks (OGGZ * oggz)
return oggz_vector_size (oggz->streams);
}
/* Generate 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.
*/
long
oggz_serialno_new (OGGZ * oggz)
{
long serialno;
static long serialno = 0;
int k;
if (serialno == 0) serialno = time(NULL);
do {
serialno = oggz_random();
} while (oggz_get_stream (oggz, serialno) != NULL);
for (k = 0; k < 3 || serialno == 0; k++)
serialno = 11117 * serialno + 211231;
} while (serialno == -1 || oggz_get_stream (oggz, serialno) != NULL);
return serialno;
}
......
......@@ -32,12 +32,6 @@
#include "config.h"
#ifdef HAVE_RANDOM
# define oggz_random random
#else
# define oggz_random rand
#endif
#ifndef WIN32
# define oggz_stat_regular(mode) (S_ISREG((mode)) || S_ISLNK((mode)))
#else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment