module ChunkyPNG::Chunk
def self.read(io)
-
(ChunkyPNG::Chung::Base)- The loaded chunk instance.
Parameters:
-
io(IO, #read) -- The IO stream to read from.
def self.read(io) length, type = read_bytes(io, 8).unpack("Na4") content = read_bytes(io, length) crc = read_bytes(io, 4).unpack("N").first verify_crc!(type, content, crc) CHUNK_TYPES.fetch(type, Generic).read(type, content) end
def self.read_bytes(io, length)
-
(ChunkyPNG::ExpectationFailed)- If not exactly length
Returns:
-
(String)- A binary string of exactly length bytes.
Parameters:
-
length(Integer) -- The IO exact number of bytes to read. -
io(IO, #read) -- The IO stream to read from.
def self.read_bytes(io, length) data = io.read(length) raise ExpectationFailed, "Couldn't read #{length} bytes from IO stream." if data.nil? || data.bytesize != length data end
def self.verify_crc!(type, content, found_crc)
-
(ChunkyPNG::CRCMismatch)- An exception is raised if
Parameters:
-
found_crc(Integer) -- The chunk's found CRC value. -
content(String) -- The chunk's content. -
type(String) -- The chunk's type.
def self.verify_crc!(type, content, found_crc) expected_crc = Zlib.crc32(content, Zlib.crc32(type)) raise ChunkyPNG::CRCMismatch, "Chuck CRC mismatch!" if found_crc != expected_crc end