| 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 "xfa/fxfa/include/xfa_ffdoc.h" | 7 #include "xfa/fxfa/include/xfa_ffdoc.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 13 #include "core/fpdfdoc/include/fpdf_doc.h" | 13 #include "core/fpdfdoc/include/fpdf_doc.h" |
| 14 #include "core/fxcrt/include/fx_ext.h" | 14 #include "core/fxcrt/include/fx_ext.h" |
| 15 #include "core/fxcrt/include/fx_memory.h" | 15 #include "core/fxcrt/include/fx_memory.h" |
| 16 #include "xfa/fde/xml/fde_xml_imp.h" | 16 #include "xfa/fde/xml/fde_xml_imp.h" |
| 17 #include "xfa/fwl/core/fwl_noteimp.h" | 17 #include "xfa/fwl/core/fwl_noteimp.h" |
| 18 #include "xfa/fxfa/app/xfa_ffnotify.h" | 18 #include "xfa/fxfa/app/xfa_ffnotify.h" |
| 19 #include "xfa/fxfa/include/xfa_checksum.h" | 19 #include "xfa/fxfa/include/xfa_checksum.h" |
| 20 #include "xfa/fxfa/include/xfa_ffapp.h" | 20 #include "xfa/fxfa/include/xfa_ffapp.h" |
| 21 #include "xfa/fxfa/include/xfa_ffdocview.h" | 21 #include "xfa/fxfa/include/xfa_ffdocview.h" |
| 22 #include "xfa/fxfa/include/xfa_ffwidget.h" | 22 #include "xfa/fxfa/include/xfa_ffwidget.h" |
| 23 #include "xfa/fxfa/include/xfa_fontmgr.h" | 23 #include "xfa/fxfa/include/xfa_fontmgr.h" |
| 24 #include "xfa/fxfa/parser/xfa_document.h" | 24 #include "xfa/fxfa/parser/xfa_document.h" |
| 25 #include "xfa/fxfa/parser/xfa_document_serialize.h" | 25 #include "xfa/fxfa/parser/xfa_document_serialize.h" |
| 26 #include "xfa/fxfa/parser/xfa_parser.h" | |
| 27 #include "xfa/fxfa/parser/xfa_parser_imp.h" | 26 #include "xfa/fxfa/parser/xfa_parser_imp.h" |
| 28 #include "xfa/fxfa/parser/xfa_parser_imp.h" | 27 #include "xfa/fxfa/parser/xfa_parser_imp.h" |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 struct FX_BASE64DATA { | 31 struct FX_BASE64DATA { |
| 33 uint32_t data1 : 2; | 32 uint32_t data1 : 2; |
| 34 uint32_t data2 : 6; | 33 uint32_t data2 : 6; |
| 35 uint32_t data3 : 4; | 34 uint32_t data3 : 4; |
| 36 uint32_t data4 : 4; | 35 uint32_t data4 : 4; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 *pDstEnd++ = ((uint8_t*)&dstData)[2]; | 142 *pDstEnd++ = ((uint8_t*)&dstData)[2]; |
| 144 } | 143 } |
| 145 } | 144 } |
| 146 return pDstEnd - pDst; | 145 return pDstEnd - pDst; |
| 147 } | 146 } |
| 148 | 147 |
| 149 } // namespace | 148 } // namespace |
| 150 | 149 |
| 151 CXFA_FFDoc::CXFA_FFDoc(CXFA_FFApp* pApp, IXFA_DocProvider* pDocProvider) | 150 CXFA_FFDoc::CXFA_FFDoc(CXFA_FFApp* pApp, IXFA_DocProvider* pDocProvider) |
| 152 : m_pDocProvider(pDocProvider), | 151 : m_pDocProvider(pDocProvider), |
| 153 m_pDocument(nullptr), | 152 m_pDocumentParser(nullptr), |
| 154 m_pStream(nullptr), | 153 m_pStream(nullptr), |
| 155 m_pApp(pApp), | 154 m_pApp(pApp), |
| 156 m_pNotify(nullptr), | 155 m_pNotify(nullptr), |
| 157 m_pPDFDoc(nullptr), | 156 m_pPDFDoc(nullptr), |
| 158 m_dwDocType(XFA_DOCTYPE_Static), | 157 m_dwDocType(XFA_DOCTYPE_Static), |
| 159 m_bOwnStream(TRUE) {} | 158 m_bOwnStream(TRUE) {} |
| 159 |
| 160 CXFA_FFDoc::~CXFA_FFDoc() { | 160 CXFA_FFDoc::~CXFA_FFDoc() { |
| 161 CloseDoc(); | 161 CloseDoc(); |
| 162 } | 162 } |
| 163 |
| 163 uint32_t CXFA_FFDoc::GetDocType() { | 164 uint32_t CXFA_FFDoc::GetDocType() { |
| 164 return m_dwDocType; | 165 return m_dwDocType; |
| 165 } | 166 } |
| 167 |
| 166 int32_t CXFA_FFDoc::StartLoad() { | 168 int32_t CXFA_FFDoc::StartLoad() { |
| 167 m_pNotify = new CXFA_FFNotify(this); | 169 m_pNotify.reset(new CXFA_FFNotify(this)); |
| 168 CXFA_DocumentParser* pDocParser = new CXFA_DocumentParser(m_pNotify); | 170 m_pDocumentParser.reset(new CXFA_DocumentParser(m_pNotify.get())); |
| 169 int32_t iStatus = pDocParser->StartParse(m_pStream); | 171 int32_t iStatus = m_pDocumentParser->StartParse(m_pStream); |
| 170 m_pDocument = pDocParser->GetDocument(); | |
| 171 return iStatus; | 172 return iStatus; |
| 172 } | 173 } |
| 174 |
| 173 FX_BOOL XFA_GetPDFContentsFromPDFXML(CFDE_XMLNode* pPDFElement, | 175 FX_BOOL XFA_GetPDFContentsFromPDFXML(CFDE_XMLNode* pPDFElement, |
| 174 uint8_t*& pByteBuffer, | 176 uint8_t*& pByteBuffer, |
| 175 int32_t& iBufferSize) { | 177 int32_t& iBufferSize) { |
| 176 CFDE_XMLElement* pDocumentElement = nullptr; | 178 CFDE_XMLElement* pDocumentElement = nullptr; |
| 177 for (CFDE_XMLNode* pXMLNode = | 179 for (CFDE_XMLNode* pXMLNode = |
| 178 pPDFElement->GetNodeItem(CFDE_XMLNode::FirstChild); | 180 pPDFElement->GetNodeItem(CFDE_XMLNode::FirstChild); |
| 179 pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) { | 181 pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) { |
| 180 if (pXMLNode->GetType() == FDE_XMLNODE_Element) { | 182 if (pXMLNode->GetType() == FDE_XMLNODE_Element) { |
| 181 CFX_WideString wsTagName; | 183 CFX_WideString wsTagName; |
| 182 CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode); | 184 CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 CXFA_Node* pNextSibling = | 229 CXFA_Node* pNextSibling = |
| 228 pChildNode->GetNodeItem(XFA_NODEITEM_NextSibling); | 230 pChildNode->GetNodeItem(XFA_NODEITEM_NextSibling); |
| 229 pNewRoot->RemoveChild(pChildNode); | 231 pNewRoot->RemoveChild(pChildNode); |
| 230 pOriginRoot->InsertChild(pChildNode); | 232 pOriginRoot->InsertChild(pChildNode); |
| 231 pChildNode = pNextSibling; | 233 pChildNode = pNextSibling; |
| 232 pNextSibling = nullptr; | 234 pNextSibling = nullptr; |
| 233 } | 235 } |
| 234 } | 236 } |
| 235 } | 237 } |
| 236 int32_t CXFA_FFDoc::DoLoad(IFX_Pause* pPause) { | 238 int32_t CXFA_FFDoc::DoLoad(IFX_Pause* pPause) { |
| 237 int32_t iStatus = m_pDocument->GetParser()->DoParse(pPause); | 239 int32_t iStatus = m_pDocumentParser->DoParse(pPause); |
| 238 if (iStatus == XFA_PARSESTATUS_Done && !m_pPDFDoc) { | 240 if (iStatus == XFA_PARSESTATUS_Done && !m_pPDFDoc) { |
| 239 CXFA_Node* pPDFNode = ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Pdf)); | 241 CXFA_Node* pPDFNode = ToNode( |
| 242 m_pDocumentParser->GetDocument()->GetXFAObject(XFA_HASHCODE_Pdf)); |
| 240 if (!pPDFNode) { | 243 if (!pPDFNode) { |
| 241 return XFA_PARSESTATUS_SyntaxErr; | 244 return XFA_PARSESTATUS_SyntaxErr; |
| 242 } | 245 } |
| 243 CFDE_XMLNode* pPDFXML = pPDFNode->GetXMLMappingNode(); | 246 CFDE_XMLNode* pPDFXML = pPDFNode->GetXMLMappingNode(); |
| 244 if (pPDFXML->GetType() != FDE_XMLNODE_Element) { | 247 if (pPDFXML->GetType() != FDE_XMLNODE_Element) { |
| 245 return XFA_PARSESTATUS_SyntaxErr; | 248 return XFA_PARSESTATUS_SyntaxErr; |
| 246 } | 249 } |
| 247 int32_t iBufferSize = 0; | 250 int32_t iBufferSize = 0; |
| 248 uint8_t* pByteBuffer = nullptr; | 251 uint8_t* pByteBuffer = nullptr; |
| 249 IFX_FileRead* pXFAReader = nullptr; | 252 IFX_FileRead* pXFAReader = nullptr; |
| 250 if (XFA_GetPDFContentsFromPDFXML(pPDFXML, pByteBuffer, iBufferSize)) { | 253 if (XFA_GetPDFContentsFromPDFXML(pPDFXML, pByteBuffer, iBufferSize)) { |
| 251 pXFAReader = FX_CreateMemoryStream(pByteBuffer, iBufferSize, TRUE); | 254 pXFAReader = FX_CreateMemoryStream(pByteBuffer, iBufferSize, TRUE); |
| 252 } else { | 255 } else { |
| 253 CFX_WideString wsHref; | 256 CFX_WideString wsHref; |
| 254 static_cast<CFDE_XMLElement*>(pPDFXML)->GetString(L"href", wsHref); | 257 static_cast<CFDE_XMLElement*>(pPDFXML)->GetString(L"href", wsHref); |
| 255 if (!wsHref.IsEmpty()) { | 258 if (!wsHref.IsEmpty()) { |
| 256 pXFAReader = GetDocProvider()->OpenLinkedFile(this, wsHref); | 259 pXFAReader = GetDocProvider()->OpenLinkedFile(this, wsHref); |
| 257 } | 260 } |
| 258 } | 261 } |
| 259 if (!pXFAReader) { | 262 if (!pXFAReader) |
| 260 return XFA_PARSESTATUS_SyntaxErr; | 263 return XFA_PARSESTATUS_SyntaxErr; |
| 261 } | 264 |
| 262 CPDF_Document* pPDFDocument = | 265 CPDF_Document* pPDFDocument = |
| 263 GetDocProvider()->OpenPDF(this, pXFAReader, TRUE); | 266 GetDocProvider()->OpenPDF(this, pXFAReader, TRUE); |
| 264 ASSERT(!m_pPDFDoc); | 267 ASSERT(!m_pPDFDoc); |
| 265 if (!OpenDoc(pPDFDocument)) { | 268 if (!OpenDoc(pPDFDocument)) |
| 266 return XFA_PARSESTATUS_SyntaxErr; | 269 return XFA_PARSESTATUS_SyntaxErr; |
| 267 } | 270 |
| 268 IXFA_Parser* pParser = IXFA_Parser::Create(m_pDocument, TRUE); | 271 CXFA_Document* doc = m_pDocumentParser->GetDocument(); |
| 269 if (!pParser) { | 272 std::unique_ptr<CXFA_SimpleParser> pParser( |
| 273 new CXFA_SimpleParser(doc, true)); |
| 274 if (!pParser) |
| 270 return XFA_PARSESTATUS_SyntaxErr; | 275 return XFA_PARSESTATUS_SyntaxErr; |
| 271 } | 276 |
| 272 CXFA_Node* pRootNode = nullptr; | 277 CXFA_Node* pRootNode = nullptr; |
| 273 if (pParser->StartParse(m_pStream) == XFA_PARSESTATUS_Ready && | 278 if (pParser->StartParse(m_pStream) == XFA_PARSESTATUS_Ready && |
| 274 pParser->DoParse(nullptr) == XFA_PARSESTATUS_Done) { | 279 pParser->DoParse(nullptr) == XFA_PARSESTATUS_Done) { |
| 275 pRootNode = pParser->GetRootNode(); | 280 pRootNode = pParser->GetRootNode(); |
| 276 } | 281 } |
| 277 if (pRootNode && m_pDocument->GetRoot()) { | 282 if (pRootNode && doc->GetRoot()) { |
| 278 XFA_XPDPacket_MergeRootNode(m_pDocument->GetRoot(), pRootNode); | 283 XFA_XPDPacket_MergeRootNode(doc->GetRoot(), pRootNode); |
| 279 iStatus = XFA_PARSESTATUS_Done; | 284 iStatus = XFA_PARSESTATUS_Done; |
| 280 } else { | 285 } else { |
| 281 iStatus = XFA_PARSESTATUS_StatusErr; | 286 iStatus = XFA_PARSESTATUS_StatusErr; |
| 282 } | 287 } |
| 283 pParser->Release(); | |
| 284 pParser = nullptr; | |
| 285 } | 288 } |
| 286 return iStatus; | 289 return iStatus; |
| 287 } | 290 } |
| 288 void CXFA_FFDoc::StopLoad() { | 291 void CXFA_FFDoc::StopLoad() { |
| 289 m_pApp->GetXFAFontMgr()->LoadDocFonts(this); | 292 m_pApp->GetXFAFontMgr()->LoadDocFonts(this); |
| 290 m_dwDocType = XFA_DOCTYPE_Static; | 293 m_dwDocType = XFA_DOCTYPE_Static; |
| 291 CXFA_Node* pConfig = ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Config)); | 294 CXFA_Node* pConfig = ToNode( |
| 295 m_pDocumentParser->GetDocument()->GetXFAObject(XFA_HASHCODE_Config)); |
| 292 if (!pConfig) { | 296 if (!pConfig) { |
| 293 return; | 297 return; |
| 294 } | 298 } |
| 295 CXFA_Node* pAcrobat = pConfig->GetFirstChildByClass(XFA_Element::Acrobat); | 299 CXFA_Node* pAcrobat = pConfig->GetFirstChildByClass(XFA_Element::Acrobat); |
| 296 if (!pAcrobat) { | 300 if (!pAcrobat) { |
| 297 return; | 301 return; |
| 298 } | 302 } |
| 299 CXFA_Node* pAcrobat7 = pAcrobat->GetFirstChildByClass(XFA_Element::Acrobat7); | 303 CXFA_Node* pAcrobat7 = pAcrobat->GetFirstChildByClass(XFA_Element::Acrobat7); |
| 300 if (!pAcrobat7) { | 304 if (!pAcrobat7) { |
| 301 return; | 305 return; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 IFX_FileRead* pFileRead = new CXFA_FileRead(xfaStreams); | 372 IFX_FileRead* pFileRead = new CXFA_FileRead(xfaStreams); |
| 369 m_pPDFDoc = pPDFDoc; | 373 m_pPDFDoc = pPDFDoc; |
| 370 if (m_pStream) { | 374 if (m_pStream) { |
| 371 m_pStream->Release(); | 375 m_pStream->Release(); |
| 372 m_pStream = nullptr; | 376 m_pStream = nullptr; |
| 373 } | 377 } |
| 374 m_pStream = pFileRead; | 378 m_pStream = pFileRead; |
| 375 m_bOwnStream = TRUE; | 379 m_bOwnStream = TRUE; |
| 376 return TRUE; | 380 return TRUE; |
| 377 } | 381 } |
| 382 |
| 378 FX_BOOL CXFA_FFDoc::CloseDoc() { | 383 FX_BOOL CXFA_FFDoc::CloseDoc() { |
| 379 for (const auto& pair : m_TypeToDocViewMap) | 384 for (const auto& pair : m_TypeToDocViewMap) |
| 380 pair.second->RunDocClose(); | 385 pair.second->RunDocClose(); |
| 381 | 386 |
| 382 if (m_pDocument) | 387 CXFA_Document* doc = |
| 383 m_pDocument->ClearLayoutData(); | 388 m_pDocumentParser ? m_pDocumentParser->GetDocument() : nullptr; |
| 389 if (doc) |
| 390 doc->ClearLayoutData(); |
| 384 | 391 |
| 385 m_TypeToDocViewMap.clear(); | 392 m_TypeToDocViewMap.clear(); |
| 386 | 393 |
| 387 if (m_pDocument) { | 394 m_pNotify.reset(nullptr); |
| 388 m_pDocument->GetParser()->Release(); | |
| 389 m_pDocument = nullptr; | |
| 390 } | |
| 391 | |
| 392 delete m_pNotify; | |
| 393 m_pNotify = nullptr; | |
| 394 | |
| 395 m_pApp->GetXFAFontMgr()->ReleaseDocFonts(this); | 395 m_pApp->GetXFAFontMgr()->ReleaseDocFonts(this); |
| 396 | 396 |
| 397 if (m_dwDocType != XFA_DOCTYPE_XDP && m_pStream && m_bOwnStream) { | 397 if (m_dwDocType != XFA_DOCTYPE_XDP && m_pStream && m_bOwnStream) { |
| 398 m_pStream->Release(); | 398 m_pStream->Release(); |
| 399 m_pStream = nullptr; | 399 m_pStream = nullptr; |
| 400 } | 400 } |
| 401 | 401 |
| 402 for (const auto& pair : m_HashToDibDpiMap) | 402 for (const auto& pair : m_HashToDibDpiMap) |
| 403 delete pair.second.pDibSource; | 403 delete pair.second.pDibSource; |
| 404 | 404 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 CFX_DIBitmap* pDibSource = XFA_LoadImageFromBuffer( | 467 CFX_DIBitmap* pDibSource = XFA_LoadImageFromBuffer( |
| 468 pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi); | 468 pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi); |
| 469 m_HashToDibDpiMap[dwHash] = {pDibSource, iImageXDpi, iImageYDpi}; | 469 m_HashToDibDpiMap[dwHash] = {pDibSource, iImageXDpi, iImageYDpi}; |
| 470 pImageFileRead->Release(); | 470 pImageFileRead->Release(); |
| 471 return pDibSource; | 471 return pDibSource; |
| 472 } | 472 } |
| 473 | 473 |
| 474 bool CXFA_FFDoc::SavePackage(XFA_HashCode code, | 474 bool CXFA_FFDoc::SavePackage(XFA_HashCode code, |
| 475 IFX_FileWrite* pFile, | 475 IFX_FileWrite* pFile, |
| 476 CXFA_ChecksumContext* pCSContext) { | 476 CXFA_ChecksumContext* pCSContext) { |
| 477 std::unique_ptr<CXFA_DataExporter> pExport( | 477 CXFA_Document* doc = m_pDocumentParser->GetDocument(); |
| 478 new CXFA_DataExporter(m_pDocument)); | 478 |
| 479 CXFA_Node* pNode = code == XFA_HASHCODE_Xfa | 479 std::unique_ptr<CXFA_DataExporter> pExport(new CXFA_DataExporter(doc)); |
| 480 ? m_pDocument->GetRoot() | 480 CXFA_Node* pNode = code == XFA_HASHCODE_Xfa ? doc->GetRoot() |
| 481 : ToNode(m_pDocument->GetXFAObject(code)); | 481 : ToNode(doc->GetXFAObject(code)); |
| 482 if (!pNode) | 482 if (!pNode) |
| 483 return !!pExport->Export(pFile); | 483 return !!pExport->Export(pFile); |
| 484 | 484 |
| 485 CFX_ByteString bsChecksum; | 485 CFX_ByteString bsChecksum; |
| 486 if (pCSContext) | 486 if (pCSContext) |
| 487 bsChecksum = pCSContext->GetChecksum(); | 487 bsChecksum = pCSContext->GetChecksum(); |
| 488 | 488 |
| 489 return !!pExport->Export( | 489 return !!pExport->Export( |
| 490 pFile, pNode, 0, bsChecksum.GetLength() ? bsChecksum.c_str() : nullptr); | 490 pFile, pNode, 0, bsChecksum.GetLength() ? bsChecksum.c_str() : nullptr); |
| 491 } | 491 } |
| 492 | 492 |
| 493 FX_BOOL CXFA_FFDoc::ImportData(IFX_FileRead* pStream, FX_BOOL bXDP) { | 493 FX_BOOL CXFA_FFDoc::ImportData(IFX_FileRead* pStream, FX_BOOL bXDP) { |
| 494 std::unique_ptr<CXFA_DataImporter> importer( | 494 std::unique_ptr<CXFA_DataImporter> importer( |
| 495 new CXFA_DataImporter(m_pDocument)); | 495 new CXFA_DataImporter(m_pDocumentParser->GetDocument())); |
| 496 return importer->ImportData(pStream); | 496 return importer->ImportData(pStream); |
| 497 } | 497 } |
| OLD | NEW |