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

Side by Side Diff: src/images/SkImageDecoder_libjpeg.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 * Copyright 2007 The Android Open Source Project 2 * Copyright 2007 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #include "SkImageDecoder.h" 9 #include "SkImageDecoder.h"
10 #include "SkImageEncoder.h" 10 #include "SkImageEncoder.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 cinfo->mem->max_memory_to_use = 30 * 1024 * 1024; 47 cinfo->mem->max_memory_to_use = 30 * 1024 * 1024;
48 #else 48 #else
49 cinfo->mem->max_memory_to_use = 5 * 1024 * 1024; 49 cinfo->mem->max_memory_to_use = 5 * 1024 * 1024;
50 #endif 50 #endif
51 #endif // SK_BUILD_FOR_ANDROID 51 #endif // SK_BUILD_FOR_ANDROID
52 } 52 }
53 53
54 ////////////////////////////////////////////////////////////////////////// 54 //////////////////////////////////////////////////////////////////////////
55 ////////////////////////////////////////////////////////////////////////// 55 //////////////////////////////////////////////////////////////////////////
56 56
57 static void do_nothing(jpeg_common_struct*, int) { /* do nothing */ }
scroggo 2013/10/01 22:04:14 Could you make this name more specific? Though it
58
57 static void initialize_info(jpeg_decompress_struct* cinfo, skjpeg_source_mgr* sr c_mgr) { 59 static void initialize_info(jpeg_decompress_struct* cinfo, skjpeg_source_mgr* sr c_mgr) {
58 SkASSERT(cinfo != NULL); 60 SkASSERT(cinfo != NULL);
59 SkASSERT(src_mgr != NULL); 61 SkASSERT(src_mgr != NULL);
60 jpeg_create_decompress(cinfo); 62 jpeg_create_decompress(cinfo);
61 overwrite_mem_buffer_size(cinfo); 63 overwrite_mem_buffer_size(cinfo);
62 cinfo->src = src_mgr; 64 cinfo->src = src_mgr;
65 cinfo->err->emit_message = &do_nothing;
scroggo 2013/10/01 22:04:14 Why not do this inside an #if !defined(SK_DEBUG)
63 } 66 }
64 67
65 #ifdef SK_BUILD_FOR_ANDROID 68 #ifdef SK_BUILD_FOR_ANDROID
66 class SkJPEGImageIndex { 69 class SkJPEGImageIndex {
67 public: 70 public:
68 SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder) 71 SkJPEGImageIndex(SkStreamRewindable* stream, SkImageDecoder* decoder)
69 : fSrcMgr(stream, decoder) 72 : fSrcMgr(stream, decoder)
70 , fInfoInitialized(false) 73 , fInfoInitialized(false)
71 , fHuffmanCreated(false) 74 , fHuffmanCreated(false)
72 , fDecompressStarted(false) 75 , fDecompressStarted(false)
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 cinfo.out_color_space == JCS_RGB_565))) 552 cinfo.out_color_space == JCS_RGB_565)))
550 { 553 {
551 JSAMPLE* rowptr = (JSAMPLE*)bm->getPixels(); 554 JSAMPLE* rowptr = (JSAMPLE*)bm->getPixels();
552 INT32 const bpr = bm->rowBytes(); 555 INT32 const bpr = bm->rowBytes();
553 556
554 while (cinfo.output_scanline < cinfo.output_height) { 557 while (cinfo.output_scanline < cinfo.output_height) {
555 int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1); 558 int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1);
556 // if row_count == 0, then we didn't get a scanline, so abort. 559 // if row_count == 0, then we didn't get a scanline, so abort.
557 // if we supported partial images, we might return true in this case 560 // if we supported partial images, we might return true in this case
558 if (0 == row_count) { 561 if (0 == row_count) {
559 return return_false(cinfo, *bm, "read_scanlines"); 562 // Ignore error: return_false(cinfo, *bm, "read_scanlines");
scroggo 2013/10/01 22:04:14 This line can be removed. Same for the other cases
563 return true;
scroggo 2013/10/01 22:04:14 Like I said before, this will leave the remainder
560 } 564 }
561 if (this->shouldCancelDecode()) { 565 if (this->shouldCancelDecode()) {
562 return return_false(cinfo, *bm, "shouldCancelDecode"); 566 return return_false(cinfo, *bm, "shouldCancelDecode");
563 } 567 }
564 rowptr += bpr; 568 rowptr += bpr;
565 } 569 }
566 jpeg_finish_decompress(&cinfo); 570 jpeg_finish_decompress(&cinfo);
567 return true; 571 return true;
568 } 572 }
569 #endif 573 #endif
(...skipping 29 matching lines...) Expand all
599 // Possibly skip initial rows [sampler.srcY0] 603 // Possibly skip initial rows [sampler.srcY0]
600 if (!skip_src_rows(&cinfo, srcRow, sampler.srcY0())) { 604 if (!skip_src_rows(&cinfo, srcRow, sampler.srcY0())) {
601 return return_false(cinfo, *bm, "skip rows"); 605 return return_false(cinfo, *bm, "skip rows");
602 } 606 }
603 607
604 // now loop through scanlines until y == bm->height() - 1 608 // now loop through scanlines until y == bm->height() - 1
605 for (int y = 0;; y++) { 609 for (int y = 0;; y++) {
606 JSAMPLE* rowptr = (JSAMPLE*)srcRow; 610 JSAMPLE* rowptr = (JSAMPLE*)srcRow;
607 int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1); 611 int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1);
608 if (0 == row_count) { 612 if (0 == row_count) {
609 return return_false(cinfo, *bm, "read_scanlines"); 613 // Ignore error: return_false(cinfo, *bm, "read_scanlines");
614 return true;
610 } 615 }
611 if (this->shouldCancelDecode()) { 616 if (this->shouldCancelDecode()) {
612 return return_false(cinfo, *bm, "shouldCancelDecode"); 617 return return_false(cinfo, *bm, "shouldCancelDecode");
613 } 618 }
614 619
615 if (JCS_CMYK == cinfo.out_color_space) { 620 if (JCS_CMYK == cinfo.out_color_space) {
616 convert_CMYK_to_RGB(srcRow, cinfo.output_width); 621 convert_CMYK_to_RGB(srcRow, cinfo.output_width);
617 } 622 }
618 623
619 sampler.next(srcRow); 624 sampler.next(srcRow);
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 INT32 const bpr = bitmap.rowBytes(); 786 INT32 const bpr = bitmap.rowBytes();
782 int rowTotalCount = 0; 787 int rowTotalCount = 0;
783 788
784 while (rowTotalCount < height) { 789 while (rowTotalCount < height) {
785 int rowCount = jpeg_read_tile_scanline(cinfo, 790 int rowCount = jpeg_read_tile_scanline(cinfo,
786 fImageIndex->huffmanIndex(), 791 fImageIndex->huffmanIndex(),
787 &rowptr); 792 &rowptr);
788 // if row_count == 0, then we didn't get a scanline, so abort. 793 // if row_count == 0, then we didn't get a scanline, so abort.
789 // if we supported partial images, we might return true in this case 794 // if we supported partial images, we might return true in this case
790 if (0 == rowCount) { 795 if (0 == rowCount) {
791 return return_false(*cinfo, bitmap, "read_scanlines"); 796 // Ignore error: return_false(*cinfo, bitmap, "read_scanlines");
797 return true;
792 } 798 }
793 if (this->shouldCancelDecode()) { 799 if (this->shouldCancelDecode()) {
794 return return_false(*cinfo, bitmap, "shouldCancelDecode"); 800 return return_false(*cinfo, bitmap, "shouldCancelDecode");
795 } 801 }
796 rowTotalCount += rowCount; 802 rowTotalCount += rowCount;
797 rowptr += bpr; 803 rowptr += bpr;
798 } 804 }
799 805
800 if (swapOnly) { 806 if (swapOnly) {
801 bm->swap(bitmap); 807 bm->swap(bitmap);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 // Possibly skip initial rows [sampler.srcY0] 844 // Possibly skip initial rows [sampler.srcY0]
839 if (!skip_src_rows_tile(cinfo, fImageIndex->huffmanIndex(), srcRow, sampler. srcY0())) { 845 if (!skip_src_rows_tile(cinfo, fImageIndex->huffmanIndex(), srcRow, sampler. srcY0())) {
840 return return_false(*cinfo, bitmap, "skip rows"); 846 return return_false(*cinfo, bitmap, "skip rows");
841 } 847 }
842 848
843 // now loop through scanlines until y == bitmap->height() - 1 849 // now loop through scanlines until y == bitmap->height() - 1
844 for (int y = 0;; y++) { 850 for (int y = 0;; y++) {
845 JSAMPLE* rowptr = (JSAMPLE*)srcRow; 851 JSAMPLE* rowptr = (JSAMPLE*)srcRow;
846 int row_count = jpeg_read_tile_scanline(cinfo, fImageIndex->huffmanIndex (), &rowptr); 852 int row_count = jpeg_read_tile_scanline(cinfo, fImageIndex->huffmanIndex (), &rowptr);
847 if (0 == row_count) { 853 if (0 == row_count) {
848 return return_false(*cinfo, bitmap, "read_scanlines"); 854 // Ignore error: return_false(*cinfo, bitmap, "read_scanlines");
855 return true;
849 } 856 }
850 if (this->shouldCancelDecode()) { 857 if (this->shouldCancelDecode()) {
851 return return_false(*cinfo, bitmap, "shouldCancelDecode"); 858 return return_false(*cinfo, bitmap, "shouldCancelDecode");
852 } 859 }
853 860
854 if (JCS_CMYK == cinfo->out_color_space) { 861 if (JCS_CMYK == cinfo->out_color_space) {
855 convert_CMYK_to_RGB(srcRow, width); 862 convert_CMYK_to_RGB(srcRow, width);
856 } 863 }
857 864
858 sampler.next(srcRow); 865 sampler.next(srcRow);
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 return SkImageDecoder::kUnknown_Format; 1159 return SkImageDecoder::kUnknown_Format;
1153 } 1160 }
1154 1161
1155 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { 1162 static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) {
1156 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL; 1163 return (SkImageEncoder::kJPEG_Type == t) ? SkNEW(SkJPEGImageEncoder) : NULL;
1157 } 1164 }
1158 1165
1159 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory); 1166 static SkImageDecoder_DecodeReg gDReg(sk_libjpeg_dfactory);
1160 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg); 1167 static SkImageDecoder_FormatReg gFormatReg(get_format_jpeg);
1161 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); 1168 static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698