| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
/* libmwaw |
| 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 |
|
| 33 |
#include <sys/stat.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#if defined(WITH_EXTENDED_FS) && WITH_EXTENDED_FS>0 |
| 36 |
# include <sys/xattr.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
|
| 43 |
#include <algorithm> |
| 44 |
#include <cstring> |
| 45 |
#include <iostream> |
| 46 |
#include <map> |
| 47 |
#include <vector> |
| 48 |
|
| 49 |
#include "helper.h" |
| 50 |
#include <librevenge/librevenge.h> |
| 51 |
#include <libmwaw/libmwaw.hxx> |
| 52 |
|
| 53 |
namespace libmwawHelper |
| 54 |
{ |
| 55 |
#ifndef __EMSCRIPTEN__ |
| 56 |
//////////////////////////////////////////////////////////// |
| 57 |
// static class to create a RVNGInputStream for some data |
| 58 |
//////////////////////////////////////////////////////////// |
| 59 |
|
| 60 |
/** internal class used to create a RVNGInputStream from a unsigned char's pointer |
| 61 |
|
| 62 |
\note this class (highly inspired from librevenge) does not |
| 63 |
implement the isStructured's protocol, ie. it only returns false. |
| 64 |
*/ |
| 65 |
class StringStream final : public librevenge::RVNGInputStream |
| 66 |
{ |
| 67 |
public: |
| 68 |
//! constructor |
| 69 |
explicit StringStream(std::vector<unsigned char> const &buffer) |
| 70 |
: librevenge::RVNGInputStream() |
| 71 |
, m_buffer(buffer) |
| 72 |
, m_offset(0) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
//! destructor |
| 77 |
~StringStream() final |
| 78 |
{ |
| 79 |
} |
| 80 |
|
| 81 |
/**! reads numbytes data. |
| 82 |
|
| 83 |
* \return a pointer to the read elements |
| 84 |
*/ |
| 85 |
const unsigned char *read(unsigned long numBytes, unsigned long &numBytesRead) final; |
| 86 |
//! returns actual offset position |
| 87 |
long tell() final |
| 88 |
{ |
| 89 |
return m_offset; |
| 90 |
} |
| 91 |
/*! \brief seeks to a offset position, from actual, beginning or ending position |
| 92 |
* \return 0 if ok |
| 93 |
*/ |
| 94 |
int seek(long offset, librevenge::RVNG_SEEK_TYPE seekType) final; |
| 95 |
//! returns true if we are at the end of the section/file |
| 96 |
bool isEnd() final |
| 97 |
{ |
| 98 |
if (long(m_offset) >= long(m_buffer.size())) |
| 99 |
return true; |
| 100 |
|
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
/** returns true if the stream is ole |
| 105 |
|
| 106 |
\sa returns always false*/ |
| 107 |
bool isStructured() final |
| 108 |
{ |
| 109 |
return false; |
| 110 |
} |
| 111 |
/** returns the number of sub streams. |
| 112 |
|
| 113 |
\sa returns always 0*/ |
| 114 |
unsigned subStreamCount() final |
| 115 |
{ |
| 116 |
return 0; |
| 117 |
} |
| 118 |
/** returns the ith sub streams name |
| 119 |
|
| 120 |
\sa returns always 0*/ |
| 121 |
const char *subStreamName(unsigned) final |
| 122 |
{ |
| 123 |
return 0; |
| 124 |
} |
| 125 |
/** returns true if a substream with name exists |
| 126 |
|
| 127 |
\sa returns always false*/ |
| 128 |
bool existsSubStream(const char *) final |
| 129 |
{ |
| 130 |
return false; |
| 131 |
} |
| 132 |
/** return a new stream for a ole zone |
| 133 |
|
| 134 |
\sa returns always 0 */ |
| 135 |
librevenge::RVNGInputStream *getSubStreamByName(const char *) final |
| 136 |
{ |
| 137 |
return 0; |
| 138 |
} |
| 139 |
/** return a new stream for a ole zone |
| 140 |
|
| 141 |
\sa returns always 0 */ |
| 142 |
librevenge::RVNGInputStream *getSubStreamById(unsigned) final |
| 143 |
{ |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
private: |
| 148 |
//! the stream buffer |
| 149 |
std::vector<unsigned char> m_buffer; |
| 150 |
//! the stream offset |
| 151 |
long m_offset; |
| 152 |
StringStream(const StringStream &) = delete; // copy is not allowed |
| 153 |
StringStream &operator=(const StringStream &) = delete; // assignment is not allowed |
| 154 |
}; |
| 155 |
|
| 156 |
const unsigned char *StringStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
| 157 |
{ |
| 158 |
numBytesRead = 0; |
| 159 |
|
| 160 |
if (numBytes == 0) |
| 161 |
return 0; |
| 162 |
|
| 163 |
long numBytesToRead; |
| 164 |
|
| 165 |
if (static_cast<unsigned long>(m_offset)+numBytes < m_buffer.size()) |
| 166 |
numBytesToRead = long(numBytes); |
| 167 |
else |
| 168 |
numBytesToRead = long(m_buffer.size()) - m_offset; |
| 169 |
|
| 170 |
numBytesRead = static_cast<unsigned long>(numBytesToRead); // about as paranoid as we can be.. |
| 171 |
|
| 172 |
if (numBytesToRead == 0) |
| 173 |
return 0; |
| 174 |
|
| 175 |
long oldOffset = m_offset; |
| 176 |
m_offset += numBytesToRead; |
| 177 |
|
| 178 |
return &m_buffer[size_t(oldOffset)]; |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
int StringStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType) |
| 183 |
{ |
| 184 |
if (seekType == librevenge::RVNG_SEEK_CUR) |
| 185 |
m_offset += offset; |
| 186 |
else if (seekType == librevenge::RVNG_SEEK_SET) |
| 187 |
m_offset = offset; |
| 188 |
else if (seekType == librevenge::RVNG_SEEK_END) |
| 189 |
m_offset = offset+long(m_buffer.size()); |
| 190 |
|
| 191 |
if (m_offset < 0) { |
| 192 |
m_offset = 0; |
| 193 |
return -1; |
| 194 |
} |
| 195 |
if (long(m_offset) > long(m_buffer.size())) { |
| 196 |
m_offset = long(m_buffer.size()); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
/** internal class used to create a structrured RVNGInputStream from some files given there path name or there data */ |
| 204 |
class FolderStream final : public librevenge::RVNGInputStream |
| 205 |
{ |
| 206 |
public: |
| 207 |
//! constructor |
| 208 |
FolderStream() |
| 209 |
: librevenge::RVNGInputStream() |
| 210 |
, m_nameToPathMap() |
| 211 |
, m_nameToBufferMap() |
| 212 |
{ |
| 213 |
} |
| 214 |
|
| 215 |
//! destructor |
| 216 |
~FolderStream() final |
| 217 |
{ |
| 218 |
} |
| 219 |
|
| 220 |
//! add a file |
| 221 |
void addFile(std::string const &path, std::string const &shortName) |
| 222 |
{ |
| 223 |
m_nameToPathMap[shortName]=path; |
| 224 |
} |
| 225 |
//! add a file |
| 226 |
void addFile(std::vector<unsigned char> const &buffer, std::string const &shortName) |
| 227 |
{ |
| 228 |
m_nameToBufferMap[shortName]=buffer; |
| 229 |
} |
| 230 |
/**! reads numbytes data. |
| 231 |
|
| 232 |
* \return a pointer to the read elements |
| 233 |
*/ |
| 234 |
const unsigned char *read(unsigned long, unsigned long &) final |
| 235 |
{ |
| 236 |
return 0; |
| 237 |
} |
| 238 |
//! returns actual offset position |
| 239 |
long tell() final |
| 240 |
{ |
| 241 |
return 0; |
| 242 |
} |
| 243 |
/*! \brief seeks to a offset position, from actual, beginning or ending position |
| 244 |
* \return 0 if ok |
| 245 |
*/ |
| 246 |
int seek(long, librevenge::RVNG_SEEK_TYPE) final |
| 247 |
{ |
| 248 |
return 1; |
| 249 |
} |
| 250 |
//! returns true if we are at the end of the section/file |
| 251 |
bool isEnd() final |
| 252 |
{ |
| 253 |
return true; |
| 254 |
} |
| 255 |
|
| 256 |
/** returns true if the stream is ole |
| 257 |
|
| 258 |
\sa returns always false*/ |
| 259 |
bool isStructured() final |
| 260 |
{ |
| 261 |
return true; |
| 262 |
} |
| 263 |
/** returns the number of sub streams. |
| 264 |
|
| 265 |
\sa returns always 2*/ |
| 266 |
unsigned subStreamCount() final |
| 267 |
{ |
| 268 |
return unsigned(m_nameToPathMap.size()+m_nameToBufferMap.size()); |
| 269 |
} |
| 270 |
/** returns the ith sub streams name */ |
| 271 |
const char *subStreamName(unsigned id) final |
| 272 |
{ |
| 273 |
if (id<m_nameToPathMap.size()) { |
| 274 |
auto it=m_nameToPathMap.begin(); |
| 275 |
for (unsigned i=0; i<id; ++i) { |
| 276 |
if (it==m_nameToPathMap.end()) return 0; |
| 277 |
++it; |
| 278 |
} |
| 279 |
if (it==m_nameToPathMap.end()) return 0; |
| 280 |
return it->first.c_str(); |
| 281 |
} |
| 282 |
id=unsigned(id-m_nameToPathMap.size()); |
| 283 |
auto it=m_nameToBufferMap.begin(); |
| 284 |
for (unsigned i=0; i<id; ++i) { |
| 285 |
if (it==m_nameToBufferMap.end()) return 0; |
| 286 |
++it; |
| 287 |
} |
| 288 |
if (it==m_nameToBufferMap.end()) return 0; |
| 289 |
return it->first.c_str(); |
| 290 |
} |
| 291 |
/** returns true if a substream with name exists */ |
| 292 |
bool existsSubStream(const char *name) final |
| 293 |
{ |
| 294 |
return name && (m_nameToPathMap.find(name)!= m_nameToPathMap.end() || m_nameToBufferMap.find(name)!= m_nameToBufferMap.end()); |
| 295 |
} |
| 296 |
/** return a new stream for a ole zone */ |
| 297 |
librevenge::RVNGInputStream *getSubStreamByName(const char *name) final; |
| 298 |
/** return a new stream for a ole zone */ |
| 299 |
librevenge::RVNGInputStream *getSubStreamById(unsigned id) final |
| 300 |
{ |
| 301 |
char const *name=subStreamName(id); |
| 302 |
if (name==0) return 0; |
| 303 |
return getSubStreamByName(name); |
| 304 |
} |
| 305 |
private: |
| 306 |
/// the map short name to path |
| 307 |
std::map<std::string, std::string> m_nameToPathMap; |
| 308 |
/// the map short name to buffer |
| 309 |
std::map<std::string, std::vector<unsigned char> > m_nameToBufferMap; |
| 310 |
FolderStream(const FolderStream &) = delete; // copy is not allowed |
| 311 |
FolderStream &operator=(const FolderStream &) = delete; // assignment is not allowed |
| 312 |
}; |
| 313 |
|
| 314 |
librevenge::RVNGInputStream *FolderStream::getSubStreamByName(const char *name) |
| 315 |
{ |
| 316 |
if (m_nameToPathMap.find(name) != m_nameToPathMap.end()) |
| 317 |
return new librevenge::RVNGFileStream(m_nameToPathMap.find(name)->second.c_str()); |
| 318 |
if (m_nameToBufferMap.find(name) != m_nameToBufferMap.end()) |
| 319 |
return new StringStream(m_nameToBufferMap.find(name)->second); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
//////////////////////////////////////////////////////////// |
| 324 |
// static interface to the file system |
| 325 |
//////////////////////////////////////////////////////////// |
| 326 |
|
| 327 |
#if defined(WITH_EXTENDED_FS) && (WITH_EXTENDED_FS>0) |
| 328 |
static std::shared_ptr<FolderStream> getFileInput(char const *fName) |
| 329 |
{ |
| 330 |
std::shared_ptr<FolderStream> res; |
| 331 |
if (!fName) |
| 332 |
return res; |
| 333 |
// the rsrc fork can be accessed by adding "/..namedfork/rsrc" to the file name |
| 334 |
std::string rsrcName(fName); |
| 335 |
rsrcName += "/..namedfork/rsrc"; |
| 336 |
struct stat status; |
| 337 |
if (stat(rsrcName.c_str(), &status) || !S_ISREG(status.st_mode) || status.st_size==0) |
| 338 |
return res; |
| 339 |
/* I do not find any way to access the finderinfo fork directly. |
| 340 |
So I use getxattr */ |
| 341 |
# if WITH_EXTENDED_FS==1 |
| 342 |
# define MWAW_EXTENDED_FS , 0, XATTR_SHOWCOMPRESSION |
| 343 |
# else |
| 344 |
# define MWAW_EXTENDED_FS |
| 345 |
# endif |
| 346 |
if (getxattr(fName, "com.apple.FinderInfo", 0, 0 MWAW_EXTENDED_FS)<=0) |
| 347 |
return res; |
| 348 |
ssize_t sz=getxattr(fName, "com.apple.FinderInfo", 0, 0 MWAW_EXTENDED_FS); |
| 349 |
if (sz<=0) return res; |
| 350 |
std::vector<unsigned char> buffer; |
| 351 |
buffer.resize(size_t(sz)); |
| 352 |
if (getxattr(fName, "com.apple.FinderInfo", &buffer[0], size_t(sz) MWAW_EXTENDED_FS) != sz) |
| 353 |
return res; |
| 354 |
// ok, let create the folder stream |
| 355 |
res.reset(new FolderStream); |
| 356 |
res->addFile(fName, "DataFork"); |
| 357 |
res->addFile(rsrcName, "RsrcFork"); |
| 358 |
res->addFile(buffer, "InfoFork"); |
| 359 |
return res; |
| 360 |
} |
| 361 |
#else |
| 362 |
static std::shared_ptr<FolderStream> getFileInput(char const *) |
| 363 |
{ |
| 364 |
return std::shared_ptr<FolderStream>(); |
| 365 |
} |
| 366 |
#endif |
| 367 |
|
| 368 |
/* check if the file contains some resource, if yes, try to |
| 369 |
convert it in a structured input which can be parsed by libmwaw */ |
| 370 |
static std::shared_ptr<librevenge::RVNGInputStream> createFolderInput(char const *fName, librevenge::RVNGInputStream &input) |
| 371 |
try |
| 372 |
{ |
| 373 |
std::shared_ptr<FolderStream> res; |
| 374 |
|
| 375 |
/* we do not want to compress already compressed file. |
| 376 |
So check if the file is structured, is a binhex file |
| 377 |
*/ |
| 378 |
if (!fName) return res; |
| 379 |
|
| 380 |
unsigned long fileSize= input.seek(0, librevenge::RVNG_SEEK_END)==0 ? static_cast<unsigned long>(input.tell()) : 0; |
| 381 |
|
| 382 |
if (fileSize>46) { |
| 383 |
input.seek(0, librevenge::RVNG_SEEK_SET); |
| 384 |
unsigned long numBytesRead; |
| 385 |
const unsigned char *buf=input.read(46, numBytesRead); |
| 386 |
if (buf && numBytesRead==46 && strcmp(reinterpret_cast<char const *>(buf), "(This file must be converted with BinHex 4.0)")==0) |
| 387 |
return res; |
| 388 |
} |
| 389 |
res=getFileInput(fName); |
| 390 |
if (res) return res; |
| 391 |
|
| 392 |
// check if the resource are stored in a ._XXX or a __MACOSX/.XXX file |
| 393 |
std::string originalFile(fName); |
| 394 |
/** find folder and base file name*/ |
| 395 |
size_t sPos=originalFile.rfind('/'); |
| 396 |
std::string folder(""), fileName(""); |
| 397 |
if (sPos==std::string::npos) |
| 398 |
fileName = originalFile; |
| 399 |
else { |
| 400 |
folder=originalFile.substr(0,sPos+1); |
| 401 |
fileName=originalFile.substr(sPos+1); |
| 402 |
} |
| 403 |
for (int test=0; test<2; ++test) { |
| 404 |
std::string rsrcName=test==0 ? folder+"._"+fileName : folder+"__MACOSX/._"+fileName; |
| 405 |
struct stat status; |
| 406 |
if (stat(rsrcName.c_str(), &status) || !S_ISREG(status.st_mode) || status.st_size==0) |
| 407 |
continue; |
| 408 |
res.reset(new FolderStream); |
| 409 |
res->addFile(originalFile, "DataFork"); |
| 410 |
res->addFile(rsrcName, "RsrcInfo"); |
| 411 |
return res; |
| 412 |
} |
| 413 |
|
| 414 |
return res; |
| 415 |
} |
| 416 |
catch (...) |
| 417 |
{ |
| 418 |
return std::shared_ptr<librevenge::RVNGInputStream>(); |
| 419 |
} |
| 420 |
#endif |
| 421 |
|
| 422 |
//////////////////////////////////////////////////////////// |
| 423 |
// main functions |
| 424 |
//////////////////////////////////////////////////////////// |
| 425 |
|
| 426 |
std::shared_ptr<librevenge::RVNGInputStream> isSupported |
| 427 |
(char const *filename, MWAWDocument::Confidence &confidence, MWAWDocument::Kind &kind) |
| 428 |
{ |
| 429 |
std::shared_ptr<librevenge::RVNGInputStream> input(new librevenge::RVNGFileStream(filename)); |
| 430 |
MWAWDocument::Type type; |
| 431 |
#ifndef __EMSCRIPTEN__ |
| 432 |
try { |
| 433 |
auto mimeInput=createFolderInput(filename, *input); |
| 434 |
if (mimeInput) { |
| 435 |
confidence=MWAWDocument::isFileFormatSupported(mimeInput.get(), type, kind); |
| 436 |
if (confidence == MWAWDocument::MWAW_C_EXCELLENT) |
| 437 |
return mimeInput; |
| 438 |
} |
| 439 |
} |
| 440 |
catch (...) { |
| 441 |
} |
| 442 |
#endif |
| 443 |
try { |
| 444 |
confidence = MWAWDocument::isFileFormatSupported(input.get(), type, kind); |
| 445 |
if (confidence == MWAWDocument::MWAW_C_EXCELLENT) |
| 446 |
return input; |
| 447 |
} |
| 448 |
catch (...) { |
| 449 |
} |
| 450 |
return std::shared_ptr<librevenge::RVNGInputStream>(); |
| 451 |
} |
| 452 |
|
| 453 |
bool checkErrorAndPrintMessage(MWAWDocument::Result result) |
| 454 |
{ |
| 455 |
if (result == MWAWDocument::MWAW_R_FILE_ACCESS_ERROR) |
| 456 |
fprintf(stderr, "ERROR: File Exception!\n"); |
| 457 |
else if (result == MWAWDocument::MWAW_R_PARSE_ERROR) |
| 458 |
fprintf(stderr, "ERROR: Parse Exception!\n"); |
| 459 |
else if (result == MWAWDocument::MWAW_R_OLE_ERROR) |
| 460 |
fprintf(stderr, "ERROR: File is an OLE document!\n"); |
| 461 |
else if (result != MWAWDocument::MWAW_R_OK) |
| 462 |
fprintf(stderr, "ERROR: Unknown Error!\n"); |
| 463 |
else |
| 464 |
return false; |
| 465 |
return true; |
| 466 |
} |
| 467 |
|
| 468 |
} |
| 469 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |