Skip to content

Commit

Permalink
Warn on unsupported magic matches, for the record
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Mar 4, 2024
1 parent 1d63b4a commit 243b8c6
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions script/generate_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ def binary_strings(object)
end
end

def get_matches(parent)
def get_matches(mime, parent)
parent.elements.map {|match|
children = get_matches(match)
children = get_matches(mime, match)

type = match['type']
value = match['value']
offset = match['offset'] || '0'
offset = offset.split(':').map {|x| x.to_i }

mask = match['mask']

# We only support masks of whole bytes against a string type
if mask && (!mask.match?(/\A0x(FF|00)*\z/) || type != 'string')
# We only support masks of whole bytes against a string type
warn "#{mime['type']}: unsupported mask #{match.to_s}"
next nil
end

Expand All @@ -76,6 +78,7 @@ def get_matches(parent)
mask_length = (match_offset[1] - match_offset[0]) / 2
segments << [mask_offset, mask_length]
end

chain = children
segments.reverse_each do |(mask_offset, mask_length)|
masked_value = value[mask_offset, mask_length]
Expand Down Expand Up @@ -108,7 +111,12 @@ def get_matches(parent)
when 'byte'
value = str2int(value)
value = value.chr
when nil
nil
else
warn "#{mime['type']}: unsupported #{type} match: #{match.to_s}"
end

children.empty? ? [offset, value] : [offset, value, children]
}.compact
end
Expand All @@ -133,7 +141,7 @@ def get_matches(parent)
exts = (mime/'glob').map{|x| x['pattern'] =~ /^\*\.([^\[\]]+)$/ ? $1.downcase : nil }.compact
(mime/'magic').each do |magic|
priority = (magic['priority'] || '50').to_i
matches = get_matches(magic)
matches = get_matches(mime, magic)
magics << [priority, type, matches]
end
if !exts.empty?
Expand Down

0 comments on commit 243b8c6

Please sign in to comment.