[前][次][番号順一覧][スレッド一覧]

ruby-changes:36241

From: usa <ko1@a...>
Date: Sun, 9 Nov 2014 04:03:00 +0900 (JST)
Subject: [ruby-changes:36241] usa:r48322 (trunk): * lib/securerandom.rb (SecureRandom::AdvApi32): split from `initialize`.

usa	2014-11-09 04:02:48 +0900 (Sun, 09 Nov 2014)

  New Revision: 48322

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48322

  Log:
    * lib/securerandom.rb (SecureRandom::AdvApi32): split from `initialize`.
      thanks @zzak to remember it.

  Modified files:
    trunk/ChangeLog
    trunk/lib/securerandom.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48321)
+++ ChangeLog	(revision 48322)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Nov  9 04:01:46 2014  NAKAMURA Usaku  <usa@r...>
+
+	* lib/securerandom.rb (SecureRandom::AdvApi32): split from `initialize`.
+	  thanks @zzak to remember it.
+
 Sun Nov  9 02:05:33 2014  Tanaka Akira  <akr@f...>
 
 	* test/net/http/utils.rb: Don't connect to spawned server
Index: lib/securerandom.rb
===================================================================
--- lib/securerandom.rb	(revision 48321)
+++ lib/securerandom.rb	(revision 48322)
@@ -39,6 +39,38 @@ end https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L39
 #   p SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
 #   p SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
 module SecureRandom
+  module AdvApi32 # :nodoc:
+    require "fiddle/import"
+    extend Fiddle::Importer
+    dlload "advapi32"
+    extern "int CryptAcquireContext(void*, void*, void*, unsigned long, unsigned long)"
+    extern "int CryptGenRandom(void*, unsigned long, void*)"
+
+    unless defined?(@hProv)
+      hProvStr = " " * Fiddle::SIZEOF_VOIDP
+      prov_rsa_full = 1
+      crypt_verifycontext = 0xF0000000
+
+      if CryptAcquireContext(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0
+        raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
+      end
+      type = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q' : 'l'
+      @hProv, = hProvStr.unpack(type)
+    end
+
+    def self.hProv
+      @hProv
+    end
+  end if /mswin|mingw/ =~ RUBY_PLATFORM
+
+  module Kernel32 # :nodoc:
+    require "fiddle/import"
+    extend Fiddle::Importer
+    dlload "kernel32"
+    extern "unsigned long GetLastError()"
+    extern "unsigned long FormatMessageA(unsigned long, void*, unsigned long, unsigned long, void*, unsigned long, void*)"
+  end if /mswin|mingw/ =~ RUBY_PLATFORM
+
   # SecureRandom.random_bytes generates a random binary string.
   #
   # The argument _n_ specifies the length of the result string.
@@ -89,36 +121,9 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L121
       end
     end
 
-    unless defined?(@has_win32)
-      begin
-        advapi32 = Module.new do
-          require "fiddle/import"
-          extend Fiddle::Importer
-          dlload "advapi32"
-          extern "int CryptAcquireContext(void*, void*, void*, unsigned long, unsigned long)"
-          extern "int CryptGenRandom(void*, unsigned long, void*)"
-        end
-
-        @crypt_gen_random = advapi32.method(:CryptGenRandom)
-
-        hProvStr = " " * Fiddle::SIZEOF_VOIDP
-        prov_rsa_full = 1
-        crypt_verifycontext = 0xF0000000
-
-        if advapi32.CryptAcquireContext(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0
-          raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
-        end
-        type = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q' : 'l'
-        @hProv, = hProvStr.unpack(type)
-
-        @has_win32 = true
-      rescue LoadError
-        @has_win32 = false
-      end
-    end
-    if @has_win32
+    if defined?(AdvApi32)
       bytes = " ".force_encoding("ASCII-8BIT") * n
-      if @crypt_gen_random.call(@hProv, bytes.size, bytes) == 0
+      if AdvApi32.CryptGenRandom(AdvApi32.hProv, bytes.size, bytes) == 0
         raise SystemCallError, "CryptGenRandom failed: #{lastWin32ErrorMessage}"
       end
       return bytes
@@ -266,14 +271,12 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L271
 
   # Following code is based on David Garamond's GUID library for Ruby.
   def self.lastWin32ErrorMessage # :nodoc:
-    get_last_error = Win32API.new("kernel32", "GetLastError", '', 'L')
-    format_message = Win32API.new("kernel32", "FormatMessageA", 'LPLLPLPPPPPPPP', 'L')
     format_message_ignore_inserts = 0x00000200
     format_message_from_system    = 0x00001000
 
-    code = get_last_error.call
+    code = Kernel32.GetLastError()
     msg = "\0" * 1024
-    len = format_message.call(format_message_ignore_inserts + format_message_from_system, 0, code, 0, msg, 1024, nil, nil, nil, nil, nil, nil, nil, nil)
+    len = Kernel32.FormatMessageA(format_message_ignore_inserts + format_message_from_system, 0, code, 0, msg, 1024, nil)
     msg[0, len].force_encoding("filesystem").tr("\r", '').chomp
   end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]