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

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

Issue 1902713003: Cleanups from prior CLs. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 8 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 | « no previous file | core/fpdfapi/fpdf_edit/include/cpdf_creator.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 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
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;
Tom Sepez 2016/04/19 20:48:21 nit: blank line between methods&members
dsinclair 2016/04/20 13:09:12 Done.
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 return;
505 return TRUE;
506 } 503 }
507 m_dwSize = pHandler->EncryptGetSize(objnum, 0, src_data, src_size); 504 m_dwSize = pHandler->EncryptGetSize(objnum, 0, src_data, src_size);
508 m_pData = FX_Alloc(uint8_t, m_dwSize); 505 m_pData = FX_Alloc(uint8_t, m_dwSize);
509 pHandler->EncryptContent(objnum, 0, src_data, src_size, m_pData, m_dwSize); 506 pHandler->EncryptContent(objnum, 0, src_data, src_size, m_pData, m_dwSize);
510 m_bNewBuf = TRUE; 507 m_bNewBuf = TRUE;
511 return TRUE;
512 } 508 }
509
513 CPDF_Encryptor::~CPDF_Encryptor() { 510 CPDF_Encryptor::~CPDF_Encryptor() {
514 if (m_bNewBuf) { 511 if (m_bNewBuf)
515 FX_Free(m_pData); 512 FX_Free(m_pData);
516 }
517 } 513 }
518 514
519 } // namespace 515 } // namespace
520 516
521 CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {} 517 CPDF_ObjectStream::CPDF_ObjectStream() : m_dwObjNum(0), m_index(0) {}
522 FX_BOOL CPDF_ObjectStream::Start() { 518 FX_BOOL CPDF_ObjectStream::Start() {
523 m_ObjNumArray.RemoveAll(); 519 m_ObjNumArray.RemoveAll();
524 m_OffsetArray.RemoveAll(); 520 m_OffsetArray.RemoveAll();
525 m_Buffer.Clear(); 521 m_Buffer.Clear();
526 m_dwObjNum = 0; 522 m_dwObjNum = 0;
(...skipping 14 matching lines...) Expand all
541 m_OffsetArray.Add(m_Buffer.GetLength()); 537 m_OffsetArray.Add(m_Buffer.GetLength());
542 m_Buffer.AppendBlock(pBuffer, dwSize); 538 m_Buffer.AppendBlock(pBuffer, dwSize);
543 return 1; 539 return 1;
544 } 540 }
545 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { 541 FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) {
546 FXSYS_assert(pCreator); 542 FXSYS_assert(pCreator);
547 if (m_ObjNumArray.GetSize() == 0) { 543 if (m_ObjNumArray.GetSize() == 0) {
548 return 0; 544 return 0;
549 } 545 }
550 CFX_FileBufferArchive* pFile = &pCreator->m_File; 546 CFX_FileBufferArchive* pFile = &pCreator->m_File;
551 CPDF_CryptoHandler* pHandler = pCreator->m_pCryptoHandler;
552 FX_FILESIZE ObjOffset = pCreator->m_Offset; 547 FX_FILESIZE ObjOffset = pCreator->m_Offset;
553 if (!m_dwObjNum) { 548 if (!m_dwObjNum) {
554 m_dwObjNum = ++pCreator->m_dwLastObjNum; 549 m_dwObjNum = ++pCreator->m_dwLastObjNum;
555 } 550 }
556 CFX_ByteTextBuf tempBuffer; 551 CFX_ByteTextBuf tempBuffer;
557 int32_t iCount = m_ObjNumArray.GetSize(); 552 int32_t iCount = m_ObjNumArray.GetSize();
558 for (int32_t i = 0; i < iCount; i++) { 553 for (int32_t i = 0; i < iCount; i++) {
559 tempBuffer << m_ObjNumArray.ElementAt(i) << " " 554 tempBuffer << m_ObjNumArray.ElementAt(i) << " "
560 << m_OffsetArray.ElementAt(i) << " "; 555 << m_OffsetArray.ElementAt(i) << " ";
561 } 556 }
(...skipping 14 matching lines...) Expand all
576 if (pFile->AppendString("/First ") < 0) { 571 if (pFile->AppendString("/First ") < 0) {
577 return -1; 572 return -1;
578 } 573 }
579 if ((len = pFile->AppendDWord((uint32_t)tempBuffer.GetLength())) < 0) { 574 if ((len = pFile->AppendDWord((uint32_t)tempBuffer.GetLength())) < 0) {
580 return -1; 575 return -1;
581 } 576 }
582 if (pFile->AppendString("/Length ") < 0) { 577 if (pFile->AppendString("/Length ") < 0) {
583 return -1; 578 return -1;
584 } 579 }
585 offset += len + 15; 580 offset += len + 15;
586 if (!pCreator->m_bCompress && !pHandler) { 581
582 if (!pCreator->m_bCompress && !pCreator->m_pCryptoHandler) {
587 if ((len = pFile->AppendDWord( 583 if ((len = pFile->AppendDWord(
588 (uint32_t)(tempBuffer.GetLength() + m_Buffer.GetLength()))) < 0) { 584 (uint32_t)(tempBuffer.GetLength() + m_Buffer.GetLength()))) < 0) {
589 return -1; 585 return -1;
590 } 586 }
591 offset += len; 587 offset += len;
592 if ((len = pFile->AppendString(">>stream\r\n")) < 0) { 588 if ((len = pFile->AppendString(">>stream\r\n")) < 0) {
593 return -1; 589 return -1;
594 } 590 }
595 if (pFile->AppendBlock(tempBuffer.GetBuffer(), tempBuffer.GetLength()) < 591 if (pFile->AppendBlock(tempBuffer.GetBuffer(), tempBuffer.GetLength()) <
596 0) { 592 0) {
597 return -1; 593 return -1;
598 } 594 }
599 if (pFile->AppendBlock(m_Buffer.GetBuffer(), m_Buffer.GetLength()) < 0) { 595 if (pFile->AppendBlock(m_Buffer.GetBuffer(), m_Buffer.GetLength()) < 0) {
600 return -1; 596 return -1;
601 } 597 }
602 offset += len + tempBuffer.GetLength() + m_Buffer.GetLength(); 598 offset += len + tempBuffer.GetLength() + m_Buffer.GetLength();
603 } else { 599 } else {
604 tempBuffer << m_Buffer; 600 tempBuffer << m_Buffer;
605 CPDF_FlateEncoder encoder; 601 CPDF_FlateEncoder encoder(tempBuffer.GetBuffer(), tempBuffer.GetLength(),
606 encoder.Initialize(tempBuffer.GetBuffer(), tempBuffer.GetLength(), 602 pCreator->m_bCompress);
607 pCreator->m_bCompress); 603 CPDF_Encryptor encryptor(pCreator->m_pCryptoHandler, m_dwObjNum,
608 CPDF_Encryptor encryptor; 604 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) { 605 if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) {
612 return -1; 606 return -1;
613 } 607 }
614 offset += len; 608 offset += len;
615 if (pCreator->m_bCompress) { 609 if (pCreator->m_bCompress) {
616 if (pFile->AppendString("/Filter /FlateDecode") < 0) { 610 if (pFile->AppendString("/Filter /FlateDecode") < 0) {
617 return -1; 611 return -1;
618 } 612 }
619 offset += 20; 613 offset += 20;
620 } 614 }
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 FX_CHAR offset_buf[20]; 802 FX_CHAR offset_buf[20];
809 FXSYS_memset(offset_buf, 0, sizeof(offset_buf)); 803 FXSYS_memset(offset_buf, 0, sizeof(offset_buf));
810 FXSYS_i64toa(m_PrevOffset, offset_buf, 10); 804 FXSYS_i64toa(m_PrevOffset, offset_buf, 10);
811 int32_t len = (int32_t)FXSYS_strlen(offset_buf); 805 int32_t len = (int32_t)FXSYS_strlen(offset_buf);
812 if (pFile->AppendBlock(offset_buf, len) < 0) { 806 if (pFile->AppendBlock(offset_buf, len) < 0) {
813 return FALSE; 807 return FALSE;
814 } 808 }
815 offset += len + 6; 809 offset += len + 6;
816 } 810 }
817 FX_BOOL bPredictor = TRUE; 811 FX_BOOL bPredictor = TRUE;
818 CPDF_FlateEncoder encoder; 812 CPDF_FlateEncoder encoder(m_Buffer.GetBuffer(), m_Buffer.GetLength(),
819 encoder.Initialize(m_Buffer.GetBuffer(), m_Buffer.GetLength(), 813 pCreator->m_bCompress, bPredictor);
820 pCreator->m_bCompress, bPredictor);
821 if (pCreator->m_bCompress) { 814 if (pCreator->m_bCompress) {
822 if (pFile->AppendString("/Filter /FlateDecode") < 0) { 815 if (pFile->AppendString("/Filter /FlateDecode") < 0) {
823 return FALSE; 816 return FALSE;
824 } 817 }
825 offset += 20; 818 offset += 20;
826 if (bPredictor) { 819 if (bPredictor) {
827 if ((len = pFile->AppendString( 820 if ((len = pFile->AppendString(
828 "/DecodeParms<</Columns 7/Predictor 12>>")) < 0) { 821 "/DecodeParms<</Columns 7/Predictor 12>>")) < 0) {
829 return FALSE; 822 return FALSE;
830 } 823 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
908 m_IndexArray.push_back({objnum, 1}); 901 m_IndexArray.push_back({objnum, 1});
909 } 902 }
910 CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) { 903 CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) {
911 m_pDocument = pDoc; 904 m_pDocument = pDoc;
912 m_pParser = (CPDF_Parser*)pDoc->m_pParser; 905 m_pParser = (CPDF_Parser*)pDoc->m_pParser;
913 m_bCompress = TRUE; 906 m_bCompress = TRUE;
914 if (m_pParser) { 907 if (m_pParser) {
915 m_pEncryptDict = m_pParser->GetEncryptDict(); 908 m_pEncryptDict = m_pParser->GetEncryptDict();
916 m_pCryptoHandler = m_pParser->GetCryptoHandler(); 909 m_pCryptoHandler = m_pParser->GetCryptoHandler();
917 } else { 910 } else {
918 m_pEncryptDict = NULL; 911 m_pEncryptDict = nullptr;
919 m_pCryptoHandler = NULL; 912 m_pCryptoHandler = nullptr;
920 } 913 }
921 m_bSecurityChanged = FALSE; 914 m_bSecurityChanged = FALSE;
922 m_bStandardSecurity = FALSE; 915 m_pMetadata = nullptr;
923 m_pMetadata = NULL;
924 m_bEncryptCloned = FALSE; 916 m_bEncryptCloned = FALSE;
925 m_bEncryptMetadata = FALSE; 917 m_bEncryptMetadata = FALSE;
926 m_Offset = 0; 918 m_Offset = 0;
927 m_iStage = -1; 919 m_iStage = -1;
928 m_dwFlags = 0; 920 m_dwFlags = 0;
929 m_Pos = NULL; 921 m_Pos = nullptr;
930 m_XrefStart = 0; 922 m_XrefStart = 0;
931 m_pXRefStream = NULL; 923 m_pXRefStream = nullptr;
932 m_ObjectStreamSize = 200; 924 m_ObjectStreamSize = 200;
933 m_dwLastObjNum = m_pDocument->GetLastObjNum(); 925 m_dwLastObjNum = m_pDocument->GetLastObjNum();
934 m_pIDArray = NULL; 926 m_pIDArray = nullptr;
935 m_FileVersion = 0; 927 m_FileVersion = 0;
936 m_dwEnryptObjNum = 0; 928 m_dwEnryptObjNum = 0;
937 m_bNewCrypto = FALSE; 929 m_bLocalCryptoHandler = 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
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
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;
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 || !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, objnum, (uint8_t*)str.c_str(),
1134 encryptor.Initialize(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), 1121 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, 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
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 || pObj == m_pEncryptDict)
1228 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset); 1212 return PDF_CreatorAppendObject(pObj, &m_File, m_Offset);
1229 } 1213 if (m_File.AppendString("<<") < 0)
1230 if (m_File.AppendString("<<") < 0) {
1231 return -1; 1214 return -1;
1232 } 1215
1233 m_Offset += 2; 1216 m_Offset += 2;
1234 const CPDF_Dictionary* p = pObj->AsDictionary(); 1217 const CPDF_Dictionary* p = pObj->AsDictionary();
1235 bool bSignDict = p->IsSignatureDict(); 1218 bool bSignDict = p->IsSignatureDict();
1236 for (const auto& it : *p) { 1219 for (const auto& it : *p) {
1237 FX_BOOL bSignValue = FALSE; 1220 FX_BOOL bSignValue = FALSE;
1238 const CFX_ByteString& key = it.first; 1221 const CFX_ByteString& key = it.first;
1239 CPDF_Object* pValue = it.second; 1222 CPDF_Object* pValue = it.second;
1240 if (m_File.AppendString("/") < 0) { 1223 if (m_File.AppendString("/") < 0) {
1241 return -1; 1224 return -1;
1242 } 1225 }
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after
2006 } 1989 }
2007 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone()); 1990 m_pIDArray->Add(m_pIDArray->GetObjectAt(0)->Clone());
2008 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) { 1991 if (m_pEncryptDict && !pOldIDArray && m_pParser && bNewId) {
2009 if (m_pEncryptDict->GetStringBy("Filter") == "Standard") { 1992 if (m_pEncryptDict->GetStringBy("Filter") == "Standard") {
2010 CFX_ByteString user_pass = m_pParser->GetPassword(); 1993 CFX_ByteString user_pass = m_pParser->GetPassword();
2011 uint32_t flag = PDF_ENCRYPT_CONTENT; 1994 uint32_t flag = PDF_ENCRYPT_CONTENT;
2012 1995
2013 CPDF_SecurityHandler handler; 1996 CPDF_SecurityHandler handler;
2014 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(), 1997 handler.OnCreate(m_pEncryptDict, m_pIDArray, user_pass.raw_str(),
2015 user_pass.GetLength(), flag); 1998 user_pass.GetLength(), flag);
2016 if (m_bNewCrypto) { 1999 if (m_bLocalCryptoHandler)
2017 delete m_pCryptoHandler; 2000 delete m_pCryptoHandler;
2018 }
2019 m_pCryptoHandler = new CPDF_CryptoHandler; 2001 m_pCryptoHandler = new CPDF_CryptoHandler;
2020 m_pCryptoHandler->Init(m_pEncryptDict, &handler); 2002 m_pCryptoHandler->Init(m_pEncryptDict, &handler);
2021 m_bNewCrypto = TRUE; 2003 m_bLocalCryptoHandler = TRUE;
2022 m_bSecurityChanged = TRUE; 2004 m_bSecurityChanged = TRUE;
2023 } 2005 }
2024 } 2006 }
2025 } 2007 }
2026 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) { 2008 int32_t CPDF_Creator::Continue(IFX_Pause* pPause) {
2027 if (m_iStage < 0) { 2009 if (m_iStage < 0) {
2028 return m_iStage; 2010 return m_iStage;
2029 } 2011 }
2030 int32_t iRet = 0; 2012 int32_t iRet = 0;
2031 while (m_iStage < 100) { 2013 while (m_iStage < 100) {
(...skipping 20 matching lines...) Expand all
2052 FX_BOOL CPDF_Creator::SetFileVersion(int32_t fileVersion) { 2034 FX_BOOL CPDF_Creator::SetFileVersion(int32_t fileVersion) {
2053 if (fileVersion < 10 || fileVersion > 17) { 2035 if (fileVersion < 10 || fileVersion > 17) {
2054 return FALSE; 2036 return FALSE;
2055 } 2037 }
2056 m_FileVersion = fileVersion; 2038 m_FileVersion = fileVersion;
2057 return TRUE; 2039 return TRUE;
2058 } 2040 }
2059 void CPDF_Creator::RemoveSecurity() { 2041 void CPDF_Creator::RemoveSecurity() {
2060 ResetStandardSecurity(); 2042 ResetStandardSecurity();
2061 m_bSecurityChanged = TRUE; 2043 m_bSecurityChanged = TRUE;
2062 m_pEncryptDict = NULL; 2044 m_pEncryptDict = nullptr;
2063 m_pCryptoHandler = NULL; 2045 m_pCryptoHandler = nullptr;
2064 } 2046 }
2065 void CPDF_Creator::ResetStandardSecurity() { 2047 void CPDF_Creator::ResetStandardSecurity() {
2066 if (m_bStandardSecurity || m_bNewCrypto) { 2048 if (!m_bLocalCryptoHandler)
2067 delete m_pCryptoHandler;
2068 m_pCryptoHandler = NULL;
2069 }
2070 m_bNewCrypto = FALSE;
2071 if (!m_bStandardSecurity) {
2072 return; 2049 return;
2073 } 2050
2074 if (m_pEncryptDict) { 2051 delete m_pCryptoHandler;
2075 m_pEncryptDict->Release(); 2052 m_pCryptoHandler = nullptr;
2076 m_pEncryptDict = NULL; 2053 m_bLocalCryptoHandler = FALSE;
2077 }
2078 m_bStandardSecurity = FALSE;
2079 } 2054 }
OLDNEW
« no previous file with comments | « no previous file | core/fpdfapi/fpdf_edit/include/cpdf_creator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698