Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 1 | // Copyright 2017 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #include "core/fpdfapi/edit/cpdf_flateencoder.h" |
| 8 | |
| 9 | #include <memory> |
| 10 | |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 11 | #include "constants/stream_dict_common.h" |
Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 13 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 14 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
| 15 | |
Lei Zhang | 4a8478e | 2018-04-05 19:08:20 +0000 | [diff] [blame] | 16 | CPDF_FlateEncoder::CPDF_FlateEncoder(const CPDF_Stream* pStream, |
| 17 | bool bFlateEncode) |
Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 18 | : m_dwSize(0), m_pAcc(pdfium::MakeRetain<CPDF_StreamAcc>(pStream)) { |
Lei Zhang | 07401ba | 2017-12-11 22:12:08 +0000 | [diff] [blame] | 19 | m_pAcc->LoadAllDataRaw(); |
Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 20 | |
| 21 | bool bHasFilter = pStream && pStream->HasFilter(); |
| 22 | if (bHasFilter && !bFlateEncode) { |
| 23 | auto pDestAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream); |
Lei Zhang | 07401ba | 2017-12-11 22:12:08 +0000 | [diff] [blame] | 24 | pDestAcc->LoadAllDataFiltered(); |
Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 25 | |
| 26 | m_dwSize = pDestAcc->GetSize(); |
| 27 | m_pData = pDestAcc->DetachData(); |
| 28 | m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
| 29 | m_pDict->RemoveFor("Filter"); |
| 30 | return; |
| 31 | } |
| 32 | if (bHasFilter || !bFlateEncode) { |
Lei Zhang | 2cc287a | 2018-04-05 19:09:10 +0000 | [diff] [blame] | 33 | m_pData = m_pAcc->GetData(); |
Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 34 | m_dwSize = m_pAcc->GetSize(); |
| 35 | m_pDict = pStream->GetDict(); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // TODO(thestig): Move to Init() and check return value. |
| 40 | uint8_t* buffer = nullptr; |
| 41 | ::FlateEncode(m_pAcc->GetData(), m_pAcc->GetSize(), &buffer, &m_dwSize); |
| 42 | |
| 43 | m_pData = std::unique_ptr<uint8_t, FxFreeDeleter>(buffer); |
| 44 | m_pDict = ToDictionary(pStream->GetDict()->Clone()); |
| 45 | m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); |
| 46 | m_pDict->SetNewFor<CPDF_Name>("Filter", "FlateDecode"); |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 47 | m_pDict->RemoveFor(pdfium::stream::kDecodeParms); |
Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Dan Sinclair | a0b1954 | 2017-05-09 12:36:08 -0400 | [diff] [blame] | 50 | CPDF_FlateEncoder::~CPDF_FlateEncoder() {} |
| 51 | |
| 52 | void CPDF_FlateEncoder::CloneDict() { |
| 53 | if (m_pDict.IsOwned()) |
| 54 | return; |
| 55 | |
| 56 | m_pDict = ToDictionary(m_pDict->Clone()); |
| 57 | ASSERT(m_pDict.IsOwned()); |
| 58 | } |