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

Side by Side Diff: core/fxge/ge/fx_ge_fontmap.cpp

Issue 1829013002: Added bounds checking to GetNameFromTT to handle corrupt files. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Fixed nits. Created 4 years, 9 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/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp ('k') | core/include/fxge/fx_font.h » ('j') | 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 <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 } 674 }
675 void CFX_FontMapper::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { 675 void CFX_FontMapper::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) {
676 if (!pFontInfo) { 676 if (!pFontInfo) {
677 return; 677 return;
678 } 678 }
679 if (m_pFontInfo) { 679 if (m_pFontInfo) {
680 m_pFontInfo->Release(); 680 m_pFontInfo->Release();
681 } 681 }
682 m_pFontInfo = pFontInfo; 682 m_pFontInfo = pFontInfo;
683 } 683 }
684 static CFX_ByteString GetStringFromTable(const uint8_t* string_ptr,
685 FX_DWORD string_ptr_length,
686 uint16_t offset,
687 uint16_t length) {
688 if (string_ptr_length < offset + length) {
689 return CFX_ByteString();
690 }
691 return CFX_ByteStringC(string_ptr + offset, length);
692 }
693 CFX_ByteString GetNameFromTT(const uint8_t* name_table,
694 FX_DWORD name_table_size,
695 FX_DWORD name_id) {
696 if (!name_table || name_table_size < 6) {
697 return CFX_ByteString();
698 }
699 FX_DWORD name_count = GET_TT_SHORT(name_table + 2);
700 FX_DWORD string_offset = GET_TT_SHORT(name_table + 4);
701 // We will ignore the possibility of overlap of structures and
702 // string table as if it's all corrupt there's not a lot we can do.
703 if (name_table_size < string_offset) {
704 return CFX_ByteString();
705 }
684 706
685 CFX_ByteString GetNameFromTT(const uint8_t* name_table, FX_DWORD name_id) {
686 const uint8_t* ptr = name_table + 2;
687 int name_count = GET_TT_SHORT(ptr);
688 int string_offset = GET_TT_SHORT(ptr + 2);
689 const uint8_t* string_ptr = name_table + string_offset; 707 const uint8_t* string_ptr = name_table + string_offset;
690 ptr += 4; 708 FX_DWORD string_ptr_size = name_table_size - string_offset;
691 for (int i = 0; i < name_count; i++) { 709 name_table += 6;
692 if (GET_TT_SHORT(ptr + 6) == name_id && GET_TT_SHORT(ptr) == 1 && 710 name_table_size -= 6;
693 GET_TT_SHORT(ptr + 2) == 0) { 711 if (name_table_size < name_count * 12) {
694 return CFX_ByteStringC(string_ptr + GET_TT_SHORT(ptr + 10), 712 return CFX_ByteString();
695 GET_TT_SHORT(ptr + 8)); 713 }
714
715 for (FX_DWORD i = 0; i < name_count; i++, name_table += 12) {
716 if (GET_TT_SHORT(name_table + 6) == name_id &&
717 GET_TT_SHORT(name_table) == 1 && GET_TT_SHORT(name_table + 2) == 0) {
718 return GetStringFromTable(string_ptr, string_ptr_size,
719 GET_TT_SHORT(name_table + 10),
720 GET_TT_SHORT(name_table + 8));
696 } 721 }
697 ptr += 12;
698 } 722 }
699 return CFX_ByteString(); 723 return CFX_ByteString();
700 } 724 }
701 725
702 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) { 726 CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) {
703 if (!m_pFontInfo) 727 if (!m_pFontInfo)
704 return CFX_ByteString(); 728 return CFX_ByteString();
705 729
706 FX_DWORD size = m_pFontInfo->GetFontData(hFont, kTableNAME, nullptr, 0); 730 FX_DWORD size = m_pFontInfo->GetFontData(hFont, kTableNAME, nullptr, 0);
707 if (!size) 731 if (!size)
708 return CFX_ByteString(); 732 return CFX_ByteString();
709 733
710 std::vector<uint8_t> buffer(size); 734 std::vector<uint8_t> buffer(size);
711 uint8_t* buffer_ptr = buffer.data(); 735 uint8_t* buffer_ptr = buffer.data();
712 FX_DWORD bytes_read = 736 FX_DWORD bytes_read =
713 m_pFontInfo->GetFontData(hFont, kTableNAME, buffer_ptr, size); 737 m_pFontInfo->GetFontData(hFont, kTableNAME, buffer_ptr, size);
714 return (bytes_read == size) ? GetNameFromTT(buffer_ptr, 6) : CFX_ByteString(); 738 return bytes_read == size ? GetNameFromTT(buffer_ptr, bytes_read, 6)
739 : CFX_ByteString();
715 } 740 }
716 741
717 void CFX_FontMapper::AddInstalledFont(const CFX_ByteString& name, int charset) { 742 void CFX_FontMapper::AddInstalledFont(const CFX_ByteString& name, int charset) {
718 if (!m_pFontInfo) { 743 if (!m_pFontInfo) {
719 return; 744 return;
720 } 745 }
721 if (m_CharsetArray.Find((FX_DWORD)charset) == -1) { 746 if (m_CharsetArray.Find((FX_DWORD)charset) == -1) {
722 m_CharsetArray.Add((FX_DWORD)charset); 747 m_CharsetArray.Add((FX_DWORD)charset);
723 m_FaceArray.push_back(name); 748 m_FaceArray.push_back(name);
724 } 749 }
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
1435 FX_DWORD nTables = GET_TT_SHORT(buffer + 4); 1460 FX_DWORD nTables = GET_TT_SHORT(buffer + 4);
1436 CFX_ByteString tables = FPDF_ReadStringFromFile(pFile, nTables * 16); 1461 CFX_ByteString tables = FPDF_ReadStringFromFile(pFile, nTables * 16);
1437 if (tables.IsEmpty()) { 1462 if (tables.IsEmpty()) {
1438 return; 1463 return;
1439 } 1464 }
1440 CFX_ByteString names = 1465 CFX_ByteString names =
1441 FPDF_LoadTableFromTT(pFile, tables, nTables, 0x6e616d65); 1466 FPDF_LoadTableFromTT(pFile, tables, nTables, 0x6e616d65);
1442 if (names.IsEmpty()) { 1467 if (names.IsEmpty()) {
1443 return; 1468 return;
1444 } 1469 }
1445 CFX_ByteString facename = GetNameFromTT(names, 1); 1470 CFX_ByteString facename = GetNameFromTT(names, names.GetLength(), 1);
1446 CFX_ByteString style = GetNameFromTT(names, 2); 1471 if (facename.IsEmpty()) {
1472 return;
1473 }
1474 CFX_ByteString style = GetNameFromTT(names, names.GetLength(), 2);
1447 if (style != "Regular") { 1475 if (style != "Regular") {
1448 facename += " " + style; 1476 facename += " " + style;
1449 } 1477 }
1450 if (pdfium::ContainsKey(m_FontList, facename)) 1478 if (pdfium::ContainsKey(m_FontList, facename))
1451 return; 1479 return;
1452 1480
1453 CFX_FontFaceInfo* pInfo = 1481 CFX_FontFaceInfo* pInfo =
1454 new CFX_FontFaceInfo(path, facename, tables, offset, filesize); 1482 new CFX_FontFaceInfo(path, facename, tables, offset, filesize);
1455 CFX_ByteString os2 = FPDF_LoadTableFromTT(pFile, tables, nTables, 0x4f532f32); 1483 CFX_ByteString os2 = FPDF_LoadTableFromTT(pFile, tables, nTables, 0x4f532f32);
1456 if (os2.GetLength() >= 86) { 1484 if (os2.GetLength() >= 86) {
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
1614 int PDF_GetStandardFontName(CFX_ByteString* name) { 1642 int PDF_GetStandardFontName(CFX_ByteString* name) {
1615 AltFontName* found = static_cast<AltFontName*>( 1643 AltFontName* found = static_cast<AltFontName*>(
1616 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames), 1644 FXSYS_bsearch(name->c_str(), g_AltFontNames, FX_ArraySize(g_AltFontNames),
1617 sizeof(AltFontName), CompareString)); 1645 sizeof(AltFontName), CompareString));
1618 if (!found) 1646 if (!found)
1619 return -1; 1647 return -1;
1620 1648
1621 *name = g_Base14FontNames[found->m_Index]; 1649 *name = g_Base14FontNames[found->m_Index];
1622 return found->m_Index; 1650 return found->m_Index;
1623 } 1651 }
OLDNEW
« no previous file with comments | « core/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp ('k') | core/include/fxge/fx_font.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698