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 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 } | 509 } |
510 | 510 |
511 CPDF_Encryptor::~CPDF_Encryptor() { | 511 CPDF_Encryptor::~CPDF_Encryptor() { |
512 if (m_bNewBuf) | 512 if (m_bNewBuf) |
513 FX_Free(m_pData); | 513 FX_Free(m_pData); |
514 } | 514 } |
515 | 515 |
516 } // namespace | 516 } // namespace |
517 | 517 |
518 CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {} | 518 CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {} |
519 FX_BOOL CPDF_ObjectStream::Start() { | 519 |
520 m_ObjNumArray.RemoveAll(); | 520 void CPDF_ObjectStream::Start() { |
521 m_OffsetArray.RemoveAll(); | 521 m_Items.clear(); |
522 m_Buffer.Clear(); | 522 m_Buffer.Clear(); |
523 m_dwObjNum = 0; | 523 m_dwObjNum = 0; |
524 m_index = 0; | 524 m_index = 0; |
525 return TRUE; | |
526 } | 525 } |
527 int32_t CPDF_ObjectStream::CompressIndirectObject(uint32_t dwObjNum, | 526 |
528 const CPDF_Object* pObj) { | 527 void CPDF_ObjectStream::CompressIndirectObject(uint32_t dwObjNum, |
529 m_ObjNumArray.Add(dwObjNum); | 528 const CPDF_Object* pObj) { |
530 m_OffsetArray.Add(m_Buffer.GetLength()); | 529 m_Items.push_back({dwObjNum, m_Buffer.GetLength()}); |
531 m_Buffer << pObj; | 530 m_Buffer << pObj; |
532 return 1; | |
533 } | 531 } |
534 int32_t CPDF_ObjectStream::CompressIndirectObject(uint32_t dwObjNum, | 532 |
535 const uint8_t* pBuffer, | 533 void CPDF_ObjectStream::CompressIndirectObject(uint32_t dwObjNum, |
536 uint32_t dwSize) { | 534 const uint8_t* pBuffer, |
537 m_ObjNumArray.Add(dwObjNum); | 535 uint32_t dwSize) { |
538 m_OffsetArray.Add(m_Buffer.GetLength()); | 536 m_Items.push_back({dwObjNum, m_Buffer.GetLength()}); |
539 m_Buffer.AppendBlock(pBuffer, dwSize); | 537 m_Buffer.AppendBlock(pBuffer, dwSize); |
540 return 1; | |
541 } | 538 } |
| 539 |
542 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { | 540 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { |
543 FXSYS_assert(pCreator); | 541 FXSYS_assert(pCreator); |
544 if (m_ObjNumArray.GetSize() == 0) { | 542 if (m_Items.empty()) |
545 return 0; | 543 return 0; |
546 } | 544 |
547 CFX_FileBufferArchive* pFile = &pCreator->m_File; | 545 CFX_FileBufferArchive* pFile = &pCreator->m_File; |
548 FX_FILESIZE ObjOffset = pCreator->m_Offset; | 546 FX_FILESIZE ObjOffset = pCreator->m_Offset; |
549 if (!m_dwObjNum) { | 547 if (!m_dwObjNum) { |
550 m_dwObjNum = ++pCreator->m_dwLastObjNum; | 548 m_dwObjNum = ++pCreator->m_dwLastObjNum; |
551 } | 549 } |
552 CFX_ByteTextBuf tempBuffer; | 550 CFX_ByteTextBuf tempBuffer; |
553 int32_t iCount = m_ObjNumArray.GetSize(); | 551 for (const auto& pair : m_Items) |
554 for (int32_t i = 0; i < iCount; i++) { | 552 tempBuffer << pair.objnum << " " << pair.offset << " "; |
555 tempBuffer << m_ObjNumArray.ElementAt(i) << " " | 553 |
556 << m_OffsetArray.ElementAt(i) << " "; | |
557 } | |
558 FX_FILESIZE& offset = pCreator->m_Offset; | 554 FX_FILESIZE& offset = pCreator->m_Offset; |
559 int32_t len = pFile->AppendDWord(m_dwObjNum); | 555 int32_t len = pFile->AppendDWord(m_dwObjNum); |
560 if (len < 0) { | 556 if (len < 0) { |
561 return -1; | 557 return -1; |
562 } | 558 } |
563 offset += len; | 559 offset += len; |
564 if ((len = pFile->AppendString(" 0 obj\r\n<</Type /ObjStm /N ")) < 0) { | 560 if ((len = pFile->AppendString(" 0 obj\r\n<</Type /ObjStm /N ")) < 0) { |
565 return -1; | 561 return -1; |
566 } | 562 } |
567 offset += len; | 563 offset += len; |
568 if ((len = pFile->AppendDWord((uint32_t)iCount)) < 0) { | 564 uint32_t iCount = pdfium::CollectionSize<uint32_t>(m_Items); |
| 565 if ((len = pFile->AppendDWord(iCount)) < 0) { |
569 return -1; | 566 return -1; |
570 } | 567 } |
571 offset += len; | 568 offset += len; |
572 if (pFile->AppendString("/First ") < 0) { | 569 if (pFile->AppendString("/First ") < 0) { |
573 return -1; | 570 return -1; |
574 } | 571 } |
575 if ((len = pFile->AppendDWord((uint32_t)tempBuffer.GetLength())) < 0) { | 572 if ((len = pFile->AppendDWord((uint32_t)tempBuffer.GetLength())) < 0) { |
576 return -1; | 573 return -1; |
577 } | 574 } |
578 if (pFile->AppendString("/Length ") < 0) { | 575 if (pFile->AppendString("/Length ") < 0) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 : m_PrevOffset(0), m_dwTempObjNum(0), m_iSeg(0) {} | 628 : m_PrevOffset(0), m_dwTempObjNum(0), m_iSeg(0) {} |
632 FX_BOOL CPDF_XRefStream::Start() { | 629 FX_BOOL CPDF_XRefStream::Start() { |
633 m_IndexArray.clear(); | 630 m_IndexArray.clear(); |
634 m_Buffer.Clear(); | 631 m_Buffer.Clear(); |
635 m_iSeg = 0; | 632 m_iSeg = 0; |
636 return TRUE; | 633 return TRUE; |
637 } | 634 } |
638 int32_t CPDF_XRefStream::CompressIndirectObject(uint32_t dwObjNum, | 635 int32_t CPDF_XRefStream::CompressIndirectObject(uint32_t dwObjNum, |
639 const CPDF_Object* pObj, | 636 const CPDF_Object* pObj, |
640 CPDF_Creator* pCreator) { | 637 CPDF_Creator* pCreator) { |
641 if (!pCreator) { | 638 if (!pCreator) |
642 return 0; | 639 return 0; |
643 } | 640 |
644 m_ObjStream.CompressIndirectObject(dwObjNum, pObj); | 641 m_ObjStream.CompressIndirectObject(dwObjNum, pObj); |
645 if (m_ObjStream.m_ObjNumArray.GetSize() < pCreator->m_ObjectStreamSize && | 642 if (pdfium::CollectionSize<int32_t>(m_ObjStream.m_Items) < |
| 643 pCreator->m_ObjectStreamSize && |
646 m_ObjStream.m_Buffer.GetLength() < PDF_OBJECTSTREAM_MAXLENGTH) { | 644 m_ObjStream.m_Buffer.GetLength() < PDF_OBJECTSTREAM_MAXLENGTH) { |
647 return 1; | 645 return 1; |
648 } | 646 } |
649 return EndObjectStream(pCreator); | 647 return EndObjectStream(pCreator); |
650 } | 648 } |
651 int32_t CPDF_XRefStream::CompressIndirectObject(uint32_t dwObjNum, | 649 int32_t CPDF_XRefStream::CompressIndirectObject(uint32_t dwObjNum, |
652 const uint8_t* pBuffer, | 650 const uint8_t* pBuffer, |
653 uint32_t dwSize, | 651 uint32_t dwSize, |
654 CPDF_Creator* pCreator) { | 652 CPDF_Creator* pCreator) { |
655 if (!pCreator) { | 653 if (!pCreator) |
656 return 0; | 654 return 0; |
657 } | 655 |
658 m_ObjStream.CompressIndirectObject(dwObjNum, pBuffer, dwSize); | 656 m_ObjStream.CompressIndirectObject(dwObjNum, pBuffer, dwSize); |
659 if (m_ObjStream.m_ObjNumArray.GetSize() < pCreator->m_ObjectStreamSize && | 657 if (pdfium::CollectionSize<int32_t>(m_ObjStream.m_Items) < |
| 658 pCreator->m_ObjectStreamSize && |
660 m_ObjStream.m_Buffer.GetLength() < PDF_OBJECTSTREAM_MAXLENGTH) { | 659 m_ObjStream.m_Buffer.GetLength() < PDF_OBJECTSTREAM_MAXLENGTH) { |
661 return 1; | 660 return 1; |
662 } | 661 } |
663 return EndObjectStream(pCreator); | 662 return EndObjectStream(pCreator); |
664 } | 663 } |
665 | 664 |
666 int32_t CPDF_XRefStream::EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF) { | 665 int32_t CPDF_XRefStream::EndObjectStream(CPDF_Creator* pCreator, FX_BOOL bEOF) { |
667 FX_FILESIZE objOffset = 0; | 666 FX_FILESIZE objOffset = 0; |
668 if (bEOF) { | 667 if (bEOF) { |
669 objOffset = m_ObjStream.End(pCreator); | 668 objOffset = m_ObjStream.End(pCreator); |
670 if (objOffset < 0) { | 669 if (objOffset < 0) { |
671 return -1; | 670 return -1; |
672 } | 671 } |
673 } | 672 } |
674 uint32_t& dwObjStmNum = m_ObjStream.m_dwObjNum; | 673 uint32_t& dwObjStmNum = m_ObjStream.m_dwObjNum; |
675 if (!dwObjStmNum) { | 674 if (!dwObjStmNum) { |
676 dwObjStmNum = ++pCreator->m_dwLastObjNum; | 675 dwObjStmNum = ++pCreator->m_dwLastObjNum; |
677 } | 676 } |
678 int32_t iSize = m_ObjStream.m_ObjNumArray.GetSize(); | 677 int32_t iSize = pdfium::CollectionSize<int32_t>(m_ObjStream.m_Items); |
679 size_t iSeg = m_IndexArray.size(); | 678 size_t iSeg = m_IndexArray.size(); |
680 if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) { | 679 if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) { |
681 if (m_dwTempObjNum == 0) { | 680 if (m_dwTempObjNum == 0) { |
682 AppendIndex0(m_Buffer, true); | 681 AppendIndex0(m_Buffer, true); |
683 m_dwTempObjNum++; | 682 m_dwTempObjNum++; |
684 } | 683 } |
685 uint32_t end_num = m_IndexArray.back().objnum + m_IndexArray.back().count; | 684 uint32_t end_num = m_IndexArray.back().objnum + m_IndexArray.back().count; |
686 int index = 0; | 685 int index = 0; |
687 for (; m_dwTempObjNum < end_num; m_dwTempObjNum++) { | 686 for (; m_dwTempObjNum < end_num; m_dwTempObjNum++) { |
688 FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(m_dwTempObjNum); | 687 FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(m_dwTempObjNum); |
689 if (offset) { | 688 if (offset) { |
690 if (index >= iSize || | 689 if (index >= iSize || |
691 m_dwTempObjNum != m_ObjStream.m_ObjNumArray[index]) { | 690 m_dwTempObjNum != m_ObjStream.m_Items[index].objnum) { |
692 AppendIndex1(m_Buffer, *offset); | 691 AppendIndex1(m_Buffer, *offset); |
693 } else { | 692 } else { |
694 AppendIndex2(m_Buffer, dwObjStmNum, index++); | 693 AppendIndex2(m_Buffer, dwObjStmNum, index++); |
695 } | 694 } |
696 } else { | 695 } else { |
697 AppendIndex0(m_Buffer, false); | 696 AppendIndex0(m_Buffer, false); |
698 } | 697 } |
699 } | 698 } |
700 if (iSize > 0 && bEOF) { | 699 if (iSize > 0 && bEOF) { |
701 pCreator->m_ObjectOffset.Add(dwObjStmNum, 1); | 700 pCreator->m_ObjectOffset.Add(dwObjStmNum, 1); |
702 pCreator->m_ObjectOffset[dwObjStmNum] = objOffset; | 701 pCreator->m_ObjectOffset[dwObjStmNum] = objOffset; |
703 } | 702 } |
704 m_iSeg = iSeg; | 703 m_iSeg = iSeg; |
705 if (bEOF) { | 704 if (bEOF) { |
706 m_ObjStream.Start(); | 705 m_ObjStream.Start(); |
707 } | 706 } |
708 return 1; | 707 return 1; |
709 } | 708 } |
710 for (auto it = m_IndexArray.begin() + m_iSeg; it != m_IndexArray.end(); | 709 for (auto it = m_IndexArray.begin() + m_iSeg; it != m_IndexArray.end(); |
711 ++it) { | 710 ++it) { |
712 for (uint32_t m = it->objnum; m < it->objnum + it->count; ++m) { | 711 for (uint32_t m = it->objnum; m < it->objnum + it->count; ++m) { |
713 if (m_ObjStream.m_index >= iSize || | 712 if (m_ObjStream.m_index >= iSize || |
714 m != m_ObjStream.m_ObjNumArray.ElementAt(it - m_IndexArray.begin())) { | 713 m != m_ObjStream.m_Items[it - m_IndexArray.begin()].objnum) { |
715 AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]); | 714 AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]); |
716 } else { | 715 } else { |
717 AppendIndex2(m_Buffer, dwObjStmNum, m_ObjStream.m_index++); | 716 AppendIndex2(m_Buffer, dwObjStmNum, m_ObjStream.m_index++); |
718 } | 717 } |
719 } | 718 } |
720 } | 719 } |
721 if (iSize > 0 && bEOF) { | 720 if (iSize > 0 && bEOF) { |
722 AppendIndex1(m_Buffer, objOffset); | 721 AppendIndex1(m_Buffer, objOffset); |
723 m_IndexArray.push_back({dwObjStmNum, 1}); | 722 m_IndexArray.push_back({dwObjStmNum, 1}); |
724 iSeg += 1; | 723 iSeg += 1; |
(...skipping 1321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2046 m_pCryptoHandler = nullptr; | 2045 m_pCryptoHandler = nullptr; |
2047 } | 2046 } |
2048 void CPDF_Creator::ResetStandardSecurity() { | 2047 void CPDF_Creator::ResetStandardSecurity() { |
2049 if (!m_bLocalCryptoHandler) | 2048 if (!m_bLocalCryptoHandler) |
2050 return; | 2049 return; |
2051 | 2050 |
2052 delete m_pCryptoHandler; | 2051 delete m_pCryptoHandler; |
2053 m_pCryptoHandler = nullptr; | 2052 m_pCryptoHandler = nullptr; |
2054 m_bLocalCryptoHandler = FALSE; | 2053 m_bLocalCryptoHandler = FALSE; |
2055 } | 2054 } |
OLD | NEW |