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

Side by Side Diff: core/fpdfapi/edit/fpdf_edit_create.cpp

Issue 2510223002: Make CPDF_Dictionary use unique pointers. (Closed)
Patch Set: rebase Created 4 years, 1 month 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/fpdfapi/edit/cpdf_pagecontentgenerator.cpp ('k') | core/fpdfapi/font/cpdf_font.cpp » ('j') | 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/fpdfapi/edit/editint.h" 7 #include "core/fpdfapi/edit/editint.h"
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "core/fpdfapi/edit/cpdf_creator.h" 11 #include "core/fpdfapi/edit/cpdf_creator.h"
12 #include "core/fpdfapi/parser/cpdf_array.h" 12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_crypto_handler.h" 13 #include "core/fpdfapi/parser/cpdf_crypto_handler.h"
14 #include "core/fpdfapi/parser/cpdf_dictionary.h" 14 #include "core/fpdfapi/parser/cpdf_dictionary.h"
15 #include "core/fpdfapi/parser/cpdf_document.h" 15 #include "core/fpdfapi/parser/cpdf_document.h"
16 #include "core/fpdfapi/parser/cpdf_name.h"
17 #include "core/fpdfapi/parser/cpdf_number.h"
16 #include "core/fpdfapi/parser/cpdf_parser.h" 18 #include "core/fpdfapi/parser/cpdf_parser.h"
17 #include "core/fpdfapi/parser/cpdf_reference.h" 19 #include "core/fpdfapi/parser/cpdf_reference.h"
18 #include "core/fpdfapi/parser/cpdf_security_handler.h" 20 #include "core/fpdfapi/parser/cpdf_security_handler.h"
19 #include "core/fpdfapi/parser/cpdf_stream.h" 21 #include "core/fpdfapi/parser/cpdf_stream.h"
20 #include "core/fpdfapi/parser/cpdf_stream_acc.h" 22 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
21 #include "core/fpdfapi/parser/cpdf_string.h" 23 #include "core/fpdfapi/parser/cpdf_string.h"
22 #include "core/fpdfapi/parser/fpdf_parser_decode.h" 24 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
23 #include "core/fxcrt/fx_ext.h" 25 #include "core/fxcrt/fx_ext.h"
24 #include "third_party/base/stl_util.h" 26 #include "third_party/base/stl_util.h"
25 27
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 break; 123 break;
122 } 124 }
123 case CPDF_Object::DICTIONARY: { 125 case CPDF_Object::DICTIONARY: {
124 if (pFile->AppendString("<<") < 0) { 126 if (pFile->AppendString("<<") < 0) {
125 return -1; 127 return -1;
126 } 128 }
127 offset += 2; 129 offset += 2;
128 const CPDF_Dictionary* p = pObj->AsDictionary(); 130 const CPDF_Dictionary* p = pObj->AsDictionary();
129 for (const auto& it : *p) { 131 for (const auto& it : *p) {
130 const CFX_ByteString& key = it.first; 132 const CFX_ByteString& key = it.first;
131 CPDF_Object* pValue = it.second; 133 CPDF_Object* pValue = it.second.get();
132 if (pFile->AppendString("/") < 0) { 134 if (pFile->AppendString("/") < 0) {
133 return -1; 135 return -1;
134 } 136 }
135 if ((len = pFile->AppendString(PDF_NameEncode(key).AsStringC())) < 0) { 137 if ((len = pFile->AppendString(PDF_NameEncode(key).AsStringC())) < 0) {
136 return -1; 138 return -1;
137 } 139 }
138 offset += len + 1; 140 offset += len + 1;
139 if (!pValue->IsInline()) { 141 if (!pValue->IsInline()) {
140 if (pFile->AppendString(" ") < 0) { 142 if (pFile->AppendString(" ") < 0) {
141 return -1; 143 return -1;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 int32_t PDF_CreatorWriteTrailer(CPDF_Document* pDocument, 192 int32_t PDF_CreatorWriteTrailer(CPDF_Document* pDocument,
191 CFX_FileBufferArchive* pFile, 193 CFX_FileBufferArchive* pFile,
192 CPDF_Array* pIDArray) { 194 CPDF_Array* pIDArray) {
193 FX_FILESIZE offset = 0; 195 FX_FILESIZE offset = 0;
194 int32_t len = 0; 196 int32_t len = 0;
195 CPDF_Parser* pParser = pDocument->GetParser(); 197 CPDF_Parser* pParser = pDocument->GetParser();
196 if (pParser) { 198 if (pParser) {
197 CPDF_Dictionary* p = pParser->GetTrailer(); 199 CPDF_Dictionary* p = pParser->GetTrailer();
198 for (const auto& it : *p) { 200 for (const auto& it : *p) {
199 const CFX_ByteString& key = it.first; 201 const CFX_ByteString& key = it.first;
200 CPDF_Object* pValue = it.second; 202 CPDF_Object* pValue = it.second.get();
201 if (key == "Encrypt" || key == "Size" || key == "Filter" || 203 if (key == "Encrypt" || key == "Size" || key == "Filter" ||
202 key == "Index" || key == "Length" || key == "Prev" || key == "W" || 204 key == "Index" || key == "Length" || key == "Prev" || key == "W" ||
203 key == "XRefStm" || key == "Type" || key == "ID") { 205 key == "XRefStm" || key == "Type" || key == "ID") {
204 continue; 206 continue;
205 } 207 }
206 if (key == "DecodeParms") 208 if (key == "DecodeParms")
207 continue; 209 continue;
208 210
209 if (pFile->AppendString(("/")) < 0) { 211 if (pFile->AppendString(("/")) < 0) {
210 return -1; 212 return -1;
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 m_pData = (uint8_t*)m_Acc.GetData(); 435 m_pData = (uint8_t*)m_Acc.GetData();
434 m_dwSize = m_Acc.GetSize(); 436 m_dwSize = m_Acc.GetSize();
435 m_pDict = pStream->GetDict(); 437 m_pDict = pStream->GetDict();
436 return; 438 return;
437 } 439 }
438 m_bNewData = true; 440 m_bNewData = true;
439 m_bCloned = true; 441 m_bCloned = true;
440 // TODO(thestig): Move to Init() and check return value. 442 // TODO(thestig): Move to Init() and check return value.
441 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &m_pData, &m_dwSize); 443 ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &m_pData, &m_dwSize);
442 m_pDict = ToDictionary(pStream->GetDict()->Clone().release()); 444 m_pDict = ToDictionary(pStream->GetDict()->Clone().release());
443 m_pDict->SetIntegerFor("Length", m_dwSize); 445 m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize));
444 m_pDict->SetNameFor("Filter", "FlateDecode"); 446 m_pDict->SetNewFor<CPDF_Name>("Filter", "FlateDecode");
445 m_pDict->RemoveFor("DecodeParms"); 447 m_pDict->RemoveFor("DecodeParms");
446 } 448 }
447 449
448 CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer, 450 CPDF_FlateEncoder::CPDF_FlateEncoder(const uint8_t* pBuffer,
449 uint32_t size, 451 uint32_t size,
450 bool bFlateEncode, 452 bool bFlateEncode,
451 bool bXRefStream) 453 bool bXRefStream)
452 : m_pData(nullptr), 454 : m_pData(nullptr),
453 m_dwSize(0), 455 m_dwSize(0),
454 m_pDict(nullptr), 456 m_pDict(nullptr),
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 } 988 }
987 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, 989 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream,
988 uint32_t objnum, 990 uint32_t objnum,
989 CPDF_CryptoHandler* pCrypto) { 991 CPDF_CryptoHandler* pCrypto) {
990 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pStream->AsStream()), 992 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pStream->AsStream()),
991 pStream != m_pMetadata); 993 pStream != m_pMetadata);
992 CPDF_Encryptor encryptor(pCrypto, objnum, encoder.m_pData, encoder.m_dwSize); 994 CPDF_Encryptor encryptor(pCrypto, objnum, encoder.m_pData, encoder.m_dwSize);
993 if ((uint32_t)encoder.m_pDict->GetIntegerFor("Length") != 995 if ((uint32_t)encoder.m_pDict->GetIntegerFor("Length") !=
994 encryptor.m_dwSize) { 996 encryptor.m_dwSize) {
995 encoder.CloneDict(); 997 encoder.CloneDict();
996 encoder.m_pDict->SetIntegerFor("Length", encryptor.m_dwSize); 998 encoder.m_pDict->SetNewFor<CPDF_Number>(
999 "Length", static_cast<int>(encryptor.m_dwSize));
997 } 1000 }
998 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { 1001 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) {
999 return -1; 1002 return -1;
1000 } 1003 }
1001 int len = m_File.AppendString("stream\r\n"); 1004 int len = m_File.AppendString("stream\r\n");
1002 if (len < 0) { 1005 if (len < 0) {
1003 return -1; 1006 return -1;
1004 } 1007 }
1005 m_Offset += len; 1008 m_Offset += len;
1006 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { 1009 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 break; 1102 break;
1100 } 1103 }
1101 case CPDF_Object::STREAM: { 1104 case CPDF_Object::STREAM: {
1102 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pObj->AsStream()), 1105 CPDF_FlateEncoder encoder(const_cast<CPDF_Stream*>(pObj->AsStream()),
1103 true); 1106 true);
1104 CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, encoder.m_pData, 1107 CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, encoder.m_pData,
1105 encoder.m_dwSize); 1108 encoder.m_dwSize);
1106 if ((uint32_t)encoder.m_pDict->GetIntegerFor("Length") != 1109 if ((uint32_t)encoder.m_pDict->GetIntegerFor("Length") !=
1107 encryptor.m_dwSize) { 1110 encryptor.m_dwSize) {
1108 encoder.CloneDict(); 1111 encoder.CloneDict();
1109 encoder.m_pDict->SetIntegerFor("Length", encryptor.m_dwSize); 1112 encoder.m_pDict->SetNewFor<CPDF_Number>(
1113 "Length", static_cast<int>(encryptor.m_dwSize));
1110 } 1114 }
1111 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { 1115 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) {
1112 return -1; 1116 return -1;
1113 } 1117 }
1114 if ((len = m_File.AppendString("stream\r\n")) < 0) { 1118 if ((len = m_File.AppendString("stream\r\n")) < 0) {
1115 return -1; 1119 return -1;
1116 } 1120 }
1117 m_Offset += len; 1121 m_Offset += len;
1118 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) { 1122 if (m_File.AppendBlock(encryptor.m_pData, encryptor.m_dwSize) < 0) {
1119 return -1; 1123 return -1;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1182 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset); 1186 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset);
1183 if (m_File.AppendString("<<") < 0) 1187 if (m_File.AppendString("<<") < 0)
1184 return -1; 1188 return -1;
1185 1189
1186 m_Offset += 2; 1190 m_Offset += 2;
1187 const CPDF_Dictionary* p = pObj->AsDictionary(); 1191 const CPDF_Dictionary* p = pObj->AsDictionary();
1188 bool bSignDict = p->IsSignatureDict(); 1192 bool bSignDict = p->IsSignatureDict();
1189 for (const auto& it : *p) { 1193 for (const auto& it : *p) {
1190 bool bSignValue = false; 1194 bool bSignValue = false;
1191 const CFX_ByteString& key = it.first; 1195 const CFX_ByteString& key = it.first;
1192 CPDF_Object* pValue = it.second; 1196 CPDF_Object* pValue = it.second.get();
1193 if (m_File.AppendString("/") < 0) { 1197 if (m_File.AppendString("/") < 0) {
1194 return -1; 1198 return -1;
1195 } 1199 }
1196 if ((len = m_File.AppendString(PDF_NameEncode(key).AsStringC())) < 0) { 1200 if ((len = m_File.AppendString(PDF_NameEncode(key).AsStringC())) < 0) {
1197 return -1; 1201 return -1;
1198 } 1202 }
1199 m_Offset += len + 1; 1203 m_Offset += len + 1;
1200 if (bSignDict && key == "Contents") { 1204 if (bSignDict && key == "Contents") {
1201 bSignValue = true; 1205 bSignValue = true;
1202 } 1206 }
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
1704 return -1; 1708 return -1;
1705 } 1709 }
1706 if (m_File.AppendString(" 0 obj <<") < 0) { 1710 if (m_File.AppendString(" 0 obj <<") < 0) {
1707 return -1; 1711 return -1;
1708 } 1712 }
1709 } 1713 }
1710 if (m_pParser) { 1714 if (m_pParser) {
1711 CPDF_Dictionary* p = m_pParser->GetTrailer(); 1715 CPDF_Dictionary* p = m_pParser->GetTrailer();
1712 for (const auto& it : *p) { 1716 for (const auto& it : *p) {
1713 const CFX_ByteString& key = it.first; 1717 const CFX_ByteString& key = it.first;
1714 CPDF_Object* pValue = it.second; 1718 CPDF_Object* pValue = it.second.get();
1715 // TODO(ochang): Consolidate with similar check in 1719 // TODO(ochang): Consolidate with similar check in
1716 // PDF_CreatorWriteTrailer. 1720 // PDF_CreatorWriteTrailer.
1717 if (key == "Encrypt" || key == "Size" || key == "Filter" || 1721 if (key == "Encrypt" || key == "Size" || key == "Filter" ||
1718 key == "Index" || key == "Length" || key == "Prev" || key == "W" || 1722 key == "Index" || key == "Length" || key == "Prev" || key == "W" ||
1719 key == "XRefStm" || key == "ID") { 1723 key == "XRefStm" || key == "ID") {
1720 continue; 1724 continue;
1721 } 1725 }
1722 if (m_File.AppendString(("/")) < 0) { 1726 if (m_File.AppendString(("/")) < 0) {
1723 return -1; 1727 return -1;
1724 } 1728 }
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
2009 m_pCryptoHandler = nullptr; 2013 m_pCryptoHandler = nullptr;
2010 } 2014 }
2011 void CPDF_Creator::ResetStandardSecurity() { 2015 void CPDF_Creator::ResetStandardSecurity() {
2012 if (!m_bLocalCryptoHandler) 2016 if (!m_bLocalCryptoHandler)
2013 return; 2017 return;
2014 2018
2015 delete m_pCryptoHandler; 2019 delete m_pCryptoHandler;
2016 m_pCryptoHandler = nullptr; 2020 m_pCryptoHandler = nullptr;
2017 m_bLocalCryptoHandler = false; 2021 m_bLocalCryptoHandler = false;
2018 } 2022 }
OLDNEW
« no previous file with comments | « core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp ('k') | core/fpdfapi/font/cpdf_font.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698