| 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 "editint.h" | 7 #include "editint.h" |
| 8 | 8 |
| 9 #include "core/include/fxcrt/fx_ext.h" | 9 #include "core/include/fxcrt/fx_ext.h" |
| 10 #include "core/include/fpdfapi/fpdf_serial.h" | 10 #include "core/include/fpdfapi/fpdf_serial.h" |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 for (i = 0; i < 2; i++) { | 300 for (i = 0; i < 2; i++) { |
| 301 *pBuffer++ = FX_Random_MT_Generate(pContext); | 301 *pBuffer++ = FX_Random_MT_Generate(pContext); |
| 302 } | 302 } |
| 303 FX_Random_MT_Close(pContext); | 303 FX_Random_MT_Close(pContext); |
| 304 return TRUE; | 304 return TRUE; |
| 305 } | 305 } |
| 306 class CPDF_FlateEncoder { | 306 class CPDF_FlateEncoder { |
| 307 public: | 307 public: |
| 308 CPDF_FlateEncoder(); | 308 CPDF_FlateEncoder(); |
| 309 ~CPDF_FlateEncoder(); | 309 ~CPDF_FlateEncoder(); |
| 310 FX_BOOL Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode); | 310 bool Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode); |
| 311 FX_BOOL Initialize(const uint8_t* pBuffer, | 311 bool Initialize(const uint8_t* pBuffer, |
| 312 FX_DWORD size, | 312 FX_DWORD size, |
| 313 FX_BOOL bFlateEncode, | 313 bool bFlateEncode, |
| 314 FX_BOOL bXRefStream = FALSE); | 314 bool bXRefStream); |
| 315 void CloneDict(); | 315 void CloneDict(); |
| 316 uint8_t* m_pData; | 316 uint8_t* m_pData; |
| 317 FX_DWORD m_dwSize; | 317 FX_DWORD m_dwSize; |
| 318 CPDF_Dictionary* m_pDict; | 318 CPDF_Dictionary* m_pDict; |
| 319 FX_BOOL m_bCloned; | 319 FX_BOOL m_bCloned; |
| 320 FX_BOOL m_bNewData; | 320 FX_BOOL m_bNewData; |
| 321 CPDF_StreamAcc m_Acc; | 321 CPDF_StreamAcc m_Acc; |
| 322 }; | 322 }; |
| 323 CPDF_FlateEncoder::CPDF_FlateEncoder() { | 323 CPDF_FlateEncoder::CPDF_FlateEncoder() { |
| 324 m_pData = NULL; | 324 m_pData = NULL; |
| 325 m_dwSize = 0; | 325 m_dwSize = 0; |
| 326 m_pDict = NULL; | 326 m_pDict = NULL; |
| 327 m_bCloned = FALSE; | 327 m_bCloned = FALSE; |
| 328 m_bNewData = FALSE; | 328 m_bNewData = FALSE; |
| 329 } | 329 } |
| 330 void CPDF_FlateEncoder::CloneDict() { | 330 void CPDF_FlateEncoder::CloneDict() { |
| 331 if (!m_bCloned) { | 331 if (!m_bCloned) { |
| 332 m_pDict = ToDictionary(m_pDict->Clone()); | 332 m_pDict = ToDictionary(m_pDict->Clone()); |
| 333 ASSERT(m_pDict); | 333 ASSERT(m_pDict); |
| 334 m_bCloned = TRUE; | 334 m_bCloned = TRUE; |
| 335 } | 335 } |
| 336 } | 336 } |
| 337 FX_BOOL CPDF_FlateEncoder::Initialize(CPDF_Stream* pStream, | 337 bool CPDF_FlateEncoder::Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode) { |
| 338 FX_BOOL bFlateEncode) { | |
| 339 m_Acc.LoadAllData(pStream, TRUE); | 338 m_Acc.LoadAllData(pStream, TRUE); |
| 340 if ((pStream && pStream->GetDict() && | 339 if ((pStream && pStream->GetDict() && |
| 341 pStream->GetDict()->KeyExist("Filter")) || | 340 pStream->GetDict()->KeyExist("Filter")) || |
| 342 !bFlateEncode) { | 341 !bFlateEncode) { |
| 343 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { | 342 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { |
| 344 CPDF_StreamAcc destAcc; | 343 CPDF_StreamAcc destAcc; |
| 345 destAcc.LoadAllData(pStream); | 344 destAcc.LoadAllData(pStream); |
| 346 m_dwSize = destAcc.GetSize(); | 345 m_dwSize = destAcc.GetSize(); |
| 347 m_pData = (uint8_t*)destAcc.DetachData(); | 346 m_pData = (uint8_t*)destAcc.DetachData(); |
| 348 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 347 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
| 349 m_pDict->RemoveAt("Filter"); | 348 m_pDict->RemoveAt("Filter"); |
| 350 m_bNewData = TRUE; | 349 m_bNewData = TRUE; |
| 351 m_bCloned = TRUE; | 350 m_bCloned = TRUE; |
| 352 } else { | 351 } else { |
| 353 m_pData = (uint8_t*)m_Acc.GetData(); | 352 m_pData = (uint8_t*)m_Acc.GetData(); |
| 354 m_dwSize = m_Acc.GetSize(); | 353 m_dwSize = m_Acc.GetSize(); |
| 355 m_pDict = pStream->GetDict(); | 354 m_pDict = pStream->GetDict(); |
| 356 } | 355 } |
| 357 return TRUE; | 356 return true; |
| 358 } | 357 } |
| 359 m_pData = NULL; | 358 m_pData = nullptr; |
| 360 m_dwSize = 0; | 359 m_dwSize = 0; |
| 361 m_bNewData = TRUE; | 360 m_bNewData = TRUE; |
| 362 m_bCloned = TRUE; | 361 m_bCloned = TRUE; |
| 363 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); | 362 if (!::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &m_pData, &m_dwSize)) |
| 363 return false; |
| 364 |
| 364 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 365 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
| 365 m_pDict->SetAtInteger("Length", m_dwSize); | 366 m_pDict->SetAtInteger("Length", m_dwSize); |
| 366 m_pDict->SetAtName("Filter", "FlateDecode"); | 367 m_pDict->SetAtName("Filter", "FlateDecode"); |
| 367 m_pDict->RemoveAt("DecodeParms"); | 368 m_pDict->RemoveAt("DecodeParms"); |
| 368 return TRUE; | 369 return true; |
| 369 } | 370 } |
| 370 FX_BOOL CPDF_FlateEncoder::Initialize(const uint8_t* pBuffer, | 371 |
| 371 FX_DWORD size, | 372 bool CPDF_FlateEncoder::Initialize(const uint8_t* pBuffer, |
| 372 FX_BOOL bFlateEncode, | 373 FX_DWORD size, |
| 373 FX_BOOL bXRefStream) { | 374 bool bFlateEncode, |
| 375 bool bXRefStream) { |
| 374 if (!bFlateEncode) { | 376 if (!bFlateEncode) { |
| 375 m_pData = (uint8_t*)pBuffer; | 377 m_pData = (uint8_t*)pBuffer; |
| 376 m_dwSize = size; | 378 m_dwSize = size; |
| 377 return TRUE; | 379 return true; |
| 378 } | 380 } |
| 379 m_bNewData = TRUE; | 381 m_bNewData = TRUE; |
| 382 bool ret; |
| 380 if (bXRefStream) { | 383 if (bXRefStream) { |
| 381 ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); | 384 ret = ::PngEncode(pBuffer, size, &m_pData, &m_dwSize); |
| 382 } else { | 385 } else { |
| 383 ::FlateEncode(pBuffer, size, m_pData, m_dwSize); | 386 ret = ::FlateEncode(pBuffer, size, &m_pData, &m_dwSize); |
| 384 } | 387 } |
| 385 return TRUE; | 388 return ret; |
| 386 } | 389 } |
| 390 |
| 387 CPDF_FlateEncoder::~CPDF_FlateEncoder() { | 391 CPDF_FlateEncoder::~CPDF_FlateEncoder() { |
| 388 if (m_bCloned && m_pDict) { | 392 if (m_bCloned && m_pDict) { |
| 389 m_pDict->Release(); | 393 m_pDict->Release(); |
| 390 } | 394 } |
| 391 if (m_bNewData) { | 395 if (m_bNewData) { |
| 392 FX_Free(m_pData); | 396 FX_Free(m_pData); |
| 393 } | 397 } |
| 394 } | 398 } |
| 395 class CPDF_Encryptor { | 399 class CPDF_Encryptor { |
| 396 public: | 400 public: |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 0) { | 515 0) { |
| 512 return -1; | 516 return -1; |
| 513 } | 517 } |
| 514 if (pFile->AppendBlock(m_Buffer.GetBuffer(), m_Buffer.GetLength()) < 0) { | 518 if (pFile->AppendBlock(m_Buffer.GetBuffer(), m_Buffer.GetLength()) < 0) { |
| 515 return -1; | 519 return -1; |
| 516 } | 520 } |
| 517 offset += len + tempBuffer.GetLength() + m_Buffer.GetLength(); | 521 offset += len + tempBuffer.GetLength() + m_Buffer.GetLength(); |
| 518 } else { | 522 } else { |
| 519 tempBuffer << m_Buffer; | 523 tempBuffer << m_Buffer; |
| 520 CPDF_FlateEncoder encoder; | 524 CPDF_FlateEncoder encoder; |
| 521 encoder.Initialize(tempBuffer.GetBuffer(), tempBuffer.GetLength(), | 525 if (!encoder.Initialize(tempBuffer.GetBuffer(), tempBuffer.GetLength(), |
| 522 pCreator->m_bCompress); | 526 pCreator->m_bCompress, false)) { |
| 527 return -1; |
| 528 } |
| 523 CPDF_Encryptor encryptor; | 529 CPDF_Encryptor encryptor; |
| 524 encryptor.Initialize(pHandler, m_dwObjNum, encoder.m_pData, | 530 if (!encryptor.Initialize(pHandler, m_dwObjNum, encoder.m_pData, |
| 525 encoder.m_dwSize); | 531 encoder.m_dwSize)) { |
| 532 return -1; |
| 533 } |
| 526 if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { | 534 if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { |
| 527 return -1; | 535 return -1; |
| 528 } | 536 } |
| 529 offset += len; | 537 offset += len; |
| 530 if (pCreator->m_bCompress) { | 538 if (pCreator->m_bCompress) { |
| 531 if (pFile->AppendString("/Filter /FlateDecode") < 0) { | 539 if (pFile->AppendString("/Filter /FlateDecode") < 0) { |
| 532 return -1; | 540 return -1; |
| 533 } | 541 } |
| 534 offset += 20; | 542 offset += 20; |
| 535 } | 543 } |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 } | 769 } |
| 762 FX_CHAR offset_buf[20]; | 770 FX_CHAR offset_buf[20]; |
| 763 FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); | 771 FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); |
| 764 FXSYS_i64toa(m_PrevOffset, offset_buf, 10); | 772 FXSYS_i64toa(m_PrevOffset, offset_buf, 10); |
| 765 int32_t len = (int32_t)FXSYS_strlen(offset_buf); | 773 int32_t len = (int32_t)FXSYS_strlen(offset_buf); |
| 766 if (pFile->AppendBlock(offset_buf, len) < 0) { | 774 if (pFile->AppendBlock(offset_buf, len) < 0) { |
| 767 return FALSE; | 775 return FALSE; |
| 768 } | 776 } |
| 769 offset += len + 6; | 777 offset += len + 6; |
| 770 } | 778 } |
| 771 FX_BOOL bPredictor = TRUE; | |
| 772 CPDF_FlateEncoder encoder; | 779 CPDF_FlateEncoder encoder; |
| 773 encoder.Initialize(m_Buffer.GetBuffer(), m_Buffer.GetLength(), | 780 if (!encoder.Initialize(m_Buffer.GetBuffer(), m_Buffer.GetLength(), |
| 774 pCreator->m_bCompress, bPredictor); | 781 pCreator->m_bCompress, true)) { |
| 782 return FALSE; |
| 783 } |
| 775 if (pCreator->m_bCompress) { | 784 if (pCreator->m_bCompress) { |
| 776 if (pFile->AppendString("/Filter /FlateDecode") < 0) { | 785 if (pFile->AppendString("/Filter /FlateDecode") < 0) { |
| 777 return FALSE; | 786 return FALSE; |
| 778 } | 787 } |
| 779 offset += 20; | 788 offset += 20; |
| 780 if (bPredictor) { | 789 if ((len = pFile->AppendString("/DecodeParms<</Columns 7/Predictor 12>>")) < |
| 781 if ((len = pFile->AppendString( | 790 0) { |
| 782 "/DecodeParms<</Columns 7/Predictor 12>>")) < 0) { | 791 return FALSE; |
| 783 return FALSE; | |
| 784 } | |
| 785 offset += len; | |
| 786 } | 792 } |
| 793 offset += len; |
| 787 } | 794 } |
| 788 if (pFile->AppendString("/Length ") < 0) { | 795 if (pFile->AppendString("/Length ") < 0) { |
| 789 return FALSE; | 796 return FALSE; |
| 790 } | 797 } |
| 791 if ((len = pFile->AppendDWord(encoder.m_dwSize)) < 0) { | 798 if ((len = pFile->AppendDWord(encoder.m_dwSize)) < 0) { |
| 792 return FALSE; | 799 return FALSE; |
| 793 } | 800 } |
| 794 offset += len + 8; | 801 offset += len + 8; |
| 795 if (bEOF) { | 802 if (bEOF) { |
| 796 if ((len = PDF_CreatorWriteTrailer(pCreator->m_pDocument, pFile, | 803 if ((len = PDF_CreatorWriteTrailer(pCreator->m_pDocument, pFile, |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 } | 1002 } |
| 996 if (!m_pXRefStream->Start()) { | 1003 if (!m_pXRefStream->Start()) { |
| 997 return -1; | 1004 return -1; |
| 998 } | 1005 } |
| 999 return 0; | 1006 return 0; |
| 1000 } | 1007 } |
| 1001 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, | 1008 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, |
| 1002 FX_DWORD objnum, | 1009 FX_DWORD objnum, |
| 1003 CPDF_CryptoHandler* pCrypto) { | 1010 CPDF_CryptoHandler* pCrypto) { |
| 1004 CPDF_FlateEncoder encoder; | 1011 CPDF_FlateEncoder encoder; |
| 1005 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), | 1012 if (!encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), |
| 1006 pStream == m_pMetadata ? FALSE : m_bCompress); | 1013 pStream == m_pMetadata ? FALSE : m_bCompress)) { |
| 1014 return -1; |
| 1015 } |
| 1007 CPDF_Encryptor encryptor; | 1016 CPDF_Encryptor encryptor; |
| 1008 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, | 1017 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, |
| 1009 encoder.m_dwSize)) { | 1018 encoder.m_dwSize)) { |
| 1010 return -1; | 1019 return -1; |
| 1011 } | 1020 } |
| 1012 if ((FX_DWORD)encoder.m_pDict->GetInteger("Length") != encryptor.m_dwSize) { | 1021 if ((FX_DWORD)encoder.m_pDict->GetInteger("Length") != encryptor.m_dwSize) { |
| 1013 encoder.CloneDict(); | 1022 encoder.CloneDict(); |
| 1014 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1023 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
| 1015 } | 1024 } |
| 1016 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1025 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1100 FX_BOOL bHex = pObj->AsString()->IsHex(); | 1109 FX_BOOL bHex = pObj->AsString()->IsHex(); |
| 1101 if (!m_pCryptoHandler || !bEncrypt) { | 1110 if (!m_pCryptoHandler || !bEncrypt) { |
| 1102 CFX_ByteString content = PDF_EncodeString(str, bHex); | 1111 CFX_ByteString content = PDF_EncodeString(str, bHex); |
| 1103 if ((len = m_File.AppendString(content)) < 0) { | 1112 if ((len = m_File.AppendString(content)) < 0) { |
| 1104 return -1; | 1113 return -1; |
| 1105 } | 1114 } |
| 1106 m_Offset += len; | 1115 m_Offset += len; |
| 1107 break; | 1116 break; |
| 1108 } | 1117 } |
| 1109 CPDF_Encryptor encryptor; | 1118 CPDF_Encryptor encryptor; |
| 1110 encryptor.Initialize(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), | 1119 if (!encryptor.Initialize(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), |
| 1111 str.GetLength()); | 1120 str.GetLength())) { |
| 1121 return -1; |
| 1122 } |
| 1112 CFX_ByteString content = PDF_EncodeString( | 1123 CFX_ByteString content = PDF_EncodeString( |
| 1113 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), | 1124 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), |
| 1114 bHex); | 1125 bHex); |
| 1115 if ((len = m_File.AppendString(content)) < 0) { | 1126 if ((len = m_File.AppendString(content)) < 0) { |
| 1116 return -1; | 1127 return -1; |
| 1117 } | 1128 } |
| 1118 m_Offset += len; | 1129 m_Offset += len; |
| 1119 break; | 1130 break; |
| 1120 } | 1131 } |
| 1121 case PDFOBJ_STREAM: { | 1132 case PDFOBJ_STREAM: { |
| 1122 CPDF_FlateEncoder encoder; | 1133 CPDF_FlateEncoder encoder; |
| 1123 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), | 1134 if (!encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), |
| 1124 m_bCompress); | 1135 m_bCompress)) { |
| 1136 return -1; |
| 1137 } |
| 1138 |
| 1125 CPDF_Encryptor encryptor; | 1139 CPDF_Encryptor encryptor; |
| 1126 CPDF_CryptoHandler* pHandler = m_pCryptoHandler; | 1140 CPDF_CryptoHandler* pHandler = m_pCryptoHandler; |
| 1127 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); | 1141 if (!encryptor.Initialize(pHandler, objnum, encoder.m_pData, |
| 1142 encoder.m_dwSize)) { |
| 1143 return -1; |
| 1144 } |
| 1128 if ((FX_DWORD)encoder.m_pDict->GetInteger("Length") != | 1145 if ((FX_DWORD)encoder.m_pDict->GetInteger("Length") != |
| 1129 encryptor.m_dwSize) { | 1146 encryptor.m_dwSize) { |
| 1130 encoder.CloneDict(); | 1147 encoder.CloneDict(); |
| 1131 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1148 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
| 1132 } | 1149 } |
| 1133 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1150 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
| 1134 return -1; | 1151 return -1; |
| 1135 } | 1152 } |
| 1136 if ((len = m_File.AppendString("stream\r\n")) < 0) { | 1153 if ((len = m_File.AppendString("stream\r\n")) < 0) { |
| 1137 return -1; | 1154 return -1; |
| (...skipping 966 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2104 m_bNewCrypto = FALSE; | 2121 m_bNewCrypto = FALSE; |
| 2105 if (!m_bStandardSecurity) { | 2122 if (!m_bStandardSecurity) { |
| 2106 return; | 2123 return; |
| 2107 } | 2124 } |
| 2108 if (m_pEncryptDict) { | 2125 if (m_pEncryptDict) { |
| 2109 m_pEncryptDict->Release(); | 2126 m_pEncryptDict->Release(); |
| 2110 m_pEncryptDict = NULL; | 2127 m_pEncryptDict = NULL; |
| 2111 } | 2128 } |
| 2112 m_bStandardSecurity = FALSE; | 2129 m_bStandardSecurity = FALSE; |
| 2113 } | 2130 } |
| OLD | NEW |