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 |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 m_Acc.LoadAllData(pStream, TRUE); | 419 m_Acc.LoadAllData(pStream, TRUE); |
420 if ((pStream && pStream->GetDict() && | 420 if ((pStream && pStream->GetDict() && |
421 pStream->GetDict()->KeyExist("Filter")) || | 421 pStream->GetDict()->KeyExist("Filter")) || |
422 !bFlateEncode) { | 422 !bFlateEncode) { |
423 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { | 423 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { |
424 CPDF_StreamAcc destAcc; | 424 CPDF_StreamAcc destAcc; |
425 destAcc.LoadAllData(pStream); | 425 destAcc.LoadAllData(pStream); |
426 m_dwSize = destAcc.GetSize(); | 426 m_dwSize = destAcc.GetSize(); |
427 m_pData = (uint8_t*)destAcc.DetachData(); | 427 m_pData = (uint8_t*)destAcc.DetachData(); |
428 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 428 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
429 m_pDict->RemoveAt("Filter"); | 429 m_pDict->RemoveFor("Filter"); |
430 m_bNewData = TRUE; | 430 m_bNewData = TRUE; |
431 m_bCloned = TRUE; | 431 m_bCloned = TRUE; |
432 } else { | 432 } else { |
433 m_pData = (uint8_t*)m_Acc.GetData(); | 433 m_pData = (uint8_t*)m_Acc.GetData(); |
434 m_dwSize = m_Acc.GetSize(); | 434 m_dwSize = m_Acc.GetSize(); |
435 m_pDict = pStream->GetDict(); | 435 m_pDict = pStream->GetDict(); |
436 } | 436 } |
437 return; | 437 return; |
438 } | 438 } |
439 | 439 |
440 m_bNewData = TRUE; | 440 m_bNewData = TRUE; |
441 m_bCloned = TRUE; | 441 m_bCloned = TRUE; |
442 // TODO(thestig): Move to Init() and check return value. | 442 // TODO(thestig): Move to Init() and check return value. |
443 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &m_pData, &m_dwSize); | 443 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &m_pData, &m_dwSize); |
444 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 444 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
445 m_pDict->SetAtInteger("Length", m_dwSize); | 445 m_pDict->SetIntegerFor("Length", m_dwSize); |
446 m_pDict->SetAtName("Filter", "FlateDecode"); | 446 m_pDict->SetNameFor("Filter", "FlateDecode"); |
447 m_pDict->RemoveAt("DecodeParms"); | 447 m_pDict->RemoveFor("DecodeParms"); |
448 } | 448 } |
449 | 449 |
450 CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, | 450 CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, |
451 uint32_t size, | 451 uint32_t size, |
452 bool bFlateEncode, | 452 bool bFlateEncode, |
453 bool bXRefStream) | 453 bool bXRefStream) |
454 : m_pData(nullptr), | 454 : m_pData(nullptr), |
455 m_dwSize(0), | 455 m_dwSize(0), |
456 m_pDict(nullptr), | 456 m_pDict(nullptr), |
457 m_bCloned(FALSE), | 457 m_bCloned(FALSE), |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 | 916 |
917 uint32_t objnum = pObj->GetObjNum(); | 917 uint32_t objnum = pObj->GetObjNum(); |
918 if (m_pParser && m_pParser->GetObjectGenNum(objnum) > 0) | 918 if (m_pParser && m_pParser->GetObjectGenNum(objnum) > 0) |
919 return 1; | 919 return 1; |
920 | 920 |
921 if (pObj->IsNumber()) | 921 if (pObj->IsNumber()) |
922 return 1; | 922 return 1; |
923 | 923 |
924 CPDF_Dictionary* pDict = pObj->GetDict(); | 924 CPDF_Dictionary* pDict = pObj->GetDict(); |
925 if (pObj->IsStream()) { | 925 if (pObj->IsStream()) { |
926 if (pDict && pDict->GetStringBy("Type") == "XRef") | 926 if (pDict && pDict->GetStringFor("Type") == "XRef") |
927 return 0; | 927 return 0; |
928 return 1; | 928 return 1; |
929 } | 929 } |
930 | 930 |
931 if (pDict) { | 931 if (pDict) { |
932 if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) | 932 if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) |
933 return 1; | 933 return 1; |
934 if (pDict->IsSignatureDict()) | 934 if (pDict->IsSignatureDict()) |
935 return 1; | 935 return 1; |
936 if (pDict->GetStringBy("Type") == "Page") | 936 if (pDict->GetStringFor("Type") == "Page") |
937 return 1; | 937 return 1; |
938 } | 938 } |
939 | 939 |
940 m_pXRefStream->AddObjectNumberToIndexArray(objnum); | 940 m_pXRefStream->AddObjectNumberToIndexArray(objnum); |
941 if (m_pXRefStream->CompressIndirectObject(objnum, pObj, this) < 0) | 941 if (m_pXRefStream->CompressIndirectObject(objnum, pObj, this) < 0) |
942 return -1; | 942 return -1; |
943 if (!IsXRefNeedEnd(m_pXRefStream.get(), m_dwFlags)) | 943 if (!IsXRefNeedEnd(m_pXRefStream.get(), m_dwFlags)) |
944 return 0; | 944 return 0; |
945 if (!m_pXRefStream->End(this)) | 945 if (!m_pXRefStream->End(this)) |
946 return -1; | 946 return -1; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 return -1; | 986 return -1; |
987 } | 987 } |
988 return 0; | 988 return 0; |
989 } | 989 } |
990 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, | 990 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, |
991 uint32_t objnum, | 991 uint32_t objnum, |
992 CPDF_CryptoHandler* pCrypto) { | 992 CPDF_CryptoHandler* pCrypto) { |
993 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pStream->AsStream()), | 993 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pStream->AsStream()), |
994 pStream != m_pMetadata); | 994 pStream != m_pMetadata); |
995 CPDF_Encryptor encryptor(pCrypto, objnum, encoder.m_pData, encoder.m_dwSize); | 995 CPDF_Encryptor encryptor(pCrypto, objnum, encoder.m_pData, encoder.m_dwSize); |
996 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { | 996 if ((uint32_t)encoder.m_pDict->GetIntegerFor("Length") != |
| 997 encryptor.m_dwSize) { |
997 encoder.CloneDict(); | 998 encoder.CloneDict(); |
998 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 999 encoder.m_pDict->SetIntegerFor("Length", encryptor.m_dwSize); |
999 } | 1000 } |
1000 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1001 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1001 return -1; | 1002 return -1; |
1002 } | 1003 } |
1003 int len = m_File.AppendString("stream\r\n"); | 1004 int len = m_File.AppendString("stream\r\n"); |
1004 if (len < 0) { | 1005 if (len < 0) { |
1005 return -1; | 1006 return -1; |
1006 } | 1007 } |
1007 m_Offset += len; | 1008 m_Offset += len; |
1008 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { | 1009 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1098 return -1; | 1099 return -1; |
1099 } | 1100 } |
1100 m_Offset += len; | 1101 m_Offset += len; |
1101 break; | 1102 break; |
1102 } | 1103 } |
1103 case CPDF_Object::STREAM: { | 1104 case CPDF_Object::STREAM: { |
1104 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pObj->AsStream()), | 1105 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pObj->AsStream()), |
1105 TRUE); | 1106 TRUE); |
1106 CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, encoder.m_pData, | 1107 CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, encoder.m_pData, |
1107 encoder.m_dwSize); | 1108 encoder.m_dwSize); |
1108 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != | 1109 if ((uint32_t)encoder.m_pDict->GetIntegerFor("Length") != |
1109 encryptor.m_dwSize) { | 1110 encryptor.m_dwSize) { |
1110 encoder.CloneDict(); | 1111 encoder.CloneDict(); |
1111 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1112 encoder.m_pDict->SetIntegerFor("Length", encryptor.m_dwSize); |
1112 } | 1113 } |
1113 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1114 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1114 return -1; | 1115 return -1; |
1115 } | 1116 } |
1116 if ((len = m_File.AppendString("stream\r\n")) < 0) { | 1117 if ((len = m_File.AppendString("stream\r\n")) < 0) { |
1117 return -1; | 1118 return -1; |
1118 } | 1119 } |
1119 m_Offset += len; | 1120 m_Offset += len; |
1120 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { | 1121 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { |
1121 return -1; | 1122 return -1; |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1439 int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { | 1440 int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { |
1440 ASSERT(m_iStage > -1 || m_iStage < 20); | 1441 ASSERT(m_iStage > -1 || m_iStage < 20); |
1441 if (m_iStage == 0) { | 1442 if (m_iStage == 0) { |
1442 if (!m_pParser) { | 1443 if (!m_pParser) { |
1443 m_dwFlags &= ~FPDFCREATE_INCREMENTAL; | 1444 m_dwFlags &= ~FPDFCREATE_INCREMENTAL; |
1444 } | 1445 } |
1445 if (m_bSecurityChanged && (m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0) { | 1446 if (m_bSecurityChanged && (m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0) { |
1446 m_dwFlags &= ~FPDFCREATE_INCREMENTAL; | 1447 m_dwFlags &= ~FPDFCREATE_INCREMENTAL; |
1447 } | 1448 } |
1448 CPDF_Dictionary* pDict = m_pDocument->GetRoot(); | 1449 CPDF_Dictionary* pDict = m_pDocument->GetRoot(); |
1449 m_pMetadata = pDict ? pDict->GetDirectObjectBy("Metadata") : nullptr; | 1450 m_pMetadata = pDict ? pDict->GetDirectObjectFor("Metadata") : nullptr; |
1450 if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) { | 1451 if (m_dwFlags & FPDFCREATE_OBJECTSTREAM) { |
1451 m_pXRefStream.reset(new CPDF_XRefStream); | 1452 m_pXRefStream.reset(new CPDF_XRefStream); |
1452 m_pXRefStream->Start(); | 1453 m_pXRefStream->Start(); |
1453 if ((m_dwFlags & FPDFCREATE_INCREMENTAL) != 0 && m_pParser) { | 1454 if ((m_dwFlags & FPDFCREATE_INCREMENTAL) != 0 && m_pParser) { |
1454 FX_FILESIZE prev = m_pParser->GetLastXRefOffset(); | 1455 FX_FILESIZE prev = m_pParser->GetLastXRefOffset(); |
1455 m_pXRefStream->m_PrevOffset = prev; | 1456 m_pXRefStream->m_PrevOffset = prev; |
1456 } | 1457 } |
1457 } | 1458 } |
1458 m_iStage = 10; | 1459 m_iStage = 10; |
1459 } | 1460 } |
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1952 return; | 1953 return; |
1953 } | 1954 } |
1954 std::vector<uint8_t> buffer = | 1955 std::vector<uint8_t> buffer = |
1955 PDF_GenerateFileID((uint32_t)(uintptr_t) this, m_dwLastObjNum); | 1956 PDF_GenerateFileID((uint32_t)(uintptr_t) this, m_dwLastObjNum); |
1956 CFX_ByteString bsBuffer(buffer.data(), buffer.size()); | 1957 CFX_ByteString bsBuffer(buffer.data(), buffer.size()); |
1957 m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); | 1958 m_pIDArray->Add(new CPDF_String(bsBuffer, TRUE), m_pDocument); |
1958 return; | 1959 return; |
1959 } | 1960 } |
1960 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone()); | 1961 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone()); |
1961 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { | 1962 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { |
1962 if (m_pEncryptDict->GetStringBy("Filter") == "Standard") { | 1963 if (m_pEncryptDict->GetStringFor("Filter") == "Standard") { |
1963 CFX_ByteString user_pass = m_pParser->GetPassword(); | 1964 CFX_ByteString user_pass = m_pParser->GetPassword(); |
1964 uint32_t flag = PDF_ENCRYPT_CONTENT; | 1965 uint32_t flag = PDF_ENCRYPT_CONTENT; |
1965 | 1966 |
1966 CPDF_SecurityHandler handler; | 1967 CPDF_SecurityHandler handler; |
1967 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(), | 1968 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(), |
1968 user_pass.GetLength(), flag); | 1969 user_pass.GetLength(), flag); |
1969 if (m_bLocalCryptoHandler) | 1970 if (m_bLocalCryptoHandler) |
1970 delete m_pCryptoHandler; | 1971 delete m_pCryptoHandler; |
1971 m_pCryptoHandler = new CPDF_CryptoHandler; | 1972 m_pCryptoHandler = new CPDF_CryptoHandler; |
1972 m_pCryptoHandler->Init(m_pEncryptDict, &handler); | 1973 m_pCryptoHandler->Init(m_pEncryptDict, &handler); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2015 m_pCryptoHandler = nullptr; | 2016 m_pCryptoHandler = nullptr; |
2016 } | 2017 } |
2017 void CPDF_Creator::ResetStandardSecurity() { | 2018 void CPDF_Creator::ResetStandardSecurity() { |
2018 if (!m_bLocalCryptoHandler) | 2019 if (!m_bLocalCryptoHandler) |
2019 return; | 2020 return; |
2020 | 2021 |
2021 delete m_pCryptoHandler; | 2022 delete m_pCryptoHandler; |
2022 m_pCryptoHandler = nullptr; | 2023 m_pCryptoHandler = nullptr; |
2023 m_bLocalCryptoHandler = FALSE; | 2024 m_bLocalCryptoHandler = FALSE; |
2024 } | 2025 } |
OLD | NEW |