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 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 return -1; | 380 return -1; |
381 if (pFile->AppendByte(FX_GETBYTEOFFSET0(offset)) < 0) | 381 if (pFile->AppendByte(FX_GETBYTEOFFSET0(offset)) < 0) |
382 return -1; | 382 return -1; |
383 if (pFile->AppendByte(0) < 0) | 383 if (pFile->AppendByte(0) < 0) |
384 return -1; | 384 return -1; |
385 return 0; | 385 return 0; |
386 } | 386 } |
387 | 387 |
388 class CPDF_FlateEncoder { | 388 class CPDF_FlateEncoder { |
389 public: | 389 public: |
390 CPDF_FlateEncoder(); | 390 CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode); |
| 391 CPDF_FlateEncoder(const uint8_t* pBuffer, |
| 392 uint32_t size, |
| 393 FX_BOOL bFlateEncode, |
| 394 FX_BOOL bXRefStream = FALSE); |
391 ~CPDF_FlateEncoder(); | 395 ~CPDF_FlateEncoder(); |
392 FX_BOOL Initialize(CPDF_Stream* pStream, FX_BOOL bFlateEncode); | 396 |
393 FX_BOOL Initialize(const uint8_t* pBuffer, | |
394 uint32_t size, | |
395 FX_BOOL bFlateEncode, | |
396 FX_BOOL bXRefStream = FALSE); | |
397 void CloneDict(); | 397 void CloneDict(); |
| 398 |
398 uint8_t* m_pData; | 399 uint8_t* m_pData; |
399 uint32_t m_dwSize; | 400 uint32_t m_dwSize; |
400 CPDF_Dictionary* m_pDict; | 401 CPDF_Dictionary* m_pDict; |
401 FX_BOOL m_bCloned; | 402 FX_BOOL m_bCloned; |
402 FX_BOOL m_bNewData; | 403 FX_BOOL m_bNewData; |
403 CPDF_StreamAcc m_Acc; | 404 CPDF_StreamAcc m_Acc; |
404 }; | 405 }; |
405 CPDF_FlateEncoder::CPDF_FlateEncoder() { | 406 |
406 m_pData = NULL; | |
407 m_dwSize = 0; | |
408 m_pDict = NULL; | |
409 m_bCloned = FALSE; | |
410 m_bNewData = FALSE; | |
411 } | |
412 void CPDF_FlateEncoder::CloneDict() { | 407 void CPDF_FlateEncoder::CloneDict() { |
413 if (!m_bCloned) { | 408 if (!m_bCloned) { |
414 m_pDict = ToDictionary(m_pDict->Clone()); | 409 m_pDict = ToDictionary(m_pDict->Clone()); |
415 ASSERT(m_pDict); | 410 ASSERT(m_pDict); |
416 m_bCloned = TRUE; | 411 m_bCloned = TRUE; |
417 } | 412 } |
418 } | 413 } |
419 FX_BOOL CPDF_FlateEncoder::Initialize(CPDF_Stream* pStream, | 414 |
420 FX_BOOL bFlateEncode) { | 415 CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode) |
| 416 : m_pData(nullptr), |
| 417 m_dwSize(0), |
| 418 m_pDict(nullptr), |
| 419 m_bCloned(FALSE), |
| 420 m_bNewData(FALSE) { |
421 m_Acc.LoadAllData(pStream, TRUE); | 421 m_Acc.LoadAllData(pStream, TRUE); |
422 if ((pStream && pStream->GetDict() && | 422 if ((pStream && pStream->GetDict() && |
423 pStream->GetDict()->KeyExist("Filter")) || | 423 pStream->GetDict()->KeyExist("Filter")) || |
424 !bFlateEncode) { | 424 !bFlateEncode) { |
425 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { | 425 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { |
426 CPDF_StreamAcc destAcc; | 426 CPDF_StreamAcc destAcc; |
427 destAcc.LoadAllData(pStream); | 427 destAcc.LoadAllData(pStream); |
428 m_dwSize = destAcc.GetSize(); | 428 m_dwSize = destAcc.GetSize(); |
429 m_pData = (uint8_t*)destAcc.DetachData(); | 429 m_pData = (uint8_t*)destAcc.DetachData(); |
430 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 430 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
431 m_pDict->RemoveAt("Filter"); | 431 m_pDict->RemoveAt("Filter"); |
432 m_bNewData = TRUE; | 432 m_bNewData = TRUE; |
433 m_bCloned = TRUE; | 433 m_bCloned = TRUE; |
434 } else { | 434 } else { |
435 m_pData = (uint8_t*)m_Acc.GetData(); | 435 m_pData = (uint8_t*)m_Acc.GetData(); |
436 m_dwSize = m_Acc.GetSize(); | 436 m_dwSize = m_Acc.GetSize(); |
437 m_pDict = pStream->GetDict(); | 437 m_pDict = pStream->GetDict(); |
438 } | 438 } |
439 return TRUE; | 439 return; |
440 } | 440 } |
441 m_pData = NULL; | 441 |
442 m_dwSize = 0; | |
443 m_bNewData = TRUE; | 442 m_bNewData = TRUE; |
444 m_bCloned = TRUE; | 443 m_bCloned = TRUE; |
445 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); | 444 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); |
446 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 445 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
447 m_pDict->SetAtInteger("Length", m_dwSize); | 446 m_pDict->SetAtInteger("Length", m_dwSize); |
448 m_pDict->SetAtName("Filter", "FlateDecode"); | 447 m_pDict->SetAtName("Filter", "FlateDecode"); |
449 m_pDict->RemoveAt("DecodeParms"); | 448 m_pDict->RemoveAt("DecodeParms"); |
450 return TRUE; | |
451 } | 449 } |
452 FX_BOOL CPDF_FlateEncoder::Initialize(const uint8_t* pBuffer, | 450 |
453 uint32_t size, | 451 CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, |
454 FX_BOOL bFlateEncode, | 452 uint32_t size, |
455 FX_BOOL bXRefStream) { | 453 FX_BOOL bFlateEncode, |
| 454 FX_BOOL bXRefStream) |
| 455 : m_pData(nullptr), |
| 456 m_dwSize(0), |
| 457 m_pDict(nullptr), |
| 458 m_bCloned(FALSE), |
| 459 m_bNewData(FALSE) { |
456 if (!bFlateEncode) { | 460 if (!bFlateEncode) { |
457 m_pData = (uint8_t*)pBuffer; | 461 m_pData = (uint8_t*)pBuffer; |
458 m_dwSize = size; | 462 m_dwSize = size; |
459 return TRUE; | 463 return; |
460 } | 464 } |
461 m_bNewData = TRUE; | 465 m_bNewData = TRUE; |
462 if (bXRefStream) { | 466 if (bXRefStream) |
463 ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); | 467 ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); |
464 } else { | 468 else |
465 ::FlateEncode(pBuffer, size, m_pData, m_dwSize); | 469 ::FlateEncode(pBuffer, size, m_pData, m_dwSize); |
466 } | |
467 return TRUE; | |
468 } | 470 } |
| 471 |
469 CPDF_FlateEncoder::~CPDF_FlateEncoder() { | 472 CPDF_FlateEncoder::~CPDF_FlateEncoder() { |
470 if (m_bCloned && m_pDict) { | 473 if (m_bCloned && m_pDict) |
471 m_pDict->Release(); | 474 m_pDict->Release(); |
472 } | 475 if (m_bNewData) |
473 if (m_bNewData) { | |
474 FX_Free(m_pData); | 476 FX_Free(m_pData); |
475 } | |
476 } | 477 } |
| 478 |
477 class CPDF_Encryptor { | 479 class CPDF_Encryptor { |
478 public: | 480 public: |
479 CPDF_Encryptor(); | 481 CPDF_Encryptor(CPDF_CryptoHandler* pHandler, |
| 482 int objnum, |
| 483 uint8_t* src_data, |
| 484 uint32_t src_size); |
480 ~CPDF_Encryptor(); | 485 ~CPDF_Encryptor(); |
481 FX_BOOL Initialize(CPDF_CryptoHandler* pHandler, | 486 |
482 int objnum, | |
483 uint8_t* src_data, | |
484 uint32_t src_size); | |
485 uint8_t* m_pData; | 487 uint8_t* m_pData; |
486 uint32_t m_dwSize; | 488 uint32_t m_dwSize; |
487 FX_BOOL m_bNewBuf; | 489 FX_BOOL m_bNewBuf; |
488 }; | 490 }; |
489 CPDF_Encryptor::CPDF_Encryptor() { | 491 |
490 m_pData = NULL; | 492 CPDF_Encryptor::CPDF_Encryptor(CPDF_CryptoHandler* pHandler, |
491 m_dwSize = 0; | 493 int objnum, |
492 m_bNewBuf = FALSE; | 494 uint8_t* src_data, |
493 } | 495 uint32_t src_size) |
494 FX_BOOL CPDF_Encryptor::Initialize(CPDF_CryptoHandler* pHandler, | 496 : m_pData(nullptr), m_dwSize(0), m_bNewBuf(FALSE) { |
495 int objnum, | 497 if (src_size == 0) |
496 uint8_t* src_data, | 498 return; |
497 uint32_t src_size) { | 499 |
498 if (src_size == 0) { | |
499 return TRUE; | |
500 } | |
501 if (!pHandler) { | 500 if (!pHandler) { |
502 m_pData = (uint8_t*)src_data; | 501 m_pData = (uint8_t*)src_data; |
503 m_dwSize = src_size; | 502 m_dwSize = src_size; |
504 m_bNewBuf = FALSE; | 503 return; |
505 return TRUE; | |
506 } | 504 } |
507 m_dwSize = pHandler->EncryptGetSize(objnum, 0, src_data, src_size); | 505 m_dwSize = pHandler->EncryptGetSize(objnum, 0, src_data, src_size); |
508 m_pData = FX_Alloc(uint8_t, m_dwSize); | 506 m_pData = FX_Alloc(uint8_t, m_dwSize); |
509 pHandler->EncryptContent(objnum, 0, src_data, src_size, m_pData, m_dwSize); | 507 pHandler->EncryptContent(objnum, 0, src_data, src_size, m_pData, m_dwSize); |
510 m_bNewBuf = TRUE; | 508 m_bNewBuf = TRUE; |
511 return TRUE; | |
512 } | 509 } |
| 510 |
513 CPDF_Encryptor::~CPDF_Encryptor() { | 511 CPDF_Encryptor::~CPDF_Encryptor() { |
514 if (m_bNewBuf) { | 512 if (m_bNewBuf) |
515 FX_Free(m_pData); | 513 FX_Free(m_pData); |
516 } | |
517 } | 514 } |
518 | 515 |
519 } // namespace | 516 } // namespace |
520 | 517 |
521 CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {} | 518 CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {} |
522 FX_BOOL CPDF_ObjectStream::Start() { | 519 FX_BOOL CPDF_ObjectStream::Start() { |
523 m_ObjNumArray.RemoveAll(); | 520 m_ObjNumArray.RemoveAll(); |
524 m_OffsetArray.RemoveAll(); | 521 m_OffsetArray.RemoveAll(); |
525 m_Buffer.Clear(); | 522 m_Buffer.Clear(); |
526 m_dwObjNum = 0; | 523 m_dwObjNum = 0; |
(...skipping 14 matching lines...) Expand all Loading... |
541 m_OffsetArray.Add(m_Buffer.GetLength()); | 538 m_OffsetArray.Add(m_Buffer.GetLength()); |
542 m_Buffer.AppendBlock(pBuffer, dwSize); | 539 m_Buffer.AppendBlock(pBuffer, dwSize); |
543 return 1; | 540 return 1; |
544 } | 541 } |
545 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { | 542 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { |
546 FXSYS_assert(pCreator); | 543 FXSYS_assert(pCreator); |
547 if (m_ObjNumArray.GetSize() == 0) { | 544 if (m_ObjNumArray.GetSize() == 0) { |
548 return 0; | 545 return 0; |
549 } | 546 } |
550 CFX_FileBufferArchive* pFile = &pCreator->m_File; | 547 CFX_FileBufferArchive* pFile = &pCreator->m_File; |
551 CPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler; | |
552 FX_FILESIZE ObjOffset = pCreator->m_Offset; | 548 FX_FILESIZE ObjOffset = pCreator->m_Offset; |
553 if (!m_dwObjNum) { | 549 if (!m_dwObjNum) { |
554 m_dwObjNum = ++pCreator->m_dwLastObjNum; | 550 m_dwObjNum = ++pCreator->m_dwLastObjNum; |
555 } | 551 } |
556 CFX_ByteTextBuf tempBuffer; | 552 CFX_ByteTextBuf tempBuffer; |
557 int32_t iCount = m_ObjNumArray.GetSize(); | 553 int32_t iCount = m_ObjNumArray.GetSize(); |
558 for (int32_t i = 0; i < iCount; i++) { | 554 for (int32_t i = 0; i < iCount; i++) { |
559 tempBuffer << m_ObjNumArray.ElementAt(i) << " " | 555 tempBuffer << m_ObjNumArray.ElementAt(i) << " " |
560 << m_OffsetArray.ElementAt(i) << " "; | 556 << m_OffsetArray.ElementAt(i) << " "; |
561 } | 557 } |
(...skipping 14 matching lines...) Expand all Loading... |
576 if (pFile->AppendString("/First ") < 0) { | 572 if (pFile->AppendString("/First ") < 0) { |
577 return -1; | 573 return -1; |
578 } | 574 } |
579 if ((len = pFile->AppendDWord((uint32_t)tempBuffer.GetLength())) < 0) { | 575 if ((len = pFile->AppendDWord((uint32_t)tempBuffer.GetLength())) < 0) { |
580 return -1; | 576 return -1; |
581 } | 577 } |
582 if (pFile->AppendString("/Length ") < 0) { | 578 if (pFile->AppendString("/Length ") < 0) { |
583 return -1; | 579 return -1; |
584 } | 580 } |
585 offset += len + 15; | 581 offset += len + 15; |
586 if (!pCreator->m_bCompress && !pHandler) { | 582 |
| 583 if (!pCreator->m_bCompress && !pCreator->m_pCryptoHandler) { |
587 if ((len = pFile->AppendDWord( | 584 if ((len = pFile->AppendDWord( |
588 (uint32_t)(tempBuffer.GetLength() + m_Buffer.GetLength()))) < 0) { | 585 (uint32_t)(tempBuffer.GetLength() + m_Buffer.GetLength()))) < 0) { |
589 return -1; | 586 return -1; |
590 } | 587 } |
591 offset += len; | 588 offset += len; |
592 if ((len = pFile->AppendString(">>stream\r\n")) < 0) { | 589 if ((len = pFile->AppendString(">>stream\r\n")) < 0) { |
593 return -1; | 590 return -1; |
594 } | 591 } |
595 if (pFile->AppendBlock(tempBuffer.GetBuffer(), tempBuffer.GetLength()) < | 592 if (pFile->AppendBlock(tempBuffer.GetBuffer(), tempBuffer.GetLength()) < |
596 0) { | 593 0) { |
597 return -1; | 594 return -1; |
598 } | 595 } |
599 if (pFile->AppendBlock(m_Buffer.GetBuffer(), m_Buffer.GetLength()) < 0) { | 596 if (pFile->AppendBlock(m_Buffer.GetBuffer(), m_Buffer.GetLength()) < 0) { |
600 return -1; | 597 return -1; |
601 } | 598 } |
602 offset += len + tempBuffer.GetLength() + m_Buffer.GetLength(); | 599 offset += len + tempBuffer.GetLength() + m_Buffer.GetLength(); |
603 } else { | 600 } else { |
604 tempBuffer << m_Buffer; | 601 tempBuffer << m_Buffer; |
605 CPDF_FlateEncoder encoder; | 602 CPDF_FlateEncoder encoder(tempBuffer.GetBuffer(), tempBuffer.GetLength(), |
606 encoder.Initialize(tempBuffer.GetBuffer(), tempBuffer.GetLength(), | 603 pCreator->m_bCompress); |
607 pCreator->m_bCompress); | 604 CPDF_Encryptor encryptor(pCreator->m_pCryptoHandler, m_dwObjNum, |
608 CPDF_Encryptor encryptor; | 605 encoder.m_pData, encoder.m_dwSize); |
609 encryptor.Initialize(pHandler, m_dwObjNum, encoder.m_pData, | |
610 encoder.m_dwSize); | |
611 if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { | 606 if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { |
612 return -1; | 607 return -1; |
613 } | 608 } |
614 offset += len; | 609 offset += len; |
615 if (pCreator->m_bCompress) { | 610 if (pCreator->m_bCompress) { |
616 if (pFile->AppendString("/Filter /FlateDecode") < 0) { | 611 if (pFile->AppendString("/Filter /FlateDecode") < 0) { |
617 return -1; | 612 return -1; |
618 } | 613 } |
619 offset += 20; | 614 offset += 20; |
620 } | 615 } |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 FX_CHAR offset_buf[20]; | 803 FX_CHAR offset_buf[20]; |
809 FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); | 804 FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); |
810 FXSYS_i64toa(m_PrevOffset, offset_buf, 10); | 805 FXSYS_i64toa(m_PrevOffset, offset_buf, 10); |
811 int32_t len = (int32_t)FXSYS_strlen(offset_buf); | 806 int32_t len = (int32_t)FXSYS_strlen(offset_buf); |
812 if (pFile->AppendBlock(offset_buf, len) < 0) { | 807 if (pFile->AppendBlock(offset_buf, len) < 0) { |
813 return FALSE; | 808 return FALSE; |
814 } | 809 } |
815 offset += len + 6; | 810 offset += len + 6; |
816 } | 811 } |
817 FX_BOOL bPredictor = TRUE; | 812 FX_BOOL bPredictor = TRUE; |
818 CPDF_FlateEncoder encoder; | 813 CPDF_FlateEncoder encoder(m_Buffer.GetBuffer(), m_Buffer.GetLength(), |
819 encoder.Initialize(m_Buffer.GetBuffer(), m_Buffer.GetLength(), | 814 pCreator->m_bCompress, bPredictor); |
820 pCreator->m_bCompress, bPredictor); | |
821 if (pCreator->m_bCompress) { | 815 if (pCreator->m_bCompress) { |
822 if (pFile->AppendString("/Filter /FlateDecode") < 0) { | 816 if (pFile->AppendString("/Filter /FlateDecode") < 0) { |
823 return FALSE; | 817 return FALSE; |
824 } | 818 } |
825 offset += 20; | 819 offset += 20; |
826 if (bPredictor) { | 820 if (bPredictor) { |
827 if ((len = pFile->AppendString( | 821 if ((len = pFile->AppendString( |
828 "/DecodeParms<</Columns 7/Predictor 12>>")) < 0) { | 822 "/DecodeParms<</Columns 7/Predictor 12>>")) < 0) { |
829 return FALSE; | 823 return FALSE; |
830 } | 824 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 m_IndexArray.push_back({objnum, 1}); | 902 m_IndexArray.push_back({objnum, 1}); |
909 } | 903 } |
910 CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) { | 904 CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) { |
911 m_pDocument = pDoc; | 905 m_pDocument = pDoc; |
912 m_pParser = (CPDF_Parser*)pDoc->m_pParser; | 906 m_pParser = (CPDF_Parser*)pDoc->m_pParser; |
913 m_bCompress = TRUE; | 907 m_bCompress = TRUE; |
914 if (m_pParser) { | 908 if (m_pParser) { |
915 m_pEncryptDict = m_pParser->GetEncryptDict(); | 909 m_pEncryptDict = m_pParser->GetEncryptDict(); |
916 m_pCryptoHandler = m_pParser->GetCryptoHandler(); | 910 m_pCryptoHandler = m_pParser->GetCryptoHandler(); |
917 } else { | 911 } else { |
918 m_pEncryptDict = NULL; | 912 m_pEncryptDict = nullptr; |
919 m_pCryptoHandler = NULL; | 913 m_pCryptoHandler = nullptr; |
920 } | 914 } |
921 m_bSecurityChanged = FALSE; | 915 m_bSecurityChanged = FALSE; |
922 m_bStandardSecurity = FALSE; | 916 m_pMetadata = nullptr; |
923 m_pMetadata = NULL; | |
924 m_bEncryptCloned = FALSE; | 917 m_bEncryptCloned = FALSE; |
925 m_bEncryptMetadata = FALSE; | 918 m_bEncryptMetadata = FALSE; |
926 m_Offset = 0; | 919 m_Offset = 0; |
927 m_iStage = -1; | 920 m_iStage = -1; |
928 m_dwFlags = 0; | 921 m_dwFlags = 0; |
929 m_Pos = NULL; | 922 m_Pos = nullptr; |
930 m_XrefStart = 0; | 923 m_XrefStart = 0; |
931 m_pXRefStream = NULL; | 924 m_pXRefStream = nullptr; |
932 m_ObjectStreamSize = 200; | 925 m_ObjectStreamSize = 200; |
933 m_dwLastObjNum = m_pDocument->GetLastObjNum(); | 926 m_dwLastObjNum = m_pDocument->GetLastObjNum(); |
934 m_pIDArray = NULL; | 927 m_pIDArray = nullptr; |
935 m_FileVersion = 0; | 928 m_FileVersion = 0; |
936 m_dwEnryptObjNum = 0; | 929 m_dwEnryptObjNum = 0; |
937 m_bNewCrypto = FALSE; | 930 m_bLocalCryptoHandler = FALSE; |
938 } | 931 } |
939 CPDF_Creator::~CPDF_Creator() { | 932 CPDF_Creator::~CPDF_Creator() { |
940 ResetStandardSecurity(); | 933 ResetStandardSecurity(); |
941 if (m_bEncryptCloned && m_pEncryptDict) { | 934 if (m_bEncryptCloned && m_pEncryptDict) { |
942 m_pEncryptDict->Release(); | 935 m_pEncryptDict->Release(); |
943 m_pEncryptDict = NULL; | 936 m_pEncryptDict = NULL; |
944 } | 937 } |
945 Clear(); | 938 Clear(); |
946 } | 939 } |
947 | 940 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 return -1; | 1011 return -1; |
1019 } | 1012 } |
1020 if (!m_pXRefStream->Start()) { | 1013 if (!m_pXRefStream->Start()) { |
1021 return -1; | 1014 return -1; |
1022 } | 1015 } |
1023 return 0; | 1016 return 0; |
1024 } | 1017 } |
1025 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, | 1018 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, |
1026 uint32_t objnum, | 1019 uint32_t objnum, |
1027 CPDF_CryptoHandler* pCrypto) { | 1020 CPDF_CryptoHandler* pCrypto) { |
1028 CPDF_FlateEncoder encoder; | 1021 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pStream->AsStream()), |
1029 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), | 1022 pStream == m_pMetadata ? FALSE : m_bCompress); |
1030 pStream == m_pMetadata ? FALSE : m_bCompress); | 1023 CPDF_Encryptor encryptor(pCrypto, objnum, encoder.m_pData, encoder.m_dwSize); |
1031 CPDF_Encryptor encryptor; | |
1032 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, | |
1033 encoder.m_dwSize)) { | |
1034 return -1; | |
1035 } | |
1036 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { | 1024 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { |
1037 encoder.CloneDict(); | 1025 encoder.CloneDict(); |
1038 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1026 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
1039 } | 1027 } |
1040 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1028 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1041 return -1; | 1029 return -1; |
1042 } | 1030 } |
1043 int len = m_File.AppendString("stream\r\n"); | 1031 int len = m_File.AppendString("stream\r\n"); |
1044 if (len < 0) { | 1032 if (len < 0) { |
1045 return -1; | 1033 return -1; |
(...skipping 14 matching lines...) Expand all Loading... |
1060 int32_t len = m_File.AppendDWord(objnum); | 1048 int32_t len = m_File.AppendDWord(objnum); |
1061 if (len < 0) | 1049 if (len < 0) |
1062 return -1; | 1050 return -1; |
1063 | 1051 |
1064 m_Offset += len; | 1052 m_Offset += len; |
1065 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) | 1053 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) |
1066 return -1; | 1054 return -1; |
1067 | 1055 |
1068 m_Offset += len; | 1056 m_Offset += len; |
1069 if (pObj->IsStream()) { | 1057 if (pObj->IsStream()) { |
1070 CPDF_CryptoHandler* pHandler = nullptr; | 1058 CPDF_CryptoHandler* pHandler = (pObj == m_pMetadata && !m_bEncryptMetadata) |
1071 pHandler = | 1059 ? nullptr |
1072 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; | 1060 : m_pCryptoHandler; |
1073 if (WriteStream(pObj, objnum, pHandler) < 0) | 1061 if (WriteStream(pObj, objnum, pHandler) < 0) |
1074 return -1; | 1062 return -1; |
1075 } else { | 1063 } else { |
1076 if (WriteDirectObj(objnum, pObj) < 0) | 1064 if (WriteDirectObj(objnum, pObj) < 0) |
1077 return -1; | 1065 return -1; |
1078 } | 1066 } |
1079 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) | 1067 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) |
1080 return -1; | 1068 return -1; |
1081 | 1069 |
1082 m_Offset += len; | 1070 m_Offset += len; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 CFX_ByteString str = pObj->GetString(); | 1111 CFX_ByteString str = pObj->GetString(); |
1124 FX_BOOL bHex = pObj->AsString()->IsHex(); | 1112 FX_BOOL bHex = pObj->AsString()->IsHex(); |
1125 if (!m_pCryptoHandler || !bEncrypt) { | 1113 if (!m_pCryptoHandler || !bEncrypt) { |
1126 CFX_ByteString content = PDF_EncodeString(str, bHex); | 1114 CFX_ByteString content = PDF_EncodeString(str, bHex); |
1127 if ((len = m_File.AppendString(content.AsStringC())) < 0) { | 1115 if ((len = m_File.AppendString(content.AsStringC())) < 0) { |
1128 return -1; | 1116 return -1; |
1129 } | 1117 } |
1130 m_Offset += len; | 1118 m_Offset += len; |
1131 break; | 1119 break; |
1132 } | 1120 } |
1133 CPDF_Encryptor encryptor; | 1121 CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), |
1134 encryptor.Initialize(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), | 1122 str.GetLength()); |
1135 str.GetLength()); | |
1136 CFX_ByteString content = PDF_EncodeString( | 1123 CFX_ByteString content = PDF_EncodeString( |
1137 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), | 1124 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), |
1138 bHex); | 1125 bHex); |
1139 if ((len = m_File.AppendString(content.AsStringC())) < 0) { | 1126 if ((len = m_File.AppendString(content.AsStringC())) < 0) { |
1140 return -1; | 1127 return -1; |
1141 } | 1128 } |
1142 m_Offset += len; | 1129 m_Offset += len; |
1143 break; | 1130 break; |
1144 } | 1131 } |
1145 case CPDF_Object::STREAM: { | 1132 case CPDF_Object::STREAM: { |
1146 CPDF_FlateEncoder encoder; | 1133 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pObj->AsStream()), |
1147 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), | 1134 m_bCompress); |
1148 m_bCompress); | 1135 CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, encoder.m_pData, |
1149 CPDF_Encryptor encryptor; | 1136 encoder.m_dwSize); |
1150 CPDF_CryptoHandler* pHandler = m_pCryptoHandler; | |
1151 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); | |
1152 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != | 1137 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != |
1153 encryptor.m_dwSize) { | 1138 encryptor.m_dwSize) { |
1154 encoder.CloneDict(); | 1139 encoder.CloneDict(); |
1155 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1140 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
1156 } | 1141 } |
1157 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1142 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1158 return -1; | 1143 return -1; |
1159 } | 1144 } |
1160 if ((len = m_File.AppendString("stream\r\n")) < 0) { | 1145 if ((len = m_File.AppendString("stream\r\n")) < 0) { |
1161 return -1; | 1146 return -1; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1217 } | 1202 } |
1218 } | 1203 } |
1219 } | 1204 } |
1220 if (m_File.AppendString("]") < 0) { | 1205 if (m_File.AppendString("]") < 0) { |
1221 return -1; | 1206 return -1; |
1222 } | 1207 } |
1223 m_Offset += 1; | 1208 m_Offset += 1; |
1224 break; | 1209 break; |
1225 } | 1210 } |
1226 case CPDF_Object::DICTIONARY: { | 1211 case CPDF_Object::DICTIONARY: { |
1227 if (!m_pCryptoHandler || pObj == m_pEncryptDict) { | 1212 if (!m_pCryptoHandler || pObj == m_pEncryptDict) |
1228 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset); | 1213 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset); |
1229 } | 1214 if (m_File.AppendString("<<") < 0) |
1230 if (m_File.AppendString("<<") < 0) { | |
1231 return -1; | 1215 return -1; |
1232 } | 1216 |
1233 m_Offset += 2; | 1217 m_Offset += 2; |
1234 const CPDF_Dictionary* p = pObj->AsDictionary(); | 1218 const CPDF_Dictionary* p = pObj->AsDictionary(); |
1235 bool bSignDict = p->IsSignatureDict(); | 1219 bool bSignDict = p->IsSignatureDict(); |
1236 for (const auto& it : *p) { | 1220 for (const auto& it : *p) { |
1237 FX_BOOL bSignValue = FALSE; | 1221 FX_BOOL bSignValue = FALSE; |
1238 const CFX_ByteString& key = it.first; | 1222 const CFX_ByteString& key = it.first; |
1239 CPDF_Object* pValue = it.second; | 1223 CPDF_Object* pValue = it.second; |
1240 if (m_File.AppendString("/") < 0) { | 1224 if (m_File.AppendString("/") < 0) { |
1241 return -1; | 1225 return -1; |
1242 } | 1226 } |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2006 } | 1990 } |
2007 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone()); | 1991 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone()); |
2008 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { | 1992 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { |
2009 if (m_pEncryptDict->GetStringBy("Filter") == "Standard") { | 1993 if (m_pEncryptDict->GetStringBy("Filter") == "Standard") { |
2010 CFX_ByteString user_pass = m_pParser->GetPassword(); | 1994 CFX_ByteString user_pass = m_pParser->GetPassword(); |
2011 uint32_t flag = PDF_ENCRYPT_CONTENT; | 1995 uint32_t flag = PDF_ENCRYPT_CONTENT; |
2012 | 1996 |
2013 CPDF_SecurityHandler handler; | 1997 CPDF_SecurityHandler handler; |
2014 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(), | 1998 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(), |
2015 user_pass.GetLength(), flag); | 1999 user_pass.GetLength(), flag); |
2016 if (m_bNewCrypto) { | 2000 if (m_bLocalCryptoHandler) |
2017 delete m_pCryptoHandler; | 2001 delete m_pCryptoHandler; |
2018 } | |
2019 m_pCryptoHandler = new CPDF_CryptoHandler; | 2002 m_pCryptoHandler = new CPDF_CryptoHandler; |
2020 m_pCryptoHandler->Init(m_pEncryptDict, &handler); | 2003 m_pCryptoHandler->Init(m_pEncryptDict, &handler); |
2021 m_bNewCrypto = TRUE; | 2004 m_bLocalCryptoHandler = TRUE; |
2022 m_bSecurityChanged = TRUE; | 2005 m_bSecurityChanged = TRUE; |
2023 } | 2006 } |
2024 } | 2007 } |
2025 } | 2008 } |
2026 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { | 2009 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { |
2027 if (m_iStage < 0) { | 2010 if (m_iStage < 0) { |
2028 return m_iStage; | 2011 return m_iStage; |
2029 } | 2012 } |
2030 int32_t iRet = 0; | 2013 int32_t iRet = 0; |
2031 while (m_iStage < 100) { | 2014 while (m_iStage < 100) { |
(...skipping 20 matching lines...) Expand all Loading... |
2052 FX_BOOL CPDF_Creator::SetFileVersion(int32_t fileVersion) { | 2035 FX_BOOL CPDF_Creator::SetFileVersion(int32_t fileVersion) { |
2053 if (fileVersion < 10 || fileVersion > 17) { | 2036 if (fileVersion < 10 || fileVersion > 17) { |
2054 return FALSE; | 2037 return FALSE; |
2055 } | 2038 } |
2056 m_FileVersion = fileVersion; | 2039 m_FileVersion = fileVersion; |
2057 return TRUE; | 2040 return TRUE; |
2058 } | 2041 } |
2059 void CPDF_Creator::RemoveSecurity() { | 2042 void CPDF_Creator::RemoveSecurity() { |
2060 ResetStandardSecurity(); | 2043 ResetStandardSecurity(); |
2061 m_bSecurityChanged = TRUE; | 2044 m_bSecurityChanged = TRUE; |
2062 m_pEncryptDict = NULL; | 2045 m_pEncryptDict = nullptr; |
2063 m_pCryptoHandler = NULL; | 2046 m_pCryptoHandler = nullptr; |
2064 } | 2047 } |
2065 void CPDF_Creator::ResetStandardSecurity() { | 2048 void CPDF_Creator::ResetStandardSecurity() { |
2066 if (m_bStandardSecurity || m_bNewCrypto) { | 2049 if (!m_bLocalCryptoHandler) |
2067 delete m_pCryptoHandler; | |
2068 m_pCryptoHandler = NULL; | |
2069 } | |
2070 m_bNewCrypto = FALSE; | |
2071 if (!m_bStandardSecurity) { | |
2072 return; | 2050 return; |
2073 } | 2051 |
2074 if (m_pEncryptDict) { | 2052 delete m_pCryptoHandler; |
2075 m_pEncryptDict->Release(); | 2053 m_pCryptoHandler = nullptr; |
2076 m_pEncryptDict = NULL; | 2054 m_bLocalCryptoHandler = FALSE; |
2077 } | |
2078 m_bStandardSecurity = FALSE; | |
2079 } | 2055 } |
OLD | NEW |