<commit_before>
module Sec
  attach_function 'SecKeychainCopyDefault', [:pointer], :osstatus
  attach_function 'SecKeychainDelete', [:keychainref], :osstatus
  attach_function 'SecKeychainOpen', [:string, :pointer], :osstatus
  attach_function 'SecKeychainGetPath', [:keychainref, :pointer, :pointer], :osstatus

  attach_function 'SecKeychainCreate', [:string, :uint32, :pointer, :char, :pointer, :pointer], :osstatus
  attach_function 'SecItemCopyMatching', [:pointer, :pointer], :osstatus
  
  class KeychainSettings < FFI::Struct
    layout  :version, :uint32,
            :lock_on_sleep, :uchar,
            :use_lock_interval, :uchar, #apple ignores this
            :lock_interval, :uint32
  end

  attach_function 'SecKeychainSetSettings', [:keychainref, KeychainSettings], :osstatus
  attach_function 'SecKeychainCopySettings', [:keychainref, KeychainSettings], :osstatus

  attach_function 'SecKeychainLock', [:keychainref], :osstatus
  attach_function 'SecKeychainUnlock', [:keychainref, :uint32, :pointer, :uchar], :osstatus

  attach_function 'SecKeychainGetStatus', [:keychainref, :pointer], :osstatus

  enum :keychainStatus, [
    :kSecUnlockStateStatus, 1,
    :kSecReadPermStatus,    2,
    :kSecWritePermStatus,   4,
  ]
end


class Keychain < Sec::Base
  register_type 'SecKeychain'

  class << self
    # creates a new keychain file and adds it to the keychain search path ( SecKeychainCreate )
    #
    # See https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/c/func/SecKeychainCreate
    # @param [String] The path to the keychain file to create
    #   If it is not absolute it is interpreted relative to ~/Library/Keychains
    # @param [String] The password to use for the keychain
    # @return [Keychain] a keychain object representing the newly created keychain

    def create(path, password)
      password = <PASSWORD>(Encoding::UTF_8)
      path = path.encode(Encoding::UTF_8)

      out_buffer = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecKeychainCreate(path, password.bytesize, FFI::MemoryPointer.from_string(password), 0,
                                          nil, out_buffer)

      Sec.check_osstatus(status)
      new(out_buffer.read_pointer).release_on_gc
    end

    # Gets the default keychain object ( SecKeychainCopyDefault )
    #
    # See https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/c/func/SecKeychainCopyDefault
    # @return [Keychain] a keychain object
    def default
      out_buffer = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecKeychainCopyDefault(out_buffer);
      Sec.check_osstatus(status)

      new(out_buffer.read_pointer).release_on_gc
    end

    # Opens the keychain file at the specified path and adds it to the keychain search path ( SecKeychainOpen )
    #
    # See https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/c/func/SecKeychainCopyDefault
    # @param [String] path to the keychain file
    # @return [Keychain] a keychain object
    def open(path)
      out_buffer = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecKeychainOpen(path,out_buffer);
      Sec.check_osstatus(status)
      new(out_buffer.read_pointer).release_on_gc
    end

    # Returns a scope for internet passwords contained in all keychains
    #
    # @return [Keychain::Scope] a new scope object
    def internet_passwords
      Scope.new(Sec::Classes::INTERNET)
    end

    # Returns a scope for generic passwords in all keychains
    #
    # @return [Keychain::Scope] a new scope object
    def generic_passwords
      Scope.new(Sec::Classes::GENERIC)
    end

  end

  # Returns whether the keychain will be locked if the machine goes to sleep<commit_msg>clarify open<commit_after>
module Sec
  attach_function 'SecKeychainCopyDefault', [:pointer], :osstatus
  attach_function 'SecKeychainDelete', [:keychainref], :osstatus
  attach_function 'SecKeychainOpen', [:string, :pointer], :osstatus
  attach_function 'SecKeychainGetPath', [:keychainref, :pointer, :pointer], :osstatus

  attach_function 'SecKeychainCreate', [:string, :uint32, :pointer, :char, :pointer, :pointer], :osstatus
  attach_function 'SecItemCopyMatching', [:pointer, :pointer], :osstatus
  
  class KeychainSettings < FFI::Struct
    layout  :version, :uint32,
            :lock_on_sleep, :uchar,
            :use_lock_interval, :uchar, #apple ignores this
            :lock_interval, :uint32
  end

  attach_function 'SecKeychainSetSettings', [:keychainref, KeychainSettings], :osstatus
  attach_function 'SecKeychainCopySettings', [:keychainref, KeychainSettings], :osstatus

  attach_function 'SecKeychainLock', [:keychainref], :osstatus
  attach_function 'SecKeychainUnlock', [:keychainref, :uint32, :pointer, :uchar], :osstatus

  attach_function 'SecKeychainGetStatus', [:keychainref, :pointer], :osstatus

  enum :keychainStatus, [
    :kSecUnlockStateStatus, 1,
    :kSecReadPermStatus,    2,
    :kSecWritePermStatus,   4,
  ]
end


class Keychain < Sec::Base
  register_type 'SecKeychain'

  class << self
    # creates a new keychain file and adds it to the keychain search path ( SecKeychainCreate )
    #
    # See https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/c/func/SecKeychainCreate
    # @param [String] The path to the keychain file to create
    #   If it is not absolute it is interpreted relative to ~/Library/Keychains
    # @param [String] The password to use for the keychain
    # @return [Keychain] a keychain object representing the newly created keychain

    def create(path, password)
      password = password.encode(Encoding::UTF_8)
      path = path.encode(Encoding::UTF_8)

      out_buffer = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecKeychainCreate(path, password.bytesize, FFI::MemoryPointer.from_string(password), 0,
                                          nil, out_buffer)

      Sec.check_osstatus(status)
      new(out_buffer.read_pointer).release_on_gc
    end

    # Gets the default keychain object ( SecKeychainCopyDefault )
    #
    # See https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/c/func/SecKeychainCopyDefault
    # @return [Keychain] a keychain object
    def default
      out_buffer = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecKeychainCopyDefault(out_buffer);
      Sec.check_osstatus(status)

      new(out_buffer.read_pointer).release_on_gc
    end

    # Opens the keychain file at the specified path and adds it to the keychain search path ( SecKeychainOpen )
    #
    # Will succeed even if the file doesn't exists (however most operations on the keychain will then fail)
    #
    # See https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/c/func/SecKeychainCopyDefault
    # @param [String] path to the keychain file
    # @return [Keychain] a keychain object
    def open(path)
      out_buffer = FFI::MemoryPointer.new(:pointer)
      status = Sec.SecKeychainOpen(path,out_buffer);
      Sec.check_osstatus(status)
      new(out_buffer.read_pointer).release_on_gc
    end

    # Returns a scope for internet passwords contained in all keychains
    #
    # @return [Keychain::Scope] a new scope object
    def internet_passwords
      Scope.new(Sec::Classes::INTERNET)
    end

    # Returns a scope for generic passwords in all keychains
    #
    # @return [Keychain::Scope] a new scope object
    def generic_passwords
      Scope.new(Sec::Classes::GENERIC)
    end

  end

  # Returns whether the keychain will be locked if the machine goes to sleep
{'fraction_non_alphanumeric': 0.09652019304038609, 'fraction_numerical': 0.003556007112014224, 'mean_word_length': 3.9157303370786516, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 4, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '628445', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2294, 'n_words': 585}