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/fpdf_edit/editint.h" | 7 #include "core/fpdfapi/fpdf_edit/editint.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "core/fpdfapi/fpdf_edit/include/cpdf_creator.h" | 11 #include "core/fpdfapi/fpdf_edit/include/cpdf_creator.h" |
12 #include "core/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" | 12 #include "core/fpdfapi/fpdf_parser/cpdf_crypto_handler.h" |
13 #include "core/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h" | 13 #include "core/fpdfapi/fpdf_parser/cpdf_security_handler.h" |
14 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 14 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
15 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | 15 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
16 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 16 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
17 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" | 17 #include "core/fpdfapi/fpdf_parser/include/cpdf_parser.h" |
18 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" | 18 #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" |
19 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" | 19 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
20 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" | 20 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" |
21 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" | 21 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" |
22 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" | 22 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
23 #include "core/fpdfapi/fpdf_parser/ipdf_crypto_handler.h" | |
24 #include "core/fxcrt/include/fx_ext.h" | 23 #include "core/fxcrt/include/fx_ext.h" |
25 #include "third_party/base/stl_util.h" | 24 #include "third_party/base/stl_util.h" |
26 | 25 |
27 #define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024) | 26 #define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024) |
28 #define PDF_XREFSTREAM_MAXSIZE 10000 | 27 #define PDF_XREFSTREAM_MAXSIZE 10000 |
29 | 28 |
30 // TODO(ochang): Make helper for appending "objnum 0 R ". | 29 // TODO(ochang): Make helper for appending "objnum 0 R ". |
31 | 30 |
32 namespace { | 31 namespace { |
33 | 32 |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
472 m_pDict->Release(); | 471 m_pDict->Release(); |
473 } | 472 } |
474 if (m_bNewData) { | 473 if (m_bNewData) { |
475 FX_Free(m_pData); | 474 FX_Free(m_pData); |
476 } | 475 } |
477 } | 476 } |
478 class CPDF_Encryptor { | 477 class CPDF_Encryptor { |
479 public: | 478 public: |
480 CPDF_Encryptor(); | 479 CPDF_Encryptor(); |
481 ~CPDF_Encryptor(); | 480 ~CPDF_Encryptor(); |
482 FX_BOOL Initialize(IPDF_CryptoHandler* pHandler, | 481 FX_BOOL Initialize(CPDF_CryptoHandler* pHandler, |
483 int objnum, | 482 int objnum, |
484 uint8_t* src_data, | 483 uint8_t* src_data, |
485 uint32_t src_size); | 484 uint32_t src_size); |
486 uint8_t* m_pData; | 485 uint8_t* m_pData; |
487 uint32_t m_dwSize; | 486 uint32_t m_dwSize; |
488 FX_BOOL m_bNewBuf; | 487 FX_BOOL m_bNewBuf; |
489 }; | 488 }; |
490 CPDF_Encryptor::CPDF_Encryptor() { | 489 CPDF_Encryptor::CPDF_Encryptor() { |
491 m_pData = NULL; | 490 m_pData = NULL; |
492 m_dwSize = 0; | 491 m_dwSize = 0; |
493 m_bNewBuf = FALSE; | 492 m_bNewBuf = FALSE; |
494 } | 493 } |
495 FX_BOOL CPDF_Encryptor::Initialize(IPDF_CryptoHandler* pHandler, | 494 FX_BOOL CPDF_Encryptor::Initialize(CPDF_CryptoHandler* pHandler, |
Tom Sepez
2016/04/19 17:35:43
Always returns true, should be void, or do work in
| |
496 int objnum, | 495 int objnum, |
497 uint8_t* src_data, | 496 uint8_t* src_data, |
498 uint32_t src_size) { | 497 uint32_t src_size) { |
499 if (src_size == 0) { | 498 if (src_size == 0) { |
500 return TRUE; | 499 return TRUE; |
501 } | 500 } |
502 if (!pHandler) { | 501 if (!pHandler) { |
503 m_pData = (uint8_t*)src_data; | 502 m_pData = (uint8_t*)src_data; |
504 m_dwSize = src_size; | 503 m_dwSize = src_size; |
505 m_bNewBuf = FALSE; | 504 m_bNewBuf = FALSE; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
542 m_OffsetArray.Add(m_Buffer.GetLength()); | 541 m_OffsetArray.Add(m_Buffer.GetLength()); |
543 m_Buffer.AppendBlock(pBuffer, dwSize); | 542 m_Buffer.AppendBlock(pBuffer, dwSize); |
544 return 1; | 543 return 1; |
545 } | 544 } |
546 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { | 545 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { |
547 FXSYS_assert(pCreator); | 546 FXSYS_assert(pCreator); |
548 if (m_ObjNumArray.GetSize() == 0) { | 547 if (m_ObjNumArray.GetSize() == 0) { |
549 return 0; | 548 return 0; |
550 } | 549 } |
551 CFX_FileBufferArchive* pFile = &pCreator->m_File; | 550 CFX_FileBufferArchive* pFile = &pCreator->m_File; |
552 IPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; | 551 CPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; |
553 FX_FILESIZE ObjOffset = pCreator->m_Offset; | 552 FX_FILESIZE ObjOffset = pCreator->m_Offset; |
554 if (!m_dwObjNum) { | 553 if (!m_dwObjNum) { |
555 m_dwObjNum = ++pCreator->m_dwLastObjNum; | 554 m_dwObjNum = ++pCreator->m_dwLastObjNum; |
556 } | 555 } |
557 CFX_ByteTextBuf tempBuffer; | 556 CFX_ByteTextBuf tempBuffer; |
558 int32_t iCount = m_ObjNumArray.GetSize(); | 557 int32_t iCount = m_ObjNumArray.GetSize(); |
559 for (int32_t i = 0; i < iCount; i++) { | 558 for (int32_t i = 0; i < iCount; i++) { |
560 tempBuffer << m_ObjNumArray.ElementAt(i) << " " | 559 tempBuffer << m_ObjNumArray.ElementAt(i) << " " |
561 << m_OffsetArray.ElementAt(i) << " "; | 560 << m_OffsetArray.ElementAt(i) << " "; |
562 } | 561 } |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1018 if (!m_pXRefStream->End(this)) { | 1017 if (!m_pXRefStream->End(this)) { |
1019 return -1; | 1018 return -1; |
1020 } | 1019 } |
1021 if (!m_pXRefStream->Start()) { | 1020 if (!m_pXRefStream->Start()) { |
1022 return -1; | 1021 return -1; |
1023 } | 1022 } |
1024 return 0; | 1023 return 0; |
1025 } | 1024 } |
1026 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, | 1025 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, |
1027 uint32_t objnum, | 1026 uint32_t objnum, |
1028 IPDF_CryptoHandler* pCrypto) { | 1027 CPDF_CryptoHandler* pCrypto) { |
1029 CPDF_FlateEncoder encoder; | 1028 CPDF_FlateEncoder encoder; |
1030 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), | 1029 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), |
1031 pStream == m_pMetadata ? FALSE : m_bCompress); | 1030 pStream == m_pMetadata ? FALSE : m_bCompress); |
1032 CPDF_Encryptor encryptor; | 1031 CPDF_Encryptor encryptor; |
1033 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, | 1032 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, |
1034 encoder.m_dwSize)) { | 1033 encoder.m_dwSize)) { |
1035 return -1; | 1034 return -1; |
1036 } | 1035 } |
1037 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { | 1036 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { |
1038 encoder.CloneDict(); | 1037 encoder.CloneDict(); |
(...skipping 22 matching lines...) Expand all Loading... | |
1061 int32_t len = m_File.AppendDWord(objnum); | 1060 int32_t len = m_File.AppendDWord(objnum); |
1062 if (len < 0) | 1061 if (len < 0) |
1063 return -1; | 1062 return -1; |
1064 | 1063 |
1065 m_Offset += len; | 1064 m_Offset += len; |
1066 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) | 1065 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) |
1067 return -1; | 1066 return -1; |
1068 | 1067 |
1069 m_Offset += len; | 1068 m_Offset += len; |
1070 if (pObj->IsStream()) { | 1069 if (pObj->IsStream()) { |
1071 IPDF_CryptoHandler* pHandler = nullptr; | 1070 CPDF_CryptoHandler* pHandler = nullptr; |
Tom Sepez
2016/04/19 17:35:43
nit: combine with next line.
| |
1072 pHandler = | 1071 pHandler = |
1073 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; | 1072 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; |
1074 if (WriteStream(pObj, objnum, pHandler) < 0) | 1073 if (WriteStream(pObj, objnum, pHandler) < 0) |
1075 return -1; | 1074 return -1; |
1076 } else { | 1075 } else { |
1077 if (WriteDirectObj(objnum, pObj) < 0) | 1076 if (WriteDirectObj(objnum, pObj) < 0) |
1078 return -1; | 1077 return -1; |
1079 } | 1078 } |
1080 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) | 1079 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) |
1081 return -1; | 1080 return -1; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1141 return -1; | 1140 return -1; |
1142 } | 1141 } |
1143 m_Offset += len; | 1142 m_Offset += len; |
1144 break; | 1143 break; |
1145 } | 1144 } |
1146 case CPDF_Object::STREAM: { | 1145 case CPDF_Object::STREAM: { |
1147 CPDF_FlateEncoder encoder; | 1146 CPDF_FlateEncoder encoder; |
1148 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), | 1147 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), |
1149 m_bCompress); | 1148 m_bCompress); |
1150 CPDF_Encryptor encryptor; | 1149 CPDF_Encryptor encryptor; |
1151 IPDF_CryptoHandler* pHandler = m_pCryptoHandler; | 1150 CPDF_CryptoHandler* pHandler = m_pCryptoHandler; |
1152 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); | 1151 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); |
1153 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != | 1152 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != |
1154 encryptor.m_dwSize) { | 1153 encryptor.m_dwSize) { |
1155 encoder.CloneDict(); | 1154 encoder.CloneDict(); |
1156 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1155 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
1157 } | 1156 } |
1158 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1157 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1159 return -1; | 1158 return -1; |
1160 } | 1159 } |
1161 if ((len = m_File.AppendString("stream\r\n")) < 0) { | 1160 if ((len = m_File.AppendString("stream\r\n")) < 0) { |
(...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2001 } | 2000 } |
2002 std::vector<uint8_t> buffer = | 2001 std::vector<uint8_t> buffer = |
2003 PDF_GenerateFileID((uint32_t)(uintptr_t) this, m_dwLastObjNum); | 2002 PDF_GenerateFileID((uint32_t)(uintptr_t) this, m_dwLastObjNum); |
2004 CFX_ByteString bsBuffer(buffer.data(), buffer.size()); | 2003 CFX_ByteString bsBuffer(buffer.data(), buffer.size()); |
2005 m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); | 2004 m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); |
2006 return; | 2005 return; |
2007 } | 2006 } |
2008 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone()); | 2007 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone()); |
2009 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { | 2008 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { |
2010 if (m_pEncryptDict->GetStringBy("Filter") == "Standard") { | 2009 if (m_pEncryptDict->GetStringBy("Filter") == "Standard") { |
2011 CPDF_StandardSecurityHandler handler; | |
2012 CFX_ByteString user_pass = m_pParser->GetPassword(); | 2010 CFX_ByteString user_pass = m_pParser->GetPassword(); |
2013 uint32_t flag = PDF_ENCRYPT_CONTENT; | 2011 uint32_t flag = PDF_ENCRYPT_CONTENT; |
2012 | |
2013 CPDF_SecurityHandler handler; | |
2014 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(), | 2014 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(), |
2015 user_pass.GetLength(), flag); | 2015 user_pass.GetLength(), flag); |
2016 if (m_bNewCrypto) { | 2016 if (m_bNewCrypto) { |
Tom Sepez
2016/04/19 17:35:43
This pattern is better served by an unique ptr.
| |
2017 delete m_pCryptoHandler; | 2017 delete m_pCryptoHandler; |
2018 } | 2018 } |
2019 m_pCryptoHandler = new CPDF_StandardCryptoHandler; | 2019 m_pCryptoHandler = new CPDF_CryptoHandler; |
2020 m_pCryptoHandler->Init(m_pEncryptDict, &handler); | 2020 m_pCryptoHandler->Init(m_pEncryptDict, &handler); |
2021 m_bNewCrypto = TRUE; | 2021 m_bNewCrypto = TRUE; |
2022 m_bSecurityChanged = TRUE; | 2022 m_bSecurityChanged = TRUE; |
2023 } | 2023 } |
2024 } | 2024 } |
2025 } | 2025 } |
2026 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { | 2026 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { |
2027 if (m_iStage < 0) { | 2027 if (m_iStage < 0) { |
2028 return m_iStage; | 2028 return m_iStage; |
2029 } | 2029 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2070 m_bNewCrypto = FALSE; | 2070 m_bNewCrypto = FALSE; |
2071 if (!m_bStandardSecurity) { | 2071 if (!m_bStandardSecurity) { |
2072 return; | 2072 return; |
2073 } | 2073 } |
2074 if (m_pEncryptDict) { | 2074 if (m_pEncryptDict) { |
2075 m_pEncryptDict->Release(); | 2075 m_pEncryptDict->Release(); |
2076 m_pEncryptDict = NULL; | 2076 m_pEncryptDict = NULL; |
2077 } | 2077 } |
2078 m_bStandardSecurity = FALSE; | 2078 m_bStandardSecurity = FALSE; |
2079 } | 2079 } |
OLD | NEW |