Advertisement
Guest User

Untitled

a guest
Feb 12th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def extract_chunk(input, output)
  2.    lenword = input.read(4)
  3.    length = lenword.unpack('N')[0]
  4.    type = input.read(4)
  5.    data = length>0 ? input.read(length) : ""
  6.    crc = input.read(4)
  7.    return nil if length<0 || !(('A'..'z')===type[0,1])
  8.    #return nil if validate_crc(type+data, crc)
  9.    output.write lenword
  10.    output.write type
  11.    output.write data
  12.    output.write crc
  13.    return type
  14. end
  15.  
  16. def extract_png(input, output)
  17.     hdr = input.read(8)
  18.     raise "Not a PNG File" if hdr[0,4]!= "\211PNG"
  19.     raise "file not in binary mode" if hdr[4,4]!="\r\n\032\n"
  20.     output.write(hdr)
  21.     loop do
  22.       chunk_type = extract_chunk(input,output)
  23.       p chunk_type
  24.       break if  chunk_type.nil? || chunk_type == 'IEND'
  25.     end
  26. end
  27.  
  28. i=0
  29. fp = File.new("has_embedded_png.dat","rb")
  30. begin
  31.     m= /\211PNG/.match(fp.read)
  32.     raise "no PNG" if !m
  33.     fp.seek(m.begin(0))
  34.     #fp.seek 22946097
  35.     ofp = File.new("out#{i}.png","wb")
  36.     extract_png(fp,ofp)
  37.     i = i + 1
  38. end while m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement