| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
|
| 3 |
/* libmwaw |
| 4 |
* Version: MPL 2.0 / LGPLv2+ |
| 5 |
* |
| 6 |
* The contents of this file are subject to the Mozilla Public License Version |
| 7 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 8 |
* the License or as specified alternatively below. You may obtain a copy of |
| 9 |
* the License at http://www.mozilla.org/MPL/ |
| 10 |
* |
| 11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 |
* for the specific language governing rights and limitations under the |
| 14 |
* License. |
| 15 |
* |
| 16 |
* Major Contributor(s): |
| 17 |
* Copyright (C) 2002 William Lachance ([email protected]) |
| 18 |
* Copyright (C) 2002,2004 Marc Maurer ([email protected]) |
| 19 |
* Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 20 |
* Copyright (C) 2006, 2007 Andrew Ziem |
| 21 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 22 |
* |
| 23 |
* |
| 24 |
* All Rights Reserved. |
| 25 |
* |
| 26 |
* For minor contributions see the git repository. |
| 27 |
* |
| 28 |
* Alternatively, the contents of this file may be used under the terms of |
| 29 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 30 |
* in which case the provisions of the LGPLv2+ are applicable |
| 31 |
* instead of those above. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include <limits> |
| 37 |
#include <cmath> |
| 38 |
#include <cstring> |
| 39 |
|
| 40 |
#include <librevenge-stream/librevenge-stream.h> |
| 41 |
#include <librevenge/librevenge.h> |
| 42 |
|
| 43 |
#include "MWAWDebug.hxx" |
| 44 |
|
| 45 |
#include "MWAWInputStream.hxx" |
| 46 |
#include "MWAWStringStream.hxx" |
| 47 |
|
| 48 |
MWAWInputStream::MWAWInputStream(std::shared_ptr<librevenge::RVNGInputStream> inp, bool inverted) |
| 49 |
: m_stream(inp) |
| 50 |
, m_streamSize(0) |
| 51 |
, m_readLimit(-1) |
| 52 |
, m_prevLimits() |
| 53 |
, m_fInfoType("") |
| 54 |
, m_fInfoCreator("") |
| 55 |
, m_resourceFork() |
| 56 |
, m_inverseRead(inverted) |
| 57 |
{ |
| 58 |
updateStreamSize(); |
| 59 |
} |
| 60 |
|
| 61 |
MWAWInputStream::MWAWInputStream(librevenge::RVNGInputStream *inp, bool inverted, bool checkCompression) |
| 62 |
: m_stream() |
| 63 |
, m_streamSize(0) |
| 64 |
, m_readLimit(-1) |
| 65 |
, m_prevLimits() |
| 66 |
, m_fInfoType("") |
| 67 |
, m_fInfoCreator("") |
| 68 |
, m_resourceFork() |
| 69 |
, m_inverseRead(inverted) |
| 70 |
{ |
| 71 |
if (!inp) return; |
| 72 |
|
| 73 |
m_stream = std::shared_ptr<librevenge::RVNGInputStream>(inp, MWAW_shared_ptr_noop_deleter<librevenge::RVNGInputStream>()); |
| 74 |
updateStreamSize(); |
| 75 |
if (!checkCompression) |
| 76 |
return; |
| 77 |
|
| 78 |
// first check if the file is "local" structure one |
| 79 |
if (unsplitInternalMergeStream()) |
| 80 |
updateStreamSize(); |
| 81 |
// then check the zip format |
| 82 |
if (unzipStream()) |
| 83 |
updateStreamSize(); |
| 84 |
|
| 85 |
// then check if the data are in binhex format |
| 86 |
if (unBinHex()) |
| 87 |
updateStreamSize(); |
| 88 |
|
| 89 |
// now check for MacMIME format in m_stream or in m_resourceFork |
| 90 |
if (unMacMIME()) |
| 91 |
updateStreamSize(); |
| 92 |
if (m_stream) |
| 93 |
seek(0, librevenge::RVNG_SEEK_SET); |
| 94 |
if (m_resourceFork) |
| 95 |
m_resourceFork->seek(0, librevenge::RVNG_SEEK_SET); |
| 96 |
} |
| 97 |
|
| 98 |
MWAWInputStream::~MWAWInputStream() |
| 99 |
{ |
| 100 |
} |
| 101 |
|
| 102 |
std::shared_ptr<MWAWInputStream> MWAWInputStream::get(librevenge::RVNGBinaryData const &data, bool inverted) |
| 103 |
{ |
| 104 |
std::shared_ptr<MWAWInputStream> res; |
| 105 |
if (!data.size()) |
| 106 |
return res; |
| 107 |
auto *dataStream = const_cast<librevenge::RVNGInputStream *>(data.getDataStream()); |
| 108 |
if (!dataStream) { |
| 109 |
MWAW_DEBUG_MSG(("MWAWInputStream::get: can not retrieve a librevenge::RVNGInputStream\n")); |
| 110 |
return res; |
| 111 |
} |
| 112 |
res.reset(new MWAWInputStream(dataStream, inverted)); |
| 113 |
if (res && res->size()>=long(data.size())) { |
| 114 |
res->seek(0, librevenge::RVNG_SEEK_SET); |
| 115 |
return res; |
| 116 |
} |
| 117 |
MWAW_DEBUG_MSG(("MWAWInputStream::get: the final stream seems bad\n")); |
| 118 |
res.reset(); |
| 119 |
return res; |
| 120 |
} |
| 121 |
|
| 122 |
void MWAWInputStream::recomputeStreamSize() |
| 123 |
{ |
| 124 |
m_streamSize=0; |
| 125 |
updateStreamSize(); |
| 126 |
} |
| 127 |
|
| 128 |
void MWAWInputStream::updateStreamSize() |
| 129 |
{ |
| 130 |
if (!m_stream) |
| 131 |
m_streamSize=0; |
| 132 |
else { |
| 133 |
long actPos = tell(); |
| 134 |
m_stream->seek(0, librevenge::RVNG_SEEK_END); |
| 135 |
m_streamSize=tell(); |
| 136 |
m_stream->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
const uint8_t *MWAWInputStream::read(size_t numBytes, unsigned long &numBytesRead) |
| 141 |
{ |
| 142 |
if (!hasDataFork()) |
| 143 |
throw libmwaw::FileException(); |
| 144 |
return m_stream->read(numBytes,numBytesRead); |
| 145 |
} |
| 146 |
|
| 147 |
long MWAWInputStream::tell() |
| 148 |
{ |
| 149 |
if (!hasDataFork()) |
| 150 |
return 0; |
| 151 |
return m_stream->tell(); |
| 152 |
} |
| 153 |
|
| 154 |
int MWAWInputStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType) |
| 155 |
{ |
| 156 |
if (!hasDataFork()) { |
| 157 |
if (offset == 0) |
| 158 |
return 0; |
| 159 |
throw libmwaw::FileException(); |
| 160 |
} |
| 161 |
|
| 162 |
if (seekType == librevenge::RVNG_SEEK_CUR) |
| 163 |
offset += tell(); |
| 164 |
else if (seekType==librevenge::RVNG_SEEK_END) |
| 165 |
offset += m_streamSize; |
| 166 |
|
| 167 |
if (offset < 0) |
| 168 |
offset = 0; |
| 169 |
if (m_readLimit > 0 && offset > long(m_readLimit)) |
| 170 |
offset = long(m_readLimit); |
| 171 |
if (offset > size()) |
| 172 |
offset = size(); |
| 173 |
|
| 174 |
return m_stream->seek(offset, librevenge::RVNG_SEEK_SET); |
| 175 |
} |
| 176 |
|
| 177 |
bool MWAWInputStream::isEnd() |
| 178 |
{ |
| 179 |
if (!hasDataFork()) |
| 180 |
return true; |
| 181 |
long pos = m_stream->tell(); |
| 182 |
if (m_readLimit > 0 && pos >= m_readLimit) return true; |
| 183 |
if (pos >= size()) return true; |
| 184 |
|
| 185 |
return m_stream->isEnd(); |
| 186 |
} |
| 187 |
|
| 188 |
unsigned long MWAWInputStream::readULong(librevenge::RVNGInputStream *stream, int num, unsigned long a, bool inverseRead) |
| 189 |
{ |
| 190 |
if (!stream || num <= 0 || stream->isEnd()) return a; |
| 191 |
if (num > 8) { // reading more than 8 bytes does not make any sense, ... |
| 192 |
MWAW_DEBUG_MSG(("MWAWInputStream::readULong: argh called with %d bytes, resets it to 8 bytes\n", num)); |
| 193 |
num = 8; |
| 194 |
} |
| 195 |
else if (num > 4) { // normally, must be (not recursively) called with 1,2,4 bytes, so... |
| 196 |
MWAW_DEBUG_MSG(("MWAWInputStream::readULong: argh called with %d bytes\n", num)); |
| 197 |
} |
| 198 |
if (inverseRead) { |
| 199 |
unsigned long val = readU8(stream); |
| 200 |
return val + (readULong(stream, num-1,0, inverseRead) << 8); |
| 201 |
} |
| 202 |
switch (num) { |
| 203 |
case 4: |
| 204 |
case 2: |
| 205 |
case 1: { |
| 206 |
unsigned long numBytesRead; |
| 207 |
uint8_t const *p = stream->read(static_cast<unsigned long>(num), numBytesRead); |
| 208 |
if (!p || int(numBytesRead) != num) |
| 209 |
return 0; |
| 210 |
switch (num) { |
| 211 |
case 4: |
| 212 |
return static_cast<unsigned long>(p[3])|(static_cast<unsigned long>(p[2])<<8)| |
| 213 |
(static_cast<unsigned long>(p[1])<<16)|(static_cast<unsigned long>(p[0])<<24)|((a<<16)<<16); |
| 214 |
case 2: |
| 215 |
return (static_cast<unsigned long>(p[1]))|(static_cast<unsigned long>(p[0])<<8)|(a<<16); |
| 216 |
case 1: |
| 217 |
return (static_cast<unsigned long>(p[0]))|(a<<8); |
| 218 |
default: |
| 219 |
break; |
| 220 |
} |
| 221 |
break; |
| 222 |
} |
| 223 |
default: |
| 224 |
return readULong(stream, num-1,(a<<8) + static_cast<unsigned long>(readU8(stream)), inverseRead); |
| 225 |
} |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
long MWAWInputStream::readLong(int num) |
| 230 |
{ |
| 231 |
auto v = long(readULong(num)); |
| 232 |
switch (num) { |
| 233 |
case 4: |
| 234 |
return static_cast<int32_t>(v); |
| 235 |
case 2: |
| 236 |
return static_cast<int16_t>(v); |
| 237 |
case 1: |
| 238 |
return static_cast<int8_t>(v); |
| 239 |
default: |
| 240 |
return v; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
uint8_t MWAWInputStream::readU8(librevenge::RVNGInputStream *stream) |
| 245 |
{ |
| 246 |
if (!stream) |
| 247 |
return 0; |
| 248 |
unsigned long numBytesRead; |
| 249 |
uint8_t const *p = stream->read(sizeof(uint8_t), numBytesRead); |
| 250 |
|
| 251 |
if (!p || numBytesRead != sizeof(uint8_t)) |
| 252 |
return 0; |
| 253 |
|
| 254 |
return *p; |
| 255 |
} |
| 256 |
|
| 257 |
bool MWAWInputStream::readDouble8(double &res, bool &isNotANumber) |
| 258 |
{ |
| 259 |
if (!m_stream) return false; |
| 260 |
long pos=tell(); |
| 261 |
if (m_readLimit > 0 && pos+8 > m_readLimit) return false; |
| 262 |
if (pos+8 > m_streamSize) return false; |
| 263 |
|
| 264 |
isNotANumber=false; |
| 265 |
res=0; |
| 266 |
auto mantExp=int(readULong(1)); |
| 267 |
auto val=static_cast<int>(readULong(1)); |
| 268 |
int exp=(mantExp<<4)+(val>>4); |
| 269 |
double mantisse=double(val&0xF)/16.; |
| 270 |
double factor=1./16/256.; |
| 271 |
for (int j = 0; j < 6; ++j, factor/=256) |
| 272 |
mantisse+=double(readULong(1))*factor; |
| 273 |
int sign = 1; |
| 274 |
if (exp & 0x800) { |
| 275 |
exp &= 0x7ff; |
| 276 |
sign = -1; |
| 277 |
} |
| 278 |
if (exp == 0) { |
| 279 |
if (mantisse <= 1.e-5 || mantisse >= 1-1.e-5) |
| 280 |
return true; |
| 281 |
// a Nan representation ? |
| 282 |
return false; |
| 283 |
} |
| 284 |
if (exp == 0x7FF) { |
| 285 |
if (mantisse >= 1.-1e-5) { |
| 286 |
isNotANumber=true; |
| 287 |
res=std::numeric_limits<double>::quiet_NaN(); |
| 288 |
return true; // ok 0x7FF and 0xFFF are nan |
| 289 |
} |
| 290 |
return false; |
| 291 |
} |
| 292 |
exp -= 0x3ff; |
| 293 |
res = std::ldexp(1.+mantisse, exp); |
| 294 |
if (sign == -1) |
| 295 |
res *= -1.; |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
bool MWAWInputStream::readDouble10(double &res, bool &isNotANumber) |
| 300 |
{ |
| 301 |
if (!m_stream) return false; |
| 302 |
long pos=tell(); |
| 303 |
if (m_readLimit > 0 && pos+10 > m_readLimit) return false; |
| 304 |
if (pos+10 > m_streamSize) return false; |
| 305 |
|
| 306 |
auto exp = static_cast<int>(readULong(2)); |
| 307 |
int sign = 1; |
| 308 |
if (exp & 0x8000) { |
| 309 |
exp &= 0x7fff; |
| 310 |
sign = -1; |
| 311 |
} |
| 312 |
exp -= 0x3fff; |
| 313 |
|
| 314 |
isNotANumber=false; |
| 315 |
auto mantisse = static_cast<unsigned long>(readULong(4)); |
| 316 |
if ((mantisse & 0x80000001) == 0) { |
| 317 |
// unormalized number are not frequent, but can appear at least for date, ... |
| 318 |
if (readULong(4) != 0) |
| 319 |
seek(-4, librevenge::RVNG_SEEK_CUR); |
| 320 |
else { |
| 321 |
if (exp == -0x3fff && mantisse == 0) { |
| 322 |
res=0; |
| 323 |
return true; // ok zero |
| 324 |
} |
| 325 |
if (exp == 0x4000 && (mantisse & 0xFFFFFFL)==0) { // ok Nan |
| 326 |
isNotANumber = true; |
| 327 |
res=std::numeric_limits<double>::quiet_NaN(); |
| 328 |
return true; |
| 329 |
} |
| 330 |
return false; |
| 331 |
} |
| 332 |
} |
| 333 |
// or std::ldexp((total value)/double(0x80000000), exp); |
| 334 |
res=std::ldexp(double(readULong(4)),exp-63)+std::ldexp(double(mantisse),exp-31); |
| 335 |
if (sign == -1) |
| 336 |
res *= -1.; |
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
bool MWAWInputStream::readDoubleReverted8(double &res, bool &isNotANumber) |
| 341 |
{ |
| 342 |
if (!m_stream) return false; |
| 343 |
long pos=tell(); |
| 344 |
if (m_readLimit > 0 && pos+8 > m_readLimit) return false; |
| 345 |
if (pos+8 > m_streamSize) return false; |
| 346 |
|
| 347 |
isNotANumber=false; |
| 348 |
res=0; |
| 349 |
int bytes[6]; |
| 350 |
for (auto &b : bytes) b=static_cast<int>(readULong(1)); |
| 351 |
|
| 352 |
auto val=static_cast<int>(readULong(1)); |
| 353 |
auto mantExp=int(readULong(1)); |
| 354 |
int exp=(mantExp<<4)+(val>>4); |
| 355 |
double mantisse=double(val&0xF)/16.; |
| 356 |
double factor=1./16./256.; |
| 357 |
for (int j = 0; j < 6; ++j, factor/=256) |
| 358 |
mantisse+=double(bytes[5-j])*factor; |
| 359 |
int sign = 1; |
| 360 |
if (exp & 0x800) { |
| 361 |
exp &= 0x7ff; |
| 362 |
sign = -1; |
| 363 |
} |
| 364 |
if (exp == 0) { |
| 365 |
if (mantisse <= 1.e-5 || mantisse >= 1-1.e-5) |
| 366 |
return true; |
| 367 |
// a Nan representation ? |
| 368 |
return false; |
| 369 |
} |
| 370 |
if (exp == 0x7FF) { |
| 371 |
if (mantisse >= 1.-1e-5) { |
| 372 |
isNotANumber=true; |
| 373 |
res=std::numeric_limits<double>::quiet_NaN(); |
| 374 |
return true; // ok 0x7FF and 0xFFF are nan |
| 375 |
} |
| 376 |
return false; |
| 377 |
} |
| 378 |
exp -= 0x3ff; |
| 379 |
res = std::ldexp(1.+mantisse, exp); |
| 380 |
if (sign == -1) |
| 381 |
res *= -1.; |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
//////////////////////////////////////////////////////////// |
| 386 |
// |
| 387 |
// BinHex 4.0 |
| 388 |
// |
| 389 |
//////////////////////////////////////////////////////////// |
| 390 |
bool MWAWInputStream::unBinHex() |
| 391 |
{ |
| 392 |
if (!hasDataFork() || size() < 45) |
| 393 |
return false; |
| 394 |
// check header |
| 395 |
seek(0, librevenge::RVNG_SEEK_SET); |
| 396 |
unsigned long nRead; |
| 397 |
auto const *str=reinterpret_cast<char const *>(read(45,nRead)); |
| 398 |
if (str==nullptr || nRead!=45 |
| 399 |
|| strncmp(str, "(This file must be converted with BinHex 4.0)",45)) |
| 400 |
return false; |
| 401 |
int numEOL = 0; |
| 402 |
while (!isEnd()) { |
| 403 |
auto c = char(readLong(1)); |
| 404 |
if (c == '\n') { |
| 405 |
numEOL++; |
| 406 |
continue; |
| 407 |
} |
| 408 |
seek(-1, librevenge::RVNG_SEEK_CUR); |
| 409 |
break; |
| 410 |
} |
| 411 |
if (isEnd() || !numEOL || (char(readLong(1)))!= ':') |
| 412 |
return false; |
| 413 |
|
| 414 |
// first phase reconstruct the file |
| 415 |
static char const binChar[65] = "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr"; |
| 416 |
std::map<unsigned char,int> binMap; |
| 417 |
for (int i = 0; i < 64; i++) binMap[static_cast<unsigned char>(binChar[i])]=i; |
| 418 |
bool endData = false; |
| 419 |
int numActByte = 0, actVal = 0; |
| 420 |
librevenge::RVNGBinaryData content; |
| 421 |
bool findRepetitif = false; |
| 422 |
while (1) { |
| 423 |
if (isEnd()) { |
| 424 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: do not find ending ':' character\n")); |
| 425 |
return false; |
| 426 |
} |
| 427 |
auto c = static_cast<unsigned char>(readLong(1)); |
| 428 |
if (c == '\n') continue; |
| 429 |
int readVal = 0; |
| 430 |
if (c == ':') |
| 431 |
endData = true; |
| 432 |
else if (binMap.find(c) == binMap.end()) { |
| 433 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: find unexpected char when decoding file\n")); |
| 434 |
return false; |
| 435 |
} |
| 436 |
else |
| 437 |
readVal = binMap.find(c)->second; |
| 438 |
int wVal = -1; |
| 439 |
if (numActByte==0) |
| 440 |
actVal |= (readVal<<2); |
| 441 |
else if (numActByte==2) { |
| 442 |
wVal = (actVal | readVal); |
| 443 |
actVal = 0; |
| 444 |
} |
| 445 |
else if (numActByte==4) { |
| 446 |
wVal = actVal | ((readVal>>2)&0xF); |
| 447 |
actVal = (readVal&0x3)<<6; |
| 448 |
} |
| 449 |
else if (numActByte==6) { |
| 450 |
wVal = actVal | ((readVal>>4)&0x3); |
| 451 |
actVal = (readVal&0xf)<<4; |
| 452 |
} |
| 453 |
numActByte = (numActByte+6)%8; |
| 454 |
|
| 455 |
int maxToWrite = (endData&&actVal) ? 2 : 1; |
| 456 |
for (int wPos = 0; wPos < maxToWrite; wPos++) { |
| 457 |
int value = wPos ? actVal : wVal; |
| 458 |
if (value == -1) continue; |
| 459 |
if (!findRepetitif && value != 0x90) { |
| 460 |
content.append(static_cast<unsigned char>(value)); |
| 461 |
continue; |
| 462 |
} |
| 463 |
if (value == 0x90 && !findRepetitif) { |
| 464 |
findRepetitif = true; |
| 465 |
continue; |
| 466 |
} |
| 467 |
|
| 468 |
if (value == 1 || value == 2) { |
| 469 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: find bad value after repetitif character\n")); |
| 470 |
return false; |
| 471 |
} |
| 472 |
findRepetitif = false; |
| 473 |
if (value == 0) { |
| 474 |
content.append(0x90); |
| 475 |
continue; |
| 476 |
} |
| 477 |
if (content.size()==0) { |
| 478 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: find repetitif character in the first position\n")); |
| 479 |
return false; |
| 480 |
} |
| 481 |
unsigned char lChar = content.getDataBuffer()[content.size()-1]; |
| 482 |
for (int i = 0; i < value-1; i++) |
| 483 |
content.append(lChar); |
| 484 |
} |
| 485 |
if (endData) break; |
| 486 |
} |
| 487 |
if (findRepetitif) { |
| 488 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: find repetitif character in the last position\n")); |
| 489 |
return false; |
| 490 |
} |
| 491 |
auto contentSize=long(content.size()); |
| 492 |
if (contentSize < 27) { |
| 493 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: the content file is too small\n")); |
| 494 |
return false; |
| 495 |
} |
| 496 |
auto *contentInput=const_cast<librevenge::RVNGInputStream *>(content.getDataStream()); |
| 497 |
auto fileLength = static_cast<int>(readU8(contentInput)); |
| 498 |
if (fileLength < 1 || fileLength > 64 || long(fileLength+21) > contentSize) { |
| 499 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: the file name size seems odd\n")); |
| 500 |
return false; |
| 501 |
} |
| 502 |
contentInput->seek(fileLength+1, librevenge::RVNG_SEEK_CUR); // filename + version |
| 503 |
// creator, type |
| 504 |
std::string type(""), creator(""); |
| 505 |
for (int p = 0; p < 4; p++) { |
| 506 |
auto c = char(readU8(contentInput)); |
| 507 |
if (c) |
| 508 |
type += c; |
| 509 |
} |
| 510 |
for (int p = 0; p < 4; p++) { |
| 511 |
auto c = char(readU8(contentInput)); |
| 512 |
if (c) |
| 513 |
creator += c; |
| 514 |
} |
| 515 |
if (creator.length()==4 && type.length()==4) { |
| 516 |
m_fInfoType = type; |
| 517 |
m_fInfoCreator = creator; |
| 518 |
} |
| 519 |
else if (creator.length() || type.length()) { |
| 520 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: the file name size seems odd\n")); |
| 521 |
} |
| 522 |
contentInput->seek(2, librevenge::RVNG_SEEK_CUR); // skip flags |
| 523 |
auto dataLength = long(readULong(contentInput,4,0,false)); |
| 524 |
auto rsrcLength = long(readULong(contentInput,4,0,false)); |
| 525 |
long pos = contentInput->tell()+2; // skip CRC |
| 526 |
if (dataLength<0 || rsrcLength < 0 || (dataLength==0 && rsrcLength==0) || |
| 527 |
pos+dataLength+rsrcLength+4 > contentSize) { |
| 528 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: the data/rsrc fork size seems odd\n")); |
| 529 |
return false; |
| 530 |
} |
| 531 |
// now read the rsrc and the data fork |
| 532 |
if (rsrcLength && getResourceForkStream()) { |
| 533 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: I already have a resource fork!!!!\n")); |
| 534 |
} |
| 535 |
else if (rsrcLength) { |
| 536 |
contentInput->seek(pos+dataLength+2, librevenge::RVNG_SEEK_SET); |
| 537 |
unsigned long numBytesRead = 0; |
| 538 |
const unsigned char *data = |
| 539 |
contentInput->read(static_cast<unsigned long>(rsrcLength), numBytesRead); |
| 540 |
if (numBytesRead != static_cast<unsigned long>(rsrcLength) || !data) { |
| 541 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: can not read the resource fork\n")); |
| 542 |
} |
| 543 |
else { |
| 544 |
std::shared_ptr<librevenge::RVNGInputStream> rsrc(new MWAWStringStream(data, static_cast<unsigned int>(numBytesRead))); |
| 545 |
m_resourceFork.reset(new MWAWInputStream(rsrc,false)); |
| 546 |
} |
| 547 |
} |
| 548 |
if (!dataLength) |
| 549 |
m_stream.reset(); |
| 550 |
else { |
| 551 |
contentInput->seek(pos, librevenge::RVNG_SEEK_SET); |
| 552 |
unsigned long numBytesRead = 0; |
| 553 |
const unsigned char *data = |
| 554 |
contentInput->read(static_cast<unsigned long>(dataLength), numBytesRead); |
| 555 |
if (numBytesRead != static_cast<unsigned long>(dataLength) || !data) { |
| 556 |
MWAW_DEBUG_MSG(("MWAWInputStream::unBinHex: can not read the data fork\n")); |
| 557 |
return false; |
| 558 |
} |
| 559 |
m_stream.reset(new MWAWStringStream(data, static_cast<unsigned int>(numBytesRead))); |
| 560 |
} |
| 561 |
|
| 562 |
return true; |
| 563 |
} |
| 564 |
|
| 565 |
//////////////////////////////////////////////////////////// |
| 566 |
// |
| 567 |
// Internal : if the main stream is a structured file, which |
| 568 |
// contains a DataFork, a RsrcFork and a InfoFork or RsrcInfo |
| 569 |
// stream, ... |
| 570 |
// |
| 571 |
//////////////////////////////////////////////////////////// |
| 572 |
bool MWAWInputStream::unsplitInternalMergeStream() |
| 573 |
{ |
| 574 |
if (!isStructured() || m_resourceFork || !m_stream->existsSubStream("DataFork")) |
| 575 |
return false; |
| 576 |
try { |
| 577 |
if (m_stream->subStreamCount()==2 && m_stream->existsSubStream("RsrcInfo")) { |
| 578 |
std::shared_ptr<librevenge::RVNGInputStream> newRsrcFork(m_stream->getSubStreamByName("RsrcInfo")); |
| 579 |
if (newRsrcFork) { // rsrcinfo must be a MacMIME file, it can not be empty |
| 580 |
m_stream.reset(m_stream->getSubStreamByName("DataFork")); // empty data fork is rare, but possible |
| 581 |
m_resourceFork.reset(new MWAWInputStream(newRsrcFork, m_inverseRead)); // rsrcinfo will be decoded by unMacMIME |
| 582 |
} |
| 583 |
else { |
| 584 |
MWAW_DEBUG_MSG(("MWAWInputStream::unsplitInternalMergeStream: sorry, can not extract the data fork or the rsrc info fork\n")); |
| 585 |
} |
| 586 |
} |
| 587 |
else if (m_stream->subStreamCount()==3 && m_stream->existsSubStream("RsrcFork") && m_stream->existsSubStream("InfoFork")) { |
| 588 |
std::shared_ptr<librevenge::RVNGInputStream> newRsrcFork(m_stream->getSubStreamByName("RsrcFork")); |
| 589 |
if (newRsrcFork) { |
| 590 |
std::shared_ptr<librevenge::RVNGInputStream> newInfo(m_stream->getSubStreamByName("InfoFork")); |
| 591 |
m_stream.reset(m_stream->getSubStreamByName("DataFork")); |
| 592 |
m_resourceFork.reset(new MWAWInputStream(newRsrcFork, m_inverseRead)); |
| 593 |
// we must decode the information here |
| 594 |
unsigned long numBytesRead = 0; |
| 595 |
const unsigned char *data = newInfo ? newInfo->read(8, numBytesRead) : nullptr; |
| 596 |
if (numBytesRead != 8 || !data) { |
| 597 |
MWAW_DEBUG_MSG(("MWAWInputStream::unsplitInternalMergeStream: sorry, unknown file information size\n")); |
| 598 |
} |
| 599 |
else { |
| 600 |
bool ok = true; |
| 601 |
std::string type(""), creator(""); |
| 602 |
for (int p = 0; p < 4; p++) { |
| 603 |
if (!data[p]) { |
| 604 |
ok = false; |
| 605 |
break; |
| 606 |
} |
| 607 |
type += char(data[p]); |
| 608 |
} |
| 609 |
for (int p = 4; ok && p < 8; p++) { |
| 610 |
if (!data[p]) { |
| 611 |
ok = false; |
| 612 |
break; |
| 613 |
} |
| 614 |
creator += char(data[p]); |
| 615 |
} |
| 616 |
if (ok) { |
| 617 |
m_fInfoType = type; |
| 618 |
m_fInfoCreator = creator; |
| 619 |
} |
| 620 |
else if (type.length()) { |
| 621 |
MWAW_DEBUG_MSG(("MWAWInputStream::unsplitInternalMergeStream: can not read find info\n")); |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
else { |
| 626 |
MWAW_DEBUG_MSG(("MWAWInputStream::unsplitInternalMergeStream: sorry, can not extract the data fork or the rsrc info fork\n")); |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
catch (...) { |
| 631 |
} |
| 632 |
return false; |
| 633 |
} |
| 634 |
|
| 635 |
//////////////////////////////////////////////////////////// |
| 636 |
// |
| 637 |
// Zip |
| 638 |
// |
| 639 |
//////////////////////////////////////////////////////////// |
| 640 |
bool MWAWInputStream::unzipStream() |
| 641 |
{ |
| 642 |
if (!isStructured()) return false; |
| 643 |
seek(0, librevenge::RVNG_SEEK_SET); |
| 644 |
unsigned numStream=m_stream->subStreamCount(); |
| 645 |
std::vector<std::string> names; |
| 646 |
for (unsigned n=0; n < numStream; ++n) { |
| 647 |
char const *nm=m_stream->subStreamName(n); |
| 648 |
if (!nm) continue; |
| 649 |
std::string name(nm); |
| 650 |
if (name.empty() || name[name.length()-1]=='/') continue; |
| 651 |
names.push_back(nm); |
| 652 |
} |
| 653 |
if (names.size() == 1) { |
| 654 |
// ok as the OLE file must have at least MN and MN0 OLE |
| 655 |
m_stream.reset(m_stream->getSubStreamByName(names[0].c_str())); |
| 656 |
return true; |
| 657 |
} |
| 658 |
if (names.size() != 2) |
| 659 |
return false; |
| 660 |
// test if XXX and ._XXX or __MACOSX/._XXX |
| 661 |
if (names[1].length() < names[0].length()) { |
| 662 |
std::string tmp = names[1]; |
| 663 |
names[1] = names[0]; |
| 664 |
names[0] = tmp; |
| 665 |
} |
| 666 |
size_t length = names[0].length(); |
| 667 |
std::string prefix(""); |
| 668 |
if (names[1].length()==length+2) |
| 669 |
prefix = "._"; |
| 670 |
else if (names[1].length()==length+11) |
| 671 |
prefix = "__MACOSX/._"; |
| 672 |
prefix += names[0]; |
| 673 |
if (prefix != names[1]) return false; |
| 674 |
std::shared_ptr<librevenge::RVNGInputStream> rsrcPtr(m_stream->getSubStreamByName(names[1].c_str())); |
| 675 |
m_resourceFork.reset(new MWAWInputStream(rsrcPtr,false)); |
| 676 |
m_stream.reset(m_stream->getSubStreamByName(names[0].c_str())); |
| 677 |
return true; |
| 678 |
} |
| 679 |
|
| 680 |
//////////////////////////////////////////////////////////// |
| 681 |
// |
| 682 |
// MacMIME part |
| 683 |
// |
| 684 |
//////////////////////////////////////////////////////////// |
| 685 |
bool MWAWInputStream::unMacMIME() |
| 686 |
{ |
| 687 |
if (m_resourceFork) { |
| 688 |
std::shared_ptr<librevenge::RVNGInputStream> newDataInput, newRsrcInput; |
| 689 |
bool ok = unMacMIME(m_resourceFork.get(), newDataInput, newRsrcInput); |
| 690 |
if (ok && newDataInput) { |
| 691 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: Argh!!! find data stream in the resource block\n")); |
| 692 |
ok = false; |
| 693 |
} |
| 694 |
if (ok && newRsrcInput) |
| 695 |
m_resourceFork.reset(new MWAWInputStream(newRsrcInput,false)); |
| 696 |
else if (ok) |
| 697 |
m_resourceFork.reset(); |
| 698 |
} |
| 699 |
|
| 700 |
if (m_stream) { |
| 701 |
std::shared_ptr<librevenge::RVNGInputStream> newDataInput, newRsrcInput; |
| 702 |
bool ok = unMacMIME(this, newDataInput, newRsrcInput); |
| 703 |
if (ok && !newDataInput) { |
| 704 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: Argh!!! data block contains only resources\n")); |
| 705 |
} |
| 706 |
if (ok) { |
| 707 |
m_stream = newDataInput; |
| 708 |
if (newRsrcInput) { |
| 709 |
if (m_resourceFork) { |
| 710 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: Oops!!! find a second resource block, ignored\n")); |
| 711 |
} |
| 712 |
else |
| 713 |
m_resourceFork.reset(new MWAWInputStream(newRsrcInput,false)); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
return true; |
| 718 |
} |
| 719 |
|
| 720 |
/* freely inspired from http://tools.ietf.org/html/rfc1740#appendix-A |
| 721 |
*/ |
| 722 |
bool MWAWInputStream::unMacMIME(MWAWInputStream *inp, |
| 723 |
std::shared_ptr<librevenge::RVNGInputStream> &dataInput, |
| 724 |
std::shared_ptr<librevenge::RVNGInputStream> &rsrcInput) const |
| 725 |
{ |
| 726 |
dataInput.reset(); |
| 727 |
rsrcInput.reset(); |
| 728 |
if (!inp || !inp->hasDataFork() || inp->size()<26) return false; |
| 729 |
|
| 730 |
try { |
| 731 |
inp->seek(0, librevenge::RVNG_SEEK_SET); |
| 732 |
auto magicNumber = long(inp->readULong(4)); |
| 733 |
if (magicNumber != 0x00051600 && magicNumber != 0x00051607) |
| 734 |
return false; |
| 735 |
auto version = long(inp->readULong(4)); |
| 736 |
if (version != 0x20000) { |
| 737 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: unknown version: %lx\n", static_cast<long unsigned int>(version))); |
| 738 |
return false; |
| 739 |
} |
| 740 |
inp->seek(16, librevenge::RVNG_SEEK_CUR); // filename |
| 741 |
auto numEntries = long(inp->readULong(2)); |
| 742 |
if (inp->isEnd() || numEntries <= 0) { |
| 743 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: can not read number of entries\n")); |
| 744 |
return false; |
| 745 |
} |
| 746 |
#ifdef DEBUG |
| 747 |
static const char *what[] = { |
| 748 |
"", "Data", "Rsrc", "FileName", "Comment", "IconBW", "IconColor", "", |
| 749 |
"FileDates", "FinderInfo", "MacInfo", "ProDosInfo", |
| 750 |
"MSDosInfo", "AFPName", "AFPInfo", "AFPDirId" |
| 751 |
}; |
| 752 |
#endif |
| 753 |
for (long i = 0; i < numEntries; i++) { |
| 754 |
long pos = inp->tell(); |
| 755 |
auto wh = static_cast<int>(inp->readULong(4)); |
| 756 |
if (wh <= 0 || wh >= 16 || inp->isEnd()) { |
| 757 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: find unknown id: %d\n", wh)); |
| 758 |
return false; |
| 759 |
} |
| 760 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: find %s entry\n", what[wh])); |
| 761 |
if (wh > 2 && wh != 9) { |
| 762 |
inp->seek(8, librevenge::RVNG_SEEK_CUR); |
| 763 |
continue; |
| 764 |
} |
| 765 |
auto entryPos = long(inp->readULong(4)); |
| 766 |
unsigned long entrySize = inp->readULong(4); |
| 767 |
if (entrySize == 0) { |
| 768 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: %s entry is empty\n", what[wh])); |
| 769 |
continue; |
| 770 |
} |
| 771 |
if (entryPos <= pos) { |
| 772 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: find bad entry pos\n")); |
| 773 |
return false; |
| 774 |
} |
| 775 |
/* try to read the data */ |
| 776 |
inp->seek(entryPos, librevenge::RVNG_SEEK_SET); |
| 777 |
if (inp->tell() != entryPos) { |
| 778 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: can not seek entry pos %lx\n", static_cast<long unsigned int>(entryPos))); |
| 779 |
return false; |
| 780 |
} |
| 781 |
unsigned long numBytesRead = 0; |
| 782 |
const unsigned char *data = inp->read(entrySize, numBytesRead); |
| 783 |
if (numBytesRead != entrySize || !data) { |
| 784 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: can not read %lX byte\n", static_cast<long unsigned int>(entryPos))); |
| 785 |
return false; |
| 786 |
} |
| 787 |
if (wh==1) |
| 788 |
dataInput.reset(new MWAWStringStream(data, static_cast<unsigned int>(numBytesRead))); |
| 789 |
else if (wh==2) |
| 790 |
rsrcInput.reset(new MWAWStringStream(data, static_cast<unsigned int>(numBytesRead))); |
| 791 |
else { // the finder info |
| 792 |
if (entrySize < 8) { |
| 793 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: finder info size is odd\n")); |
| 794 |
} |
| 795 |
else { |
| 796 |
bool ok = true; |
| 797 |
std::string type(""), creator(""); |
| 798 |
for (int p = 0; p < 4; p++) { |
| 799 |
if (!data[p]) { |
| 800 |
ok = false; |
| 801 |
break; |
| 802 |
} |
| 803 |
type += char(data[p]); |
| 804 |
} |
| 805 |
for (int p = 4; ok && p < 8; p++) { |
| 806 |
if (!data[p]) { |
| 807 |
ok = false; |
| 808 |
break; |
| 809 |
} |
| 810 |
creator += char(data[p]); |
| 811 |
} |
| 812 |
if (ok) { |
| 813 |
m_fInfoType = type; |
| 814 |
m_fInfoCreator = creator; |
| 815 |
} |
| 816 |
else if (type.length()) { |
| 817 |
MWAW_DEBUG_MSG(("MWAWInputStream::unMacMIME: can not read find info\n")); |
| 818 |
} |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
inp->seek(pos+12, librevenge::RVNG_SEEK_SET); |
| 823 |
} |
| 824 |
} |
| 825 |
catch (...) { |
| 826 |
return false; |
| 827 |
} |
| 828 |
return true; |
| 829 |
} |
| 830 |
|
| 831 |
//////////////////////////////////////////////////////////// |
| 832 |
// |
| 833 |
// OLE part |
| 834 |
// |
| 835 |
//////////////////////////////////////////////////////////// |
| 836 |
|
| 837 |
bool MWAWInputStream::isStructured() |
| 838 |
{ |
| 839 |
if (!m_stream) return false; |
| 840 |
long pos=m_stream->tell(); |
| 841 |
bool ok=m_stream->isStructured(); |
| 842 |
m_stream->seek(pos, librevenge::RVNG_SEEK_SET); |
| 843 |
return ok; |
| 844 |
} |
| 845 |
|
| 846 |
unsigned MWAWInputStream::subStreamCount() |
| 847 |
{ |
| 848 |
if (!m_stream || !m_stream->isStructured()) { |
| 849 |
MWAW_DEBUG_MSG(("MWAWInputStream::subStreamCount: called on unstructured file\n")); |
| 850 |
return 0; |
| 851 |
} |
| 852 |
return m_stream->subStreamCount(); |
| 853 |
} |
| 854 |
|
| 855 |
std::string MWAWInputStream::subStreamName(unsigned id) |
| 856 |
{ |
| 857 |
if (!m_stream || !m_stream->isStructured()) { |
| 858 |
MWAW_DEBUG_MSG(("MWAWInputStream::subStreamName: called on unstructured file\n")); |
| 859 |
return std::string(""); |
| 860 |
} |
| 861 |
char const *nm=m_stream->subStreamName(id); |
| 862 |
if (!nm) { |
| 863 |
MWAW_DEBUG_MSG(("MWAWInputStream::subStreamName: can not find stream %d\n", int(id))); |
| 864 |
return std::string(""); |
| 865 |
} |
| 866 |
return std::string(nm); |
| 867 |
} |
| 868 |
|
| 869 |
std::shared_ptr<MWAWInputStream> MWAWInputStream::getSubStreamByName(std::string const &name) |
| 870 |
{ |
| 871 |
std::shared_ptr<MWAWInputStream> empty; |
| 872 |
if (!m_stream || !m_stream->isStructured() || name.empty()) { |
| 873 |
MWAW_DEBUG_MSG(("MWAWInputStream::getSubStreamByName: called on unstructured file\n")); |
| 874 |
return empty; |
| 875 |
} |
| 876 |
|
| 877 |
long actPos = tell(); |
| 878 |
seek(0, librevenge::RVNG_SEEK_SET); |
| 879 |
std::shared_ptr<librevenge::RVNGInputStream> res(m_stream->getSubStreamByName(name.c_str())); |
| 880 |
seek(actPos, librevenge::RVNG_SEEK_SET); |
| 881 |
|
| 882 |
if (!res) |
| 883 |
return empty; |
| 884 |
std::shared_ptr<MWAWInputStream> inp(new MWAWInputStream(res,m_inverseRead)); |
| 885 |
inp->seek(0, librevenge::RVNG_SEEK_SET); |
| 886 |
return inp; |
| 887 |
} |
| 888 |
|
| 889 |
std::shared_ptr<MWAWInputStream> MWAWInputStream::getSubStreamById(unsigned id) |
| 890 |
{ |
| 891 |
std::shared_ptr<MWAWInputStream> empty; |
| 892 |
if (!m_stream || !m_stream->isStructured()) { |
| 893 |
MWAW_DEBUG_MSG(("MWAWInputStream::getSubStreamById: called on unstructured file\n")); |
| 894 |
return empty; |
| 895 |
} |
| 896 |
|
| 897 |
long actPos = tell(); |
| 898 |
seek(0, librevenge::RVNG_SEEK_SET); |
| 899 |
std::shared_ptr<librevenge::RVNGInputStream> res(m_stream->getSubStreamById(id)); |
| 900 |
seek(actPos, librevenge::RVNG_SEEK_SET); |
| 901 |
|
| 902 |
if (!res) |
| 903 |
return empty; |
| 904 |
std::shared_ptr<MWAWInputStream> inp(new MWAWInputStream(res,m_inverseRead)); |
| 905 |
inp->seek(0, librevenge::RVNG_SEEK_SET); |
| 906 |
return inp; |
| 907 |
} |
| 908 |
|
| 909 |
//////////////////////////////////////////////////////////// |
| 910 |
// |
| 911 |
// a function to read a data block |
| 912 |
// |
| 913 |
//////////////////////////////////////////////////////////// |
| 914 |
|
| 915 |
bool MWAWInputStream::readDataBlock(long sz, librevenge::RVNGBinaryData &data) |
| 916 |
{ |
| 917 |
if (!hasDataFork()) return false; |
| 918 |
|
| 919 |
data.clear(); |
| 920 |
if (sz < 0) return false; |
| 921 |
if (sz == 0) return true; |
| 922 |
long endPos=tell()+sz; |
| 923 |
if (endPos > size()) return false; |
| 924 |
if (m_readLimit > 0 && endPos > long(m_readLimit)) return false; |
| 925 |
|
| 926 |
const unsigned char *readData; |
| 927 |
unsigned long sizeRead; |
| 928 |
if ((readData=m_stream->read(static_cast<unsigned long>(sz), sizeRead)) == nullptr || long(sizeRead)!=sz) |
| 929 |
return false; |
| 930 |
data.append(readData, sizeRead); |
| 931 |
return true; |
| 932 |
} |
| 933 |
|
| 934 |
bool MWAWInputStream::readEndDataBlock(librevenge::RVNGBinaryData &data) |
| 935 |
{ |
| 936 |
data.clear(); |
| 937 |
if (!hasDataFork()) return false; |
| 938 |
|
| 939 |
long endPos=m_readLimit>0 ? m_readLimit : size(); |
| 940 |
return readDataBlock(endPos-tell(), data); |
| 941 |
} |
| 942 |
|
| 943 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |