Debian Codegraph @

src/lib/RagTime5Document.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 <algorithm>
35 #include <cctype>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <set>
40 #include <sstream>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWTextListener.hxx"
45 #include "MWAWFont.hxx"
46 #include "MWAWGraphicStyle.hxx"
47 #include "MWAWGraphicShape.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWParagraph.hxx"
50 #include "MWAWPictBitmap.hxx"
51 #include "MWAWPictMac.hxx"
52 #include "MWAWPosition.hxx"
53 #include "MWAWPrinter.hxx"
54 #include "MWAWRSRCParser.hxx"
55 #include "MWAWStringStream.hxx"
56
57 #include "RagTime5Chart.hxx"
58 #include "RagTime5ClusterManager.hxx"
59 #include "RagTime5Formula.hxx"
60 #include "RagTime5Graph.hxx"
61 #include "RagTime5Layout.hxx"
62 #include "RagTime5Pipeline.hxx"
63 #include "RagTime5StructManager.hxx"
64 #include "RagTime5StyleManager.hxx"
65 #include "RagTime5Spreadsheet.hxx"
66 #include "RagTime5Text.hxx"
67
68 #include "RagTime5Document.hxx"
69
70 /** Internal: the structures of a RagTime5Document */
71 namespace RagTime5DocumentInternal
72 {
73 //! Internal: the helper to read doc info parse
74 struct DocInfoFieldParser final : public RagTime5StructManager::FieldParser {
75 //! constructor
76 explicit DocInfoFieldParser(RagTime5Document &doc)
77 : RagTime5StructManager::FieldParser("DocInfo")
78 , m_document(doc)
79 {
80 }
81 //! destructor
82 ~DocInfoFieldParser() final;
83 //! parse a field
84 bool parseField(RagTime5StructManager::Field &field, RagTime5Zone &zone, int /*n*/, libmwaw::DebugStream &f) final
85 {
86 if (field.m_type==RagTime5StructManager::Field::T_FieldList && field.m_fileType==0x1f7827) {
87 for (auto const &child : field.m_fieldList) {
88 if (child.m_type==RagTime5StructManager::Field::T_Unstructured && child.m_fileType==0x32040 && child.m_entry.valid()) {
89 f << child;
90
91 long actPos=zone.getInput()->tell();
92 m_document.readDocInfoClusterData(zone, child.m_entry);
93 zone.getInput()->seek(actPos, librevenge::RVNG_SEEK_SET);
94 return true;
95 }
96 MWAW_DEBUG_MSG(("RagTime5DocumentInternal::DocInfoFieldParser::parseField: find some unknown mainData block\n"));
97 f << "##mainData=" << child << ",";
98 }
99 }
100 else
101 f << field;
102 return true;
103 }
104
105 protected:
106 //! the main parser
107 RagTime5Document &m_document;
108 };
109
110 DocInfoFieldParser::~DocInfoFieldParser()
111 {
112 }
113
114 //! Internal: the helper to read index + unicode string for a RagTime5Document
115 struct IndexUnicodeParser final : public RagTime5StructManager::DataParser {
116 //! constructor
117 IndexUnicodeParser(RagTime5Document &, bool readIndex, std::string const &zoneName)
118 : RagTime5StructManager::DataParser(zoneName)
119 , m_readIndex(readIndex)
120 , m_idToStringMap()
121 , m_indicesMap()
122 {
123 }
124 //! destructor
125 ~IndexUnicodeParser() final;
126 //! try to parse a data
127 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &/*zone*/, int n, libmwaw::DebugStream &f) final
128 {
129 long pos=input->tell();
130 int id=n;
131 if (m_readIndex) {
132 if (endPos-pos<4) {
133 MWAW_DEBUG_MSG(("RagTime5DocumentInternal::IndexUnicodeParser::parse: bad data size\n"));
134 return false;
135 }
136 id=static_cast<int>(input->readULong(4));
137 f << "id=" << id << ",";
138 }
139 else if (!m_indicesMap.empty()) {
140 auto it = m_indicesMap.find(n);
141 if (it != m_indicesMap.end())
142 id=it->second;
143 else
144 id=0;
145 }
146 librevenge::RVNGString str("");
147 if (endPos==input->tell())
148 ;
149 else if (!RagTime5StructManager::readUnicodeString(input, endPos, str))
150 f << "###";
151 f << "\"" << str.cstr() << "\",";
152 m_idToStringMap[id]=str;
153 return true;
154 }
155
156 //! a flag to know if we need to read the index
157 bool m_readIndex;
158 //! the data
159 std::map<int, librevenge::RVNGString> m_idToStringMap;
160 //! the map n to index if given
161 std::map<int,int> m_indicesMap;
162 };
163
164 IndexUnicodeParser::~IndexUnicodeParser()
165 {
166 }
167
168 //! Internal: the helper to read a clustList
169 struct ClustListParser final : public RagTime5StructManager::DataParser {
170 //! constructor
171 ClustListParser(RagTime5ClusterManager &clusterManager, int fieldSize, std::string const &zoneName)
172 : RagTime5StructManager::DataParser(zoneName)
173 , m_fieldSize(fieldSize)
174 , m_linkList()
175 , m_idToNameMap()
176 , m_clusterManager(clusterManager)
177 {
178 if (m_fieldSize<4) {
179 MWAW_DEBUG_MSG(("RagTime5DocumentInternal::ClustListParser: bad field size\n"));
180 m_fieldSize=0;
181 }
182 }
183 //! destructor
184 ~ClustListParser() final;
185 //! returns the not null list dataId list
186 std::vector<int> getIdList() const
187 {
188 std::vector<int> res;
189 for (auto const &lnk : m_linkList) {
190 if (lnk.m_dataId>0)
191 res.push_back(lnk.m_dataId);
192 }
193 return res;
194 }
195 //! return the cluster name
196 std::string getClusterDebugName(int id) const
197 {
198 return m_clusterManager.getClusterDebugName(id);
199 }
200
201 //! try to parse a data
202 bool parseData(MWAWInputStreamPtr &input, long endPos, RagTime5Zone &/*zone*/, int n, libmwaw::DebugStream &f) final
203 {
204 long pos=input->tell();
205 if (m_idToNameMap.find(n)!=m_idToNameMap.end())
206 f << m_idToNameMap.find(n)->second.cstr() << ",";
207 if (endPos-pos!=m_fieldSize) {
208 MWAW_DEBUG_MSG(("RagTime5DocumentInternal::ClustListParser::parse: bad data size\n"));
209 return false;
210 }
211 std::vector<int> listIds;
212 if (!RagTime5StructManager::readDataIdList(input, 1, listIds)) {
213 MWAW_DEBUG_MSG(("RagTime5DocumentInternal::ClustListParser::parse: can not read an cluster id\n"));
214 f << "##clusterIds,";
215 return false;
216 }
217 RagTime5StructManager::ZoneLink link;
218 link.m_dataId=listIds[0];
219 if (listIds[0])
220 // a e,2003,200b, ... cluster
221 f << getClusterDebugName(listIds[0]) << ",";
222 if (m_fieldSize>=10) {
223 link.m_subZoneId[0]=long(input->readULong(4));
224 link.m_subZoneId[1]=long(input->readLong(2));
225 }
226 f << link;
227 m_linkList.push_back(link);
228 return true;
229 }
230
231 //! the field size
232 int m_fieldSize;
233 //! the list of read cluster
234 std::vector<RagTime5StructManager::ZoneLink> m_linkList;
235 //! the name
236 std::map<int, librevenge::RVNGString> m_idToNameMap;
237 private:
238 //! the main zone manager
239 RagTime5ClusterManager &m_clusterManager;
240 //! copy constructor, not implemented
241 ClustListParser(ClustListParser &orig);
242 //! copy operator, not ximplemented
243 ClustListParser &operator=(ClustListParser &orig);
244 };
245
246 ClustListParser::~ClustListParser()
247 {
248 }
249
250 ////////////////////////////////////////
251 //! Internal: the state of a RagTime5Document
252 struct State {
253 //! constructor
254 State()
255 : m_version(5)
256 , m_zonesEntry()
257 , m_zonesList()
258 , m_zoneIdToTypeMap()
259 , m_zoneInfo()
260 , m_mainClusterId(0)
261 , m_mainTypeId(0)
262 , m_buttonFormulaLink()
263 , m_dataIdZoneMap()
264 , m_pageZonesIdMap()
265 , m_sendZoneSet()
266 , m_hasLayout(false)
267 , m_numPages(0)
268 , m_headerHeight(0)
269 , m_footerHeight(0)
270 {
271 }
272
273 //! the document version
274 int m_version;
275 //! the main zone entry
276 MWAWEntry m_zonesEntry;
277 //! the zone list
278 std::vector<std::shared_ptr<RagTime5Zone> > m_zonesList;
279 //! a map id to type string
280 std::map<int, std::string> m_zoneIdToTypeMap;
281 //! the zone info zone (ie. the first zone)
282 std::shared_ptr<RagTime5Zone> m_zoneInfo;
283 //! the main cluster id
284 int m_mainClusterId;
285 //! the main type id
286 int m_mainTypeId;
287 //! the buttons formula link
288 RagTime5ClusterManager::Link m_buttonFormulaLink;
289 //! a map: data id->entry (datafork)
290 std::map<int, std::shared_ptr<RagTime5Zone> > m_dataIdZoneMap;
291 //! a map: page->main zone id
292 std::map<int, std::vector<int> > m_pageZonesIdMap;
293 //! a set used to avoid looping when sending zone
294 std::set<int> m_sendZoneSet;
295 //! a flag to know if the file has some layout
296 bool m_hasLayout;
297 int m_numPages /** the number of page of the final document */;
298
299 int m_headerHeight /** the header height if known */,
300 m_footerHeight /** the footer height if known */;
301 };
302
303 }
304
305
306 ////////////////////////////////////////////////////////////
307 // constructor/destructor, ...
308 ////////////////////////////////////////////////////////////
309 RagTime5Document::RagTime5Document(MWAWParser &parser)
310 : m_parser(&parser)
311 , m_parserState(parser.getParserState())
312 , m_state()
313 , m_chartParser()
314 , m_formulaParser()
315 , m_graphParser()
316 , m_layoutParser()
317 , m_pipelineParser()
318 , m_spreadsheetParser()
319 , m_textParser()
320 , m_clusterManager()
321 , m_structManager()
322 , m_styleManager()
323
324 , m_newPage(nullptr)
325 , m_sendFootnote(nullptr)
326 {
327 init();
328 }
329
330 RagTime5Document::~RagTime5Document()
331 {
332 }
333
334 void RagTime5Document::init()
335 {
336 m_structManager.reset(new RagTime5StructManager(*this));
337 m_clusterManager.reset(new RagTime5ClusterManager(*this));
338 m_styleManager.reset(new RagTime5StyleManager(*this));
339
340 m_chartParser.reset(new RagTime5Chart(*this));
341 m_formulaParser.reset(new RagTime5Formula(*this));
342 m_graphParser.reset(new RagTime5Graph(*this));
343 m_layoutParser.reset(new RagTime5Layout(*this));
344 m_pipelineParser.reset(new RagTime5Pipeline(*this));
345 m_spreadsheetParser.reset(new RagTime5Spreadsheet(*this));
346 m_textParser.reset(new RagTime5Text(*this));
347
348 m_state.reset(new RagTime5DocumentInternal::State);
349 }
350
351 librevenge::RVNGPropertyList RagTime5Document::getDocumentMetaData() const
352 {
353 return librevenge::RVNGPropertyList();
354 }
355
356 int RagTime5Document::version() const
357 {
358 return m_state->m_version;
359 }
360
361 void RagTime5Document::setVersion(int vers)
362 {
363 m_state->m_version=vers;
364 }
365
366 int RagTime5Document::numPages() const
367 {
368 if (m_state->m_numPages<=0) {
369 if (m_parserState->m_kind==MWAWDocument::MWAW_K_SPREADSHEET)
370 m_state->m_numPages=1;
371 else {
372 int nPages=m_layoutParser->numPages();
373 if (nPages<=0)
374 nPages=1;
375 else
376 m_state->m_hasLayout=true;
377 m_state->m_numPages=nPages;
378 }
379 }
380 return m_state->m_numPages;
381 }
382
383 void RagTime5Document::updatePageSpanList(std::vector<MWAWPageSpan> &spanList)
384 {
385 MWAWPageSpan ps(m_parser->getPageSpan());
386 ps.setPageSpan(numPages());
387 spanList.push_back(ps);
388 }
389
390 bool RagTime5Document::sendButtonZoneAsText(MWAWListenerPtr listener, int buttonId)
391 {
392 return m_graphParser->sendButtonZoneAsText(listener, buttonId);
393 }
394
395 std::shared_ptr<RagTime5ClusterManager> RagTime5Document::getClusterManager()
396 {
397 return m_clusterManager;
398 }
399
400 std::shared_ptr<RagTime5StructManager> RagTime5Document::getStructManager()
401 {
402 return m_structManager;
403 }
404
405 std::shared_ptr<RagTime5StyleManager> RagTime5Document::getStyleManager()
406 {
407 return m_styleManager;
408 }
409
410 std::shared_ptr<RagTime5Formula> RagTime5Document::getFormulaParser()
411 {
412 return m_formulaParser;
413 }
414
415 std::shared_ptr<RagTime5Graph> RagTime5Document::getGraphParser()
416 {
417 return m_graphParser;
418 }
419
420 std::shared_ptr<RagTime5Spreadsheet> RagTime5Document::getSpreadsheetParser()
421 {
422 return m_spreadsheetParser;
423 }
424
425 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readButtonCluster(RagTime5Zone &zone, int zoneType)
426 {
427 return m_graphParser->readButtonCluster(zone, zoneType);
428 }
429
430 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readChartCluster(RagTime5Zone &zone, int zoneType)
431 {
432 return m_chartParser->readChartCluster(zone, zoneType);
433 }
434
435 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readGraphicCluster(RagTime5Zone &zone, int zoneType)
436 {
437 return m_graphParser->readGraphicCluster(zone, zoneType);
438 }
439
440 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readLayoutCluster(RagTime5Zone &zone, int zoneType)
441 {
442 return m_layoutParser->readLayoutCluster(zone, zoneType);
443 }
444
445 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readPipelineCluster(RagTime5Zone &zone, int zoneType)
446 {
447 return m_pipelineParser->readPipelineCluster(zone, zoneType);
448 }
449
450 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readPictureCluster(RagTime5Zone &zone, int zoneType)
451 {
452 return m_graphParser->readPictureCluster(zone, zoneType);
453 }
454
455 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readSpreadsheetCluster(RagTime5Zone &zone, int zoneType)
456 {
457 return m_spreadsheetParser->readSpreadsheetCluster(zone, zoneType);
458 }
459
460 std::shared_ptr<RagTime5ClusterManager::Cluster> RagTime5Document::readTextCluster(RagTime5Zone &zone, int zoneType)
461 {
462 return m_textParser->readTextCluster(zone, zoneType);
463 }
464
465 std::shared_ptr<RagTime5Zone> RagTime5Document::getDataZone(int dataId) const
466 {
467 if (m_state->m_dataIdZoneMap.find(dataId)==m_state->m_dataIdZoneMap.end())
468 return std::shared_ptr<RagTime5Zone>();
469 return m_state->m_dataIdZoneMap.find(dataId)->second;
470 }
471
472 RagTime5ClusterManager::Cluster::Type RagTime5Document::getClusterType(int zId) const
473 {
474 return m_clusterManager->getClusterType(zId);
475 }
476
477 RagTime5ClusterManager::Cluster::Type RagTime5Document::getPipelineContainerType(int pipelineId) const
478 {
479 return m_pipelineParser->getContainerType(pipelineId);
480 }
481
482 ////////////////////////////////////////////////////////////
483 // new page
484 ////////////////////////////////////////////////////////////
485 void RagTime5Document::newPage(int number, bool softBreak)
486 {
487 if (!m_parser || !m_newPage) {
488 MWAW_DEBUG_MSG(("RagTime5Document::newPage: can not find newPage callback\n"));
489 return;
490 }
491 (m_parser->*m_newPage)(number, softBreak);
492 }
493
494 ////////////////////////////////////////////////////////////
495 //
496 // Intermediate level
497 //
498 ////////////////////////////////////////////////////////////
499
500 bool RagTime5Document::createZones()
501 {
502 int const vers=version();
503 if (vers<5) {
504 MWAW_DEBUG_MSG(("RagTime5Document::createZones: must not be called for v%d document\n", vers));
505 return false;
506 }
507
508 if (m_state->m_zonesList.empty()) {
509 if (!findZones(m_state->m_zonesEntry))
510 return false;
511 ascii().addPos(m_state->m_zonesEntry.end());
512 ascii().addNote("FileHeader-End");
513 }
514
515 if (m_state->m_zonesList.size()<20) {
516 // even an empty file seems to have almost ~80 zones, so...
517 MWAW_DEBUG_MSG(("RagTime5Document::createZones: the zone list seems too short\n"));
518 return false;
519 }
520 // we need to find the string's zones and update the map zoneId to string data
521 m_state->m_zoneInfo=m_state->m_zonesList[0];
522 if (!findZonesKind())
523 return false;
524 // now, we can update all the zones: kinds, input, ...
525 for (size_t i=1; i<m_state->m_zonesList.size(); ++i)
526 updateZone(m_state->m_zonesList[i]);
527
528 if (!useMainZoneInfoData()) return false;
529
530 // now, parse the formula in spreadsheet and in button
531 m_spreadsheetParser->parseSpreadsheetFormulas();
532 if (!m_state->m_buttonFormulaLink.empty())
533 m_formulaParser->readFormulaClusters(m_state->m_buttonFormulaLink, -1);
534
535 // check for unread clusters
536 for (auto const &zone : m_state->m_zonesList) {
537 if (!zone || zone->m_isParsed || zone->getKindLastPart(zone->m_kinds[1].empty())!="Cluster")
538 continue;
539 if (zone->m_entry.valid()) {
540 MWAW_DEBUG_MSG(("RagTime5Document::createZones: find unparsed cluster zone %d\n", zone->m_ids[0]));
541 }
542 readClusterZone(*zone);
543 }
544 // now read the screen rep list zone: CHECKME: can we remove this check, now ?
545 for (auto const &zone : m_state->m_zonesList) {
546 if (!zone || zone->m_isParsed || (!zone->m_entry.valid()&&zone->m_variableD[0]!=1) || zone->getKindLastPart(zone->m_kinds[1].empty())!="ScreenRepList")
547 continue;
548 m_graphParser->readPictureList(*zone);
549 }
550
551 return true;
552 }
553
554 bool RagTime5Document::findZonesKind()
555 {
556 libmwaw::DebugStream f;
557 if (!m_state->m_zoneIdToTypeMap.empty())
558 return true;
559 for (size_t i=1; i<m_state->m_zonesList.size(); ++i) {
560 if (!m_state->m_zonesList[i])
561 continue;
562 auto &zone=*m_state->m_zonesList[i];
563 // id=0 correspond to the file header already read, so ignored it
564 if (zone.m_ids[0]==0 && zone.m_level==1) {
565 zone.m_isParsed=true;
566 continue;
567 }
568
569 std::string what("");
570 if (zone.m_idsFlag[1]!=0 || (zone.m_ids[1]!=23 && zone.m_ids[1]!=24) || zone.m_ids[2]!=21)
571 continue;
572 // normally a string, update the zone input(always uncompressed) and read the zone
573 if (!updateZoneInput(zone) || !readString(zone, what) || what.empty())
574 continue;
575 if (m_state->m_zoneIdToTypeMap.find(zone.m_ids[0])!=m_state->m_zoneIdToTypeMap.end()) {
576 MWAW_DEBUG_MSG(("RagTime5Document::findZonesKind: a type with id=%d already exists\n", zone.m_ids[0]));
577 }
578 else {
579 m_state->m_zoneIdToTypeMap[zone.m_ids[0]]=what;
580 f.str("");
581 f << what << ",";
582 ascii().addPos(zone.m_defPosition);
583 ascii().addNote(f.str().c_str());
584 }
585 }
586 return true;
587 }
588
589 bool RagTime5Document::parseMainZoneInfoData(RagTime5Zone const &zoneInfo)
590 {
591 if (zoneInfo.m_isParsed)
592 return true;
593
594 zoneInfo.m_isParsed=true;
595 for (auto const &it : zoneInfo.m_childIdToZoneMap) {
596 std::shared_ptr<RagTime5Zone> zone=it.second;
597 if (!zone) continue;
598 zone->m_isParsed=true;
599 switch (it.first) {
600 case 3: // alway with gd=[1,_]
601 if (zone->m_variableD[0]==1 && zone->m_variableD[1]) {
602 MWAW_DEBUG_MSG(("RagTime5Document::parseMainZoneInfoData: find a zone 3\n"));
603 ascii().addPos(zone->m_defPosition);
604 ascii().addNote("###");
605 }
606 break;
607 case 4: // list of zones limits, safe to ignore
608 case 5: // file limits, safe to ignore
609 break;
610 case 6: // alway with gd=[_,_]
611 if (zone->m_variableD[1]) {
612 MWAW_DEBUG_MSG(("RagTime5Document::parseMainZoneInfoData: find a zone 6\n"));
613 ascii().addPos(zone->m_defPosition);
614 ascii().addNote("###");
615 }
616 break;
617 case 10: { // the type zone
618 if (zone->m_variableD[0]!=1) {
619 MWAW_DEBUG_MSG(("RagTime5Document::parseMainZoneInfoData: the type zone seems bads\n"));
620 break;
621 }
622 m_state->m_mainTypeId=zone->m_variableD[1];
623 break;
624 }
625 case 11:
626 if (zone->m_variableD[0]!=1) {
627 MWAW_DEBUG_MSG(("RagTime5Document::parseMainZoneInfoData: the main cluster zone seems bads\n"));
628 break;
629 }
630 m_state->m_mainClusterId=zone->m_variableD[1];
631 break;
632 default:
633 MWAW_DEBUG_MSG(("RagTime5Document::parseMainZoneInfoData: find unknown main zone %d\n", it.first));
634 ascii().addPos(zone->m_defPosition);
635 ascii().addNote("###");
636 break;
637 }
638 }
639 if (!m_state->m_mainClusterId) {
640 MWAW_DEBUG_MSG(("RagTime5Document::parseMainZoneInfoData: can not find the cluster id try 13\n"));
641 m_state->m_mainClusterId=13;
642 }
643 return true;
644 }
645
646 bool RagTime5Document::useMainZoneInfoData()
647 {
648 if (!m_state->m_zoneInfo || m_state->m_zoneInfo->m_ids[0]!=1) {
649 MWAW_DEBUG_MSG(("RagTime5Document::useMainZoneInfoData: can not find the zone information zone, impossible to continue\n"));
650 return false;
651 }
652 parseMainZoneInfoData(*m_state->m_zoneInfo);
653
654 // the type id
655 if (m_state->m_mainTypeId) {
656 auto dZone=getDataZone(m_state->m_mainTypeId);
657 if (!dZone || !dZone->m_entry.valid()) {
658 MWAW_DEBUG_MSG(("RagTime5Document::useMainZoneInfoData: can not find the type zone\n"));
659 }
660 else {
661 if (dZone->getKindLastPart()!="ItemData" || !m_structManager->readTypeDefinitions(*dZone)) {
662 MWAW_DEBUG_MSG(("RagTime5Document::useMainZoneInfoData: unexpected list of block type\n"));
663 }
664 }
665 }
666 // the main cluster
667 auto dZone=getDataZone(m_state->m_mainClusterId);
668 if (!dZone) {
669 MWAW_DEBUG_MSG(("RagTime5Document::useMainZoneInfoData: can not find the main cluster zone\n"));
670 return true;
671 }
672 dZone->m_extra+="main,";
673 if (dZone->getKindLastPart(dZone->m_kinds[1].empty())!="Cluster" || !readClusterZone(*dZone, 0)) {
674 MWAW_DEBUG_MSG(("RagTime5Document::useMainZoneInfoData: unexpected main cluster zone type\n"));
675 }
676 return true;
677 }
678
679 bool RagTime5Document::readZoneData(RagTime5Zone &zone)
680 {
681 if (!zone.m_entry.valid()) {
682 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: can not find the entry\n"));
683 return false;
684 }
685 libmwaw::DebugStream f;
686 int usedId=zone.m_kinds[1].empty() ? 0 : 1;
687 std::string actType=zone.getKindLastPart(usedId==0);
688
689 std::string kind=zone.getKindLastPart();
690 // the "RagTime" string
691 if (kind=="CodeName") {
692 std::string what;
693 if (zone.m_kinds[1]!="BESoftware:7BitASCII:Type" || !readString(zone, what)) {
694 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: can not read codename for zone %d\n", zone.m_ids[0]));
695 zone.m_isParsed=true;
696 f << "Entries(CodeName)[" << zone << "]:###";
697 libmwaw::DebugFile &ascFile=zone.ascii();
698 ascFile.addPos(zone.m_entry.begin());
699 ascFile.addNote(f.str().c_str());
700 }
701 for (auto const &it : zone.m_childIdToZoneMap) {
702 std::shared_ptr<RagTime5Zone> child=it.second;
703 if (!child || child->m_isParsed) continue;
704 if (child->getKindLastPart()=="DocuVersion" && readDocumentVersion(*child))
705 continue;
706 if (child->getKindLastPart()=="7BitASCII") {
707 child->m_isParsed=true;
708 ascii().addPos(child->m_defPosition);
709 ascii().addNote("codeName[type]");
710 continue;
711 }
712 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find unknown child for codename for zone %d\n", zone.m_ids[0]));
713 ascii().addPos(child->m_defPosition);
714 ascii().addNote("###unkCodeName");
715 }
716 return true;
717 }
718 //
719 // first test for picture data
720 //
721
722 // checkme: find how we can retrieve the next data without parsing unparsed data
723 if (kind=="ScreenRepMatchData" || kind=="ScreenRepMatchDataColor") {
724 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find unexpected %s for zone %d\n", kind.c_str(), zone.m_ids[0]));
725 return m_graphParser->readPictureMatch(zone, kind=="ScreenRepMatchDataColor");
726 }
727 if (kind=="DocuVersion") {
728 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find unexpected docuVersion\n"));
729 return readDocumentVersion(zone);
730 }
731 if (kind=="Thumbnail")
732 return m_graphParser->readPictureData(zone);
733 if (m_graphParser->readPictureData(zone)) {
734 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find some unparsed picture %d\n", zone.m_ids[0]));
735 ascii().addPos(zone.m_defPosition);
736 ascii().addNote("###unparsed");
737 return true;
738 }
739 if (kind=="ScriptComment" || kind=="ScriptName") {
740 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find unexpected %s\n", kind.c_str()));
741 return readScriptComment(zone);
742 }
743 std::string name("");
744 if (kind=="OSAScript" || kind=="TCubics") {
745 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find unexpected %s\n", kind.c_str()));
746 name=kind;
747 }
748 else if (kind=="ItemData" || kind=="Unicode") {
749 actType=zone.getKindLastPart(zone.m_kinds[1].empty());
750 if (actType=="Unicode" || kind=="Unicode") {
751 // hilo/lohi is not always set, so this can cause problem....
752 if (readUnicodeString(zone))
753 return true;
754 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: can not read a unicode zone %d\n", zone.m_ids[0]));
755 f << "Entries(StringUnicode)[" << zone << "]:###";
756 zone.m_isParsed=true;
757 libmwaw::DebugFile &ascFile=zone.ascii();
758 ascFile.addPos(zone.m_entry.begin());
759 ascFile.addNote(f.str().c_str());
760 return true;
761 }
762 if (zone.m_entry.length()==164 && zone.m_level==1)
763 name="ZoneUnkn0";
764 else {
765 name="ItemDta";
766 // checkme: often Data22 is not parsed, but there can be others
767 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find a unparsed %s zone %d\n", zone.m_level==1 ? "data" : "main", zone.m_ids[0]));
768 }
769 }
770 else {
771 MWAW_DEBUG_MSG(("RagTime5Document::readZoneData: find a unknown type for zone=%d\n", zone.m_ids[0]));
772 name="UnknownZone";
773 }
774 libmwaw::DebugFile &ascFile=zone.ascii();
775 f << "Entries(" << name << "):" << zone;
776 zone.m_isParsed=true;
777 ascFile.addPos(zone.m_entry.begin());
778 ascFile.addNote(f.str().c_str());
779 ascFile.addPos(zone.m_entry.end());
780 ascFile.addNote("_");
781 return true;
782 }
783
784 ////////////////////////////////////////////////////////////
785 // parse the different zones
786 ////////////////////////////////////////////////////////////
787 bool RagTime5Document::readString(RagTime5Zone &zone, std::string &text)
788 {
789 if (!zone.m_entry.valid()) return false;
790 MWAWInputStreamPtr input=zone.getInput();
791 libmwaw::DebugFile &ascFile=zone.ascii();
792 libmwaw::DebugStream f;
793 f << "Entries(StringZone)[" << zone << "]:";
794 input->seek(zone.m_entry.begin(), librevenge::RVNG_SEEK_SET);
795 text="";
796 for (long i=0; i<zone.m_entry.length(); ++i) {
797 auto c=char(input->readULong(1));
798 if (c==0 && i+1==zone.m_entry.length()) break;
799 if (c<0x1f)
800 return false;
801 text+=c;
802 }
803 f << "\"" << text << "\",";
804 if (input->tell()!=zone.m_entry.end()) {
805 MWAW_DEBUG_MSG(("RagTime5Document::readString: find extra data\n"));
806 f << "###";
807 ascFile.addDelimiter(input->tell(),'|');
808 }
809 zone.m_isParsed=true;
810 ascFile.addPos(zone.m_entry.begin());
811 ascFile.addNote(f.str().c_str());
812 ascFile.addPos(zone.m_entry.end());
813 ascFile.addNote("_");
814 return true;
815 }
816
817 bool RagTime5Document::readUnicodeString(RagTime5Zone &zone, std::string const &what)
818 {
819 if (zone.m_entry.length()==0) return true;
820 MWAWInputStreamPtr input=zone.getInput();
821 libmwaw::DebugFile &ascFile=zone.ascii();
822 libmwaw::DebugStream f;
823 if (what.empty())
824 f << "Entries(StringUnicode)[" << zone << "]:";
825 else
826 f << "Entries(" << what << ")[" << zone << "]:";
827 input->setReadInverted(!zone.m_hiLoEndian);
828 input->seek(zone.m_entry.begin(), librevenge::RVNG_SEEK_SET);
829 librevenge::RVNGString string;
830 if (!m_structManager->readUnicodeString(input, zone.m_entry.end(), string))
831 f << "###";
832 else
833 f << string.cstr();
834 zone.m_isParsed=true;
835 ascFile.addPos(zone.m_entry.begin());
836 ascFile.addNote(f.str().c_str());
837 ascFile.addPos(zone.m_entry.end());
838 ascFile.addNote("_");
839 input->setReadInverted(false);
840 return true;
841 }
842
843 bool RagTime5Document::readUnicodeStringList(RagTime5ClusterManager::NameLink const &nameLink, std::map<int, librevenge::RVNGString> &idToStringMap)
844 {
845 RagTime5DocumentInternal::IndexUnicodeParser dataParser(*this, false, "UnicodeNames");
846 std::vector<long> posToNames[2];
847 for (int i=0; i<2; ++i) {
848 if (!nameLink.m_posToNames[i].empty())
849 posToNames[i]=nameLink.m_posToNames[i];
850 else if (!nameLink.m_posToNamesLinks[i].empty())
851 readLongList(nameLink.m_posToNamesLinks[i], posToNames[i]);
852 }
853 long numPosToNames=long(posToNames[1].size());
854 for (auto const &id : posToNames[0]) {
855 if (id>=0 && id<numPosToNames)
856 dataParser.m_indicesMap[int(posToNames[1][size_t(id)])]=int(id);
857 }
858 RagTime5ClusterManager::Link link;
859 link.m_ids=nameLink.m_ids;
860 link.m_longList=nameLink.m_decalList;
861 if (!readListZone(link, dataParser))
862 return false;
863 idToStringMap=dataParser.m_idToStringMap;
864 return true;
865 }
866
867 bool RagTime5Document::readLongListWithSize(int dataId, int fSz, std::vector<long> &listPosition, std::string const &zoneName)
868 {
869 listPosition.clear();
870 if (!dataId || fSz<=0 || fSz>4)
871 return false;
872
873 auto zone=getDataZone(dataId);
874 if (!zone || !zone->m_entry.valid() || (zone->m_entry.length()%fSz) ||
875 zone->getKindLastPart(zone->m_kinds[1].empty())!="ItemData") {
876 MWAW_DEBUG_MSG(("RagTime5Document::readLongListWithSize: the zone %d seems bad\n", dataId));
877 return false;
878 }
879 MWAWEntry entry=zone->m_entry;
880 MWAWInputStreamPtr input=zone->getInput();
881 input->setReadInverted(!zone->m_hiLoEndian);
882 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
883
884 zone->m_isParsed=true;
885 libmwaw::DebugStream f;
886
887 if (!zoneName.empty()) {
888 std::string zName(zoneName);
889 if (zName[0]>='a'&&zName[0]<='z')
890 zName[0]=char(zName[0]+'A'-'a');
891 f << "Entries(" << zName << ")[" << *zone << "]:";
892 }
893 else
894 f << "Entries(ListLong" << fSz << ")[" << *zone << "]:";
895 auto N=int(entry.length()/fSz);
896 for (int i=0; i<N; ++i) {
897 long ptr=input->readLong(fSz);
898 listPosition.push_back(ptr);
899 if (ptr==-2147483648) // 80000000
900 f << "inf,";
901 else if (ptr)
902 f << ptr << ",";
903 else
904 f << "_,";
905 }
906 input->setReadInverted(false);
907 zone->ascii().addPos(entry.begin());
908 zone->ascii().addNote(f.str().c_str());
909 zone->ascii().addPos(entry.end());
910 zone->ascii().addNote("_");
911 return true;
912 }
913
914 bool RagTime5Document::readLongList(RagTime5ClusterManager::Link const &link, std::vector<long> &list)
915 {
916 if (!link.m_ids.empty() && link.m_ids[0] &&
917 readLongListWithSize(link.m_ids[0], link.m_fieldSize, list, link.m_name))
918 return true;
919 list=link.m_longList;
920 return !list.empty();
921 }
922
923 bool RagTime5Document::readPositions(int posId, std::vector<long> &listPosition)
924 {
925 return readLongListWithSize(posId, 4, listPosition, "Positions");
926 }
927
928 ////////////////////////////////////////////////////////////
929 // Cluster
930 ////////////////////////////////////////////////////////////
931 bool RagTime5Document::readClusterRootData(RagTime5ClusterManager::ClusterRoot &cluster)
932 {
933 // first read the list of child cluster and update the list of cluster for the cluster manager
934 std::vector<int> listClusters;
935 for (auto const &zone : m_state->m_zonesList) {
936 if (!zone || zone->m_isParsed || !zone->m_entry.valid() || zone->getKindLastPart(zone->m_kinds[1].empty())!="Cluster")
937 continue;
938 listClusters.push_back(zone->m_ids[0]);
939 }
940
941 if (cluster.m_listClusterId==0) {
942 MWAW_DEBUG_MSG(("RagTime5ClusterManager::readClusterRootData: cluster list id is not set, try zone id+1\n"));
943 cluster.m_listClusterId=cluster.m_zoneId+1;
944 }
945 std::vector<int> listChilds;
946 m_clusterManager->readClusterMainList(cluster, listChilds, listClusters);
947 std::set<int> seens;
948 // the list of graphic type
949 if (!cluster.m_graphicTypeLink.empty() && m_graphParser->readGraphicTypes(cluster.m_graphicTypeLink)) {
950 if (cluster.m_graphicTypeLink.m_ids.size()>2 && cluster.m_graphicTypeLink.m_ids[1])
951 seens.insert(cluster.m_graphicTypeLink.m_ids[1]);
952 }
953 // the different styles ( beginning with colors, then graphic styles and text styles )
954 for (int i=0; i<8; ++i) {
955 int const order[]= {7, 6, 1, 2, 0, 4, 3, 5};
956 int cId=cluster.m_styleClusterIds[order[i]];
957 if (!cId) continue;
958
959 int const wh[]= {0x480, 0x480, 0x480, 0x480, 0x480, -1, 0x480, 0x8042};
960 auto dZone= getDataZone(cId);
961 if (!dZone || dZone->getKindLastPart(dZone->m_kinds[1].empty())!="Cluster" || !readClusterZone(*dZone, wh[order[i]])) {
962 MWAW_DEBUG_MSG(("RagTime5Document::readClusterRootData: can not find cluster style zone %d\n", cId));
963 continue;
964 }
965 seens.insert(cId);
966 }
967 // the formula def cluster list
968 if (!cluster.m_listClusterLink[1].empty()) {
969 RagTime5DocumentInternal::ClustListParser parser(*m_clusterManager.get(), 4, "FormulaList");
970 readFixedSizeZone(cluster.m_listClusterLink[1], parser);
971 // TODO: read the field cluster's data here
972 }
973 // list of style
974 if (!cluster.m_listClusterLink[2].empty()) {
975 RagTime5DocumentInternal::ClustListParser parser(*m_clusterManager.get(), 4, "RootUnknALst2");
976 readFixedSizeZone(cluster.m_listClusterLink[2], parser);
977 }
978 // now the main cluster list
979 for (int i=0; i<1; ++i) {
980 int cId=cluster.m_clusterIds[i];
981 if (cId==0) continue;
982 auto data=getDataZone(cId);
983 if (!data || !data->m_entry.valid() || data->getKindLastPart(data->m_kinds[1].empty())!="Cluster") {
984 MWAW_DEBUG_MSG(("RagTime5ClusterManager::readClusterRootData: the cluster zone %d seems bad\n", cId));
985 continue;
986 }
987 int const wh[]= {0x10000};
988 if (readClusterZone(*data, wh[i]))
989 seens.insert(cId);
990 }
991 if (!cluster.m_functionNameLink.empty())
992 m_formulaParser->readFunctionNames(cluster.m_functionNameLink);
993 m_state->m_buttonFormulaLink=cluster.m_formulaLink;
994 for (auto const &lnk : cluster.m_settingLinks) {
995 if (lnk.empty()) continue;
996 RagTime5StructManager::FieldParser defaultParser("Settings");
997 readStructZone(lnk, defaultParser, 0);
998 }
999 if (!cluster.m_docInfoLink.empty()) {
1000 RagTime5DocumentInternal::DocInfoFieldParser parser(*this);
1001 readStructZone(cluster.m_docInfoLink, parser, 18);
1002 }
1003 if (!cluster.m_listUnicodeLink.empty()) {
1004 RagTime5DocumentInternal::IndexUnicodeParser parser(*this, true, "RootUnicodeLst");
1005 readListZone(cluster.m_listUnicodeLink, parser);
1006 }
1007
1008 // unknown link
1009 if (!cluster.m_linkUnknown.empty()) { // find always an empty list
1010 RagTime5StructManager::DataParser parser("RootUnknC");
1011 readListZone(cluster.m_linkUnknown, parser);
1012 }
1013 // now read the not parsed childs
1014 for (auto cId : listChilds) {
1015 if (cId==0 || seens.find(cId)!=seens.end())
1016 continue;
1017 auto dZone= getDataZone(cId);
1018 if (!dZone || dZone->getKindLastPart(dZone->m_kinds[1].empty())!="Cluster" || !readClusterZone(*dZone)) {
1019 MWAW_DEBUG_MSG(("RagTime5Document::readClusterRootData: can not find cluster zone %d\n", cId));
1020 continue;
1021 }
1022 seens.insert(cId);
1023 }
1024
1025 for (auto const &link : cluster.m_linksList) {
1026 if (link.m_type==RagTime5ClusterManager::Link::L_List) {
1027 readListZone(link);
1028 continue;
1029 }
1030 else if (link.m_type==RagTime5ClusterManager::Link::L_LongList) {
1031 std::vector<long> list;
1032 readLongList(link, list);
1033 continue;
1034 }
1035 else if (link.m_type==RagTime5ClusterManager::Link::L_UnknownClusterC) {
1036 m_clusterManager->readUnknownClusterC(link);
1037 continue;
1038 }
1039
1040 if (link.empty()) continue;
1041 std::shared_ptr<RagTime5Zone> data=getDataZone(link.m_ids[0]);
1042 if (!data || data->m_isParsed) {
1043 MWAW_DEBUG_MSG(("RagTime5Document::readClusterRootData: can not find data zone %d\n", link.m_ids[0]));
1044 continue;
1045 }
1046 data->m_hiLoEndian=cluster.m_hiLoEndian;
1047 if (link.m_fieldSize==0 && !data->m_entry.valid())
1048 continue;
1049 switch (link.m_type) {
1050 case RagTime5ClusterManager::Link::L_FieldsList:
1051 case RagTime5ClusterManager::Link::L_List:
1052 case RagTime5ClusterManager::Link::L_LongList:
1053 case RagTime5ClusterManager::Link::L_UnicodeList:
1054 case RagTime5ClusterManager::Link::L_UnknownClusterC:
1055 break;
1056 case RagTime5ClusterManager::Link::L_ClusterLink: {
1057 std::vector<RagTime5StructManager::ZoneLink> links;
1058 readClusterLinkList(*data, link, links);
1059 break;
1060 }
1061 case RagTime5ClusterManager::Link::L_Unknown:
1062 #if !defined(__clang__)
1063 default:
1064 #endif
1065 readFixedSizeZone(link, "");
1066 break;
1067 }
1068 }
1069
1070 return true;
1071 }
1072
1073 bool RagTime5Document::readChildList(RagTime5ClusterManager::Link const &link, std::vector<RagTime5StructManager::ZoneLink> &childList, bool findN)
1074 {
1075 if (link.m_ids.empty())
1076 return true;
1077 auto dataZone=getDataZone(link.m_ids[0]);
1078 if (!dataZone || dataZone->m_entry.length()<=0) // ok, empty list
1079 return true;
1080 if (!dataZone->m_entry.valid() ||
1081 dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData") {
1082 MWAW_DEBUG_MSG(("RagTime5Document::readChildList: the child zone %d seems bad\n",
1083 link.m_ids[0]));
1084 return false;
1085 }
1086 if (findN) {
1087 if (dataZone->m_entry.length()%12) {
1088 MWAW_DEBUG_MSG(("RagTime5Document::readChildList: can not compute the number of child for zone %d\n",
1089 link.m_ids[0]));
1090 return false;
1091 }
1092 auto finalLink=link;
1093 finalLink.m_N=int(dataZone->m_entry.length()/12);
1094 if (!readClusterLinkList(*dataZone, finalLink, childList))
1095 return false;
1096 }
1097 else if (!readClusterLinkList(*dataZone, link, childList))
1098 return false;
1099 checkClusterList(childList);
1100 return true;
1101 }
1102
1103 bool RagTime5Document::checkClusterList(std::vector<int> const &list)
1104 {
1105 bool ok=true;
1106 for (auto cId : list) {
1107 if (cId==0) continue;
1108 auto data=getDataZone(cId);
1109 if (!data || !data->m_entry.valid() || data->getKindLastPart(data->m_kinds[1].empty())!="Cluster") {
1110 MWAW_DEBUG_MSG(("RagTime5ClusterManager::checkClusterList: the cluster zone %d seems bad\n", cId));
1111 ok=false;
1112 }
1113 }
1114 return ok;
1115 }
1116
1117 bool RagTime5Document::checkClusterList(std::vector<RagTime5StructManager::ZoneLink> const &list)
1118 {
1119 bool ok=true;
1120 for (auto const &lnk : list) {
1121 int cId=lnk.m_dataId;
1122 if (cId==0) continue;
1123 auto data=getDataZone(cId);
1124 if (!data || !data->m_entry.valid() || data->getKindLastPart(data->m_kinds[1].empty())!="Cluster") {
1125 MWAW_DEBUG_MSG(("RagTime5ClusterManager::checkClusterList: the cluster zone %d seems bad\n", cId));
1126 ok=false;
1127 }
1128 }
1129 return ok;
1130 }
1131
1132 bool RagTime5Document::readClusterZone(RagTime5Zone &zone, int zoneType)
1133 {
1134 std::shared_ptr<RagTime5ClusterManager::Cluster> cluster;
1135 if (!m_clusterManager->readCluster(zone, cluster, zoneType) || !cluster)
1136 return false;
1137 checkClusterList(cluster->m_clusterIdsList);
1138
1139 switch (cluster->m_type) {
1140 // main zone
1141 case RagTime5ClusterManager::Cluster::C_ButtonZone:
1142 case RagTime5ClusterManager::Cluster::C_ChartZone:
1143 case RagTime5ClusterManager::Cluster::C_FormulaDef:
1144 case RagTime5ClusterManager::Cluster::C_FormulaPos:
1145 case RagTime5ClusterManager::Cluster::C_GraphicZone:
1146 case RagTime5ClusterManager::Cluster::C_GroupZone:
1147 case RagTime5ClusterManager::Cluster::C_Layout:
1148 case RagTime5ClusterManager::Cluster::C_PictureZone:
1149 case RagTime5ClusterManager::Cluster::C_Pipeline:
1150 case RagTime5ClusterManager::Cluster::C_SpreadsheetZone:
1151 case RagTime5ClusterManager::Cluster::C_Sound:
1152 case RagTime5ClusterManager::Cluster::C_TextZone: // parsing already done
1153 return true;
1154 case RagTime5ClusterManager::Cluster::C_ClusterGProp:
1155 return readClusterGProp(*cluster);
1156 case RagTime5ClusterManager::Cluster::C_ClusterC:
1157 return readUnknownClusterCData(*cluster);
1158 case RagTime5ClusterManager::Cluster::C_ColorPattern:
1159 return m_graphParser->readColorPatternZone(*cluster);
1160 case RagTime5ClusterManager::Cluster::C_Root: {
1161 auto *root=dynamic_cast<RagTime5ClusterManager::ClusterRoot *>(cluster.get());
1162 if (!root) {
1163 MWAW_DEBUG_MSG(("RagTime5ClusterManager::readClusterZone: can not find the root pointer\n"));
1164 return false;
1165 }
1166 readClusterRootData(*root);
1167 return true;
1168 }
1169
1170 // style
1171 case RagTime5ClusterManager::Cluster::C_FormatStyles:
1172 return m_styleManager->readFormats(*cluster);
1173 case RagTime5ClusterManager::Cluster::C_ColorStyles:
1174 return m_styleManager->readGraphicColors(*cluster);
1175 case RagTime5ClusterManager::Cluster::C_GraphicStyles:
1176 return m_styleManager->readGraphicStyles(*cluster);
1177 case RagTime5ClusterManager::Cluster::C_TextStyles:
1178 return m_styleManager->readTextStyles(*cluster);
1179 case RagTime5ClusterManager::Cluster::C_UnitStyles: {
1180 RagTime5StructManager::FieldParser defaultParser("Units");
1181 return readStructZone(cluster->m_dataLink, defaultParser, 14, &cluster->m_nameLink);
1182 }
1183
1184 case RagTime5ClusterManager::Cluster::C_Empty:
1185 case RagTime5ClusterManager::Cluster::C_Unknown:
1186 #if !defined(__clang__)
1187 default:
1188 #endif
1189 break;
1190 }
1191
1192 if (!cluster->m_nameLink.empty()) {
1193 std::map<int, librevenge::RVNGString> idToStringMap;
1194 readUnicodeStringList(cluster->m_nameLink, idToStringMap);
1195 }
1196
1197 for (auto const &link : cluster->m_linksList) {
1198 if (link.m_type==RagTime5ClusterManager::Link::L_List)
1199 readListZone(link);
1200 else
1201 readFixedSizeZone(link, "");
1202 }
1203 return true;
1204 }
1205
1206 bool RagTime5Document::readClusterLinkList
1207 (RagTime5ClusterManager::Link const &link, std::vector<RagTime5StructManager::ZoneLink> &list, std::string const &name)
1208 {
1209 RagTime5DocumentInternal::ClustListParser parser(*m_clusterManager.get(), 10, !name.empty() ? name : link.getZoneName());
1210 if (!link.empty())
1211 readListZone(link, parser);
1212 list=parser.m_linkList;
1213 checkClusterList(list);
1214 return true;
1215 }
1216
1217 bool RagTime5Document::readClusterLinkList(RagTime5Zone &zone, RagTime5ClusterManager::Link const &link,
1218 std::vector<RagTime5StructManager::ZoneLink> &listLinks)
1219 {
1220 listLinks.clear();
1221 if (!zone.m_entry.valid()) {
1222 if (link.m_N && link.m_fieldSize) {
1223 MWAW_DEBUG_MSG(("RagTime5Document::readClusterLinkList: can not find data zone %d\n", link.m_ids[0]));
1224 }
1225 return false;
1226 }
1227
1228 MWAWInputStreamPtr input=zone.getInput();
1229 bool const hiLo=zone.m_hiLoEndian;
1230 input->setReadInverted(!hiLo);
1231 input->seek(zone.m_entry.begin(), librevenge::RVNG_SEEK_SET);
1232 zone.m_isParsed=true;
1233
1234 libmwaw::DebugFile &ascFile=zone.ascii();
1235 libmwaw::DebugStream f;
1236 std::string zoneName=link.m_name.empty() ? "ClustLink" : link.m_name;
1237 zoneName[0]=char(std::toupper((unsigned char) zoneName[0]));
1238 f << "Entries(" << zoneName << ")[" << zone << "]:";
1239 if (link.m_N*link.m_fieldSize>zone.m_entry.length() || link.m_N*link.m_fieldSize < 0 ||
1240 link.m_N>zone.m_entry.length() || link.m_fieldSize!=12) {
1241 MWAW_DEBUG_MSG(("RagTime5Document::readClusterLinkList: bad fieldSize/N for zone %d\n", link.m_ids[0]));
1242 f << "###";
1243 ascFile.addPos(zone.m_entry.begin());
1244 ascFile.addNote(f.str().c_str());
1245 return true;
1246 }
1247 ascFile.addPos(zone.m_entry.begin());
1248 ascFile.addNote(f.str().c_str());
1249
1250 listLinks.resize(size_t(link.m_N)+1);
1251 for (int i=0; i<link.m_N; ++i) {
1252 long pos=input->tell();
1253 f.str("");
1254 f << zoneName << "-" << i+1 << ":";
1255 RagTime5StructManager::ZoneLink cLink;
1256
1257 std::vector<int> listIds;
1258 if (!m_structManager->readDataIdList(input, 1, listIds)) {
1259 MWAW_DEBUG_MSG(("RagTime5Document::readClusterLinkList: a link seems bad\n"));
1260 f << "###id,";
1261 ascFile.addPos(pos);
1262 ascFile.addNote(f.str().c_str());
1263 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1264 continue;
1265 }
1266 else if (listIds[0]==0) {
1267 ascFile.addPos(pos);
1268 ascFile.addNote("_");
1269 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1270 continue;
1271 }
1272
1273 cLink.m_dataId=listIds[0];
1274 f << m_clusterManager->getClusterDebugName(listIds[0]) << ",";
1275 cLink.m_subZoneId[0]=long(input->readULong(4)); // 0 or 80000000 and a small int
1276 cLink.m_subZoneId[1]=long(input->readLong(4)); // small int
1277 f << cLink;
1278 listLinks[size_t(i+1)]=cLink;
1279 ascFile.addPos(pos);
1280 ascFile.addNote(f.str().c_str());
1281 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1282 }
1283 if (input->tell()!=zone.m_entry.end()) {
1284 f.str("");
1285 f << zoneName << ":end";
1286 ascFile.addPos(input->tell());
1287 ascFile.addNote(f.str().c_str());
1288 }
1289
1290 return true;
1291 }
1292
1293 ////////////////////////////////////////////////////////////
1294 // structured zone
1295 ////////////////////////////////////////////////////////////
1296 bool RagTime5Document::readDocInfoClusterData(RagTime5Zone &zone, MWAWEntry const &entry)
1297 {
1298 if (!entry.valid() || entry.length()<160) {
1299 MWAW_DEBUG_MSG(("RagTime5Document::readDocInfoClusterData: the entry does not seems valid\n"));
1300 return false;
1301 }
1302 libmwaw::DebugFile &ascFile=zone.ascii();
1303 libmwaw::DebugStream f;
1304 MWAWInputStreamPtr input=zone.getInput();
1305 long pos=entry.begin();
1306 input->seek(pos, librevenge::RVNG_SEEK_SET);
1307
1308 f << "DocInfo[dataA]:";
1309 // checkme the field data seems always in hilo endian...
1310 bool actEndian=input->readInverted();
1311 input->setReadInverted(false);
1312
1313 auto val=static_cast<int>(input->readULong(2)); // always 0
1314 if (val) f << "f0=" << val;
1315 auto dataSz=long(input->readULong(4));
1316 if (pos+dataSz>entry.end()) {
1317 MWAW_DEBUG_MSG(("RagTime5Document::readDocInfoClusterData: the main data size seems bad\n"));
1318 f << "###dSz=" << dataSz << ",";
1319 ascFile.addDelimiter(input->tell(),'|');
1320 ascFile.addPos(pos);
1321 ascFile.addNote(f.str().c_str());
1322 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1323 input->setReadInverted(actEndian);
1324 return true;
1325 }
1326 for (int i=0; i<2; ++i) { // f1=2
1327 val=static_cast<int>(input->readULong(2));
1328 if (val) f << "f" << i << "=" << val << ",";
1329 }
1330 auto sSz=static_cast<int>(input->readULong(1));
1331 long actPos=input->tell();
1332 if (sSz>25) {
1333 MWAW_DEBUG_MSG(("RagTime5Document::readDocInfoClusterData: the dataA string size seems bad\n"));
1334 f << "###sSz=" << sSz << ",";
1335 sSz=0;
1336 }
1337 std::string text("");
1338 for (int i=0; i<sSz; ++i) text += char(input->readULong(1));
1339 f << text << ",";
1340 input->seek(actPos+25, librevenge::RVNG_SEEK_SET);
1341 f << "IDS=["; // maybe some char
1342 for (int i=0; i<7; ++i) { // _, ?, ?, ?, 0, 0|4, ?
1343 val=static_cast<int>(input->readULong(2));
1344 if (val) f << std::hex << val << std::dec << ",";
1345 else f << "_,";
1346 }
1347 f << "],";
1348 sSz=static_cast<int>(input->readULong(1));
1349 actPos=input->tell();
1350 if (sSz>62) {
1351 MWAW_DEBUG_MSG(("RagTime5Document::readDocInfoClusterData: the dataA string2 size seems bad\n"));
1352 f << "###sSz2=" << sSz << ",";
1353 sSz=0;
1354 }
1355 text=("");
1356 for (int i=0; i<sSz; ++i) text += char(input->readULong(1));
1357 f << text << ",";
1358 input->seek(actPos+63, librevenge::RVNG_SEEK_SET);
1359 ascFile.addPos(pos);
1360 ascFile.addNote(f.str().c_str());
1361
1362 pos=input->tell();
1363 f.str("");
1364 f << "DocInfo[dataB]:";
1365 f << "IDS=["; // maybe some char
1366 for (int i=0; i<8; ++i) {
1367 val=static_cast<int>(input->readULong(2));
1368 if (val) f << std::hex << val << std::dec << ",";
1369 else f << "_,";
1370 }
1371 f << "],";
1372 for (int i=0; i<11; ++i) { // f0=-1|2|6, f1=-1|2|4, f3=0|17|21,
1373 val=static_cast<int>(input->readLong(2));
1374 if (val) f << "f" << i << "=" << val << ",";
1375 }
1376 val=static_cast<int>(input->readLong(1)); // 0
1377 if (val) f << "f11=" << val << ",";
1378 sSz=static_cast<int>(input->readULong(1));
1379 if (sSz>64||pos+sSz+4>entry.end()) {
1380 MWAW_DEBUG_MSG(("RagTime5Document::readDocInfoClusterData: the string size for dataB data seems bad\n"));
1381 f << "###sSz3=" << sSz << ",";
1382 ascFile.addDelimiter(input->tell(),'|');
1383 ascFile.addPos(pos);
1384 ascFile.addNote(f.str().c_str());
1385 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1386 input->setReadInverted(actEndian);
1387 return true;
1388 }
1389 text=("");
1390 for (int i=0; i<sSz; ++i) text += char(input->readULong(1));
1391 f << text << ",";
1392 if ((sSz%2)==1)
1393 input->seek(1, librevenge::RVNG_SEEK_CUR);
1394 ascFile.addPos(pos);
1395 ascFile.addNote(f.str().c_str());
1396
1397 pos=input->tell();
1398 f.str("");
1399 f << "DocInfo[dataC]:";
1400 if (input->readLong(2)!=1 || (val=static_cast<int>(input->readLong(2)))<=0 || (val%4) || pos+6+val>entry.end()) {
1401 MWAW_DEBUG_MSG(("RagTime5Document::readDocInfoClusterData: oops something is bad[dataC]\n"));
1402 f << "###val=" << val << ",";
1403 ascFile.addPos(pos);
1404 ascFile.addNote(f.str().c_str());
1405 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1406 input->setReadInverted(actEndian);
1407 return true;
1408 }
1409 int N=val/4;
1410 f << "list=[";
1411 for (int i=0; i<N; ++i) {
1412 val=static_cast<int>(input->readLong(4));
1413 if (val)
1414 f << std::hex << val << std::dec << ",";
1415 else
1416 f << "_,";
1417 }
1418 f << "],";
1419 val=static_cast<int>(input->readLong(2)); // always 2
1420 if (val!=2) f << "f0=" << val << ",";
1421 sSz=static_cast<int>(input->readULong(2));
1422 if (input->tell()+sSz+4>entry.end()) {
1423 MWAW_DEBUG_MSG(("RagTime5Document::readDocInfoClusterData: string size seems bad[dataC]\n"));
1424 f << "###sSz=" << sSz << ",";
1425 ascFile.addPos(pos);
1426 ascFile.addNote(f.str().c_str());
1427 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1428 input->setReadInverted(actEndian);
1429 return true;
1430 }
1431 text=("");
1432 for (int i=0; i<sSz; ++i) text += char(input->readULong(1));
1433 f << text << ",";
1434 if ((sSz%2)==1)
1435 input->seek(1, librevenge::RVNG_SEEK_CUR);
1436 ascFile.addPos(pos);
1437 ascFile.addNote(f.str().c_str());
1438
1439 pos=input->tell();
1440 f.str("");
1441 f << "DocInfo[dataD]:";
1442 ascFile.addPos(pos);
1443 ascFile.addNote(f.str().c_str());
1444
1445 input->setReadInverted(actEndian);
1446 return true;
1447 }
1448
1449 bool RagTime5Document::readScriptComment(RagTime5Zone &zone)
1450 {
1451 if (!zone.m_entry.valid() ||
1452 zone.getKindLastPart(zone.m_kinds[1].empty())!="Unicode") {
1453 zone.addErrorInDebugFile("ScriptComment");
1454 MWAW_DEBUG_MSG(("RagTime5Document::readScriptComment: the script comment zone %d seems bad\n", zone.m_ids[0]));
1455 return true;
1456 }
1457 readUnicodeString(zone, "ScriptComment");
1458 libmwaw::DebugStream f;
1459 for (auto const &it : zone.m_childIdToZoneMap) {
1460 auto child=it.second;
1461 if (!child || child->m_isParsed) continue;
1462 child->m_isParsed=true;
1463 switch (it.first) {
1464 case 3: // find one time with no data
1465 if (child->m_entry.valid()) {
1466 MWAW_DEBUG_MSG(("RagTime5Document::readScriptComment: find data with child3\n"));
1467 libmwaw::DebugFile &ascFile=child->ascii();
1468 f.str("");
1469 f << "ScriptComment[" << *child << "child3]:";
1470 ascFile.addPos(child->m_entry.begin());
1471 ascFile.addNote(f.str().c_str());
1472 ascFile.addPos(child->m_entry.end());
1473 ascFile.addNote("_");
1474 }
1475 break;
1476 case 8:
1477 ascii().addPos(child->m_defPosition);
1478 ascii().addNote("scriptComment[refCount]");
1479 break;
1480 default: {
1481 std::string kind=child->getKindLastPart();
1482 if (kind=="Unicode") { // the script name
1483 child->m_hiLoEndian=zone.m_hiLoEndian;
1484 readUnicodeString(*child, "ScriptNameData");
1485 break;
1486 }
1487 if (kind=="32Bit") {
1488 if (child->m_variableD[0]!=0 || child->m_variableD[1]!=1) { // do not show in meny
1489 MWAW_DEBUG_MSG(("RagTime5Document::readScriptComment: find unknown flag\n"));
1490 ascii().addPos(child->m_defPosition);
1491 ascii().addNote("scriptData[showInMenu]:###");
1492 }
1493 if (child->m_entry.valid()) {
1494 libmwaw::DebugFile &ascFile=child->ascii();
1495 f.str("");
1496 f << "Entries(ScriptData)[" << *child << "]:###";
1497 MWAW_DEBUG_MSG(("RagTime5Document::readScriptComment: find unknown script data\n"));
1498 ascFile.addPos(child->m_entry.begin());
1499 ascFile.addNote(f.str().c_str());
1500 ascFile.addPos(child->m_entry.end());
1501 ascFile.addNote("_");
1502 }
1503 break;
1504 }
1505 if (kind=="OSAScript") {
1506 if (child->m_entry.valid()) {
1507 libmwaw::DebugFile &ascFile=child->ascii();
1508 f.str("");
1509 f << "Entries(OSAScript)[" << *child << "]:";
1510 ascFile.addPos(child->m_entry.begin());
1511 ascFile.addNote(f.str().c_str());
1512 ascFile.addPos(child->m_entry.end());
1513 ascFile.addNote("_");
1514 }
1515 break;
1516 }
1517 MWAW_DEBUG_MSG(("RagTime5Document::readScriptComment: find unknown child zone\n"));
1518 child->addErrorInDebugFile("ScriptComment");
1519 break;
1520 }
1521 }
1522 }
1523 return true;
1524 }
1525
1526 bool RagTime5Document::readClusterGProp(RagTime5ClusterManager::Cluster &cluster)
1527 {
1528 RagTime5ClusterManager::Link const &link=cluster.m_dataLink;
1529 if (link.m_ids.size()<2 || !link.m_ids[1]) {
1530 MWAW_DEBUG_MSG(("RagTime5Document::readClusterGProp: can not find the main data\n"));
1531 return false;
1532 }
1533 // probably a cluster with only on field, so...
1534 RagTime5StructManager::GObjPropFieldParser defaultParser("RootGObjProp");
1535 if (!readStructZone(link, defaultParser, 8, &cluster.m_nameLink)) {
1536 auto dataZone=getDataZone(link.m_ids[1]);
1537 if (dataZone)
1538 dataZone->addErrorInDebugFile("RootGObjProp");
1539 MWAW_DEBUG_MSG(("RagTime5Document::readClusterGProp: unexpected type for zone %d\n", link.m_ids[1]));
1540 }
1541
1542 for (auto const &lnk : cluster.m_linksList) {
1543 MWAW_DEBUG_MSG(("RagTime5Document::readClusterGProp: find extra data\n"));
1544 RagTime5StructManager::DataParser defParser("UnknBUnknown2");
1545 readFixedSizeZone(lnk, defParser);
1546 }
1547
1548 return true;
1549 }
1550
1551 bool RagTime5Document::readUnknownClusterCData(RagTime5ClusterManager::Cluster const &cluster)
1552 {
1553 RagTime5ClusterManager::Link const &link=cluster.m_dataLink;
1554 if (link.m_ids.empty()) {
1555 MWAW_DEBUG_MSG(("RagTime5Document::readUnknownClusterCData: can not find the main data\n"));
1556 return false;
1557 }
1558 std::stringstream s;
1559 s << "UnknC_" << char('A'+link.m_fileType[0]) << "_";
1560 std::string zoneName=s.str();
1561
1562 if (link.m_type==RagTime5ClusterManager::Link::L_List) {
1563 if (link.m_fileType[1]==0x310) {
1564 // find id=8,"Rechenblatt 1": spreadsheet name ?
1565 RagTime5DocumentInternal::IndexUnicodeParser parser(*this, true, zoneName+"0");
1566 readListZone(link, parser);
1567 }
1568 else {
1569 RagTime5StructManager::DataParser parser(zoneName+"0");
1570 readListZone(link, parser);
1571 }
1572 }
1573 else {
1574 RagTime5StructManager::DataParser defaultParser(zoneName+"0");
1575 readFixedSizeZone(link, defaultParser);
1576 }
1577 for (auto const &lnk : cluster.m_linksList) {
1578 RagTime5StructManager::DataParser parser(zoneName+"1");
1579 readFixedSizeZone(lnk, parser);
1580 }
1581
1582 return true;
1583 }
1584
1585 bool RagTime5Document::readListZone(RagTime5ClusterManager::Link const &link)
1586 {
1587 RagTime5StructManager::DataParser parser(link.getZoneName());
1588 return readListZone(link, parser);
1589 }
1590
1591 bool RagTime5Document::readListZone(RagTime5ClusterManager::Link const &link, RagTime5StructManager::DataParser &parser)
1592 {
1593 if (link.m_ids.size()<2 || !link.m_ids[1])
1594 return false;
1595
1596 std::vector<long> decal;
1597 if (link.m_ids[0])
1598 readPositions(link.m_ids[0], decal);
1599 if (decal.empty())
1600 decal=link.m_longList;
1601
1602 int const dataId=link.m_ids[1];
1603 auto dataZone=getDataZone(dataId);
1604 auto N=int(decal.size());
1605
1606 if (!dataZone || !dataZone->m_entry.valid() ||
1607 dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData" || N<=1) {
1608 if (N==1 && dataZone && !dataZone->m_entry.valid()) {
1609 // a zone with 0 zone is ok...
1610 dataZone->m_isParsed=true;
1611 libmwaw::DebugStream f;
1612 f << "[" << parser.getZoneName() << "]";
1613 ascii().addPos(dataZone->m_defPosition);
1614 ascii().addNote(f.str().c_str());
1615 return true;
1616 }
1617 MWAW_DEBUG_MSG(("RagTime5Document::readListZone: the data zone %d seems bad\n", dataId));
1618 return false;
1619 }
1620
1621 dataZone->m_isParsed=true;
1622 MWAWEntry entry=dataZone->m_entry;
1623 libmwaw::DebugFile &ascFile=dataZone->ascii();
1624 libmwaw::DebugStream f;
1625 f << "Entries(" << parser.getZoneName() << ")[" << *dataZone << "]:";
1626 ascFile.addPos(entry.end());
1627 ascFile.addNote("_");
1628 ascFile.addPos(entry.begin());
1629 ascFile.addNote(f.str().c_str());
1630
1631 MWAWInputStreamPtr input=dataZone->getInput();
1632 input->setReadInverted(!dataZone->m_hiLoEndian);
1633 long debPos=entry.begin();
1634 long endPos=entry.end();
1635
1636 for (int i=0; i<N-1; ++i) {
1637 long pos=decal[size_t(i)], lastPos=decal[size_t(i+1)];
1638 if (pos==lastPos) continue;
1639 if (pos<0 || pos>lastPos || debPos+lastPos>endPos) {
1640 MWAW_DEBUG_MSG(("RagTime5Document::readListZone: can not read the data zone %d-%d seems bad\n", dataId, i));
1641 continue;
1642 }
1643 input->seek(debPos+pos, librevenge::RVNG_SEEK_SET);
1644 f.str("");
1645 f << parser.getZoneName(i+1) << ":";
1646 if (!parser.parseData(input, debPos+lastPos, *dataZone, i+1, f))
1647 f << "###";
1648 ascFile.addPos(debPos+pos);
1649 ascFile.addNote(f.str().c_str());
1650 ascFile.addPos(debPos+lastPos);
1651 ascFile.addNote("_");
1652 }
1653
1654 input->setReadInverted(false);
1655 return true;
1656 }
1657
1658 bool RagTime5Document::readFixedSizeZone(RagTime5ClusterManager::Link const &link, std::string const &name)
1659 {
1660 RagTime5StructManager::DataParser parser(name.empty() ? link.getZoneName() : name);
1661 return readFixedSizeZone(link, parser);
1662 }
1663
1664 bool RagTime5Document::readFixedSizeZone(RagTime5ClusterManager::Link const &link, RagTime5StructManager::DataParser &parser)
1665 {
1666 if (link.m_ids.empty() || !link.m_ids[0])
1667 return false;
1668
1669 int const dataId=link.m_ids[0];
1670 auto dataZone=getDataZone(dataId);
1671
1672 if (!dataZone || !dataZone->m_entry.valid() ||
1673 dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData" ||
1674 link.m_fieldSize<=0 || link.m_N>dataZone->m_entry.length()/link.m_fieldSize ||
1675 link.m_N>dataZone->m_entry.length() || link.m_N<0) {
1676 if ((link.m_N==0 || link.m_fieldSize==0) && dataZone && !dataZone->m_entry.valid()) {
1677 // a zone with 0 zone is ok...
1678 dataZone->m_isParsed=true;
1679 return true;
1680 }
1681 MWAW_DEBUG_MSG(("RagTime5Document::readFixedSizeZone: the data zone %d seems bad\n", dataId));
1682 if (dataZone) dataZone->addErrorInDebugFile(parser.getZoneName());
1683 return false;
1684 }
1685
1686 dataZone->m_isParsed=true;
1687 MWAWEntry entry=dataZone->m_entry;
1688 libmwaw::DebugFile &ascFile=dataZone->ascii();
1689 libmwaw::DebugStream f;
1690 f << "Entries(" << parser.getZoneName() << ")[" << *dataZone << "]:";
1691 ascFile.addPos(entry.end());
1692 ascFile.addNote("_");
1693 ascFile.addPos(entry.begin());
1694 ascFile.addNote(f.str().c_str());
1695
1696 MWAWInputStreamPtr input=dataZone->getInput();
1697 input->setReadInverted(!dataZone->m_hiLoEndian);
1698 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1699 long endPos=entry.end();
1700
1701 for (int i=0; i<link.m_N; ++i) {
1702 long pos=input->tell();
1703 f.str("");
1704 f << parser.getZoneName(i+1) << ":";
1705 if (!parser.parseData(input, pos+link.m_fieldSize, *dataZone, i+1, f))
1706 f << "###";
1707 ascFile.addPos(pos);
1708 ascFile.addNote(f.str().c_str());
1709 input->seek(pos+link.m_fieldSize, librevenge::RVNG_SEEK_SET);
1710 }
1711 long pos=input->tell();
1712 if (pos!=endPos) {
1713 f.str("");
1714 f << parser.getZoneName() << ":#end";
1715 ascFile.addPos(pos);
1716 ascFile.addNote(f.str().c_str());
1717 }
1718 input->setReadInverted(false);
1719 return true;
1720 }
1721
1722 bool RagTime5Document::readStructZone(RagTime5ClusterManager::Link const &link, RagTime5StructManager::FieldParser &parser, int headerSz, RagTime5ClusterManager::NameLink *nameLink)
1723 {
1724 if (link.m_ids.size()<2 || !link.m_ids[1])
1725 return false;
1726
1727 std::map<int, librevenge::RVNGString> idToNameMap;
1728 if (nameLink && !nameLink->empty()) {
1729 readUnicodeStringList(*nameLink, idToNameMap);
1730 *nameLink=RagTime5ClusterManager::NameLink();
1731 }
1732 std::vector<long> decal;
1733 if (link.m_ids[0])
1734 readPositions(link.m_ids[0], decal);
1735 if (decal.empty())
1736 decal=link.m_longList;
1737 int const dataId=link.m_ids[1];
1738 auto dataZone=getDataZone(dataId);
1739 if (!dataZone || !dataZone->m_entry.valid() ||
1740 dataZone->getKindLastPart(dataZone->m_kinds[1].empty())!="ItemData") {
1741 if (decal.size()==1) {
1742 // a zone with 0 zone is ok...
1743 if (dataZone)
1744 dataZone->m_isParsed=true;
1745 return true;
1746 }
1747 MWAW_DEBUG_MSG(("RagTime5Document::readStructZone: the data zone %d seems bad\n", dataId));
1748 return false;
1749 }
1750 dataZone->m_isParsed=true;
1751 MWAWEntry entry=dataZone->m_entry;
1752 libmwaw::DebugFile &ascFile=dataZone->ascii();
1753 libmwaw::DebugStream f;
1754 f << "Entries(" << parser.getZoneName() << ")[" << *dataZone << "]:";
1755 ascFile.addPos(entry.end());
1756 ascFile.addNote("_");
1757 ascFile.addPos(entry.begin());
1758 ascFile.addNote(f.str().c_str());
1759
1760 auto N=int(decal.size());
1761 MWAWInputStreamPtr input=dataZone->getInput();
1762 input->setReadInverted(!dataZone->m_hiLoEndian);
1763 long debPos=entry.begin();
1764 long endPos=entry.end();
1765 if (N==0) {
1766 MWAW_DEBUG_MSG(("RagTime5Document::readStructZone: can not find decal list for zone %d, let try to continue\n", dataId));
1767 input->seek(debPos, librevenge::RVNG_SEEK_SET);
1768 int n=0;
1769 while (input->tell()+8 < endPos) {
1770 long pos=input->tell();
1771 int id=++n;
1772 librevenge::RVNGString name("");
1773 if (idToNameMap.find(id)!=idToNameMap.end())
1774 name=idToNameMap.find(id)->second;
1775 if (!readStructData(*dataZone, endPos, id, headerSz, parser, name)) {
1776 input->seek(pos, librevenge::RVNG_SEEK_SET);
1777 break;
1778 }
1779 }
1780 if (input->tell()!=endPos) {
1781 static bool first=true;
1782 if (first) {
1783 MWAW_DEBUG_MSG(("RagTime5Document::readStructZone: can not read some block\n"));
1784 first=false;
1785 }
1786 ascFile.addPos(debPos);
1787 ascFile.addNote("###");
1788 }
1789 }
1790 else {
1791 for (int i=0; i<N-1; ++i) {
1792 long pos=decal[size_t(i)];
1793 long nextPos=decal[size_t(i+1)];
1794 if (pos<0 || debPos+pos>endPos) {
1795 MWAW_DEBUG_MSG(("RagTime5Document::readStructZone: can not read the data zone %d-%d seems bad\n", dataId, i));
1796 continue;
1797 }
1798 librevenge::RVNGString name("");
1799 if (idToNameMap.find(i+1)!=idToNameMap.end())
1800 name=idToNameMap.find(i+1)->second;
1801 input->seek(debPos+pos, librevenge::RVNG_SEEK_SET);
1802 readStructData(*dataZone, debPos+nextPos, i+1, headerSz, parser, name);
1803 if (input->tell()!=debPos+nextPos) {
1804 static bool first=true;
1805 if (first) {
1806 MWAW_DEBUG_MSG(("RagTime5Document::readStructZone: can not read some block\n"));
1807 first=false;
1808 }
1809 ascFile.addPos(debPos+pos);
1810 ascFile.addNote("###");
1811 }
1812 }
1813 }
1814 return true;
1815 }
1816
1817 bool RagTime5Document::readStructData(RagTime5Zone &zone, long endPos, int n, int headerSz,
1818 RagTime5StructManager::FieldParser &parser, librevenge::RVNGString const &dataName)
1819 {
1820 MWAWInputStreamPtr input=zone.getInput();
1821 long pos=input->tell();
1822 if ((headerSz && pos+headerSz>endPos) || (headerSz==0 && pos+5>endPos)) return false;
1823 libmwaw::DebugFile &ascFile=zone.ascii();
1824 libmwaw::DebugStream f;
1825 std::string const zoneName=parser.getZoneName(n);
1826 int m=0;
1827 if (headerSz>0) {
1828 f << zoneName << "[A]:";
1829 if (!dataName.empty()) f << dataName.cstr() << ",";
1830 int val;
1831 if (headerSz==14) {
1832 val=static_cast<int>(input->readLong(4));
1833 if (val!=1) f << "numUsed=" << val << ",";
1834 f << "f1=" << std::hex << input->readULong(2) << std::dec << ",";
1835 val=static_cast<int>(input->readLong(2)); // sometimes form an increasing sequence but not always
1836 if (val!=n) f << "id=" << val << ",";
1837
1838 RagTime5StructManager::Field field;
1839 field.m_fileType=input->readULong(4);
1840 field.m_type=RagTime5StructManager::Field::T_Long;
1841 field.m_longValue[0]=input->readLong(2);
1842 parser.parseHeaderField(field, zone, n, f);
1843 }
1844 else if (headerSz==8) {
1845 val=static_cast<int>(input->readLong(2));
1846 if (val!=1) f << "numUsed=" << val << ",";
1847 val=static_cast<int>(input->readLong(2)); // sometimes form an increasing sequence but not always
1848 if (val!=n) f << "id=" << val << ",";
1849 f << "type=" << std::hex << input->readULong(4) << std::dec << ","; // 0 or 01458042
1850 }
1851 else if (headerSz==18) { // docinfo header
1852 val=static_cast<int>(input->readLong(4)); // 1 or 3
1853 if (val!=1) f << "numUsed?=" << val << ",";
1854 val=static_cast<int>(input->readLong(4)); // always 0
1855 if (val) f << "f0=" << val << ",";
1856 f << "ID=" << std::hex << input->readULong(4) << ","; // a big number
1857 val=static_cast<int>(input->readLong(4));
1858 if (val!=0x1f6817) // doc info type
1859 f << "type=" << std::hex << val << std::dec << ",";
1860 val=static_cast<int>(input->readLong(2)); // always 0
1861 if (val) f << "f1=" << val << ",";
1862 input->seek(pos+headerSz, librevenge::RVNG_SEEK_SET);
1863 }
1864 else {
1865 MWAW_DEBUG_MSG(("RagTime5Document::readStructData: find unknown header size\n"));
1866 f << "###hSz";
1867 input->seek(pos+headerSz, librevenge::RVNG_SEEK_SET);
1868 }
1869 ascFile.addPos(pos);
1870 ascFile.addNote(f.str().c_str());
1871 }
1872 pos=input->tell();
1873 if (parser.m_regroupFields) {
1874 f.str("");
1875 f << zoneName << "[B]:";
1876 if (headerSz==0 && !dataName.empty()) f << dataName.cstr() << ",";
1877 }
1878 while (!input->isEnd()) {
1879 long actPos=input->tell();
1880 if (actPos>=endPos) break;
1881
1882 if (!parser.m_regroupFields) {
1883 f.str("");
1884 f << zoneName << "[B" << ++m << "]:";
1885 if (m==1 && headerSz==0 && !dataName.empty()) f << dataName.cstr() << ",";
1886 }
1887 RagTime5StructManager::Field field;
1888 if (!m_structManager->readField(input, endPos, ascFile, field, headerSz ? 0 : endPos-actPos)) {
1889 input->seek(actPos, librevenge::RVNG_SEEK_SET);
1890 break;
1891 }
1892 if (!parser.parseField(field, zone, n, f))
1893 f << "#" << field;
1894 if (!parser.m_regroupFields) {
1895 ascFile.addPos(actPos);
1896 ascFile.addNote(f.str().c_str());
1897 }
1898 }
1899 if (parser.m_regroupFields && pos!=input->tell()) {
1900 ascFile.addPos(pos);
1901 ascFile.addNote(f.str().c_str());
1902 }
1903 return true;
1904 }
1905
1906 ////////////////////////////////////////////////////////////
1907 // zone unpack/create ascii file, ...
1908 ////////////////////////////////////////////////////////////
1909 bool RagTime5Document::updateZone(std::shared_ptr<RagTime5Zone> &zone)
1910 {
1911 if (!zone || zone->m_isInitialised || zone->m_isParsed)
1912 return true;
1913
1914 zone->m_isInitialised=true;
1915 // update the kinds of this zone
1916 for (int j=1; j<3; ++j) {
1917 if (!zone->m_ids[j]) continue;
1918 if (m_state->m_zoneIdToTypeMap.find(zone->m_ids[j])==m_state->m_zoneIdToTypeMap.end()) {
1919 // the main zone seems to point to a cluster id...
1920 if (zone->m_ids[0]<=6) continue;
1921 MWAW_DEBUG_MSG(("RagTime5Document::updateZone: can not find the type for %d:%d\n", zone->m_ids[0],j));
1922 ascii().addPos(zone->m_defPosition);
1923 ascii().addNote("###type,");
1924 }
1925 else {
1926 zone->m_kinds[j-1]=m_state->m_zoneIdToTypeMap.find(zone->m_ids[j])->second;
1927 libmwaw::DebugStream f;
1928 f << zone->m_kinds[j-1] << ",";
1929 ascii().addPos(zone->m_defPosition);
1930 ascii().addNote(f.str().c_str());
1931 }
1932 }
1933
1934 // update the zone input
1935 if (!zone->m_entriesList.empty() && !updateZoneInput(*zone))
1936 return false;
1937
1938 // check for pack zones and unpack them
1939 int usedId=zone->m_kinds[1].empty() ? 0 : 1;
1940 std::string actType=zone->getKindLastPart(usedId==0);
1941 if (actType=="Pack") {
1942 if (zone->m_entry.valid() && !unpackZone(*zone)) {
1943 MWAW_DEBUG_MSG(("RagTime5Document::updateZone: can not unpack the zone %d\n", zone->m_ids[0]));
1944 libmwaw::DebugStream f;
1945 libmwaw::DebugFile &ascFile=zone->ascii();
1946 f << "Entries(BADPACK)[" << zone << "]:###" << zone->m_kinds[usedId];
1947 ascFile.addPos(zone->m_entry.begin());
1948 ascFile.addNote(f.str().c_str());
1949 zone->m_entry=MWAWEntry();
1950 }
1951 size_t length=zone->m_kinds[usedId].size();
1952 if (length>5)
1953 zone->m_kinds[usedId].resize(length-5);
1954 else
1955 zone->m_kinds[usedId]="";
1956 }
1957
1958 // check hilo flag
1959 usedId=zone->m_kinds[1].empty() ? 0 : 1;
1960 actType=zone->getKindLastPart(usedId==0);
1961 if (actType=="HiLo" || actType=="LoHi") {
1962 zone->m_hiLoEndian=actType=="HiLo";
1963 size_t length=zone->m_kinds[usedId].size();
1964 if (length>5)
1965 zone->m_kinds[usedId].resize(length-5);
1966 else
1967 zone->m_kinds[usedId]="";
1968 }
1969 // update the zone kind
1970 std::string kind=zone->getKindLastPart();
1971 if (kind=="Type") {
1972 size_t length=zone->m_kinds[0].size();
1973 if (length>5)
1974 zone->m_kinds[0].resize(length-5);
1975 else
1976 zone->m_kinds[0]="";
1977 zone->m_extra += "type,";
1978 }
1979
1980 return true;
1981 }
1982 bool RagTime5Document::updateZoneInput(RagTime5Zone &zone)
1983 {
1984 if (zone.getInput() || zone.m_entriesList.empty())
1985 return true;
1986 std::stringstream s;
1987 s << "Zone" << std::hex << zone.m_entriesList[0].begin() << std::dec;
1988 zone.setAsciiFileName(s.str());
1989
1990 MWAWInputStreamPtr input = m_parser->getInput();
1991 if (zone.m_entriesList.size()==1) {
1992 zone.setInput(input);
1993 zone.m_entry=zone.m_entriesList[0];
1994 return true;
1995 }
1996
1997 libmwaw::DebugStream f;
1998 f << "Entries(" << zone.getZoneName() << "):";
1999 std::shared_ptr<MWAWStringStream> newStream;
2000 int n=0;
2001 for (auto const &entry : zone.m_entriesList) {
2002 if (!entry.valid() || !input->checkPosition(entry.end())) {
2003 MWAW_DEBUG_MSG(("RagTime5Document::updateZoneInput: can not read some data\n"));
2004 f << "###";
2005 ascii().addPos(entry.begin());
2006 ascii().addNote(f.str().c_str());
2007 return false;
2008 }
2009 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2010
2011 unsigned long read;
2012 const unsigned char *dt = input->read(static_cast<unsigned long>(entry.length()), read);
2013 if (!dt || long(read) != entry.length()) {
2014 MWAW_DEBUG_MSG(("RagTime5Document::updateZoneInput: can not read some data\n"));
2015 f << "###";
2016 ascii().addPos(entry.begin());
2017 ascii().addNote(f.str().c_str());
2018 return false;
2019 }
2020 ascii().skipZone(entry.begin(), entry.end()-1);
2021 if (n++==0)
2022 newStream.reset(new MWAWStringStream(dt, static_cast<unsigned int>(entry.length())));
2023 else
2024 newStream->append(dt, static_cast<unsigned int>(entry.length()));
2025 }
2026
2027 MWAWInputStreamPtr newInput(new MWAWInputStream(newStream, false));
2028 zone.setInput(newInput);
2029 zone.m_entry.setBegin(0);
2030 zone.m_entry.setLength(newInput->size());
2031
2032 return true;
2033 }
2034
2035 bool RagTime5Document::unpackZone(RagTime5Zone &zone, MWAWEntry const &entry, std::vector<unsigned char> &data)
2036 {
2037 if (!entry.valid())
2038 return false;
2039
2040 MWAWInputStreamPtr input=zone.getInput();
2041 long pos=entry.begin(), endPos=entry.end();
2042 if (entry.length()<4 || !input || !input->checkPosition(endPos)) {
2043 MWAW_DEBUG_MSG(("RagTime5Document::unpackZone: the input seems bad\n"));
2044 return false;
2045 }
2046
2047 bool actEndian=input->readInverted();
2048 input->setReadInverted(false);
2049 input->seek(pos, librevenge::RVNG_SEEK_SET);
2050
2051 data.resize(0);
2052 auto sz=static_cast<unsigned long>(input->readULong(4));
2053 if (sz==0) {
2054 input->setReadInverted(actEndian);
2055 return true;
2056 }
2057 auto flag=int(sz>>24);
2058 sz &= 0xFFFFFF;
2059 if ((flag&0xf) || (flag&0xf0)==0 || !(sz&0xFFFFFF)) {
2060 input->setReadInverted(actEndian);
2061 return false;
2062 }
2063
2064 int nBytesRead=0, szField=9;
2065 unsigned int read=0;
2066 size_t mapPos=0;
2067 data.reserve(size_t(sz));
2068 std::vector<std::vector<unsigned char> > mapToString;
2069 mapToString.reserve(size_t(entry.length()-6));
2070 bool ok=false;
2071 while (!input->isEnd()) {
2072 if (static_cast<int>(mapPos)==(1<<szField)-0x102)
2073 ++szField;
2074 if (input->tell()>=endPos) {
2075 MWAW_DEBUG_MSG(("RagTime5Document::unpackZone: oops can not find last data\n"));
2076 ok=false;
2077 break;
2078 }
2079 do {
2080 read = (read<<8)+static_cast<unsigned int>(input->readULong(1));
2081 nBytesRead+=8;
2082 }
2083 while (nBytesRead<szField);
2084 unsigned int val=(read >> (nBytesRead-szField));
2085 nBytesRead-=szField;
2086 read &= ((1<<nBytesRead)-1);
2087
2088 if (val<0x100) {
2089 auto c=static_cast<unsigned char>(val);
2090 data.push_back(c);
2091 if (mapPos>= mapToString.size())
2092 mapToString.resize(mapPos+1);
2093 mapToString[mapPos++]=std::vector<unsigned char>(1,c);
2094 continue;
2095 }
2096 if (val==0x100) { // begin
2097 if (!data.empty()) {
2098 // data are reset when mapPos=3835, so it is ok
2099 mapPos=0;
2100 mapToString.resize(0);
2101 szField=9;
2102 }
2103 continue;
2104 }
2105 if (val==0x101) {
2106 ok=read==0;
2107 if (!ok) {
2108 MWAW_DEBUG_MSG(("RagTime5Document::unpackZone: find 0x101 in bad position\n"));
2109 }
2110 break;
2111 }
2112 auto readPos=size_t(val-0x102);
2113 if (readPos >= mapToString.size()) {
2114 MWAW_DEBUG_MSG(("RagTime5Document::unpackZone: find bad position\n"));
2115 ok = false;
2116 break;
2117 }
2118 std::vector<unsigned char> final=mapToString[readPos++];
2119 if (readPos==mapToString.size())
2120 final.push_back(final[0]);
2121 else
2122 final.push_back(mapToString[readPos][0]);
2123 data.insert(data.end(), final.begin(), final.end());
2124 if (mapPos>= mapToString.size())
2125 mapToString.resize(mapPos+1);
2126 mapToString[mapPos++]=final;
2127 }
2128
2129 if (ok && data.size()!=size_t(sz)) {
2130 MWAW_DEBUG_MSG(("RagTime5Document::unpackZone: oops the data file is bad\n"));
2131 ok=false;
2132 }
2133 if (!ok) {
2134 MWAW_DEBUG_MSG(("RagTime5Document::unpackZone: stop with mapPos=%ld and totalSize=%ld/%ld\n", long(mapPos), long(data.size()), long(sz)));
2135 }
2136 input->setReadInverted(actEndian);
2137 return ok;
2138 }
2139
2140 bool RagTime5Document::unpackZone(RagTime5Zone &zone)
2141 {
2142 if (!zone.m_entry.valid())
2143 return false;
2144
2145 std::vector<unsigned char> newData;
2146 if (!unpackZone(zone, zone.m_entry, newData))
2147 return false;
2148 long pos=zone.m_entry.begin(), endPos=zone.m_entry.end();
2149 MWAWInputStreamPtr input=zone.getInput();
2150 if (input->tell()!=endPos) {
2151 MWAW_DEBUG_MSG(("RagTime5Document::unpackZone: find some extra data\n"));
2152 return false;
2153 }
2154 if (newData.empty()) {
2155 // empty zone
2156 zone.ascii().addPos(pos);
2157 zone.ascii().addNote("_");
2158 zone.m_entry.setLength(0);
2159 zone.m_extra += "packed,";
2160 return true;
2161 }
2162
2163 if (input.get()==m_parser->getInput().get())
2164 ascii().skipZone(pos, endPos-1);
2165
2166 std::shared_ptr<MWAWStringStream> newStream(new MWAWStringStream(&newData[0], static_cast<unsigned int>(newData.size())));
2167 MWAWInputStreamPtr newInput(new MWAWInputStream(newStream, false));
2168 zone.setInput(newInput);
2169 zone.m_entry.setBegin(0);
2170 zone.m_entry.setLength(newInput->size());
2171 zone.m_extra += "packed,";
2172 return true;
2173 }
2174
2175 ////////////////////////////////////////////////////////////
2176 // read the different zones
2177 ////////////////////////////////////////////////////////////
2178 bool RagTime5Document::readDocumentVersion(RagTime5Zone &zone)
2179 {
2180 MWAWInputStreamPtr input = zone.getInput();
2181 MWAWEntry &entry=zone.m_entry;
2182
2183 zone.m_isParsed=true;
2184 ascii().addPos(zone.m_defPosition);
2185 ascii().addNote("doc[version],");
2186
2187 libmwaw::DebugFile &ascFile=zone.ascii();
2188 libmwaw::DebugStream f;
2189 f << "Entries(DocVersion):";
2190 ascFile.addPos(entry.end());
2191 ascFile.addNote("_");
2192 if ((entry.length())%6!=2) {
2193 MWAW_DEBUG_MSG(("RagTime5Document::readDocumentVersion: the entry size seem bads\n"));
2194 f << "###";
2195 ascFile.addPos(entry.begin());
2196 ascFile.addNote(f.str().c_str());
2197 return true;
2198 }
2199 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2200 auto val=static_cast<int>(input->readLong(1)); // find 2-4
2201 f << "f0=" << val << ",";
2202 val=static_cast<int>(input->readLong(1)); // always 0
2203 if (val)
2204 f << "f1=" << val << ",";
2205 auto N=int(entry.length()/6);
2206 for (int i=0; i<N; ++i) {
2207 // v0: last used version, v1: first used version, ... ?
2208 f << "v" << i << "=" << input->readLong(1);
2209 val = static_cast<int>(input->readULong(1));
2210 if (val)
2211 f << "." << val;
2212 val = static_cast<int>(input->readULong(1)); // 20|60|80
2213 if (val != 0x80)
2214 f << ":" << std::hex << val << std::dec;
2215 for (int j=0; j<3; ++j) { // often 0 or small number
2216 val = static_cast<int>(input->readULong(1));
2217 if (val)
2218 f << ":" << val << "[" << j << "]";
2219 }
2220 f << ",";
2221 }
2222 ascFile.addPos(entry.begin());
2223 ascFile.addNote(f.str().c_str());
2224 return true;
2225 }
2226
2227 ////////////////////////////////////////////////////////////
2228 // find the different zones in a OLE1 struct files
2229 ////////////////////////////////////////////////////////////
2230 bool RagTime5Document::findZones(MWAWEntry const &entry)
2231 {
2232 libmwaw::DebugStream f;
2233 MWAWInputStreamPtr input = m_parser->getInput();
2234 long pos=entry.begin();
2235 if (!input->checkPosition(entry.end())) {
2236 MWAW_DEBUG_MSG(("RagTime5Document::findZones: main entry seems too bad\n"));
2237 f << "###";
2238 ascii().addPos(pos);
2239 ascii().addNote(f.str().c_str());
2240 return false;
2241 }
2242
2243 int n=0;
2244 input->seek(pos, librevenge::RVNG_SEEK_SET);
2245
2246 std::shared_ptr<RagTime5Zone> actualZone, actualChildZone; // the actual zone
2247 while (!input->isEnd()) {
2248 pos=input->tell();
2249 if (pos>=entry.end()) break;
2250 auto level=static_cast<int>(input->readULong(1));
2251 if (level==0x18) {
2252 while (input->tell()<entry.end()) {
2253 if (input->readULong(1)==0xFF)
2254 continue;
2255 input->seek(-1, librevenge::RVNG_SEEK_CUR);
2256 break;
2257 }
2258 ascii().addPos(pos);
2259 ascii().addNote("_");
2260 continue;
2261 }
2262 f.str("");
2263 // create a new zone, set the default input and default ascii file
2264 std::shared_ptr<RagTime5Zone> zone(new RagTime5Zone(input, ascii()));
2265 zone->m_defPosition=pos;
2266 zone->m_level=level;
2267 // level=3: 0001, 59-78 + sometimes g4=[_,1]
2268 if (pos+4>entry.end() || level < 1 || level > 3) {
2269 zone->m_extra=f.str();
2270 if (n++==0)
2271 f << "Entries(Zones)[1]:";
2272 else
2273 f << "Zones-" << n << ":";
2274 f << *zone << "###";
2275 MWAW_DEBUG_MSG(("RagTime5Document::findZones: find unknown level\n"));
2276 ascii().addPos(pos);
2277 ascii().addNote(f.str().c_str());
2278 break;
2279 }
2280 for (int i=0; i<4-level; ++i) {
2281 zone->m_idsFlag[i]=static_cast<int>(input->readULong(2)); // alway 0/1?
2282 zone->m_ids[i]=static_cast<int>(input->readULong(2));
2283 }
2284 bool ok=true;
2285 do {
2286 auto type2=static_cast<int>(input->readULong(1));
2287 switch (type2) {
2288 case 4: // always 0, 1
2289 case 0xa: // always 0, 0: never seens in v5 but frequent in v6
2290 case 0xb: { // find in some pc file
2291 ok = input->tell()+4+(type2==4 ? 1 : 0)<=entry.end();
2292 if (!ok) break;
2293 int data[2];
2294 for (int &i : data)
2295 i=static_cast<int>(input->readULong(2));
2296 if (type2==4) {
2297 if (data[0]==0 && data[1]==1)
2298 f << "selected,";
2299 else if (data[0]==0)
2300 f << "#selected=" << data[1] << ",";
2301 else
2302 f << "#selected=[" << data[0] << "," << data[1] << "],";
2303 }
2304 else
2305 f << "g" << std::hex << type2 << std::dec << "=[" << data[0] << "," << data[1] << "],";
2306 break;
2307 }
2308 case 5:
2309 case 6: { // 6 entry followed by other data
2310 ok = input->tell()+8+(type2==6 ? 1 : 0)<=entry.end();
2311 if (!ok) break;
2312 MWAWEntry zEntry;
2313 zEntry.setBegin(long(input->readULong(4)));
2314 zEntry.setLength(long(input->readULong(4)));
2315 zone->m_entriesList.push_back(zEntry);
2316 break;
2317 }
2318 case 9:
2319 ok=input->tell()<=entry.end();
2320 break;
2321 case 0xd: // always 0 || c000
2322 ok = input->tell()+4<=entry.end();
2323 if (!ok) break;
2324 for (int &i : zone->m_variableD)
2325 i=static_cast<int>(input->readULong(2));
2326 break;
2327 case 0x18:
2328 while (input->tell()<entry.end()) {
2329 if (input->readULong(1)==0xFF)
2330 continue;
2331 input->seek(-1, librevenge::RVNG_SEEK_CUR);
2332 break;
2333 }
2334 ok=input->tell()+1<entry.end();
2335 break;
2336 default:
2337 ok=false;
2338 MWAW_DEBUG_MSG(("RagTime5Document::findZones: find unknown type2=%d\n", type2));
2339 f << "type2=" << type2 << ",";
2340 break;
2341 }
2342 if (!ok || (type2&1) || (type2==0xa))
2343 break;
2344 }
2345 while (1);
2346 switch (zone->m_level) {
2347 case 1:
2348 actualZone=zone;
2349 actualChildZone.reset();
2350 break;
2351 case 2:
2352 if (!actualZone || actualZone->m_childIdToZoneMap.find(zone->m_ids[0])!=actualZone->m_childIdToZoneMap.end()) {
2353 MWAW_DEBUG_MSG(("RagTime5Document::findZones: can not add child to a zone %d\n", zone->m_ids[0]));
2354 f << "##badChild";
2355 }
2356 else {
2357 zone->m_parentName=actualZone->getZoneName();
2358 actualZone->m_childIdToZoneMap[zone->m_ids[0]]=zone;
2359 }
2360 actualChildZone=zone;
2361 break;
2362 case 3:
2363 if (!actualChildZone || actualChildZone->m_childIdToZoneMap.find(zone->m_ids[0])!=actualChildZone->m_childIdToZoneMap.end()) {
2364 // checkme: can happen in 6.0 files after a jpeg picture with level 1, ...
2365 MWAW_DEBUG_MSG(("RagTime5Document::findZones: can not add child to a zone %d\n", zone->m_ids[0]));
2366 f << "#noparent";
2367 }
2368 else {
2369 zone->m_parentName=actualChildZone->getZoneName();
2370 actualChildZone->m_childIdToZoneMap[zone->m_ids[0]]=zone;
2371 }
2372 break;
2373 default:
2374 break;
2375 }
2376
2377 // store 1 level zone (expect the first one which is the main info zone)
2378 if (!m_state->m_zonesList.empty() && zone->m_level==1) {
2379 if (m_state->m_dataIdZoneMap.find(zone->m_ids[0])!=m_state->m_dataIdZoneMap.end()) {
2380 MWAW_DEBUG_MSG(("RagTime5Document::findZonesKind: data zone with id=%d already exists\n", zone->m_ids[0]));
2381 }
2382 else
2383 m_state->m_dataIdZoneMap[zone->m_ids[0]]=zone;
2384 }
2385
2386 m_state->m_zonesList.push_back(zone);
2387 zone->m_extra=f.str();
2388 f.str("");
2389 if (n++==0)
2390 f << "Entries(Zones)[1]:";
2391 else
2392 f << "Zones-" << n << ":";
2393 f << *zone;
2394
2395
2396 if (!ok) {
2397 MWAW_DEBUG_MSG(("RagTime5Document::findZones: find unknown data\n"));
2398 f << "###";
2399 if (input->tell()!=pos)
2400 ascii().addDelimiter(input->tell(),'|');
2401 ascii().addPos(pos);
2402 ascii().addNote(f.str().c_str());
2403 break;
2404 }
2405 ascii().addPos(pos);
2406 ascii().addNote(f.str().c_str());
2407 }
2408 return true;
2409 }
2410
2411 ////////////////////////////////////////////////////////////
2412 //
2413 // Low level
2414 //
2415 ////////////////////////////////////////////////////////////
2416
2417 ////////////////////////////////////////////////////////////
2418 // color map
2419 ////////////////////////////////////////////////////////////
2420
2421 ////////////////////////////////////////////////////////////
2422 // read the header
2423 ////////////////////////////////////////////////////////////
2424 bool RagTime5Document::checkIsSpreadsheet()
2425 {
2426 if (m_state->m_zonesList.empty() && !findZones(m_state->m_zonesEntry))
2427 return false;
2428 if (m_state->m_zonesList.size()<20)
2429 return false;
2430 if (!m_state->m_zonesList[0] || !findZonesKind())
2431 return false;
2432 if (!parseMainZoneInfoData(*m_state->m_zonesList[0]))
2433 return false;
2434
2435 auto dZone=getDataZone(m_state->m_mainClusterId);
2436 if (!dZone)
2437 return false;
2438 updateZone(dZone);
2439 std::shared_ptr<RagTime5ClusterManager::Cluster> cluster=m_clusterManager->readRootCluster(*dZone);
2440 if (!cluster)
2441 return false;
2442 auto root=std::dynamic_pointer_cast<RagTime5ClusterManager::ClusterRoot>(cluster);
2443 if (!root || !root->m_listClusterId)
2444 return false;
2445 auto lZone=getDataZone(root->m_listClusterId);
2446 if (!lZone)
2447 return false;
2448 updateZone(lZone);
2449 if (!lZone || lZone->getKindLastPart(lZone->m_kinds[1].empty())!="ItemData" ||
2450 lZone->m_entry.length()<24 || (lZone->m_entry.length()%8))
2451 return false;
2452
2453 MWAWEntry const &entry=lZone->m_entry;
2454 MWAWInputStreamPtr input=lZone->getInput();
2455 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2456 input->setReadInverted(!lZone->m_hiLoEndian);
2457 auto N=int(entry.length()/8);
2458 bool firstFound=false;
2459 // look a file which begins by a spreadsheet and which has no layout, no other spreadsheet, ...
2460 for (int i=0; i<N; ++i) {
2461 long pos=input->tell();
2462 std::vector<int> listIds;
2463 if (!m_structManager->readDataIdList(input, 1, listIds) || listIds.empty() || listIds[0]==0) {
2464 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
2465 continue;
2466 }
2467 auto val=static_cast<int>(input->readULong(2)); // the type
2468 input->seek(2, librevenge::RVNG_SEEK_CUR);
2469 bool needCheck=false;
2470 switch ((val&0xfff3fd7)) {
2471 case 0: // root
2472 case 2: // script
2473 case 0x42: // color pattern
2474 case 0x104: // pipeline
2475 case 0x204: // pipeline
2476 case 0x480: // style
2477 break;
2478 case 1: // layout
2479 return false;
2480 default:
2481 needCheck=true;
2482 break;
2483 }
2484 if (!needCheck) continue;
2485 auto clustZone=getDataZone(listIds[0]);
2486 if (!clustZone)
2487 return false;
2488 updateZone(clustZone);
2489 int type=m_clusterManager->getClusterType(*clustZone, val);
2490 if (type==1) // a layout
2491 return false;
2492 if ((type&0x40000)==0x40000) { // a shape
2493 if (!firstFound) {
2494 if (type!=0x40002) return false; // first is not a spreadsheet
2495 firstFound=true;
2496 }
2497 else if (type==0x40002) // too many spreadsheets
2498 return false;
2499 }
2500 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
2501 }
2502 if (!firstFound)
2503 return false;
2504 return true;
2505 }
2506
2507 bool RagTime5Document::checkHeader(MWAWHeader *header, bool strict)
2508 {
2509 *m_state = RagTime5DocumentInternal::State();
2510
2511 MWAWInputStreamPtr input = m_parser->getInput();
2512 if (!input || !input->hasDataFork())
2513 return false;
2514
2515 libmwaw::DebugStream f;
2516 f << "FileHeader:";
2517 if (!input->checkPosition(32)) {
2518 MWAW_DEBUG_MSG(("RagTime5Document::checkHeader: file is too short\n"));
2519 return false;
2520 }
2521 input->seek(0,librevenge::RVNG_SEEK_SET);
2522 if (input->readULong(4)!=0x43232b44 || input->readULong(4)!=0xa4434da5
2523 || input->readULong(4)!=0x486472d7)
2524 return false;
2525 int val;
2526 for (int i=0; i<3; ++i) {
2527 val=static_cast<int>(input->readLong(2));
2528 if (val!=i) f << "f" << i << "=" << val << ",";
2529 }
2530 val=static_cast<int>(input->readLong(2)); // always 0?
2531 if (val) f << "f3=" << val << ",";
2532 m_state->m_zonesEntry.setBegin(long(input->readULong(4)));
2533 m_state->m_zonesEntry.setLength(long(input->readULong(4)));
2534 if (m_state->m_zonesEntry.length()<137 ||
2535 !input->checkPosition(m_state->m_zonesEntry.begin()+137))
2536 return false;
2537 if (strict && !input->checkPosition(m_state->m_zonesEntry.end()))
2538 return false;
2539 val=static_cast<int>(input->readLong(1));
2540 if (val==1)
2541 f << "compacted,";
2542 else if (val)
2543 f << "g0=" << val << ",";
2544 val=static_cast<int>(input->readLong(1));
2545 setVersion(5);
2546 switch (val) {
2547 case 0:
2548 f << "vers=5,";
2549 break;
2550 case 4:
2551 f << "vers=6.5,";
2552 setVersion(6);
2553 break;
2554 default:
2555 f << "#vers=" << val << ",";
2556 break;
2557 }
2558 for (int i=0; i<2; ++i) {
2559 val=static_cast<int>(input->readLong(1));
2560 if (val) f << "g" << i+1 << "=" << val << ",";
2561 }
2562 // ok, we can finish initialization
2563 if (header) {
2564 bool isSpreadsheet=checkIsSpreadsheet();
2565 header->reset(MWAWDocument::MWAW_T_RAGTIME, version(), isSpreadsheet ? MWAWDocument::MWAW_K_SPREADSHEET : MWAWDocument::MWAW_K_TEXT);
2566 }
2567 ascii().addPos(0);
2568 ascii().addNote(f.str().c_str());
2569
2570 ascii().addPos(input->tell());
2571 ascii().addNote("_");
2572
2573 return true;
2574 }
2575
2576 ////////////////////////////////////////////////////////////
2577 // send data to the listener
2578 ////////////////////////////////////////////////////////////
2579
2580 bool RagTime5Document::sendZones(MWAWListenerPtr listener)
2581 {
2582 if (!listener) {
2583 MWAW_DEBUG_MSG(("RagTime5Document::sendZones: can not find the listener\n"));
2584 return false;
2585 }
2586 if (m_state->m_hasLayout)
2587 m_layoutParser->sendPageContents();
2588 else {
2589 MWAW_DEBUG_MSG(("RagTime5Document::sendZones: no layout, try to send the main zones\n"));
2590 m_clusterManager->sendClusterMainList();
2591 }
2592 return true;
2593 }
2594
2595 bool RagTime5Document::sendSpreadsheet(MWAWListenerPtr listener)
2596 {
2597 if (!listener) {
2598 MWAW_DEBUG_MSG(("RagTime5Document::sendSpreadsheet: can not find the listener\n"));
2599 return false;
2600 }
2601 std::vector<int> sheetIds=m_spreadsheetParser->getSheetIdList();
2602 if (sheetIds.size()!=1) {
2603 MWAW_DEBUG_MSG(("RagTime5Document::sendSpreadsheet: Oops, %d spreadsheets exist\n", int(sheetIds.size())));
2604 return false;
2605 }
2606 return send(sheetIds[0], listener, MWAWPosition());
2607 }
2608
2609 bool RagTime5Document::send(int zoneId, MWAWListenerPtr listener, MWAWPosition const &pos, int partId, int cellId, double totalWidth)
2610 {
2611 if (m_state->m_sendZoneSet.find(zoneId)!=m_state->m_sendZoneSet.end()) {
2612 MWAW_DEBUG_MSG(("RagTime5Document::send: argh zone %d is already in the sent set\n", zoneId));
2613 return false;
2614 }
2615
2616 m_state->m_sendZoneSet.insert(zoneId);
2617 auto type=m_clusterManager->getClusterType(zoneId);
2618 bool ok=false;
2619 if (type==RagTime5ClusterManager::Cluster::C_ButtonZone || type==RagTime5ClusterManager::Cluster::C_GraphicZone || type==RagTime5ClusterManager::Cluster::C_PictureZone)
2620 ok=m_graphParser->send(zoneId, listener, pos);
2621 else if (type==RagTime5ClusterManager::Cluster::C_TextZone)
2622 ok=m_textParser->send(zoneId, listener, partId, cellId, totalWidth);
2623 else if (type==RagTime5ClusterManager::Cluster::C_SpreadsheetZone)
2624 ok=m_spreadsheetParser->send(zoneId, listener, pos, partId);
2625 else if (type==RagTime5ClusterManager::Cluster::C_Pipeline)
2626 ok=m_pipelineParser->send(zoneId, listener, pos, partId, totalWidth);
2627 m_state->m_sendZoneSet.erase(zoneId);
2628 if (ok)
2629 return true;
2630 static bool first=true;
2631 if (first) {
2632 MWAW_DEBUG_MSG(("RagTime5Document::send: not fully implemented\n"));
2633 first=false;
2634 }
2635 return false;
2636 }
2637
2638 void RagTime5Document::flushExtra(MWAWListenerPtr listener, bool onlyCheck)
2639 {
2640 if (!listener) {
2641 MWAW_DEBUG_MSG(("RagTime5Document::flushExtra: can not find the listener\n"));
2642 return;
2643 }
2644 m_textParser->flushExtra(onlyCheck);
2645 m_graphParser->flushExtra(onlyCheck);
2646 m_spreadsheetParser->flushExtra(onlyCheck);
2647
2648 // look for unparsed data
2649 int notRead=0;
2650 for (auto const &zone : m_state->m_zonesList) {
2651 if (!zone || zone->m_isParsed || !zone->m_entry.valid())
2652 continue;
2653 ascii().addPos(zone->m_defPosition);
2654 ascii().addNote("[notParsed]");
2655 readZoneData(*zone);
2656 ++notRead;
2657 }
2658 if (notRead) {
2659 MWAW_DEBUG_MSG(("RagTime5Document::flushExtra: find %d/%d unparsed data\n", notRead, static_cast<int>(m_state->m_zonesList.size())));
2660 }
2661 }
2662
2663 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: