| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* libwps |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 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/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2009, 2011 Alonso Laurent ([email protected]) |
| 11 | * Copyright (C) 2006, 2007 Andrew Ziem |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 14 | * Copyright (C) 2003-2005 William Lachance ([email protected]) |
| 15 | * |
| 16 | * For minor contributions see the git repository. |
| 17 | * |
| 18 | * Alternatively, the contents of this file may be used under the terms |
| 19 | * of the GNU Lesser General Public License Version 2.1 or later |
| 20 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 21 | * applicable instead of those above. |
| 22 | * |
| 23 | * For further information visit http://libwps.sourceforge.net |
| 24 | */ |
| 25 | |
| 26 | #include <cstdlib> |
| 27 | #include <cstring> |
| 28 | #include <map> |
| 29 | #include <sstream> |
| 30 | #include <string> |
| 31 | |
| 32 | #include <librevenge/librevenge.h> |
| 33 | |
| 34 | #include "WPSOLE1Parser.h" |
| 35 | |
| 36 | #include "WPSDebug.h" |
| 37 | #include "WPSOLEObject.h" |
| 38 | #include "WPSStream.h" |
| 39 | #include "WPSStringStream.h" |
| 40 | |
| 41 | using namespace libwps; |
| 42 | |
| 43 | ////////////////////////////////////////////////// |
| 44 | // internal structure |
| 45 | ////////////////////////////////////////////////// |
| 46 | namespace WPSOLE1ParserInternal |
| 47 | { |
| 48 | //! an OLE Zone |
| 49 | struct OLEZone |
| 50 | { |
| 51 | //! constructor |
| 52 | explicit OLEZone(int levl) |
| 53 | : m_level(levl) |
| 54 | , m_defPosition(0) |
| 55 | , m_varIdToValueMap() |
| 56 | , m_idsList() |
| 57 | , m_beginList() |
| 58 | , m_lengthList() |
| 59 | , m_childList() |
| 60 | , m_parsed(false) |
| 61 | { |
| 62 | } |
| 63 | //! the level |
| 64 | int m_level; |
| 65 | //! the position where this zone is defined |
| 66 | long m_defPosition; |
| 67 | //! a list of variable |
| 68 | std::map<int,unsigned long> m_varIdToValueMap; |
| 69 | //! the list of pair id:type |
| 70 | std::vector<int> m_idsList; |
| 71 | //! the list of pointers |
| 72 | std::vector<long> m_beginList; |
| 73 | //! the list of length |
| 74 | std::vector<long> m_lengthList; |
| 75 | //! the list of child |
| 76 | std::vector<OLEZone> m_childList; |
| 77 | //! the list of names |
| 78 | std::string m_names[2]; |
| 79 | //! a flag to know if the zone is parsed |
| 80 | mutable bool m_parsed; |
| 81 | }; |
| 82 | |
| 83 | /** Internal: internal method to keep ole1 state */ |
| 84 | struct State |
| 85 | { |
| 86 | /// constructor |
| 87 | explicit State(std::shared_ptr<WPSStream> const &fileStream) |
| 88 | : m_fileStream(fileStream) |
| 89 | , m_idToZoneMap() |
| 90 | , m_idToTypeNameMap() |
| 91 | , m_pictureIdToZoneIdList() |
| 92 | , m_pictureIdToZoneIdListCreated(false) { } |
| 93 | /// the file stream |
| 94 | std::shared_ptr<WPSStream> m_fileStream; |
| 95 | /// the map id to zone |
| 96 | std::map<int, OLEZone> m_idToZoneMap; |
| 97 | /// the map id to zone type |
| 98 | std::map<int, std::string> m_idToTypeNameMap; |
| 99 | /// a map local id to picture id zone |
| 100 | std::vector<int> m_pictureIdToZoneIdList; |
| 101 | /// a flag to know if the pictureIdToZoneIdList is created |
| 102 | bool m_pictureIdToZoneIdListCreated; |
| 103 | /// create the pictureIdToZoneIdList |
| 104 | void createPictureIdToZoneIdList(); |
| 105 | }; |
| 106 | |
| 107 | void State::createPictureIdToZoneIdList() |
| 108 | { |
| 109 | if (m_pictureIdToZoneIdListCreated) return; |
| 110 | m_pictureIdToZoneIdListCreated=true; |
| 111 | for (auto const &it : m_idToZoneMap) |
| 112 | { |
| 113 | if (it.second.m_names[1]=="Lotus:TOOLS:OEMString") |
| 114 | m_pictureIdToZoneIdList.push_back(it.first); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | //////////////////////////////////////////////////////////// |
| 120 | // constructor/destructor |
| 121 | //////////////////////////////////////////////////////////// |
| 122 | WPSOLE1Parser::WPSOLE1Parser(std::shared_ptr<WPSStream> const &fileStream) |
| 123 | : m_state(new WPSOLE1ParserInternal::State(fileStream)) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | WPSOLE1Parser::~WPSOLE1Parser() |
| 128 | { |
| 129 | #ifdef DEBUG |
| 130 | for (auto const &oIt : m_state->m_idToZoneMap) |
| 131 | { |
| 132 | if (oIt.first>=0) checkIfParsed(oIt.second); |
| 133 | } |
| 134 | #endif |
| 135 | } |
| 136 | |
| 137 | //////////////////////////////////////////////////////////// |
| 138 | // read the file structure |
| 139 | //////////////////////////////////////////////////////////// |
| 140 | bool WPSOLE1Parser::createZones() |
| 141 | { |
| 142 | if (!m_state->m_fileStream) return false; |
| 143 | WPSStream &stream=*(m_state->m_fileStream); |
| 144 | if (!stream.checkFilePosition(20)) |
| 145 | return false; |
| 146 | RVNGInputStreamPtr &input = stream.m_input; |
| 147 | libwps::DebugFile &ascFile=stream.m_ascii; |
| 148 | libwps::DebugStream f; |
| 149 | input->seek(-8, librevenge::RVNG_SEEK_END); |
| 150 | auto pos=long(libwps::readU32(input)); |
| 151 | auto sz=long(libwps::readU32(input)); |
| 152 | long endPos=pos+sz; |
| 153 | if (pos<=0||sz<=10 || pos+sz<=0 || !stream.checkFilePosition(endPos)) |
| 154 | return false; |
| 155 | ascFile.addPos(pos); |
| 156 | ascFile.addNote("Entries(OLE1Struct):"); |
| 157 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 158 | |
| 159 | WPSOLE1ParserInternal::OLEZone badOLE(0); |
| 160 | std::vector<WPSOLE1ParserInternal::OLEZone *> parentOLE; |
| 161 | while (!input->isEnd()) |
| 162 | { |
| 163 | pos=input->tell(); |
| 164 | if (pos+1>=endPos) break; |
| 165 | f.str(""); |
| 166 | f << "OLE1Struct:"; |
| 167 | auto level=int(libwps::readU8(input)); |
| 168 | if (level==0x18) |
| 169 | { |
| 170 | // can be followed by many FF |
| 171 | ascFile.addPos(pos); |
| 172 | ascFile.addNote(f.str().c_str()); |
| 173 | break; |
| 174 | } |
| 175 | if (pos+10>=endPos) break; |
| 176 | if (level<1 || level>3) |
| 177 | { |
| 178 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 179 | break; |
| 180 | } |
| 181 | if (level!=1) |
| 182 | f << "level=" << level << ","; |
| 183 | int const nIds=8-2*level; |
| 184 | if (pos+1+2*nIds+1>=endPos) break; |
| 185 | f << "ids=["; |
| 186 | std::vector<int> listIds; |
| 187 | for (int i=0; i<nIds; ++i) // f0=0|1, f2=1-18: some type?, f3=0-1 |
| 188 | { |
| 189 | auto val=int(libwps::readU16(input)); |
| 190 | listIds.push_back(val); |
| 191 | if (val) f << val << ","; |
| 192 | else f << "_,"; |
| 193 | } |
| 194 | f << "],"; |
| 195 | |
| 196 | WPSOLE1ParserInternal::OLEZone *ole; |
| 197 | if (level<=int(parentOLE.size())) parentOLE.resize(size_t(level-1)); |
| 198 | if (level==1) |
| 199 | { |
| 200 | // the first entry is a special 1:1, rename it with id=-1, it contains |
| 201 | // in varD the maxId |
| 202 | // in child 2:4 ole1Struct, 2:5 file |
| 203 | int id=m_state->m_idToZoneMap.empty() ? -1 : listIds[0]; |
| 204 | if (m_state->m_idToZoneMap.find(id) != m_state->m_idToZoneMap.end()) |
| 205 | { |
| 206 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: find a dupplicated id\n")); |
| 207 | f << "##dupplicated,"; |
| 208 | badOLE=WPSOLE1ParserInternal::OLEZone(level); |
| 209 | ole = &badOLE; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | m_state->m_idToZoneMap.insert(std::map<int, WPSOLE1ParserInternal::OLEZone>::value_type(id,WPSOLE1ParserInternal::OLEZone(level))); |
| 214 | ole=&m_state->m_idToZoneMap.find(id)->second; |
| 215 | parentOLE.push_back(ole); |
| 216 | } |
| 217 | } |
| 218 | else if (level-2>=int(parentOLE.size())) |
| 219 | { |
| 220 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: can not find some parent\n")); |
| 221 | f << "##parent[no],"; |
| 222 | badOLE=WPSOLE1ParserInternal::OLEZone(level); |
| 223 | ole = &badOLE; |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | auto *parent=parentOLE[size_t(level-2)]; |
| 228 | parent->m_childList.push_back(WPSOLE1ParserInternal::OLEZone(level)); |
| 229 | ole=&parent->m_childList.back(); |
| 230 | parentOLE.push_back(ole); |
| 231 | } |
| 232 | ole->m_idsList=listIds; |
| 233 | ole->m_defPosition=pos; |
| 234 | bool ok=false; |
| 235 | while (true) |
| 236 | { |
| 237 | long actPos=input->tell(); |
| 238 | if (actPos+1>endPos) break; |
| 239 | auto type=int(libwps::readU8(input)); |
| 240 | bool done=false; |
| 241 | switch (type) |
| 242 | { |
| 243 | case 4: // 1|2 seems related to the zone type 1:main? 2:auxiliary? |
| 244 | case 0xa: // never seems |
| 245 | case 0xb: // always with 1,0 |
| 246 | case 0xd: // zone ptr? |
| 247 | { |
| 248 | if (actPos+5>endPos) break; |
| 249 | ok=true; |
| 250 | unsigned long val=libwps::readU32(input); |
| 251 | if (ole->m_varIdToValueMap.find(type)!=ole->m_varIdToValueMap.end()) |
| 252 | { |
| 253 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: oops some value are already set\n")); |
| 254 | f << "###"; |
| 255 | } |
| 256 | else |
| 257 | ole->m_varIdToValueMap[type]=val; |
| 258 | f << "var" << std::hex << type << std::dec << "=" << std::hex << val << std::dec << ","; |
| 259 | done=(type==0xa || type==0xb || type==0xd); |
| 260 | break; |
| 261 | } |
| 262 | case 5: |
| 263 | case 6: |
| 264 | { |
| 265 | if (actPos+9>endPos) break; |
| 266 | ok=true; |
| 267 | auto dPtr=long(libwps::readU32(input)); |
| 268 | auto dSz=long(libwps::readU32(input)); |
| 269 | if (dSz>0) |
| 270 | { |
| 271 | if (dPtr<0 || (long)((unsigned long)dPtr+(unsigned long)dSz)<dPtr || !stream.checkFilePosition(dPtr+dSz)) |
| 272 | { |
| 273 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: oops some zone seems bad\n")); |
| 274 | f << "###"; |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | ole->m_beginList.push_back(dPtr); |
| 279 | ole->m_lengthList.push_back(dSz); |
| 280 | } |
| 281 | f << "ptr" << type << "=" << std::hex << dPtr << "<->" << dPtr+dSz << std::dec << ","; |
| 282 | } |
| 283 | done=type==5; |
| 284 | break; |
| 285 | } |
| 286 | case 9: |
| 287 | f << "data9,"; |
| 288 | if (ole->m_varIdToValueMap.find(type)!=ole->m_varIdToValueMap.end()) |
| 289 | { |
| 290 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: oops some value are already set\n")); |
| 291 | f << "###"; |
| 292 | } |
| 293 | else |
| 294 | ole->m_varIdToValueMap[type]=0; |
| 295 | done=ok=true; |
| 296 | break; |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | if (done) break; |
| 301 | if (!ok) break; |
| 302 | } |
| 303 | if (!ok) |
| 304 | { |
| 305 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 306 | break; |
| 307 | } |
| 308 | if (!ole->m_lengthList.empty()) |
| 309 | { |
| 310 | if (level==1 && listIds[4]==21 && listIds[5]==0 && ole->m_lengthList.size()==1 && ole->m_lengthList[0]<100) |
| 311 | { |
| 312 | // basic string |
| 313 | long actPos=input->tell(); |
| 314 | libwps::DebugStream f2; |
| 315 | input->seek(ole->m_beginList[0], librevenge::RVNG_SEEK_SET); |
| 316 | f2 << "OLE1Struct[name]:"; |
| 317 | std::string name; |
| 318 | for (int i=0; i<int(ole->m_lengthList[0])-1; ++i) name+=char(libwps::readU8(input)); |
| 319 | f2 << name; |
| 320 | ascFile.addPos(ole->m_beginList[0]); |
| 321 | ascFile.addNote(f2.str().c_str()); |
| 322 | m_state->m_idToTypeNameMap[listIds[0]]=name; |
| 323 | f << "name=" << name; |
| 324 | ole->m_parsed=true; |
| 325 | input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 326 | } |
| 327 | } |
| 328 | ascFile.addPos(pos); |
| 329 | ascFile.addNote(f.str().c_str()); |
| 330 | } |
| 331 | // do not replace by auto oIt : m_idToZoneMap here, as we want to modify m_idToZoneMap->second |
| 332 | for (auto &oIt : m_state->m_idToZoneMap) |
| 333 | updateZoneNames(oIt.second); |
| 334 | |
| 335 | if (input->tell()+4<endPos) |
| 336 | { |
| 337 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: I have loose the trail!!!\n")); |
| 338 | ascFile.addPos(input->tell()); |
| 339 | ascFile.addNote("OLE1Struct-###:"); |
| 340 | } |
| 341 | ascFile.addPos(endPos); |
| 342 | ascFile.addNote("OLE1Struct-end:"); |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | std::shared_ptr<WPSStream> WPSOLE1Parser::getStreamForName(std::string const &name) const |
| 347 | { |
| 348 | if (name.empty()) return std::shared_ptr<WPSStream>(); |
| 349 | for (auto const &oIt : m_state->m_idToZoneMap) |
| 350 | { |
| 351 | if (oIt.second.m_names[1]==name) |
| 352 | return getStream(oIt.second); |
| 353 | } |
| 354 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: can not find any stream with name=%s\n", name.c_str())); |
| 355 | return std::shared_ptr<WPSStream>(); |
| 356 | } |
| 357 | |
| 358 | std::shared_ptr<WPSStream> WPSOLE1Parser::getStreamForId(int id) const |
| 359 | { |
| 360 | if (m_state->m_idToZoneMap.find(id)==m_state->m_idToZoneMap.end()) |
| 361 | { |
| 362 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: can not find any stream with id=%d\n", id)); |
| 363 | return std::shared_ptr<WPSStream>(); |
| 364 | } |
| 365 | return getStream(m_state->m_idToZoneMap.find(id)->second); |
| 366 | } |
| 367 | |
| 368 | bool WPSOLE1Parser::updateZoneNames(WPSOLE1ParserInternal::OLEZone &ole) const |
| 369 | { |
| 370 | libwps::DebugStream f; |
| 371 | f << "["; |
| 372 | size_t maxId=ole.m_idsList.size()/2; |
| 373 | size_t firstId=ole.m_level==1 ? 1 : 0; |
| 374 | for (size_t i=firstId; i<maxId; ++i) |
| 375 | { |
| 376 | if (ole.m_idsList[2*i+1]!=1 || (i==0 && maxId==3)) continue; |
| 377 | int nameId=ole.m_idsList[2*i]; |
| 378 | if (m_state->m_idToTypeNameMap.find(nameId)!=m_state->m_idToTypeNameMap.end()) |
| 379 | { |
| 380 | ole.m_names[i-firstId]=m_state->m_idToTypeNameMap.find(nameId)->second; |
| 381 | f << ole.m_names[i-firstId]; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: oops can not find some names\n")); |
| 386 | f << "##nameId=" << nameId << ","; |
| 387 | } |
| 388 | if (i+1!=maxId) f << "/"; |
| 389 | } |
| 390 | f << "]"; |
| 391 | for (auto &child : ole.m_childList) |
| 392 | updateZoneNames(child); |
| 393 | if (m_state->m_fileStream.get()!=nullptr) |
| 394 | { |
| 395 | m_state->m_fileStream->m_ascii.addPos(ole.m_defPosition); |
| 396 | m_state->m_fileStream->m_ascii.addNote(f.str().c_str()); |
| 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | std::shared_ptr<WPSStream> WPSOLE1Parser::getStream(WPSOLE1ParserInternal::OLEZone const &zone) const |
| 402 | { |
| 403 | std::shared_ptr<WPSStream> res; |
| 404 | zone.m_parsed=true; |
| 405 | if (zone.m_beginList.empty() || !m_state->m_fileStream || |
| 406 | zone.m_idsList.empty() || zone.m_beginList.size()!=zone.m_lengthList.size()) |
| 407 | return res; |
| 408 | RVNGInputStreamPtr input=m_state->m_fileStream->m_input; |
| 409 | if (zone.m_beginList.size()==1) |
| 410 | { |
| 411 | res.reset(new WPSStream(input, m_state->m_fileStream->m_ascii)); |
| 412 | res->m_eof=zone.m_beginList[0]+zone.m_lengthList[0]; |
| 413 | input->seek(zone.m_beginList[0], librevenge::RVNG_SEEK_SET); |
| 414 | return res; |
| 415 | } |
| 416 | std::shared_ptr<WPSStringStream> newInput; |
| 417 | for (size_t i=0; i<zone.m_beginList.size(); ++i) |
| 418 | { |
| 419 | input->seek(zone.m_beginList[i], librevenge::RVNG_SEEK_SET); |
| 420 | unsigned long numRead; |
| 421 | const unsigned char *data=input->read(static_cast<unsigned long>(zone.m_lengthList[i]), numRead); |
| 422 | if (!data || long(numRead)!=zone.m_lengthList[i]) |
| 423 | { |
| 424 | WPS_DEBUG_MSG(("WPSOLE1Parser::getStream: can not read some data\n")); |
| 425 | return res; |
| 426 | } |
| 427 | if (i==0) |
| 428 | newInput.reset(new WPSStringStream(data, unsigned(numRead))); |
| 429 | else |
| 430 | newInput->append(data, unsigned(numRead)); |
| 431 | m_state->m_fileStream->m_ascii.skipZone(zone.m_beginList[i], zone.m_beginList[i]+zone.m_lengthList[i]-1); |
| 432 | } |
| 433 | res.reset(new WPSStream(newInput)); |
| 434 | newInput->seek(0, librevenge::RVNG_SEEK_SET); |
| 435 | std::stringstream s; |
| 436 | s << "Data" << zone.m_idsList[0]; |
| 437 | res->m_ascii.open(s.str()); |
| 438 | res->m_ascii.setStream(newInput); |
| 439 | return res; |
| 440 | } |
| 441 | |
| 442 | bool WPSOLE1Parser::updateEmbeddedObject(int localId, WPSEmbeddedObject &object) const |
| 443 | { |
| 444 | m_state->createPictureIdToZoneIdList(); |
| 445 | if (localId<=0 || localId>int(m_state->m_pictureIdToZoneIdList.size())) |
| 446 | { |
| 447 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: can not find any zone with id=%d\n", localId)); |
| 448 | return false; |
| 449 | } |
| 450 | int id=m_state->m_pictureIdToZoneIdList[size_t(localId-1)]; |
| 451 | if (m_state->m_idToZoneMap.find(id)==m_state->m_idToZoneMap.end()) |
| 452 | { |
| 453 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: can not find any zone with id=%d\n", id)); |
| 454 | return false; |
| 455 | } |
| 456 | auto const &zone=m_state->m_idToZoneMap.find(id)->second; |
| 457 | if (zone.m_names[1]!="Lotus:TOOLS:OEMString") |
| 458 | { |
| 459 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: the zone name \"%s\"seems odd\n", zone.m_names[1].empty() ? "" : zone.m_names[1].c_str())); |
| 460 | } |
| 461 | zone.m_parsed=true; |
| 462 | bool done=false; |
| 463 | /* normally two children: |
| 464 | - first with name "Lotus:TOOLS:OEMString" which contains .ole in varD |
| 465 | - second with name "Lotus:TOOLS:ByteStream" which contains the data |
| 466 | */ |
| 467 | for (auto &child : zone.m_childList) |
| 468 | { |
| 469 | if (!child.m_beginList.empty()) |
| 470 | { |
| 471 | std::shared_ptr<WPSStream> stream=getStream(child); |
| 472 | if (stream) done |= WPSOLEObject::readOLE(stream, object); |
| 473 | } |
| 474 | } |
| 475 | if (!done) |
| 476 | { |
| 477 | WPS_DEBUG_MSG(("WPSOLE1Parser::createZones: can not find any picture child for zone with id=%d\n", id)); |
| 478 | } |
| 479 | return done; |
| 480 | } |
| 481 | |
| 482 | bool WPSOLE1Parser::updateMetaData(librevenge::RVNGPropertyList &metadata, libwps_tools_win::Font::Type encoding) const |
| 483 | { |
| 484 | for (auto const &oIt : m_state->m_idToZoneMap) |
| 485 | { |
| 486 | WPSOLE1ParserInternal::OLEZone const &zone=oIt.second; |
| 487 | if (zone.m_names[1]!="Doc Info Object") |
| 488 | continue; |
| 489 | // either a node which regroup all document info or a list of node with informations |
| 490 | size_t numChilds=zone.m_childList.size(); |
| 491 | for (size_t c=0; c<(numChilds ? numChilds : 1); ++c) |
| 492 | { |
| 493 | auto const &child=numChilds ? zone.m_childList[c] : zone; |
| 494 | if (child.m_beginList.empty()) continue; |
| 495 | std::shared_ptr<WPSStream> childStream=getStream(child); |
| 496 | if (!childStream) continue; |
| 497 | |
| 498 | RVNGInputStreamPtr input=childStream->m_input; |
| 499 | long pos=input->tell(); |
| 500 | libwps::DebugFile &ascFile=childStream->m_ascii; |
| 501 | libwps::DebugStream f; |
| 502 | f << "Entries(MetaData)[" << child.m_names[0] << "]:"; |
| 503 | if (!childStream->checkFilePosition(pos+4)) |
| 504 | { |
| 505 | WPS_DEBUG_MSG(("WPSOLE1Parser::updateMetaData: a meta data zone seems too short\n")); |
| 506 | f << "###"; |
| 507 | ascFile.addPos(pos); |
| 508 | ascFile.addNote(f.str().c_str()); |
| 509 | continue; |
| 510 | } |
| 511 | auto id=int(libwps::readU16(input)); |
| 512 | auto dSz=int(libwps::readU16(input)); |
| 513 | if (!childStream->checkFilePosition(pos+4+dSz)) |
| 514 | { |
| 515 | WPS_DEBUG_MSG(("WPSOLE1Parser::updateMetaData: a meta data zone seems too short\n")); |
| 516 | f << "###"; |
| 517 | ascFile.addPos(pos); |
| 518 | ascFile.addNote(f.str().c_str()); |
| 519 | continue; |
| 520 | } |
| 521 | int wh=-1; |
| 522 | if (child.m_names[0]=="Doc Info Author") wh=9; |
| 523 | else if (child.m_names[0]=="Doc Info Last Revisor") wh=5; |
| 524 | else if (child.m_names[0]=="Doc Info Comments") wh=0; |
| 525 | else if (child.m_names[0]=="Doc Info Property") wh=1; // find always sSz=0 |
| 526 | else if (child.m_names[0]=="Doc Info Title") wh=1; |
| 527 | else if (child.m_names[0]=="Doc Info Subject") wh=2; |
| 528 | else if (child.m_names[0]=="Doc Info Keywords") wh=3; |
| 529 | else if (child.m_names[0]=="Doc Info Revisions") wh=4; |
| 530 | else if (child.m_names[0]=="Doc Info Editing Time") wh=6; // sSz=4 + 2 int |
| 531 | else if (child.m_names[0]=="Doc Info Revisions Count") wh=0xc; // sSz=2 + count |
| 532 | else if (child.m_names[0]=="Doc Info Creation Date") wh=7; // sz=a or c |
| 533 | else if (child.m_names[0]=="Doc Info Last Revision Date") wh=0xa; |
| 534 | else if (child.m_names[0]=="Doc Info Last Printed Date") wh=0xd; |
| 535 | if (wh==-1 || wh!=(id&0xFE7F)) |
| 536 | { |
| 537 | WPS_DEBUG_MSG(("WPSOLE1Parser::updateMetaData: find unknown data\n")); |
| 538 | f << "###unknown"; |
| 539 | ascFile.addPos(pos); |
| 540 | ascFile.addNote(f.str().c_str()); |
| 541 | continue; |
| 542 | } |
| 543 | bool ok=false; |
| 544 | switch (wh) |
| 545 | { |
| 546 | case 0: |
| 547 | case 1: |
| 548 | case 2: |
| 549 | case 3: |
| 550 | case 4: |
| 551 | case 5: |
| 552 | case 9: |
| 553 | { |
| 554 | std::string text; |
| 555 | for (int i=0; i<dSz; ++i) |
| 556 | { |
| 557 | auto ch=char(libwps::readU8(input)); |
| 558 | if (!ch) break; |
| 559 | text.push_back(ch); |
| 560 | } |
| 561 | auto name=libwps_tools_win::Font::unicodeString(text, encoding); |
| 562 | ok=true; |
| 563 | if (name.empty()) break; |
| 564 | if (wh==9) |
| 565 | metadata.insert("dc:creator", name); |
| 566 | else if (wh==1) |
| 567 | metadata.insert("dc:title", name); |
| 568 | else if (wh==2) |
| 569 | metadata.insert("dc:subject", name); |
| 570 | else if (wh==3) |
| 571 | metadata.insert("meta:keywords", name); |
| 572 | else if (wh==0) |
| 573 | metadata.insert("dc:description", name); |
| 574 | f << name.cstr() << ","; |
| 575 | break; |
| 576 | } |
| 577 | case 0xc: |
| 578 | if (dSz!=2) break; |
| 579 | ok=true; |
| 580 | f << "rev=" << libwps::readU16(input) << ","; |
| 581 | break; |
| 582 | case 6: |
| 583 | { |
| 584 | if (dSz!=4) break; |
| 585 | ok=true; |
| 586 | f << "time=" << libwps::readU16(input) << ","; |
| 587 | int val=libwps::readU16(input); // 0 |
| 588 | if (val) f << "f0=" << val << ","; |
| 589 | break; |
| 590 | } |
| 591 | case 7: |
| 592 | case 0xa: |
| 593 | case 0xd: |
| 594 | { |
| 595 | if (dSz!=10 && dSz!=12) break; |
| 596 | ok=true; |
| 597 | f << "date=" << libwps::readU16(input) << ","; |
| 598 | int const numData=(dSz-2)/2; |
| 599 | for (int i=0; i<numData; i++) // f0=0, f1=0-16, f2=0-36, f3=0-39, f4=0 |
| 600 | { |
| 601 | int val=libwps::readU16(input); // 0 |
| 602 | if (val) f << "f" << i << "=" << val << ","; |
| 603 | } |
| 604 | break; |
| 605 | } |
| 606 | default: |
| 607 | ok=false; |
| 608 | break; |
| 609 | } |
| 610 | if (!ok) |
| 611 | { |
| 612 | WPS_DEBUG_MSG(("WPSOLE1Parser::updateMetaData: can not read some data\n")); |
| 613 | f << "##unknown,"; |
| 614 | } |
| 615 | if (input->tell()!=pos+4+dSz) |
| 616 | ascFile.addDelimiter(input->tell(),'|'); |
| 617 | ascFile.addPos(pos); |
| 618 | ascFile.addNote(f.str().c_str()); |
| 619 | } |
| 620 | } |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | void WPSOLE1Parser::checkIfParsed(WPSOLE1ParserInternal::OLEZone const &zone) const |
| 625 | { |
| 626 | if (zone.m_parsed) return; |
| 627 | for (auto const &child : zone.m_childList) checkIfParsed(child); |
| 628 | if (zone.m_beginList.empty() || !m_state->m_fileStream) return; |
| 629 | if (zone.m_names[1]=="Lotus:TOOLS:ByteStream") |
| 630 | { |
| 631 | auto stream=getStream(zone); |
| 632 | WPSEmbeddedObject object; |
| 633 | if (stream && WPSOLEObject::readOLE(stream, object)) return; |
| 634 | } |
| 635 | libwps::DebugStream f; |
| 636 | f << "Entries(Unparsed):"; |
| 637 | for (const auto &name : zone.m_names) |
| 638 | { |
| 639 | if (!name.empty()) f << name << ","; |
| 640 | } |
| 641 | m_state->m_fileStream->m_ascii.addPos(zone.m_beginList[0]); |
| 642 | m_state->m_fileStream->m_ascii.addNote(f.str().c_str()); |
| 643 | } |
| 644 | |
| 645 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |