• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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 "core/fxcodec/jbig2/JBig2_SddProc.h"
8 
9 #include <algorithm>
10 #include <memory>
11 #include <utility>
12 #include <vector>
13 
14 #include "core/fxcodec/jbig2/JBig2_ArithIntDecoder.h"
15 #include "core/fxcodec/jbig2/JBig2_GrdProc.h"
16 #include "core/fxcodec/jbig2/JBig2_GrrdProc.h"
17 #include "core/fxcodec/jbig2/JBig2_HuffmanDecoder.h"
18 #include "core/fxcodec/jbig2/JBig2_HuffmanTable.h"
19 #include "core/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h"
20 #include "core/fxcodec/jbig2/JBig2_SymbolDict.h"
21 #include "core/fxcodec/jbig2/JBig2_TrdProc.h"
22 #include "third_party/base/ptr_util.h"
23 
decode_Arith(CJBig2_ArithDecoder * pArithDecoder,std::vector<JBig2ArithCtx> * gbContext,std::vector<JBig2ArithCtx> * grContext)24 std::unique_ptr<CJBig2_SymbolDict> CJBig2_SDDProc::decode_Arith(
25     CJBig2_ArithDecoder* pArithDecoder,
26     std::vector<JBig2ArithCtx>* gbContext,
27     std::vector<JBig2ArithCtx>* grContext) {
28   std::vector<std::unique_ptr<CJBig2_Image>> SDNEWSYMS;
29   uint32_t HCHEIGHT, NSYMSDECODED;
30   int32_t HCDH;
31   uint32_t SYMWIDTH, TOTWIDTH;
32   int32_t DW;
33   uint32_t I, J, REFAGGNINST;
34   std::vector<bool> EXFLAGS;
35   uint32_t EXINDEX;
36   bool CUREXFLAG;
37   uint32_t EXRUNLENGTH;
38   uint32_t nTmp;
39   uint32_t SBNUMSYMS;
40   uint8_t SBSYMCODELEN;
41   int32_t RDXI, RDYI;
42   uint32_t num_ex_syms;
43   // Pointers are not owned
44   std::vector<CJBig2_Image*> SBSYMS;
45   std::unique_ptr<CJBig2_ArithIaidDecoder> IAID;
46   std::unique_ptr<CJBig2_SymbolDict> pDict;
47   auto IADH = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
48   auto IADW = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
49   auto IAAI = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
50   auto IARDX = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
51   auto IARDY = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
52   auto IAEX = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
53   auto IADT = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
54   auto IAFS = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
55   auto IADS = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
56   auto IAIT = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
57   auto IARI = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
58   auto IARDW = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
59   auto IARDH = pdfium::MakeUnique<CJBig2_ArithIntDecoder>();
60   nTmp = 0;
61   while ((uint32_t)(1 << nTmp) < (SDNUMINSYMS + SDNUMNEWSYMS)) {
62     nTmp++;
63   }
64   IAID = pdfium::MakeUnique<CJBig2_ArithIaidDecoder>((uint8_t)nTmp);
65   SDNEWSYMS.resize(SDNUMNEWSYMS);
66 
67   HCHEIGHT = 0;
68   NSYMSDECODED = 0;
69   while (NSYMSDECODED < SDNUMNEWSYMS) {
70     std::unique_ptr<CJBig2_Image> BS;
71     IADH->decode(pArithDecoder, &HCDH);
72     HCHEIGHT = HCHEIGHT + HCDH;
73     if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE)
74       return nullptr;
75 
76     SYMWIDTH = 0;
77     TOTWIDTH = 0;
78     for (;;) {
79       if (!IADW->decode(pArithDecoder, &DW))
80         break;
81 
82       if (NSYMSDECODED >= SDNUMNEWSYMS)
83         return nullptr;
84 
85       SYMWIDTH = SYMWIDTH + DW;
86       if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE)
87         return nullptr;
88 
89       if (HCHEIGHT == 0 || SYMWIDTH == 0) {
90         TOTWIDTH = TOTWIDTH + SYMWIDTH;
91         SDNEWSYMS[NSYMSDECODED] = nullptr;
92         NSYMSDECODED = NSYMSDECODED + 1;
93         continue;
94       }
95       TOTWIDTH = TOTWIDTH + SYMWIDTH;
96       if (SDREFAGG == 0) {
97         auto pGRD = pdfium::MakeUnique<CJBig2_GRDProc>();
98         pGRD->MMR = 0;
99         pGRD->GBW = SYMWIDTH;
100         pGRD->GBH = HCHEIGHT;
101         pGRD->GBTEMPLATE = SDTEMPLATE;
102         pGRD->TPGDON = 0;
103         pGRD->USESKIP = 0;
104         pGRD->GBAT[0] = SDAT[0];
105         pGRD->GBAT[1] = SDAT[1];
106         pGRD->GBAT[2] = SDAT[2];
107         pGRD->GBAT[3] = SDAT[3];
108         pGRD->GBAT[4] = SDAT[4];
109         pGRD->GBAT[5] = SDAT[5];
110         pGRD->GBAT[6] = SDAT[6];
111         pGRD->GBAT[7] = SDAT[7];
112         BS = pGRD->decode_Arith(pArithDecoder, gbContext->data());
113         if (!BS)
114           return nullptr;
115       } else {
116         IAAI->decode(pArithDecoder, (int*)&REFAGGNINST);
117         if (REFAGGNINST > 1) {
118           auto pDecoder = pdfium::MakeUnique<CJBig2_TRDProc>();
119           pDecoder->SBHUFF = SDHUFF;
120           pDecoder->SBREFINE = 1;
121           pDecoder->SBW = SYMWIDTH;
122           pDecoder->SBH = HCHEIGHT;
123           pDecoder->SBNUMINSTANCES = REFAGGNINST;
124           pDecoder->SBSTRIPS = 1;
125           pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
126           SBNUMSYMS = pDecoder->SBNUMSYMS;
127           nTmp = 0;
128           while ((uint32_t)(1 << nTmp) < SBNUMSYMS) {
129             nTmp++;
130           }
131           SBSYMCODELEN = (uint8_t)nTmp;
132           pDecoder->SBSYMCODELEN = SBSYMCODELEN;
133           SBSYMS.resize(SBNUMSYMS);
134           std::copy(SDINSYMS, SDINSYMS + SDNUMINSYMS, SBSYMS.begin());
135           for (size_t i = 0; i < NSYMSDECODED; ++i)
136             SBSYMS[i + SDNUMINSYMS] = SDNEWSYMS[i].get();
137           pDecoder->SBSYMS = SBSYMS.data();
138           pDecoder->SBDEFPIXEL = 0;
139           pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
140           pDecoder->TRANSPOSED = 0;
141           pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
142           pDecoder->SBDSOFFSET = 0;
143           auto SBHUFFFS = pdfium::MakeUnique<CJBig2_HuffmanTable>(
144               HuffmanTable_B6, HuffmanTable_B6_Size, HuffmanTable_HTOOB_B6);
145           auto SBHUFFDS = pdfium::MakeUnique<CJBig2_HuffmanTable>(
146               HuffmanTable_B8, HuffmanTable_B8_Size, HuffmanTable_HTOOB_B8);
147           auto SBHUFFDT = pdfium::MakeUnique<CJBig2_HuffmanTable>(
148               HuffmanTable_B11, HuffmanTable_B11_Size, HuffmanTable_HTOOB_B11);
149           auto SBHUFFRDW = pdfium::MakeUnique<CJBig2_HuffmanTable>(
150               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
151           auto SBHUFFRDH = pdfium::MakeUnique<CJBig2_HuffmanTable>(
152               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
153           auto SBHUFFRDX = pdfium::MakeUnique<CJBig2_HuffmanTable>(
154               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
155           auto SBHUFFRDY = pdfium::MakeUnique<CJBig2_HuffmanTable>(
156               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
157           auto SBHUFFRSIZE = pdfium::MakeUnique<CJBig2_HuffmanTable>(
158               HuffmanTable_B1, HuffmanTable_B1_Size, HuffmanTable_HTOOB_B1);
159           pDecoder->SBHUFFFS = SBHUFFFS.get();
160           pDecoder->SBHUFFDS = SBHUFFDS.get();
161           pDecoder->SBHUFFDT = SBHUFFDT.get();
162           pDecoder->SBHUFFRDW = SBHUFFRDW.get();
163           pDecoder->SBHUFFRDH = SBHUFFRDH.get();
164           pDecoder->SBHUFFRDX = SBHUFFRDX.get();
165           pDecoder->SBHUFFRDY = SBHUFFRDY.get();
166           pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
167           pDecoder->SBRTEMPLATE = SDRTEMPLATE;
168           pDecoder->SBRAT[0] = SDRAT[0];
169           pDecoder->SBRAT[1] = SDRAT[1];
170           pDecoder->SBRAT[2] = SDRAT[2];
171           pDecoder->SBRAT[3] = SDRAT[3];
172           JBig2IntDecoderState ids;
173           ids.IADT = IADT.get();
174           ids.IAFS = IAFS.get();
175           ids.IADS = IADS.get();
176           ids.IAIT = IAIT.get();
177           ids.IARI = IARI.get();
178           ids.IARDW = IARDW.get();
179           ids.IARDH = IARDH.get();
180           ids.IARDX = IARDX.get();
181           ids.IARDY = IARDY.get();
182           ids.IAID = IAID.get();
183           BS = pDecoder->decode_Arith(pArithDecoder, grContext->data(), &ids);
184           if (!BS)
185             return nullptr;
186         } else if (REFAGGNINST == 1) {
187           SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
188           uint32_t IDI;
189           IAID->decode(pArithDecoder, &IDI);
190           IARDX->decode(pArithDecoder, &RDXI);
191           IARDY->decode(pArithDecoder, &RDYI);
192           if (IDI >= SBNUMSYMS)
193             return nullptr;
194 
195           SBSYMS.resize(SBNUMSYMS);
196           std::copy(SDINSYMS, SDINSYMS + SDNUMINSYMS, SBSYMS.begin());
197           for (size_t i = 0; i < NSYMSDECODED; ++i)
198             SBSYMS[i + SDNUMINSYMS] = SDNEWSYMS[i].get();
199           if (!SBSYMS[IDI])
200             return nullptr;
201 
202           auto pGRRD = pdfium::MakeUnique<CJBig2_GRRDProc>();
203           pGRRD->GRW = SYMWIDTH;
204           pGRRD->GRH = HCHEIGHT;
205           pGRRD->GRTEMPLATE = SDRTEMPLATE;
206           pGRRD->GRREFERENCE = SBSYMS[IDI];
207           pGRRD->GRREFERENCEDX = RDXI;
208           pGRRD->GRREFERENCEDY = RDYI;
209           pGRRD->TPGRON = 0;
210           pGRRD->GRAT[0] = SDRAT[0];
211           pGRRD->GRAT[1] = SDRAT[1];
212           pGRRD->GRAT[2] = SDRAT[2];
213           pGRRD->GRAT[3] = SDRAT[3];
214           BS = pGRRD->decode(pArithDecoder, grContext->data());
215           if (!BS)
216             return nullptr;
217         }
218       }
219       SDNEWSYMS[NSYMSDECODED] = std::move(BS);
220       NSYMSDECODED = NSYMSDECODED + 1;
221     }
222   }
223   EXINDEX = 0;
224   CUREXFLAG = 0;
225   EXFLAGS.resize(SDNUMINSYMS + SDNUMNEWSYMS);
226   num_ex_syms = 0;
227   while (EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
228     IAEX->decode(pArithDecoder, (int*)&EXRUNLENGTH);
229     if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS)
230       return nullptr;
231 
232     if (EXRUNLENGTH != 0) {
233       for (I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
234         if (CUREXFLAG)
235           num_ex_syms++;
236         EXFLAGS[I] = CUREXFLAG;
237       }
238     }
239     EXINDEX = EXINDEX + EXRUNLENGTH;
240     CUREXFLAG = !CUREXFLAG;
241   }
242   if (num_ex_syms > SDNUMEXSYMS)
243     return nullptr;
244 
245   pDict = pdfium::MakeUnique<CJBig2_SymbolDict>();
246   I = J = 0;
247   for (I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
248     if (!EXFLAGS[I] || J >= SDNUMEXSYMS)
249       continue;
250     if (I < SDNUMINSYMS) {
251       pDict->AddImage(SDINSYMS[I]
252                           ? pdfium::MakeUnique<CJBig2_Image>(*SDINSYMS[I])
253                           : nullptr);
254     } else {
255       pDict->AddImage(std::move(SDNEWSYMS[I - SDNUMINSYMS]));
256     }
257     ++J;
258   }
259   return pDict;
260 }
261 
decode_Huffman(CJBig2_BitStream * pStream,std::vector<JBig2ArithCtx> * gbContext,std::vector<JBig2ArithCtx> * grContext)262 std::unique_ptr<CJBig2_SymbolDict> CJBig2_SDDProc::decode_Huffman(
263     CJBig2_BitStream* pStream,
264     std::vector<JBig2ArithCtx>* gbContext,
265     std::vector<JBig2ArithCtx>* grContext) {
266   std::vector<std::unique_ptr<CJBig2_Image>> SDNEWSYMS;
267   std::vector<uint32_t> SDNEWSYMWIDTHS;
268   uint32_t HCHEIGHT, NSYMSDECODED;
269   int32_t HCDH;
270   uint32_t SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
271   int32_t DW;
272   uint32_t I, J, REFAGGNINST;
273   std::vector<bool> EXFLAGS;
274   uint32_t EXINDEX;
275   bool CUREXFLAG;
276   uint32_t EXRUNLENGTH;
277   int32_t nVal;
278   uint32_t nTmp;
279   uint32_t SBNUMSYMS;
280   uint8_t SBSYMCODELEN;
281   uint32_t IDI;
282   int32_t RDXI, RDYI;
283   uint32_t BMSIZE;
284   uint32_t stride;
285   uint32_t num_ex_syms;
286   // Pointers are not owned
287   std::vector<CJBig2_Image*> SBSYMS;
288   auto pHuffmanDecoder = pdfium::MakeUnique<CJBig2_HuffmanDecoder>(pStream);
289   SDNEWSYMS.resize(SDNUMNEWSYMS);
290   if (SDREFAGG == 0)
291     SDNEWSYMWIDTHS.resize(SDNUMNEWSYMS);
292   auto pDict = pdfium::MakeUnique<CJBig2_SymbolDict>();
293   std::unique_ptr<CJBig2_HuffmanTable> pTable;
294 
295   HCHEIGHT = 0;
296   NSYMSDECODED = 0;
297   std::unique_ptr<CJBig2_Image> BS;
298   while (NSYMSDECODED < SDNUMNEWSYMS) {
299     if (pHuffmanDecoder->decodeAValue(SDHUFFDH, &HCDH) != 0)
300       return nullptr;
301 
302     HCHEIGHT = HCHEIGHT + HCDH;
303     if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE)
304       return nullptr;
305 
306     SYMWIDTH = 0;
307     TOTWIDTH = 0;
308     HCFIRSTSYM = NSYMSDECODED;
309     for (;;) {
310       nVal = pHuffmanDecoder->decodeAValue(SDHUFFDW, &DW);
311       if (nVal == JBIG2_OOB)
312         break;
313       if (nVal != 0)
314         return nullptr;
315       if (NSYMSDECODED >= SDNUMNEWSYMS)
316         return nullptr;
317 
318       SYMWIDTH = SYMWIDTH + DW;
319       if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE)
320         return nullptr;
321       if (HCHEIGHT == 0 || SYMWIDTH == 0) {
322         TOTWIDTH = TOTWIDTH + SYMWIDTH;
323         SDNEWSYMS[NSYMSDECODED] = nullptr;
324         NSYMSDECODED = NSYMSDECODED + 1;
325         continue;
326       }
327       TOTWIDTH = TOTWIDTH + SYMWIDTH;
328       if (SDREFAGG == 1) {
329         if (pHuffmanDecoder->decodeAValue(SDHUFFAGGINST, (int*)&REFAGGNINST) !=
330             0) {
331           return nullptr;
332         }
333         BS = nullptr;
334         if (REFAGGNINST > 1) {
335           auto pDecoder = pdfium::MakeUnique<CJBig2_TRDProc>();
336           pDecoder->SBHUFF = SDHUFF;
337           pDecoder->SBREFINE = 1;
338           pDecoder->SBW = SYMWIDTH;
339           pDecoder->SBH = HCHEIGHT;
340           pDecoder->SBNUMINSTANCES = REFAGGNINST;
341           pDecoder->SBSTRIPS = 1;
342           pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
343           SBNUMSYMS = pDecoder->SBNUMSYMS;
344           std::vector<JBig2HuffmanCode> SBSYMCODES(SBNUMSYMS);
345           nTmp = 1;
346           while (static_cast<uint32_t>(1 << nTmp) < SBNUMSYMS)
347             ++nTmp;
348           for (I = 0; I < SBNUMSYMS; ++I) {
349             SBSYMCODES[I].codelen = nTmp;
350             SBSYMCODES[I].code = I;
351           }
352           pDecoder->SBSYMCODES = std::move(SBSYMCODES);
353           SBSYMS.resize(SBNUMSYMS);
354           std::copy(SDINSYMS, SDINSYMS + SDNUMINSYMS, SBSYMS.begin());
355           for (size_t i = 0; i < NSYMSDECODED; ++i)
356             SBSYMS[i + SDNUMINSYMS] = SDNEWSYMS[i].get();
357           pDecoder->SBSYMS = SBSYMS.data();
358           pDecoder->SBDEFPIXEL = 0;
359           pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
360           pDecoder->TRANSPOSED = 0;
361           pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
362           pDecoder->SBDSOFFSET = 0;
363           auto SBHUFFFS = pdfium::MakeUnique<CJBig2_HuffmanTable>(
364               HuffmanTable_B6, HuffmanTable_B6_Size, HuffmanTable_HTOOB_B6);
365           auto SBHUFFDS = pdfium::MakeUnique<CJBig2_HuffmanTable>(
366               HuffmanTable_B8, HuffmanTable_B8_Size, HuffmanTable_HTOOB_B8);
367           auto SBHUFFDT = pdfium::MakeUnique<CJBig2_HuffmanTable>(
368               HuffmanTable_B11, HuffmanTable_B11_Size, HuffmanTable_HTOOB_B11);
369           auto SBHUFFRDW = pdfium::MakeUnique<CJBig2_HuffmanTable>(
370               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
371           auto SBHUFFRDH = pdfium::MakeUnique<CJBig2_HuffmanTable>(
372               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
373           auto SBHUFFRDX = pdfium::MakeUnique<CJBig2_HuffmanTable>(
374               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
375           auto SBHUFFRDY = pdfium::MakeUnique<CJBig2_HuffmanTable>(
376               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
377           auto SBHUFFRSIZE = pdfium::MakeUnique<CJBig2_HuffmanTable>(
378               HuffmanTable_B1, HuffmanTable_B1_Size, HuffmanTable_HTOOB_B1);
379           pDecoder->SBHUFFFS = SBHUFFFS.get();
380           pDecoder->SBHUFFDS = SBHUFFDS.get();
381           pDecoder->SBHUFFDT = SBHUFFDT.get();
382           pDecoder->SBHUFFRDW = SBHUFFRDW.get();
383           pDecoder->SBHUFFRDH = SBHUFFRDH.get();
384           pDecoder->SBHUFFRDX = SBHUFFRDX.get();
385           pDecoder->SBHUFFRDY = SBHUFFRDY.get();
386           pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get();
387           pDecoder->SBRTEMPLATE = SDRTEMPLATE;
388           pDecoder->SBRAT[0] = SDRAT[0];
389           pDecoder->SBRAT[1] = SDRAT[1];
390           pDecoder->SBRAT[2] = SDRAT[2];
391           pDecoder->SBRAT[3] = SDRAT[3];
392           BS = pDecoder->decode_Huffman(pStream, grContext->data());
393           if (!BS)
394             return nullptr;
395 
396         } else if (REFAGGNINST == 1) {
397           SBNUMSYMS = SDNUMINSYMS + SDNUMNEWSYMS;
398           nTmp = 1;
399           while ((uint32_t)(1 << nTmp) < SBNUMSYMS) {
400             nTmp++;
401           }
402           SBSYMCODELEN = (uint8_t)nTmp;
403           uint32_t uVal = 0;
404           for (;;) {
405             if (pStream->read1Bit(&nTmp) != 0)
406               return nullptr;
407 
408             uVal = (uVal << 1) | nTmp;
409             if (uVal >= SBNUMSYMS)
410               return nullptr;
411 
412             IDI = SBSYMCODELEN == 0 ? uVal : SBNUMSYMS;
413             if (IDI < SBNUMSYMS)
414               break;
415           }
416           auto SBHUFFRDX = pdfium::MakeUnique<CJBig2_HuffmanTable>(
417               HuffmanTable_B15, HuffmanTable_B15_Size, HuffmanTable_HTOOB_B15);
418           auto SBHUFFRSIZE = pdfium::MakeUnique<CJBig2_HuffmanTable>(
419               HuffmanTable_B1, HuffmanTable_B1_Size, HuffmanTable_HTOOB_B1);
420           if ((pHuffmanDecoder->decodeAValue(SBHUFFRDX.get(), &RDXI) != 0) ||
421               (pHuffmanDecoder->decodeAValue(SBHUFFRDX.get(), &RDYI) != 0) ||
422               (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE.get(), &nVal) != 0)) {
423             return nullptr;
424           }
425           pStream->alignByte();
426           nTmp = pStream->getOffset();
427           SBSYMS.resize(SBNUMSYMS);
428           std::copy(SDINSYMS, SDINSYMS + SDNUMINSYMS, SBSYMS.begin());
429           for (size_t i = 0; i < NSYMSDECODED; ++i)
430             SBSYMS[i + SDNUMINSYMS] = SDNEWSYMS[i].get();
431           auto pGRRD = pdfium::MakeUnique<CJBig2_GRRDProc>();
432           pGRRD->GRW = SYMWIDTH;
433           pGRRD->GRH = HCHEIGHT;
434           pGRRD->GRTEMPLATE = SDRTEMPLATE;
435           pGRRD->GRREFERENCE = SBSYMS[IDI];
436           pGRRD->GRREFERENCEDX = RDXI;
437           pGRRD->GRREFERENCEDY = RDYI;
438           pGRRD->TPGRON = 0;
439           pGRRD->GRAT[0] = SDRAT[0];
440           pGRRD->GRAT[1] = SDRAT[1];
441           pGRRD->GRAT[2] = SDRAT[2];
442           pGRRD->GRAT[3] = SDRAT[3];
443           auto pArithDecoder = pdfium::MakeUnique<CJBig2_ArithDecoder>(pStream);
444           BS = pGRRD->decode(pArithDecoder.get(), grContext->data());
445           if (!BS)
446             return nullptr;
447 
448           pStream->alignByte();
449           pStream->offset(2);
450           if ((uint32_t)nVal != (pStream->getOffset() - nTmp))
451             return nullptr;
452         }
453         SDNEWSYMS[NSYMSDECODED] = std::move(BS);
454       }
455       if (SDREFAGG == 0)
456         SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
457       NSYMSDECODED = NSYMSDECODED + 1;
458     }
459     if (SDREFAGG == 0) {
460       if (pHuffmanDecoder->decodeAValue(SDHUFFBMSIZE, (int32_t*)&BMSIZE) != 0)
461         return nullptr;
462 
463       pStream->alignByte();
464       std::unique_ptr<CJBig2_Image> BHC;
465       if (BMSIZE == 0) {
466         stride = (TOTWIDTH + 7) >> 3;
467         if (pStream->getByteLeft() >= stride * HCHEIGHT) {
468           BHC = pdfium::MakeUnique<CJBig2_Image>(TOTWIDTH, HCHEIGHT);
469           for (I = 0; I < HCHEIGHT; I++) {
470             JBIG2_memcpy(BHC->data() + I * BHC->stride(), pStream->getPointer(),
471                          stride);
472             pStream->offset(stride);
473           }
474         } else {
475           return nullptr;
476         }
477       } else {
478         auto pGRD = pdfium::MakeUnique<CJBig2_GRDProc>();
479         pGRD->MMR = 1;
480         pGRD->GBW = TOTWIDTH;
481         pGRD->GBH = HCHEIGHT;
482         pGRD->Start_decode_MMR(&BHC, pStream);
483         pStream->alignByte();
484       }
485       nTmp = 0;
486       if (!BHC)
487         continue;
488 
489       for (I = HCFIRSTSYM; I < NSYMSDECODED; ++I) {
490         SDNEWSYMS[I] = BHC->subImage(nTmp, 0, SDNEWSYMWIDTHS[I], HCHEIGHT);
491         nTmp += SDNEWSYMWIDTHS[I];
492       }
493     }
494   }
495   EXINDEX = 0;
496   CUREXFLAG = 0;
497   pTable = pdfium::MakeUnique<CJBig2_HuffmanTable>(
498       HuffmanTable_B1, HuffmanTable_B1_Size, HuffmanTable_HTOOB_B1);
499   EXFLAGS.resize(SDNUMINSYMS + SDNUMNEWSYMS);
500   num_ex_syms = 0;
501   while (EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
502     if (pHuffmanDecoder->decodeAValue(pTable.get(), (int*)&EXRUNLENGTH) != 0)
503       return nullptr;
504 
505     if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS)
506       return nullptr;
507 
508     if (EXRUNLENGTH != 0) {
509       for (I = EXINDEX; I < EXINDEX + EXRUNLENGTH; ++I) {
510         if (CUREXFLAG)
511           num_ex_syms++;
512 
513         EXFLAGS[I] = CUREXFLAG;
514       }
515     }
516     EXINDEX = EXINDEX + EXRUNLENGTH;
517     CUREXFLAG = !CUREXFLAG;
518   }
519   if (num_ex_syms > SDNUMEXSYMS)
520     return nullptr;
521 
522   I = J = 0;
523   for (I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; ++I) {
524     if (!EXFLAGS[I] || J >= SDNUMEXSYMS)
525       continue;
526     if (I < SDNUMINSYMS) {
527       pDict->AddImage(SDINSYMS[I]
528                           ? pdfium::MakeUnique<CJBig2_Image>(*SDINSYMS[I])
529                           : nullptr);
530     } else {
531       pDict->AddImage(std::move(SDNEWSYMS[I - SDNUMINSYMS]));
532     }
533     ++J;
534   }
535   return pDict;
536 }
537