@@ -276,41 +276,60 @@ Deno.test("decodePNG() rejects due to unsupported bitDepth", async () => {
276276 ) ;
277277} ) ;
278278
279- Deno . test ( "decodePNG() rejects due to expecting an IDAT or IEND chunk" , async ( ) => {
280- const encoded = await encodePNG ( new Uint8Array ( 4 ) , {
281- width : 1 ,
282- height : 1 ,
283- compression : 0 ,
284- filter : 0 ,
285- interlace : 0 ,
286- } ) ;
287- const view = new DataView (
288- encoded . buffer ,
289- encoded . byteOffset ,
290- encoded . byteLength ,
291- ) ;
292- const [ offset , chunk ] = function ( ) : [ number , Chunk ] {
293- for ( let o = 8 ; o < encoded . length ; ) {
294- const chunk = getChunk ( encoded , view , o ) ;
295- if ( [ 73 , 69 , 78 , 68 ] . every ( ( x , i ) => x === chunk . type [ i ] ) ) {
296- return [ o , chunk ] ;
279+ Deno . test (
280+ "decodePNG() rejects due to a non-IDAT chunk existing in between IDAT chunks" ,
281+ async ( ) => {
282+ let encoded = await encodePNG ( new Uint8Array ( 4 ) , {
283+ width : 1 ,
284+ height : 1 ,
285+ compression : 0 ,
286+ filter : 0 ,
287+ interlace : 0 ,
288+ } ) ;
289+ const view = new DataView (
290+ encoded . buffer ,
291+ encoded . byteOffset ,
292+ encoded . byteLength ,
293+ ) ;
294+ const offset = function ( ) : number {
295+ for ( let o = 8 ; o < encoded . length ; ) {
296+ const chunk = getChunk ( encoded , view , o ) ;
297+ if ( [ 73 , 69 , 78 , 68 ] . every ( ( x , i ) => x === chunk . type [ i ] ) ) {
298+ return o ;
299+ }
300+ o += chunk . length + 12 ;
297301 }
298- o += chunk . length + 12 ;
299- }
300- throw new Error ( "INVALID" ) ;
301- } ( ) ;
302- ++ chunk . type [ 0 ] ;
303- view . setUint32 (
304- offset + 8 + chunk . length ,
305- calcCRC ( encoded . subarray ( offset + 4 , offset + 8 + chunk . length ) ) ,
306- ) ;
302+ throw new Error ( "INVALID" ) ;
303+ } ( ) ;
304+
305+ const chunkJDAT = new Uint8Array ( 12 ) ;
306+ chunkJDAT . set ( [ 74 , 68 , 65 , 84 ] , 4 ) ;
307+ let crc = calcCRC ( chunkJDAT . subarray ( 4 , - 4 ) ) ;
308+ chunkJDAT [ 8 ] = crc >> 24 & 0xFF ;
309+ chunkJDAT [ 9 ] = crc >> 16 & 0xFF ;
310+ chunkJDAT [ 10 ] = crc >> 8 & 0xFF ;
311+ chunkJDAT [ 11 ] = crc & 0xFF ;
312+ const chunkIDAT = new Uint8Array ( 12 ) ;
313+ chunkIDAT . set ( [ 73 , 68 , 65 , 84 ] , 4 ) ;
314+ crc = calcCRC ( chunkIDAT . subarray ( 4 , - 4 ) ) ;
315+ chunkIDAT [ 8 ] = crc >> 24 & 0xFF ;
316+ chunkIDAT [ 9 ] = crc >> 16 & 0xFF ;
317+ chunkIDAT [ 10 ] = crc >> 8 & 0xFF ;
318+ chunkIDAT [ 11 ] = crc & 0xFF ;
319+ encoded = concat ( [
320+ encoded . subarray ( 0 , offset ) ,
321+ chunkJDAT ,
322+ chunkIDAT ,
323+ encoded . subarray ( offset ) ,
324+ ] ) ;
307325
308- await assertRejects (
309- ( ) => decodePNG ( encoded ) ,
310- TypeError ,
311- "An IDAT or IEND chunk was expected. Found: JEND" ,
312- ) ;
313- } ) ;
326+ await assertRejects (
327+ ( ) => decodePNG ( encoded ) ,
328+ TypeError ,
329+ "A non-IDAT chunk (JDAT) was found between IDAT chunks" ,
330+ ) ;
331+ } ,
332+ ) ;
314333
315334Deno . test ( "decodePNG() rejects due to receiving too many PLTE chunks" , async ( ) => {
316335 let encoded = await encodePNG ( Uint8Array . from ( [ 0 , 1 , 2 , 3 ] ) , {
0 commit comments