Debian Codegraph @

src/lib/WPS8Text.cpp

raw symbols SCIP
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 <limits>
26 #include <map>
27
28 #include "libwps_internal.h"
29
30 #include "WPSContentListener.h"
31 #include "WPSEntry.h"
32 #include "WPSList.h"
33 #include "WPSParagraph.h"
34 #include "WPSTextSubDocument.h"
35
36 #include "WPS8.h"
37 #include "WPS8Struct.h"
38 #include "WPS8TextStyle.h"
39
40 #include "WPS8Text.h"
41
42 /** Internal and low level: the structures of a WPS8Text used to parse PLC*/
43 namespace WPS8PLCInternal
44 {
45 /** Internal and low level: the PLC different types and their structures */
46 struct PLC;
47
48 //! a map of known plc
49 struct KnownPLC
50 {
51 public:
52 //! constructor
53 KnownPLC();
54 //! destructor
55 ~KnownPLC();
56 //! returns the PLC corresponding to a name
57 PLC get(std::string const &name);
58 protected:
59 //! creates the map of known PLC
60 void createMapping();
61 //! map name -> known PLC
62 std::map<std::string, PLC> m_knowns;
63 };
64 }
65
66 namespace WPS8TextInternal
67 {
68 /** different types
69 *
70 * - BTE: font/paragraph properties
71 * - OBJECT: object properties: image, table, ..
72 * - STRS: the text zones
73 * - TCD: the text subdivision ( cells subdivision in a table, ...)
74 * - TOKEN: field type: date/time/..
75 * - BMKT: bookmark, or a field in a datafile ?
76 */
77 enum PLCType { BTE=0, TCD, STRS, OBJECT, TOKEN, BMKT, Unknown};
78
79 /** Internal: class to store a field definition (BKMT) */
80 struct Bookmark
81 {
82 //! constructor
83 Bookmark()
84 : m_id(-1)
85 , m_text("")
86 , m_error("") {}
87 //! operator <<
88 friend std::ostream &operator<<(std::ostream &o, Bookmark const &tok);
89
90 //! an index
91 int m_id;
92 //! the field value
93 librevenge::RVNGString m_text;
94 //! a string used to store the parsing errors
95 std::string m_error;
96 };
97 //! operator<< for a Bookmark
98 std::ostream &operator<<(std::ostream &o, Bookmark const &bmk)
99 {
100 o << std::dec << "Bookm" << bmk.m_id << "='" << bmk.m_text.cstr() << "'";
101 if (!bmk.m_error.empty()) o << ", err=[" << bmk.m_error << "]";
102 return o;
103 }
104 /** Internal: class to store the note position */
105 struct Notes
106 {
107 /** constructor */
108 Notes()
109 : m_zoneNote(-1)
110 , m_zoneCorr(-1)
111 , m_type(WPSContentListener::FOOTNOTE)
112 , m_note(false)
113 , m_corr(nullptr)
114 , m_positions() {}
115 /** copy constructor */
116 Notes(Notes const &) = default;
117 Notes(Notes &&) = default;
118 /** copy operator */
119 Notes &operator=(Notes const &) = default;
120 Notes &operator=(Notes &&) = default;
121 WPSEntry getCorrespondanceEntry(long offset) const
122 {
123 WPSEntry res;
124 if (!m_corr) return res;
125 size_t nPos = m_positions.size();
126 for (size_t i = 0; i+1 < nPos; i++)
127 {
128 if (m_positions[i] != offset) continue;
129 res.setBegin(m_corr->m_positions[i]);
130 res.setEnd(m_corr->m_positions[i+1]);
131 res.setId(m_type); // set the type
132 res.setType("Text");
133 break;
134 }
135 return res;
136 }
137
138 /** the id of the zone which corresponds to the data */
139 int m_zoneNote;
140 /** the id of the zone which called/used this data */
141 int m_zoneCorr;
142 /** the note type : footnote or endnote */
143 WPSContentListener::NoteType m_type;
144 /** a flag to know if this is the note content */
145 bool m_note;
146 /** a pointer to the corresponding notes */
147 Notes const *m_corr;
148 /** the positions of the data in the file */
149 std::vector<long> m_positions;
150
151 /*! \struct Compare
152 * \brief internal struct used to create sorted map
153 */
154 struct Compare
155 {
156 //! comparaison function
157 bool operator()(Notes const *n1, Notes const *n2) const
158 {
159 int diff = n1->m_zoneNote - n2->m_zoneNote;
160 if (diff) return diff < 0;
161 diff = n1->m_zoneCorr - n2->m_zoneCorr;
162 if (diff) return diff < 0;
163 diff = int(n1->m_type)-int(n2->m_type);
164 return diff < 0;
165 }
166 };
167 //! operator <<
168 friend std::ostream &operator<<(std::ostream &o, Notes const &note);
169 };
170
171 //! operator<< for an object
172 std::ostream &operator<<(std::ostream &o, Notes const &note)
173 {
174 o << std::dec;
175 switch (note.m_type)
176 {
177 case WPSContentListener::ENDNOTE:
178 o << "endnote";
179 break;
180 case WPSContentListener::FOOTNOTE:
181 o << "footnote";
182 break;
183 default:
184 o << "###Unknown" << int(note.m_type);
185 break;
186 }
187 o << " in zone=" << note.m_zoneNote << "(corr. zone=" << note.m_zoneCorr << ")";
188 size_t numPos = note.m_positions.size();
189 o << ": N=" << numPos-1;
190 o << ",ptrs=(" << std::hex;
191 for (auto const &pos : note.m_positions) o << "0x" << pos << ",";
192 o << ")" << std::dec;
193 return o;
194 }
195 /** Internal: class to store an object definition */
196 struct Object
197 {
198 //! the object type
199 enum Type { UnknownObject = 0, Table, Image };
200
201 //! constructor
202 Object()
203 : m_type(UnknownObject)
204 , m_id(-1)
205 , m_size()
206 , m_unknown(0)
207 , m_error("") {}
208 //! operator <<
209 friend std::ostream &operator<<(std::ostream &o, Object const &obj);
210
211 //! the type (normally a Type, ...)
212 int m_type;
213 //! an identificator
214 int m_id;
215 //! the size of the object in a page
216 Vec2f m_size;
217 //! unknown data
218 long m_unknown;
219 //! a string used to store the parsing errors
220 std::string m_error;
221 };
222
223 //! operator<< for an object
224 std::ostream &operator<<(std::ostream &o, Object const &obj)
225 {
226 o << std::dec;
227 switch (obj.m_type)
228 {
229 case Object::Table:
230 o << "Table";
231 break;
232 // an object store in another Ole: id-> gives a pointer
233 case Object::Image:
234 o << "Object";
235 break;
236 default:
237 o << "Unknown" << -1-obj.m_type;
238 break;
239 }
240 if (obj.m_id > -1) o << ",eobj(id)=" << obj.m_id;
241 o <<": size(" << obj.m_size << ")";
242
243 // Object : unkn=0,1
244 // Table : unkn = 609a1b52, 64cf1858, 64e5da2f, 3311ef0
245 if (obj.m_unknown) o << std::hex << ", unkn=" << obj.m_unknown << std::dec;
246 if (!obj.m_error.empty()) o << ", err=" << obj.m_error;
247 return o;
248 }
249
250 /** Internal: class to store a field definition (TOKN) */
251 struct Token
252 {
253 //! constructor
254 Token()
255 : m_type(WPSField::None)
256 , m_textLength(-1)
257 , m_unknown(-1)
258 , m_text("")
259 , m_error("") {}
260 //! operator <<
261 friend std::ostream &operator<<(std::ostream &o, Token const &tok);
262
263 //! the field type
264 WPSField::Type m_type;
265 //! the length of the text corresponding to the token
266 int m_textLength;
267 //! an unknown value
268 int m_unknown;
269 //! the field value
270 librevenge::RVNGString m_text;
271 //! a string used to store the parsing errors
272 std::string m_error;
273 };
274 //! operator<< for a Token
275 std::ostream &operator<<(std::ostream &o, Token const &tok)
276 {
277 o << std::dec;
278 switch (tok.m_type)
279 {
280 case WPSField::PageCount:
281 o << "field[pageCount],";
282 break;
283 case WPSField::PageNumber:
284 o << "field[page],";
285 break;
286 case WPSField::PageNumberNext:
287 o << "field[nextpage],";
288 break;
289 case WPSField::Date:
290 o << "field[date],";
291 break;
292 case WPSField::Time:
293 o << "field[time],";
294 break;
295 case WPSField::Title:
296 o << "field[title],";
297 break;
298 case WPSField::Link:
299 o << "field[link],";
300 break;
301 case WPSField::Database:
302 case WPSField::None:
303 default:
304 o << "##field[unknown]" << ",";
305 break;
306 }
307 if (tok.m_text.len()) o << "value='" << tok.m_text.cstr() << "',";
308 if (tok.m_textLength != -1) o << "textLen=" << tok.m_textLength << ",";
309 if (tok.m_unknown != -1) o << "unkn=" << tok.m_unknown << ",";
310 if (!tok.m_error.empty()) o << "err=[" << tok.m_error << "]";
311 return o;
312 }
313 /** Internal: class to store the PLC: Pointer List Content ? */
314 struct DataPLC
315 {
316 //! constructor
317 DataPLC()
318 : m_name("")
319 , m_type(Unknown)
320 , m_value(-1)
321 , m_error("") {}
322 //! operator<<
323 friend std::ostream &operator<<(std::ostream &o, DataPLC const &plc);
324 //! the entry name
325 std::string m_name;
326 //! the plc type
327 PLCType m_type;
328 //! a potential value
329 long m_value;
330 //! a string used to store the parsing errors
331 std::string m_error;
332 };
333 //! operator<< for a DataPLC
334 std::ostream &operator<<(std::ostream &o, DataPLC const &plc)
335 {
336 o << "type=" << plc.m_name << ",";
337 if (plc.m_value != -1) o << "val=" << std::hex << plc.m_value << std::dec << ", ";
338 if (!plc.m_error.empty()) o << "errors=(" << plc.m_error << ")";
339 return o;
340 }
341
342 /** Internal: the state of a WPS4Text */
343 struct State
344 {
345 //! constructor
346 State()
347 : m_textZones()
348 , m_bookmarkMap()
349 , m_notesList()
350 , m_notesMap()
351 , m_object()
352 , m_objectMap()
353 , m_tokenMap()
354 , m_tcdMap()
355 , m_objectTypes()
356 , m_plcList()
357 , m_knownPLC()
358 {
359 initTypeMaps();
360 }
361
362 //! initializes the type map
363 void initTypeMaps();
364 //! returns the entry corresponding to a type id
365 WPSEntry getTextZones(int id) const
366 {
367 for (auto const &z : m_textZones)
368 {
369 if (!z.valid()) continue;
370 if (z.id() == id) return z;
371 }
372 return WPSEntry();
373 }
374 //! tests if a text zone is simillar to \a entry, if yes, updates is parsed field
375 void setParsed(WPSEntry const &entry, bool fl)
376 {
377 for (auto const &z : m_textZones)
378 {
379 if (z != entry) continue;
380 z.setParsed(fl);
381 return;
382 }
383 }
384 //! try to return a entry for a cell in table zones
385 WPSEntry getTCDZone(int strsId, int cellId) const
386 {
387 if (strsId < 0 || strsId >= int(m_textZones.size()) || cellId < 0)
388 return WPSEntry();
389 if (m_tcdMap.find(strsId) == m_tcdMap.end())
390 {
391 if (cellId != 0)
392 return WPSEntry();
393 // a table with 1 cell has no tcd,
394 m_textZones[size_t(strsId)].setParsed(true);
395 return m_textZones[size_t(strsId)];
396 }
397
398 auto const &endPos = m_tcdMap.find(strsId)->second;
399 if (cellId >= int(endPos.size()))
400 return WPSEntry();
401
402 m_textZones[size_t(strsId)].setParsed(true);
403 WPSEntry res = m_textZones[size_t(strsId)];
404 if (cellId)
405 res.setBegin(endPos[size_t(cellId-1)]+2);
406 res.setEnd(endPos[size_t(cellId)]);
407 return res;
408 }
409
410 //! the list of different text zones
411 std::vector<WPSEntry> m_textZones;
412
413 //! a map text offset->bookmark
414 std::map<long, Bookmark> m_bookmarkMap;
415
416 //! the list of notes
417 std::vector<Notes> m_notesList;
418
419 //! a map text offset->notes
420 std::map<long, Notes *> m_notesMap;
421
422 //! actual object
423 Object m_object;
424 //! a map text offset->object
425 std::map<long, Object> m_objectMap;
426
427 //! a map text offset->token
428 std::map<long, Token> m_tokenMap;
429
430 //! a map strsId -> last positions of cells
431 std::map<int, std::vector<long> > m_tcdMap;
432
433 //! the object type
434 std::map<int,int> m_objectTypes;
435 //! a list of all plcs
436 std::vector<DataPLC> m_plcList;
437 //! the known plc
438 WPS8PLCInternal::KnownPLC m_knownPLC;
439 };
440
441 void State::initTypeMaps()
442 {
443 static int const objectTypes[] =
444 {
445 0, 0x1A, 1, 0x22, 2, 0x22, 3, 0x22, 4, 0x22
446 };
447 for (int i = 0; i+1 < int(WPS_N_ELEMENTS(objectTypes)); i+=2)
448 m_objectTypes[objectTypes[i]] = objectTypes[i+1];
449 }
450
451 //! Internal: the subdocument of a WPS8Text
452 class SubDocument final : public WPSTextSubDocument
453 {
454 public:
455 //! constructor for a note/endnote entry
456 SubDocument(RVNGInputStreamPtr const &input, WPS8Text &pars, WPSEntry const &entry)
457 : WPSTextSubDocument(input, nullptr)
458 , m_textParser(&pars)
459 , m_entry(entry)
460 , m_text("") {}
461 //! constructor for a comment entry
462 SubDocument(RVNGInputStreamPtr const &input, librevenge::RVNGString const &text)
463 : WPSTextSubDocument(input, nullptr)
464 , m_textParser(nullptr)
465 , m_entry()
466 , m_text(text) {}
467
468 //! destructor
469 ~SubDocument() final {}
470
471 //! operator==
472 bool operator==(WPSSubDocumentPtr const &doc) const final
473 {
474 if (!WPSSubDocument::operator==(doc))
475 return false;
476 auto const *sDoc = dynamic_cast<SubDocument const *>(doc.get());
477 if (!sDoc) return false;
478 if (m_entry != sDoc->m_entry) return false;
479 if (m_textParser != sDoc->m_textParser) return false;
480 if (m_text != sDoc->m_text) return false;
481 return true;
482 }
483
484 //! the parser function
485 void parse(WPSContentListenerPtr &listener, libwps::SubDocumentType type) final;
486
487 WPS8Text *m_textParser;
488 WPSEntry m_entry;
489 librevenge::RVNGString m_text;
490 private:
491 SubDocument(SubDocument const &) = delete;
492 SubDocument &operator=(SubDocument const &) = delete;
493 };
494
495 void SubDocument::parse(WPSContentListenerPtr &listener, libwps::SubDocumentType type)
496 {
497 if (!listener.get())
498 {
499 WPS_DEBUG_MSG(("SubDocument::parse: no listener\n"));
500 return;
501 }
502 if (!dynamic_cast<WPSContentListener *>(listener.get()))
503 {
504 WPS_DEBUG_MSG(("SubDocument::parse: bad listener\n"));
505 return;
506 }
507 auto &listen = reinterpret_cast<WPSContentListenerPtr &>(listener);
508
509 if (type==libwps::DOC_COMMENT_ANNOTATION)
510 {
511 listen->insertUnicodeString(m_text);
512 return;
513 }
514 if (!m_textParser)
515 {
516 listen->insertCharacter(' ');
517 WPS_DEBUG_MSG(("SubDocument::parse: bad parser\n"));
518 return;
519 }
520 if (!m_entry.valid())
521 {
522 listen->insertCharacter(' ');
523 return;
524 }
525 long actPos = m_input->tell();
526 if (type == libwps::DOC_NOTE)
527 m_textParser->readText(m_entry);
528 else
529 {
530 WPS_DEBUG_MSG(("SubDocument::parse: find unknown type of document...\n"));
531 }
532 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
533 }
534
535 }
536
537 //////////////////////////////////////////////////////////////////////////////
538 // MAIN CODE
539 //////////////////////////////////////////////////////////////////////////////
540
541 // constructor/destructor
542 WPS8Text::WPS8Text(WPS8Parser &parser)
543 : WPSTextParser(parser, parser.getInput())
544 , m_listener()
545 , m_styleParser()
546 , m_state()
547 {
548 m_state.reset(new WPS8TextInternal::State);
549 m_styleParser.reset(new WPS8TextStyle(*this));
550 }
551
552 WPS8Text::~WPS8Text()
553 {
554 }
555
556 //! sets the listener
557 void WPS8Text::setListener(WPSContentListenerPtr &listen)
558 {
559 m_listener = listen;
560 m_styleParser->setListener(listen);
561 }
562
563
564 // number of page
565 int WPS8Text::numPages() const
566 {
567 int numPage = 1;
568 m_input->seek(m_textPositions.begin(), librevenge::RVNG_SEEK_SET);
569 while (!m_input->isEnd() && m_input->tell() < m_textPositions.end())
570 {
571 if (libwps::readU16(m_input.get()) == 0x0C) numPage++;
572 }
573 return numPage;
574 }
575
576
577 ////////////////////////////////////////////////////////////
578 // interface with WPS8TextStyle
579 ////////////////////////////////////////////////////////////
580 bool WPS8Text::readFont(long endPos, int &id, std::string &mess)
581 {
582 return m_styleParser->readFont(endPos, id, mess);
583 }
584
585 bool WPS8Text::readParagraph(long endPos, int &id, std::string &mess)
586 {
587 return m_styleParser->readParagraph(endPos, id, mess);
588 }
589
590 ////////////////////////////////////////////////////////////
591 // retrieves zone
592 ////////////////////////////////////////////////////////////
593 WPSEntry WPS8Text::getEntry(int strsId) const
594 {
595 if (strsId < 0 || strsId >= int(m_state->m_textZones.size())) return WPSEntry();
596 return m_state->m_textZones[size_t(strsId)];
597 }
598
599 WPSEntry WPS8Text::getHeaderEntry() const
600 {
601 WPSEntry res=m_state->getTextZones(6);
602 if (!res.valid() || res.length() != 2) return res;
603 // check if the zone is empty ie. only a eol
604 RVNGInputStreamPtr input = const_cast<WPS8Text *>(this)->getInput();
605 long actPos = input->tell();
606 input->seek(res.begin(), librevenge::RVNG_SEEK_SET);
607 bool empty = libwps::read16(input) == 0xd;
608 input->seek(actPos, librevenge::RVNG_SEEK_SET);
609 if (empty)
610 {
611 m_state->setParsed(res, true);
612 return WPSEntry();
613 }
614 return res;
615 }
616
617 WPSEntry WPS8Text::getFooterEntry() const
618 {
619 WPSEntry res=m_state->getTextZones(7);
620 if (!res.valid() || res.length() != 2) return res;
621 // check if the zone is empty ie. only a eol
622 RVNGInputStreamPtr input = const_cast<WPS8Text *>(this)->getInput();
623 long actPos = input->tell();
624 input->seek(res.begin(), librevenge::RVNG_SEEK_SET);
625 bool empty = libwps::read16(input) == 0xd;
626 input->seek(actPos, librevenge::RVNG_SEEK_SET);
627 if (empty)
628 {
629 m_state->setParsed(res, true);
630 return WPSEntry();
631 }
632 return res;
633 }
634
635 WPSEntry WPS8Text::getTextEntry() const
636 {
637 return m_state->getTextZones(1);
638 }
639
640 int WPS8Text::getNumTextZones() const
641 {
642 return int(m_state->m_textZones.size());
643 }
644 int WPS8Text::getTextZoneType(int strsId) const
645 {
646 if (strsId < 0 || strsId >= int(m_state->m_textZones.size()))
647 {
648 WPS_DEBUG_MSG(("WPS8Text::getTextZoneType: call with unkown id=%d\n", strsId));
649 return -1;
650 }
651 return m_state->m_textZones[size_t(strsId)].id();
652 }
653
654 void WPS8Text::readTextInCell(int strsId, int cellId)
655 {
656 if (!m_listener) return;
657 WPSEntry entry = m_state->getTCDZone(strsId, cellId);
658 if (entry.length()==0)
659 {
660 m_listener->insertCharacter(' ');
661 return;
662 }
663 readText(entry);
664 }
665
666 /**
667 * Read the range of the document text using previously-read
668 * formatting information, up to but excluding entry.end().
669 *
670 */
671 void WPS8Text::readText(WPSEntry const &entry)
672 {
673 if (!m_listener)
674 {
675 WPS_DEBUG_MSG(("WPS8Text::readText: called without listener!!!\n"));
676 return;
677 }
678 RVNGInputStreamPtr input = getInput();
679 m_state->setParsed(entry,true);
680 bool mainZone = entry.id()==1;
681 if (mainZone && mainParser().numColumns() > 1)
682 {
683 int numColumns = mainParser().numColumns();
684 if (m_listener->isSectionOpened())
685 m_listener->closeSection();
686 auto w = int(72*mainParser().pageWidth()/float(numColumns));
687 std::vector<int> colSize(size_t(numColumns), w);
688 m_listener->openSection(colSize, librevenge::RVNG_POINT);
689 }
690 int lastCId=-1, lastPId=-1; /* -2: nothing, -1: send default, >= 0: readId */
691 auto plcIt = m_FODList.begin();
692 while (plcIt != m_FODList.end() && plcIt->m_pos < entry.begin())
693 {
694 auto const &plc = *(plcIt++);
695 if (plc.m_type==DataFOD::ATTR_TEXT)
696 lastCId = plc.m_id;
697 else if (plc.m_type==DataFOD::ATTR_PARAG)
698 lastPId = plc.m_id;
699 }
700 int actualPage = 1;
701 WPS8TextStyle::FontData special;
702 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
703 while (!input->isEnd())
704 {
705 long pos = input->tell();
706 libwps::DebugStream f;
707 f << "TEXT:";
708 if (pos+1 >= entry.end())
709 break;
710 long finalPos = entry.end();
711 for (; plcIt != m_FODList.end(); ++plcIt)
712 {
713 DataFOD const &plc = *plcIt;
714 if (plc.m_pos < pos)
715 {
716 WPS_DEBUG_MSG(("WPS8Text::readText: ### problem with pos\n"));
717 continue;
718 }
719 if (plc.m_pos > pos)
720 {
721 if (plc.m_pos < finalPos)
722 finalPos = plc.m_pos;
723 break;
724 }
725 switch (plc.m_type)
726 {
727 case DataFOD::ATTR_TEXT:
728 {
729 lastCId = -2;
730 if (plc.m_id < 0)
731 f << "[C_]";
732 else
733 f << "[C" << plc.m_id << "]";
734 m_styleParser->sendFont(plc.m_id, special);
735 break;
736 }
737 case DataFOD::ATTR_PARAG:
738 {
739 lastPId = -2;
740 if (plc.m_id < 0)
741 f << "[P_]";
742 else
743 f << "[P" << plc.m_id << "]";
744 m_styleParser->sendParagraph(plc.m_id);
745 break;
746 }
747 case DataFOD::ATTR_PLC:
748 {
749 if (plc.m_id < 0) break;
750 if (plc.m_id >= int(m_state->m_plcList.size()))
751 {
752 f << "#[PLC"<< plc.m_id << "]";
753 WPS_DEBUG_MSG(("WPS8Text::readText: can not find plc %d\n",plc.m_id));
754 break;
755 }
756 auto const &thePLC = m_state->m_plcList[size_t(plc.m_id)];
757 f << "[" << thePLC << "]";
758 #ifdef DEBUG
759 // no sure, if we want to output this data
760 if (thePLC.m_type == WPS8TextInternal::BMKT)
761 {
762 if (m_state->m_bookmarkMap.find(pos) == m_state->m_bookmarkMap.end())
763 {
764 WPS_DEBUG_MSG(("WPS8Text::readText: can not find bookmark for pos %lX\n", static_cast<unsigned long>(pos)));
765 }
766 else
767 {
768 std::shared_ptr<WPSSubDocument> doc(new WPS8TextInternal::SubDocument(input, m_state->m_bookmarkMap.find(pos)->second.m_text));
769 m_listener->insertComment(doc);
770 }
771 }
772 #endif
773 break;
774 }
775 case DataFOD::ATTR_UNKN:
776 default:
777 break;
778 }
779 }
780 if (lastCId >= -1)
781 {
782 m_styleParser->sendFont(lastCId, special);
783 lastCId = -2;
784 }
785 if (lastPId >= -1)
786 {
787 m_styleParser->sendParagraph(lastPId);
788 lastPId = -2;
789 }
790 f << ":";
791 if ((finalPos-pos)%2)
792 {
793 WPS_DEBUG_MSG(("WPS8Text::readText: ### len is odd\n"));
794 throw libwps::ParseException();
795 }
796 while (!input->isEnd())
797 {
798 if (input->tell()+1 >= finalPos) break;
799
800 uint16_t readVal = libwps::readU16(input);
801 if (0x00 == readVal)
802 continue;
803 f << char(readVal);
804
805 switch (readVal)
806 {
807 case 0x9:
808 m_listener->insertTab();
809 break;
810
811 case 0x0A:
812 m_listener->insertEOL(true);
813 break;
814
815 case 0x0C:
816 if (mainZone)
817 mainParser().newPage(++actualPage);
818 else
819 {
820 WPS_DEBUG_MSG(("WPS8Text::readText: find page break in auxilliary zone\n"));
821 m_listener->insertEOL();
822 }
823 break;
824 case 0x0D:
825 m_listener->insertEOL();
826 break;
827
828 case 0x0E:
829 m_listener->insertBreak(WPS_COLUMN_BREAK);
830 break;
831
832 case 0x1E: // checkme: non-breaking hyphen
833 m_listener->insertUnicode(0x2011);
834 break;
835
836 case 0x1F: // non-breaking space ? ( old: optional breaking hyphen)
837 m_listener->insertUnicode(0xA0);
838 break;
839
840 case 0x23:
841 // TODO: fields, pictures, etc.
842 switch (special.m_type)
843 {
844 case WPS8TextStyle::FontData::T_None:
845 m_listener->insertCharacter('#');
846 break;
847 case WPS8TextStyle::FontData::T_Footnote:
848 case WPS8TextStyle::FontData::T_Endnote:
849 {
850 long fPos = input->tell()-2; // the note can be linked, so must retrieve the pos...
851 if (m_state->m_notesMap.find(fPos) == m_state->m_notesMap.end())
852 {
853 WPS_DEBUG_MSG(("WPS8Text::readText can not find notes for position : %lx\n", static_cast<unsigned long>(fPos)));
854 break;
855 }
856 auto const &note = *m_state->m_notesMap[fPos];
857 if (!note.m_corr || note.m_note) break;
858 WPSEntry nEntry = note.getCorrespondanceEntry(fPos);
859 std::shared_ptr<WPSSubDocument> doc(new WPS8TextInternal::SubDocument(input, *this, nEntry));
860 m_listener->insertNote(special.m_type==WPS8TextStyle::FontData::T_Footnote ? WPSContentListener::FOOTNOTE : WPSContentListener::ENDNOTE, doc);
861 break;
862 }
863 case WPS8TextStyle::FontData::T_Field:
864 switch (special.m_fieldType)
865 {
866 case WPS8TextStyle::FontData::F_PageNumber:
867 m_listener->insertField(WPSField(WPSField::PageNumber));
868 break;
869 case WPS8TextStyle::FontData::F_Date:
870 case WPS8TextStyle::FontData::F_Time:
871 {
872 std::string format = special.format();
873 if (format.length())
874 {
875 WPSField field(WPSField::Date);
876 field.m_DTFormat=format;
877 m_listener->insertField(field);
878 }
879 else
880 {
881 WPS_DEBUG_MSG(("WPS8Text::readText: unknown date/time format for position : %lX\n", static_cast<unsigned long>(pos)));
882 }
883 break;
884 }
885 case WPS8TextStyle::FontData::F_None:
886 default:
887 m_listener->insertUnicode(0x263B);
888 break;
889 }
890 special = WPS8TextStyle::FontData();
891 break;
892 default:
893 m_listener->insertUnicode(0x263B);
894 }
895 break;
896
897 case 0xfffc:
898 {
899 if (special.m_type != WPS8TextStyle::FontData::T_Object)
900 break;
901 long objPos = input->tell()-2;
902 if (m_state->m_objectMap.find(objPos) == m_state->m_objectMap.end())
903 {
904 WPS_DEBUG_MSG(("WPSText::readText can not find EOB for position : %lX\n", static_cast<unsigned long>(objPos)));
905 break;
906 }
907 auto const &obj = m_state->m_objectMap.find(objPos)->second;
908 if (obj.m_type == WPS8TextInternal::Object::Image)
909 mainParser().sendObject(obj.m_size, obj.m_id, true);
910 else if (obj.m_type == WPS8TextInternal::Object::Table)
911 mainParser().sendTable(obj.m_size, obj.m_id);
912 else
913 {
914 WPS_DEBUG_MSG(("WPSText::readText do not know how to send object in position : %lX\n", static_cast<unsigned long>(objPos)));
915 }
916 break;
917 }
918 default:
919 if (readVal < 28)
920 {
921 // do not add unprintable control which can create invalid odt file
922 WPS_DEBUG_MSG(("WPS8Text::readText(find unprintable character: ignored)\n"));
923 break;
924 }
925 m_listener->insertUnicode(uint32_t(readUTF16LE(input, finalPos, readVal)));
926 break;
927 }
928 }
929 ascii().addPos(pos);
930 ascii().addNote(f.str().c_str());
931 }
932 }
933
934 ////////////////////////////////////////////////////////////
935 // send unsent zone
936 ////////////////////////////////////////////////////////////
937 void WPS8Text::flushExtra()
938 {
939 if (!m_listener) return;
940 for (auto const &zone : m_state->m_textZones)
941 {
942 if (!zone.valid() || zone.id() == 2 || zone.id() ==3
943 || zone.isParsed())
944 continue;
945 readText(zone);
946 }
947 }
948
949 /**
950 * create the main structures
951 */
952 bool WPS8Text::readStructures()
953 {
954 auto const &nameTable = getNameEntryMap();
955 /* What is the total length of the text? */
956 auto pos = nameTable.lower_bound("TEXT");
957 if (nameTable.end() == pos)
958 {
959 WPS_DEBUG_MSG(("Works: error: no TEXT in header index table\n"));
960 return false;
961 }
962 else
963 m_textPositions = pos->second;
964
965 // determine the text subpart
966 pos = nameTable.find("STRS");
967 bool ok = nameTable.end() != pos && pos->second.hasType("PLC ");
968 if (ok)
969 {
970 std::vector<long> textPtrs;
971 std::vector<long> listValues;
972 m_state->m_textZones.resize(0);
973 ok = readPLC(pos->second, textPtrs, listValues, &WPS8Text::textZonesDataParser);
974 }
975 if (!ok)
976 {
977 WPS_DEBUG_MSG(("WPS8Text::readEntries: error: can not find the TEXT subdivision\n"));
978 m_state->m_textZones.resize(0);
979 // we create a false zone
980 WPSEntry zone = m_textPositions;
981 zone.setId(1);
982 m_state->m_textZones.push_back(zone);
983 }
984
985 if (!m_styleParser->readStructures())
986 return false;
987
988 // BMKT : text position of bookmark ?
989 pos = nameTable.lower_bound("BMKT");
990 while (pos != nameTable.end())
991 {
992 WPSEntry const &entry = pos++->second;
993 if (!entry.hasName("BMKT")) break;
994 if (!entry.hasType("PLC ")) continue;
995
996 std::vector<long> textPtrs;
997 std::vector<long> listValues;
998
999 if (!readPLC(entry, textPtrs, listValues,
1000 &WPS8Text::defDataParser, &WPS8Text::bmktEndDataParser)) continue;
1001 }
1002
1003 //
1004 // look for footnote end note
1005 //
1006 m_state->m_notesList.resize(0);
1007 pos = nameTable.lower_bound("FTN ");
1008 while (pos != nameTable.end())
1009 {
1010 WPSEntry const &entry = pos++->second;
1011 if (!entry.hasName("FTN ")) break;
1012 if (!entry.hasType("FTN ")) continue;
1013
1014 readNotes(entry);
1015 }
1016 pos = nameTable.lower_bound("EDN ");
1017 while (pos != nameTable.end())
1018 {
1019 WPSEntry const &entry = pos++->second;
1020 if (!entry.hasName("EDN ")) break;
1021 if (!entry.hasType("EDN ")) continue;
1022
1023 readNotes(entry);
1024 }
1025 createNotesCorrespondance();
1026
1027 // read EOBJ zone : object position
1028 pos = nameTable.lower_bound("EOBJ");
1029 while (pos != nameTable.end())
1030 {
1031 WPSEntry const &entry = pos++->second;
1032 if (!entry.hasName("EOBJ")) break;
1033 if (!entry.hasType("PLC ")) continue;
1034
1035 std::vector<long> textPtrs;
1036 std::vector<long> listValues;
1037 m_state->m_object = WPS8TextInternal::Object();
1038 if (!readPLC(entry, textPtrs, listValues, &WPS8Text::objectDataParser))
1039 continue;
1040 }
1041
1042 // TOKN : text position of token
1043 pos = nameTable.lower_bound("TOKN");
1044 while (pos != nameTable.end())
1045 {
1046 WPSEntry const &entry = pos++->second;
1047 if (!entry.hasName("TOKN")) break;
1048 if (!entry.hasType("PLC ")) continue;
1049
1050 std::vector<long> textPtrs;
1051 std::vector<long> listValues;
1052
1053 if (!readPLC(entry, textPtrs, listValues,
1054 &WPS8Text::defDataParser, &WPS8Text::tokenEndDataParser)) continue;
1055 }
1056
1057 // TCD : table separator
1058 pos = nameTable.lower_bound("TCD ");
1059 while (pos != nameTable.end())
1060 {
1061 WPSEntry const &entry = pos++->second;
1062 if (!entry.hasName("TCD ")) break;
1063 if (!entry.hasType("PLC ")) continue;
1064
1065 std::vector<long> textPtrs;
1066 std::vector<long> listValues;
1067 if (!readPLC(entry, textPtrs, listValues)) continue;
1068 m_state->m_tcdMap[entry.id()] = textPtrs;
1069 }
1070
1071 #ifdef DEBUG
1072 // read style sheet zone ? can be safety skipped in normal mode
1073 pos = nameTable.lower_bound("STSH");
1074 while (nameTable.end() != pos)
1075 {
1076 WPSEntry const &entry = pos++->second;
1077 if (!entry.hasName("STSH")) break;
1078 if (!entry.hasType("STSH")) continue;
1079
1080 if (!m_styleParser->readSTSH(entry))
1081 {
1082 ascii().addPos(entry.begin());
1083 ascii().addNote("STSH##");
1084 }
1085 }
1086 #endif
1087 return true;
1088 }
1089
1090 ////////////////////////////////////////////////////////////
1091 // basic strings functions:
1092 ////////////////////////////////////////////////////////////
1093 /* Read an UTF16 character in LE byte ordering, convert it. Courtesy of glib2 */
1094 long WPS8Text::readUTF16LE(RVNGInputStreamPtr input, long endPos, uint16_t firstC)
1095 {
1096 if (firstC >= 0xdc00 && firstC < 0xe000)
1097 {
1098 WPS_DEBUG_MSG(("WPS8Text::readUTF16LE: error: character = %i (0x%X)\n", firstC, unsigned(firstC)));
1099 return 0xfffd;
1100 }
1101 if (firstC >= 0xd800 && firstC < 0xdc00)
1102 {
1103 firstC = uint16_t(firstC - 0xd800);
1104 if (input->tell() == endPos)
1105 {
1106 WPS_DEBUG_MSG(("WPS8Text::readUTF16LE: error: find high surrogate without low\n"));
1107 return 0xfffd;
1108 }
1109 uint16_t lowVal = libwps::readU16(input);
1110 if (lowVal >= 0xdc00 && lowVal < 0xe000)
1111 {
1112 lowVal = uint16_t(lowVal - 0xdc00);
1113 return firstC* 0x400 + lowVal + 0x10000;
1114 }
1115 else
1116 {
1117 WPS_DEBUG_MSG(("WPS8Text::readUTF16LE: error: surrogate character = %i (0x%X)\n", firstC, unsigned(firstC)));
1118 return 0xfffd;
1119 }
1120 }
1121 /** CHECKME: for Symbol font, we must probably convert 0xF0xx and 0x00xx in a 0x03yy symbol :-~ */
1122 if (firstC>=28) return firstC;
1123 WPS_DEBUG_MSG(("WPS8Text::readUTF16LE: error: character = %i (0x%X)\n", firstC, unsigned(firstC)));
1124 return 0xfffd;
1125 }
1126
1127 bool WPS8Text::readString(RVNGInputStreamPtr input, long page_size,
1128 librevenge::RVNGString &res)
1129 {
1130 res = "";
1131 long page_offset = input->tell();
1132 long endString = page_offset + page_size;
1133
1134 while (input->tell() < endString-1 && !input->isEnd())
1135 {
1136 uint16_t val = libwps::readU16(input);
1137 if (!val) break;
1138
1139 long unicode = readUTF16LE(input, endString, val);
1140 if (unicode != 0xfffd) libwps::appendUnicode(val, res);
1141 }
1142 return true;
1143 }
1144
1145 ////////////////////////////////////////////////////////////
1146 // end note or footnote / FTN code
1147 ////////////////////////////////////////////////////////////
1148 bool WPS8Text::readNotes(WPSEntry const &entry)
1149 {
1150 if (!entry.hasType(entry.name()))
1151 {
1152 WPS_DEBUG_MSG(("WPS8Text::readNotes: warning: readNotes name=%s, type=%s\n",
1153 entry.name().c_str(), entry.type().c_str()));
1154 return false;
1155 }
1156
1157 if (entry.length() < 20)
1158 {
1159 WPS_DEBUG_MSG(("WPS8Text::readNotes: warning: readNotes length=0x%ld\n", entry.length()));
1160 return false;
1161 }
1162
1163
1164 RVNGInputStreamPtr input = getInput();
1165 long debPos = entry.begin();
1166 long length = entry.length();
1167 long endPos = entry.end();
1168 input->seek(debPos, librevenge::RVNG_SEEK_SET);
1169
1170 auto zone = long(libwps::readU32(input));
1171 auto numTZones = int(m_state->m_textZones.size());
1172 if (zone < 0 || zone >= numTZones)
1173 {
1174 WPS_DEBUG_MSG(("WPS8Text::readNotes: warning: readNotes unknown zone: %d, numZones = %d\n", int(zone), numTZones));
1175 return false;
1176 }
1177
1178 // CHECK ME, it is the number of notes or only an unknow field
1179 auto nNotes = int(libwps::read32(input));
1180 if (nNotes < 0 || 16+4*(3*long(nNotes)+1) > length)
1181 {
1182 WPS_DEBUG_MSG(("WPS8Text::readNotes: warning: readNotes number=%d\n", nNotes));
1183 return false;
1184 }
1185 libwps::DebugStream f;
1186
1187 entry.setParsed();
1188 WPS8TextInternal::Notes notes;
1189 if (strncmp(entry.name().c_str(),"FTN ",3)==0)
1190 notes.m_type = WPSContentListener::FOOTNOTE;
1191 else
1192 notes.m_type = WPSContentListener::ENDNOTE;
1193 notes.m_zoneNote = entry.id();
1194 notes.m_zoneCorr = int(zone);
1195
1196 f << "unkn=" << libwps::readU32(input) << ", fl=(";
1197
1198 for (int i = 0; i < 4; i++) f << int(libwps::read8(input)) << ",";
1199 f << "),";
1200
1201 long begTPos = m_textPositions.begin();
1202 long endTPos = m_textPositions.end();
1203
1204 if (entry.id() >= 0 && entry.id() < int(m_state->m_textZones.size()))
1205 {
1206 begTPos = m_state->m_textZones[size_t(entry.id())].begin();
1207 endTPos = m_state->m_textZones[size_t(entry.id())].end();
1208 }
1209 else
1210 {
1211 WPS_DEBUG_MSG(("WPS8Text::readNotes: unknown entry id = %d\n", int(entry.id())));
1212 }
1213 auto &notesPos = notes.m_positions;
1214 for (int i = 0; i <= nNotes; i++)
1215 {
1216 long ptr = begTPos + 2*long(libwps::read32(input));
1217 if (ptr >= begTPos && ptr <= endTPos) notesPos.push_back(ptr);
1218 else f << "###pbptr" << i << "=" << std::hex << ptr << std::dec << ",";
1219 }
1220 m_state->m_notesList.push_back(notes);
1221
1222 ascii().addPos(debPos);
1223 libwps::DebugStream f2;
1224 f2 << notes << "," << f.str();
1225 ascii().addNote(f2.str().c_str());
1226
1227 long pos;
1228 for (int i = 0; i < nNotes; i++)
1229 {
1230 pos = input->tell();
1231 f.str("");
1232 f << entry.name() << i << ":";
1233 auto type = int(libwps::read16(input));
1234 switch (type)
1235 {
1236 case 0:
1237 f << "alpha,";
1238 break;
1239 case 1:
1240 f << "numeric,";
1241 break;
1242 default:
1243 f << "#type=" << type << ",";
1244 break;
1245 }
1246 f << "id=" << libwps::read16(input) << ",";
1247 auto val = long(libwps::read32(input));
1248 if (val != -1) f << "label" << val << ",";
1249 ascii().addPos(pos);
1250 ascii().addNote(f.str().c_str());
1251 }
1252
1253 // no sure what we can do of the end of the data ?
1254 pos = input->tell();
1255 if (pos+12 > endPos)
1256 {
1257 WPS_DEBUG_MSG(("WPS8Text::readNotes: unexpected end size\n"));
1258 f.str("");
1259 f << entry.name() << "[###End]";
1260 ascii().addPos(pos);
1261 ascii().addNote(f.str().c_str());
1262 return true;
1263 }
1264 f.str("");
1265 f << entry.name() << "[A]:";
1266 for (int i = 0; i < 2; i++) // 2 size ?
1267 {
1268 f << libwps::read32(input) << ",";
1269 }
1270 // fixme: how to read the end of data and find the labels ???
1271 ascii().addDelimiter(input->tell(),'|');
1272 ascii().addPos(pos);
1273 ascii().addNote(f.str().c_str());
1274 ascii().addPos(endPos);
1275 ascii().addNote("_");
1276
1277 return true;
1278 }
1279
1280 void WPS8Text::createNotesCorrespondance()
1281 {
1282 using WPS8TextInternal::Notes;
1283 auto &notesList = m_state->m_notesList;
1284 if (notesList.empty()) return;
1285
1286 // a map to store notes and their correspondance
1287 std::map<Notes const *, int, Notes::Compare> nMap;
1288 auto &notesMap = m_state->m_notesMap;
1289
1290 for (auto const &note : notesList)
1291 {
1292 int nSameNote = 0;
1293 auto nIt = nMap.find(&note);
1294 if (nIt != nMap.end())
1295 {
1296 WPS_DEBUG_MSG(("WPS8Text::createNotesCorrespondance: find some dupplicated notes\n"));
1297 nSameNote = nIt->second+1;
1298 }
1299 nMap[&note] = nSameNote;
1300 }
1301
1302 for (auto const &it : nMap)
1303 {
1304 auto &notes = const_cast<Notes &>(*it.first);
1305 if (it.second != 0)
1306 continue;
1307
1308 WPSEntry entry = getEntry(notes.m_zoneCorr);
1309 if (entry.id() != 2 && entry.id() != 3)
1310 // not the reference zone
1311 continue;
1312
1313 // notes in notes are not actually accepted
1314 entry = getEntry(notes.m_zoneNote);
1315 if (entry.id() == 2 || entry.id() == 3)
1316 {
1317 WPS_DEBUG_MSG(("WPS8Text::createNotesCorrespondance: find a note contained in a note, ignored\n"));
1318 continue;
1319 }
1320
1321 Notes correspondingNotes;
1322 correspondingNotes.m_type = notes.m_type;
1323 correspondingNotes.m_zoneNote = notes.m_zoneCorr;
1324 correspondingNotes.m_zoneCorr = notes.m_zoneNote;
1325 auto nIt2 = nMap.find(&correspondingNotes);
1326 if (nIt2 == nMap.end())
1327 {
1328 WPS_DEBUG_MSG(("WPS8Text::createNotesCorrespondance: can not find the corresponding note\n"));
1329 continue;
1330 }
1331 int nNotes = int(notes.m_positions.size())-1;
1332 if (int(nIt2->first->m_positions.size())-1 != nNotes || nNotes <= 0)
1333 {
1334 WPS_DEBUG_MSG(("WPS8Text::createNotesCorrespondance: the notes' lists have not the same number of elements \n"));
1335 continue;
1336 }
1337
1338 if (nIt2->second != 0) continue; // a duplicated notes
1339 auto &notesCor = const_cast<Notes &>(*nIt2->first);
1340 notes.m_corr = &notesCor;
1341 notes.m_note = false;
1342 notesCor.m_corr = &notes;
1343 notesCor.m_note = true;
1344
1345 /** we can now creates the link position -> notes */
1346 for (size_t i = 0; i < size_t(nNotes); i++)
1347 {
1348 notesMap[notes.m_positions[i]] = &notes;
1349 notesMap[notesCor.m_positions[i]] = &notesCor;
1350 }
1351
1352 m_state->m_textZones[size_t(notes.m_zoneCorr)].setParsed(true);
1353 }
1354 }
1355
1356 ////////////////////////////////////////////////////////////
1357 // plc: default, strs
1358 ////////////////////////////////////////////////////////////
1359 bool WPS8Text::defDataParser
1360 (long, long, int, WPS8Struct::FileData const &data, std::string &mess)
1361 {
1362 (void)this; // Make sure that this function can be used as a DataParser.
1363 mess = "";
1364 libwps::DebugStream f;
1365 if (!data.isRead() && !data.readArrayBlock() && data.m_recursData.size() == 0)
1366 {
1367 // we read nothing -> we stop
1368 f << ", " << data;
1369 mess = f.str();
1370 return true;
1371 }
1372
1373 if (data.m_recursData.empty()) return true;
1374
1375 f << "{";
1376 for (auto const &dt : data.m_recursData) f << dt << ",";
1377 f << "}";
1378
1379 mess = f.str();
1380 return true;
1381 }
1382
1383 ////////////////////////////////////////////////////////////
1384 // Text zones/ STRS zone
1385 ////////////////////////////////////////////////////////////
1386 bool WPS8Text::textZonesDataParser
1387 (long bot, long eot, int /*nId*/, WPS8Struct::FileData const &data, std::string &mess)
1388 {
1389 mess = "";
1390 if (bot < m_textPositions.begin() || eot > m_textPositions.end()) return false;
1391
1392 libwps::DebugStream f;
1393 if (!data.isRead() && !data.readArrayBlock() && data.m_recursData.empty())
1394 {
1395 f << ", " << data;
1396 mess = f.str();
1397 WPS_DEBUG_MSG(("WPS8Text::textZonesDataParser: unknown structure\n"));
1398 return false;
1399 }
1400
1401 bool idSet = false;
1402 int id = -1;
1403
1404 for (auto const &dt : data.m_recursData)
1405 {
1406 if (dt.isBad()) continue;
1407 if (dt.id()!=0 || dt.type() != 0x22)
1408 {
1409 WPS_DEBUG_MSG(("WPS8Text::textZonesDataParser: unexpected id for %d[%d]\n", data.id(), data.type()));
1410 f << "###" << data << ",";
1411 continue;
1412 }
1413 /* 1=Main, 2=Footnote, 3=Endnote, 5=Contact/Section/Table/Column/..,
1414 6=Header, 7=Footer */
1415 f << "id=" << dt.m_value << ",";
1416 id = int(dt.m_value);
1417 idSet = true;
1418 }
1419
1420 if (!idSet)
1421 {
1422 size_t numZ = m_state->m_textZones.size();
1423 if (numZ == 0) WPS_DEBUG_MSG(("WPS8Text::textZonesDataParser: error: readSTRS with no type\n"));
1424 else
1425 {
1426 id = m_state->m_textZones[numZ-1].id();
1427 f << "rId=" << id;
1428 }
1429 }
1430
1431 mess = f.str();
1432
1433 WPSEntry zone;
1434 zone.setBegin(bot);
1435 zone.setEnd(eot);
1436 zone.setType("Text");
1437 zone.setId(id);
1438 m_state->m_textZones.push_back(zone);
1439
1440 return true;
1441 }
1442
1443 ////////////////////////////////////////////////////////////
1444 // Bookmark/ BMKT zone
1445 //
1446 // Note: I only find in very few file -> to be checked
1447 ////////////////////////////////////////////////////////////
1448 bool WPS8Text::bmktEndDataParser(long endPage, std::vector<long> const &textPtrs)
1449 {
1450 RVNGInputStreamPtr input=getInput();
1451 typedef WPS8TextInternal::Bookmark Bookmark;
1452
1453 int numBmkt = int(textPtrs.size())-1;
1454 if (numBmkt <= 0) return false;
1455
1456 libwps::DebugStream f;
1457
1458 long pos = input->tell();
1459 ascii().addPos(pos);
1460
1461 f << "BMKT(id)=(";
1462 std::vector<Bookmark> listBmk;
1463
1464 for (int i = 0; i < numBmkt; i++)
1465 {
1466 Bookmark bmk;
1467
1468 pos = input->tell();
1469 if (input->tell() + 4 > endPage) return false;
1470
1471 auto id = int(libwps::read32(input));
1472 f << id << ",";
1473 if (id < 0 || (id > numBmkt && id > 100))
1474 {
1475 WPS_DEBUG_MSG(("WPS8Text::bmktEndDataParser: odd index =%d\n", id));
1476 input->seek(pos, librevenge::RVNG_SEEK_SET);
1477 return false;
1478 }
1479 bmk.m_id = id;
1480 listBmk.push_back(bmk);
1481 }
1482 f << ")";
1483
1484 auto sz1 = int(libwps::readU32(input));
1485 pos = input->tell();
1486 if (sz1 < 4*numBmkt || pos+1+sz1 > endPage)
1487 {
1488 WPS_DEBUG_MSG(("WPS8Text::bmktEndDataParser: pb with sz1 =%d\n", sz1));
1489 input->seek(pos, librevenge::RVNG_SEEK_SET);
1490 return false;
1491 }
1492 f << ", Size(StrZone) = " << sz1;
1493 ascii().addNote(f.str().c_str());
1494
1495 // ------ Problematic part -------------
1496 ascii().addPos(input->tell());
1497 f.str("");
1498 f << "BMKT(##unkn):";
1499 f << libwps::read32(input) <<",";
1500 f << "dim?=" << float(libwps::read32(input))/914400.f <<"," << std::hex;
1501 while (input->tell() < endPage-sz1 && !input->isEnd())
1502 f << libwps::read16(input) << ",";
1503 f << std::dec;
1504 ascii().addNote(f.str().c_str());
1505 // File1= 0b000000 (N?) - 4c891700:00000000:001fcd00 when numBmkt=11
1506 // File2= 01000000 (N?) - 988f1200:00000000:0045dd01 when numBmkt=1
1507 // probably int, dim(*914400), int, short, short
1508 // ------ end of problematic part -------------
1509
1510 input->seek(endPage-sz1, librevenge::RVNG_SEEK_SET);
1511
1512 pos = input->tell();
1513 ascii().addPos(pos);
1514 f.str("");
1515 f << "BMKT(strPos)=(";
1516
1517 std::vector<long> strPos;
1518 for (int i = 0; i < numBmkt; i++)
1519 {
1520 long sPos = pos+long(libwps::read32(input));
1521
1522 if (sPos > endPage)
1523 {
1524 WPS_DEBUG_MSG(("WPS8Text::bmktEndDataParser: pb with %dth pos=%ld\n", i, sPos));
1525 return false;
1526 }
1527 strPos.push_back(sPos);
1528 f << std::hex << sPos << ",";
1529 }
1530 f << ")";
1531
1532 ascii().addNote(f.str().c_str());
1533 ascii().addPos(input->tell());
1534
1535 for (size_t i = 0; i < size_t(numBmkt); i++)
1536 {
1537 pos = strPos[i];
1538 f.str("");
1539 f << "BMKT(" << std::dec << i << ")=";
1540 if (pos == endPage) f << "_"; // can this happens ?
1541 else
1542 {
1543 input->seek(pos, librevenge::RVNG_SEEK_SET);
1544 librevenge::RVNGString val;
1545 auto sz = int(libwps::readU16(input));
1546 if (pos + 2+ 2*sz > endPage || !readString(input, 2*sz, val))
1547 {
1548 WPS_DEBUG_MSG(("WPS8Text::bmktEndDataParser: pb with %dth string size=%d\n", int(i), sz));
1549 return false;
1550 }
1551 listBmk[i].m_text=val;
1552 f << "\"" << val.cstr() << "\"";
1553 }
1554 ascii().addPos(strPos[i]);
1555 ascii().addNote(f.str().c_str());
1556 }
1557 input->seek(endPage, librevenge::RVNG_SEEK_SET);
1558
1559 // ok, we store the bookmark and we create the plc and the data fod ...
1560 std::vector<DataFOD> fods;
1561 for (size_t t = 0; t < size_t(numBmkt); t++)
1562 {
1563 if (m_state->m_bookmarkMap.find(textPtrs[t]) != m_state->m_bookmarkMap.end())
1564 {
1565 WPS_DEBUG_MSG(("WPS8Text::bmktEndDataParser: warning: already a bkmt in position %lx\n", static_cast<unsigned long>(textPtrs[t])));
1566 continue;
1567 }
1568 else m_state->m_bookmarkMap[textPtrs[t]] = listBmk[t];
1569 auto newId = int(m_state->m_plcList.size());
1570
1571 WPS8TextInternal::DataPLC plc;
1572 plc.m_type = WPS8TextInternal::BMKT;
1573 plc.m_name = "BOOKMARK";
1574 f.str("");
1575 f << listBmk[t];
1576 plc.m_error = f.str();
1577 m_state->m_plcList.push_back(plc);
1578
1579 DataFOD fod;
1580 fod.m_type = DataFOD::ATTR_PLC;
1581 fod.m_pos = textPtrs[t];
1582 fod.m_id = newId;
1583
1584 fods.push_back(fod);
1585 }
1586
1587 if (fods.size())
1588 m_FODList = mergeSortedFODLists(m_FODList, fods);
1589
1590 return true;
1591
1592 }
1593
1594 ////////////////////////////////////////////////////////////
1595 // Object/ Eobj code
1596 ////////////////////////////////////////////////////////////
1597 bool WPS8Text::objectDataParser(long bot, long /*eot*/, int /*id*/,
1598 WPS8Struct::FileData const &data, std::string &mess)
1599 {
1600 typedef WPS8TextInternal::Object Object;
1601 if (m_state->m_objectMap.find(bot) != m_state->m_objectMap.end())
1602 {
1603 WPS_DEBUG_MSG(("WPS8Text::objectDataParser: error: eobj already exists in this position\n"));
1604 return true;
1605 }
1606
1607 Object obj = m_state->m_object;
1608 obj.m_error="";
1609
1610 mess = "";
1611
1612 long val[5] = { 0, 0, 0, 0, 0 };
1613 bool setVal[5] = { false, false, false, false, false };
1614 libwps::DebugStream f;
1615 for (auto const &dt : data.m_recursData)
1616 {
1617 if (dt.isBad()) continue;
1618 if (m_state->m_objectTypes.find(dt.id())==m_state->m_objectTypes.end())
1619 {
1620 WPS_DEBUG_MSG(("WPS8Text::objectDataParser: unexpected id %d\n", dt.id()));
1621 f << "###" << dt << ",";
1622 continue;
1623 }
1624 if (m_state->m_objectTypes.find(dt.id())->second != dt.type())
1625 {
1626 WPS_DEBUG_MSG(("WPS8Text::objectDataParser: unexpected type for %d\n", dt.id()));
1627 f << "###" << dt << ",";
1628 continue;
1629 }
1630 val[dt.id()] = dt.m_value;
1631 setVal[dt.id()] = true;
1632 }
1633 obj.m_error = f.str();
1634
1635 if (setVal[0])
1636 {
1637 if (val[0] == 2) obj.m_type = Object::Image;
1638 else if (val[0] == 3) obj.m_type = Object::Table;
1639 else
1640 {
1641 static bool first = true;
1642 if (first)
1643 {
1644 first = false;
1645 WPS_DEBUG_MSG(("WPS8Text::objectDataParser: unknown type: %ld\n", val[0]));
1646 }
1647 obj.m_type = int(-1 - val[0]);
1648 }
1649 }
1650 if (setVal[3]) obj.m_id = int(val[3]);
1651
1652 if (setVal[1]) obj.m_size.setX(float(val[1])/914400.f);
1653 if (setVal[2]) obj.m_size.setY(float(val[2])/914400.f);
1654
1655 if (setVal[4]) obj.m_unknown = val[4];
1656
1657 m_state->m_objectMap[bot] = m_state->m_object = obj;
1658
1659 f.str("");
1660 f << obj;
1661 mess = f.str();
1662
1663 return true;
1664 }
1665
1666 ////////////////////////////////////////////////////////////
1667 // Token/ Tokn zone
1668 ////////////////////////////////////////////////////////////
1669 bool WPS8Text::tokenEndDataParser(long endPage, std::vector<long> const &textPtrs)
1670 {
1671 int numTokn = int(textPtrs.size())-1;
1672 if (numTokn <= 0) return false;
1673
1674 RVNGInputStreamPtr input = getInput();
1675 libwps::DebugStream f;
1676 WPS8TextInternal::Token tokn;
1677 std::vector<WPS8TextInternal::Token> listToken;
1678
1679 for (size_t i = 0; i < size_t(numTokn); i++)
1680 {
1681 long pos = input->tell();
1682 if (input->tell() + 2 > endPage) return false;
1683
1684 auto sz = int(libwps::read16(input));
1685 if (sz < 2 || pos+sz > endPage)
1686 {
1687 input->seek(pos, librevenge::RVNG_SEEK_SET);
1688 return false;
1689 }
1690
1691 WPS8Struct::FileData data;
1692 std::string error;
1693 if (!readBlockData(input, pos+sz, data, error) && data.m_recursData.size() == 0)
1694 {
1695 input->seek(pos, librevenge::RVNG_SEEK_SET);
1696 return false;
1697 }
1698
1699 tokn.m_error ="";
1700 f.str("");
1701 for (auto const &dt : data.m_recursData)
1702 {
1703 if (dt.isBad()) continue;
1704 if (dt.id()<0 || dt.id()>2 || dt.type()!=0x22)
1705 {
1706 WPS_DEBUG_MSG(("WPS8Text::tokenEndDataParser[A]: unexpected id %d[%d]\n", data.id(), data.type()));
1707 f << "###" << dt << ",";
1708 continue;
1709 }
1710 switch (dt.id())
1711 {
1712 case 0: // some unknown flags ? link=0x140, pageNumber=0, unknown(6)=0x400
1713 tokn.m_unknown=int(dt.m_value);
1714 break;
1715 case 1: // looks like the text size
1716 tokn.m_textLength=int(dt.m_value);
1717 break;
1718 case 2: // -2=link, -5=page number, -6=special char
1719 switch (dt.m_value)
1720 {
1721 case -2:
1722 tokn.m_type = WPSField::Link;
1723 break;
1724 case -5:
1725 tokn.m_type = WPSField::PageNumber;
1726 break;
1727 case -6:
1728 f << "special[char],";
1729 break;
1730 default:
1731 WPS_DEBUG_MSG(("WPS8Text::tokenEndDataParser: unknown type=%d\n", int(dt.m_value)));
1732 f << "###type=" << dt.m_value << ",";
1733 }
1734 break;
1735 default:
1736 f << "###id=" << dt.id() << ",";
1737 break;
1738 }
1739 }
1740
1741 if (!error.empty()) f << error;
1742 if (!f.str().empty()) tokn.m_error = f.str();
1743
1744 listToken.push_back(tokn);
1745
1746 f.str("");
1747 f << std::dec << "TOKN/PLC" << i << ": " << tokn;
1748 ascii().addPos(pos);
1749 ascii().addNote(f.str().c_str());
1750 }
1751
1752 std::vector<int> idString;
1753 idString.resize(size_t(numTokn), -1);
1754
1755 int numFollow = 0;
1756 for (int i = 0; i < numTokn; i++)
1757 {
1758 long pos = input->tell();
1759 if (input->tell() + 2 > endPage) return false;
1760
1761 WPS8Struct::FileData data;
1762 auto sz = int(libwps::read16(input));
1763 if (sz < 2 || pos+sz > endPage)
1764 {
1765 input->seek(pos, librevenge::RVNG_SEEK_SET);
1766 return false;
1767 }
1768
1769 std::string error;
1770 if (!readBlockData(input, pos+sz, data, error) && data.m_recursData.size() == 0)
1771 {
1772 input->seek(pos, librevenge::RVNG_SEEK_SET);
1773 return false;
1774 }
1775
1776 f.str("");
1777 f << "TOKN(PLC" << i << "-a): ";
1778
1779 bool idSet = false;
1780 int id = -1;
1781 for (auto const &dt : data.m_recursData)
1782 {
1783 if (dt.isBad()) continue;
1784 if (dt.id() != 0 || dt.type()!=0x22)
1785 {
1786 WPS_DEBUG_MSG(("WPS8Text::tokenEndDataParser[B]: unexpected id %d[%d]\n", data.id(), data.type()));
1787 f << "###" << dt << ",";
1788 continue;
1789 }
1790
1791 id = int(dt.m_value);
1792 if (id >= 0)
1793 {
1794 idString[size_t(i)] = id;
1795 f << "strings" << id << ","; // id of the following strs
1796 }
1797 else if (id == -1) f << "_,"; // no string
1798 else f << "###id=" << id << ","; // exits ???
1799 idSet = true;
1800 }
1801
1802 if (idSet && id >= 0) numFollow++;
1803 if (!error.empty()) f << error;
1804
1805 ascii().addPos(pos);
1806 ascii().addNote(f.str().c_str());
1807 }
1808
1809 std::vector<long> sPtrs;
1810 if (numFollow != 0)
1811 {
1812 // read the strings header
1813 long pos = input->tell();
1814 if (pos+20 > endPage) return false;
1815 f.str("");
1816 f << "TOKN(strings):";
1817 auto unkn = long(libwps::read32(input));
1818 if (unkn+20+pos != endPage)
1819 f << "unkn=" << std::hex << unkn+20+pos << std::dec << ",";
1820 auto N = int(libwps::read32(input));
1821 if (N != numFollow) f << "###";
1822 f << "N=" << N << ",";
1823 // 8760a4 | b37464 | 6092ea37 | 6105db0e | ffffffff
1824 f << std::hex << libwps::readU32(input) << std::dec << ",";
1825 for (int i = 0; i <2; i++)
1826 {
1827 auto val = int(libwps::read32(input));
1828 if (val) f << "###f" << i << "=" << val << ",";
1829 }
1830 f << "ptr=(";
1831
1832 if (N < 0 || N > std::numeric_limits<int>::max()/4 || 4*N > endPage-pos+20)
1833 {
1834 input->seek(pos, librevenge::RVNG_SEEK_SET);
1835 return false;
1836 }
1837
1838 for (int i = 0; i < N; i++)
1839 {
1840 auto val = long(libwps::read32(input));
1841 if (val < 4*N)
1842 {
1843 input->seek(pos, librevenge::RVNG_SEEK_SET);
1844 return false;
1845 }
1846 val += pos+20;
1847 if (val > endPage)
1848 {
1849 input->seek(pos, librevenge::RVNG_SEEK_SET);
1850 return false;
1851 }
1852 sPtrs.push_back(val);
1853 f << std::hex << val << ",";
1854 }
1855 f << ")" << std::dec;
1856
1857 ascii().addPos(pos);
1858 ascii().addNote(f.str().c_str());
1859 }
1860
1861 size_t numStrings = sPtrs.size();
1862 for (size_t i = 0; i < numStrings; i++)
1863 {
1864 long pos = sPtrs[i];
1865 input->seek(pos, librevenge::RVNG_SEEK_SET);
1866 auto sz = int(libwps::read16(input));
1867 if (sz < 0 || pos+2*sz+2 > endPage) return false;
1868 librevenge::RVNGString str;
1869 readString(input, 2*sz, str);
1870
1871 bool find = false;
1872 for (size_t t = 0; t < size_t(numTokn); t++)
1873 {
1874 if (idString[t] != int(i)) continue;
1875 find = true;
1876 listToken[t].m_text = str;
1877 }
1878 f.str("");
1879 f << "TOKN/string" << i << ":" << str.cstr();
1880 if (!find) f << ", ###not find";
1881 ascii().addPos(pos);
1882 ascii().addNote(f.str().c_str());
1883 }
1884
1885 // ok, we store the token and we create the plc and the data fod ...
1886 std::vector<DataFOD> fods;
1887 for (size_t t = 0; t < size_t(numTokn); t++)
1888 {
1889 if (m_state->m_tokenMap.find(textPtrs[t]) != m_state->m_tokenMap.end())
1890 {
1891 WPS_DEBUG_MSG(("WPS8Text::tokenEndDataParser: warning: already a tokn in position %lx\n", static_cast<unsigned long>(textPtrs[t])));
1892 continue;
1893 }
1894 else m_state->m_tokenMap[textPtrs[t]] = listToken[t];
1895 auto newId = int(m_state->m_plcList.size());
1896
1897 WPS8TextInternal::DataPLC plc;
1898 plc.m_type = WPS8TextInternal::TOKEN;
1899 plc.m_name = "TOKEN";
1900 f.str("");
1901 f << listToken[t];
1902 plc.m_error = f.str();
1903 m_state->m_plcList.push_back(plc);
1904
1905 DataFOD fod;
1906 fod.m_type = DataFOD::ATTR_PLC;
1907 fod.m_pos = textPtrs[t];
1908 fod.m_id = newId;
1909
1910 fods.push_back(fod);
1911 }
1912
1913 if (fods.size())
1914 m_FODList = mergeSortedFODLists(m_FODList, fods);
1915
1916 return true;
1917 }
1918
1919 /////////////////////////////////////////////////////////////////////
1920 // VERY LOW LEVEL (PLC) //
1921 /////////////////////////////////////////////////////////////////////
1922
1923 /** Internal and low level: the structures of a WPS4Text used to parse PLC*/
1924 namespace WPS8PLCInternal
1925 {
1926 /** Internal and low level: the PLC different types and their structures */
1927 struct PLC
1928 {
1929 /** the PLC types */
1930 typedef enum WPS8TextInternal::PLCType PLCType;
1931 /** the way to define the text positions
1932 *
1933 * - P_ABS: absolute position,
1934 * - P_MREL: position are relative to the beginning of the main text offset,
1935 * - P_MINCR: position are the length of text consecutive zones in the main text offset,
1936 * - P_ZREL: position are relative to the beginning of a specific text offset,
1937 * - P_ZINCR: position are the length of text consecutive zones in a specific text offset
1938 */
1939 typedef enum { P_ABS=0, P_MREL, P_ZREL, P_MINCR, P_ZINCR, P_UNKNOWN} Position;
1940 /** the type of the content
1941 *
1942 * - T_CST: size is constant
1943 * - T_STRUCT: a structured type ( which unknown size)
1944 * - T_COMPLEX: a complex way to define content in many parts
1945 */
1946 typedef enum { T_CST=0, T_STRUCT, T_COMPLEX, T_UNKNOWN} Type;
1947
1948 //! constructor
1949 explicit PLC(PLCType w= WPS8TextInternal::Unknown, Position p=P_UNKNOWN, Type t=T_UNKNOWN)
1950 : m_type(w)
1951 , m_pos(p)
1952 , m_contentType(t) {}
1953
1954 //! PLC type
1955 PLCType m_type;
1956 //! the way to define the text positions
1957 Position m_pos;
1958 //! the type of the content
1959 Type m_contentType;
1960 };
1961
1962 KnownPLC::KnownPLC() : m_knowns()
1963 {
1964 createMapping();
1965 }
1966
1967 KnownPLC::~KnownPLC()
1968 {
1969 }
1970
1971 PLC KnownPLC::get(std::string const &name)
1972 {
1973 auto pos = m_knowns.find(name);
1974 if (pos == m_knowns.end()) return PLC();
1975 return pos->second;
1976 }
1977
1978 void KnownPLC::createMapping()
1979 {
1980 m_knowns["BTEP"]=PLC(WPS8TextInternal::BTE,PLC::P_ABS, PLC::T_CST);
1981 m_knowns["BTEC"]=PLC(WPS8TextInternal::BTE,PLC::P_ABS, PLC::T_CST);
1982 m_knowns["TCD "]=PLC(WPS8TextInternal::TCD,PLC::P_ZREL, PLC::T_CST);
1983 m_knowns["EOBJ"]=PLC(WPS8TextInternal::OBJECT,PLC::P_ZREL, PLC::T_STRUCT); // or maybe P_MREL
1984 m_knowns["STRS"]=PLC(WPS8TextInternal::STRS, PLC::P_MINCR, PLC::T_STRUCT);
1985 m_knowns["TOKN"]=PLC(WPS8TextInternal::TOKEN, PLC::P_ZREL, PLC::T_COMPLEX);
1986 m_knowns["BMKT"]=PLC(WPS8TextInternal::BMKT, PLC::P_ZREL, PLC::T_COMPLEX); // or maybe P_MREL
1987 }
1988 }
1989
1990 bool WPS8Text::readPLC
1991 (WPSEntry const &entry,
1992 std::vector<long> &textPtrs, std::vector<long> &listValues,
1993 WPS8Text::DataParser parser, WPS8Text::EndDataParser endParser)
1994 {
1995 RVNGInputStreamPtr input = getInput();
1996 if (!entry.hasType("PLC "))
1997 {
1998 WPS_DEBUG_MSG(("WPS8Text::readPLC: warning: PLC name=%s, type=%s\n",
1999 entry.name().c_str(), entry.type().c_str()));
2000 return false;
2001 }
2002
2003 long page_offset = entry.begin();
2004 long length = entry.length();
2005 long endPage = entry.end();
2006 if (length < 16)
2007 {
2008 WPS_DEBUG_MSG(("WPS8Text::readPLC: warning: PLC length=0x%lx\n", static_cast<unsigned long>(length)));
2009 return false;
2010 }
2011
2012 input->seek(page_offset, librevenge::RVNG_SEEK_SET);
2013 unsigned nPLC = libwps::readU32(input);
2014 unsigned dataSz = libwps::readU32(input);
2015 libwps::DebugStream f;
2016 f << "PLC: N=" << nPLC << ", dSize=" << dataSz;
2017 auto plcType = m_state->m_knownPLC.get(entry.name());
2018
2019 if (plcType.m_contentType == WPS8PLCInternal::PLC::T_UNKNOWN && long(4ul*nPLC+16 + dataSz *nPLC) == length)
2020 plcType.m_contentType = WPS8PLCInternal::PLC::T_CST;
2021 else if (plcType.m_contentType == WPS8PLCInternal::PLC::T_CST)
2022 {
2023 if (long(4ul*nPLC+16 + dataSz *nPLC) != length)
2024 {
2025 WPS_DEBUG_MSG(("WPS8Text::readPLC: warning: unknown data size\n"));
2026 dataSz = 0;
2027 f << ", ###data=Unkn";
2028 }
2029 }
2030 else dataSz = 0;
2031
2032 if (4ul*nPLC+16 + static_cast<unsigned long>(dataSz)*nPLC > static_cast<unsigned long>(length))
2033 {
2034 WPS_DEBUG_MSG(("WPS8Text::readPLC: warning: PLC length=0x%lx, N=%u\n", static_cast<unsigned long>(length), nPLC));
2035 return false;
2036 }
2037 entry.setParsed();
2038
2039 int fl[4];
2040 for (int i = 0; i < 4; i++)
2041 {
2042 fl[i] = int(libwps::read8(input));
2043 if (fl[i] == 0) continue;
2044 f << ", f"<<i<<"=" << fl[i];
2045 }
2046
2047 // find the zone
2048 WPSEntry textZone = m_textPositions;
2049 if (plcType.m_pos == WPS8PLCInternal::PLC::P_ZREL
2050 || plcType.m_pos == WPS8PLCInternal::PLC::P_ZINCR)
2051 {
2052 if (entry.id() >= 0 && entry.id() < int(m_state->m_textZones.size()))
2053 textZone = m_state->m_textZones[size_t(entry.id())];
2054 else
2055 {
2056 WPS_DEBUG_MSG(("WPS8Text::readPLC: warning: can not find the zone\n"));
2057 f << ", ###zone=" << entry.id();
2058 }
2059 }
2060
2061 // read text pointer
2062 std::vector<WPS8Text::DataFOD> fods;
2063 textPtrs.resize(0);
2064 long lastPtr = textZone.begin();
2065 f << ",pos = (";
2066 for (unsigned i = 0; i <= nPLC; i++)
2067 {
2068 auto pos = long(libwps::readU32(input));
2069 switch (plcType.m_pos)
2070 {
2071 case WPS8PLCInternal::PLC::P_ABS:
2072 if (pos == 0) pos = textZone.begin();
2073 break;
2074 case WPS8PLCInternal::PLC::P_ZREL:
2075 case WPS8PLCInternal::PLC::P_MREL:
2076 pos = 2*pos+textZone.begin();
2077 break;
2078 case WPS8PLCInternal::PLC::P_MINCR:
2079 case WPS8PLCInternal::PLC::P_ZINCR:
2080 {
2081 long newPos = lastPtr + 2*pos;
2082 pos = lastPtr;
2083 lastPtr = newPos;
2084 break;
2085 }
2086 case WPS8PLCInternal::PLC::P_UNKNOWN:
2087 default:
2088 break;
2089 }
2090 bool ok = pos >= textZone.begin() && pos <= textZone.end();
2091 if (!ok)
2092 {
2093 WPS_DEBUG_MSG(("WPS8Text::readPLC: warning: can not find pos\n"));
2094 f << "###";
2095 }
2096 f << std::hex << pos << ",";
2097
2098 DataFOD fod;
2099 fod.m_type = DataFOD::ATTR_PLC;
2100 fod.m_pos = ok ? pos : 0;
2101
2102 textPtrs.push_back(fod.m_pos);
2103 if (i != nPLC) fods.push_back(fod);
2104 }
2105 f << ")";
2106
2107
2108 // read data
2109 libwps::DebugStream f2;
2110 bool ok = true;
2111
2112 listValues.resize(0);
2113 long pos = input->tell();
2114 for (size_t i = 0; i < size_t(nPLC); i++)
2115 {
2116 if (plcType.m_contentType == WPS8PLCInternal::PLC::T_COMPLEX) break;
2117 WPS8TextInternal::DataPLC plc;
2118 plc.m_type = plcType.m_type;
2119 plc.m_name = entry.name();
2120 bool printPLC = true;
2121 switch (plcType.m_contentType)
2122 {
2123 case WPS8PLCInternal::PLC::T_CST :
2124 {
2125 if (dataSz == 0)
2126 {
2127 printPLC = false;
2128 break;
2129 }
2130 if (dataSz > 4 || dataSz==3)
2131 {
2132 f2.str("");
2133 for (unsigned j = 0; j < dataSz; j++)
2134 f2 << std::hex << int(libwps::readU8(input)) << ",";
2135 plc.m_error = f2.str();
2136 }
2137 else
2138 {
2139 plc.m_value = dataSz==1 ? long(libwps::readU8(input)) :
2140 dataSz==2 ? long(libwps::readU16(input)) :
2141 long(libwps::readU32(input));
2142 listValues.push_back(plc.m_value);
2143 }
2144 break;
2145 }
2146 case WPS8PLCInternal::PLC::T_STRUCT :
2147 {
2148 auto sz = long(libwps::read16(input));
2149 if (sz < 2 || sz+pos > endPage)
2150 {
2151 ok = false;
2152 break;
2153 }
2154
2155 WPS8Struct::FileData mainData;
2156 std::string error;
2157 readBlockData(input, sz+pos,mainData, error);
2158
2159 if (parser)
2160 {
2161 std::string mess;
2162 if (!((i + 1 < textPtrs.size()) && (this->*parser)(textPtrs[i], textPtrs[i+1], int(i), mainData, mess)))
2163 {
2164 ok = false;
2165 break;
2166 }
2167 plc.m_error = mess;
2168 }
2169 else
2170 {
2171 f2.str("");
2172 f2 << mainData;
2173 if (!error.empty()) f2 << ", ###" << error;
2174 plc.m_error = f2.str();
2175 }
2176 break;
2177 }
2178 case WPS8PLCInternal::PLC::T_COMPLEX:
2179 case WPS8PLCInternal::PLC::T_UNKNOWN:
2180 default:
2181 ok = false;
2182 printPLC= false;
2183 break;
2184 }
2185
2186 const auto plcID = int(m_state->m_plcList.size());
2187 if (i < fods.size())
2188 {
2189 fods[i].m_id = plcID;
2190 if (ok) fods[i].m_defPos = pos;
2191 }
2192 m_state->m_plcList.push_back(plc);
2193
2194 if (printPLC)
2195 {
2196 f2.str("");
2197 f2 << plc.m_name << i << "(PLC"<<plcID<<"):" << plc;
2198 ascii().addPos(pos);
2199 ascii().addNote(f2.str().c_str());
2200 }
2201
2202 if (ok) pos = input->tell();
2203 else plcType.m_contentType = WPS8PLCInternal::PLC::T_UNKNOWN;
2204 }
2205 if (fods.size())
2206 m_FODList = mergeSortedFODLists(m_FODList, fods);
2207
2208 ascii().addPos(page_offset);
2209 ascii().addNote(f.str().c_str());
2210
2211 if (pos == endPage) return ok;
2212
2213 if (ok && endParser)
2214 {
2215 input->seek(pos, librevenge::RVNG_SEEK_SET);
2216 ok = (this->*endParser)(endPage, textPtrs);
2217 pos = input->tell();
2218 if (pos == endPage) return ok;
2219 }
2220
2221 ascii().addPos(pos);
2222
2223 f.str("");
2224 f << "###" << entry.name() << "/PLC";
2225 ascii().addNote(f.str().c_str());
2226 return ok;
2227 }
2228
2229 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */