Debian Codegraph @

src/lib/WPS4.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2006, 2007 Andrew Ziem
11 * Copyright (C) 2004 Marc Maurer ([email protected])
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 *
14 * For minor contributions see the git repository.
15 *
16 * Alternatively, the contents of this file may be used under the terms
17 * of the GNU Lesser General Public License Version 2.1 or later
18 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
19 * applicable instead of those above.
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <librevenge-stream/librevenge-stream.h>
26
27 #include "libwps_internal.h"
28 #include "libwps_tools_win.h"
29
30 #include "WPSContentListener.h"
31 #include "WPSEntry.h"
32 #include "WPSHeader.h"
33 #include "WPSOLEParser.h"
34 #include "WPSPageSpan.h"
35 #include "WPSTextSubDocument.h"
36
37 #include "WPS4Graph.h"
38 #include "WPS4Text.h"
39
40 #include "WPS4.h"
41
42 namespace WPS4ParserInternal
43 {
44 //! Internal: the subdocument of a WPS4Parser
45 class SubDocument final : public WPSTextSubDocument
46 {
47 public:
48 //! type of an entry stored in textId
49 enum Type { Unknown, MN };
50 //! constructor for a text entry
51 SubDocument(RVNGInputStreamPtr const &input, WPS4Parser &pars, WPSEntry const &entry)
52 : WPSTextSubDocument(input, &pars), m_entry(entry) {}
53 //! destructor
54 ~SubDocument() final {}
55
56 //! operator==
57 bool operator==(std::shared_ptr<WPSSubDocument> const &doc) const final
58 {
59 if (!doc || !WPSTextSubDocument::operator==(doc))
60 return false;
61 auto const *sDoc = dynamic_cast<SubDocument const *>(doc.get());
62 if (!sDoc) return false;
63 return m_entry == sDoc->m_entry;
64 }
65
66 //! the parser function
67 void parse(std::shared_ptr<WPSContentListener> &listener, libwps::SubDocumentType subDocumentType) final;
68 //! the entry
69 WPSEntry m_entry;
70 };
71
72 void SubDocument::parse(std::shared_ptr<WPSContentListener> &listener, libwps::SubDocumentType subDocumentType)
73 {
74 if (!listener.get())
75 {
76 WPS_DEBUG_MSG(("WPS4ParserInternal::SubDocument::parse: no listener\n"));
77 return;
78 }
79 if (!dynamic_cast<WPSContentListener *>(listener.get()))
80 {
81 WPS_DEBUG_MSG(("WPS4ParserInternal::SubDocument::parse: bad listener\n"));
82 return;
83 }
84
85 auto &listen = reinterpret_cast<WPSContentListenerPtr &>(listener);
86 if (!m_parser)
87 {
88 listen->insertCharacter(' ');
89 WPS_DEBUG_MSG(("WPS4ParserInternal::SubDocument::parse: bad parser\n"));
90 return;
91 }
92
93 if (m_entry.isParsed() && subDocumentType != libwps::DOC_HEADER_FOOTER)
94 {
95 listen->insertCharacter(' ');
96 WPS_DEBUG_MSG(("WPS4ParserInternal::SubDocument::parse: this zone is already parsed\n"));
97 return;
98 }
99 m_entry.setParsed(true);
100 if (m_entry.type() != "TEXT")
101 {
102 listen->insertCharacter(' ');
103 WPS_DEBUG_MSG(("WPS4ParserInternal::SubDocument::parse: send not Text entry is not implemented\n"));
104 return;
105 }
106
107 if (!m_entry.valid())
108 {
109 if (subDocumentType != libwps::DOC_COMMENT_ANNOTATION)
110 {
111 WPS_DEBUG_MSG(("SubDocument::parse: empty document found...\n"));
112 }
113 listen->insertCharacter(' ');
114 return;
115 }
116
117 auto *mnParser = dynamic_cast<WPS4Parser *>(m_parser);
118 if (!mnParser)
119 {
120 WPS_DEBUG_MSG(("SubDocument::parse: bad parser...\n"));
121 listen->insertCharacter(' ');
122 return;
123 }
124 mnParser->send(m_entry, subDocumentType);
125 }
126
127 //! the state of WPS4
128 struct State
129 {
130 explicit State(libwps_tools_win::Font::Type fontType)
131 : m_isDosFile(false)
132 , m_eof(-1)
133 , m_fontType(fontType)
134 , m_OEMFontType(libwps_tools_win::Font::UNKNOWN)
135 , m_pageSpan()
136 , m_noFirstPageHeader(false)
137 , m_noFirstPageFooter(false)
138 , m_numColumns(1)
139 , m_actPage(0)
140 , m_numPages(0)
141 {
142 }
143 //! flag to know if the file is or not a dos file
144 bool m_isDosFile;
145 //! the last file position
146 long m_eof;
147 //! the user font type
148 libwps_tools_win::Font::Type m_fontType;
149 //! the OEM code page
150 libwps_tools_win::Font::Type m_OEMFontType;
151 //! the actual document size
152 WPSPageSpan m_pageSpan;
153 bool m_noFirstPageHeader /* true if the first page has no header */;
154 bool m_noFirstPageFooter /* true if the first page has no footer */;
155 int m_numColumns /** the number of columns */;
156 int m_actPage /** the actual page*/, m_numPages /* the number of pages */;
157 };
158 }
159
160 // constructor, destructor
161 WPS4Parser::WPS4Parser(RVNGInputStreamPtr &input, WPSHeaderPtr &header,
162 libwps_tools_win::Font::Type encoding)
163 : WPSParser(input, header)
164 , m_listener()
165 , m_graphParser()
166 , m_textParser()
167 , m_state()
168 {
169 m_state.reset(new WPS4ParserInternal::State(encoding));
170 m_graphParser.reset(new WPS4Graph(*this));
171 m_textParser.reset(new WPS4Text(*this, input));
172 }
173
174 WPS4Parser::~WPS4Parser()
175 {
176 }
177
178 // small funtion ( dimension, color, fileSize, ...)
179 float WPS4Parser::pageHeight() const
180 {
181 return float(m_state->m_pageSpan.getFormLength()-m_state->m_pageSpan.getMarginTop()-m_state->m_pageSpan.getMarginBottom());
182 }
183
184 float WPS4Parser::pageWidth() const
185 {
186 return float(m_state->m_pageSpan.getFormWidth()-m_state->m_pageSpan.getMarginLeft()-m_state->m_pageSpan.getMarginRight());
187 }
188
189 int WPS4Parser::numColumns() const
190 {
191 return m_state->m_numColumns;
192 }
193
194 libwps_tools_win::Font::Type WPS4Parser::getDefaultFontType() const
195 {
196 if (m_state->m_fontType != libwps_tools_win::Font::UNKNOWN)
197 return m_state->m_fontType;
198 return version()<=2 ? libwps_tools_win::Font::DOS_850 : libwps_tools_win::Font::WIN3_WEUROPE;
199 }
200
201 libwps_tools_win::Font::Type WPS4Parser::getOEMFontType() const
202 {
203 return m_state->m_OEMFontType;
204 }
205
206 bool WPS4Parser::getColor(int id, WPSColor &color) const
207 {
208 if (m_state->m_isDosFile)
209 {
210 static const uint32_t colorDosMap[]=
211 {
212 0x0, 0xFF0000, 0x00FF00, 0x0000FF,
213 0x00FFFF, 0xFF00FF, 0xFFFF00
214 };
215 if (id < 0 || id >= 7)
216 {
217 WPS_DEBUG_MSG(("WPS4Parser::getColor(): unknown Dos color id: %d\n",id));
218 return false;
219 }
220 color=WPSColor(colorDosMap[id]);
221 return true;
222 }
223 static const uint32_t colorMap[]=
224 {
225 // 0x00RRGGBB
226 0, // auto
227 0,
228 0x0000FF, 0x00FFFF,
229 0x00FF00, 0xFF00FF, 0xFF0000, 0xFFFF00,
230 0x808080, 0xFFFFFF, 0x000080, 0x008080,
231 0x008000, 0x800080, 0x800000, 0xC0C0C0
232 };
233 if (id < 0 || id >= 16)
234 {
235 WPS_DEBUG_MSG(("WPS4Parser::getColor(): unknown color id: %d\n",id));
236 return false;
237 }
238 color = WPSColor(colorMap[id]);
239 return true;
240 }
241
242 void WPS4Parser::setSizeFile(long sz)
243 {
244 if (sz > m_state->m_eof)
245 m_state->m_eof = sz;
246 }
247
248 bool WPS4Parser::checkFilePosition(long pos)
249 {
250 if (m_state->m_eof < 0)
251 {
252 RVNGInputStreamPtr input = getInput();
253 long actPos = input->tell();
254 input->seek(0, librevenge::RVNG_SEEK_END);
255 m_state->m_eof=input->tell();
256 input->seek(actPos, librevenge::RVNG_SEEK_SET);
257 }
258 return pos <= m_state->m_eof;
259 }
260
261 // listener, new page
262 void WPS4Parser::setListener(std::shared_ptr<WPSContentListener> const &listener)
263 {
264 m_listener = listener;
265 m_graphParser->setListener(m_listener);
266 m_textParser->setListener(m_listener);
267 }
268
269 std::shared_ptr<WPSContentListener> WPS4Parser::createListener(librevenge::RVNGTextInterface *interface)
270 {
271 std::vector<WPSPageSpan> pageList;
272 WPSPageSpan page1(m_state->m_pageSpan), ps(m_state->m_pageSpan);
273
274 WPSEntry ent = m_textParser->getHeaderEntry();
275 if (ent.valid())
276 {
277 WPSSubDocumentPtr subdoc(new WPS4ParserInternal::SubDocument
278 (getInput(), *this, ent));
279 ps.setHeaderFooter(WPSPageSpan::HEADER, WPSPageSpan::ALL, subdoc);
280 if (!m_state->m_noFirstPageHeader)
281 page1.setHeaderFooter(WPSPageSpan::HEADER, WPSPageSpan::ALL, subdoc);
282 }
283 ent = m_textParser->getFooterEntry();
284 if (ent.valid())
285 {
286 WPSSubDocumentPtr subdoc(new WPS4ParserInternal::SubDocument
287 (getInput(), *this, ent));
288 ps.setHeaderFooter(WPSPageSpan::FOOTER, WPSPageSpan::ALL, subdoc);
289 if (!m_state->m_noFirstPageFooter)
290 page1.setHeaderFooter(WPSPageSpan::FOOTER, WPSPageSpan::ALL, subdoc);
291 }
292
293 int numPages = m_textParser->numPages();
294 int graphPages = m_graphParser->numPages();
295 if (graphPages>=numPages) numPages = graphPages;
296 #ifdef DEBUG
297 // create all the pages + an empty page, if we have some remaining data...
298 numPages++;
299 #endif
300 pageList.push_back(page1);
301 for (int i = 1; i < numPages; i++) pageList.push_back(ps);
302 m_state->m_numPages=numPages;
303 return std::shared_ptr<WPSContentListener>
304 (new WPSContentListener(pageList, interface));
305 }
306
307 void WPS4Parser::newPage(int number)
308 {
309 if (number <= m_state->m_actPage || number > m_state->m_numPages)
310 return;
311
312 while (m_state->m_actPage < number)
313 {
314 m_state->m_actPage++;
315
316 if (m_listener.get() == nullptr || m_state->m_actPage == 1)
317 continue;
318 m_listener->insertBreak(WPS_PAGE_BREAK);
319 m_graphParser->sendObjects(m_state->m_actPage);
320 }
321 }
322
323 ////////////////////////////////////////////////////////////
324 // interface with the graph/text parser
325 ////////////////////////////////////////////////////////////
326 int WPS4Parser::readObject(RVNGInputStreamPtr input, WPSEntry const &entry)
327 {
328 long actPos = input->tell();
329 int id=m_graphParser->readObject(input, entry);
330 input->seek(actPos, librevenge::RVNG_SEEK_SET);
331 return id;
332 }
333
334 void WPS4Parser::sendObject(WPSPosition const &position, int id)
335 {
336 return m_graphParser->sendObject(position, id);
337 }
338
339 void WPS4Parser::send(WPSEntry const &entry, libwps::SubDocumentType)
340 {
341 if (!entry.hasType("TEXT"))
342 {
343 WPS_DEBUG_MSG(("WPS4Parser::send: unknown entry type '%s'\n", entry.type().c_str()));
344 if (m_listener.get()) m_listener->insertCharacter(' ');
345 return;
346 }
347 RVNGInputStreamPtr input = getInput();
348 long actPos = input->tell();
349 m_textParser->readText(entry);
350 input->seek(actPos, librevenge::RVNG_SEEK_SET);
351 }
352
353 void WPS4Parser::createDocument(WPSEntry const &entry, libwps::SubDocumentType type)
354 {
355 if (m_listener.get() == nullptr) return;
356 WPSSubDocumentPtr subdoc(new WPS4ParserInternal::SubDocument
357 (getInput(), *this, entry));
358 if (type == libwps::DOC_COMMENT_ANNOTATION)
359 m_listener->insertComment(subdoc);
360 else
361 {
362 WPS_DEBUG_MSG(("WPS4Parser:createDocument error: unknown type: \"%d\"\n", int(type)));
363 }
364 }
365
366 void WPS4Parser::createNote(WPSEntry const &entry, librevenge::RVNGString const &label)
367 {
368 if (m_listener.get() == nullptr) return;
369 WPSSubDocumentPtr subdoc(new WPS4ParserInternal::SubDocument
370 (getInput(), *this, entry));
371 m_listener->insertLabelNote(WPSContentListener::FOOTNOTE, label, subdoc);
372 }
373
374 void WPS4Parser::createTextBox(WPSEntry const &entry, WPSPosition const &pos, librevenge::RVNGPropertyList const &extras)
375 {
376 if (m_listener.get() == nullptr) return;
377 WPSSubDocumentPtr subdoc(new WPS4ParserInternal::SubDocument
378 (getInput(), *this, entry));
379 m_listener->insertTextBox(pos, subdoc, extras);
380 }
381
382 ////////////////////////////////////////////////////////////
383 // main funtions to parse a document, its OLE structures and its main input
384 ////////////////////////////////////////////////////////////
385
386 // main function to parse the document
387 void WPS4Parser::parse(librevenge::RVNGTextInterface *documentInterface)
388 {
389 RVNGInputStreamPtr input=getInput();
390 if (!input)
391 {
392 WPS_DEBUG_MSG(("WPS4Parser::parse: does not find main ole\n"));
393 throw (libwps::ParseException());
394 }
395 if (!checkHeader(nullptr, true))
396 throw (libwps::ParseException());
397 try
398 {
399 createOLEStructures();
400 }
401 catch (...)
402 {
403 WPS_DEBUG_MSG(("WPS4Parser::parse: exception catched when parsing secondary OLEs\n"));
404 }
405
406 ascii().setStream(input);
407 ascii().open("MN0");
408 try
409 {
410 createStructures();
411 }
412 catch (...)
413 {
414 WPS_DEBUG_MSG(("WPS4Parser::parse: exception catched when parsing MN0\n"));
415 throw (libwps::ParseException());
416 }
417
418 setListener(createListener(documentInterface));
419 if (!m_listener)
420 {
421 WPS_DEBUG_MSG(("WPS4Parser::parse: can not create the listener\n"));
422 throw (libwps::ParseException());
423 }
424 m_listener->startDocument();
425 m_textParser->sendObjects(-1);
426 WPSEntry ent = m_textParser->getMainTextEntry();
427 if (ent.valid())
428 m_textParser->readText(ent);
429 else
430 {
431 WPS_DEBUG_MSG(("WPS4Parser::parse: can not find main text entry\n"));
432 throw (libwps::ParseException());
433 }
434 #ifdef DEBUG
435 m_textParser->flushExtra();
436 m_graphParser->sendObjects(-1);
437 #endif
438 m_listener->endDocument();
439 m_listener.reset();
440
441 ascii().reset();
442 }
443
444 // find and create all the zones ( normal/ole )
445 bool WPS4Parser::createStructures()
446 {
447 if (!findZones()) throw libwps::ParseException();
448 if (!m_textParser->readStructures()) throw libwps::ParseException();
449 m_graphParser->computePositions();
450
451 #ifdef DEBUG
452 WPS4Parser::NameMultiMap::iterator pos;
453 pos = getNameEntryMap().find("PRNT");
454 if (pos != getNameEntryMap().end()) readPrnt(pos->second);
455 pos = getNameEntryMap().find("DocWInfo");
456 if (pos != getNameEntryMap().end()) readDocWindowsInfo(pos->second);
457 #endif
458
459 return true;
460 }
461
462 bool WPS4Parser::createOLEStructures()
463 {
464 RVNGInputStreamPtr input=getFileInput();
465 if (!input) return false;
466
467 if (!input->isStructured()) return true;
468 WPSOLEParser oleParser("MN0", getDefaultFontType());
469 if (!oleParser.parse(input)) return false;
470
471 m_graphParser->storeObjects(oleParser.getObjectsMap());
472 #ifdef DEBUG
473 // there can remain some embedded Works subdocument ( WKS, ... )
474 // with name MN0 and some unknown picture ole
475 auto unparsed = oleParser.getNotParse();
476
477 for (auto const &name : unparsed)
478 {
479 if (name == "MN0")
480 continue;
481 WPS_DEBUG_MSG(("WPS4Parser::createOLEStructures:: Find unparsed ole: %s\n", name.c_str()));
482
483 #ifdef DEBUG_WITH_FILES
484 RVNGInputStreamPtr ole(input->getSubStreamByName(name.c_str()));
485 if (!ole.get())
486 {
487 WPS_DEBUG_MSG(("WPS4Parser::createOLEStructures: error: can find OLE part: \"%s\"\n", name.c_str()));
488 continue;
489 }
490
491 librevenge::RVNGBinaryData data;
492 if (libwps::readDataToEnd(ole,data))
493 libwps::Debug::dumpFile(data, name.c_str());
494 #endif
495 }
496 #endif
497 return true;
498 }
499
500 ////////////////////////////////////////////////////////////
501 // low level
502 ////////////////////////////////////////////////////////////
503
504 // ------------------------------------------------------------
505 // function to read/check the file header or to check that an entry is ok
506 // ------------------------------------------------------------
507
508 // parse a basic entry: ie offset + size and check if it is valid
509 bool WPS4Parser::parseEntry(std::string const &name)
510 {
511 RVNGInputStreamPtr input = getInput();
512 long actPos = input->tell();
513 WPSEntry zone;
514 zone.setBegin(long(libwps::readU32(input)));
515 zone.setLength(libwps::readU16(input));
516 zone.setType(name);
517
518 bool ok = zone.valid() && checkFilePosition(zone.end());
519
520 if (ok)
521 {
522 getNameEntryMap().insert(NameMultiMap::value_type(zone.type(), zone));
523
524 ascii().addPos(zone.begin());
525 std::string nm = "ZZ";
526 nm+= name;
527 ascii().addNote(nm.c_str());
528 ascii().addPos(zone.end());
529 ascii().addNote("_");
530 }
531
532 libwps::DebugStream f;
533 if (ok)
534 f << "Entries(ZZ"<< name << ")="
535 << std::hex << zone.begin() << "(" << zone.length() << ")";
536 else
537 f << "___";
538 ascii().addPos(actPos);
539 ascii().addNote(f.str().c_str());
540
541 return ok;
542 }
543
544 // basic function to check if the header is ok
545 bool WPS4Parser::checkHeader(WPSHeader *header, bool /*strict*/)
546 {
547 RVNGInputStreamPtr input = getInput();
548 if (!input || !checkFilePosition(0x100))
549 {
550 WPS_DEBUG_MSG(("WPS4Parser::checkHeader: file is too short\n"));
551 return false;
552 }
553
554 /* let's do the strict minimum, we does not want to break old code */
555 input->seek(0x0, librevenge::RVNG_SEEK_SET);
556 uint8_t firstOffset = libwps::readU8(input);
557 uint8_t secondOffset = libwps::readU8(input);
558 if (secondOffset != 0xFE || firstOffset > 7) return false;
559 if (header)
560 {
561 if (firstOffset==1)
562 header->setNeedEncoding(true);
563 header->setMajorVersion(firstOffset<4 ? 2 : firstOffset<6 ? 3 : 4);
564 }
565 return true;
566 }
567
568 // read the document structure ...
569 bool WPS4Parser::findZones()
570 {
571 RVNGInputStreamPtr input = getInput();
572
573 if (!checkFilePosition(0x100))
574 {
575 WPS_DEBUG_MSG(("WPS4Parser::findZones: error: incomplete header\n"));
576 throw libwps::ParseException();
577 }
578
579 input->seek(0x0, librevenge::RVNG_SEEK_SET);
580 libwps::DebugStream f, f2;
581 f << "Entries(ZZHeader):";
582 int vers = libwps::read8(input);
583 long val = libwps::read8(input); // always 0xfe?
584 int apCreator = libwps::readU16(input);
585 int worksVersion = 0;
586 switch (vers)
587 {
588 case 1:
589 m_state->m_isDosFile = true;
590 switch (apCreator)
591 {
592 case 0xda1:
593 apCreator = 2;
594 WPS_FALLTHROUGH;
595 case 0:
596 case 1:
597 worksVersion = 1;
598 f << "vers=dos" << 1+apCreator << ",";
599 apCreator = 0;
600 break;
601 default:
602 // checkme
603 worksVersion= (apCreator<10000) ? 1 : 2;
604 f << "vers=dos3/win2,";
605 break;
606 }
607 break;
608 case 4:
609 worksVersion=3;
610 if (apCreator==0x4e27)
611 {
612 f << "vers=Win3.0,";
613 apCreator = 0;
614 }
615 else
616 f << "vers=Win3,";
617 break;
618 case 6:
619 worksVersion=4;
620 if (apCreator==0x5375)
621 {
622 f << "vers=Win4.0,";
623 apCreator = 0;
624 }
625 else
626 f << "vers=Win4,";
627 break;
628 default:
629 WPS_DEBUG_MSG(("WPS4Parser::findZones: can not read the version\n"));
630 f << "vers=unkn:" << vers << ",";
631 break;
632 }
633 if (worksVersion)
634 setVersion(worksVersion);
635 if (val != -2) f << "##unk=" << val << ",";
636 if (apCreator && apCreator != 0x4755)
637 f << "##apCreator=" << std::hex << apCreator << std::dec << ",";
638
639 f << "unkn1=("; // in general : same number appear two time
640 for (int i = 0; i < 2; i++)
641 f << std::hex << libwps::readU32(input) << ",";
642 f << "),dim?=(";
643 for (int i = 0; i < 2; i++)
644 f << libwps::readU16(input)/1440. << ",";
645 f << "),";
646 auto oem=int(libwps::readU16(input));
647 if ((oem>>4)&0x7ff)
648 {
649 auto type=libwps_tools_win::Font::getTypeForOEM((oem>>4)&0x7ff);
650 if (type != libwps_tools_win::Font::UNKNOWN)
651 {
652 m_state->m_OEMFontType = type;
653 f << "codePage=" << libwps_tools_win::Font::getTypeName(type).cstr() << ",";
654 }
655 else
656 f << "#codePage=" << std::hex << ((oem>>4)&0x7ff) << std::dec << ",";
657 }
658 if (oem & 0x800f) f << "codePage[extra]=" << std::hex << (oem & 0x800f) << std::dec << ",";
659 val=libwps::read16(input);
660 if (val) f << "unkn2=" << val << ",";
661 f << "dim2=" << libwps::read16(input)/1440. << ",";
662 val = libwps::read32(input);
663 if (val) f << std::dec << "unkn3=" << val <<",";
664 ascii().addPos(0);
665 ascii().addNote(f.str().c_str());
666
667 f.str("");
668
669 // 0x1e -> 0x64
670 if (!m_textParser->readEntries()) return false;
671 // 0x64 -> 0x80
672 long actPos = 0x64;
673 input->seek(actPos, librevenge::RVNG_SEEK_SET);
674 readDocDim();
675
676 if (version() <= 1)
677 {
678 // CHECKME:
679 ascii().addPos(0x80);
680 ascii().addNote("ZZHeader-I");
681 ascii().addPos(0xd0);
682 ascii().addNote("ZZHeader-filename");
683 return true;
684 }
685
686 actPos = 0x80;
687 input->seek(actPos, librevenge::RVNG_SEEK_SET);
688 parseEntry("EOBJ");
689
690 actPos = 0x86;
691 input->seek(actPos, librevenge::RVNG_SEEK_SET);
692 f.str("");
693 f << std::hex;
694 // {-1,-1}|{0,0}, 0,0, 0x[08][03][235=2col 6=3col 8=4col], 0|425|720
695 for (int i = 0; i < 4; i++)
696 {
697 val = libwps::readU16(input);
698 if (!val)
699 {
700 f << "_,";
701 continue;
702 }
703 f << val << ",";
704 }
705 f << std::dec;
706 val = libwps::readU16(input);
707 if (val & 0xFF00) f << "#unkn=" << (val >> 8) << ",";
708 val &= 0xFF;
709 auto numCols = int(val/2);
710 if (numCols >= 1 && numCols <= 13)
711 {
712 if (version() >= 3)
713 m_state->m_numColumns = numCols;
714 else
715 {
716 // can this happen ?
717 WPS_DEBUG_MSG(("WPS4Parser::findZones: find some column in dos file: ignored\n"));
718 }
719 if (numCols != 1)
720 {
721 f << "numCols=" << numCols;
722 if (val & 1) f << "[borderLim]";
723 }
724 f << ",";
725 }
726 else if (val)
727 f << "##cols,";
728 int colSep = libwps::readU16(input);
729 if (colSep)
730 f << "colSep=" << colSep/1440. << ",";
731 ascii().addPos(actPos);
732 if (f.str().length())
733 {
734 f2.str("");
735 f2 << "ZZHeader-I(unkn):"<< f.str();
736 ascii().addNote(f2.str().c_str());
737 }
738 else ascii().addNote("___");
739
740 parseEntry("End1");
741
742 actPos = 0x98;
743 input->seek(actPos, librevenge::RVNG_SEEK_SET);
744 f.str("");
745 f << "ZZHeader-II:";
746 bool empty = true;
747 auto begP = long(libwps::readU32(input));
748 if (begP)
749 {
750 if (begP <= 0 || !checkFilePosition(begP)) f << "###";
751 else
752 {
753 ascii().addPos(begP);
754 ascii().addNote("EOText");
755 }
756 f << std::hex << "EOText=" << begP << ",";
757 empty = false;
758 }
759
760 int num = libwps::readU16(input);
761 long sz = libwps::readU16(input);
762 begP = long(libwps::readU32(input));
763 if (begP)
764 {
765 if (begP <= 0 || !checkFilePosition(begP)) f << "###";
766 else if (checkFilePosition(begP+num *sz))
767 {
768 WPSEntry zone;
769 zone.setType("PRNT");
770 for (int i = 0; i < num; i++)
771 {
772 zone.setBegin(begP + i*sz);
773 zone.setLength(sz);
774 zone.setId(i);
775 getNameEntryMap().insert(NameMultiMap::value_type(zone.type(), zone));
776
777 ascii().addPos(zone.begin());
778 std::string nm = "ZZPRNT(";
779 nm+= char('0'+i);
780 nm+=')';
781 ascii().addNote(nm.c_str());
782 ascii().addPos(zone.end());
783 ascii().addNote("_");
784 }
785 }
786 else
787 {
788 ascii().addPos(begP);
789 ascii().addNote("ZZPRNT");
790 }
791
792 f << std::hex << "Entries(ZZPRNT)=" << begP << "(" << sz << ")";
793 if (num != 1) f << "x" << num;
794 empty = false;
795 }
796
797 ascii().addPos(actPos);
798 if (empty) ascii().addNote("___");
799 else ascii().addNote(f.str().c_str());
800
801 parseEntry("DTTM");
802 parseEntry("DocWInfo");
803
804 actPos = 0xb0;
805 input->seek(actPos, librevenge::RVNG_SEEK_SET);
806 f.str("");
807 f << "ZZHeader-III:";
808 empty = true;
809
810 // ok, try to see if we can find other things...
811 int i = 0;
812 while (input->tell()+4 <= 0xd0)
813 {
814 val = long(libwps::readU32(input));
815 if (val)
816 {
817 f << "f" << std::dec << i << "=" << std::hex << val << ",";
818 empty = false;
819 }
820 i++;
821 }
822
823 ascii().addPos(actPos);
824 if (empty) ascii().addNote("___");
825 else ascii().addNote(f.str().c_str());
826
827 // find always a list of 0 here
828 ascii().addPos(0xd0);
829 ascii().addNote("ZZHeader-filename");
830
831 return true;
832 }
833
834 // ------------------------------------------------------------
835 // functions to read the document's dimension or printer structures
836 // ------------------------------------------------------------
837
838 // Read the page format from the file.
839 bool WPS4Parser::readDocDim()
840 {
841 WPSPageSpan page = WPSPageSpan();
842 RVNGInputStreamPtr &input = getInput();
843 input->seek(0x64, librevenge::RVNG_SEEK_SET);
844 long actPos = input->tell();
845
846 libwps::DebugStream f;
847 f << "Entries(DocDim):";
848 double margin[4], size[2];
849 for (double &i : margin) i = libwps::readU16(input)/1440.;
850 for (double &i : size) i = libwps::readU16(input)/1440.;
851
852 if (margin[0]+margin[1] > size[0] || margin[2]+margin[3] > size[1])
853 {
854 WPS_DEBUG_MSG(("WPS4Parser::readDocDim: error: the margins are too large for the page size\n"));
855 return false;
856 }
857 page.setMarginTop(margin[0]);
858 page.setMarginBottom(margin[1]);
859 page.setMarginLeft(margin[2]);
860 // decrease a little right margin if possible
861 double rMargin = margin[3] > 0.4 ? margin[3]-0.2 : 0.5*margin[3];
862 page.setMarginRight(rMargin);
863 page.setFormLength(size[0]);
864 page.setFormWidth(size[1]);
865
866 int unkns[8];
867 for (int &unkn : unkns) unkn = libwps::readU16(input);
868 int page_orientation = unkns[5];
869 if (page_orientation == 0)
870 page.setFormOrientation(WPSPageSpan::PORTRAIT);
871 else if (page_orientation == 1)
872 page.setFormOrientation(WPSPageSpan::LANDSCAPE);
873 else
874 {
875 WPS_DEBUG_MSG(("WPS4Parser::readDocDim: error: bad page orientation code\n"));
876 }
877 m_state->m_pageSpan = page;
878
879 f << "margin=("<< margin[2] << "x" << margin[0]
880 << ", " << margin[3] << "x" << margin[1] << "), ";
881 f << "size=" << size[1] << "x" << size[0] << ",";
882 if (page_orientation) f << "orien=" << page_orientation << ",";
883 if (unkns[0]!=1) f << "firstPage=" << unkns[0] << ",";
884 if (unkns[1]==1)
885 {
886 m_state->m_noFirstPageHeader=true;
887 f << "noPage1Header,";
888 }
889 else if (unkns[1]) f << "#noPage1Header=" << unkns[1] << ",";
890 if (unkns[2]==1)
891 {
892 m_state->m_noFirstPageHeader=true;
893 f << "noPage1Footer,";
894 }
895 else if (unkns[2]) f << "#noPage1Footer=" << unkns[2] << ",";
896 f << "headerH=" << unkns[3]/1440. << ",";
897 f << "footerH=" << unkns[4]/1440. << ",";
898 if (unkns[6] != 100) f << "zoom=" << unkns[6] << "%,";
899 if (unkns[7]) f << "#unkn=" << unkns[7] << ","; // alway 0 ?
900 ascii().addPos(actPos);
901 ascii().addNote(f.str().c_str());
902 return true;
903 }
904
905 // PRNT : the printer definition
906 bool WPS4Parser::readPrnt(WPSEntry const &entry)
907 {
908 if (!entry.valid()) return false;
909
910 RVNGInputStreamPtr &input = getInput();
911 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
912 libwps::DebugStream f;
913
914 long length = entry.length();
915 if (length < 0x174)
916 {
917 WPS_DEBUG_MSG(("WPS4Parser::readPrnt: length::=%ld is to short\n", length));
918 return false;
919 }
920 f << std::hex;
921
922 for (int st = 0; st < 2; st++)
923 {
924 float dim[8];
925 for (int i = 0; i < 8; i++)
926 {
927 if (i == 4 || i == 5)
928 dim[i] = float(libwps::readU32(input)/1440.);
929 else
930 dim[i] = float(libwps::read32(input)/1440.);
931 }
932 f << "dim"<< st << "=" << dim[5] << "x" << dim[4] << ",";
933 f << "margin"<< st << "=[" << dim[0] << "x" << dim[2] << ","
934 << dim[3] << "x" << dim[1] << "],";
935 f << "head/foot??"<< st << "=" << dim[6] << "x" << dim[7] << ",";
936 }
937
938 f << std::dec;
939 long val;
940
941 /* I find f0=1, f1=0|1, f2=0|1, f4=100, f5=0|15, f23=0|372
942 Note: f23=372 and strLen!=0 only when f1=1 and f2=1 ( probably related)
943 */
944 for (int i = 0; i < 24; i++)
945 {
946 val = libwps::read32(input);
947 if (val) f << "f" << i << "=" << val << ",";
948 }
949 val = libwps::read32(input);
950 if (val) f << "strLen?=" << val << ",";
951
952 /* I only find 0 here */
953 for (int i = 0; i < 52; i++)
954 {
955 val = libwps::read32(input);
956 if (val) f << "g" << i << "=" << val << ",";
957 }
958
959 ascii().addPos(entry.begin());
960 ascii().addNote(f.str().c_str());
961
962 length -= 372;
963
964 if (length)
965 {
966 /* In my files, I find one time a strLen string: a header/footer? */
967 ascii().addPos(input->tell());
968 f.str("");
969 f << "ZZPRNT(II):";
970 for (long i = 0; i < length; i++)
971 f << char(libwps::readU8(input));
972 ascii().addNote(f.str().c_str());
973 }
974 return true;
975 }
976
977 // Link : in Works4 seems to contain data corresponding to the page border, ...
978 bool WPS4Parser::readDocWindowsInfo(WPSEntry const &entry)
979 {
980 if (!entry.valid()) return false;
981
982 RVNGInputStreamPtr &input = getInput();
983 long length = entry.length();
984 if (length < 0x154)
985 {
986 WPS_DEBUG_MSG(("WPS4Parser::readDocWindowsInfo: length::=%ld is to short\n", length));
987 return false;
988 }
989
990 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
991 libwps::DebugStream f;
992
993 std::string str("");
994 int pos = 0, debSPos = -1;
995 // Find in one file str[0]="C:\Databases\Elem 02-03.wdb",str[120]="Query1"
996 while (pos++ < 0x132) // try to find some strings
997 {
998 auto c = char(libwps::read8(input));
999 if (c == '\0')
1000 {
1001 if (debSPos>=0)
1002 f << "str[" << std::hex << debSPos << "]=\"" << str << "\",";
1003 debSPos = -1;
1004 str = std::string("");
1005 continue;
1006 }
1007
1008 if (debSPos < 0) debSPos = pos-1;
1009 str += c;
1010 }
1011 if (debSPos>=0)
1012 f << "str[" << debSPos << "]=\"" << str << "\",";
1013 ascii().addPos(entry.begin());
1014 ascii().addNote(f.str().c_str());
1015 f.str("");
1016
1017
1018 input->seek(entry.begin()+0x132, librevenge::RVNG_SEEK_SET);
1019 f << "ZZDocWInfo(II):" << std::hex;
1020 // f0=f1=-1 in one file, f0=f1=0 in another file
1021 // f0=e6|1b0|2d0 ( but 2d0 in 2/3 of the files), 100<f1<438
1022 for (int i = 0; i < 2; i++)
1023 {
1024 long val = libwps::read32(input);
1025 if (val)
1026 f << "f" << i << "=" << val << ",";
1027 }
1028
1029 int dim[2]; // TB, LR
1030 for (int &i : dim) i = libwps::read16(input);
1031 if (dim[0] || dim[1])
1032 f << "pageBorderDist=" << dim[1]/1440. << "x" << dim[0]/1440. << ",";
1033 long val = libwps::readU8(input);
1034 WPSColor color;
1035 if (val && getColor(int(val), color))
1036 f << "pageBorderColor=" << color << ",";
1037 else if (val)
1038 f << "#pageBorderColor=" << std::hex << val << std::dec << ",";
1039 val = libwps::readU8(input);
1040 if (val)
1041 f << "pageBorderStyle=" << val << ",";
1042 val = long(libwps::readU32(input));
1043 if (val&2)
1044 f << "pageBorderShaded,";
1045 if (val&1)
1046 f << "firstPageBorder,";
1047 val &= 0xFFFFFFFCL;
1048 if (val)
1049 f << "#pageBorder?=" << std::hex << val << std::dec << ",";
1050
1051 // in one file: nothing ( case when f0=f1=-1)
1052 //
1053 // h0-h1: seems to correspond to a text zone
1054 for (int i = 0; i < 4; i++)
1055 {
1056 long val_ = libwps::read32(input);
1057 if (i == 0)
1058 {
1059 f << "textpos?=[" << 0x100+val_<< ",";
1060 continue;
1061 }
1062 if (i == 1)
1063 {
1064 f << 0x100+val_ << "],";
1065 continue;
1066 }
1067 if (val_)
1068 f << "h" << i << "=" << val_ << ",";
1069 }
1070
1071 ascii().addPos(entry.begin()+0x132);
1072 ascii().addNote(f.str().c_str());
1073
1074 if (input->tell() != entry.end())
1075 {
1076 // can this happens ?
1077 ascii().addPos(input->tell());
1078 ascii().addNote("ZZDocWInfo(III)");
1079 }
1080 return true;
1081 }
1082
1083 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */