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 uint8_t* m_pData; | 398 uint8_t* m_pData; |
399 uint32_t m_dwSize; | 399 uint32_t m_dwSize; |
400 CPDF_Dictionary* m_pDict; | 400 CPDF_Dictionary* m_pDict; |
401 FX_BOOL m_bCloned; | 401 FX_BOOL m_bCloned; |
402 FX_BOOL m_bNewData; | 402 FX_BOOL m_bNewData; |
403 CPDF_StreamAcc m_Acc; | 403 CPDF_StreamAcc m_Acc; |
404 }; | 404 }; |
405 CPDF_FlateEncoder::CPDF_FlateEncoder() { | 405 |
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() { | 406 void CPDF_FlateEncoder::CloneDict() { |
413 if (!m_bCloned) { | 407 if (!m_bCloned) { |
414 m_pDict = ToDictionary(m_pDict->Clone()); | 408 m_pDict = ToDictionary(m_pDict->Clone()); |
415 ASSERT(m_pDict); | 409 ASSERT(m_pDict); |
416 m_bCloned = TRUE; | 410 m_bCloned = TRUE; |
417 } | 411 } |
418 } | 412 } |
419 FX_BOOL CPDF_FlateEncoder::Initialize(CPDF_Stream* pStream, | 413 |
420 FX_BOOL bFlateEncode) { | 414 CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, FX_BOOL bFlateEncode) |
415 : m_pData(nullptr), | |
416 m_dwSize(0), | |
417 m_pDict(nullptr), | |
418 m_bCloned(FALSE), | |
419 m_bNewData(FALSE) { | |
421 m_Acc.LoadAllData(pStream, TRUE); | 420 m_Acc.LoadAllData(pStream, TRUE); |
422 if ((pStream && pStream->GetDict() && | 421 if ((pStream && pStream->GetDict() && |
423 pStream->GetDict()->KeyExist("Filter")) || | 422 pStream->GetDict()->KeyExist("Filter")) || |
424 !bFlateEncode) { | 423 !bFlateEncode) { |
425 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { | 424 if (pStream->GetDict()->KeyExist("Filter") && !bFlateEncode) { |
426 CPDF_StreamAcc destAcc; | 425 CPDF_StreamAcc destAcc; |
427 destAcc.LoadAllData(pStream); | 426 destAcc.LoadAllData(pStream); |
428 m_dwSize = destAcc.GetSize(); | 427 m_dwSize = destAcc.GetSize(); |
429 m_pData = (uint8_t*)destAcc.DetachData(); | 428 m_pData = (uint8_t*)destAcc.DetachData(); |
430 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 429 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
431 m_pDict->RemoveAt("Filter"); | 430 m_pDict->RemoveAt("Filter"); |
432 m_bNewData = TRUE; | 431 m_bNewData = TRUE; |
433 m_bCloned = TRUE; | 432 m_bCloned = TRUE; |
434 } else { | 433 } else { |
435 m_pData = (uint8_t*)m_Acc.GetData(); | 434 m_pData = (uint8_t*)m_Acc.GetData(); |
436 m_dwSize = m_Acc.GetSize(); | 435 m_dwSize = m_Acc.GetSize(); |
437 m_pDict = pStream->GetDict(); | 436 m_pDict = pStream->GetDict(); |
438 } | 437 } |
439 return TRUE; | 438 return; |
440 } | 439 } |
441 m_pData = NULL; | 440 |
442 m_dwSize = 0; | |
443 m_bNewData = TRUE; | 441 m_bNewData = TRUE; |
444 m_bCloned = TRUE; | 442 m_bCloned = TRUE; |
445 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); | 443 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), m_pData, m_dwSize); |
446 m_pDict = ToDictionary(pStream->GetDict()->Clone()); | 444 m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
447 m_pDict->SetAtInteger("Length", m_dwSize); | 445 m_pDict->SetAtInteger("Length", m_dwSize); |
448 m_pDict->SetAtName("Filter", "FlateDecode"); | 446 m_pDict->SetAtName("Filter", "FlateDecode"); |
449 m_pDict->RemoveAt("DecodeParms"); | 447 m_pDict->RemoveAt("DecodeParms"); |
450 return TRUE; | |
451 } | 448 } |
452 FX_BOOL CPDF_FlateEncoder::Initialize(const uint8_t* pBuffer, | 449 |
453 uint32_t size, | 450 CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, |
454 FX_BOOL bFlateEncode, | 451 uint32_t size, |
455 FX_BOOL bXRefStream) { | 452 FX_BOOL bFlateEncode, |
453 FX_BOOL bXRefStream) | |
454 : m_pData(nullptr), | |
455 m_dwSize(0), | |
456 m_pDict(nullptr), | |
457 m_bCloned(FALSE), | |
458 m_bNewData(FALSE) { | |
456 if (!bFlateEncode) { | 459 if (!bFlateEncode) { |
457 m_pData = (uint8_t*)pBuffer; | 460 m_pData = (uint8_t*)pBuffer; |
458 m_dwSize = size; | 461 m_dwSize = size; |
459 return TRUE; | 462 return; |
460 } | 463 } |
461 m_bNewData = TRUE; | 464 m_bNewData = TRUE; |
462 if (bXRefStream) { | 465 if (bXRefStream) |
463 ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); | 466 ::FlateEncode(pBuffer, size, 12, 1, 8, 7, m_pData, m_dwSize); |
464 } else { | 467 else |
465 ::FlateEncode(pBuffer, size, m_pData, m_dwSize); | 468 ::FlateEncode(pBuffer, size, m_pData, m_dwSize); |
466 } | |
467 return TRUE; | |
468 } | 469 } |
470 | |
469 CPDF_FlateEncoder::~CPDF_FlateEncoder() { | 471 CPDF_FlateEncoder::~CPDF_FlateEncoder() { |
470 if (m_bCloned && m_pDict) { | 472 if (m_bCloned && m_pDict) |
471 m_pDict->Release(); | 473 m_pDict->Release(); |
472 } | 474 if (m_bNewData) |
473 if (m_bNewData) { | |
474 FX_Free(m_pData); | 475 FX_Free(m_pData); |
475 } | |
476 } | 476 } |
477 | |
477 class CPDF_Encryptor { | 478 class CPDF_Encryptor { |
478 public: | 479 public: |
479 CPDF_Encryptor(); | 480 CPDF_Encryptor(CPDF_CryptoHandler* pHandler, |
481 int objnum, | |
482 uint8_t* src_data, | |
483 uint32_t src_size); | |
480 ~CPDF_Encryptor(); | 484 ~CPDF_Encryptor(); |
481 FX_BOOL Initialize(CPDF_CryptoHandler* pHandler, | 485 |
482 int objnum, | |
483 uint8_t* src_data, | |
484 uint32_t src_size); | |
485 uint8_t* m_pData; | 486 uint8_t* m_pData; |
486 uint32_t m_dwSize; | 487 uint32_t m_dwSize; |
487 FX_BOOL m_bNewBuf; | 488 FX_BOOL m_bNewBuf; |
488 }; | 489 }; |
489 CPDF_Encryptor::CPDF_Encryptor() { | 490 |
490 m_pData = NULL; | 491 CPDF_Encryptor::CPDF_Encryptor(CPDF_CryptoHandler* pHandler, |
491 m_dwSize = 0; | 492 int objnum, |
492 m_bNewBuf = FALSE; | 493 uint8_t* src_data, |
493 } | 494 uint32_t src_size) |
494 FX_BOOL CPDF_Encryptor::Initialize(CPDF_CryptoHandler* pHandler, | 495 : m_pData(nullptr), m_dwSize(0), m_bNewBuf(FALSE) { |
495 int objnum, | 496 if (src_size == 0) |
496 uint8_t* src_data, | 497 return; |
497 uint32_t src_size) { | 498 |
498 if (src_size == 0) { | |
499 return TRUE; | |
500 } | |
501 if (!pHandler) { | 499 if (!pHandler) { |
502 m_pData = (uint8_t*)src_data; | 500 m_pData = (uint8_t*)src_data; |
503 m_dwSize = src_size; | 501 m_dwSize = src_size; |
504 m_bNewBuf = FALSE; | 502 m_bNewBuf = FALSE; |
Lei Zhang
2016/04/19 19:16:39
No need to set to FALSE again.
dsinclair
2016/04/19 19:50:22
Done.
| |
505 return TRUE; | 503 return; |
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.get()) { | |
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.get(), 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
906 m_IndexArray.back().count += 1; | 900 m_IndexArray.back().count += 1; |
907 else | 901 else |
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.reset(m_pParser->GetCryptoHandler()); |
Lei Zhang
2016/04/19 19:16:39
I'm a bit worried about who actually owns the poin
dsinclair
2016/04/19 19:50:22
You're right, this is broken.
| |
917 } else { | 911 } else { |
918 m_pEncryptDict = NULL; | 912 m_pEncryptDict = NULL; |
919 m_pCryptoHandler = NULL; | 913 m_pCryptoHandler.reset(); |
920 } | 914 } |
921 m_bSecurityChanged = FALSE; | 915 m_bSecurityChanged = FALSE; |
922 m_bStandardSecurity = FALSE; | |
923 m_pMetadata = NULL; | 916 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 = NULL; |
930 m_XrefStart = 0; | 923 m_XrefStart = 0; |
931 m_pXRefStream = NULL; | 924 m_pXRefStream = NULL; |
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 = NULL; |
935 m_FileVersion = 0; | 928 m_FileVersion = 0; |
936 m_dwEnryptObjNum = 0; | 929 m_dwEnryptObjNum = 0; |
937 m_bNewCrypto = FALSE; | |
938 } | 930 } |
939 CPDF_Creator::~CPDF_Creator() { | 931 CPDF_Creator::~CPDF_Creator() { |
940 ResetStandardSecurity(); | 932 ResetStandardSecurity(); |
941 if (m_bEncryptCloned && m_pEncryptDict) { | 933 if (m_bEncryptCloned && m_pEncryptDict) { |
942 m_pEncryptDict->Release(); | 934 m_pEncryptDict->Release(); |
943 m_pEncryptDict = NULL; | 935 m_pEncryptDict = NULL; |
944 } | 936 } |
945 Clear(); | 937 Clear(); |
946 } | 938 } |
947 | 939 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1018 return -1; | 1010 return -1; |
1019 } | 1011 } |
1020 if (!m_pXRefStream->Start()) { | 1012 if (!m_pXRefStream->Start()) { |
1021 return -1; | 1013 return -1; |
1022 } | 1014 } |
1023 return 0; | 1015 return 0; |
1024 } | 1016 } |
1025 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, | 1017 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, |
1026 uint32_t objnum, | 1018 uint32_t objnum, |
1027 CPDF_CryptoHandler* pCrypto) { | 1019 CPDF_CryptoHandler* pCrypto) { |
1028 CPDF_FlateEncoder encoder; | 1020 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pStream->AsStream()), |
1029 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), | 1021 pStream == m_pMetadata ? FALSE : m_bCompress); |
1030 pStream == m_pMetadata ? FALSE : m_bCompress); | 1022 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) { | 1023 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != encryptor.m_dwSize) { |
1037 encoder.CloneDict(); | 1024 encoder.CloneDict(); |
1038 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1025 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
1039 } | 1026 } |
1040 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1027 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1041 return -1; | 1028 return -1; |
1042 } | 1029 } |
1043 int len = m_File.AppendString("stream\r\n"); | 1030 int len = m_File.AppendString("stream\r\n"); |
1044 if (len < 0) { | 1031 if (len < 0) { |
1045 return -1; | 1032 return -1; |
(...skipping 14 matching lines...) Expand all Loading... | |
1060 int32_t len = m_File.AppendDWord(objnum); | 1047 int32_t len = m_File.AppendDWord(objnum); |
1061 if (len < 0) | 1048 if (len < 0) |
1062 return -1; | 1049 return -1; |
1063 | 1050 |
1064 m_Offset += len; | 1051 m_Offset += len; |
1065 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) | 1052 if ((len = m_File.AppendString(" 0 obj\r\n")) < 0) |
1066 return -1; | 1053 return -1; |
1067 | 1054 |
1068 m_Offset += len; | 1055 m_Offset += len; |
1069 if (pObj->IsStream()) { | 1056 if (pObj->IsStream()) { |
1070 CPDF_CryptoHandler* pHandler = nullptr; | 1057 CPDF_CryptoHandler* pHandler = (pObj == m_pMetadata && !m_bEncryptMetadata) |
1071 pHandler = | 1058 ? nullptr |
1072 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; | 1059 : m_pCryptoHandler.get(); |
1073 if (WriteStream(pObj, objnum, pHandler) < 0) | 1060 if (WriteStream(pObj, objnum, pHandler) < 0) |
1074 return -1; | 1061 return -1; |
1075 } else { | 1062 } else { |
1076 if (WriteDirectObj(objnum, pObj) < 0) | 1063 if (WriteDirectObj(objnum, pObj) < 0) |
1077 return -1; | 1064 return -1; |
1078 } | 1065 } |
1079 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) | 1066 if ((len = m_File.AppendString("\r\nendobj\r\n")) < 0) |
1080 return -1; | 1067 return -1; |
1081 | 1068 |
1082 m_Offset += len; | 1069 m_Offset += len; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1115 return -1; | 1102 return -1; |
1116 } | 1103 } |
1117 if ((len = m_File.AppendString(pObj->GetString().AsStringC())) < 0) { | 1104 if ((len = m_File.AppendString(pObj->GetString().AsStringC())) < 0) { |
1118 return -1; | 1105 return -1; |
1119 } | 1106 } |
1120 m_Offset += len + 1; | 1107 m_Offset += len + 1; |
1121 break; | 1108 break; |
1122 case CPDF_Object::STRING: { | 1109 case CPDF_Object::STRING: { |
1123 CFX_ByteString str = pObj->GetString(); | 1110 CFX_ByteString str = pObj->GetString(); |
1124 FX_BOOL bHex = pObj->AsString()->IsHex(); | 1111 FX_BOOL bHex = pObj->AsString()->IsHex(); |
1125 if (!m_pCryptoHandler || !bEncrypt) { | 1112 if (!m_pCryptoHandler.get() || !bEncrypt) { |
1126 CFX_ByteString content = PDF_EncodeString(str, bHex); | 1113 CFX_ByteString content = PDF_EncodeString(str, bHex); |
1127 if ((len = m_File.AppendString(content.AsStringC())) < 0) { | 1114 if ((len = m_File.AppendString(content.AsStringC())) < 0) { |
1128 return -1; | 1115 return -1; |
1129 } | 1116 } |
1130 m_Offset += len; | 1117 m_Offset += len; |
1131 break; | 1118 break; |
1132 } | 1119 } |
1133 CPDF_Encryptor encryptor; | 1120 CPDF_Encryptor encryptor(m_pCryptoHandler.get(), objnum, |
1134 encryptor.Initialize(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), | 1121 (uint8_t*)str.c_str(), str.GetLength()); |
1135 str.GetLength()); | |
1136 CFX_ByteString content = PDF_EncodeString( | 1122 CFX_ByteString content = PDF_EncodeString( |
1137 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), | 1123 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), |
1138 bHex); | 1124 bHex); |
1139 if ((len = m_File.AppendString(content.AsStringC())) < 0) { | 1125 if ((len = m_File.AppendString(content.AsStringC())) < 0) { |
1140 return -1; | 1126 return -1; |
1141 } | 1127 } |
1142 m_Offset += len; | 1128 m_Offset += len; |
1143 break; | 1129 break; |
1144 } | 1130 } |
1145 case CPDF_Object::STREAM: { | 1131 case CPDF_Object::STREAM: { |
1146 CPDF_FlateEncoder encoder; | 1132 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pObj->AsStream()), |
1147 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), | 1133 m_bCompress); |
1148 m_bCompress); | 1134 CPDF_Encryptor encryptor(m_pCryptoHandler.get(), objnum, encoder.m_pData, |
1149 CPDF_Encryptor encryptor; | 1135 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") != | 1136 if ((uint32_t)encoder.m_pDict->GetIntegerBy("Length") != |
1153 encryptor.m_dwSize) { | 1137 encryptor.m_dwSize) { |
1154 encoder.CloneDict(); | 1138 encoder.CloneDict(); |
1155 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); | 1139 encoder.m_pDict->SetAtInteger("Length", encryptor.m_dwSize); |
1156 } | 1140 } |
1157 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1141 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1158 return -1; | 1142 return -1; |
1159 } | 1143 } |
1160 if ((len = m_File.AppendString("stream\r\n")) < 0) { | 1144 if ((len = m_File.AppendString("stream\r\n")) < 0) { |
1161 return -1; | 1145 return -1; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1217 } | 1201 } |
1218 } | 1202 } |
1219 } | 1203 } |
1220 if (m_File.AppendString("]") < 0) { | 1204 if (m_File.AppendString("]") < 0) { |
1221 return -1; | 1205 return -1; |
1222 } | 1206 } |
1223 m_Offset += 1; | 1207 m_Offset += 1; |
1224 break; | 1208 break; |
1225 } | 1209 } |
1226 case CPDF_Object::DICTIONARY: { | 1210 case CPDF_Object::DICTIONARY: { |
1227 if (!m_pCryptoHandler || pObj == m_pEncryptDict) { | 1211 if (!m_pCryptoHandler.get() || pObj == m_pEncryptDict) { |
1228 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset); | 1212 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset); |
1229 } | 1213 } |
1230 if (m_File.AppendString("<<") < 0) { | 1214 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; |
(...skipping 768 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 m_pCryptoHandler.reset(new CPDF_CryptoHandler); |
2017 delete m_pCryptoHandler; | |
2018 } | |
2019 m_pCryptoHandler = new CPDF_CryptoHandler; | |
2020 m_pCryptoHandler->Init(m_pEncryptDict, &handler); | 2001 m_pCryptoHandler->Init(m_pEncryptDict, &handler); |
2021 m_bNewCrypto = TRUE; | |
2022 m_bSecurityChanged = TRUE; | 2002 m_bSecurityChanged = TRUE; |
2023 } | 2003 } |
2024 } | 2004 } |
2025 } | 2005 } |
2026 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { | 2006 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { |
2027 if (m_iStage < 0) { | 2007 if (m_iStage < 0) { |
2028 return m_iStage; | 2008 return m_iStage; |
2029 } | 2009 } |
2030 int32_t iRet = 0; | 2010 int32_t iRet = 0; |
2031 while (m_iStage < 100) { | 2011 while (m_iStage < 100) { |
(...skipping 20 matching lines...) Expand all Loading... | |
2052 FX_BOOL CPDF_Creator::SetFileVersion(int32_t fileVersion) { | 2032 FX_BOOL CPDF_Creator::SetFileVersion(int32_t fileVersion) { |
2053 if (fileVersion < 10 || fileVersion > 17) { | 2033 if (fileVersion < 10 || fileVersion > 17) { |
2054 return FALSE; | 2034 return FALSE; |
2055 } | 2035 } |
2056 m_FileVersion = fileVersion; | 2036 m_FileVersion = fileVersion; |
2057 return TRUE; | 2037 return TRUE; |
2058 } | 2038 } |
2059 void CPDF_Creator::RemoveSecurity() { | 2039 void CPDF_Creator::RemoveSecurity() { |
2060 ResetStandardSecurity(); | 2040 ResetStandardSecurity(); |
2061 m_bSecurityChanged = TRUE; | 2041 m_bSecurityChanged = TRUE; |
2062 m_pEncryptDict = NULL; | 2042 m_pEncryptDict = nullptr; |
2063 m_pCryptoHandler = NULL; | 2043 m_pCryptoHandler.reset(); |
2064 } | 2044 } |
2065 void CPDF_Creator::ResetStandardSecurity() { | 2045 void CPDF_Creator::ResetStandardSecurity() { |
2066 if (m_bStandardSecurity || m_bNewCrypto) { | 2046 m_pCryptoHandler.reset(); |
2067 delete m_pCryptoHandler; | |
2068 m_pCryptoHandler = NULL; | |
2069 } | |
2070 m_bNewCrypto = FALSE; | |
2071 if (!m_bStandardSecurity) { | |
2072 return; | |
2073 } | |
2074 if (m_pEncryptDict) { | |
2075 m_pEncryptDict->Release(); | |
2076 m_pEncryptDict = NULL; | |
2077 } | |
2078 m_bStandardSecurity = FALSE; | |
2079 } | 2047 } |
OLD | NEW |