tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / sdext / source / pdfimport / xpdfwrapper / wrapper_gpl.cxx
blobebed120708cc237367f9ad317d7342c942b63913
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "pdfioutdev_gpl.hxx"
21 #ifdef _WIN32
22 # include <io.h>
23 # include <fcntl.h> /*_O_BINARY*/
24 #define WIN32_LEAN_AND_MEAN
25 #include <Windows.h>
26 #endif
27 #ifndef SYSTEM_POPPLER
28 #include <string> // std::string
29 #include <cstddef> // std::size_t
30 #include <config_folders.h> //LIBO_SHARE_FOLDER
31 #endif
33 FILE* g_binary_out=stderr;
35 #ifdef _WIN32
37 // Use Unicode API
39 static const wchar_t *ownerPassword = nullptr;
40 static const wchar_t *userPassword = nullptr;
41 static const wchar_t *outputFile = nullptr;
42 static const wchar_t *options = L"";
44 #define TO_STRING_VIEW(s) std::wstring_view(L##s)
45 using my_string = std::wstring;
47 // Poppler expects UTF-8 strings on Windows - see its openFile in poppler/goo/gfile.cc.
48 static std::string myStringToStdString(std::wstring_view s)
50 int len = WideCharToMultiByte(CP_UTF8, 0, s.data(), s.size(), nullptr, 0, nullptr, nullptr);
51 char* buff = static_cast<char*>(_alloca(len * sizeof(char)));
52 len = WideCharToMultiByte(CP_UTF8, 0, s.data(), s.size(), buff, len, nullptr, nullptr);
53 return std::string(buff, len);
56 #else // ! _WIN32
58 static const char *ownerPassword = nullptr;
59 static const char *userPassword = nullptr;
60 static const char *outputFile = nullptr;
61 static const char *options = "";
63 #define TO_STRING_VIEW(s) std::string_view(s)
64 using my_string = std::string;
66 static std::string myStringToStdString(std::string&& s) { return std::move(s); }
68 #endif
70 #ifdef _WIN32
71 int wmain(int argc, wchar_t **argv)
72 #else
73 int main(int argc, char **argv)
74 #endif
76 int k = 1;
77 while (k < argc)
79 if (argv[k] == TO_STRING_VIEW("-f"))
81 outputFile = argv[k+1];
82 argc -= 2;
83 for (int j = k; j < argc; ++j)
84 argv[j] = argv[j+2];
86 else if (argv[k] == TO_STRING_VIEW("-o"))
88 options = argv[k+1];
89 argc -= 2;
90 for (int j = k; j < argc; ++j)
91 argv[j] = argv[j+2];
94 else if (argv[k] == TO_STRING_VIEW("-opw"))
96 ownerPassword = argv[k+1];
97 argc -= 2;
98 for (int j = k; j < argc; ++j)
99 argv[j] = argv[j+2];
101 else if (argv[k] == TO_STRING_VIEW("-upw"))
103 userPassword = argv[k+1];
104 argc -= 2;
105 for (int j = k; j < argc; ++j)
106 argv[j] = argv[j+2];
108 ++k;
111 /* Get data directory location */
112 #ifdef SYSTEM_POPPLER
113 const char* datadir = nullptr;
114 #else
115 /* Creates an absolute path to the poppler_data directory, by taking the path
116 * to the xpdfimport executable (provided in argv[0], and concatenating a
117 * relative path to the poppler_data directory from the program directory. */
118 const my_string execPath = argv[0];
119 const std::size_t filenameStartPos = execPath.find_last_of(TO_STRING_VIEW("/\\")) + 1;
120 const my_string programPath = execPath.substr(0, filenameStartPos);
121 const std::string popplerDataPath = myStringToStdString(programPath + my_string(TO_STRING_VIEW("../" LIBO_SHARE_FOLDER "/xpdfimport/poppler_data")));
122 const char* datadir = popplerDataPath.c_str();
123 #endif
125 // read config file
126 #if POPPLER_CHECK_VERSION(0, 83, 0)
127 globalParams = std::make_unique<GlobalParams>(datadir);
128 #else
129 globalParams = new GlobalParams(datadir);
130 #endif
131 globalParams->setErrQuiet(true);
132 #if defined(_MSC_VER)
133 globalParams->setupBaseFonts(nullptr);
134 #endif
136 // try to read a possible open password from stdin
137 char aPwBuf[129];
138 aPwBuf[128] = 0;
139 if( ! fgets( aPwBuf, sizeof(aPwBuf)-1, stdin ) )
140 aPwBuf[0] = 0; // mark as empty
141 else
143 for( size_t i = 0; i < sizeof(aPwBuf); i++ )
145 if( aPwBuf[i] == '\n' )
147 aPwBuf[i] = 0;
148 break;
153 // PDFDoc takes over ownership for all strings below
154 GooString* pFileName = new GooString(myStringToStdString(argv[1]));
156 // check for password string(s)
157 GooString* pOwnerPasswordStr( aPwBuf[0] != 0
158 ? new GooString( aPwBuf )
159 : (ownerPassword
160 ? new GooString(myStringToStdString(ownerPassword))
161 : nullptr ) );
162 GooString* pUserPasswordStr( aPwBuf[0] != 0
163 ? new GooString( aPwBuf )
164 : (userPassword
165 ? new GooString(myStringToStdString(userPassword))
166 : nullptr ) );
167 if (outputFile)
168 #if defined _WIN32
169 g_binary_out = _wfopen(outputFile, L"wb");
170 #else
171 g_binary_out = fopen(outputFile,"wb");
172 #endif
174 #ifdef _WIN32
175 // Win actually modifies output for O_TEXT file mode, so need to
176 // revert to binary here
177 _setmode( _fileno( g_binary_out ), _O_BINARY );
178 #endif
180 #if POPPLER_CHECK_VERSION(22, 6, 0)
181 PDFDoc aDoc( std::make_unique<GooString>(pFileName),
182 std::optional<GooString>(pOwnerPasswordStr),
183 std::optional<GooString>(pUserPasswordStr) );
184 #else
185 PDFDoc aDoc( pFileName,
186 pOwnerPasswordStr,
187 pUserPasswordStr );
188 #endif
190 if (!aDoc.isOk())
191 return aDoc.getErrorCode();
193 pdfi::PDFOutDev aOutDev(&aDoc);
194 if (options == TO_STRING_VIEW("SkipImages")) {
195 aOutDev.setSkipImages(true);
198 // tell the receiver early - needed for proper progress calculation
199 const int nPages = aDoc.getNumPages();
200 pdfi::PDFOutDev::setPageNum(nPages);
202 // virtual resolution of the PDF OutputDev in dpi
203 static const int PDFI_OUTDEV_RESOLUTION = 7200;
205 // do the conversion
206 for (int i = 1; i <= nPages; ++i)
208 aDoc.displayPage(&aOutDev,
210 PDFI_OUTDEV_RESOLUTION,
211 PDFI_OUTDEV_RESOLUTION,
212 0, true, true, true);
213 aDoc.processLinks(&aOutDev, i);
216 return 0;
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */