[xiph-commits] r3174 - in liboggz/trunk: . src/liboggz

conrad at svn.annodex.net conrad at svn.annodex.net
Mon Jul 30 02:49:22 PDT 2007


Author: conrad
Date: 2007-07-30 02:49:21 -0700 (Mon, 30 Jul 2007)
New Revision: 3174

Modified:
   liboggz/trunk/configure.ac
   liboggz/trunk/src/liboggz/oggz.c
   liboggz/trunk/src/liboggz/oggz_compat.h
Log:
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.


Modified: liboggz/trunk/configure.ac
===================================================================
--- liboggz/trunk/configure.ac	2007-07-30 08:11:12 UTC (rev 3173)
+++ liboggz/trunk/configure.ac	2007-07-30 09:49:21 UTC (rev 3174)
@@ -36,7 +36,7 @@
 
 # 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)

Modified: liboggz/trunk/src/liboggz/oggz.c
===================================================================
--- liboggz/trunk/src/liboggz/oggz.c	2007-07-30 08:11:12 UTC (rev 3173)
+++ liboggz/trunk/src/liboggz/oggz.c	2007-07-30 09:49:21 UTC (rev 3174)
@@ -439,14 +439,26 @@
   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;
 }

Modified: liboggz/trunk/src/liboggz/oggz_compat.h
===================================================================
--- liboggz/trunk/src/liboggz/oggz_compat.h	2007-07-30 08:11:12 UTC (rev 3173)
+++ liboggz/trunk/src/liboggz/oggz_compat.h	2007-07-30 09:49:21 UTC (rev 3174)
@@ -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



More information about the commits mailing list