427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
# File 'lib/tessel.rb', line 427
def decode(bytes, max_pixels: 16384 * 16384, max_metadata_bytes: MAX_METADATA_BYTES, verify_crc: true)
bytes = String(bytes).b
raise DecodeError, "invalid PNG signature" unless bytes.start_with?(SIGNATURE)
raise ArgumentError, "max_metadata_bytes must be non-negative" unless max_metadata_bytes.is_a?(Integer) && !max_metadata_bytes.negative?
chunks = []
offset = SIGNATURE.bytesize
while offset < bytes.bytesize
raise DecodeError.new("truncated PNG chunk", offset: offset) if offset + 12 > bytes.bytesize
length = bytes.byteslice(offset, 4).unpack1("N")
type = bytes.byteslice(offset + 4, 4)
raise DecodeError.new("invalid PNG chunk length", offset: offset, chunk: type) if length > 0x7fff_ffff
raise DecodeError.new("invalid PNG chunk type", offset: offset, chunk: type) unless type.match?(/\A[A-Za-z]{4}\z/)
data_start = offset + 8
data_end = data_start + length
raise DecodeError.new("truncated PNG chunk", offset: offset, chunk: type) if data_end + 4 > bytes.bytesize
data = bytes.byteslice(data_start, length)
actual_crc = bytes.byteslice(data_end, 4).unpack1("N")
if verify_crc && Zlib.crc32(type + data) != actual_crc
raise DecodeError.new("PNG CRC mismatch", offset: offset, chunk: type)
end
chunks << [type, data]
offset = data_end + 4
break if type == "IEND"
end
raise DecodeError.new("trailing data after IEND", offset: offset) unless offset == bytes.bytesize
raise DecodeError, "PNG is missing IEND" unless chunks.last&.first == "IEND"
raise DecodeError, "IEND must be empty" unless chunks.last[1].empty?
raise DecodeError, "PNG is missing IDAT" unless chunks.any? { |type, _| type == "IDAT" }
raise DecodeError, "PNG must have one IHDR" unless chunks.count { |type, _| type == "IHDR" } == 1
idat_index = chunks.index { |type, _| type == "IDAT" }
%w[PLTE tRNS].each do |type|
positions = chunks.each_index.select { |index| chunks[index][0] == type }
raise DecodeError, "duplicate PNG #{type} chunk" if positions.length > 1
raise DecodeError, "invalid PNG chunk order" if positions.first && positions.first > idat_index
end
plte_index = chunks.index { |type, _| type == "PLTE" }
trns_index = chunks.index { |type, _| type == "tRNS" }
raise DecodeError, "invalid PNG chunk order" if plte_index && trns_index && trns_index < plte_index
raise UnsupportedError, "unknown critical PNG chunk" if chunks.any? { |type, _| type.getbyte(0) < 97 && !%w[IHDR PLTE IDAT IEND].include?(type) }
idat_started = false
idat_ended = false
chunks.each do |type, _|
if type == "IDAT"
raise DecodeError, "PNG IDAT chunks must be consecutive" if idat_ended
idat_started = true
elsif idat_started && type != "IEND"
idat_ended = true
end
end
parse(chunks, max_pixels: max_pixels, max_metadata_bytes: max_metadata_bytes)
rescue Zlib::Error => e
raise DecodeError, "invalid PNG data: #{e.message}"
end
|