Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: src/images/SkImageDecoder_libpng.cpp

Issue 24449003: Make Jpeg decoding more fault resistant. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: lint changes, bugfix, description changed Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkImageDecoder.h" 10 #include "SkImageDecoder.h"
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 png_bytep trans; 192 png_bytep trans;
193 int num_trans; 193 int num_trans;
194 194
195 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { 195 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
196 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL); 196 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL);
197 return num_trans > 0; 197 return num_trans > 0;
198 } 198 }
199 return false; 199 return false;
200 } 200 }
201 201
202 void do_nothing(png_structp, png_const_charp) { /* do nothing */ }
scroggo 2013/10/01 22:04:14 More specific name?
203
204
202 bool SkPNGImageDecoder::onDecodeInit(SkStream* sk_stream, png_structp *png_ptrp, 205 bool SkPNGImageDecoder::onDecodeInit(SkStream* sk_stream, png_structp *png_ptrp,
203 png_infop *info_ptrp) { 206 png_infop *info_ptrp) {
204 /* Create and initialize the png_struct with the desired error handler 207 /* Create and initialize the png_struct with the desired error handler
205 * functions. If you want to use the default stderr and longjump method, 208 * functions. If you want to use the default stderr and longjump method,
206 * you can supply NULL for the last three parameters. We also supply the 209 * you can supply NULL for the last three parameters. We also supply the
207 * the compiler header file version, so that we know if the application 210 * the compiler header file version, so that we know if the application
208 * was compiled with a compatible version of the library. */ 211 * was compiled with a compatible version of the library. */
209 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 212 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
210 NULL, sk_error_fn, NULL); 213 NULL, sk_error_fn, NULL);
211 // png_voidp user_error_ptr, user_error_fn, user_warning_fn); 214 // png_voidp user_error_ptr, user_error_fn, user_warning_fn);
212 if (png_ptr == NULL) { 215 if (png_ptr == NULL) {
213 return false; 216 return false;
214 } 217 }
218
219 png_error_ptr warning_fn = &do_nothing;
scroggo 2013/10/01 22:04:14 Again, this stuff should only be done #if !define
220 png_voidp error_ptr = png_get_error_ptr(png_ptr);
221 png_error_ptr error_fn = NULL; // leave as default
222 png_set_error_fn(png_ptr, error_ptr, error_fn, warning_fn);
223
215 *png_ptrp = png_ptr; 224 *png_ptrp = png_ptr;
216 225
217 /* Allocate/initialize the memory for image information. */ 226 /* Allocate/initialize the memory for image information. */
218 png_infop info_ptr = png_create_info_struct(png_ptr); 227 png_infop info_ptr = png_create_info_struct(png_ptr);
219 if (info_ptr == NULL) { 228 if (info_ptr == NULL) {
220 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL); 229 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
221 return false; 230 return false;
222 } 231 }
223 *info_ptrp = info_ptr; 232 *info_ptrp = info_ptr;
224 233
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 which means we will find more matches than we should. The real 497 which means we will find more matches than we should. The real
489 fix seems to be to see the actual 16bit components, do the 498 fix seems to be to see the actual 16bit components, do the
490 compare, and then knock it down to 8bits ourselves. 499 compare, and then knock it down to 8bits ourselves.
491 */ 500 */
492 if (colorType & PNG_COLOR_MASK_COLOR) { 501 if (colorType & PNG_COLOR_MASK_COLOR) {
493 if (16 == bitDepth) { 502 if (16 == bitDepth) {
494 *theTranspColorp = SkPackARGB32(0xFF, transpColor->red >> 8, 503 *theTranspColorp = SkPackARGB32(0xFF, transpColor->red >> 8,
495 transpColor->green >> 8, 504 transpColor->green >> 8,
496 transpColor->blue >> 8); 505 transpColor->blue >> 8);
497 } else { 506 } else {
498 *theTranspColorp = SkPackARGB32(0xFF, transpColor->red, 507 *theTranspColorp = SkPackARGB32(0xFF,
499 transpColor->green, 508 0xFF & (transpColor->red),
500 transpColor->blue); 509 0xFF & (transpColor->green),
510 0xFF & (transpColor->blue));
511 /* We apply the mask because in a very small
scroggo 2013/10/01 22:04:14 Shouldn't this go above the code it applies to?
512 number of corrupt PNGs, (transpColor->red > 255)
513 and (bitDepth == 8), for certain versions of libpng. */
501 } 514 }
502 } else { // gray 515 } else { // gray
503 if (16 == bitDepth) { 516 if (16 == bitDepth) {
504 *theTranspColorp = SkPackARGB32(0xFF, transpColor->gray >> 8 , 517 *theTranspColorp = SkPackARGB32(0xFF, transpColor->gray >> 8 ,
505 transpColor->gray >> 8, 518 transpColor->gray >> 8,
506 transpColor->gray >> 8); 519 transpColor->gray >> 8);
507 } else { 520 } else {
508 *theTranspColorp = SkPackARGB32(0xFF, transpColor->gray, 521 *theTranspColorp = SkPackARGB32(0xFF,
scroggo 2013/10/01 22:04:14 Similar comment explaining the mask?
509 transpColor->gray, 522 0xFF & (transpColor->gray),
510 transpColor->gray); 523 0xFF & (transpColor->gray),
524 0xFF & (transpColor->gray));
511 } 525 }
512 } 526 }
513 } 527 }
514 528
515 if (valid || 529 if (valid ||
516 PNG_COLOR_TYPE_RGB_ALPHA == colorType || 530 PNG_COLOR_TYPE_RGB_ALPHA == colorType ||
517 PNG_COLOR_TYPE_GRAY_ALPHA == colorType) { 531 PNG_COLOR_TYPE_GRAY_ALPHA == colorType) {
518 *hasAlphap = true; 532 *hasAlphap = true;
519 } 533 }
520 534
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after
1192 return SkImageDecoder::kUnknown_Format; 1206 return SkImageDecoder::kUnknown_Format;
1193 } 1207 }
1194 1208
1195 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { 1209 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) {
1196 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; 1210 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL;
1197 } 1211 }
1198 1212
1199 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory); 1213 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory);
1200 static SkImageDecoder_FormatReg gFormatReg(get_format_png); 1214 static SkImageDecoder_FormatReg gFormatReg(get_format_png);
1201 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); 1215 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698