| 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 <algorithm> |
| 35 |
#include <iomanip> |
| 36 |
#include <iostream> |
| 37 |
#include <set> |
| 38 |
#include <sstream> |
| 39 |
#include <utility> |
| 40 |
|
| 41 |
#include <librevenge/librevenge.h> |
| 42 |
|
| 43 |
#include "MWAWDebug.hxx" |
| 44 |
#include "MWAWEntry.hxx" |
| 45 |
#include "MWAWOLEParser.hxx" |
| 46 |
|
| 47 |
#include "PowerPoint3OLE.hxx" |
| 48 |
|
| 49 |
/** Internal: the structures of a PowerPoint3OLE */ |
| 50 |
namespace PowerPoint3OLEInternal |
| 51 |
{ |
| 52 |
//////////////////////////////////////// |
| 53 |
//! Internal: the state of a PowerPoint3OLE |
| 54 |
struct State { |
| 55 |
//! constructor |
| 56 |
explicit State(MWAWInputStreamPtr input, int vers) |
| 57 |
: m_input(input) |
| 58 |
, m_version(vers) |
| 59 |
, m_oleParser() |
| 60 |
, m_unparsedNameSet() |
| 61 |
{ |
| 62 |
} |
| 63 |
/** the input */ |
| 64 |
MWAWInputStreamPtr m_input; |
| 65 |
/** the version */ |
| 66 |
int m_version; |
| 67 |
/** the ole parser */ |
| 68 |
std::shared_ptr<MWAWOLEParser> m_oleParser; |
| 69 |
/** the list of unparsed zone */ |
| 70 |
std::set<std::string> m_unparsedNameSet; |
| 71 |
}; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
//////////////////////////////////////////////////////////// |
| 76 |
// constructor/destructor, ... |
| 77 |
//////////////////////////////////////////////////////////// |
| 78 |
PowerPoint3OLE::PowerPoint3OLE(MWAWInputStreamPtr const &input, int vers, MWAWFontConverterPtr const &fontConverter, int fId) |
| 79 |
: m_state(new PowerPoint3OLEInternal::State(input, vers)) |
| 80 |
{ |
| 81 |
char const *mainOle =(version()<=4 ? "PP40" : "PowerPoint Document"); |
| 82 |
if (input && input->isStructured() && input->getSubStreamByName(mainOle)) |
| 83 |
m_state->m_oleParser.reset(new MWAWOLEParser(mainOle, fontConverter, fId)); |
| 84 |
} |
| 85 |
|
| 86 |
PowerPoint3OLE::~PowerPoint3OLE() |
| 87 |
{ |
| 88 |
} |
| 89 |
|
| 90 |
int PowerPoint3OLE::version() const |
| 91 |
{ |
| 92 |
return m_state->m_version; |
| 93 |
} |
| 94 |
|
| 95 |
int PowerPoint3OLE::getFontEncoding() const |
| 96 |
{ |
| 97 |
if (m_state->m_oleParser) |
| 98 |
return m_state->m_oleParser->getFontEncoding(); |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
void PowerPoint3OLE::updateMetaData(librevenge::RVNGPropertyList &metaData) const |
| 103 |
{ |
| 104 |
if (m_state->m_oleParser) |
| 105 |
m_state->m_oleParser->updateMetaData(metaData); |
| 106 |
} |
| 107 |
|
| 108 |
//////////////////////////////////////////////////////////// |
| 109 |
// the parser |
| 110 |
//////////////////////////////////////////////////////////// |
| 111 |
bool PowerPoint3OLE::parse() |
| 112 |
{ |
| 113 |
int const vers=version(); |
| 114 |
MWAWInputStreamPtr input=m_state->m_input; |
| 115 |
if (!input || !m_state->m_oleParser || !m_state->m_oleParser->parse(input)) return false; |
| 116 |
std::vector<std::string> unparsed = m_state->m_oleParser->getNotParse(); |
| 117 |
|
| 118 |
for (auto const &name : unparsed) { |
| 119 |
// separated the directory and the name |
| 120 |
// MatOST/MatadorObject1/Ole10Native |
| 121 |
// -> dir="MatOST/MatadorObject1", base="Ole10Native" |
| 122 |
std::string::size_type pos = name.find_last_of('/'); |
| 123 |
std::string base; |
| 124 |
if (pos == std::string::npos) base = name; |
| 125 |
else if (pos == 0) base = name.substr(1); |
| 126 |
else |
| 127 |
base = name.substr(pos+1); |
| 128 |
|
| 129 |
MWAWInputStreamPtr ole = input->getSubStreamByName(name.c_str()); |
| 130 |
if (!ole.get()) { |
| 131 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::createZones: error: can not find OLE part: \"%s\"\n", name.c_str())); |
| 132 |
continue; |
| 133 |
} |
| 134 |
ole->setReadInverted(true); |
| 135 |
bool done=false; |
| 136 |
switch (base[0]) { |
| 137 |
case 'C': |
| 138 |
if (base=="Current User") |
| 139 |
done=parseCurrentUser(ole, name); |
| 140 |
else if (base=="Current ID") |
| 141 |
done=parseCurrentId(ole, name); |
| 142 |
break; |
| 143 |
case 'H': |
| 144 |
if (vers>=7 && name=="Header") |
| 145 |
done=parseHeader(ole, name); |
| 146 |
break; |
| 147 |
case 'P': |
| 148 |
if (vers>=7 && name=="PersistentStorage Directory") |
| 149 |
done=parsePersistentStorage(ole, name); |
| 150 |
break; |
| 151 |
default: |
| 152 |
break; |
| 153 |
} |
| 154 |
if (done) continue; |
| 155 |
m_state->m_unparsedNameSet.insert(name); |
| 156 |
} |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
void PowerPoint3OLE::checkForUnparsedStream() |
| 161 |
{ |
| 162 |
int const vers=version(); |
| 163 |
for (auto const &name : m_state->m_unparsedNameSet) { |
| 164 |
if (vers>=7 && name=="Text_Content") continue; |
| 165 |
auto pos = name.find_last_of('/'); |
| 166 |
std::string base; |
| 167 |
if (pos == std::string::npos) base = name; |
| 168 |
else if (pos == 0) base = name.substr(1); |
| 169 |
else |
| 170 |
base = name.substr(pos+1); |
| 171 |
MWAWInputStreamPtr ole = m_state->m_input->getSubStreamByName(name.c_str()); |
| 172 |
if (!ole.get()) { |
| 173 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::checkForUnparsedStream: error: can not find OLE part: \"%s\"\n", name.c_str())); |
| 174 |
continue; |
| 175 |
} |
| 176 |
libmwaw::DebugFile asciiFile(ole); |
| 177 |
asciiFile.open(name); |
| 178 |
libmwaw::DebugStream f; |
| 179 |
f << "Entries(" << base << "):"; |
| 180 |
asciiFile.addPos(0); |
| 181 |
asciiFile.addNote(f.str().c_str()); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
//////////////////////////////////////////////////////////// |
| 186 |
// try to read the different stream |
| 187 |
//////////////////////////////////////////////////////////// |
| 188 |
bool PowerPoint3OLE::parseCurrentId(MWAWInputStreamPtr input, std::string const &name) |
| 189 |
{ |
| 190 |
if (!input||input->size()!=4) { |
| 191 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parseCurrentId: can not find the input\n")); |
| 192 |
return false; |
| 193 |
} |
| 194 |
libmwaw::DebugFile ascFile(input); |
| 195 |
ascFile.open(name); |
| 196 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 197 |
libmwaw::DebugStream f; |
| 198 |
f << "Entries(CurrentId):"; |
| 199 |
auto val=int(input->readLong(4)); |
| 200 |
if (val) f << "id=" << val << ","; |
| 201 |
ascFile.addPos(0); |
| 202 |
ascFile.addNote(f.str().c_str()); |
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
bool PowerPoint3OLE::parseCurrentUser(MWAWInputStreamPtr input, std::string const &name) |
| 207 |
{ |
| 208 |
int const vers=version(); |
| 209 |
int const szSize=vers<=4 ? 1 : 4; |
| 210 |
if (!input||input->size() < szSize) { |
| 211 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parseCurrentUser: can not find the input\n")); |
| 212 |
return false; |
| 213 |
} |
| 214 |
libmwaw::DebugFile ascFile(input); |
| 215 |
ascFile.open(name); |
| 216 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 217 |
long endPos=input->size(); |
| 218 |
libmwaw::DebugStream f; |
| 219 |
f << "Entries(CurrentUser):"; |
| 220 |
auto sSz=int(input->readULong(szSize)); |
| 221 |
if (sSz<0 || sSz>input->size()-szSize) { |
| 222 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parseCurrentUser: the stream size seems bad\n")); |
| 223 |
f << "###sSz,"; |
| 224 |
ascFile.addPos(0); |
| 225 |
ascFile.addNote(f.str().c_str()); |
| 226 |
return true; |
| 227 |
} |
| 228 |
std::string user; |
| 229 |
for (int c=0; c<sSz; ++c) { |
| 230 |
auto ch=char(input->readULong(1)); |
| 231 |
if (ch) |
| 232 |
user+=ch; |
| 233 |
else if (c+1!=sSz) |
| 234 |
f << "###"; |
| 235 |
} |
| 236 |
f << user; |
| 237 |
ascFile.addPos(0); |
| 238 |
ascFile.addNote(f.str().c_str()); |
| 239 |
if (input->tell()!=endPos) { |
| 240 |
ascFile.addPos(input->tell()); |
| 241 |
ascFile.addNote("CurrentUser:##extra"); |
| 242 |
} |
| 243 |
return true; |
| 244 |
} |
| 245 |
|
| 246 |
bool PowerPoint3OLE::parseHeader(MWAWInputStreamPtr input, std::string const &name) |
| 247 |
{ |
| 248 |
if (!input || input->size()<19) { |
| 249 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parseHeader: the input seems bad\n")); |
| 250 |
return false; |
| 251 |
} |
| 252 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 253 |
long endPos=input->size(); |
| 254 |
libmwaw::DebugFile ascFile(input); |
| 255 |
ascFile.open(name); |
| 256 |
libmwaw::DebugStream f; |
| 257 |
f << "Entries(Headr):"; |
| 258 |
std::string text; // Microsoft (R) PowerPoint (R) Windows |
| 259 |
for (long i=0; i<endPos; ++i) { |
| 260 |
auto c=char(input->readULong(1)); |
| 261 |
if (!c) break; |
| 262 |
text+=c; |
| 263 |
} |
| 264 |
f << text << ","; |
| 265 |
if (input->tell()+18>endPos) { |
| 266 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parseHeader: the input seems short\n")); |
| 267 |
f << "###"; |
| 268 |
ascFile.addDelimiter(input->tell(),'|'); |
| 269 |
ascFile.addPos(0); |
| 270 |
ascFile.addNote(f.str().c_str()); |
| 271 |
return true; |
| 272 |
} |
| 273 |
for (int i=0; i<9; ++i) { |
| 274 |
auto val=int(input->readULong(2)); |
| 275 |
int const expected[]= {7,0,0x3f0,0,0xc05f,0xe391,1,0,0}; |
| 276 |
if (val!=expected[i]) f << "f" << i << "=" << std::hex << val << std::dec << ","; |
| 277 |
} |
| 278 |
ascFile.addPos(0); |
| 279 |
ascFile.addNote(f.str().c_str()); |
| 280 |
if (input->tell()!=endPos) { |
| 281 |
ascFile.addPos(input->tell()); |
| 282 |
ascFile.addNote("Headr:###"); |
| 283 |
} |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
bool PowerPoint3OLE::parsePersistentStorage(MWAWInputStreamPtr input, std::string const &name) |
| 288 |
{ |
| 289 |
if (!input || input->size()<62) { |
| 290 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parsePersistentStorage: the input seems bad\n")); |
| 291 |
return false; |
| 292 |
} |
| 293 |
input->seek(0, librevenge::RVNG_SEEK_SET); |
| 294 |
long endPos=input->size(); |
| 295 |
libmwaw::DebugFile ascFile(input); |
| 296 |
ascFile.open(name); |
| 297 |
libmwaw::DebugStream f; |
| 298 |
f << "Entries(PersistentStorage):"; |
| 299 |
int val; |
| 300 |
for (int i=0; i<2; ++i) { // f1=3c6|3e0 |
| 301 |
val=int(input->readULong(2)); |
| 302 |
int const expected[]= {7,0x3e0}; |
| 303 |
if (val!=expected[i]) |
| 304 |
f << "f" << i << "=" << val << ","; |
| 305 |
} |
| 306 |
for (int i=0; i<13; ++i) { // f9=f11=f13=1 |
| 307 |
val=int(input->readULong(2)); |
| 308 |
if (val) f << "f" << i+2 << "=" << val << ","; |
| 309 |
} |
| 310 |
auto sSz=int(input->readULong(4)); |
| 311 |
if (sSz<0 || endPos-30-8<sSz || 30+sSz+8>endPos) { |
| 312 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parsePersistentStorage: the string size seems bad\n")); |
| 313 |
f << "###sSz=" << sSz << ","; |
| 314 |
ascFile.addPos(0); |
| 315 |
ascFile.addNote(f.str().c_str()); |
| 316 |
return true; |
| 317 |
} |
| 318 |
std::string text; // PowerPoint Document |
| 319 |
for (int i=0; i<sSz; ++i) { |
| 320 |
auto c=char(input->readULong(1)); |
| 321 |
if (c) |
| 322 |
text+=c; |
| 323 |
else if (i+1!=sSz) { |
| 324 |
MWAW_DEBUG_MSG(("PowerPoint3OLE::parsePersistentStorage: the name seems bad\n")); |
| 325 |
f << "##name,"; |
| 326 |
} |
| 327 |
} |
| 328 |
f << text << ","; |
| 329 |
for (int i=0; i<4; ++i) { // g0=1 |
| 330 |
val=int(input->readULong(2)); |
| 331 |
if (val) f << "g" << i << "=" << val << ","; |
| 332 |
} |
| 333 |
ascFile.addPos(0); |
| 334 |
ascFile.addNote(f.str().c_str()); |
| 335 |
if (input->tell()!=endPos) { // seems junk, unsure |
| 336 |
ascFile.addPos(input->tell()); |
| 337 |
ascFile.addNote("_"); |
| 338 |
} |
| 339 |
return true; |
| 340 |
} |
| 341 |
|
| 342 |
//////////////////////////////////////////////////////////// |
| 343 |
// try to send data |
| 344 |
//////////////////////////////////////////////////////////// |
| 345 |
|
| 346 |
|
| 347 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |