1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include "JBig2_ArithIntDecoder.h" 8 CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder() 9 { 10 IAx = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), 512); 11 JBIG2_memset(IAx, 0, sizeof(JBig2ArithCtx) * 512); 12 } 13 CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder() 14 { 15 m_pModule->JBig2_Free(IAx); 16 } 17 int CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult) 18 { 19 int PREV, V; 20 int S, D; 21 int nNeedBits, nTemp, i; 22 PREV = 1; 23 S = pArithDecoder->DECODE(IAx + PREV); 24 PREV = (PREV << 1) | S; 25 D = pArithDecoder->DECODE(IAx + PREV); 26 PREV = (PREV << 1) | D; 27 if(D) { 28 D = pArithDecoder->DECODE(IAx + PREV); 29 PREV = (PREV << 1) | D; 30 if(D) { 31 D = pArithDecoder->DECODE(IAx + PREV); 32 PREV = (PREV << 1) | D; 33 if(D) { 34 D = pArithDecoder->DECODE(IAx + PREV); 35 PREV = (PREV << 1) | D; 36 if(D) { 37 D = pArithDecoder->DECODE(IAx + PREV); 38 PREV = (PREV << 1) | D; 39 if(D) { 40 nNeedBits = 32; 41 V = 4436; 42 } else { 43 nNeedBits = 12; 44 V = 340; 45 } 46 } else { 47 nNeedBits = 8; 48 V = 84; 49 } 50 } else { 51 nNeedBits = 6; 52 V = 20; 53 } 54 } else { 55 nNeedBits = 4; 56 V = 4; 57 } 58 } else { 59 nNeedBits = 2; 60 V = 0; 61 } 62 nTemp = 0; 63 for(i = 0; i < nNeedBits; i++) { 64 D = pArithDecoder->DECODE(IAx + PREV); 65 if(PREV < 256) { 66 PREV = (PREV << 1) | D; 67 } else { 68 PREV = (((PREV << 1) | D) & 511) | 256; 69 } 70 nTemp = (nTemp << 1) | D; 71 } 72 V += nTemp; 73 if(S == 1 && V > 0) { 74 V = -V; 75 } 76 *nResult = V; 77 if(S == 1 && V == 0) { 78 return JBIG2_OOB; 79 } 80 return 0; 81 } 82 CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA) 83 { 84 SBSYMCODELEN = SBSYMCODELENA; 85 IAID = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), (1 << SBSYMCODELEN)); 86 JBIG2_memset(IAID, 0, sizeof(JBig2ArithCtx) * (int)(1 << SBSYMCODELEN)); 87 } 88 CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder() 89 { 90 m_pModule->JBig2_Free(IAID); 91 } 92 int CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult) 93 { 94 int PREV; 95 int D; 96 int i; 97 PREV = 1; 98 for(i = 0; i < SBSYMCODELEN; i++) { 99 D = pArithDecoder->DECODE(IAID + PREV); 100 PREV = (PREV << 1) | D; 101 } 102 PREV = PREV - (1 << SBSYMCODELEN); 103 *nResult = PREV; 104 return 0; 105 } 106