1 /***************************************************************************
2 * Copyright (C) 2005 by Dominik Seichter *
3 * domseichter@web.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "Uncompress.h"
22
23 #include <cstdio>
24
25 using namespace PoDoFo;
26
UnCompress()27 UnCompress::UnCompress()
28 : m_pDocument( NULL )
29 {
30 }
31
~UnCompress()32 UnCompress::~UnCompress()
33 {
34 delete m_pDocument;
35 }
36
Init(const char * pszInput,const char * pszOutput)37 void UnCompress::Init( const char* pszInput, const char* pszOutput )
38 {
39 if( m_pDocument )
40 delete m_pDocument;
41
42 m_pDocument = new PdfMemDocument( pszInput );
43
44 this->UncompressObjects();
45
46 PdfWriter writer( &(m_pDocument->GetObjects()), new PdfObject( *(m_pDocument->GetTrailer() ) ) );
47 writer.SetWriteMode( ePdfWriteMode_Clean );
48 writer.Write( pszOutput );
49 }
50
UncompressObjects()51 void UnCompress::UncompressObjects()
52 {
53 TIVecObjects it = m_pDocument->GetObjects().begin();
54
55 while( it != m_pDocument->GetObjects().end() )
56 {
57 printf("Reading %i %i R\n", (*it)->Reference().ObjectNumber(), (*it)->Reference().GenerationNumber() );
58 if( (*it)->HasStream() )
59 {
60 try {
61 printf("-> Uncompressing object %i %i\n",
62 (*it)->Reference().ObjectNumber(), (*it)->Reference().GenerationNumber() );
63 PdfMemStream* pStream = dynamic_cast<PdfMemStream*>((*it)->GetStream());
64 if( !pStream )
65 PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
66 printf("-> Original Length: %" PDF_FORMAT_INT64 "\n",
67 static_cast<pdf_int64>(pStream->GetLength()) );
68 try {
69 pStream->Uncompress();
70 } catch( PdfError & e ) {
71 if( e.GetError() == ePdfError_Flate )
72 {
73 // Ignore ZLib errors
74 fprintf( stderr, "WARNING: ZLib error ignored for this object.\n");
75 }
76 else
77 throw e;
78 }
79 printf("-> Uncompressed Length: %" PDF_FORMAT_INT64 "\n",
80 static_cast<pdf_int64>(pStream->GetLength()) );
81 } catch( PdfError & e ) {
82 e.PrintErrorMsg();
83 if( e.GetError() != ePdfError_UnsupportedFilter )
84 throw e;
85 }
86 }
87
88 ++it;
89 }
90 }
91