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_HuffmanTable.h" | 7 #include "JBig2_HuffmanTable.h" |
| 8 |
| 9 #include <string.h> |
| 10 |
| 11 #include "../../../include/fxcrt/fx_memory.h" |
8 #include "JBig2_BitStream.h" | 12 #include "JBig2_BitStream.h" |
9 #include <string.h> | |
10 | 13 |
11 CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable, | 14 CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable, |
12 int nLines, | 15 int nLines, |
13 FX_BOOL bHTOOB) { | 16 FX_BOOL bHTOOB) { |
14 init(); | 17 init(); |
15 m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB); | 18 m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB); |
16 } | 19 } |
17 | 20 |
18 CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream) { | 21 CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream) { |
19 init(); | 22 init(); |
20 m_bOK = parseFromCodedBuffer(pStream); | 23 m_bOK = parseFromCodedBuffer(pStream); |
21 } | 24 } |
22 | 25 |
23 CJBig2_HuffmanTable::~CJBig2_HuffmanTable() { | 26 CJBig2_HuffmanTable::~CJBig2_HuffmanTable() { |
24 if (CODES) { | 27 FX_Free(CODES); |
25 m_pModule->JBig2_Free(CODES); | 28 FX_Free(PREFLEN); |
26 } | 29 FX_Free(RANGELEN); |
27 if (PREFLEN) { | 30 FX_Free(RANGELOW); |
28 m_pModule->JBig2_Free(PREFLEN); | |
29 } | |
30 if (RANGELEN) { | |
31 m_pModule->JBig2_Free(RANGELEN); | |
32 } | |
33 if (RANGELOW) { | |
34 m_pModule->JBig2_Free(RANGELOW); | |
35 } | |
36 } | 31 } |
37 void CJBig2_HuffmanTable::init() { | 32 void CJBig2_HuffmanTable::init() { |
38 HTOOB = FALSE; | 33 HTOOB = FALSE; |
39 NTEMP = 0; | 34 NTEMP = 0; |
40 CODES = NULL; | 35 CODES = NULL; |
41 PREFLEN = NULL; | 36 PREFLEN = NULL; |
42 RANGELEN = NULL; | 37 RANGELEN = NULL; |
43 RANGELOW = NULL; | 38 RANGELOW = NULL; |
44 } | 39 } |
45 int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, | 40 int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, |
46 int nLines, | 41 int nLines, |
47 FX_BOOL bHTOOB) { | 42 FX_BOOL bHTOOB) { |
48 int CURLEN, LENMAX, CURCODE, CURTEMP, i; | 43 int CURLEN, LENMAX, CURCODE, CURTEMP, i; |
49 int* LENCOUNT; | 44 int* LENCOUNT; |
50 int* FIRSTCODE; | 45 int* FIRSTCODE; |
51 HTOOB = bHTOOB; | 46 HTOOB = bHTOOB; |
52 NTEMP = nLines; | 47 NTEMP = nLines; |
53 CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP); | 48 CODES = (int*)FX_AllocOrDie(sizeof(int), NTEMP); |
54 PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP); | 49 PREFLEN = (int*)FX_AllocOrDie(sizeof(int), NTEMP); |
55 RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP); | 50 RANGELEN = (int*)FX_AllocOrDie(sizeof(int), NTEMP); |
56 RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP); | 51 RANGELOW = (int*)FX_AllocOrDie(sizeof(int), NTEMP); |
57 LENMAX = 0; | 52 LENMAX = 0; |
58 for (i = 0; i < NTEMP; i++) { | 53 for (i = 0; i < NTEMP; i++) { |
59 PREFLEN[i] = pTable[i].PREFLEN; | 54 PREFLEN[i] = pTable[i].PREFLEN; |
60 RANGELEN[i] = pTable[i].RANDELEN; | 55 RANGELEN[i] = pTable[i].RANDELEN; |
61 RANGELOW[i] = pTable[i].RANGELOW; | 56 RANGELOW[i] = pTable[i].RANGELOW; |
62 if (PREFLEN[i] > LENMAX) { | 57 if (PREFLEN[i] > LENMAX) { |
63 LENMAX = PREFLEN[i]; | 58 LENMAX = PREFLEN[i]; |
64 } | 59 } |
65 } | 60 } |
66 LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1)); | 61 LENCOUNT = (int*)FX_AllocOrDie(sizeof(int), (LENMAX + 1)); |
67 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); | 62 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); |
68 FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1)); | 63 FIRSTCODE = (int*)FX_AllocOrDie(sizeof(int), (LENMAX + 1)); |
69 for (i = 0; i < NTEMP; i++) { | 64 for (i = 0; i < NTEMP; i++) { |
70 LENCOUNT[PREFLEN[i]]++; | 65 LENCOUNT[PREFLEN[i]]++; |
71 } | 66 } |
72 CURLEN = 1; | 67 CURLEN = 1; |
73 FIRSTCODE[0] = 0; | 68 FIRSTCODE[0] = 0; |
74 LENCOUNT[0] = 0; | 69 LENCOUNT[0] = 0; |
75 while (CURLEN <= LENMAX) { | 70 while (CURLEN <= LENMAX) { |
76 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; | 71 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; |
77 CURCODE = FIRSTCODE[CURLEN]; | 72 CURCODE = FIRSTCODE[CURLEN]; |
78 CURTEMP = 0; | 73 CURTEMP = 0; |
79 while (CURTEMP < NTEMP) { | 74 while (CURTEMP < NTEMP) { |
80 if (PREFLEN[CURTEMP] == CURLEN) { | 75 if (PREFLEN[CURTEMP] == CURLEN) { |
81 CODES[CURTEMP] = CURCODE; | 76 CODES[CURTEMP] = CURCODE; |
82 CURCODE = CURCODE + 1; | 77 CURCODE = CURCODE + 1; |
83 } | 78 } |
84 CURTEMP = CURTEMP + 1; | 79 CURTEMP = CURTEMP + 1; |
85 } | 80 } |
86 CURLEN = CURLEN + 1; | 81 CURLEN = CURLEN + 1; |
87 } | 82 } |
88 m_pModule->JBig2_Free(LENCOUNT); | 83 FX_Free(LENCOUNT); |
89 m_pModule->JBig2_Free(FIRSTCODE); | 84 FX_Free(FIRSTCODE); |
90 return 1; | 85 return 1; |
91 } | 86 } |
92 | 87 |
93 #define HT_CHECK_MEMORY_ADJUST \ | 88 #define HT_CHECK_MEMORY_ADJUST \ |
94 if (NTEMP >= nSize) { \ | 89 if (NTEMP >= nSize) { \ |
95 nSize += 16; \ | 90 nSize += 16; \ |
96 PREFLEN = (int*)m_pModule->JBig2_Realloc(PREFLEN, sizeof(int) * nSize); \ | 91 PREFLEN = (int*)FX_Realloc(uint8_t, PREFLEN, sizeof(int) * nSize); \ |
97 RANGELEN = (int*)m_pModule->JBig2_Realloc(RANGELEN, sizeof(int) * nSize); \ | 92 RANGELEN = (int*)FX_Realloc(uint8_t, RANGELEN, sizeof(int) * nSize); \ |
98 RANGELOW = (int*)m_pModule->JBig2_Realloc(RANGELOW, sizeof(int) * nSize); \ | 93 RANGELOW = (int*)FX_Realloc(uint8_t, RANGELOW, sizeof(int) * nSize); \ |
99 } | 94 } |
100 int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { | 95 int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { |
101 unsigned char HTPS, HTRS; | 96 unsigned char HTPS, HTRS; |
102 FX_DWORD HTLOW, HTHIGH; | 97 FX_DWORD HTLOW, HTHIGH; |
103 FX_DWORD CURRANGELOW; | 98 FX_DWORD CURRANGELOW; |
104 FX_DWORD nSize = 16; | 99 FX_DWORD nSize = 16; |
105 int CURLEN, LENMAX, CURCODE, CURTEMP; | 100 int CURLEN, LENMAX, CURCODE, CURTEMP; |
106 int* LENCOUNT; | 101 int* LENCOUNT; |
107 int* FIRSTCODE; | 102 int* FIRSTCODE; |
108 unsigned char cTemp; | 103 unsigned char cTemp; |
109 if (pStream->read1Byte(&cTemp) == -1) { | 104 if (pStream->read1Byte(&cTemp) == -1) { |
110 goto failed; | 105 goto failed; |
111 } | 106 } |
112 HTOOB = cTemp & 0x01; | 107 HTOOB = cTemp & 0x01; |
113 HTPS = ((cTemp >> 1) & 0x07) + 1; | 108 HTPS = ((cTemp >> 1) & 0x07) + 1; |
114 HTRS = ((cTemp >> 4) & 0x07) + 1; | 109 HTRS = ((cTemp >> 4) & 0x07) + 1; |
115 if (pStream->readInteger(&HTLOW) == -1 || | 110 if (pStream->readInteger(&HTLOW) == -1 || |
116 pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) { | 111 pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) { |
117 goto failed; | 112 goto failed; |
118 } | 113 } |
119 PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize); | 114 PREFLEN = (int*)FX_AllocOrDie(sizeof(int), nSize); |
120 RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize); | 115 RANGELEN = (int*)FX_AllocOrDie(sizeof(int), nSize); |
121 RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize); | 116 RANGELOW = (int*)FX_AllocOrDie(sizeof(int), nSize); |
122 CURRANGELOW = HTLOW; | 117 CURRANGELOW = HTLOW; |
123 NTEMP = 0; | 118 NTEMP = 0; |
124 do { | 119 do { |
125 HT_CHECK_MEMORY_ADJUST | 120 HT_CHECK_MEMORY_ADJUST |
126 if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) || | 121 if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) || |
127 (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) { | 122 (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) { |
128 goto failed; | 123 goto failed; |
129 } | 124 } |
130 RANGELOW[NTEMP] = CURRANGELOW; | 125 RANGELOW[NTEMP] = CURRANGELOW; |
131 CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]); | 126 CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]); |
(...skipping 13 matching lines...) Expand all Loading... |
145 RANGELEN[NTEMP] = 32; | 140 RANGELEN[NTEMP] = 32; |
146 RANGELOW[NTEMP] = HTHIGH; | 141 RANGELOW[NTEMP] = HTHIGH; |
147 NTEMP = NTEMP + 1; | 142 NTEMP = NTEMP + 1; |
148 if (HTOOB) { | 143 if (HTOOB) { |
149 HT_CHECK_MEMORY_ADJUST | 144 HT_CHECK_MEMORY_ADJUST |
150 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { | 145 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { |
151 goto failed; | 146 goto failed; |
152 } | 147 } |
153 NTEMP = NTEMP + 1; | 148 NTEMP = NTEMP + 1; |
154 } | 149 } |
155 CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP); | 150 CODES = (int*)FX_AllocOrDie(sizeof(int), NTEMP); |
156 LENMAX = 0; | 151 LENMAX = 0; |
157 for (int i = 0; i < NTEMP; i++) { | 152 for (int i = 0; i < NTEMP; i++) { |
158 if (PREFLEN[i] > LENMAX) { | 153 if (PREFLEN[i] > LENMAX) { |
159 LENMAX = PREFLEN[i]; | 154 LENMAX = PREFLEN[i]; |
160 } | 155 } |
161 } | 156 } |
162 LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1)); | 157 LENCOUNT = (int*)FX_AllocOrDie(sizeof(int), (LENMAX + 1)); |
163 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); | 158 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); |
164 FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1)); | 159 FIRSTCODE = (int*)FX_AllocOrDie(sizeof(int), (LENMAX + 1)); |
165 for (int i = 0; i < NTEMP; i++) { | 160 for (int i = 0; i < NTEMP; i++) { |
166 LENCOUNT[PREFLEN[i]]++; | 161 LENCOUNT[PREFLEN[i]]++; |
167 } | 162 } |
168 CURLEN = 1; | 163 CURLEN = 1; |
169 FIRSTCODE[0] = 0; | 164 FIRSTCODE[0] = 0; |
170 LENCOUNT[0] = 0; | 165 LENCOUNT[0] = 0; |
171 while (CURLEN <= LENMAX) { | 166 while (CURLEN <= LENMAX) { |
172 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; | 167 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; |
173 CURCODE = FIRSTCODE[CURLEN]; | 168 CURCODE = FIRSTCODE[CURLEN]; |
174 CURTEMP = 0; | 169 CURTEMP = 0; |
175 while (CURTEMP < NTEMP) { | 170 while (CURTEMP < NTEMP) { |
176 if (PREFLEN[CURTEMP] == CURLEN) { | 171 if (PREFLEN[CURTEMP] == CURLEN) { |
177 CODES[CURTEMP] = CURCODE; | 172 CODES[CURTEMP] = CURCODE; |
178 CURCODE = CURCODE + 1; | 173 CURCODE = CURCODE + 1; |
179 } | 174 } |
180 CURTEMP = CURTEMP + 1; | 175 CURTEMP = CURTEMP + 1; |
181 } | 176 } |
182 CURLEN = CURLEN + 1; | 177 CURLEN = CURLEN + 1; |
183 } | 178 } |
184 m_pModule->JBig2_Free(LENCOUNT); | 179 FX_Free(LENCOUNT); |
185 m_pModule->JBig2_Free(FIRSTCODE); | 180 FX_Free(FIRSTCODE); |
186 return TRUE; | 181 return TRUE; |
187 failed: | 182 failed: |
188 return FALSE; | 183 return FALSE; |
189 } | 184 } |
OLD | NEW |