From 17453d03c3bdd18a893ae86058a6184939aee026 Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Thu, 13 Jul 2017 10:06:15 -0400 Subject: More unique_ptrs in JBig2 code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL makes CJBig2_Segment own the results: symbol, pattern, huffman, and huffman. This causes a lot more unique_ptr usage in JBig2 code. Change-Id: I1f0a5bfaaf85053658b467bef5325c72d1f496c7 Reviewed-on: https://pdfium-review.googlesource.com/7690 Commit-Queue: Nicolás Peña Reviewed-by: Lei Zhang --- core/fxcodec/jbig2/JBig2_SddProc.cpp | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'core/fxcodec/jbig2/JBig2_SddProc.cpp') diff --git a/core/fxcodec/jbig2/JBig2_SddProc.cpp b/core/fxcodec/jbig2/JBig2_SddProc.cpp index e296e32d21..0d2f35581e 100644 --- a/core/fxcodec/jbig2/JBig2_SddProc.cpp +++ b/core/fxcodec/jbig2/JBig2_SddProc.cpp @@ -22,7 +22,7 @@ #include "core/fxcrt/fx_basic.h" #include "third_party/base/ptr_util.h" -CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith( +std::unique_ptr CJBig2_SDDProc::decode_Arith( CJBig2_ArithDecoder* pArithDecoder, std::vector* gbContext, std::vector* grContext) { @@ -31,7 +31,6 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith( int32_t HCDH; uint32_t SYMWIDTH, TOTWIDTH; int32_t DW; - CJBig2_Image* BS; uint32_t I, J, REFAGGNINST; std::vector EXFLAGS; uint32_t EXINDEX; @@ -69,7 +68,7 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith( HCHEIGHT = 0; NSYMSDECODED = 0; while (NSYMSDECODED < SDNUMNEWSYMS) { - BS = nullptr; + std::unique_ptr BS; IADH->decode(pArithDecoder, &HCDH); HCHEIGHT = HCHEIGHT + HCDH; if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) @@ -218,8 +217,7 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith( return nullptr; } } - SDNEWSYMS[NSYMSDECODED] = pdfium::WrapUnique(BS); - BS = nullptr; + SDNEWSYMS[NSYMSDECODED] = std::move(BS); NSYMSDECODED = NSYMSDECODED + 1; } } @@ -259,10 +257,10 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith( } ++J; } - return pDict.release(); + return pDict; } -CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( +std::unique_ptr CJBig2_SDDProc::decode_Huffman( CJBig2_BitStream* pStream, std::vector* gbContext, std::vector* grContext) { @@ -272,7 +270,6 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( int32_t HCDH; uint32_t SYMWIDTH, TOTWIDTH, HCFIRSTSYM; int32_t DW; - CJBig2_Image *BS, *BHC; uint32_t I, J, REFAGGNINST; std::vector EXFLAGS; uint32_t EXINDEX; @@ -292,7 +289,6 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( std::vector SBSYMS; auto pHuffmanDecoder = pdfium::MakeUnique(pStream); SDNEWSYMS.resize(SDNUMNEWSYMS); - BHC = nullptr; if (SDREFAGG == 0) SDNEWSYMWIDTHS.resize(SDNUMNEWSYMS); auto pDict = pdfium::MakeUnique(); @@ -300,7 +296,7 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( HCHEIGHT = 0; NSYMSDECODED = 0; - BS = nullptr; + std::unique_ptr BS; while (NSYMSDECODED < SDNUMNEWSYMS) { if (pHuffmanDecoder->decodeAValue(SDHUFFDH, &HCDH) != 0) return nullptr; @@ -456,12 +452,10 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( pStream->alignByte(); pStream->offset(2); - if ((uint32_t)nVal != (pStream->getOffset() - nTmp)) { - delete BS; + if ((uint32_t)nVal != (pStream->getOffset() - nTmp)) return nullptr; - } } - SDNEWSYMS[NSYMSDECODED] = pdfium::WrapUnique(BS); + SDNEWSYMS[NSYMSDECODED] = std::move(BS); } if (SDREFAGG == 0) SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH; @@ -472,10 +466,11 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( return nullptr; pStream->alignByte(); + std::unique_ptr BHC; if (BMSIZE == 0) { stride = (TOTWIDTH + 7) >> 3; if (pStream->getByteLeft() >= stride * HCHEIGHT) { - BHC = new CJBig2_Image(TOTWIDTH, HCHEIGHT); + BHC = pdfium::MakeUnique(TOTWIDTH, HCHEIGHT); for (I = 0; I < HCHEIGHT; I++) { JBIG2_memcpy(BHC->m_pData + I * BHC->stride(), pStream->getPointer(), stride); @@ -493,16 +488,14 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( pStream->alignByte(); } nTmp = 0; - if (!BHC) { + if (!BHC) continue; - } + for (I = HCFIRSTSYM; I < NSYMSDECODED; I++) { SDNEWSYMS[I] = pdfium::WrapUnique( BHC->subImage(nTmp, 0, SDNEWSYMWIDTHS[I], HCHEIGHT)); nTmp += SDNEWSYMWIDTHS[I]; } - delete BHC; - BHC = nullptr; } } EXINDEX = 0; @@ -545,5 +538,5 @@ CJBig2_SymbolDict* CJBig2_SDDProc::decode_Huffman( } ++J; } - return pDict.release(); + return pDict; } -- cgit v1.2.3