@@ -231,96 +231,76 @@ static bool png_process_ihdr(struct png_ihdr *ihdr)
231231static void png_reverse_filter_copy_line_rgb (uint32_t * data ,
232232 const uint8_t * decoded , unsigned width , unsigned bpp )
233233{
234- unsigned i ;
234+ uint32_t * data_ptr = NULL ;
235235
236236 bpp /= 8 ;
237237
238- for (i = 0 ; i < width ; i ++ )
238+ for (data_ptr = & data [ 0 ]; data_ptr < data + width ; data_ptr ++ )
239239 {
240- uint32_t r , g , b ;
241-
242- r = * decoded ;
243- decoded += bpp ;
244- g = * decoded ;
245- decoded += bpp ;
246- b = * decoded ;
247- decoded += bpp ;
248- data [i ] = (0xffu << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
240+ uint32_t r = * (decoded );
241+ uint32_t g = * (decoded + bpp );
242+ uint32_t b = * (decoded + bpp + bpp );
243+ decoded += (3 * bpp );
244+ * data_ptr = (0xffu << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
249245 }
250246}
251247
252248static void png_reverse_filter_copy_line_rgba (uint32_t * data ,
253249 const uint8_t * decoded , unsigned width , unsigned bpp )
254250{
255- unsigned i ;
251+ uint32_t * data_ptr = NULL ;
256252
257253 bpp /= 8 ;
258254
259- for (i = 0 ; i < width ; i ++ )
255+ for (data_ptr = & data [ 0 ]; data_ptr < data + width ; data_ptr ++ )
260256 {
261- uint32_t r , g , b , a ;
262- r = * decoded ;
263- decoded += bpp ;
264- g = * decoded ;
265- decoded += bpp ;
266- b = * decoded ;
267- decoded += bpp ;
268- a = * decoded ;
269- decoded += bpp ;
270- data [i ] = (a << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
257+ uint32_t r = * (decoded );
258+ uint32_t g = * (decoded + bpp );
259+ uint32_t b = * (decoded + bpp + bpp );
260+ uint32_t a = * (decoded + bpp + bpp + bpp );
261+ decoded += (4 * bpp );
262+ * data_ptr = (a << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
271263 }
272264}
273265
274266static void png_reverse_filter_copy_line_bw (uint32_t * data ,
275267 const uint8_t * decoded , unsigned width , unsigned depth )
276268{
277- unsigned i , bit ;
278- static const unsigned mul_table [] = { 0 , 0xff , 0x55 , 0 , 0x11 , 0 , 0 , 0 , 0x01 };
279- unsigned mul , mask ;
280-
281- if (depth == 16 )
282- {
283- for (i = 0 ; i < width ; i ++ )
284- {
285- uint32_t val = decoded [i << 1 ];
286- data [i ] = (val * 0x010101 ) | (0xffu << 24 );
287- }
288- return ;
289- }
290-
291- mul = mul_table [depth ];
292- mask = (1 << depth ) - 1 ;
293- bit = 0 ;
294-
295- for (i = 0 ; i < width ; i ++ , bit += depth )
269+ unsigned i ;
270+ static const unsigned
271+ mul_table [] = { 0 , 0xff , 0x55 , 0 , 0x11 , 0 , 0 , 0 , 0x01 };
272+ unsigned mul = mul_table [depth ];
273+ unsigned mask = (1 << depth ) - 1 ;
274+ unsigned bit = 0 ;
275+ uint32_t * data_ptr = NULL ;
276+
277+ for ( i = 0 , data_ptr = & data [0 ]
278+ ; i < width
279+ ; i ++ , data_ptr ++ , bit += depth )
296280 {
297281 unsigned byte = bit >> 3 ;
298282 unsigned val = decoded [byte ] >> (8 - depth - (bit & 7 ));
299283
300284 val &= mask ;
301285 val *= mul ;
302- data [ i ] = (val * 0x010101 ) | (0xffu << 24 );
286+ * data_ptr = (val * 0x010101 ) | (0xffu << 24 );
303287 }
304288}
305289
306290static void png_reverse_filter_copy_line_gray_alpha (uint32_t * data ,
307291 const uint8_t * decoded , unsigned width ,
308292 unsigned bpp )
309293{
310- unsigned i ;
294+ uint32_t * data_ptr = NULL ;
311295
312296 bpp /= 8 ;
313297
314- for (i = 0 ; i < width ; i ++ )
298+ for (data_ptr = & data [ 0 ]; data_ptr < data + width ; data_ptr ++ )
315299 {
316- uint32_t gray , alpha ;
317-
318- gray = * decoded ;
319- decoded += bpp ;
320- alpha = * decoded ;
321- decoded += bpp ;
322-
323- data [i ] = (gray * 0x010101 ) | (alpha << 24 );
300+ uint32_t gray = * (decoded );
301+ uint32_t alpha = * (decoded + bpp );
302+ decoded += (2 * bpp );
303+ * data_ptr = (gray * 0x010101 ) | (alpha << 24 );
324304 }
325305}
326306
@@ -480,10 +460,13 @@ static void png_reverse_filter_adam7_deinterlace_pass(uint32_t *data,
480460 for (y = 0 ; y < pass_height ;
481461 y ++ , data += ihdr -> width * pass -> stride_y , input += pass_width )
482462 {
483- uint32_t * out = data ;
463+ uint32_t * out = data ;
464+ const uint32_t * input_ptr = NULL ;
484465
485- for (x = 0 ; x < pass_width ; x ++ , out += pass -> stride_x )
486- * out = input [x ];
466+ for ( input_ptr = & input [0 ]
467+ ; input_ptr < input + pass_width
468+ ; input_ptr ++ , out += pass -> stride_x )
469+ * out = * input_ptr ;
487470 }
488471}
489472
@@ -626,7 +609,23 @@ static int png_reverse_filter_copy_line(uint32_t *data, const struct png_ihdr *i
626609 switch (ihdr -> color_type )
627610 {
628611 case PNG_IHDR_COLOR_GRAY :
629- png_reverse_filter_copy_line_bw (data , pngp -> decoded_scanline , ihdr -> width , ihdr -> depth );
612+ if (ihdr -> depth == 16 )
613+ {
614+ unsigned i ;
615+ const uint8_t * decoded = pngp -> decoded_scanline ;
616+ unsigned width = ihdr -> width ;
617+ uint32_t * data_ptr = NULL ;
618+
619+ for ( i = 0 , data_ptr = & data [0 ]
620+ ; i < width
621+ ; data_ptr ++ , i ++ )
622+ {
623+ uint32_t val = decoded [i << 1 ];
624+ * data_ptr = (val * 0x010101 ) | (0xffu << 24 );
625+ }
626+ }
627+ else
628+ png_reverse_filter_copy_line_bw (data , pngp -> decoded_scanline , ihdr -> width , ihdr -> depth );
630629 break ;
631630 case PNG_IHDR_COLOR_RGB :
632631 png_reverse_filter_copy_line_rgb (data , pngp -> decoded_scanline , ihdr -> width , ihdr -> depth );
@@ -833,14 +832,17 @@ static int rpng_load_image_argb_process_inflate_init(rpng_t *rpng, uint32_t **da
833832static bool png_read_plte (uint8_t * buf ,
834833 uint32_t * buffer , unsigned entries )
835834{
836- unsigned i ;
835+ uint8_t * buf_ptr = NULL ;
836+ uint32_t * buffer_ptr = NULL ;
837837
838- for (i = 0 ; i < entries ; i ++ )
838+ for ( buf_ptr = & buf [0 ], buffer_ptr = & buffer [0 ]
839+ ; buffer_ptr < buffer + entries
840+ ; buf_ptr += 3 , buffer_ptr ++ )
839841 {
840- uint32_t r = buf [ 3 * i + 0 ] ;
841- uint32_t g = buf [ 3 * i + 1 ] ;
842- uint32_t b = buf [ 3 * i + 2 ] ;
843- buffer [ i ] = (r << 16 ) | (g << 8 ) | (b << 0 ) | (0xffu << 24 );
842+ uint32_t r = * ( buf_ptr ) ;
843+ uint32_t g = * ( buf_ptr + 1 ) ;
844+ uint32_t b = * ( buf_ptr + 2 ) ;
845+ * buffer_ptr = (r << 16 ) | (g << 8 ) | (b << 0 ) | (0xffu << 24 );
844846 }
845847
846848 return true;
@@ -956,16 +958,15 @@ static bool read_chunk_header(uint8_t *buf, uint8_t *buf_end,
956958{
957959 unsigned i ;
958960 uint8_t dword [4 ];
961+ uint8_t * dword_ptr = NULL ;
962+ uint8_t * buf_ptr = NULL ;
959963
960- dword [0 ] = '\0' ;
964+ dword [0 ] = '\0' ;
961965
962- /* Check whether reading the header will overflow
963- * the data buffer */
964- if (buf_end - buf < 8 )
965- return false;
966-
967- for (i = 0 ; i < 4 ; i ++ )
968- dword [i ] = buf [i ];
966+ for ( dword_ptr = & dword [0 ], buf_ptr = & buf [0 ]
967+ ; dword_ptr < dword + 4
968+ ; dword_ptr ++ , buf_ptr ++ )
969+ * dword_ptr = * buf_ptr ;
969970
970971 chunk -> size = dword_be (dword );
971972
@@ -1019,6 +1020,10 @@ bool rpng_iterate_image(rpng_t *rpng)
10191020 if (buf > rpng -> buff_end )
10201021 return false;
10211022
1023+ /* Check whether reading the header will overflow
1024+ * the data buffer */
1025+ if (rpng -> buff_end - buf < 8 )
1026+ return false;
10221027 if (!read_chunk_header (buf , rpng -> buff_end , & chunk ))
10231028 return false;
10241029
0 commit comments