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 "core/fxcodec/jbig2/JBig2_Context.h" | 7 #include "core/fxcodec/jbig2/JBig2_Context.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <list> | 10 #include <list> |
(...skipping 1259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1270 } | 1270 } |
1271 return JBIG2_SUCCESS; | 1271 return JBIG2_SUCCESS; |
1272 } | 1272 } |
1273 | 1273 |
1274 JBig2HuffmanCode* CJBig2_Context::decodeSymbolIDHuffmanTable( | 1274 JBig2HuffmanCode* CJBig2_Context::decodeSymbolIDHuffmanTable( |
1275 CJBig2_BitStream* pStream, | 1275 CJBig2_BitStream* pStream, |
1276 uint32_t SBNUMSYMS) { | 1276 uint32_t SBNUMSYMS) { |
1277 const size_t kRunCodesSize = 35; | 1277 const size_t kRunCodesSize = 35; |
1278 int32_t runcodes[kRunCodesSize]; | 1278 int32_t runcodes[kRunCodesSize]; |
1279 int32_t runcodes_len[kRunCodesSize]; | 1279 int32_t runcodes_len[kRunCodesSize]; |
1280 for (int32_t i = 0; i < kRunCodesSize; ++i) { | 1280 for (size_t i = 0; i < kRunCodesSize; ++i) { |
1281 if (pStream->readNBits(4, &runcodes_len[i]) != 0) | 1281 if (pStream->readNBits(4, &runcodes_len[i]) != 0) |
1282 return nullptr; | 1282 return nullptr; |
1283 } | 1283 } |
1284 huffman_assign_code(runcodes, runcodes_len, kRunCodesSize); | 1284 huffman_assign_code(runcodes, runcodes_len, kRunCodesSize); |
1285 | 1285 |
1286 std::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES( | 1286 std::unique_ptr<JBig2HuffmanCode, FxFreeDeleter> SBSYMCODES( |
1287 FX_Alloc(JBig2HuffmanCode, SBNUMSYMS)); | 1287 FX_Alloc(JBig2HuffmanCode, SBNUMSYMS)); |
1288 int32_t run; | 1288 int32_t run; |
1289 int32_t i = 0; | 1289 int32_t i = 0; |
1290 while (i < (int)SBNUMSYMS) { | 1290 while (i < (int)SBNUMSYMS) { |
1291 int32_t j; | 1291 size_t j; |
1292 int32_t nVal = 0; | 1292 int32_t nVal = 0; |
1293 int32_t nBits = 0; | 1293 int32_t nBits = 0; |
1294 uint32_t nTemp; | 1294 uint32_t nTemp; |
1295 while (true) { | 1295 while (true) { |
1296 if (pStream->read1Bit(&nTemp) != 0) | 1296 if (pStream->read1Bit(&nTemp) != 0) |
1297 return nullptr; | 1297 return nullptr; |
1298 | 1298 |
1299 nVal = (nVal << 1) | nTemp; | 1299 nVal = (nVal << 1) | nTemp; |
1300 ++nBits; | 1300 ++nBits; |
1301 for (j = 0; j < kRunCodesSize; ++j) { | 1301 for (j = 0; j < kRunCodesSize; ++j) { |
1302 if (nBits == runcodes_len[j] && nVal == runcodes[j]) { | 1302 if (nBits == runcodes_len[j] && nVal == runcodes[j]) |
1303 break; | 1303 break; |
1304 } | |
1305 } | 1304 } |
1306 if (j < kRunCodesSize) { | 1305 if (j < kRunCodesSize) |
1307 break; | 1306 break; |
1308 } | |
1309 } | 1307 } |
1310 int32_t runcode = j; | 1308 int32_t runcode = static_cast<int32_t>(j); |
1311 if (runcode < 32) { | 1309 if (runcode < 32) { |
1312 SBSYMCODES.get()[i].codelen = runcode; | 1310 SBSYMCODES.get()[i].codelen = runcode; |
1313 run = 0; | 1311 run = 0; |
1314 } else if (runcode == 32) { | 1312 } else if (runcode == 32) { |
1315 if (pStream->readNBits(2, &nTemp) != 0) | 1313 if (pStream->readNBits(2, &nTemp) != 0) |
1316 return nullptr; | 1314 return nullptr; |
1317 run = nTemp + 3; | 1315 run = nTemp + 3; |
1318 } else if (runcode == 33) { | 1316 } else if (runcode == 33) { |
1319 if (pStream->readNBits(3, &nTemp) != 0) | 1317 if (pStream->readNBits(3, &nTemp) != 0) |
1320 return nullptr; | 1318 return nullptr; |
1321 run = nTemp + 3; | 1319 run = nTemp + 3; |
1322 } else if (runcode == 34) { | 1320 } else if (runcode == 34) { |
1323 if (pStream->readNBits(7, &nTemp) != 0) | 1321 if (pStream->readNBits(7, &nTemp) != 0) |
1324 return nullptr; | 1322 return nullptr; |
1325 run = nTemp + 11; | 1323 run = nTemp + 11; |
1326 } | 1324 } |
1327 if (run > 0) { | 1325 if (run > 0) { |
1328 if (i + run > (int)SBNUMSYMS) | 1326 if (i + run > (int)SBNUMSYMS) |
1329 return nullptr; | 1327 return nullptr; |
1330 for (j = 0; j < run; ++j) { | 1328 for (int32_t k = 0; k < run; ++k) { |
1331 if (runcode == 32 && i > 0) { | 1329 if (runcode == 32 && i > 0) { |
1332 SBSYMCODES.get()[i + j].codelen = SBSYMCODES.get()[i - 1].codelen; | 1330 SBSYMCODES.get()[i + k].codelen = SBSYMCODES.get()[i - 1].codelen; |
1333 } else { | 1331 } else { |
1334 SBSYMCODES.get()[i + j].codelen = 0; | 1332 SBSYMCODES.get()[i + k].codelen = 0; |
1335 } | 1333 } |
1336 } | 1334 } |
1337 i += run; | 1335 i += run; |
1338 } else { | 1336 } else { |
1339 ++i; | 1337 ++i; |
1340 } | 1338 } |
1341 } | 1339 } |
1342 huffman_assign_code(SBSYMCODES.get(), SBNUMSYMS); | 1340 huffman_assign_code(SBSYMCODES.get(), SBNUMSYMS); |
1343 return SBSYMCODES.release(); | 1341 return SBSYMCODES.release(); |
1344 } | 1342 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1408 SBSYMCODES[CURTEMP].code = CURCODE; | 1406 SBSYMCODES[CURTEMP].code = CURCODE; |
1409 CURCODE = CURCODE + 1; | 1407 CURCODE = CURCODE + 1; |
1410 } | 1408 } |
1411 CURTEMP = CURTEMP + 1; | 1409 CURTEMP = CURTEMP + 1; |
1412 } | 1410 } |
1413 CURLEN = CURLEN + 1; | 1411 CURLEN = CURLEN + 1; |
1414 } | 1412 } |
1415 FX_Free(LENCOUNT); | 1413 FX_Free(LENCOUNT); |
1416 FX_Free(FIRSTCODE); | 1414 FX_Free(FIRSTCODE); |
1417 } | 1415 } |
OLD | NEW |