Skip to content

Instantly share code, notes, and snippets.

@tijn
Last active January 1, 2016 16:49
Show Gist options
  • Save tijn/8173052 to your computer and use it in GitHub Desktop.
Save tijn/8173052 to your computer and use it in GitHub Desktop.
Trying to help Arjan
# this script proves that no matter how many bytes you leave out of the original string it won't result in a valid date that is in the specified range (within one year)
string = "\x04\x02R\xA6a\xC4M"
bytes = string.unpack("C*")
MIN = Time.new(2013,1,1)
MAX = Time.new(2013,12,31)
# equivalent to:
# string.bytes
def one_item_removed(array)
array.each_index do |i|
result = array.dup
result[i] = nil
yield result.compact
end
end
def two_items_removed(array, &block)
one_item_removed(array) do |a|
one_item_removed(array, &block)
end
end
def items_removed(array, n, &block)
case n
when 0
block.call array
when 1
one_item_removed(array, &block)
when 2..array.size
items_removed(array, n -1) do |x|
one_item_removed(x, &block)
end
else
raise "what? #{n}"
end
end
def time(bytes)
long = bytes.pack("C*").unpack("L*").first
return "nil" if long.nil?
t = Time.at long
in_range = t > MIN && t < MAX
raise "GEVONDEN!" if in_range
puts "#{t} #{'<===' if in_range}"
end
puts
puts "original"
puts time(bytes)
puts
puts "1 byte removed"
items_removed(bytes, 1) do |x|
puts time(x)
end
(2..bytes.size).each do |n|
puts
puts "#{n} bytes removed"
items_removed(bytes, n) do |x|
puts time(x)
end
end
@tijn
Copy link
Author

tijn commented Dec 29, 2013

I'm quite proud of line 33 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment