diff options
author | Lei Zhang <thestig@chromium.org> | 2015-10-07 21:31:16 -0700 |
---|---|---|
committer | Lei Zhang <thestig@chromium.org> | 2015-10-07 21:31:16 -0700 |
commit | 26c9e2caf07b57aeec24a13dd0a824d05af37fa2 (patch) | |
tree | cc0b8de203a983b5cdae1fd6bf851ebf81a0d41b /core/src/fxcodec | |
parent | 1862e57553c857596b29db8a176a98ade956ea1b (diff) | |
download | pdfium-26c9e2caf07b57aeec24a13dd0a824d05af37fa2.tar.xz |
Merge to XFA: Remove gotos in JBig2 code.
TBR=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1382613003 .
(cherry picked from commit 8a9ce5778e0789beb7ba15a62c592a353a51bcdd)
Review URL: https://codereview.chromium.org/1397683002 .
Diffstat (limited to 'core/src/fxcodec')
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_Context.cpp | 1182 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_Context.h | 2 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_GsidProc.cpp | 114 | ||||
-rw-r--r-- | core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp | 87 |
4 files changed, 603 insertions, 782 deletions
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.cpp b/core/src/fxcodec/jbig2/JBig2_Context.cpp index ca895e5b9c..3e0ebb36e3 100644 --- a/core/src/fxcodec/jbig2/JBig2_Context.cpp +++ b/core/src/fxcodec/jbig2/JBig2_Context.cpp @@ -15,6 +15,18 @@ #include "JBig2_SddProc.h" #include "JBig2_TrdProc.h" +namespace { + +size_t GetHuffContextSize(uint8_t val) { + return val == 0 ? 65536 : val == 1 ? 8192 : 1024; +} + +size_t GetRefAggContextSize(FX_BOOL val) { + return val ? 1024 : 8192; +} + +} // namespace + // Implement a very small least recently used (LRU) cache. It is very // common for a JBIG2 dictionary to span multiple pages in a PDF file, // and we do not want to decode the same dictionary over and over @@ -57,7 +69,6 @@ CJBig2_Context::CJBig2_Context(const uint8_t* pGlobalData, m_PauseStep(10), m_pPause(pPause), m_ProcessingStatus(FXCODEC_STATUS_FRAME_READY), - m_pArithDecoder(NULL), m_gbContext(NULL), m_dwOffset(0), m_pSymbolDictCache(pSymbolDictCache) { @@ -72,8 +83,6 @@ CJBig2_Context::CJBig2_Context(const uint8_t* pGlobalData, } CJBig2_Context::~CJBig2_Context() { - delete m_pArithDecoder; - m_pArithDecoder = NULL; FX_Free(m_gbContext); m_gbContext = NULL; delete m_pGlobalContext; @@ -101,7 +110,7 @@ int32_t CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause) { m_PauseStep = 2; return JBIG2_SUCCESS; } - if ((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) { + if (nRet == JBIG2_END_OF_PAGE || nRet == JBIG2_END_OF_FILE) { m_pSegment.reset(); return JBIG2_SUCCESS; } @@ -152,7 +161,7 @@ int32_t CJBig2_Context::decode_RandomOrgnazation(IFX_Pause* pPause) { for (; m_nSegmentDecoded < m_SegmentList.size(); ++m_nSegmentDecoded) { int32_t nRet = parseSegmentData(m_SegmentList.get(m_nSegmentDecoded), pPause); - if ((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) + if (nRet == JBIG2_END_OF_PAGE || nRet == JBIG2_END_OF_FILE) return JBIG2_SUCCESS; if (nRet != JBIG2_SUCCESS) @@ -221,15 +230,14 @@ int32_t CJBig2_Context::Continue(IFX_Pause* pPause) { } CJBig2_Segment* CJBig2_Context::findSegmentByNumber(FX_DWORD dwNumber) { - CJBig2_Segment* pSeg; if (m_pGlobalContext) { - pSeg = m_pGlobalContext->findSegmentByNumber(dwNumber); + CJBig2_Segment* pSeg = m_pGlobalContext->findSegmentByNumber(dwNumber); if (pSeg) { return pSeg; } } for (size_t i = 0; i < m_SegmentList.size(); ++i) { - pSeg = m_SegmentList.get(i); + CJBig2_Segment* pSeg = m_SegmentList.get(i); if (pSeg->m_dwNumber == dwNumber) { return pSeg; } @@ -240,24 +248,21 @@ CJBig2_Segment* CJBig2_Context::findReferredSegmentByTypeAndIndex( CJBig2_Segment* pSegment, uint8_t cType, int32_t nIndex) { - CJBig2_Segment* pSeg; - int32_t i, count; - count = 0; - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { - pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); + int32_t count = 0; + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { + CJBig2_Segment* pSeg = + findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); if (pSeg && pSeg->m_cFlags.s.type == cType) { - if (count == nIndex) { + if (count == nIndex) return pSeg; - } else { - count++; - } + ++count; } } return NULL; } int32_t CJBig2_Context::parseSegmentHeader(CJBig2_Segment* pSegment) { - if ((m_pStream->readInteger(&pSegment->m_dwNumber) != 0) || - (m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0)) { + if (m_pStream->readInteger(&pSegment->m_dwNumber) != 0 || + m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0) { return JBIG2_ERROR_TOO_SHORT; } @@ -287,7 +292,7 @@ int32_t CJBig2_Context::parseSegmentHeader(CJBig2_Segment* pSegment) { if (pSegment->m_nReferred_to_segment_count) { pSegment->m_pReferred_to_segment_numbers = FX_Alloc(FX_DWORD, pSegment->m_nReferred_to_segment_count); - for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { switch (cSSize) { case 1: if (m_pStream->read1Byte(&cTemp) != 0) @@ -374,12 +379,12 @@ int32_t CJBig2_Context::ProcessingParseSegmentData(CJBig2_Segment* pSegment, case 48: { FX_WORD wTemp; nonstd::unique_ptr<JBig2PageInfo> pPageInfo(new JBig2PageInfo); - if ((m_pStream->readInteger(&pPageInfo->m_dwWidth) != 0) || - (m_pStream->readInteger(&pPageInfo->m_dwHeight) != 0) || - (m_pStream->readInteger(&pPageInfo->m_dwResolutionX) != 0) || - (m_pStream->readInteger(&pPageInfo->m_dwResolutionY) != 0) || - (m_pStream->read1Byte(&pPageInfo->m_cFlags) != 0) || - (m_pStream->readShortInteger(&wTemp) != 0)) { + if (m_pStream->readInteger(&pPageInfo->m_dwWidth) != 0 || + m_pStream->readInteger(&pPageInfo->m_dwHeight) != 0 || + m_pStream->readInteger(&pPageInfo->m_dwResolutionX) != 0 || + m_pStream->readInteger(&pPageInfo->m_dwResolutionY) != 0 || + m_pStream->read1Byte(&pPageInfo->m_cFlags) != 0 || + m_pStream->readShortInteger(&wTemp) != 0) { return JBIG2_ERROR_TOO_SHORT; } pPageInfo->m_bIsStriped = ((wTemp >> 15) & 1) ? TRUE : FALSE; @@ -428,200 +433,175 @@ int32_t CJBig2_Context::ProcessingParseSegmentData(CJBig2_Segment* pSegment, int32_t CJBig2_Context::parseSymbolDict(CJBig2_Segment* pSegment, IFX_Pause* pPause) { - FX_DWORD dwTemp; FX_WORD wFlags; - uint8_t cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST; - CJBig2_HuffmanTable* Table_B1 = nullptr; - CJBig2_HuffmanTable* Table_B2 = nullptr; - CJBig2_HuffmanTable* Table_B3 = nullptr; - CJBig2_HuffmanTable* Table_B4 = nullptr; - CJBig2_HuffmanTable* Table_B5 = nullptr; - int32_t i, nIndex, nRet; - CJBig2_Segment* pSeg = nullptr; - CJBig2_Segment* pLRSeg = nullptr; - FX_BOOL bUsed; - CJBig2_Image** SDINSYMS = nullptr; - JBig2ArithCtx* gbContext = nullptr; - JBig2ArithCtx* grContext = nullptr; - CJBig2_ArithDecoder* pArithDecoder; - CJBig2_SDDProc* pSymbolDictDecoder = new CJBig2_SDDProc(); - const uint8_t* key = pSegment->m_pData; - FX_BOOL cache_hit = false; - if (m_pStream->readShortInteger(&wFlags) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } + if (m_pStream->readShortInteger(&wFlags) != 0) + return JBIG2_ERROR_TOO_SHORT; + + nonstd::unique_ptr<CJBig2_SDDProc> pSymbolDictDecoder(new CJBig2_SDDProc); pSymbolDictDecoder->SDHUFF = wFlags & 0x0001; pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001; pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003; pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003; - cSDHUFFDH = (wFlags >> 2) & 0x0003; - cSDHUFFDW = (wFlags >> 4) & 0x0003; - cSDHUFFBMSIZE = (wFlags >> 6) & 0x0001; - cSDHUFFAGGINST = (wFlags >> 7) & 0x0001; + uint8_t cSDHUFFDH = (wFlags >> 2) & 0x0003; + uint8_t cSDHUFFDW = (wFlags >> 4) & 0x0003; + uint8_t cSDHUFFBMSIZE = (wFlags >> 6) & 0x0001; + uint8_t cSDHUFFAGGINST = (wFlags >> 7) & 0x0001; if (pSymbolDictDecoder->SDHUFF == 0) { - if (pSymbolDictDecoder->SDTEMPLATE == 0) { - dwTemp = 8; - } else { - dwTemp = 2; - } - for (i = 0; i < (int32_t)dwTemp; i++) { - if (m_pStream->read1Byte((uint8_t*)&pSymbolDictDecoder->SDAT[i]) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } + const FX_DWORD dwTemp = (pSymbolDictDecoder->SDTEMPLATE == 0) ? 8 : 2; + for (FX_DWORD i = 0; i < dwTemp; ++i) { + if (m_pStream->read1Byte((uint8_t*)&pSymbolDictDecoder->SDAT[i]) != 0) + return JBIG2_ERROR_TOO_SHORT; } } - if ((pSymbolDictDecoder->SDREFAGG == 1) && - (pSymbolDictDecoder->SDRTEMPLATE == 0)) { - for (i = 0; i < 4; i++) { - if (m_pStream->read1Byte((uint8_t*)&pSymbolDictDecoder->SDRAT[i]) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } + if (pSymbolDictDecoder->SDREFAGG == 1 && + pSymbolDictDecoder->SDRTEMPLATE == 0) { + for (int32_t i = 0; i < 4; ++i) { + if (m_pStream->read1Byte((uint8_t*)&pSymbolDictDecoder->SDRAT[i]) != 0) + return JBIG2_ERROR_TOO_SHORT; } } - if ((m_pStream->readInteger(&pSymbolDictDecoder->SDNUMEXSYMS) != 0) || - (m_pStream->readInteger(&pSymbolDictDecoder->SDNUMNEWSYMS) != 0)) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; + if (m_pStream->readInteger(&pSymbolDictDecoder->SDNUMEXSYMS) != 0 || + m_pStream->readInteger(&pSymbolDictDecoder->SDNUMNEWSYMS) != 0) { + return JBIG2_ERROR_TOO_SHORT; } if (pSymbolDictDecoder->SDNUMEXSYMS > JBIG2_MAX_EXPORT_SYSMBOLS || pSymbolDictDecoder->SDNUMNEWSYMS > JBIG2_MAX_NEW_SYSMBOLS) { - nRet = JBIG2_ERROR_LIMIT; - goto failed; + return JBIG2_ERROR_LIMIT; } - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { - if (!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { + if (!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) + return JBIG2_ERROR_FATAL; } + CJBig2_Segment* pLRSeg = nullptr; pSymbolDictDecoder->SDNUMINSYMS = 0; - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { - pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { + CJBig2_Segment* pSeg = + findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); if (pSeg->m_cFlags.s.type == 0) { pSymbolDictDecoder->SDNUMINSYMS += pSeg->m_Result.sd->SDNUMEXSYMS; pLRSeg = pSeg; } } - if (pSymbolDictDecoder->SDNUMINSYMS == 0) { - SDINSYMS = NULL; - } else { - SDINSYMS = FX_Alloc(CJBig2_Image*, pSymbolDictDecoder->SDNUMINSYMS); - dwTemp = 0; - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { - pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); + + nonstd::unique_ptr<CJBig2_Image*, FxFreeDeleter> SDINSYMS; + if (pSymbolDictDecoder->SDNUMINSYMS != 0) { + SDINSYMS.reset(FX_Alloc(CJBig2_Image*, pSymbolDictDecoder->SDNUMINSYMS)); + FX_DWORD dwTemp = 0; + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { + CJBig2_Segment* pSeg = + findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); if (pSeg->m_cFlags.s.type == 0) { - JBIG2_memcpy(SDINSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS, + JBIG2_memcpy(SDINSYMS.get() + dwTemp, pSeg->m_Result.sd->SDEXSYMS, pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*)); dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS; } } } - pSymbolDictDecoder->SDINSYMS = SDINSYMS; + pSymbolDictDecoder->SDINSYMS = SDINSYMS.get(); + + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B1; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B2; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B3; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B4; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B5; if (pSymbolDictDecoder->SDHUFF == 1) { - if ((cSDHUFFDH == 2) || (cSDHUFFDW == 2)) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } - nIndex = 0; + if (cSDHUFFDH == 2 || cSDHUFFDW == 2) + return JBIG2_ERROR_FATAL; + + int32_t nIndex = 0; if (cSDHUFFDH == 0) { - Table_B4 = new CJBig2_HuffmanTable(HuffmanTable_B4, - FX_ArraySize(HuffmanTable_B4), - HuffmanTable_HTOOB_B4); - pSymbolDictDecoder->SDHUFFDH = Table_B4; + Table_B4.reset(new CJBig2_HuffmanTable(HuffmanTable_B4, + FX_ArraySize(HuffmanTable_B4), + HuffmanTable_HTOOB_B4)); + pSymbolDictDecoder->SDHUFFDH = Table_B4.get(); } else if (cSDHUFFDH == 1) { - Table_B5 = new CJBig2_HuffmanTable(HuffmanTable_B5, - FX_ArraySize(HuffmanTable_B5), - HuffmanTable_HTOOB_B5); - pSymbolDictDecoder->SDHUFFDH = Table_B5; + Table_B5.reset(new CJBig2_HuffmanTable(HuffmanTable_B5, + FX_ArraySize(HuffmanTable_B5), + HuffmanTable_HTOOB_B5)); + pSymbolDictDecoder->SDHUFFDH = Table_B5.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pSymbolDictDecoder->SDHUFFDH = pSeg->m_Result.ht; } if (cSDHUFFDW == 0) { - Table_B2 = new CJBig2_HuffmanTable(HuffmanTable_B2, - FX_ArraySize(HuffmanTable_B2), - HuffmanTable_HTOOB_B2); - pSymbolDictDecoder->SDHUFFDW = Table_B2; + Table_B2.reset(new CJBig2_HuffmanTable(HuffmanTable_B2, + FX_ArraySize(HuffmanTable_B2), + HuffmanTable_HTOOB_B2)); + pSymbolDictDecoder->SDHUFFDW = Table_B2.get(); } else if (cSDHUFFDW == 1) { - Table_B3 = new CJBig2_HuffmanTable(HuffmanTable_B3, - FX_ArraySize(HuffmanTable_B3), - HuffmanTable_HTOOB_B3); - pSymbolDictDecoder->SDHUFFDW = Table_B3; + Table_B3.reset(new CJBig2_HuffmanTable(HuffmanTable_B3, + FX_ArraySize(HuffmanTable_B3), + HuffmanTable_HTOOB_B3)); + pSymbolDictDecoder->SDHUFFDW = Table_B3.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pSymbolDictDecoder->SDHUFFDW = pSeg->m_Result.ht; } if (cSDHUFFBMSIZE == 0) { - Table_B1 = new CJBig2_HuffmanTable(HuffmanTable_B1, - FX_ArraySize(HuffmanTable_B1), - HuffmanTable_HTOOB_B1); - pSymbolDictDecoder->SDHUFFBMSIZE = Table_B1; + Table_B1.reset(new CJBig2_HuffmanTable(HuffmanTable_B1, + FX_ArraySize(HuffmanTable_B1), + HuffmanTable_HTOOB_B1)); + pSymbolDictDecoder->SDHUFFBMSIZE = Table_B1.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pSymbolDictDecoder->SDHUFFBMSIZE = pSeg->m_Result.ht; } if (pSymbolDictDecoder->SDREFAGG == 1) { if (cSDHUFFAGGINST == 0) { if (!Table_B1) { - Table_B1 = new CJBig2_HuffmanTable(HuffmanTable_B1, - FX_ArraySize(HuffmanTable_B1), - HuffmanTable_HTOOB_B1); + Table_B1.reset(new CJBig2_HuffmanTable(HuffmanTable_B1, + FX_ArraySize(HuffmanTable_B1), + HuffmanTable_HTOOB_B1)); } - pSymbolDictDecoder->SDHUFFAGGINST = Table_B1; + pSymbolDictDecoder->SDHUFFAGGINST = Table_B1.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pSymbolDictDecoder->SDHUFFAGGINST = pSeg->m_Result.ht; } } } + + nonstd::unique_ptr<JBig2ArithCtx, FxFreeDeleter> gbContext; + nonstd::unique_ptr<JBig2ArithCtx, FxFreeDeleter> grContext; if ((wFlags & 0x0100) && pLRSeg && pLRSeg->m_Result.sd->m_bContextRetained) { if (pSymbolDictDecoder->SDHUFF == 0) { - dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 - ? 65536 - : pSymbolDictDecoder->SDTEMPLATE == 1 ? 8192 : 1024; - gbContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memcpy(gbContext, pLRSeg->m_Result.sd->m_gbContext, - sizeof(JBig2ArithCtx) * dwTemp); + const size_t size = GetHuffContextSize(pSymbolDictDecoder->SDTEMPLATE); + gbContext.reset(FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memcpy(gbContext.get(), pLRSeg->m_Result.sd->m_gbContext, + sizeof(JBig2ArithCtx) * size); } if (pSymbolDictDecoder->SDREFAGG == 1) { - dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13; - grContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memcpy(grContext, pLRSeg->m_Result.sd->m_grContext, - sizeof(JBig2ArithCtx) * dwTemp); + const size_t size = GetRefAggContextSize(pSymbolDictDecoder->SDRTEMPLATE); + grContext.reset(FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memcpy(grContext.get(), pLRSeg->m_Result.sd->m_grContext, + sizeof(JBig2ArithCtx) * size); } } else { if (pSymbolDictDecoder->SDHUFF == 0) { - dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 - ? 65536 - : pSymbolDictDecoder->SDTEMPLATE == 1 ? 8192 : 1024; - gbContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx) * dwTemp); + const size_t size = GetHuffContextSize(pSymbolDictDecoder->SDTEMPLATE); + gbContext.reset(FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memset(gbContext.get(), 0, sizeof(JBig2ArithCtx) * size); } if (pSymbolDictDecoder->SDREFAGG == 1) { - dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13; - grContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx) * dwTemp); + const size_t size = GetRefAggContextSize(pSymbolDictDecoder->SDRTEMPLATE); + grContext.reset(FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memset(grContext.get(), 0, sizeof(JBig2ArithCtx) * size); } } + const uint8_t* key = pSegment->m_pData; + FX_BOOL cache_hit = false; pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER; for (std::list<CJBig2_CachePair>::iterator it = m_pSymbolDictCache->begin(); it != m_pSymbolDictCache->end(); ++it) { @@ -636,23 +616,20 @@ int32_t CJBig2_Context::parseSymbolDict(CJBig2_Segment* pSegment, } if (!cache_hit) { if (pSymbolDictDecoder->SDHUFF == 0) { - pArithDecoder = new CJBig2_ArithDecoder(m_pStream.get()); - pSegment->m_Result.sd = - pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext); - delete pArithDecoder; - if (pSegment->m_Result.sd == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + nonstd::unique_ptr<CJBig2_ArithDecoder> pArithDecoder( + new CJBig2_ArithDecoder(m_pStream.get())); + pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith( + pArithDecoder.get(), gbContext.get(), grContext.get()); + if (!pSegment->m_Result.sd) + return JBIG2_ERROR_FATAL; + m_pStream->alignByte(); m_pStream->offset(2); } else { pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman( - m_pStream.get(), gbContext, grContext, pPause); - if (pSegment->m_Result.sd == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + m_pStream.get(), gbContext.get(), grContext.get(), pPause); + if (!pSegment->m_Result.sd) + return JBIG2_ERROR_FATAL; m_pStream->alignByte(); } #ifndef DISABLE_SYMBOL_CACHE @@ -670,74 +647,29 @@ int32_t CJBig2_Context::parseSymbolDict(CJBig2_Segment* pSegment, if (wFlags & 0x0200) { pSegment->m_Result.sd->m_bContextRetained = TRUE; if (pSymbolDictDecoder->SDHUFF == 0) { - pSegment->m_Result.sd->m_gbContext = gbContext; + pSegment->m_Result.sd->m_gbContext = gbContext.release(); } if (pSymbolDictDecoder->SDREFAGG == 1) { - pSegment->m_Result.sd->m_grContext = grContext; + pSegment->m_Result.sd->m_grContext = grContext.release(); } - bUsed = TRUE; - } else { - bUsed = FALSE; - } - delete pSymbolDictDecoder; - FX_Free(SDINSYMS); - delete Table_B1; - delete Table_B2; - delete Table_B3; - delete Table_B4; - delete Table_B5; - if (bUsed == FALSE) { - FX_Free(gbContext); - FX_Free(grContext); } return JBIG2_SUCCESS; -failed: - delete pSymbolDictDecoder; - FX_Free(SDINSYMS); - delete Table_B1; - delete Table_B2; - delete Table_B3; - delete Table_B4; - delete Table_B5; - FX_Free(gbContext); - FX_Free(grContext); - return nRet; } int32_t CJBig2_Context::parseTextRegion(CJBig2_Segment* pSegment) { - FX_DWORD dwTemp; FX_WORD wFlags; - int32_t i, nIndex, nRet; JBig2RegionInfo ri; - CJBig2_Segment* pSeg; - CJBig2_Image** SBSYMS = nullptr; - JBig2HuffmanCode* SBSYMCODES = nullptr; - uint8_t cSBHUFFFS, cSBHUFFDS, cSBHUFFDT, cSBHUFFRDW, cSBHUFFRDH, cSBHUFFRDX, - cSBHUFFRDY, cSBHUFFRSIZE; - CJBig2_HuffmanTable* Table_B1 = nullptr; - CJBig2_HuffmanTable* Table_B6 = nullptr; - CJBig2_HuffmanTable* Table_B7 = nullptr; - CJBig2_HuffmanTable* Table_B8 = nullptr; - CJBig2_HuffmanTable* Table_B9 = nullptr; - CJBig2_HuffmanTable* Table_B10 = nullptr; - CJBig2_HuffmanTable* Table_B11 = nullptr; - CJBig2_HuffmanTable* Table_B12 = nullptr; - CJBig2_HuffmanTable* Table_B13 = nullptr; - CJBig2_HuffmanTable* Table_B14 = nullptr; - CJBig2_HuffmanTable* Table_B15 = nullptr; - JBig2ArithCtx* grContext = nullptr; - CJBig2_ArithDecoder* pArithDecoder; - CJBig2_TRDProc* pTRD = new CJBig2_TRDProc(); - if ((parseRegionInfo(&ri) != JBIG2_SUCCESS) || - (m_pStream->readShortInteger(&wFlags) != 0)) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; + if (parseRegionInfo(&ri) != JBIG2_SUCCESS || + m_pStream->readShortInteger(&wFlags) != 0) { + return JBIG2_ERROR_TOO_SHORT; } + + nonstd::unique_ptr<CJBig2_TRDProc> pTRD(new CJBig2_TRDProc); pTRD->SBW = ri.width; pTRD->SBH = ri.height; pTRD->SBHUFF = wFlags & 0x0001; pTRD->SBREFINE = (wFlags >> 1) & 0x0001; - dwTemp = (wFlags >> 2) & 0x0003; + FX_DWORD dwTemp = (wFlags >> 2) & 0x0003; pTRD->SBSTRIPS = 1 << dwTemp; pTRD->REFCORNER = (JBig2Corner)((wFlags >> 4) & 0x0003); pTRD->TRANSPOSED = (wFlags >> 6) & 0x0001; @@ -748,11 +680,19 @@ int32_t CJBig2_Context::parseTextRegion(CJBig2_Segment* pSegment) { pTRD->SBDSOFFSET = pTRD->SBDSOFFSET - 0x0020; } pTRD->SBRTEMPLATE = (wFlags >> 15) & 0x0001; + + uint8_t cSBHUFFFS; + uint8_t cSBHUFFDS; + uint8_t cSBHUFFDT; + uint8_t cSBHUFFRDW; + uint8_t cSBHUFFRDH; + uint8_t cSBHUFFRDX; + uint8_t cSBHUFFRDY; + uint8_t cSBHUFFRSIZE; if (pTRD->SBHUFF == 1) { - if (m_pStream->readShortInteger(&wFlags) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } + if (m_pStream->readShortInteger(&wFlags) != 0) + return JBIG2_ERROR_TOO_SHORT; + cSBHUFFFS = wFlags & 0x0003; cSBHUFFDS = (wFlags >> 2) & 0x0003; cSBHUFFDT = (wFlags >> 4) & 0x0003; @@ -762,253 +702,257 @@ int32_t CJBig2_Context::parseTextRegion(CJBig2_Segment* pSegment) { cSBHUFFRDY = (wFlags >> 12) & 0x0003; cSBHUFFRSIZE = (wFlags >> 14) & 0x0001; } - if ((pTRD->SBREFINE == 1) && (pTRD->SBRTEMPLATE == 0)) { - for (i = 0; i < 4; i++) { - if (m_pStream->read1Byte((uint8_t*)&pTRD->SBRAT[i]) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } + if (pTRD->SBREFINE == 1 && pTRD->SBRTEMPLATE == 0) { + for (int32_t i = 0; i < 4; ++i) { + if (m_pStream->read1Byte((uint8_t*)&pTRD->SBRAT[i]) != 0) + return JBIG2_ERROR_TOO_SHORT; } } - if (m_pStream->readInteger(&pTRD->SBNUMINSTANCES) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { - if (!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + if (m_pStream->readInteger(&pTRD->SBNUMINSTANCES) != 0) + return JBIG2_ERROR_TOO_SHORT; + + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { + if (!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) + return JBIG2_ERROR_FATAL; } + pTRD->SBNUMSYMS = 0; - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { - pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { + CJBig2_Segment* pSeg = + findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); if (pSeg->m_cFlags.s.type == 0) { pTRD->SBNUMSYMS += pSeg->m_Result.sd->SDNUMEXSYMS; } } + + nonstd::unique_ptr<CJBig2_Image*, FxFreeDeleter> SBSYMS; if (pTRD->SBNUMSYMS > 0) { - SBSYMS = FX_Alloc(CJBig2_Image*, pTRD->SBNUMSYMS); + SBSYMS.reset(FX_Alloc(CJBig2_Image*, pTRD->SBNUMSYMS)); dwTemp = 0; - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { - pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); + for (int32_t i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { + CJBig2_Segment* pSeg = + findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]); if (pSeg->m_cFlags.s.type == 0) { - JBIG2_memcpy(SBSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS, + JBIG2_memcpy(SBSYMS.get() + dwTemp, pSeg->m_Result.sd->SDEXSYMS, pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*)); dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS; } } - pTRD->SBSYMS = SBSYMS; + pTRD->SBSYMS = SBSYMS.get(); } else { pTRD->SBSYMS = NULL; } + + nonstd::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES; if (pTRD->SBHUFF == 1) { - SBSYMCODES = decodeSymbolIDHuffmanTable(m_pStream.get(), pTRD->SBNUMSYMS); - if (SBSYMCODES == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + SBSYMCODES.reset( + decodeSymbolIDHuffmanTable(m_pStream.get(), pTRD->SBNUMSYMS)); + if (!SBSYMCODES) + return JBIG2_ERROR_FATAL; + m_pStream->alignByte(); - pTRD->SBSYMCODES = SBSYMCODES; + pTRD->SBSYMCODES = SBSYMCODES.get(); } else { dwTemp = 0; while ((FX_DWORD)(1 << dwTemp) < pTRD->SBNUMSYMS) { - dwTemp++; + ++dwTemp; } pTRD->SBSYMCODELEN = (uint8_t)dwTemp; } + + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B1; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B6; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B7; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B8; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B9; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B10; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B11; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B12; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B13; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B14; + nonstd::unique_ptr<CJBig2_HuffmanTable> Table_B15; if (pTRD->SBHUFF == 1) { - if ((cSBHUFFFS == 2) || (cSBHUFFRDW == 2) || (cSBHUFFRDH == 2) || - (cSBHUFFRDX == 2) || (cSBHUFFRDY == 2)) { - nRet = JBIG2_ERROR_FATAL; - goto failed; + if (cSBHUFFFS == 2 || cSBHUFFRDW == 2 || cSBHUFFRDH == 2 || + cSBHUFFRDX == 2 || cSBHUFFRDY == 2) { + return JBIG2_ERROR_FATAL; } - nIndex = 0; + int32_t nIndex = 0; if (cSBHUFFFS == 0) { - Table_B6 = new CJBig2_HuffmanTable(HuffmanTable_B6, - FX_ArraySize(HuffmanTable_B6), - HuffmanTable_HTOOB_B6); - pTRD->SBHUFFFS = Table_B6; + Table_B6.reset(new CJBig2_HuffmanTable(HuffmanTable_B6, + FX_ArraySize(HuffmanTable_B6), + HuffmanTable_HTOOB_B6)); + pTRD->SBHUFFFS = Table_B6.get(); } else if (cSBHUFFFS == 1) { - Table_B7 = new CJBig2_HuffmanTable(HuffmanTable_B7, - FX_ArraySize(HuffmanTable_B7), - HuffmanTable_HTOOB_B7); - pTRD->SBHUFFFS = Table_B7; + Table_B7.reset(new CJBig2_HuffmanTable(HuffmanTable_B7, + FX_ArraySize(HuffmanTable_B7), + HuffmanTable_HTOOB_B7)); + pTRD->SBHUFFFS = Table_B7.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFFS = pSeg->m_Result.ht; } if (cSBHUFFDS == 0) { - Table_B8 = new CJBig2_HuffmanTable(HuffmanTable_B8, - FX_ArraySize(HuffmanTable_B8), - HuffmanTable_HTOOB_B8); - pTRD->SBHUFFDS = Table_B8; + Table_B8.reset(new CJBig2_HuffmanTable(HuffmanTable_B8, + FX_ArraySize(HuffmanTable_B8), + HuffmanTable_HTOOB_B8)); + pTRD->SBHUFFDS = Table_B8.get(); } else if (cSBHUFFDS == 1) { - Table_B9 = new CJBig2_HuffmanTable(HuffmanTable_B9, - FX_ArraySize(HuffmanTable_B9), - HuffmanTable_HTOOB_B9); - pTRD->SBHUFFDS = Table_B9; + Table_B9.reset(new CJBig2_HuffmanTable(HuffmanTable_B9, + FX_ArraySize(HuffmanTable_B9), + HuffmanTable_HTOOB_B9)); + pTRD->SBHUFFDS = Table_B9.get(); } else if (cSBHUFFDS == 2) { - Table_B10 = new CJBig2_HuffmanTable(HuffmanTable_B10, - FX_ArraySize(HuffmanTable_B10), - HuffmanTable_HTOOB_B10); - pTRD->SBHUFFDS = Table_B10; + Table_B10.reset(new CJBig2_HuffmanTable(HuffmanTable_B10, + FX_ArraySize(HuffmanTable_B10), + HuffmanTable_HTOOB_B10)); + pTRD->SBHUFFDS = Table_B10.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFDS = pSeg->m_Result.ht; } if (cSBHUFFDT == 0) { - Table_B11 = new CJBig2_HuffmanTable(HuffmanTable_B11, - FX_ArraySize(HuffmanTable_B11), - HuffmanTable_HTOOB_B11); - pTRD->SBHUFFDT = Table_B11; + Table_B11.reset(new CJBig2_HuffmanTable(HuffmanTable_B11, + FX_ArraySize(HuffmanTable_B11), + HuffmanTable_HTOOB_B11)); + pTRD->SBHUFFDT = Table_B11.get(); } else if (cSBHUFFDT == 1) { - Table_B12 = new CJBig2_HuffmanTable(HuffmanTable_B12, - FX_ArraySize(HuffmanTable_B12), - HuffmanTable_HTOOB_B12); - pTRD->SBHUFFDT = Table_B12; + Table_B12.reset(new CJBig2_HuffmanTable(HuffmanTable_B12, + FX_ArraySize(HuffmanTable_B12), + HuffmanTable_HTOOB_B12)); + pTRD->SBHUFFDT = Table_B12.get(); } else if (cSBHUFFDT == 2) { - Table_B13 = new CJBig2_HuffmanTable(HuffmanTable_B13, - FX_ArraySize(HuffmanTable_B13), - HuffmanTable_HTOOB_B13); - pTRD->SBHUFFDT = Table_B13; + Table_B13.reset(new CJBig2_HuffmanTable(HuffmanTable_B13, + FX_ArraySize(HuffmanTable_B13), + HuffmanTable_HTOOB_B13)); + pTRD->SBHUFFDT = Table_B13.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFDT = pSeg->m_Result.ht; } if (cSBHUFFRDW == 0) { - Table_B14 = new CJBig2_HuffmanTable(HuffmanTable_B14, - FX_ArraySize(HuffmanTable_B14), - HuffmanTable_HTOOB_B14); - pTRD->SBHUFFRDW = Table_B14; + Table_B14.reset(new CJBig2_HuffmanTable(HuffmanTable_B14, + FX_ArraySize(HuffmanTable_B14), + HuffmanTable_HTOOB_B14)); + pTRD->SBHUFFRDW = Table_B14.get(); } else if (cSBHUFFRDW == 1) { - Table_B15 = new CJBig2_HuffmanTable(HuffmanTable_B15, - FX_ArraySize(HuffmanTable_B15), - HuffmanTable_HTOOB_B15); - pTRD->SBHUFFRDW = Table_B15; + Table_B15.reset(new CJBig2_HuffmanTable(HuffmanTable_B15, + FX_ArraySize(HuffmanTable_B15), + HuffmanTable_HTOOB_B15)); + pTRD->SBHUFFRDW = Table_B15.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFRDW = pSeg->m_Result.ht; } if (cSBHUFFRDH == 0) { if (!Table_B14) { - Table_B14 = new CJBig2_HuffmanTable(HuffmanTable_B14, - FX_ArraySize(HuffmanTable_B14), - HuffmanTable_HTOOB_B14); + Table_B14.reset(new CJBig2_HuffmanTable(HuffmanTable_B14, + FX_ArraySize(HuffmanTable_B14), + HuffmanTable_HTOOB_B14)); } - pTRD->SBHUFFRDH = Table_B14; + pTRD->SBHUFFRDH = Table_B14.get(); } else if (cSBHUFFRDH == 1) { if (!Table_B15) { - Table_B15 = new CJBig2_HuffmanTable(HuffmanTable_B15, - FX_ArraySize(HuffmanTable_B15), - HuffmanTable_HTOOB_B15); + Table_B15.reset(new CJBig2_HuffmanTable(HuffmanTable_B15, + FX_ArraySize(HuffmanTable_B15), + HuffmanTable_HTOOB_B15)); } - pTRD->SBHUFFRDH = Table_B15; + pTRD->SBHUFFRDH = Table_B15.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFRDH = pSeg->m_Result.ht; } if (cSBHUFFRDX == 0) { if (!Table_B14) { - Table_B14 = new CJBig2_HuffmanTable(HuffmanTable_B14, - FX_ArraySize(HuffmanTable_B14), - HuffmanTable_HTOOB_B14); + Table_B14.reset(new CJBig2_HuffmanTable(HuffmanTable_B14, + FX_ArraySize(HuffmanTable_B14), + HuffmanTable_HTOOB_B14)); } - pTRD->SBHUFFRDX = Table_B14; + pTRD->SBHUFFRDX = Table_B14.get(); } else if (cSBHUFFRDX == 1) { if (!Table_B15) { - Table_B15 = new CJBig2_HuffmanTable(HuffmanTable_B15, - FX_ArraySize(HuffmanTable_B15), - HuffmanTable_HTOOB_B15); + Table_B15.reset(new CJBig2_HuffmanTable(HuffmanTable_B15, + FX_ArraySize(HuffmanTable_B15), + HuffmanTable_HTOOB_B15)); } - pTRD->SBHUFFRDX = Table_B15; + pTRD->SBHUFFRDX = Table_B15.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFRDX = pSeg->m_Result.ht; } if (cSBHUFFRDY == 0) { if (!Table_B14) { - Table_B14 = new CJBig2_HuffmanTable(HuffmanTable_B14, - FX_ArraySize(HuffmanTable_B14), - HuffmanTable_HTOOB_B14); + Table_B14.reset(new CJBig2_HuffmanTable(HuffmanTable_B14, + FX_ArraySize(HuffmanTable_B14), + HuffmanTable_HTOOB_B14)); } - pTRD->SBHUFFRDY = Table_B14; + pTRD->SBHUFFRDY = Table_B14.get(); } else if (cSBHUFFRDY == 1) { if (!Table_B15) { - Table_B15 = new CJBig2_HuffmanTable(HuffmanTable_B15, - FX_ArraySize(HuffmanTable_B15), - HuffmanTable_HTOOB_B15); + Table_B15.reset(new CJBig2_HuffmanTable(HuffmanTable_B15, + FX_ArraySize(HuffmanTable_B15), + HuffmanTable_HTOOB_B15)); } - pTRD->SBHUFFRDY = Table_B15; + pTRD->SBHUFFRDY = Table_B15.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFRDY = pSeg->m_Result.ht; } if (cSBHUFFRSIZE == 0) { - Table_B1 = new CJBig2_HuffmanTable(HuffmanTable_B1, - FX_ArraySize(HuffmanTable_B1), - HuffmanTable_HTOOB_B1); - pTRD->SBHUFFRSIZE = Table_B1; + Table_B1.reset(new CJBig2_HuffmanTable(HuffmanTable_B1, + FX_ArraySize(HuffmanTable_B1), + HuffmanTable_HTOOB_B1)); + pTRD->SBHUFFRSIZE = Table_B1.get(); } else { - pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++); - if (!pSeg) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + CJBig2_Segment* pSeg = + findReferredSegmentByTypeAndIndex(pSegment, 53, ++nIndex); + if (!pSeg) + return JBIG2_ERROR_FATAL; pTRD->SBHUFFRSIZE = pSeg->m_Result.ht; } } + nonstd::unique_ptr<JBig2ArithCtx, FxFreeDeleter> grContext; if (pTRD->SBREFINE == 1) { - dwTemp = pTRD->SBRTEMPLATE ? 1 << 10 : 1 << 13; - grContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx) * dwTemp); + const size_t size = GetRefAggContextSize(pTRD->SBRTEMPLATE); + grContext.reset(FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memset(grContext.get(), 0, sizeof(JBig2ArithCtx) * size); } if (pTRD->SBHUFF == 0) { - pArithDecoder = new CJBig2_ArithDecoder(m_pStream.get()); + nonstd::unique_ptr<CJBig2_ArithDecoder> pArithDecoder( + new CJBig2_ArithDecoder(m_pStream.get())); pSegment->m_nResultType = JBIG2_IMAGE_POINTER; - pSegment->m_Result.im = pTRD->decode_Arith(pArithDecoder, grContext); - delete pArithDecoder; - if (pSegment->m_Result.im == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + pSegment->m_Result.im = + pTRD->decode_Arith(pArithDecoder.get(), grContext.get()); + if (!pSegment->m_Result.im) + return JBIG2_ERROR_FATAL; m_pStream->alignByte(); m_pStream->offset(2); } else { pSegment->m_nResultType = JBIG2_IMAGE_POINTER; - pSegment->m_Result.im = pTRD->decode_Huffman(m_pStream.get(), grContext); - if (pSegment->m_Result.im == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + pSegment->m_Result.im = + pTRD->decode_Huffman(m_pStream.get(), grContext.get()); + if (!pSegment->m_Result.im) + return JBIG2_ERROR_FATAL; m_pStream->alignByte(); } if (pSegment->m_cFlags.s.type != 4) { @@ -1024,121 +968,67 @@ int32_t CJBig2_Context::parseTextRegion(CJBig2_Segment* pSegment) { delete pSegment->m_Result.im; pSegment->m_Result.im = NULL; } - delete pTRD; - FX_Free(SBSYMS); - FX_Free(SBSYMCODES); - FX_Free(grContext); - delete Table_B1; - delete Table_B6; - delete Table_B7; - delete Table_B8; - delete Table_B9; - delete Table_B10; - delete Table_B11; - delete Table_B12; - delete Table_B13; - delete Table_B14; - delete Table_B15; return JBIG2_SUCCESS; -failed: - delete pTRD; - FX_Free(SBSYMS); - FX_Free(SBSYMCODES); - FX_Free(grContext); - delete Table_B1; - delete Table_B6; - delete Table_B7; - delete Table_B8; - delete Table_B9; - delete Table_B10; - delete Table_B11; - delete Table_B12; - delete Table_B13; - delete Table_B14; - delete Table_B15; - return nRet; } int32_t CJBig2_Context::parsePatternDict(CJBig2_Segment* pSegment, IFX_Pause* pPause) { - FX_DWORD dwTemp; uint8_t cFlags; - JBig2ArithCtx* gbContext; - CJBig2_ArithDecoder* pArithDecoder; - int32_t nRet; - CJBig2_PDDProc* pPDD = new CJBig2_PDDProc(); - if ((m_pStream->read1Byte(&cFlags) != 0) || - (m_pStream->read1Byte(&pPDD->HDPW) != 0) || - (m_pStream->read1Byte(&pPDD->HDPH) != 0) || - (m_pStream->readInteger(&pPDD->GRAYMAX) != 0)) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } - if (pPDD->GRAYMAX > JBIG2_MAX_PATTERN_INDEX) { - nRet = JBIG2_ERROR_LIMIT; - goto failed; + nonstd::unique_ptr<CJBig2_PDDProc> pPDD(new CJBig2_PDDProc); + if (m_pStream->read1Byte(&cFlags) != 0 || + m_pStream->read1Byte(&pPDD->HDPW) != 0 || + m_pStream->read1Byte(&pPDD->HDPH) != 0 || + m_pStream->readInteger(&pPDD->GRAYMAX) != 0) { + return JBIG2_ERROR_TOO_SHORT; } + if (pPDD->GRAYMAX > JBIG2_MAX_PATTERN_INDEX) + return JBIG2_ERROR_LIMIT; + pPDD->HDMMR = cFlags & 0x01; pPDD->HDTEMPLATE = (cFlags >> 1) & 0x03; pSegment->m_nResultType = JBIG2_PATTERN_DICT_POINTER; if (pPDD->HDMMR == 0) { - dwTemp = - pPDD->HDTEMPLATE == 0 ? 65536 : pPDD->HDTEMPLATE == 1 ? 8192 : 1024; - gbContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx) * dwTemp); - pArithDecoder = new CJBig2_ArithDecoder(m_pStream.get()); + const size_t size = GetHuffContextSize(pPDD->HDTEMPLATE); + nonstd::unique_ptr<JBig2ArithCtx, FxFreeDeleter> gbContext( + FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memset(gbContext.get(), 0, sizeof(JBig2ArithCtx) * size); + nonstd::unique_ptr<CJBig2_ArithDecoder> pArithDecoder( + new CJBig2_ArithDecoder(m_pStream.get())); pSegment->m_Result.pd = - pPDD->decode_Arith(pArithDecoder, gbContext, pPause); - delete pArithDecoder; - if (pSegment->m_Result.pd == NULL) { - FX_Free(gbContext); - nRet = JBIG2_ERROR_FATAL; - goto failed; - } - FX_Free(gbContext); + pPDD->decode_Arith(pArithDecoder.get(), gbContext.get(), pPause); + if (!pSegment->m_Result.pd) + return JBIG2_ERROR_FATAL; + m_pStream->alignByte(); m_pStream->offset(2); } else { pSegment->m_Result.pd = pPDD->decode_MMR(m_pStream.get(), pPause); - if (pSegment->m_Result.pd == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + if (!pSegment->m_Result.pd) + return JBIG2_ERROR_FATAL; m_pStream->alignByte(); } - delete pPDD; return JBIG2_SUCCESS; -failed: - delete pPDD; - return nRet; } + int32_t CJBig2_Context::parseHalftoneRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause) { - FX_DWORD dwTemp; uint8_t cFlags; JBig2RegionInfo ri; - CJBig2_Segment* pSeg; - CJBig2_PatternDict* pPatternDict; - JBig2ArithCtx* gbContext; - CJBig2_ArithDecoder* pArithDecoder; - int32_t nRet; - CJBig2_HTRDProc* pHRD = new CJBig2_HTRDProc(); - if ((parseRegionInfo(&ri) != JBIG2_SUCCESS) || - (m_pStream->read1Byte(&cFlags) != 0) || - (m_pStream->readInteger(&pHRD->HGW) != 0) || - (m_pStream->readInteger(&pHRD->HGH) != 0) || - (m_pStream->readInteger((FX_DWORD*)&pHRD->HGX) != 0) || - (m_pStream->readInteger((FX_DWORD*)&pHRD->HGY) != 0) || - (m_pStream->readShortInteger(&pHRD->HRX) != 0) || - (m_pStream->readShortInteger(&pHRD->HRY) != 0)) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } - if (pHRD->HGW == 0 || pHRD->HGH == 0) { - nRet = JBIG2_ERROR_FATAL; - goto failed; + nonstd::unique_ptr<CJBig2_HTRDProc> pHRD(new CJBig2_HTRDProc); + if (parseRegionInfo(&ri) != JBIG2_SUCCESS || + m_pStream->read1Byte(&cFlags) != 0 || + m_pStream->readInteger(&pHRD->HGW) != 0 || + m_pStream->readInteger(&pHRD->HGH) != 0 || + m_pStream->readInteger((FX_DWORD*)&pHRD->HGX) != 0 || + m_pStream->readInteger((FX_DWORD*)&pHRD->HGY) != 0 || + m_pStream->readShortInteger(&pHRD->HRX) != 0 || + m_pStream->readShortInteger(&pHRD->HRY) != 0) { + return JBIG2_ERROR_TOO_SHORT; } + if (pHRD->HGW == 0 || pHRD->HGH == 0) + return JBIG2_ERROR_FATAL; + pHRD->HBW = ri.width; pHRD->HBH = ri.height; pHRD->HMMR = cFlags & 0x01; @@ -1146,54 +1036,48 @@ int32_t CJBig2_Context::parseHalftoneRegion(CJBig2_Segment* pSegment, pHRD->HENABLESKIP = (cFlags >> 3) & 0x01; pHRD->HCOMBOP = (JBig2ComposeOp)((cFlags >> 4) & 0x07); pHRD->HDEFPIXEL = (cFlags >> 7) & 0x01; - if (pSegment->m_nReferred_to_segment_count != 1) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } - pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]); - if ((pSeg == NULL) || (pSeg->m_cFlags.s.type != 16)) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } - pPatternDict = pSeg->m_Result.pd; - if ((pPatternDict == NULL) || (pPatternDict->NUMPATS == 0)) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + if (pSegment->m_nReferred_to_segment_count != 1) + return JBIG2_ERROR_FATAL; + + CJBig2_Segment* pSeg = + findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]); + if (!pSeg || (pSeg->m_cFlags.s.type != 16)) + return JBIG2_ERROR_FATAL; + + CJBig2_PatternDict* pPatternDict = pSeg->m_Result.pd; + if (!pPatternDict || (pPatternDict->NUMPATS == 0)) + return JBIG2_ERROR_FATAL; + pHRD->HNUMPATS = pPatternDict->NUMPATS; pHRD->HPATS = pPatternDict->HDPATS; pHRD->HPW = pPatternDict->HDPATS[0]->m_nWidth; pHRD->HPH = pPatternDict->HDPATS[0]->m_nHeight; pSegment->m_nResultType = JBIG2_IMAGE_POINTER; if (pHRD->HMMR == 0) { - dwTemp = pHRD->HTEMPLATE == 0 ? 65536 : pHRD->HTEMPLATE == 1 ? 8192 : 1024; - gbContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx) * dwTemp); - pArithDecoder = new CJBig2_ArithDecoder(m_pStream.get()); + const size_t size = GetHuffContextSize(pHRD->HTEMPLATE); + nonstd::unique_ptr<JBig2ArithCtx, FxFreeDeleter> gbContext( + FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memset(gbContext.get(), 0, sizeof(JBig2ArithCtx) * size); + nonstd::unique_ptr<CJBig2_ArithDecoder> pArithDecoder( + new CJBig2_ArithDecoder(m_pStream.get())); pSegment->m_Result.im = - pHRD->decode_Arith(pArithDecoder, gbContext, pPause); - delete pArithDecoder; - if (pSegment->m_Result.im == NULL) { - FX_Free(gbContext); - nRet = JBIG2_ERROR_FATAL; - goto failed; - } - FX_Free(gbContext); + pHRD->decode_Arith(pArithDecoder.get(), gbContext.get(), pPause); + if (!pSegment->m_Result.im) + return JBIG2_ERROR_FATAL; + m_pStream->alignByte(); m_pStream->offset(2); } else { pSegment->m_Result.im = pHRD->decode_MMR(m_pStream.get(), pPause); - if (pSegment->m_Result.im == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + if (!pSegment->m_Result.im) + return JBIG2_ERROR_FATAL; m_pStream->alignByte(); } if (pSegment->m_cFlags.s.type != 20) { if (!m_bBufSpecified) { JBig2PageInfo* pPageInfo = m_PageInfoList.back(); - if ((pPageInfo->m_bIsStriped == 1) && - (ri.y + ri.height > m_pPage->m_nHeight)) { + if (pPageInfo->m_bIsStriped == 1 && + ri.y + ri.height > m_pPage->m_nHeight) { m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0); } } @@ -1202,65 +1086,55 @@ int32_t CJBig2_Context::parseHalftoneRegion(CJBig2_Segment* pSegment, delete pSegment->m_Result.im; pSegment->m_Result.im = NULL; } - delete pHRD; return JBIG2_SUCCESS; -failed: - delete pHRD; - return nRet; } int32_t CJBig2_Context::parseGenericRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause) { - FX_DWORD dwTemp; - uint8_t cFlags; - int32_t i, nRet; if (!m_pGRD) { - m_pGRD.reset(new CJBig2_GRDProc); - if ((parseRegionInfo(&m_ri) != JBIG2_SUCCESS) || - (m_pStream->read1Byte(&cFlags) != 0)) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } - if (m_ri.height < 0 || m_ri.width < 0) { - nRet = JBIG2_FAILED; - goto failed; - } - m_pGRD->GBW = m_ri.width; - m_pGRD->GBH = m_ri.height; - m_pGRD->MMR = cFlags & 0x01; - m_pGRD->GBTEMPLATE = (cFlags >> 1) & 0x03; - m_pGRD->TPGDON = (cFlags >> 3) & 0x01; - if (m_pGRD->MMR == 0) { - if (m_pGRD->GBTEMPLATE == 0) { - for (i = 0; i < 8; i++) { - if (m_pStream->read1Byte((uint8_t*)&m_pGRD->GBAT[i]) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; + nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc); + uint8_t cFlags; + if (parseRegionInfo(&m_ri) != JBIG2_SUCCESS || + m_pStream->read1Byte(&cFlags) != 0) { + return JBIG2_ERROR_TOO_SHORT; + } + if (m_ri.height < 0 || m_ri.width < 0) + return JBIG2_FAILED; + + pGRD->GBW = m_ri.width; + pGRD->GBH = m_ri.height; + pGRD->MMR = cFlags & 0x01; + pGRD->GBTEMPLATE = (cFlags >> 1) & 0x03; + pGRD->TPGDON = (cFlags >> 3) & 0x01; + if (pGRD->MMR == 0) { + if (pGRD->GBTEMPLATE == 0) { + for (int32_t i = 0; i < 8; ++i) { + if (m_pStream->read1Byte((uint8_t*)&pGRD->GBAT[i]) != 0) { + return JBIG2_ERROR_TOO_SHORT; } } } else { - for (i = 0; i < 2; i++) { - if (m_pStream->read1Byte((uint8_t*)&m_pGRD->GBAT[i]) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; + for (int32_t i = 0; i < 2; ++i) { + if (m_pStream->read1Byte((uint8_t*)&pGRD->GBAT[i]) != 0) { + return JBIG2_ERROR_TOO_SHORT; } } } } - m_pGRD->USESKIP = 0; + pGRD->USESKIP = 0; + m_pGRD = nonstd::move(pGRD); } pSegment->m_nResultType = JBIG2_IMAGE_POINTER; if (m_pGRD->MMR == 0) { - dwTemp = - m_pGRD->GBTEMPLATE == 0 ? 65536 : m_pGRD->GBTEMPLATE == 1 ? 8192 : 1024; - if (m_gbContext == NULL) { - m_gbContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memset(m_gbContext, 0, sizeof(JBig2ArithCtx) * dwTemp); - } - if (m_pArithDecoder == NULL) { - m_pArithDecoder = new CJBig2_ArithDecoder(m_pStream.get()); + if (!m_gbContext) { + const size_t size = GetHuffContextSize(m_pGRD->GBTEMPLATE); + m_gbContext = FX_Alloc(JBig2ArithCtx, size); + JBIG2_memset(m_gbContext, 0, sizeof(JBig2ArithCtx) * size); + } + if (!m_pArithDecoder) { + m_pArithDecoder.reset(new CJBig2_ArithDecoder(m_pStream.get())); m_ProcessingStatus = m_pGRD->Start_decode_Arith( - &pSegment->m_Result.im, m_pArithDecoder, m_gbContext, pPause); + &pSegment->m_Result.im, m_pArithDecoder.get(), m_gbContext, pPause); } else { m_ProcessingStatus = m_pGRD->Continue_decode(pPause); } @@ -1281,17 +1155,14 @@ int32_t CJBig2_Context::parseGenericRegion(CJBig2_Segment* pSegment, } return JBIG2_SUCCESS; } else { - delete m_pArithDecoder; - m_pArithDecoder = NULL; - if (pSegment->m_Result.im == NULL) { - FX_Free(m_gbContext); - nRet = JBIG2_ERROR_FATAL; - m_gbContext = NULL; - m_ProcessingStatus = FXCODEC_STATUS_ERROR; - goto failed; - } + m_pArithDecoder.reset(); FX_Free(m_gbContext); m_gbContext = NULL; + if (!pSegment->m_Result.im) { + m_ProcessingStatus = FXCODEC_STATUS_ERROR; + m_pGRD.reset(); + return JBIG2_ERROR_FATAL; + } m_pStream->alignByte(); m_pStream->offset(2); } @@ -1301,9 +1172,9 @@ int32_t CJBig2_Context::parseGenericRegion(CJBig2_Segment* pSegment, while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { m_pGRD->Continue_decode(pPause); } - if (pSegment->m_Result.im == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; + if (!pSegment->m_Result.im) { + m_pGRD.reset(); + return JBIG2_ERROR_FATAL; } m_pStream->alignByte(); } @@ -1325,73 +1196,59 @@ int32_t CJBig2_Context::parseGenericRegion(CJBig2_Segment* pSegment, } m_pGRD.reset(); return JBIG2_SUCCESS; -failed: - m_pGRD.reset(); - return nRet; } int32_t CJBig2_Context::parseGenericRefinementRegion(CJBig2_Segment* pSegment) { - FX_DWORD dwTemp; JBig2RegionInfo ri; - CJBig2_Segment* pSeg; - int32_t i, nRet; uint8_t cFlags; - JBig2ArithCtx* grContext; - CJBig2_ArithDecoder* pArithDecoder; - CJBig2_GRRDProc* pGRRD = new CJBig2_GRRDProc(); - if ((parseRegionInfo(&ri) != JBIG2_SUCCESS) || - (m_pStream->read1Byte(&cFlags) != 0)) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; + if (parseRegionInfo(&ri) != JBIG2_SUCCESS || + m_pStream->read1Byte(&cFlags) != 0) { + return JBIG2_ERROR_TOO_SHORT; } + nonstd::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc); pGRRD->GRW = ri.width; pGRRD->GRH = ri.height; pGRRD->GRTEMPLATE = cFlags & 0x01; pGRRD->TPGRON = (cFlags >> 1) & 0x01; if (pGRRD->GRTEMPLATE == 0) { - for (i = 0; i < 4; i++) { - if (m_pStream->read1Byte((uint8_t*)&pGRRD->GRAT[i]) != 0) { - nRet = JBIG2_ERROR_TOO_SHORT; - goto failed; - } + for (int32_t i = 0; i < 4; ++i) { + if (m_pStream->read1Byte((uint8_t*)&pGRRD->GRAT[i]) != 0) + return JBIG2_ERROR_TOO_SHORT; } } - pSeg = NULL; + CJBig2_Segment* pSeg = nullptr; if (pSegment->m_nReferred_to_segment_count > 0) { - for (i = 0; i < pSegment->m_nReferred_to_segment_count; i++) { + int32_t i; + for (i = 0; i < pSegment->m_nReferred_to_segment_count; ++i) { pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]); - if (pSeg == NULL) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } - if ((pSeg->m_cFlags.s.type == 4) || (pSeg->m_cFlags.s.type == 20) || - (pSeg->m_cFlags.s.type == 36) || (pSeg->m_cFlags.s.type == 40)) { + if (!pSeg) + return JBIG2_ERROR_FATAL; + + if (pSeg->m_cFlags.s.type == 4 || pSeg->m_cFlags.s.type == 20 || + pSeg->m_cFlags.s.type == 36 || pSeg->m_cFlags.s.type == 40) { break; } } - if (i >= pSegment->m_nReferred_to_segment_count) { - nRet = JBIG2_ERROR_FATAL; - goto failed; - } + if (i >= pSegment->m_nReferred_to_segment_count) + return JBIG2_ERROR_FATAL; + pGRRD->GRREFERENCE = pSeg->m_Result.im; } else { pGRRD->GRREFERENCE = m_pPage.get(); } pGRRD->GRREFERENCEDX = 0; pGRRD->GRREFERENCEDY = 0; - dwTemp = pGRRD->GRTEMPLATE ? 1 << 10 : 1 << 13; - grContext = FX_Alloc(JBig2ArithCtx, dwTemp); - JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx) * dwTemp); - pArithDecoder = new CJBig2_ArithDecoder(m_pStream.get()); + const size_t size = GetRefAggContextSize(pGRRD->GRTEMPLATE); + nonstd::unique_ptr<JBig2ArithCtx, FxFreeDeleter> grContext( + FX_Alloc(JBig2ArithCtx, size)); + JBIG2_memset(grContext.get(), 0, sizeof(JBig2ArithCtx) * size); + nonstd::unique_ptr<CJBig2_ArithDecoder> pArithDecoder( + new CJBig2_ArithDecoder(m_pStream.get())); pSegment->m_nResultType = JBIG2_IMAGE_POINTER; - pSegment->m_Result.im = pGRRD->decode(pArithDecoder, grContext); - delete pArithDecoder; - if (pSegment->m_Result.im == NULL) { - FX_Free(grContext); - nRet = JBIG2_ERROR_FATAL; - goto failed; - } - FX_Free(grContext); + pSegment->m_Result.im = pGRRD->decode(pArithDecoder.get(), grContext.get()); + if (!pSegment->m_Result.im) + return JBIG2_ERROR_FATAL; + m_pStream->alignByte(); m_pStream->offset(2); if (pSegment->m_cFlags.s.type != 40) { @@ -1407,12 +1264,9 @@ int32_t CJBig2_Context::parseGenericRefinementRegion(CJBig2_Segment* pSegment) { delete pSegment->m_Result.im; pSegment->m_Result.im = NULL; } - delete pGRRD; return JBIG2_SUCCESS; -failed: - delete pGRRD; - return nRet; } + int32_t CJBig2_Context::parseTable(CJBig2_Segment* pSegment) { pSegment->m_nResultType = JBIG2_HUFFMAN_TABLE_POINTER; pSegment->m_Result.ht = new CJBig2_HuffmanTable(m_pStream.get()); @@ -1424,103 +1278,97 @@ int32_t CJBig2_Context::parseTable(CJBig2_Segment* pSegment) { m_pStream->alignByte(); return JBIG2_SUCCESS; } + int32_t CJBig2_Context::parseRegionInfo(JBig2RegionInfo* pRI) { - if ((m_pStream->readInteger((FX_DWORD*)&pRI->width) != 0) || - (m_pStream->readInteger((FX_DWORD*)&pRI->height) != 0) || - (m_pStream->readInteger((FX_DWORD*)&pRI->x) != 0) || - (m_pStream->readInteger((FX_DWORD*)&pRI->y) != 0) || - (m_pStream->read1Byte(&pRI->flags) != 0)) { + if (m_pStream->readInteger((FX_DWORD*)&pRI->width) != 0 || + m_pStream->readInteger((FX_DWORD*)&pRI->height) != 0 || + m_pStream->readInteger((FX_DWORD*)&pRI->x) != 0 || + m_pStream->readInteger((FX_DWORD*)&pRI->y) != 0 || + m_pStream->read1Byte(&pRI->flags) != 0) { return JBIG2_ERROR_TOO_SHORT; } return JBIG2_SUCCESS; } + JBig2HuffmanCode* CJBig2_Context::decodeSymbolIDHuffmanTable( CJBig2_BitStream* pStream, FX_DWORD SBNUMSYMS) { - JBig2HuffmanCode* SBSYMCODES; - int32_t runcodes[35]; - int32_t runcodes_len[35]; - int32_t runcode; - int32_t i; - int32_t j; - int32_t nVal; - int32_t nBits; + size_t kRunCodesSize = 35; + int32_t runcodes[kRunCodesSize]; + int32_t runcodes_len[kRunCodesSize]; + for (int32_t i = 0; i < kRunCodesSize; ++i) { + if (pStream->readNBits(4, &runcodes_len[i]) != 0) + return nullptr; + } + huffman_assign_code(runcodes, runcodes_len, kRunCodesSize); + + nonstd::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES( + FX_Alloc(JBig2HuffmanCode, SBNUMSYMS)); int32_t run; - FX_DWORD nTemp; - SBSYMCODES = FX_Alloc(JBig2HuffmanCode, SBNUMSYMS); - for (i = 0; i < 35; i++) { - if (pStream->readNBits(4, &runcodes_len[i]) != 0) { - goto failed; - } - } - huffman_assign_code(runcodes, runcodes_len, 35); - i = 0; + int32_t i = 0; while (i < (int)SBNUMSYMS) { - nVal = 0; - nBits = 0; - for (;;) { - if (pStream->read1Bit(&nTemp) != 0) { - goto failed; - } + int32_t j; + int32_t nVal = 0; + int32_t nBits = 0; + FX_DWORD nTemp; + while (true) { + if (pStream->read1Bit(&nTemp) != 0) + return nullptr; + nVal = (nVal << 1) | nTemp; - nBits++; - for (j = 0; j < 35; j++) { - if ((nBits == runcodes_len[j]) && (nVal == runcodes[j])) { + ++nBits; + for (j = 0; j < kRunCodesSize; ++j) { + if (nBits == runcodes_len[j] && nVal == runcodes[j]) { break; } } - if (j < 35) { + if (j < kRunCodesSize) { break; } } - runcode = j; + int32_t runcode = j; if (runcode < 32) { - SBSYMCODES[i].codelen = runcode; + SBSYMCODES.get()[i].codelen = runcode; run = 0; } else if (runcode == 32) { - if (pStream->readNBits(2, &nTemp) != 0) { - goto failed; - } + if (pStream->readNBits(2, &nTemp) != 0) + return nullptr; run = nTemp + 3; } else if (runcode == 33) { - if (pStream->readNBits(3, &nTemp) != 0) { - goto failed; - } + if (pStream->readNBits(3, &nTemp) != 0) + return nullptr; run = nTemp + 3; } else if (runcode == 34) { - if (pStream->readNBits(7, &nTemp) != 0) { - goto failed; - } + if (pStream->readNBits(7, &nTemp) != 0) + return nullptr; run = nTemp + 11; } if (run > 0) { - if (i + run > (int)SBNUMSYMS) { - goto failed; - } - for (j = 0; j < run; j++) { + if (i + run > (int)SBNUMSYMS) + return nullptr; + for (j = 0; j < run; ++j) { if (runcode == 32 && i > 0) { - SBSYMCODES[i + j].codelen = SBSYMCODES[i - 1].codelen; + SBSYMCODES.get()[i + j].codelen = SBSYMCODES.get()[i - 1].codelen; } else { - SBSYMCODES[i + j].codelen = 0; + SBSYMCODES.get()[i + j].codelen = 0; } } i += run; } else { - i++; + ++i; } } - huffman_assign_code(SBSYMCODES, SBNUMSYMS); - return SBSYMCODES; -failed: - FX_Free(SBSYMCODES); - return NULL; + huffman_assign_code(SBSYMCODES.get(), SBNUMSYMS); + return SBSYMCODES.release(); } + void CJBig2_Context::huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP) { + // TODO(thestig) CJBig2_HuffmanTable::parseFromCodedBuffer() has similar code. int CURLEN, LENMAX, CURCODE, CURTEMP, i; int* LENCOUNT; int* FIRSTCODE; LENMAX = 0; - for (i = 0; i < NTEMP; i++) { + for (i = 0; i < NTEMP; ++i) { if (PREFLEN[i] > LENMAX) { LENMAX = PREFLEN[i]; } @@ -1528,8 +1376,8 @@ void CJBig2_Context::huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP) { LENCOUNT = FX_Alloc(int, LENMAX + 1); JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); FIRSTCODE = FX_Alloc(int, LENMAX + 1); - for (i = 0; i < NTEMP; i++) { - LENCOUNT[PREFLEN[i]]++; + for (i = 0; i < NTEMP; ++i) { + ++LENCOUNT[PREFLEN[i]]; } CURLEN = 1; FIRSTCODE[0] = 0; @@ -1556,7 +1404,7 @@ void CJBig2_Context::huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int* LENCOUNT; int* FIRSTCODE; LENMAX = 0; - for (i = 0; i < NTEMP; i++) { + for (i = 0; i < NTEMP; ++i) { if (SBSYMCODES[i].codelen > LENMAX) { LENMAX = SBSYMCODES[i].codelen; } @@ -1564,8 +1412,8 @@ void CJBig2_Context::huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, LENCOUNT = FX_Alloc(int, (LENMAX + 1)); JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); FIRSTCODE = FX_Alloc(int, (LENMAX + 1)); - for (i = 0; i < NTEMP; i++) { - LENCOUNT[SBSYMCODES[i].codelen]++; + for (i = 0; i < NTEMP; ++i) { + ++LENCOUNT[SBSYMCODES[i].codelen]; } CURLEN = 1; FIRSTCODE[0] = 0; diff --git a/core/src/fxcodec/jbig2/JBig2_Context.h b/core/src/fxcodec/jbig2/JBig2_Context.h index 8681609ca0..3c283d1d5b 100644 --- a/core/src/fxcodec/jbig2/JBig2_Context.h +++ b/core/src/fxcodec/jbig2/JBig2_Context.h @@ -118,7 +118,7 @@ class CJBig2_Context { int32_t m_PauseStep; IFX_Pause* m_pPause; FXCODEC_STATUS m_ProcessingStatus; - CJBig2_ArithDecoder* m_pArithDecoder; + nonstd::unique_ptr<CJBig2_ArithDecoder> m_pArithDecoder; nonstd::unique_ptr<CJBig2_GRDProc> m_pGRD; JBig2ArithCtx* m_gbContext; nonstd::unique_ptr<CJBig2_Segment> m_pSegment; diff --git a/core/src/fxcodec/jbig2/JBig2_GsidProc.cpp b/core/src/fxcodec/jbig2/JBig2_GsidProc.cpp index b97a212926..d89bb8f249 100644 --- a/core/src/fxcodec/jbig2/JBig2_GsidProc.cpp +++ b/core/src/fxcodec/jbig2/JBig2_GsidProc.cpp @@ -15,15 +15,6 @@ FX_DWORD* CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext, IFX_Pause* pPause) { - CJBig2_Image** GSPLANES; - int32_t J, K; - FX_DWORD x, y; - FX_DWORD* GSVALS; - GSPLANES = FX_Alloc(CJBig2_Image*, GSBPP); - GSVALS = FX_Alloc2D(FX_DWORD, GSW, GSH); - JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*) * GSBPP); - JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD) * GSW * GSH); - nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc()); pGRD->MMR = GSMMR; pGRD->GBW = GSW; @@ -46,105 +37,100 @@ FX_DWORD* CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder, pGRD->GBAT[6] = -2; pGRD->GBAT[7] = -2; } + + nonstd::unique_ptr<CJBig2_Image*, FxFreeDeleter> GSPLANES( + FX_Alloc(CJBig2_Image*, GSBPP)); + JBIG2_memset(GSPLANES.get(), 0, sizeof(CJBig2_Image*) * GSBPP); FXCODEC_STATUS status = pGRD->Start_decode_Arith( - &GSPLANES[GSBPP - 1], pArithDecoder, gbContext, nullptr); + &GSPLANES.get()[GSBPP - 1], pArithDecoder, gbContext, nullptr); while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { pGRD->Continue_decode(pPause); } - if (!GSPLANES[GSBPP - 1]) { - goto failed; - } - J = GSBPP - 2; + if (!GSPLANES.get()[GSBPP - 1]) + return nullptr; + + int32_t J = GSBPP - 2; while (J >= 0) { FXCODEC_STATUS status = pGRD->Start_decode_Arith( - &GSPLANES[J], pArithDecoder, gbContext, nullptr); + &GSPLANES.get()[J], pArithDecoder, gbContext, nullptr); while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { pGRD->Continue_decode(pPause); } - if (!GSPLANES[J]) { - for (K = GSBPP - 1; K > J; K--) { - delete GSPLANES[K]; - goto failed; + if (!GSPLANES.get()[J]) { + for (int32_t K = GSBPP - 1; K > J; --K) { + delete GSPLANES.get()[K]; + return nullptr; } } - GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR); + GSPLANES.get()[J]->composeFrom(0, 0, GSPLANES.get()[J + 1], + JBIG2_COMPOSE_XOR); J = J - 1; } - for (y = 0; y < GSH; y++) { - for (x = 0; x < GSW; x++) { - for (J = 0; J < GSBPP; J++) { - GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J; + nonstd::unique_ptr<FX_DWORD, FxFreeDeleter> GSVALS( + FX_Alloc2D(FX_DWORD, GSW, GSH)); + JBIG2_memset(GSVALS.get(), 0, sizeof(FX_DWORD) * GSW * GSH); + for (FX_DWORD y = 0; y < GSH; ++y) { + for (FX_DWORD x = 0; x < GSW; ++x) { + for (J = 0; J < GSBPP; ++J) { + GSVALS.get()[y * GSW + x] |= GSPLANES.get()[J]->getPixel(x, y) << J; } } } - for (J = 0; J < GSBPP; J++) { - delete GSPLANES[J]; + for (J = 0; J < GSBPP; ++J) { + delete GSPLANES.get()[J]; } - FX_Free(GSPLANES); - return GSVALS; -failed: - FX_Free(GSPLANES); - FX_Free(GSVALS); - return nullptr; + return GSVALS.release(); } FX_DWORD* CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream* pStream, IFX_Pause* pPause) { - CJBig2_Image** GSPLANES; - int32_t J, K; - FX_DWORD x, y; - FX_DWORD* GSVALS; - GSPLANES = FX_Alloc(CJBig2_Image*, GSBPP); - GSVALS = FX_Alloc2D(FX_DWORD, GSW, GSH); - JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*) * GSBPP); - JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD) * GSW * GSH); - nonstd::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc()); pGRD->MMR = GSMMR; pGRD->GBW = GSW; pGRD->GBH = GSH; + + nonstd::unique_ptr<CJBig2_Image*> GSPLANES(FX_Alloc(CJBig2_Image*, GSBPP)); + JBIG2_memset(GSPLANES.get(), 0, sizeof(CJBig2_Image*) * GSBPP); FXCODEC_STATUS status = - pGRD->Start_decode_MMR(&GSPLANES[GSBPP - 1], pStream, nullptr); + pGRD->Start_decode_MMR(&GSPLANES.get()[GSBPP - 1], pStream, nullptr); while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { pGRD->Continue_decode(pPause); } - if (!GSPLANES[GSBPP - 1]) { - goto failed; - } + if (!GSPLANES.get()[GSBPP - 1]) + return nullptr; + pStream->alignByte(); pStream->offset(3); - J = GSBPP - 2; + int32_t J = GSBPP - 2; while (J >= 0) { FXCODEC_STATUS status = - pGRD->Start_decode_MMR(&GSPLANES[J], pStream, nullptr); + pGRD->Start_decode_MMR(&GSPLANES.get()[J], pStream, nullptr); while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { pGRD->Continue_decode(pPause); } - if (!GSPLANES[J]) { - for (K = GSBPP - 1; K > J; K--) { - delete GSPLANES[K]; - goto failed; + if (!GSPLANES.get()[J]) { + for (int32_t K = GSBPP - 1; K > J; --K) { + delete GSPLANES.get()[K]; + return nullptr; } } pStream->alignByte(); pStream->offset(3); - GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR); + GSPLANES.get()[J]->composeFrom(0, 0, GSPLANES.get()[J + 1], + JBIG2_COMPOSE_XOR); J = J - 1; } - for (y = 0; y < GSH; y++) { - for (x = 0; x < GSW; x++) { - for (J = 0; J < GSBPP; J++) { - GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J; + nonstd::unique_ptr<FX_DWORD> GSVALS(FX_Alloc2D(FX_DWORD, GSW, GSH)); + JBIG2_memset(GSVALS.get(), 0, sizeof(FX_DWORD) * GSW * GSH); + for (FX_DWORD y = 0; y < GSH; ++y) { + for (FX_DWORD x = 0; x < GSW; ++x) { + for (J = 0; J < GSBPP; ++J) { + GSVALS.get()[y * GSW + x] |= GSPLANES.get()[J]->getPixel(x, y) << J; } } } - for (J = 0; J < GSBPP; J++) { - delete GSPLANES[J]; + for (J = 0; J < GSBPP; ++J) { + delete GSPLANES.get()[J]; } - FX_Free(GSPLANES); - return GSVALS; -failed: - FX_Free(GSPLANES); - FX_Free(GSVALS); - return nullptr; + return GSVALS.release(); } diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp index 704145806a..60b5f3ca37 100644 --- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp +++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp @@ -8,6 +8,8 @@ #include <string.h> +#include <vector> + #include "../../../include/fxcrt/fx_memory.h" #include "JBig2_BitStream.h" @@ -40,7 +42,7 @@ void CJBig2_HuffmanTable::init() { int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, int nLines, FX_BOOL bHTOOB) { - int CURLEN, LENMAX, CURCODE, CURTEMP, i; + int CURLEN, LENMAX, CURCODE, CURTEMP; int* LENCOUNT; int* FIRSTCODE; HTOOB = bHTOOB; @@ -50,7 +52,7 @@ int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, RANGELEN = FX_Alloc(int, NTEMP); RANGELOW = FX_Alloc(int, NTEMP); LENMAX = 0; - for (i = 0; i < NTEMP; i++) { + for (int i = 0; i < NTEMP; i++) { PREFLEN[i] = pTable[i].PREFLEN; RANGELEN[i] = pTable[i].RANDELEN; RANGELOW[i] = pTable[i].RANGELOW; @@ -61,7 +63,7 @@ int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, LENCOUNT = FX_Alloc(int, LENMAX + 1); JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); FIRSTCODE = FX_Alloc(int, LENMAX + 1); - for (i = 0; i < NTEMP; i++) { + for (int i = 0; i < NTEMP; i++) { LENCOUNT[PREFLEN[i]]++; } CURLEN = 1; @@ -93,92 +95,77 @@ int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, RANGELOW = FX_Realloc(int, RANGELOW, nSize); \ } int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { - unsigned char HTPS, HTRS; - FX_DWORD HTLOW, HTHIGH; - FX_DWORD CURRANGELOW; - FX_DWORD nSize = 16; - int CURLEN, LENMAX, CURCODE, CURTEMP; - int* LENCOUNT; - int* FIRSTCODE; unsigned char cTemp; - if (pStream->read1Byte(&cTemp) == -1) { - goto failed; - } + if (pStream->read1Byte(&cTemp) == -1) + return FALSE; + HTOOB = cTemp & 0x01; - HTPS = ((cTemp >> 1) & 0x07) + 1; - HTRS = ((cTemp >> 4) & 0x07) + 1; + unsigned char HTPS = ((cTemp >> 1) & 0x07) + 1; + unsigned char HTRS = ((cTemp >> 4) & 0x07) + 1; + FX_DWORD HTLOW; + FX_DWORD HTHIGH; if (pStream->readInteger(&HTLOW) == -1 || pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) { - goto failed; + return FALSE; } + FX_DWORD nSize = 16; PREFLEN = FX_Alloc(int, nSize); RANGELEN = FX_Alloc(int, nSize); RANGELOW = FX_Alloc(int, nSize); - CURRANGELOW = HTLOW; + FX_DWORD CURRANGELOW = HTLOW; NTEMP = 0; do { HT_CHECK_MEMORY_ADJUST if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) || (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) { - goto failed; + return FALSE; } RANGELOW[NTEMP] = CURRANGELOW; CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]); NTEMP = NTEMP + 1; } while (CURRANGELOW < HTHIGH); HT_CHECK_MEMORY_ADJUST - if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { - goto failed; - } + if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) + return FALSE; + RANGELEN[NTEMP] = 32; RANGELOW[NTEMP] = HTLOW - 1; NTEMP = NTEMP + 1; HT_CHECK_MEMORY_ADJUST - if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { - goto failed; - } + if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) + return FALSE; + RANGELEN[NTEMP] = 32; RANGELOW[NTEMP] = HTHIGH; NTEMP = NTEMP + 1; if (HTOOB) { HT_CHECK_MEMORY_ADJUST - if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { - goto failed; - } + if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) + return FALSE; NTEMP = NTEMP + 1; } CODES = FX_Alloc(int, NTEMP); - LENMAX = 0; + int LENMAX = 0; for (int i = 0; i < NTEMP; i++) { if (PREFLEN[i] > LENMAX) { LENMAX = PREFLEN[i]; } } - LENCOUNT = FX_Alloc(int, (LENMAX + 1)); - JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); - FIRSTCODE = FX_Alloc(int, (LENMAX + 1)); - for (int i = 0; i < NTEMP; i++) { + + std::vector<int> LENCOUNT(LENMAX + 1); + for (int i = 0; i < NTEMP; ++i) LENCOUNT[PREFLEN[i]]++; - } - CURLEN = 1; - FIRSTCODE[0] = 0; LENCOUNT[0] = 0; - while (CURLEN <= LENMAX) { - FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; - CURCODE = FIRSTCODE[CURLEN]; - CURTEMP = 0; - while (CURTEMP < NTEMP) { - if (PREFLEN[CURTEMP] == CURLEN) { - CODES[CURTEMP] = CURCODE; - CURCODE = CURCODE + 1; - } - CURTEMP = CURTEMP + 1; + + std::vector<int> FIRSTCODE(LENMAX + 1); + FIRSTCODE[0] = 0; + for (int i = 0; i <= LENMAX; ++i) { + FIRSTCODE[i] = (FIRSTCODE[i - 1] + LENCOUNT[i - 1]) << 1; + int CURCODE = FIRSTCODE[i]; + for (int j = 0; j < NTEMP; ++j) { + if (PREFLEN[j] == i) + CODES[j] = CURCODE++; } - CURLEN = CURLEN + 1; } - FX_Free(LENCOUNT); - FX_Free(FIRSTCODE); return TRUE; -failed: - return FALSE; } |