Chromium Code Reviews| 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 | 8 |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 } | 86 } |
| 87 | 87 |
| 88 #define HT_CHECK_MEMORY_ADJUST \ | 88 #define HT_CHECK_MEMORY_ADJUST \ |
| 89 if (NTEMP >= nSize) { \ | 89 if (NTEMP >= nSize) { \ |
| 90 nSize += 16; \ | 90 nSize += 16; \ |
| 91 PREFLEN = FX_Realloc(int, PREFLEN, nSize); \ | 91 PREFLEN = FX_Realloc(int, PREFLEN, nSize); \ |
| 92 RANGELEN = FX_Realloc(int, RANGELEN, nSize); \ | 92 RANGELEN = FX_Realloc(int, RANGELEN, nSize); \ |
| 93 RANGELOW = FX_Realloc(int, RANGELOW, nSize); \ | 93 RANGELOW = FX_Realloc(int, RANGELOW, nSize); \ |
| 94 } | 94 } |
| 95 int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream* pStream) { | 95 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; | 96 unsigned char cTemp; |
| 104 if (pStream->read1Byte(&cTemp) == -1) { | 97 if (pStream->read1Byte(&cTemp) == -1) |
| 105 goto failed; | 98 return FALSE; |
| 106 } | 99 |
| 107 HTOOB = cTemp & 0x01; | 100 HTOOB = cTemp & 0x01; |
| 108 HTPS = ((cTemp >> 1) & 0x07) + 1; | 101 unsigned char HTPS = ((cTemp >> 1) & 0x07) + 1; |
| 109 HTRS = ((cTemp >> 4) & 0x07) + 1; | 102 unsigned char HTRS = ((cTemp >> 4) & 0x07) + 1; |
| 103 FX_DWORD HTLOW; | |
| 104 FX_DWORD HTHIGH; | |
| 110 if (pStream->readInteger(&HTLOW) == -1 || | 105 if (pStream->readInteger(&HTLOW) == -1 || |
| 111 pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) { | 106 pStream->readInteger(&HTHIGH) == -1 || HTLOW > HTHIGH) { |
| 112 goto failed; | 107 return FALSE; |
| 113 } | 108 } |
| 109 FX_DWORD nSize = 16; | |
| 114 PREFLEN = FX_Alloc(int, nSize); | 110 PREFLEN = FX_Alloc(int, nSize); |
| 115 RANGELEN = FX_Alloc(int, nSize); | 111 RANGELEN = FX_Alloc(int, nSize); |
| 116 RANGELOW = FX_Alloc(int, nSize); | 112 RANGELOW = FX_Alloc(int, nSize); |
| 117 CURRANGELOW = HTLOW; | 113 FX_DWORD CURRANGELOW = HTLOW; |
| 118 NTEMP = 0; | 114 NTEMP = 0; |
| 119 do { | 115 do { |
| 120 HT_CHECK_MEMORY_ADJUST | 116 HT_CHECK_MEMORY_ADJUST |
| 121 if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) || | 117 if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) || |
| 122 (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) { | 118 (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) { |
| 123 goto failed; | 119 return FALSE; |
| 124 } | 120 } |
| 125 RANGELOW[NTEMP] = CURRANGELOW; | 121 RANGELOW[NTEMP] = CURRANGELOW; |
| 126 CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]); | 122 CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]); |
| 127 NTEMP = NTEMP + 1; | 123 NTEMP = NTEMP + 1; |
| 128 } while (CURRANGELOW < HTHIGH); | 124 } while (CURRANGELOW < HTHIGH); |
| 129 HT_CHECK_MEMORY_ADJUST | 125 HT_CHECK_MEMORY_ADJUST |
| 130 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { | 126 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) |
| 131 goto failed; | 127 return FALSE; |
| 132 } | 128 |
| 133 RANGELEN[NTEMP] = 32; | 129 RANGELEN[NTEMP] = 32; |
| 134 RANGELOW[NTEMP] = HTLOW - 1; | 130 RANGELOW[NTEMP] = HTLOW - 1; |
| 135 NTEMP = NTEMP + 1; | 131 NTEMP = NTEMP + 1; |
| 136 HT_CHECK_MEMORY_ADJUST | 132 HT_CHECK_MEMORY_ADJUST |
| 137 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { | 133 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) |
| 138 goto failed; | 134 return FALSE; |
| 139 } | 135 |
| 140 RANGELEN[NTEMP] = 32; | 136 RANGELEN[NTEMP] = 32; |
| 141 RANGELOW[NTEMP] = HTHIGH; | 137 RANGELOW[NTEMP] = HTHIGH; |
| 142 NTEMP = NTEMP + 1; | 138 NTEMP = NTEMP + 1; |
| 143 if (HTOOB) { | 139 if (HTOOB) { |
| 144 HT_CHECK_MEMORY_ADJUST | 140 HT_CHECK_MEMORY_ADJUST |
| 145 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) { | 141 if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) |
| 146 goto failed; | 142 return FALSE; |
| 147 } | |
| 148 NTEMP = NTEMP + 1; | 143 NTEMP = NTEMP + 1; |
| 149 } | 144 } |
| 150 CODES = FX_Alloc(int, NTEMP); | 145 CODES = FX_Alloc(int, NTEMP); |
| 151 LENMAX = 0; | 146 int LENMAX = 0; |
| 152 for (int i = 0; i < NTEMP; i++) { | 147 for (int i = 0; i < NTEMP; i++) { |
| 153 if (PREFLEN[i] > LENMAX) { | 148 if (PREFLEN[i] > LENMAX) { |
| 154 LENMAX = PREFLEN[i]; | 149 LENMAX = PREFLEN[i]; |
| 155 } | 150 } |
| 156 } | 151 } |
| 157 LENCOUNT = FX_Alloc(int, (LENMAX + 1)); | 152 int* LENCOUNT = FX_Alloc(int, (LENMAX + 1)); |
|
Tom Sepez
2015/10/05 16:34:32
maybe nonstd::unique_ptr here and at 154 to lose t
Lei Zhang
2015/10/07 07:51:12
Made them std::vectors.
| |
| 158 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); | 153 JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1)); |
| 159 FIRSTCODE = FX_Alloc(int, (LENMAX + 1)); | 154 int* FIRSTCODE = FX_Alloc(int, (LENMAX + 1)); |
| 160 for (int i = 0; i < NTEMP; i++) { | 155 for (int i = 0; i < NTEMP; i++) { |
| 161 LENCOUNT[PREFLEN[i]]++; | 156 LENCOUNT[PREFLEN[i]]++; |
| 162 } | 157 } |
| 163 CURLEN = 1; | 158 int CURLEN = 1; |
| 164 FIRSTCODE[0] = 0; | 159 FIRSTCODE[0] = 0; |
| 165 LENCOUNT[0] = 0; | 160 LENCOUNT[0] = 0; |
| 166 while (CURLEN <= LENMAX) { | 161 while (CURLEN <= LENMAX) { |
| 167 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; | 162 FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1; |
| 168 CURCODE = FIRSTCODE[CURLEN]; | 163 int CURCODE = FIRSTCODE[CURLEN]; |
| 169 CURTEMP = 0; | 164 int CURTEMP = 0; |
| 170 while (CURTEMP < NTEMP) { | 165 while (CURTEMP < NTEMP) { |
| 171 if (PREFLEN[CURTEMP] == CURLEN) { | 166 if (PREFLEN[CURTEMP] == CURLEN) { |
| 172 CODES[CURTEMP] = CURCODE; | 167 CODES[CURTEMP] = CURCODE; |
| 173 CURCODE = CURCODE + 1; | 168 CURCODE = CURCODE + 1; |
| 174 } | 169 } |
| 175 CURTEMP = CURTEMP + 1; | 170 CURTEMP = CURTEMP + 1; |
| 176 } | 171 } |
| 177 CURLEN = CURLEN + 1; | 172 CURLEN = CURLEN + 1; |
| 178 } | 173 } |
| 179 FX_Free(LENCOUNT); | 174 FX_Free(LENCOUNT); |
| 180 FX_Free(FIRSTCODE); | 175 FX_Free(FIRSTCODE); |
| 181 return TRUE; | 176 return TRUE; |
| 182 failed: | |
| 183 return FALSE; | |
| 184 } | 177 } |
| OLD | NEW |