| 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) 2006, 2007 Andrew Ziem |
| 11 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * |
| 14 | * For minor contributions see the git repository. |
| 15 | * |
| 16 | * Alternatively, the contents of this file may be used under the terms |
| 17 | * of the GNU Lesser General Public License Version 2.1 or later |
| 18 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 19 | * applicable instead of those above. |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include <map> |
| 26 | #include <sstream> |
| 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include <librevenge-stream/librevenge-stream.h> |
| 31 | |
| 32 | #include "libwps_internal.h" |
| 33 | |
| 34 | #include "WPSContentListener.h" |
| 35 | #include "WPSDebug.h" |
| 36 | #include "WPSEntry.h" |
| 37 | #include "WPSFont.h" |
| 38 | #include "WPSHeader.h" |
| 39 | #include "WPSPageSpan.h" |
| 40 | #include "WPSParagraph.h" |
| 41 | #include "WPSPosition.h" |
| 42 | #include "WPSTextSubDocument.h" |
| 43 | |
| 44 | #include "PocketWord.h" |
| 45 | |
| 46 | namespace PocketWordParserInternal |
| 47 | { |
| 48 | |
| 49 | //! the state of PocketWord |
| 50 | struct State |
| 51 | { |
| 52 | //! constructor |
| 53 | explicit State(libwps_tools_win::Font::Type encoding) |
| 54 | : m_eof(-1) |
| 55 | , m_version(6) |
| 56 | , m_encoding(encoding) |
| 57 | , m_badFile(false) |
| 58 | , m_typeEntryList() |
| 59 | , m_idToEntryMap() |
| 60 | , m_typeToEntryMap() |
| 61 | , m_pageSpan() |
| 62 | , m_idToFontNameMap() |
| 63 | { |
| 64 | m_pageSpan.setMarginLeft(0.1); |
| 65 | m_pageSpan.setMarginRight(0.1); |
| 66 | m_pageSpan.setMarginTop(0.1); |
| 67 | m_pageSpan.setMarginBottom(0.1); |
| 68 | } |
| 69 | //! returns an entry corresponding to a file identificator |
| 70 | WPSEntry getEntry(int id, int &type) const |
| 71 | { |
| 72 | auto it=m_idToEntryMap.find(id); |
| 73 | if (it==m_idToEntryMap.end() || it->second >= m_typeEntryList.size()) |
| 74 | { |
| 75 | WPS_DEBUG_MSG(("PocketWordParserInternal::State::getEntry can not find entry for id=%d\n", id)); |
| 76 | type=-1; |
| 77 | return WPSEntry(); |
| 78 | } |
| 79 | auto const &typeEntrie=m_typeEntryList[it->second]; |
| 80 | type=typeEntrie.first; |
| 81 | typeEntrie.second.setParsed(true); |
| 82 | return typeEntrie.second; |
| 83 | } |
| 84 | //! try to retrieve a color |
| 85 | bool getColor(int id, WPSColor &col) const |
| 86 | { |
| 87 | static WPSColor const colors[16]= |
| 88 | { |
| 89 | WPSColor(0,0,0), WPSColor(128,128,128), WPSColor(192,192,192), WPSColor(255,255,255), |
| 90 | WPSColor(255,0,0), WPSColor(0,255,0), WPSColor(0,0,255), WPSColor(0,255,255), |
| 91 | WPSColor(255,0,255), WPSColor(0,255,0,255), WPSColor(128,0,0), WPSColor(0,128,0), |
| 92 | WPSColor(0,0,128), WPSColor(0,128,128), WPSColor(128,0,128), WPSColor(0,128,0,128) |
| 93 | }; |
| 94 | if (id<0 || id>=16) |
| 95 | return false; |
| 96 | col=colors[id]; |
| 97 | return true; |
| 98 | } |
| 99 | //! the last file position |
| 100 | long m_eof; |
| 101 | //! the file version |
| 102 | int m_version; |
| 103 | //! the file encoding |
| 104 | libwps_tools_win::Font::Type m_encoding; |
| 105 | //! some file does not respect the unique indice conditions, ... |
| 106 | bool m_badFile; |
| 107 | |
| 108 | //! the list of type, entry |
| 109 | std::vector<std::pair<int, WPSEntry> > m_typeEntryList; |
| 110 | //! the file id to (type, entry) index |
| 111 | std::map<int, size_t > m_idToEntryMap; |
| 112 | //! a type to a list of (type,entry) indices |
| 113 | std::map<int, std::vector<size_t> > m_typeToEntryMap; |
| 114 | |
| 115 | //! the page span |
| 116 | WPSPageSpan m_pageSpan; |
| 117 | //! the correspondance between index and font name |
| 118 | std::map<int, librevenge::RVNGString> m_idToFontNameMap; |
| 119 | }; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | // constructor, destructor |
| 124 | PocketWordParser::PocketWordParser(RVNGInputStreamPtr &input, WPSHeaderPtr &header, libwps_tools_win::Font::Type encoding) |
| 125 | : WPSParser(input, header) |
| 126 | , m_listener() |
| 127 | , m_state() |
| 128 | { |
| 129 | m_state.reset(new PocketWordParserInternal::State(encoding)); |
| 130 | } |
| 131 | |
| 132 | PocketWordParser::~PocketWordParser() |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | bool PocketWordParser::checkFilePosition(long pos) const |
| 137 | { |
| 138 | if (m_state->m_eof < 0) |
| 139 | { |
| 140 | RVNGInputStreamPtr input = const_cast<PocketWordParser *>(this)->getInput(); |
| 141 | long actPos = input->tell(); |
| 142 | input->seek(0, librevenge::RVNG_SEEK_END); |
| 143 | m_state->m_eof=input->tell(); |
| 144 | input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 145 | } |
| 146 | return pos>=0 && pos <= m_state->m_eof; |
| 147 | } |
| 148 | |
| 149 | // listener |
| 150 | std::shared_ptr<WPSContentListener> PocketWordParser::createListener(librevenge::RVNGTextInterface *interface) |
| 151 | { |
| 152 | // look for page dimensions |
| 153 | auto it=m_state->m_typeToEntryMap.find(7); |
| 154 | if (it==m_state->m_typeToEntryMap.end() || it->second.empty() || |
| 155 | it->second[0]>=m_state->m_typeEntryList.size()) |
| 156 | { |
| 157 | WPS_DEBUG_MSG(("PocketWordParser::createListener: can not find the page dimensions\n")); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | if (it->second.size()>1) |
| 162 | { |
| 163 | WPS_DEBUG_MSG(("PocketWordParser::createListener: using multiple page dimensions is unimplemented\n")); |
| 164 | } |
| 165 | WPSEntry const &dEntry=m_state->m_typeEntryList[it->second[0]].second; |
| 166 | if (dEntry.valid()) |
| 167 | readPageDims(dEntry); |
| 168 | } |
| 169 | std::vector<WPSPageSpan> pageList; |
| 170 | WPSPageSpan ps(m_state->m_pageSpan); |
| 171 | pageList.push_back(ps); |
| 172 | auto listener=std::make_shared<WPSContentListener>(pageList, interface); |
| 173 | return listener; |
| 174 | } |
| 175 | |
| 176 | //////////////////////////////////////////////////////////// |
| 177 | // main funtions to parse a document |
| 178 | //////////////////////////////////////////////////////////// |
| 179 | |
| 180 | // main function to parse the document |
| 181 | void PocketWordParser::parse(librevenge::RVNGTextInterface *documentInterface) |
| 182 | { |
| 183 | RVNGInputStreamPtr input=getInput(); |
| 184 | if (!input) |
| 185 | { |
| 186 | WPS_DEBUG_MSG(("PocketWordParser::parse: does not find main input\n")); |
| 187 | throw (libwps::ParseException()); |
| 188 | } |
| 189 | if (!checkHeader(nullptr, true)) |
| 190 | throw (libwps::ParseException()); |
| 191 | |
| 192 | ascii().setStream(input); |
| 193 | ascii().open("main-1"); |
| 194 | try |
| 195 | { |
| 196 | checkHeader(nullptr); |
| 197 | if (!createZones()) |
| 198 | throw (libwps::ParseException()); |
| 199 | m_listener=createListener(documentInterface); |
| 200 | if (!m_listener) |
| 201 | { |
| 202 | WPS_DEBUG_MSG(("PocketWordParser::parse: can not create the listener\n")); |
| 203 | throw (libwps::ParseException()); |
| 204 | } |
| 205 | m_listener->startDocument(); |
| 206 | sendData(); |
| 207 | #ifdef DEBUG |
| 208 | checkUnparsed(); |
| 209 | #endif |
| 210 | m_listener->endDocument(); |
| 211 | } |
| 212 | catch (...) |
| 213 | { |
| 214 | WPS_DEBUG_MSG(("PocketWordParser::parse: exception catched when parsing the main document\n")); |
| 215 | throw (libwps::ParseException()); |
| 216 | } |
| 217 | m_listener.reset(); |
| 218 | ascii().reset(); |
| 219 | } |
| 220 | |
| 221 | bool PocketWordParser::createZones() |
| 222 | { |
| 223 | auto input=getInput(); |
| 224 | if (!input) |
| 225 | throw (libwps::ParseException()); |
| 226 | int lastId=-1; |
| 227 | while (checkFilePosition(input->tell()+6)) |
| 228 | { |
| 229 | long pos=input->tell(); |
| 230 | int type=libwps::readU16(input); |
| 231 | int id=int(libwps::readU16(input)); |
| 232 | long len=long(libwps::readU16(input)); |
| 233 | if (type!=85) |
| 234 | len*=4; |
| 235 | else |
| 236 | { |
| 237 | // checkme: I always find id=0, so id may store the hi of len |
| 238 | len=long(len + (id<<16)); |
| 239 | id=65536+lastId; |
| 240 | } |
| 241 | if (len<0 || !checkFilePosition(pos+6+len)) |
| 242 | { |
| 243 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 244 | break; |
| 245 | } |
| 246 | WPSEntry entry; |
| 247 | entry.setBegin(pos+6); |
| 248 | entry.setLength(len); |
| 249 | entry.setId(id); |
| 250 | |
| 251 | size_t newId=m_state->m_typeEntryList.size(); |
| 252 | m_state->m_typeEntryList.push_back(std::make_pair(type,entry)); |
| 253 | |
| 254 | // normally, one id by zone, but some file the same id for each zone which shared the same type :-~ |
| 255 | // for instance, PocketWord files created by OpenOffice/LibreOffice :-~ |
| 256 | if (m_state->m_idToEntryMap.find(id)==m_state->m_idToEntryMap.end()) |
| 257 | m_state->m_idToEntryMap[id]=newId; |
| 258 | else if (!m_state->m_badFile) |
| 259 | { |
| 260 | WPS_DEBUG_MSG(("PocketWordParser::createZones: this file contains zone with similar indices!!!\n")); |
| 261 | m_state->m_badFile=true; |
| 262 | } |
| 263 | |
| 264 | if (m_state->m_typeToEntryMap.find(type)==m_state->m_typeToEntryMap.end()) |
| 265 | m_state->m_typeToEntryMap[type]=std::vector<size_t>(); |
| 266 | m_state->m_typeToEntryMap.find(type)->second.push_back(newId); |
| 267 | lastId=id; |
| 268 | input->seek(entry.end(), librevenge::RVNG_SEEK_SET); |
| 269 | } |
| 270 | if (!input->isEnd()) |
| 271 | { |
| 272 | ascii().addPos(input->tell()); |
| 273 | ascii().addNote("Entries(Bad):###"); |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | void PocketWordParser::checkUnparsed() |
| 279 | { |
| 280 | auto input=getInput(); |
| 281 | for (size_t i=0; i<m_state->m_typeEntryList.size(); ++i) |
| 282 | { |
| 283 | auto const &type=m_state->m_typeEntryList[i].first; |
| 284 | auto const &entry=m_state->m_typeEntryList[i].second; |
| 285 | if (entry.isParsed()) |
| 286 | continue; |
| 287 | bool done=false; |
| 288 | std::string name; |
| 289 | switch (type) |
| 290 | { |
| 291 | case 0: |
| 292 | done=readFontNames(entry); |
| 293 | break; |
| 294 | case 7: |
| 295 | done=readPageDims(entry); |
| 296 | break; |
| 297 | case 8: |
| 298 | done=readUnkn8(entry); |
| 299 | break; |
| 300 | case 20: |
| 301 | case 21: |
| 302 | done=readUnkn2021(entry, type); |
| 303 | break; |
| 304 | case 64: |
| 305 | { |
| 306 | std::vector<int> paraId; |
| 307 | done=readParagraphList(entry, paraId); |
| 308 | break; |
| 309 | } |
| 310 | case 65: |
| 311 | done=m_listener && sendParagraph(i); |
| 312 | break; |
| 313 | case 66: |
| 314 | done=readParagraphDims(entry); |
| 315 | break; |
| 316 | case 67: |
| 317 | done=readParagraphUnkn(entry); |
| 318 | break; |
| 319 | case 84: // find only sound, but this may be some picture |
| 320 | { |
| 321 | WPSEmbeddedObject object; |
| 322 | done=readSound(entry, object); |
| 323 | break; |
| 324 | } |
| 325 | /* 103: 670001000400000000005a00 |
| 326 | bdbox? |
| 327 | 000001000000010000000000000000000000000 |
| 328 | + flag? |
| 329 | */ |
| 330 | case 130: // find 820000000000000000000000 or 8200010000000000000000005a000000 where 5a is a 103 id |
| 331 | name="End"; |
| 332 | break; |
| 333 | |
| 334 | /* 131: N, Nmax?, 000000040000005f002a00d301de010000000000000000 |
| 335 | then a list a 103 id |
| 336 | */ |
| 337 | default: |
| 338 | break; |
| 339 | } |
| 340 | if (!done) |
| 341 | { |
| 342 | libwps::DebugStream f; |
| 343 | if (name.empty()) |
| 344 | f << "Entries(Zone" << type << "A):"; |
| 345 | else |
| 346 | f << "Entries(" << name << "):"; |
| 347 | f << "id=" << entry.id() << ","; |
| 348 | ascii().addPos(entry.begin()-6); |
| 349 | ascii().addNote(f.str().c_str()); |
| 350 | } |
| 351 | if (input->tell()!=entry.end()) |
| 352 | ascii().addDelimiter(input->tell(), '|'); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | bool PocketWordParser::readFontNames(WPSEntry const &entry) |
| 357 | { |
| 358 | entry.setParsed(true); |
| 359 | |
| 360 | auto input=getInput(); |
| 361 | long pos=entry.begin(); |
| 362 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 363 | libwps::DebugStream f; |
| 364 | f << "Entries(FontNames):id=" << entry.id() << ","; |
| 365 | if ((entry.length()%80) != 16) |
| 366 | { |
| 367 | WPS_DEBUG_MSG(("PocketWordParser::readFontNames: the length seems bad\n")); |
| 368 | f << "###"; |
| 369 | ascii().addPos(pos-6); |
| 370 | ascii().addNote(f.str().c_str()); |
| 371 | return true; |
| 372 | } |
| 373 | int val; |
| 374 | for (int i=0; i<2; ++i) // f3=1|2, f5=5|6 |
| 375 | { |
| 376 | val=int(libwps::readU16(input)); |
| 377 | int const expected[]= {0,0xa}; |
| 378 | if (val==expected[i]) continue; |
| 379 | f << "f" << i << "=" << val << ","; |
| 380 | } |
| 381 | int numFonts=int(libwps::readU16(input)); |
| 382 | if (numFonts!=1) |
| 383 | f << "num[fonts]=" <<numFonts << ","; |
| 384 | for (int i=0; i<5; ++i) // g0=..*100?, g1=..x100? |
| 385 | { |
| 386 | val=int(libwps::readU16(input)); |
| 387 | if (val==0) |
| 388 | continue; |
| 389 | f << "g" << i << "=" << val << ","; |
| 390 | } |
| 391 | if (numFonts<=0 || 80*numFonts+16>entry.length()) |
| 392 | { |
| 393 | WPS_DEBUG_MSG(("PocketWordParser::readFontNames: the number of fonts seems bad\n")); |
| 394 | f << "###N,"; |
| 395 | numFonts=int(entry.length()/80); |
| 396 | } |
| 397 | ascii().addPos(pos-6); |
| 398 | ascii().addNote(f.str().c_str()); |
| 399 | for (int i=0; i< numFonts; ++i) |
| 400 | { |
| 401 | pos=input->tell(); |
| 402 | f.str(""); |
| 403 | f << "FontNames-" << i << ":"; |
| 404 | |
| 405 | int fId=int(libwps::readU16(input)); |
| 406 | f << "fId=" << fId << ","; |
| 407 | val=int(libwps::readU16(input)); |
| 408 | if (val!=1) |
| 409 | f << "f0=" << val << ","; |
| 410 | val=int(libwps::readU16(input)); // 0|1|2|8 |
| 411 | if (val) |
| 412 | f << "f1=" << val << ","; |
| 413 | val=int(libwps::readU16(input)); |
| 414 | if (val) // 0|1|202|2cc |
| 415 | f << "fl=" << std::hex << val << std::dec << ","; |
| 416 | for (int j=0; j<4; ++j) // f2=0-3 |
| 417 | { |
| 418 | val=int(libwps::readU16(input)); |
| 419 | if (val) |
| 420 | f << "f" << j+2 << "=" << val << ","; |
| 421 | } |
| 422 | librevenge::RVNGString name; |
| 423 | for (int j=0; j<32; ++j) |
| 424 | { |
| 425 | val=int(libwps::readU16(input)); |
| 426 | if (!val) |
| 427 | break; |
| 428 | libwps::appendUnicode(uint32_t(val), name); |
| 429 | } |
| 430 | f << name.cstr(); |
| 431 | if (m_state->m_idToFontNameMap.find(fId)!=m_state->m_idToFontNameMap.end()) |
| 432 | { |
| 433 | WPS_DEBUG_MSG(("PocketWordParser::readFontNames: a font with id=%d already exists\n", fId)); |
| 434 | f << "###fId,"; |
| 435 | } |
| 436 | else |
| 437 | m_state->m_idToFontNameMap[fId]=name; |
| 438 | ascii().addPos(pos); |
| 439 | ascii().addNote(f.str().c_str()); |
| 440 | input->seek(pos+80, librevenge::RVNG_SEEK_SET); |
| 441 | } |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | bool PocketWordParser::readPageDims(WPSEntry const &entry) |
| 446 | { |
| 447 | auto input=getInput(); |
| 448 | if (!input) |
| 449 | throw (libwps::ParseException()); |
| 450 | entry.setParsed(true); |
| 451 | long pos=entry.begin(); |
| 452 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 453 | libwps::DebugStream f; |
| 454 | f << "Entries(PageDims):id=" << entry.id() << ","; |
| 455 | if (entry.length()!=84) |
| 456 | { |
| 457 | WPS_DEBUG_MSG(("PocketWordParser::readPageDims: the length seems bad\n")); |
| 458 | f << "###"; |
| 459 | ascii().addPos(pos-6); |
| 460 | ascii().addNote(f.str().c_str()); |
| 461 | return true; |
| 462 | } |
| 463 | int val=int(libwps::readU16(input)); // 10|42|50 |
| 464 | if (val) |
| 465 | f << "fl0=" << std::hex << val << std::dec << ","; |
| 466 | val=int(libwps::readU16(input)); |
| 467 | if (val!=1) |
| 468 | f << "f0=" << val << ","; |
| 469 | double dims[2]; |
| 470 | for (auto &d : dims) |
| 471 | { |
| 472 | d=double(libwps::readU16(input)); |
| 473 | d+=double(libwps::readU16(input))/65536; |
| 474 | } |
| 475 | f << "dim=" << dims[0]/20 << "x" << dims[1]/20 << ","; |
| 476 | if (dims[0]>1440) |
| 477 | m_state->m_pageSpan.setFormWidth(dims[0]/1440.); |
| 478 | if (dims[1]>1440) |
| 479 | m_state->m_pageSpan.setFormLength(dims[1]/1440.); |
| 480 | val=int(libwps::readU16(input)); // f|226 |
| 481 | if (val!=0xf) |
| 482 | f << "fl1=" << std::hex << val << std::dec << ","; |
| 483 | val=int(libwps::readU16(input)); |
| 484 | if (val) |
| 485 | f << "f1=" << val << ","; |
| 486 | int margins[4]; // assume LTRB |
| 487 | f << "margins=["; |
| 488 | for (auto &m : margins) |
| 489 | { |
| 490 | m=int(libwps::readU16(input)); |
| 491 | f << double(m)/20. << ","; |
| 492 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 493 | } |
| 494 | f << "],"; |
| 495 | if (double(margins[0]+margins[2])<dims[0]/2) |
| 496 | { |
| 497 | m_state->m_pageSpan.setMarginLeft(double(margins[0])/1440); |
| 498 | m_state->m_pageSpan.setMarginRight(double(margins[2])/1440); |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | WPS_DEBUG_MSG(("PocketWordParser::readPageDims: the LR margins seem bad\n")); |
| 503 | f << "###"; |
| 504 | } |
| 505 | if (double(margins[1]+margins[3])<dims[1]/2) |
| 506 | { |
| 507 | m_state->m_pageSpan.setMarginTop(double(margins[1])/1440); |
| 508 | m_state->m_pageSpan.setMarginBottom(double(margins[3])/1440); |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | WPS_DEBUG_MSG(("PocketWordParser::readPageDims: the TB margins seem bad\n")); |
| 513 | f << "###"; |
| 514 | } |
| 515 | for (int i=0; i<3; ++i) // 0 |
| 516 | { |
| 517 | val=int(libwps::readU16(input)); |
| 518 | if (val) |
| 519 | f << "f" << i+2 << "=" << val << ","; |
| 520 | } |
| 521 | for (int d=0; d<2; ++d) |
| 522 | { |
| 523 | f << "unkn" << d << "=["; |
| 524 | for (int i=0; i<8; ++i) |
| 525 | { |
| 526 | val=int(libwps::readU16(input)); |
| 527 | int const expected = i==4 ? 0xa : i==7 ? 4 : 0; |
| 528 | if (val!=expected) |
| 529 | f << "f" << i << "=" << val << ","; |
| 530 | } |
| 531 | f << "],"; |
| 532 | } |
| 533 | for (int i=0; i<7; ++i) // g3=0|1 |
| 534 | { |
| 535 | val=int(libwps::readU16(input)); |
| 536 | if (val) |
| 537 | f << "g" << i << "=" << val << ","; |
| 538 | } |
| 539 | ascii().addPos(pos-6); |
| 540 | ascii().addNote(f.str().c_str()); |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | bool PocketWordParser::sendParagraph(size_t paraId) |
| 545 | { |
| 546 | auto input=getInput(); |
| 547 | if (!input) |
| 548 | throw (libwps::ParseException()); |
| 549 | if (paraId>=m_state->m_typeEntryList.size() || m_state->m_typeEntryList[paraId].first!=65) |
| 550 | { |
| 551 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: can not find paragraph %d\n", int(paraId))); |
| 552 | return true; |
| 553 | } |
| 554 | WPSEntry const &entry=m_state->m_typeEntryList[paraId].second; |
| 555 | entry.setParsed(true); |
| 556 | long pos=entry.begin(); |
| 557 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 558 | libwps::DebugStream f; |
| 559 | f << "Entries(Paragraph):id=" << entry.id() << ","; |
| 560 | if (entry.length()<22) |
| 561 | { |
| 562 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: the length seems bad\n")); |
| 563 | f << "###"; |
| 564 | ascii().addPos(pos-6); |
| 565 | ascii().addNote(f.str().c_str()); |
| 566 | return true; |
| 567 | } |
| 568 | int textLen=int(libwps::readU16(input)); |
| 569 | f << "text[len]=" << textLen << ","; |
| 570 | int textFormLen=int(libwps::readU16(input)); |
| 571 | f << "text[form,len]=" << textFormLen << ","; |
| 572 | if (22+textFormLen>entry.length()) |
| 573 | { |
| 574 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: the form length seems bad\n")); |
| 575 | f << "###"; |
| 576 | ascii().addPos(pos-6); |
| 577 | ascii().addNote(f.str().c_str()); |
| 578 | return true; |
| 579 | } |
| 580 | int numLines=int(libwps::readU16(input)); |
| 581 | if (numLines!=1) |
| 582 | f << "num[line]=" << numLines << ","; |
| 583 | int val=int(libwps::read16(input)); |
| 584 | if (val==0) |
| 585 | f << "*,"; |
| 586 | else if (val!=-1) |
| 587 | f << "f0=" << val << ","; |
| 588 | f << "id[paraDims]=" << libwps::readU16(input) << ","; // ? id |
| 589 | WPSParagraph para; |
| 590 | for (int i=0; i<4; ++i) |
| 591 | { |
| 592 | val=i==1 ? int(libwps::read16(input)) : int(libwps::readU16(input)); |
| 593 | if (!val) continue; |
| 594 | char const *wh[]= {"f1", "indent[spec]", "left[marg]", "right[marg]"}; |
| 595 | if (i>=1 && i<=3) |
| 596 | para.m_margins[i-1]=double(val)/72/20; // checkme: what is the unit, something between pt and dpi |
| 597 | f << wh[i] << "=" << val << ","; |
| 598 | } |
| 599 | for (int i=0; i<2; ++i) |
| 600 | { |
| 601 | val=int(libwps::readU8(input)); |
| 602 | if (!val) continue; |
| 603 | char const *wh[]= {"bullet", "align"}; |
| 604 | f << wh[i] << "=" << std::hex << val << std::dec << ","; |
| 605 | if (i==0 && val==0xff) |
| 606 | { |
| 607 | para.m_listLevel.m_type=libwps::BULLET; |
| 608 | libwps::appendUnicode(0x2022, para.m_listLevel.m_bullet); |
| 609 | para.m_listLevelIndex=1; |
| 610 | para.m_listLevel.m_labelIndent=para.m_margins[1]; |
| 611 | para.m_margins[1]=0; |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | switch (val) |
| 616 | { |
| 617 | case 1: |
| 618 | para.m_justify=libwps::JustificationRight; |
| 619 | break; |
| 620 | case 2: |
| 621 | para.m_justify=libwps::JustificationCenter; |
| 622 | break; |
| 623 | default: |
| 624 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: find unknown justification=%d\n", val)); |
| 625 | f << "###"; |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | for (int i=0; i<2; ++i) |
| 632 | { |
| 633 | val=int(libwps::readU16(input)); |
| 634 | if (!val) continue; |
| 635 | f << "f" << i+2 << "=" << val << ","; |
| 636 | } |
| 637 | m_listener->setParagraph(para); |
| 638 | ascii().addPos(pos-6); |
| 639 | ascii().addNote(f.str().c_str()); |
| 640 | |
| 641 | pos=input->tell(); |
| 642 | long endPos=entry.end(); |
| 643 | f.str(""); |
| 644 | f << "Paragraph-Text:"; |
| 645 | // 83 04=? 9f05=? 9480=? 9980=' a305=t romanian fe 523 163 21b |
| 646 | static std::map<uint8_t,int> const normalSpecialMap= |
| 647 | { |
| 648 | {0xc1,1}, {0xc2,2}, {0xc3,2}, {0xc4,1}, {0xc5,2}, |
| 649 | {0xe5,2}, {0xe6,2}, {0xe7,2}, {0xe8,2}, |
| 650 | {0xe9,1}, {0xea,1}, {0xeb,1}, {0xec,1}, |
| 651 | {0xef,3} |
| 652 | }; |
| 653 | // the only tag created by the OpenOffice/LibreOffice convertor |
| 654 | static std::map<uint8_t,int> const badSpecialMap= |
| 655 | { |
| 656 | {0xc4,1}, |
| 657 | {0xe5,2}, {0xe6,2}, {0xe7,2}, {0xe8,2}, |
| 658 | {0xe9,1}, {0xea,1}, {0xeb,1}, {0xec,1}, |
| 659 | }; |
| 660 | std::map<uint8_t,int> const &specialMap=m_state->m_badFile ? badSpecialMap : normalSpecialMap; |
| 661 | libwps_tools_win::Font::Type fontType=m_state->m_encoding==libwps_tools_win::Font::UNKNOWN ? |
| 662 | libwps_tools_win::Font::WIN3_WEUROPE : m_state->m_encoding; |
| 663 | m_listener->setFont(WPSFont()); |
| 664 | while (input->tell()<endPos) |
| 665 | { |
| 666 | auto c=libwps::readU8(input); |
| 667 | auto it=specialMap.find(c); |
| 668 | if (it!=specialMap.end() && input->tell()+it->second<=endPos) |
| 669 | { |
| 670 | bool done=true; |
| 671 | long actPos=input->tell(); |
| 672 | if (it->second==4) |
| 673 | val=int(libwps::readU32(input)); |
| 674 | else if (it->second==3) |
| 675 | val=int(libwps::readU16(input))+int(libwps::readU8(input)<<16); |
| 676 | else if (it->second==2) |
| 677 | val=int(libwps::readU16(input)); |
| 678 | else |
| 679 | val=int(libwps::readU8(input)); |
| 680 | if (c==0xc4 && val==0) // end of parsing |
| 681 | break; |
| 682 | switch (c) |
| 683 | { |
| 684 | case 0xc1: |
| 685 | c=(unsigned char)(val); |
| 686 | done=false; |
| 687 | break; |
| 688 | case 0xc2: |
| 689 | f << "[paraUnkn=" << val << "]"; |
| 690 | break; |
| 691 | // 0xc3: unsure, find one time an id to a paragraph and an id to a unexisting zone |
| 692 | case 0xc4: |
| 693 | if (val==4) |
| 694 | { |
| 695 | f << "\t"; |
| 696 | m_listener->insertTab(); |
| 697 | } |
| 698 | else if (m_state->m_badFile && val>0x1f) |
| 699 | { |
| 700 | done=false; |
| 701 | input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 702 | } |
| 703 | else |
| 704 | f << "[##" << std::hex << int(c) << std::dec << "=" << val << "]"; |
| 705 | break; |
| 706 | case 0xc5: |
| 707 | { |
| 708 | int cType; |
| 709 | WPSEntry cEntry=m_state->getEntry(val, cType); |
| 710 | f << "[Obj" << val << "]"; |
| 711 | if (!cEntry.valid()) |
| 712 | { |
| 713 | f << "###"; |
| 714 | break; |
| 715 | } |
| 716 | if (cType!=84) |
| 717 | { |
| 718 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: object %d does not correspond to a 84 zone\n", val)); |
| 719 | f << "###"; |
| 720 | break; |
| 721 | } |
| 722 | WPSEmbeddedObject object; |
| 723 | actPos=input->tell(); |
| 724 | if (readSound(cEntry, object) && !object.isEmpty()) |
| 725 | { |
| 726 | WPSPosition objPos(Vec2f(0,0), Vec2f(72,72), librevenge::RVNG_POINT); |
| 727 | objPos.setRelativePosition(WPSPosition::Char); |
| 728 | m_listener->insertObject(objPos, object); |
| 729 | } |
| 730 | input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 731 | break; |
| 732 | } |
| 733 | case 0xe5: |
| 734 | case 0xe6: |
| 735 | case 0xe7: |
| 736 | case 0xe8: |
| 737 | case 0xe9: |
| 738 | case 0xea: |
| 739 | case 0xeb: |
| 740 | case 0xec: |
| 741 | { |
| 742 | WPSFont font=m_listener->getFont(); |
| 743 | if (c==0xe5) |
| 744 | { |
| 745 | f << "[FN" << val << "]"; |
| 746 | auto fIt=m_state->m_idToFontNameMap.find(val); |
| 747 | if (fIt!=m_state->m_idToFontNameMap.end()) |
| 748 | font.m_name=fIt->second; |
| 749 | else if (val==0) // checkme: maybe system |
| 750 | font.m_name="courier"; |
| 751 | else |
| 752 | { |
| 753 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: can not find font %d\n", val)); |
| 754 | f << "###"; |
| 755 | } |
| 756 | } |
| 757 | else if (c==0xe6) |
| 758 | { |
| 759 | f << "[FS=" << val << "]"; |
| 760 | font.m_size=double(val ? val : 12); // checkme: what is default size |
| 761 | } |
| 762 | else if (c==0xe7) |
| 763 | { |
| 764 | if (m_state->getColor(val, font.m_color)) |
| 765 | f << "[FC=" << font.m_color << "]"; |
| 766 | else |
| 767 | { |
| 768 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: unknown color %d\n", val)); |
| 769 | f << "[FC=" << val << "]###"; |
| 770 | } |
| 771 | } |
| 772 | else if (c==0xe8) |
| 773 | { |
| 774 | f << "[Fw=" << val << "]"; |
| 775 | if (val==4 || val==1) // normal, fine |
| 776 | font.m_attributes&=~(unsigned int)(WPS_BOLD_BIT); |
| 777 | else if (val==7 || val==8) // bold, thick |
| 778 | font.m_attributes|=WPS_BOLD_BIT; |
| 779 | else |
| 780 | { |
| 781 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: unknown font weigth %d\n", val)); |
| 782 | f << "###"; |
| 783 | } |
| 784 | } |
| 785 | else if (c==0xe9) |
| 786 | { |
| 787 | f << "[FIt=" << val << "]"; |
| 788 | if (val==0) |
| 789 | font.m_attributes&=~(unsigned int)(WPS_ITALICS_BIT); |
| 790 | else if (val==1) |
| 791 | font.m_attributes|=WPS_ITALICS_BIT; |
| 792 | else |
| 793 | { |
| 794 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: unknown italic flag %d\n", val)); |
| 795 | f << "###"; |
| 796 | } |
| 797 | } |
| 798 | else if (c==0xea) |
| 799 | { |
| 800 | f << "[FUnd=" << val << "]"; |
| 801 | if (val==0) |
| 802 | font.m_attributes&=~(unsigned int)(WPS_UNDERLINE_BIT); |
| 803 | else if (val==1) |
| 804 | font.m_attributes|=WPS_UNDERLINE_BIT; |
| 805 | else |
| 806 | { |
| 807 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: unknown underline flag %d\n", val)); |
| 808 | f << "###"; |
| 809 | } |
| 810 | } |
| 811 | else if (c==0xeb) |
| 812 | { |
| 813 | f << "[FStr=" << val << "]"; |
| 814 | if (val==0) |
| 815 | font.m_attributes&=~(unsigned int)(WPS_STRIKEOUT_BIT); |
| 816 | else if (val==1) |
| 817 | font.m_attributes|=WPS_STRIKEOUT_BIT; |
| 818 | else |
| 819 | { |
| 820 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: unknown strike flag %d\n", val)); |
| 821 | f << "###"; |
| 822 | } |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | f << "[FHil=" << val << "]"; |
| 827 | if (val==0) |
| 828 | font.m_attributes&=~(unsigned int)(WPS_REVERSEVIDEO_BIT); |
| 829 | else if (val==1) |
| 830 | font.m_attributes|=WPS_REVERSEVIDEO_BIT; |
| 831 | else |
| 832 | { |
| 833 | WPS_DEBUG_MSG(("PocketWordParser::sendParagraph: unknown hilite flag %d\n", val)); |
| 834 | f << "###"; |
| 835 | } |
| 836 | } |
| 837 | m_listener->setFont(font); |
| 838 | break; |
| 839 | } |
| 840 | default: |
| 841 | f << "[C###" << std::hex << int(c) << std::dec << "=" << val << "]"; |
| 842 | break; |
| 843 | } |
| 844 | if (done) |
| 845 | { |
| 846 | if (val>100 && it->second<3) f << "##"; |
| 847 | continue; |
| 848 | } |
| 849 | |
| 850 | } |
| 851 | if (c<0x1f) |
| 852 | f << "[###" << std::hex << int(c) << std::dec << "]"; |
| 853 | else |
| 854 | { |
| 855 | f << char(c); |
| 856 | m_listener->insertUnicode(uint32_t(libwps_tools_win::Font::unicode((unsigned char)(c), fontType))); |
| 857 | } |
| 858 | } |
| 859 | m_listener->insertEOL(); |
| 860 | ascii().addPos(pos); |
| 861 | ascii().addNote(f.str().c_str()); |
| 862 | |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | bool PocketWordParser::readParagraphDims(WPSEntry const &entry) |
| 867 | { |
| 868 | auto input=getInput(); |
| 869 | if (!input) |
| 870 | throw (libwps::ParseException()); |
| 871 | long pos=entry.begin(); |
| 872 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 873 | libwps::DebugStream f; |
| 874 | f << "Entries(ParaDims):id=" << entry.id() << ","; |
| 875 | |
| 876 | if ((entry.length()%2)!=0 || (entry.length()%10)>2) |
| 877 | { |
| 878 | WPS_DEBUG_MSG(("PocketWordParser::readParagraphDims: the form length seems bad\n")); |
| 879 | f << "###"; |
| 880 | ascii().addPos(pos-6); |
| 881 | ascii().addNote(f.str().c_str()); |
| 882 | return true; |
| 883 | } |
| 884 | |
| 885 | ascii().addPos(pos-6); |
| 886 | ascii().addNote(f.str().c_str()); |
| 887 | |
| 888 | int N=int(entry.length()/10); |
| 889 | for (int i=0; i<N; ++i) |
| 890 | { |
| 891 | pos=input->tell(); |
| 892 | f.str(""); |
| 893 | f << "ParaDims-L" << i << ":"; |
| 894 | f << "num[char]=" << libwps::readU16(input) << ","; |
| 895 | int val=int(libwps::readU32(input)); |
| 896 | if (val) |
| 897 | f << "fill?=" << val << ","; |
| 898 | f << "w=" << libwps::readU16(input) << ","; // in point/2 ? |
| 899 | f << "h=" << int(libwps::readU8(input)) << ","; |
| 900 | // 2-6 some enum corresponding to h? 2:h~=10, 3:h~=12, 4:h~=14 |
| 901 | f << "fl=" << int(libwps::readU8(input)) << ","; |
| 902 | ascii().addPos(pos); |
| 903 | ascii().addNote(f.str().c_str()); |
| 904 | input->seek(pos+10, librevenge::RVNG_SEEK_SET); |
| 905 | } |
| 906 | return true; |
| 907 | } |
| 908 | |
| 909 | bool PocketWordParser::readParagraphUnkn(WPSEntry const &entry) |
| 910 | { |
| 911 | // maybe some tabs |
| 912 | auto input=getInput(); |
| 913 | if (!input) |
| 914 | throw (libwps::ParseException()); |
| 915 | long pos=entry.begin(); |
| 916 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 917 | libwps::DebugStream f; |
| 918 | f << "Entries(ParaUnkn):id=" << entry.id() << ","; |
| 919 | |
| 920 | if (entry.length()<11) // 20 or 24 |
| 921 | { |
| 922 | WPS_DEBUG_MSG(("PocketWordParser::readParagraphUnkn: the form length seems bad\n")); |
| 923 | f << "###"; |
| 924 | ascii().addPos(pos-6); |
| 925 | ascii().addNote(f.str().c_str()); |
| 926 | return true; |
| 927 | } |
| 928 | int val=int(libwps::readU16(input)); // f0 with sz=20|f2 with sz=24 |
| 929 | f << "fl=" << std::hex << val << std::dec << ","; |
| 930 | val=int(libwps::readU16(input)); // 40 |
| 931 | if (val!=0x40) |
| 932 | f << "f0=" << val << ","; |
| 933 | f << "id2=" << libwps::readU16(input) << ","; // 1-3 |
| 934 | val=int(libwps::readU16(input)); // 1 |
| 935 | if (val!=1) |
| 936 | f << "f1=" << val << ","; |
| 937 | int sz2=libwps::readU16(input); |
| 938 | if ((sz2%3)==0 && input->tell()+sz2<=entry.end()) |
| 939 | { |
| 940 | // really unsure |
| 941 | int N=sz2/3; |
| 942 | for (int i=0; i<N; ++i) |
| 943 | { |
| 944 | f << "unkn" << i << "=["; |
| 945 | for (int j=0; j<3; ++j) |
| 946 | { |
| 947 | val=int(libwps::readU8(input)); |
| 948 | if (val) |
| 949 | f << val << ","; |
| 950 | else |
| 951 | f << "_,"; |
| 952 | } |
| 953 | f << "],"; |
| 954 | } |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | WPS_DEBUG_MSG(("PocketWordParser::readParagraphUnkn: something seems bads\n")); |
| 959 | f << "##sz[data]=" << sz2 << ","; // 6,9,c |
| 960 | } |
| 961 | ascii().addPos(pos-6); |
| 962 | ascii().addNote(f.str().c_str()); |
| 963 | return true; |
| 964 | } |
| 965 | |
| 966 | bool PocketWordParser::readParagraphList(WPSEntry const &entry, std::vector<int> ¶Id) |
| 967 | { |
| 968 | auto input=getInput(); |
| 969 | if (!input) |
| 970 | throw (libwps::ParseException()); |
| 971 | entry.setParsed(true); |
| 972 | long pos=entry.begin(); |
| 973 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 974 | libwps::DebugStream f; |
| 975 | f << "Entries(ParaList):id=" << entry.id() << ","; |
| 976 | |
| 977 | if ((entry.length()%8)!=0 || entry.length()<24) |
| 978 | { |
| 979 | WPS_DEBUG_MSG(("PocketWordParser::readParagraphList: the form length seems bad\n")); |
| 980 | f << "###"; |
| 981 | ascii().addPos(pos-6); |
| 982 | ascii().addNote(f.str().c_str()); |
| 983 | return true; |
| 984 | } |
| 985 | f << "vals=["; |
| 986 | for (int i=0; i<6; ++i) // some big number often [X,Y,Z,W,0,0] with X~Y~W and Z much bigger |
| 987 | { |
| 988 | f << libwps::readU32(input) << ","; |
| 989 | } |
| 990 | f << "],"; |
| 991 | ascii().addPos(pos-6); |
| 992 | ascii().addNote(f.str().c_str()); |
| 993 | |
| 994 | int N=int(entry.length()/8)-3; |
| 995 | for (int i=0; i<N; ++i) |
| 996 | { |
| 997 | pos=input->tell(); |
| 998 | f.str(""); |
| 999 | f << "ParaList-L" << i << ":"; |
| 1000 | int val=int(libwps::readU16(input)); |
| 1001 | if (val!=1) |
| 1002 | f << "num[lines]=" << val << ","; |
| 1003 | f << "num[char]=" << int(libwps::readU16(input)) << ","; |
| 1004 | paraId.push_back(int(libwps::readU16(input))); |
| 1005 | f << "id=" << paraId.back() << ","; |
| 1006 | val=int(libwps::readU16(input)); // always 0? |
| 1007 | if (val) |
| 1008 | f << "f0=" << val << ","; |
| 1009 | ascii().addPos(pos); |
| 1010 | ascii().addNote(f.str().c_str()); |
| 1011 | input->seek(pos+8, librevenge::RVNG_SEEK_SET); |
| 1012 | } |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
| 1016 | bool PocketWordParser::readSound(WPSEntry const &entry, WPSEmbeddedObject &object) |
| 1017 | { |
| 1018 | // unsure, may also be a picture |
| 1019 | auto input=getInput(); |
| 1020 | if (!input) |
| 1021 | throw (libwps::ParseException()); |
| 1022 | entry.setParsed(true); |
| 1023 | long pos=entry.begin(); |
| 1024 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 1025 | libwps::DebugStream f; |
| 1026 | f << "Entries(Sound):id=" << entry.id() << ","; |
| 1027 | // unsure find many times entry.length()==430, but one time entry.length()==1274... |
| 1028 | // I suspect that testing if entry.length()<0x32e must be ok |
| 1029 | if (entry.length()<0x430) |
| 1030 | { |
| 1031 | WPS_DEBUG_MSG(("PocketWordParser::readSound: the length seems bad\n")); |
| 1032 | f << "###"; |
| 1033 | ascii().addPos(pos-6); |
| 1034 | ascii().addNote(f.str().c_str()); |
| 1035 | return true; |
| 1036 | } |
| 1037 | for (int i=0; i<4; ++i) |
| 1038 | { |
| 1039 | int val=int(libwps::readU16(input)); |
| 1040 | int const expected[]= {0,1,0x49,0}; |
| 1041 | if (val!=expected[i]) |
| 1042 | f << "f" << i << "=" << val << ","; |
| 1043 | } |
| 1044 | long pictSize=libwps::readU32(input); |
| 1045 | f << "sz=" << pictSize << ","; |
| 1046 | int val=int(libwps::readU16(input)); |
| 1047 | if (val) // 0 or big number |
| 1048 | f << "unkn=" << std::hex << val << std::dec << ","; |
| 1049 | val=int(libwps::readU16(input)); |
| 1050 | if (val) // 5-7 or 4450 |
| 1051 | f << "f4=" << std::hex << val << std::dec << ","; |
| 1052 | for (int i=0; i<2; ++i) // 0 |
| 1053 | { |
| 1054 | val=int(libwps::readU16(input)); |
| 1055 | if (val) |
| 1056 | f << "f" << i+5 << "=" << val << ","; |
| 1057 | } |
| 1058 | f << "checksum?=" << std::hex << libwps::readU32(input) << std::dec << ","; |
| 1059 | for (int i=0; i<50; ++i) // 0 |
| 1060 | { |
| 1061 | val=int(libwps::readU16(input)); |
| 1062 | if (val) |
| 1063 | f << "g" << i << "=" << val << ","; |
| 1064 | } |
| 1065 | ascii().addPos(pos-6); |
| 1066 | ascii().addNote(f.str().c_str()); |
| 1067 | |
| 1068 | for (int st=0; st<2; ++st) |
| 1069 | { |
| 1070 | pos=input->tell(); |
| 1071 | f.str(""); |
| 1072 | f << "Sound-" << st << ":"; |
| 1073 | for (int i=0; i<107; ++i) // 0 |
| 1074 | { |
| 1075 | val=int(libwps::readU16(input)); |
| 1076 | if (val) |
| 1077 | f << "f" << i << "=" << val << ","; |
| 1078 | } |
| 1079 | ascii().addPos(pos); |
| 1080 | ascii().addNote(f.str().c_str()); |
| 1081 | } |
| 1082 | |
| 1083 | pos=input->tell(); |
| 1084 | f.str(""); |
| 1085 | f << "Sound-name:"; |
| 1086 | librevenge::RVNGString name; // always VoiceNote.wav |
| 1087 | for (int j=0; j<128; ++j) |
| 1088 | { |
| 1089 | val=int(libwps::readU16(input)); |
| 1090 | if (!val) |
| 1091 | break; |
| 1092 | libwps::appendUnicode(uint32_t(val), name); |
| 1093 | } |
| 1094 | f << name.cstr(); |
| 1095 | input->seek(pos+256, librevenge::RVNG_SEEK_SET); |
| 1096 | ascii().addPos(pos); |
| 1097 | ascii().addNote(f.str().c_str()); |
| 1098 | |
| 1099 | pos=input->tell(); |
| 1100 | f.str(""); |
| 1101 | f << "Sound-2:"; |
| 1102 | for (int i=0; i<132; ++i) // 0 |
| 1103 | { |
| 1104 | val=int(libwps::readU16(input)); |
| 1105 | if (val) |
| 1106 | f << "f" << i << "=" << val << ","; |
| 1107 | } |
| 1108 | ascii().addPos(pos); |
| 1109 | ascii().addNote(f.str().c_str()); |
| 1110 | |
| 1111 | // try to read the wav file |
| 1112 | int cType; |
| 1113 | WPSEntry cEntry=m_state->getEntry(int(entry.id()+65536), cType); |
| 1114 | if (!cEntry.valid() || cType!=85) |
| 1115 | { |
| 1116 | WPS_DEBUG_MSG(("PocketWordParser::readSound: can not find data corresponding to %d\n", entry.id())); |
| 1117 | return true; |
| 1118 | } |
| 1119 | readSoundData(cEntry, pictSize, object); |
| 1120 | input->seek(entry.end(), librevenge::RVNG_SEEK_SET); |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
| 1124 | bool PocketWordParser::readSoundData(WPSEntry const &entry, long pictSize, WPSEmbeddedObject &object) |
| 1125 | { |
| 1126 | // unsure, may also be a picture |
| 1127 | auto input=getInput(); |
| 1128 | if (!input) |
| 1129 | throw (libwps::ParseException()); |
| 1130 | entry.setParsed(); |
| 1131 | long pos=entry.begin(); |
| 1132 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 1133 | libwps::DebugStream f; |
| 1134 | f << "Entries(SndData):id=" << entry.id() << ","; |
| 1135 | if (entry.length()<20 || entry.length()<pictSize || entry.length()>pictSize+20) |
| 1136 | { |
| 1137 | WPS_DEBUG_MSG(("PocketWordParser::readSoundData: the length seems bad\n")); |
| 1138 | f << "###"; |
| 1139 | ascii().addPos(pos-6); |
| 1140 | ascii().addNote(f.str().c_str()); |
| 1141 | return true; |
| 1142 | } |
| 1143 | ascii().addPos(pos-6); |
| 1144 | ascii().addNote(f.str().c_str()); |
| 1145 | |
| 1146 | librevenge::RVNGBinaryData data; |
| 1147 | if (!libwps::readData(input,(unsigned long)pictSize,data)) |
| 1148 | { |
| 1149 | WPS_DEBUG_MSG(("PocketWordParser::readSoundData: can not read the sound\n")); |
| 1150 | f << "###"; |
| 1151 | ascii().addPos(pos); |
| 1152 | ascii().addNote("SndData:###"); |
| 1153 | return true; |
| 1154 | } |
| 1155 | object=WPSEmbeddedObject(data); // checkme: probably better to set media-type="application/vnd.sun.star.media" |
| 1156 | |
| 1157 | static int pictId=0; |
| 1158 | std::stringstream s; |
| 1159 | s << "Snd" << ++pictId << ".wav"; |
| 1160 | libwps::Debug::dumpFile(data, s.str().c_str()); |
| 1161 | ascii().skipZone(pos, entry.end()-1); |
| 1162 | return true; |
| 1163 | } |
| 1164 | |
| 1165 | bool PocketWordParser::readUnkn8(WPSEntry const &entry) |
| 1166 | { |
| 1167 | // checkme can be pref |
| 1168 | auto input=getInput(); |
| 1169 | if (!input) |
| 1170 | throw (libwps::ParseException()); |
| 1171 | long pos=entry.begin(); |
| 1172 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 1173 | libwps::DebugStream f; |
| 1174 | f << "Entries(UnknC):id=" << entry.id() << ","; |
| 1175 | if (entry.length()!=64) |
| 1176 | { |
| 1177 | WPS_DEBUG_MSG(("PocketWordParser::readUnkn8: the length seems bad\n")); |
| 1178 | f << "###"; |
| 1179 | ascii().addPos(pos-6); |
| 1180 | ascii().addNote(f.str().c_str()); |
| 1181 | return true; |
| 1182 | } |
| 1183 | int val=int(libwps::readU16(input)); |
| 1184 | if (val!=1) |
| 1185 | f << "f0=" << val << ","; |
| 1186 | for (int i=0; i<9; ++i) // f2=0|2, f6=0|10e|170|b, f8=0|1f62|237d |
| 1187 | { |
| 1188 | val=int(libwps::readU16(input)); |
| 1189 | if (val) |
| 1190 | f << "f" << i+1 << "=" << std::hex << val << std::dec << ","; |
| 1191 | } |
| 1192 | val=int(libwps::readU16(input)); // 8|20 |
| 1193 | if (val!=20) |
| 1194 | f << "f10=" << val << ","; |
| 1195 | for (int i=0; i<17; ++i) // g3~g11,g5=g13 some dimension/positions in points? |
| 1196 | { |
| 1197 | val=int(libwps::readU16(input)); |
| 1198 | if (val) |
| 1199 | f << "g" << i << "=" << std::hex << val << std::dec << ","; |
| 1200 | } |
| 1201 | // then 0000000000000000 or 09040000e4040100 or 09040904e4040101 some item id? |
| 1202 | ascii().addPos(pos-6); |
| 1203 | ascii().addNote(f.str().c_str()); |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
| 1207 | bool PocketWordParser::readUnkn2021(WPSEntry const &entry, int type) |
| 1208 | { |
| 1209 | // checkme follow the font name, so this can be the styles |
| 1210 | auto input=getInput(); |
| 1211 | long pos=entry.begin(); |
| 1212 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 1213 | libwps::DebugStream f; |
| 1214 | f << "Entries(Unkn" << (type==20 ? "A": "B") << "):id=" << entry.id() << ","; |
| 1215 | if (entry.length() < 4) |
| 1216 | { |
| 1217 | WPS_DEBUG_MSG(("PocketWordParser::readUnkn2021: the length seems bad\n")); |
| 1218 | f << "###"; |
| 1219 | ascii().addPos(pos-6); |
| 1220 | ascii().addNote(f.str().c_str()); |
| 1221 | return true; |
| 1222 | } |
| 1223 | for (int i=0; i<2; ++i) // a number of data + ??? |
| 1224 | { |
| 1225 | int val=int(libwps::readU16(input)); |
| 1226 | if (!val) |
| 1227 | continue; |
| 1228 | f << "f" << i << "=" << val << ","; |
| 1229 | } |
| 1230 | if (entry.length() > 4) |
| 1231 | { |
| 1232 | WPS_DEBUG_MSG(("PocketWordParser::readUnkn2021: find unexpected data in zone %d\n", type)); |
| 1233 | f << "###"; |
| 1234 | } |
| 1235 | ascii().addPos(pos-6); |
| 1236 | ascii().addNote(f.str().c_str()); |
| 1237 | return true; |
| 1238 | } |
| 1239 | |
| 1240 | //////////////////////////////////////////////////////////// |
| 1241 | // low level |
| 1242 | //////////////////////////////////////////////////////////// |
| 1243 | |
| 1244 | |
| 1245 | // basic function to check if the header is ok |
| 1246 | bool PocketWordParser::checkHeader(WPSHeader *header, bool strict) |
| 1247 | { |
| 1248 | RVNGInputStreamPtr input = getInput(); |
| 1249 | if (!input || !checkFilePosition(0x74)) // size of the header and a font |
| 1250 | { |
| 1251 | WPS_DEBUG_MSG(("PocketWordParser::checkHeader: file is too short\n")); |
| 1252 | return false; |
| 1253 | } |
| 1254 | |
| 1255 | input->seek(0x0, librevenge::RVNG_SEEK_SET); |
| 1256 | if (libwps::readU32(input)!=0x77705c7b || libwps::readU32(input)!=0x1569 || libwps::readU16(input)!=0x101) |
| 1257 | return false; |
| 1258 | libwps::DebugStream f; |
| 1259 | f << "FileHeader:"; |
| 1260 | int vers=int(libwps::readU16(input)); |
| 1261 | if (vers<6 || vers>7) |
| 1262 | { |
| 1263 | WPS_DEBUG_MSG(("PocketWordParser::checkHeader: unknown version %d\n", vers)); |
| 1264 | return false; |
| 1265 | } |
| 1266 | f << "v" << vers << ","; |
| 1267 | int val=int(libwps::readU16(input)); |
| 1268 | if (val) |
| 1269 | f << "f0=" << val << ","; |
| 1270 | if (strict) |
| 1271 | { |
| 1272 | long pos=input->tell(); |
| 1273 | // check the first length |
| 1274 | input->seek(4, librevenge::RVNG_SEEK_CUR); // type, id |
| 1275 | val=int(libwps::readU16(input)); |
| 1276 | if (!checkFilePosition(input->tell()+4*val)) |
| 1277 | { |
| 1278 | WPS_DEBUG_MSG(("PocketWordParser::checkHeader: can read the font name length\n")); |
| 1279 | return false; |
| 1280 | } |
| 1281 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 1282 | } |
| 1283 | ascii().addPos(0); |
| 1284 | ascii().addNote(f.str().c_str()); |
| 1285 | |
| 1286 | m_state->m_version=vers; |
| 1287 | if (header) |
| 1288 | { |
| 1289 | header->setMajorVersion(vers); |
| 1290 | header->setNeedEncoding(true); |
| 1291 | } |
| 1292 | return true; |
| 1293 | } |
| 1294 | |
| 1295 | //////////////////////////////////////////////////////////// |
| 1296 | // send the different data |
| 1297 | //////////////////////////////////////////////////////////// |
| 1298 | void PocketWordParser::sendData() |
| 1299 | { |
| 1300 | auto input=getInput(); |
| 1301 | if (!input || !m_listener) |
| 1302 | throw (libwps::ParseException()); |
| 1303 | |
| 1304 | // first, retrieves the font names |
| 1305 | auto it=m_state->m_typeToEntryMap.find(0); |
| 1306 | if (it==m_state->m_typeToEntryMap.end()) |
| 1307 | { |
| 1308 | WPS_DEBUG_MSG(("PocketWordParser::sendData: can not find the fonts names\n")); |
| 1309 | } |
| 1310 | else |
| 1311 | { |
| 1312 | for (auto const &id : it->second) |
| 1313 | { |
| 1314 | if (id >= m_state->m_typeEntryList.size() || !m_state->m_typeEntryList[id].second.valid()) |
| 1315 | { |
| 1316 | WPS_DEBUG_MSG(("PocketWordParser::sendData: oops pb when reading the fonts names\n")); |
| 1317 | continue; |
| 1318 | } |
| 1319 | readFontNames(m_state->m_typeEntryList[id].second); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | // now, retrieve the list of zone, we need to send |
| 1324 | if (!m_state->m_badFile) |
| 1325 | { |
| 1326 | auto lIt=m_state->m_typeToEntryMap.find(64); |
| 1327 | if (lIt==m_state->m_typeToEntryMap.end()) |
| 1328 | { |
| 1329 | WPS_DEBUG_MSG(("PocketWordParser::sendData: can not find any paragraph list\n")); |
| 1330 | } |
| 1331 | else |
| 1332 | { |
| 1333 | for (auto const &id : lIt->second) |
| 1334 | { |
| 1335 | if (id >= m_state->m_typeEntryList.size() || !m_state->m_typeEntryList[id].second.valid()) |
| 1336 | { |
| 1337 | WPS_DEBUG_MSG(("PocketWordParser::sendData: oops pb when reading some paragraph list\n")); |
| 1338 | continue; |
| 1339 | } |
| 1340 | std::vector<int> paraId; |
| 1341 | readParagraphList(m_state->m_typeEntryList[id].second, paraId); |
| 1342 | for (auto &pId : paraId) |
| 1343 | { |
| 1344 | if (pId==0) |
| 1345 | continue; |
| 1346 | auto pIt=m_state->m_idToEntryMap.find(pId); |
| 1347 | if (pIt==m_state->m_idToEntryMap.end()) |
| 1348 | { |
| 1349 | WPS_DEBUG_MSG(("PocketWordParser::sendData: can not find paragraph %d\n", pId)); |
| 1350 | continue; |
| 1351 | } |
| 1352 | sendParagraph(pIt->second); |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | return; |
| 1357 | } |
| 1358 | auto pIt=m_state->m_typeToEntryMap.find(65); |
| 1359 | if (pIt==m_state->m_typeToEntryMap.end()) |
| 1360 | { |
| 1361 | WPS_DEBUG_MSG(("PocketWordParser::sendData: can not find any paragraph\n")); |
| 1362 | return; |
| 1363 | } |
| 1364 | for (auto const &id : pIt->second) |
| 1365 | sendParagraph(id); |
| 1366 | } |
| 1367 | |
| 1368 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |