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