Skip to content

Commit ee2764a

Browse files
committed
throw an error if the IHDR is missing from the PNG
1 parent 165eac9 commit ee2764a

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/inline.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,15 @@ function display(d::InlineDisplay, M::MIME, x)
8989
end
9090

9191

92-
# look for a short sequence in an array
93-
function get_ind(sub_arr, arr)
94-
nsub = length(sub_arr)
95-
search_end = length(arr) - nsub + 1
96-
return findfirst(i -> all(sub_arr .== arr[i:i+nsub-1]) , 1:search_end)
97-
end
9892

9993
# extract width and height from a PNG file header inside a base64 string
10094
function png_wh(img::String)
10195
# PNG header is 8 bytes, 4 byte chunk size, 4 byte IHDR string, 8 bytes for w, h
102-
decoded = base64decode(img[1:32])
103-
ihdr = get_ind(b"IHDR", decoded) + 4 # find the location of the header
104-
w, h = ntoh.(reinterpret(Int32, decoded[ihdr:ihdr+8-1])) # get the 8 bytes after
96+
decoded = base64decode(img[1:32]) # Base64 encodes 6 bits per character
97+
if any(decoded[13:16] .!= b"IHDR") # check if the header looks reasonable
98+
throw(ArgumentError("Base64-encoded PNG has a badly formed header."))
99+
end
100+
w, h = ntoh.(reinterpret(Int32, decoded[17:24])) # get the 8 bytes after
105101
return w, h
106102
end
107103

0 commit comments

Comments
 (0)