OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "core/fpdfapi/fpdf_parser/cfdf_document.h" | |
8 | |
9 #include "core/fpdfapi/edit/cpdf_creator.h" | |
10 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h" | |
11 #include "core/fpdfapi/fpdf_parser/cpdf_syntax_parser.h" | |
12 #include "third_party/base/ptr_util.h" | |
13 | |
14 CFDF_Document::CFDF_Document() | |
15 : CPDF_IndirectObjectHolder(), | |
16 m_pRootDict(nullptr), | |
17 m_pFile(nullptr), | |
18 m_bOwnFile(FALSE), | |
19 m_pByteStringPool(pdfium::MakeUnique<CFX_ByteStringPool>()) {} | |
20 | |
21 CFDF_Document::~CFDF_Document() { | |
22 if (m_bOwnFile && m_pFile) | |
23 m_pFile->Release(); | |
24 m_pByteStringPool.DeleteObject(); // Make weak. | |
25 } | |
26 | |
27 CFDF_Document* CFDF_Document::CreateNewDoc() { | |
28 CFDF_Document* pDoc = new CFDF_Document; | |
29 pDoc->m_pRootDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); | |
30 pDoc->AddIndirectObject(pDoc->m_pRootDict); | |
31 pDoc->m_pRootDict->SetFor("FDF", | |
32 new CPDF_Dictionary(pDoc->GetByteStringPool())); | |
33 return pDoc; | |
34 } | |
35 | |
36 CFDF_Document* CFDF_Document::ParseFile(IFX_FileRead* pFile, FX_BOOL bOwnFile) { | |
37 if (!pFile) | |
38 return nullptr; | |
39 | |
40 std::unique_ptr<CFDF_Document> pDoc(new CFDF_Document); | |
41 pDoc->ParseStream(pFile, bOwnFile); | |
42 return pDoc->m_pRootDict ? pDoc.release() : nullptr; | |
43 } | |
44 | |
45 CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, uint32_t size) { | |
46 return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size), | |
47 TRUE); | |
48 } | |
49 | |
50 void CFDF_Document::ParseStream(IFX_FileRead* pFile, FX_BOOL bOwnFile) { | |
51 m_pFile = pFile; | |
52 m_bOwnFile = bOwnFile; | |
53 CPDF_SyntaxParser parser; | |
54 parser.InitParser(m_pFile, 0); | |
55 while (1) { | |
56 bool bNumber; | |
57 CFX_ByteString word = parser.GetNextWord(&bNumber); | |
58 if (bNumber) { | |
59 uint32_t objnum = FXSYS_atoui(word.c_str()); | |
60 word = parser.GetNextWord(&bNumber); | |
61 if (!bNumber) | |
62 break; | |
63 | |
64 word = parser.GetNextWord(nullptr); | |
65 if (word != "obj") | |
66 break; | |
67 | |
68 CPDF_Object* pObj = parser.GetObject(this, objnum, 0, true); | |
69 if (!pObj) | |
70 break; | |
71 | |
72 ReplaceIndirectObjectIfHigherGeneration(objnum, pObj); | |
73 word = parser.GetNextWord(nullptr); | |
74 if (word != "endobj") | |
75 break; | |
76 } else { | |
77 if (word != "trailer") | |
78 break; | |
79 | |
80 if (CPDF_Dictionary* pMainDict = | |
81 ToDictionary(parser.GetObject(this, 0, 0, true))) { | |
82 m_pRootDict = pMainDict->GetDictFor("Root"); | |
83 pMainDict->Release(); | |
84 } | |
85 break; | |
86 } | |
87 } | |
88 } | |
89 | |
90 FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { | |
91 if (!m_pRootDict) | |
92 return FALSE; | |
93 | |
94 buf << "%FDF-1.2\r\n"; | |
95 for (const auto& pair : *this) | |
96 buf << pair.first << " 0 obj\r\n" | |
97 << pair.second.get() << "\r\nendobj\r\n\r\n"; | |
98 | |
99 buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() | |
100 << " 0 R>>\r\n%%EOF\r\n"; | |
101 return TRUE; | |
102 } | |
OLD | NEW |