Debian Codegraph @

src/lib/Quattro.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 <algorithm>
26 #include <cmath>
27 #include <sstream>
28 #include <stack>
29 #include <utility>
30
31 #include <librevenge-stream/librevenge-stream.h>
32
33 #include "libwps_internal.h"
34 #include "libwps_tools_win.h"
35
36 #include "WKSContentListener.h"
37 #include "WKSSubDocument.h"
38
39 #include "WPSCell.h"
40 #include "WPSEntry.h"
41 #include "WPSFont.h"
42 #include "WPSHeader.h"
43 #include "WPSOLEParser.h"
44 #include "WPSOLEStream.h"
45 #include "WPSPageSpan.h"
46 #include "WPSStream.h"
47 #include "WPSStringStream.h"
48
49 //#include "QuattroChart.h"
50 #include "QuattroFormula.h"
51 #include "QuattroGraph.h"
52 #include "QuattroSpreadsheet.h"
53
54 #include "Quattro.h"
55
56 using namespace libwps;
57
58 //! Internal: namespace to define internal class of QuattroParser
59 namespace QuattroParserInternal
60 {
61 //! the font of a QuattroParser
62 struct Font final : public WPSFont
63 {
64 //! constructor
65 explicit Font(libwps_tools_win::Font::Type type) : WPSFont(), m_type(type)
66 {
67 }
68 Font(Font const &)=default;
69 //! destructor
70 ~Font() final;
71 //! font encoding type
72 libwps_tools_win::Font::Type m_type;
73 };
74
75 Font::~Font()
76 {
77 }
78
79 //! Internal: the subdocument of a WPS4Parser
80 class SubDocument final : public WKSSubDocument
81 {
82 public:
83 //! constructor for a text entry
84 SubDocument(RVNGInputStreamPtr const &input, QuattroParser &pars, bool header) :
85 WKSSubDocument(input, &pars), m_header(header) {}
86 //! destructor
87 ~SubDocument() final {}
88
89 //! operator==
90 bool operator==(std::shared_ptr<WPSSubDocument> const &doc) const final
91 {
92 if (!doc || !WKSSubDocument::operator==(doc))
93 return false;
94 auto const *sDoc = dynamic_cast<SubDocument const *>(doc.get());
95 if (!sDoc) return false;
96 return m_header == sDoc->m_header;
97 }
98
99 //! the parser function
100 void parse(std::shared_ptr<WKSContentListener> &listener, libwps::SubDocumentType subDocumentType) final;
101 //! a flag to known if we need to send the header or the footer
102 bool m_header;
103 };
104
105 void SubDocument::parse(std::shared_ptr<WKSContentListener> &listener, libwps::SubDocumentType)
106 {
107 if (!listener.get())
108 {
109 WPS_DEBUG_MSG(("QuattroParserInternal::SubDocument::parse: no listener\n"));
110 return;
111 }
112 if (!dynamic_cast<WKSContentListener *>(listener.get()))
113 {
114 WPS_DEBUG_MSG(("QuattroParserInternal::SubDocument::parse: bad listener\n"));
115 return;
116 }
117
118 QuattroParser *pser = m_parser ? dynamic_cast<QuattroParser *>(m_parser) : nullptr;
119 if (!pser)
120 {
121 listener->insertCharacter(' ');
122 WPS_DEBUG_MSG(("QuattroParserInternal::SubDocument::parse: bad parser\n"));
123 return;
124 }
125 pser->sendHeaderFooter(m_header);
126 }
127
128 //! a zone name
129 struct ZoneName
130 {
131 //! constructor
132 explicit ZoneName(char const *name, char const *extra=nullptr)
133 : m_name(name)
134 , m_extra(extra==nullptr ? "" : extra)
135 {
136 }
137 //! the zone name
138 std::string m_name;
139 //! the extra data
140 std::string m_extra;
141 };
142
143 //! the state of QuattroParser
144 struct State
145 {
146 //! constructor
147 explicit State(libwps_tools_win::Font::Type fontType, char const *password)
148 : m_fontType(fontType)
149 , m_version(-1)
150 , m_metaData()
151 , m_actualSheet(-1)
152 , m_fontsList()
153 , m_colorsList()
154 , m_idToExternalFileMap()
155 , m_idToExternalNameMap()
156 , m_idToFieldMap()
157 , m_pageSpan()
158 , m_actPage(0)
159 , m_numPages(0)
160 , m_headerString()
161 , m_footerString()
162 , m_password(password)
163 , m_isEncrypted(false)
164 , m_isDecoded(false)
165 , m_idToZoneNameMap()
166 , m_readingZone341(false)
167 {
168 }
169 //! return the default font style
170 libwps_tools_win::Font::Type getDefaultFontType() const
171 {
172 if (m_fontType != libwps_tools_win::Font::UNKNOWN)
173 return m_fontType;
174 return libwps_tools_win::Font::WIN3_WEUROPE;
175 }
176
177 //! returns a color corresponding to an id
178 bool getColor(int id, WPSColor &color) const;
179 //! returns a default font (Courier12) with file's version to define the default encoding */
180 static WPSFont getDefaultFont()
181 {
182 WPSFont res;
183 res.m_name="Times New Roman";
184 res.m_size=12;
185 return res;
186 }
187 //! init the zone name map
188 void initZoneNameMap();
189 //! the user font type
190 libwps_tools_win::Font::Type m_fontType;
191 //! the file version
192 int m_version;
193 //! the meta data
194 librevenge::RVNGPropertyList m_metaData;
195 //! the actual sheet
196 int m_actualSheet;
197 //! the font list
198 std::vector<Font> m_fontsList;
199 //! the color list
200 std::vector<WPSColor> m_colorsList;
201 //! map id to filename
202 std::map<int, librevenge::RVNGString> m_idToExternalFileMap;
203 //! map id to external name
204 std::map<int, librevenge::RVNGString> m_idToExternalNameMap;
205 //! map id to field
206 std::map<int, std::pair<librevenge::RVNGString,QuattroFormulaInternal::CellReference> >m_idToFieldMap;
207 //! the actual document size
208 WPSPageSpan m_pageSpan;
209 int m_actPage /** the actual page*/, m_numPages /* the number of pages */;
210 //! the header string
211 librevenge::RVNGString m_headerString;
212 //! the footer string
213 librevenge::RVNGString m_footerString;
214 //! the password (if known)
215 char const *m_password;
216 //! true if the file is encrypted
217 bool m_isEncrypted;
218 //! true if the main stream has been decoded
219 bool m_isDecoded;
220 //! map zone id to zone name
221 std::map<int, ZoneName> m_idToZoneNameMap;
222 //! true if zone 341 is being read. Avoids recursion.
223 bool m_readingZone341;
224 private:
225 State(State const &)=delete;
226 State &operator=(State const &)=delete;
227 };
228
229 bool State::getColor(int id, WPSColor &color) const
230 {
231 if (m_colorsList.empty())
232 {
233 auto *THIS=const_cast<State *>(this);
234 static const uint32_t quattroColorMap[]=
235 {
236 0xFFFFFF, 0xC0C0C0, 0x808080, 0x000000,
237 0xFF0000, 0x00FF00, 0x0000FF, 0x00FFFF,
238 0xFF00FF, 0xFFFF00, 0x800080, 0x000080,
239 0x808000, 0x008000, 0x800000, 0x008080
240 };
241 for (uint32_t i : quattroColorMap)
242 THIS->m_colorsList.push_back(WPSColor(i));
243 }
244 if (id < 0 || id >= int(m_colorsList.size()))
245 {
246 WPS_DEBUG_MSG(("QuattroParserInternal::State::getColor(): unknown Quattro Pro color id: %d\n",id));
247 return false;
248 }
249 color = m_colorsList[size_t(id)];
250 return true;
251 }
252
253 void State::initZoneNameMap()
254 {
255 if (!m_idToZoneNameMap.empty())
256 return;
257 m_idToZoneNameMap=std::map<int,ZoneName>
258 {
259 { 0x0, ZoneName("File", "begin") },
260 { 0x1, ZoneName("File", "end") },
261 { 0x2, ZoneName("Recalculation", "mode") },
262 { 0x3, ZoneName("Recalculation", "order") },
263 { 0x6, ZoneName("Sheet", "size") },
264 { 0xb, ZoneName("FldName") },
265 { 0xc, ZoneName("Cell", "blank") },
266 { 0xd, ZoneName("Cell", "int") },
267 { 0xe, ZoneName("Cell", "float") },
268 { 0xf, ZoneName("Cell", "label") },
269 { 0x10, ZoneName("Cell", "formula") },
270 { 0x18, ZoneName("Range", "table") },
271 { 0x19, ZoneName("Range", "query") },
272 { 0x1a, ZoneName("Print", "block") },
273 { 0x1b, ZoneName("Range", "sort,block") },
274 { 0x1c, ZoneName("Range", "fill") },
275 { 0x1d, ZoneName("Range", "sort,firstKey") },
276 { 0x20, ZoneName("Range", "frequency") },
277 { 0x23, ZoneName("Range", "sort,secondKey") },
278 { 0x24, ZoneName("Protection") },
279 { 0x25, ZoneName("Print", "footer") },
280 { 0x26, ZoneName("Print", "header") },
281 { 0x27, ZoneName("Print", "setup") },
282 { 0x28, ZoneName("Print", "margins") },
283 { 0x2f, ZoneName("Recalculation", "iteration,count") },
284 { 0x30, ZoneName("Print", "pagebreak") },
285 { 0x33, ZoneName("Cell", "string,value") },
286 { 0x4b, ZoneName("Password", "data") },
287 { 0x4c, ZoneName("Password", "level") },
288 { 0x4d, ZoneName("System", "property") },
289 { 0x66, ZoneName("Range", "parse") },
290 { 0x67, ZoneName("Range", "regression") },
291 { 0x69, ZoneName("Range", "matrix") },
292 { 0x96, ZoneName("Column", "size") },
293 { 0x97, ZoneName("External", "link") },
294 { 0x98, ZoneName("External", "name") },
295 { 0x99, ZoneName("Macro", "library") },
296 // 0x9d Cell style (never seen)
297 // 0x9e Conditional border color (never seen)
298 { 0x9f, ZoneName("Range", "sort,thirdKey") },
299 { 0xa0, ZoneName("Range", "sort,fourstKey") },
300 { 0xa1, ZoneName("Range", "sort,fifthKey") },
301 { 0xb7, ZoneName("Range", "solve for") },
302 { 0xc9, ZoneName("Version") },
303 { 0xca, ZoneName("Sheet", "begin") },
304 { 0xcb, ZoneName("Sheet", "end") },
305 { 0xcc, ZoneName("Sheet", "name") },
306 { 0xce, ZoneName("Cell", "style") },
307 { 0xcf, ZoneName("FontDef") },
308 { 0xd0, ZoneName("StyleName") },
309 { 0xd1, ZoneName("Sheet", "attribute") },
310 { 0xd2, ZoneName("Pane", "row,default") },
311 { 0xd3, ZoneName("Pane", "row2,default") },
312 { 0xd4, ZoneName("Pane", "col,default") },
313 { 0xd5, ZoneName("Pane", "col2,default") },
314 { 0xd6, ZoneName("Pane", "row") },
315 { 0xd7, ZoneName("Pane", "row2") },
316 { 0xd8, ZoneName("Pane", "col") },
317 { 0xd9, ZoneName("Pane", "col2") },
318 { 0xda, ZoneName("Pane", "font,max") },
319 { 0xdb, ZoneName("Pane", "font2,max") },
320 { 0xdc, ZoneName("Pane", "row,hidden") },
321 { 0xdd, ZoneName("Pane", "row2,hidden") },
322 { 0xde, ZoneName("Pane", "col,hidden") },
323 { 0xdf, ZoneName("Pane", "col2,hidden") },
324 { 0xe0, ZoneName("Pane", "style") },
325 { 0xe1, ZoneName("Pane", "style2") },
326 { 0xe2, ZoneName("PageGroup", "on") },
327 { 0xe3, ZoneName("PageGroup") },
328 { 0xe4, ZoneName("DLLIdFunct", "e5") },
329 { 0xe5, ZoneName("DLLIdFunct", "e6") },
330 { 0xe6, ZoneName("UserFormat") },
331 { 0xe7, ZoneName("Column", "def,attr") },
332 { 0xe8, ZoneName("ColorList") },
333 { 0xe9, ZoneName("Collection") }, // list of cell reference data, never seens
334 { 0xed, ZoneName("Print", "beg,names") },
335 { 0xee, ZoneName("Print", "formula") },
336 { 0xef, ZoneName("Print", "block,delimiter") },
337 { 0xf0, ZoneName("Print", "page,delimiter") },
338 { 0xf1, ZoneName("Print", "copies") },
339 { 0xf2, ZoneName("Print", "pages") },
340 { 0xf3, ZoneName("Print", "density") },
341 { 0xf4, ZoneName("Print", "tofit") },
342 { 0xf5, ZoneName("Print", "scaling") },
343 { 0xf6, ZoneName("Print", "paper,type") },
344 { 0xf7, ZoneName("Print", "orientation") },
345 { 0xf8, ZoneName("Print", "left,border") },
346 { 0xf9, ZoneName("Print", "top,border") },
347 { 0xfa, ZoneName("Print", "center,blocks") },
348 { 0xfb, ZoneName("Print", "end") },
349 { 0xfc, ZoneName("Print", "header,font") },
350
351 { 0x101, ZoneName("Print", "headings") },
352 { 0x102, ZoneName("Print", "gridlines") },
353 { 0x103, ZoneName("Optimizer") },
354 { 0x104, ZoneName("Optimizer", "constraint") },
355 { 0x105, ZoneName("Pane", "row,range") },
356 { 0x106, ZoneName("Pane", "row2,range") },
357 { 0x107, ZoneName("Pane", "font,max,range") },
358 { 0x108, ZoneName("Pane", "font2,max,range") },
359 { 0x109, ZoneName("Print", "beg,record") },
360 { 0x10a, ZoneName("Print", "beg,graph") },
361 { 0x10c, ZoneName("Print", "draft,margins") },
362 { 0x10d, ZoneName("Show", "compatible") },
363 { 0x110, ZoneName("Print", "footer,font") },
364 { 0x111, ZoneName("Print", "area") },
365
366 { 0x12e, ZoneName("Object", "number") },
367 { 0x12f, ZoneName("Query", "table,command") },
368 { 0x132, ZoneName("Formula", "compile") },
369 { 0x133, ZoneName("Formula", "audit") },
370 { 0x134, ZoneName("Sheet", "tab,color") },
371 { 0x135, ZoneName("Sheet", "zoom") },
372 { 0x136, ZoneName("Show", "notebook,object") },
373 { 0x137, ZoneName("Sheet", "protection") },
374
375 { 0x154, ZoneName("UserFormat", "complete") },
376
377 { 0x191, ZoneName("View", "begin") },
378 { 0x192, ZoneName("View", "end") },
379 { 0x193, ZoneName("View", "window") },
380 { 0x194, ZoneName("View", "location") },
381 { 0x195, ZoneName("View", "split") },
382 { 0x196, ZoneName("View", "synchronize") },
383 { 0x197, ZoneName("View", "pane,info") },
384 { 0x198, ZoneName("View", "pane2,info") },
385 { 0x199, ZoneName("View", "page") },
386 { 0x19a, ZoneName("View", "page2") },
387 { 0x19b, ZoneName("View", "current") },
388 { 0x19c, ZoneName("View", "display,settings") },
389 { 0x19e, ZoneName("View", "zoom") },
390
391 { 0x259, ZoneName("Graph", "begin,name") },
392 { 0x25a, ZoneName("Graph", "end") },
393 { 0x25d, ZoneName("Graph", "icon,coord") },
394 { 0x25e, ZoneName("Slide", "begin") },
395 { 0x25f, ZoneName("Slide", "end") },
396 { 0x260, ZoneName("Slide", "icon,coord") },
397 { 0x262, ZoneName("Slide", "time") },
398 { 0x263, ZoneName("Slide", "effect0") },
399 { 0x264, ZoneName("Graph", "version") },
400 { 0x265, ZoneName("Slide", "speed") },
401 { 0x266, ZoneName("Slide", "effect1") },
402 { 0x267, ZoneName("Slide", "level") },
403 { 0x26a, ZoneName("Slide", "type") },
404 { 0x26b, ZoneName("Slide", "comment") },
405 { 0x26c, ZoneName("Slide", "master,name") },
406 { 0x2bc, ZoneName("Graph", "beg,record") },
407 { 0x2bd, ZoneName("Chart", "beg,serie") },
408 { 0x2be, ZoneName("Chart", "end,serie") },
409 { 0x2bf, ZoneName("Serie", "Xlabel") },
410 { 0x2c0, ZoneName("Serie", "Zlabel") },
411 { 0x2c1, ZoneName("Serie", "legend") },
412 { 0x2c2, ZoneName("Serie", "number") },
413 { 0x2c3, ZoneName("Serie", "beg,data") },
414 { 0x2c4, ZoneName("Serie", "end,data") },
415 { 0x2c6, ZoneName("Serie", "data") },
416 { 0x2c7, ZoneName("Serie", "label") },
417 { 0x2c8, ZoneName("Serie", "legend") },
418 { 0x2c9, ZoneName("Chart", "beg,record") },
419 { 0x2ca, ZoneName("Chart", "end,record") },
420 { 0x2cb, ZoneName("Graph", "extension") },
421 { 0x2cd, ZoneName("Chart", "beg,save") },
422 { 0x2ce, ZoneName("Chart", "end,save") },
423 { 0x2db, ZoneName("Graph", "display,order") },
424 { 0x2dc, ZoneName("Serie", "extension") },
425
426 { 0x31f, ZoneName("Graph", "end,record") },
427 { 0x321, ZoneName("Object", "begin") },
428 { 0x322, ZoneName("Object", "end") },
429
430 { 0x335, ZoneName("GrDialog","textbox")},
431 { 0x337, ZoneName("GrDialog","37")},
432 { 0x33e, ZoneName("GrRect", "circle")},
433 { 0x33f, ZoneName("GrDialog","3f")},
434 { 0x342, ZoneName("GrDialog","42")},
435 { 0x343, ZoneName("GrDialog","button")},
436 { 0x345, ZoneName("GrDialog","bitmap")},
437 { 0x349, ZoneName("GrDialog","49")},
438 { 0x34a, ZoneName("GrDialog","4a")},
439 { 0x34e, ZoneName("GrDialog","4e")},
440 { 0x34f, ZoneName("GrDialog","4f")},
441 { 0x351, ZoneName("GrDialog","51")},
442 { 0x35a, ZoneName("GrLine")},
443 { 0x35b, ZoneName("GrPolygon")},
444 { 0x35c, ZoneName("GrPolygon","line")},
445 { 0x35d, ZoneName("GrDialog","5d")},
446 { 0x35e, ZoneName("GrDialog","main")},
447 { 0x364, ZoneName("GrRect")},
448 { 0x36d, ZoneName("GrDialog","6d")},
449 { 0x36f, ZoneName("GrTextBox")},
450 { 0x379, ZoneName("GrRect", "round")},
451 { 0x37b, ZoneName("GrLine", "arrow")},
452 { 0x37c, ZoneName("GrPolygon","line,bezier")},
453
454 { 0x381, ZoneName("Object", "frame,ole") },
455 { 0x382, ZoneName("Object", "image") },
456 { 0x383, ZoneName("Object", "bitmap") },
457 { 0x384, ZoneName("Object", "chart") },
458 { 0x385, ZoneName("Object", "frame") },
459 { 0x386, ZoneName("Object", "button") },
460 { 0x388, ZoneName("GrPolygon","bezier")},
461 { 0x38b, ZoneName("Object", "ole") },
462
463 { 0x4d3, ZoneName("Object", "shape") }
464 };
465 }
466
467 }
468
469 // constructor, destructor
470 QuattroParser::QuattroParser(RVNGInputStreamPtr &input, WPSHeaderPtr &header,
471 libwps_tools_win::Font::Type encoding, char const *password)
472 : WKSParser(input, header)
473 , m_listener()
474 , m_state(new QuattroParserInternal::State(encoding, password))
475 //, m_chartParser(new QuattroChart(*this))
476 , m_graphParser(new QuattroGraph(*this))
477 , m_spreadsheetParser(new QuattroSpreadsheet(*this))
478 {
479 }
480
481 QuattroParser::~QuattroParser()
482 {
483 }
484
485 int QuattroParser::version() const
486 {
487 return m_state->m_version;
488 }
489
490 libwps_tools_win::Font::Type QuattroParser::getDefaultFontType() const
491 {
492 return m_state->getDefaultFontType();
493 }
494
495 bool QuattroParser::getExternalFileName(int fId, librevenge::RVNGString &fName) const
496 {
497 auto it = m_state->m_idToExternalFileMap.find(fId);
498 if (it!=m_state->m_idToExternalFileMap.end())
499 {
500 fName=it->second;
501 return true;
502 }
503 WPS_DEBUG_MSG(("QuattroParser::getExternalFileName: can not find %d name\n", fId));
504 return false;
505 }
506
507 bool QuattroParser::getField(int fId, librevenge::RVNGString &text,
508 QuattroFormulaInternal::CellReference &ref,
509 librevenge::RVNGString const &fileName) const
510 {
511 ref.m_cells.clear();
512 if (fId&0x4000)
513 {
514 auto it=m_state->m_idToExternalNameMap.find(fId & 0xbfff);
515 if (it!=m_state->m_idToExternalNameMap.end() && !it->second.empty())
516 {
517 text=it->second;
518 WKSContentListener::FormulaInstruction instr;
519 instr.m_type=instr.F_Text;
520 if (!fileName.empty())
521 {
522 instr.m_content="[";
523 instr.m_content+=fileName.cstr();
524 instr.m_content+="]";
525 }
526 instr.m_content+=it->second.cstr();
527 ref.addInstruction(instr);
528 return true;
529 }
530 WPS_DEBUG_MSG(("QuattroParser::getField: can not find %d name\n", fId&0xbfff));
531 return false;
532 }
533 auto it = m_state->m_idToFieldMap.find(fId);
534 if (it!=m_state->m_idToFieldMap.end())
535 {
536 text=it->second.first;
537 ref=it->second.second;
538 if (!fileName.empty()) // unsure
539 {
540 for (auto &r : ref.m_cells)
541 {
542 if (r.m_type==r.F_Cell || r.m_type==r.F_CellList)
543 r.m_fileName=fileName;
544 }
545 }
546 return true;
547 }
548 WPS_DEBUG_MSG(("QuattroParser::getField: can not find %d field\n", fId));
549 return false;
550 }
551
552 //////////////////////////////////////////////////////////////////////
553 // interface with QuattroGraph
554 //////////////////////////////////////////////////////////////////////
555
556 bool QuattroParser::sendPageGraphics(int sheetId) const
557 {
558 return m_graphParser->sendPageGraphics(sheetId);
559 }
560
561 bool QuattroParser::sendGraphics(int sheetId, Vec2i const &cell) const
562 {
563 return m_graphParser->sendGraphics(sheetId, cell);
564 }
565
566
567 //////////////////////////////////////////////////////////////////////
568 // interface with QuattroSpreadsheet
569 //////////////////////////////////////////////////////////////////////
570
571 Vec2f QuattroParser::getCellPosition(int sheetId, Vec2i const &cell) const
572 {
573 return m_spreadsheetParser->getPosition(sheetId, cell);
574 }
575
576 bool QuattroParser::getColor(int id, WPSColor &color) const
577 {
578 return m_state->getColor(id, color);
579 }
580
581 bool QuattroParser::getFont(int id, WPSFont &font, libwps_tools_win::Font::Type &type) const
582 {
583 if (id < 0 || id>=int(m_state->m_fontsList.size()))
584 {
585 WPS_DEBUG_MSG(("QuattroParser::getFont: can not find font %d\n", id));
586 return false;
587 }
588 auto const &ft=m_state->m_fontsList[size_t(id)];
589 font=ft;
590 type=ft.m_type;
591 return true;
592 }
593
594 // main function to parse the document
595 void QuattroParser::parse(librevenge::RVNGSpreadsheetInterface *documentInterface)
596 {
597 RVNGInputStreamPtr input=getInput();
598 if (!input)
599 {
600 WPS_DEBUG_MSG(("QuattroParser::parse: does not find main ole\n"));
601 throw (libwps::ParseException());
602 }
603
604 if (!checkHeader(nullptr)) throw(libwps::ParseException());
605
606 bool ok=false;
607 try
608 {
609 ascii().setStream(input);
610 ascii().open("MN0");
611 if (checkHeader(nullptr) && readZones())
612 m_listener=createListener(documentInterface);
613 if (m_listener)
614 {
615 //m_chartParser->setListener(m_listener);
616 m_graphParser->setListener(m_listener);
617 m_spreadsheetParser->setListener(m_listener);
618
619 m_graphParser->updateState();
620 m_spreadsheetParser->updateState();
621
622 m_listener->startDocument();
623 int numSheet=m_spreadsheetParser->getNumSpreadsheets();
624 if (numSheet==0) ++numSheet;
625 for (int i=0; i<numSheet; ++i)
626 m_spreadsheetParser->sendSpreadsheet(i, m_graphParser->getGraphicCellsInSheet(i));
627 m_listener->endDocument();
628 m_listener.reset();
629 ok = true;
630 }
631 }
632 catch (...)
633 {
634 WPS_DEBUG_MSG(("QuattroParser::parse: exception catched when parsing MN0\n"));
635 throw (libwps::ParseException());
636 }
637
638 ascii().reset();
639 if (!ok)
640 throw(libwps::ParseException());
641 }
642
643 std::shared_ptr<WKSContentListener> QuattroParser::createListener(librevenge::RVNGSpreadsheetInterface *interface)
644 {
645 std::vector<WPSPageSpan> pageList;
646 WPSPageSpan ps(m_state->m_pageSpan);
647 int numSheet=m_spreadsheetParser->getNumSpreadsheets();
648 if (numSheet<=0) numSheet=1;
649 if (!m_state->m_headerString.empty())
650 {
651 WPSSubDocumentPtr subdoc(new QuattroParserInternal::SubDocument
652 (getInput(), *this, true));
653 ps.setHeaderFooter(WPSPageSpan::HEADER, WPSPageSpan::ALL, subdoc);
654 }
655 if (!m_state->m_footerString.empty())
656 {
657 WPSSubDocumentPtr subdoc(new QuattroParserInternal::SubDocument
658 (getInput(), *this, false));
659 ps.setHeaderFooter(WPSPageSpan::FOOTER, WPSPageSpan::ALL, subdoc);
660 }
661 ps.setPageSpan(numSheet);
662 pageList.push_back(ps);
663 auto listener=std::make_shared<WKSContentListener>(pageList, interface);
664 listener->setMetaData(m_state->m_metaData);
665 return listener;
666 }
667
668 ////////////////////////////////////////////////////////////
669 // low level
670 ////////////////////////////////////////////////////////////
671 // read the header
672 ////////////////////////////////////////////////////////////
673 bool QuattroParser::checkHeader(WPSHeader *header, bool strict)
674 {
675 m_state.reset(new QuattroParserInternal::State(m_state->m_fontType, m_state->m_password));
676 auto input=getInput();
677 auto mainStream=std::make_shared<WPSStream>(input, ascii());
678 if (!mainStream || !checkHeader(mainStream, strict))
679 return false;
680 if (header)
681 {
682 header->setMajorVersion(m_state->m_version);
683 header->setCreator(libwps::WPS_QUATTRO_PRO);
684 header->setKind(libwps::WPS_SPREADSHEET);
685 header->setIsEncrypted(m_state->m_isEncrypted);
686 header->setNeedEncoding(true);
687 }
688 return true;
689 }
690
691 bool QuattroParser::checkHeader(std::shared_ptr<WPSStream> stream, bool strict)
692 {
693 if (!stream)
694 {
695 WPS_DEBUG_MSG(("QuattroParser::checkHeader: called without stream\n"));
696 return false;
697 }
698
699 RVNGInputStreamPtr input = stream->m_input;
700 libwps::DebugFile &ascFile=stream->m_ascii;
701 libwps::DebugStream f;
702
703 if (!input || !stream->checkFilePosition(12))
704 {
705 WPS_DEBUG_MSG(("QuattroParser::checkHeader: file is too short\n"));
706 return false;
707 }
708
709 input->seek(0,librevenge::RVNG_SEEK_SET);
710 auto firstOffset = int(libwps::readU8(input));
711 auto type = int(libwps::read8(input));
712 f << "FileHeader:";
713 if (firstOffset == 0 && type == 0)
714 m_state->m_version=1000;
715 else
716 {
717 WPS_DEBUG_MSG(("QuattroParser::checkHeader: find unexpected first data\n"));
718 return false;
719 }
720 auto val=int(libwps::read16(input));
721 if (val==2)
722 {
723 // version
724 val=int(libwps::readU16(input));
725 if (val==0x1001)
726 {
727 m_state->m_version=1001;
728 f << "quattropro[wb1],";
729 }
730 else if (val==0x1002)
731 {
732 m_state->m_version=1002;
733 f << "quattropro[wb2],";
734 }
735 else if (val==0x1007)
736 {
737 m_state->m_version=1003;
738 f << "quattropro[wb3],";
739 }
740 else
741 {
742 WPS_DEBUG_MSG(("QuattroParser::checkHeader: find unknown file version\n"));
743 return false;
744 }
745 }
746 else
747 {
748 WPS_DEBUG_MSG(("QuattroParser::checkHeader: header contain unexpected size field data\n"));
749 return false;
750 }
751 input->seek(0, librevenge::RVNG_SEEK_SET);
752 if (strict)
753 {
754 for (int i=0; i < 6; ++i)
755 {
756 if (!readZone(stream)) return false;
757 if (m_state->m_isEncrypted) break;
758 }
759 }
760 ascFile.addPos(0);
761 ascFile.addNote(f.str().c_str());
762
763 return true;
764 }
765
766 bool QuattroParser::readZones()
767 {
768 int const vers=version();
769 m_graphParser->cleanState();
770 m_spreadsheetParser->cleanState();
771 m_state->initZoneNameMap();
772
773 std::shared_ptr<WPSStream> stream(new WPSStream(getInput(), ascii()));
774 RVNGInputStreamPtr &input = stream->m_input;
775 libwps::DebugFile &ascFile=stream->m_ascii;
776 input->seek(0, librevenge::RVNG_SEEK_SET);
777 while (true)
778 {
779 if (!stream->checkFilePosition(input->tell()+4))
780 break;
781 if (!readZone(stream))
782 break;
783 if (m_state->m_isEncrypted && !m_state->m_isDecoded)
784 throw(libwps::PasswordException());
785 }
786
787 //
788 // look for ending
789 //
790 long pos = input->tell();
791 if (!stream->checkFilePosition(pos+4))
792 {
793 WPS_DEBUG_MSG(("QuattroParser::readZones: cell header is too short\n"));
794 return m_spreadsheetParser->getNumSpreadsheets()>0;
795 }
796 auto type = int(libwps::readU16(input)); // 1
797 auto length = int(libwps::readU16(input));
798 if (length)
799 {
800 WPS_DEBUG_MSG(("QuattroParser::readZones: parse breaks before ending\n"));
801 ascFile.addPos(pos);
802 ascFile.addNote("Entries(BAD):###");
803 return m_spreadsheetParser->getNumSpreadsheets()>0;
804 }
805
806 ascFile.addPos(pos);
807 if (type != 1)
808 {
809 WPS_DEBUG_MSG(("QuattroParser::readZones: odd end cell type: %d\n", type));
810 ascFile.addNote("Entries(BAD):###");
811 return m_spreadsheetParser->getNumSpreadsheets();
812 }
813 ascFile.addNote("Entries(EndSpreadsheet)");
814
815 // checkme: crypted .wb3 files also contain an OLE zone, but it seems empty...
816 if (vers==1002 || (vers==1003 && m_state->m_isEncrypted))
817 readOLEZones(stream);
818 if (vers==1003)
819 {
820 // object in PerfectOffice_OBJECTS/_1507007992_c/, ... *
821 parseOLEStream(getFileInput(), "PerfectOffice_MAIN");
822 }
823 return m_spreadsheetParser->getNumSpreadsheets();
824 }
825
826 bool QuattroParser::parseOLEStream(RVNGInputStreamPtr input, std::string const &avoid)
827 {
828 if (!input || !input->isStructured())
829 {
830 WPS_DEBUG_MSG(("QuattroParser::parseOLEStream: oops, can not find the input stream\n"));
831 return false;
832 }
833 std::map<std::string,size_t> dirToIdMap;
834 WPSOLEParser oleParser(avoid, getDefaultFontType(),
835 [&dirToIdMap](std::string const &dir)
836 {
837 if (dirToIdMap.find(dir)==dirToIdMap.end())
838 dirToIdMap[dir]=dirToIdMap.size();
839 return int(dirToIdMap.find(dir)->second);
840 });
841 oleParser.parse(input);
842 oleParser.updateMetaData(m_state->m_metaData);
843 auto objectMap=oleParser.getObjectsMap();
844 std::map<librevenge::RVNGString,WPSEmbeddedObject> nameToObjectsMap;
845 for (auto it : dirToIdMap)
846 {
847 if (it.first.empty()) continue;
848 for (int wh=0; wh<2; ++wh)
849 {
850 std::string name=it.first+"/"+(wh==0 ? "LinkInfo" : "BOlePart");
851 RVNGInputStreamPtr cOle(input->getSubStreamByName(name.c_str()));
852 if (!cOle)
853 {
854 WPS_DEBUG_MSG(("QuattroParser::parseOLEStream: oops, can not find link info for dir %s\n", name.c_str()));
855 continue;
856 }
857 libwps::DebugFile asciiFile(cOle);
858 asciiFile.open(libwps::Debug::flattenFileName(name));
859 if (wh==1)
860 readOleBOlePart(std::make_shared<WPSStream>(cOle,asciiFile));
861 else
862 {
863 librevenge::RVNGString linkName;
864 if (readOleLinkInfo(std::make_shared<WPSStream>(cOle,asciiFile),linkName) && !linkName.empty())
865 {
866 if (objectMap.find(int(it.second))==objectMap.end())
867 {
868 WPS_DEBUG_MSG(("QuattroParser::parseOLEStream: oops, can not find embedded data for %s\n", name.c_str()));
869 continue;
870 }
871 nameToObjectsMap[linkName]=objectMap.find(int(it.second))->second;
872 }
873 }
874 }
875 }
876 if (!nameToObjectsMap.empty())
877 m_graphParser->storeObjects(nameToObjectsMap);
878 return true;
879 }
880
881 bool QuattroParser::readOLEZones(std::shared_ptr<WPSStream> &stream)
882 {
883 if (!stream)
884 return false;
885 RVNGInputStreamPtr input = stream->m_input;
886 libwps::DebugFile &ascFile=stream->m_ascii;
887 libwps::DebugStream f;
888 f << "Entries(OLEData)[header]:";
889 long pos = input->tell();
890 long endPos = stream->m_eof;
891 if (!stream->checkFilePosition(pos+18))
892 {
893 WPS_DEBUG_MSG(("QuattroParser::readOLEZones: the zone seems to short\n"));
894 ascFile.addPos(pos);
895 ascFile.addNote(f.str().c_str());
896 return false;
897 }
898 for (int i=0; i<4; ++i) // 0
899 {
900 auto val=int(libwps::read16(input));
901 if (val) f << "f" << i << "=" << val << ",";
902 }
903 auto sSz=long(libwps::readU32(input));
904 librevenge::RVNGString text; // QPW$ExtendedStorage$6.0
905 if (sSz<=0 || sSz > endPos-input->tell()-6 || !readCString(stream,text,sSz))
906 {
907 WPS_DEBUG_MSG(("QuattroParser::readOLEZones: can not read header's type\n"));
908 f << "##sSz,";
909 ascFile.addPos(pos);
910 ascFile.addNote(f.str().c_str());
911 return true;
912 }
913 f << "type=" << text.cstr() << ",";
914 ascFile.addPos(pos);
915 ascFile.addNote(f.str().c_str());
916 while (input->tell()+6<=endPos)
917 {
918 pos=input->tell();
919 f.str("");
920 f << "OLEData:";
921 auto type=int(libwps::read16(input));
922 sSz=long(libwps::readU32(input));
923 if (sSz < 0 || sSz > endPos-pos-6 || type<1 || type>2 || (sSz==0 && type==2))
924 {
925 input->seek(pos, librevenge::RVNG_SEEK_SET);
926 break;
927 }
928 if (type==1)
929 {
930 if (sSz)
931 f << "###sz=" << sSz << ",";
932 f << "end,";
933 ascFile.addPos(pos);
934 ascFile.addNote(f.str().c_str());
935 return true;
936 }
937 ascFile.addPos(pos);
938 ascFile.addNote(f.str().c_str());
939 unsigned long numRead;
940 const unsigned char *data=input->read(static_cast<unsigned long>(sSz), numRead);
941 if (data && long(numRead)==sSz)
942 {
943 auto ole=libwps_OLE::getOLEInputStream(std::make_shared<WPSStringStream>(data, unsigned(numRead)));
944 if (!ole)
945 {
946 WPS_DEBUG_MSG(("QuattroParser::readOLEZones::readOLE: oops, can not decode the ole\n"));
947 }
948 else
949 {
950 /* normally /_Date_XXX/ Where Date~1507005964 and XXX is an hexadecimal data.
951 Moreever, a file can be saved many times, so must read /_Date_XXX/ to retrieve the correspondance
952 */
953 ascFile.skipZone(pos+6, pos+6+sSz-1);
954 parseOLEStream(ole);
955 }
956 }
957 else
958 {
959 WPS_DEBUG_MSG(("QuattroParser::readOLEZones::readOLE: I can not find the data\n"));
960 input->seek(pos, librevenge::RVNG_SEEK_SET);
961 break;
962 }
963 input->seek(pos+6+sSz, librevenge::RVNG_SEEK_SET);
964 }
965 if (input->tell()<endPos)
966 {
967 WPS_DEBUG_MSG(("QuattroParser::readOLEZones: find extra data\n"));
968 ascFile.addPos(input->tell());
969 ascFile.addNote("OLEData:###extra");
970 }
971 return true;
972 }
973
974 bool QuattroParser::readZone(std::shared_ptr<WPSStream> &stream)
975 {
976 if (!stream)
977 return false;
978 RVNGInputStreamPtr input = stream->m_input;
979 libwps::DebugFile &ascFile=stream->m_ascii;
980 libwps::DebugStream f;
981 long pos = input->tell();
982 auto id = int(libwps::readU16(input));
983 auto sz = long(libwps::readU16(input));
984 if (sz<0 || !stream->checkFilePosition(pos+4+sz))
985 {
986 WPS_DEBUG_MSG(("QuattroParser::readZone: size is bad\n"));
987 input->seek(pos, librevenge::RVNG_SEEK_SET);
988 return false;
989 }
990
991 if (id&0x8000)
992 {
993 // very rare, unsure what this means ; is it possible to have
994 // other flags here ?
995 WPS_DEBUG_MSG(("QuattroParser::readZone: find type[8] flags\n"));
996 ascFile.addPos(pos);
997 ascFile.addNote("#flag8000,");
998 id &= 0x7fff;
999 }
1000
1001 if (id>=0x800) // I never found anything biggest than 47d, so must be ok
1002 {
1003 input->seek(pos, librevenge::RVNG_SEEK_SET);
1004 return false;
1005 }
1006
1007 if (sz>=0xFF00 && stream->checkFilePosition(pos+4+sz+4))
1008 {
1009 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
1010 if (libwps::readU16(input)==0x10f)
1011 {
1012 // incomplete block, we must rebuild it
1013 input->seek(pos, librevenge::RVNG_SEEK_SET);
1014 unsigned long numRead;
1015 const unsigned char *data=input->read(4+static_cast<unsigned long>(sz), numRead);
1016 if (data && long(numRead)==4+sz)
1017 {
1018 ascFile.skipZone(pos,pos+4+sz-1);
1019 auto newInput=std::make_shared<WPSStringStream>(data, unsigned(numRead));
1020 bool ok=true;
1021 while (true)
1022 {
1023 long actPos=input->tell();
1024 if (!stream->checkFilePosition(actPos+4) || libwps::readU16(input)!=0x10f)
1025 {
1026 input->seek(actPos, librevenge::RVNG_SEEK_SET);
1027 break;
1028 }
1029 auto extraSize=long(libwps::readU16(input));
1030 if (!stream->checkFilePosition(actPos+4+extraSize))
1031 {
1032 ok=false;
1033 break;
1034 }
1035 ascFile.addPos(actPos);
1036 ascFile.addNote("Entries(ExtraData):");
1037 if (!extraSize)
1038 break;
1039 data=input->read(static_cast<unsigned long>(extraSize), numRead);
1040 if (!data || long(numRead)!=extraSize)
1041 {
1042 ok=false;
1043 break;
1044 }
1045 newInput->append(data, unsigned(numRead));
1046 ascFile.skipZone(actPos+4,actPos+4+extraSize-1);
1047 }
1048 if (ok)
1049 {
1050 std::stringstream s;
1051 static int complexDataNum=0;
1052 s << "Data" << ++complexDataNum;
1053 auto newStream=std::make_shared<WPSStream>(newInput);
1054 newStream->m_ascii.open(s.str());
1055 newStream->m_ascii.setStream(newInput);
1056 readZone(newStream);
1057 return true;
1058 }
1059 }
1060 WPS_DEBUG_MSG(("QuattroParser::readZone: can not reconstruct a zone\n"));
1061 ascFile.addPos(pos);
1062 ascFile.addNote("Entries(###Bad):");
1063 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
1064 return true;
1065 }
1066 }
1067 auto zIt=m_state->m_idToZoneNameMap.find(id);
1068 if (zIt==m_state->m_idToZoneNameMap.end())
1069 f << "Entries(Zone" << std::hex << id << std::dec << "A):";
1070 else if (zIt->second.m_extra.empty())
1071 f << "Entries(" << zIt->second.m_name << "):";
1072 else
1073 f << "Entries(" << zIt->second.m_name << ")[" << zIt->second.m_extra << "]:";
1074 if (id>1)
1075 {
1076 ascFile.addPos(pos);
1077 ascFile.addNote(f.str().c_str());
1078 }
1079 f.str("");
1080
1081 bool ok = true, isParsed = false, needWriteInAscii = false;
1082 int val;
1083 input->seek(pos, librevenge::RVNG_SEEK_SET);
1084 switch (id)
1085 {
1086 case 0:
1087 if (sz!=2) break;
1088 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1089 f << "version=" << std::hex << libwps::readU16(input) << std::dec << ",";
1090 isParsed=needWriteInAscii=true;
1091 break;
1092 case 0x1: // EOF
1093 ok = false;
1094 break;
1095
1096 // no data
1097 case 0xfb: // print end record/end
1098 case 0x191:
1099 case 0x192:
1100 case 0x25a:
1101 case 0x25b:
1102 case 0x25c:
1103 case 0x25f:
1104 case 0x2bc:
1105 case 0x2bd:
1106 case 0x2be:
1107 case 0x2c3:
1108 case 0x2c4:
1109 case 0x2c9:
1110 case 0x2ca:
1111 case 0x2cd:
1112 case 0x2ce:
1113 case 0x31f:
1114 if (sz!=0) break;
1115 isParsed=needWriteInAscii=true;
1116 break;
1117 // boolean
1118 case 0x2: // calculation mode 0: manual, 1:background, FF:auto
1119 case 0x3: // calculation order 0: default, 1:column, ff:row
1120 case 0x24: // protected if FF
1121 case 0x2f: // number of iteration
1122 case 0x30: // print page break 0=yes, 1=no
1123 case 0x99: // 0: no macro, 1: macro library
1124 case 0xe2: // 0: off, 1: on
1125 case 0xee: // 0: print results, 1: print formula
1126 case 0xf3: // 0: draft, 1: final
1127 case 0xf4: // 0: no, 1: yes, sometimes with size 5
1128 case 0xf7: // 0: landscape, 1: portrait
1129 case 0xfa: // 0: no centering, 1: print centered
1130 case 0x101: // 0: do not print headings, 1: print headings
1131 case 0x102: // 0: do not print gridlines, 1: print them
1132 case 0x109: // in fact, a name always empty
1133 case 0x10a: // in fact, a name always empty
1134 case 0x111: // 0: current page, 1: notebook, 2: block selection
1135 case 0x132: // 0: no formula compiled, 1: formula compiled
1136 case 0x133: // 0: no audit display, 1: display audit
1137 case 0x137: // 0: disabled, 1: enabled
1138 case 0x196: // 0: synchronized, 2: not synchronized
1139 case 0x19b: // 1: pane 1, 2: pane 2
1140 if (sz==0)
1141 {
1142 f << "##";
1143 break;
1144 }
1145 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1146 val=int(libwps::readU8(input));
1147 if (val==1) f << "true,";
1148 else if (val) f << "#val=" << val << ",";
1149 if (id==0xf4 && sz==5)
1150 {
1151 for (int i=0; i<2; ++i)
1152 {
1153 val=int(libwps::readU16(input));
1154 if (val!=1) f << "f" << i << "=" << val << ",";
1155 }
1156 }
1157 isParsed=needWriteInAscii=true;
1158 break;
1159 case 0x4c: // with 0: none, 1:low, 2:medium, 3:high
1160 case 0x4d: // with 0
1161 case 0xc9: // with [026]{00,01,c8}
1162 case 0xe0: // default panel style
1163 case 0xe1: // default panel2 style
1164 case 0xf1: // number of copies
1165 case 0xf5: // with scaling in %
1166 case 0xf6: // paper type: 0|1|9|101
1167 case 0x12d: // with 1
1168 case 0x136: // 0: show all objects, 1: show outline objects, 2: hidde object
1169 case 0x19e: // zoom factor
1170 case 0x262: // time
1171 case 0x263: // slide effect
1172 case 0x265: // speed
1173 case 0x266: // slide special effect
1174 case 0x26a: // 0: small, 1: medium, 2: large, 3: list of name
1175 case 0x2c2: // number of series
1176 if (sz!=2)
1177 {
1178 f << "##";
1179 break;
1180 }
1181 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1182 val=int(libwps::readU16(input));
1183 if (val) f << "f0=" << std::hex << val << std::dec << ",";
1184 break;
1185 case 0xe7: // f0=column, f1>>3=def attritude
1186 case 0xef: // f0=number of line, f1:0=specified number of line, 1=page feed
1187 case 0xf0: // f0=number of line, f1:0=specified number of line, 1=page feed
1188 case 0x12e: // f0-f1=number
1189 case 0x25d: // f0=x, f1=y
1190 case 0x260: // f0=x, f1=y
1191 case 0x264: // version
1192 if (sz!=4)
1193 {
1194 f << "##";
1195 break;
1196 }
1197 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1198 for (int i=0; i<2; ++i)
1199 {
1200 val=int(libwps::readU16(input));
1201 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1202 }
1203 break;
1204 case 0xf2: // f0=min page, f1=max page, f2:0=print range, 1=print all
1205 if (sz!=6)
1206 {
1207 f << "##";
1208 break;
1209 }
1210 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1211 for (int i=0; i<3; ++i)
1212 {
1213 val=int(libwps::readU16(input));
1214 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1215 }
1216 break;
1217 // not basic
1218 case 0x6: // active worksheet range
1219 ok = m_spreadsheetParser->readSheetSize(stream);
1220 isParsed = true;
1221 break;
1222 case 0xb: // named range
1223 readFieldName(stream);
1224 isParsed=true;
1225 break;
1226 case 0xc: // blank cell
1227 case 0xd: // integer cell
1228 case 0xe: // floating cell
1229 case 0xf: // label cell
1230 case 0x10: // formula cell
1231 case 0x33: // value of string formula
1232 // case 10e: seems relative to cell with formula: list of dependency? can have some text
1233 ok = m_spreadsheetParser->readCell(stream);
1234 isParsed=true;
1235 break;
1236
1237 case 0x18: // data table range
1238 case 0x19: // query range
1239 case 0x1a: // print block
1240 case 0x1b: // sort range
1241 case 0x1c: // fill range
1242 case 0x1d: // primary sort key range
1243 case 0x20: // distribution range
1244 case 0x23: // secondary sort key range
1245 case 0x66: // parse
1246 case 0x67: // regression
1247 case 0x69: // matrix
1248 case 0x9f: // sort third key
1249 case 0xa0: // sort four key
1250 case 0xa1: // sort fifth key
1251 case 0xb7: // solve for
1252 case 0xf8: // left border
1253 case 0xf9: // top border
1254 case 0x10d: //compatible slide show
1255 case 0x2bf: // X axis label
1256 case 0x2c0: // Z axis label
1257 case 0x2c1: // legend
1258 case 0x2c6: // serie data
1259 case 0x2c7: // serie legend
1260 readBlockList(stream);
1261 isParsed=true;
1262 break;
1263 // checkme this nodes appear two times, even/odd page ?
1264 case 0x25: // footer
1265 case 0x26: // header
1266 readHeaderFooter(stream, id==0x26);
1267 isParsed = true;
1268 break;
1269 case 0x27: // print setup
1270 if (sz<1) break;
1271 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1272 val=int(libwps::readU8(input));
1273 if (val+1<sz) break;
1274 // now data to send to the printer
1275 break;
1276 case 0x28: // print margin: one by spreadsheet
1277 if (sz!=12) break;
1278 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1279 f << "margs=[";
1280 for (int i=0; i<4; ++i) // LRTB
1281 f << float(libwps::read16(input))/20.f << ",";
1282 f << "],";
1283 f << "hf[height]=[";
1284 for (int i=0; i<2; ++i) // header, footer height
1285 f << float(libwps::read16(input))/20.f << ",";
1286 f << "],";
1287 isParsed=needWriteInAscii=true;
1288 break;
1289
1290 case 0x4b:
1291 m_state->m_isEncrypted=true;
1292 if (sz==20)
1293 {
1294 m_state->m_isEncrypted=true;
1295 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1296 uint16_t fileKey(libwps::readU16(input));
1297 f << "pass=" << std::hex << fileKey << std::dec << ",";
1298 f << "len=" << int(libwps::readU16(input)) << ",";
1299 isParsed = needWriteInAscii = true;
1300 std::vector<uint8_t> keys;
1301 keys.resize(16);
1302 for (auto &k : keys) k=uint8_t(libwps::readU8(input));
1303 // to check users password:
1304 // call libwps::encodeLotusPassword(m_state->m_password, key, lotusKeys, someDefValues);
1305 // and check if int16_t(key<<8|key>>8)==fileKey
1306 if (!m_state->m_isDecoded)
1307 {
1308 auto newInput=decodeStream(input, keys);
1309 if (newInput)
1310 {
1311 // let's replace the current input by the decoded input
1312 m_state->m_isDecoded=true;
1313 stream->m_input=newInput;
1314 stream->m_ascii.setStream(newInput);
1315 }
1316 }
1317 }
1318 if (!m_state->m_isDecoded)
1319 {
1320 WPS_DEBUG_MSG(("QuattroParser::parse: can not decode the file\n"));
1321 }
1322 break;
1323 case 0x96:
1324 readCellPosition(stream);
1325 isParsed=true;
1326 break;
1327 case 0x97: // name use to refer to some external spreadsheet
1328 case 0x98: // external name
1329 readExternalData(stream);
1330 isParsed=true;
1331 break;
1332
1333 case 0xca:
1334 case 0xcb:
1335 isParsed=m_spreadsheetParser->readBeginEndSheet(stream, m_state->m_actualSheet);
1336 break;
1337 case 0xcc:
1338 isParsed=m_spreadsheetParser->readSheetName(stream);
1339 break;
1340 case 0xce:
1341 ok = m_spreadsheetParser->readCellStyle(stream);
1342 isParsed = true;
1343 break;
1344 case 0xcf:
1345 case 0xfc:
1346 case 0x110:
1347 isParsed=readFontDef(stream);
1348 break;
1349 case 0xd0:
1350 isParsed=readStyleName(stream);
1351 break;
1352 case 0xd1:
1353 isParsed=readPaneAttribute(stream);
1354 break;
1355 case 0xd6:
1356 case 0xd7:
1357 isParsed = m_spreadsheetParser->readRowSize(stream);
1358 break;
1359 case 0xd8:
1360 case 0xd9:
1361 isParsed = m_spreadsheetParser->readColumnSize(stream);
1362 break;
1363 case 0xd2: // default row height
1364 case 0xd3:
1365 case 0xd4: // default col width
1366 case 0xd5:
1367 isParsed=m_spreadsheetParser->readColumnRowDefaultSize(stream);
1368 break;
1369 case 0xda:
1370 case 0xdb:
1371 if (sz!=3 && sz!=4)
1372 {
1373 f << "##";
1374 break;
1375 }
1376 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1377 f << "row=" << libwps::readU16(input) << ",";
1378 f << "F" << int(libwps::readU8(input)) << ",";
1379 if (sz==4)
1380 {
1381 val=int(libwps::readU8(input)); // 0, 5b, da
1382 if (val) f << "f0=" << std::hex << val << std::dec << ",";
1383 }
1384 break;
1385 case 0xdc:
1386 case 0xdd:
1387 case 0xde:
1388 case 0xdf:
1389 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1390 f << "hidden=[";
1391 for (long i=0; i<sz; ++i)
1392 {
1393 val=int(libwps::readU8(input));
1394 if (val==0) continue;
1395 for (int d=0, b=1; d<8; ++d, b<<=1) // check byte order
1396 {
1397 if (val&b) f << 8*i+d << ",";
1398 }
1399 }
1400 f << "],";
1401 break;
1402 case 0xe3:
1403 case 0xe4:
1404 case 0xe5:
1405 case 0xe6: // user format, FIXME: parse me and use me
1406 {
1407 if (sz<3) break;
1408 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1409 val=int(libwps::readU16(input));
1410 if (id==0xe3)
1411 f << "sheet" << (val&0xff) << "=>" << (val>>8) << ",";
1412 else
1413 f << "id=" << val << ",";
1414 librevenge::RVNGString text;
1415 if (!readCString(stream, text,sz-2))
1416 f << "###";
1417 else
1418 {
1419 if (id==0xe4 || id==0xe5)
1420 m_spreadsheetParser->addDLLIdName(val, text, id==0xe4);
1421 else if (id==0xe6)
1422 m_spreadsheetParser->addUserFormat(val, text);
1423 f << text.cstr() << ",";
1424 }
1425 break;
1426 }
1427 case 0xe8:
1428 isParsed=readColorList(stream);
1429 break;
1430 case 0xed:
1431 case 0x259:
1432 case 0x25e:
1433 case 0x261:
1434 case 0x26b:
1435 case 0x26c:
1436 case 0x2c8:
1437 {
1438 if (sz<1) break;
1439 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1440 librevenge::RVNGString text;
1441 if (!readCString(stream, text,sz))
1442 f << "###";
1443 else
1444 f << text.cstr() << ",";
1445 break;
1446 }
1447 // case 0xfe: some counter
1448 case 0x103:
1449 isParsed=readOptimizer(stream);
1450 break;
1451 // 0x104: optimizer constraint: readme
1452 case 0x105:
1453 case 0x106:
1454 isParsed=m_spreadsheetParser->readRowRangeSize(stream);
1455 break;
1456 case 0x107: // never seems
1457 case 0x108:
1458 if (sz!=5)
1459 {
1460 f << "##";
1461 break;
1462 }
1463 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1464 f << "rows=" << libwps::readU16(input) << ",";
1465 f << "x" << libwps::readU16(input) << ",";
1466 f << "F" << int(libwps::readU8(input)) << ",";
1467 break;
1468 case 0x10c: // draft margin: never seens
1469 if (sz!=12) break;
1470 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1471 f << "margs=[";
1472 for (int i=0; i<4; ++i) // LRTB
1473 f << float(libwps::read16(input))/20.f << ",";
1474 f << "],";
1475 f << "height=" << float(libwps::read16(input))/20.f << ",";
1476 f << "units=" << libwps::read16(input) << ","; // 0: char, 1: inches, 2: centimters
1477 isParsed=needWriteInAscii=true;
1478 break;
1479 case 0x12f:
1480 isParsed=readQueryCommand(stream);
1481 break;
1482 case 0x134:
1483 {
1484 if (sz!=4)
1485 {
1486 f << "###";
1487 break;
1488 }
1489 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1490 unsigned char colors[4];
1491 for (auto &c : colors) c=static_cast<unsigned char>(libwps::readU8(input));
1492 f << WPSColor(colors[0],colors[1],colors[2]) << ",";
1493 break;
1494 }
1495 case 0x135:
1496 {
1497 if (sz!=4)
1498 {
1499 f << "###";
1500 break;
1501 }
1502 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1503 int values[2];
1504 for (auto &v : values) v=int(libwps::readU16(input));
1505 if (values[0]==100)
1506 f << values[1] << "%,";
1507 else if (values[0]!=1 || values[1]!=1)
1508 f << values[1] << "/" << values[0] << ",";
1509 break;
1510 }
1511 case 0x193:
1512 {
1513 if (sz!=6)
1514 {
1515 f << "###";
1516 break;
1517 }
1518 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1519 f << "size=" << libwps::readU16(input) << "x";
1520 f << libwps::readU16(input) << ",";
1521 f << "state=" << libwps::readU16(input) << ","; // 0:normal, 1:max, 2:min, 3:hidden
1522 break;
1523 }
1524 case 0x194:
1525 {
1526 if (sz!=4)
1527 {
1528 f << "###";
1529 break;
1530 }
1531 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1532 f << "pos=" << libwps::read16(input) << "x";
1533 f << libwps::read16(input) << ",";
1534 break;
1535 }
1536 case 0x195:
1537 {
1538 if (sz!=6)
1539 {
1540 f << "###";
1541 break;
1542 }
1543 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1544 f << "type=" << libwps::readU16(input) << ","; // 0: no split, 1: horizontal, 2:vertical
1545 f << "split=" << libwps::readU16(input) << "%,";
1546 f << libwps::readU16(input) << "%],";
1547 break;
1548 }
1549 case 0x197: // one by sheet
1550 case 0x198:
1551 isParsed=m_spreadsheetParser->readViewInfo(stream);
1552 break;
1553 case 0x19c:
1554 {
1555 if (sz!=8)
1556 {
1557 f << "###";
1558 break;
1559 }
1560 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1561 for (int i=0; i<4; ++i) // f0: group mode, f1: hori bar vis, f2: vert bar vis, f3: vis tabs
1562 {
1563 val=int(libwps::readU16(input));
1564 if (val!=1) f << "f" << i << "=" << val << ",";
1565 }
1566 break;
1567 }
1568 case 0x267:
1569 if (sz!=2)
1570 {
1571 f << "###";
1572 break;
1573 }
1574 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1575 val=int(libwps::readU16(input));
1576 f << "ident=" << (val&0xff) << ",";
1577 if (val&0x100) f << "no[master],";
1578 if (val&0x1000) f << "skip[slide],";
1579 break;
1580 case 0x2db: // find with size 0 or 2
1581 if (sz==0) break;
1582 if (sz%2)
1583 {
1584 f << "###";
1585 break;
1586 }
1587 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1588 for (long i=0; i<sz; ++i)
1589 {
1590 val=int(libwps::readU8(input));
1591 if (val) // small number 0-c
1592 f << "f" << i << "=" << std::hex << val << std::dec << ",";
1593 }
1594 break;
1595 case 0x2dc:
1596 isParsed=readSerieExtension(stream);
1597 break;
1598 case 0x321:
1599 case 0x322:
1600 isParsed=m_graphParser->readBeginEnd(stream, m_state->m_actualSheet);
1601 break;
1602 // case 0x324: a list of XX:20?: seems relative to graph/chart
1603 case 0x33e: // oval
1604 isParsed = m_graphParser->readRect(stream);
1605 break;
1606 case 0x341: // maybe chart ?
1607 isParsed = readZone341(stream);
1608 break;
1609 case 0x335:
1610 case 0x337:
1611 case 0x33f:
1612 case 0x342:
1613 case 0x343:
1614 case 0x345:
1615 case 0x349:
1616 case 0x34a:
1617 case 0x34e:
1618 case 0x34f:
1619 case 0x351:
1620 case 0x35d:
1621 case 0x36d:
1622 isParsed = m_graphParser->readDialogUnknown(stream);
1623 break;
1624 case 0x35a:
1625 isParsed = m_graphParser->readLine(stream);
1626 break;
1627 case 0x35b: // polygon
1628 case 0x35c: // polyline
1629 case 0x37c: // free polyline
1630 case 0x388: // free polygon
1631 isParsed = m_graphParser->readPolygon(stream);
1632 break;
1633 case 0x35e:
1634 isParsed = m_graphParser->readDialog(stream);
1635 break;
1636 case 0x364:
1637 isParsed = m_graphParser->readRect(stream);
1638 break;
1639 case 0x36f:
1640 isParsed = m_graphParser->readTextBox(stream);
1641 break;
1642 case 0x379: // round rect
1643 isParsed = m_graphParser->readRect(stream);
1644 break;
1645 case 0x37b: // arrow
1646 isParsed = m_graphParser->readLine(stream);
1647 break;
1648 case 0x381: // frame wb2
1649 isParsed = m_graphParser->readFrameOLE(stream);
1650 break;
1651 case 0x382: // only wb2?
1652 isParsed = m_graphParser->readImage(stream);
1653 break;
1654 case 0x383: // only wb2?
1655 isParsed = m_graphParser->readBitmap(stream);
1656 break;
1657 case 0x384:
1658 isParsed = m_graphParser->readChart(stream);
1659 break;
1660 case 0x385:
1661 isParsed = m_graphParser->readFrame(stream);
1662 break;
1663 case 0x386:
1664 isParsed = m_graphParser->readButton(stream);
1665 break;
1666 case 0x38b:
1667 isParsed = m_graphParser->readOLEData(stream);
1668 break;
1669
1670 case 0x4d3: // wb2 and wb3
1671 isParsed = m_graphParser->readShape(stream);
1672 break;
1673 default:
1674 break;
1675 }
1676
1677 if (!ok)
1678 {
1679 input->seek(pos, librevenge::RVNG_SEEK_SET);
1680 return false;
1681 }
1682 if (isParsed)
1683 {
1684 if (needWriteInAscii)
1685 {
1686 ascFile.addPos(pos);
1687 ascFile.addNote(f.str().c_str());
1688 }
1689 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
1690 return true;
1691 }
1692
1693 if (sz && input->tell()!=pos && input->tell()!=pos+4+sz)
1694 ascFile.addDelimiter(input->tell(),'|');
1695 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
1696 ascFile.addPos(pos);
1697 ascFile.addNote(f.str().c_str());
1698 return true;
1699 }
1700
1701 ////////////////////////////////////////////////////////////
1702 // generic
1703 ////////////////////////////////////////////////////////////
1704
1705 bool QuattroParser::readCString(std::shared_ptr<WPSStream> stream, librevenge::RVNGString &string, long maxSize)
1706 {
1707 RVNGInputStreamPtr input = stream->m_input;
1708 long pos = input->tell();
1709 string.clear();
1710 if (!stream->checkFilePosition(pos+maxSize))
1711 {
1712 WPS_DEBUG_MSG(("QuattroParser::readCString: string's size seems bad\n"));
1713 return false;
1714 }
1715 std::string text;
1716 for (long i=0; i<maxSize; ++i)
1717 {
1718 auto c = char(libwps::readU8(input));
1719 if (c == '\0') break;
1720 text.push_back(c);
1721 }
1722 if (!text.empty())
1723 string=libwps_tools_win::Font::unicodeString(text, getDefaultFontType());
1724 return true;
1725 }
1726
1727 bool QuattroParser::readFieldName(std::shared_ptr<WPSStream> stream)
1728 {
1729 RVNGInputStreamPtr input = stream->m_input;
1730 libwps::DebugFile &ascFile=stream->m_ascii;
1731 libwps::DebugStream f;
1732
1733 long pos = input->tell();
1734 auto type = long(libwps::readU16(input)&0x7fff);
1735 if (type != 0xb)
1736 {
1737 WPS_DEBUG_MSG(("QuattroParser::readFieldName: not a zoneB type\n"));
1738 return false;
1739 }
1740 auto sz = long(libwps::readU16(input));
1741 long endPos=pos+4+sz;
1742 if (sz < 4)
1743 {
1744 WPS_DEBUG_MSG(("QuattroParser::readFieldName: size seems bad\n"));
1745 f << "###";
1746 ascFile.addPos(pos);
1747 ascFile.addNote(f.str().c_str());
1748 return true;
1749 }
1750 auto id=int(libwps::readU16(input));
1751 f << "id=" << id << ",";
1752 auto val=int(libwps::readU8(input)); // always 1?
1753 bool hasRef=(val&1);
1754 if ((val&1)==0) f << "no[ref],";
1755 if (val&2) f << "deleted,";
1756 // val&4 must be zero, other reserved
1757 librevenge::RVNGString name;
1758 auto sSz=int(libwps::readU8(input));
1759 if (4+sSz+(hasRef ? 6 : 0)>sz || !readCString(stream,name,sSz))
1760 {
1761 f << "##sSz,";
1762 ascFile.addPos(pos);
1763 ascFile.addNote(f.str().c_str());
1764 return true;
1765 }
1766 else if (!name.empty())
1767 f << name.cstr() << ',';
1768 input->seek(pos+4+4+sSz, librevenge::RVNG_SEEK_SET);
1769 if (!hasRef)
1770 {
1771 ascFile.addPos(pos);
1772 ascFile.addNote(f.str().c_str());
1773 return true;
1774 }
1775 QuattroFormulaInternal::CellReference instr;
1776 if (!m_spreadsheetParser->readCellReference(stream, endPos, instr))
1777 {
1778 WPS_DEBUG_MSG(("QuattroParser::readFieldName: can not read some reference\n"));
1779 f << "###";
1780 ascFile.addPos(pos);
1781 ascFile.addNote(f.str().c_str());
1782 return true;
1783 }
1784 else if (!instr.empty())
1785 {
1786 f << instr;
1787 if (m_state->m_idToFieldMap.find(id)!=m_state->m_idToFieldMap.end())
1788 {
1789 WPS_DEBUG_MSG(("QuattroParser::readFieldName: oops a field with id=%d already exists\n", id));
1790 }
1791 else
1792 m_state->m_idToFieldMap[id]=std::pair<librevenge::RVNGString,QuattroFormulaInternal::CellReference>(name, instr);
1793 }
1794 ascFile.addPos(pos);
1795 ascFile.addNote(f.str().c_str());
1796 return true;
1797 }
1798
1799 bool QuattroParser::readExternalData(std::shared_ptr<WPSStream> stream)
1800 {
1801 RVNGInputStreamPtr input = stream->m_input;
1802 libwps::DebugFile &ascFile=stream->m_ascii;
1803 libwps::DebugStream f;
1804 long pos = input->tell();
1805 auto type = int(libwps::readU16(input)&0x7fff);
1806
1807 if (type != 0x97 && type != 0x98)
1808 {
1809 WPS_DEBUG_MSG(("QuattroParser::readExternalData: not a font zone\n"));
1810 return false;
1811 }
1812 auto sz = long(libwps::readU16(input));
1813 if (sz<3)
1814 {
1815 WPS_DEBUG_MSG(("QuattroParser::readExternalData: seems very short\n"));
1816 f << "###";
1817 ascFile.addPos(pos);
1818 ascFile.addNote(f.str().c_str());
1819 return true;
1820 }
1821 auto id=int(libwps::readU16(input));
1822 f << "id=" << id << ",";
1823 librevenge::RVNGString name;
1824 auto &map=type==0x98? m_state->m_idToExternalNameMap : m_state->m_idToExternalFileMap;
1825 if (!readCString(stream,name,sz-2))
1826 {
1827 f << "##name,";
1828 }
1829 else if (map.find(id)!=map.end())
1830 {
1831 WPS_DEBUG_MSG(("QuattroParser::readExternalData: id=%d already found\n", id));
1832 f << "##duplicated,";
1833 }
1834 else if (!name.empty() || type==0x97) // external[file]=="" means current
1835 map[id]=name;
1836 if (!name.empty())
1837 f << name.cstr() << ',';
1838 /* if name not empty,
1839 0x97 is followed by first page/second page (or 00000000e97f9409)
1840 0x98 is followed by index of owner + a potential cell ref?
1841 */
1842 if (input->tell()!=pos+4+sz) ascFile.addDelimiter(input->tell(),'|');
1843 ascFile.addPos(pos);
1844 ascFile.addNote(f.str().c_str());
1845 return true;
1846 }
1847
1848 bool QuattroParser::readFontDef(std::shared_ptr<WPSStream> stream)
1849 {
1850 RVNGInputStreamPtr input = stream->m_input;
1851 libwps::DebugFile &ascFile=stream->m_ascii;
1852 libwps::DebugStream f;
1853 long pos = input->tell();
1854 auto type = int(libwps::readU16(input)&0x7fff);
1855
1856 if (type != 0xcf && type != 0xfc && type!=0x110)
1857 {
1858 WPS_DEBUG_MSG(("QuattroParser::readFontDef: not a font zone\n"));
1859 return false;
1860 }
1861 auto sz = long(libwps::readU16(input));
1862 if (type==0xcf)
1863 f << "[F" << m_state->m_fontsList.size() << "],";
1864 QuattroParserInternal::Font font(getDefaultFontType());
1865 if (sz!=0x24)
1866 {
1867 WPS_DEBUG_MSG(("QuattroParser::readFontDef: seems very bad\n"));
1868 f << "###";
1869 ascFile.addPos(pos);
1870 ascFile.addNote(f.str().c_str());
1871 m_state->m_fontsList.push_back(font);
1872 return true;
1873 }
1874 auto fSize = int(libwps::readU16(input));
1875 if (fSize >= 1 && fSize <= 50) // in fact maximum is 500
1876 font.m_size=double(fSize);
1877 else
1878 f << "###fSize=" << fSize << ",";
1879 auto flags = int(libwps::readU16(input));
1880 uint32_t attributes = 0;
1881 if (flags & 1) attributes |= WPS_BOLD_BIT;
1882 if (flags & 2) attributes |= WPS_ITALICS_BIT;
1883 if (flags & 4) attributes |= WPS_UNDERLINE_BIT;
1884 if (flags & 8) attributes |= WPS_SUBSCRIPT_BIT; // reserved
1885 if (flags & 0x10) attributes |= WPS_SUPERSCRIPT_BIT; // reserved
1886 if (flags & 0x20) attributes |= WPS_STRIKEOUT_BIT;
1887 if (flags & 0x40) attributes |= WPS_DOUBLE_UNDERLINE_BIT; // reserved
1888 if (flags & 0x80) attributes |= WPS_OUTLINE_BIT; // reserved
1889 if (flags & 0x100) attributes |= WPS_SHADOW_BIT; // reserved
1890
1891 font.m_attributes=attributes;
1892 flags &= 0xfe00;
1893 if (flags)
1894 f << "##fl=" << std::hex << flags << std::dec << ",";
1895 librevenge::RVNGString name;
1896 if (!readCString(stream,name,32))
1897 {
1898 f << "##name,";
1899 }
1900 else
1901 font.m_name=name;
1902 if (type==0xcf)
1903 m_state->m_fontsList.push_back(font);
1904
1905 f << font;
1906 ascFile.addPos(pos);
1907 ascFile.addNote(f.str().c_str());
1908 return true;
1909 }
1910
1911 bool QuattroParser::readColorList(std::shared_ptr<WPSStream> stream)
1912 {
1913 RVNGInputStreamPtr input = stream->m_input;
1914 libwps::DebugFile &ascFile=stream->m_ascii;
1915 libwps::DebugStream f;
1916 long pos = input->tell();
1917 auto type = int(libwps::readU16(input)&0x7fff);
1918
1919 if (type != 0xe8)
1920 {
1921 WPS_DEBUG_MSG(("QuattroParser::readColorList: not a font zone\n"));
1922 return false;
1923 }
1924 auto sz = long(libwps::readU16(input));
1925 if (sz<0x40 || (sz%4))
1926 {
1927 WPS_DEBUG_MSG(("QuattroParser::readColorList: seems very bad\n"));
1928 f << "###";
1929 ascFile.addPos(pos);
1930 ascFile.addNote(f.str().c_str());
1931 return true;
1932 }
1933 auto N=size_t(sz/4);
1934 m_state->m_colorsList.resize(N);
1935 for (auto &c: m_state->m_colorsList)
1936 {
1937 uint8_t cols[4];
1938 for (auto &co : cols) co=uint8_t(libwps::readU8(input));
1939 c=WPSColor(cols[0],cols[1],cols[2],cols[3]);
1940 f << c << ",";
1941 }
1942 ascFile.addPos(pos);
1943 ascFile.addNote(f.str().c_str());
1944 return true;
1945 }
1946
1947 bool QuattroParser::readStyleName(std::shared_ptr<WPSStream> stream)
1948 {
1949 RVNGInputStreamPtr input = stream->m_input;
1950 libwps::DebugFile &ascFile=stream->m_ascii;
1951 libwps::DebugStream f;
1952 long pos = input->tell();
1953 auto type = int(libwps::readU16(input))&0x7fff;
1954
1955 if (type != 0xd0)
1956 {
1957 WPS_DEBUG_MSG(("QuattroParser::readStyleName: not a font zone\n"));
1958 return false;
1959 }
1960 auto sz = long(libwps::readU16(input));
1961 if (sz<4)
1962 {
1963 WPS_DEBUG_MSG(("QuattroParser::readStyleName: seems very bad\n"));
1964 f << "###";
1965 ascFile.addPos(pos);
1966 ascFile.addNote(f.str().c_str());
1967 return true;
1968 }
1969 auto id=int(libwps::readU16(input)); // current style id
1970 f << "St" << id << ",";
1971 auto val=int(libwps::readU16(input));
1972 if ((val&0x3fff)!=id) f << "attrib[id]=" << val << ",";
1973 if (sz!=4) // no name seems ok
1974 {
1975 librevenge::RVNGString name;
1976 if (!readCString(stream,name,sz-4))
1977 {
1978 f << "##name,";
1979 }
1980 else if (!name.empty())
1981 f << name.cstr() << ",";
1982 if (input->tell()!=pos+4+sz)
1983 ascFile.addDelimiter(input->tell(),'|');
1984 }
1985 ascFile.addPos(pos);
1986 ascFile.addNote(f.str().c_str());
1987 return true;
1988 }
1989
1990 // ----------------------------------------------------------------------
1991 // Header/Footer
1992 // ----------------------------------------------------------------------
1993 void QuattroParser::sendHeaderFooter(bool header)
1994 {
1995 if (!m_listener)
1996 {
1997 WPS_DEBUG_MSG(("QuattroParser::sendHeaderFooter: can not find the listener\n"));
1998 return;
1999 }
2000
2001 m_listener->setFont(m_state->getDefaultFont());
2002 auto const &text = header ? m_state->m_headerString : m_state->m_footerString;
2003 m_listener->insertUnicodeString(text);
2004 }
2005
2006 bool QuattroParser::readHeaderFooter(std::shared_ptr<WPSStream> stream, bool header)
2007 {
2008 RVNGInputStreamPtr input = stream->m_input;
2009 libwps::DebugFile &ascFile=stream->m_ascii;
2010 libwps::DebugStream f;
2011 long pos = input->tell();
2012 auto type = int(libwps::readU16(input)&0x7fff);
2013 if (type != 0x0026 && type != 0x0025)
2014 {
2015 WPS_DEBUG_MSG(("QuattroParser::readHeaderFooter: not a header/footer\n"));
2016 return false;
2017 }
2018 auto sz = long(libwps::readU16(input));
2019 long endPos = pos+4+sz;
2020
2021 librevenge::RVNGString text;
2022 if (!readCString(stream,text,sz))
2023 {
2024 f << "##sSz,";
2025 ascFile.addPos(pos);
2026 ascFile.addNote(f.str().c_str());
2027 return true;
2028 }
2029 if (!text.empty())
2030 {
2031 if (header)
2032 m_state->m_headerString=text;
2033 else
2034 m_state->m_footerString=text;
2035 f << text.cstr();
2036 }
2037 if (input->tell()!=endPos)
2038 ascFile.addDelimiter(input->tell(), '|');
2039 ascFile.addPos(pos);
2040 ascFile.addNote(f.str().c_str());
2041
2042 return true;
2043 }
2044
2045 bool QuattroParser::readOptimizer(std::shared_ptr<WPSStream> stream)
2046 {
2047 RVNGInputStreamPtr input = stream->m_input;
2048 libwps::DebugFile &ascFile=stream->m_ascii;
2049 libwps::DebugStream f;
2050
2051 long pos = input->tell();
2052 auto type = long(libwps::readU16(input)&0x7fff);
2053 auto sz = long(libwps::readU16(input));
2054 long endPos=pos+4+sz;
2055 if (type != 0x103)
2056 {
2057 WPS_DEBUG_MSG(("QuattroParser::readOptimizer: not an optimizer zone"));
2058 return false;
2059 }
2060 if (sz<84)
2061 {
2062 WPS_DEBUG_MSG(("QuattroParser::readOptimizer: seems very bad\n"));
2063 f << "###";
2064 ascFile.addPos(pos);
2065 ascFile.addNote(f.str().c_str());
2066 return true;
2067 }
2068 for (int i=0; i<2; ++i)
2069 {
2070 long actPos=input->tell();
2071 QuattroFormulaInternal::CellReference instr;
2072 if (!m_spreadsheetParser->readCellReference(stream, endPos, instr))
2073 {
2074 WPS_DEBUG_MSG(("QuattroParser::readOptimizer: can not read some reference\n"));
2075 f << "###";
2076 ascFile.addPos(pos);
2077 ascFile.addNote(f.str().c_str());
2078 return true;
2079 }
2080 else if (!instr.empty())
2081 f << "cell" << i << "=" << instr << ",";
2082 input->seek(actPos+10, librevenge::RVNG_SEEK_SET);
2083 }
2084 int val=int(libwps::readU16(input));
2085 if (val==1) f << "goal=min,";
2086 else if (val==2) f << "goal=max,";
2087 else if (val==3) f << "goal=targeted,";
2088 else if (val) f << "##goal=" << val << ",";
2089 double value;
2090 bool isNaN;
2091 long actPos=input->tell();
2092 if (libwps::readDouble8(input, value, isNaN))
2093 f << "reached=" << value << ",";
2094 else
2095 {
2096 f << "###reached,";
2097 input->seek(actPos+10, librevenge::RVNG_SEEK_SET);
2098 }
2099
2100 val=int(libwps::readU16(input));
2101 if (val==1) f << "quadratic,";
2102 else if (val) f << "##estimate=" << val << ",";
2103 val=int(libwps::readU16(input));
2104 if (val==1) f << "derivated=central,";
2105 else if (val) f << "##derivated=" << val << ",";
2106 val=int(libwps::readU16(input));
2107 if (val==1) f << "search=conjugate,";
2108 else if (val) f << "##search=" << val << ",";
2109 val=int(libwps::readU16(input));
2110 if (val==1) f << "linear,";
2111 else if (val) f << "##linear=" << val << ",";
2112 val=int(libwps::readU16(input));
2113 if (val==1) f << "show[result],";
2114 else if (val) f << "##show[result]=" << val << ",";
2115
2116 val=int(libwps::readU16(input));
2117 if (val!=100) f << "max[time]=" << val << ",";
2118 val=int(libwps::readU16(input));
2119 if (val!=100) f << "max[iteration]=" << val << ",";
2120 actPos=input->tell();
2121 if (libwps::readDouble8(input, value, isNaN))
2122 f << "precision=" << value << ",";
2123 else
2124 {
2125 f << "###precision,";
2126 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
2127 }
2128 for (int i=0; i<3; ++i)
2129 {
2130 actPos=input->tell();
2131 QuattroFormulaInternal::CellReference instr;
2132 if (!m_spreadsheetParser->readCellReference(stream, endPos, instr))
2133 {
2134 WPS_DEBUG_MSG(("QuattroParser::readOptimizer: can not read some reference\n"));
2135 f << "###";
2136 ascFile.addPos(pos);
2137 ascFile.addNote(f.str().c_str());
2138 return true;
2139 }
2140 else if (!instr.empty())
2141 f << "cell" << i+3 << "=" << instr << ",";
2142 input->seek(actPos+10, librevenge::RVNG_SEEK_SET);
2143 }
2144 input->seek(2, librevenge::RVNG_SEEK_CUR); // unused
2145 if (sz>=94)
2146 {
2147 actPos=input->tell();
2148 if (libwps::readDouble8(input, value, isNaN))
2149 f << "tolerance=" << value << ",";
2150 else
2151 {
2152 f << "###tolerance,";
2153 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
2154 }
2155 val=int(libwps::readU16(input));
2156 if (val) f << "autoScale=" << val << ",";
2157 }
2158 if (input->tell()!=endPos)
2159 ascFile.addDelimiter(input->tell(), '|');
2160 ascFile.addPos(pos);
2161 ascFile.addNote(f.str().c_str());
2162
2163 return true;
2164 }
2165
2166 bool QuattroParser::readQueryCommand(std::shared_ptr<WPSStream> stream)
2167 {
2168 RVNGInputStreamPtr input = stream->m_input;
2169 libwps::DebugFile &ascFile=stream->m_ascii;
2170 libwps::DebugStream f;
2171
2172 long pos = input->tell();
2173 auto type = long(libwps::readU16(input)&0x7fff);
2174 auto sz = long(libwps::readU16(input));
2175 long endPos=pos+4+sz;
2176 if (type != 0x12f)
2177 {
2178 WPS_DEBUG_MSG(("QuattroParser::readQueryCommand: not an queryCommand zone"));
2179 return false;
2180 }
2181 if (sz<22)
2182 {
2183 WPS_DEBUG_MSG(("QuattroParser::readQueryCommand: seems very bad\n"));
2184 f << "###";
2185 ascFile.addPos(pos);
2186 ascFile.addNote(f.str().c_str());
2187 return true;
2188 }
2189 for (int i=0; i<2; ++i)
2190 {
2191 long actPos=input->tell();
2192 QuattroFormulaInternal::CellReference instr;
2193 if (!m_spreadsheetParser->readCellReference(stream, endPos, instr))
2194 {
2195 WPS_DEBUG_MSG(("QuattroParser::readQueryCommand: can not read some reference\n"));
2196 f << "###";
2197 ascFile.addPos(pos);
2198 ascFile.addNote(f.str().c_str());
2199 return true;
2200 }
2201 else if (!instr.empty())
2202 f << "cell" << i << "=" << instr << ",";
2203 input->seek(actPos+10, librevenge::RVNG_SEEK_SET);
2204 }
2205 int val=int(libwps::readU16(input));
2206 if (val) f << "id=" << val << ",";
2207 // then a name?
2208 if (input->tell()!=endPos)
2209 ascFile.addDelimiter(input->tell(), '|');
2210 ascFile.addPos(pos);
2211 ascFile.addNote(f.str().c_str());
2212
2213 return true;
2214 }
2215
2216 ////////////////////////////////////////////////////////////
2217 // Unknown
2218 ////////////////////////////////////////////////////////////
2219
2220 bool QuattroParser::readBlockList(std::shared_ptr<WPSStream> stream)
2221 {
2222 RVNGInputStreamPtr input = stream->m_input;
2223 libwps::DebugFile &ascFile=stream->m_ascii;
2224 libwps::DebugStream f;
2225
2226 long pos = input->tell();
2227 auto type = long(libwps::readU16(input)&0x7fff);
2228 int N=0;
2229 auto sz = long(libwps::readU16(input));
2230 long endPos=pos+4+sz;
2231 int extraSize=0;
2232 switch (type)
2233 {
2234 case 0x18: // table, extra value: 0,1,2: 0,1,2 free variables
2235 case 0x19: // query, extra value: 0-4: meaning none,find,extract,delete, unique
2236 N=3;
2237 extraSize=2;
2238 break;
2239 case 0x1a: // print block
2240 N=1;
2241 break;
2242 case 0x1b: // sort block
2243 N=1;
2244 extraSize=4; // f0=0(numbers first),1(labels first), f1=0(dictionnary),1(ascii) order
2245 break;
2246 case 0x1c: // fill range
2247 N=1;
2248 break;
2249 case 0x1d: // key
2250 case 0x23:
2251 case 0x9f:
2252 case 0xa0:
2253 case 0xa1:
2254 N=1;
2255 extraSize=2; // 0: ascending, 1: descending when sz==12
2256 break;
2257 case 0x20: // frequency
2258 case 0x66: // parse
2259 N=2;
2260 break;
2261 case 0x67: // regression
2262 N=3;
2263 extraSize=2; // 0: compute-y-intersect, 1: y-intersect is 0
2264 break;
2265 case 0x69: // matrix
2266 N=5;
2267 break;
2268 case 0xb7: // solve
2269 N=2;
2270 extraSize=18;
2271 break;
2272 case 0xf8: // print left
2273 case 0xf9: // print top
2274 N=1;
2275 break;
2276 case 0x10d: // show compatible
2277 N=1;
2278 break;
2279 case 0x2bf: // X label
2280 case 0x2c0: // Z label
2281 case 0x2c1: // legend
2282 case 0x2c6: // serie data
2283 case 0x2c7: // serie legend
2284 N=1;
2285 break;
2286 default:
2287 break;
2288 }
2289 bool fixedSize10=10*N+extraSize==sz;
2290 for (int i=0; i<N; ++i)
2291 {
2292 QuattroFormulaInternal::CellReference instr;
2293 long actPos=input->tell();
2294 if (!m_spreadsheetParser->readCellReference(stream, endPos, instr))
2295 {
2296 WPS_DEBUG_MSG(("QuattroParser::readBlockList: can not read a reference\n"));
2297 f << "###";
2298 input->seek(actPos, librevenge::RVNG_SEEK_SET);
2299 ascFile.addPos(pos);
2300 ascFile.addNote(f.str().c_str());
2301
2302 return true;
2303 }
2304 if (!instr.empty())
2305 f << "cell" << i << "=" << instr << ",";
2306 if (fixedSize10)
2307 input->seek(actPos+10, librevenge::RVNG_SEEK_SET);
2308 }
2309 long remainSize=endPos-input->tell();
2310 if (type==0xb7 && (remainSize==2 || remainSize==18))
2311 {
2312 for (int i=0; i<(remainSize==2 ? 0 : 2); ++i) // target, accuracy
2313 {
2314 long actPos=input->tell();
2315 double val;
2316 bool isNaN;
2317 if (libwps::readDouble8(input, val, isNaN))
2318 f << "f" << i << "=" << val << ",";
2319 else
2320 f << "###f" << i << ",";
2321 input->seek(actPos+8, librevenge::RVNG_SEEK_SET);
2322 }
2323 f << "max[iter]=" << libwps::readU16(input) << ",";
2324 }
2325 else if (remainSize!=extraSize)
2326 {
2327 ascFile.addDelimiter(input->tell(),'|');
2328 WPS_DEBUG_MSG(("QuattroParser::readBlockList: unexpected extra data\n"));
2329 f << "###";
2330 }
2331 else
2332 {
2333 for (int i=0; i<extraSize/2; ++i)
2334 {
2335 auto val=int(libwps::read16(input));
2336 if (val) f << "f" << i << "=" << val << ",";
2337 }
2338 }
2339 ascFile.addPos(pos);
2340 ascFile.addNote(f.str().c_str());
2341
2342 return true;
2343 }
2344
2345 bool QuattroParser::readCellPosition(std::shared_ptr<WPSStream> stream)
2346 {
2347 RVNGInputStreamPtr input = stream->m_input;
2348 libwps::DebugFile &ascFile=stream->m_ascii;
2349 libwps::DebugStream f;
2350 long pos = input->tell();
2351 auto type = int(libwps::readU16(input)&0x7fff);
2352
2353 if (type != 0x96)
2354 {
2355 WPS_DEBUG_MSG(("QuattroParser::readCellPosition: not a cell position zone\n"));
2356 return false;
2357 }
2358 auto sz = long(libwps::readU16(input));
2359 if (sz%6)
2360 {
2361 WPS_DEBUG_MSG(("QuattroParser::readCellPosition: size seems very bad\n"));
2362 f << "###";
2363 ascFile.addPos(pos);
2364 ascFile.addNote(f.str().c_str());
2365 return true;
2366 }
2367 auto N=int(sz/6);
2368 for (int i=0; i<N; ++i)
2369 {
2370 int cellPos[3]; // col, rowMin, rowMax
2371 for (auto &c : cellPos) c=int(libwps::readU16(input));
2372 f << "C" << cellPos[0] << "[" << cellPos[1] << "->" << cellPos[2] << "],";
2373 }
2374 ascFile.addPos(pos);
2375 ascFile.addNote(f.str().c_str());
2376 return true;
2377 }
2378
2379 bool QuattroParser::readPaneAttribute(std::shared_ptr<WPSStream> stream)
2380 {
2381 RVNGInputStreamPtr input = stream->m_input;
2382 libwps::DebugFile &ascFile=stream->m_ascii;
2383 libwps::DebugStream f;
2384 long pos = input->tell();
2385 auto type = int(libwps::readU16(input)&0x7fff);
2386
2387 if (type != 0xd1)
2388 {
2389 WPS_DEBUG_MSG(("QuattroParser::readPaneAttribute: not a attribute zone\n"));
2390 return false;
2391 }
2392 auto sz = long(libwps::readU16(input));
2393 if (sz<30)
2394 {
2395 WPS_DEBUG_MSG(("QuattroParser::readPaneAttribute: size seems very bad\n"));
2396 f << "###";
2397 ascFile.addPos(pos);
2398 ascFile.addNote(f.str().c_str());
2399 return true;
2400 }
2401 int val=int(libwps::readU8(input));
2402 if (val==0)
2403 f << "diplay[no],";
2404 else if (val!=1)
2405 f << "##display=" << val << ",";
2406 val=int(libwps::readU8(input));
2407 WPSColor color;
2408 if (!getColor(val, color))
2409 f << "##lineColor=" << val << ",";
2410 else if (!color.isBlack())
2411 f << "lineColor=" << color << ",";
2412 val=int(libwps::readU16(input));
2413 switch (val)
2414 {
2415 case 0:
2416 f << "lab[align]=default,";
2417 break;
2418 case 1:
2419 break;
2420 case 2:
2421 f << "lab[align]=center,";
2422 break;
2423 case 3:
2424 f << "lab[align]=right,";
2425 break;
2426 default:
2427 f << "##lab[align]=" << val << ",";
2428 break;
2429 }
2430 val=int(libwps::readU16(input));
2431 switch (val)
2432 {
2433 case 0:
2434 f << "number[align]=default,";
2435 break;
2436 case 1:
2437 f << "number[align]=left,";
2438 break;
2439 case 2:
2440 f << "number[align]=center,";
2441 break;
2442 case 3:
2443 break;
2444 default:
2445 f << "##number[align]=" << val << ",";
2446 break;
2447 }
2448 val=int(libwps::readU16(input));
2449 if (val==1)
2450 {
2451 f << "has[cond],";
2452 for (int i=0; i<2; ++i)
2453 {
2454 double value;
2455 bool isNaN;
2456 if (libwps::readDouble8(input, value, isNaN))
2457 f << "cond" << i << "=" << value << ",";
2458 else
2459 f << "###cond" << i << ",";
2460 }
2461 }
2462 else if (val)
2463 f << "##has[cond]=" << val << ",";
2464 input->seek(pos+4+8+16, librevenge::RVNG_SEEK_SET);
2465 f << "colors=[";
2466 for (int i=0; i<4; ++i) // color < cond0, normal, color> cond1, error
2467 {
2468 val=int(libwps::readU8(input));
2469 int const expected[]= {4,3,5,4};
2470 if (val==expected[i])
2471 f << "_,";
2472 else if (!getColor(val, color))
2473 f << "##" << val << ",";
2474 else
2475 f << color << ",";
2476 }
2477 f << "],";
2478 input->seek(2, librevenge::RVNG_SEEK_CUR); // reserved
2479 if (sz!=30) // find sometimes +4|8 bytes
2480 ascFile.addDelimiter(input->tell(),'|');
2481 ascFile.addPos(pos);
2482 ascFile.addNote(f.str().c_str());
2483 return true;
2484 }
2485
2486 bool QuattroParser::readSerieExtension(std::shared_ptr<WPSStream> stream)
2487 {
2488 RVNGInputStreamPtr input = stream->m_input;
2489 libwps::DebugFile &ascFile=stream->m_ascii;
2490 libwps::DebugStream f;
2491 long pos = input->tell();
2492 auto type = int(libwps::readU16(input)&0x7fff);
2493
2494 if (type != 0x2dc)
2495 {
2496 WPS_DEBUG_MSG(("QuattroParser::readSerieExtension: not a extension zone\n"));
2497 return false;
2498 }
2499 auto sz = long(libwps::readU16(input));
2500 long endPos=pos+4+sz;
2501 if (sz<6)
2502 {
2503 WPS_DEBUG_MSG(("QuattroParser::readSerieExtension: size seems very bad\n"));
2504 f << "###";
2505 ascFile.addPos(pos);
2506 ascFile.addNote(f.str().c_str());
2507 return true;
2508 }
2509 int val=int(libwps::readU16(input));
2510 if (val==1)
2511 f << "ysecondary,";
2512 else if (val)
2513 f << "##ysecondaty=" << val << ",";
2514 val=int(libwps::readU16(input));
2515 if (val>=1 && val<5)
2516 {
2517 char const *wh[]= {nullptr, "bar", "line", "area", "high-low"};
2518 f << wh[val] << ",";
2519 }
2520 else if (val)
2521 f << "#override=" << val << ",";
2522 input->seek(2, librevenge::RVNG_SEEK_CUR);
2523 if (sz<10)
2524 {
2525 ascFile.addPos(pos);
2526 ascFile.addNote(f.str().c_str());
2527 return true;
2528 }
2529 val=int(libwps::readU16(input));
2530 if (val) f << "f0=" << val << ",";
2531 int dSz=int(libwps::readU16(input));
2532 if (dSz+5>sz || dSz<4)
2533 {
2534 f << "###dSz=" << dSz << ",";
2535 WPS_DEBUG_MSG(("QuattroParser::readSerieExtension: can not read the size extension\n"));
2536 ascFile.addDelimiter(input->tell(),'|');
2537 ascFile.addPos(pos);
2538 ascFile.addNote(f.str().c_str());
2539 return true;
2540 }
2541 val=int(libwps::readU16(input));
2542 if (val) f << "type=" << val << ","; // 0: aggregation, 1: moving aggregation, 2: linear, 3: expon
2543 val=int(libwps::readU16(input));
2544 if (val&1) f << "filter[in legend],";
2545 if (val&2) f << "filter[in table],";
2546 if (val&4) f << "table[can be increased],";
2547 val &= 0xfff8;
2548 if (val) f << "fl=" << std::hex << val << std::dec << ",";
2549 if (input->tell()!=endPos)
2550 ascFile.addDelimiter(input->tell(),'|');
2551 ascFile.addPos(pos);
2552 ascFile.addNote(f.str().c_str());
2553 return true;
2554 }
2555
2556 bool QuattroParser::readZone341(std::shared_ptr<WPSStream> stream)
2557 {
2558 if (m_state->m_readingZone341)
2559 {
2560 WPS_DEBUG_MSG(("QuattroParser::readZone341: recursive call\n"));
2561 return false;
2562 }
2563 RVNGInputStreamPtr input = stream->m_input;
2564 libwps::DebugFile &ascFile=stream->m_ascii;
2565 libwps::DebugStream f;
2566 long pos = input->tell();
2567 auto type = int(libwps::readU16(input)&0x7fff);
2568 if (type != 0x341)
2569 {
2570 WPS_DEBUG_MSG(("QuattroParser::readZone341: not a 341 zone\n"));
2571 return false;
2572 }
2573 auto sz = long(libwps::readU16(input));
2574 long endPos=pos+4+sz;
2575 int const headerSize=version()>=1003 ? 82 : 75;
2576 if (sz<headerSize)
2577 {
2578 WPS_DEBUG_MSG(("QuattroParser::readZone341: size seems very bad\n"));
2579 f << "###";
2580 ascFile.addPos(pos);
2581 ascFile.addNote(f.str().c_str());
2582 return true;
2583 }
2584 ascFile.addDelimiter(input->tell(),'|');
2585 input->seek(pos+4+headerSize, librevenge::RVNG_SEEK_SET);
2586 ascFile.addPos(pos);
2587 ascFile.addNote(f.str().c_str());
2588
2589 while (input->tell()+4<=endPos)
2590 {
2591 pos=input->tell();
2592 bool end=(libwps::readU16(input)&0x7fff)==0x31f;
2593 input->seek(pos,librevenge::RVNG_SEEK_SET);
2594 m_state->m_readingZone341 = true;
2595 const bool ok = readZone(stream);
2596 m_state->m_readingZone341 = false;
2597 if (!ok || input->tell()>endPos)
2598 {
2599 WPS_DEBUG_MSG(("QuattroParser::readZone341: find extra data\n"));
2600 ascFile.addPos(pos);
2601 ascFile.addNote("Zone341:###extra");
2602 return true;
2603 }
2604 if (end)
2605 {
2606 if (input->tell()<endPos)
2607 {
2608 ascFile.addPos(input->tell());
2609 ascFile.addNote("_");
2610 }
2611 return true;
2612 }
2613 }
2614 if (sz!=headerSize)
2615 {
2616 WPS_DEBUG_MSG(("QuattroParser::readZone341: oops, does not find end zone\n"));
2617 }
2618 return true;
2619 }
2620
2621 ////////////////////////////////////////////////////////////
2622 // ole stream
2623 ////////////////////////////////////////////////////////////
2624 bool QuattroParser::readOleLinkInfo(std::shared_ptr<WPSStream> stream, librevenge::RVNGString &link)
2625 {
2626 if (!stream || !stream->checkFilePosition(4))
2627 {
2628 WPS_DEBUG_MSG(("QuattroParser::readLinkInfo: unexpected zone\n"));
2629 return false;
2630 }
2631 RVNGInputStreamPtr input = stream->m_input;
2632 libwps::DebugFile &ascFile=stream->m_ascii;
2633 libwps::DebugStream f;
2634 f << "Entries(LinkInfo):";
2635 int val=libwps::readU8(input);
2636 if (val!=0x53)
2637 f << "f0=" << std::hex << val << std::dec << ",";
2638 val=libwps::readU16(input); // 2 or 3
2639 if (val) f << "f1=" << val << ",";
2640 if (!readCString(stream, link, stream->m_eof-3))
2641 {
2642 WPS_DEBUG_MSG(("QuattroParser::readLinkInfo: can not read the link\n"));
2643 f << "##link,";
2644 ascFile.addPos(0);
2645 ascFile.addNote(f.str().c_str());
2646 return false;
2647 }
2648 if (!link.empty())
2649 f << "link=" << link.cstr() << ",";
2650 ascFile.addPos(0);
2651 ascFile.addNote(f.str().c_str());
2652 return true;
2653 }
2654
2655 bool QuattroParser::readOleBOlePart(std::shared_ptr<WPSStream> stream)
2656 {
2657 if (!stream || !stream->checkFilePosition(20))
2658 {
2659 WPS_DEBUG_MSG(("QuattroParser::readOleBOlePart: unexpected zone\n"));
2660 return false;
2661 }
2662 RVNGInputStreamPtr input = stream->m_input;
2663 libwps::DebugFile &ascFile=stream->m_ascii;
2664 libwps::DebugStream f;
2665 f << "Entries(BOlePart):";
2666 for (int i=0; i<5; ++i) // f0=1, f1=f2=small int(often 1), f3=f4=small int(often 1)
2667 {
2668 auto val=int(libwps::read32(input));
2669 if (val!=1) f << "f" << i << "=" << val << ",";
2670 }
2671 ascFile.addPos(0);
2672 ascFile.addNote(f.str().c_str());
2673 return true;
2674 }
2675
2676 ////////////////////////////////////////////////////////////
2677 // decode
2678 ////////////////////////////////////////////////////////////
2679 RVNGInputStreamPtr QuattroParser::decodeStream(RVNGInputStreamPtr input, std::vector<uint8_t> const &key) const
2680 {
2681 int const vers=version();
2682 if (!input || key.size()!=16)
2683 {
2684 WPS_DEBUG_MSG(("QuattroParser::decodeStream: the arguments seems bad\n"));
2685 return RVNGInputStreamPtr();
2686 }
2687 long actPos=input->tell();
2688 input->seek(0,librevenge::RVNG_SEEK_SET);
2689 librevenge::RVNGBinaryData data;
2690 if (!libwps::readDataToEnd(input, data) || !data.getDataBuffer())
2691 {
2692 WPS_DEBUG_MSG(("QuattroParser::decodeStream: can not read the original input\n"));
2693 return RVNGInputStreamPtr();
2694 }
2695 auto *buf=const_cast<unsigned char *>(data.getDataBuffer());
2696 auto endPos=long(data.size());
2697 input->seek(actPos,librevenge::RVNG_SEEK_SET);
2698 uint32_t d7=0;
2699 std::stack<long> stack;
2700 stack.push(endPos);
2701 const int zone341Size=vers<=1002 ? 75 : 82;
2702 while (!input->isEnd() && !stack.empty())
2703 {
2704 long pos=input->tell();
2705 if (pos+4>stack.top()) break;
2706 auto id=int(libwps::readU16(input)&0x7fff);
2707 auto sSz=int(libwps::readU16(input));
2708 if (pos+4+sSz>stack.top())
2709 {
2710 input->seek(pos,librevenge::RVNG_SEEK_SET);
2711 break;
2712 }
2713 if (id==0x341 && sSz>zone341Size)
2714 {
2715 // special case a container with header of size 75 + different subzones
2716 stack.push(pos+4+sSz);
2717 sSz=zone341Size; // transform only the header
2718 }
2719 for (int i=0; i<sSz; ++i)
2720 {
2721 auto c=uint8_t(libwps::readU8(input));
2722 c=(c^key[(d7++)&0xf]);
2723 buf[pos+4+i]=uint8_t((c>>5)|(c<<3));
2724 }
2725 // main zone ends with zone 1, zone 341 ends with zone 31f
2726 if (id==(stack.size()==1 ? 1 : 0x31f))
2727 {
2728 input->seek(stack.top(),librevenge::RVNG_SEEK_SET);
2729 stack.pop();
2730 }
2731 }
2732 if (input->tell()!=endPos)
2733 {
2734 WPS_DEBUG_MSG(("QuattroParser::decodeStream: can not decode the end of the file, data may be bad %lx %lx\n", static_cast<unsigned long>(input->tell()), static_cast<unsigned long>(endPos)));
2735 }
2736 RVNGInputStreamPtr res(new WPSStringStream(data.getDataBuffer(), static_cast<unsigned int>(endPos)));
2737 res->seek(actPos, librevenge::RVNG_SEEK_SET);
2738 return res;
2739 }
2740 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */