Class: Zip::ExtraField

Inherits:
Hash
  • Object
show all
Defined in:
lib/zip/extra_field.rb

Defined Under Namespace

Classes: Generic, IUnix, NTFS, OldUnix, UniversalTime, Zip64, Zip64Placeholder

Constant Summary collapse

ID_MAP =
{}

Instance Method Summary collapse

Constructor Details

#initialize(binstr = nil) ⇒ ExtraField

Returns a new instance of ExtraField.



5
6
7
# File 'lib/zip/extra_field.rb', line 5

def initialize(binstr = nil)
  merge(binstr) if binstr
end

Instance Method Details

#c_dir_sizeObject



80
81
82
# File 'lib/zip/extra_field.rb', line 80

def c_dir_size
  to_c_dir_bin.bytesize
end

#create(name) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/zip/extra_field.rb', line 54

def create(name)
  unless (field_class = ID_MAP.values.find { |k| k.name == name })
    raise Error, "Unknown extra field '#{name}'"
  end

  self[name] = field_class.new
end

#create_unknown_itemObject



28
29
30
31
32
33
34
35
# File 'lib/zip/extra_field.rb', line 28

def create_unknown_item
  s = +''
  class << s
    alias_method :to_c_dir_bin, :to_s
    alias_method :to_local_bin, :to_s
  end
  self['Unknown'] = s
end

#extra_field_type_exist(binstr, id, len, index) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/zip/extra_field.rb', line 9

def extra_field_type_exist(binstr, id, len, index)
  field_name = ID_MAP[id].name
  if member?(field_name)
    self[field_name].merge(binstr[index, len + 4])
  else
    field_obj        = ID_MAP[id].new(binstr[index, len + 4])
    self[field_name] = field_obj
  end
end

#extra_field_type_unknown(binstr, len, index) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/zip/extra_field.rb', line 19

def extra_field_type_unknown(binstr, len, index)
  create_unknown_item unless self['Unknown']
  if !len || len + 4 > binstr[index..-1].bytesize
    self['Unknown'] << binstr[index..-1]
    return
  end
  self['Unknown'] << binstr[index, len + 4]
end

#local_sizeObject Also known as: length, size



84
85
86
# File 'lib/zip/extra_field.rb', line 84

def local_size
  to_local_bin.bytesize
end

#merge(binstr) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zip/extra_field.rb', line 37

def merge(binstr)
  return if binstr.empty?

  i = 0
  while i < binstr.bytesize
    id  = binstr[i, 2]
    len = binstr[i + 2, 2].to_s.unpack1('v')
    if id && ID_MAP.member?(id)
      extra_field_type_exist(binstr, id, len, i)
    elsif id
      create_unknown_item unless self['Unknown']
      break unless extra_field_type_unknown(binstr, len, i)
    end
    i += len + 4
  end
end

#ordered_valuesObject

place Unknown last, so “extra” data that is missing the proper signature/size does not prevent known fields from being read back in



64
65
66
67
68
# File 'lib/zip/extra_field.rb', line 64

def ordered_values
  result = []
  each { |k, v| k == 'Unknown' ? result.push(v) : result.unshift(v) }
  result
end

#to_c_dir_binObject



76
77
78
# File 'lib/zip/extra_field.rb', line 76

def to_c_dir_bin
  ordered_values.map! { |v| v.to_c_dir_bin.force_encoding('BINARY') }.join
end

#to_local_binObject Also known as: to_s



70
71
72
# File 'lib/zip/extra_field.rb', line 70

def to_local_bin
  ordered_values.map! { |v| v.to_local_bin.force_encoding('BINARY') }.join
end