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 <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "core/fpdfapi/edit/cpdf_creator.h" | 12 #include "core/fpdfapi/edit/cpdf_creator.h" |
13 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 13 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
14 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" | 14 #include "core/fpdfapi/parser/cpdf_syntax_parser.h" |
15 #include "third_party/base/ptr_util.h" | 15 #include "third_party/base/ptr_util.h" |
16 | 16 |
17 CFDF_Document::CFDF_Document() | 17 CFDF_Document::CFDF_Document() |
18 : CPDF_IndirectObjectHolder(), | 18 : CPDF_IndirectObjectHolder(), m_pRootDict(nullptr) {} |
19 m_pRootDict(nullptr), | |
20 m_pFile(nullptr), | |
21 m_bOwnFile(false) {} | |
22 | 19 |
23 CFDF_Document::~CFDF_Document() { | 20 CFDF_Document::~CFDF_Document() {} |
24 if (m_bOwnFile && m_pFile) | |
25 m_pFile->Release(); | |
26 } | |
27 | 21 |
28 std::unique_ptr<CFDF_Document> CFDF_Document::CreateNewDoc() { | 22 std::unique_ptr<CFDF_Document> CFDF_Document::CreateNewDoc() { |
29 auto pDoc = pdfium::MakeUnique<CFDF_Document>(); | 23 auto pDoc = pdfium::MakeUnique<CFDF_Document>(); |
30 pDoc->m_pRootDict = pDoc->NewIndirect<CPDF_Dictionary>(); | 24 pDoc->m_pRootDict = pDoc->NewIndirect<CPDF_Dictionary>(); |
31 pDoc->m_pRootDict->SetNewFor<CPDF_Dictionary>("FDF"); | 25 pDoc->m_pRootDict->SetNewFor<CPDF_Dictionary>("FDF"); |
32 return pDoc; | 26 return pDoc; |
33 } | 27 } |
34 | 28 |
35 std::unique_ptr<CFDF_Document> CFDF_Document::ParseFile( | 29 std::unique_ptr<CFDF_Document> CFDF_Document::ParseFile( |
36 IFX_SeekableReadStream* pFile, | 30 const CFX_RetainPtr<IFX_SeekableReadStream>& pFile) { |
37 bool bOwnFile) { | |
38 if (!pFile) | 31 if (!pFile) |
39 return nullptr; | 32 return nullptr; |
40 | 33 |
41 auto pDoc = pdfium::MakeUnique<CFDF_Document>(); | 34 auto pDoc = pdfium::MakeUnique<CFDF_Document>(); |
42 pDoc->ParseStream(pFile, bOwnFile); | 35 pDoc->ParseStream(pFile); |
43 return pDoc->m_pRootDict ? std::move(pDoc) : nullptr; | 36 return pDoc->m_pRootDict ? std::move(pDoc) : nullptr; |
44 } | 37 } |
45 | 38 |
46 std::unique_ptr<CFDF_Document> CFDF_Document::ParseMemory(const uint8_t* pData, | 39 std::unique_ptr<CFDF_Document> CFDF_Document::ParseMemory(uint8_t* pData, |
47 uint32_t size) { | 40 uint32_t size) { |
48 return CFDF_Document::ParseFile( | 41 return CFDF_Document::ParseFile(IFX_MemoryStream::Create(pData, size)); |
49 IFX_MemoryStream::Create((uint8_t*)pData, size), true); | |
50 } | 42 } |
51 | 43 |
52 void CFDF_Document::ParseStream(IFX_SeekableReadStream* pFile, bool bOwnFile) { | 44 void CFDF_Document::ParseStream( |
| 45 const CFX_RetainPtr<IFX_SeekableReadStream>& pFile) { |
53 m_pFile = pFile; | 46 m_pFile = pFile; |
54 m_bOwnFile = bOwnFile; | |
55 CPDF_SyntaxParser parser; | 47 CPDF_SyntaxParser parser; |
56 parser.InitParser(m_pFile, 0); | 48 parser.InitParser(m_pFile, 0); |
57 while (1) { | 49 while (1) { |
58 bool bNumber; | 50 bool bNumber; |
59 CFX_ByteString word = parser.GetNextWord(&bNumber); | 51 CFX_ByteString word = parser.GetNextWord(&bNumber); |
60 if (bNumber) { | 52 if (bNumber) { |
61 uint32_t objnum = FXSYS_atoui(word.c_str()); | 53 uint32_t objnum = FXSYS_atoui(word.c_str()); |
62 if (!objnum) | 54 if (!objnum) |
63 break; | 55 break; |
64 | 56 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 | 91 |
100 buf << "%FDF-1.2\r\n"; | 92 buf << "%FDF-1.2\r\n"; |
101 for (const auto& pair : *this) | 93 for (const auto& pair : *this) |
102 buf << pair.first << " 0 obj\r\n" | 94 buf << pair.first << " 0 obj\r\n" |
103 << pair.second.get() << "\r\nendobj\r\n\r\n"; | 95 << pair.second.get() << "\r\nendobj\r\n\r\n"; |
104 | 96 |
105 buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() | 97 buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() |
106 << " 0 R>>\r\n%%EOF\r\n"; | 98 << " 0 R>>\r\n%%EOF\r\n"; |
107 return true; | 99 return true; |
108 } | 100 } |
OLD | NEW |