Debian Codegraph @

src/lib/MoreParser.cxx

raw
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 <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <map>
38 #include <set>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWTextListener.hxx"
44 #include "MWAWFontConverter.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictData.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWPrinter.hxx"
50 #include "MWAWRSRCParser.hxx"
51 #include "MWAWSubDocument.hxx"
52
53 #include "MoreText.hxx"
54
55 #include "MoreParser.hxx"
56
57 /** Internal: the structures of a MoreParser */
58 namespace MoreParserInternal
59 {
60 ////////////////////////////////////////
61 //! Internal: the state of a MoreParser
62 struct State {
63 //! constructor
64 State()
65 : m_typeEntryMap()
66 , m_backgroundColor(MWAWColor::white())
67 , m_colorList()
68 , m_actPage(0)
69 , m_numPages(0)
70 {
71 }
72 //! set the default color map
73 void setDefaultColorList(int version);
74
75 //! a map type -> entry
76 std::multimap<std::string, MWAWEntry> m_typeEntryMap;
77 //! the organization back page color
78 MWAWColor m_backgroundColor;
79 //! a list colorId -> color
80 std::vector<MWAWColor> m_colorList;
81 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
82 };
83
84 void State::setDefaultColorList(int version)
85 {
86 if (m_colorList.size()) return;
87 if (version==3) {
88 uint32_t const defCol[32] = {
89 0x000000,0x333333,0x555555,0x7f7f7f,0x999999,0xbbbbbb,0xdddddd,0xffffff,
90 0xfcf305,0xf20884,0xdd0806,0x02abea,0x008011,0x0000d4,0x7f007f,0x7f3f00,
91 0xffff80,0xff80ff,0xff8080,0x80ffff,0x80ff80,0x8080ff,0x008080,0x006699,
92 0xffcccc,0xcccccc,0xcc9999,0xcc9966,0xcc6633,0xcccc99,0x999966,0x666633
93 };
94 m_colorList.resize(32);
95 for (size_t i = 0; i < 32; i++)
96 m_colorList[i] = defCol[i];
97 return;
98 }
99 }
100
101 ////////////////////////////////////////
102 //! Internal: the subdocument of a MoreParser
103 class SubDocument final : public MWAWSubDocument
104 {
105 public:
106 SubDocument(MoreParser &pars, MWAWInputStreamPtr const &input)
107 : MWAWSubDocument(&pars, input, MWAWEntry())
108 {
109 }
110
111 //! destructor
112 ~SubDocument() final {}
113
114 //! operator!=
115 bool operator!=(MWAWSubDocument const &doc) const final
116 {
117 if (MWAWSubDocument::operator!=(doc)) return true;
118 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
119 if (!sDoc) return true;
120 return false;
121 }
122
123 //! the parser function
124 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
125
126 };
127
128 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
129 {
130 if (!listener.get()) {
131 MWAW_DEBUG_MSG(("MoreParserInternal::SubDocument::parse: no listener\n"));
132 return;
133 }
134 MWAW_DEBUG_MSG(("MoreParserInternal::SubDocument::parse: not implemented\n"));
135 //static_cast<MoreParser *>(m_parser)->sendHeaderFooter();
136 }
137 }
138
139
140 ////////////////////////////////////////////////////////////
141 // constructor/destructor, ...
142 ////////////////////////////////////////////////////////////
143 MoreParser::MoreParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
144 : MWAWTextParser(input, rsrcParser, header)
145 , m_state()
146 , m_textParser()
147 {
148 init();
149 }
150
151 MoreParser::~MoreParser()
152 {
153 }
154
155 void MoreParser::init()
156 {
157 resetTextListener();
158 setAsciiName("main-1");
159
160 m_state.reset(new MoreParserInternal::State);
161
162 // reduce the margin (in case, the page is not defined)
163 getPageSpan().setMargins(0.1);
164
165 m_textParser.reset(new MoreText(*this));
166 }
167
168 MWAWInputStreamPtr MoreParser::rsrcInput()
169 {
170 return getRSRCParser()->getInput();
171 }
172
173 libmwaw::DebugFile &MoreParser::rsrcAscii()
174 {
175 return getRSRCParser()->ascii();
176 }
177
178 ////////////////////////////////////////////////////////////
179 // position and height
180 ////////////////////////////////////////////////////////////
181 MWAWVec2f MoreParser::getPageLeftTop() const
182 {
183 return MWAWVec2f(float(getPageSpan().getMarginLeft()),
184 float(getPageSpan().getMarginTop()));
185 }
186
187 bool MoreParser::checkAndStore(MWAWEntry const &entry)
188 {
189 if (!entry.valid() || entry.begin() < 0x80 || !getInput()->checkPosition(entry.end()))
190 return false;
191 if (entry.type().empty()) {
192 MWAW_DEBUG_MSG(("MoreParser::checkAndStore: entry type is not set\n"));
193 return false;
194 }
195
196 m_state->m_typeEntryMap.insert
197 (std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry));
198 return true;
199 }
200
201 bool MoreParser::checkAndFindSize(MWAWEntry &entry)
202 {
203 MWAWInputStreamPtr &input= getInput();
204 if (entry.begin()<0 || !input->checkPosition(entry.begin()+4))
205 return false;
206 long actPos=input->tell();
207 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
208 entry.setLength(4+long(input->readULong(4)));
209 input->seek(actPos,librevenge::RVNG_SEEK_SET);
210 return input->checkPosition(entry.end());
211 }
212
213 ////////////////////////////////////////////////////////////
214 // interface with the text parser
215 ////////////////////////////////////////////////////////////
216
217 ////////////////////////////////////////////////////////////
218 // new page
219 ////////////////////////////////////////////////////////////
220 void MoreParser::newPage(int number)
221 {
222 if (number <= m_state->m_actPage || number > m_state->m_numPages)
223 return;
224
225 while (m_state->m_actPage < number) {
226 m_state->m_actPage++;
227 if (!getTextListener() || m_state->m_actPage == 1)
228 continue;
229 getTextListener()->insertBreak(MWAWTextListener::PageBreak);
230 }
231 }
232
233 bool MoreParser::getColor(int id, MWAWColor &col) const
234 {
235 auto numColor = static_cast<int>(m_state->m_colorList.size());
236 if (!numColor) {
237 m_state->setDefaultColorList(version());
238 numColor = int(m_state->m_colorList.size());
239 }
240 if (id < 0 || id >= numColor)
241 return false;
242 col = m_state->m_colorList[size_t(id)];
243 return true;
244 }
245
246 ////////////////////////////////////////////////////////////
247 // the parser
248 ////////////////////////////////////////////////////////////
249 void MoreParser::parse(librevenge::RVNGTextInterface *docInterface)
250 {
251 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
252 bool ok = false;
253 try {
254 // create the asciiFile
255 ascii().setStream(getInput());
256 ascii().open(asciiName());
257
258 checkHeader(nullptr);
259 ok = createZones();
260 if (ok) {
261 createDocument(docInterface);
262 m_textParser->sendMainText();
263 }
264 ascii().reset();
265 }
266 catch (...) {
267 MWAW_DEBUG_MSG(("MoreParser::parse: exception catched when parsing\n"));
268 ok = false;
269 }
270
271 resetTextListener();
272 if (!ok) throw(libmwaw::ParseException());
273 }
274
275 ////////////////////////////////////////////////////////////
276 // create the document
277 ////////////////////////////////////////////////////////////
278 void MoreParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
279 {
280 if (!documentInterface) return;
281 if (getTextListener()) {
282 MWAW_DEBUG_MSG(("MoreParser::createDocument: listener already exist\n"));
283 return;
284 }
285
286 // update the page
287 m_state->m_actPage = 0;
288
289 // create the page list
290 int numPages = 1;
291 if (m_textParser->numPages() > numPages)
292 numPages = m_textParser->numPages();
293 m_state->m_numPages = numPages;
294
295 MWAWPageSpan ps(getPageSpan());
296 ps.setPageSpan(m_state->m_numPages+1);
297 ps.setBackgroundColor(m_state->m_backgroundColor);
298 MWAWSubDocumentPtr doc=m_textParser->getHeaderFooter(true);
299 if (doc) {
300 MWAWHeaderFooter header(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
301 header.m_subDocument=doc;
302 ps.setHeaderFooter(header);
303 }
304 doc=m_textParser->getHeaderFooter(false);
305 if (doc) {
306 MWAWHeaderFooter header(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
307 header.m_subDocument=doc;
308 ps.setHeaderFooter(header);
309 }
310
311 std::vector<MWAWPageSpan> pageList(1,ps);
312 MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
313 setTextListener(listen);
314 listen->startDocument();
315 }
316
317 ////////////////////////////////////////////////////////////
318 //
319 // Intermediate level
320 //
321 ////////////////////////////////////////////////////////////
322 bool MoreParser::createZones()
323 {
324 int vers=version();
325 if (vers<2) {
326 MWAW_DEBUG_MSG(("MoreParser::createZones: do not know how to createZone for v1\n"));
327 return false;
328 }
329 if (!readZonesList())
330 return false;
331
332 auto it = m_state->m_typeEntryMap.find("PrintInfo");
333 if (it != m_state->m_typeEntryMap.end())
334 readPrintInfo(it->second);
335
336 it = m_state->m_typeEntryMap.find("DocInfo");
337 if (it != m_state->m_typeEntryMap.end())
338 readDocumentInfo(it->second);
339
340 it = m_state->m_typeEntryMap.find("Fonts");
341 if (it != m_state->m_typeEntryMap.end())
342 m_textParser->readFonts(it->second);
343
344 bool ok=false;
345 it = m_state->m_typeEntryMap.find("Topic");
346 if (it != m_state->m_typeEntryMap.end())
347 ok=m_textParser->readTopic(it->second);
348 if (!ok) // no need to continue if we can not read the text position
349 return false;
350
351 it = m_state->m_typeEntryMap.find("Comment");
352 if (it != m_state->m_typeEntryMap.end())
353 m_textParser->readComment(it->second);
354
355 it = m_state->m_typeEntryMap.find("SpeakerNote");
356 if (it != m_state->m_typeEntryMap.end())
357 m_textParser->readSpeakerNote(it->second);
358
359 it = m_state->m_typeEntryMap.find("Slide");
360 if (it != m_state->m_typeEntryMap.end())
361 readSlideList(it->second);
362
363 it = m_state->m_typeEntryMap.find("Outline");
364 if (it != m_state->m_typeEntryMap.end())
365 m_textParser->readOutlineList(it->second);
366
367 it = m_state->m_typeEntryMap.find("FreePos");
368 if (it != m_state->m_typeEntryMap.end())
369 readFreePos(it->second);
370
371 it = m_state->m_typeEntryMap.find("Unknown9");
372 if (it != m_state->m_typeEntryMap.end())
373 readUnknown9(it->second);
374
375 for (auto const &fIt : m_state->m_typeEntryMap) {
376 MWAWEntry const &entry=fIt.second;
377 if (entry.isParsed())
378 continue;
379 libmwaw::DebugStream f;
380 f << "Entries(" << entry.type() << "):";
381 ascii().addPos(entry.begin());
382 ascii().addNote(f.str().c_str());
383 ascii().addPos(entry.end());
384 ascii().addNote("_");
385 }
386
387 return m_textParser->createZones();
388 }
389
390 bool MoreParser::readZonesList()
391 {
392 int vers=version();
393 if (vers<2)
394 return false;
395 MWAWInputStreamPtr input = getInput();
396 if (!input->checkPosition(0x80)) {
397 MWAW_DEBUG_MSG(("MoreParser::readZonesList: file is too short\n"));
398 return false;
399 }
400 long pos=8;
401 input->seek(pos, librevenge::RVNG_SEEK_SET);
402 libmwaw::DebugStream f;
403 f << "Entries(Zones):";
404 for (int i=0; i < 9; i++) {
405 MWAWEntry entry;
406 entry.setBegin(long(input->readULong(4)));
407 entry.setLength(long(input->readULong(4)));
408 static char const *names[]= {
409 "PrintInfo", "DocInfo", "Unknown2", "Topic",
410 "Comment", "Slide", "Outline", "FreePos", "SpeakerNote"
411 };
412 entry.setType(names[i]);
413 if (!entry.length())
414 continue;
415 f << names[i] << "(" << std::hex << entry.begin() << "<->" << entry.end()
416 << std::dec << "), ";
417 if (!checkAndStore(entry)) {
418 MWAW_DEBUG_MSG(("MoreParser::readZonesList: can not read entry %d\n", i));
419 f << "###";
420 }
421 }
422 auto unkn=long(input->readULong(4));
423 if (unkn) f << "unkn=" << unkn << ",";
424 /* checkme: another list begins here, but I am not sure of its length :-~ */
425 for (int i=0; i < 5; i++) {
426 static char const *names[]=
427 { "Unknown9", "Fonts", "UnknownB","UnknownC", "UnknownD" };
428 MWAWEntry entry;
429 entry.setBegin(long(input->readULong(4)));
430 entry.setLength(long(input->readULong(4)));
431 entry.setType(names[i]);
432 if (!entry.length())
433 continue;
434 f << names[i] << "(" << std::hex << entry.begin() << "<->" << entry.end()
435 << std::dec << "), ";
436 if (!checkAndStore(entry)) {
437 MWAW_DEBUG_MSG(("MoreParser::readZonesList: can not read entry %d\n", i));
438 f << "###";
439 }
440 }
441 unkn=long(input->readULong(4)); // always 0?
442 if (unkn) f << "unkn2=" << std::hex << unkn << std::dec << ",";
443 ascii().addPos(pos);
444 ascii().addNote(f.str().c_str());
445
446 return !m_state->m_typeEntryMap.empty();
447 }
448
449 ////////////////////////////////////////////////////////////
450 //
451 // Low level
452 //
453 ////////////////////////////////////////////////////////////
454
455 ////////////////////////////////////////////////////////////
456 // read the print info
457 ////////////////////////////////////////////////////////////
458 bool MoreParser::readPrintInfo(MWAWEntry const &entry)
459 {
460 if (!entry.valid() || entry.length() != 120) {
461 MWAW_DEBUG_MSG(("MoreParser::readPrintInfo: the entry is bad\n"));
462 return false;
463 }
464
465 long pos = entry.begin();
466 MWAWInputStreamPtr input = getInput();
467 libmwaw::DebugStream f;
468
469 input->seek(pos, librevenge::RVNG_SEEK_SET);
470 libmwaw::PrinterInfo info;
471 if (!info.read(input)) return false;
472 f << "Entries(PrintInfo):"<< info;
473 entry.setParsed(true);
474
475 MWAWVec2i paperSize = info.paper().size();
476 MWAWVec2i pageSize = info.page().size();
477 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
478 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
479
480 // define margin from print info
481 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
482 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
483
484 // move margin left | top
485 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
486 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
487 lTopMargin -= MWAWVec2i(decalX, decalY);
488 rBotMargin += MWAWVec2i(decalX, decalY);
489
490 // decrease right | bottom
491 int rightMarg = rBotMargin.x() -10;
492 if (rightMarg < 0) rightMarg=0;
493 int botMarg = rBotMargin.y() -50;
494 if (botMarg < 0) botMarg=0;
495
496 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
497 getPageSpan().setMarginBottom(botMarg/72.0);
498 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
499 getPageSpan().setMarginRight(rightMarg/72.0);
500 getPageSpan().setFormLength(paperSize.y()/72.);
501 getPageSpan().setFormWidth(paperSize.x()/72.);
502
503 ascii().addPos(pos);
504 ascii().addNote(f.str().c_str());
505 return true;
506 }
507
508 ////////////////////////////////////////////////////////////
509 // read the document info
510 ////////////////////////////////////////////////////////////
511 bool MoreParser::readDocumentInfo(MWAWEntry const &entry)
512 {
513 if (!entry.valid() || entry.length() != 436) {
514 MWAW_DEBUG_MSG(("MoreParser::readDocumentInfo: the entry is bad\n"));
515 return false;
516 }
517
518 long pos = entry.begin();
519 MWAWInputStreamPtr input = getInput();
520 libmwaw::DebugStream f;
521
522 input->seek(pos, librevenge::RVNG_SEEK_SET);
523 f << "Entries(DocInfo):";
524 entry.setParsed(true);
525 double margins[4]; // LR TB
526 for (auto &margin : margins) margin=double(input->readULong(2))/1440.;
527 f << "margins=" << margins[0] << "x" << margins[2]
528 << "<->" << margins[1] << "x" << margins[3] << ",";
529 int val;
530 for (int i=0; i < 2; i++) { // always 1: related to header/footer?
531 val=static_cast<int>(input->readLong(1));
532 if (val!=1) f << "fl" << i << "=" << val << ",";
533 }
534 double dim[3];
535 for (auto &d: dim) d=double(input->readULong(2))/72.;
536 f << "dim=" << dim[0] << "x" << dim[1];
537 if (dim[1]<dim[2]||dim[1]>dim[2])
538 f << "[" << dim[2] << "],";
539 else
540 f << ",";
541 if (dim[0]>0 && dim[1]>0 &&
542 margins[0]>=0 && margins[1]>=0 && margins[2]>=0 && margins[3]>=0 &&
543 2.*(margins[0]+margins[1])<dim[0] && 2.*(margins[2]+margins[3])<dim[1]) {
544 getPageSpan().setMarginLeft(margins[0]);
545 getPageSpan().setMarginRight(margins[1]);
546 getPageSpan().setMarginTop(margins[2]);
547 getPageSpan().setMarginBottom(margins[3]);
548 // has we do not know how to retrieve the page orientation
549 if ((dim[0]>=dim[1]) ==
550 (getPageSpan().getFormWidth()>=getPageSpan().getFormLength())) {
551 getPageSpan().setFormWidth(dim[0]);
552 getPageSpan().setFormLength(dim[1]);
553 }
554 }
555 else {
556 MWAW_DEBUG_MSG(("MoreParser::readDocumentInfo: can not read the page dimension\n"));
557 f << "###";
558 }
559 static int const expectedVal[4]= {0,3,1,0}; // unknown
560 for (int i=0; i < 4; i++) {
561 val=static_cast<int>(input->readLong(2));
562 if (val!=expectedVal[i])
563 f << "f" << i << "=" << val << ",";
564 }
565 val=static_cast<int>(input->readLong(2));
566 if (val!=3) f << "fId?=" << val << ",";
567 val=static_cast<int>(input->readLong(2));
568 if (val!=12) f << "fSz?=" << val << ",";
569 for (int i=0; i < 2; i++) { // always 1: related to font flag
570 val=static_cast<int>(input->readLong(1));
571 if (val!=1) f << "fl" << i+2 << "=" << val << ",";
572 }
573
574 ascii().addDelimiter(input->tell(),'|');
575 ascii().addPos(pos);
576 ascii().addNote(f.str().c_str());
577
578 pos=entry.begin()+160;
579 input->seek(pos,librevenge::RVNG_SEEK_SET);
580 f.str("");
581 f << "DocInfo-II:";
582 ascii().addPos(pos);
583 ascii().addNote(f.str().c_str());
584
585 pos=entry.begin()+268;
586 input->seek(pos,librevenge::RVNG_SEEK_SET);
587 f.str("");
588 f << "DocInfo-III:";
589 for (int st=0; st <7; st++) {
590 // normal white,white,black,white,black,background=white,white
591 unsigned char color[3];
592 for (auto &c : color) c=static_cast<unsigned char>(input->readULong(2)>>8);
593 MWAWColor col(color[0], color[1], color[2]);
594 if (st==2 || st==4) {
595 if (col.isBlack())
596 continue;
597 }
598 else if (col.isWhite())
599 continue;
600 if (st==5) {
601 m_state->m_backgroundColor=col;
602 f << "backColor=" << col << ",";
603 }
604 else
605 f << "color" << st << "?=" << col << ",";
606 }
607 for (int i=0; i < 60; i++) { // always 0 excepted f57=0|1 ?
608 val=static_cast<int>(input->readLong(2));
609 if (val)
610 f << "f" << i << "=" << val << ",";
611 }
612 for (int i=0; i < 3; i++) { // always 5,5,-1 ?
613 val=static_cast<int>(input->readLong(2));
614 int expVal=i==2?-1:5;
615 if (val!=expVal)
616 f << "g" << i << "=" << val << ",";
617 }
618 ascii().addPos(pos);
619 ascii().addNote(f.str().c_str());
620 return true;
621 }
622
623 ////////////////////////////////////////////////////////////
624 // read the list of free position
625 ////////////////////////////////////////////////////////////
626 bool MoreParser::readFreePos(MWAWEntry const &entry)
627 {
628 if (!entry.valid() || entry.length()<4) {
629 MWAW_DEBUG_MSG(("MoreParser::readFreePos: the entry is bad\n"));
630 return false;
631 }
632
633 long pos = entry.begin();
634 MWAWInputStreamPtr &input= getInput();
635 libmwaw::DebugStream f;
636 entry.setParsed(true);
637
638 input->seek(pos, librevenge::RVNG_SEEK_SET);
639 auto N=static_cast<int>(input->readULong(4));
640 f << "Entries(FreePos):N=" << N;
641 if (N > (entry.length() - 4) / 8) {
642 MWAW_DEBUG_MSG(("MoreParser::readFreePos: the number of entry seems bad\n"));
643 f << "###";
644 ascii().addPos(pos);
645 ascii().addNote(f.str().c_str());
646 return false;
647 }
648 ascii().addPos(pos);
649 ascii().addNote(f.str().c_str());
650
651 int val;
652 std::vector<MWAWEntry> filePositions;
653 std::set<long> seenPos;
654 for (int i=0; i < N; i++) {
655 if (input->isEnd())
656 break;
657 pos = input->tell();
658 if (!input->checkPosition(pos+4)) {
659 MWAW_DEBUG_MSG(("MoreParser::readFreePos: can not read some position\n"));
660 break;
661 }
662 long fPos = input->readLong(4);
663 f.str("");
664 f << "FreePos-" << i << ":";
665 f << std::hex << fPos << std::dec << ",";
666 if (fPos<0 || !input->checkPosition(fPos) || seenPos.find(fPos)!=seenPos.end()) {
667 MWAW_DEBUG_MSG(("MoreParser::readFreePos: find invalid position\n"));
668 f << "###";
669 ascii().addPos(pos);
670 ascii().addNote(f.str().c_str());
671 break;
672 }
673 seenPos.insert(fPos);
674 MWAWEntry tEntry;
675 tEntry.setBegin(fPos);
676 auto what=static_cast<int>(input->readULong(2));
677 if (what==0) {
678 tEntry.setLength(static_cast<int>(input->readULong(2)));
679 f << "length=" << tEntry.length() << ",";
680 }
681 else {
682 if (what!=0x7FFF) // 0x7FFF: last entry, fPos correspond to eof position
683 f << "#wh=" << std::hex << what << std::dec << ",";
684 val = static_cast<int>(input->readULong(2)); // probably junk
685 if (val) f << "f0=" << std::hex << val << std::dec << ",";
686 }
687 if (tEntry.valid()) {
688 if (!input->checkPosition(tEntry.end())) {
689 MWAW_DEBUG_MSG(("MoreParser::readFreePos: the entry does not seems valid\n"));
690 f << "###";
691 }
692 else
693 filePositions.push_back(tEntry);
694 }
695 ascii().addPos(pos);
696 ascii().addNote(f.str().c_str());
697 }
698
699 if (input->tell()!=entry.end()) { // end can be junk field
700 ascii().addPos(input->tell());
701 ascii().addNote("FreePos-#");
702 }
703 ascii().addPos(entry.end());
704 ascii().addNote("_");
705
706 for (auto const &tEntry : filePositions) {
707 ascii().addPos(tEntry.begin());
708 ascii().addNote("FreePos-data:");
709 ascii().addPos(tEntry.end());
710 ascii().addNote("_");
711 }
712
713 return true;
714 }
715
716 ////////////////////////////////////////////////////////////
717 // read the header
718 ////////////////////////////////////////////////////////////
719 bool MoreParser::checkHeader(MWAWHeader *header, bool strict)
720 {
721 *m_state = MoreParserInternal::State();
722 MWAWInputStreamPtr input = getInput();
723 if (!input || !input->hasDataFork() || !input->checkPosition(0x80))
724 return false;
725
726 libmwaw::DebugStream f;
727 f << "FileHeader:";
728 input->seek(0,librevenge::RVNG_SEEK_SET);
729 auto val=static_cast<int>(input->readLong(2));
730 int vers;
731 switch (val) {
732 case 3:
733 vers=2;
734 if (input->readULong(4)!=0x4d524949) // MRII
735 return false;
736 break;
737 case 6:
738 vers=3;
739 if (input->readULong(4)!=0x4d4f5233) // MOR3
740 return false;
741 break;
742 default:
743 return false;
744 }
745 setVersion(vers);
746 val=static_cast<int>(input->readLong(2));
747 if (val!=0x80) {
748 if (strict)
749 return false;
750 f << "f0=" << std::hex << val << std::dec << ",";
751 }
752 if (strict) {
753 for (int i=0; i < 8; i++) {
754 MWAWEntry entry;
755 entry.setBegin(long(input->readULong(4)));
756 entry.setLength(long(input->readULong(4)));
757 if (!entry.length())
758 continue;
759 if (!input->checkPosition(entry.end()-1))
760 return false;
761 }
762 }
763 if (header)
764 header->reset(MWAWDocument::MWAW_T_MORE, vers);
765 ascii().addPos(0);
766 ascii().addNote(f.str().c_str());
767 return true;
768 }
769
770 //////////////////////////////////////////////
771 // slide
772 //////////////////////////////////////////////
773 bool MoreParser::readSlideList(MWAWEntry const &entry)
774 {
775 if (!entry.valid() || (entry.length()%8)) {
776 MWAW_DEBUG_MSG(("MoreParser::readSlideList: the entry is bad\n"));
777 return false;
778 }
779
780 long pos = entry.begin();
781 MWAWInputStreamPtr &input= getInput();
782 libmwaw::DebugStream f;
783
784 input->seek(pos, librevenge::RVNG_SEEK_SET);
785 entry.setParsed(true);
786
787 ascii().addPos(pos);
788 ascii().addNote("Entries(Slide)");
789
790 auto N=int(entry.length()/8);
791 std::vector<MWAWEntry> filePositions;
792 for (int i=0; i < N; i++) {
793 pos=input->tell();
794
795 f.str("");
796 f << "Slide-" << i << ":";
797 long fPos = input->readLong(4);
798 f << "pos=" << std::hex << fPos << std::dec << ",";
799 MWAWEntry tEntry;
800 tEntry.setBegin(fPos);
801 if (fPos==0x50) // checkme: default or related to filePosition 0x50 ?
802 ;
803 else if (!checkAndFindSize(tEntry)) {
804 MWAW_DEBUG_MSG(("MoreParser::readSlideList: can not read a file position\n"));
805 f << "###";
806 }
807 else
808 filePositions.push_back(tEntry);
809 auto val = static_cast<int>(input->readLong(2)); // always -1 ?
810 if (val != -1)
811 f << "f0=" << val << ",";
812 val = static_cast<int>(input->readLong(2)); // always 0 ?
813 if (val)
814 f << "f1=" << val << ",";
815
816 ascii().addPos(pos);
817 ascii().addNote(f.str().c_str());
818 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
819 }
820 int n=0;
821 for (auto const &tEntry : filePositions) {
822 if (readSlide(tEntry))
823 continue;
824 f.str("");
825 f << "Slide-###" << n++ << "[data]:";
826 ascii().addPos(tEntry.begin());
827 ascii().addNote(f.str().c_str());
828 ascii().addPos(tEntry.end());
829 ascii().addNote("_");
830 }
831 return true;
832 }
833
834 bool MoreParser::readSlide(MWAWEntry const &entry)
835 {
836 if (!entry.valid() || entry.length()<16) {
837 MWAW_DEBUG_MSG(("MoreParser::readSlide: the entry is bad\n"));
838 return false;
839 }
840 long pos = entry.begin();
841 long endPos = entry.end();
842 MWAWInputStreamPtr &input= getInput();
843 libmwaw::DebugStream f;
844
845 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip size
846 entry.setParsed(true);
847
848 f << "Slide[data]:";
849 ascii().addPos(pos);
850 ascii().addNote(f.str().c_str());
851 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
852
853 int n=0;
854 while (1) {
855 pos = input->tell();
856 if (pos+2 > endPos)
857 break;
858 auto type=static_cast<int>(input->readLong(2));
859 int dataSz=0;
860 if (type & 0x1)
861 dataSz=4;
862 else {
863 switch (type) {
864 case 0x66: // group: arg num group
865 case 0x68: // group of num 6a,70* ?
866 case 0x72: // group of num 74* ?
867 case 0x74: // [ id : val ]
868 dataSz=4;
869 break;
870 case 0x6a: // pattern?, text, id, ...
871 case 0x70: // size=0x4a ?
872 dataSz=4+static_cast<int>(input->readULong(4));
873 break;
874 default:
875 MWAW_DEBUG_MSG(("MoreParser::readSlide: argh... find unexpected type %d\n", type));
876 break;
877 }
878 }
879 if (!dataSz || pos+2+dataSz > endPos) {
880 input->seek(pos, librevenge::RVNG_SEEK_SET);
881 break;
882 }
883
884 f.str("");
885 f << "Slide-" << n++ << "[data]:";
886 f << "type=" << std::hex << (type&0xFFFE) << std::dec;
887 if (type&1) f << "*";
888 f << ",";
889 if (dataSz==4)
890 f << "N=" << input->readLong(4) << ",";
891 if (type==0x6a) {
892 MWAWEntry dEntry;
893 dEntry.setBegin(pos+2+4);
894 dEntry.setLength(dataSz-4);
895 // can also be some text and ?
896 if (m_textParser->parseUnknown(dEntry,-6))
897 ;
898 else if (readGraphic(dEntry))
899 f << "graphic,";
900 else
901 f << "#";
902 }
903 input->seek(pos+2+dataSz, librevenge::RVNG_SEEK_SET);
904 ascii().addPos(pos);
905 ascii().addNote(f.str().c_str());
906 }
907 pos = input->tell();
908 if (pos!=endPos) {
909 ascii().addPos(pos);
910 ascii().addNote("Slide-###[data]:");
911 }
912
913 ascii().addPos(endPos);
914 ascii().addNote("_");
915
916 return true;
917 }
918
919 bool MoreParser::readGraphic(MWAWEntry const &entry)
920 {
921 if (!entry.valid() || entry.length()<0xd)
922 return false;
923
924 long pos = entry.begin();
925 MWAWInputStreamPtr input = getInput();
926 input->seek(pos, librevenge::RVNG_SEEK_SET);
927
928 // first check
929 auto readSize = int(input->readULong(2));
930 input->seek(8, librevenge::RVNG_SEEK_CUR); // skip dim
931 long lastFlag = input->readLong(2);
932 switch (lastFlag) {
933 case 0x1101: {
934 if (readSize+2 != entry.length() && readSize+3 != entry.length())
935 return false;
936 break;
937 }
938 case 0x0011: {
939 if (entry.length() < 42) return false;
940 if (input->readULong(2) != 0x2ff) return false;
941 if (input->readULong(2) != 0xC00) return false;
942 break;
943 }
944 default:
945 return false;
946 }
947
948 input->seek(pos, librevenge::RVNG_SEEK_SET);
949
950 MWAWBox2f box;
951 if (MWAWPictData::check(input, static_cast<int>(entry.length()), box)==MWAWPict::MWAW_R_BAD)
952 return false;
953 #ifdef DEBUG_WITH_FILES
954 if (1) {
955 librevenge::RVNGBinaryData file;
956 input->seek(pos, librevenge::RVNG_SEEK_SET);
957 input->readDataBlock(entry.length(), file);
958 static int volatile pictName = 0;
959 libmwaw::DebugStream f;
960 f << "Pict-" << ++pictName << ".pct";
961 libmwaw::Debug::dumpFile(file, f.str().c_str());
962 }
963 #endif
964 ascii().skipZone(pos, entry.end()-1);
965 return true;
966 }
967
968 ////////////////////////////////////////////////////////////
969 // read some unknow zone
970 ////////////////////////////////////////////////////////////
971
972 // checkme: not sure...
973 bool MoreParser::readUnknown9(MWAWEntry const &entry)
974 {
975 if (!entry.valid() || entry.length() < 26) {
976 MWAW_DEBUG_MSG(("MoreParser::readUnknown9: the entry is bad\n"));
977 return false;
978 }
979
980 long pos = entry.begin();
981 long endPos = entry.end();
982 MWAWInputStreamPtr input = getInput();
983 libmwaw::DebugStream f;
984 entry.setParsed(true);
985
986 input->seek(pos, librevenge::RVNG_SEEK_SET);
987 f << "Entries(Unknown9):";
988 auto N=static_cast<int>(input->readLong(4));
989 f << "N=" << N << ",";
990 ascii().addPos(pos);
991 ascii().addNote(f.str().c_str());
992
993 for (int n=0; n<N; n++) {
994 pos=input->tell();
995 if (pos+6>endPos)
996 break;
997 if (n==0) {
998 if (readColors(endPos))
999 continue;
1000 input->seek(pos, librevenge::RVNG_SEEK_SET);
1001 }
1002 auto type=static_cast<int>(input->readULong(2)); // find 1: color 2:?
1003 if (type > 10) break;
1004 auto dataSz = long(input->readULong(4));
1005 if (dataSz<= 0 || pos+6+dataSz > endPos) {
1006 input->seek(pos, librevenge::RVNG_SEEK_SET);
1007 break;
1008 }
1009
1010 bool ok=false;
1011 long endFPos = pos+6+dataSz;
1012
1013 f.str("");
1014 f << "Unknown9-" << n << ":type=" << type << ",";
1015 if (type==2) {
1016 MoreStruct::Pattern pattern;
1017 ok=readPattern(endFPos, pattern);
1018 if (ok)
1019 f << pattern << ",";
1020 if (!ok) {
1021 std::string mess("");
1022 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
1023 ok = readBackside(endFPos, mess);
1024 if (ok)
1025 f << "backside," << mess;
1026 }
1027 if (!ok) {
1028 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
1029 ok = readUnkn9Sub(endFPos);
1030 if (ok)
1031 f << "Unkn9A,";
1032 }
1033 }
1034 if (!ok) {
1035 MWAW_DEBUG_MSG(("MoreParser::readUnknown9: find some unknown structure\n"));
1036 f << "###";
1037 }
1038 else if (endFPos!=input->tell()) {
1039 MWAW_DEBUG_MSG(("MoreParser::readUnknown9: find some extra data\n"));
1040 f << "###";
1041 ascii().addDelimiter(input->tell(),'|');
1042 }
1043 input->seek(endFPos, librevenge::RVNG_SEEK_SET);
1044
1045 ascii().addPos(pos);
1046 ascii().addNote(f.str().c_str());
1047 ascii().addPos(endFPos);
1048 ascii().addNote("_");
1049 }
1050 pos=input->tell();
1051 if (pos!=endPos) {
1052 MWAW_DEBUG_MSG(("MoreParser::readUnknown9: the parsing stopped before end\n"));
1053 ascii().addPos(pos);
1054 ascii().addNote("Unknown9(II)");
1055 }
1056 return true;
1057 }
1058
1059 // a list of colors ( the first zone of block9)
1060 bool MoreParser::readColors(long endPos)
1061 {
1062 MWAWInputStreamPtr input = getInput();
1063 long pos = input->tell();
1064 if (pos+22 > endPos)
1065 return false;
1066 if (input->readLong(2)!=1)
1067 return false;
1068
1069 libmwaw::DebugStream f;
1070 f << "Entries(ColorL):";
1071 auto dataSz=long(input->readULong(4));
1072 if (pos+6+dataSz > endPos)
1073 return false;
1074 long val= input->readLong(4); // 3ff or 413 a size ?
1075 if (val) f << "f0=" << val << ",";
1076 val= input->readLong(2); // always 0
1077 if (val) f << "f1=" << val << ",";
1078 auto maxCols=static_cast<int>(input->readLong(2));
1079 f << "nCol=" << maxCols << ",";
1080 if (maxCols<0 || 16+8*maxCols != dataSz)
1081 return false;
1082 ascii().addPos(pos);
1083 ascii().addNote(f.str().c_str());
1084 for (int i=0; i <= maxCols; i++) {
1085 pos=input->tell();
1086 f.str("");
1087 f << "ColorL" << i << ",";
1088 auto id=static_cast<int>(input->readLong(2));
1089 if (id!=i) f << "#id=" << id << ",";
1090 unsigned char rgb[3];
1091 for (auto &c : rgb) c=static_cast<unsigned char>(input->readULong(2)>>8);
1092 MWAWColor col(rgb[0], rgb[1], rgb[2]);
1093 f << "col=" << col << ",";
1094 ascii().addPos(pos);
1095 ascii().addNote(f.str().c_str());
1096 }
1097 return true;
1098 }
1099
1100 // a backside definition? ( the last zones of block9)
1101 bool MoreParser::readBackside(long endPos, std::string &extra)
1102 {
1103 extra="";
1104
1105 MWAWInputStreamPtr input = getInput();
1106 long pos = input->tell();
1107 if (pos+0x2e > endPos)
1108 return false;
1109
1110 std::string name("");
1111 for (int i=0; i < 8; i++)
1112 name += char(input->readULong(1));
1113 if (name != "BACKSIDE")
1114 return false;
1115
1116 libmwaw::DebugStream f;
1117 auto val=static_cast<int>(input->readULong(1)); // small number between 1 and 8
1118 f << "f0=" << val << ",";
1119 val=static_cast<int>(input->readLong(1)); // always 0 ?
1120 if (val) f << "f1=" << val << ",";
1121 for (int i=0; i < 4; i++) { // always 0?
1122 val = static_cast<int>(input->readLong(2));
1123 if (val) f << "f" << i+2 << "=" << val << ",";
1124 }
1125 int center[2]; // checkme: xy
1126 for (auto &c : center) c=static_cast<int>(input->readLong(2));
1127 if (center[0]!=500 || center[1]!=500)
1128 f << "center=" << center[0] << "x" << center[1] << ",";
1129 int dim[4];
1130 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
1131 if (dim[0] || dim[1] || dim[2]!=1000 || dim[3]!=1000)
1132 f << "dim=" << dim[0] << "x" << dim[1] << "<->" << dim[2] << "x" << dim[3] << ",";
1133 for (int i=0; i < 2; i++) { // g0: small number between 1 and a, g1:16*small number
1134 val = static_cast<int>(input->readLong(2));
1135 if (val) f << "g" << i << "=" << val << ",";
1136 }
1137 unsigned char rgb[3];
1138 for (auto &c: rgb) c=static_cast<unsigned char>(input->readULong(2)>>8);
1139 f << "col0=" << MWAWColor(rgb[0], rgb[1], rgb[2]) << ",";
1140 for (auto &c: rgb) c=static_cast<unsigned char>(input->readULong(2)>>8);
1141 f << "col1=" << MWAWColor(rgb[0], rgb[1], rgb[2]) << ",";
1142 extra=f.str();
1143 return true;
1144 }
1145
1146 // a pattern ( the zones of block9 which follow color)
1147 bool MoreParser::readPattern(long endPos, MoreStruct::Pattern &pattern)
1148 {
1149 pattern = MoreStruct::Pattern();
1150 MWAWInputStreamPtr input = getInput();
1151 long pos = input->tell();
1152 if (pos+0x1c > endPos)
1153 return false;
1154
1155 std::string name("");
1156 for (int i=0; i < 8; i++)
1157 name += char(input->readULong(1));
1158 if (name != "BACKPTRN")
1159 return false;
1160
1161 for (auto &data : pattern.m_pattern) data = static_cast<unsigned char>(input->readULong(1));
1162
1163 // checkme: in general frontColor=backColor, but not always...
1164 unsigned char rgb[3];
1165 for (auto &c : rgb) c=static_cast<unsigned char>(input->readULong(2)>>8);
1166 pattern.m_frontColor=MWAWColor(rgb[0], rgb[1], rgb[2]);
1167 for (auto &c : rgb) c=static_cast<unsigned char>(input->readULong(2)>>8);
1168 pattern.m_backColor=MWAWColor(rgb[0], rgb[1], rgb[2]);
1169 return true;
1170 }
1171
1172 /* a ? ( the middle zone of block9). checkme: structure */
1173 bool MoreParser::readUnkn9Sub(long endPos)
1174 {
1175 MWAWInputStreamPtr input = getInput();
1176 long debPos = input->tell();
1177 if (debPos+118 > endPos)
1178 return false;
1179
1180 long pos = debPos;
1181 libmwaw::DebugStream f;
1182 f << "Entries(Unkn9A):";
1183 long val=input->readLong(2); // always 1?
1184 if (val!=1) f << "f0=" << val << ",";
1185 val=input->readLong(4); // always 1c
1186 if (val!=0x1c) f << "f1=" << val << ",";
1187 val=input->readLong(4); // always 4e
1188 if (val!=0x4e) f << "f2=" << val << ",";
1189 for (int i=0; i < 5; i++) { // 0 excepted f5=-1
1190 val=input->readLong(2);
1191 if (val) f << "f" << i+3 << "=" << val << ",";
1192 }
1193 /* find [0,2,2,2,2,2,7e,0] or [0,0,0,0,0,0,ff,0,] or [db,6d,b6,db,6d,b6,db,6d,]:
1194 maybe a pattern */
1195 f << "pattern?=[";
1196 for (int i=0; i < 8; i++)
1197 f << std::hex << input->readULong(1) << std::dec << ",";
1198 f << "],";
1199
1200 static int const expectedVal[]= {0, 0, 0x8004, 0, 0, 8, 8 };
1201 for (int i=0; i < 7; i++) {
1202 val=long(input->readULong(2));
1203 if (val!=expectedVal[i]) f << "g" << i << "=" << val << ",";
1204 }
1205 for (int i=0; i < 9; i++) {
1206 val=input->readLong(2);
1207 int expected=(i==4||i==6) ? 0x48:0;
1208 if (val != expected) f << "h" << i << "=" << val << ",";
1209 }
1210
1211 ascii().addPos(pos);
1212 ascii().addNote(f.str().c_str());
1213
1214 pos=debPos+60;
1215 input->seek(pos, librevenge::RVNG_SEEK_SET);
1216 f.str("");
1217 f << "Unkn9A-II:";
1218 for (int i=0; i < 9; i++) {
1219 val=input->readLong(2);
1220 int expected= i==1 ? 1 : i<3 ? 4 : i==6 ? 0x6e : 0;
1221 if (val != expected) f << "f" << i << "=" << val << ",";
1222 }
1223 /* now 8 uint32_t zones:
1224 Z1,Z2,..,Z5: always similar, Z7: often 0
1225 note: I only find in 0..3 in the 8 uint4_t which compose a uint32_t
1226 */
1227 f << "unkn=[";
1228 for (int i=0; i < 8; i++) {
1229 val=long(input->readULong(4));
1230 if (val)
1231 f << std::hex << val << std::dec << ",";
1232 else
1233 f << "_,";
1234 }
1235 f << "],";
1236 for (int i=0; i < 3; i++) { // always 0?
1237 val=input->readLong(2);
1238 if (val)
1239 f << "g=" << val << ",";
1240 }
1241 ascii().addPos(pos);
1242 ascii().addNote(f.str().c_str());
1243
1244 pos=debPos+116;
1245 input->seek(pos, librevenge::RVNG_SEEK_SET);
1246 auto N=static_cast<int>(input->readLong(2));
1247 f.str("");
1248 f << "Unkn9A-III:N=" << N << ",";
1249 if (pos+2+(N+1)*8 > endPos) {
1250 MWAW_DEBUG_MSG(("MoreParser::readUnkn9Sub: can not read end of zone\n"));
1251 f << "###";
1252 ascii().addPos(pos);
1253 ascii().addNote(f.str().c_str());
1254 return false;
1255 }
1256 ascii().addPos(pos);
1257 ascii().addNote(f.str().c_str());
1258 for (int n=0; n <= N; n++) {
1259 pos = input->tell();
1260 f.str("");
1261 f << "Unkn9A-III[" << n << "]:";
1262 val = input->readLong(2);
1263 if (int(val) != n) f << "#id=" << val << ",";
1264
1265 ascii().addDelimiter(input->tell(),'|');
1266 ascii().addPos(pos);
1267 ascii().addNote(f.str().c_str());
1268 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
1269 }
1270 return true;
1271 }
1272
1273 ////////////////////////////////////////////////////////////
1274 // MoreStruct implementation
1275 ////////////////////////////////////////////////////////////
1276
1277 namespace MoreStruct
1278 {
1279 std::ostream &operator<<(std::ostream &o, Pattern const &pat)
1280 {
1281 o << "pat=[" << std::hex;
1282 for (auto data : pat.m_pattern)
1283 o << data << ",";
1284 o << std::dec << "],";
1285 if (!pat.m_frontColor.isBlack())
1286 o << "frontColor=" << pat.m_frontColor << ",";
1287 if (!pat.m_backColor.isWhite())
1288 o << "backColor=" << pat.m_backColor << ",";
1289 return o;
1290 }
1291 }
1292 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: