<commit_before>require 'ffi'
require 'openssl'
require 'fileutils'

require_relative "root/version"

module OpenSSL::Win::Root

  On = Gem.win_platform?

  module Crypt
    extend FFI::Library
    ffi_lib 'crypt32'
    ffi_convention :stdcall

    attach_function :open, :CertOpenSystemStoreA, [:pointer, :string], :pointer
    attach_function :close, :CertCloseStore, [:pointer, :uint], :int
    attach_function :enum, :CertEnumCertificatesInStore, [:pointer, :pointer], :pointer

    class Ctx < FFI::Struct
      layout :dwCertEncodingType, :uint,
        :pbCertEncoded, :pointer,
        :cbCertEncoded, :uint,
        :pCertInfo, :pointer,
        :hCertStore, :pointer

      def crt
        OpenSSL::X509::Certificate.new self[:pbCertEncoded].read_string self[:cbCertEncoded]
      end
    end

    # Based on Puppet::Util::Windows::RootCerts
    def self.each
      store = open nil, 'ROOT'
      begin
        ctx = nil
        yield Ctx.new(ctx).crt until (ctx = enum store, ctx).null?
      ensure
        close store, 0
      end
    end
  end if On

  def self.save(path=path)
    Dir.glob(File.join path, '*'){|f| File.unlink f}
    hashes={}
    Crypt.each do |crt|
      hash=crt.subject.hash
      name=File.join path, '%08x.%i' % [hash, hashes[hash]||=0]
      hashes[hash]+=1
      File.open name, 'w' do |f|
        f.puts <<-EOT
Subject: #{crt.subject}
Valid:   #{crt.not_before} - #{crt.not_after}
Saved:   #{self} v#{VERSION} @#{Time.now}
#{crt.to_pem}
        EOT
      end
    end
  end

  def self.path
    return @path if @path
    x = File.expand_path '..', __FILE__
    x = File.dirname x until File.exists? File.join x, 'Gemfile'
    x = File.join x, 'pem'
    FileUtils.mkdir_p x
    @path = x
  end

  def self.inject
    OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE.add_path path
    path
  end

  def self.go!
    t = Thread.new{ save }
    at_exit{t.join}
    inject
  end

  go! if On

end
<commit_msg>Skip duplicates<commit_after>require 'ffi'
require 'openssl'
require 'fileutils'

require_relative "root/version"

module OpenSSL::Win::Root

  On = Gem.win_platform?

  # Based on Puppet::Util::Windows::RootCerts
  module Crypt
    extend FFI::Library
    ffi_lib 'crypt32'
    ffi_convention :stdcall

    attach_function :open, :CertOpenSystemStoreA, [:pointer, :string], :pointer
    attach_function :close, :CertCloseStore, [:pointer, :uint], :int
    attach_function :enum, :CertEnumCertificatesInStore, [:pointer, :pointer], :pointer

    class Ctx < FFI::Struct
      layout :dwCertEncodingType, :uint,
        :pbCertEncoded, :pointer,
        :cbCertEncoded, :uint,
        :pCertInfo, :pointer,
        :hCertStore, :pointer

      def crt
        OpenSSL::X509::Certificate.new self[:pbCertEncoded].read_string self[:cbCertEncoded]
      end
    end

    def self.each
      store = open nil, 'ROOT'
      begin
        ctx = nil
        yield Ctx.new(ctx).crt until (ctx = enum store, ctx).null?
      ensure
        close store, 0
      end
    end
  end if On

  # Path where certificates will be
  def self.path
    return @path if @path
    x = File.expand_path '..', __FILE__
    x = File.dirname x until File.exists? File.join x, 'Gemfile'
    x = File.join x, 'pem'
    FileUtils.mkdir_p x
    @path = x
  end

  # Almost c_rehash
  def self.save(path=path)
    Dir.glob(File.join path, '*'){|f| File.unlink f}
    hashes={}
    Crypt.each do |crt|
      peers=hashes[hash=crt.subject.hash]||=[]
      id=OpenSSL::Digest::SHA1.new.digest crt.to_der
      next if peers.include? id
      name=File.join path, '%08x.%i' % [hash, peers.length]
      peers << id
      File.open name, 'w' do |f|
        f.puts <<-EOT
Subject: #{crt.subject}
Valid:   #{crt.not_before} - #{crt.not_after}
Saved:   #{self} v#{VERSION} @#{Time.now}
#{crt.to_pem}
        EOT
      end
    end
  end

  # Instruct OpenSSL to use fetched certificates
  def self.inject
    OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE.add_path path
    path
  end

  def self.go!
    t = Thread.new{ save }
    at_exit{t.join}
    inject
  end

  go! if On

end

{'fraction_non_alphanumeric': 0.12478548663888207, 'fraction_numerical': 0.004658004412846286, 'mean_word_length': 2.7328453796889294, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '320359', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1456, 'n_words': 361}