Debian Codegraph @

src/lib/CanvasParser.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 <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <set>
39 #include <sstream>
40 #include <stack>
41 #include <utility>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWGraphicListener.hxx"
47 #include "MWAWGraphicStyle.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWPosition.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWRSRCParser.hxx"
52 #include "MWAWStringStream.hxx"
53
54 #include "CanvasGraph.hxx"
55 #include "CanvasStyleManager.hxx"
56
57 #include "CanvasParser.hxx"
58
59 /** Internal: the structures of a CanvasParser */
60 namespace CanvasParserInternal
61 {
62 //! Internal: the layer of a CanvasParser
63 struct Layer {
64 //! constructor
65 Layer()
66 : m_name()
67 , m_numShapes()
68 , m_shapesId()
69 {
70 }
71 //! the layer name
72 librevenge::RVNGString m_name;
73 //! the number of shape
74 int m_numShapes;
75 //! the shape id
76 std::vector<int> m_shapesId;
77 };
78
79 ////////////////////////////////////////
80 //! Internal and low level: the decoder of a canvas file
81 struct Decoder {
82 //! constructor
83 Decoder()
84 : m_version(2)
85 , m_isWindows(false)
86 , m_input()
87 , m_inputPos(0)
88 , m_stream()
89 {
90 }
91 //! first function to init the output (and copy the first headerSize characters)
92 bool initOutput(MWAWInputStreamPtr &input, unsigned long const headerSize=0x89c);
93 //! returns true if the input is completly decoded
94 bool isEnd() const
95 {
96 return m_inputPos>=m_input->size();
97 }
98 //! try to read the following sz bytes and append them to the output
99 bool append(long length);
100 //! try to decode a part of the input
101 bool decode(long length=-1);
102 //! try to decode a part of the input: v3
103 bool decode3(long length);
104 //! try to unpack some bits from buffer into buffer2, assuming buffer and buffer2 differs
105 bool unpackBits(unsigned char const(&buffer)[256], int n,
106 unsigned char (&buffer2)[256], int &n2) const;
107
108 //! the file version
109 int m_version;
110 //! a flag to know if the file is a windows file
111 bool m_isWindows;
112 //! the initial input
113 MWAWInputStreamPtr m_input;
114 //! the input current position
115 long m_inputPos;
116 //! the current stream
117 std::shared_ptr<MWAWStringStream> m_stream;
118 };
119
120 bool Decoder::unpackBits(unsigned char const(&buffer)[256], int n,
121 unsigned char (&buffer2)[256], int &n2) const
122 {
123 if (n<=0 || n>256 || &buffer == &buffer2) {
124 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::unpackBits: bad arguments\n"));
125 return false;
126 }
127 int r=0;
128 n2=0;
129 // canvas only packs zone with less than 127 characters
130 // => we must not found <M> M+1 bits <N> N+1 bits
131 bool lastCopy=false;
132 while (r+1<n) {
133 int c=buffer[r++];
134 if (c>=0x81) {
135 unsigned char val=buffer[r++];
136 int num=0x101-c;
137 if (n2+num>256)
138 return false;
139 for (int i=0; i<num; ++i)
140 buffer2[n2++]=val;
141 lastCopy=false;
142 }
143 else {
144 // normally c==0x80 is reserved, but must not be used
145 if (lastCopy && !m_isWindows)
146 return false;
147 int num=c+1;
148 if (r+num>n || n2+num>256)
149 return false;
150 for (int i=0; i<num; ++i)
151 buffer2[n2++]=buffer[r++];
152 lastCopy=true;
153 }
154 }
155 return r==n;
156 }
157
158 bool Decoder::initOutput(MWAWInputStreamPtr &input, unsigned long const headerSize)
159 {
160 m_input=input;
161 if (!m_input || !m_input->checkPosition(long(headerSize)+20)) {
162 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::initOutput: can not find the input\n"));
163 return false;
164 }
165
166 m_input->seek(0, librevenge::RVNG_SEEK_SET);
167 unsigned long read;
168 const unsigned char *dt = m_input->read(headerSize, read);
169 if (!dt || read != headerSize) {
170 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::initOutput: can not read some data\n"));
171 return false;
172 }
173 m_stream.reset(new MWAWStringStream(dt, unsigned(headerSize)));
174 m_inputPos=long(headerSize);
175 return true;
176 }
177
178 bool Decoder::append(long length)
179 {
180 if (length==0)
181 return true;
182 if (!m_input || !m_stream || length<0 || !m_input->checkPosition(m_input->tell()+length)) {
183 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::append: the zone seems too short\n"));
184 return false;
185 }
186 auto actIPos=m_input->tell();
187 auto actOPos=m_stream->tell();
188 m_input->seek(m_inputPos, librevenge::RVNG_SEEK_SET);
189 m_stream->seek(0, librevenge::RVNG_SEEK_END);
190
191 unsigned long read;
192 const unsigned char *dt = m_input->read((unsigned long)length, read);
193 bool ok=true;
194 if (!dt || read != (unsigned long)length) {
195 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::append: can not read some data\n"));
196 ok=false;
197 }
198 if (ok) {
199 m_stream->append(dt, unsigned(length));
200 m_inputPos=m_input->tell();
201 }
202
203 m_input->seek(actIPos, librevenge::RVNG_SEEK_SET);
204 m_stream->seek(actOPos, librevenge::RVNG_SEEK_SET);
205 return ok;
206 }
207
208 bool Decoder::decode(long length)
209 {
210 if (!m_input || !m_stream) {
211 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode: can not find the input/output\n"));
212 return false;
213 }
214 auto actIPos=m_input->tell();
215 auto actOPos=m_stream->tell();
216 m_input->seek(m_inputPos, librevenge::RVNG_SEEK_SET);
217 m_stream->seek(0, librevenge::RVNG_SEEK_END);
218
219 long lastPos=m_input->size();
220 bool ok=true;
221 if (m_inputPos>=lastPos)
222 ok=false;
223 if (m_version<=2) {
224 long nWrite=0;
225 unsigned char data[256], data2[256];
226 while (ok && !m_input->isEnd()) {
227 if (length>=0 && nWrite>=length)
228 break;
229 long pos=m_input->tell();
230 int zSz=int(m_input->readULong(1));
231 long endPos=pos+zSz;
232 if (zSz==0 || endPos>lastPos) {
233 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode: can not read some data zSz=%d, pos=%lx\n", zSz, (unsigned long) pos));
234 ok=false;
235 break;
236 }
237 for (int i=0; i<zSz; ++i) data[i]=(unsigned char)(m_input->readULong(1));
238 int n;
239 if (!unpackBits(data, zSz, data2, n)) {
240 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode: can not read some data at %lx\n", (unsigned long) pos));
241 ok=false;
242 break;
243 }
244 m_stream->append(data2, static_cast<unsigned int>(n));
245 nWrite+=n;
246 }
247 if (ok && length>=0 && nWrite!=length) {
248 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode: can not decode some data\n"));
249 ok=false;
250 }
251 }
252 else if (ok)
253 ok=decode3(length);
254
255 if (ok)
256 m_inputPos=m_input->tell();
257
258 m_input->seek(actIPos, librevenge::RVNG_SEEK_SET);
259 m_stream->seek(actOPos, librevenge::RVNG_SEEK_SET);
260 return ok;
261 }
262
263 #ifdef DEBUG_WITH_FILES
264 // flag to debug/or not the uncompress function
265 static bool s_showData=false;
266 #endif
267 bool Decoder::decode3(long length)
268 {
269 if (!m_input || !m_stream) {
270 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode3: can not find the input/output\n"));
271 return false;
272 }
273 long lastPos=m_input->size();
274 long numWrite=0;
275
276 int const maxFinalSize=120;
277 unsigned char data[256], data2[256];
278 bool forceDict=false;
279
280 unsigned char m_dict[30];
281 std::set<unsigned char> m_dictKeys;
282 bool m_isDictInitialized=false;
283 long lastDictPos=0;
284 // a zone is stored:
285 // - either as a list of [length] packbits [checksum]
286 // - or as a dictionary (30 keys) and a list of [length] bytes where bytes can be:
287 // . packbits [checksum] as before
288 // . or compressed with dictionary of (packbits [checksum])
289 // I supposed that the dictionary is only created if the zone's length is greated than a constant (to be verified).
290 // There remains also the problem to know if (packbits [checksum]) has been compressed with the dictionary or not ;
291 // currently, I test if I can decode these sub zones with the dictionary, ...
292 while (m_input->tell()<lastPos) {
293 if (length>=0 && numWrite>=length)
294 return numWrite==length;
295
296 long pos=m_input->tell();
297 int zSz=int(m_input->readULong(1));
298
299 // FIXME: find a method to detect if the zone begins with a dictionary, maybe length>some constant
300 if ((length<0 || numWrite==0) && lastDictPos+30!=pos && (zSz<2 || zSz>maxFinalSize+3 || forceDict)) {
301 if (pos+30>lastPos) {
302 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode3: can not read a dictionary at pos=%lx\n", (unsigned long) pos));
303 return false;
304 }
305 // create the dictionary
306 lastDictPos=pos;
307 m_dict[0]=(unsigned char)(zSz);
308 for (int i=1; i<30; ++i)
309 m_dict[i]=(unsigned char)m_input->readULong(1);
310 m_dictKeys.clear();
311 for (auto &c : m_dict) m_dictKeys.insert(c);
312 m_isDictInitialized=true;
313 forceDict=false;
314 continue;
315 }
316 else if (forceDict) {
317 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode3: can not place a dictionary at pos=%lx\n", (unsigned long) pos));
318 return false;
319 }
320
321 long endPos=pos+1+zSz;
322 if (endPos>lastPos) {
323 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode3: force a dictionary in pos=%lx\n", (unsigned long) pos));
324 forceDict=true;
325 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
326 continue;
327 }
328 // FIXME: find a method if the data are compressed or not
329 int const lastChecksumSz=m_isWindows ? 1 : 0;
330 for (int step=0; step<3; ++step) {
331 if (step==2) {
332 MWAW_DEBUG_MSG(("CanvasParserInternal::Decoder::decode3: force a dictionary in pos=%lx\n", (unsigned long) pos));
333 forceDict=true;
334 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
335 break;
336 }
337 m_input->seek(pos+1, librevenge::RVNG_SEEK_SET);
338 int numChar=int(m_input->readULong(1));
339 #ifdef DEBUG_WITH_FILES
340 int nChar=numChar;
341 #endif
342 if (step==0) {
343 if (!m_isDictInitialized || zSz>numChar || numChar>2*zSz || numChar>maxFinalSize+2+lastChecksumSz) continue;
344 // try to decode with the dictionary has been used to pack the data
345 bool ok=true;
346 int w=0;
347 unsigned char c;
348 bool readC=false;
349 while (m_input->tell()<=endPos && w<numChar) {
350 int newC=0;
351 for (int st=0; st<4; ++st) {
352 int val;
353 if (!readC) {
354 if (m_input->tell()>endPos) {
355 ok=false;
356 break;
357 }
358 c=(unsigned char) m_input->readULong(1);
359 val=int(c>>4);
360 }
361 else
362 val=int(c&0xf);
363 readC=!readC;
364
365 if (val && st<2) {
366 data[w++]=m_dict[15*st+val-1];
367 break;
368 }
369 newC=(newC<<4)|val;
370 if (st==3) {
371 if (m_dictKeys.find((unsigned char) newC)!=m_dictKeys.end()) {
372 ok=false;
373 break;
374 }
375 data[w++]=(unsigned char) newC;
376 }
377 }
378 if (ok==false)
379 break;
380 }
381 if (ok==false || w!=numChar || m_input->tell()<endPos)
382 continue;
383 }
384 else {
385 // basic copy
386 // checkme: on mac, the first bytes is always ignored when numChar+1==zSz ;
387 // but only sometimes on windows :-~
388 if (numChar+1!=zSz) {
389 m_input->seek(-1, librevenge::RVNG_SEEK_CUR);
390 numChar=zSz;
391 }
392 for (int i=0; i<numChar; ++i)
393 data[i]=(unsigned char) m_input->readULong(1);
394 }
395
396 // first check the checksum
397 int checkSum;
398 bool ok=false;
399 for (int step2=0; step2<2; ++step2) {
400 if (step2==1) {
401 if (!m_isWindows || step!=1 || numChar+1!=zSz)
402 break;
403 m_input->seek(-zSz, librevenge::RVNG_SEEK_CUR);
404 numChar=zSz;
405 for (int i=0; i<numChar; ++i)
406 data[i]=(unsigned char) m_input->readULong(1);
407 }
408 checkSum=0;
409 for (int i=0; i<numChar-1; ++i)
410 checkSum+=int(data[i]);
411 if (numChar==0 || (checkSum&0xff)!=int(data[numChar-1]))
412 continue;
413 ok=true;
414 break;
415 }
416 if (!ok) continue;
417
418 --numChar;
419 #ifdef DEBUG_WITH_FILES
420 if (s_showData) {
421 std::cout << zSz << "[" << nChar << "," << numChar << "]:";
422 auto prev=std::cout.fill('0');
423 for (int i=0; i < numChar; ++i)
424 std::cout << std::hex << std::setw(2) << int(data[i]) << std::dec;
425 std::cout.fill(prev);
426 std::cout << "\n";
427 }
428 #endif
429 // then check if we can unpack the data
430 int finalN;
431 if (!unpackBits(data, numChar, data2, finalN) || finalN>maxFinalSize+lastChecksumSz || (m_isWindows && finalN<=numChar) ||
432 (length>=0 && numWrite+finalN>length+lastChecksumSz) || finalN<1+lastChecksumSz) {
433 if (m_isWindows && (length<0 || numWrite+numChar<=length+lastChecksumSz) &&
434 numChar<=maxFinalSize+lastChecksumSz && numChar>=1+lastChecksumSz) {
435 for (int i=0; i < numChar; ++i)
436 data2[i]=data[i];
437 finalN=numChar;
438 }
439 else
440 continue;
441 }
442 #ifdef DEBUG_WITH_FILES
443 if (s_showData) {
444 std::cout << "\t" << finalN << ":";
445 auto prev=std::cout.fill('0');
446 for (int i=0; i < finalN; ++i)
447 std::cout << std::hex << std::setw(2) << int(data2[i]) << std::dec;
448 std::cout.fill(prev);
449 std::cout << "\n";
450 }
451 #endif
452 if (lastChecksumSz==1) {
453 checkSum=0;
454 for (int i=0; i<finalN-1; ++i)
455 checkSum+=int(data2[i]);
456 if ((checkSum&0xff)!=int(data2[finalN-1]))
457 continue;
458 --finalN;
459 }
460 m_stream->append(data2, static_cast<unsigned int>(finalN));
461 numWrite+=finalN;
462 break;
463 }
464 }
465 return length<0 || numWrite==length;
466 }
467 ////////////////////////////////////////
468 //! Internal: the state of a CanvasParser
469 struct State {
470 //! constructor
471 State()
472 : m_isWindowsFile(false)
473 , m_lengths()
474 , m_brushLengths()
475 , m_bitmapSize(0)
476 , m_input()
477 , m_decoder()
478 , m_numLayers(1)
479 , m_numShapes(0)
480 , m_numViews(0)
481 , m_numColors(256) // FIXME: check if this number is stored in the file or not
482 , m_numPatterns(120)
483 , m_sprayLengths()
484
485 , m_numPages(1,1)
486 , m_pageDimension(425, 624)
487 , m_layers()
488
489 , m_metaData()
490 {
491 }
492
493 //! true if this is a windows file
494 bool m_isWindowsFile;
495 //! the file header first 4+1 lengths
496 std::vector<unsigned long> m_lengths;
497 //! the brush lengths
498 std::vector<unsigned long> m_brushLengths;
499 //! the file bitmap size (Windows v3)
500 long m_bitmapSize;
501 //! the uncompressed input
502 MWAWInputStreamPtr m_input;
503 //! the main decoder
504 Decoder m_decoder;
505 //! the number of layer
506 int m_numLayers;
507 //! the number of shapes
508 int m_numShapes;
509 //! the number of views
510 int m_numViews;
511 //! the number of colors
512 int m_numColors;
513 //! the number of patterns
514 int m_numPatterns;
515 //! the list of spray size
516 std::vector<unsigned long> m_sprayLengths;
517
518 //! the number of pages
519 MWAWVec2i m_numPages;
520 //! the page dimension
521 MWAWVec2i m_pageDimension;
522 //! the layer
523 std::vector<Layer> m_layers;
524
525 //! the meta data
526 librevenge::RVNGPropertyList m_metaData;
527 };
528
529 }
530
531 ////////////////////////////////////////////////////////////
532 // constructor/destructor, ...
533 ////////////////////////////////////////////////////////////
534 CanvasParser::CanvasParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
535 : MWAWGraphicParser(input, rsrcParser, header)
536 , m_state()
537 , m_graphParser()
538 , m_styleManager()
539 {
540 resetGraphicListener();
541 setAsciiName("main-1");
542
543 m_state.reset(new CanvasParserInternal::State);
544
545 m_styleManager.reset(new CanvasStyleManager(*this));
546 m_graphParser.reset(new CanvasGraph(*this));
547
548 getPageSpan().setMargins(0.1);
549 }
550
551 CanvasParser::~CanvasParser()
552 {
553 }
554
555 MWAWInputStreamPtr &CanvasParser::getInput()
556 {
557 if (m_state->m_input)
558 return m_state->m_input;
559 return MWAWGraphicParser::getInput();
560 }
561
562 bool CanvasParser::decode(long length)
563 {
564 long prevSize=m_state->m_input ? m_state->m_input->size() : 0;
565 if (!m_state->m_input || !m_state->m_decoder.decode(length)) {
566 if (m_state->m_decoder.m_stream)
567 m_state->m_decoder.m_stream->resize((unsigned long)(prevSize));
568 return false;
569 }
570 m_state->m_input->recomputeStreamSize();
571 return true;
572 }
573
574 bool CanvasParser::isWindowsFile() const
575 {
576 return m_state->m_isWindowsFile;
577 }
578
579 ////////////////////////////////////////////////////////////
580 // the parser
581 ////////////////////////////////////////////////////////////
582 void CanvasParser::parse(librevenge::RVNGDrawingInterface *docInterface)
583 {
584 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
585 bool ok = false;
586 try {
587 checkHeader(nullptr);
588 ascii().setStream(getInput());
589 ascii().open(asciiName());
590 if (!readFileHeader())
591 throw libmwaw::ParseException();
592
593 bool const isWindows=isWindowsFile();
594 m_state->m_decoder.m_isWindows=isWindows;
595 m_state->m_decoder.m_version=version();
596 if (!m_state->m_decoder.initOutput(getInput(), isWindows ? (unsigned long)(0x920+m_state->m_bitmapSize) : 0x89c) || !m_state->m_decoder.m_stream)
597 throw libmwaw::ParseException();
598 m_state->m_input.reset(new MWAWInputStream(m_state->m_decoder.m_stream, isWindows));
599
600 // update the style manager and the graph parser and the asciiFile input
601 m_styleManager->setInput(m_state->m_input);
602 m_graphParser->setInput(m_state->m_input);
603 ascii().setStream(m_state->m_input);
604
605 ok = createZones();
606 if (ok) {
607 createDocument(docInterface);
608 for (auto const &layer : m_state->m_layers)
609 send(layer);
610 m_graphParser->checkUnsent();
611 }
612 }
613 catch (...) {
614 MWAW_DEBUG_MSG(("CanvasParser::parse: exception catched when parsing\n"));
615 ok = false;
616 }
617
618 ascii().reset();
619 resetGraphicListener();
620 if (!ok) throw(libmwaw::ParseException());
621 }
622
623 ////////////////////////////////////////////////////////////
624 // create the document
625 ////////////////////////////////////////////////////////////
626 void CanvasParser::createDocument(librevenge::RVNGDrawingInterface *documentInterface)
627 {
628 if (!documentInterface) return;
629 if (getGraphicListener()) {
630 MWAW_DEBUG_MSG(("CanvasParser::createDocument: listener already exist\n"));
631 return;
632 }
633
634 // create the page list
635 MWAWPageSpan ps(getPageSpan());
636 if (m_state->m_numPages!=MWAWVec2i(1,1)) {
637 ps.setFormWidth(double(m_state->m_numPages[0])*ps.getFormWidth());
638 ps.setFormLength(double(m_state->m_numPages[1])*ps.getFormLength());
639 }
640 ps.setPageSpan(1);
641 std::vector<MWAWPageSpan> pageList(1,ps);
642 MWAWGraphicListenerPtr listen(new MWAWGraphicListener(*getParserState(), pageList, documentInterface));
643 setGraphicListener(listen);
644
645 if (!m_state->m_metaData.empty())
646 listen->setDocumentMetaData(m_state->m_metaData);
647 listen->startDocument();
648 }
649
650 ////////////////////////////////////////////////////////////
651 //
652 // Intermediate level
653 //
654 ////////////////////////////////////////////////////////////
655 bool CanvasParser::createZones()
656 {
657 auto input=getInput();
658 if (!input) return false;
659
660 MWAWRSRCParserPtr rsrcParser = getRSRCParser();
661 if (rsrcParser) {
662 auto rsrcInput=rsrcParser->getInput();
663 auto &rsrcAscii=rsrcParser->ascii();
664 auto const &entryMap = rsrcParser->getEntriesMap();
665
666 for (int w=0; w<5; ++w) {
667 static char const *wh[]= {"HeAd" /*11164*/, "Jinf" /* 10568 */, "WIND" /* 10568 */, "LPol" /* 2,4,... */, "USER" /*0*/};
668 auto it = entryMap.lower_bound(wh[w]);
669 while (it != entryMap.end() && it->first==wh[w]) {
670 auto const &entry=it++->second;
671 if (!entry.valid()) continue;
672 switch (w) {
673 case 0:
674 readRSRCFileHeader(rsrcInput, entry, rsrcAscii);
675 break;
676 case 1:
677 readPrintInfo(rsrcInput, entry, rsrcAscii);
678 break;
679 case 2:
680 readWindows(rsrcInput, entry, rsrcAscii);
681 break;
682 case 3:
683 readLPOL(rsrcInput, entry, rsrcAscii);
684 break;
685 case 4:
686 default:
687 readUsers(rsrcInput, entry, rsrcAscii);
688 break;
689 }
690 }
691 }
692 }
693
694 bool const isWindows=m_state->m_isWindowsFile;
695 input->seek(0x3c, librevenge::RVNG_SEEK_SET);
696 if (isWindows) {
697 if (!m_graphParser->readFileBitmap(m_state->m_bitmapSize) || !input->checkPosition(input->tell()+132))
698 return false;
699 long pos=input->tell();
700 libmwaw::DebugStream f;
701 f << "Entries(Brush):lengths=[";
702 for (int i=0; i<32; ++i) {
703 auto length=input->readULong(4);
704 m_state->m_brushLengths.push_back(length);
705 f << length << ",";
706 }
707 f << "],";
708 f << "f0=" << input->readULong(4) << ","; // small number
709 ascii().addPos(pos);
710 ascii().addNote(f.str().c_str());
711 }
712 if (!readUnknownZoneHeader() || !m_styleManager->readPenSize() || !readDocumentHeader() || !readMacroNames() ||
713 !readUnknownZone0() || !m_styleManager->readArrows() || !readFormats() || !readGrids() || !readUnknownZone1())
714 return false;
715
716 if (!m_graphParser->readShapes(m_state->m_numShapes, m_state->m_lengths[0], m_state->m_lengths[1]))
717 return false;
718
719 if (!readLayers() || !readViews() || !m_styleManager->readPatterns(m_state->m_numPatterns))
720 return false;
721
722 if (!m_styleManager->readColors(m_state->m_numColors))
723 return true;
724 if (!readUnknownZone2() || !readBrushes() || !readUnknownZone3())
725 return true;
726 if (!readSprays() || !readUnknownZone4())
727 return true;
728
729 // end of v2
730 if (m_state->m_decoder.isEnd())
731 return true;
732
733 if (!m_styleManager->readDashes(6) || !readEndV3())
734 return true;
735
736 if (isWindows && !readRSRCWindowsFile())
737 return true;
738
739 if (m_state->m_decoder.isEnd())
740 return true;
741
742 decode(-1);
743 MWAW_DEBUG_MSG(("CanvasParser::createZones: unexpected last zone size\n"));
744 ascii().addPos(input->tell());
745 ascii().addNote("Entries(Last):###");
746
747 return true;
748 }
749
750 bool CanvasParser::readLayers()
751 {
752 if (long(m_state->m_lengths[2])<0 || !decode(long(m_state->m_lengths[2]))) {
753 MWAW_DEBUG_MSG(("CanvasParser::readLayers: can not decode the input\n"));
754 return false;
755 }
756 MWAWInputStreamPtr input = getInput();
757 long pos=input->tell();
758 long endPos=pos+long(m_state->m_lengths[2]);
759 if (!input->checkPosition(endPos) || m_state->m_numLayers < 0 ||
760 long(m_state->m_lengths[2])/42 < m_state->m_numLayers) {
761 MWAW_DEBUG_MSG(("CanvasParser::readLayers: zone seems too short\n"));
762 return false;
763 }
764 libmwaw::DebugStream f;
765 f << "Entries(Layer):";
766 ascii().addPos(pos);
767 ascii().addNote(f.str().c_str());
768
769 std::vector<unsigned long> dataSize;
770 m_state->m_layers.resize(size_t(m_state->m_numLayers));
771 for (size_t i=0; i<size_t(m_state->m_numLayers); ++i) {
772 pos=input->tell();
773 f.str("");
774 f << "Layer-" << i << ":";
775 CanvasParserInternal::Layer &layer=m_state->m_layers[i];
776 dataSize.push_back(input->readULong(4));
777 f << "dSz=" << dataSize.back() << ",";
778 layer.m_numShapes=int(input->readULong(2));
779 f << "n[shapes]=" << layer.m_numShapes << ",";
780 ascii().addDelimiter(input->tell(),'|');
781 input->seek(pos+22, librevenge::RVNG_SEEK_SET);
782 ascii().addDelimiter(input->tell(),'|');
783 if (readString(layer.m_name, 20))
784 f << layer.m_name.cstr() << ",";
785 else {
786 f << "###name,";
787 MWAW_DEBUG_MSG(("CanvasParser::readLayers: bad name\n"));
788 }
789 input->seek(pos+42, librevenge::RVNG_SEEK_SET);
790 ascii().addPos(pos);
791 ascii().addNote(f.str().c_str());
792 }
793 if (input->tell()!=endPos) {
794 ascii().addPos(input->tell());
795 ascii().addNote("Layer-End:");
796 input->seek(endPos, librevenge::RVNG_SEEK_SET);
797 }
798
799 for (size_t i=0; i<size_t(m_state->m_numLayers); ++i) {
800 if (!dataSize[i]) continue;
801 if (long(dataSize[i])<0 || !decode(long(dataSize[i]))) {
802 MWAW_DEBUG_MSG(("CanvasParser::readLayers: can not decode the data %d input\n", int(i)));
803 return false;
804 }
805 pos=input->tell();
806 f.str("");
807 f << "Layer-data" << i << ":";
808 auto &layer=m_state->m_layers[i];
809 if (!input->checkPosition(pos+long(dataSize[size_t(i)]))) {
810 MWAW_DEBUG_MSG(("CanvasParser::readLayers: can not find data %d\n", int(i)));
811 f << "###";
812 ascii().addPos(pos);
813 ascii().addNote(f.str().c_str());
814 return false;
815 }
816 if (long(dataSize[size_t(i)])<2*layer.m_numShapes) {
817 MWAW_DEBUG_MSG(("CanvasParser::readLayers: the size seems too short\n"));
818 f << "###";
819 }
820 else {
821 if (layer.m_numShapes)
822 f << "f0=" << std::hex << input->readULong(2) << std::dec << ",";
823 f << "ids=[";
824 for (int j=1; j<layer.m_numShapes; ++j) {
825 layer.m_shapesId.push_back(int(input->readULong(2)));
826 f << layer.m_shapesId.back() << ",";
827 }
828 f << "],";
829 if (long(dataSize[size_t(i)])!=2*layer.m_numShapes)
830 ascii().addDelimiter(input->tell(),'|');
831 }
832 input->seek(pos+long(dataSize[size_t(i)]), librevenge::RVNG_SEEK_SET);
833 ascii().addPos(pos);
834 ascii().addNote(f.str().c_str());
835 }
836
837 return true;
838 }
839
840 ////////////////////////////////////////////////////////////
841 // read the header
842 ////////////////////////////////////////////////////////////
843 bool CanvasParser::checkHeader(MWAWHeader *header, bool strict)
844 {
845 MWAWInputStreamPtr input = getInput();
846 if (!input || !input->hasDataFork() || !input->checkPosition(0x89e))
847 return false;
848
849 input->setReadInverted(false);
850 input->seek(0x36, librevenge::RVNG_SEEK_SET);
851 int val=int(input->readULong(2));
852 int vers=0;
853 switch (val) {
854 case 1:
855 vers=2;
856 break;
857 case 2:
858 vers=3;
859 break;
860 case 0x100:
861 input->setReadInverted(true);
862 m_state->m_isWindowsFile=true;
863 vers=3;
864 break;
865 default:
866 MWAW_DEBUG_MSG(("CanvasParser::checkHeader: unknown version=%d\n", val));
867 return false;
868 }
869
870 input->seek(0, librevenge::RVNG_SEEK_SET);
871 unsigned long lengths[3];
872 for (auto &l : lengths) {
873 // check that no shape/shape data/layer lengths is empty
874 l=input->readULong(4);
875 if (l==0)
876 return false;
877 }
878 if (strict) {
879 // try to decode the shape and the shape data zone
880 CanvasParserInternal::Decoder decoder;
881 decoder.m_isWindows=m_state->m_isWindowsFile;
882 decoder.m_version=vers;
883 input->seek(0x38, librevenge::RVNG_SEEK_SET);
884 unsigned long bitmapSize=m_state->m_isWindowsFile ? input->readULong(4) : 0;
885 if (long(bitmapSize)<0 || (m_state->m_isWindowsFile && !input->checkPosition(long(0x920+bitmapSize))) ||
886 !decoder.initOutput(input, m_state->m_isWindowsFile ? 0x920+bitmapSize : 0x89c) ||
887 !decoder.decode(long(lengths[0])) || !decoder.decode(long(lengths[1])))
888 return false;
889 }
890 setVersion(vers);
891 if (header)
892 header->reset(MWAWDocument::MWAW_T_CANVAS, vers, MWAWDocument::MWAW_K_DRAW);
893
894 return true;
895 }
896
897 bool CanvasParser::readFileHeader()
898 {
899 auto input=getInput();
900 long const endPos=0x3c;
901 if (!input || !input->checkPosition(endPos)) {
902 MWAW_DEBUG_MSG(("CanvasParser::readFileHeader: file is too short\n"));
903 return false;
904 }
905
906 m_state->m_lengths.clear();
907
908 libmwaw::DebugStream f;
909 f << "FileHeader:";
910 input->seek(0, librevenge::RVNG_SEEK_SET);
911 f << "length=[";
912 for (int i=0; i<13; ++i) {
913 auto length=input->readULong(4);
914 if (i>=4 && i<12)
915 m_state->m_brushLengths.push_back(length);
916 else
917 m_state->m_lengths.push_back(length);
918 f << length << ",";
919 }
920 f << "],";
921 ascii().addPos(0);
922 ascii().addNote(f.str().c_str());
923
924 long pos=input->tell();
925 f.str("");
926 f << "FileHeader-end:";
927 int val;
928 val=int(input->readLong(1));
929 if (val==1)
930 f << "little[endian],";
931 else if (val)
932 f << "##endian=" << val << ",";
933 val=int(input->readULong(1)); // v2: 100, v3: 104
934 switch (val) {
935 case 100:
936 f << "v2.0,";
937 break;
938 case 102:
939 f << "v2.1,";
940 break;
941 case 104:
942 f << "v3.0,";
943 break;
944 case 105:
945 f << "v3.5,";
946 break;
947 case 107: // or windows 3.5
948 f << "v3.5.2,";
949 break;
950 default:
951 f << "version=" << val << ",";
952 break;
953 }
954 val=int(input->readULong(2));
955 if (val!=1) f << "vers=" << val+1 << ",";
956 if (isWindowsFile()) {
957 m_state->m_bitmapSize=long(input->readULong(4));
958 f << "bitmap[size]=" << m_state->m_bitmapSize << ",";
959 }
960 if (input->tell()!=endPos)
961 ascii().addDelimiter(input->tell(),'|');
962 ascii().addPos(pos);
963 ascii().addNote(f.str().c_str());
964 input->seek(endPos, librevenge::RVNG_SEEK_SET);
965 return true;
966 }
967
968 bool CanvasParser::readDocumentHeader()
969 {
970 auto input=getInput();
971 long pos=input ? input->tell() : 0;
972 if (!input || !input->checkPosition(pos+230)) {
973 MWAW_DEBUG_MSG(("CanvasParser::readDocumentHeader: file is too short\n"));
974 return false;
975 }
976
977 libmwaw::DebugStream f;
978 f << "Entries(Document):";
979 input->seek(pos+46, librevenge::RVNG_SEEK_SET);
980 ascii().addDelimiter(input->tell(),'|');
981 int dim[2];
982 for (auto &d : dim) d=int(input->readULong(2));
983 if (dim[0]!=1 || dim[1]!=1) {
984 m_state->m_numPages=MWAWVec2i(dim[0], dim[1]);
985 f << "pages=" << m_state->m_numPages << ",";
986 if (dim[0]<=0 || dim[0]>15 || dim[1]<=0 || dim[1]>15) {
987 MWAW_DEBUG_MSG(("CanvasParser::readDocumentHeader: the number of pages seems bad\n"));
988 f << "###";
989 m_state->m_numPages=MWAWVec2i(1,1);
990 }
991 }
992 ascii().addDelimiter(input->tell(),'|');
993 input->seek(pos+60, librevenge::RVNG_SEEK_SET);
994 ascii().addPos(pos);
995 ascii().addNote(f.str().c_str());
996
997 pos=input->tell();
998 f.str("");
999 f << "Document-0:";
1000 m_state->m_numPatterns=int(input->readULong(2));
1001 if (m_state->m_numPatterns!=120)
1002 f << "num[patterns]=" << m_state->m_numPatterns << ",";
1003 ascii().addDelimiter(input->tell(),'|');
1004 input->seek(pos+58, librevenge::RVNG_SEEK_SET);
1005 ascii().addPos(pos);
1006 ascii().addNote(f.str().c_str());
1007
1008 pos=input->tell();
1009 f.str("");
1010 f << "Document-1:";
1011 int val=int(input->readULong(2));
1012 m_state->m_numShapes=int(input->readULong(2));
1013 f << "num[shapes]=" << m_state->m_numShapes << ",";
1014 if (val!=m_state->m_numShapes)
1015 f << "max[shapes]=" << val << ",";
1016 val=int(input->readULong(2));
1017 f << "f0=" << val << ","; // small number
1018 ascii().addDelimiter(input->tell(),'|');
1019 input->seek(pos+30, librevenge::RVNG_SEEK_SET);
1020 ascii().addPos(pos);
1021 ascii().addNote(f.str().c_str());
1022
1023 pos=input->tell();
1024 f.str("");
1025 f << "Document-2:";
1026 for (int i=0; i<9; ++i) { // f0=-1|1, f1=1|5, f2=0|1,f3=0|2, f7=-1000|-2000
1027 val=int(input->readLong(2));
1028 int const expected[]= {-1,1,0,0,1,0,0,-1000,-1000};
1029 if (val==expected[i])
1030 continue;
1031 if (i==4) {
1032 m_state->m_numLayers=val;
1033 f << "N[layer]=" << val << ",";
1034 }
1035 else if (i==6) {
1036 m_state->m_numViews=val;
1037 f << "N[view]=" << val << ",";
1038 }
1039 else
1040 f << "f" << i << "=" << val << ",";
1041 }
1042 if (version()==2) {
1043 librevenge::RVNGString text;
1044 if (readString(text, 64)) // find "1page Grp B&W"
1045 f << text.cstr() << ",";
1046 else
1047 f << "###string,";
1048 }
1049 else // something like 0a40800...0
1050 ascii().addDelimiter(input->tell(),'|');
1051 input->seek(pos+18+64, librevenge::RVNG_SEEK_SET);
1052 ascii().addPos(pos);
1053 ascii().addNote(f.str().c_str());
1054 return true;
1055 }
1056
1057 bool CanvasParser::readMacroNames()
1058 {
1059 auto input=getInput();
1060 long pos=input ? input->tell() : 0;
1061 if (!input || !input->checkPosition(pos+32*20)) {
1062 MWAW_DEBUG_MSG(("CanvasParser::readMacroNames: file is too short\n"));
1063 return false;
1064 }
1065
1066 libmwaw::DebugStream f;
1067 for (int i=0; i<32; ++i) {
1068 pos=input->tell();
1069 f.str("");
1070 f << "Entries(Macro)[" << i << "]:";
1071 librevenge::RVNGString text;
1072 if (readString(text, 20, true)) {
1073 if (text.empty()) {
1074 ascii().addPos(pos);
1075 ascii().addNote("_");
1076 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1077 continue;
1078 }
1079 f << "name=" << text.cstr() << ",";
1080 }
1081 else
1082 f << "##name,";
1083 ascii().addPos(pos);
1084 ascii().addNote(f.str().c_str());
1085 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1086 }
1087 return true;
1088 }
1089
1090 bool CanvasParser::readUnknownZoneHeader()
1091 {
1092 auto input=getInput();
1093 long pos=input ? input->tell() : 0;
1094 if (!input || !input->checkPosition(pos+28)) {
1095 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZoneHeader: file is too short\n"));
1096 return false;
1097 }
1098
1099 libmwaw::DebugStream f;
1100 f << "Entries(ZoneH):";
1101 ascii().addPos(pos);
1102 ascii().addNote(f.str().c_str());
1103 input->seek(pos+28, librevenge::RVNG_SEEK_SET);
1104 return true;
1105 }
1106
1107 bool CanvasParser::readUnknownZone0()
1108 {
1109 auto input=getInput();
1110 long pos=input ? input->tell() : 0;
1111 if (!input || !input->checkPosition(pos+252)) {
1112 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone0: file is too short\n"));
1113 return false;
1114 }
1115
1116 libmwaw::DebugStream f;
1117 f << "Entries(Zone0):";
1118 int val=int(input->readLong(2));
1119 if (val!=-1) // or 5
1120 f << "f0=" << val << ",";
1121 for (int i=0; i<20; ++i) { // f1=0|30, f7=f9=0|8000, f12=0|1, f14=f19=0|5, f16=f17=0|20, f20=0|29
1122 val=int(input->readULong(2));
1123 if (!val) continue;
1124 if (val<0x1000)
1125 f << "f" << i+1 << "=" << val << ",";
1126 else
1127 f << "f" << i+1 << "=" << std::hex << val << std::dec << ",";
1128 }
1129 for (int st=0; st<2; ++st) {
1130 unsigned char col[3];
1131 for (auto &c : col) c=(unsigned char)(input->readULong(2)>>8);
1132 MWAWColor color(col[0],col[1],col[2]);
1133 if (color!=MWAWColor::black())
1134 f << "col" << st << "=" << color << ",";
1135 }
1136 f << "id=" << std::hex << input->readULong(4) << std::dec << ",";
1137 ascii().addPos(pos);
1138 ascii().addNote(f.str().c_str());
1139
1140 pos=input->tell();
1141 f.str("");
1142 f << "Zone0-1:";
1143 for (int i=0; i<9; ++i) { // f0=3|5|4e, f1=1
1144 val=int(input->readULong(2));
1145 if (!val) continue;
1146 if (val<0x1000)
1147 f << "f" << i+1 << "=" << val << ",";
1148 else
1149 f << "f" << i+1 << "=" << std::hex << val << std::dec << ",";
1150 }
1151 librevenge::RVNGString text;
1152 if (readString(text, 20))
1153 f << "name=" << text.cstr() << ","; // Layer
1154 else
1155 f << "###name,";
1156 input->seek(pos+18+20, librevenge::RVNG_SEEK_SET);
1157 ascii().addPos(pos);
1158 ascii().addNote(f.str().c_str());
1159
1160 pos=input->tell();
1161 f.str("");
1162 f << "Zone0-2:";
1163 f << "font=[";
1164 f << "id=" << input->readULong(2) << ",";
1165 val=int(input->readULong(2));
1166 if (val) f << "fl=" << std::hex << val << std::dec << ",";
1167 f << "sz=" << input->readULong(2) << ",";
1168 f << "],";
1169 for (int i=0; i<36; ++i) { // f3=-2, f7=0|1, f11=0|28, f13=0|18, f15=0|8, f35=0|-16
1170 val=int(input->readLong(2));
1171 if (!val) continue;
1172 f << "f" << i << "=" << val << ",";
1173 }
1174 ascii().addPos(pos);
1175 ascii().addNote(f.str().c_str());
1176
1177 pos=input->tell();
1178 f.str("");
1179 f << "Zone0-3:";
1180 for (int i=0; i<7; ++i) {
1181 val=int(input->readLong(2));
1182 int const expected[]= {-50,16,-26,0,0,0,0};
1183 if (val==expected[i]) continue;
1184 f << "f" << i << "=" << val << ",";
1185 }
1186 for (int st=0; st<2; ++st) {
1187 int dim[4];
1188 for (auto &d : dim) d=int(input->readLong(2));
1189 if (dim[0]!=dim[2])
1190 f << "box" << st << "=" << MWAWBox2i(MWAWVec2i(dim[0],dim[1]),MWAWVec2i(dim[2],dim[3])) << ",";
1191 }
1192 for (int i=0; i<12; ++i) {
1193 val=int(input->readLong(2));
1194 int const expected[]= {1,16,0,3,1,0,0x48 /* or 4b*/, 0, 1, 0, 1, 0};
1195 if (val==expected[i]) continue;
1196 f << "g" << i << "=" << val << ",";
1197 }
1198 if (readString(text, 20)) // checkme: in v3.5 windows, probably junk
1199 f << "name=" << text.cstr() << ",";
1200 else
1201 f << "###name,";
1202 input->seek(pos+14+16+24+20, librevenge::RVNG_SEEK_SET);
1203 for (int i=0; i<2; ++i) {
1204 val=int(input->readLong(2));
1205 if (!val) continue;
1206 f << "overlap[" << (i==0 ? "H" : "V") << "]=" << val << ",";
1207 }
1208 ascii().addPos(pos);
1209 ascii().addNote(f.str().c_str());
1210 return true;
1211 }
1212
1213 bool CanvasParser::readBrushes()
1214 {
1215 auto input=getInput();
1216 if (!input) {
1217 MWAW_DEBUG_MSG(("CanvasParser::readBrushes: file is too short\n"));
1218 return false;
1219 }
1220
1221 long pos=input->tell();
1222 libmwaw::DebugStream f;
1223 f << "Entries(Brush):";
1224 ascii().addPos(pos);
1225 ascii().addNote(f.str().c_str());
1226
1227 bool const isWindows=isWindowsFile();
1228 if (!isWindows) {
1229 for (size_t i=0 ; i<m_state->m_brushLengths.size() ; ++i) {
1230 auto len=m_state->m_brushLengths[i];
1231 if (!len) continue;
1232 if (long(len)<0 || !decode(long(len))) {
1233 MWAW_DEBUG_MSG(("CanvasParser::readBrushes: can not decode the input %d\n", int(i)));
1234 return false;
1235 }
1236 pos=input->tell();
1237 f.str("");
1238 f << "Brush-" << i << ":";
1239 int N=int(input->readULong(2));
1240 if (!input->checkPosition(pos+2+4*N) || 2+4*N>long(len)) {
1241 MWAW_DEBUG_MSG(("CanvasParser::readBrushes: can not read a brush\n"));
1242 return false;
1243 }
1244 input->seek(pos+long(len), librevenge::RVNG_SEEK_SET);
1245 ascii().addPos(pos);
1246 ascii().addNote(f.str().c_str());
1247 }
1248 return true;
1249 }
1250
1251 // windows file have more brushes, the first 36 are generally stored by pair, the last one are stored one by one ?
1252 for (size_t i=0 ; i<m_state->m_brushLengths.size() ; ++i) {
1253 auto len=m_state->m_brushLengths[i];
1254 if (i+1<m_state->m_brushLengths.size())
1255 len+=m_state->m_brushLengths[i+1];
1256 if (!len) {
1257 ++i;
1258 continue;
1259 }
1260 if (i>=36 || len>256 || long(len)<0 || !decode(long(len))) { // check me: nbig blocks are stored one by one, what is the limit ?
1261 len=m_state->m_brushLengths[i];
1262 if (long(len)<0 || !decode(long(len))) {
1263 MWAW_DEBUG_MSG(("CanvasParser::readBrushes: can not decode the input %d\n", int(i)));
1264 return false;
1265 }
1266 pos=input->tell();
1267 f.str("");
1268 f << "Brush-" << i << ":";
1269 ascii().addPos(pos);
1270 ascii().addNote(f.str().c_str());
1271
1272 input->seek(pos+long(len), librevenge::RVNG_SEEK_SET);
1273 continue;
1274 }
1275 pos=input->tell();
1276 f.str("");
1277 f << "Brush-" << i << ":";
1278 ascii().addPos(pos);
1279 ascii().addNote(f.str().c_str());
1280
1281 f.str("");
1282 f << "Brush-" << ++i << ":";
1283 ascii().addPos(pos+long(m_state->m_brushLengths[i-1]));
1284 ascii().addNote(f.str().c_str());
1285
1286 input->seek(pos+long(len), librevenge::RVNG_SEEK_SET);
1287 }
1288 return true;
1289 }
1290
1291 bool CanvasParser::readSprays()
1292 {
1293 auto input=getInput();
1294 for (auto const &l : m_state->m_sprayLengths) {
1295 if (!l) continue;
1296 if (long(l)<0 || !decode(long(l))) {
1297 MWAW_DEBUG_MSG(("CanvasParser::readSprays: can not decode the input\n"));
1298 return false;
1299 }
1300 long pos=input->tell();
1301 if (long(l)<=0 || !input->checkPosition(pos+long(l))) {
1302 MWAW_DEBUG_MSG(("CanvasParser::readSprays: can not read a spray\n"));
1303 return false;
1304 }
1305 /* spray:
1306 ID dSz=0006 bdBox=ffefffef00100010 ymin,xmin ymax,xmax
1307 then dY*dSz
1308 ID dSz=0004 bdBox=fff7fff30007000b
1309 */
1310 ascii().addPos(pos);
1311 ascii().addNote("Spray:");
1312 input->seek(pos+long(l), librevenge::RVNG_SEEK_SET);
1313 }
1314 return true;
1315 }
1316
1317 bool CanvasParser::readFormats()
1318 {
1319 auto input=getInput();
1320 long pos=input ? input->tell() : 0;
1321 if (!input || !input->checkPosition(pos+8+6*44)) {
1322 MWAW_DEBUG_MSG(("CanvasParser::readFormats: file is too short\n"));
1323 return false;
1324 }
1325
1326 libmwaw::DebugStream f;
1327 f << "Entries(Format):";
1328 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
1329 ascii().addPos(pos);
1330 ascii().addNote(f.str().c_str());
1331 for (int u=0; u<6; ++u) {
1332 pos=input->tell();
1333 f.str("");
1334 f << "Format-" << u << ":";
1335 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
1336 ascii().addDelimiter(input->tell(),'|');
1337 for (int i=0; i<4; ++i) {
1338 int val=int(input->readULong(4));
1339 if (val!=0x10000)
1340 f << "dim" << i << "=" << double(val)/double(0x10000) << ",";
1341 }
1342 librevenge::RVNGString text;
1343 if (readString(text, 20))
1344 f << "name=" << text.cstr() << ",";
1345 else
1346 f << "###name,";
1347 input->seek(pos+44, librevenge::RVNG_SEEK_SET);
1348 ascii().addPos(pos);
1349 ascii().addNote(f.str().c_str());
1350 }
1351 return true;
1352 }
1353
1354 bool CanvasParser::readGrids()
1355 {
1356 auto input=getInput();
1357 long pos=input ? input->tell() : 0;
1358 if (!input || !input->checkPosition(pos+18*20)) {
1359 MWAW_DEBUG_MSG(("CanvasParser::readGrids: file is too short\n"));
1360 return false;
1361 }
1362
1363 libmwaw::DebugStream f;
1364 for (int i=0; i<3; ++i) {
1365 pos=input->tell();
1366 f.str("");
1367 f << "Entries(Grid)[" << i << "]:";
1368 librevenge::RVNGString text;
1369 if (readString(text, 20)) {
1370 if (text.empty()) {
1371 ascii().addPos(pos);
1372 ascii().addNote("_");
1373 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1374 continue;
1375 }
1376 f << "name=" << text.cstr() << ",";
1377 }
1378 else
1379 f << "###name,";
1380 ascii().addPos(pos);
1381 ascii().addNote(f.str().c_str());
1382 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1383 }
1384 pos=input->tell();
1385 f.str("");
1386 f << "Entries(Spray):lengths=[";
1387 for (int i=0; i<20; ++i) {
1388 m_state->m_sprayLengths.push_back(input->readULong(4));
1389 if (m_state->m_sprayLengths.back())
1390 f << m_state->m_sprayLengths.back() << ",";
1391 else
1392 f << "_,";
1393 }
1394 f << "],";
1395 ascii().addPos(pos);
1396 ascii().addNote(f.str().c_str());
1397 for (int i=0; i<11; ++i) {
1398 // checkme: make no sense
1399 pos=input->tell();
1400 f.str("");
1401 f << "Entries(Grid)[" << i+3 << "]:";
1402 librevenge::RVNGString text;
1403 if (readString(text, 20)) {
1404 if (text.empty()) {
1405 ascii().addPos(pos);
1406 ascii().addNote("_");
1407 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1408 continue;
1409 }
1410 f << "name=" << text.cstr() << ",";
1411 }
1412 else
1413 f << "###name,";
1414 ascii().addPos(pos);
1415 ascii().addNote(f.str().c_str());
1416 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1417 }
1418 return true;
1419 }
1420
1421 bool CanvasParser::readUnknownZone1()
1422 {
1423 auto input=getInput();
1424 long pos=input ? input->tell() : 0;
1425 if (!input || !input->checkPosition(pos+162)) {
1426 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone1: file is too short\n"));
1427 return false;
1428 }
1429
1430 libmwaw::DebugStream f;
1431 f << "Entries(Zone1):";
1432 int val;
1433 for (int i=0; i<18; ++i) {
1434 val=int(input->readULong(2));
1435 if (!val) continue;
1436 f << "f" << i << "=" << val << ",";
1437 }
1438 for (int i=0; i<16; ++i) {
1439 val=int(input->readLong(2));
1440 int const expected[]= { 1, 0, 1, 1, 10, 0, 10, 0, 100 /* or 10000 */, 1,
1441 2, 1/* or 4*/, 0/*or 1*/, 0, 1, 1
1442 };
1443 if (val==expected[i]) continue;
1444 f << "g" << i << "=" << val << ",";
1445 }
1446 for (int i=0; i<9; ++i) {
1447 val=int(input->readLong(4));
1448 int const expected[]= { 100 /* or 120*/, 100, 100, 1 /* or 4*/, 1, 1, 1, 1, 1 };
1449 if (val==65536*expected[i]) continue;
1450 f << "h" << i << "=" << double(val)/65536. << ",";
1451 }
1452 ascii().addPos(pos);
1453 ascii().addNote(f.str().c_str());
1454 pos=input->tell();
1455 f.str("");
1456 f << "Zone1-1:";
1457 for (int i=0; i<10; ++i) { // f5=small number, f6/f7: related to spray(a list of bit?) f14: related to brush?
1458 val=int(input->readLong(2));
1459 int const expected[]= { 50, 10, 0 /* or 1*/, 0, 0, 0, 2, 0, 0, 0};
1460 if (val==expected[i]) continue;
1461 f << "f" << i << "=" << val << ",";
1462 }
1463 for (int i=0; i<19; ++i) { // f27= almost always 1, f28=almost always 3
1464 val=int(input->readLong(2));
1465 if (!val) continue;
1466 f << "f" << i+10 << "=" << val << ",";
1467 }
1468 ascii().addPos(pos);
1469 ascii().addNote(f.str().c_str());
1470 return true;
1471 }
1472
1473 bool CanvasParser::readUnknownZone2()
1474 {
1475 if (!decode(96)) {
1476 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone2: can not decode the input\n"));
1477 return false;
1478 }
1479 auto input=getInput();
1480 long pos=input ? input->tell() : 0;
1481 if (!input || !input->checkPosition(pos+96)) {
1482 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone2: file is too short\n"));
1483 return false;
1484 }
1485
1486 libmwaw::DebugStream f;
1487 f << "Entries(Zone2):";
1488 ascii().addPos(pos);
1489 ascii().addNote(f.str().c_str());
1490 input->seek(pos+96, librevenge::RVNG_SEEK_SET);
1491 return true;
1492 }
1493
1494 bool CanvasParser::readUnknownZone3()
1495 {
1496 if (long(m_state->m_lengths[4])==0)
1497 return true;
1498 if (long(m_state->m_lengths[4])<0 || !decode(long(m_state->m_lengths[4]))) {
1499 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone3: can not decode the input\n"));
1500 return false;
1501 }
1502
1503 // 003a07000b010202080205030a040e0400050605030609060c0607070f0705080908000903090c09060a0a0a0e0a040b010c070c0a0c0d0d040e080f
1504 auto input=getInput();
1505 long pos=input ? input->tell() : 0;
1506 int sz=input ? int(input->readULong(2)) : 0;
1507 if (!input || !input->checkPosition(pos+2+sz) || 2+sz>long(m_state->m_lengths[4])) {
1508 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone3: file is too short\n"));
1509 return false;
1510 }
1511
1512 libmwaw::DebugStream f;
1513 f << "Entries(Zone3):";
1514 ascii().addPos(pos);
1515 ascii().addNote(f.str().c_str());
1516 input->seek(pos+long(m_state->m_lengths[4]), librevenge::RVNG_SEEK_SET);
1517 return true;
1518 }
1519
1520 bool CanvasParser::readUnknownZone4()
1521 {
1522 if (!decode(486)) {
1523 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone4: can not decode data\n"));
1524 return false;
1525 }
1526 MWAWInputStreamPtr input = getInput();
1527 long pos=input->tell();
1528 long endPos=pos+486;
1529 if (!input->checkPosition(endPos)) {
1530 MWAW_DEBUG_MSG(("CanvasParser::readUnknownZone4: zone seems too short\n"));
1531 return false;
1532 }
1533 libmwaw::DebugStream f;
1534 f << "Entries(Zone4):";
1535 ascii().addPos(pos);
1536 ascii().addNote(f.str().c_str());
1537
1538 int val=int(input->readLong(2)); // small number
1539 if (val) f << "f0=" << val << ",";
1540 for (int i=0; i<2; ++i) { // f1=-1|25, f2=0|8
1541 val=int(input->readLong(1));
1542 if (val) f << "f" << i+1 << "=" << val << ",";
1543 }
1544 int dim[2];
1545 for (auto &d : dim) d=int(input->readLong(2));
1546 m_state->m_pageDimension=MWAWVec2i(dim[0], dim[1]);
1547 f << "dim=" << m_state->m_pageDimension << ",";
1548 ascii().addDelimiter(input->tell(),'|');
1549 ascii().addPos(pos);
1550 ascii().addNote(f.str().c_str());
1551 ascii().addPos(pos+200);
1552 ascii().addNote("Zone4-0");
1553 ascii().addPos(pos+350);
1554 ascii().addNote("Zone4-1");
1555 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1556 return true;
1557 }
1558
1559 bool CanvasParser::readViews()
1560 {
1561 if (long(m_state->m_lengths[3])<0 || (m_state->m_lengths[3] && !decode(long(m_state->m_lengths[3])))) {
1562 MWAW_DEBUG_MSG(("CanvasParser::readViews: can not decode the input\n"));
1563 return false;
1564 }
1565 MWAWInputStreamPtr input = getInput();
1566 long pos=input->tell();
1567 long endPos=pos+long(m_state->m_lengths[3]);
1568 if (!input->checkPosition(endPos) ||
1569 long(m_state->m_lengths[3])/26 < m_state->m_numViews) {
1570 MWAW_DEBUG_MSG(("CanvasParser::readViews: zone seems too short\n"));
1571 return false;
1572 }
1573 libmwaw::DebugStream f;
1574 f << "Entries(View):";
1575 ascii().addPos(pos);
1576 ascii().addNote(f.str().c_str());
1577
1578 for (int i=0; i<m_state->m_numViews; ++i) {
1579 pos=input->tell();
1580 f.str("");
1581 f << "View-" << i << ":";
1582 // 3 int: a position + ?
1583 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
1584 ascii().addDelimiter(input->tell(),'|');
1585 librevenge::RVNGString text;
1586 if (readString(text, 20))
1587 f << text.cstr() << ",";
1588 else {
1589 f << "###name,";
1590 MWAW_DEBUG_MSG(("CanvasParser::readViews: bad name\n"));
1591 }
1592 input->seek(pos+26, librevenge::RVNG_SEEK_SET);
1593 ascii().addPos(pos);
1594 ascii().addNote(f.str().c_str());
1595 }
1596 if (input->tell()!=endPos && m_state->m_numViews) {
1597 ascii().addPos(input->tell());
1598 ascii().addNote("View-End:");
1599 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1600 }
1601
1602 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1603 ascii().addPos(pos);
1604 ascii().addNote(f.str().c_str());
1605
1606 return true;
1607 }
1608
1609 bool CanvasParser::readEndV3()
1610 {
1611 if (!decode(40)) {
1612 MWAW_DEBUG_MSG(("CanvasParser::readEndV3: can not decode the input zone\n"));
1613 return false;
1614 }
1615 MWAWInputStreamPtr input = getInput();
1616 long pos=input->tell();
1617 if (!input->checkPosition(pos+40)) {
1618 MWAW_DEBUG_MSG(("CanvasParser::readEndV3: zone seems too short\n"));
1619 return false;
1620 }
1621 libmwaw::DebugStream f;
1622 f << "Entries(EndV3):lengths=[";
1623 long lengths[10];
1624 for (auto &l : lengths) {
1625 l=long(input->readULong(4));
1626 if (l)
1627 f << l << ",";
1628 else
1629 f << "_,";
1630 }
1631 f << "],";
1632 ascii().addPos(pos);
1633 ascii().addNote(f.str().c_str());
1634
1635 for (int i=0; i<10; ++i) {
1636 if (lengths[i]==0)
1637 continue;
1638 if (!decode(lengths[i])) {
1639 MWAW_DEBUG_MSG(("CanvasParser::readEndV3: can not decode the zone %d\n", i));
1640 return false;
1641 }
1642 pos=input->tell();
1643 int const expectedLengths[]= {60, 46, 34, 8, 132,
1644 0, 0, 0, 0, 0
1645 };
1646 int const dataLength=expectedLengths[i];
1647 long endPos=pos+lengths[i];
1648 if (lengths[i]<0 || endPos<=pos || !input->checkPosition(endPos) ||
1649 (dataLength && lengths[i]<dataLength)) {
1650 MWAW_DEBUG_MSG(("CanvasParser::readEndV3: zone %d seems too short\n", i));
1651 ascii().addPos(pos);
1652 ascii().addNote("Entries(Bad):###");
1653 return false;
1654 }
1655 bool done=false;
1656 switch (i) {
1657 case 0:
1658 done=m_styleManager->readDashes(int(lengths[i]/60), true);
1659 break;
1660 case 4:
1661 done=m_styleManager->readFonts(int(lengths[i]/132));
1662 break;
1663 default:
1664 break;
1665 }
1666
1667 char const *wh[]= {"Dash", nullptr, nullptr, nullptr, "Font",
1668 nullptr, nullptr, nullptr, nullptr, nullptr
1669 };
1670 std::string what;
1671 if (wh[i])
1672 what=wh[i];
1673 else {
1674 std::stringstream s;
1675 s << "ZoneA" << i;
1676 what=s.str();
1677 }
1678 if (done) {
1679 if (input->tell()!=endPos) {
1680 MWAW_DEBUG_MSG(("CanvasParser::readEndV3: find extra data for zone %d\n", i));
1681 f.str("");
1682 f << what << "-extra:###";
1683 ascii().addPos(input->tell());
1684 ascii().addNote(f.str().c_str());
1685 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1686 }
1687 continue;
1688 }
1689 f.str("");
1690 f << "Entries(" << what << "):";
1691 ascii().addPos(pos);
1692 ascii().addNote(f.str().c_str());
1693 if (dataLength>0) {
1694 int n=0;
1695 while (input->tell()+dataLength<=endPos) {
1696 pos=input->tell();
1697 f.str("");
1698 f << what << "-" << n++ << ":";
1699 ascii().addPos(pos);
1700 ascii().addNote(f.str().c_str());
1701 input->seek(pos+dataLength, librevenge::RVNG_SEEK_SET);
1702 }
1703 if (input->tell()!=endPos) {
1704 f.str("");
1705 f << what << ":extra##";
1706 ascii().addPos(pos);
1707 ascii().addNote(f.str().c_str());
1708 }
1709 }
1710 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1711 }
1712 return true;
1713 }
1714
1715 bool CanvasParser::readString(librevenge::RVNGString &string, int maxSize, bool canBeCString)
1716 {
1717 return readString(getInput(), string, maxSize, canBeCString);
1718 }
1719
1720 bool CanvasParser::readString(MWAWInputStreamPtr input, librevenge::RVNGString &string, int maxSize, bool canBeCString)
1721 {
1722 string.clear();
1723 if (!input) {
1724 MWAW_DEBUG_MSG(("CanvasParser::readString: can not find the input\n"));
1725 return false;
1726 }
1727 bool const isWindows=isWindowsFile();
1728 auto fontConverter=getFontConverter();
1729 int defaultFont=isWindows ? fontConverter->getId("CP1252") : 3;
1730 if (isWindows && canBeCString) {
1731 int n=0;
1732 while (!input->isEnd() && (maxSize<=0 || n<maxSize)) {
1733 char c=char(input->readULong(1));
1734 if (c==0)
1735 break;
1736 ++n;
1737 int unicode = fontConverter->unicode(defaultFont, static_cast<unsigned char>(c));
1738 if (unicode>0)
1739 libmwaw::appendUnicode(uint32_t(unicode), string);
1740 else {
1741 MWAW_DEBUG_MSG(("CanvasParser::readString: find unknown unicode for char=%d\n", int(c)));
1742 }
1743 }
1744 return true;
1745 }
1746 int sSz=int(input->readULong(1));
1747 if ((maxSize<=0 || sSz<maxSize) && input->checkPosition(input->tell()+sSz)) {
1748 for (int ch=0; ch<sSz; ++ch) {
1749 char c=char(input->readULong(1));
1750 if (c==0)
1751 break;
1752 int unicode = fontConverter->unicode(defaultFont, static_cast<unsigned char>(c));
1753 if (unicode>0)
1754 libmwaw::appendUnicode(uint32_t(unicode), string);
1755 else {
1756 MWAW_DEBUG_MSG(("CanvasParser::readString: find unknown unicode for char=%d\n", int(c)));
1757 }
1758 }
1759 }
1760 else {
1761 MWAW_DEBUG_MSG(("CanvasParser::readString: bad size=%d\n", sSz));
1762 return false;
1763 }
1764 return true;
1765 }
1766
1767 // ------------------------------------------------------------
1768 // mac resource fork
1769 // ------------------------------------------------------------
1770
1771 bool CanvasParser::readPrintInfo(MWAWInputStreamPtr input, MWAWEntry const &entry, libmwaw::DebugFile &ascFile)
1772 {
1773 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
1774 return false;
1775 if (entry.length()<120) {
1776 MWAW_DEBUG_MSG(("CanvasParser::readPrintInfo: the zone seems too small\n"));
1777 return false;
1778 }
1779 entry.setParsed(true);
1780 libmwaw::DebugStream f;
1781 f << "Entries(PrintInfo)[" << entry.id() << "]:";
1782 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
1783 libmwaw::PrinterInfo info;
1784 if (!info.read(input)) {
1785 MWAW_DEBUG_MSG(("CanvasParser::readPrintInfo: can not read the zone length\n"));
1786 f << "###";
1787 ascFile.addPos(entry.begin()-4);
1788 ascFile.addNote(f.str().c_str());
1789 return false;
1790 }
1791 f << info;
1792 MWAWVec2i paperSize = info.paper().size();
1793 MWAWVec2i pageSize = info.page().size();
1794 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1795 paperSize.x() <= 0 || paperSize.y() <= 0) {
1796 f << "###";
1797 ascFile.addPos(entry.begin()-4);
1798 ascFile.addNote(f.str().c_str());
1799 return false;
1800 }
1801
1802 if (entry.id()==10568) {
1803 // define margin from print info
1804 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1805 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
1806
1807 // move margin left | top
1808 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1809 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1810 lTopMargin -= MWAWVec2i(decalX, decalY);
1811 rBotMargin += MWAWVec2i(decalX, decalY);
1812
1813 // decrease right | bottom
1814 int rightMarg = rBotMargin.x() -10;
1815 if (rightMarg < 0) rightMarg=0;
1816 int botMarg = rBotMargin.y() -50;
1817 if (botMarg < 0) botMarg=0;
1818
1819 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
1820 getPageSpan().setMarginBottom(botMarg/72.0);
1821 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
1822 getPageSpan().setMarginRight(rightMarg/72.0);
1823 getPageSpan().setFormLength(paperSize.y()/72.);
1824 getPageSpan().setFormWidth(paperSize.x()/72.);
1825 }
1826 else {
1827 MWAW_DEBUG_MSG(("CanvasParser::readPrintInfo: find unexpected\n"));
1828 }
1829 if (entry.length()>124)
1830 ascFile.addDelimiter(input->tell(),'|');
1831 ascFile.addPos(entry.begin()-4);
1832 ascFile.addNote(f.str().c_str());
1833 return true;
1834 }
1835
1836 bool CanvasParser::readLPOL(MWAWInputStreamPtr input, MWAWEntry const &entry, libmwaw::DebugFile &ascFile)
1837 {
1838 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
1839 return false;
1840 if (entry.length()<2) {
1841 MWAW_DEBUG_MSG(("CanvasParser::readLPOL: the zone seems too small\n"));
1842 return false;
1843 }
1844 entry.setParsed(true);
1845 libmwaw::DebugStream f;
1846 f << "Entries(LPOL)[" << entry.id() << "]:";
1847 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
1848 int N=int(input->readULong(2));
1849 if (2+4*N>entry.length()) {
1850 f << "###N=" << N << ",";
1851 MWAW_DEBUG_MSG(("CanvasParser::readLPOL: can not read the number of elements\n"));
1852 ascFile.addPos(entry.begin()-4);
1853 ascFile.addNote(f.str().c_str());
1854 return false;
1855 }
1856 for (int i=0; i<N; ++i) {
1857 f << "[";
1858 for (int v=0; v<4; ++v) f << input->readLong(1) << ",";
1859 f << "],";
1860 }
1861 if (input->tell()!=entry.end())
1862 ascFile.addDelimiter(input->tell(),'|');
1863 ascFile.addPos(entry.begin()-4);
1864 ascFile.addNote(f.str().c_str());
1865 return true;
1866 }
1867
1868 bool CanvasParser::readRSRCFileHeader(MWAWInputStreamPtr input, MWAWEntry const &entry, libmwaw::DebugFile &ascFile)
1869 {
1870 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
1871 return false;
1872 if (entry.length()<56) {
1873 MWAW_DEBUG_MSG(("CanvasParser:readRSRCFileHeader: the zone seems too small\n"));
1874 return false;
1875 }
1876 entry.setParsed(true);
1877 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
1878 libmwaw::DebugStream f;
1879 f << "FileHeader[RSRC" << entry.id() << "]:";
1880 f << "length?=[";
1881 for (int i=0; i<13; ++i) {
1882 auto length=input->readULong(4);
1883 f << length << ",";
1884 }
1885 int val=int(input->readLong(2)); // v2: 100, v3: 104
1886 switch (val) {
1887 case 100:
1888 f << "v2.0,";
1889 break;
1890 case 102:
1891 f << "v2.1,";
1892 break;
1893 case 104:
1894 f << "v3.0,";
1895 break;
1896 default:
1897 f << "version=" << val << ",";
1898 break;
1899 }
1900 val=int(input->readULong(2));
1901 if (val!=1) f << "vers=" << val+1 << ",";
1902
1903 if (input->tell()!=entry.end())
1904 ascFile.addDelimiter(input->tell(),'|');
1905 ascFile.addPos(entry.begin()-4);
1906 ascFile.addNote(f.str().c_str());
1907 return true;
1908 }
1909
1910 bool CanvasParser::readUsers(MWAWInputStreamPtr input, MWAWEntry const &entry, libmwaw::DebugFile &ascFile)
1911 {
1912 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
1913 return false;
1914 if (entry.length()<514) {
1915 MWAW_DEBUG_MSG(("CanvasParser::readUsers: the zone seems too small\n"));
1916 return false;
1917 }
1918 entry.setParsed(true);
1919 libmwaw::DebugStream f;
1920 f << "Entries(Users)[" << entry.id() << "]:";
1921
1922 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
1923 librevenge::RVNGString text;
1924 if (readString(input, text, 64)) { // user name
1925 if (!text.empty())
1926 m_state->m_metaData.insert("meta:initial-creator", text);
1927 f << text.cstr() << ",";
1928 }
1929 else {
1930 f << "###name,";
1931 MWAW_DEBUG_MSG(("CanvasParser::readUsers: bad user name\n"));
1932 }
1933 input->seek(entry.begin()+64,librevenge::RVNG_SEEK_SET);
1934 ascFile.addDelimiter(input->tell(),'|');
1935 input->seek(entry.begin()+128,librevenge::RVNG_SEEK_SET);
1936 ascFile.addPos(entry.begin()-4);
1937 ascFile.addNote(f.str().c_str());
1938
1939 long pos=input->tell();
1940 f.str("");
1941 f << "Users-0:";
1942 input->seek(pos+128,librevenge::RVNG_SEEK_SET);
1943 ascFile.addPos(pos);
1944 ascFile.addNote(f.str().c_str());
1945
1946 pos=input->tell();
1947 f.str("");
1948 f << "Users-1:";
1949 int val=int(input->readLong(2));
1950 if (val) f << "f0=" << val << ",";
1951 if (readString(input, text, 64)) // limits?
1952 f << text.cstr() << ",";
1953 else {
1954 f << "###dir,";
1955 MWAW_DEBUG_MSG(("CanvasParser::readUsers: bad dir\n"));
1956 }
1957 input->seek(pos+2+64,librevenge::RVNG_SEEK_SET);
1958 ascFile.addDelimiter(input->tell(),'|');
1959 input->seek(pos+128,librevenge::RVNG_SEEK_SET);
1960 ascFile.addPos(pos);
1961 ascFile.addNote(f.str().c_str());
1962
1963 pos=input->tell();
1964 f.str("");
1965 f << "Users-2:";
1966 input->seek(pos+130,librevenge::RVNG_SEEK_SET);
1967 ascFile.addPos(pos);
1968 ascFile.addNote(f.str().c_str());
1969
1970 if (input->tell()!=entry.end())
1971 ascFile.addDelimiter(input->tell(),'|');
1972 return true;
1973 }
1974
1975 bool CanvasParser::readWindows(MWAWInputStreamPtr input, MWAWEntry const &entry, libmwaw::DebugFile &ascFile)
1976 {
1977 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
1978 return false;
1979 if (entry.length()<20) {
1980 MWAW_DEBUG_MSG(("CanvasParser::readWindows: the zone seems too small\n"));
1981 return false;
1982 }
1983 entry.setParsed(true);
1984 libmwaw::DebugStream f;
1985 f << "Entries(Windows)[" << entry.id() << "]:";
1986 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
1987 int dim[4];
1988 for (auto &d : dim) d=int(input->readLong(2));
1989 f << "win=" << MWAWBox2i(MWAWVec2i(dim[0],dim[1]),MWAWVec2i(dim[2],dim[3])) << ",";
1990 for (int i=0; i<6; ++i) { // f6=small number
1991 int val=int(input->readLong(2));
1992 if (val)
1993 f << "f" << i << "=" << val << ",";
1994 }
1995 if (input->tell()!=entry.end())
1996 ascFile.addDelimiter(input->tell(),'|');
1997 ascFile.addPos(entry.begin()-4);
1998 ascFile.addNote(f.str().c_str());
1999 return true;
2000 }
2001
2002 // ------------------------------------------------------------
2003 // windows resource fork
2004 // ------------------------------------------------------------
2005
2006 bool CanvasParser::readRSRCWindowsFile()
2007 {
2008 auto input=getInput();
2009 if (!input)
2010 return false;
2011
2012 MWAWEntry entries[2];
2013 for (int step=0; step<2; ++step) {
2014 if (!m_state->m_decoder.append(4)) {
2015 MWAW_DEBUG_MSG(("CanvasParser::readRSRCWindowsFile: zone5 can not retrieve the length of zone %dB\n", step));
2016 return false;
2017 }
2018 input->recomputeStreamSize();
2019 long pos=input->tell();
2020 long sz=long(input->readULong(4));
2021 long endPos=pos+4+sz;
2022 if (sz<0 || !decode(sz) || !input->checkPosition(endPos)) {
2023 MWAW_DEBUG_MSG(("CanvasParser::readRSRCWindowsFile: can not decode zone %dB\n", step));
2024 return false;
2025 }
2026
2027 entries[step].setBegin(pos+4);
2028 entries[step].setLength(sz);
2029 libmwaw::DebugStream f;
2030 if (step==0)
2031 f << "Entries(RSRCMap):";
2032 else
2033 f << "Entries(RSRCData):";
2034 ascii().addPos(pos);
2035 ascii().addNote(f.str().c_str());
2036 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2037 }
2038
2039 long endPos=input->tell();
2040 int N=int(entries[0].length()/64);
2041 input->seek(entries[0].begin(), librevenge::RVNG_SEEK_SET);
2042 for (int n=0; n<N; ++n) {
2043 long pos=input->tell();
2044 libmwaw::DebugStream f;
2045 f << "RSRCMap-" << n << ":";
2046
2047 bool ok=true;
2048 std::string types[2];
2049 for (auto &text : types) {
2050 for (int c=0; c<4; ++c) {
2051 char ch=char(input->readULong(1));
2052 if (ch==0) {
2053 ok=false;
2054 break;
2055 }
2056 text+=ch;
2057 }
2058 if (!ok) break;
2059 f << text << ",";
2060 }
2061 if (!ok) { // empty field
2062 ascii().addPos(pos);
2063 ascii().addNote("_");
2064 input->seek(pos+64, librevenge::RVNG_SEEK_SET);
2065 continue;
2066 }
2067
2068 int val=int(input->readULong(2)); // 1 or 100
2069 if (val!=1) f << "f0=" << val << ",";
2070 librevenge::RVNGString name;
2071 if (!readString(name, 28, true))
2072 f << "##name,";
2073 else if (!name.empty())
2074 f << name.cstr() << ",";
2075 input->seek(pos+38, librevenge::RVNG_SEEK_SET);
2076 MWAWEntry entry;
2077 entry.setBegin(entries[1].begin()+input->readLong(4));
2078 entry.setLength(input->readLong(4));
2079 f << "unkn=" << std::hex << input->readULong(4) << std::dec << ",";
2080 val=int(input->readLong(4));
2081 if (val!=n)
2082 f << "id0=" << val << ",";
2083 int val2=int(input->readLong(4));
2084 if (val2!=val)
2085 f << "id1=" << val2 << ",";
2086 entry.setId(int(input->readLong(2)));
2087 if (entry.valid()) {
2088 f << std::hex << entry.begin() << "<->" << entry.end() << std::dec << entry << ",";
2089 if (entry.end()<=endPos) {
2090 static std::map<std::string, int> const nameToId= {
2091 { "Page", 0}, {"PSST", 1}, {"DevM", 2}, {"CSet", 3}, {"CVal", 4}, {"CNam", 5},
2092 { "FLDF", 6}
2093 };
2094 int wh=nameToId.find(types[1])!=nameToId.end() ? nameToId.find(types[1])->second : -1;
2095 bool done=false;
2096 long actPos=input->tell();
2097 switch (wh) {
2098 case 0:
2099 done=readPage(entry);
2100 break;
2101 case 1:
2102 done=readPrinterSST(entry);
2103 break;
2104 case 2:
2105 done=readPrinterDev(entry);
2106 break;
2107 case 3:
2108 done=readCSet(entry);
2109 break;
2110 case 4:
2111 done=m_styleManager->readColorValues(entry);
2112 break;
2113 case 5:
2114 done=readCNam(entry);
2115 break;
2116 case 6: {
2117 MWAWGraphicStyle::Gradient gradient;
2118 done=m_styleManager->readGradient(entry, gradient);
2119 break;
2120 }
2121 default:
2122 break;
2123 }
2124 input->seek(actPos, librevenge::RVNG_SEEK_SET);
2125 if (!done) {
2126 libmwaw::DebugStream f2;
2127 f2 << "RSRCData-" << types[1] << "[" << entry.id() << "]:";
2128 ascii().addPos(entry.begin());
2129 ascii().addNote(f2.str().c_str());
2130 }
2131 }
2132 else
2133 f << "###";
2134 }
2135 ascii().addDelimiter(input->tell(),'|');
2136 ascii().addPos(pos);
2137 ascii().addNote(f.str().c_str());
2138 input->seek(pos+64, librevenge::RVNG_SEEK_SET);
2139 }
2140 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2141 return true;
2142 }
2143
2144 bool CanvasParser::readCNam(MWAWEntry const &entry)
2145 {
2146 auto input=getInput();
2147 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
2148 return false;
2149 if (entry.length()<256) {
2150 MWAW_DEBUG_MSG(("CanvasParser::readCNam: the zone seems too small\n"));
2151 return false;
2152 }
2153 // checkme: find always an empty zone
2154 libmwaw::DebugStream f;
2155 f << "Entries(CNam)[" << entry.id() << "]:";
2156 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
2157 ascii().addPos(entry.begin());
2158 ascii().addNote(f.str().c_str());
2159
2160 for (int st=0; st<2; ++st) {
2161 long pos=input->tell();
2162 f.str("");
2163 f << "CNam-" << st << ":";
2164 for (int i=0; i<64; ++i) {
2165 int val=int(input->readLong(2));
2166 if (val)
2167 f << "f" << i << "=" << val << ",";
2168 }
2169 ascii().addPos(pos);
2170 ascii().addNote(f.str().c_str());
2171 }
2172 return true;
2173 }
2174
2175 bool CanvasParser::readCSet(MWAWEntry const &entry)
2176 {
2177 auto input=getInput();
2178 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
2179 return false;
2180 if (entry.length()<160) {
2181 MWAW_DEBUG_MSG(("CanvasParser::readCSet: the zone seems too small\n"));
2182 return false;
2183 }
2184 libmwaw::DebugStream f;
2185 f << "Entries(CSet)[" << entry.id() << "]:";
2186 input->seek(entry.begin()+31,librevenge::RVNG_SEEK_SET);
2187 ascii().addDelimiter(input->tell(), '|');
2188 librevenge::RVNGString name;
2189 if (!readString(name, 128, true)) // find Default
2190 f << "##name,";
2191 else if (!name.empty())
2192 f << name.cstr() << ",";
2193 input->seek(entry.begin()+31+128, librevenge::RVNG_SEEK_SET);
2194 ascii().addDelimiter(input->tell(), '|');
2195 ascii().addPos(entry.begin());
2196 ascii().addNote(f.str().c_str());
2197 return true;
2198 }
2199
2200 bool CanvasParser::readPage(MWAWEntry const &entry)
2201 {
2202 auto input=getInput();
2203 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
2204 return false;
2205 if (entry.length()<936) {
2206 MWAW_DEBUG_MSG(("CanvasParser::readPage: the zone seems too small\n"));
2207 return false;
2208 }
2209 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
2210 libmwaw::DebugStream f;
2211 f << "Entries(Page)[" << entry.id() << "]:";
2212 int val;
2213 for (int i=0; i<64; ++i) { // f0=1
2214 val=int(input->readLong(2));
2215 if (val)
2216 f << "f" << i << "=" << val << ",";
2217 }
2218 ascii().addPos(entry.begin());
2219 ascii().addNote(f.str().c_str());
2220
2221 long pos=input->tell();
2222 f.str("");
2223 f << "Page-A0:";
2224 for (int i=0; i<2; ++i) {
2225 val=int(input->readLong(2));
2226 if (val)
2227 f << "f" << i << "=" << val << ",";
2228 }
2229 librevenge::RVNGString name;
2230 if (!readString(name, 128, true))
2231 f << "##name,";
2232 else if (!name.empty())
2233 f << "printer=" << name.cstr() << ",";
2234 input->seek(pos+4+128, librevenge::RVNG_SEEK_SET);
2235 ascii().addPos(pos);
2236 ascii().addNote(f.str().c_str());
2237
2238 pos=input->tell();
2239 f.str("");
2240 f << "Page-A1:";
2241 for (int i=0; i<64; ++i) {
2242 val=int(input->readLong(2));
2243 if (val)
2244 f << "f" << i << "=" << val << ",";
2245 }
2246 ascii().addPos(pos);
2247 ascii().addNote(f.str().c_str());
2248
2249 pos=input->tell();
2250 f.str("");
2251 f << "Page-A2:";
2252 for (int i=0; i<18; ++i) {
2253 val=int(input->readLong(2));
2254 int const expected[]= {
2255 1/*or 7*/,1,0,8,0,
2256 0xe/*or 0x13*/,0, 0x30f, 0, 0x255/* or 251*/,
2257 0, 0x318, 0, 0x264, 0/* or 4*/,
2258 0,1,1
2259 };
2260 if (val!=expected[i])
2261 f << "f" << i << "=" << val << ",";
2262 }
2263 for (int i=0; i<46; ++i) {
2264 val=int(input->readLong(2));
2265 if (val)
2266 f << "g" << i << "=" << val << ",";
2267 }
2268 ascii().addPos(pos);
2269 ascii().addNote(f.str().c_str());
2270 input->seek(pos+128, librevenge::RVNG_SEEK_SET);
2271
2272 for (int wh=3; wh<7; ++wh) {
2273 pos=input->tell();
2274 f.str("");
2275 f << "Page-A" << wh << ":";
2276 for (int i=0; i<(wh==6 ? 18 : 64); ++i) {
2277 val=int(input->readLong(2));
2278 if (val)
2279 f << "f" << i << "=" << val << ",";
2280 }
2281 ascii().addPos(pos);
2282 ascii().addNote(f.str().c_str());
2283 }
2284 return true;
2285 }
2286
2287 bool CanvasParser::readPrinterDev(MWAWEntry const &entry)
2288 {
2289 auto input=getInput();
2290 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
2291 return false;
2292 if (entry.length()<198) {
2293 MWAW_DEBUG_MSG(("CanvasParser::readPrinterDev: the zone seems too small\n"));
2294 return false;
2295 }
2296 libmwaw::DebugStream f;
2297 f << "Entries(Printer)[Dev," << entry.id() << "]:";
2298 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
2299 librevenge::RVNGString name;
2300 if (!readString(name, 32, true))
2301 f << "##name,";
2302 else if (!name.empty())
2303 f << name.cstr() << ",";
2304 input->seek(entry.begin()+32, librevenge::RVNG_SEEK_SET);
2305
2306 ascii().addDelimiter(input->tell(),'|');
2307 ascii().addPos(entry.begin());
2308 ascii().addNote(f.str().c_str());
2309 input->seek(entry.begin()+128, librevenge::RVNG_SEEK_SET);
2310
2311 long pos=input->tell();
2312 f.str("");
2313 f << "Printer-A[Dev]:";
2314 ascii().addPos(pos);
2315 ascii().addNote(f.str().c_str());
2316 return true;
2317 }
2318
2319 bool CanvasParser::readPrinterSST(MWAWEntry const &entry)
2320 {
2321 auto input=getInput();
2322 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
2323 return false;
2324 if (entry.length()<113) {
2325 MWAW_DEBUG_MSG(("CanvasParser::readPrinterSST: the zone seems too small\n"));
2326 return false;
2327 }
2328 libmwaw::DebugStream f;
2329 f << "Entries(Printer)[" << entry.id() << "]:";
2330 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
2331 for (int i=0; i<4; ++i) { // find FILE:, "", driver file, driver name
2332 long pos=input->tell();
2333 librevenge::RVNGString name;
2334 if (!readString(name, 25, true))
2335 f << "##name,";
2336 else if (!name.empty())
2337 f << "text" << i << "=" << name.cstr() << ",";
2338 input->seek(pos+25, librevenge::RVNG_SEEK_SET);
2339 }
2340 int val;
2341 for (int i=0; i<4; ++i) {
2342 val=int(input->readLong(i==3 ? 1 : 2));
2343 if (val) f << "f" << i << "=" << val << ",";
2344 }
2345 for (int i=0; i<3; ++i) {
2346 val=int(input->readLong(2));
2347 int const expected[]= {1, 0xc6 /*or c8 or 34c */, 1};
2348 if (val!=expected[i]) f << "f" << i << "=" << val << ",";
2349 }
2350 ascii().addPos(entry.begin());
2351 ascii().addNote(f.str().c_str());
2352 return true;
2353 }
2354 ////////////////////////////////////////////////////////////
2355 //
2356 // send data
2357 //
2358 ////////////////////////////////////////////////////////////
2359
2360
2361 bool CanvasParser::send(CanvasParserInternal::Layer const &layer)
2362 {
2363 MWAWGraphicListenerPtr listener=getGraphicListener();
2364 if (!listener) {
2365 MWAW_DEBUG_MSG(("CanvasParser::send[layer]: can not find the listener\n"));
2366 return false;
2367 }
2368 if (layer.m_shapesId.empty())
2369 return true;
2370 bool openLayer=false;
2371 if (!layer.m_name.empty())
2372 openLayer=listener->openLayer(layer.m_name);
2373 for (auto const &id : layer.m_shapesId)
2374 m_graphParser->sendShape(id);
2375 if (openLayer)
2376 listener->closeLayer();
2377 return true;
2378 }
2379
2380
2381 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: