Class: FilesHunter::Decoders::ICO

Inherits:
BeginPatternDecoder show all
Defined in:
lib/fileshunter/Decoders/ICO.rb

Constant Summary collapse

BEGIN_PATTERN_ICO =
Regexp.new("\x00\x00[\x01\x02]\x00.....\x00", nil, 'n')
ALLOWED_BPP_VALUES =
[ 0, 1, 4, 8, 16, 24, 32 ]

Instance Method Summary collapse

Methods inherited from BeginPatternDecoder

#find_segments

Methods inherited from FilesHunter::Decoder

#segments_found, #setup

Instance Method Details

#decode(offset) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fileshunter/Decoders/ICO.rb', line 15

def decode(offset)
  ending_offset = nil

  extension = ((@data[offset+2] == "\x01") ? :ico : :cur)
  nbr_images = BinData::Uint16le.read(@data[offset+4..offset+5])
  invalid_data("@#{offset} - Number of images is 0") if (nbr_images == 0)
  cursor = offset + 6
  # Read all image headers
  images = []
  nbr_images.times do |idx_image|
    #image_width = @data[cursor].ord
    #image_height = @data[cursor+1].ord
    nbr_colors = @data[cursor+2].ord
    invalid_data("@#{cursor} - Invalid ICONDIRENTRY header") if (@data[cursor+3].ord != 0)
    color_planes = BinData::Uint16le.read(@data[cursor+4..cursor+5])
    invalid_data("@#{cursor} - Invalid color planes") if ((extension == :ico) and (color_planes > 1))
    bpp = BinData::Uint16le.read(@data[cursor+6..cursor+7])
    invalid_data("@#{cursor} - Invalid bpp value") if ((extension == :ico) and (!ALLOWED_BPP_VALUES.include?(bpp)))
    invalid_data("@#{cursor} - Invalid number of colors") if ((extension == :ico) and (bpp >= 8) and (nbr_colors != 0))
    image_size = BinData::Uint32le.read(@data[cursor+8..cursor+11])
    invalid_data("@#{cursor} - Invalid image size") if (image_size == 0)
    image_offset = BinData::Uint32le.read(@data[cursor+12..cursor+15])
    images << [ image_offset, image_size ]
    cursor += 16
  end
  progress(cursor)
  # Make sure images are not overlapping
  next_offset_min = cursor-offset
  images.sort.each do |image_offset, image_size|
    invalid_data("@#{cursor} - Invalid image offset: #{image_offset} could not be before #{next_offset_min} as it belongs to another image") if (image_offset < next_offset_min)
    next_offset_min += image_size
  end
  # OK now we consider we might have a valid file
  log_debug "@#{cursor} - #{extension.to_s} file with #{nbr_images} images."
  found_relevant_data(extension)
  (
    :nbr_images => nbr_images
  )
  cursor = offset + next_offset_min
  progress(cursor)
  ending_offset = cursor
  # # Decode each image
  # images.each do |image_offset, image_size|
  #   invalid_data("@#{cursor} - Image offset (#{image_offset}) should be #{cursor-offset}") if (cursor-offset != image_offset)
  #   cursor += image_size
  #   progress(cursor)
  # end
  # ending_offset = cursor

  return ending_offset
end

#get_begin_patternObject



11
12
13
# File 'lib/fileshunter/Decoders/ICO.rb', line 11

def get_begin_pattern
  return BEGIN_PATTERN_ICO, { :offset_inc => 3, :max_regexp_size => 10 }
end