1 /*************************************************************************
2  *
3  *  OpenOffice.org - a multi-platform office productivity suite
4  *
5  *  The Contents of this file are made available subject to
6  *  the terms of GNU General Public License Version 2.
7  *
8  *
9  *    GNU General Public License, version 2
10  *    =============================================
11  *    Copyright 2005 by Sun Microsystems, Inc.
12  *    901 San Antonio Road, Palo Alto, CA 94303, USA
13  *
14  *    This program is free software; you can redistribute it and/or
15  *    modify it under the terms of the GNU General Public License as
16  *    published by the Free Software Foundation; either version 2 of
17  *    the License, or (at your option) any later version.
18  *
19  *    This program is distributed in the hope that it will be useful,
20  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *    GNU General Public License for more details.
23  *
24  *    You should have received a copy of the GNU General Public
25  *    License along with this program; if not, write to the Free
26  *    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27  *    Boston, MA 02110-1301, USA.
28  *
29  ************************************************************************/
30 
31 #include "pdfioutdev_gpl.hxx"
32 //#include "SecurityHandler.h"
33 #ifdef WNT
34 # include <io.h>
35 # include <fcntl.h>  /*_O_BINARY*/
36 #endif
37 
38 FILE* g_binary_out=stderr;
39 
40 #ifndef SYSTEM_POPPLER
41 static char ownerPassword[33] = "\001";
42 static char userPassword[33]  = "\001";
43 static char outputFile[256]   = "\001";
44 
45 static ArgDesc argDesc[] = {
46   {(char*)"-f",          argString,      outputFile,     sizeof(outputFile),
47    (char*)"output file for binary streams"},
48   {(char*)"-opw",        argString,      ownerPassword,  sizeof(ownerPassword),
49    (char*)"owner password (for encrypted files)"},
50   {(char*)"-upw",        argString,      userPassword,   sizeof(userPassword),
51    (char*)"user password (for encrypted files)"},
52   {NULL, argString, NULL, 0, NULL }
53 };
54 #else
55 static const char *ownerPassword = "\001";
56 static const char *userPassword  = "\001";
57 static const char *outputFile   = "\001";
58 #endif
59 
60 int main(int argc, char **argv)
61 {
62 #ifndef SYSTEM_POPPLER
63     // parse args; initialize to defaults
64     if( !parseArgs(argDesc, &argc, argv) )
65         return 1;
66 #else
67     int k = 0;
68     while (k < argc)
69     {
70         if (!strcmp(argv[k], "-f"))
71         {
72             outputFile = argv[k+1];
73             --argc;
74             for (int j = k; j < argc; ++j)
75                 argv[j] = argv[j+1];
76         }
77         else if (!strcmp(argv[k], "-opw"))
78         {
79             ownerPassword = argv[k+1];
80             --argc;
81             for (int j = k; j < argc; ++j)
82                 argv[j] = argv[j+1];
83         }
84         else if (!strcmp(argv[k], "-upw"))
85         {
86             userPassword = argv[k+1];
87             --argc;
88             for (int j = k; j < argc; ++j)
89                 argv[j] = argv[j+1];
90         }
91 	++k;
92     }
93 #endif
94 
95     if( argc < 2 )
96         return 1;
97 
98     // read config file
99     globalParams = new GlobalParams(
100 #ifndef SYSTEM_POPPLER
101         (char*)""
102 #endif
103     );
104     globalParams->setErrQuiet(gTrue);
105 #if !defined(SYSTEM_POPPLER) || defined(_MSC_VER)
106     globalParams->setupBaseFonts(NULL);
107 #endif
108 
109     // try to read a possible open password form stdin
110     char aPwBuf[129];
111     aPwBuf[128] = 0;
112     if( ! fgets( aPwBuf, sizeof(aPwBuf)-1, stdin ) )
113         aPwBuf[0] = 0; // mark as empty
114     else
115     {
116         for( unsigned int i = 0; i < sizeof(aPwBuf); i++ )
117         {
118             if( aPwBuf[i] == '\n' )
119             {
120                 aPwBuf[i] = 0;
121                 break;
122             }
123         }
124     }
125 
126     // PDFDoc takes over ownership for all strings below
127     GooString* pFileName    = new GooString(argv[1]);
128     GooString* pTempErrFileName     = new GooString("_err.pdf");
129     GooString* pTempErrFileNamePath = new GooString(argv[0]);
130 
131     GooString* pErrFileName = new GooString(pTempErrFileNamePath,pTempErrFileName);
132 
133 
134     // check for password string(s)
135     GooString* pOwnerPasswordStr( aPwBuf[0] != 0
136                                  ? new GooString( aPwBuf )
137                                  : (ownerPassword[0] != '\001'
138                                     ? new GooString(ownerPassword)
139                                     : (GooString *)NULL ) );
140     GooString* pUserPasswordStr(  userPassword[0] != '\001'
141                                   ? new GooString(userPassword)
142                                   : (GooString *)NULL );
143     if( outputFile[0] != '\001' )
144         g_binary_out = fopen(outputFile,"wb");
145 
146 #ifdef WNT
147     // Win actually modifies output for O_TEXT file mode, so need to
148     // revert to binary here
149     _setmode( _fileno( g_binary_out ), _O_BINARY );
150 #endif
151 
152     PDFDoc aDoc( pFileName,
153                  pOwnerPasswordStr,
154                  pUserPasswordStr );
155 
156     PDFDoc aErrDoc( pErrFileName,
157                  pOwnerPasswordStr,
158                  pUserPasswordStr );
159 
160 
161    // Check various permissions.
162    if ( !aDoc.isOk()||
163         !aDoc.okToPrint() ||
164         !aDoc.okToChange()||
165         !aDoc.okToCopy()||
166         !aDoc.okToAddNotes() )
167    {
168         pdfi::PDFOutDev* pOutDev( new pdfi::PDFOutDev(&aErrDoc) );
169 
170         const int nPages = aErrDoc.isOk() ? aErrDoc.getNumPages() : 0;
171 
172         // tell receiver early - needed for proper progress calculation
173         pOutDev->setPageNum( nPages );
174 
175         // virtual resolution of the PDF OutputDev in dpi
176         static const int PDFI_OUTDEV_RESOLUTION=7200;
177 
178        // do the conversion
179        for( int i=1; i<=nPages; ++i )
180        {
181           aErrDoc.displayPage( pOutDev,
182                             i,
183                             PDFI_OUTDEV_RESOLUTION,
184                             PDFI_OUTDEV_RESOLUTION,
185                             0, gTrue, gTrue, gTrue );
186           aErrDoc.processLinks( pOutDev, i );
187        }
188    }
189    else
190    {
191       pdfi::PDFOutDev* pOutDev( new pdfi::PDFOutDev(&aDoc) );
192 
193       // tell receiver early - needed for proper progress calculation
194       pOutDev->setPageNum( aDoc.getNumPages() );
195 
196       // virtual resolution of the PDF OutputDev in dpi
197       static const int PDFI_OUTDEV_RESOLUTION=7200;
198 
199       // do the conversion
200       const int nPages = aDoc.getNumPages();
201       for( int i=1; i<=nPages; ++i )
202       {
203         aDoc.displayPage( pOutDev,
204                           i,
205                           PDFI_OUTDEV_RESOLUTION,
206                           PDFI_OUTDEV_RESOLUTION,
207                           0, gTrue, gTrue, gTrue );
208         aDoc.processLinks( pOutDev, i );
209       }
210    }
211     return 0;
212 }
213 
214