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/src/fpdfapi/fpdf_edit/editint.h" | 7 #include "core/src/fpdfapi/fpdf_edit/editint.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "core/include/fpdfapi/cpdf_document.h" | 11 #include "core/include/fpdfapi/cpdf_document.h" |
12 #include "core/include/fpdfapi/cpdf_parser.h" | 12 #include "core/include/fpdfapi/cpdf_parser.h" |
13 #include "core/include/fpdfapi/fpdf_parser.h" | 13 #include "core/include/fpdfapi/fpdf_parser.h" |
14 #include "core/include/fpdfapi/fpdf_serial.h" | 14 #include "core/include/fpdfapi/fpdf_serial.h" |
| 15 #include "core/include/fpdfapi/ipdf_crypto_handler.h" |
15 #include "core/include/fxcrt/fx_ext.h" | 16 #include "core/include/fxcrt/fx_ext.h" |
| 17 #include "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" |
16 #include "core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h" | 18 #include "core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.h" |
17 #include "third_party/base/stl_util.h" | 19 #include "third_party/base/stl_util.h" |
18 | 20 |
19 #define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024) | 21 #define PDF_OBJECTSTREAM_MAXLENGTH (256 * 1024) |
20 #define PDF_XREFSTREAM_MAXSIZE 10000 | 22 #define PDF_XREFSTREAM_MAXSIZE 10000 |
21 | 23 |
22 // TODO(ochang): Make helper for appending "objnum 0 R ". | 24 // TODO(ochang): Make helper for appending "objnum 0 R ". |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 m_pDict->Release(); | 465 m_pDict->Release(); |
464 } | 466 } |
465 if (m_bNewData) { | 467 if (m_bNewData) { |
466 FX_Free(m_pData); | 468 FX_Free(m_pData); |
467 } | 469 } |
468 } | 470 } |
469 class CPDF_Encryptor { | 471 class CPDF_Encryptor { |
470 public: | 472 public: |
471 CPDF_Encryptor(); | 473 CPDF_Encryptor(); |
472 ~CPDF_Encryptor(); | 474 ~CPDF_Encryptor(); |
473 FX_BOOL Initialize(CPDF_CryptoHandler* pHandler, | 475 FX_BOOL Initialize(IPDF_CryptoHandler* pHandler, |
474 int objnum, | 476 int objnum, |
475 uint8_t* src_data, | 477 uint8_t* src_data, |
476 FX_DWORD src_size); | 478 FX_DWORD src_size); |
477 uint8_t* m_pData; | 479 uint8_t* m_pData; |
478 FX_DWORD m_dwSize; | 480 FX_DWORD m_dwSize; |
479 FX_BOOL m_bNewBuf; | 481 FX_BOOL m_bNewBuf; |
480 }; | 482 }; |
481 CPDF_Encryptor::CPDF_Encryptor() { | 483 CPDF_Encryptor::CPDF_Encryptor() { |
482 m_pData = NULL; | 484 m_pData = NULL; |
483 m_dwSize = 0; | 485 m_dwSize = 0; |
484 m_bNewBuf = FALSE; | 486 m_bNewBuf = FALSE; |
485 } | 487 } |
486 FX_BOOL CPDF_Encryptor::Initialize(CPDF_CryptoHandler* pHandler, | 488 FX_BOOL CPDF_Encryptor::Initialize(IPDF_CryptoHandler* pHandler, |
487 int objnum, | 489 int objnum, |
488 uint8_t* src_data, | 490 uint8_t* src_data, |
489 FX_DWORD src_size) { | 491 FX_DWORD src_size) { |
490 if (src_size == 0) { | 492 if (src_size == 0) { |
491 return TRUE; | 493 return TRUE; |
492 } | 494 } |
493 if (!pHandler) { | 495 if (!pHandler) { |
494 m_pData = (uint8_t*)src_data; | 496 m_pData = (uint8_t*)src_data; |
495 m_dwSize = src_size; | 497 m_dwSize = src_size; |
496 m_bNewBuf = FALSE; | 498 m_bNewBuf = FALSE; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 m_OffsetArray.Add(m_Buffer.GetLength()); | 535 m_OffsetArray.Add(m_Buffer.GetLength()); |
534 m_Buffer.AppendBlock(pBuffer, dwSize); | 536 m_Buffer.AppendBlock(pBuffer, dwSize); |
535 return 1; | 537 return 1; |
536 } | 538 } |
537 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { | 539 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { |
538 FXSYS_assert(pCreator); | 540 FXSYS_assert(pCreator); |
539 if (m_ObjNumArray.GetSize() == 0) { | 541 if (m_ObjNumArray.GetSize() == 0) { |
540 return 0; | 542 return 0; |
541 } | 543 } |
542 CFX_FileBufferArchive* pFile = &pCreator->m_File; | 544 CFX_FileBufferArchive* pFile = &pCreator->m_File; |
543 CPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; | 545 IPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; |
544 FX_FILESIZE ObjOffset = pCreator->m_Offset; | 546 FX_FILESIZE ObjOffset = pCreator->m_Offset; |
545 if (!m_dwObjNum) { | 547 if (!m_dwObjNum) { |
546 m_dwObjNum = ++pCreator->m_dwLastObjNum; | 548 m_dwObjNum = ++pCreator->m_dwLastObjNum; |
547 } | 549 } |
548 CFX_ByteTextBuf tempBuffer; | 550 CFX_ByteTextBuf tempBuffer; |
549 int32_t iCount = m_ObjNumArray.GetSize(); | 551 int32_t iCount = m_ObjNumArray.GetSize(); |
550 for (int32_t i = 0; i < iCount; i++) { | 552 for (int32_t i = 0; i < iCount; i++) { |
551 tempBuffer << m_ObjNumArray.ElementAt(i) << " " | 553 tempBuffer << m_ObjNumArray.ElementAt(i) << " " |
552 << m_OffsetArray.ElementAt(i) << " "; | 554 << m_OffsetArray.ElementAt(i) << " "; |
553 } | 555 } |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1009 if (!m_pXRefStream->End(this)) { | 1011 if (!m_pXRefStream->End(this)) { |
1010 return -1; | 1012 return -1; |
1011 } | 1013 } |
1012 if (!m_pXRefStream->Start()) { | 1014 if (!m_pXRefStream->Start()) { |
1013 return -1; | 1015 return -1; |
1014 } | 1016 } |
1015 return 0; | 1017 return 0; |
1016 } | 1018 } |
1017 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, | 1019 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, |
1018 FX_DWORD objnum, | 1020 FX_DWORD objnum, |
1019 CPDF_CryptoHandler* pCrypto) { | 1021 IPDF_CryptoHandler* pCrypto) { |
1020 CPDF_FlateEncoder encoder; | 1022 CPDF_FlateEncoder encoder; |
1021 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), | 1023 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), |
1022 pStream == m_pMetadata ? FALSE : m_bCompress); | 1024 pStream == m_pMetadata ? FALSE : m_bCompress); |
1023 CPDF_Encryptor encryptor; | 1025 CPDF_Encryptor encryptor; |
1024 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, | 1026 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, |
1025 encoder.m_dwSize)) { | 1027 encoder.m_dwSize)) { |
1026 return -1; | 1028 return -1; |
1027 } | 1029 } |
1028 if ((FX_DWORD)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { | 1030 if ((FX_DWORD)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { |
1029 encoder.CloneDict(); | 1031 encoder.CloneDict(); |
(...skipping 22 matching lines...) Expand all Loading... |
1052 int32_t len = m_File.AppendDWord(objnum); | 1054 int32_t len = m_File.AppendDWord(objnum); |
1053 if (len < 0) | 1055 if (len < 0) |
1054 return -1; | 1056 return -1; |
1055 | 1057 |
1056 m_Offset += len; | 1058 m_Offset += len; |
1057 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) | 1059 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) |
1058 return -1; | 1060 return -1; |
1059 | 1061 |
1060 m_Offset += len; | 1062 m_Offset += len; |
1061 if (pObj->IsStream()) { | 1063 if (pObj->IsStream()) { |
1062 CPDF_CryptoHandler* pHandler = nullptr; | 1064 IPDF_CryptoHandler* pHandler = nullptr; |
1063 pHandler = | 1065 pHandler = |
1064 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; | 1066 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; |
1065 if (WriteStream(pObj, objnum, pHandler) < 0) | 1067 if (WriteStream(pObj, objnum, pHandler) < 0) |
1066 return -1; | 1068 return -1; |
1067 } else { | 1069 } else { |
1068 if (WriteDirectObj(objnum, pObj) < 0) | 1070 if (WriteDirectObj(objnum, pObj) < 0) |
1069 return -1; | 1071 return -1; |
1070 } | 1072 } |
1071 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) | 1073 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) |
1072 return -1; | 1074 return -1; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1132 return -1; | 1134 return -1; |
1133 } | 1135 } |
1134 m_Offset += len; | 1136 m_Offset += len; |
1135 break; | 1137 break; |
1136 } | 1138 } |
1137 case CPDF_Object::STREAM: { | 1139 case CPDF_Object::STREAM: { |
1138 CPDF_FlateEncoder encoder; | 1140 CPDF_FlateEncoder encoder; |
1139 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), | 1141 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), |
1140 m_bCompress); | 1142 m_bCompress); |
1141 CPDF_Encryptor encryptor; | 1143 CPDF_Encryptor encryptor; |
1142 CPDF_CryptoHandler* pHandler = m_pCryptoHandler; | 1144 IPDF_CryptoHandler* pHandler = m_pCryptoHandler; |
1143 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); | 1145 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); |
1144 if ((FX_DWORD)encoder.m_pDict->GetIntegerBy("Length") != | 1146 if ((FX_DWORD)encoder.m_pDict->GetIntegerBy("Length") != |
1145 encryptor.m_dwSize) { | 1147 encryptor.m_dwSize) { |
1146 encoder.CloneDict(); | 1148 encoder.CloneDict(); |
1147 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1149 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
1148 } | 1150 } |
1149 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1151 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1150 return -1; | 1152 return -1; |
1151 } | 1153 } |
1152 if ((len = m_File.AppendString("stream\r\n")) < 0) { | 1154 if ((len = m_File.AppendString("stream\r\n")) < 0) { |
(...skipping 906 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2059 m_bNewCrypto = FALSE; | 2061 m_bNewCrypto = FALSE; |
2060 if (!m_bStandardSecurity) { | 2062 if (!m_bStandardSecurity) { |
2061 return; | 2063 return; |
2062 } | 2064 } |
2063 if (m_pEncryptDict) { | 2065 if (m_pEncryptDict) { |
2064 m_pEncryptDict->Release(); | 2066 m_pEncryptDict->Release(); |
2065 m_pEncryptDict = NULL; | 2067 m_pEncryptDict = NULL; |
2066 } | 2068 } |
2067 m_bStandardSecurity = FALSE; | 2069 m_bStandardSecurity = FALSE; |
2068 } | 2070 } |
OLD | NEW |