| 1 | /** |
| 2 | * SPDX-FileCopyrightText: (C) 2005 Dominik Seichter <[email protected]> |
| 3 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include "Uncompress.h" |
| 7 | |
| 8 | #include <cstdio> |
| 9 | |
| 10 | using namespace std; |
| 11 | using namespace PoDoFo; |
| 12 | |
| 13 | UnCompress::UnCompress() |
| 14 | : m_document(nullptr) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | UnCompress::~UnCompress() |
| 19 | { |
| 20 | delete m_document; |
| 21 | } |
| 22 | |
| 23 | void UnCompress::Init(const string_view& input, const string_view& output) |
| 24 | { |
| 25 | if (m_document) |
| 26 | delete m_document; |
| 27 | |
| 28 | m_document = new PdfMemDocument(); |
| 29 | m_document->Load(input); |
| 30 | |
| 31 | this->UncompressObjects(); |
| 32 | |
| 33 | m_document->Save(output, PdfSaveOptions::Clean |
| 34 | | PdfSaveOptions::NoMetadataUpdate | PdfSaveOptions::NoFlateCompress); |
| 35 | } |
| 36 | |
| 37 | void UnCompress::UncompressObjects() |
| 38 | { |
| 39 | auto& objects = m_document->GetObjects(); |
| 40 | |
| 41 | for (auto obj : objects) |
| 42 | { |
| 43 | printf("Reading %i %i R\n", obj->GetIndirectReference().ObjectNumber(), obj->GetIndirectReference().GenerationNumber()); |
| 44 | auto stream = obj->GetStream(); |
| 45 | if (stream == nullptr) |
| 46 | continue; |
| 47 | |
| 48 | { |
| 49 | try |
| 50 | { |
| 51 | printf("-> Uncompressing object %i %i\n", |
| 52 | obj->GetIndirectReference().ObjectNumber(), obj->GetIndirectReference().GenerationNumber()); |
| 53 | |
| 54 | printf("-> Original Length: %zu\n", stream->GetLength()); |
| 55 | try |
| 56 | { |
| 57 | stream->Unwrap(); |
| 58 | } |
| 59 | catch (PdfError& e) |
| 60 | { |
| 61 | if (e.GetCode() == PdfErrorCode::Flate) |
| 62 | { |
| 63 | // Ignore ZLib errors |
| 64 | fprintf(stderr, "WARNING: ZLib error ignored for this object.\n"); |
| 65 | } |
| 66 | else |
| 67 | throw e; |
| 68 | } |
| 69 | printf("-> Uncompressed Length: %zu\n", stream->GetLength()); |
| 70 | } |
| 71 | catch (PdfError& e) |
| 72 | { |
| 73 | e.PrintErrorMsg(); |
| 74 | if (e.GetCode() != PdfErrorCode::UnsupportedFilter) |
| 75 | throw e; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |