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

Side by Side Diff: core/fpdfapi/page/cpdf_streamparser.cpp

Issue 2520133002: Remove some WrapUnique() calls by returing unique_ptrs (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/page/cpdf_streamcontentparser.cpp ('k') | core/fpdfapi/page/pageint.h » ('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 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 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/page/pageint.h" 7 #include "core/fpdfapi/page/pageint.h"
8 8
9 #include <limits.h> 9 #include <limits.h>
10 10
11 #include <memory>
11 #include <utility> 12 #include <utility>
12 13
13 #include "core/fpdfapi/cpdf_modulemgr.h" 14 #include "core/fpdfapi/cpdf_modulemgr.h"
14 #include "core/fpdfapi/page/cpdf_docpagedata.h" 15 #include "core/fpdfapi/page/cpdf_docpagedata.h"
15 #include "core/fpdfapi/parser/cpdf_array.h" 16 #include "core/fpdfapi/parser/cpdf_array.h"
16 #include "core/fpdfapi/parser/cpdf_boolean.h" 17 #include "core/fpdfapi/parser/cpdf_boolean.h"
17 #include "core/fpdfapi/parser/cpdf_dictionary.h" 18 #include "core/fpdfapi/parser/cpdf_dictionary.h"
18 #include "core/fpdfapi/parser/cpdf_document.h" 19 #include "core/fpdfapi/parser/cpdf_document.h"
19 #include "core/fpdfapi/parser/cpdf_name.h" 20 #include "core/fpdfapi/parser/cpdf_name.h"
20 #include "core/fpdfapi/parser/cpdf_null.h" 21 #include "core/fpdfapi/parser/cpdf_null.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 dest_buf = 0; 106 dest_buf = 0;
106 return (uint32_t)-1; 107 return (uint32_t)-1;
107 } 108 }
108 109
109 } // namespace 110 } // namespace
110 111
111 CPDF_StreamParser::CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize) 112 CPDF_StreamParser::CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize)
112 : m_pBuf(pData), 113 : m_pBuf(pData),
113 m_Size(dwSize), 114 m_Size(dwSize),
114 m_Pos(0), 115 m_Pos(0),
115 m_pLastObj(nullptr),
116 m_pPool(nullptr) {} 116 m_pPool(nullptr) {}
117 117
118 CPDF_StreamParser::CPDF_StreamParser( 118 CPDF_StreamParser::CPDF_StreamParser(
119 const uint8_t* pData, 119 const uint8_t* pData,
120 uint32_t dwSize, 120 uint32_t dwSize,
121 const CFX_WeakPtr<CFX_ByteStringPool>& pPool) 121 const CFX_WeakPtr<CFX_ByteStringPool>& pPool)
122 : m_pBuf(pData), 122 : m_pBuf(pData),
123 m_Size(dwSize), 123 m_Size(dwSize),
124 m_Pos(0), 124 m_Pos(0),
125 m_pLastObj(nullptr),
126 m_pPool(pPool) {} 125 m_pPool(pPool) {}
127 126
128 CPDF_StreamParser::~CPDF_StreamParser() { 127 CPDF_StreamParser::~CPDF_StreamParser() {}
129 delete m_pLastObj;
130 }
131 128
132 CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, 129 std::unique_ptr<CPDF_Stream> CPDF_StreamParser::ReadInlineStream(
133 CPDF_Dictionary* pDict, 130 CPDF_Document* pDoc,
134 CPDF_Object* pCSObj) { 131 CPDF_Dictionary* pDict,
132 CPDF_Object* pCSObj) {
135 if (m_Pos == m_Size) 133 if (m_Pos == m_Size)
136 return nullptr; 134 return nullptr;
137 135
138 if (PDFCharIsWhitespace(m_pBuf[m_Pos])) 136 if (PDFCharIsWhitespace(m_pBuf[m_Pos]))
139 m_Pos++; 137 m_Pos++;
140 138
141 CFX_ByteString Decoder; 139 CFX_ByteString Decoder;
142 CPDF_Dictionary* pParam = nullptr; 140 CPDF_Dictionary* pParam = nullptr;
143 CPDF_Object* pFilter = pDict->GetDirectObjectFor("Filter"); 141 CPDF_Object* pFilter = pDict->GetDirectObjectFor("Filter");
144 if (pFilter) { 142 if (pFilter) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 break; 224 break;
227 } 225 }
228 dwStreamSize += m_Pos - dwPrevPos; 226 dwStreamSize += m_Pos - dwPrevPos;
229 } 227 }
230 m_Pos = dwSavePos; 228 m_Pos = dwSavePos;
231 pData = FX_Alloc(uint8_t, dwStreamSize); 229 pData = FX_Alloc(uint8_t, dwStreamSize);
232 FXSYS_memcpy(pData, m_pBuf + m_Pos, dwStreamSize); 230 FXSYS_memcpy(pData, m_pBuf + m_Pos, dwStreamSize);
233 m_Pos += dwStreamSize; 231 m_Pos += dwStreamSize;
234 } 232 }
235 pDict->SetNewFor<CPDF_Number>("Length", (int)dwStreamSize); 233 pDict->SetNewFor<CPDF_Number>("Length", (int)dwStreamSize);
236 return new CPDF_Stream(pData, dwStreamSize, pdfium::WrapUnique(pDict)); 234 return pdfium::MakeUnique<CPDF_Stream>(pData, dwStreamSize,
235 pdfium::WrapUnique(pDict));
237 } 236 }
238 237
239 CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() { 238 CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() {
240 delete m_pLastObj; 239 m_pLastObj.reset();
241 m_pLastObj = nullptr;
242
243 m_WordSize = 0; 240 m_WordSize = 0;
244 bool bIsNumber = true;
245 if (!PositionIsInBounds()) 241 if (!PositionIsInBounds())
246 return EndOfData; 242 return EndOfData;
247 243
248 int ch = m_pBuf[m_Pos++]; 244 int ch = m_pBuf[m_Pos++];
249 while (1) { 245 while (1) {
250 while (PDFCharIsWhitespace(ch)) { 246 while (PDFCharIsWhitespace(ch)) {
251 if (!PositionIsInBounds()) 247 if (!PositionIsInBounds())
252 return EndOfData; 248 return EndOfData;
253 249
254 ch = m_pBuf[m_Pos++]; 250 ch = m_pBuf[m_Pos++];
(...skipping 11 matching lines...) Expand all
266 break; 262 break;
267 } 263 }
268 } 264 }
269 265
270 if (PDFCharIsDelimiter(ch) && ch != '/') { 266 if (PDFCharIsDelimiter(ch) && ch != '/') {
271 m_Pos--; 267 m_Pos--;
272 m_pLastObj = ReadNextObject(false, 0); 268 m_pLastObj = ReadNextObject(false, 0);
273 return Others; 269 return Others;
274 } 270 }
275 271
272 bool bIsNumber = true;
276 while (1) { 273 while (1) {
277 if (m_WordSize < kMaxWordBuffer) 274 if (m_WordSize < kMaxWordBuffer)
278 m_WordBuffer[m_WordSize++] = ch; 275 m_WordBuffer[m_WordSize++] = ch;
279 276
280 if (!PDFCharIsNumeric(ch)) 277 if (!PDFCharIsNumeric(ch))
281 bIsNumber = false; 278 bIsNumber = false;
282 279
283 if (!PositionIsInBounds()) 280 if (!PositionIsInBounds())
284 break; 281 break;
285 282
286 ch = m_pBuf[m_Pos++]; 283 ch = m_pBuf[m_Pos++];
287 284
288 if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) { 285 if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) {
289 m_Pos--; 286 m_Pos--;
290 break; 287 break;
291 } 288 }
292 } 289 }
293 290
294 m_WordBuffer[m_WordSize] = 0; 291 m_WordBuffer[m_WordSize] = 0;
295 if (bIsNumber) 292 if (bIsNumber)
296 return Number; 293 return Number;
297 294
298 if (m_WordBuffer[0] == '/') 295 if (m_WordBuffer[0] == '/')
299 return Name; 296 return Name;
300 297
301 if (m_WordSize == 4) { 298 if (m_WordSize == 4) {
302 if (memcmp(m_WordBuffer, "true", 4) == 0) { 299 if (memcmp(m_WordBuffer, "true", 4) == 0) {
303 m_pLastObj = new CPDF_Boolean(true); 300 m_pLastObj = pdfium::MakeUnique<CPDF_Boolean>(true);
304 return Others; 301 return Others;
305 } 302 }
306 if (memcmp(m_WordBuffer, "null", 4) == 0) { 303 if (memcmp(m_WordBuffer, "null", 4) == 0) {
307 m_pLastObj = new CPDF_Null; 304 m_pLastObj = pdfium::MakeUnique<CPDF_Null>();
308 return Others; 305 return Others;
309 } 306 }
310 } else if (m_WordSize == 5) { 307 } else if (m_WordSize == 5) {
311 if (memcmp(m_WordBuffer, "false", 5) == 0) { 308 if (memcmp(m_WordBuffer, "false", 5) == 0) {
312 m_pLastObj = new CPDF_Boolean(false); 309 m_pLastObj = pdfium::MakeUnique<CPDF_Boolean>(false);
313 return Others; 310 return Others;
314 } 311 }
315 } 312 }
316 return Keyword; 313 return Keyword;
317 } 314 }
318 315
319 CPDF_Object* CPDF_StreamParser::GetObject() { 316 std::unique_ptr<CPDF_Object> CPDF_StreamParser::ReadNextObject(
320 CPDF_Object* pObj = m_pLastObj; 317 bool bAllowNestedArray,
321 m_pLastObj = nullptr; 318 uint32_t dwInArrayLevel) {
322 return pObj;
323 }
324
325 CPDF_Object* CPDF_StreamParser::ReadNextObject(bool bAllowNestedArray,
326 uint32_t dwInArrayLevel) {
327 bool bIsNumber; 319 bool bIsNumber;
328 GetNextWord(bIsNumber); 320 GetNextWord(bIsNumber);
329 if (!m_WordSize) 321 if (!m_WordSize)
330 return nullptr; 322 return nullptr;
331 323
332 if (bIsNumber) { 324 if (bIsNumber) {
333 m_WordBuffer[m_WordSize] = 0; 325 m_WordBuffer[m_WordSize] = 0;
334 return new CPDF_Number(CFX_ByteStringC(m_WordBuffer, m_WordSize)); 326 return pdfium::MakeUnique<CPDF_Number>(
327 CFX_ByteStringC(m_WordBuffer, m_WordSize));
335 } 328 }
336 329
337 int first_char = m_WordBuffer[0]; 330 int first_char = m_WordBuffer[0];
338 if (first_char == '/') { 331 if (first_char == '/') {
339 CFX_ByteString name = 332 CFX_ByteString name =
340 PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1)); 333 PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1));
341 return new CPDF_Name(m_pPool, name); 334 return pdfium::MakeUnique<CPDF_Name>(m_pPool, name);
342 } 335 }
343 336
344 if (first_char == '(') { 337 if (first_char == '(') {
345 CFX_ByteString str = ReadString(); 338 CFX_ByteString str = ReadString();
346 return new CPDF_String(m_pPool, str, false); 339 return pdfium::MakeUnique<CPDF_String>(m_pPool, str, false);
347 } 340 }
348 341
349 if (first_char == '<') { 342 if (first_char == '<') {
350 if (m_WordSize == 1) 343 if (m_WordSize == 1)
351 return new CPDF_String(m_pPool, ReadHexString(), true); 344 return pdfium::MakeUnique<CPDF_String>(m_pPool, ReadHexString(), true);
352 345
353 CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pPool); 346 auto pDict = pdfium::MakeUnique<CPDF_Dictionary>(m_pPool);
354 while (1) { 347 while (1) {
355 GetNextWord(bIsNumber); 348 GetNextWord(bIsNumber);
356 if (m_WordSize == 2 && m_WordBuffer[0] == '>') 349 if (m_WordSize == 2 && m_WordBuffer[0] == '>')
357 break; 350 break;
358 351
359 if (!m_WordSize || m_WordBuffer[0] != '/') { 352 if (!m_WordSize || m_WordBuffer[0] != '/')
360 delete pDict;
361 return nullptr; 353 return nullptr;
362 }
363 354
364 CFX_ByteString key = 355 CFX_ByteString key =
365 PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1)); 356 PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1));
366 auto pObj = pdfium::WrapUnique(ReadNextObject(true, 0)); 357 std::unique_ptr<CPDF_Object> pObj = ReadNextObject(true, 0);
367 if (!pObj) { 358 if (!pObj)
368 delete pDict;
369 return nullptr; 359 return nullptr;
370 } 360
371 if (!key.IsEmpty()) 361 if (!key.IsEmpty())
372 pDict->SetFor(key, std::move(pObj)); 362 pDict->SetFor(key, std::move(pObj));
373 } 363 }
374 return pDict; 364 return std::move(pDict);
375 } 365 }
376 366
377 if (first_char == '[') { 367 if (first_char == '[') {
378 if ((!bAllowNestedArray && dwInArrayLevel) || 368 if ((!bAllowNestedArray && dwInArrayLevel) ||
379 dwInArrayLevel > kMaxNestedArrayLevel) { 369 dwInArrayLevel > kMaxNestedArrayLevel) {
380 return nullptr; 370 return nullptr;
381 } 371 }
382 372
383 CPDF_Array* pArray = new CPDF_Array; 373 auto pArray = pdfium::MakeUnique<CPDF_Array>();
384 while (1) { 374 while (1) {
385 CPDF_Object* pObj = ReadNextObject(bAllowNestedArray, dwInArrayLevel + 1); 375 std::unique_ptr<CPDF_Object> pObj =
376 ReadNextObject(bAllowNestedArray, dwInArrayLevel + 1);
386 if (pObj) { 377 if (pObj) {
387 pArray->Add(pdfium::WrapUnique(pObj)); 378 pArray->Add(std::move(pObj));
388 continue; 379 continue;
389 } 380 }
390 if (!m_WordSize || m_WordBuffer[0] == ']') 381 if (!m_WordSize || m_WordBuffer[0] == ']')
391 break; 382 break;
392 } 383 }
393 return pArray; 384 return std::move(pArray);
394 } 385 }
395 386
396 if (m_WordSize == 5 && !memcmp(m_WordBuffer, "false", 5)) 387 if (m_WordSize == 5 && !memcmp(m_WordBuffer, "false", 5))
397 return new CPDF_Boolean(false); 388 return pdfium::MakeUnique<CPDF_Boolean>(false);
398 389
399 if (m_WordSize == 4) { 390 if (m_WordSize == 4) {
400 if (memcmp(m_WordBuffer, "true", 4) == 0) 391 if (memcmp(m_WordBuffer, "true", 4) == 0)
401 return new CPDF_Boolean(true); 392 return pdfium::MakeUnique<CPDF_Boolean>(true);
402
403 if (memcmp(m_WordBuffer, "null", 4) == 0) 393 if (memcmp(m_WordBuffer, "null", 4) == 0)
404 return new CPDF_Null; 394 return pdfium::MakeUnique<CPDF_Null>();
405 } 395 }
406 396
407 return nullptr; 397 return nullptr;
408 } 398 }
409 399
410 // TODO(npm): the following methods are almost identical in cpdf_syntaxparser 400 // TODO(npm): the following methods are almost identical in cpdf_syntaxparser
411 void CPDF_StreamParser::GetNextWord(bool& bIsNumber) { 401 void CPDF_StreamParser::GetNextWord(bool& bIsNumber) {
412 m_WordSize = 0; 402 m_WordSize = 0;
413 bIsNumber = true; 403 bIsNumber = true;
414 if (!PositionIsInBounds()) 404 if (!PositionIsInBounds())
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 606
617 if (buf.GetLength() > kMaxStringLength) 607 if (buf.GetLength() > kMaxStringLength)
618 return CFX_ByteString(buf.GetBuffer(), kMaxStringLength); 608 return CFX_ByteString(buf.GetBuffer(), kMaxStringLength);
619 609
620 return buf.MakeString(); 610 return buf.MakeString();
621 } 611 }
622 612
623 bool CPDF_StreamParser::PositionIsInBounds() const { 613 bool CPDF_StreamParser::PositionIsInBounds() const {
624 return m_Pos < m_Size; 614 return m_Pos < m_Size;
625 } 615 }
OLDNEW
« no previous file with comments | « core/fpdfapi/page/cpdf_streamcontentparser.cpp ('k') | core/fpdfapi/page/pageint.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698