Chromium Code Reviews| 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/edit/cpdf_pagecontentgenerator.h" | 7 #include "core/fpdfapi/edit/cpdf_pagecontentgenerator.h" |
| 8 | 8 |
| 9 #include "core/fpdfapi/edit/cpdf_creator.h" | 9 #include "core/fpdfapi/edit/cpdf_creator.h" |
| 10 #include "core/fpdfapi/page/cpdf_docpagedata.h" | 10 #include "core/fpdfapi/page/cpdf_docpagedata.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 if (pContent) | 52 if (pContent) |
| 53 pPageDict->RemoveFor("Contents"); | 53 pPageDict->RemoveFor("Contents"); |
| 54 | 54 |
| 55 CPDF_Stream* pStream = new CPDF_Stream; | 55 CPDF_Stream* pStream = new CPDF_Stream; |
| 56 pStream->SetData(buf.GetBuffer(), buf.GetLength()); | 56 pStream->SetData(buf.GetBuffer(), buf.GetLength()); |
| 57 pPageDict->SetReferenceFor("Contents", m_pDocument, | 57 pPageDict->SetReferenceFor("Contents", m_pDocument, |
| 58 m_pDocument->AddIndirectObject(pStream)); | 58 m_pDocument->AddIndirectObject(pStream)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 CFX_ByteString CPDF_PageContentGenerator::RealizeResource( | 61 CFX_ByteString CPDF_PageContentGenerator::RealizeResource( |
| 62 CPDF_Object* pResourceObj, | 62 uint32_t dwResourceObjNum, |
| 63 const CFX_ByteString& bsType) { | 63 const CFX_ByteString& bsType) { |
| 64 ASSERT(dwResourceObjNum); | |
| 64 if (!m_pPage->m_pResources) { | 65 if (!m_pPage->m_pResources) { |
| 65 m_pPage->m_pResources = | 66 m_pPage->m_pResources = |
| 66 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); | 67 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); |
| 67 m_pPage->m_pFormDict->SetReferenceFor( | 68 m_pPage->m_pFormDict->SetReferenceFor( |
| 68 "Resources", m_pDocument, | 69 "Resources", m_pDocument, |
| 69 m_pDocument->AddIndirectObject(m_pPage->m_pResources)); | 70 m_pDocument->AddIndirectObject(m_pPage->m_pResources)); |
| 70 } | 71 } |
| 71 CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDictFor(bsType); | 72 CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDictFor(bsType); |
| 72 if (!pResList) { | 73 if (!pResList) { |
| 73 pResList = new CPDF_Dictionary(m_pDocument->GetByteStringPool()); | 74 pResList = new CPDF_Dictionary(m_pDocument->GetByteStringPool()); |
| 74 m_pPage->m_pResources->SetFor(bsType, pResList); | 75 m_pPage->m_pResources->SetFor(bsType, pResList); |
| 75 } | 76 } |
| 76 CFX_ByteString name; | 77 CFX_ByteString name; |
| 77 int idnum = 1; | 78 int idnum = 1; |
| 78 while (1) { | 79 while (1) { |
| 79 name.Format("FX%c%d", bsType[0], idnum); | 80 name.Format("FX%c%d", bsType[0], idnum); |
| 80 if (!pResList->KeyExist(name)) { | 81 if (!pResList->KeyExist(name)) { |
| 81 break; | 82 break; |
| 82 } | 83 } |
| 83 idnum++; | 84 idnum++; |
| 84 } | 85 } |
| 85 pResList->SetReferenceFor(name, m_pDocument, | 86 pResList->SetReferenceFor(name, m_pDocument, dwResourceObjNum); |
| 86 m_pDocument->AddIndirectObject(pResourceObj)); | |
| 87 return name; | 87 return name; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf& buf, | 90 void CPDF_PageContentGenerator::ProcessImage(CFX_ByteTextBuf& buf, |
| 91 CPDF_ImageObject* pImageObj) { | 91 CPDF_ImageObject* pImageObj) { |
| 92 if ((pImageObj->m_Matrix.a == 0 && pImageObj->m_Matrix.b == 0) || | 92 if ((pImageObj->m_Matrix.a == 0 && pImageObj->m_Matrix.b == 0) || |
| 93 (pImageObj->m_Matrix.c == 0 && pImageObj->m_Matrix.d == 0)) { | 93 (pImageObj->m_Matrix.c == 0 && pImageObj->m_Matrix.d == 0)) { |
| 94 return; | 94 return; |
| 95 } | 95 } |
| 96 buf << "q " << pImageObj->m_Matrix << " cm "; | 96 buf << "q " << pImageObj->m_Matrix << " cm "; |
| 97 | |
| 97 CPDF_Image* pImage = pImageObj->GetImage(); | 98 CPDF_Image* pImage = pImageObj->GetImage(); |
| 98 if (!pImage->IsInline()) { | 99 if (pImage->IsInline()) |
| 99 CPDF_Stream* pStream = pImage->GetStream(); | 100 return; |
| 100 uint32_t dwSavedObjNum = pStream->GetObjNum(); | 101 |
| 101 CFX_ByteString name = RealizeResource(pStream, "XObject"); | 102 CPDF_Stream* pStream = pImage->GetStream(); |
| 102 if (dwSavedObjNum == 0) { | 103 bool bWasInline = pStream->IsInline(); |
| 103 pImageObj->SetUnownedImage(m_pDocument->GetPageData()->GetImage(pStream)); | 104 if (bWasInline) { |
| 104 } | 105 pStream = ToStream(pStream->Clone().release()); |
|
Tom Sepez
2016/11/10 21:15:26
Note: this is the clone that avoids the UAF. The
| |
| 105 buf << "/" << PDF_NameEncode(name) << " Do Q\n"; | 106 m_pDocument->AddIndirectObject(pStream); |
| 106 } | 107 } |
| 108 | |
| 109 CFX_ByteString name = RealizeResource(pStream->GetObjNum(), "XObject"); | |
| 110 if (bWasInline) | |
| 111 pImageObj->SetUnownedImage(m_pDocument->GetPageData()->GetImage(pStream)); | |
| 112 | |
| 113 buf << "/" << PDF_NameEncode(name) << " Do Q\n"; | |
| 107 } | 114 } |
| 108 | 115 |
| 109 void CPDF_PageContentGenerator::ProcessForm(CFX_ByteTextBuf& buf, | 116 void CPDF_PageContentGenerator::ProcessForm(CFX_ByteTextBuf& buf, |
| 110 const uint8_t* data, | 117 const uint8_t* data, |
| 111 uint32_t size, | 118 uint32_t size, |
| 112 CFX_Matrix& matrix) { | 119 CFX_Matrix& matrix) { |
| 113 if (!data || !size) | 120 if (!data || !size) |
| 114 return; | 121 return; |
| 115 | 122 |
| 123 buf << "q " << matrix << " cm "; | |
| 124 | |
| 125 CFX_FloatRect bbox = m_pPage->GetPageBBox(); | |
| 126 matrix.TransformRect(bbox); | |
| 127 | |
| 116 CPDF_Dictionary* pFormDict = | 128 CPDF_Dictionary* pFormDict = |
| 117 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); | 129 new CPDF_Dictionary(m_pDocument->GetByteStringPool()); |
| 118 pFormDict->SetNameFor("Type", "XObject"); | 130 pFormDict->SetNameFor("Type", "XObject"); |
| 119 pFormDict->SetNameFor("Subtype", "Form"); | 131 pFormDict->SetNameFor("Subtype", "Form"); |
| 120 | |
| 121 CFX_FloatRect bbox = m_pPage->GetPageBBox(); | |
| 122 matrix.TransformRect(bbox); | |
| 123 pFormDict->SetRectFor("BBox", bbox); | 132 pFormDict->SetRectFor("BBox", bbox); |
| 124 | 133 |
| 125 CPDF_Stream* pStream = new CPDF_Stream; | 134 CPDF_Stream* pStream = new CPDF_Stream; |
| 126 pStream->InitStream(data, size, pFormDict); | 135 pStream->InitStream(data, size, pFormDict); |
| 127 buf << "q " << matrix << " cm "; | |
| 128 | 136 |
| 129 CFX_ByteString name = RealizeResource(pStream, "XObject"); | 137 CFX_ByteString name = |
| 138 RealizeResource(m_pDocument->AddIndirectObject(pStream), "XObject"); | |
| 130 buf << "/" << PDF_NameEncode(name) << " Do Q\n"; | 139 buf << "/" << PDF_NameEncode(name) << " Do Q\n"; |
| 131 } | 140 } |
| 132 | 141 |
| 133 void CPDF_PageContentGenerator::TransformContent(CFX_Matrix& matrix) { | 142 void CPDF_PageContentGenerator::TransformContent(CFX_Matrix& matrix) { |
| 134 CPDF_Dictionary* pDict = m_pPage->m_pFormDict; | 143 CPDF_Dictionary* pDict = m_pPage->m_pFormDict; |
| 135 CPDF_Object* pContent = | 144 CPDF_Object* pContent = |
| 136 pDict ? pDict->GetDirectObjectFor("Contents") : nullptr; | 145 pDict ? pDict->GetDirectObjectFor("Contents") : nullptr; |
| 137 if (!pContent) | 146 if (!pContent) |
| 138 return; | 147 return; |
| 139 | 148 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 168 } else if (CPDF_Stream* pStream = pContent->AsStream()) { | 177 } else if (CPDF_Stream* pStream = pContent->AsStream()) { |
| 169 CPDF_StreamAcc contentStream; | 178 CPDF_StreamAcc contentStream; |
| 170 contentStream.LoadAllData(pStream); | 179 contentStream.LoadAllData(pStream); |
| 171 ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix); | 180 ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix); |
| 172 } | 181 } |
| 173 CPDF_Stream* pStream = new CPDF_Stream; | 182 CPDF_Stream* pStream = new CPDF_Stream; |
| 174 pStream->SetData(buf.GetBuffer(), buf.GetLength()); | 183 pStream->SetData(buf.GetBuffer(), buf.GetLength()); |
| 175 m_pPage->m_pFormDict->SetReferenceFor( | 184 m_pPage->m_pFormDict->SetReferenceFor( |
| 176 "Contents", m_pDocument, m_pDocument->AddIndirectObject(pStream)); | 185 "Contents", m_pDocument, m_pDocument->AddIndirectObject(pStream)); |
| 177 } | 186 } |
| OLD | NEW |