OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 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/fpdfapi/fpdf_parser/cpdf_hint_tables.h" | 7 #include "core/fpdfapi/fpdf_parser/cpdf_hint_tables.h" |
8 | 8 |
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_data_avail.h" | 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_data_avail.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 int nStreamLen = ReadPrimaryHintStreamLength(); | 53 int nStreamLen = ReadPrimaryHintStreamLength(); |
54 if (nStreamOffset < 0 || nStreamLen < 1) | 54 if (nStreamOffset < 0 || nStreamLen < 1) |
55 return false; | 55 return false; |
56 | 56 |
57 const uint32_t kHeaderSize = 288; | 57 const uint32_t kHeaderSize = 288; |
58 if (hStream->BitsRemaining() < kHeaderSize) | 58 if (hStream->BitsRemaining() < kHeaderSize) |
59 return false; | 59 return false; |
60 | 60 |
61 // Item 1: The least number of objects in a page. | 61 // Item 1: The least number of objects in a page. |
62 uint32_t dwObjLeastNum = hStream->GetBits(32); | 62 uint32_t dwObjLeastNum = hStream->GetBits(32); |
| 63 if (!dwObjLeastNum) |
| 64 return FALSE; |
63 | 65 |
64 // Item 2: The location of the first page's page object. | 66 // Item 2: The location of the first page's page object. |
65 uint32_t dwFirstObjLoc = hStream->GetBits(32); | 67 uint32_t dwFirstObjLoc = hStream->GetBits(32); |
66 if (dwFirstObjLoc > static_cast<uint32_t>(nStreamOffset)) { | 68 if (dwFirstObjLoc > static_cast<uint32_t>(nStreamOffset)) { |
67 FX_SAFE_UINT32 safeLoc = pdfium::base::checked_cast<uint32_t>(nStreamLen); | 69 FX_SAFE_UINT32 safeLoc = pdfium::base::checked_cast<uint32_t>(nStreamLen); |
68 safeLoc += dwFirstObjLoc; | 70 safeLoc += dwFirstObjLoc; |
69 if (!safeLoc.IsValid()) | 71 if (!safeLoc.IsValid()) |
70 return false; | 72 return false; |
71 m_szFirstPageObjOffset = | 73 m_szFirstPageObjOffset = |
72 pdfium::base::checked_cast<FX_FILESIZE>(safeLoc.ValueOrDie()); | 74 pdfium::base::checked_cast<FX_FILESIZE>(safeLoc.ValueOrDie()); |
73 } else { | 75 } else { |
74 m_szFirstPageObjOffset = | 76 m_szFirstPageObjOffset = |
75 pdfium::base::checked_cast<FX_FILESIZE>(dwFirstObjLoc); | 77 pdfium::base::checked_cast<FX_FILESIZE>(dwFirstObjLoc); |
76 } | 78 } |
77 | 79 |
78 // Item 3: The number of bits needed to represent the difference | 80 // Item 3: The number of bits needed to represent the difference |
79 // between the greatest and least number of objects in a page. | 81 // between the greatest and least number of objects in a page. |
80 uint32_t dwDeltaObjectsBits = hStream->GetBits(16); | 82 uint32_t dwDeltaObjectsBits = hStream->GetBits(16); |
| 83 if (!dwDeltaObjectsBits) |
| 84 return FALSE; |
81 | 85 |
82 // Item 4: The least length of a page in bytes. | 86 // Item 4: The least length of a page in bytes. |
83 uint32_t dwPageLeastLen = hStream->GetBits(32); | 87 uint32_t dwPageLeastLen = hStream->GetBits(32); |
| 88 if (!dwPageLeastLen) |
| 89 return FALSE; |
84 | 90 |
85 // Item 5: The number of bits needed to represent the difference | 91 // Item 5: The number of bits needed to represent the difference |
86 // between the greatest and least length of a page, in bytes. | 92 // between the greatest and least length of a page, in bytes. |
87 uint32_t dwDeltaPageLenBits = hStream->GetBits(16); | 93 uint32_t dwDeltaPageLenBits = hStream->GetBits(16); |
| 94 if (!dwDeltaPageLenBits) |
| 95 return FALSE; |
88 | 96 |
89 // Skip Item 6, 7, 8, 9 total 96 bits. | 97 // Skip Item 6, 7, 8, 9 total 96 bits. |
90 hStream->SkipBits(96); | 98 hStream->SkipBits(96); |
91 | 99 |
92 // Item 10: The number of bits needed to represent the greatest | 100 // Item 10: The number of bits needed to represent the greatest |
93 // number of shared object references. | 101 // number of shared object references. |
94 uint32_t dwSharedObjBits = hStream->GetBits(16); | 102 uint32_t dwSharedObjBits = hStream->GetBits(16); |
95 | 103 |
96 // Item 11: The number of bits needed to represent the numerically | 104 // Item 11: The number of bits needed to represent the numerically |
97 // greatest shared object identifier used by the pages. | 105 // greatest shared object identifier used by the pages. |
98 uint32_t dwSharedIdBits = hStream->GetBits(16); | 106 uint32_t dwSharedIdBits = hStream->GetBits(16); |
| 107 if (!dwSharedObjBits) |
| 108 return FALSE; |
99 | 109 |
100 // Item 12: The number of bits needed to represent the numerator of | 110 // Item 12: The number of bits needed to represent the numerator of |
101 // the fractional position for each shared object reference. For each | 111 // the fractional position for each shared object reference. For each |
102 // shared object referenced from a page, there is an indication of | 112 // shared object referenced from a page, there is an indication of |
103 // where in the page's content stream the object is first referenced. | 113 // where in the page's content stream the object is first referenced. |
104 uint32_t dwSharedNumeratorBits = hStream->GetBits(16); | 114 uint32_t dwSharedNumeratorBits = hStream->GetBits(16); |
| 115 if (!dwSharedIdBits) |
| 116 return FALSE; |
105 | 117 |
106 // Item 13: Skip Item 13 which has 16 bits. | 118 // Item 13: Skip Item 13 which has 16 bits. |
107 hStream->SkipBits(16); | 119 hStream->SkipBits(16); |
108 | 120 |
109 // The maximum number of bits allowed to represent the greatest number of | 121 // The maximum number of bits allowed to represent the greatest number of |
110 // shared object references. 2^39 should be more than enough. | 122 // shared object references. 2^39 should be more than enough. |
111 constexpr uint32_t kMaxSharedObjBits = 39; | 123 constexpr uint32_t kMaxSharedObjBits = 39; |
112 if (dwSharedObjBits > kMaxSharedObjBits) | 124 if (dwSharedObjBits > kMaxSharedObjBits) |
113 return false; | 125 return false; |
114 | 126 |
115 const int nPages = GetNumberOfPages(); | 127 const int nPages = GetNumberOfPages(); |
116 if (nPages < 1) | 128 if (nPages < 1 || nPages >= FPDF_PAGE_MAX_NUM) |
117 return false; | 129 return false; |
118 | 130 |
119 const uint32_t dwPages = pdfium::base::checked_cast<uint32_t>(nPages); | 131 const uint32_t dwPages = pdfium::base::checked_cast<uint32_t>(nPages); |
120 FX_SAFE_UINT32 required_bits = dwDeltaObjectsBits; | 132 FX_SAFE_UINT32 required_bits = dwDeltaObjectsBits; |
121 required_bits *= dwPages; | 133 required_bits *= dwPages; |
122 if (!CanReadFromBitStream(hStream, required_bits)) | 134 if (!CanReadFromBitStream(hStream, required_bits)) |
123 return false; | 135 return false; |
124 | 136 |
125 for (int i = 0; i < nPages; ++i) { | 137 for (int i = 0; i < nPages; ++i) { |
126 FX_SAFE_UINT32 safeDeltaObj = hStream->GetBits(dwDeltaObjectsBits); | 138 FX_SAFE_UINT32 safeDeltaObj = hStream->GetBits(dwDeltaObjectsBits); |
127 safeDeltaObj += dwObjLeastNum; | 139 safeDeltaObj += dwObjLeastNum; |
128 if (!safeDeltaObj.IsValid()) | 140 if (!safeDeltaObj.IsValid()) |
129 return false; | 141 return false; |
130 m_dwDeltaNObjsArray.push_back(safeDeltaObj.ValueOrDie()); | 142 m_dwDeltaNObjsArray.push_back(safeDeltaObj.ValueOrDie()); |
131 } | 143 } |
132 hStream->ByteAlign(); | 144 hStream->ByteAlign(); |
133 | 145 |
134 required_bits = dwDeltaPageLenBits; | 146 required_bits = dwDeltaPageLenBits; |
135 required_bits *= dwPages; | 147 required_bits *= dwPages; |
136 if (!CanReadFromBitStream(hStream, required_bits)) | 148 if (!CanReadFromBitStream(hStream, required_bits)) |
137 return false; | 149 return false; |
138 | 150 |
139 CFX_ArrayTemplate<uint32_t> dwPageLenArray; | 151 std::vector<uint32_t> dwPageLenArray; |
140 for (int i = 0; i < nPages; ++i) { | 152 for (int i = 0; i < nPages; ++i) { |
141 FX_SAFE_UINT32 safePageLen = hStream->GetBits(dwDeltaPageLenBits); | 153 FX_SAFE_UINT32 safePageLen = hStream->GetBits(dwDeltaPageLenBits); |
142 safePageLen += dwPageLeastLen; | 154 safePageLen += dwPageLeastLen; |
143 if (!safePageLen.IsValid()) | 155 if (!safePageLen.IsValid()) |
144 return false; | 156 return false; |
145 dwPageLenArray.Add(safePageLen.ValueOrDie()); | 157 |
| 158 dwPageLenArray.push_back(safePageLen.ValueOrDie()); |
146 } | 159 } |
147 | 160 |
148 int nOffsetE = GetEndOfFirstPageOffset(); | 161 int nOffsetE = GetEndOfFirstPageOffset(); |
149 if (nOffsetE < 0) | 162 if (nOffsetE < 0) |
150 return false; | 163 return false; |
151 | 164 |
152 int nFirstPageNum = GetFirstPageNumber(); | 165 int nFirstPageNum = GetFirstPageNumber(); |
153 for (int i = 0; i < nPages; ++i) { | 166 for (int i = 0; i < nPages; ++i) { |
154 if (i == nFirstPageNum) { | 167 if (i == nFirstPageNum) { |
155 m_szPageOffsetArray.push_back(m_szFirstPageObjOffset); | 168 m_szPageOffsetArray.push_back(m_szFirstPageObjOffset); |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 } | 489 } |
477 | 490 |
478 int CPDF_HintTables::ReadPrimaryHintStream(int index) const { | 491 int CPDF_HintTables::ReadPrimaryHintStream(int index) const { |
479 CPDF_Array* pRange = m_pLinearizedDict->GetArrayBy("H"); | 492 CPDF_Array* pRange = m_pLinearizedDict->GetArrayBy("H"); |
480 if (!pRange) | 493 if (!pRange) |
481 return -1; | 494 return -1; |
482 | 495 |
483 CPDF_Object* pStreamLen = pRange->GetDirectObjectAt(index); | 496 CPDF_Object* pStreamLen = pRange->GetDirectObjectAt(index); |
484 return pStreamLen ? pStreamLen->GetInteger() : -1; | 497 return pStreamLen ? pStreamLen->GetInteger() : -1; |
485 } | 498 } |
OLD | NEW |