Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(605)

Side by Side Diff: core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp

Issue 1382613003: Remove gotos in JBig2 code. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: address comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_GsidProc.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 8
9 #include <string.h> 9 #include <string.h>
10 10
11 #include <vector>
12
11 #include "../../../include/fxcrt/fx_memory.h" 13 #include "../../../include/fxcrt/fx_memory.h"
12 #include "JBig2_BitStream.h" 14 #include "JBig2_BitStream.h"
13 15
14 CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable, 16 CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable,
15 int nLines, 17 int nLines,
16 FX_BOOL bHTOOB) { 18 FX_BOOL bHTOOB) {
17 init(); 19 init();
18 m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB); 20 m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB);
19 } 21 }
20 22
(...skipping 12 matching lines...) Expand all
33 HTOOB = FALSE; 35 HTOOB = FALSE;
34 NTEMP = 0; 36 NTEMP = 0;
35 CODES = NULL; 37 CODES = NULL;
36 PREFLEN = NULL; 38 PREFLEN = NULL;
37 RANGELEN = NULL; 39 RANGELEN = NULL;
38 RANGELOW = NULL; 40 RANGELOW = NULL;
39 } 41 }
40 int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable, 42 int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine* pTable,
41 int nLines, 43 int nLines,
42 FX_BOOL bHTOOB) { 44 FX_BOOL bHTOOB) {
43 int CURLEN, LENMAX, CURCODE, CURTEMP, i; 45 int CURLEN, LENMAX, CURCODE, CURTEMP;
44 int* LENCOUNT; 46 int* LENCOUNT;
45 int* FIRSTCODE; 47 int* FIRSTCODE;
46 HTOOB = bHTOOB; 48 HTOOB = bHTOOB;
47 NTEMP = nLines; 49 NTEMP = nLines;
48 CODES = FX_Alloc(int, NTEMP); 50 CODES = FX_Alloc(int, NTEMP);
49 PREFLEN = FX_Alloc(int, NTEMP); 51 PREFLEN = FX_Alloc(int, NTEMP);
50 RANGELEN = FX_Alloc(int, NTEMP); 52 RANGELEN = FX_Alloc(int, NTEMP);
51 RANGELOW = FX_Alloc(int, NTEMP); 53 RANGELOW = FX_Alloc(int, NTEMP);
52 LENMAX = 0; 54 LENMAX = 0;
53 for (i = 0; i < NTEMP; i++) { 55 for (int i = 0; i < NTEMP; i++) {
54 PREFLEN[i] = pTable[i].PREFLEN; 56 PREFLEN[i] = pTable[i].PREFLEN;
55 RANGELEN[i] = pTable[i].RANDELEN; 57 RANGELEN[i] = pTable[i].RANDELEN;
56 RANGELOW[i] = pTable[i].RANGELOW; 58 RANGELOW[i] = pTable[i].RANGELOW;
57 if (PREFLEN[i] > LENMAX) { 59 if (PREFLEN[i] > LENMAX) {
58 LENMAX = PREFLEN[i]; 60 LENMAX = PREFLEN[i];
59 } 61 }
60 } 62 }
61 LENCOUNT = FX_Alloc(int, LENMAX + 1); 63 LENCOUNT = FX_Alloc(int, LENMAX + 1);
62 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); 64 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
63 FIRSTCODE = FX_Alloc(int, LENMAX + 1); 65 FIRSTCODE = FX_Alloc(int, LENMAX + 1);
64 for (i = 0; i < NTEMP; i++) { 66 for (int i = 0; i < NTEMP; i++) {
65 LENCOUNT[PREFLEN[i]]++; 67 LENCOUNT[PREFLEN[i]]++;
66 } 68 }
67 CURLEN = 1; 69 CURLEN = 1;
68 FIRSTCODE[0] = 0; 70 FIRSTCODE[0] = 0;
69 LENCOUNT[0] = 0; 71 LENCOUNT[0] = 0;
70 while (CURLEN <= LENMAX) { 72 while (CURLEN <= LENMAX) {
71 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; 73 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
72 CURCODE = FIRSTCODE[CURLEN]; 74 CURCODE = FIRSTCODE[CURLEN];
73 CURTEMP = 0; 75 CURTEMP = 0;
74 while (CURTEMP < NTEMP) { 76 while (CURTEMP < NTEMP) {
(...skipping 11 matching lines...) Expand all
86 } 88 }
87 89
88 #define HT_CHECK_MEMORY_ADJUST \ 90 #define HT_CHECK_MEMORY_ADJUST \
89 if (NTEMP >= nSize) { \ 91 if (NTEMP >= nSize) { \
90 nSize += 16; \ 92 nSize += 16; \
91 PREFLEN = FX_Realloc(int, PREFLEN, nSize); \ 93 PREFLEN = FX_Realloc(int, PREFLEN, nSize); \
92 RANGELEN = FX_Realloc(int, RANGELEN, nSize); \ 94 RANGELEN = FX_Realloc(int, RANGELEN, nSize); \
93 RANGELOW = FX_Realloc(int, RANGELOW, nSize); \ 95 RANGELOW = FX_Realloc(int, RANGELOW, nSize); \
94 } 96 }
95 int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { 97 int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) {
96 unsigned char HTPS, HTRS;
97 FX_DWORD HTLOW, HTHIGH;
98 FX_DWORD CURRANGELOW;
99 FX_DWORD nSize = 16;
100 int CURLEN, LENMAX, CURCODE, CURTEMP;
101 int* LENCOUNT;
102 int* FIRSTCODE;
103 unsigned char cTemp; 98 unsigned char cTemp;
104 if (pStream->read1Byte(&cTemp) == -1) { 99 if (pStream->read1Byte(&cTemp) == -1)
105 goto failed; 100 return FALSE;
106 } 101
107 HTOOB = cTemp & 0x01; 102 HTOOB = cTemp & 0x01;
108 HTPS = ((cTemp >> 1) & 0x07) + 1; 103 unsigned char HTPS = ((cTemp >> 1) & 0x07) + 1;
109 HTRS = ((cTemp >> 4) & 0x07) + 1; 104 unsigned char HTRS = ((cTemp >> 4) & 0x07) + 1;
105 FX_DWORD HTLOW;
106 FX_DWORD HTHIGH;
110 if (pStream->readInteger(&HTLOW) == -1 || 107 if (pStream->readInteger(&HTLOW) == -1 ||
111 pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) { 108 pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) {
112 goto failed; 109 return FALSE;
113 } 110 }
111 FX_DWORD nSize = 16;
114 PREFLEN = FX_Alloc(int, nSize); 112 PREFLEN = FX_Alloc(int, nSize);
115 RANGELEN = FX_Alloc(int, nSize); 113 RANGELEN = FX_Alloc(int, nSize);
116 RANGELOW = FX_Alloc(int, nSize); 114 RANGELOW = FX_Alloc(int, nSize);
117 CURRANGELOW = HTLOW; 115 FX_DWORD CURRANGELOW = HTLOW;
118 NTEMP = 0; 116 NTEMP = 0;
119 do { 117 do {
120 HT_CHECK_MEMORY_ADJUST 118 HT_CHECK_MEMORY_ADJUST
121 if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) || 119 if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) ||
122 (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) { 120 (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) {
123 goto failed; 121 return FALSE;
124 } 122 }
125 RANGELOW[NTEMP] = CURRANGELOW; 123 RANGELOW[NTEMP] = CURRANGELOW;
126 CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]); 124 CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]);
127 NTEMP = NTEMP + 1; 125 NTEMP = NTEMP + 1;
128 } while (CURRANGELOW < HTHIGH); 126 } while (CURRANGELOW < HTHIGH);
129 HT_CHECK_MEMORY_ADJUST 127 HT_CHECK_MEMORY_ADJUST
130 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { 128 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
131 goto failed; 129 return FALSE;
132 } 130
133 RANGELEN[NTEMP] = 32; 131 RANGELEN[NTEMP] = 32;
134 RANGELOW[NTEMP] = HTLOW - 1; 132 RANGELOW[NTEMP] = HTLOW - 1;
135 NTEMP = NTEMP + 1; 133 NTEMP = NTEMP + 1;
136 HT_CHECK_MEMORY_ADJUST 134 HT_CHECK_MEMORY_ADJUST
137 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { 135 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
138 goto failed; 136 return FALSE;
139 } 137
140 RANGELEN[NTEMP] = 32; 138 RANGELEN[NTEMP] = 32;
141 RANGELOW[NTEMP] = HTHIGH; 139 RANGELOW[NTEMP] = HTHIGH;
142 NTEMP = NTEMP + 1; 140 NTEMP = NTEMP + 1;
143 if (HTOOB) { 141 if (HTOOB) {
144 HT_CHECK_MEMORY_ADJUST 142 HT_CHECK_MEMORY_ADJUST
145 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { 143 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
146 goto failed; 144 return FALSE;
147 }
148 NTEMP = NTEMP + 1; 145 NTEMP = NTEMP + 1;
149 } 146 }
150 CODES = FX_Alloc(int, NTEMP); 147 CODES = FX_Alloc(int, NTEMP);
151 LENMAX = 0; 148 int LENMAX = 0;
152 for (int i = 0; i < NTEMP; i++) { 149 for (int i = 0; i < NTEMP; i++) {
153 if (PREFLEN[i] > LENMAX) { 150 if (PREFLEN[i] > LENMAX) {
154 LENMAX = PREFLEN[i]; 151 LENMAX = PREFLEN[i];
155 } 152 }
156 } 153 }
157 LENCOUNT = FX_Alloc(int, (LENMAX + 1)); 154
158 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); 155 std::vector<int> LENCOUNT(LENMAX + 1);
159 FIRSTCODE = FX_Alloc(int, (LENMAX + 1)); 156 for (int i = 0; i < NTEMP; ++i)
160 for (int i = 0; i < NTEMP; i++) {
161 LENCOUNT[PREFLEN[i]]++; 157 LENCOUNT[PREFLEN[i]]++;
158 LENCOUNT[0] = 0;
159
160 std::vector<int> FIRSTCODE(LENMAX + 1);
161 FIRSTCODE[0] = 0;
162 for (int i = 0; i <= LENMAX; ++i) {
163 FIRSTCODE[i] = (FIRSTCODE[i - 1] + LENCOUNT[i - 1]) << 1;
164 int CURCODE = FIRSTCODE[i];
165 for (int j = 0; j < NTEMP; ++j) {
166 if (PREFLEN[j] == i)
167 CODES[j] = CURCODE++;
168 }
162 } 169 }
163 CURLEN = 1;
164 FIRSTCODE[0] = 0;
165 LENCOUNT[0] = 0;
166 while (CURLEN <= LENMAX) {
167 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
168 CURCODE = FIRSTCODE[CURLEN];
169 CURTEMP = 0;
170 while (CURTEMP < NTEMP) {
171 if (PREFLEN[CURTEMP] == CURLEN) {
172 CODES[CURTEMP] = CURCODE;
173 CURCODE = CURCODE + 1;
174 }
175 CURTEMP = CURTEMP + 1;
176 }
177 CURLEN = CURLEN + 1;
178 }
179 FX_Free(LENCOUNT);
180 FX_Free(FIRSTCODE);
181 return TRUE; 170 return TRUE;
182 failed:
183 return FALSE;
184 } 171 }
OLDNEW
« no previous file with comments | « core/src/fxcodec/jbig2/JBig2_GsidProc.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698