Skip to content

Instantly share code, notes, and snippets.

@alpicola
Created June 6, 2011 13:40
Show Gist options
  • Save alpicola/1010276 to your computer and use it in GitHub Desktop.
Save alpicola/1010276 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
SOI = "\xff\xd8"
APP0 = "\xff\xe0"
DQT = "\xff\xdb"
DHT = "\xff\xc4"
SOF0 = "\xff\xc0"
SOS = "\xff\xda"
EOI = "\xff\xd9"
ARGF.set_encoding('BINARY')
if ARGF.read(2) != SOI
raise 'file is not jpeg'
end
headers = []
marker = nil
while marker != SOS
marker = ARGF.read(2)
length = ARGF.read(2)
content = ARGF.read(length.unpack('n')[0] - 2)
case marker
when DQT
rand(10).succ.times do
content[rand(content.length)] = rand(0xff).chr
end
end
headers << marker + length + content
end
body = ARGF.read
body.sub!(/#{EOI}\Z/, '') or raise 'invalid jpeg file'
print SOI
print headers.join('')
print body
print EOI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment