From bb924c0038cef39f5f9df10767b74c1d8d26fde7 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 2 May 2005 14:55:09 +0000 Subject: [PATCH] Added the Windows implementation of the "improved" random number generation. --- Makefile.in | 16 ++++++++++------ src/common/random.cpp | 36 +++++++++++++++++++++++++++++++++++- src/common/random.h | 3 ++- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Makefile.in b/Makefile.in index 401c9f5d1..1c65ff2cb 100644 --- a/Makefile.in +++ b/Makefile.in @@ -107,6 +107,7 @@ endif ifeq (1,$(MINGW)) MMG_RESOURCES = src/mmg/mmg-resources.rc +LIBRPCRT = -lrpcrt4 endif DEP_COMMON = src/common/libmtxcommon.$(LIBMTXCOMMONEXT) @@ -319,7 +320,8 @@ libmtxcommon_OBJECTS := $(patsubst %.cpp,%.o,$(libmtxcommon_SOURCES)) src/common/libmtxcommon.dll: $(libmtxcommon_OBJECTS) @echo ' LD/DLL ' $@ $(Q)$(LINKSHARED) -Wl,--out-implib=$@.a -o $@ $(libmtxcommon_OBJECTS) \ - -liconv -lz $(COMPRESSION_LIBRARIES) -lmatroska -lebml -lexpat + -liconv -lz $(COMPRESSION_LIBRARIES) -lmatroska -lebml \ + -lexpat -lrpcrt4 src/common/libmtxcommon.a: $(libmtxcommon_OBJECTS) @rm -f $@ @@ -369,7 +371,7 @@ mkvmerge_LDADD = -lmtxinput -lmtxoutput \ -lmtxcommon -lmatroska -lebml \ -lavi -lrmff -lmpegparser \ $(FLAC_LIBS) -lvorbis -logg -lz $(COMPRESSION_LIBRARIES) \ - -lexpat $(ICONV_LIBS) $(LIBINTL_LIBS) + -lexpat $(ICONV_LIBS) $(LIBINTL_LIBS) $(LIBRPCRT) # # src/info @@ -380,7 +382,7 @@ mkvinfo_OBJECTS := $(patsubst %.cpp,%.o,$(mkvinfo_SOURCES)) mkvinfo_DEPENDENCIES += $(DEP_COMMON) mkvinfo_LDADD = -lmtxcommon -lmatroska -lebml \ $(WXWINDOWS_LIBS) \ - $(ICONV_LIBS) $(LIBINTL_LIBS) + $(ICONV_LIBS) $(LIBINTL_LIBS) $(LIBRPCRT) # # src/extract @@ -391,7 +393,7 @@ mkvextract_OBJECTS := $(patsubst %.cpp,%.o,$(mkvextract_SOURCES)) mkvextract_DEPENDENCIES += $(DEP_COMMON) $(DEP_AVILIB) $(DEP_RMFF) mkvextract_LDADD = -lmtxcommon -lvorbis -logg -lavi -lmatroska -lebml -lrmff \ $(ICONV_LIBS) $(LIBINTL_LIBS) -lexpat \ - -lz $(COMPRESSION_LIBRARIES) + -lz $(COMPRESSION_LIBRARIES) $(LIBRPCRT) # # src @@ -400,7 +402,8 @@ mkvextract_LDADD = -lmtxcommon -lvorbis -logg -lavi -lmatroska -lebml -lrmff \ base64tool_SOURCES = src/base64tool.cpp base64tool_OBJECTS := $(patsubst %.cpp,%.o,$(base64tool_SOURCES)) base64tool_DEPENDENCIES += $(DEP_COMMON) -base64tool_LDADD = -lmtxcommon $(ICONV_LIBS) $(LIBINTL_LIBS) +base64tool_LDADD = -lmtxcommon $(ICONV_LIBS) $(LIBINTL_LIBS) \ + $(LIBRPCRT) # # src/mmg @@ -411,7 +414,8 @@ mmg_OBJECTS := $(patsubst %.cpp,%.o,$(mmg_SOURCES)) \ $(patsubst %.rc,%.o,$(MMG_RESOURCES)) mmg_DEPENDENCIES += $(DEP_COMMON) mmg_LDADD = -lmtxcommon -lmatroska -lebml -lexpat $(ICONV_LIBS) \ - $(WXWINDOWS_LIBS) $(LIBINTL_LIBS) $(MINGW_GUIAPP) + $(WXWINDOWS_LIBS) $(LIBINTL_LIBS) $(MINGW_GUIAPP) \ + $(LIBRPCRT) mkvmerge: src/mkvmerge@EXEEXT@ diff --git a/src/common/random.cpp b/src/common/random.cpp index dc8ed697a..04564f485 100644 --- a/src/common/random.cpp +++ b/src/common/random.cpp @@ -18,6 +18,8 @@ #if !defined(SYS_WINDOWS) # include # include +#else +# include #endif #include "mm_io.h" @@ -25,9 +27,41 @@ #if defined(SYS_WINDOWS) +bool random_c::m_seeded = false; + +void +random_c::generate_bytes(void *destination, + int num_bytes) { + int i, num_left; + + i = 0; + while (num_bytes > 0) { + UUID uuid; + RPC_STATUS status; + + status = UuidCreate(&uuid); + if ((RPC_S_OK != status) && (RPC_S_UUID_LOCAL_ONLY != status)) { + if (!m_seeded) { + srand(GetTickCount()); + m_seeded = true; + } + + while (0 > num_bytes) { + ((unsigned char *)destination)[i + num_bytes] = + (unsigned char)(256.0 * rand() / (RAND_MAX + 1.0)); + --num_bytes; + } + } + + num_left = num_bytes > 8 ? 8 : num_bytes; + memcpy((unsigned char *)destination + i, &uuid.Data4, num_left); + num_bytes -= num_left; + i += num_left; + } +} + #else // defined(SYS_WINDOWS) -bool random_c::m_seeded = false; auto_ptr random_c::m_dev_urandom; bool random_c::m_tried_dev_urandom = false; diff --git a/src/common/random.h b/src/common/random.h index 1ad9f06ee..fc06f1fe1 100644 --- a/src/common/random.h +++ b/src/common/random.h @@ -23,8 +23,9 @@ class MTX_DLL_API random_c { private: -#if !defined(SYS_WINDOWS) static bool m_seeded; + +#if !defined(SYS_WINDOWS) static auto_ptr m_dev_urandom; static bool m_tried_dev_urandom; #endif