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/include/fpdfapi/fpdf_parser.h" | |
8 | |
9 #include "core/include/fpdfapi/fpdf_serial.h" | |
10 #include "core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h" | |
11 | |
12 CFDF_Document::CFDF_Document() : CPDF_IndirectObjectHolder(NULL) { | |
13 m_pRootDict = NULL; | |
14 m_pFile = NULL; | |
15 m_bOwnFile = FALSE; | |
16 } | |
17 CFDF_Document::~CFDF_Document() { | |
18 if (m_bOwnFile && m_pFile) { | |
19 m_pFile->Release(); | |
20 } | |
21 } | |
22 CFDF_Document* CFDF_Document::CreateNewDoc() { | |
23 CFDF_Document* pDoc = new CFDF_Document; | |
24 pDoc->m_pRootDict = new CPDF_Dictionary; | |
25 pDoc->AddIndirectObject(pDoc->m_pRootDict); | |
26 CPDF_Dictionary* pFDFDict = new CPDF_Dictionary; | |
27 pDoc->m_pRootDict->SetAt("FDF", pFDFDict); | |
28 return pDoc; | |
29 } | |
30 CFDF_Document* CFDF_Document::ParseFile(IFX_FileRead* pFile, FX_BOOL bOwnFile) { | |
31 if (!pFile) { | |
32 return NULL; | |
33 } | |
34 CFDF_Document* pDoc = new CFDF_Document; | |
35 pDoc->ParseStream(pFile, bOwnFile); | |
36 if (!pDoc->m_pRootDict) { | |
37 delete pDoc; | |
38 return NULL; | |
39 } | |
40 return pDoc; | |
41 } | |
42 CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, FX_DWORD size) { | |
43 return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size), | |
44 TRUE); | |
45 } | |
46 void CFDF_Document::ParseStream(IFX_FileRead* pFile, FX_BOOL bOwnFile) { | |
47 m_pFile = pFile; | |
48 m_bOwnFile = bOwnFile; | |
49 CPDF_SyntaxParser parser; | |
50 parser.InitParser(m_pFile, 0); | |
51 while (1) { | |
52 bool bNumber; | |
53 CFX_ByteString word = parser.GetNextWord(&bNumber); | |
54 if (bNumber) { | |
55 FX_DWORD objnum = FXSYS_atoui(word); | |
56 word = parser.GetNextWord(&bNumber); | |
57 if (!bNumber) { | |
58 break; | |
59 } | |
60 word = parser.GetNextWord(nullptr); | |
61 if (word != "obj") { | |
62 break; | |
63 } | |
64 CPDF_Object* pObj = parser.GetObject(this, objnum, 0, true); | |
65 if (!pObj) { | |
66 break; | |
67 } | |
68 InsertIndirectObject(objnum, pObj); | |
69 word = parser.GetNextWord(nullptr); | |
70 if (word != "endobj") { | |
71 break; | |
72 } | |
73 } else { | |
74 if (word != "trailer") { | |
75 break; | |
76 } | |
77 if (CPDF_Dictionary* pMainDict = | |
78 ToDictionary(parser.GetObject(this, 0, 0, true))) { | |
79 m_pRootDict = pMainDict->GetDictBy("Root"); | |
80 pMainDict->Release(); | |
81 } | |
82 break; | |
83 } | |
84 } | |
85 } | |
86 FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { | |
87 if (!m_pRootDict) { | |
88 return FALSE; | |
89 } | |
90 buf << "%FDF-1.2\r\n"; | |
91 for (const auto& pair : m_IndirectObjs) { | |
92 buf << pair.first << " 0 obj\r\n" << pair.second << "\r\nendobj\r\n\r\n"; | |
93 } | |
94 buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum() | |
95 << " 0 R>>\r\n%%EOF\r\n"; | |
96 return TRUE; | |
97 } | |
OLD | NEW |