Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 7 | #include "xfa/fxfa/cxfa_ffdoc.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 8 | |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 9 | #include <algorithm> |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <vector> |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 12 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 14 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 15 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 16 | #include "core/fpdfdoc/cpdf_nametree.h" |
Dan Sinclair | 283a043 | 2017-04-20 14:11:21 -0400 | [diff] [blame] | 17 | #include "core/fxcrt/cfx_memorystream.h" |
Ryan Harrison | ccf206a | 2017-11-14 16:05:53 +0000 | [diff] [blame] | 18 | #include "core/fxcrt/cfx_seekablemultistream.h" |
Dan Sinclair | cfb1944 | 2017-04-20 13:13:04 -0400 | [diff] [blame] | 19 | #include "core/fxcrt/fx_extension.h" |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 20 | #include "core/fxcrt/fx_memory.h" |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 21 | #include "core/fxcrt/xml/cfx_xmlelement.h" |
| 22 | #include "core/fxcrt/xml/cfx_xmlnode.h" |
Lei Zhang | d859d57 | 2018-01-17 20:44:26 +0000 | [diff] [blame] | 23 | #include "fxjs/xfa/cjx_object.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 24 | #include "third_party/base/ptr_util.h" |
dsinclair | 447b1f3 | 2016-12-08 10:06:32 -0800 | [diff] [blame] | 25 | #include "xfa/fwl/cfwl_notedriver.h" |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 26 | #include "xfa/fxfa/cxfa_ffapp.h" |
| 27 | #include "xfa/fxfa/cxfa_ffdocview.h" |
Dan Sinclair | 24ef633 | 2017-07-24 10:52:57 -0400 | [diff] [blame] | 28 | #include "xfa/fxfa/cxfa_ffnotify.h" |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 29 | #include "xfa/fxfa/cxfa_ffwidget.h" |
| 30 | #include "xfa/fxfa/cxfa_fontmgr.h" |
Dan Sinclair | df4f30e | 2017-12-14 20:51:03 +0000 | [diff] [blame] | 31 | #include "xfa/fxfa/parser/cxfa_acrobat.h" |
| 32 | #include "xfa/fxfa/parser/cxfa_acrobat7.h" |
dsinclair | 3a7cc73 | 2016-07-21 12:04:34 -0700 | [diff] [blame] | 33 | #include "xfa/fxfa/parser/cxfa_dataexporter.h" |
| 34 | #include "xfa/fxfa/parser/cxfa_dataimporter.h" |
dsinclair | 1628024 | 2016-07-21 12:03:47 -0700 | [diff] [blame] | 35 | #include "xfa/fxfa/parser/cxfa_document.h" |
Dan Sinclair | 876efaa | 2018-04-12 13:39:28 +0000 | [diff] [blame] | 36 | #include "xfa/fxfa/parser/cxfa_document_parser.h" |
Dan Sinclair | df4f30e | 2017-12-14 20:51:03 +0000 | [diff] [blame] | 37 | #include "xfa/fxfa/parser/cxfa_dynamicrender.h" |
Dan Sinclair | efcae5d | 2017-03-29 14:47:46 -0400 | [diff] [blame] | 38 | #include "xfa/fxfa/parser/cxfa_node.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 39 | |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | struct FX_BASE64DATA { |
| 43 | uint32_t data1 : 2; |
| 44 | uint32_t data2 : 6; |
| 45 | uint32_t data3 : 4; |
| 46 | uint32_t data4 : 4; |
| 47 | uint32_t data5 : 6; |
| 48 | uint32_t data6 : 2; |
| 49 | uint32_t data7 : 8; |
| 50 | }; |
| 51 | |
| 52 | const uint8_t kStartValuesRemoved = 43; |
| 53 | const uint8_t kDecoderMapSize = 80; |
| 54 | const uint8_t g_FXBase64DecoderMap[kDecoderMapSize] = { |
| 55 | 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, |
| 56 | 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, |
| 57 | 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, |
| 58 | 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, |
| 59 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, |
| 60 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, |
| 61 | 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, |
| 62 | }; |
| 63 | |
| 64 | uint8_t base64DecoderValue(uint8_t val) { |
| 65 | if (val < kStartValuesRemoved || val >= kStartValuesRemoved + kDecoderMapSize) |
| 66 | return 0xFF; |
| 67 | return g_FXBase64DecoderMap[val - kStartValuesRemoved]; |
| 68 | } |
| 69 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 70 | void Base64DecodePiece(const char src[4], |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 71 | int32_t iChars, |
| 72 | FX_BASE64DATA& dst, |
| 73 | int32_t& iBytes) { |
| 74 | ASSERT(iChars > 0 && iChars < 5); |
| 75 | iBytes = 1; |
| 76 | dst.data2 = base64DecoderValue(static_cast<uint8_t>(src[0])); |
| 77 | if (iChars > 1) { |
| 78 | uint8_t b = base64DecoderValue(static_cast<uint8_t>(src[1])); |
| 79 | dst.data1 = b >> 4; |
| 80 | dst.data4 = b; |
| 81 | if (iChars > 2) { |
| 82 | iBytes = 2; |
| 83 | b = base64DecoderValue(static_cast<uint8_t>(src[2])); |
| 84 | dst.data3 = b >> 2; |
| 85 | dst.data6 = b; |
| 86 | if (iChars > 3) { |
| 87 | iBytes = 3; |
| 88 | dst.data5 = base64DecoderValue(static_cast<uint8_t>(src[3])); |
| 89 | } else { |
| 90 | dst.data5 = 0; |
| 91 | } |
| 92 | } else { |
| 93 | dst.data3 = 0; |
| 94 | } |
| 95 | } else { |
| 96 | dst.data1 = 0; |
| 97 | } |
| 98 | } |
| 99 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 100 | int32_t Base64DecodeW(const wchar_t* pSrc, int32_t iSrcLen, uint8_t* pDst) { |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 101 | ASSERT(pSrc); |
| 102 | if (iSrcLen < 1) { |
| 103 | return 0; |
| 104 | } |
| 105 | while (iSrcLen > 0 && pSrc[iSrcLen - 1] == '=') { |
| 106 | iSrcLen--; |
| 107 | } |
| 108 | if (iSrcLen < 1) { |
| 109 | return 0; |
| 110 | } |
| 111 | if (!pDst) { |
| 112 | int32_t iDstLen = iSrcLen / 4 * 3; |
| 113 | iSrcLen %= 4; |
| 114 | if (iSrcLen == 1) { |
| 115 | iDstLen += 1; |
| 116 | } else if (iSrcLen == 2) { |
| 117 | iDstLen += 1; |
| 118 | } else if (iSrcLen == 3) { |
| 119 | iDstLen += 2; |
| 120 | } |
| 121 | return iDstLen; |
| 122 | } |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 123 | char srcData[4]; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 124 | FX_BASE64DATA dstData; |
| 125 | int32_t iChars = 4, iBytes; |
| 126 | uint8_t* pDstEnd = pDst; |
| 127 | while (iSrcLen > 0) { |
| 128 | if (iSrcLen > 3) { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 129 | srcData[0] = (char)*pSrc++; |
| 130 | srcData[1] = (char)*pSrc++; |
| 131 | srcData[2] = (char)*pSrc++; |
| 132 | srcData[3] = (char)*pSrc++; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 133 | iSrcLen -= 4; |
| 134 | } else { |
| 135 | *((uint32_t*)&dstData) = 0; |
| 136 | *((uint32_t*)srcData) = 0; |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 137 | srcData[0] = (char)*pSrc++; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 138 | if (iSrcLen > 1) { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 139 | srcData[1] = (char)*pSrc++; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 140 | } |
| 141 | if (iSrcLen > 2) { |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 142 | srcData[2] = (char)*pSrc++; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 143 | } |
| 144 | iChars = iSrcLen; |
| 145 | iSrcLen = 0; |
| 146 | } |
| 147 | Base64DecodePiece(srcData, iChars, dstData, iBytes); |
| 148 | *pDstEnd++ = ((uint8_t*)&dstData)[0]; |
| 149 | if (iBytes > 1) { |
| 150 | *pDstEnd++ = ((uint8_t*)&dstData)[1]; |
| 151 | } |
| 152 | if (iBytes > 2) { |
| 153 | *pDstEnd++ = ((uint8_t*)&dstData)[2]; |
| 154 | } |
| 155 | } |
| 156 | return pDstEnd - pDst; |
| 157 | } |
| 158 | |
| 159 | } // namespace |
| 160 | |
dsinclair | a440bb3 | 2016-09-14 07:01:54 -0700 | [diff] [blame] | 161 | CXFA_FFDoc::CXFA_FFDoc(CXFA_FFApp* pApp, IXFA_DocEnvironment* pDocEnvironment) |
Lei Zhang | 8509d3e | 2017-12-04 06:13:55 +0000 | [diff] [blame] | 162 | : m_pDocEnvironment(pDocEnvironment), m_pApp(pApp) {} |
dsinclair | a1b0772 | 2016-07-11 08:20:58 -0700 | [diff] [blame] | 163 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 164 | CXFA_FFDoc::~CXFA_FFDoc() { |
| 165 | CloseDoc(); |
| 166 | } |
dsinclair | a1b0772 | 2016-07-11 08:20:58 -0700 | [diff] [blame] | 167 | |
Dan Sinclair | 62110c9 | 2018-04-12 13:40:49 +0000 | [diff] [blame^] | 168 | bool CXFA_FFDoc::ParseDoc(CPDF_Object* pElementXFA) { |
| 169 | std::vector<CPDF_Stream*> xfaStreams; |
| 170 | if (pElementXFA->IsArray()) { |
| 171 | CPDF_Array* pXFAArray = (CPDF_Array*)pElementXFA; |
| 172 | for (size_t i = 0; i < pXFAArray->GetCount() / 2; i++) { |
| 173 | if (CPDF_Stream* pStream = pXFAArray->GetStreamAt(i * 2 + 1)) |
| 174 | xfaStreams.push_back(pStream); |
| 175 | } |
| 176 | } else if (pElementXFA->IsStream()) { |
| 177 | xfaStreams.push_back(pElementXFA->AsStream()); |
| 178 | } |
| 179 | if (xfaStreams.empty()) |
Dan Sinclair | 2efb11c | 2018-04-12 13:21:48 +0000 | [diff] [blame] | 180 | return false; |
| 181 | |
Dan Sinclair | 62110c9 | 2018-04-12 13:40:49 +0000 | [diff] [blame^] | 182 | auto stream = pdfium::MakeRetain<CFX_SeekableMultiStream>(xfaStreams); |
Dan Sinclair | 0cad116 | 2018-04-12 13:23:08 +0000 | [diff] [blame] | 183 | |
Dan Sinclair | 0cad116 | 2018-04-12 13:23:08 +0000 | [diff] [blame] | 184 | // Note, we don't pass the document into the constructor as currently that |
| 185 | // triggers different behaviour in the parser. |
Dan Sinclair | 876efaa | 2018-04-12 13:39:28 +0000 | [diff] [blame] | 186 | CXFA_DocumentParser parser; |
Dan Sinclair | 0cad116 | 2018-04-12 13:23:08 +0000 | [diff] [blame] | 187 | parser.SetFactory(m_pDocument.get()); |
Dan Sinclair | 62110c9 | 2018-04-12 13:40:49 +0000 | [diff] [blame^] | 188 | if (!parser.Parse(stream, XFA_PacketType::Xdp)) |
Dan Sinclair | 0cad116 | 2018-04-12 13:23:08 +0000 | [diff] [blame] | 189 | return false; |
| 190 | |
| 191 | m_pXMLRoot = parser.GetXMLRoot(); |
| 192 | m_pDocument->SetRoot(parser.GetRootNode()); |
| 193 | return true; |
| 194 | } |
| 195 | |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 196 | bool XFA_GetPDFContentsFromPDFXML(CFX_XMLNode* pPDFElement, |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 197 | uint8_t*& pByteBuffer, |
| 198 | int32_t& iBufferSize) { |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 199 | CFX_XMLElement* pDocumentElement = nullptr; |
Dan Sinclair | fa3765c | 2018-02-13 21:44:33 +0000 | [diff] [blame] | 200 | for (CFX_XMLNode* pXMLNode = pPDFElement->GetFirstChild(); pXMLNode; |
| 201 | pXMLNode = pXMLNode->GetNextSibling()) { |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 202 | if (pXMLNode->GetType() == FX_XMLNODE_Element) { |
| 203 | CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 204 | WideString wsTagName = pXMLElement->GetName(); |
dan sinclair | 65c7c23 | 2017-02-02 14:05:30 -0800 | [diff] [blame] | 205 | if (wsTagName == L"document") { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 206 | pDocumentElement = pXMLElement; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | if (!pDocumentElement) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 212 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 213 | } |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 214 | CFX_XMLElement* pChunkElement = nullptr; |
Dan Sinclair | fa3765c | 2018-02-13 21:44:33 +0000 | [diff] [blame] | 215 | for (CFX_XMLNode* pXMLNode = pDocumentElement->GetFirstChild(); pXMLNode; |
| 216 | pXMLNode = pXMLNode->GetNextSibling()) { |
Dan Sinclair | 0d86ecb | 2017-04-19 09:19:57 -0400 | [diff] [blame] | 217 | if (pXMLNode->GetType() == FX_XMLNODE_Element) { |
| 218 | CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 219 | WideString wsTagName = pXMLElement->GetName(); |
dan sinclair | 65c7c23 | 2017-02-02 14:05:30 -0800 | [diff] [blame] | 220 | if (wsTagName == L"chunk") { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 221 | pChunkElement = pXMLElement; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | if (!pChunkElement) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 227 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 228 | } |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 229 | WideString wsPDFContent = pChunkElement->GetTextData(); |
tsepez | bd9748d | 2016-04-13 21:40:19 -0700 | [diff] [blame] | 230 | iBufferSize = |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 231 | Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 232 | pByteBuffer = FX_Alloc(uint8_t, iBufferSize + 1); |
| 233 | pByteBuffer[iBufferSize] = '0'; // FIXME: I bet this is wrong. |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 234 | Base64DecodeW(wsPDFContent.c_str(), wsPDFContent.GetLength(), pByteBuffer); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 235 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 236 | } |
| 237 | void XFA_XPDPacket_MergeRootNode(CXFA_Node* pOriginRoot, CXFA_Node* pNewRoot) { |
Dan Sinclair | 18a6069 | 2018-01-10 16:30:56 +0000 | [diff] [blame] | 238 | CXFA_Node* pChildNode = pNewRoot->GetFirstChild(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 239 | while (pChildNode) { |
| 240 | CXFA_Node* pOriginChild = |
| 241 | pOriginRoot->GetFirstChildByName(pChildNode->GetNameHash()); |
| 242 | if (pOriginChild) { |
Dan Sinclair | 18a6069 | 2018-01-10 16:30:56 +0000 | [diff] [blame] | 243 | pChildNode = pChildNode->GetNextSibling(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 244 | } else { |
Dan Sinclair | 18a6069 | 2018-01-10 16:30:56 +0000 | [diff] [blame] | 245 | CXFA_Node* pNextSibling = pChildNode->GetNextSibling(); |
Dan Sinclair | 185832c | 2017-11-04 03:31:29 +0000 | [diff] [blame] | 246 | pNewRoot->RemoveChild(pChildNode, true); |
| 247 | pOriginRoot->InsertChild(pChildNode, nullptr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 248 | pChildNode = pNextSibling; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 249 | pNextSibling = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
dsinclair | 89f8fa8 | 2016-09-14 06:11:08 -0700 | [diff] [blame] | 253 | |
Dan Sinclair | 3d06022 | 2017-03-22 15:29:20 -0400 | [diff] [blame] | 254 | CXFA_FFDocView* CXFA_FFDoc::CreateDocView() { |
| 255 | if (!m_DocView) |
| 256 | m_DocView = pdfium::MakeUnique<CXFA_FFDocView>(this); |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 257 | |
Dan Sinclair | 3d06022 | 2017-03-22 15:29:20 -0400 | [diff] [blame] | 258 | return m_DocView.get(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 259 | } |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 260 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 261 | CXFA_FFDocView* CXFA_FFDoc::GetDocView(CXFA_LayoutProcessor* pLayout) { |
Dan Sinclair | 3d06022 | 2017-03-22 15:29:20 -0400 | [diff] [blame] | 262 | return m_DocView && m_DocView->GetXFALayout() == pLayout ? m_DocView.get() |
| 263 | : nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 264 | } |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 265 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 266 | CXFA_FFDocView* CXFA_FFDoc::GetDocView() { |
Dan Sinclair | 3d06022 | 2017-03-22 15:29:20 -0400 | [diff] [blame] | 267 | return m_DocView.get(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 268 | } |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 269 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 270 | bool CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 271 | if (!pPDFDoc) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 272 | return false; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 273 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 274 | const CPDF_Dictionary* pRoot = pPDFDoc->GetRoot(); |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 275 | if (!pRoot) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 276 | return false; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 277 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 278 | CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 279 | if (!pAcroForm) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 280 | return false; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 281 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 282 | CPDF_Object* pElementXFA = pAcroForm->GetDirectObjectFor("XFA"); |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 283 | if (!pElementXFA) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 284 | return false; |
dsinclair | fc2cdf8 | 2016-05-19 18:07:11 -0700 | [diff] [blame] | 285 | |
Dan Sinclair | 62110c9 | 2018-04-12 13:40:49 +0000 | [diff] [blame^] | 286 | m_pPDFDoc = pPDFDoc; |
| 287 | |
| 288 | m_pNotify = pdfium::MakeUnique<CXFA_FFNotify>(this); |
| 289 | m_pDocument = pdfium::MakeUnique<CXFA_Document>(m_pNotify.get()); |
| 290 | if (!ParseDoc(pElementXFA)) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 291 | return false; |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 292 | |
Dan Sinclair | 62110c9 | 2018-04-12 13:40:49 +0000 | [diff] [blame^] | 293 | // At this point we've got an XFA document and we want to always return |
| 294 | // true to signify the load succeeded. |
| 295 | |
| 296 | m_pPDFFontMgr = pdfium::MakeUnique<CFGAS_PDFFontMgr>( |
| 297 | GetPDFDoc(), GetApp()->GetFDEFontMgr()); |
| 298 | |
| 299 | m_FormType = FormType::kXFAForeground; |
| 300 | CXFA_Node* pConfig = ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Config)); |
| 301 | if (!pConfig) |
| 302 | return true; |
| 303 | |
| 304 | CXFA_Acrobat* pAcrobat = |
| 305 | pConfig->GetFirstChildByClass<CXFA_Acrobat>(XFA_Element::Acrobat); |
| 306 | if (!pAcrobat) |
| 307 | return true; |
| 308 | |
| 309 | CXFA_Acrobat7* pAcrobat7 = |
| 310 | pAcrobat->GetFirstChildByClass<CXFA_Acrobat7>(XFA_Element::Acrobat7); |
| 311 | if (!pAcrobat7) |
| 312 | return true; |
| 313 | |
| 314 | CXFA_DynamicRender* pDynamicRender = |
| 315 | pAcrobat7->GetFirstChildByClass<CXFA_DynamicRender>( |
| 316 | XFA_Element::DynamicRender); |
| 317 | if (!pDynamicRender) |
| 318 | return true; |
| 319 | |
| 320 | WideString wsType = pDynamicRender->JSObject()->GetContent(false); |
| 321 | if (wsType == L"required") |
| 322 | m_FormType = FormType::kXFAFull; |
| 323 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 324 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 325 | } |
dsinclair | a1b0772 | 2016-07-11 08:20:58 -0700 | [diff] [blame] | 326 | |
Tom Sepez | cc20513 | 2017-05-16 14:01:47 -0700 | [diff] [blame] | 327 | void CXFA_FFDoc::CloseDoc() { |
| 328 | if (m_DocView) { |
Dan Sinclair | 3d06022 | 2017-03-22 15:29:20 -0400 | [diff] [blame] | 329 | m_DocView->RunDocClose(); |
Tom Sepez | cc20513 | 2017-05-16 14:01:47 -0700 | [diff] [blame] | 330 | m_DocView.reset(); |
| 331 | } |
Dan Sinclair | 0cad116 | 2018-04-12 13:23:08 +0000 | [diff] [blame] | 332 | if (m_pDocument) { |
| 333 | m_pDocument->ReleaseXMLNodesIfNeeded(); |
| 334 | m_pDocument->ClearLayoutData(); |
| 335 | } |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 336 | |
Dan Sinclair | 0cad116 | 2018-04-12 13:23:08 +0000 | [diff] [blame] | 337 | m_pDocument.reset(); |
| 338 | m_pXMLRoot.reset(); |
Tom Sepez | cc20513 | 2017-05-16 14:01:47 -0700 | [diff] [blame] | 339 | m_pNotify.reset(); |
Dan Sinclair | a44a480 | 2017-09-21 10:45:18 -0400 | [diff] [blame] | 340 | m_pPDFFontMgr.reset(); |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 341 | m_HashToDibDpiMap.clear(); |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 342 | m_pApp->ClearEventTargets(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 343 | } |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 344 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 345 | RetainPtr<CFX_DIBitmap> CXFA_FFDoc::GetPDFNamedImage( |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 346 | const WideStringView& wsName, |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 347 | int32_t& iImageXDpi, |
| 348 | int32_t& iImageYDpi) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 349 | if (!m_pPDFDoc) |
| 350 | return nullptr; |
| 351 | |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 352 | uint32_t dwHash = FX_HashCode_GetW(wsName, false); |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 353 | auto it = m_HashToDibDpiMap.find(dwHash); |
| 354 | if (it != m_HashToDibDpiMap.end()) { |
| 355 | iImageXDpi = it->second.iImageXDpi; |
| 356 | iImageYDpi = it->second.iImageYDpi; |
Tom Sepez | f0799fe | 2017-03-28 09:31:32 -0700 | [diff] [blame] | 357 | return it->second.pDibSource.As<CFX_DIBitmap>(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 360 | const CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 361 | if (!pRoot) |
| 362 | return nullptr; |
| 363 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 364 | CPDF_Dictionary* pNames = pRoot->GetDictFor("Names"); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 365 | if (!pNames) |
| 366 | return nullptr; |
| 367 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 368 | CPDF_Dictionary* pXFAImages = pNames->GetDictFor("XFAImages"); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 369 | if (!pXFAImages) |
| 370 | return nullptr; |
| 371 | |
| 372 | CPDF_NameTree nametree(pXFAImages); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 373 | CPDF_Object* pObject = nametree.LookupValue(WideString(wsName)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 374 | if (!pObject) { |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 375 | for (size_t i = 0; i < nametree.GetCount(); i++) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 376 | WideString wsTemp; |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 377 | CPDF_Object* pTempObject = nametree.LookupValueAndName(i, &wsTemp); |
| 378 | if (wsTemp == wsName) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 379 | pObject = pTempObject; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 385 | CPDF_Stream* pStream = ToStream(pObject); |
| 386 | if (!pStream) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 387 | return nullptr; |
| 388 | |
Tom Sepez | afd0d1f | 2017-04-04 14:37:18 -0700 | [diff] [blame] | 389 | auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream); |
Lei Zhang | 07401ba | 2017-12-11 22:12:08 +0000 | [diff] [blame] | 390 | pAcc->LoadAllDataFiltered(); |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 391 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 392 | RetainPtr<IFX_SeekableStream> pImageFileRead = |
Dan Sinclair | 283a043 | 2017-04-20 14:11:21 -0400 | [diff] [blame] | 393 | pdfium::MakeRetain<CFX_MemoryStream>( |
| 394 | const_cast<uint8_t*>(pAcc->GetData()), pAcc->GetSize(), false); |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 395 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 396 | RetainPtr<CFX_DIBitmap> pDibSource = XFA_LoadImageFromBuffer( |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 397 | pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi); |
| 398 | m_HashToDibDpiMap[dwHash] = {pDibSource, iImageXDpi, iImageYDpi}; |
tsepez | 084174a | 2016-04-25 12:37:27 -0700 | [diff] [blame] | 399 | return pDibSource; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Dan Sinclair | f96dfed | 2017-11-08 19:57:00 +0000 | [diff] [blame] | 402 | bool CXFA_FFDoc::SavePackage(CXFA_Node* pNode, |
Dan Sinclair | e1a41af | 2018-04-12 13:13:43 +0000 | [diff] [blame] | 403 | const RetainPtr<IFX_SeekableStream>& pFile) { |
| 404 | ASSERT(pNode || GetXFADoc()->GetRoot()); |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 405 | |
Dan Sinclair | e1a41af | 2018-04-12 13:13:43 +0000 | [diff] [blame] | 406 | CXFA_DataExporter exporter; |
| 407 | return exporter.Export(pFile, pNode ? pNode : GetXFADoc()->GetRoot()); |
tsepez | 3857904 | 2016-05-16 17:05:38 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 410 | bool CXFA_FFDoc::ImportData(const RetainPtr<IFX_SeekableStream>& pStream, |
Dan Sinclair | 4fcdf05 | 2017-04-18 11:55:27 -0400 | [diff] [blame] | 411 | bool bXDP) { |
Dan Sinclair | 0cad116 | 2018-04-12 13:23:08 +0000 | [diff] [blame] | 412 | auto importer = pdfium::MakeUnique<CXFA_DataImporter>(m_pDocument.get()); |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 413 | return importer->ImportData(pStream); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 414 | } |