Debian Codegraph @

podofo/test/ParserTest/ParserTest.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominik Seichter *
3 * [email protected] *
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 <podofo-main.h>
22
23 #include <iostream>
24 #include <string>
25 using std::cerr;
26 using std::cout;
27 using std::cin;
28 using std::flush;
29 using std::endl;
30 using std::string;
31
32 using namespace PoDoFo;
33
34 void print_help()
35 {
36 cerr << "Usage: ParserTest [-d] [-clean] [-compact] <input_filename> [<output_filename>]\n"
37 << " -d Enable demand loading of objects\n"
38 << " -clean Write a clean PDF that is readable in a text editor\n"
39 << " -compact Write the PDF as compact as possible\n"
40 << flush;
41 }
42
43 void enc_test()
44 {
45 /*
46 PdfString documentId;
47 documentId.SetHexData( "BF37541A9083A51619AD5924ECF156DF", 32 );
48
49 PdfEncrypt enc( "user", "podofo", 0 );
50 enc.GenerateEncryptionKey( documentId );
51
52 printf("\n\nTrying authentication!\n");
53
54 PdfOutputDevice debug( &(std::cout) );
55 printf("Debug: ");
56 documentId.Write( &debug );
57 printf("\n\n");
58 std::string documentIdStr( documentId.GetString(), documentId.GetLength() );
59 std::string password = "user";
60 std::string uValue( reinterpret_cast<const char*>(enc.GetUValue()), 32 );
61 std::string oValue( reinterpret_cast<const char*>(enc.GetOValue()), 32 );
62
63 if( enc.Authenticate(documentIdStr, password,
64 uValue, oValue,
65 enc.GetPValue(), 40, 2) )
66 {
67
68 printf("Successfull\n");
69 }
70 else
71 printf("FAILED\n");
72
73 enc.SetCurrentReference( PdfReference( 7, 0 ) );
74 const char* pBuffer1 = "Somekind of drawing \001 buffer that possibly \003 could contain PDF drawing commands";
75 const char* pBuffer2 = " possibly could contain PDF drawing\003 commands";
76 long lLen = strlen( pBuffer1 ) + 2 * strlen( pBuffer2 );
77
78
79 char* pEncBuffer = static_cast<char*>(malloc( sizeof(char) * lLen ));
80 memcpy( pEncBuffer, pBuffer1, strlen( pBuffer1 ) * sizeof(char) );
81 memcpy( pEncBuffer + strlen(pBuffer1), pBuffer2, strlen( pBuffer2 ) );
82 memcpy( pEncBuffer + strlen(pBuffer1) + strlen( pBuffer2 ), pBuffer2, strlen( pBuffer2 ) );
83
84 enc.Encrypt( reinterpret_cast<unsigned char*>(pEncBuffer), lLen );
85
86 PdfMemoryOutputStream mem( lLen );
87 PdfOutputStream* pStream = enc.CreateEncryptionOutputStream( &mem );
88 pStream->Write( pBuffer1, strlen( pBuffer1 ) );
89 pStream->Write( pBuffer2, strlen( pBuffer2 ) );
90 pStream->Write( pBuffer2, strlen( pBuffer2 ) );
91 pStream->Close();
92
93 printf("Result: %i \n", memcmp( pEncBuffer, mem.TakeBuffer(), lLen ) );
94
95
96 enc.Encrypt( reinterpret_cast<unsigned char*>(pEncBuffer), lLen );
97 printf("Decrypted buffer: %s\n", pEncBuffer );
98 */
99 }
100
101 void write_back( PdfParser* pParser, const char* pszFilename, EPdfWriteMode eWriteMode )
102 {
103 //enc_test();
104
105 PdfWriter writer( pParser );
106 writer.SetWriteMode( eWriteMode );
107 /*
108 PdfEncrypt encrypt( "user", "podofo", 0,
109 PdfEncrypt::ePdfEncryptAlgorithm_RC4V2, PdfEncrypt::ePdfKeyLength_128 );
110 */
111 //writer.SetUseXRefStream( true );
112 //writer.SetLinearized( true );
113 writer.SetPdfVersion( ePdfVersion_1_6 );
114 //writer.SetEncrypted( encrypt );
115 writer.Write( pszFilename );
116 }
117
118 int main( int argc, char* argv[] )
119 {
120 PdfError::EnableLogging(true);
121 PdfError::EnableDebug(true);
122
123 PdfVecObjects objects;
124 PdfParser parser( &objects );
125 EPdfWriteMode eWriteMode = ePdfWriteMode_Default;
126 objects.SetAutoDelete( true );
127
128 bool useDemandLoading = false;
129 bool useStrictMode = false;
130 const char* pszInput = NULL;
131 const char* pszFilename = NULL;
132
133 for( int i=1; i<argc; i++ )
134 {
135 if( argv[i][0] == '-' )
136 {
137
138 if (string("-d") == argv[i])
139 {
140 useDemandLoading = true;
141 }
142 else if (string("-s") == argv[1])
143 {
144 useStrictMode = true;
145 }
146 else if (string("-clean") == argv[i])
147 {
148 eWriteMode = ePdfWriteMode_Clean;
149 }
150 else if (string("-compact") == argv[i])
151 {
152 eWriteMode = ePdfWriteMode_Compact;
153 }
154 }
155 else
156 {
157 if( pszInput == NULL )
158 {
159 pszInput = argv[i];
160 }
161 else if( pszFilename == NULL )
162 {
163 pszFilename = argv[i];
164 }
165 else
166 {
167 print_help();
168 return 0;
169 }
170 }
171 }
172
173 if( pszInput == NULL )
174 {
175 print_help();
176 cerr << "Usage: ParserTest [-d] [-s] [-clean] [-compact] <input_filename> [<output_filename>]\n"
177 << " -d Enable demand loading of objects\n"
178 << " -s Enable strict parsing mode\n"
179 << " -clean Enable clean writing mode\n"
180 << " -compact Enable compact writing mode\n"
181 << flush;
182 return 0;
183 }
184
185 cerr << "This test reads a PDF file from disk and writes it to a new pdf file." << endl;
186 cerr << "The PDF file should look unmodified in any viewer" << endl;
187 cerr << "---" << endl;
188 cerr << "Number of incremental updates: " << parser.GetNumberOfIncrementalUpdates() << endl;
189
190 try {
191 cerr << "Parsing " << pszInput << " with demand loading "
192 << (useDemandLoading ? "on" : "off")
193 << " with strict parsing "
194 << (useStrictMode ? "on" : "off")
195 << " ..." << flush;
196
197 bool bIncorrectPw = false;
198 std::string pw;
199 parser.SetStrictParsing( useStrictMode );
200 do {
201 try {
202 if( !bIncorrectPw )
203 parser.ParseFile( pszInput, useDemandLoading );
204 else
205 parser.SetPassword( pw );
206
207 bIncorrectPw = false;
208 } catch( PdfError & e ) {
209 if( e.GetError() == ePdfError_InvalidPassword )
210 {
211 cout << endl << "Password :";
212 std::getline( cin, pw );
213 cout << endl;
214
215 // try to continue with the new password
216 bIncorrectPw = true;
217 }
218 else
219 throw e;
220 }
221 } while( bIncorrectPw );
222
223 cerr << " done" << endl;
224
225 cerr << "PdfVersion=" << parser.GetPdfVersion() << endl;
226 cerr << "PdfVersionString=" << parser.GetPdfVersionString() << endl;
227
228 /*
229 cerr << "=============\n");
230 PdfObject* pCheat = objects.CreateObject( "Cheat" );
231 std::reverse( objects.begin(), objects.end() );
232 objects.RenumberObjects( const_cast<PdfObject*>(parser.GetTrailer()) );
233 pCheat = objects.CreateObject("LastObject");
234 cerr << "=============\n");
235 */
236
237 if (pszFilename)
238 {
239 cerr << "Writing..." << flush;
240 write_back( &parser, pszFilename, eWriteMode );
241 cerr << " done" << endl;
242 }
243 } catch( PdfError & e ) {
244 e.PrintErrorMsg();
245 return e.GetError();
246 }
247
248 if (pszFilename)
249 cerr << "Parsed and wrote successfully" << endl;
250 else
251 cerr << "Parsed successfully" << endl;
252
253 /*
254 cerr << "Now with PdfMemDocument" << endl;
255 PdfMemDocument document;
256 document.Load( pszInput );
257 document.Write( pszFilename );
258 cerr << "DONE: Now with PdfMemDocument" << endl;
259 */
260
261 return 0;
262 }