Debian Codegraph @

src/lib/MWAWRSRCParser.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <librevenge/librevenge.h>
35
36 #include "MWAWCell.hxx"
37 #include "MWAWInputStream.hxx"
38 #include "MWAWEntry.hxx"
39
40 #include "MWAWRSRCParser.hxx"
41
42 MWAWRSRCParser::MWAWRSRCParser(MWAWInputStreamPtr const &input)
43 : m_input(input)
44 , m_entryMap()
45 , m_asciiName("")
46 , m_asciiFile(MWAWInputStreamPtr())
47 , m_parsed(false)
48 {
49 }
50
51 MWAWRSRCParser::~MWAWRSRCParser()
52 {
53 #ifdef DEBUG
54 try {
55 libmwaw::DebugStream f;
56 for (auto const &it : m_entryMap) {
57 MWAWEntry const &tEntry = it.second;
58 if (tEntry.isParsed()) continue;
59
60 if (tEntry.type()=="CODE") { // skip the code...
61 ascii().skipZone(tEntry.begin()-4, tEntry.end()-1);
62 continue;
63 }
64 f.str("");
65 f << "Entries(RSRC" << tEntry.type() << "):" << tEntry.id();
66 ascii().addPos(tEntry.begin()-4);
67 ascii().addNote(f.str().c_str());
68 ascii().addPos(tEntry.end());
69 ascii().addNote("_");
70 }
71 }
72 catch (...) {
73 }
74 #endif
75
76 ascii().reset();
77 }
78
79 MWAWEntry MWAWRSRCParser::getEntry(std::string type, int id) const
80 {
81 if (!m_parsed)
82 const_cast<MWAWRSRCParser *>(this)->parse();
83 auto it = m_entryMap.lower_bound(type);
84 while (it != m_entryMap.end()) {
85 if (it->first != type)
86 break;
87 MWAWEntry const &tEntry = (it++)->second;
88 if (tEntry.id()==id)
89 return tEntry;
90 }
91
92 return MWAWEntry();
93 }
94
95 bool MWAWRSRCParser::parse()
96 {
97 if (m_parsed)
98 return !m_entryMap.empty();
99 m_parsed = true;
100 if (!m_input) return false;
101
102 if (m_asciiName.length()) {
103 ascii().setStream(m_input);
104 ascii().open("RSRC");
105 }
106 try {
107 libmwaw::DebugStream f;
108 m_input->seek(0, librevenge::RVNG_SEEK_SET);
109 long pos = m_input->tell();
110 MWAWEntry data, map;
111 data.setBegin(m_input->readLong(4));
112 map.setBegin(m_input->readLong(4));
113 data.setLength(m_input->readLong(4));
114 map.setLength(m_input->readLong(4));
115 // data.length()==0 can be ok, if no data
116 if (!map.valid() || (!data.valid()&&data.length()!=0)) {
117 MWAW_DEBUG_MSG(("MWAWRSRCParser::parse: can not read the header\n"));
118 return false;
119 }
120 long endPos = data.end() > map.end() ? data.end() : map.end();
121 m_input->seek(endPos, librevenge::RVNG_SEEK_SET);
122 if (m_input->tell() != endPos) {
123 MWAW_DEBUG_MSG(("MWAWRSRCParser::parse: stream seems too small\n"));
124 return false;
125 }
126 f << "Header:";
127 ascii().addPos(pos);
128 ascii().addNote(f.str().c_str());
129 ascii().addPos(16);
130 ascii().addNote("_");
131
132 if (!parseMap(map, data.begin())) return false;
133
134 #ifdef DEBUG_WITH_FILES
135 // try to parse the system reserved zone
136 m_input->seek(48, librevenge::RVNG_SEEK_SET);
137 int strSz=int(m_input->readULong(1));
138 if (data.begin()>=0x100 && map.begin()>=0x100 && (strSz>0 && strSz<32)) {
139 f.str("");
140 f << "Entries(RSRCPriv):name=";
141 pos=48;
142 std::string name;
143 for (int i=0; i<strSz; ++i) name+=char(m_input->readULong(1));
144 f << name << ",";
145 ascii().addDelimiter(m_input->tell(),'|');
146 ascii().addPos(pos);
147 ascii().addNote(f.str().c_str());
148 m_input->seek(pos+32, librevenge::RVNG_SEEK_SET);
149
150 f.str("");
151 f << "RSRCPriv-2:";
152 pos=m_input->tell();
153 int val=int(m_input->readLong(2));
154 if (val) f << "f0=" << val << ",";
155 for (int w=0; w<2; ++w) {
156 long aPos=m_input->tell();
157 std::string type;
158 for (int j=0; j<4; ++j) {
159 char c=char(m_input->readULong(1));
160 if (!c) break;
161 type+=c;
162 }
163 f << (w==0 ? "type" : "creator") << "=" << type << ",";
164 m_input->seek(aPos+4, librevenge::RVNG_SEEK_SET);
165 }
166 for (int i=0; i<12; ++i) { // f1=256, f2-3: small number
167 val=int(m_input->readLong(2));
168 if (val)
169 f << "f" << i+1 << "=" << val << ",";
170 }
171 auto date=m_input->readULong(4);
172 if (date) {
173 int Y, M, D, HH, MM, SS;
174 MWAWCellContent::double2Date(double(date/3600/24)+1460., Y, M, D); // change the reference date from 1/1/1904 to 1/1/1900
175 double time=double(date%(3600*24))/3600/24;
176 MWAWCellContent::double2Time(time, HH, MM, SS);
177 f << "date=" << D << "/" << M << "/" << Y << " " << HH << ":" << MM << ",";
178 }
179 f << "sz=[";
180 for (int i=0; i<2; ++i) {
181 val=int(m_input->readULong(4));
182 if (val)
183 f << val << ",";
184 else
185 f << "_,";
186 }
187 f << "],";
188 ascii().addPos(pos);
189 ascii().addNote(f.str().c_str());
190
191 ascii().addPos(m_input->tell());
192 ascii().addNote("_");
193 }
194 #endif
195 for (auto &it : m_entryMap) {
196 MWAWEntry &tEntry = it.second;
197 if (tEntry.begin()+4 >= data.end()) {
198 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseMap: can not read entry %s[%d]\n", tEntry.type().c_str(), tEntry.id()));
199 continue;
200 }
201 m_input->seek(tEntry.begin(), librevenge::RVNG_SEEK_SET);
202 tEntry.setBegin(tEntry.begin()+4);
203 const auto length = long(m_input->readULong(4));
204 if (tEntry.begin() + length > data.end()) {
205 MWAW_DEBUG_MSG(("MWAWRSRCParser::parse: entry %s[%d] has got invalid length %ld\n", tEntry.type().c_str(), tEntry.id(), length));
206 continue;
207 }
208 tEntry.setLength(length);
209 }
210
211 auto it = m_entryMap.lower_bound("vers");
212 while (it != m_entryMap.end()) {
213 if (it->first != "vers")
214 break;
215 MWAWEntry const &tEntry = (it++)->second;
216 Version vers;
217 parseVers(tEntry, vers);
218 }
219 it = m_entryMap.lower_bound("STR ");
220 while (it != m_entryMap.end()) {
221 if (it->first != "STR ")
222 break;
223 std::string str;
224 MWAWEntry const &tEntry = (it++)->second;
225 parseSTR(tEntry, str);
226 }
227 it = m_entryMap.lower_bound("STR#");
228 while (it != m_entryMap.end()) {
229 if (it->first != "STR#")
230 break;
231 std::vector<std::string> list;
232 MWAWEntry const &tEntry = (it++)->second;
233 parseSTRList(tEntry, list);
234 }
235 }
236 catch (...) {
237 MWAW_DEBUG_MSG(("MWAWRSRCParser::parse: can not parse the input\n"));
238 return false;
239 }
240 return true;
241 }
242
243 bool MWAWRSRCParser::parseMap(MWAWEntry const &entry, long dataBegin)
244 {
245 if (!m_input) return false;
246
247 if (entry.length() < 28) {
248 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseMap: entry map is two short\n"));
249 return false;
250 }
251
252 libmwaw::DebugStream f, f2;
253 m_input->seek(entry.begin()+24, librevenge::RVNG_SEEK_SET);
254 f << "Entries(RSRCMap):";
255 auto offsetTypes=long(m_input->readULong(2));
256 auto offsetNameLists=long(m_input->readULong(2));
257 auto numTypes = static_cast<int>(m_input->readULong(2));
258 if (offsetTypes+2 > entry.length() || offsetNameLists > entry.length()) {
259 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseMap: the offsets seems bad\n"));
260 return false;
261 }
262 // this case can appear if no data
263 if (numTypes == 0xFFFF)
264 numTypes = -1;
265 f << "N=" << numTypes+1;
266 ascii().addPos(entry.begin());
267 ascii().addNote(f.str().c_str());
268 if (m_input->tell() != entry.begin()+offsetTypes+2) {
269 ascii().addPos(m_input->tell());
270 ascii().addNote("_");
271 }
272
273 long pos = entry.begin()+offsetTypes+2;
274 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
275 if (pos+8*(numTypes+1) > entry.end()) {
276 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseMap: the type zones seems too short\n"));
277 return false;
278 }
279 std::vector<MWAWEntry> typesList;
280 for (int i = 0; i <= numTypes; i++) {
281 pos = m_input->tell();
282 f.str("");
283 f << "RSRCMap[Type" << i << "]:";
284 std::string type("");
285 for (int c = 0; c < 4; c++)
286 type+=char(m_input->readULong(1));
287 MWAWEntry tEntry;
288 tEntry.setType(type);
289 tEntry.setId(static_cast<int>(m_input->readULong(2))+1); // the number of entry
290 tEntry.setBegin(entry.begin()+offsetTypes+long(m_input->readULong(2)));
291 typesList.push_back(tEntry);
292 f << tEntry << ":" << std::hex << tEntry.begin() << std::dec << ",";
293 ascii().addPos(pos);
294 ascii().addNote(f.str().c_str());
295 m_input->seek(pos+8, librevenge::RVNG_SEEK_SET);
296 }
297 ascii().addPos(m_input->tell());
298 ascii().addNote("_");
299 for (auto const &tEntry : typesList) {
300 if (tEntry.begin()+12*tEntry.id() > entry.end()) {
301 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseMap: can not read entry %s[%d]\n", tEntry.type().c_str(), tEntry.id()));
302 continue;
303 }
304 m_input->seek(tEntry.begin(), librevenge::RVNG_SEEK_SET);
305 for (int n = 0; n < tEntry.id(); n++) {
306 pos = m_input->tell();
307 f.str("");
308 f << "RSRCMap[Rsrc" << n << "]:";
309 MWAWEntry rsrc(tEntry);
310 rsrc.setId(static_cast<int>(m_input->readLong(2)));
311 auto offset = long(m_input->readULong(2));
312 if (offset != 0xFFFF) {
313 std::string name("");
314 if (offset+offsetNameLists+1 <= entry.length()) {
315 long actPos = m_input->tell();
316 m_input->seek(entry.begin()+offset+offsetNameLists, librevenge::RVNG_SEEK_SET);
317 auto nSz = static_cast<int>(m_input->readULong(1));
318 if (offset+offsetNameLists+1+nSz <= entry.length()) {
319 for (int j = 0; j < nSz; j++)
320 name+=char(m_input->readULong(1));
321 f2.str("");
322 f2 << "nameList:" << name;
323 ascii().addPos(entry.begin()+offset+offsetNameLists);
324 ascii().addNote(f2.str().c_str());
325 }
326 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
327 }
328 if (!name.length()) {
329 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseMap: can not read name of entry %s[%d]\n", tEntry.type().c_str(), tEntry.id()));
330 f << "#listNamesOffset=" << std::hex << offset << std::dec << ",";
331 }
332 else {
333 rsrc.setName(name);
334 f << "name=" << name << ",";
335 }
336 }
337 unsigned long dOffset = m_input->readULong(4);
338 if (dOffset & 0xFF000000) {
339 f << "attributes=" << (dOffset>>12) << ",";
340 dOffset &= 0xFFFFFF;
341 }
342 rsrc.setBegin(dataBegin+long(dOffset));
343 m_entryMap.insert(std::multimap<std::string, MWAWEntry>::value_type(rsrc.type(), rsrc));
344 f << rsrc.type() << ":" << rsrc.id() << ",";
345 f << "pos=" << std::hex << rsrc.begin() << std::dec << ",";
346
347 ascii().addPos(pos);
348 ascii().addNote(f.str().c_str());
349 m_input->seek(pos+12, librevenge::RVNG_SEEK_SET);
350 }
351 }
352 if (offsetNameLists != entry.length()) {
353 ascii().addPos(entry.begin()+offsetNameLists);
354 ascii().addNote("RSRCMap[nameList]");
355 }
356 return true;
357 }
358
359 ////////////////////////////////////////////////////////////
360 // read a string resource
361 // -16396: application-missing name string resource
362 // -16397: application-missing string resource
363 ////////////////////////////////////////////////////////////
364 bool MWAWRSRCParser::parseSTR(MWAWEntry const &entry, std::string &str)
365 {
366 str="";
367 if (!m_input || !entry.valid()) {
368 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseSTR: entry is invalid\n"));
369 return false;
370 }
371 entry.setParsed(true);
372 libmwaw::DebugStream f;
373 m_input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
374 auto sz = long(m_input->readULong(1));
375 if (sz+1 > entry.length()) {
376 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseSTR: string length is too small\n"));
377 return false;
378 }
379 for (long i = 0; i < sz; i++) {
380 if (m_input->isEnd()) {
381 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseSTR: file is too short\n"));
382 return false;
383 }
384 str += char(m_input->readULong(1));
385 }
386 f << "Entries(RSRCSTR)[" << entry.type() << ":" << entry.id() << "]:" << str;
387 if (sz+1 != entry.length()) {
388 // can this happens ?
389 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseSTR: ARGGS multiple strings\n"));
390 ascii().addDelimiter(m_input->tell(),'|');
391 f << "###";
392 }
393 ascii().addPos(entry.begin()-4);
394 ascii().addNote(f.str().c_str());
395 return true;
396 }
397
398 bool MWAWRSRCParser::parseSTRList(MWAWEntry const &entry, std::vector<std::string> &list)
399 {
400 list.resize(0);
401 if (!m_input || !entry.valid() || entry.length()<2) {
402 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseSTRList: the entry is bad\n"));
403 return false;
404 }
405 entry.setParsed(true);
406 long pos = entry.begin();
407 long endPos = entry.end();
408 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
409
410 libmwaw::DebugStream f;
411 f << "Entries(RSRCListStr)[" << entry.type() << ":" << entry.id() << "]:";
412 auto N=static_cast<int>(m_input->readULong(2));
413 ascii().addPos(pos-4);
414 ascii().addNote(f.str().c_str());
415
416 for (int i = 0; i < N; i++) {
417 f.str("");
418 f << "RSRCListStr-" << i << ":";
419 pos = m_input->tell();
420 if (pos+1 > endPos) {
421 f << "###";
422 ascii().addPos(pos);
423 ascii().addNote(f.str().c_str());
424
425 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseSTRList: can not read string %d\n", i));
426 return false;
427 }
428 auto sz = static_cast<int>(m_input->readULong(1));
429 if (pos+1+sz > endPos) {
430 f.str("");
431 f << "###";
432 ascii().addPos(pos);
433 ascii().addNote(f.str().c_str());
434
435 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseSTRList: string size %d is bad\n", i));
436 return false;
437 }
438
439 std::string str("");
440 for (int c=0; c < sz; c++)
441 str += char(m_input->readULong(1));
442 list.push_back(str);
443 f << str << ",";
444
445 ascii().addPos(pos);
446 ascii().addNote(f.str().c_str());
447 }
448 return true;
449 }
450
451 bool MWAWRSRCParser::parseClut(MWAWEntry const &entry, std::vector<MWAWColor> &list)
452 {
453 list.resize(0);
454 if (!m_input || !entry.valid() || entry.length()<8) {
455 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseClut: the entry is bad\n"));
456 return false;
457 }
458 entry.setParsed(true);
459 long pos = entry.begin();
460 // skip seed
461 m_input->seek(pos+4, librevenge::RVNG_SEEK_SET);
462
463 libmwaw::DebugStream f;
464 f << "Entries(RSRCClut)[" << entry.type() << ":" << entry.id() << "]:";
465 auto flags = static_cast<int>(m_input->readULong(2));
466 if (flags==0x8000) f << "indexed,";
467 else if (flags) f << "#flags=" << std::hex << flags << ",";
468 auto N= static_cast<int>(m_input->readULong(2));
469 if (entry.length()==8+8*(N+1)) // N can be num or maxId
470 N++;
471 f << "N=" << N << ",";
472 if (entry.length()!=8+8*N) {
473 f << "###";
474 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseClut: find unexpected size/format\n"));
475 ascii().addPos(pos-4);
476 ascii().addNote(f.str().c_str());
477 return false;
478 }
479 ascii().addPos(pos-4);
480 ascii().addNote(f.str().c_str());
481
482 for (int i = 0; i < N; i++) {
483 pos = m_input->tell();
484 f.str("");
485 f << "RSRCClut-" << i << ":";
486 auto index = static_cast<int>(m_input->readULong(2));
487 if (index != i) {
488 static bool first=true;
489 if (first) {
490 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseClut: find some odd index value\n"));
491 first = false;
492 }
493 f << "#index=" << index << ",";
494 }
495 unsigned char col[3];
496 for (auto &c : col) c = static_cast<unsigned char>(m_input->readULong(2)>>8);
497 MWAWColor color(col[0],col[1],col[2]);
498 list.push_back(color);
499 f << color << ",";
500
501 ascii().addPos(pos);
502 ascii().addNote(f.str().c_str());
503 }
504 return true;
505 }
506
507 ////////////////////////////////////////////////////////////
508 // read the version:
509 // id=1 version of the appliction
510 // id=2 version of the file
511 ////////////////////////////////////////////////////////////
512 std::ostream &operator<< (std::ostream &o, MWAWRSRCParser::Version const &vers)
513 {
514 o << vers.m_string;
515 if (vers.m_versionString.length())
516 o << "(" << vers.m_versionString << ")";
517 o << ",";
518 o << "vers=" << vers.m_majorVersion;
519 if (vers.m_minorVersion)
520 o << "(" << vers.m_minorVersion << ")";
521 o << ",";
522 if (vers.m_countryCode)
523 o << "country=" << std::hex << vers.m_countryCode << std::dec << ",";
524 o << vers.m_extra;
525 return o;
526 }
527
528 bool MWAWRSRCParser::parseVers(MWAWEntry const &entry, Version &vers)
529 {
530 vers = Version();
531 if (!m_input || !entry.valid() || entry.length()<8 || !m_input->checkPosition(entry.end())) {
532 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseVers: entry is invalid\n"));
533 return false;
534 }
535 entry.setParsed(true);
536 libmwaw::DebugStream f;
537 m_input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
538 vers.m_majorVersion = static_cast<int>(m_input->readULong(1));
539 vers.m_minorVersion = static_cast<int>(m_input->readULong(1));
540 auto val = long(m_input->readULong(1));
541 if (val) f << "devStage=" << val << ",";
542 val = long(m_input->readULong(1));
543 if (val) f << "preReleaseLevel=" << std::hex << val << std::dec << ",";
544 vers.m_countryCode = static_cast<int>(m_input->readULong(2));
545 for (int i = 0; i < 2; i++) {
546 auto sz = static_cast<int>(m_input->readULong(1));
547 long pos = m_input->tell();
548 if (pos+sz > entry.end()) {
549 MWAW_DEBUG_MSG(("MWAWRSRCParser::parseVers: can not read strings %d\n",i));
550 return false;
551 }
552 std::string str("");
553 for (int c = 0; c < sz; c++)
554 str+=char(m_input->readULong(1));
555 if (i==0)
556 vers.m_versionString = str;
557 else
558 vers.m_string = str;
559 }
560 vers.m_extra = f.str();
561 f << "Entries(RSRCvers)[" << entry.id() << "]:" << vers;
562 ascii().addPos(entry.begin()-4);
563 ascii().addNote(f.str().c_str());
564 return true;
565 }
566
567 ////////////////////////////////////////////////////////////
568 // read a pict resource
569 ////////////////////////////////////////////////////////////
570 bool MWAWRSRCParser::parsePICT(MWAWEntry const &entry, librevenge::RVNGBinaryData &pict)
571 {
572 pict.clear();
573 if (!m_input || !entry.valid() || entry.length()<0xd) {
574 MWAW_DEBUG_MSG(("MWAWRSRCParser::parsePICT: entry is invalid\n"));
575 return false;
576 }
577
578 libmwaw::DebugStream f;
579 f << "Entries(RSRC" << entry.type() << ")[" << entry.id() << "]:";
580 m_input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
581 m_input->readDataBlock(entry.length(), pict);
582
583 #ifdef DEBUG_WITH_FILES
584 if (!entry.isParsed()) {
585 ascii().skipZone(entry.begin(), entry.end()-1);
586 libmwaw::DebugStream f2;
587 f2 << "RSRC-" << entry.type() << "_" << entry.id() << ".pct";
588 libmwaw::Debug::dumpFile(pict, f2.str().c_str());
589 }
590 #endif
591
592 ascii().addPos(entry.begin()-4);
593 ascii().addNote(f.str().c_str());
594
595 entry.setParsed(true);
596 return true;
597 }
598 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: