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/fpdfapi/parser/cfdf_document.h" | 7 #include "core/fpdfapi/parser/cfdf_document.h" |
8 | 8 |
9 #include "core/fpdfapi/edit/cpdf_creator.h" | 9 #include "core/fpdfapi/edit/cpdf_creator.h" |
10 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 10 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
11 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" | 11 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" |
12 #include "third_party/base/ptr_util.h" | 12 #include "third_party/base/ptr_util.h" |
13 | 13 |
14 CFDF_Document::CFDF_Document() | 14 CFDF_Document::CFDF_Document() |
15 : CPDF_IndirectObjectHolder(), | 15 : CPDF_IndirectObjectHolder(), |
16 m_pRootDict(nullptr), | 16 m_pRootDict(nullptr), |
17 m_pFile(nullptr), | 17 m_pFile(nullptr), |
18 m_bOwnFile(FALSE), | 18 m_bOwnFile(false), |
19 m_pByteStringPool(pdfium::MakeUnique<CFX_ByteStringPool>()) {} | 19 m_pByteStringPool(pdfium::MakeUnique<CFX_ByteStringPool>()) {} |
20 | 20 |
21 CFDF_Document::~CFDF_Document() { | 21 CFDF_Document::~CFDF_Document() { |
22 if (m_bOwnFile && m_pFile) | 22 if (m_bOwnFile && m_pFile) |
23 m_pFile->Release(); | 23 m_pFile->Release(); |
24 m_pByteStringPool.DeleteObject(); // Make weak. | 24 m_pByteStringPool.DeleteObject(); // Make weak. |
25 } | 25 } |
26 | 26 |
27 CFDF_Document* CFDF_Document::CreateNewDoc() { | 27 CFDF_Document* CFDF_Document::CreateNewDoc() { |
28 CFDF_Document* pDoc = new CFDF_Document; | 28 CFDF_Document* pDoc = new CFDF_Document; |
29 pDoc->m_pRootDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); | 29 pDoc->m_pRootDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); |
30 pDoc->AddIndirectObject(pDoc->m_pRootDict); | 30 pDoc->AddIndirectObject(pDoc->m_pRootDict); |
31 pDoc->m_pRootDict->SetFor("FDF", | 31 pDoc->m_pRootDict->SetFor("FDF", |
32 new CPDF_Dictionary(pDoc->GetByteStringPool())); | 32 new CPDF_Dictionary(pDoc->GetByteStringPool())); |
33 return pDoc; | 33 return pDoc; |
34 } | 34 } |
35 | 35 |
36 CFDF_Document* CFDF_Document::ParseFile(IFX_SeekableReadStream* pFile, | 36 CFDF_Document* CFDF_Document::ParseFile(IFX_SeekableReadStream* pFile, |
37 FX_BOOL bOwnFile) { | 37 bool bOwnFile) { |
38 if (!pFile) | 38 if (!pFile) |
39 return nullptr; | 39 return nullptr; |
40 | 40 |
41 std::unique_ptr<CFDF_Document> pDoc(new CFDF_Document); | 41 std::unique_ptr<CFDF_Document> pDoc(new CFDF_Document); |
42 pDoc->ParseStream(pFile, bOwnFile); | 42 pDoc->ParseStream(pFile, bOwnFile); |
43 return pDoc->m_pRootDict ? pDoc.release() : nullptr; | 43 return pDoc->m_pRootDict ? pDoc.release() : nullptr; |
44 } | 44 } |
45 | 45 |
46 CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, uint32_t size) { | 46 CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, uint32_t size) { |
47 return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size), | 47 return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size), |
48 TRUE); | 48 true); |
49 } | 49 } |
50 | 50 |
51 void CFDF_Document::ParseStream(IFX_SeekableReadStream* pFile, | 51 void CFDF_Document::ParseStream(IFX_SeekableReadStream* pFile, bool bOwnFile) { |
52 FX_BOOL bOwnFile) { | |
53 m_pFile = pFile; | 52 m_pFile = pFile; |
54 m_bOwnFile = bOwnFile; | 53 m_bOwnFile = bOwnFile; |
55 CPDF_SyntaxParser parser; | 54 CPDF_SyntaxParser parser; |
56 parser.InitParser(m_pFile, 0); | 55 parser.InitParser(m_pFile, 0); |
57 while (1) { | 56 while (1) { |
58 bool bNumber; | 57 bool bNumber; |
59 CFX_ByteString word = parser.GetNextWord(&bNumber); | 58 CFX_ByteString word = parser.GetNextWord(&bNumber); |
60 if (bNumber) { | 59 if (bNumber) { |
61 uint32_t objnum = FXSYS_atoui(word.c_str()); | 60 uint32_t objnum = FXSYS_atoui(word.c_str()); |
62 word = parser.GetNextWord(&bNumber); | 61 word = parser.GetNextWord(&bNumber); |
(...skipping 19 matching lines...) Expand all Loading... |
82 if (CPDF_Dictionary* pMainDict = | 81 if (CPDF_Dictionary* pMainDict = |
83 ToDictionary(parser.GetObject(this, 0, 0, true))) { | 82 ToDictionary(parser.GetObject(this, 0, 0, true))) { |
84 m_pRootDict = pMainDict->GetDictFor("Root"); | 83 m_pRootDict = pMainDict->GetDictFor("Root"); |
85 pMainDict->Release(); | 84 pMainDict->Release(); |
86 } | 85 } |
87 break; | 86 break; |
88 } | 87 } |
89 } | 88 } |
90 } | 89 } |
91 | 90 |
92 FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { | 91 bool CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { |
93 if (!m_pRootDict) | 92 if (!m_pRootDict) |
94 return FALSE; | 93 return false; |
95 | 94 |
96 buf << "%FDF-1.2\r\n"; | 95 buf << "%FDF-1.2\r\n"; |
97 for (const auto& pair : *this) | 96 for (const auto& pair : *this) |
98 buf << pair.first << " 0 obj\r\n" | 97 buf << pair.first << " 0 obj\r\n" |
99 << pair.second.get() << "\r\nendobj\r\n\r\n"; | 98 << pair.second.get() << "\r\nendobj\r\n\r\n"; |
100 | 99 |
101 buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() | 100 buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() |
102 << " 0 R>>\r\n%%EOF\r\n"; | 101 << " 0 R>>\r\n%%EOF\r\n"; |
103 return TRUE; | 102 return true; |
104 } | 103 } |
OLD | NEW |