Debian Codegraph @

src/lib/WKS4Format.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 <cmath>
26 #include <sstream>
27
28 #include <librevenge-stream/librevenge-stream.h>
29
30 #include "libwps_internal.h"
31 #include "libwps_tools_win.h"
32
33 #include "WKSContentListener.h"
34 #include "WKSSubDocument.h"
35
36 #include "WKS4Format.h"
37
38 #include "WKS4.h"
39
40 using namespace libwps;
41
42 //! Internal: namespace to define internal class of WKS4Format
43 namespace WKS4FormatInternal
44 {
45 //! small struct used to defined a font name
46 struct FontName
47 {
48 //! constructor
49 FontName()
50 : m_name()
51 , m_id(-2)
52 {
53 for (int &i : m_size) i=0;
54 }
55 //! the font name
56 std::string m_name;
57 //! the font id
58 int m_id;
59 //! the font height, font size
60 int m_size[2];
61 };
62
63 //! the state of WKS4Format
64 struct State
65 {
66 //! constructor
67 explicit State()
68 : m_eof(-1)
69 , m_idFontNameMap()
70 {
71 }
72
73 //! the last file position
74 long m_eof;
75 //! a map id to font name style
76 std::map<int, FontName> m_idFontNameMap;
77 };
78
79 }
80
81 // constructor, destructor
82 WKS4Format::WKS4Format(WKS4Parser &parser, RVNGInputStreamPtr const &input)
83 : m_input(input)
84 , m_mainParser(parser)
85 , m_state(new WKS4FormatInternal::State())
86 , m_asciiFile()
87 {
88 }
89
90 WKS4Format::~WKS4Format()
91 {
92 }
93
94 bool WKS4Format::checkFilePosition(long pos)
95 {
96 if (m_state->m_eof < 0)
97 {
98 long actPos = m_input->tell();
99 m_input->seek(0, librevenge::RVNG_SEEK_END);
100 m_state->m_eof=m_input->tell();
101 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
102 }
103 return pos <= m_state->m_eof;
104 }
105
106
107 // main function to parse the document
108 bool WKS4Format::parse()
109 {
110 if (!m_input)
111 {
112 WPS_DEBUG_MSG(("WKS4Format::parse: does not find main file\n"));
113 return false;
114 }
115
116 if (!checkHeader()) return false;
117
118 bool ok=false;
119 try
120 {
121 ascii().setStream(m_input);
122 ascii().open("FMT");
123
124 ok = checkHeader() && readZones();
125 }
126 catch (...)
127 {
128 WPS_DEBUG_MSG(("WKS4Format::parse: exception catched when parsing FMT\n"));
129 return false;
130 }
131
132 ascii().reset();
133 return ok;
134 }
135
136 ////////////////////////////////////////////////////////////
137 // low level
138 ////////////////////////////////////////////////////////////
139 // read the header
140 ////////////////////////////////////////////////////////////
141 bool WKS4Format::checkHeader(bool strict)
142 {
143 m_state.reset(new WKS4FormatInternal::State);
144 libwps::DebugStream f;
145
146 if (!checkFilePosition(12))
147 {
148 WPS_DEBUG_MSG(("WKS4Format::checkHeader: file is too short\n"));
149 return false;
150 }
151
152 m_input->seek(0,librevenge::RVNG_SEEK_SET);
153 auto firstOffset = int(libwps::readU8(m_input));
154 auto type = int(libwps::read8(m_input));
155 f << "FileHeader:FMT,";
156 if (firstOffset != 0 || type != 0)
157 {
158 WPS_DEBUG_MSG(("WKS4Format::checkHeader: find unexpected first data\n"));
159 return false;
160 }
161 auto val=int(libwps::read16(m_input));
162 if (val==2)
163 {
164 // version
165 val=int(libwps::readU16(m_input));
166 if (val!=0x8006)
167 {
168 WPS_DEBUG_MSG(("WKS4Format::checkHeader: find unknown file version\n"));
169 return false;
170 }
171 }
172 else
173 {
174 WPS_DEBUG_MSG(("WKS4Format::checkHeader: header contain unexpected size field data\n"));
175 return false;
176 }
177
178 m_input->seek(0, librevenge::RVNG_SEEK_SET);
179 if (strict)
180 {
181 for (int i=0; i < 4; ++i)
182 {
183 if (!readZone()) return false;
184 }
185 }
186 ascii().addPos(0);
187 ascii().addNote(f.str().c_str());
188
189 return true;
190 }
191
192 bool WKS4Format::readZones()
193 {
194 m_input->seek(0, librevenge::RVNG_SEEK_SET);
195 while (readZone()) ;
196
197 //
198 // look for ending
199 //
200 long pos = m_input->tell();
201 if (!checkFilePosition(pos+4))
202 {
203 WPS_DEBUG_MSG(("WKS4Format::readZones: cell header is too short\n"));
204 return false;
205 }
206 auto type = int(libwps::readU16(m_input)); // 1
207 auto length = int(libwps::readU16(m_input));
208 if (length)
209 {
210 WPS_DEBUG_MSG(("WKS4Format::readZones: parse breaks before ending\n"));
211 ascii().addPos(pos);
212 ascii().addNote("Entries(BAD):###");
213 return false;
214 }
215
216 ascii().addPos(pos);
217 if (type != 1)
218 {
219 WPS_DEBUG_MSG(("WKS4Format::readZones: odd end cell type: %d\n", type));
220 ascii().addNote("Entries(BAD):###");
221 }
222 else
223 ascii().addNote("__End");
224
225 return true;
226 }
227
228 bool WKS4Format::readZone()
229 {
230 libwps::DebugStream f;
231 long pos = m_input->tell();
232 auto id = int(libwps::readU8(m_input));
233 auto type = int(libwps::read8(m_input));
234 auto sz = long(libwps::readU16(m_input));
235 if (sz<0 || !checkFilePosition(pos+4+sz))
236 {
237 WPS_DEBUG_MSG(("WKS4Format::readZone: size is bad\n"));
238 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
239 return false;
240 }
241
242 f << "Entries(FMT" << std::hex << id << std::dec << "E):";
243 bool ok = true, isParsed = false, needWriteInAscii = false;
244 int val;
245 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
246 switch (type)
247 {
248 case 0:
249 switch (id)
250 {
251 case 0:
252 if (sz!=2) break;
253 m_input->seek(pos+4, librevenge::RVNG_SEEK_SET);
254 f.str("");
255 f << "version=" << std::hex << libwps::readU16(m_input) << std::dec << ",";
256 isParsed=needWriteInAscii=true;
257 break;
258 case 0x1: // EOF
259 ok = false;
260 break;
261 // boolean
262 case 0x2: // Calculation mode 0 or FF
263 case 0x83: // always with 0, can also be a string
264 case 0x84: // always with 0, can also be a string
265 case 0x85: // always with 0, can also be a string
266 case 0x93: // 4
267 case 0x96: // 0 or FF
268 case 0x97: // 5F
269 case 0x98: // 0|2|3
270 case 0x99: // 0|4 or FF
271 case 0x9c: // 0
272 case 0xa3: // 0 or FF
273 f.str("");
274 if (id==2)
275 f << "Entries(Byte2Z):";
276 else
277 f << "Entries(FMTByte" << std::hex << id << std::dec << "Z):";
278 if (sz!=1)
279 {
280 f << "###";
281 break;
282 }
283 m_input->seek(pos+4, librevenge::RVNG_SEEK_SET);
284 val=int(libwps::readU8(m_input));
285 if (val==0xFF) f << "true,";
286 else if (val) f << "#val=" << val << ",";
287 isParsed=needWriteInAscii=true;
288 break;
289 case 0x87: // always with 0000
290 case 0x88: // always with 0000
291 case 0x8e: // with 57|64
292 case 0x9a: // with 800
293 case 0x9b: // with 720
294 f.str("");
295 f << "Entries(FMTInt" << std::hex << id << std::dec << "Z):";
296 if (sz!=2)
297 {
298 f << "###";
299 break;
300 }
301 m_input->seek(pos+4, librevenge::RVNG_SEEK_SET);
302 val=int(libwps::readU16(m_input));
303 if (val) f << "val=" << val << ",";
304 isParsed=needWriteInAscii=true;
305 break;
306 case 0x86:
307 case 0x89:
308 case 0xba: // header?
309 case 0xbb: // footer?
310 {
311 f.str("");
312 if (id==0x86)
313 f << "Entries(FMTPrinter):";
314 else if (id==0x89)
315 f << "Entries(FMTPrinter):shortName,";
316 else if (id==0xba)
317 f << "Entries(FMTHeader):";
318 else
319 f << "Entries(FMTFooter):";
320 if (sz<1)
321 {
322 f << "###";
323 break;
324 }
325 m_input->seek(pos+4, librevenge::RVNG_SEEK_SET);
326 std::string text;
327 for (long i=0; i<sz; ++i) text+=char(libwps::readU8(m_input));
328 f << text << ",";
329 isParsed=needWriteInAscii=true;
330 break;
331 }
332 case 0xae:
333 isParsed=readFontName();
334 break;
335 case 0xaf:
336 case 0xb1:
337 isParsed=readFontSize();
338 break;
339 case 0xb0:
340 isParsed=readFontId();
341 break;
342 case 0xb8: // always 0101
343 f.str("");
344 f << "Entries(FMTInts" << std::hex << id << std::dec << "Z):";
345 if (sz!=2)
346 {
347 f << "###";
348 break;
349 }
350 m_input->seek(pos+4, librevenge::RVNG_SEEK_SET);
351 for (int i=0; i<2; ++i)
352 {
353 val=int(libwps::readU8(m_input));
354 if (val!=1) f << "f" << i << "=" << val << ",";
355 }
356 isParsed=needWriteInAscii=true;
357 break;
358
359 default:
360 break;
361 }
362 break;
363 default:
364 ok = false;
365 break;
366 }
367
368 if (!ok)
369 {
370 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
371 return false;
372 }
373 if (isParsed)
374 {
375 if (needWriteInAscii)
376 {
377 ascii().addPos(pos);
378 ascii().addNote(f.str().c_str());
379 }
380 m_input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
381 return true;
382 }
383
384 if (sz && m_input->tell()!=pos && m_input->tell()!=pos+4+sz)
385 ascii().addDelimiter(m_input->tell(),'|');
386 m_input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
387 ascii().addPos(pos);
388 ascii().addNote(f.str().c_str());
389 return true;
390 }
391
392 ////////////////////////////////////////////////////////////
393 // Font
394 ////////////////////////////////////////////////////////////
395 bool WKS4Format::readFontName()
396 {
397 libwps::DebugStream f;
398
399 long pos = m_input->tell();
400 auto type = int(libwps::read16(m_input));
401 if (type!=0xae)
402 {
403 WPS_DEBUG_MSG(("WKS4Format::readFontName: not a font name definition\n"));
404 return false;
405 }
406 auto sz = long(libwps::readU16(m_input));
407 long endPos=pos+4+sz;
408 f << "Entries(FMTFont)[name]:";
409 if (sz < 2)
410 {
411 WPS_DEBUG_MSG(("WKS4Format::readFontName: the zone is too short\n"));
412 f << "###";
413 ascii().addPos(pos);
414 ascii().addNote(f.str().c_str());
415 return true;
416 }
417 auto id=int(libwps::readU8(m_input));
418 f << "id=" << id << ",";
419 bool nameOk=true;
420 std::string name("");
421 for (long i=1; i<sz; ++i)
422 {
423 auto c=char(libwps::readU8(m_input));
424 if (!c) break;
425 if (nameOk && !(c==' ' || (c>='0'&&c<='9') || (c>='a'&&c<='z') || (c>='A'&&c<='Z')))
426 {
427 nameOk=false;
428 WPS_DEBUG_MSG(("WKS4Format::readFontName: find odd character in name\n"));
429 f << "#";
430 }
431 name += c;
432 }
433 f << name << ",";
434 if (m_state->m_idFontNameMap.find(id)!=m_state->m_idFontNameMap.end())
435 {
436 WPS_DEBUG_MSG(("WKS4Format::readFontName: can not update font map for id=%d\n", id));
437 }
438 else
439 {
440 WKS4FormatInternal::FontName font;
441 font.m_name=name;
442 m_state->m_idFontNameMap[id]=font;
443 }
444 if (m_input->tell()!=endPos)
445 {
446 WPS_DEBUG_MSG(("WKS4Format::readFontName: find extra data\n"));
447 f << "###extra";
448 m_input->seek(endPos, librevenge::RVNG_SEEK_SET);
449 }
450 ascii().addPos(pos);
451 ascii().addNote(f.str().c_str());
452 return true;
453 }
454
455 bool WKS4Format::readFontId()
456 {
457 libwps::DebugStream f;
458
459 long pos = m_input->tell();
460 auto type = int(libwps::read16(m_input));
461 if (type!=0xb0)
462 {
463 WPS_DEBUG_MSG(("WKS4Format::readFontId: not a font id definition\n"));
464 return false;
465 }
466 auto sz = long(libwps::readU16(m_input));
467 long endPos=pos+4+sz;
468 f << "Entries(FMTFont)[ids]:";
469 if ((sz%2)!=0)
470 {
471 WPS_DEBUG_MSG(("WKS4Format::readFontId: the zone size is odd\n"));
472 f << "###";
473 ascii().addPos(pos);
474 ascii().addNote(f.str().c_str());
475 return true;
476 }
477 f << "ids=[";
478 bool isFirstError=true;
479 for (int i=0; i<int(sz)/2; ++i)
480 {
481 auto id =int(libwps::readU16(m_input));
482 f << id << ",";
483 if (m_state->m_idFontNameMap.find(i)!=m_state->m_idFontNameMap.end())
484 m_state->m_idFontNameMap.find(i)->second.m_id=id;
485 else if (isFirstError)
486 {
487 isFirstError=false;
488 WPS_DEBUG_MSG(("WKS4Format::readFontId: can not update some font map for id=%d\n", id));
489 }
490 }
491 f << "],";
492 if (m_input->tell()!=endPos)
493 {
494 WPS_DEBUG_MSG(("WKS4Format::readFontId: find extra data\n"));
495 f << "###extra";
496 m_input->seek(endPos, librevenge::RVNG_SEEK_SET);
497 }
498 ascii().addPos(pos);
499 ascii().addNote(f.str().c_str());
500 return true;
501 }
502
503 bool WKS4Format::readFontSize()
504 {
505 libwps::DebugStream f;
506
507 long pos = m_input->tell();
508 auto type = int(libwps::read16(m_input));
509 if (type!=0xaf && type!=0xb1)
510 {
511 WPS_DEBUG_MSG(("WKS4Format::readFontSize: not a font size definition\n"));
512 return false;
513 }
514 auto sz = long(libwps::readU16(m_input));
515 long endPos=pos+4+sz;
516 int const wh=type==0xaf ? 0 : 1;
517 f << "Entries(FMTFont)[size" << wh << "]:";
518 if ((sz%2)!=0)
519 {
520 WPS_DEBUG_MSG(("WKS4Format::readFontSize: the zone size is odd\n"));
521 f << "###";
522 ascii().addPos(pos);
523 ascii().addNote(f.str().c_str());
524 return true;
525 }
526 f << "size=[";
527 bool isFirstError=true;
528 for (int i=0; i<int(sz)/2; ++i)
529 {
530 auto size =int(libwps::readU16(m_input));
531 f << size << ",";
532 if (m_state->m_idFontNameMap.find(i)!=m_state->m_idFontNameMap.end())
533 m_state->m_idFontNameMap.find(i)->second.m_size[wh]=size;
534 else if (isFirstError)
535 {
536 isFirstError=false;
537 WPS_DEBUG_MSG(("WKS4Format::readFontSize: can not update some font map for size=%d\n", size));
538 }
539 }
540 f << "],";
541 if (m_input->tell()!=endPos)
542 {
543 WPS_DEBUG_MSG(("WKS4Format::readFontSize: find extra data\n"));
544 f << "###extra";
545 m_input->seek(endPos, librevenge::RVNG_SEEK_SET);
546 }
547 ascii().addPos(pos);
548 ascii().addNote(f.str().c_str());
549 return true;
550 }
551 ////////////////////////////////////////////////////////////
552 // Unknown
553 ////////////////////////////////////////////////////////////
554
555
556 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */