| 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/xfa_ffdoc.h" | 7 #include "xfa/fxfa/xfa_ffdoc.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } // namespace | 148 } // namespace |
| 149 | 149 |
| 150 CXFA_FFDoc::CXFA_FFDoc(CXFA_FFApp* pApp, IXFA_DocEnvironment* pDocEnvironment) | 150 CXFA_FFDoc::CXFA_FFDoc(CXFA_FFApp* pApp, IXFA_DocEnvironment* pDocEnvironment) |
| 151 : m_pDocEnvironment(pDocEnvironment), | 151 : m_pDocEnvironment(pDocEnvironment), |
| 152 m_pDocumentParser(nullptr), | 152 m_pDocumentParser(nullptr), |
| 153 m_pStream(nullptr), | 153 m_pStream(nullptr), |
| 154 m_pApp(pApp), | 154 m_pApp(pApp), |
| 155 m_pNotify(nullptr), | 155 m_pNotify(nullptr), |
| 156 m_pPDFDoc(nullptr), | 156 m_pPDFDoc(nullptr), |
| 157 m_dwDocType(XFA_DOCTYPE_Static), | 157 m_dwDocType(XFA_DOCTYPE_Static), |
| 158 m_bOwnStream(TRUE) {} | 158 m_bOwnStream(true) {} |
| 159 | 159 |
| 160 CXFA_FFDoc::~CXFA_FFDoc() { | 160 CXFA_FFDoc::~CXFA_FFDoc() { |
| 161 CloseDoc(); | 161 CloseDoc(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 uint32_t CXFA_FFDoc::GetDocType() { | 164 uint32_t CXFA_FFDoc::GetDocType() { |
| 165 return m_dwDocType; | 165 return m_dwDocType; |
| 166 } | 166 } |
| 167 | 167 |
| 168 int32_t CXFA_FFDoc::StartLoad() { | 168 int32_t CXFA_FFDoc::StartLoad() { |
| 169 m_pNotify.reset(new CXFA_FFNotify(this)); | 169 m_pNotify.reset(new CXFA_FFNotify(this)); |
| 170 m_pDocumentParser.reset(new CXFA_DocumentParser(m_pNotify.get())); | 170 m_pDocumentParser.reset(new CXFA_DocumentParser(m_pNotify.get())); |
| 171 int32_t iStatus = m_pDocumentParser->StartParse(m_pStream, XFA_XDPPACKET_XDP); | 171 int32_t iStatus = m_pDocumentParser->StartParse(m_pStream, XFA_XDPPACKET_XDP); |
| 172 return iStatus; | 172 return iStatus; |
| 173 } | 173 } |
| 174 | 174 |
| 175 FX_BOOL XFA_GetPDFContentsFromPDFXML(CFDE_XMLNode* pPDFElement, | 175 bool XFA_GetPDFContentsFromPDFXML(CFDE_XMLNode* pPDFElement, |
| 176 uint8_t*& pByteBuffer, | 176 uint8_t*& pByteBuffer, |
| 177 int32_t& iBufferSize) { | 177 int32_t& iBufferSize) { |
| 178 CFDE_XMLElement* pDocumentElement = nullptr; | 178 CFDE_XMLElement* pDocumentElement = nullptr; |
| 179 for (CFDE_XMLNode* pXMLNode = | 179 for (CFDE_XMLNode* pXMLNode = |
| 180 pPDFElement->GetNodeItem(CFDE_XMLNode::FirstChild); | 180 pPDFElement->GetNodeItem(CFDE_XMLNode::FirstChild); |
| 181 pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) { | 181 pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) { |
| 182 if (pXMLNode->GetType() == FDE_XMLNODE_Element) { | 182 if (pXMLNode->GetType() == FDE_XMLNODE_Element) { |
| 183 CFX_WideString wsTagName; | 183 CFX_WideString wsTagName; |
| 184 CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode); | 184 CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode); |
| 185 pXMLElement->GetTagName(wsTagName); | 185 pXMLElement->GetTagName(wsTagName); |
| 186 if (wsTagName == FX_WSTRC(L"document")) { | 186 if (wsTagName == FX_WSTRC(L"document")) { |
| 187 pDocumentElement = pXMLElement; | 187 pDocumentElement = pXMLElement; |
| 188 break; | 188 break; |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 if (!pDocumentElement) { | 192 if (!pDocumentElement) { |
| 193 return FALSE; | 193 return false; |
| 194 } | 194 } |
| 195 CFDE_XMLElement* pChunkElement = nullptr; | 195 CFDE_XMLElement* pChunkElement = nullptr; |
| 196 for (CFDE_XMLNode* pXMLNode = | 196 for (CFDE_XMLNode* pXMLNode = |
| 197 pDocumentElement->GetNodeItem(CFDE_XMLNode::FirstChild); | 197 pDocumentElement->GetNodeItem(CFDE_XMLNode::FirstChild); |
| 198 pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) { | 198 pXMLNode; pXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::NextSibling)) { |
| 199 if (pXMLNode->GetType() == FDE_XMLNODE_Element) { | 199 if (pXMLNode->GetType() == FDE_XMLNODE_Element) { |
| 200 CFX_WideString wsTagName; | 200 CFX_WideString wsTagName; |
| 201 CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode); | 201 CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode); |
| 202 pXMLElement->GetTagName(wsTagName); | 202 pXMLElement->GetTagName(wsTagName); |
| 203 if (wsTagName == FX_WSTRC(L"chunk")) { | 203 if (wsTagName == FX_WSTRC(L"chunk")) { |
| 204 pChunkElement = pXMLElement; | 204 pChunkElement = pXMLElement; |
| 205 break; | 205 break; |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 } | 208 } |
| 209 if (!pChunkElement) { | 209 if (!pChunkElement) { |
| 210 return FALSE; | 210 return false; |
| 211 } | 211 } |
| 212 CFX_WideString wsPDFContent; | 212 CFX_WideString wsPDFContent; |
| 213 pChunkElement->GetTextData(wsPDFContent); | 213 pChunkElement->GetTextData(wsPDFContent); |
| 214 iBufferSize = | 214 iBufferSize = |
| 215 Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), nullptr); | 215 Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), nullptr); |
| 216 pByteBuffer = FX_Alloc(uint8_t, iBufferSize + 1); | 216 pByteBuffer = FX_Alloc(uint8_t, iBufferSize + 1); |
| 217 pByteBuffer[iBufferSize] = '0'; // FIXME: I bet this is wrong. | 217 pByteBuffer[iBufferSize] = '0'; // FIXME: I bet this is wrong. |
| 218 Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), pByteBuffer); | 218 Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), pByteBuffer); |
| 219 return TRUE; | 219 return true; |
| 220 } | 220 } |
| 221 void XFA_XPDPacket_MergeRootNode(CXFA_Node* pOriginRoot, CXFA_Node* pNewRoot) { | 221 void XFA_XPDPacket_MergeRootNode(CXFA_Node* pOriginRoot, CXFA_Node* pNewRoot) { |
| 222 CXFA_Node* pChildNode = pNewRoot->GetNodeItem(XFA_NODEITEM_FirstChild); | 222 CXFA_Node* pChildNode = pNewRoot->GetNodeItem(XFA_NODEITEM_FirstChild); |
| 223 while (pChildNode) { | 223 while (pChildNode) { |
| 224 CXFA_Node* pOriginChild = | 224 CXFA_Node* pOriginChild = |
| 225 pOriginRoot->GetFirstChildByName(pChildNode->GetNameHash()); | 225 pOriginRoot->GetFirstChildByName(pChildNode->GetNameHash()); |
| 226 if (pOriginChild) { | 226 if (pOriginChild) { |
| 227 pChildNode = pChildNode->GetNodeItem(XFA_NODEITEM_NextSibling); | 227 pChildNode = pChildNode->GetNodeItem(XFA_NODEITEM_NextSibling); |
| 228 } else { | 228 } else { |
| 229 CXFA_Node* pNextSibling = | 229 CXFA_Node* pNextSibling = |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 return pair.second.get(); | 283 return pair.second.get(); |
| 284 } | 284 } |
| 285 return nullptr; | 285 return nullptr; |
| 286 } | 286 } |
| 287 | 287 |
| 288 CXFA_FFDocView* CXFA_FFDoc::GetDocView() { | 288 CXFA_FFDocView* CXFA_FFDoc::GetDocView() { |
| 289 auto it = m_TypeToDocViewMap.begin(); | 289 auto it = m_TypeToDocViewMap.begin(); |
| 290 return it != m_TypeToDocViewMap.end() ? it->second.get() : nullptr; | 290 return it != m_TypeToDocViewMap.end() ? it->second.get() : nullptr; |
| 291 } | 291 } |
| 292 | 292 |
| 293 FX_BOOL CXFA_FFDoc::OpenDoc(IFX_SeekableReadStream* pStream, | 293 bool CXFA_FFDoc::OpenDoc(IFX_SeekableReadStream* pStream, bool bTakeOverFile) { |
| 294 FX_BOOL bTakeOverFile) { | |
| 295 m_bOwnStream = bTakeOverFile; | 294 m_bOwnStream = bTakeOverFile; |
| 296 m_pStream = pStream; | 295 m_pStream = pStream; |
| 297 return TRUE; | 296 return true; |
| 298 } | 297 } |
| 299 FX_BOOL CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { | 298 bool CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { |
| 300 if (!pPDFDoc) | 299 if (!pPDFDoc) |
| 301 return FALSE; | 300 return false; |
| 302 | 301 |
| 303 CPDF_Dictionary* pRoot = pPDFDoc->GetRoot(); | 302 CPDF_Dictionary* pRoot = pPDFDoc->GetRoot(); |
| 304 if (!pRoot) | 303 if (!pRoot) |
| 305 return FALSE; | 304 return false; |
| 306 | 305 |
| 307 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); | 306 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); |
| 308 if (!pAcroForm) | 307 if (!pAcroForm) |
| 309 return FALSE; | 308 return false; |
| 310 | 309 |
| 311 CPDF_Object* pElementXFA = pAcroForm->GetDirectObjectFor("XFA"); | 310 CPDF_Object* pElementXFA = pAcroForm->GetDirectObjectFor("XFA"); |
| 312 if (!pElementXFA) | 311 if (!pElementXFA) |
| 313 return FALSE; | 312 return false; |
| 314 | 313 |
| 315 std::vector<CPDF_Stream*> xfaStreams; | 314 std::vector<CPDF_Stream*> xfaStreams; |
| 316 if (pElementXFA->IsArray()) { | 315 if (pElementXFA->IsArray()) { |
| 317 CPDF_Array* pXFAArray = (CPDF_Array*)pElementXFA; | 316 CPDF_Array* pXFAArray = (CPDF_Array*)pElementXFA; |
| 318 for (size_t i = 0; i < pXFAArray->GetCount() / 2; i++) { | 317 for (size_t i = 0; i < pXFAArray->GetCount() / 2; i++) { |
| 319 if (CPDF_Stream* pStream = pXFAArray->GetStreamAt(i * 2 + 1)) | 318 if (CPDF_Stream* pStream = pXFAArray->GetStreamAt(i * 2 + 1)) |
| 320 xfaStreams.push_back(pStream); | 319 xfaStreams.push_back(pStream); |
| 321 } | 320 } |
| 322 } else if (pElementXFA->IsStream()) { | 321 } else if (pElementXFA->IsStream()) { |
| 323 xfaStreams.push_back((CPDF_Stream*)pElementXFA); | 322 xfaStreams.push_back((CPDF_Stream*)pElementXFA); |
| 324 } | 323 } |
| 325 if (xfaStreams.empty()) | 324 if (xfaStreams.empty()) |
| 326 return FALSE; | 325 return false; |
| 327 | 326 |
| 328 IFX_SeekableReadStream* pFileRead = new CXFA_FileRead(xfaStreams); | 327 IFX_SeekableReadStream* pFileRead = new CXFA_FileRead(xfaStreams); |
| 329 m_pPDFDoc = pPDFDoc; | 328 m_pPDFDoc = pPDFDoc; |
| 330 if (m_pStream) { | 329 if (m_pStream) { |
| 331 m_pStream->Release(); | 330 m_pStream->Release(); |
| 332 m_pStream = nullptr; | 331 m_pStream = nullptr; |
| 333 } | 332 } |
| 334 m_pStream = pFileRead; | 333 m_pStream = pFileRead; |
| 335 m_bOwnStream = TRUE; | 334 m_bOwnStream = true; |
| 336 return TRUE; | 335 return true; |
| 337 } | 336 } |
| 338 | 337 |
| 339 FX_BOOL CXFA_FFDoc::CloseDoc() { | 338 bool CXFA_FFDoc::CloseDoc() { |
| 340 for (const auto& pair : m_TypeToDocViewMap) | 339 for (const auto& pair : m_TypeToDocViewMap) |
| 341 pair.second->RunDocClose(); | 340 pair.second->RunDocClose(); |
| 342 | 341 |
| 343 CXFA_Document* doc = | 342 CXFA_Document* doc = |
| 344 m_pDocumentParser ? m_pDocumentParser->GetDocument() : nullptr; | 343 m_pDocumentParser ? m_pDocumentParser->GetDocument() : nullptr; |
| 345 if (doc) | 344 if (doc) |
| 346 doc->ClearLayoutData(); | 345 doc->ClearLayoutData(); |
| 347 | 346 |
| 348 m_TypeToDocViewMap.clear(); | 347 m_TypeToDocViewMap.clear(); |
| 349 | 348 |
| 350 m_pNotify.reset(nullptr); | 349 m_pNotify.reset(nullptr); |
| 351 m_pApp->GetXFAFontMgr()->ReleaseDocFonts(this); | 350 m_pApp->GetXFAFontMgr()->ReleaseDocFonts(this); |
| 352 | 351 |
| 353 if (m_dwDocType != XFA_DOCTYPE_XDP && m_pStream && m_bOwnStream) { | 352 if (m_dwDocType != XFA_DOCTYPE_XDP && m_pStream && m_bOwnStream) { |
| 354 m_pStream->Release(); | 353 m_pStream->Release(); |
| 355 m_pStream = nullptr; | 354 m_pStream = nullptr; |
| 356 } | 355 } |
| 357 | 356 |
| 358 for (const auto& pair : m_HashToDibDpiMap) | 357 for (const auto& pair : m_HashToDibDpiMap) |
| 359 delete pair.second.pDibSource; | 358 delete pair.second.pDibSource; |
| 360 | 359 |
| 361 m_HashToDibDpiMap.clear(); | 360 m_HashToDibDpiMap.clear(); |
| 362 m_pApp->ClearEventTargets(); | 361 m_pApp->ClearEventTargets(); |
| 363 return TRUE; | 362 return true; |
| 364 } | 363 } |
| 365 void CXFA_FFDoc::SetDocType(uint32_t dwType) { | 364 void CXFA_FFDoc::SetDocType(uint32_t dwType) { |
| 366 m_dwDocType = dwType; | 365 m_dwDocType = dwType; |
| 367 } | 366 } |
| 368 CPDF_Document* CXFA_FFDoc::GetPDFDoc() { | 367 CPDF_Document* CXFA_FFDoc::GetPDFDoc() { |
| 369 return m_pPDFDoc; | 368 return m_pPDFDoc; |
| 370 } | 369 } |
| 371 | 370 |
| 372 CFX_DIBitmap* CXFA_FFDoc::GetPDFNamedImage(const CFX_WideStringC& wsName, | 371 CFX_DIBitmap* CXFA_FFDoc::GetPDFNamedImage(const CFX_WideStringC& wsName, |
| 373 int32_t& iImageXDpi, | 372 int32_t& iImageXDpi, |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 return !!pExport->Export(pFile); | 437 return !!pExport->Export(pFile); |
| 439 | 438 |
| 440 CFX_ByteString bsChecksum; | 439 CFX_ByteString bsChecksum; |
| 441 if (pCSContext) | 440 if (pCSContext) |
| 442 bsChecksum = pCSContext->GetChecksum(); | 441 bsChecksum = pCSContext->GetChecksum(); |
| 443 | 442 |
| 444 return !!pExport->Export( | 443 return !!pExport->Export( |
| 445 pFile, pNode, 0, bsChecksum.GetLength() ? bsChecksum.c_str() : nullptr); | 444 pFile, pNode, 0, bsChecksum.GetLength() ? bsChecksum.c_str() : nullptr); |
| 446 } | 445 } |
| 447 | 446 |
| 448 FX_BOOL CXFA_FFDoc::ImportData(IFX_SeekableReadStream* pStream, FX_BOOL bXDP) { | 447 bool CXFA_FFDoc::ImportData(IFX_SeekableReadStream* pStream, bool bXDP) { |
| 449 std::unique_ptr<CXFA_DataImporter> importer( | 448 std::unique_ptr<CXFA_DataImporter> importer( |
| 450 new CXFA_DataImporter(m_pDocumentParser->GetDocument())); | 449 new CXFA_DataImporter(m_pDocumentParser->GetDocument())); |
| 451 return importer->ImportData(pStream); | 450 return importer->ImportData(pStream); |
| 452 } | 451 } |
| OLD | NEW |