Debian Codegraph @

src/lib/Canvas5Parser.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 <cmath>
35 #include <cctype>
36 #include <iomanip>
37 #include <iostream>
38 #include <limits>
39 #include <algorithm>
40 #include <set>
41 #include <sstream>
42 #include <stack>
43 #include <string>
44 #include <utility>
45
46 #include <librevenge/librevenge.h>
47
48 #include "MWAWFontConverter.hxx"
49 #include "MWAWGraphicListener.hxx"
50 #include "MWAWGraphicShape.hxx"
51 #include "MWAWGraphicStyle.hxx"
52 #include "MWAWHeader.hxx"
53 #include "MWAWOLEParser.hxx"
54 #include "MWAWParagraph.hxx"
55 #include "MWAWPictBitmap.hxx"
56 #include "MWAWPictData.hxx"
57 #include "MWAWPrinter.hxx"
58 #include "MWAWPosition.hxx"
59 #include "MWAWRSRCParser.hxx"
60 #include "MWAWStringStream.hxx"
61
62 #include "Canvas5Graph.hxx"
63 #include "Canvas5Image.hxx"
64 #include "Canvas5Structure.hxx"
65 #include "Canvas5StyleManager.hxx"
66
67 #include "Canvas5Parser.hxx"
68
69 /** Internal: the structures of a Canvas5Parser */
70 namespace Canvas5ParserInternal
71 {
72 //! Internal: the slide data of a Canvas5Parser
73 struct Slide {
74 Slide()
75 : m_dim(0,0)
76 , m_numLayers(0)
77 , m_layers()
78 {
79 }
80
81 //! the slide dimension
82 MWAWVec2f m_dim;
83 //! the number of layer
84 int m_numLayers;
85 //! the layer id
86 std::vector<int> m_layers;
87 };
88
89 //! Internal: the layer of a Canvas5Parser
90 struct Layer {
91 //! constructor
92 Layer()
93 : m_name()
94 , m_numShapes()
95 , m_shapesId()
96 , m_type(-1)
97 {
98 }
99 //! the layer name
100 librevenge::RVNGString m_name;
101 //! the number of shape
102 int m_numShapes;
103 //! the shape id
104 std::vector<int> m_shapesId;
105 //! the layer type (unknonw)
106 int m_type;
107 };
108
109 ////////////////////////////////////////
110 //! Internal: the state of a Canvas5Parser
111 struct State {
112 //! constructor
113 State()
114 : m_isWindowsFile(false)
115 , m_stream()
116
117 , m_type(1)
118 , m_fileFlags(0)
119 , m_documentSetup(0)
120 , m_facingPages(false)
121
122 , m_numSlides(1)
123 , m_slideIds()
124 , m_idToSlide()
125
126 , m_numLayers(1)
127 , m_idToLayer()
128 , m_layerIdInMasterSet()
129 , m_numShapes(0)
130
131 , m_idToTextLink()
132
133 , m_metaData()
134 {
135 }
136
137 //! true if this is a windows file
138 bool m_isWindowsFile;
139 //! the current stream
140 std::shared_ptr<Canvas5Structure::Stream> m_stream;
141
142 //! the document type 1: graphic, 2: list of pages, 3: slides
143 int m_type;
144 //! the file flags
145 int m_fileFlags;
146 //! the document setup: 0 full page, 1: two page bottom/down, 2: four page
147 int m_documentSetup;
148 //! true if the document uses facing page
149 bool m_facingPages;
150
151 //! the number of slides
152 int m_numSlides;
153 //! the slides id
154 std::vector<int> m_slideIds;
155 //! the slide data
156 std::map<int, Slide> m_idToSlide;
157
158 //! the number of layer
159 int m_numLayers;
160 //! the layer data
161 std::map<int, Layer> m_idToLayer;
162 //! the list of layer present in the master page
163 std::set<int> m_layerIdInMasterSet;
164 //! the number of shapes
165 int m_numShapes;
166
167 //! the id the text link map
168 std::map<int, librevenge::RVNGString> m_idToTextLink;
169
170 //! the meta data
171 librevenge::RVNGPropertyList m_metaData;
172 };
173
174 }
175
176 ////////////////////////////////////////////////////////////
177 // constructor/destructor, ...
178 ////////////////////////////////////////////////////////////
179 Canvas5Parser::Canvas5Parser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
180 : MWAWGraphicParser(input, rsrcParser, header)
181 , m_state()
182 , m_graphParser()
183 , m_imageParser()
184 , m_styleManager()
185 {
186 resetGraphicListener();
187 setAsciiName("main-1");
188
189 m_state.reset(new Canvas5ParserInternal::State);
190
191 m_styleManager.reset(new Canvas5StyleManager(*this));
192 m_imageParser.reset(new Canvas5Image(*this));
193 m_graphParser.reset(new Canvas5Graph(*this));
194
195 getPageSpan().setMargins(0.1);
196 }
197
198 Canvas5Parser::~Canvas5Parser()
199 {
200 }
201
202 bool Canvas5Parser::isWindowsFile() const
203 {
204 return m_state->m_isWindowsFile;
205 }
206
207
208 librevenge::RVNGString Canvas5Parser::getTextLink(int textLinkId) const
209 {
210 auto const &it=m_state->m_idToTextLink.find(textLinkId);
211 if (it==m_state->m_idToTextLink.end()) {
212 MWAW_DEBUG_MSG(("Canvas5Parser::getTextLink: can not find the a with id=%d\n", textLinkId));
213 return librevenge::RVNGString();
214 }
215 return it->second;
216 }
217
218 ////////////////////////////////////////////////////////////
219 // the parser
220 ////////////////////////////////////////////////////////////
221 void Canvas5Parser::parse(librevenge::RVNGDrawingInterface *docInterface)
222 {
223 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
224 bool ok = false;
225 try {
226 checkHeader(nullptr);
227
228 auto input=decode(getInput(), version());
229 if (!input)
230 throw(libmwaw::ParseException());
231
232 // create the main stream
233 m_state->m_stream=std::make_shared<Canvas5Structure::Stream>(input);
234 m_state->m_stream->ascii().open(asciiName());
235
236 ok = createZones();
237 if (ok)
238 createDocument(docInterface);
239 }
240 catch (...) {
241 MWAW_DEBUG_MSG(("Canvas5Parser::parse: exception catched when parsing\n"));
242 ok = false;
243 }
244
245 ascii().reset();
246 resetGraphicListener();
247 if (!ok) throw(libmwaw::ParseException());
248 }
249
250 ////////////////////////////////////////////////////////////
251 // create the document
252 ////////////////////////////////////////////////////////////
253 void Canvas5Parser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
254 {
255 if (!documentInterface) return;
256 if (getGraphicListener()) {
257 MWAW_DEBUG_MSG(("Canvas5Parser::createDocument: listener already exist\n"));
258 return;
259 }
260
261 std::vector<MWAWPageSpan> pageList;
262 MWAWPageSpan page(getPageSpan());
263
264 bool createMasterPage=false;
265 size_t numMasters=m_state->m_type==1 ? 0 : 1;
266 size_t numPagesOnOnePage=1, decal=0;
267 int const vers=version();
268 if (m_state->m_type==2) {
269 switch (m_state->m_documentSetup) {
270 case 0:
271 if (m_state->m_facingPages) {
272 /** FIXME: it is simpler to create a big page which contains the left and right page,
273 but it may be better to create each page and to only keep in each page the
274 used shapes, ie. to translate back the right shape and also decompose the master
275 page shapes in left/right
276 */
277 numMasters=2;
278 numPagesOnOnePage=2;
279 decal=1; // first page is the left page
280 page.setFormWidth(2*page.getFormWidth());
281 }
282 break;
283 case 1:
284 if (vers>5)
285 break;
286 page.setFormLength(page.getFormLength()/2);
287 break;
288 case 2:
289 if (vers>5)
290 break;
291 page.setFormWidth(page.getFormWidth()/2);
292 page.setFormLength(page.getFormLength()/2);
293 break;
294 default:
295 break;
296 }
297 }
298 std::vector<std::vector<int> > listSlides;
299 for (size_t p=0, n=decal; p<m_state->m_slideIds.size(); ++p) {
300 int sId=m_state->m_slideIds[p];
301 auto const &it=m_state->m_idToSlide.find(sId);
302 if (it==m_state->m_idToSlide.end()) {
303 MWAW_DEBUG_MSG(("Canvas5Parser::createDocument: can not find the slide %d\n", sId));
304 continue;
305 }
306 auto const &slide=it->second;
307 /*
308 if type==1(illustration), one slide, multiple layer
309 if type==2(publication), the first slide is the master page
310 if type==3(slide), the first slide is the master page
311 */
312 MWAWPageSpan ps(page);
313 ps.setPageSpan(1);
314 if (p>=numMasters && createMasterPage) {
315 for (auto l : slide.m_layers) {
316 if (l!=1) continue;
317 ps.setMasterPageName(librevenge::RVNGString("Master"));
318 break;
319 }
320 }
321 if (p==0 && p<numMasters) {
322 for (auto l : slide.m_layers) {
323 auto lIt=m_state->m_idToLayer.find(l);
324 if (lIt!=m_state->m_idToLayer.end() && !lIt->second.m_shapesId.empty())
325 m_state->m_layerIdInMasterSet.insert(l);
326 }
327 createMasterPage=!m_state->m_layerIdInMasterSet.empty();
328 }
329 if (p<numMasters) continue;
330 size_t nPage=n++/numPagesOnOnePage;
331 if (listSlides.size() >= nPage) {
332 listSlides.resize(nPage+1);
333 pageList.push_back(ps);
334 }
335 listSlides[nPage].push_back(sId);
336 }
337
338 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
339 setGraphicListener(listen);
340 listen->setDocumentMetaData(m_state->m_metaData);
341
342 listen->startDocument();
343
344 if (createMasterPage) {
345 MWAWPageSpan ps(page);
346 ps.setMasterPageName(librevenge::RVNGString("Master"));
347 if (!listen->openMasterPage(ps)) {
348 MWAW_DEBUG_MSG(("Canvas5Parser::createDocument: can not create the master page\n"));
349 }
350 else {
351 for (auto &lId : m_state->m_layerIdInMasterSet) {
352 auto lIt=m_state->m_idToLayer.find(lId);
353 if (lIt!=m_state->m_idToLayer.end())
354 send(lIt->second);
355 }
356 listen->closeMasterPage();
357 }
358 }
359
360 bool first=true;
361 for (auto const &lId : listSlides) {
362 if (!first)
363 listen->insertBreak(MWAWListener::PageBreak);
364 first=false;
365 for (auto id : lId) {
366 auto const &it=m_state->m_idToSlide.find(id);
367 if (it==m_state->m_idToSlide.end()) {
368 MWAW_DEBUG_MSG(("Canvas5Parser::createDocument: can not find slide %d\n", id));
369 continue;
370 }
371 send(it->second);
372 }
373 }
374 }
375
376 ////////////////////////////////////////////////////////////
377 //
378 // Intermediate level
379 //
380 ////////////////////////////////////////////////////////////
381 bool Canvas5Parser::createZones()
382 {
383 MWAWRSRCParserPtr rsrcParser = getRSRCParser();
384 if (rsrcParser) {
385 Canvas5Structure::Stream stream(rsrcParser->getInput(), rsrcParser->ascii());
386 auto const &entryMap = rsrcParser->getEntriesMap();
387
388 for (int w=0; w<2; ++w) {
389 // also some icons: ICN#, icl8, ics#, ics8
390 static char const *wh[]= {"pnot" /*0*/, "PICT" /* value in pnot */};
391 auto it = entryMap.lower_bound(wh[w]);
392 while (it != entryMap.end() && it->first==wh[w]) {
393 auto const &entry=it++->second;
394 if (!entry.valid()) continue;
395 switch (w) {
396 case 0:
397 readPnot(stream, entry);
398 break;
399 case 1:
400 default:
401 readPicture(stream, entry);
402 break;
403 }
404 }
405 }
406 }
407
408 auto stream=m_state->m_stream;
409 int const vers=version();
410 if (!stream || !stream->input() || !readFileHeader(stream))
411 return false;
412 if (vers<9) {
413 if (!readMainBlock(stream) || !m_imageParser->readImages(stream))
414 return false;
415 }
416 else if (!readMainBlock9(stream))
417 return false;
418 if (!readFileRSRCs(stream))
419 return false;
420
421 auto input=stream->input();
422 bool ok;
423 if (vers>5) {
424 long pos=input->tell();
425 ok=readSI200(*stream);
426 if (!ok)
427 input->seek(pos, librevenge::RVNG_SEEK_SET);
428 }
429 if (!input->isEnd()) {
430 long pos=input->tell();
431 ok=readFileDesc(*stream);
432 if (!ok)
433 input->seek(pos, librevenge::RVNG_SEEK_SET);
434 }
435
436 if (input->isEnd())
437 return !m_state->m_idToSlide.empty();
438
439 MWAW_DEBUG_MSG(("Canvas5Parser::createZones: find extra data\n"));
440 int n=0;
441 long pos=input->tell();
442 libmwaw::DebugStream f;
443 auto &ascFile=stream->ascii();
444 ascFile.addPos(pos);
445 ascFile.addNote("Entries(Extra):###");
446
447 while (!input->isEnd()) {
448 pos=input->tell();
449 f.str("");
450 f << "Extra-" << ++n << ":";
451 ascFile.addPos(pos);
452 ascFile.addNote(f.str().c_str());
453 input->seek(pos+256, librevenge::RVNG_SEEK_SET);
454 }
455 return !m_state->m_idToSlide.empty();
456 }
457
458 bool Canvas5Parser::readMainBlock(std::shared_ptr<Canvas5Structure::Stream> stream)
459 {
460 if (!stream || !stream->input()) return false;
461
462 if (!Canvas5Structure::readPreview(*stream, (m_state->m_fileFlags&3)!=2) ||
463 !readDocumentSettings(stream))
464 return false;
465 if (!m_graphParser->findShapeDataZones(stream))
466 return false;
467 if (!m_graphParser->readShapes(*stream, m_state->m_numShapes))
468 return false;
469
470 if (!readSlides(stream) || !readLayers(stream))
471 return false;
472
473 if (!m_styleManager->readInks(stream))
474 return false;
475
476 if (!m_graphParser->readMatrices(stream))
477 return false;
478
479 //
480 // the styles
481 //
482 if (!m_styleManager->readStrokes(stream) || !m_styleManager->readPenStyles(stream) ||
483 !m_styleManager->readArrows(stream) || !m_styleManager->readDashes(stream))
484 return false;
485
486 if (!m_styleManager->readParaStyles(stream) || !m_styleManager->readCharStyles(stream))
487 return false;
488
489 return readTextLinks(stream);
490 }
491
492 bool Canvas5Parser::readMainBlock9(std::shared_ptr<Canvas5Structure::Stream> stream)
493 {
494 if (!stream || !stream->input()) return false;
495
496 auto input=stream->input();
497 auto &ascFile=stream->ascii();
498 libmwaw::DebugStream f;
499 std::map<std::string,std::pair<int, char const *> > const nameToTagId= {
500 { "PREVIEW", {0, "Preview"} }, {"SAVEGLOBS", {1, "DocSettings"}}, {"RECTOBJHANDLE", {2, nullptr} }, {"OBJECTDATA", {3, "DataShap"} },
501 { "DOCPAGELIST", {4, "Slide"}}, { "DOCLAYERLIST", {5, "Layer"}}, { "INKCLUSTER", {6, "Color"} }, { "MATRIXCLUSTER", {7, "Matrix"}},
502 { "FRAMECLUSTER", {8, nullptr}}, { "OBJSTYLECLUSTER", {9, nullptr} }, { "MASKCLUSTER", {10, "Mask"}}, {"PARASTYLECLUSTER", {11, "ParaStyl"}},
503 { "CHARSTYLECLUSTER", {12, "CharStyle"}}, { "IMAGECLUSTER", {13, "Image"}}, {"OBJNAMECLUSTER", {14, "ObjName"}}, {"DEPCLUSTER", {15, nullptr} },
504 };
505 MWAWEntry shapeEntry;
506 while (!input->isEnd()) {
507 long pos=input->tell();
508 std::string tag;
509 int fTag;
510 if (!getTAG9(*stream, tag, fTag) || fTag!=0) {
511 input->seek(pos, librevenge::RVNG_SEEK_SET);
512 return true;
513 }
514 f.str("");
515 f << "Entries(" << tag << ")[TAG]:";
516 ascFile.addPos(pos);
517 ascFile.addNote(f.str().c_str());
518
519 bool ok=true;
520 pos=input->tell();
521 f.str("");
522 auto wh=-1;
523 std::string what(tag);
524 auto const &it=nameToTagId.find(tag);
525 if (it!=nameToTagId.end()) {
526 wh=it->second.first;
527 if (it->second.second)
528 what=it->second.second;
529 }
530 f << "Entries(" << what << ")[data]:";
531 long len=-1;
532 switch (wh) {
533 case 0: {
534 int fFlags=int(input->readULong(1));
535 ok=Canvas5Structure::readPreview(*stream, (fFlags&3)!=2);
536 break;
537 }
538 case 1:
539 ok=readDocumentSettings(stream);
540 break;
541 case 2:
542 len=long(input->readULong(4));
543 ok=(long)((unsigned long)pos+4+(unsigned long)len)>=pos+4 && input->checkPosition(pos+4+len);
544 if (!ok)
545 break;
546 shapeEntry.setBegin(pos);
547 shapeEntry.setLength(len+4);
548 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
549 break;
550 case 3: {
551 ok=m_graphParser->findShapeDataZones(stream);
552 if (ok && !shapeEntry.valid()) {
553 MWAW_DEBUG_MSG(("Canvas5Parser::readMainBlock9: oops, can not find the object handle zone\n"));
554 break;
555 }
556 if (!ok)
557 break;
558 long actPos=input->tell();
559 input->seek(shapeEntry.begin(), librevenge::RVNG_SEEK_SET);
560 m_graphParser->readShapes(*stream, m_state->m_numShapes);
561 input->seek(actPos, librevenge::RVNG_SEEK_SET);
562 break;
563 }
564 case 4:
565 ok=readSlides(stream);
566 break;
567 case 5:
568 ok=readLayers(stream);
569 break;
570 case 6:
571 ok=m_styleManager->readInks9(stream);
572 break;
573 case 7:
574 ok=m_graphParser->readMatrices(stream);
575 break;
576 case 8:
577 ok=m_styleManager->readFrameStyles9(stream);
578 break;
579 case 9: // readStyle
580 case 10: // unseen but probably fSz=18+276
581 ok=readArray9(stream, what);
582 break;
583 case 11:
584 ok=m_styleManager->readParaStyles(stream);
585 break;
586 case 12:
587 ok=m_styleManager->readCharStyles(stream);
588 break;
589 case 13:
590 ok=m_imageParser->readImages9(stream);
591 break;
592 case 14:
593 ok=readArray9(stream, what, &Canvas5Parser::stringDataFunction);
594 break;
595 case 15: // A,B: dependency_group list
596 ok=readArray9(stream, what) && readArray9(stream, what+"-A") && readArray9(stream, what+"-B");
597 break;
598 default:
599 ok=readArray9(stream, what);
600 break;
601 }
602 if (!ok) {
603 input->seek(pos, librevenge::RVNG_SEEK_SET);
604 f << "###";
605 ascFile.addPos(pos);
606 ascFile.addNote(f.str().c_str());
607 break;
608 }
609 ascFile.addPos(pos);
610 ascFile.addNote(f.str().c_str());
611
612 if (!checkTAG9(*stream, tag, 1)) break;
613 }
614 ascFile.addPos(input->tell());
615 ascFile.addNote("Entries(Extra):###");
616 return false;
617 }
618
619 bool Canvas5Parser::readFileRSRCs(std::shared_ptr<Canvas5Structure::Stream> stream)
620 {
621 if (!stream || !stream->input()) return false;
622 auto input=stream->input();
623
624 long pos=input->tell();
625 if (!input->checkPosition(pos+4)) {
626 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: the zone is too short\n"));
627 return false;
628 }
629
630 int const vers=version();
631 libmwaw::DebugStream f;
632 auto &ascFile=stream->ascii();
633 f << "Entries(RsrcList):";
634 int N=int(input->readLong(4));
635 f << "N=" << N << ",";
636 if (N<0 || (input->size()-pos-4)/16<N || pos+4+N*16<pos+4 || !input->checkPosition(pos+4+N*16)) {
637 f << "###";
638 ascFile.addPos(pos);
639 ascFile.addNote(f.str().c_str());
640 return false;
641 }
642 ascFile.addPos(pos);
643 ascFile.addNote(f.str().c_str());
644
645 int numRsrc=N;
646 for (int i=0; i<numRsrc; ++i) {
647 pos=input->tell();
648 f.str("");
649 if (!input->checkPosition(pos+16)) {
650 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: can not find block %d\n", i));
651 f << "RsrcList-" << i << ":###";
652 ascFile.addPos(pos);
653 ascFile.addNote(f.str().c_str());
654 return false;
655 }
656 unsigned what=unsigned(input->readULong(4));
657 f << "Entries(Rsrc" << (what==0x54455854 ? "TeXT" : Canvas5Structure::getString(what)) << "),";
658 int id=int(input->readLong(4));
659 f << "id=" << Canvas5Structure::getString(unsigned(id)) << ",";
660 f << "fl=" << std::hex << input->readULong(4) << std::dec << ","; // 2XXXXXX ?
661 long len=input->readLong(4);
662 long endPos=pos+16+len;
663 if (endPos<pos+16 || !input->checkPosition(endPos+4)) {
664 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: can not find block %d\n", i));
665 f << "###";
666 ascFile.addPos(pos);
667 ascFile.addNote(f.str().c_str());
668 return false;
669 }
670 input->pushLimit(endPos);
671 int val;
672 std::vector<bool> defined;
673 switch (what) {
674 case 0x446f496e: // DoIn
675 if (len!=32) {
676 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[DoIn]: unexpected size\n"));
677 f << "###";
678 break;
679 }
680 // checkme: more probably a serie of bytes
681 for (int j=0; j<4; ++j) { // f0=0|100|146-148
682 val=int(input->readLong(4));
683 if (val) f << "f" << j << "=" << val << ",";
684 }
685 f << "N=["; // 1-a, 1-3, 1-3, 1-2
686 for (int j=0; j<12; ++j) f << input->readLong(4) << ",";
687 f << "],";
688 val=int(input->readLong(4));
689 if (val==2)
690 f << "docUnit=points,";
691 else if (val!=1)
692 f << "#docUnit=" << val << ",";
693 break;
694 case 0x45646974: // Edit
695 if (len!=8) {
696 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[Edit]: unexpected size\n"));
697 f << "###";
698 break;
699 }
700 for (int j=0; j<2; ++j) {
701 val=int(input->readLong(4));
702 if (val!=(j==0 ? 0 : -1)) f << "f" << j << "=" << val << ",";
703 }
704 break;
705 case 0x4d41434f: // MACO : object from macros
706 if (len==0)
707 break;
708 if (!m_imageParser->readMACORsrc(stream))
709 f << "###";
710 break;
711 case 0x4d676f72: { // Mgor
712 if (len!=48) {
713 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[Mgor]: unexpected size\n"));
714 f << "###";
715 break;
716 }
717 ascFile.addDelimiter(input->tell(),'|');
718 input->seek(20, librevenge::RVNG_SEEK_CUR);
719 ascFile.addDelimiter(input->tell(),'|');
720 int dim[2];
721 for (auto &d : dim) d=int(input->readLong(4));
722 f << "windows[dim]=" << MWAWVec2i(dim[1], dim[0]) << ","; // ~700x1000
723 val=int(input->readLong(4));
724 if (val) f << "f0=" << val << ",";
725 break;
726 }
727 case 0x516b546d: // QkTm
728 if (len==0)
729 break;
730 if (!m_imageParser->readQkTmRsrc(*stream))
731 f << "###";
732 break;
733 case 0x54455854: // TEXT
734 switch (id) {
735 case 1: {
736 f << "char[style],";
737 Canvas5StyleManager::CharStyle font;
738 if (!m_styleManager->readCharStyle(*stream, -1, font))
739 f << "###";
740 break;
741 }
742 case 2:
743 f << "para[style],";
744 if (!m_styleManager->readParaStyle(stream, -1))
745 f << "###";
746 break;
747 case 3:
748 if (len!=2) {
749 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT]: unknown len[3]\n"));
750 f << "###";
751 break;
752 }
753 val=int(input->readLong(2));
754 if (val) f << "f0=" << val << ",";
755 break;
756 case 4: // 0
757 case 5: // small number
758 if (len!=4) {
759 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT]: unknown len[%d]\n", id));
760 f << "###";
761 break;
762 }
763 val=int(input->readLong(4));
764 if (val) f << "f0=" << val << ",";
765 break;
766 case 6:
767 if (len!=48) {
768 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT]: unknown len[6]\n"));
769 f << "###";
770 break;
771 }
772 for (int j=0; j<12; ++j) { // f1 big number
773 val=int(input->readULong(4));
774 if (val) f << "f" << j << "=" << std::hex << val << std::dec << ",";
775 }
776 break;
777 case 7:
778 if (len!=40) {
779 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT]: unknown len[7]\n"));
780 f << "###";
781 break;
782 }
783 for (int j=0; j<10; ++j) { // f0=0-19
784 val=int(input->readULong(4));
785 if (val) f << "f" << j << "=" << std::hex << val << std::dec << ",";
786 }
787 break;
788 case 8: {
789 if (len<16) {
790 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT]: unknown len[8]\n"));
791 f << "###";
792 break;
793 }
794 auto invert=input->readInverted();
795 val=int(input->readULong(2));
796 if (val==256)
797 input->setReadInverted(!invert);
798 else if (val!=1)
799 f << "f0=" << val << ",";
800 for (int j=0; j<2; ++j) {
801 val=int(input->readULong(1));
802 if (val!=1-j)
803 f << "f" << j+1 << "=" << val << ",";
804 }
805 int N0=int(input->readLong(4));
806 f << "N=" << N0 << ",";
807 if ((len-16)/4188<N0 || len!=4188*N0+16) {
808 input->setReadInverted(invert);
809 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT,8]: can not find N\n"));
810 f << "###";
811 break;
812 }
813 for (int j=0; j<4; ++j) {
814 val=int(input->readULong(2));
815 if (val)
816 f << "f" << j+3 << "=" << val << ",";
817 }
818
819 for (int j=0; j<N0; ++j) {
820 long aPos=input->tell();
821 ascFile.addPos(aPos);
822 ascFile.addNote("RsrcTeXT-B[8]:");
823 input->seek(aPos+4188, librevenge::RVNG_SEEK_SET);
824 }
825 input->setReadInverted(invert);
826 break;
827 }
828 default:
829 if (id<1001 || id>1100) {
830 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT]: unknown id=%d\n", id));
831 f << "###";
832 break;
833 }
834 if (len<40) {
835 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT,%d]: the length seems bad\n", id));
836 f << "###";
837 break;
838 }
839 val=int(input->readULong(1));
840 if (val!=1 && val!=2) {
841 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT,%d]: the length seems bad\n", id));
842 f << "###endian=" << val << ",";;
843 break;
844 }
845 else {
846 auto invert=input->readInverted();
847 input->seek(3, librevenge::RVNG_SEEK_CUR);
848 input->setReadInverted(val==2);
849 int n=int(input->readLong(4));
850 f << "N=" << n << ",";
851 if (n<0 || (len-40)/8<n || len!=40+8*n) {
852 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[TEXT,%d]: can not read the n value\n", id));
853 f << "###";
854 input->setReadInverted(invert);
855 break;
856 }
857 f << "unk=[";
858 for (int j=0; j<8; ++j)
859 f << std::hex << input->readULong(4) << std::dec << ((j%2)==0 ? "x" : ",");
860 f << "],";
861 if (n) {
862 f << "unkn1=[";
863 for (int j=0; j<2*n; ++j)
864 f << std::hex << input->readULong(4) << std::dec << ((j%2)==0 ? "x" : ",");
865 f << "],";
866 }
867 input->setReadInverted(invert);
868 }
869 break;
870 }
871 break;
872 case 0x65666665: // effe
873 if (len==0) break;
874 if (!readExtendedHeader(stream, 0xc, "Rsrceffe",
875 [](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &) {
876 auto lInput=lStream->input();
877 int lVal=int(lInput->readULong(4)); // Enve
878 libmwaw::DebugStream lF;
879 auto &asciiFile=lStream->ascii();
880 if (lVal!=1)
881 lF << "f0=" << Canvas5Structure::getString(unsigned(lVal)) << ",";
882 for (int j=0; j<2; ++j) { // f1=580
883 lVal=int(lInput->readLong(4));
884 if (lVal)
885 lF << "f" << j+1 << "=" << lVal << ",";
886 }
887 asciiFile.addPos(item.m_pos);
888 asciiFile.addNote(lF.str().c_str());
889 })) {
890 f << "###";
891 break;
892 }
893 if (input->isEnd())
894 break;
895 if (!readIndexMap(stream, "Rsrceffe")) {
896 f << "###";
897 break;
898 }
899 if (input->isEnd())
900 break;
901 if (!readDefined(*stream, defined, "Rsrceffe")) {
902 f << "###";
903 break;
904 }
905 break;
906 case 0x666e6474: //fndt
907 if (id==2) { // unsure what to parse
908 if (len<514) {
909 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: can not read id=2 fndt block\n"));
910 f << "###";
911 break;
912 }
913 break;
914 }
915 if (id==3) {
916 if (len<132) {
917 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: can not read id=3 fndt block\n"));
918 f << "###";
919 break;
920 }
921 f << "N=[";
922 for (int j=0; j<3; ++j) f << input->readLong(4) << ",";
923 f << "],";
924 ascFile.addPos(input->tell());
925 ascFile.addNote("Rsrcfndt3-A:");
926 input->seek(120, librevenge::RVNG_SEEK_CUR);
927
928 while (!input->isEnd() && input->checkPosition(input->tell()+60)) {
929 ascFile.addPos(input->tell());
930 ascFile.addNote("Rsrcfndt3-B:");
931 input->seek(60, librevenge::RVNG_SEEK_CUR);
932 }
933 break;
934 }
935 if (id==4 || id==5 || id==7 || id==8) { // id=4 replace id=3 in v7
936 if (len<(id==4 ? 168 : id==7 ? 544 : 192)) {
937 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: can not read id=%d fndt block\n", id));
938 f << "###";
939 break;
940 }
941 f << "N=["; // 2|3|1|1
942 for (int j=0; j<4; ++j) f << input->readLong(4) << ",";
943 f << "],";
944
945 libmwaw::DebugStream f2;
946 f2 << "Rsrcfndt" << id << "-A:";
947 ascFile.addPos(input->tell());
948 ascFile.addNote(f2.str().c_str());
949 input->seek(id==4 ? 32 : id==7 ? 48 : 56, librevenge::RVNG_SEEK_CUR);
950
951 while (!input->isEnd() && input->checkPosition(input->tell()+60)) {
952 f2.str("");
953 f2 << "Rsrcfndt" << id << "-B:";
954 ascFile.addPos(input->tell());
955 ascFile.addNote(f2.str().c_str());
956 input->seek(60, librevenge::RVNG_SEEK_CUR);
957 }
958 break;
959 }
960 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: unexpected id=%d fndt block\n", id));
961 f << "###";
962 break;
963 case 0x4f4c4532: // OLE2 windows (checkme, probably bad)
964 if (len!=12) {
965 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[OLE2]: unexpected size\n"));
966 f << "###";
967 break;
968 }
969 for (int j=0; j<6; ++j) { // checkme probably a serie of bytes
970 val=int(input->readLong(2));
971 int const expected[]= {0x100, 0, 0, 0, 0x200, 0};
972 if (val!=expected[j])
973 f << "f" << j << "=" << val << ",";
974 }
975 break;
976 case 0x70636567: // pceg: related to link
977 switch (id) {
978 case 1: {
979 int const headerSz=vers<9 ? 56 : 52;
980 if (len<headerSz) {
981 // N headerSz, ??, ...
982 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: unexpected size\n"));
983 f << "###";
984 break;
985 }
986 int const fieldSz=vers<9?73:81;
987 N=int(input->readLong(4));
988 f << "N=" << N << ",";
989 if (N<0 || (len-headerSz)/fieldSz<N || len<headerSz+N*fieldSz) {
990 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: can not find the number of data\n"));
991 f << "###";
992 }
993 val=int(input->readLong(4));
994 if (val!=len)
995 f << "#len=" << val << ",";
996 val=int(input->readLong(4));
997 if (val)
998 f << "f0=" << val << ",";
999 if (vers<9) {
1000 val=int(input->readLong(1));
1001 if (val!=1) f << "endian=" << val << ","; // CHECKME: we must probably check here if we need to reverse the endian
1002 input->seek(1, librevenge::RVNG_SEEK_CUR);
1003 }
1004 for (int j=0; j<(vers<9 ? 21 : 20); ++j) {
1005 val=int(input->readLong(2));
1006 if (val)
1007 f << "g" << j << "=" << val << ",";
1008 }
1009
1010 libmwaw::DebugStream f2;
1011 auto fontConverter=getFontConverter();
1012 int defaultFont=isWindowsFile() ? fontConverter->getId("CP1252") : 3;
1013 for (int j=0; j<N; ++j) {
1014 long actPos=input->tell();
1015 f2.str("");
1016 f2 << "Rsrcpceg-Tl" << j+1 << ":";
1017 if (actPos+fieldSz>endPos) {
1018 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: can not read a link\n"));
1019 f2 << "###";
1020 ascFile.addPos(actPos);
1021 ascFile.addNote(f2.str().c_str());
1022 break;
1023 }
1024 input->seek(actPos+fieldSz-2, librevenge::RVNG_SEEK_SET);
1025 ascFile.addDelimiter(input->tell(),'|');
1026 librevenge::RVNGString link;
1027 bool first=true;
1028 while (input->tell()<endPos) {
1029 char c=char(input->readULong(1));
1030 if (c==0) {
1031 if (first) {
1032 first=false;
1033 continue;
1034 }
1035 break;
1036 }
1037 first=false;
1038 int unicode = fontConverter->unicode(defaultFont, static_cast<unsigned char>(c));
1039 if (unicode>0)
1040 libmwaw::appendUnicode(uint32_t(unicode), link);
1041 else {
1042 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: find unknown unicode for char=%d\n", int(c)));
1043 }
1044 }
1045 if (!link.empty())
1046 m_state->m_idToTextLink[j+1]=link;
1047 f2 << link.cstr() << ",";
1048 ascFile.addPos(actPos);
1049 ascFile.addNote(f2.str().c_str());
1050 }
1051 break;
1052 }
1053 case 3200: { // link to graphic shape, see link[id]
1054 if (len<16) {
1055 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: unexpected size\n"));
1056 f << "###";
1057 break;
1058 }
1059 val=int(input->readLong(4));
1060 if (val!=1) f << "f0=" << val << ",";
1061 int nData=int(input->readLong(4));
1062 f << "N=" << nData << ",";
1063 for (int j=0; j<2; ++j) { // between 0 and nData
1064 val=int(input->readLong(4));
1065 if (val) f << "f" << j+1 << "=" << val << ",";
1066 }
1067 f << "data=[";
1068 for (int j=0; j<nData; ++j) {
1069 long actPos=input->tell();
1070 if (actPos+8>endPos) {
1071 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: bad entry\n"));
1072 f << "###";
1073 break;
1074 }
1075 f << "[";
1076 f << "f0=" << input->readLong(4) << ",";
1077 auto dataLen=input->readLong(4);
1078 if (dataLen==-1 && actPos+12<=endPos)
1079 f << "id=" << input->readLong(4) << ",";
1080 else if (dataLen>=0 && actPos+8+dataLen>=actPos+8 && actPos+8+dataLen<=endPos) {
1081 std::string name;
1082 for (int k=0; k<dataLen; ++k) {
1083 char c=char(input->readULong(1));
1084 if (!c)
1085 break;
1086 name+=c;
1087 }
1088 f << name << ",";
1089 input->seek(actPos+8+dataLen, librevenge::RVNG_SEEK_SET);
1090 }
1091 else {
1092 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: bad entry length\n"));
1093 f << "###";
1094 break;
1095 }
1096 f << "],";
1097 }
1098 f << "],";
1099 break;
1100 }
1101 default:
1102 /* find also
1103 id3210: 00000001000000010000000001000000000100
1104 */
1105 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pceg]: reading other than id=1 data is not implemented\n"));
1106 f << "###";
1107 }
1108 break;
1109 case 0x706f626a: // pobj
1110 case 0x7478726c: // txrl: very rare
1111 if (len%4) {
1112 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[pobj/txrl]: unexpected size\n"));
1113 f << "###";
1114 break;
1115 }
1116 for (int j=0; j<len/4; ++j) {
1117 val=int(input->readLong(4));
1118 if (val!=(j==0 ? 1 : 0))
1119 f << "f" << j << "=" << val << ",";
1120 }
1121 break;
1122 case 0x70726e74: // prnt
1123 readPrinterRsrc(*stream);
1124 break;
1125 case 0x76696e66: { // vinf
1126 if (len==0) break;
1127 switch (id) {
1128 case 1: { // v5 or v6
1129 if (len<4) {
1130 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[vinf]: can not find the number of view\n"));
1131 f << "###";
1132 break;
1133 }
1134 N=int(input->readULong(4));
1135 f << "N=" << N << ",";
1136 if (long(52*(unsigned long)(N+1)+4)<4 || N<0 || (len-4-52)/52<N || len!=52*(N+1)+4) {
1137 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[vinf]: can not find the number of view\n"));
1138 f << "###";
1139 break;
1140 }
1141 ascFile.addPos(input->tell());
1142 ascFile.addNote("_");
1143 input->seek(52, librevenge::RVNG_SEEK_CUR);
1144 libmwaw::DebugStream f2;
1145 for (int v=1; v<=N; ++v) {
1146 long actPos=input->tell();
1147 f2.str("");
1148 f2 << "Rsrcvinf-v:";
1149 std::string text;
1150 for (int c=0; c<36; ++c) {
1151 char ch=char(input->readULong(1));
1152 if (!ch) break;
1153 text+=ch;
1154 }
1155 f2 << text << ",";
1156 input->seek(actPos+36, librevenge::RVNG_SEEK_SET);
1157 f2 << "val=["; // scale then translation
1158 for (int d=0; d<4; ++d) f2 << float(input->readLong(4))/65536.f << ",";
1159 f2 << "],";
1160 ascFile.addPos(actPos);
1161 ascFile.addNote(f2.str().c_str());
1162 input->seek(actPos+52, librevenge::RVNG_SEEK_SET);
1163 }
1164 break;
1165 }
1166 case 3: { // v7
1167 if (len<4) {
1168 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[vinf]: can not find the number of view\n"));
1169 f << "###";
1170 break;
1171 }
1172 N=int(input->readULong(4));
1173 f << "N=" << N << ",";
1174 if (long(196*(unsigned long)(N+1)+4)<4 || N<0 || (len-4-196)/196<N || len!=196*(N+1)+4) {
1175 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[vinf,3]: can not find the number of view\n"));
1176 f << "###";
1177 break;
1178 }
1179 ascFile.addPos(input->tell());
1180 ascFile.addNote("_");
1181 input->seek(196, librevenge::RVNG_SEEK_CUR);
1182 libmwaw::DebugStream f2;
1183 for (int v=1; v<=N; ++v) {
1184 long actPos=input->tell();
1185 f2.str("");
1186 f2 << "Rsrcvinf-v:";
1187 std::string text;
1188 for (int c=0; c<52; ++c) { // at least 52 characters, maybe more
1189 char ch=char(input->readULong(1));
1190 if (!ch) break;
1191 text+=ch;
1192 }
1193 f2 << text << ",";
1194 input->seek(actPos+52, librevenge::RVNG_SEEK_SET);
1195 ascFile.addDelimiter(input->tell(),'|');
1196 ascFile.addPos(actPos);
1197 ascFile.addNote(f2.str().c_str());
1198 input->seek(actPos+196, librevenge::RVNG_SEEK_SET);
1199 }
1200 break;
1201 }
1202 default:
1203 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[vinf]: unexpected id=%d\n", id));
1204 f << "###";
1205 break;
1206 }
1207 break;
1208 }
1209 // ----- v6 ---------
1210 case 0x41474946: // AGIF
1211 if (len==0)
1212 break;
1213 m_imageParser->readAGIFRsrc(stream);
1214 break;
1215 case 0x4c617944: // LayD
1216 switch (id) {
1217 case 101:
1218 if (len!=8) {
1219 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[LayD,101]: unexpected size\n"));
1220 f << "###";
1221 break;
1222 }
1223 for (int j=0; j<2; ++j) { // f0=1|4, f1=1|4|5
1224 val=int(input->readLong(4));
1225 if (val!=1)
1226 f << "f" << j << "=" << Canvas5Structure::getString(unsigned(val)) << ",";
1227 }
1228 break;
1229 case 1000: { // v9
1230 if ((len%4)!=0) {
1231 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[LayD,1000]: unexpected size\n"));
1232 f << "###";
1233 break;
1234 }
1235 f << "unkn=[";
1236 int n=int(len/4);
1237 for (int j=0; j<n; ++j) {
1238 val=int(input->readULong(4));
1239 if (val>1000) // a date ?
1240 f << std::hex << val << std::dec << ",";
1241 else if (val) // small number
1242 f << val << ",";
1243 else
1244 f << "_,";
1245 }
1246 f << "],";
1247 break;
1248 }
1249 default:
1250 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[LayD]: unexpected id=%d\n", id));
1251 f << "###";
1252 break;
1253 }
1254 break;
1255 case 0x4f4c6e6b: // OLnk
1256 readOLnkRsrc(stream);
1257 break;
1258 case 0x50414750: // PAGP
1259 if (len!=4) {
1260 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[PAGD]: unexpected size\n"));
1261 f << "###";
1262 break;
1263 }
1264 f << "pag?=" << int(input->readLong(4)) << ",";
1265 break;
1266 case 0x584f4244: // XOBD
1267 readObjectDBRsrc(stream);
1268 break;
1269 // case 0x57454245: WEBE, size 10c, little endian? maybe related to a button url?
1270 case 0x74797065: // type
1271 if (len!=4) {
1272 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[type]: unexpected size\n"));
1273 f << "###";
1274 break;
1275 }
1276 val=int(input->readLong(4));
1277 if (val!=1)
1278 f << "f0=" << val << ",";
1279 break;
1280 // ----- v7 ---------
1281 case 0x6368636b: // chck
1282 if (len!=16) {
1283 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[chck]: unexpected size\n"));
1284 f << "###";
1285 break;
1286 }
1287 for (int j=0; j<2; ++j) {
1288 val=int(input->readLong(2));
1289 if (val!=1-j)
1290 f << "f" << j << "=" << val << ",";
1291 }
1292 f << "unkn=[";
1293 for (int j=0; j<10; ++j) { // unsure: either small number or big
1294 val=int(input->readLong(1));
1295 if (val)
1296 f << val << ",";
1297 else
1298 f << "_";
1299 }
1300 f << "],";
1301 val=int(input->readLong(2));
1302 if (val)
1303 f << "f1=" << val << ",";
1304 break;
1305 case 0x48544d4c: // HTML
1306 if (len!=16) {
1307 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[HTML]: unexpected size\n"));
1308 f << "###";
1309 break;
1310 }
1311 val=int(input->readLong(2));
1312 if (val!=0x100)
1313 f << "f0=" << val << ",";
1314 f << "unkn=[";
1315 for (int j=0; j<10; ++j) { // 0-2
1316 val=int(input->readLong(1));
1317 if (val)
1318 f << val << ",";
1319 else
1320 f << "_";
1321 }
1322 f << "],";
1323 val=int(input->readLong(4));
1324 if (val!=250)
1325 f << "f1=" << val << ",";
1326 break;
1327 case 0x6d747874: // mtxt (with id==KERN)
1328 if (len!=4) {
1329 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[mtext]: unexpected size\n"));
1330 f << "###";
1331 break;
1332 }
1333 for (int j=0; j<2; ++j) { // small number
1334 val=int(input->readLong(2));
1335 if (val)
1336 f << "f" << j << "=" << val << ",";
1337 }
1338 f << "],";
1339 break;
1340 // ---- v8 ----
1341 case 0x4453504c: // DSPL with id=2
1342 if (len!=8) {
1343 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[DSPL]: unexpected size\n"));
1344 f << "###";
1345 break;
1346 }
1347 for (int j=0; j<2; ++j) { // f0=72
1348 val=int(input->readLong(4));
1349 if (val)
1350 f << "f" << j << "=" << val << ",";
1351 }
1352 f << "],";
1353 break;
1354 // ----- v9 ---------
1355 case 0x23524c52: // #RLR
1356 if (len<2) {
1357 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[#RLR]: unexpected size\n"));
1358 f << "###";
1359 break;
1360 }
1361 val=int(input->readLong(2));
1362 if (val) // a sub type?
1363 f << "f0=" << val << ",";
1364 while (input->tell()<endPos) {
1365 long actPos=input->tell();
1366 if (actPos+22>endPos)
1367 break;
1368 f << "[";
1369 val=int(input->readULong(2));
1370 f << char(val>>8) << char(val&0xff) << ",";
1371 long l=long(input->readULong(4));
1372 if (l<0 || (long)((unsigned long)actPos+22+(unsigned long)l) < actPos+22 || actPos+22+l>endPos) {
1373 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[#RLR]: unexpected size\n"));
1374 f << "###";
1375 break;
1376 }
1377 std::string name;
1378 for (int j=0; j<l; ++j) {
1379 char c=char(input->readLong(1));
1380 if (!c)
1381 break;
1382 name+=c;
1383 }
1384 f << name << ",";
1385 input->seek(actPos+6+l, librevenge::RVNG_SEEK_SET);
1386 for (int j=0; j<2; ++j) {
1387 if (j==1 && input->tell()+12>=endPos) {
1388 // the last one look like a special case
1389 for (int k=0; k<3; ++k) {
1390 val=int(input->readLong(4));
1391 int const expected[]= {1,17,1};
1392 if (val!=expected[k])
1393 f << "f" << k << "=" << val << ",";
1394 }
1395 break;
1396 }
1397 double value;
1398 bool isNan;
1399 if (!readDouble(*stream, value, isNan)) {
1400 f << "###,";
1401 input->seek(6+actPos+l+8*(j+1), librevenge::RVNG_SEEK_SET);
1402 }
1403 else
1404 f << value << ",";
1405 }
1406 f << "],";
1407 }
1408 break;
1409 case 0x67697321: // gis!
1410 switch (id) {
1411 case 1: {
1412 std::string name;
1413 for (int j=0; j<len; ++j) {
1414 char c=char(input->readLong(1));
1415 if (!c) break;
1416 name+=c;
1417 }
1418 f << name << ",";
1419 break;
1420 }
1421 case 0x64676973:
1422 if (len<0xc0) {
1423 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[gis!]: unexpected length\n"));
1424 f << "###";
1425 break;
1426 }
1427 for (int j=0; j<5; ++j) {
1428 val=int(input->readULong(4));
1429 int const expected[]= {0x1e /* a000000*/, 0x17c /* c0000000*/, 0, 2, 0x18b6};
1430 if (val!=expected[j])
1431 f << "f" << j << "=" << std::hex << val << std::dec << ",";
1432 }
1433 for (int j=0; j<11; ++j) { // g0=-100,g3=40,g4=30.5,g5=47.5,g6=0|40,g9=g10=0|1
1434 double value=readDouble(*stream, 8);
1435 if (value<0 || value>0)
1436 f << "g" << j << "=" << value << ",";
1437 }
1438 for (int j=0; j<5; ++j) {
1439 val=int(input->readLong(4));
1440 if (val)
1441 f << "h" << j << "=" << val << ",";
1442 }
1443 for (int j=0; j<2; ++j) {
1444 double value=readDouble(*stream, 8);
1445 if (value<1 || value>1)
1446 f << "g" << j+6 << "=" << value << ",";
1447 }
1448 f << "unkn=[";
1449 for (int st=0; st<2; ++st) {
1450 f << "[";
1451 for (int j=0; j<5; ++j) { // f0=1, f1=2329|2384|2394, f2=0|-1, f4=3|0 : font?
1452 val=int(input->readLong(4));
1453 if (val)
1454 f << "f" << j << "=" << val << ",";
1455 }
1456 f << "],";
1457 }
1458 f << "],";
1459 f << "g8=" << readDouble(*stream, 8) << ","; // 0|792
1460 ascFile.addDelimiter(input->tell(),'|');
1461 break;
1462 // find also 0x67697332 with size=17c
1463 default:
1464 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[gis!]: unexpected id\n"));
1465 f << "###";
1466 break;
1467 }
1468 break;
1469 case 0x444e4156: // DNAV
1470 if (len>0) {
1471 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[DNAV]: unexpected size\n"));
1472 f << "###";
1473 break;
1474 }
1475 break;
1476 // case 0x4d524b50: MRKP with size 121 a list of string ?
1477 default:
1478 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs[%s]: unexpected resource\n", Canvas5Structure::getString(what).c_str()));
1479 f << "###";
1480 break;
1481 }
1482 input->popLimit();
1483 if (input->tell()!=endPos)
1484 ascFile.addDelimiter(input->tell(),'|');
1485 ascFile.addPos(pos);
1486 ascFile.addNote(f.str().c_str());
1487 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1488 }
1489
1490 pos=input->tell();
1491 long len=input->readLong(4);
1492 if ((long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
1493 MWAW_DEBUG_MSG(("Canvas5Parser::readFileRSRCs: can not find font block\n"));
1494 ascFile.addPos(pos);
1495 ascFile.addNote("Entries(Font):###");
1496 return false;
1497 }
1498 if (!m_styleManager->readFonts(stream, int(len/136)))
1499 return false;
1500 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
1501
1502 if (version()>=9)
1503 return true;
1504
1505 pos=input->tell();
1506 ascFile.addPos(pos);
1507 ascFile.addNote("Entries(RsrcStrings):");
1508
1509 if (!readUsed(*stream, "RsrcStrings"))
1510 return false;
1511
1512 if (!readIndexMap(stream, "RsrcStrings", &Canvas5Parser::stringDataFunction))
1513 return false;
1514
1515 return true;
1516 }
1517
1518 bool Canvas5Parser::readSI200(Canvas5Structure::Stream &stream)
1519 {
1520 auto input=stream.input();
1521 if (!input) return false;
1522
1523 long pos=input->tell(), begPos=pos;
1524 if (!input->checkPosition(pos+12)) {
1525 MWAW_DEBUG_MSG(("Canvas5Parser::readSI200: the zone is too short\n"));
1526 return false;
1527 }
1528 bool isWindows=isWindowsFile();
1529 libmwaw::DebugStream f;
1530 auto &ascFile=stream.ascii();
1531 f << "Entries(SumInfo):";
1532 std::string name;
1533 for (int i=0; i<8; ++i) name+=char(input->readULong(1));
1534 if (name!="%SI-0200") {
1535 MWAW_DEBUG_MSG(("Canvas5Parser::readSI200: can not find the zone name\n"));
1536 return false;
1537 }
1538 long endPos=input->size();
1539 if (!isWindows) {
1540 long len=long(input->readULong(4));
1541 endPos=pos+8+len+12;
1542 if (len<0 || endPos<pos+12 || !input->checkPosition(endPos)) {
1543 MWAW_DEBUG_MSG(("Canvas5Parser::readSI200: can not find the zone len\n"));
1544 f << "###";
1545 ascFile.addPos(pos);
1546 ascFile.addNote(f.str().c_str());
1547 return false;
1548 }
1549 }
1550 else {
1551 int val=int(input->readLong(4));
1552 if (val!=0x100)
1553 f << "f0=" << val << ",";
1554 }
1555 ascFile.addPos(pos);
1556 ascFile.addNote(f.str().c_str());
1557
1558 bool ok=true;
1559 MWAWOLEParser OLEParser("", getParserState()->m_fontConverter, 2);
1560 for (int wh=0; wh<2; ++wh) {
1561 int encoding;
1562 librevenge::RVNGPropertyList pList;
1563 if (!OLEParser.readSummaryInformation(input, wh==0 ? "SummaryInformation" : "DocumentSummaryInformation", encoding, wh==0 ? m_state->m_metaData : pList, ascFile, endPos)) {
1564 f << "###";
1565 ok=false;
1566 break;
1567 }
1568 // fixme: v7 seems to do not write the potentially unused data at the end...
1569 }
1570
1571 pos=input->tell();
1572 f.str("");
1573 f << "SumInfo-End:";
1574 if (ok && pos+28<=endPos) {
1575 long len=long(input->readLong(4));
1576 if (begPos+len<pos+4 || begPos+len>endPos)
1577 ok=false;
1578 else
1579 input->seek(begPos+len, librevenge::RVNG_SEEK_SET);
1580 }
1581 if (!ok)
1582 f << "###";
1583 ascFile.addPos(pos);
1584 ascFile.addNote(f.str().c_str());
1585 if (!isWindows) {
1586 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1587 return true;
1588 }
1589 return ok;
1590 }
1591
1592 bool Canvas5Parser::readFileDesc(Canvas5Structure::Stream &stream)
1593 {
1594 auto input=stream.input();
1595 if (!input) return false;
1596
1597 long pos=input->tell();
1598 if (!input->checkPosition(pos+0x30c)) {
1599 MWAW_DEBUG_MSG(("Canvas5Parser::readFileDesc: the zone is too short\n"));
1600 return false;
1601 }
1602
1603 libmwaw::DebugStream f;
1604 auto &ascFile=stream.ascii();
1605 f << "Entries(FileDesc):";
1606 int byteOrdering=int(input->readULong(1));
1607 switch (byteOrdering) {
1608 case 1:
1609 input->setReadInverted(true);
1610 break;
1611 case 2:
1612 input->setReadInverted(false);
1613 break;
1614 default:
1615 MWAW_DEBUG_MSG(("Canvas5Parser::readFileDesc: unknown byte ordering\n"));
1616 return false;
1617 }
1618 input->seek(3, librevenge::RVNG_SEEK_CUR);
1619 unsigned what=unsigned(input->readULong(4));
1620 if (what!=0x434e5635)
1621 return false;
1622 ascFile.addPos(pos);
1623 ascFile.addNote(f.str().c_str());
1624
1625 for (int z=0; z<3; ++z) { // z=0: always empty, z=1: full path, z=2: filename
1626 pos=input->tell();
1627 f.str("");
1628 f << "FileDesc" << z << ":";
1629 std::string text;
1630 for (int c=0; c<256; ++c) {
1631 char ch=char(input->readULong(1));
1632 if (!ch) break;
1633 text+=ch;
1634 }
1635 f << text << ",";
1636 ascFile.addPos(pos);
1637 ascFile.addNote(f.str().c_str());
1638 input->seek(pos+256, librevenge::RVNG_SEEK_SET);
1639 }
1640
1641 pos=input->tell();
1642 f.str("");
1643 f << "FileDesc-end:";
1644 what=unsigned(input->readULong(4));
1645 if (what!=0x434e5635)
1646 f << what;
1647 ascFile.addPos(pos);
1648 ascFile.addNote(f.str().c_str());
1649
1650 return true;
1651 }
1652
1653 bool Canvas5Parser::readFileHeader(std::shared_ptr<Canvas5Structure::Stream> stream)
1654 {
1655 if (!stream || !stream->input()) return false;
1656 auto input=stream->input();
1657 int const vers=version();
1658 int const headerSize=vers>=9 ? 0x2d : 0x2a;
1659 if (!input->checkPosition(headerSize)) {
1660 MWAW_DEBUG_MSG(("Canvas5Parser::readFileHeader: the zone is too short\n"));
1661 return false;
1662 }
1663 input->seek(vers>=9 ? 15 : 5, librevenge::RVNG_SEEK_SET);
1664 libmwaw::DebugStream f;
1665 auto &ascFile=stream->ascii();
1666 f << "FileHeader:";
1667 int val=int(input->readULong(1));
1668 switch (val) {
1669 case 0x17:
1670 f << "win,";
1671 break;
1672 case 0x18:
1673 f << "mac,";
1674 break;
1675 default:
1676 #ifdef DEBUG
1677 MWAW_DEBUG_MSG(("Canvas5Parser::readFileHeader: unknown file type\n"));
1678 f << "###file[type]=" << val << ",";
1679 break;
1680 #else
1681 return false;
1682 #endif
1683 }
1684 val=int(input->readULong(1));
1685 f << "byte[order]=" << val << ",";
1686 switch (val) {
1687 case 1:
1688 input->setReadInverted(true);
1689 break;
1690 case 2:
1691 input->setReadInverted(false);
1692 break;
1693 default:
1694 #ifdef DEBUG
1695 MWAW_DEBUG_MSG(("Canvas5Parser::readFileHeader: unknown byte order\n"));
1696 break;
1697 #else
1698 return false;
1699 #endif
1700 }
1701 val=int(input->readULong(2)); // 0 or 2
1702 if (val) f << "f0=" << val << ",";
1703 val=int(input->readULong(4));
1704 if (val!=0xea8da) f << "f1=" << std::hex << val << std::dec << ",";
1705
1706 std::string name;
1707 for (int i=0; i<7; ++i)
1708 name+=char(input->readULong(1));
1709 #ifdef DEBUG
1710 if (name!="CANVAS5" && name!="CANVAS6") {
1711 MWAW_DEBUG_MSG(("Canvas5Parser::readFileHeader: not a Canvas 5-8 file\n"));
1712 f << "name=" << name << ",";
1713 }
1714 #else
1715 if (name!="CANVAS5" && name!="CANVAS6")
1716 return false;
1717 #endif
1718 input->seek(1, librevenge::RVNG_SEEK_CUR);
1719 ascFile.addPos(0);
1720 ascFile.addNote(f.str().c_str());
1721
1722 long pos=input->tell();
1723 f.str("");
1724 f << "FileHeader-A:";
1725 m_state->m_type=int(input->readULong(1));
1726 switch (m_state->m_type) {
1727 case 1:
1728 f << "illustration,";
1729 break;
1730 case 2: // list of pages with header/footer
1731 f << "publi,";
1732 break;
1733 case 3:
1734 f << "slide,";
1735 break;
1736 default:
1737 MWAW_DEBUG_MSG(("Canvas5Parser::readFileHeader: unknown document type %d\n", m_state->m_type));
1738 f << "##type=" << m_state->m_type << ",";
1739 m_state->m_type=1;
1740 #ifndef DEBUG
1741 return false;
1742 #endif
1743 break;
1744 }
1745 input->seek(1, librevenge::RVNG_SEEK_CUR);
1746 int N=int(input->readULong(2)); // number block of size 400000 ?
1747 if (N) f << "h[sz]=" << N << "*256k,";
1748 val=int(input->readULong(4));
1749 f << "vers=" << (val>>8) << ",";
1750 val &= 0xff;
1751 if (val!=0x2) // 0|1|2, 0 if no FileDesc,Rsrcpceg,... ?
1752 f << "f0=" << std::hex << val << std::dec << ",";
1753 for (int i=0; i<6; ++i) {
1754 val=int(input->readULong(2));
1755 if (val==0)
1756 continue;
1757 f << "f" << i << "=" << val << ",";
1758 }
1759 if (vers<9) {
1760 m_state->m_fileFlags=int(input->readULong(1));
1761 if (m_state->m_fileFlags==0x22)
1762 f << "no[preview],";
1763 else if (m_state->m_fileFlags!=0x21)
1764 f << "fl=" << std::hex << m_state->m_fileFlags << std::dec << ",";
1765 }
1766 ascFile.addDelimiter(input->tell(),'|');
1767 ascFile.addPos(pos);
1768 ascFile.addNote(f.str().c_str());
1769 input->seek(pos+(vers>=9 ? 24 : 21), librevenge::RVNG_SEEK_SET);
1770
1771 return true;
1772 }
1773
1774 bool Canvas5Parser::readDocumentSettings(std::shared_ptr<Canvas5Structure::Stream> stream)
1775 {
1776 if (!stream || !stream->input())
1777 return false;
1778 auto input=stream->input();
1779 auto &ascFile=stream->ascii();
1780 long pos=input->tell();
1781 long endPos=pos+54;
1782 int const vers=version();
1783 if (vers>=9)
1784 endPos=pos+input->readLong(4);
1785 if (endPos<pos+54 || !input->checkPosition(endPos)) {
1786 MWAW_DEBUG_MSG(("Canvas5Parser::readDocumentSettings: the zone is too short\n"));
1787 return false;
1788 }
1789 libmwaw::DebugStream f;
1790 f << "Entries(DocSettings):";
1791 if (vers>=9) {
1792 int val=int(input->readLong(4));
1793 if (val!=100)
1794 f << "f0=" << val << ",";
1795 }
1796 f << "lengths=[";
1797 for (int i=0; i<5; ++i) { // 5 small number: f2=f3?, f4: not empty shape
1798 int val=int(input->readLong(4));
1799 if (i==1) m_state->m_numSlides=val;
1800 else if (i==3) m_state->m_numShapes=val;
1801 if (val)
1802 f << val << ",";
1803 else
1804 f << "_,";
1805 }
1806 f << "],";
1807 for (int i=0; i<(vers<9 ? 3 : 1); ++i) { // 3 small number
1808 int val=int(input->readLong(2));
1809 if (val==1)
1810 continue;
1811 if (i==0) {
1812 f << "num[layers]=" << val << ",";
1813 m_state->m_numLayers=val;
1814 continue;
1815 }
1816 else if (i==2) // checkme
1817 f << "cur[layer]=" << val << ",";
1818 else
1819 f << "f" << i+1 << "=" << val << ",";
1820 }
1821 ascFile.addPos(pos);
1822 ascFile.addNote(f.str().c_str());
1823
1824 pos=input->tell();
1825 f.str("");
1826 f << "DocSettings-A:";
1827 if (vers>=9) {
1828 ascFile.addPos(pos);
1829 ascFile.addNote(f.str().c_str());
1830 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1831 return true;
1832 }
1833 int val=int(input->readLong(2));
1834 if (val)
1835 f << "f0=" << val << ",";
1836 val=int(input->readLong(4)); // 72|100
1837 if (val!=0x480000)
1838 f << "f1=" << float(val)/65536 << ",";
1839 double dVal;
1840 bool isNan;
1841 if (!readDouble(*stream, dVal, isNan)) {
1842 MWAW_DEBUG_MSG(("Canvas5Parser::readDocumentSettings: can not read a double\n"));
1843 f << "###";
1844 }
1845 else
1846 f << "grid[dim]=" << dVal << "pt,";
1847 input->seek(pos+14, librevenge::RVNG_SEEK_SET);
1848 ascFile.addDelimiter(input->tell(),'|');
1849 ascFile.addPos(pos);
1850 ascFile.addNote(f.str().c_str());
1851 input->seek(pos+28, librevenge::RVNG_SEEK_SET);
1852
1853 if (!m_styleManager->readPenSize(stream))
1854 return false;
1855
1856 pos=input->tell();
1857 f.str("");
1858 f << "DocSettings-B:";
1859 if (!input->checkPosition(pos+4*256+134)) {
1860 MWAW_DEBUG_MSG(("Canvas5Parser::readDocumentSettings: the 0 zone seems too short\n"));
1861 f << "###";
1862 ascFile.addPos(pos);
1863 ascFile.addNote(f.str().c_str());
1864 return false;
1865 }
1866 input->seek(pos+38, librevenge::RVNG_SEEK_SET);
1867 ascFile.addDelimiter(input->tell(),'|');
1868
1869 f << "grid[dims]=[";
1870 for (int i=0; i<2; ++i) { // inches and points
1871 long actPos=input->tell();
1872 if (!readDouble(*stream, dVal, isNan)) {
1873 MWAW_DEBUG_MSG(("Canvas5Parser::readDocumentSettings: can not read a double\n"));
1874 f << "###";
1875 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
1876 }
1877 else
1878 f << dVal << (i==0 ? "in" : "pt") << ",";
1879 }
1880 ascFile.addDelimiter(input->tell(),'|');
1881 ascFile.addPos(pos);
1882 ascFile.addNote(f.str().c_str());
1883 input->seek(pos+128, librevenge::RVNG_SEEK_SET);
1884
1885 pos=input->tell();
1886 f.str("");
1887 f << "DocSettings-B1:";
1888 input->seek(pos+128+22, librevenge::RVNG_SEEK_SET);
1889 ascFile.addPos(pos);
1890 ascFile.addNote(f.str().c_str());
1891
1892 if (!m_styleManager->readFormats(stream))
1893 return false;
1894
1895 for (int i=0; i<6; ++i) {
1896 pos=input->tell();
1897 int const len=i==1 ? 118 : i==5 ? 58 : 98;
1898 f.str("");
1899 f << "DocSettings-C" << i << ":";
1900 ascFile.addPos(pos);
1901 ascFile.addNote(f.str().c_str());
1902 input->seek(pos+len, librevenge::RVNG_SEEK_SET);
1903 }
1904
1905 return true;
1906 }
1907
1908 bool Canvas5Parser::readLayers(std::shared_ptr<Canvas5Structure::Stream> stream)
1909 {
1910 if (!stream || !stream->input()) return false;
1911 auto input=stream->input();
1912 int const vers=version();
1913 long pos=input->tell();
1914 libmwaw::DebugStream f;
1915 auto &ascFile=stream->ascii();
1916 f << "Entries(Layer):";
1917 long len=input->readLong(4);
1918 long endPos=pos+4+len;
1919 int numLayers=vers<9 ? m_state->m_numLayers : int(len/60)-1;
1920 if (numLayers<0 || len<60*(numLayers+1) || endPos<pos+4 || !input->checkPosition(endPos)) {
1921 MWAW_DEBUG_MSG(("Canvas5Parser::readLayers: can not find the layer's header\n"));
1922 f << "###";
1923 ascFile.addPos(pos);
1924 ascFile.addNote(f.str().c_str());
1925 return false;
1926 }
1927 ascFile.addPos(pos);
1928 ascFile.addNote(f.str().c_str());
1929
1930 ascFile.addPos(pos+4);
1931 ascFile.addNote("_");
1932 input->seek(pos+60, librevenge::RVNG_SEEK_SET);
1933 auto fontConverter=getFontConverter();
1934 int defaultFont=isWindowsFile() ? fontConverter->getId("CP1252") : 3;
1935 for (int z=0; z<numLayers; ++z) {
1936 pos=input->tell();
1937 f.str("");
1938 f << "Layer-L" << z+1 << ":";
1939 Canvas5ParserInternal::Layer layer;
1940 for (int i=0; i<2; ++i) { // ?
1941 int val=int(input->readLong(2));
1942 if (val) f << "f" << i << "=" << val << ",";
1943 }
1944 f << "ID=" << std::hex << input->readULong(4) << std::dec << ",";
1945 int numShapes=int(input->readLong(4));
1946 f << "N=" << numShapes-1 << ",";
1947 layer.m_numShapes=numShapes-1;
1948 layer.m_type=int(input->readLong(4));
1949 f << "type=" << layer.m_type << ",";
1950 int val=int(input->readULong(4));
1951 if ((val&4)==0) f << "no[print],";
1952 if (val&8) f << "bw,";
1953 if (val&0x10) f << "protected,";
1954 val &= 0xffe3;
1955 if (val!=1) f << "fl=" << std::hex << val << std::dec << ",";
1956 val=int(input->readLong(4));
1957 if (val) // &8: also bw?
1958 f << "f0=" << val << ",";
1959 for (int i=0; i<36; ++i) {
1960 char c=char(input->readULong(1));
1961 if (!c)
1962 break;
1963
1964 int unicode = fontConverter->unicode(defaultFont, static_cast<unsigned char>(c));
1965 if (unicode>0)
1966 libmwaw::appendUnicode(uint32_t(unicode), layer.m_name);
1967 else {
1968 MWAW_DEBUG_MSG(("CanvasParser::readLayers: find unknown unicode for char=%d\n", int(c)));
1969 }
1970 }
1971 if (!layer.m_name.empty())
1972 f << layer.m_name.cstr() << ",";
1973 m_state->m_idToLayer[z+1]=layer;
1974 ascFile.addPos(pos);
1975 ascFile.addNote(f.str().c_str());
1976 input->seek(pos+60, librevenge::RVNG_SEEK_SET);
1977 }
1978 if (input->tell()!=endPos) { // find four bytes : junk?
1979 ascFile.addPos(input->tell());
1980 ascFile.addNote("Layer-end:");
1981 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1982 }
1983
1984 for (auto &it : m_state->m_idToLayer) {
1985 auto &layer = it.second;
1986 if (layer.m_type==-1) continue;
1987 int const nextShape=layer.m_numShapes+1;
1988 pos=input->tell();
1989 len=input->readLong(4);
1990 f.str("");
1991 f << "Layer-L" << it.first << ":";
1992 if (len<0 || (nextShape>1 && len<4*nextShape) || (long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
1993 MWAW_DEBUG_MSG(("Canvas5Parser::readLayers: can not find some layer\n"));
1994 f << "###";
1995 ascFile.addPos(pos);
1996 ascFile.addNote(f.str().c_str());
1997 return false;
1998 }
1999 if (nextShape>1) {
2000 input->seek(4, librevenge::RVNG_SEEK_CUR); // junk?
2001 f << "id=[";
2002 for (int s=1; s<nextShape; ++s) {
2003 layer.m_shapesId.push_back(int(input->readLong(4)));
2004 f << "S" << layer.m_shapesId.back() << ",";
2005 }
2006 f << "],";
2007 }
2008 ascFile.addPos(pos);
2009 ascFile.addNote(f.str().c_str());
2010 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
2011 }
2012 return true;
2013 }
2014
2015 bool Canvas5Parser::readPrinterRsrc(Canvas5Structure::Stream &stream)
2016 {
2017 auto input=stream.input();
2018 long pos=input->tell();
2019 if (!input->checkPosition(pos+16)) {
2020 MWAW_DEBUG_MSG(("Canvas5Parser::readPrinterRsrc: can not find the input\n"));
2021 return false;
2022 }
2023 libmwaw::DebugStream f;
2024 auto &ascFile=stream.ascii();
2025 f << "Rsrcprnt-header:";
2026 int val;
2027 for (int i=0; i<3; ++i) {
2028 val=int(input->readLong(4));
2029 if (val!=(i==0 ? 4 : 0))
2030 f << "f" << i << "=" << val << ",";
2031 }
2032
2033 long len=input->readLong(4);
2034 long endPos=pos+16+len;
2035 if (endPos<pos+16+24 || !input->checkPosition(endPos)) {
2036 MWAW_DEBUG_MSG(("Canvas5Parser::readPrinterRsrc: can not find the input\n"));
2037 f << "###";
2038 ascFile.addPos(pos);
2039 ascFile.addNote(f.str().c_str());
2040 return false;
2041 }
2042 ascFile.addPos(pos);
2043 ascFile.addNote(f.str().c_str());
2044
2045 pos=input->tell();
2046 f.str("");
2047 f << "Rsrcprnt-A:";
2048 for (int i=0; i<5; ++i) {
2049 val=int(input->readLong(4));
2050 int const expected[]= {0x4000, 0, 3, 0, 0};
2051 if (val!=expected[i])
2052 f << "f" << i << "=" << val << ",";
2053 }
2054 len=input->readLong(4);
2055 long end1Pos=pos+24+len;
2056 if (end1Pos>endPos) {
2057 MWAW_DEBUG_MSG(("Canvas5Parser::readPrinterRsrc: first block seems bad\n"));
2058 f << "###";
2059 ascFile.addPos(pos);
2060 ascFile.addNote(f.str().c_str());
2061 return false;
2062 }
2063 ascFile.addPos(pos);
2064 ascFile.addNote(f.str().c_str());
2065 int N=int(len/64);
2066 for (int i=0; i<N; ++i) {
2067 pos=input->tell();
2068 f.str("");
2069 f << "Rsrcprnt-B" << i << ":";
2070 std::string name;
2071 for (int j=0; j<32; ++j) {
2072 char ch=char(input->readLong(1));
2073 if (!ch)
2074 break;
2075 name+=ch;
2076 }
2077 f << name << ",";
2078 input->seek(pos+32, librevenge::RVNG_SEEK_SET);
2079 for (int j=0; j<6; ++j) { // 0
2080 val=int(input->readLong(2));
2081 if (val!=(j==5 ? -1 : 0))
2082 f << "f" << j << "=" << val << ",";
2083 }
2084 f << "col=[";
2085 for (int j=0; j<4; ++j) f << int(input->readULong(2)>>8) << ",";
2086 f << "],";
2087 std::string what; // cmyk, rgb, sepp
2088 for (int j=0; j<4; ++j) what+=char(input->readULong(1));
2089 f << what << ",";
2090 for (int j=0; j<4; ++j) { // g0=2d-69, g2=3c
2091 val=int(input->readLong(2));
2092 if (val!=(j==2 ? 0x3c : 0))
2093 f << "g" << j << "=" << val << ",";
2094 }
2095 ascFile.addPos(pos);
2096 ascFile.addNote(f.str().c_str());
2097 input->seek(pos+64, librevenge::RVNG_SEEK_SET);
2098 }
2099 input->seek(end1Pos, librevenge::RVNG_SEEK_SET);
2100
2101 pos=input->tell();
2102 f.str("");
2103 f << "Rsrcprnt-C:";
2104 int const vers=version();
2105 int const zoneLen=vers==5 ? 18 : 58;
2106 if (pos+zoneLen>endPos) {
2107 MWAW_DEBUG_MSG(("Canvas5Parser::readPrinterRsrc: second block seems bad\n"));
2108 f << "###";
2109 ascFile.addPos(pos);
2110 ascFile.addNote(f.str().c_str());
2111 return false;
2112 }
2113 for (int i=0; i<7; ++i) {
2114 val=int(input->readLong(2));
2115 if (val!=(i==6 ? 1 : 0))
2116 f << "f" << i << "=" << val << ",";
2117 }
2118 if (input->tell()!=pos+zoneLen-4) {
2119 ascFile.addDelimiter(input->tell(),'|');
2120 input->seek(pos+zoneLen-4, librevenge::RVNG_SEEK_SET);
2121 ascFile.addDelimiter(input->tell(),'|');
2122 }
2123 len=input->readLong(4);
2124 if ((len && len<0x78) || pos+len+zoneLen<pos || pos+len+zoneLen>endPos) {
2125 MWAW_DEBUG_MSG(("Canvas5Parser::readPrinterRsrc: printInfo block seems bad\n"));
2126 f << "###";
2127 ascFile.addPos(pos);
2128 ascFile.addNote(f.str().c_str());
2129 return false;
2130 }
2131 ascFile.addPos(pos);
2132 ascFile.addNote(f.str().c_str());
2133
2134 if (len) {
2135 pos=input->tell();
2136 f.str("");
2137 f << "Rsrcprnt-PrintInfo:";
2138 if (!m_state->m_isWindowsFile) {
2139 libmwaw::PrinterInfo info;
2140 if (!info.read(input)) {
2141 MWAW_DEBUG_MSG(("CanvasParser::readPrinterRsrc: can not read the print info data\n"));
2142 f << "###";
2143 ascFile.addPos(pos);
2144 ascFile.addNote(f.str().c_str());
2145 return false;
2146 }
2147 f << info;
2148 }
2149 else {
2150 std::string name;
2151 for (int i=0; i<32; ++i) {
2152 char c=char(input->readULong(1));
2153 if (!c)
2154 break;
2155 name += c;
2156 }
2157 f << name << ",";
2158 input->seek(pos+32, librevenge::RVNG_SEEK_SET);
2159 ascFile.addDelimiter(input->tell(),'|');
2160 // TODO: read the end of this big zone
2161 }
2162 input->seek(pos+len, librevenge::RVNG_SEEK_SET);
2163 ascFile.addPos(pos);
2164 ascFile.addNote(f.str().c_str());
2165 }
2166
2167 if (input->tell()<endPos) { // find sometimes 1,0 in mac file and in windows 1 len...
2168 ascFile.addPos(input->tell());
2169 ascFile.addNote("Rsrcprnt-end:#");
2170 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2171 }
2172
2173 return true;
2174 }
2175
2176 bool Canvas5Parser::readOLnkRsrc(std::shared_ptr<Canvas5Structure::Stream> stream)
2177 {
2178 if (!stream) {
2179 MWAW_DEBUG_MSG(("Canvas5Parser::readOLnkRsrc: no stream\n"));
2180 return false;
2181 }
2182 auto input=stream->input();
2183 long pos;
2184 libmwaw::DebugStream f;
2185 auto &ascFile=stream->ascii();
2186
2187 if (!readExtendedHeader(stream, 12, "RsrcOLnk", &Canvas5Parser::defDataFunction)) // id?, Posn, id?
2188 return false;
2189 if (!readIndexMap(stream, "RsrcOLnk")) // size 6, X, Y, 1
2190 return false;
2191
2192 std::vector<bool> defined;
2193 if (!readDefined(*stream, defined, "RsrcOLnk"))
2194 return false;
2195
2196 pos=input->tell();
2197 f.str("");
2198 f << "RsrcOLnk-A:";
2199 int N;
2200 if (!readDataHeader(*stream, 12,N)) {
2201 MWAW_DEBUG_MSG(("Canvas5Parser::readOLnkRsrc: can not the number N\n"));
2202 f << "###";
2203 ascFile.addPos(pos);
2204 ascFile.addNote(f.str().c_str());
2205 return false;
2206 }
2207 f << "N=" << N << ",";
2208 ascFile.addPos(pos);
2209 ascFile.addNote(f.str().c_str());
2210
2211 for (int i=1; i<=N; ++i) { // id?, id?, ????
2212 pos=input->tell();
2213 f.str("");
2214 f << "RsrcOLnk-A" << i << ":";
2215 ascFile.addPos(pos);
2216 ascFile.addNote(f.str().c_str());
2217 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
2218 }
2219
2220 if (input->isEnd()) {
2221 MWAW_DEBUG_MSG(("Canvas5Parser::readOLnkRsrc: can not find the last part\n"));
2222 return true;
2223 }
2224 // find 0004 here, unsure if this is normal, maybe there is some decalage
2225 pos=input->tell();
2226 f.str("");
2227 f << "RsrcOLnk-extra:#";
2228 ascFile.addPos(pos);
2229 ascFile.addNote(f.str().c_str());
2230
2231 return true;
2232 }
2233
2234 bool Canvas5Parser::readObjectDBRsrc(std::shared_ptr<Canvas5Structure::Stream> stream)
2235 {
2236 if (!stream) {
2237 MWAW_DEBUG_MSG(("Canvas5Parser::readObjectDBRsrc: can not find the stream\n"));
2238 return false;
2239 }
2240 auto input=stream->input();
2241 long pos=input->tell();
2242 if (!input->checkPosition(pos+32)) {
2243 MWAW_DEBUG_MSG(("Canvas5Parser::readObjectDBRsrc: can not find the input\n"));
2244 return false;
2245 }
2246 libmwaw::DebugStream f;
2247 auto &ascFile=stream->ascii();
2248 f << "RsrcXOBD-header:";
2249 for (int i=0; i<4; ++i) {
2250 int val=int(input->readLong(2));
2251 int const expected[]= {0, 1, 0x200, 0};
2252 if (val!=expected[i])
2253 f << "f" << i << "=" << val << ",";
2254 }
2255 float fDim[4];
2256 for (auto &d : fDim) d=float(input->readULong(4))/65536.f;
2257 f << "box=" << MWAWBox2f(MWAWVec2f(fDim[0], fDim[1]), MWAWVec2f(fDim[2], fDim[3])) << ","; // checkme: probably bad
2258 ascFile.addPos(pos);
2259 ascFile.addNote(f.str().c_str());
2260
2261 if (!readExtendedHeader(stream, 4, "RsrcXOBD0", &Canvas5Parser::defDataFunction)) // small number: some flag?
2262 return false;
2263 if (!readIndexMap(stream, "RsrcXOBD0")) // size 12, [type:4, subId:2, 1|2:1 endian?, 0:1, id:4] #XAP with suid=10, TRSP with subId=1,2, XOBD with subid=1000,1001
2264 return false;
2265 std::vector<bool> defined;
2266 if (!readDefined(*stream, defined, "RsrcXOBD0"))
2267 return false;
2268 if (!readExtendedHeader(stream, 4, "RsrcXOBD1", &Canvas5Parser::defDataFunction)) // data to type?
2269 return false;
2270 if (!readIndexMap(stream, "RsrcXOBD1")) // size at least 40, depend probably of the first index map
2271 return false;
2272 if (!readDefined(*stream, defined, "RsrcXOBD1"))
2273 return false;
2274
2275 if (input->isEnd())
2276 return true;
2277
2278 MWAW_DEBUG_MSG(("Canvas5Parser::readObjectDBRsrc: find extra data\n"));
2279 pos=input->tell();
2280 f.str("");
2281 f << "RsrcXOBD-extra:###";
2282 ascFile.addPos(pos);
2283 ascFile.addNote(f.str().c_str());
2284
2285 return true;
2286 }
2287
2288 bool Canvas5Parser::readTextLinks(std::shared_ptr<Canvas5Structure::Stream> stream)
2289 {
2290 if (!stream) {
2291 MWAW_DEBUG_MSG(("Canvas5Parser::readTextLinks: bad stream\n"));
2292 return false;
2293 }
2294 auto input=stream->input();
2295 if (!input || !input->checkPosition(input->tell()+1)) {
2296 MWAW_DEBUG_MSG(("Canvas5Parser::readTextLinks: the zone is too short\n"));
2297 return false;
2298 }
2299 auto &ascFile=stream->ascii();
2300 long pos=input->tell();
2301 ascFile.addPos(pos);
2302 ascFile.addNote("Entries(TxtLink):");
2303 input->seek(1, librevenge::RVNG_SEEK_CUR); // 0-3 | 67 | 72 | 99
2304 if (!readExtendedHeader(stream, 1, "TxtLink", &Canvas5Parser::defDataFunction))
2305 return false;
2306 if (!readIndexMap(stream, "TxtLink",
2307 [](std::shared_ptr<Canvas5Structure::Stream> lStream, Item const &item, std::string const &) {
2308 auto lInput=lStream->input();
2309 auto &asciiFile=lStream->ascii();
2310 if (item.m_length<8) {
2311 MWAW_DEBUG_MSG(("Canvas5Parser::readTextLinks: can not read the txtLink\n"));
2312 asciiFile.addPos(item.m_pos);
2313 asciiFile.addNote("###");
2314 return;
2315 }
2316 libmwaw::DebugStream f;
2317 f << "TL" << item.m_id << ":";
2318 int N=int(lInput->readULong(4));
2319 f << "N=" << N << ",";
2320 if ((item.m_length-8)/4<N || 8+4*N>item.m_length) {
2321 MWAW_DEBUG_MSG(("Canvas5Parser::readTextLinks: can not read the txtLink N\n"));
2322 asciiFile.addPos(item.m_pos);
2323 asciiFile.addNote("###");
2324 }
2325 int val=int(lInput->readULong(4));
2326 if (val)
2327 f << "f0=" << val << ",";
2328 f << "id=[";
2329 for (int i=0; i<N; ++i)
2330 f << "TLb" << lInput->readULong(4) << ",";
2331 f << "],";
2332 asciiFile.addPos(item.m_pos);
2333 asciiFile.addNote(f.str().c_str());
2334 }))
2335 return false;
2336 std::vector<bool> defined;
2337 if (!readDefined(*stream, defined, "TxtLink"))
2338 return false;
2339
2340 if (!readExtendedHeader(stream, 1, "TxtLink-B", &Canvas5Parser::defDataFunction))
2341 return false;
2342 if (!readIndexMap(stream, "TxtLink-B",
2343 [](std::shared_ptr<Canvas5Structure::Stream> lStream, Item const &item, std::string const &) {
2344 auto lInput=lStream->input();
2345 long lPos=lInput->tell();
2346 auto &asciiFile=lStream->ascii();
2347 if (item.m_length<28) {
2348 MWAW_DEBUG_MSG(("Canvas5Parser::readTextLinks: can not read the txtPlcB\n"));
2349 asciiFile.addPos(item.m_pos);
2350 asciiFile.addNote("###");
2351 }
2352 libmwaw::DebugStream f;
2353 f << "TLb" << item.m_id << ":";
2354 f << "fl=" << std::hex << lInput->readULong(2) << std::dec << ","; // 6[01]0
2355 int val=int(lInput->readULong(2)); // 78|100
2356 if (val)
2357 f << "f0=" << val << ",";
2358 f << Canvas5Structure::getString(unsigned(lInput->readULong(4))) << ","; // TexU
2359 val=int(lInput->readULong(4));
2360 if (val)
2361 f << "TLc" << val << ","; // checkme
2362 lInput->seek(4, librevenge::RVNG_SEEK_CUR);
2363 int N=int(lInput->readULong(4));
2364 f << "N=" << N << ",";
2365 if (N>(item.m_length-28)/4 || N<0 || item.m_length<28+4*N) {
2366 MWAW_DEBUG_MSG(("Canvas5Parser::readTextLinks: can not find the list of block\n"));
2367 f << "###";
2368 N=0;
2369 }
2370 lInput->seek(8, librevenge::RVNG_SEEK_CUR); // junk, flag?
2371 f << "shapes=[";
2372 for (int i=0; i<N; ++i)
2373 f << "S" << lInput->readULong(4) << ",";
2374 f << "],";
2375 if (lInput->tell()!=lPos+item.m_length)
2376 asciiFile.addDelimiter(lInput->tell(),'|');
2377 asciiFile.addPos(item.m_pos);
2378 asciiFile.addNote(f.str().c_str());
2379 }))
2380 return false;
2381 if (!readDefined(*stream, defined, "TxtLink-B"))
2382 return false;
2383
2384 if (!readExtendedHeader(stream, 1, "TxtLink-C", &Canvas5Parser::defDataFunction))
2385 return false;
2386 // find 0000000200000001000000020238fc000238fcc00000000000014440023a58100000002d0000000a010100c50238fcc00000
2387 if (!readIndexMap(stream, "TxtLink-C"))
2388 return false;
2389 return readDefined(*stream, defined, "TxtLink-C");
2390 }
2391
2392 bool Canvas5Parser::readSlides(std::shared_ptr<Canvas5Structure::Stream> stream)
2393 {
2394 if (!stream) {
2395 MWAW_DEBUG_MSG(("Canvas5Parser::readSlides: no stream\n"));
2396 return false;
2397 }
2398 auto input=stream->input();
2399 int const vers=version();
2400 int const headerSize=vers<9 ? 64 : 268;
2401 if (!input || !input->checkPosition(input->tell()+headerSize+4)) {
2402 MWAW_DEBUG_MSG(("Canvas5Parser::readSlides: the zone is too short\n"));
2403 return false;
2404 }
2405
2406 long pos=input->tell();
2407 libmwaw::DebugStream f;
2408 auto &ascFile=stream->ascii();
2409
2410 f << "Entries(Slide):";
2411 int val=int(input->readLong(4)); // 1-3
2412 if (val) f << "f0=" << val << ",";
2413 if (vers>=9) {
2414 val=int(input->readLong(4)); // 0
2415 if (val) f << "f1=" << val << ",";
2416 }
2417 float fDim[4];
2418 for (auto &d : fDim) d=float(readDouble(*stream,vers<9 ? 4 : 8));
2419 f << "dim?=" << MWAWVec2f(fDim[0], fDim[1]) << ",";
2420 MWAWVec2f pageDim=MWAWVec2f(fDim[2], fDim[3]);
2421 f << "page[dim]=" << pageDim << ",";
2422 val=int(input->readULong(4));
2423 switch (val) {
2424 case 0: // full page
2425 break;
2426 case 1: // the page's height is divided by 2, ie. we print on each page TOP: page 2*N+1(reverted), BOTTOM: page 2*N
2427 m_state->m_documentSetup=1;
2428 f << "setup=top/bottom,";
2429 break;
2430 case 2: // greetings pages, page height/width is divided by 2
2431 m_state->m_documentSetup=2;
2432 f << "setup=greetings,";
2433 break;
2434 default:
2435 MWAW_DEBUG_MSG(("Canvas5Parser::readSlides: find unknown setup type\n"));
2436 f << "###setup=" << val << ",";
2437 break;
2438 }
2439 val=int(input->readULong(4));
2440 if (val==2) {
2441 f << "pages[facing],";
2442 m_state->m_facingPages=true;
2443 }
2444 else if (val) {
2445 MWAW_DEBUG_MSG(("Canvas5Parser::readSlides: find unknown facing pages\n"));
2446 f << "###pages[facing]=" << val << ",";
2447 }
2448 for (auto &d : fDim) d=float(readDouble(*stream, vers<9 ? 4 : 8));
2449 f << "margins=" << MWAWBox2f(MWAWVec2f(fDim[0], fDim[1]),MWAWVec2f(fDim[2], fDim[3])) << ",";
2450
2451 // time to set the page dimension
2452 if (pageDim[0]>10 && pageDim[1]>10) { // I find one time 12.75x16.5
2453 // checkme: check the margins ordering
2454 if (fDim[0]>=0)
2455 getPageSpan().setMarginTop((fDim[0]>14 ? fDim[0]-14 : 0)/72);
2456 if (fDim[1]>=0)
2457 getPageSpan().setMarginLeft((fDim[1]>14 ? fDim[1]-14 : 0)/72);
2458 if (fDim[2]>=0)
2459 getPageSpan().setMarginBottom((fDim[2]>10 ? fDim[2]-10 : 0)/72);
2460 if (fDim[3]>=0)
2461 getPageSpan().setMarginRight((fDim[3]>10 ? fDim[3]-10 : 0)/72);
2462 getPageSpan().setFormLength(pageDim[1]/72);
2463 getPageSpan().setFormWidth(pageDim[0]/72);
2464 }
2465
2466 for (int i=0; i<4; ++i) { // g3=0
2467 val=int(input->readLong(2));
2468 if (!val) continue;
2469 f << "g" << i << "=" << val << ",";
2470 }
2471 int N=int(input->readLong(4));
2472 if (N)
2473 f << "N=" << N << ",";
2474 f << "IDs=[";
2475 for (int i=0; i<2; ++i) f << std::hex << input->readULong(4) << std::dec << ",";
2476 f << "],";
2477 if (vers>=9) ascFile.addDelimiter(input->tell(), '|');
2478 ascFile.addPos(pos);
2479 ascFile.addNote(f.str().c_str());
2480 input->seek(pos+headerSize, librevenge::RVNG_SEEK_SET);
2481
2482 pos=input->tell();
2483 long len=input->readLong(4);
2484 if (len<4 || N<0 || len/4<N || (long)((unsigned long)pos+4+(unsigned long)len)<pos+4 || !input->checkPosition(pos+4+len)) {
2485 MWAW_DEBUG_MSG(("Canvas5Parser::readSlides: can not read the Slides length\n"));
2486 ascFile.addPos(pos);
2487 ascFile.addNote("Entries(Bad):###");
2488 return false;
2489 }
2490 f.str("");
2491 f << "Slide-id:";
2492 input->seek(4, librevenge::RVNG_SEEK_CUR);
2493 f << "id=[";
2494 for (int i=0; i<N; ++i) {
2495 m_state->m_slideIds.push_back(int(input->readLong(4)));
2496 f << m_state->m_slideIds.back() << ",";
2497 }
2498 f << "],";
2499 ascFile.addPos(pos);
2500 ascFile.addNote(f.str().c_str());
2501 input->seek(pos+4+len, librevenge::RVNG_SEEK_SET);
2502
2503 if (vers>=9) {
2504 return readArray9(stream, "Slide",
2505 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &what) {
2506 auto lInput=lStream->input();
2507 long lPos=lInput->tell();
2508 long endPos=lPos+item.m_length;
2509 libmwaw::DebugStream lF;
2510 lF << what << "-S" << item.m_id << ":";
2511 auto &asciiFile=lStream->ascii();
2512 int lVal=int(lInput->readLong(4));
2513 if (lVal==0) { // dummy?
2514 asciiFile.addPos(lPos);
2515 asciiFile.addNote(lF.str().c_str());
2516 return;
2517 }
2518 else if (lVal!=1) {
2519 MWAW_DEBUG_MSG(("Canvas5Parser::readSlide: unexpected first value\n"));
2520 lF << "###f0=" << lVal << ",";
2521 asciiFile.addPos(lPos);
2522 asciiFile.addNote(lF.str().c_str());
2523 return;
2524 }
2525 if (item.m_length<380) {
2526 MWAW_DEBUG_MSG(("Canvas5Parser::readSlide: the size of a slide seems too short\n"));
2527 lF << "###";
2528 asciiFile.addPos(lPos);
2529 asciiFile.addNote(lF.str().c_str());
2530 return;
2531 }
2532
2533 if (m_state->m_idToSlide.find(item.m_id)==m_state->m_idToSlide.end())
2534 m_state->m_idToSlide[item.m_id]=Canvas5ParserInternal::Slide();
2535 auto &slide=m_state->m_idToSlide.find(item.m_id)->second;
2536
2537 float fDims[2];
2538 for (auto &d : fDims) d=float(readDouble(*lStream, 8));
2539 slide.m_dim = MWAWVec2f(fDims[0],fDims[1]);
2540 lF << "page[dim]=" << slide.m_dim << ",";
2541 lVal=int(lInput->readLong(4));
2542 if (lVal) // 0,1,6
2543 lF << "f1=" << lVal << ",";
2544 lF << "ID=[";
2545 for (int i=0; i<2; ++i)
2546 lF << std::hex << lInput->readULong(4) << std::dec << ",";
2547 asciiFile.addPos(lPos);
2548 asciiFile.addNote(lF.str().c_str());
2549
2550 lPos=lInput->tell();
2551 lF.str("");
2552 lF << what << "-S" << item.m_id << "[name]:";
2553 std::string text;
2554 for (int i=0; i<256; ++i) {
2555 char c=char(lInput->readULong(1));
2556 if (c==0)
2557 break;
2558 text+=c;
2559 }
2560 lF << text << ",";
2561 asciiFile.addPos(lPos);
2562 asciiFile.addNote(lF.str().c_str());
2563 lInput->seek(lPos+256, librevenge::RVNG_SEEK_SET);
2564
2565 lPos=lInput->tell();
2566 lF.str("");
2567 lF << what << "-S" << item.m_id << "[II]:";
2568 lVal=int(lInput->readLong(4));
2569 if (lVal) lF << "f0=" << lVal << ",";
2570 for (int i=0; i<2; ++i) {
2571 double dVal=readDouble(*lStream, 8);
2572 if (dVal<0 || dVal>0) lF << "unk" << i << "=" << dVal << ",";
2573 }
2574 lF << "num[layer?]=" << lInput->readLong(4) << ",";
2575 slide.m_numLayers=int(lInput->readLong(4));
2576 if (slide.m_numLayers!=1)
2577 lF << "num[layer]=" << slide.m_numLayers << ",";
2578 lF << "unkn=[";
2579 for (int i=0; i<7; ++i) { // firsts is a double, other?
2580 double dVal=readDouble(*lStream, 8);
2581 if (dVal<0 || dVal>0)
2582 lF << dVal << ",";
2583 else
2584 lF << "_,";
2585 }
2586 lF << "],";
2587 asciiFile.addPos(lPos);
2588 asciiFile.addNote(lF.str().c_str());
2589
2590 lPos=lInput->tell();
2591 lF.str("");
2592 lF << what << "-S" << item.m_id << "[layer]:";
2593 long lLen=long(lInput->readULong(4));
2594 if (slide.m_numLayers<0 || lLen<8+8*slide.m_numLayers || (lLen-8)/8<slide.m_numLayers ||
2595 lPos+4+lLen<lPos+20 || lPos+4+lLen>endPos) {
2596 MWAW_DEBUG_MSG(("Canvas5Parser::readSlide: can not find the slide list\n"));
2597 lF << "###";
2598 asciiFile.addPos(lPos);
2599 asciiFile.addNote(lF.str().c_str());
2600 return;
2601 }
2602 lF << "ID=[";
2603 for (int i=0; i<2; ++i)
2604 lF << std::hex << lInput->readULong(4) << std::dec << ",";
2605 lF << "],";
2606 for (int i=0; i<slide.m_numLayers; ++i) {
2607 lF << "[";
2608 slide.m_layers.push_back(int(lInput->readULong(4)));
2609 lF << "L" << slide.m_layers.back() << ",";
2610 lVal=int(lInput->readLong(4));
2611 if (lVal!=5)
2612 lF << "f0=" << lVal << ",";
2613 lF << "]";
2614 }
2615 asciiFile.addPos(lPos);
2616 asciiFile.addNote(lF.str().c_str());
2617 });
2618 }
2619 if (!readExtendedHeader(stream, 0xac, "Slide",
2620 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Canvas5Parser::Item const &item, std::string const &what) {
2621 auto lInput=lStream->input();
2622 long lPos=lInput->tell();
2623 libmwaw::DebugStream lF;
2624 auto &asciiFile=lStream->ascii();
2625
2626 if (m_state->m_idToSlide.find(item.m_id)==m_state->m_idToSlide.end())
2627 m_state->m_idToSlide[item.m_id]=Canvas5ParserInternal::Slide();
2628 auto &slide=m_state->m_idToSlide.find(item.m_id)->second;
2629 int lVal;
2630 for (int i=0; i<4; ++i) { // f1=240|2c0|6c0, f3=0|1
2631 lVal=int(lInput->readLong(2));
2632 if (lVal)
2633 lF << "f" << i << "=" << lVal << ",";
2634 }
2635 float fDims[2];
2636 for (auto &d : fDims) d=float(lInput->readLong(4))/65536.f;
2637 slide.m_dim = MWAWVec2f(fDims[0],fDims[1]);
2638 lF << "page[dim]=" << slide.m_dim << ",";
2639 for (int i=0; i<2; ++i) { // f5=1-13,
2640 lVal=int(lInput->readLong(2));
2641 if (lVal)
2642 lF << "f" << 4+i << "=" << lVal << ",";
2643 }
2644 asciiFile.addDelimiter(lInput->tell(),'|');
2645 lInput->seek(8, librevenge::RVNG_SEEK_CUR);
2646 asciiFile.addPos(item.m_pos);
2647 asciiFile.addNote(lF.str().c_str());
2648
2649 lPos=lInput->tell();
2650 lF.str("");
2651 lF << what << "-E" << item.m_id << "[name]:";
2652 std::string text;
2653 for (int i=0; i<128; ++i) {
2654 char c=char(lInput->readULong(1));
2655 if (c==0)
2656 break;
2657 text+=c;
2658 }
2659 lF << text << ",";
2660 asciiFile.addPos(lPos);
2661 asciiFile.addNote(lF.str().c_str());
2662 lInput->seek(lPos+128, librevenge::RVNG_SEEK_SET);
2663
2664 lPos=lInput->tell();
2665 lF.str("");
2666 lF << what << "-E" << item.m_id << "[A]:";
2667 for (int i=0; i<6; ++i) { // f2=0|260, f4=0-3, f5=0-2
2668 lVal=int(lInput->readLong(2));
2669 if (!lVal) continue;
2670 lF << "f" << i << "=" << lVal << ",";
2671 }
2672 slide.m_numLayers=int(lInput->readLong(4));
2673 if (slide.m_numLayers!=1)
2674 lF << "num[layer]=" << slide.m_numLayers << ",";
2675 asciiFile.addPos(lPos);
2676 asciiFile.addNote(lF.str().c_str());
2677 }))
2678 return false;
2679
2680 if (!readIndexMap(stream, "SlideLa",
2681 [this](std::shared_ptr<Canvas5Structure::Stream> lStream, Item const &item, std::string const &) {
2682 auto lInput=lStream->input();
2683 auto &asciiFile=lStream->ascii();
2684 auto it=m_state->m_idToSlide.find(item.m_id);
2685 if (it==m_state->m_idToSlide.end() || item.m_length<8+8*it->second.m_numLayers) {
2686 MWAW_DEBUG_MSG(("Canvas5Parser::readSlides: can not read the slides index %d\n", item.m_id));
2687 asciiFile.addPos(item.m_pos);
2688 asciiFile.addNote("###");
2689 return;
2690 }
2691 libmwaw::DebugStream lF;
2692 auto &slide=it->second;
2693 lInput->seek(8, librevenge::RVNG_SEEK_CUR); // 0
2694 lF << "layers=[";
2695 for (int i=0; i<slide.m_numLayers; ++i) {
2696 lF << "[";
2697 slide.m_layers.push_back(int(lInput->readULong(4)));
2698 lF << "L" << slide.m_layers.back() << ",";
2699 for (int j=0; j<2; ++j) { // f2 0 | big number
2700 int lVal=int(lInput->readLong(2));
2701 if (lVal) lF << "f" << i+1 << "=" << lVal << ",";
2702 }
2703 lF << "],";
2704 }
2705 lF << "],";
2706 asciiFile.addPos(item.m_pos);
2707 asciiFile.addNote(lF.str().c_str());
2708 }))
2709 return false;
2710
2711 std::vector<bool> defined;
2712 return readDefined(*stream, defined, "Slide");
2713 }
2714
2715 ////////////////////////////////////////////////////////////
2716 // read the header
2717 ////////////////////////////////////////////////////////////
2718 bool Canvas5Parser::checkHeader(MWAWHeader *header, bool strict)
2719 {
2720 MWAWInputStreamPtr input = getInput();
2721 if (!input || !input->hasDataFork() || !input->checkPosition(0x100))
2722 return false;
2723
2724 input->setReadInverted(false);
2725 input->seek(0, librevenge::RVNG_SEEK_SET);
2726 int vers=5;
2727 int byteOrdering=int(input->readLong(1));
2728 if (byteOrdering==0) {
2729 if (input->readLong(1) || input->readLong(4) || input->readLong(4))
2730 return false;
2731 byteOrdering=int(input->readLong(1));
2732 vers=9;
2733 }
2734 switch (byteOrdering) {
2735 case 1:
2736 m_state->m_isWindowsFile=true;
2737 input->setReadInverted(true);
2738 break;
2739 case 2:
2740 break;
2741 default:
2742 return false;
2743 }
2744 if (input->readULong(4)!=0x8000)
2745 return false;
2746
2747 for (int i=0; i<2; ++i) {
2748 long pos=input->tell();
2749 // try to read the ith zone header
2750 int compType=int(input->readULong(4));
2751 if (vers>=9) // OSNOLA check me compType&=0xf
2752 ;
2753 else if (compType<0 || compType>8) {
2754 compType=compType&0xf; // ok assume that this is an type (with a checksum)
2755 if (compType<0 || compType>8)
2756 return false;
2757 if (vers==5)
2758 vers=6;
2759 }
2760
2761 long len=long(input->readULong(4));
2762 if ((i==0 && len<0x800) || len>0x8000)
2763 return false;
2764
2765 long len1=long(input->readULong(4));
2766 if (len1<0 || len1>len+12 || pos+len1+12<=pos+12 || !input->checkPosition(pos+len1+12))
2767 return false;
2768 input->seek(len1, librevenge::RVNG_SEEK_CUR);
2769 if (!strict)
2770 break;
2771 }
2772 setVersion(vers);
2773 if (header)
2774 header->reset(MWAWDocument::MWAW_T_CANVAS, vers, MWAWDocument::MWAW_K_DRAW);
2775
2776 input->seek(vers>=9 ? 15 : 5, librevenge::RVNG_SEEK_SET);
2777 return true;
2778 }
2779
2780 // ------------------------------------------------------------
2781 // mac resource fork
2782 // ------------------------------------------------------------
2783
2784 bool Canvas5Parser::readPnot(Canvas5Structure::Stream &stream, MWAWEntry const &entry)
2785 {
2786 auto input=stream.input();
2787 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
2788 return false;
2789 if (entry.length()<14) {
2790 MWAW_DEBUG_MSG(("Canvas5Parser::readPnot: the zone seems too small\n"));
2791 return false;
2792 }
2793 entry.setParsed(true);
2794 libmwaw::DebugStream f;
2795 auto &ascFile=stream.ascii();
2796 f << "Entries(Pnot)[" << entry.id() << "]:";
2797 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
2798 f << "ID=" << std::hex << input->readULong(4) << std::dec << ",";
2799 int val=int(input->readLong(2));
2800 if (val) f << "f0=" << val << ",";
2801 val=int(input->readULong(4)); // PICT
2802 f << Canvas5Structure::getString(unsigned(val)) << ",";
2803 f << "id=" << input->readULong(2) << ",";
2804 val=int(input->readLong(2));
2805 if (val) f << "f1=" << val << ",";
2806
2807 ascFile.addPos(entry.begin()-4);
2808 ascFile.addNote(f.str().c_str());
2809 return true;
2810 }
2811
2812 bool Canvas5Parser::readPicture(Canvas5Structure::Stream &stream, MWAWEntry const &entry)
2813 {
2814 auto input=stream.input();
2815 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
2816 return false;
2817 if (entry.length()<14) {
2818 MWAW_DEBUG_MSG(("Canvas5Parser::readPicture: the zone seems too small\n"));
2819 return false;
2820 }
2821 entry.setParsed(true);
2822 libmwaw::DebugStream f;
2823 auto &ascFile=stream.ascii();
2824 f << "Entries(RSRCPicture)[" << entry.id() << "]:";
2825 #ifdef DEBUG_WITH_FILES
2826 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2827 // 42 is the size of a v2 empty picture, so probably ok
2828 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, int(entry.length())));
2829 if (!pict && entry.length()!=42)
2830 f << "###";
2831 else
2832 ascFile.skipZone(entry.begin(), entry.end()-1);
2833 MWAWEmbeddedObject obj;
2834 librevenge::RVNGBinaryData file;
2835 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2836 input->readDataBlock(entry.length(), file);
2837 libmwaw::DebugStream s;
2838 s << "PICT-" << entry.id() << ".pct";
2839 libmwaw::Debug::dumpFile(file, s.str().c_str());
2840 #endif
2841 ascFile.addPos(entry.begin()-4);
2842 ascFile.addNote(f.str().c_str());
2843 return true;
2844 }
2845
2846 // ------------------------------------------------------------
2847 // windows resource fork
2848 // ------------------------------------------------------------
2849
2850 ////////////////////////////////////////////////////////////
2851 //
2852 // send data
2853 //
2854 ////////////////////////////////////////////////////////////
2855
2856 bool Canvas5Parser::send(Canvas5ParserInternal::Slide const &slide)
2857 {
2858 auto listener=getGraphicListener();
2859 if (!listener) {
2860 MWAW_DEBUG_MSG(("Canvas5Parser::send[slide]: can not find the listener\n"));
2861 return false;
2862 }
2863 size_t numLayers=slide.m_layers.size();
2864 bool needOpenLayer=numLayers==2 ? (m_state->m_type==1 || slide.m_layers[0]!=1) : numLayers>2;
2865 int layerId=0;
2866 for (auto lId : slide.m_layers) {
2867 if (m_state->m_layerIdInMasterSet.find(lId)!=m_state->m_layerIdInMasterSet.end())
2868 continue; // do not resend layer in id
2869 auto it = m_state->m_idToLayer.find(lId);
2870 if (it==m_state->m_idToLayer.end()) {
2871 MWAW_DEBUG_MSG(("Canvas5Parser::send[slide]: can not find layer %d\n", lId));
2872 continue;
2873 }
2874 auto const &layer=it->second;
2875 if (needOpenLayer) {
2876 if (!layer.m_name.empty())
2877 listener->openLayer(layer.m_name);
2878 else {
2879 std::stringstream s;
2880 s << "Layer" << ++layerId;
2881 listener->openLayer(s.str().c_str());
2882 }
2883 }
2884 send(layer);
2885 if (needOpenLayer)
2886 listener->closeLayer();
2887 }
2888 return true;
2889 }
2890
2891 bool Canvas5Parser::send(Canvas5ParserInternal::Layer const &layer)
2892 {
2893 auto listener=getGraphicListener();
2894 if (!listener) {
2895 MWAW_DEBUG_MSG(("Canvas5Parser::send[layer]: can not find the listener\n"));
2896 return false;
2897 }
2898 for (auto sId : layer.m_shapesId)
2899 m_graphParser->sendShape(sId);
2900 return true;
2901 }
2902
2903 // ------------------------------------------------------------
2904 // low level
2905 // ------------------------------------------------------------
2906 int Canvas5Parser::readInteger(Canvas5Structure::Stream &stream, int fieldSize) const
2907 {
2908 auto input=stream.input();
2909 if (!input || !input->checkPosition(input->tell()+fieldSize)) {
2910 MWAW_DEBUG_MSG(("Canvas5Parser::readInteger: can not find the input\n"));
2911 return 0;
2912 }
2913 switch (fieldSize) {
2914 case 2:
2915 case 4:
2916 return int(input->readLong(fieldSize));
2917 case 8: {
2918 double val;
2919 bool isNan;
2920 if (!readDouble(stream, val, isNan))
2921 return 0;
2922 return int(val);
2923 }
2924 default:
2925 MWAW_DEBUG_MSG(("Canvas5Parser::readInteger: unexpected field size=%d\n",fieldSize));
2926 if (fieldSize>0)
2927 input->seek(fieldSize, librevenge::RVNG_SEEK_CUR);
2928 return 0;
2929 }
2930 }
2931
2932 double Canvas5Parser::readDouble(Canvas5Structure::Stream &stream, int fieldSize) const
2933 {
2934 auto input=stream.input();
2935 long endPos=input ? input->tell()+fieldSize : 0;
2936 if (!input || !input->checkPosition(endPos)) {
2937 MWAW_DEBUG_MSG(("Canvas5Parser::readDouble: can not find the input\n"));
2938 return 0;
2939 }
2940 switch (fieldSize) {
2941 case 4:
2942 return double(input->readLong(4))/65536;
2943 case 8: {
2944 double val;
2945 bool isNan;
2946 if (!readDouble(stream, val, isNan)) {
2947 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2948 return 0;
2949 }
2950 return val;
2951 }
2952 default:
2953 MWAW_DEBUG_MSG(("Canvas5Parser::readDouble: unexpected field size=%d\n",fieldSize));
2954 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2955 return 0;
2956 }
2957 }
2958
2959 bool Canvas5Parser::readDouble(Canvas5Structure::Stream &stream, double &val, bool &isNaN) const
2960 {
2961 auto input=stream.input();
2962 if (!input || !input->checkPosition(input->tell()+8)) {
2963 MWAW_DEBUG_MSG(("Canvas5Parser::readDouble: can not find the input\n"));
2964 return false;
2965 }
2966 if (input->readInverted())
2967 return input->readDoubleReverted8(val, isNaN);
2968 return input->readDouble8(val, isNaN);
2969 }
2970
2971 bool Canvas5Parser::readString(Canvas5Structure::Stream &stream, librevenge::RVNGString &string, int maxSize, bool canBeCString)
2972 {
2973 string.clear();
2974 auto input=stream.input();
2975 if (!input) {
2976 MWAW_DEBUG_MSG(("Canvas5Parser::readString: can not find the input\n"));
2977 return false;
2978 }
2979 bool const isWindows=isWindowsFile();
2980 auto fontConverter=getFontConverter();
2981 int defaultFont=isWindows ? fontConverter->getId("CP1252") : 3;
2982 if (isWindows && canBeCString) {
2983 int n=0;
2984 while (!input->isEnd() && (maxSize<=0 || n<maxSize)) {
2985 char c=char(input->readULong(1));
2986 if (c==0)
2987 break;
2988 ++n;
2989 int unicode = fontConverter->unicode(defaultFont, static_cast<unsigned char>(c));
2990 if (unicode>0)
2991 libmwaw::appendUnicode(uint32_t(unicode), string);
2992 else {
2993 MWAW_DEBUG_MSG(("Canvas5Parser::readString: find unknown unicode for char=%d\n", int(c)));
2994 }
2995 }
2996 return true;
2997 }
2998 int sSz=int(input->readULong(1));
2999 if ((maxSize<=0 || sSz<maxSize) && input->checkPosition(input->tell()+sSz)) {
3000 for (int ch=0; ch<sSz; ++ch) {
3001 char c=char(input->readULong(1));
3002 if (c==0)
3003 break;
3004 int unicode = fontConverter->unicode(defaultFont, static_cast<unsigned char>(c));
3005 if (unicode>0)
3006 libmwaw::appendUnicode(uint32_t(unicode), string);
3007 else {
3008 MWAW_DEBUG_MSG(("Canvas5Parser::readString: find unknown unicode for char=%d\n", int(c)));
3009 }
3010 }
3011 }
3012 else {
3013 MWAW_DEBUG_MSG(("Canvas5Parser::readString: bad size=%d\n", sSz));
3014 return false;
3015 }
3016 return true;
3017 }
3018
3019 bool Canvas5Parser::readDataHeader(Canvas5Structure::Stream &stream, int expectedSize, int &N)
3020 {
3021 auto input=stream.input();
3022 if (!input)
3023 return false;
3024 long pos=input->tell();
3025 if (!input->checkPosition(pos+4))
3026 return false;
3027 int dSize=int(input->readULong(4));
3028 if (!dSize) {
3029 N=0;
3030 return true;
3031 }
3032 if (dSize<0 || dSize!=expectedSize || !input->checkPosition(pos+8))
3033 return false;
3034 N=int(input->readULong(4));
3035 if (N<0 || (input->size()-pos)/long(dSize)<N || pos+8+long(dSize)*N<pos+8 || !input->checkPosition(pos+8+long(dSize)*N))
3036 return false;
3037 return true;
3038 }
3039
3040 bool Canvas5Parser::readExtendedHeader(std::shared_ptr<Canvas5Structure::Stream> stream, int expectedValue, std::string const &what, DataFunction const &func)
3041 {
3042 if (!stream) {
3043 MWAW_DEBUG_MSG(("Canvas5Parser::readExtendedHeader: no stream\n"));
3044 return false;
3045 }
3046 auto input=stream->input();
3047 long pos=input ? input->tell() : 0;
3048 libmwaw::DebugStream f;
3049 auto &ascFile=stream->ascii();
3050 f << what << "-extended:";
3051 if (!input || !input->checkPosition(input->tell()+12) || int(input->readULong(4))!=expectedValue) {
3052 MWAW_DEBUG_MSG(("Canvas5Parser::readExtendedHeader: the size seems bad\n"));
3053 f << "###";
3054 ascFile.addPos(pos);
3055 ascFile.addNote(f.str().c_str());
3056 return false;
3057 }
3058 int f0=int(input->readLong(4));
3059 int N=int(input->readULong(4));
3060 if (N) f << "N=" << N << ",";
3061 if (f0) f << "f0=" << f0 << ",";
3062 if (N<0 || pos+8+N<pos || !input->checkPosition(pos+8+N)) {
3063 f << "###";
3064 ascFile.addPos(pos);
3065 ascFile.addNote(f.str().c_str());
3066 return false;
3067 }
3068 if (expectedValue<=0 || (N%expectedValue)!=0) {
3069 MWAW_DEBUG_MSG(("Canvas5Parser::readExtendedHeader: the data size seems bad\n"));
3070 f << "###";
3071 ascFile.addDelimiter(input->tell(),'|');
3072 ascFile.addPos(pos);
3073 ascFile.addNote(f.str().c_str());
3074 input->seek(pos+12+N, librevenge::RVNG_SEEK_SET);
3075 }
3076 ascFile.addPos(pos);
3077 ascFile.addNote(f.str().c_str());
3078
3079 if (!N) return true;
3080 if (expectedValue==1) {
3081 pos=input->tell();
3082 f.str("");
3083 f<< what << "-E0:";
3084 ascFile.addPos(pos);
3085 ascFile.addNote(f.str().c_str());
3086 Item item;
3087 item.m_id=0;
3088 item.m_pos=pos;
3089 item.m_length=N;
3090 func(stream, item, what);
3091 if (input->tell()!=pos && input->tell()!=pos+N)
3092 ascFile.addDelimiter(input->tell(),'|');
3093 input->seek(pos+N, librevenge::RVNG_SEEK_SET);
3094 return true;
3095 }
3096
3097 N/=expectedValue;
3098 // the first value seems always a buffer (which contains junk data)
3099 ascFile.addPos(input->tell());
3100 ascFile.addNote("_");
3101 input->seek(expectedValue, librevenge::RVNG_SEEK_CUR);
3102 for (int i=1; i<N; ++i) {
3103 pos=input->tell();
3104 f.str("");
3105 f<< what << "-E" << i << ":";
3106 ascFile.addPos(pos);
3107 ascFile.addNote(f.str().c_str());
3108
3109 Item item;
3110 item.m_id=i;
3111 item.m_pos=pos;
3112 item.m_length=expectedValue;
3113 func(stream, item, what);
3114 if (input->tell()!=pos && input->tell()!=pos+expectedValue)
3115 ascFile.addDelimiter(input->tell(),'|');
3116 input->seek(pos+expectedValue, librevenge::RVNG_SEEK_SET);
3117 }
3118 return true;
3119 }
3120
3121 void Canvas5Parser::stringDataFunction(std::shared_ptr<Canvas5Structure::Stream> stream, Canvas5Parser::Item const &item, std::string const &)
3122 {
3123 if (!stream || !stream->input()) {
3124 MWAW_DEBUG_MSG(("Canvas5Parser::stringDataFunction: can not find any input\n"));
3125 return;
3126 }
3127 auto input=stream->input();
3128 auto &ascFile=stream->ascii();
3129 libmwaw::DebugStream f;
3130 for (int i=0; i<int(item.m_length); ++i) {
3131 char c=char(input->readULong(1));
3132 if (c==0)
3133 break;
3134 f << c;
3135 }
3136 ascFile.addPos(item.m_pos);
3137 ascFile.addNote(f.str().c_str());
3138 }
3139
3140 bool Canvas5Parser::readIndexMap(std::shared_ptr<Canvas5Structure::Stream> stream, std::string const &what, DataFunction const &func)
3141 {
3142 if (!stream || !stream->input()) {
3143 MWAW_DEBUG_MSG(("Canvas5Parser::readIndexMap: can not find the input\n"));
3144 return false;
3145 }
3146 auto input=stream->input();
3147 long pos=input->tell();
3148 libmwaw::DebugStream f;
3149 auto &ascFile = stream->ascii();
3150 f << what << "[id]:";
3151
3152 int N;
3153 if (!readDataHeader(*stream, 12,N)) {
3154 MWAW_DEBUG_MSG(("Canvas5Parser::readIndexMap: can not read the header N\n"));
3155 f << "###";
3156 ascFile.addPos(pos);
3157 ascFile.addNote(f.str().c_str());
3158 return false;
3159 }
3160 f << "N=" << N << ",";
3161 ascFile.addPos(pos);
3162 ascFile.addNote(f.str().c_str());
3163
3164 std::map<int, MWAWEntry> idToEntry;
3165 for (int i=1; i<=N; ++i) {
3166 pos=input->tell();
3167 f.str("");
3168 f << what << "-" << i << "[id]:";
3169 MWAWEntry entry;
3170 long len=long(input->readULong(4));
3171 int id=int(input->readULong(4));
3172 if (id==0) {
3173 ascFile.addPos(pos);
3174 ascFile.addNote("_");
3175 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
3176 continue;
3177 }
3178 if (id!=1) f << "id=" << id << ",";
3179 entry.setBegin(long(input->readULong(4)));
3180 entry.setLength(len);
3181 entry.setId(id);
3182 f << std::hex << entry.begin() << "<->" << entry.end() << std::dec << ",";
3183 if (entry.valid())
3184 idToEntry[i]=entry;
3185 ascFile.addPos(pos);
3186 ascFile.addNote(f.str().c_str());
3187 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
3188 }
3189
3190 pos=input->tell();
3191 if (!input->checkPosition(pos+4)) {
3192 MWAW_DEBUG_MSG(("Canvas5Parser::readIndexMap: can not find the number of map\n"));
3193 return false;
3194 }
3195 f.str("");
3196 f << what << "[data]:";
3197 N=int(input->readULong(4));
3198 f << "num=" << N << ",";
3199 ascFile.addPos(pos);
3200 ascFile.addNote(f.str().c_str());
3201 for (int z=1; z<=N; ++z) {
3202 pos=input->tell();
3203 f.str("");
3204 f << what << "[data-" << z << "]:";
3205 int len=int(input->readULong(4));
3206 f << "len=" << len << ",";
3207 long endPos=pos+4+len;
3208 if (len<0 || !input->checkPosition(pos+4+len)) {
3209 MWAW_DEBUG_MSG(("Canvas5Parser::readIndexMap: can not find the length of the map data %d\n", z));
3210 f << "###";
3211 ascFile.addPos(pos);
3212 ascFile.addNote(f.str().c_str());
3213 return false;
3214 }
3215
3216 ascFile.addPos(pos);
3217 ascFile.addNote(f.str().c_str());
3218 if (len==0) continue;
3219 pos+=4;
3220
3221 for (auto const &it : idToEntry) {
3222 auto const &entry=it.second;
3223 if (entry.id()!=z) continue;
3224 if (pos+entry.end()>endPos) {
3225 MWAW_DEBUG_MSG(("Canvas5Parser::readIndexMap: can not find data %d\n", it.first));
3226 continue;
3227 }
3228 ascFile.addPos(pos+entry.end());
3229 ascFile.addNote("_");
3230 f.str("");
3231 f << what << "-Dt" << it.first << ":";
3232 ascFile.addPos(pos+entry.begin());
3233 ascFile.addNote(f.str().c_str());
3234 input->seek(pos+entry.begin(), librevenge::RVNG_SEEK_SET);
3235 Item item;
3236 item.m_pos=pos+entry.begin();
3237 item.m_id=it.first;
3238 item.m_length=entry.length();
3239 func(stream, item, what);
3240 }
3241 input->seek(endPos, librevenge::RVNG_SEEK_SET);
3242 }
3243
3244 return true;
3245 }
3246
3247 bool Canvas5Parser::readDefined(Canvas5Structure::Stream &stream, std::vector<bool> &defined, std::string const &what)
3248 {
3249 auto input=stream.input();
3250 long pos=input ? input->tell() : 0;
3251 libmwaw::DebugStream f;
3252 auto &ascFile=stream.ascii();
3253 f << what << "[def,N]:";
3254 if (!input || !input->checkPosition(pos+16)) {
3255 MWAW_DEBUG_MSG(("Canvas5Parser::readDefined: can not find the input\n"));
3256 f << "###";
3257 ascFile.addPos(pos);
3258 ascFile.addNote(f.str().c_str());
3259 return false;
3260 }
3261 f << "N=["; //
3262 for (int i=0; i<2; ++i) f << input->readLong(4) << ",";
3263 f << "],";
3264 int val=int(input->readLong(4)); // 0
3265 if (val) f << "f0=" << val << ",";
3266 if (input->readLong(4)!=4) {
3267 MWAW_DEBUG_MSG(("Canvas5Parser::readDefined: bad header\n"));
3268 f << "###";
3269 ascFile.addPos(pos);
3270 ascFile.addNote(f.str().c_str());
3271 return false;
3272 }
3273 ascFile.addPos(pos);
3274 ascFile.addNote(f.str().c_str());
3275
3276 pos=input->tell();
3277 f.str("");
3278 f << what << "[def]:";
3279 int N;
3280 if (!readDataHeader(stream, 4,N)) {
3281 MWAW_DEBUG_MSG(("Canvas5Parser::readDefined: can not read N\n"));
3282 f << "###";
3283 ascFile.addPos(pos);
3284 ascFile.addNote(f.str().c_str());
3285 return false;
3286 }
3287 f << "N=" << N << ",";
3288 f << "def=[";
3289 defined.clear();
3290 for (int i=0; i<N; ++i) {
3291 defined.push_back(input->readLong(4)!=0); // 0 or -1
3292 f << (defined.back() ? "*" : "_") << ",";
3293 }
3294 f << "],";
3295 ascFile.addPos(pos);
3296 ascFile.addNote(f.str().c_str());
3297 input->seek(pos+8+4*N, librevenge::RVNG_SEEK_SET);
3298 return true;
3299 }
3300
3301 bool Canvas5Parser::readUsed(Canvas5Structure::Stream &stream, std::string const &what)
3302 {
3303 auto input=stream.input();
3304 long pos=input ? input->tell() : 0;
3305 libmwaw::DebugStream f;
3306 auto &ascFile=stream.ascii();
3307 f << what << "[used,N]:";
3308 if (!input || !input->checkPosition(pos+20) || input->readULong(4)!=4) {
3309 MWAW_DEBUG_MSG(("Canvas5Parser::readUsed: can not find the input\n"));
3310 f << "###";
3311 ascFile.addPos(pos);
3312 ascFile.addNote(f.str().c_str());
3313 return false;
3314 }
3315 f << "N=["; // used, id
3316 for (int i=0; i<2; ++i) f << input->readLong(4) << ",";
3317 f << "],";
3318 for (int i=0; i<2; ++i) {
3319 int val=int(input->readLong(4));
3320 if (val!=(i==1 ? 8 : 0)) f << "f" << i << "=" << val << ",";
3321 }
3322 ascFile.addPos(pos);
3323 ascFile.addNote(f.str().c_str());
3324
3325 pos=input->tell();
3326 f.str("");
3327 f << what << "[used]:";
3328 int N;
3329 if (!readDataHeader(stream,8,N)) {
3330 MWAW_DEBUG_MSG(("Canvas5Parser::readUsed: can not read N\n"));
3331 f << "###";
3332 ascFile.addPos(pos);
3333 ascFile.addNote(f.str().c_str());
3334 return false;
3335 }
3336 f << "N=" << N << ",";
3337 ascFile.addPos(pos);
3338 ascFile.addNote(f.str().c_str());
3339 for (int i=0; i<N; ++i) {
3340 pos=input->tell();
3341 f.str("");
3342 f << what << "-U" << i+1 << ":";
3343 int val=int(input->readLong(4));
3344 if (val!=-1) f << "f0=" << val << ",";
3345 val=int(input->readLong(4));
3346 if (val==0) {
3347 ascFile.addPos(pos);
3348 ascFile.addNote("_");
3349 continue;
3350 }
3351 if (val!=1) f << "used=" << val << ",";
3352 ascFile.addPos(pos);
3353 ascFile.addNote(f.str().c_str());
3354 }
3355 return true;
3356 }
3357
3358 bool Canvas5Parser::readArray9(std::shared_ptr<Canvas5Structure::Stream> stream, std::string const &what,
3359 Canvas5Parser::DataFunction const &func)
3360 {
3361 if (!stream)
3362 return false;
3363 auto input=stream->input();
3364 if (!input)
3365 return false;
3366
3367 auto &ascFile=stream->ascii();
3368 libmwaw::DebugStream f;
3369 f << what << "[header]:";
3370 long pos=input->tell();
3371 if (!checkTAG9(*stream,"ARRAY",0)) {
3372 f << "###";
3373 ascFile.addPos(pos);
3374 ascFile.addNote(f.str().c_str());
3375 return false;
3376 }
3377 pos=input->tell();
3378 if (!input->checkPosition(pos+44)) {
3379 MWAW_DEBUG_MSG(("Canvas5Parser::readArray9: the array zone seems too short\n"));
3380 f << "###";
3381 ascFile.addPos(pos);
3382 ascFile.addNote(f.str().c_str());
3383 return false;
3384 }
3385 std::string name;
3386 for (int i=0; i<32; ++i) {
3387 char c=char(input->readLong(1));
3388 if (!c)
3389 break;
3390 name+=c;
3391 }
3392 f << name << ",";
3393 input->seek(pos+32, librevenge::RVNG_SEEK_SET);
3394 int type=int(input->readLong(4));
3395 int decal=int(input->readLong(4));
3396 if (type==400)
3397 f << "fixed=" << decal << ",";
3398 else
3399 f << "type=" << type << "[" << decal << "],";
3400 int N=int(input->readLong(4));
3401 f << "N=" << N << ",";
3402 ascFile.addPos(pos);
3403 ascFile.addNote(f.str().c_str());
3404
3405 for (int i=0; i<N; ++i) {
3406 pos=input->tell();
3407 f.str("");
3408 f << what << "-" << i << ":";
3409 Canvas5Parser::Item item;
3410 item.m_pos=input->tell();
3411 item.m_decal=decal;
3412 int used;
3413 if (!readItemHeader9(*stream, item.m_id, used)) {
3414 f << "###";
3415 ascFile.addPos(pos);
3416 ascFile.addNote(f.str().c_str());
3417 return false;
3418 }
3419 f << "id=" << item.m_id << ",";
3420 if (used!=1)
3421 f << "used=" << used << ",";
3422 if (used==0) {
3423 ascFile.addPos(pos);
3424 ascFile.addNote(f.str().c_str());
3425 continue;
3426 }
3427
3428 unsigned uVal=unsigned(input->readULong(4));
3429 if (type==200 || type==400) {
3430 item.m_type=uVal;
3431 if (uVal!=1)
3432 f << "type=" << Canvas5Structure::getString(uVal) << ",";
3433 }
3434 else if (uVal)
3435 f << "f0=" << uVal << ",";
3436 long len=-1;
3437 switch (type) {
3438 case 100:
3439 case 200: // ink, the type can be found at position pos+14 ?
3440 case 500: // name
3441 case 600: // image?
3442 if (decal<0 || (long)((unsigned long)pos+18+(unsigned long)decal)<pos+18 || !input->checkPosition(pos+18+decal))
3443 break;
3444 input->seek(pos+14+decal, librevenge::RVNG_SEEK_SET);
3445 len=input->readLong(4);
3446 if (len>=0 && len+18+decal>=len)
3447 len+=18+decal;
3448 else
3449 len=-1;
3450 break;
3451 // 200: len at pos 22 f1=decal?
3452 // 500: name, len at pos 14
3453 // 600: len at pos 14
3454 case 400: // checkme: sometimes the data begin at position 14
3455 len=18+decal;
3456 break;
3457 default:
3458 break;
3459 }
3460 if (len<18 || pos+len<pos+18 || !input->checkPosition(pos+18+len)) {
3461 MWAW_DEBUG_MSG(("Canvas5Parser::readArray9: can not read an item\n"));
3462 f << "###";
3463 ascFile.addPos(pos);
3464 ascFile.addNote(f.str().c_str());
3465 return false;
3466 }
3467 ascFile.addPos(pos);
3468 ascFile.addNote(f.str().c_str());
3469 input->seek(pos+18, librevenge::RVNG_SEEK_SET);
3470 item.m_length=len-18;
3471 func(stream, item, what);
3472 input->seek(pos+len, librevenge::RVNG_SEEK_SET);
3473 }
3474 pos=input->tell();
3475 if (!checkTAG9(*stream,"ARRAY",1)) {
3476 f.str("");
3477 f << what << "-end:###";
3478 ascFile.addPos(pos);
3479 ascFile.addNote(f.str().c_str());
3480 return false;
3481 }
3482 return true;
3483 }
3484
3485 bool Canvas5Parser::readItemHeader9(Canvas5Structure::Stream &stream, int &id, int &used)
3486 {
3487 auto input=stream.input();
3488 if (!input)
3489 return false;
3490
3491 long pos=input->tell();
3492 if (!input->checkPosition(pos+14)) {
3493 MWAW_DEBUG_MSG(("Canvas5Parser::readItemHeader9: the item zone seems too short\n"));
3494 return false;
3495 }
3496 std::string name;
3497 for (int i=0; i<6; ++i) name+=char(input->readLong(1));
3498 if (name!="X+--+X") {
3499 MWAW_DEBUG_MSG(("Canvas5Parser::readItemHeader9: can not read the item header\n"));
3500 return false;
3501 }
3502 id=int(input->readLong(4));
3503 used=int(input->readLong(4));
3504 return true;
3505 }
3506
3507 // ------------------------------------------------------------
3508 // TAG
3509 // ------------------------------------------------------------
3510 bool Canvas5Parser::checkTAG9(Canvas5Structure::Stream &stream, std::string const &tag, int type)
3511 {
3512 if (version()<9)
3513 return true;
3514
3515 auto input=stream.input();
3516 if (!input)
3517 return false;
3518 long pos=input->tell();
3519
3520 std::string fTag;
3521 int fType;
3522 if (!getTAG9(stream, fTag, fType) || fTag!=tag || fType!=type)
3523 return false;
3524 auto &ascFile=stream.ascii();
3525 libmwaw::DebugStream f;
3526 f << "TAG[" << tag << "]";
3527 switch (type) {
3528 case 0:
3529 f << "begin,";
3530 break;
3531 case 1:
3532 f << "end,";
3533 break;
3534 default:
3535 f << "###";
3536 break;
3537 }
3538 ascFile.addPos(pos);
3539 ascFile.addNote(f.str().c_str());
3540 return true;
3541 }
3542
3543 bool Canvas5Parser::getTAG9(Canvas5Structure::Stream &stream, std::string &tag, int &type)
3544 {
3545 auto input=stream.input();
3546 long pos=input->tell();
3547 if (!input->checkPosition(pos+1+3+1+1+1+3+1) || input->readULong(1)!='<') {
3548 input->seek(pos, librevenge::RVNG_SEEK_SET);
3549 return false;
3550 }
3551 bool ok=true;
3552 for (int st=0; st<3; ++st) {
3553 std::string what;
3554 while (true) {
3555 if (input->isEnd()) {
3556 ok=false;
3557 break;
3558 }
3559 char c=char(input->readLong(1));
3560 if ((c=='_'&&st<2) || (c=='>'&&st==2))
3561 break;
3562 if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9'))
3563 what+=c;
3564 else {
3565 ok=false;
3566 break;
3567 }
3568 }
3569 ok=ok && !what.empty();
3570 if (!ok)
3571 break;
3572 switch (st) {
3573 case 0:
3574 if (what=="BEGIN")
3575 type=0;
3576 else if (what=="END")
3577 type=1;
3578 else
3579 ok=1;
3580 break;
3581 case 1:
3582 tag=what;
3583 break;
3584 case 2:
3585 default:
3586 ok=what=="TAG";
3587 break;
3588 }
3589 }
3590
3591 if (!ok)
3592 input->seek(pos, librevenge::RVNG_SEEK_SET);
3593 return ok;
3594 }
3595
3596 // ------------------------------------------------------------
3597 // decode stream
3598 // ------------------------------------------------------------
3599 MWAWInputStreamPtr Canvas5Parser::decode(MWAWInputStreamPtr input, int version)
3600 {
3601 MWAWInputStreamPtr res;
3602 if (!input)
3603 return res;
3604
3605 long pos=version>=9 ? 15 : 5;
3606 if (!input->checkPosition(pos+12)) {
3607 MWAW_DEBUG_MSG(("Canvas5Parser::decode: the input seems too short\n"));
3608 return res;
3609 }
3610
3611 input->seek(0, librevenge::RVNG_SEEK_SET);
3612 unsigned long read;
3613 const unsigned char *dt = input->read((unsigned long)(pos), read);
3614 if (!dt || read != (unsigned long)(pos)) {
3615 MWAW_DEBUG_MSG(("Canvas5Parser::decode: can not read some data\n"));
3616 return res;
3617 }
3618
3619 auto stream=std::make_shared<MWAWStringStream>(dt, unsigned(pos));
3620 while (!input->isEnd()) {
3621 pos=input->tell();
3622 if (!input->checkPosition(pos+12))
3623 break;
3624 int type=int(input->readULong(4));
3625 // v5: compressed type (between 0 and 8)
3626 // v6: if checksum (check=0, for i in data check+=i), Canvas store (checksum)<<4|(compressed type)
3627 if (version>=6) type=(type&0xf);
3628 unsigned long lengths[2];
3629 for (auto &l : lengths) l=input->readULong(4);
3630 long endPos=pos+12+long(lengths[1]);
3631 if (type<0 || type>8 || long(lengths[0])<=0 || long(lengths[0])>0x8000 ||
3632 lengths[0]+12<lengths[1] || long(lengths[1])<0 ||
3633 endPos<pos+12 || !input->checkPosition(endPos)) {
3634 input->seek(pos, librevenge::RVNG_SEEK_SET);
3635 break;
3636 }
3637 // checkme: v5 I only see type=0|7|8, v6 I only see type=0|8
3638 if (!Canvas5Structure::decodeZone5(input, endPos, type, lengths[0], stream)) {
3639 MWAW_DEBUG_MSG(("Canvas5Parser::decode: problem with type=%d at position=%lx\n", type, (unsigned long)pos));
3640 input->seek(pos, librevenge::RVNG_SEEK_SET);
3641 return res;
3642 }
3643 input->seek(endPos, librevenge::RVNG_SEEK_SET);
3644 #ifdef DEBUG
3645 if (lengths[0]!=0x8000) {
3646 stream->seek(0, librevenge::RVNG_SEEK_END);
3647 std::cout << "\t" << std::hex << stream->tell() << std::dec << "\n";
3648 }
3649 #endif
3650 }
3651 if (!input->isEnd()) { // last zone is not compressed
3652 MWAW_DEBUG_MSG(("Canvas5Parser::decode: stop at pos=%lx->%lx\n", (unsigned long) input->tell(),
3653 (unsigned long) stream->tell()));
3654 unsigned long remain=(unsigned long)(input->size()-input->tell());
3655 dt = input->read(remain, read);
3656 if (!dt || read != remain) {
3657 MWAW_DEBUG_MSG(("Canvas5Parser::decode: can not read some data\n"));
3658 return res;
3659 }
3660 stream->append(dt, unsigned(remain));
3661 }
3662
3663 res.reset(new MWAWInputStream(stream, false));
3664 res->seek(0, librevenge::RVNG_SEEK_SET);
3665 res->setReadInverted(input->readInverted());
3666 return res;
3667 }
3668
3669 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: