33 pdfium::span<JBig2ArithCtx> gbContexts,
34 pdfium::span<JBig2ArithCtx> grContexts) {
49 uint32_t SBSYMCODELENA = 0;
55 std::vector<std::unique_ptr<CJBig2_Image>> SDNEWSYMS(
SDNUMNEWSYMS);
56 uint32_t HCHEIGHT = 0;
57 uint32_t NSYMSDECODED = 0;
59 std::unique_ptr<CJBig2_Image> BS;
61 IADH->Decode(pArithDecoder, &HCDH);
62 HCHEIGHT = HCHEIGHT + HCDH;
66 uint32_t SYMWIDTH = 0;
69 if (!IADW->Decode(pArithDecoder, &DW))
75 SYMWIDTH = SYMWIDTH + DW;
79 if (HCHEIGHT == 0 || SYMWIDTH == 0) {
90 pGRD->USESKIP =
false;
91 pGRD->GBAT[0] = SDAT[0];
92 pGRD->GBAT[1] = SDAT[1];
93 pGRD->GBAT[2] = SDAT[2];
94 pGRD->GBAT[3] = SDAT[3];
95 pGRD->GBAT[4] = SDAT[4];
96 pGRD->GBAT[5] = SDAT[5];
97 pGRD->GBAT[6] = SDAT[6];
98 pGRD->GBAT[7] = SDAT[7];
99 BS = pGRD->DecodeArith(pArithDecoder, gbContexts);
103 uint32_t REFAGGNINST;
104 IAAI->Decode(pArithDecoder, (
int*)&REFAGGNINST);
105 if (REFAGGNINST > 1) {
116 pDecoder->SBHUFF =
SDHUFF;
117 pDecoder->SBREFINE =
true;
118 pDecoder->SBW = SYMWIDTH;
119 pDecoder->SBH = HCHEIGHT;
120 pDecoder->SBNUMINSTANCES = REFAGGNINST;
121 pDecoder->SBSTRIPS = 1;
124 while ((uint32_t)(1 << nTmp) < pDecoder->SBNUMSYMS) {
127 uint8_t SBSYMCODELEN = (uint8_t)nTmp;
128 pDecoder->SBSYMCODELEN = SBSYMCODELEN;
129 std::vector<UnownedPtr<CJBig2_Image>> SBSYMS(pDecoder->SBNUMSYMS);
130 fxcrt::Copy(pdfium::make_span(SDINSYMS).first(SDNUMINSYMS),
131 pdfium::make_span(SBSYMS));
132 for (size_t i = 0; i < NSYMSDECODED; ++i) {
135 pDecoder->SBSYMS =
std::move(SBSYMS);
136 pDecoder->SBDEFPIXEL =
false;
138 pDecoder->TRANSPOSED =
false;
140 pDecoder->SBDSOFFSET = 0;
141 pDecoder->SBHUFFFS = SBHUFFFS.get();
142 pDecoder->SBHUFFDS = SBHUFFDS.get();
143 pDecoder->SBHUFFDT = SBHUFFDT.get();
144 pDecoder->SBHUFFRDW = SBHUFFRDW.get();
145 pDecoder->SBHUFFRDH = SBHUFFRDH.get();
146 pDecoder->SBHUFFRDX = SBHUFFRDX.get();
147 pDecoder->SBHUFFRDY = SBHUFFRDY.get();
148 pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
150 pDecoder->SBRAT[0] = SDRAT[0];
151 pDecoder->SBRAT[1] = SDRAT[1];
152 pDecoder->SBRAT[2] = SDRAT[2];
153 pDecoder->SBRAT[3] = SDRAT[3];
155 ids.IADT = IADT.get();
156 ids.IAFS = IAFS.get();
157 ids.IADS = IADS.get();
158 ids.IAIT = IAIT.get();
159 ids.IARI = IARI.get();
160 ids.IARDW = IARDW.get();
161 ids.IARDH = IARDH.get();
162 ids.IARDX = IARDX.get();
163 ids.IARDY = IARDY.get();
164 ids.IAID = IAID.get();
165 BS = pDecoder->DecodeArith(pArithDecoder, grContexts, &ids);
168 }
else if (REFAGGNINST == 1) {
171 IAID->Decode(pArithDecoder, &IDI);
172 if (IDI >= SBNUMSYMS)
181 IARDX->Decode(pArithDecoder, &RDXI);
182 IARDY->Decode(pArithDecoder, &RDYI);
185 pGRRD->GRW = SYMWIDTH;
186 pGRRD->GRH = HCHEIGHT;
188 pGRRD->GRREFERENCE = sbsyms_idi;
189 pGRRD->GRREFERENCEDX = RDXI;
190 pGRRD->GRREFERENCEDY = RDYI;
191 pGRRD->TPGRON =
false;
192 pGRRD->GRAT[0] = SDRAT[0];
193 pGRRD->GRAT[1] = SDRAT[1];
194 pGRRD->GRAT[2] = SDRAT[2];
195 pGRRD->GRAT[3] = SDRAT[3];
196 BS = pGRRD->Decode(pArithDecoder, grContexts);
201 SDNEWSYMS[NSYMSDECODED] =
std::move(BS);
206 std::vector<
bool> EXFLAGS;
208 bool CUREXFLAG =
false;
209 uint32_t EXINDEX = 0;
210 uint32_t num_ex_syms = 0;
212 uint32_t EXRUNLENGTH;
213 IAEX->Decode(pArithDecoder, (
int*)&EXRUNLENGTH);
215 new_ex_size += EXRUNLENGTH;
216 if (!new_ex_size.IsValid() ||
222 num_ex_syms += EXRUNLENGTH;
223 std::fill_n(EXFLAGS.begin() + EXINDEX, EXRUNLENGTH, CUREXFLAG);
224 EXINDEX = new_ex_size.ValueOrDie();
225 CUREXFLAG = !CUREXFLAG;
230 std::unique_ptr<CJBig2_SymbolDict> pDict =
237 UNSAFE_TODO(SDINSYMS[i] ? std::make_unique<CJBig2_Image>(*SDINSYMS[i])
249 pdfium::span<JBig2ArithCtx> gbContexts,
250 pdfium::span<JBig2ArithCtx> grContexts) {
252 std::vector<std::unique_ptr<CJBig2_Image>> SDNEWSYMS(
SDNUMNEWSYMS);
253 std::vector<uint32_t> SDNEWSYMWIDTHS;
256 uint32_t HCHEIGHT = 0;
257 uint32_t NSYMSDECODED = 0;
258 std::unique_ptr<CJBig2_Image> BS;
261 if (pHuffmanDecoder->DecodeAValue(SDHUFFDH, &HCDH) != 0)
264 HCHEIGHT = HCHEIGHT + HCDH;
268 uint32_t SYMWIDTH = 0;
269 uint32_t TOTWIDTH = 0;
270 uint32_t HCFIRSTSYM = NSYMSDECODED;
273 int32_t nVal = pHuffmanDecoder->DecodeAValue(SDHUFFDW, &DW);
281 SYMWIDTH = SYMWIDTH + DW;
285 TOTWIDTH += SYMWIDTH;
286 if (HCHEIGHT == 0 || SYMWIDTH == 0) {
291 uint32_t REFAGGNINST;
292 if (pHuffmanDecoder->DecodeAValue(SDHUFFAGGINST, (
int*)&REFAGGNINST) !=
297 if (REFAGGNINST > 1) {
308 pDecoder->SBHUFF =
SDHUFF;
309 pDecoder->SBREFINE =
true;
310 pDecoder->SBW = SYMWIDTH;
311 pDecoder->SBH = HCHEIGHT;
312 pDecoder->SBNUMINSTANCES = REFAGGNINST;
313 pDecoder->SBSTRIPS = 1;
315 std::vector<JBig2HuffmanCode> SBSYMCODES(pDecoder->SBNUMSYMS);
317 while (
static_cast<uint32_t>(1 << nTmp) < pDecoder->SBNUMSYMS)
319 for (uint32_t i = 0; i < pDecoder->SBNUMSYMS; ++i) {
320 SBSYMCODES[i].codelen = nTmp;
321 SBSYMCODES[i].code = i;
323 pDecoder->SBSYMCODES =
std::move(SBSYMCODES);
324 std::vector<UnownedPtr<CJBig2_Image>> SBSYMS(pDecoder->SBNUMSYMS);
325 fxcrt::Copy(pdfium::make_span(SDINSYMS).first(SDNUMINSYMS),
326 pdfium::make_span(SBSYMS));
327 for (size_t i = 0; i < NSYMSDECODED; ++i) {
330 pDecoder->SBSYMS =
std::move(SBSYMS);
331 pDecoder->SBDEFPIXEL =
false;
333 pDecoder->TRANSPOSED =
false;
335 pDecoder->SBDSOFFSET = 0;
336 pDecoder->SBHUFFFS = SBHUFFFS.get();
337 pDecoder->SBHUFFDS = SBHUFFDS.get();
338 pDecoder->SBHUFFDT = SBHUFFDT.get();
339 pDecoder->SBHUFFRDW = SBHUFFRDW.get();
340 pDecoder->SBHUFFRDH = SBHUFFRDH.get();
341 pDecoder->SBHUFFRDX = SBHUFFRDX.get();
342 pDecoder->SBHUFFRDY = SBHUFFRDY.get();
343 pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
345 pDecoder->SBRAT[0] = SDRAT[0];
346 pDecoder->SBRAT[1] = SDRAT[1];
347 pDecoder->SBRAT[2] = SDRAT[2];
348 pDecoder->SBRAT[3] = SDRAT[3];
349 BS = pDecoder->DecodeHuffman(pStream, grContexts);
353 }
else if (REFAGGNINST == 1) {
356 while ((uint32_t)(1 << nTmp) < SBNUMSYMS) {
359 uint8_t SBSYMCODELEN = (uint8_t)nTmp;
366 uVal = (uVal << 1) | nTmp;
367 if (uVal >= SBNUMSYMS)
370 IDI = SBSYMCODELEN == 0 ? uVal : SBNUMSYMS;
383 if ((pHuffmanDecoder->DecodeAValue(SBHUFFRDX.get(), &RDXI) != 0) ||
384 (pHuffmanDecoder->DecodeAValue(SBHUFFRDX.get(), &RDYI) != 0) ||
385 (pHuffmanDecoder->DecodeAValue(SBHUFFRSIZE.get(), &nVal) != 0)) {
393 pGRRD->GRW = SYMWIDTH;
394 pGRRD->GRH = HCHEIGHT;
396 pGRRD->GRREFERENCE = sbsyms_idi;
397 pGRRD->GRREFERENCEDX = RDXI;
398 pGRRD->GRREFERENCEDY = RDYI;
399 pGRRD->TPGRON =
false;
400 pGRRD->GRAT[0] = SDRAT[0];
401 pGRRD->GRAT[1] = SDRAT[1];
402 pGRRD->GRAT[2] = SDRAT[2];
403 pGRRD->GRAT[3] = SDRAT[3];
405 BS = pGRRD->Decode(pArithDecoder.get(), grContexts);
414 SDNEWSYMS[NSYMSDECODED] =
std::move(BS);
417 SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
422 if (pHuffmanDecoder->DecodeAValue(SDHUFFBMSIZE, (int32_t*)&BMSIZE) != 0) {
426 std::unique_ptr<CJBig2_Image> BHC;
433 const uint32_t stride = (TOTWIDTH + 7) / 8;
435 safe_image_size *= HCHEIGHT;
436 if (!safe_image_size.IsValid() ||
442 for (uint32_t i = 0; i < HCHEIGHT; ++i) {
443 UNSAFE_TODO(FXSYS_memcpy(BHC->data() + i * BHC->stride(),
450 pGRD->GBW = TOTWIDTH;
451 pGRD->GBH = HCHEIGHT;
452 pGRD->StartDecodeMMR(&BHC, pStream);
459 for (uint32_t i = HCFIRSTSYM; i < NSYMSDECODED; ++i) {
460 SDNEWSYMS[i] = BHC->SubImage(nTmp, 0, SDNEWSYMWIDTHS[i], HCHEIGHT);
461 nTmp += SDNEWSYMWIDTHS[i];
466 std::unique_ptr<CJBig2_HuffmanTable> pTable =
468 std::vector<
bool> EXFLAGS;
470 bool CUREXFLAG =
false;
471 uint32_t EXINDEX = 0;
472 uint32_t num_ex_syms = 0;
474 uint32_t EXRUNLENGTH;
475 if (pHuffmanDecoder->DecodeAValue(pTable.get(), (
int*)&EXRUNLENGTH) != 0)
479 new_ex_size += EXRUNLENGTH;
480 if (!new_ex_size.IsValid() ||
486 num_ex_syms += EXRUNLENGTH;
487 std::fill_n(EXFLAGS.begin() + EXINDEX, EXRUNLENGTH, CUREXFLAG);
488 EXINDEX = new_ex_size.ValueOrDie();
489 CUREXFLAG = !CUREXFLAG;
500 UNSAFE_TODO(SDINSYMS[i] ? std::make_unique<CJBig2_Image>(*SDINSYMS[i])