26#include <osl/thread.h>
28#include <rtl/ustring.hxx>
29#include <rtl/strbuf.hxx>
40 "USAGE: %s [-h,--help]\n"
41 " %s [-pw, --password <password>] <inputfile> [<outputfile>]\n"
42 " %s <-a, --extract-add-streams> [-pw, --password <password>] <inputfile> [<outputfile>]\n"
43 " %s <-f, --extract-fonts> [-pw, --password <password>] <inputfile> [<outputfile>]\n"
44 " %s <-o, --extract-objects> <o0>[:<g0>][,<o1>[:g1][,...]] [-pw, --password <password>] <inputfile> [<outputfile>]\n"
45 " -h, --help: show help\n"
46 " -a, --extract-add-streams: extracts additional streams to outputfile_object\n"
47 " and prints the mimetype found to stdout\n"
48 " -f, --extract-fonts: extracts fonts (currently only type1 and truetype are supported\n"
49 " -o, --extract-objects: extracts object streams, the syntax of the argument is comma separated\n"
50 " object numbers, where object number and generation number are separated by \':\'\n"
51 " an omitted generation number defaults to 0\n"
52 " -pw, --password: use password for decryption\n"
54 "note: -f, -a, -o and normal unzip operation are mutually exclusive\n"
55 , pExe, pExe, pExe, pExe, pExe );
62 oslFileHandle m_aHandle;
66 void openReadFile(
const char* pOrigName );
69 FileEmitContext(
const char* pFileName,
const char* pOrigName,
const PDFContainer* pTop );
70 virtual ~FileEmitContext()
override;
72 virtual bool write(
const void* pBuf,
unsigned int nLen )
noexcept override;
73 virtual unsigned int getCurPos() noexcept override;
74 virtual
bool copyOrigBytes(
unsigned int nOrigOffset,
unsigned int nLen ) noexcept override;
75 virtual
unsigned int readOrigBytes(
unsigned int nOrigOffset,
unsigned int nLen,
void* pBuf ) noexcept override;
80FileEmitContext::FileEmitContext( const
char* pFileName, const
char* pOrigName, const
PDFContainer* pTop )
87 OStringToOUString( std::string_view( pFileName ), osl_getThreadTextEncoding() ) );
89 if( osl_getFileURLFromSystemPath( aSysFile.pData, &
aURL.pData ) != osl_File_E_None )
91 fprintf( stderr,
"filename conversion \"%s\" failed\n", pFileName );
95 if( osl_openFile(
aURL.pData, &m_aHandle, osl_File_OpenFlag_Write ) == osl_File_E_None )
97 if( osl_setFileSize( m_aHandle, 0 ) != osl_File_E_None )
99 fprintf( stderr,
"could not truncate %s\n", pFileName );
100 osl_closeFile( m_aHandle );
104 else if( osl_openFile(
aURL.pData, &m_aHandle,
105 osl_File_OpenFlag_Write |osl_File_OpenFlag_Create ) != osl_File_E_None )
107 fprintf( stderr,
"could not open %s\n", pFileName );
112 openReadFile( pOrigName );
115FileEmitContext::~FileEmitContext()
118 osl_closeFile( m_aHandle );
123void FileEmitContext::openReadFile(
const char* pInFile )
126 OStringToOUString( std::string_view( pInFile ), osl_getThreadTextEncoding() ) );
128 if( osl_getFileURLFromSystemPath( aSysFile.pData, &
aURL.pData ) != osl_File_E_None )
130 fprintf( stderr,
"filename conversion \"%s\" failed\n", pInFile );
134 if( osl_openFile(
aURL.pData, &
m_aReadHandle, osl_File_OpenFlag_Read ) != osl_File_E_None )
136 fprintf( stderr,
"could not open %s\n", pInFile );
140 if( osl_setFilePos(
m_aReadHandle, osl_Pos_End, 0 ) != osl_File_E_None )
142 fprintf( stderr,
"could not seek to end of %s\n", pInFile );
147 sal_uInt64 nFileSize = 0;
148 if( osl_getFilePos(
m_aReadHandle, &nFileSize ) != osl_File_E_None )
150 fprintf( stderr,
"could not get end pos of %s\n", pInFile );
155 m_nReadLen =
static_cast<unsigned int>(nFileSize);
158bool FileEmitContext::write(
const void* pBuf,
unsigned int nLen )
noexcept
163 sal_uInt64 nWrite =
static_cast<sal_uInt64
>(nLen);
164 sal_uInt64 nWritten = 0;
165 return (osl_writeFile( m_aHandle, pBuf, nWrite, &nWritten ) == osl_File_E_None)
166 && nWrite == nWritten;
169unsigned int FileEmitContext::getCurPos() noexcept
171 sal_uInt64 nFileSize = 0;
174 if( osl_getFilePos( m_aHandle, &nFileSize ) != osl_File_E_None )
177 return static_cast<unsigned int>(nFileSize);
180bool FileEmitContext::copyOrigBytes(
unsigned int nOrigOffset,
unsigned int nLen )
noexcept
185 if( osl_setFilePos(
m_aReadHandle, osl_Pos_Absolut, nOrigOffset ) != osl_File_E_None )
187 fprintf( stderr,
"could not seek to offset %u\n", nOrigOffset );
190 void* pBuf = std::malloc( nLen );
193 sal_uInt64 nBytesRead = 0;
194 if( osl_readFile(
m_aReadHandle, pBuf, nLen, &nBytesRead ) != osl_File_E_None
195 || nBytesRead !=
static_cast<sal_uInt64
>(nLen) )
197 fprintf( stderr,
"could not read %u bytes\n", nLen );
201 bool bRet =
write( pBuf, nLen );
206unsigned int FileEmitContext::readOrigBytes(
unsigned int nOrigOffset,
unsigned int nLen,
void* pBuf )
noexcept
211 if( osl_setFilePos(
m_aReadHandle, osl_Pos_Absolut, nOrigOffset ) != osl_File_E_None )
213 fprintf( stderr,
"could not seek to offset %u\n", nOrigOffset );
216 sal_uInt64 nBytesRead = 0;
217 if( osl_readFile(
m_aReadHandle, pBuf, nLen, &nBytesRead ) != osl_File_E_None )
219 return static_cast<unsigned int>(nBytesRead);
233 fprintf( stdout,
"have a %s PDF file\n", pPDFFile->
isEncrypted() ?
"encrypted" :
"unencrypted" );
235 fprintf( stdout,
"password %s\n",
237 nRet = pHdl( pInFile, pOutFile, pPDFFile );
247 FileEmitContext aContext( pOutFile, pInFile, pPDFFile );
249 pPDFFile->
emit(aContext);
257 for(
unsigned int i = 0;
i < nArrayElements-1 && nRet == 0;
i++ )
262 fprintf( stderr,
"error: no mimetype element\n" );
264 fprintf( stderr,
"error: no stream ref element\n" );
265 if( pMimeType && pStreamRef )
267 fprintf( stdout,
"found stream %d %d with mimetype %s\n",
273 OString aOutStream = pOutFile +
274 OString::Concat(
"_stream_") +
275 OString::number( sal_Int32(pStreamRef->
m_nNumber) ) +
278 FileEmitContext aContext( aOutStream.getStr(), pInFile, pPDFFile );
280 pObject->writeStream( aContext, pPDFFile );
284 fprintf( stderr,
"object not found\n" );
302 if( pTrailer && pTrailer->
m_pDict )
305 auto add_stream = pTrailer->
m_pDict->
m_aMap.find(
"AdditionalStreams" );
330 std::unordered_map<OString,PDFEntry*>::iterator map_it =
331 pDict->
m_aMap.find(
"Type" );
332 if( map_it == pDict->
m_aMap.end() )
338 if(
pName->m_aName !=
"FontDescriptor" )
343 map_it = pDict->
m_aMap.find(
"FontName" );
344 if( map_it == pDict->
m_aMap.end() )
349 OString aFontName(
pName->m_aName );
352 const char* pFileType =
nullptr;
354 map_it = pDict->
m_aMap.find(
"FontFile" );
355 if( map_it != pDict->
m_aMap.end() )
357 pStreamRef =
dynamic_cast<PDFObjectRef*
>(map_it->second);
365 map_it = pDict->
m_aMap.find(
"FontFile2" );
366 if( map_it != pDict->
m_aMap.end() )
368 pStreamRef =
dynamic_cast<PDFObjectRef*
>(map_it->second);
381 OStringBuffer aOutStream( OString::Concat(i_pOutFile)
383 + OString::number( sal_Int32(pStreamRef->
m_nNumber) )
390 aOutStream.append( OString::Concat(
".") + pFileType );
392 FileEmitContext aContext( aOutStream.getStr(), i_pInFile, i_pPDFFile );
394 pStream->writeStream( aContext, i_pPDFFile );
411 fprintf( stderr,
"object %d %d not found !\n",
static_cast<int>(nObject),
static_cast<int>(nGeneration) );
415 OString aOutStream = i_pOutFile +
416 OString::Concat(
"_stream_") +
417 OString::number( nObject ) +
419 OString::number( nGeneration );
420 FileEmitContext aContext( aOutStream.getStr(), i_pInFile, i_pPDFFile );
422 pStream->writeStream( aContext, i_pPDFFile );
429 const char* pInFile =
nullptr;
430 const char* pOutFile =
nullptr;
431 const char* pPassword =
nullptr;
432 OStringBuffer aOutFile( 256 );
435 for(
int nArg = 1; nArg < argc; nArg++ )
437 if( argv[nArg][0] ==
'-' )
439 if( ! rtl_str_compare(
"-pw", argv[nArg] ) ||
440 ! rtl_str_compare(
"--password" , argv[nArg] ) )
444 fprintf( stderr,
"no password given\n" );
448 pPassword = argv[nArg];
450 else if( ! rtl_str_compare(
"-h", argv[nArg] ) ||
451 ! rtl_str_compare(
"--help", argv[nArg] ) )
456 else if( ! rtl_str_compare(
"-a", argv[nArg] ) ||
457 ! rtl_str_compare(
"--extract-add-streams", argv[nArg] ) )
461 else if( ! rtl_str_compare(
"-f", argv[nArg] ) ||
462 ! rtl_str_compare(
"--extract-fonts", argv[nArg] ) )
466 else if( ! rtl_str_compare(
"-o", argv[nArg] ) ||
467 ! rtl_str_compare(
"--extract-objects", argv[nArg] ) )
473 OString aObjs( argv[nArg] );
477 OString aToken( aObjs.getToken( 0,
',',
nIndex ) );
478 sal_Int32 nObject = 0;
479 sal_Int32 nGeneration = 0;
480 sal_Int32 nGenIndex = 0;
482 if( nGenIndex != -1 )
484 s_aEmitObjects.push_back( std::pair<sal_Int32,sal_Int32>(nObject,nGeneration) );
490 fprintf( stderr,
"unrecognized option \"%s\"\n",
496 else if( pInFile ==
nullptr )
497 pInFile = argv[nArg];
498 else if( pOutFile ==
nullptr )
499 pOutFile = argv[nArg];
503 fprintf( stderr,
"no input file given\n" );
508 OString aFile( pInFile );
509 if( aFile.getLength() > 0 )
511 if( aFile.getLength() > 4 )
513 if( aFile.matchIgnoreAsciiCase(
".pdf", aFile.getLength()-4 ) )
514 aOutFile.append( pInFile, aFile.getLength() - 4 );
516 aOutFile.append( aFile );
518 aOutFile.append(
"_unzip.pdf" );
519 pOutFile = aOutFile.getStr();
523 fprintf( stderr,
"no output file given\n" );
528 return handleFile( pInFile, pOutFile, pPassword, aHdl );
virtual bool write(const void *pBuf, unsigned int nLen)=0
virtual unsigned int getCurPos()=0
EmbeddedObjectRef * pObject
static osl::File * pStream
oslFileHandle m_aReadHandle
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
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)
static int write_addStreams(const char *pInFile, const char *pOutFile, PDFFile *pPDFFile)
static int write_fonts(const char *i_pInFile, const char *i_pOutFile, PDFFile *i_pPDFFile)
int(* PDFFileHdl)(const char *, const char *, PDFFile *)
static int write_unzipFile(const char *pInFile, const char *pOutFile, PDFFile *pPDFFile)
static int handleFile(const char *pInFile, const char *pOutFile, const char *pPassword, PDFFileHdl pHdl)
static void printHelp(const char *pExe)
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
static int write_addStreamArray(const char *pOutFile, PDFArray *pStreams, PDFFile *pPDFFile, const char *pInFile)
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
PDFObject * findObject(unsigned int nNumber, unsigned int nGeneration) const
std::vector< std::unique_ptr< PDFEntry > > m_aSubElements
virtual bool emit(EmitContext &rWriteContext) const override
bool setupDecryptionData(const OString &rPwd) const
unsigned int m_nGeneration
static std::unique_ptr< PDFEntry > read(const char *pFileName)