1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <stdio.h>
#include <sal/main.h>
#include <osl/file.h>
#include <osl/thread.h>
#include <rtl/alloc.h>
#include <rtl/ustring.hxx>
#include <rtl/strbuf.hxx>
#include <pdfparse.hxx>
using namespace pdfparse;
static void printHelp( const char* pExe )
{
fprintf( stdout,
"USAGE: %s [-h,--help]\n"
" %s [-pw, --password <password>] <inputfile> [<outputfile>]\n"
" %s <-a, --extract-add-streams> [-pw, --password <password>] <inputfile> [<outputfile>]\n"
" %s <-f, --extract-fonts> [-pw, --password <password>] <inputfile> [<outputfile>]\n"
" %s <-o, --extract-objects> <o0>[:<g0>][,<o1>[:g1][,...]] [-pw, --password <password>] <inputfile> [<outputfile>]\n"
" -h, --help: show help\n"
" -a, --extract-add-streams: extracts additional streams to outputfile_object\n"
" and prints the mimetype found to stdout\n"
" -f, --extract-fonts: extracts fonts (currently only type1 and truetype are supported\n"
" -o, --extract-objects: extracts object streams, the syntax of the argument is comma separated\n"
" object numbers, where object number and generation number are separated by \':\'\n"
" an omitted generation number defaults to 0\n"
" -pw, --password: use password for decryption\n"
"\n"
"note: -f, -a, -o and normal unzip operation are mutually exclusive\n"
, pExe, pExe, pExe, pExe, pExe );
}
namespace {
class FileEmitContext : public EmitContext
{
oslFileHandle m_aHandle;
oslFileHandle m_aReadHandle;
unsigned int m_nReadLen;
void openReadFile( const char* pOrigName );
public:
FileEmitContext( const char* pFileName, const char* pOrigName, const PDFContainer* pTop );
virtual ~FileEmitContext() override;
virtual bool write( const void* pBuf, unsigned int nLen ) throw() override;
virtual unsigned int getCurPos() throw() override;
virtual bool copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) throw() override;
virtual unsigned int readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) throw() override;
};
}
FileEmitContext::FileEmitContext( const char* pFileName, const char* pOrigName, const PDFContainer* pTop )
: EmitContext( pTop ),
m_aHandle( nullptr ),
m_aReadHandle( nullptr ),
m_nReadLen( 0 )
{
OUString aSysFile( OStringToOUString( OString( pFileName ), osl_getThreadTextEncoding() ) );
OUString aURL;
if( osl_getFileURLFromSystemPath( aSysFile.pData, &aURL.pData ) != osl_File_E_None )
{
fprintf( stderr, "filename conversion \"%s\" failed\n", pFileName );
return;
}
if( osl_openFile( aURL.pData, &m_aHandle, osl_File_OpenFlag_Write ) == osl_File_E_None )
{
if( osl_setFileSize( m_aHandle, 0 ) != osl_File_E_None )
{
fprintf( stderr, "could not truncate %s\n", pFileName );
osl_closeFile( m_aHandle );
m_aHandle = nullptr;
}
}
else if( osl_openFile( aURL.pData, &m_aHandle,
osl_File_OpenFlag_Write |osl_File_OpenFlag_Create ) != osl_File_E_None )
{
fprintf( stderr, "could not open %s\n", pFileName );
return;
}
m_bDeflate = true;
openReadFile( pOrigName );
}
FileEmitContext::~FileEmitContext()
{
if( m_aHandle )
osl_closeFile( m_aHandle );
if( m_aReadHandle )
osl_closeFile( m_aReadHandle );
}
void FileEmitContext::openReadFile( const char* pInFile )
{
OUString aSysFile( OStringToOUString( OString( pInFile ), osl_getThreadTextEncoding() ) );
OUString aURL;
if( osl_getFileURLFromSystemPath( aSysFile.pData, &aURL.pData ) != osl_File_E_None )
{
fprintf( stderr, "filename conversion \"%s\" failed\n", pInFile );
return;
}
if( osl_openFile( aURL.pData, &m_aReadHandle, osl_File_OpenFlag_Read ) != osl_File_E_None )
{
fprintf( stderr, "could not open %s\n", pInFile );
return;
}
if( osl_setFilePos( m_aReadHandle, osl_Pos_End, 0 ) != osl_File_E_None )
{
fprintf( stderr, "could not seek to end of %s\n", pInFile );
osl_closeFile( m_aReadHandle );
return;
}
sal_uInt64 nFileSize = 0;
if( osl_getFilePos( m_aReadHandle, &nFileSize ) != osl_File_E_None )
{
fprintf( stderr, "could not get end pos of %s\n", pInFile );
osl_closeFile( m_aReadHandle );
return;
}
m_nReadLen = static_cast<unsigned int>(nFileSize);
}
bool FileEmitContext::write( const void* pBuf, unsigned int nLen ) throw()
{
if( ! m_aHandle )
return false;
sal_uInt64 nWrite = static_cast<sal_uInt64>(nLen);
sal_uInt64 nWritten = 0;
return (osl_writeFile( m_aHandle, pBuf, nWrite, &nWritten ) == osl_File_E_None)
&& nWrite == nWritten;
}
unsigned int FileEmitContext::getCurPos() throw()
{
sal_uInt64 nFileSize = 0;
if( m_aHandle )
{
if( osl_getFilePos( m_aHandle, &nFileSize ) != osl_File_E_None )
nFileSize = 0;
}
return static_cast<unsigned int>(nFileSize);
}
bool FileEmitContext::copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) throw()
{
if( nOrigOffset + nLen > m_nReadLen )
return false;
if( osl_setFilePos( m_aReadHandle, osl_Pos_Absolut, nOrigOffset ) != osl_File_E_None )
{
fprintf( stderr, "could not seek to offset %u\n", nOrigOffset );
return false;
}
void* pBuf = std::malloc( nLen );
if( ! pBuf )
return false;
sal_uInt64 nBytesRead = 0;
if( osl_readFile( m_aReadHandle, pBuf, nLen, &nBytesRead ) != osl_File_E_None
|| nBytesRead != static_cast<sal_uInt64>(nLen) )
{
fprintf( stderr, "could not read %u bytes\n", nLen );
std::free( pBuf );
return false;
}
bool bRet = write( pBuf, nLen );
std::free( pBuf );
return bRet;
}
unsigned int FileEmitContext::readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) throw()
{
if( nOrigOffset + nLen > m_nReadLen )
return 0;
if( osl_setFilePos( m_aReadHandle, osl_Pos_Absolut, nOrigOffset ) != osl_File_E_None )
{
fprintf( stderr, "could not seek to offset %u\n", nOrigOffset );
return 0;
}
sal_uInt64 nBytesRead = 0;
if( osl_readFile( m_aReadHandle, pBuf, nLen, &nBytesRead ) != osl_File_E_None )
return 0;
return static_cast<unsigned int>(nBytesRead);
}
typedef int(*PDFFileHdl)(const char*, const char*, PDFFile*);
static int handleFile( const char* pInFile, const char* pOutFile, const char* pPassword, PDFFileHdl pHdl )
{
int nRet = 0;
std::unique_ptr<PDFEntry> pEntry = pdfparse::PDFReader::read( pInFile );
if( pEntry )
{
PDFFile* pPDFFile = dynamic_cast<PDFFile*>(pEntry.get());
if( pPDFFile )
{
fprintf( stdout, "have a %s PDF file\n", pPDFFile->isEncrypted() ? "encrypted" : "unencrypted" );
if( pPassword )
fprintf( stdout, "password %s\n",
pPDFFile->setupDecryptionData( pPassword ) ? "matches" : "does not match" );
nRet = pHdl( pInFile, pOutFile, pPDFFile );
}
else
nRet = 20;
}
return nRet;
}
static int write_unzipFile( const char* pInFile, const char* pOutFile, PDFFile* pPDFFile )
{
FileEmitContext aContext( pOutFile, pInFile, pPDFFile );
aContext.m_bDecrypt = pPDFFile->isEncrypted();
pPDFFile->emit(aContext);
return 0;
}
static int write_addStreamArray( const char* pOutFile, PDFArray* pStreams, PDFFile* pPDFFile, const char* pInFile )
{
int nRet = 0;
unsigned int nArrayElements = pStreams->m_aSubElements.size();
for( unsigned int i = 0; i < nArrayElements-1 && nRet == 0; i++ )
{
PDFName* pMimeType = dynamic_cast<PDFName*>(pStreams->m_aSubElements[i].get());
PDFObjectRef* pStreamRef = dynamic_cast<PDFObjectRef*>(pStreams->m_aSubElements[i+1].get());
if( ! pMimeType )
fprintf( stderr, "error: no mimetype element\n" );
if( ! pStreamRef )
fprintf( stderr, "error: no stream ref element\n" );
if( pMimeType && pStreamRef )
{
fprintf( stdout, "found stream %d %d with mimetype %s\n",
pStreamRef->m_nNumber, pStreamRef->m_nGeneration,
pMimeType->m_aName.getStr() );
PDFObject* pObject = pPDFFile->findObject( pStreamRef->m_nNumber, pStreamRef->m_nGeneration );
if( pObject )
{
OString aOutStream = pOutFile +
OStringLiteral("_stream_") +
OString::number( sal_Int32(pStreamRef->m_nNumber) ) +
"_" +
OString::number( sal_Int32(pStreamRef->m_nGeneration) );
FileEmitContext aContext( aOutStream.getStr(), pInFile, pPDFFile );
aContext.m_bDecrypt = pPDFFile->isEncrypted();
pObject->writeStream( aContext, pPDFFile );
}
else
{
fprintf( stderr, "object not found\n" );
nRet = 121;
}
}
else
nRet = 120;
}
return nRet;
}
static int write_addStreams( const char* pInFile, const char* pOutFile, PDFFile* pPDFFile )
{
// find all trailers
int nRet = 0;
unsigned int nElements = pPDFFile->m_aSubElements.size();
for( unsigned i = 0; i < nElements && nRet == 0; i++ )
{
PDFTrailer* pTrailer = dynamic_cast<PDFTrailer*>(pPDFFile->m_aSubElements[i].get());
if( pTrailer && pTrailer->m_pDict )
{
// search for AdditionalStreams entry
auto add_stream = pTrailer->m_pDict->m_aMap.find( "AdditionalStreams" );
if( add_stream != pTrailer->m_pDict->m_aMap.end() )
{
PDFArray* pStreams = dynamic_cast<PDFArray*>(add_stream->second);
if( pStreams )
nRet = write_addStreamArray( pOutFile, pStreams, pPDFFile, pInFile );
}
}
}
return nRet;
}
static int write_fonts( const char* i_pInFile, const char* i_pOutFile, PDFFile* i_pPDFFile )
{
unsigned int nElements = i_pPDFFile->m_aSubElements.size();
for (unsigned i = 0; i < nElements; i++)
{
// search FontDescriptors
PDFObject* pObj = dynamic_cast<PDFObject*>(i_pPDFFile->m_aSubElements[i].get());
if( ! pObj )
continue;
PDFDict* pDict = dynamic_cast<PDFDict*>(pObj->m_pObject);
if( ! pDict )
continue;
std::unordered_map<OString,PDFEntry*>::iterator map_it =
pDict->m_aMap.find( "Type" );
if( map_it == pDict->m_aMap.end() )
continue;
PDFName* pName = dynamic_cast<PDFName*>(map_it->second);
if( ! pName )
continue;
if( pName->m_aName != "FontDescriptor" )
continue;
// the font name will be helpful, also there must be one in
// a font descriptor
map_it = pDict->m_aMap.find( "FontName" );
if( map_it == pDict->m_aMap.end() )
continue;
pName = dynamic_cast<PDFName*>(map_it->second);
if( ! pName )
continue;
OString aFontName( pName->m_aName );
PDFObjectRef* pStreamRef = nullptr;
const char* pFileType = nullptr;
// we have a font descriptor, try for a type 1 font
map_it = pDict->m_aMap.find( "FontFile" );
if( map_it != pDict->m_aMap.end() )
{
pStreamRef = dynamic_cast<PDFObjectRef*>(map_it->second);
if( pStreamRef )
pFileType = "pfa";
}
// perhaps it's a truetype file ?
if( ! pStreamRef )
{
map_it = pDict->m_aMap.find( "FontFile2" );
if( map_it != pDict->m_aMap.end() )
{
pStreamRef = dynamic_cast<PDFObjectRef*>(map_it->second);
if( pStreamRef )
pFileType = "ttf";
}
}
if( ! pStreamRef )
continue;
PDFObject* pStream = i_pPDFFile->findObject( pStreamRef );
if( ! pStream )
continue;
OStringBuffer aOutStream( i_pOutFile );
aOutStream.append( "_font_" );
aOutStream.append( sal_Int32(pStreamRef->m_nNumber) );
aOutStream.append( "_" );
aOutStream.append( sal_Int32(pStreamRef->m_nGeneration) );
aOutStream.append( "_" );
aOutStream.append( aFontName );
if( pFileType )
{
aOutStream.append( "." );
aOutStream.append( pFileType );
}
FileEmitContext aContext( aOutStream.getStr(), i_pInFile, i_pPDFFile );
aContext.m_bDecrypt = i_pPDFFile->isEncrypted();
pStream->writeStream( aContext, i_pPDFFile );
}
return 0;
}
static std::vector< std::pair< sal_Int32, sal_Int32 > > s_aEmitObjects;
static int write_objects( const char* i_pInFile, const char* i_pOutFile, PDFFile* i_pPDFFile )
{
unsigned int nElements = s_aEmitObjects.size();
for (unsigned i = 0; i < nElements; i++)
{
sal_Int32 nObject = s_aEmitObjects[i].first;
sal_Int32 nGeneration = s_aEmitObjects[i].second;
PDFObject* pStream = i_pPDFFile->findObject( nObject, nGeneration );
if( ! pStream )
{
fprintf( stderr, "object %d %d not found !\n", static_cast<int>(nObject), static_cast<int>(nGeneration) );
continue;
}
OString aOutStream = i_pOutFile +
OStringLiteral("_stream_") +
OString::number( nObject ) +
"_" +
OString::number( nGeneration );
FileEmitContext aContext( aOutStream.getStr(), i_pInFile, i_pPDFFile );
aContext.m_bDecrypt = i_pPDFFile->isEncrypted();
pStream->writeStream( aContext, i_pPDFFile );
}
return 0;
}
SAL_IMPLEMENT_MAIN_WITH_ARGS( argc, argv )
{
const char* pInFile = nullptr;
const char* pOutFile = nullptr;
const char* pPassword = nullptr;
OStringBuffer aOutFile( 256 );
PDFFileHdl aHdl = write_unzipFile;
for( int nArg = 1; nArg < argc; nArg++ )
{
if( argv[nArg][0] == '-' )
{
if( ! rtl_str_compare( "-pw", argv[nArg] ) ||
! rtl_str_compare( "--password" , argv[nArg] ) )
{
if( nArg == argc-1 )
{
fprintf( stderr, "no password given\n" );
return 1;
}
nArg++;
pPassword = argv[nArg];
}
else if( ! rtl_str_compare( "-h", argv[nArg] ) ||
! rtl_str_compare( "--help", argv[nArg] ) )
{
printHelp( argv[0] );
return 0;
}
else if( ! rtl_str_compare( "-a", argv[nArg] ) ||
! rtl_str_compare( "--extract-add-streams", argv[nArg] ) )
{
aHdl = write_addStreams;
}
else if( ! rtl_str_compare( "-f", argv[nArg] ) ||
! rtl_str_compare( "--extract-fonts", argv[nArg] ) )
{
aHdl = write_fonts;
}
else if( ! rtl_str_compare( "-o", argv[nArg] ) ||
! rtl_str_compare( "--extract-objects", argv[nArg] ) )
{
aHdl = write_objects;
nArg++;
if( nArg < argc )
{
OString aObjs( argv[nArg] );
sal_Int32 nIndex = 0;
while( nIndex != -1 )
{
OString aToken( aObjs.getToken( 0, ',', nIndex ) );
sal_Int32 nObject = 0;
sal_Int32 nGeneration = 0;
sal_Int32 nGenIndex = 0;
nObject = aToken.getToken( 0, ':', nGenIndex ).toInt32();
if( nGenIndex != -1 )
nGeneration = aToken.getToken( 0, ':', nGenIndex ).toInt32();
s_aEmitObjects.push_back( std::pair<sal_Int32,sal_Int32>(nObject,nGeneration) );
}
}
}
else
{
fprintf( stderr, "unrecognized option \"%s\"\n",
argv[nArg] );
printHelp( argv[0] );
return 1;
}
}
else if( pInFile == nullptr )
pInFile = argv[nArg];
else if( pOutFile == nullptr )
pOutFile = argv[nArg];
}
if( ! pInFile )
{
fprintf( stderr, "no input file given\n" );
return 10;
}
if( ! pOutFile )
{
OString aFile( pInFile );
if( aFile.getLength() > 0 )
{
if( aFile.getLength() > 4 )
{
if( aFile.matchIgnoreAsciiCase( ".pdf", aFile.getLength()-4 ) )
aOutFile.append( pInFile, aFile.getLength() - 4 );
else
aOutFile.append( aFile );
}
aOutFile.append( "_unzip.pdf" );
pOutFile = aOutFile.getStr();
}
else
{
fprintf( stderr, "no output file given\n" );
return 11;
}
}
return handleFile( pInFile, pOutFile, pPassword, aHdl );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|