Created
June 6, 2011 13:40
-
-
Save alpicola/1010276 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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