Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp

Issue 1660733002: Make m_IndexArray sane. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: fix iSeg. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fpdfapi/fpdf_edit/editint.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/src/fpdfapi/fpdf_edit/editint.h" 7 #include "core/src/fpdfapi/fpdf_edit/editint.h"
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 buffer.AppendByte(FX_GETBYTEOFFSET8(objnum)); 334 buffer.AppendByte(FX_GETBYTEOFFSET8(objnum));
335 buffer.AppendByte(FX_GETBYTEOFFSET0(objnum)); 335 buffer.AppendByte(FX_GETBYTEOFFSET0(objnum));
336 buffer.AppendByte(FX_GETBYTEOFFSET8(index)); 336 buffer.AppendByte(FX_GETBYTEOFFSET8(index));
337 buffer.AppendByte(FX_GETBYTEOFFSET0(index)); 337 buffer.AppendByte(FX_GETBYTEOFFSET0(index));
338 } 338 }
339 339
340 bool IsXRefNeedEnd(CPDF_XRefStream* pXRef, FX_DWORD flag) { 340 bool IsXRefNeedEnd(CPDF_XRefStream* pXRef, FX_DWORD flag) {
341 if (!(flag & FPDFCREATE_INCREMENTAL)) 341 if (!(flag & FPDFCREATE_INCREMENTAL))
342 return false; 342 return false;
343 343
344 int32_t iSize = pXRef->m_IndexArray.GetSize() / 2;
345 int32_t iCount = 0; 344 int32_t iCount = 0;
Lei Zhang 2016/02/03 00:03:28 FX_DWORD
Tom Sepez 2016/02/03 00:12:16 Done.
346 for (int32_t i = 0; i < iSize; ++i) 345 for (const auto& pair : pXRef->m_IndexArray)
347 iCount += pXRef->m_IndexArray.ElementAt(i * 2 + 1); 346 iCount += pair.count;
347
348 return iCount >= PDF_XREFSTREAM_MAXSIZE; 348 return iCount >= PDF_XREFSTREAM_MAXSIZE;
349 } 349 }
350 350
351 int32_t OutputIndex(CFX_FileBufferArchive* pFile, FX_FILESIZE offset) { 351 int32_t OutputIndex(CFX_FileBufferArchive* pFile, FX_FILESIZE offset) {
352 if (sizeof(offset) > 4) { 352 if (sizeof(offset) > 4) {
353 if (FX_GETBYTEOFFSET32(offset)) { 353 if (FX_GETBYTEOFFSET32(offset)) {
354 if (pFile->AppendByte(FX_GETBYTEOFFSET56(offset)) < 0) 354 if (pFile->AppendByte(FX_GETBYTEOFFSET56(offset)) < 0)
355 return -1; 355 return -1;
356 if (pFile->AppendByte(FX_GETBYTEOFFSET48(offset)) < 0) 356 if (pFile->AppendByte(FX_GETBYTEOFFSET48(offset)) < 0)
357 return -1; 357 return -1;
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 } 617 }
618 if ((len = pFile->AppendString("\r\nendstream\r\nendobj\r\n")) < 0) { 618 if ((len = pFile->AppendString("\r\nendstream\r\nendobj\r\n")) < 0) {
619 return -1; 619 return -1;
620 } 620 }
621 offset += len; 621 offset += len;
622 return ObjOffset; 622 return ObjOffset;
623 } 623 }
624 CPDF_XRefStream::CPDF_XRefStream() 624 CPDF_XRefStream::CPDF_XRefStream()
625 : m_PrevOffset(0), m_dwTempObjNum(0), m_iSeg(0) {} 625 : m_PrevOffset(0), m_dwTempObjNum(0), m_iSeg(0) {}
626 FX_BOOL CPDF_XRefStream::Start() { 626 FX_BOOL CPDF_XRefStream::Start() {
627 m_IndexArray.RemoveAll(); 627 m_IndexArray.clear();
628 m_Buffer.Clear(); 628 m_Buffer.Clear();
629 m_iSeg = 0; 629 m_iSeg = 0;
630 return TRUE; 630 return TRUE;
631 } 631 }
632 int32_t CPDF_XRefStream::CompressIndirectObject(FX_DWORD dwObjNum, 632 int32_t CPDF_XRefStream::CompressIndirectObject(FX_DWORD dwObjNum,
633 const CPDF_Object* pObj, 633 const CPDF_Object* pObj,
634 CPDF_Creator* pCreator) { 634 CPDF_Creator* pCreator) {
635 if (!pCreator) { 635 if (!pCreator) {
636 return 0; 636 return 0;
637 } 637 }
(...skipping 25 matching lines...) Expand all
663 objOffset = m_ObjStream.End(pCreator); 663 objOffset = m_ObjStream.End(pCreator);
664 if (objOffset < 0) { 664 if (objOffset < 0) {
665 return -1; 665 return -1;
666 } 666 }
667 } 667 }
668 FX_DWORD& dwObjStmNum = m_ObjStream.m_dwObjNum; 668 FX_DWORD& dwObjStmNum = m_ObjStream.m_dwObjNum;
669 if (!dwObjStmNum) { 669 if (!dwObjStmNum) {
670 dwObjStmNum = ++pCreator->m_dwLastObjNum; 670 dwObjStmNum = ++pCreator->m_dwLastObjNum;
671 } 671 }
672 int32_t iSize = m_ObjStream.m_ObjNumArray.GetSize(); 672 int32_t iSize = m_ObjStream.m_ObjNumArray.GetSize();
673 int32_t iSeg = m_IndexArray.GetSize() / 2; 673 int32_t iSeg = m_IndexArray.size();
Lei Zhang 2016/02/03 00:03:28 Is it easy to change this and |m_iSeg| to size_t?
Tom Sepez 2016/02/03 00:12:16 Let's try.
674 if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) { 674 if (!(pCreator->m_dwFlags & FPDFCREATE_INCREMENTAL)) {
675 if (m_dwTempObjNum == 0) { 675 if (m_dwTempObjNum == 0) {
676 AppendIndex0(m_Buffer, true); 676 AppendIndex0(m_Buffer, true);
677 m_dwTempObjNum++; 677 m_dwTempObjNum++;
678 } 678 }
679 FX_DWORD end_num = m_IndexArray.GetAt((iSeg - 1) * 2) + 679 FX_DWORD end_num = m_IndexArray.back().objnum + m_IndexArray.back().count;
680 m_IndexArray.GetAt((iSeg - 1) * 2 + 1);
681 int index = 0; 680 int index = 0;
682 for (; m_dwTempObjNum < end_num; m_dwTempObjNum++) { 681 for (; m_dwTempObjNum < end_num; m_dwTempObjNum++) {
683 FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(m_dwTempObjNum); 682 FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(m_dwTempObjNum);
684 if (offset) { 683 if (offset) {
685 if (index >= iSize || 684 if (index >= iSize ||
686 m_dwTempObjNum != m_ObjStream.m_ObjNumArray[index]) { 685 m_dwTempObjNum != m_ObjStream.m_ObjNumArray[index]) {
687 AppendIndex1(m_Buffer, *offset); 686 AppendIndex1(m_Buffer, *offset);
688 } else { 687 } else {
689 AppendIndex2(m_Buffer, dwObjStmNum, index++); 688 AppendIndex2(m_Buffer, dwObjStmNum, index++);
690 } 689 }
691 } else { 690 } else {
692 AppendIndex0(m_Buffer, false); 691 AppendIndex0(m_Buffer, false);
693 } 692 }
694 } 693 }
695 if (iSize > 0 && bEOF) { 694 if (iSize > 0 && bEOF) {
696 pCreator->m_ObjectOffset.Add(dwObjStmNum, 1); 695 pCreator->m_ObjectOffset.Add(dwObjStmNum, 1);
697 pCreator->m_ObjectOffset[dwObjStmNum] = objOffset; 696 pCreator->m_ObjectOffset[dwObjStmNum] = objOffset;
698 } 697 }
699 m_iSeg = iSeg; 698 m_iSeg = iSeg;
700 if (bEOF) { 699 if (bEOF) {
701 m_ObjStream.Start(); 700 m_ObjStream.Start();
702 } 701 }
703 return 1; 702 return 1;
704 } 703 }
705 int32_t& j = m_ObjStream.m_index; 704 for (auto it = m_IndexArray.begin() + m_iSeg; it != m_IndexArray.end();
706 for (int i = m_iSeg; i < iSeg; i++) { 705 ++it) {
707 FX_DWORD start = m_IndexArray.ElementAt(i * 2); 706 for (FX_DWORD m = it->objnum; m < it->objnum + it->count; ++m) {
708 FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start; 707 if (m_ObjStream.m_index >= iSize ||
709 for (FX_DWORD m = start; m < end; m++) { 708 m != m_ObjStream.m_ObjNumArray.ElementAt(it - m_IndexArray.begin())) {
710 if (j >= iSize || m != m_ObjStream.m_ObjNumArray.ElementAt(j)) {
711 AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]); 709 AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[m]);
712 } else { 710 } else {
713 AppendIndex2(m_Buffer, dwObjStmNum, j++); 711 AppendIndex2(m_Buffer, dwObjStmNum, m_ObjStream.m_index++);
714 } 712 }
715 } 713 }
716 } 714 }
717 if (iSize > 0 && bEOF) { 715 if (iSize > 0 && bEOF) {
718 AppendIndex1(m_Buffer, objOffset); 716 AppendIndex1(m_Buffer, objOffset);
719 m_IndexArray.Add(dwObjStmNum); 717 m_IndexArray.push_back({dwObjStmNum, 1});
720 m_IndexArray.Add(1);
721 iSeg += 1; 718 iSeg += 1;
722 } 719 }
723 m_iSeg = iSeg; 720 m_iSeg = iSeg;
724 if (bEOF) { 721 if (bEOF) {
725 m_ObjStream.Start(); 722 m_ObjStream.Start();
726 } 723 }
727 return 1; 724 return 1;
728 } 725 }
729 FX_BOOL CPDF_XRefStream::GenerateXRefStream(CPDF_Creator* pCreator, 726 FX_BOOL CPDF_XRefStream::GenerateXRefStream(CPDF_Creator* pCreator,
730 FX_BOOL bEOF) { 727 FX_BOOL bEOF) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 } 759 }
763 if ((len = pFile->AppendString(" ")) < 0) { 760 if ((len = pFile->AppendString(" ")) < 0) {
764 return FALSE; 761 return FALSE;
765 } 762 }
766 offset += len + 1; 763 offset += len + 1;
767 if ((len = pFile->AppendDWord(objnum + 1)) < 0) { 764 if ((len = pFile->AppendDWord(objnum + 1)) < 0) {
768 return FALSE; 765 return FALSE;
769 } 766 }
770 offset += len; 767 offset += len;
771 } else { 768 } else {
772 int32_t iSeg = m_IndexArray.GetSize() / 2; 769 for (const auto& pair : m_IndexArray) {
773 for (int32_t i = 0; i < iSeg; i++) { 770 if ((len = pFile->AppendDWord(pair.objnum)) < 0) {
774 if ((len = pFile->AppendDWord(m_IndexArray.ElementAt(i * 2))) < 0) {
775 return FALSE; 771 return FALSE;
776 } 772 }
777 if (pFile->AppendString(" ") < 0) { 773 if (pFile->AppendString(" ") < 0) {
778 return FALSE; 774 return FALSE;
779 } 775 }
780 offset += len + 1; 776 offset += len + 1;
781 if ((len = pFile->AppendDWord(m_IndexArray.ElementAt(i * 2 + 1))) < 0) { 777 if ((len = pFile->AppendDWord(pair.count)) < 0) {
782 return FALSE; 778 return FALSE;
783 } 779 }
784 if (pFile->AppendString(" ") < 0) { 780 if (pFile->AppendString(" ") < 0) {
785 return FALSE; 781 return FALSE;
786 } 782 }
787 offset += len + 1; 783 offset += len + 1;
788 } 784 }
789 } 785 }
790 if (pFile->AppendString("]/Size ") < 0) { 786 if (pFile->AppendString("]/Size ") < 0) {
791 return FALSE; 787 return FALSE;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 AppendIndex0(m_Buffer, true); 871 AppendIndex0(m_Buffer, true);
876 for (FX_DWORD i = 1; i < pCreator->m_dwLastObjNum + 1; i++) { 872 for (FX_DWORD i = 1; i < pCreator->m_dwLastObjNum + 1; i++) {
877 FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(i); 873 FX_FILESIZE* offset = pCreator->m_ObjectOffset.GetPtrAt(i);
878 if (offset) { 874 if (offset) {
879 AppendIndex1(m_Buffer, *offset); 875 AppendIndex1(m_Buffer, *offset);
880 } else { 876 } else {
881 AppendIndex0(m_Buffer, false); 877 AppendIndex0(m_Buffer, false);
882 } 878 }
883 } 879 }
884 } else { 880 } else {
885 int32_t iSeg = m_IndexArray.GetSize() / 2; 881 for (const auto& pair : m_IndexArray) {
886 for (int i = 0; i < iSeg; i++) { 882 for (FX_DWORD j = pair.objnum; j < pair.objnum + pair.count; ++j)
887 FX_DWORD start = m_IndexArray.ElementAt(i * 2);
888 FX_DWORD end = m_IndexArray.ElementAt(i * 2 + 1) + start;
889 for (FX_DWORD j = start; j < end; j++) {
890 AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[j]); 883 AppendIndex1(m_Buffer, pCreator->m_ObjectOffset[j]);
891 }
892 } 884 }
893 } 885 }
894 return GenerateXRefStream(pCreator, FALSE); 886 return GenerateXRefStream(pCreator, FALSE);
895 } 887 }
896 FX_BOOL CPDF_XRefStream::AddObjectNumberToIndexArray(FX_DWORD objnum) { 888 void CPDF_XRefStream::AddObjectNumberToIndexArray(FX_DWORD objnum) {
897 int32_t iSize = m_IndexArray.GetSize(); 889 if (m_IndexArray.empty()) {
898 if (iSize == 0) { 890 m_IndexArray.push_back({objnum, 1});
899 m_IndexArray.Add(objnum); 891 return;
900 m_IndexArray.Add(1);
901 } else {
902 FXSYS_assert(iSize > 1);
903 FX_DWORD startobjnum = m_IndexArray.ElementAt(iSize - 2);
904 int32_t iCount = m_IndexArray.ElementAt(iSize - 1);
905 if (objnum == startobjnum + iCount) {
906 m_IndexArray[iSize - 1] = iCount + 1;
907 } else {
908 m_IndexArray.Add(objnum);
909 m_IndexArray.Add(1);
910 }
911 } 892 }
912 return TRUE; 893 FX_DWORD nextobjnum = m_IndexArray.back().objnum + m_IndexArray.back().count;
Lei Zhang 2016/02/03 00:03:28 nit: next_objnum
894 if (objnum == nextobjnum)
895 m_IndexArray.back().count += 1;
896 else
897 m_IndexArray.push_back({objnum, 1});
913 } 898 }
914 CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) { 899 CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) {
915 m_pDocument = pDoc; 900 m_pDocument = pDoc;
916 m_pParser = (CPDF_Parser*)pDoc->m_pParser; 901 m_pParser = (CPDF_Parser*)pDoc->m_pParser;
917 m_bCompress = TRUE; 902 m_bCompress = TRUE;
918 if (m_pParser) { 903 if (m_pParser) {
919 m_pEncryptDict = m_pParser->GetEncryptDict(); 904 m_pEncryptDict = m_pParser->GetEncryptDict();
920 m_pCryptoHandler = m_pParser->GetCryptoHandler(); 905 m_pCryptoHandler = m_pParser->GetCryptoHandler();
921 } else { 906 } else {
922 m_pEncryptDict = NULL; 907 m_pEncryptDict = NULL;
(...skipping 1153 matching lines...) Expand 10 before | Expand all | Expand 10 after
2076 m_bNewCrypto = FALSE; 2061 m_bNewCrypto = FALSE;
2077 if (!m_bStandardSecurity) { 2062 if (!m_bStandardSecurity) {
2078 return; 2063 return;
2079 } 2064 }
2080 if (m_pEncryptDict) { 2065 if (m_pEncryptDict) {
2081 m_pEncryptDict->Release(); 2066 m_pEncryptDict->Release();
2082 m_pEncryptDict = NULL; 2067 m_pEncryptDict = NULL;
2083 } 2068 }
2084 m_bStandardSecurity = FALSE; 2069 m_bStandardSecurity = FALSE;
2085 } 2070 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_edit/editint.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698