Class: Rex::Zip::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/zip/entry.rb

Overview

An Entry represents a logical file or directory to be stored in an Archive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname, data, compmeth, timestamp = nil, attrs = nil, xtra = nil, comment = nil, central_dir_name = nil) ⇒ Entry

Returns a new instance of Entry.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rex/zip/entry.rb', line 14

def initialize(fname, data, compmeth, timestamp=nil, attrs=nil, xtra=nil, comment=nil, central_dir_name=nil)
  @name = fname.unpack("C*").pack("C*")
  @central_dir_name = (central_dir_name ? central_dir_name.unpack("C*").pack("C*") : nil)
  @data = data.unpack("C*").pack("C*")
  @xtra = xtra
  @xtra ||= ''
  @comment = comment
  @comment ||= ''
  @attrs = attrs
  @attrs ||= 0

  # XXX: sanitize timestmap (assume now)
  timestamp ||= Time.now
  @flags = CompFlags.new(0, compmeth, timestamp)

  if (@data)
    compress
  else
    @data = ''
    @info = CompInfo.new(0, 0, 0)
  end
  @compdata ||= ''
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



11
12
13
# File 'lib/rex/zip/entry.rb', line 11

def attrs
  @attrs
end

#central_dir_nameObject

Returns the value of attribute central_dir_name.



11
12
13
# File 'lib/rex/zip/entry.rb', line 11

def central_dir_name
  @central_dir_name
end

#commentObject

Returns the value of attribute comment.



11
12
13
# File 'lib/rex/zip/entry.rb', line 11

def comment
  @comment
end

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/rex/zip/entry.rb', line 12

def data
  @data
end

#flagsObject

Returns the value of attribute flags.



11
12
13
# File 'lib/rex/zip/entry.rb', line 11

def flags
  @flags
end

#infoObject

Returns the value of attribute info.



11
12
13
# File 'lib/rex/zip/entry.rb', line 11

def info
  @info
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/rex/zip/entry.rb', line 11

def name
  @name
end

#xtraObject

Returns the value of attribute xtra.



11
12
13
# File 'lib/rex/zip/entry.rb', line 11

def xtra
  @xtra
end

Instance Method Details

#central_dir_pathObject



78
79
80
81
# File 'lib/rex/zip/entry.rb', line 78

def central_dir_path
  return nil if @central_dir_name.blank?
  get_relative_path(@central_dir_name)
end

#compressObject

Compress the #data and store it for later use. If this entry’s compression method produces a larger blob than the original data, the method is changed to CM_STORE.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rex/zip/entry.rb', line 47

def compress
  @crc = Zlib.crc32(@data, 0)
  case @flags.compmeth

  when CM_STORE
    @compdata = @data

  when CM_DEFLATE
    z = Zlib::Deflate.new(Zlib::BEST_COMPRESSION)
    @compdata = z.deflate(@data, Zlib::FINISH)
    z.close
    @compdata = @compdata[2, @compdata.length-6]

  else
    raise 'Unsupported compression method: %u' % @flags.compmeth
  end

  # if compressing doesn't help, just store it
  if (@compdata.length > @data.length)
    @compdata = @data
    @flags.compmeth = CM_STORE
  end

  @info = CompInfo.new(@crc, @compdata.length, @data.length)
end

#inspectObject



106
107
108
# File 'lib/rex/zip/entry.rb', line 106

def inspect
  "#<#{self.class} name:#{name}, data:#{@data.length} bytes>"
end

#packObject

Return the compressed data in a format suitable for adding to an Archive



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rex/zip/entry.rb', line 87

def pack
  #  - lfh 1
  lfh = LocalFileHdr.new(self)
  ret = lfh.pack

  #  - data 1
  if (@compdata)
    ret << @compdata
  end

  if (@gpbf & GPBF_USE_DATADESC)
    #  - data desc 1
    dd = DataDesc.new(@info)
    ret << dd.pack
  end

  ret
end

#relative_pathObject



74
75
76
# File 'lib/rex/zip/entry.rb', line 74

def relative_path
  get_relative_path(@name)
end