Debian Codegraph @

src/conv/helper/helper.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2+
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 2.0 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * Major Contributor(s):
16 * Copyright (C) 2002 William Lachance ([email protected])
17 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
18 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
19 * Copyright (C) 2006, 2007 Andrew Ziem
20 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
21 *
22 *
23 * All Rights Reserved.
24 *
25 * For minor contributions see the git repository.
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
29 * in which case the provisions of the LGPLv2+ are applicable
30 * instead of those above.
31 */
32 #include <stdarg.h>
33 #include <string.h>
34
35 #include <algorithm>
36 #include <cstring>
37 #include <cstdio>
38 #include <iostream>
39 #include <map>
40 #include <vector>
41
42 #include "helper.h"
43
44 #include <librevenge/librevenge.h>
45 #include <libwps/libwps.h>
46
47 #ifndef __EMSCRIPTEN__
48 # include <sys/stat.h>
49 # include <sys/types.h>
50 #endif
51
52 namespace libwpsHelper
53 {
54 ////////////////////////////////////////////////////////////
55 // debug
56 ////////////////////////////////////////////////////////////
57 #ifdef DEBUG
58 void printDebugMsg(const char *format, ...)
59 {
60 va_list args;
61 va_start(args, format);
62 std::vfprintf(stderr, format, args);
63 va_end(args);
64 }
65 #endif
66
67 #ifndef __EMSCRIPTEN__
68 ////////////////////////////////////////////////////////////
69 // static class to create a RVNGInputStream for some data
70 ////////////////////////////////////////////////////////////
71
72 /** internal class used to create a structrured RVNGInputStream from two files
73 */
74 class FolderStream final: public librevenge::RVNGInputStream
75 {
76 public:
77 //! constructor
78 FolderStream()
79 : librevenge::RVNGInputStream()
80 , m_nameToPathMap()
81 {
82 }
83
84 //! destructor
85 ~FolderStream() override
86 {
87 }
88
89 //! add a file
90 void addFile(std::string const &path, std::string const &shortName)
91 {
92 m_nameToPathMap[shortName]=path;
93 }
94 /**! reads numbytes data.
95
96 * \return a pointer to the read elements
97 */
98 const unsigned char *read(unsigned long, unsigned long &) final
99 {
100 return nullptr;
101 }
102 //! returns actual offset position
103 long tell() final
104 {
105 return 0;
106 }
107 /*! \brief seeks to a offset position, from actual, beginning or ending position
108 * \return 0 if ok
109 */
110 int seek(long, librevenge::RVNG_SEEK_TYPE) final
111 {
112 return 1;
113 }
114 //! returns true if we are at the end of the section/file
115 bool isEnd() final
116 {
117 return true;
118 }
119
120 /** returns true if the stream is ole
121
122 \sa returns always false*/
123 bool isStructured() final
124 {
125 return true;
126 }
127 /** returns the number of sub streams.
128
129 \sa returns always 2*/
130 unsigned subStreamCount() final
131 {
132 return unsigned(m_nameToPathMap.size());
133 }
134 /** returns the ith sub streams name */
135 const char *subStreamName(unsigned id) final
136 {
137 std::map<std::string, std::string>::const_iterator it=m_nameToPathMap.begin();
138 for (unsigned i=0; i<id; ++i)
139 {
140 if (it==m_nameToPathMap.end()) return nullptr;
141 ++it;
142 }
143 if (it==m_nameToPathMap.end()) return nullptr;
144 return it->first.c_str();
145 }
146 /** returns true if a substream with name exists */
147 bool existsSubStream(const char *name) final
148 {
149 return name && m_nameToPathMap.find(name)!= m_nameToPathMap.end();
150 }
151 /** return a new stream for a ole zone */
152 librevenge::RVNGInputStream *getSubStreamByName(const char *name) final;
153 /** return a new stream for a ole zone */
154 librevenge::RVNGInputStream *getSubStreamById(unsigned id) final
155 {
156 char const *name=subStreamName(id);
157 if (name==nullptr) return nullptr;
158 return getSubStreamByName(name);
159 }
160 private:
161 /// the map short name to path
162 std::map<std::string, std::string> m_nameToPathMap;
163 FolderStream(const FolderStream &) = delete; // copy is not allowed
164 FolderStream &operator=(const FolderStream &) = delete; // assignment is not allowed
165 };
166
167 librevenge::RVNGInputStream *FolderStream::getSubStreamByName(const char *name)
168 {
169 if (m_nameToPathMap.find(name)== m_nameToPathMap.end()) return nullptr;
170 return new librevenge::RVNGFileStream(m_nameToPathMap.find(name)->second.c_str());
171 }
172
173 ////////////////////////////////////////////////////////////
174 // main functions
175 ////////////////////////////////////////////////////////////
176
177 /* check if the file is a lotus123 file and a .fm3 file exists or
178 if the file is a dos lotus file and a .fmt file exists.
179 If yes, try to convert it in a structured input which can be parsed by libwps */
180 static std::shared_ptr<librevenge::RVNGInputStream> createMergeInput(char const *fName, librevenge::RVNGInputStream &input)
181 try
182 {
183 std::shared_ptr<FolderStream> res;
184
185 /* we do not want to compress already compressed file.
186 So check if the file is structured, is a binhex file
187 */
188 if (!fName || input.isStructured()) return res;
189
190 // first check
191 std::string name(fName);
192 size_t len=name.length();
193 if (len<=4 || name[len-4]!='.') return res;
194 std::string extension=name.substr(len-3, 2);
195 if (extension!="wk" && extension!="WK")
196 return res;
197
198 // check the file header
199 if (input.seek(0, librevenge::RVNG_SEEK_SET)!=0) return res;
200 unsigned long numBytesRead;
201 const unsigned char *data=input.read(6, numBytesRead);
202 if (!data || numBytesRead!=6 || data[0]!=0 || data[1]!=0 || data[3]!=0) return res;
203 bool oldFile=false;
204 if (data[2]==2 && data[4]==6 && data[5]==4)
205 oldFile=true;
206 else if (data[2]!=0x1a || data[4]>=2 || data[5]!=0x10) return res;
207
208 // check if the .fm3 file exists
209 std::string fmName=name.substr(0, len-3);
210 if (extension=="wk")
211 fmName+=oldFile ? "fmt" : "fm3";
212 else
213 fmName+=oldFile ? "FMT" : "FM3";
214 struct stat status;
215 if (stat(fmName.c_str(), &status) || !S_ISREG(status.st_mode))
216 return res;
217
218 res.reset(new FolderStream());
219 if (oldFile)
220 {
221 res->addFile(name, "WK1");
222 res->addFile(fmName, "FMT");
223 }
224 else
225 {
226 res->addFile(name, "WK3");
227 res->addFile(fmName, "FM3");
228 }
229 return res;
230 }
231 catch (...)
232 {
233 return std::shared_ptr<librevenge::RVNGInputStream>();
234 }
235 #endif
236
237 ////////////////////////////////////////////////////////////
238 // main functions
239 ////////////////////////////////////////////////////////////
240
241 std::shared_ptr<librevenge::RVNGInputStream> isSupported
242 (char const *filename, libwps::WPSConfidence &confidence, libwps::WPSKind &kind, bool &needEncoding)
243 {
244 std::shared_ptr<librevenge::RVNGInputStream> input(new librevenge::RVNGFileStream(filename));
245 libwps::WPSCreator creator;
246 #ifndef __EMSCRIPTEN__
247 try
248 {
249 auto mergeInput=createMergeInput(filename, *input);
250 if (mergeInput)
251 {
252 confidence=libwps::WPSDocument::isFileFormatSupported(mergeInput.get(), kind, creator, needEncoding);
253 if (confidence != libwps::WPS_CONFIDENCE_NONE)
254 return mergeInput;
255 }
256 }
257 catch (...)
258 {
259 }
260 #endif
261 try
262 {
263 confidence = libwps::WPSDocument::isFileFormatSupported(input.get(), kind, creator, needEncoding);
264 if (confidence != libwps::WPS_CONFIDENCE_NONE)
265 return input;
266 }
267 catch (...)
268 {
269 }
270 return std::shared_ptr<librevenge::RVNGInputStream>();
271 }
272
273 bool checkErrorAndPrintMessage(libwps::WPSResult result)
274 {
275 if (result == libwps::WPS_ENCRYPTION_ERROR)
276 fprintf(stderr, "ERROR: Encrypted file, bad Password!\n");
277 else if (result == libwps::WPS_FILE_ACCESS_ERROR)
278 fprintf(stderr, "ERROR: File Exception!\n");
279 else if (result == libwps::WPS_PARSE_ERROR)
280 fprintf(stderr, "ERROR: Parse Exception!\n");
281 else if (result == libwps::WPS_OLE_ERROR)
282 fprintf(stderr, "ERROR: File is an OLE document, but does not contain a Microsoft Works stream!\n");
283 else if (result != libwps::WPS_OK)
284 fprintf(stderr, "ERROR: Unknown Error!\n");
285 else
286 return false;
287 return true;
288 }
289
290 }
291 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */