class Keepassx::Field::Base

Constants

DATA_LENGTH_FIELD_SIZE
FIELD_TERMINATOR
TYPE_CODE_FIELD_SIZE

Attributes

data_type[R]
name[R]
type_code[R]

Public Class Methods

new(payload) click to toggle source
# File lib/keepassx/field/base.rb, line 14
def initialize(payload)
  if payload.is_a?(StringIO)
    @type_code, data_length = payload.read(TYPE_CODE_FIELD_SIZE + DATA_LENGTH_FIELD_SIZE).unpack('SI')
    _, @name, @data_type = self.class.fields_description.find { |type_code, _, _| type_code == @type_code }

    # Not using setter because it should be raw data here
    @data = payload.read(data_length)

    # Set export_import_methods *after* setting data
    set_export_import_methods(@data_type)

  elsif payload.is_a?(Hash)
    @name = payload[:name].to_s
    @type_code, _, @data_type = self.class.fields_description.find { |_, name, _| name == @name }

    # Set export_import_methods *before* setting data
    set_export_import_methods(@data_type)

    # Using setter because we need to convert data here
    self.data = payload[:data]
  end
end

Public Instance Methods

data() click to toggle source
# File lib/keepassx/field/base.rb, line 38
def data
  send(@export_method)
end
data=(value) click to toggle source
# File lib/keepassx/field/base.rb, line 43
def data=(value)
  send(@import_method, value)
end
encode() click to toggle source
# File lib/keepassx/field/base.rb, line 74
def encode
  buffer = [type_code, size].pack 'SI'
  buffer << @data unless @data.nil?
  buffer
end
length() click to toggle source
# File lib/keepassx/field/base.rb, line 53
def length
  TYPE_CODE_FIELD_SIZE + DATA_LENGTH_FIELD_SIZE + size
end
size() click to toggle source
# File lib/keepassx/field/base.rb, line 58
def size
  case data_type
  when :null
    0
  when :int
    4
  when :date
    5
  when :uuid
    16
  else
    (@data.nil? && 0) || @data.length
  end
end
terminator?() click to toggle source
# File lib/keepassx/field/base.rb, line 48
def terminator?
  name == 'terminator'
end

Private Instance Methods

ascii() click to toggle source
# File lib/keepassx/field/base.rb, line 119
def ascii
  # TODO: Add spec
  @data.unpack1('H*')
end
ascii=(value) click to toggle source
# File lib/keepassx/field/base.rb, line 165
def ascii=(value)
  @data = [value].pack('H*')
end
date() click to toggle source
# File lib/keepassx/field/base.rb, line 125
def date
  buffer = @data.unpack('C5')
  year   = (buffer[0] << 6) | (buffer[1] >> 2)
  month  = ((buffer[1] & 0b11) << 2) | (buffer[2] >> 6)
  day    = ((buffer[2] & 0b111111) >> 1)
  hour   = ((buffer[2] & 0b1) << 4) | (buffer[3] >> 4)
  min    = ((buffer[3] & 0b1111) << 2) | (buffer[4] >> 6)
  sec    = ((buffer[4] & 0b111111))

  Time.local(year, month, day, hour, min, sec)
end
date=(value) click to toggle source
# File lib/keepassx/field/base.rb, line 170
def date=(value)
  raise ArgumentError, "Expected: Time, String or Integer, got: '#{value.class}'." unless [Time, String, Integer].include?(value.class)

  value = Time.parse(value) if value.is_a?(String)
  value = Time.at(value) if value.is_a?(Integer)

  sec, min, hour, day, month, year = value.to_a

  @data = [
    0x0000FFFF & ((year >> 6) & 0x0000003F),
    0x0000FFFF & (((year & 0x0000003f) << 2) | ((month >> 2) & 0x00000003)),
    0x0000FFFF & (((month & 0x00000003) << 6) | ((day & 0x0000001F) << 1) | ((hour >> 4) & 0x00000001)),
    0x0000FFFF & (((hour & 0x0000000F) << 4) | ((min >> 2) & 0x0000000F)),
    0x0000FFFF & (((min & 0x00000003) << 6) | (sec & 0x0000003F)),
  ].pack('<C5')
end
int() click to toggle source
# File lib/keepassx/field/base.rb, line 109
def int
  @data.unpack1('I')
end
int=(value) click to toggle source
# File lib/keepassx/field/base.rb, line 155
def int=(value)
  @data = [value].pack('I')
end
null() click to toggle source

EXPORT METHODS

# File lib/keepassx/field/base.rb, line 94
def null
  nil
end
null=(_) click to toggle source

IMPORT METHODS

# File lib/keepassx/field/base.rb, line 140
def null=(_)
  @data = nil
end
set_export_import_methods(type) click to toggle source

rubocop:disable Style/RedundantInterpolation

# File lib/keepassx/field/base.rb, line 85
def set_export_import_methods(type)
  @export_method = "#{type}".to_sym
  @import_method = "#{type}=".to_sym
end
short() click to toggle source
# File lib/keepassx/field/base.rb, line 114
def short
  @data.unpack1('S')
end
short=(value) click to toggle source
# File lib/keepassx/field/base.rb, line 160
def short=(value)
  @data = [value].pack('S')
end
shunt() click to toggle source
# File lib/keepassx/field/base.rb, line 99
def shunt
  @data
end
shunt=(value) click to toggle source
# File lib/keepassx/field/base.rb, line 145
def shunt=(value)
  @data = value
end
string() click to toggle source
# File lib/keepassx/field/base.rb, line 104
def string
  @data.chomp("\000")
end
string=(value) click to toggle source
# File lib/keepassx/field/base.rb, line 150
def string=(value)
  @data = "#{value}\000"
end