## This file generated by InlineX::C2XS (version 0.22) using Inline::C (version 0.55)
package Win32::GenRandom;
use strict;
use warnings;
use Config;
use Win32;

require Exporter;
*import = \&Exporter::import;
require DynaLoader;

$Win32::GenRandom::VERSION = '0.05';

DynaLoader::bootstrap Win32::GenRandom $Win32::GenRandom::VERSION;

use subs qw(
    PROV_FORTEZZA CRYPT_VERIFYCONTEXT CRYPT_DELETEKEYSET PROV_SSL PROV_RSA_SIG PROV_DSS
    CRYPT_NEWKEYSET PROV_DH_SCHANNEL CRYPT_MACHINE_KEYSET CRYPT_DEFAULT_CONTAINER_OPTIONAL
    PROV_MS_EXCHANGE PROV_RSA_FULL PROV_RSA_AES CRYPT_SILENT PROV_RSA_SCHANNEL PROV_DSS_DH
           );

@Win32::GenRandom::EXPORT = ();
@Win32::GenRandom::EXPORT_OK = qw(
    cgr rgr cgr_uv rgr_uv cgr_32 rgr_32 gr gr_uv gr_32 cgr_custom cgr_custom_uv cgr_custom_32
    which_crypto whw
    PROV_FORTEZZA CRYPT_VERIFYCONTEXT CRYPT_DELETEKEYSET PROV_SSL PROV_RSA_SIG PROV_DSS
    CRYPT_NEWKEYSET PROV_DH_SCHANNEL CRYPT_MACHINE_KEYSET CRYPT_DEFAULT_CONTAINER_OPTIONAL
    PROV_MS_EXCHANGE PROV_RSA_FULL PROV_RSA_AES CRYPT_SILENT PROV_RSA_SCHANNEL PROV_DSS_DH
    );

%Win32::GenRandom::EXPORT_TAGS = (all => [qw(
    cgr rgr cgr_uv rgr_uv cgr_32 rgr_32 gr gr_uv gr_32 cgr_custom cgr_custom_uv cgr_custom_32
    which_crypto whw
    PROV_FORTEZZA CRYPT_VERIFYCONTEXT CRYPT_DELETEKEYSET PROV_SSL PROV_RSA_SIG PROV_DSS
    CRYPT_NEWKEYSET PROV_DH_SCHANNEL CRYPT_MACHINE_KEYSET CRYPT_DEFAULT_CONTAINER_OPTIONAL
    PROV_MS_EXCHANGE PROV_RSA_FULL PROV_RSA_AES CRYPT_SILENT PROV_RSA_SCHANNEL PROV_DSS_DH
    )]);

my ($major, $minor) = (Win32::GetOSVersion())[1, 2];

$Win32::GenRandom::rtl_avail = ($major == 5 && $minor == 0) || $major < 5 ? 0 : 1;

sub cgr_uv {
  my @cgr;
  if(@_) {
    @cgr = cgr($_[0], $Config::Config{ivsize});
    return map {unpack "J", $_} @cgr;
  }
  @cgr = cgr($Config::Config{ivsize});
  return unpack "J", $cgr[0];
}

sub rgr_uv {
  my @rgr;
  if(@_) {
    @rgr = rgr($_[0], $Config::Config{ivsize});
    return map {unpack "J", $_} @rgr;
  }
  @rgr = rgr($Config::Config{ivsize});
  return unpack "J", $rgr[0];
}

sub cgr_32 {
  my @cgr;
  if(@_) {
    @cgr = cgr($_[0], 4);
    return map {unpack "L", $_} @cgr;
  }
  @cgr = cgr(4);
  return unpack "L", $cgr[0];
}

sub rgr_32 {
  my @rgr;
  if(@_) {
    @rgr = rgr($_[0], 4);
    return map {unpack "L", $_} @rgr;
  }
  @rgr = rgr(4);
  return unpack "L", $rgr[0];
}

sub gr {
  return rgr(@_) if $Win32::GenRandom::rtl_avail;
  return cgr(@_);
}

sub gr_uv {
  return rgr_uv(@_) if $Win32::GenRandom::rtl_avail;
  return cgr_uv(@_);
}

sub gr_32 {
  return rgr_32(@_) if $Win32::GenRandom::rtl_avail;
  return cgr_32(@_);
}

sub cgr_custom_uv {
  my @cgr;
  if(@_ == 5) {
    @cgr = cgr_custom($_[0], $Config::Config{ivsize}, $_[1], $_[2], $_[3], $_[4]);
    return map {unpack "J", $_} @cgr;
  }
  @cgr = cgr_custom($Config::Config{ivsize}, $_[0], $_[1], $_[2], $_[3]);
  return unpack "J", $cgr[0];
}

sub cgr_custom_32 {
  my @cgr;
  if(@_ == 5) {
    @cgr = cgr_custom($_[0], 4, $_[1], $_[2], $_[3], $_[4]);
    return map {unpack "L", $_} @cgr;
  }
  @cgr = cgr_custom(4, $_[0], $_[1], $_[2], $_[3]);
  return unpack "L", $cgr[0];
}

sub which_crypto {
  $Win32::GenRandom::rtl_avail ? 'RtlGenRandom' : 'CryptGenRandom';
}

sub _system_error {
   warn "$^E";
}

sub PROV_FORTEZZA                    () {return _PROV_FORTEZZA()}
sub CRYPT_VERIFYCONTEXT              () {return _CRYPT_VERIFYCONTEXT()}
sub CRYPT_DELETEKEYSET               () {return _CRYPT_DELETEKEYSET()}
sub PROV_SSL                         () {return _PROV_SSL()}
sub PROV_RSA_SIG                     () {return _PROV_RSA_SIG()}
sub PROV_DSS                         () {return _PROV_DSS()}
sub CRYPT_NEWKEYSET                  () {return _CRYPT_NEWKEYSET()}
sub PROV_DH_SCHANNEL                 () {return _PROV_DH_SCHANNEL()}
sub CRYPT_MACHINE_KEYSET             () {return _CRYPT_MACHINE_KEYSET()}
sub CRYPT_DEFAULT_CONTAINER_OPTIONAL () {return _CRYPT_DEFAULT_CONTAINER_OPTIONAL()}
sub PROV_MS_EXCHANGE                 () {return _PROV_MS_EXCHANGE()}
sub PROV_RSA_FULL                    () {return _PROV_RSA_FULL()}
sub PROV_RSA_AES                     () {return _PROV_RSA_AES()}
sub CRYPT_SILENT                     () {return _CRYPT_SILENT()}
sub PROV_RSA_SCHANNEL                () {return _PROV_RSA_SCHANNEL()}
sub PROV_DSS_DH                      () {return _PROV_DSS_DH()}

sub dl_load_flags {0} # Prevent DynaLoader from complaining and croaking

1;

__END__

