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