| OLD | NEW |
| 1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 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_TrdProc.h" | 7 #include "JBig2_TrdProc.h" |
| 8 | 8 |
| 9 #include "../../../../third_party/base/nonstd_unique_ptr.h" | 9 #include "../../../../third_party/base/nonstd_unique_ptr.h" |
| 10 #include "JBig2_ArithDecoder.h" | 10 #include "JBig2_ArithDecoder.h" |
| 11 #include "JBig2_ArithIntDecoder.h" | 11 #include "JBig2_ArithIntDecoder.h" |
| 12 #include "JBig2_HuffmanDecoder.h" | 12 #include "JBig2_HuffmanDecoder.h" |
| 13 #include "JBig2_GrrdProc.h" | 13 #include "JBig2_GrrdProc.h" |
| 14 | 14 |
| 15 CJBig2_Image* CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream* pStream, | 15 CJBig2_Image* CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream* pStream, |
| 16 JBig2ArithCtx* grContext) { | 16 JBig2ArithCtx* grContext) { |
| 17 int32_t STRIPT, FIRSTS; | 17 int32_t STRIPT, FIRSTS; |
| 18 FX_DWORD NINSTANCES; | 18 FX_DWORD NINSTANCES; |
| 19 int32_t DT, DFS, CURS; | 19 int32_t DT, DFS, CURS; |
| 20 uint8_t CURT; | |
| 21 int32_t SI, TI; | 20 int32_t SI, TI; |
| 22 FX_DWORD IDI; | |
| 23 CJBig2_Image* IBI; | 21 CJBig2_Image* IBI; |
| 24 FX_DWORD WI, HI; | 22 FX_DWORD WI, HI; |
| 25 int32_t IDS; | 23 int32_t IDS; |
| 26 FX_BOOL RI; | 24 FX_BOOL RI; |
| 27 int32_t RDWI, RDHI, RDXI, RDYI; | 25 int32_t RDWI, RDHI, RDXI, RDYI; |
| 28 CJBig2_Image* IBOI; | 26 CJBig2_Image* IBOI; |
| 29 FX_DWORD WOI, HOI; | 27 FX_DWORD WOI, HOI; |
| 30 FX_BOOL bFirst; | 28 FX_BOOL bFirst; |
| 31 FX_DWORD nTmp; | 29 FX_DWORD nTmp; |
| 32 int32_t nVal, nBits; | 30 int32_t nVal, nBits; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 59 } else { | 57 } else { |
| 60 nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS); | 58 nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS); |
| 61 if (nVal == JBIG2_OOB) { | 59 if (nVal == JBIG2_OOB) { |
| 62 break; | 60 break; |
| 63 } else if (nVal != 0) { | 61 } else if (nVal != 0) { |
| 64 return nullptr; | 62 return nullptr; |
| 65 } else { | 63 } else { |
| 66 CURS = CURS + IDS + SBDSOFFSET; | 64 CURS = CURS + IDS + SBDSOFFSET; |
| 67 } | 65 } |
| 68 } | 66 } |
| 69 if (SBSTRIPS == 1) { | 67 uint8_t CURT = 0; |
| 70 CURT = 0; | 68 if (SBSTRIPS != 1) { |
| 71 } else { | |
| 72 nTmp = 1; | 69 nTmp = 1; |
| 73 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) { | 70 while ((FX_DWORD)(1 << nTmp) < SBSTRIPS) { |
| 74 nTmp++; | 71 nTmp++; |
| 75 } | 72 } |
| 76 if (pStream->readNBits(nTmp, &nVal) != 0) | 73 if (pStream->readNBits(nTmp, &nVal) != 0) |
| 77 return nullptr; | 74 return nullptr; |
| 78 | 75 |
| 79 CURT = nVal; | 76 CURT = nVal; |
| 80 } | 77 } |
| 81 TI = STRIPT + CURT; | 78 TI = STRIPT + CURT; |
| 82 nVal = 0; | 79 nVal = 0; |
| 83 nBits = 0; | 80 nBits = 0; |
| 81 FX_DWORD IDI; |
| 84 for (;;) { | 82 for (;;) { |
| 85 if (pStream->read1Bit(&nTmp) != 0) | 83 if (pStream->read1Bit(&nTmp) != 0) |
| 86 return nullptr; | 84 return nullptr; |
| 87 | 85 |
| 88 nVal = (nVal << 1) | nTmp; | 86 nVal = (nVal << 1) | nTmp; |
| 89 nBits++; | 87 nBits++; |
| 90 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { | 88 for (IDI = 0; IDI < SBNUMSYMS; IDI++) { |
| 91 if ((nBits == SBSYMCODES[IDI].codelen) && | 89 if ((nBits == SBSYMCODES[IDI].codelen) && |
| 92 (nVal == SBSYMCODES[IDI].code)) { | 90 (nVal == SBSYMCODES[IDI].code)) { |
| 93 break; | 91 break; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 } | 210 } |
| 213 return SBREG.release(); | 211 return SBREG.release(); |
| 214 } | 212 } |
| 215 | 213 |
| 216 CJBig2_Image* CJBig2_TRDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder, | 214 CJBig2_Image* CJBig2_TRDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder, |
| 217 JBig2ArithCtx* grContext, | 215 JBig2ArithCtx* grContext, |
| 218 JBig2IntDecoderState* pIDS) { | 216 JBig2IntDecoderState* pIDS) { |
| 219 int32_t STRIPT, FIRSTS; | 217 int32_t STRIPT, FIRSTS; |
| 220 FX_DWORD NINSTANCES; | 218 FX_DWORD NINSTANCES; |
| 221 int32_t DT, DFS, CURS; | 219 int32_t DT, DFS, CURS; |
| 222 int32_t CURT; | |
| 223 int32_t SI, TI; | 220 int32_t SI, TI; |
| 224 FX_DWORD IDI; | |
| 225 CJBig2_Image* IBI; | 221 CJBig2_Image* IBI; |
| 226 FX_DWORD WI, HI; | 222 FX_DWORD WI, HI; |
| 227 int32_t IDS; | 223 int32_t IDS; |
| 228 int RI; | 224 int RI; |
| 229 int32_t RDWI, RDHI, RDXI, RDYI; | 225 int32_t RDWI, RDHI, RDXI, RDYI; |
| 230 CJBig2_Image* IBOI; | 226 CJBig2_Image* IBOI; |
| 231 FX_DWORD WOI, HOI; | 227 FX_DWORD WOI, HOI; |
| 232 FX_BOOL bFirst; | 228 FX_BOOL bFirst; |
| 233 int32_t nRet, nVal; | 229 int32_t nRet; |
| 234 int32_t bRetained; | 230 int32_t bRetained; |
| 235 CJBig2_ArithIntDecoder* IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH, | 231 CJBig2_ArithIntDecoder* IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH, |
| 236 *IARDX, *IARDY; | 232 *IARDX, *IARDY; |
| 237 CJBig2_ArithIaidDecoder* IAID; | 233 CJBig2_ArithIaidDecoder* IAID; |
| 238 if (pIDS) { | 234 if (pIDS) { |
| 239 IADT = pIDS->IADT; | 235 IADT = pIDS->IADT; |
| 240 IAFS = pIDS->IAFS; | 236 IAFS = pIDS->IAFS; |
| 241 IADS = pIDS->IADS; | 237 IADS = pIDS->IADS; |
| 242 IAIT = pIDS->IAIT; | 238 IAIT = pIDS->IAIT; |
| 243 IARI = pIDS->IARI; | 239 IARI = pIDS->IARI; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 255 IARI = new CJBig2_ArithIntDecoder(); | 251 IARI = new CJBig2_ArithIntDecoder(); |
| 256 IARDW = new CJBig2_ArithIntDecoder(); | 252 IARDW = new CJBig2_ArithIntDecoder(); |
| 257 IARDH = new CJBig2_ArithIntDecoder(); | 253 IARDH = new CJBig2_ArithIntDecoder(); |
| 258 IARDX = new CJBig2_ArithIntDecoder(); | 254 IARDX = new CJBig2_ArithIntDecoder(); |
| 259 IARDY = new CJBig2_ArithIntDecoder(); | 255 IARDY = new CJBig2_ArithIntDecoder(); |
| 260 IAID = new CJBig2_ArithIaidDecoder(SBSYMCODELEN); | 256 IAID = new CJBig2_ArithIaidDecoder(SBSYMCODELEN); |
| 261 bRetained = FALSE; | 257 bRetained = FALSE; |
| 262 } | 258 } |
| 263 nonstd::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); | 259 nonstd::unique_ptr<CJBig2_Image> SBREG(new CJBig2_Image(SBW, SBH)); |
| 264 SBREG->fill(SBDEFPIXEL); | 260 SBREG->fill(SBDEFPIXEL); |
| 265 if (IADT->decode(pArithDecoder, &STRIPT) == -1) { | 261 if (!IADT->decode(pArithDecoder, &STRIPT)) { |
| 266 goto failed; | 262 goto failed; |
| 267 } | 263 } |
| 268 STRIPT *= SBSTRIPS; | 264 STRIPT *= SBSTRIPS; |
| 269 STRIPT = -STRIPT; | 265 STRIPT = -STRIPT; |
| 270 FIRSTS = 0; | 266 FIRSTS = 0; |
| 271 NINSTANCES = 0; | 267 NINSTANCES = 0; |
| 272 while (NINSTANCES < SBNUMINSTANCES) { | 268 while (NINSTANCES < SBNUMINSTANCES) { |
| 273 if (IADT->decode(pArithDecoder, &DT) == -1) { | 269 if (!IADT->decode(pArithDecoder, &DT)) { |
| 274 goto failed; | 270 goto failed; |
| 275 } | 271 } |
| 276 DT *= SBSTRIPS; | 272 DT *= SBSTRIPS; |
| 277 STRIPT = STRIPT + DT; | 273 STRIPT = STRIPT + DT; |
| 278 bFirst = TRUE; | 274 bFirst = TRUE; |
| 279 for (;;) { | 275 for (;;) { |
| 280 if (bFirst) { | 276 if (bFirst) { |
| 281 if (IAFS->decode(pArithDecoder, &DFS) == -1) { | 277 if (!IAFS->decode(pArithDecoder, &DFS)) { |
| 282 goto failed; | 278 goto failed; |
| 283 } | 279 } |
| 284 FIRSTS = FIRSTS + DFS; | 280 FIRSTS = FIRSTS + DFS; |
| 285 CURS = FIRSTS; | 281 CURS = FIRSTS; |
| 286 bFirst = FALSE; | 282 bFirst = FALSE; |
| 287 } else { | 283 } else { |
| 288 nRet = IADS->decode(pArithDecoder, &IDS); | 284 nRet = IADS->decode(pArithDecoder, &IDS); |
| 289 if (nRet == JBIG2_OOB) { | 285 if (nRet == JBIG2_OOB) { |
| 290 break; | 286 break; |
| 291 } else if (nRet != 0) { | 287 } else if (nRet != 0) { |
| 292 goto failed; | 288 goto failed; |
| 293 } else { | 289 } else { |
| 294 CURS = CURS + IDS + SBDSOFFSET; | 290 CURS = CURS + IDS + SBDSOFFSET; |
| 295 } | 291 } |
| 296 } | 292 } |
| 297 if (NINSTANCES >= SBNUMINSTANCES) { | 293 if (NINSTANCES >= SBNUMINSTANCES) { |
| 298 break; | 294 break; |
| 299 } | 295 } |
| 300 if (SBSTRIPS == 1) { | 296 int CURT = 0; |
| 301 CURT = 0; | 297 if (SBSTRIPS != 1) { |
| 302 } else { | 298 if (!IAIT->decode(pArithDecoder, &CURT)) { |
| 303 if (IAIT->decode(pArithDecoder, &nVal) == -1) { | |
| 304 goto failed; | 299 goto failed; |
| 305 } | 300 } |
| 306 CURT = nVal; | |
| 307 } | 301 } |
| 308 TI = STRIPT + CURT; | 302 TI = STRIPT + CURT; |
| 309 if (IAID->decode(pArithDecoder, &nVal) == -1) { | 303 FX_DWORD IDI; |
| 310 goto failed; | 304 IAID->decode(pArithDecoder, &IDI); |
| 311 } | |
| 312 IDI = nVal; | |
| 313 if (IDI >= SBNUMSYMS) { | 305 if (IDI >= SBNUMSYMS) { |
| 314 goto failed; | 306 goto failed; |
| 315 } | 307 } |
| 316 if (SBREFINE == 0) { | 308 if (SBREFINE == 0) { |
| 317 RI = 0; | 309 RI = 0; |
| 318 } else { | 310 } else { |
| 319 if (IARI->decode(pArithDecoder, &RI) == -1) { | 311 if (!IARI->decode(pArithDecoder, &RI)) { |
| 320 goto failed; | 312 goto failed; |
| 321 } | 313 } |
| 322 } | 314 } |
| 323 if (!SBSYMS[IDI]) { | 315 if (!SBSYMS[IDI]) { |
| 324 goto failed; | 316 goto failed; |
| 325 } | 317 } |
| 326 if (RI == 0) { | 318 if (RI == 0) { |
| 327 IBI = SBSYMS[IDI]; | 319 IBI = SBSYMS[IDI]; |
| 328 } else { | 320 } else { |
| 329 if ((IARDW->decode(pArithDecoder, &RDWI) == -1) || | 321 if (!IARDW->decode(pArithDecoder, &RDWI) || |
| 330 (IARDH->decode(pArithDecoder, &RDHI) == -1) || | 322 !IARDH->decode(pArithDecoder, &RDHI) || |
| 331 (IARDX->decode(pArithDecoder, &RDXI) == -1) || | 323 !IARDX->decode(pArithDecoder, &RDXI) || |
| 332 (IARDY->decode(pArithDecoder, &RDYI) == -1)) { | 324 !IARDY->decode(pArithDecoder, &RDYI)) { |
| 333 goto failed; | 325 goto failed; |
| 334 } | 326 } |
| 335 IBOI = SBSYMS[IDI]; | 327 IBOI = SBSYMS[IDI]; |
| 336 WOI = IBOI->m_nWidth; | 328 WOI = IBOI->m_nWidth; |
| 337 HOI = IBOI->m_nHeight; | 329 HOI = IBOI->m_nHeight; |
| 338 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) { | 330 if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) { |
| 339 goto failed; | 331 goto failed; |
| 340 } | 332 } |
| 341 nonstd::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); | 333 nonstd::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); |
| 342 pGRRD->GRW = WOI + RDWI; | 334 pGRRD->GRW = WOI + RDWI; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 delete IAIT; | 421 delete IAIT; |
| 430 delete IARI; | 422 delete IARI; |
| 431 delete IARDW; | 423 delete IARDW; |
| 432 delete IARDH; | 424 delete IARDH; |
| 433 delete IARDX; | 425 delete IARDX; |
| 434 delete IARDY; | 426 delete IARDY; |
| 435 delete IAID; | 427 delete IAID; |
| 436 } | 428 } |
| 437 return nullptr; | 429 return nullptr; |
| 438 } | 430 } |
| OLD | NEW |