| 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 <algorithm> |
| 35 |
#include <iomanip> |
| 36 |
#include <iostream> |
| 37 |
#include <limits> |
| 38 |
#include <set> |
| 39 |
#include <sstream> |
| 40 |
|
| 41 |
#include <librevenge/librevenge.h> |
| 42 |
|
| 43 |
#include "MWAWTextListener.hxx" |
| 44 |
#include "MWAWFont.hxx" |
| 45 |
#include "MWAWFontConverter.hxx" |
| 46 |
#include "MWAWHeader.hxx" |
| 47 |
#include "MWAWParagraph.hxx" |
| 48 |
#include "MWAWPictData.hxx" |
| 49 |
#include "MWAWPosition.hxx" |
| 50 |
#include "MWAWPrinter.hxx" |
| 51 |
#include "MWAWRSRCParser.hxx" |
| 52 |
#include "MWAWSubDocument.hxx" |
| 53 |
|
| 54 |
#include "libmwaw_internal.hxx" |
| 55 |
|
| 56 |
#include "EDocParser.hxx" |
| 57 |
|
| 58 |
/** Internal: the structures of a EDocParser */ |
| 59 |
namespace EDocParserInternal |
| 60 |
{ |
| 61 |
//////////////////////////////////////// |
| 62 |
//! Internal: an index of a EDocParser |
| 63 |
struct Index { |
| 64 |
//! constructor |
| 65 |
Index() |
| 66 |
: m_levelId(0) |
| 67 |
, m_text("") |
| 68 |
, m_page(-1) |
| 69 |
, m_extra("") |
| 70 |
{ |
| 71 |
} |
| 72 |
//! operator<< |
| 73 |
friend std::ostream &operator<<(std::ostream &o, Index const &index) |
| 74 |
{ |
| 75 |
if (index.m_text.length()) o << "text=\"" << index.m_text << "\","; |
| 76 |
if (index.m_levelId) o << "levelId=" << index.m_levelId << ","; |
| 77 |
if (index.m_page>0) o << "page=" << index.m_page << ","; |
| 78 |
o << index.m_extra; |
| 79 |
return o; |
| 80 |
} |
| 81 |
//! the font id |
| 82 |
int m_levelId; |
| 83 |
//! the text |
| 84 |
std::string m_text; |
| 85 |
//! the page number |
| 86 |
int m_page; |
| 87 |
//! extra data |
| 88 |
std::string m_extra; |
| 89 |
}; |
| 90 |
|
| 91 |
//////////////////////////////////////// |
| 92 |
//! Internal: the state of a EDocParser |
| 93 |
struct State { |
| 94 |
//! constructor |
| 95 |
State() |
| 96 |
: m_compressed(false) |
| 97 |
, m_maxPictId(0) |
| 98 |
, m_idCPICMap() |
| 99 |
, m_idPICTMap() |
| 100 |
, m_indexList() |
| 101 |
, m_actPage(0) |
| 102 |
, m_numPages(0) |
| 103 |
, m_headerHeight(0) |
| 104 |
, m_footerHeight(0) |
| 105 |
{ |
| 106 |
} |
| 107 |
//! a flag to know if the data are compressed or not |
| 108 |
bool m_compressed; |
| 109 |
//! the maximum of picture to read |
| 110 |
int m_maxPictId; |
| 111 |
//! a map id -> cPIC zone |
| 112 |
std::map<int,MWAWEntry> m_idCPICMap; |
| 113 |
//! a map id -> PICT zone |
| 114 |
std::map<int,MWAWEntry> m_idPICTMap; |
| 115 |
//! the index list |
| 116 |
std::vector<Index> m_indexList; |
| 117 |
int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */; |
| 118 |
|
| 119 |
int m_headerHeight /** the header height if known */, |
| 120 |
m_footerHeight /** the footer height if known */; |
| 121 |
}; |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
//////////////////////////////////////////////////////////// |
| 126 |
// constructor/destructor, ... |
| 127 |
//////////////////////////////////////////////////////////// |
| 128 |
EDocParser::EDocParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 129 |
: MWAWTextParser(input, rsrcParser, header) |
| 130 |
, m_state() |
| 131 |
{ |
| 132 |
init(); |
| 133 |
} |
| 134 |
|
| 135 |
EDocParser::~EDocParser() |
| 136 |
{ |
| 137 |
} |
| 138 |
|
| 139 |
void EDocParser::init() |
| 140 |
{ |
| 141 |
resetTextListener(); |
| 142 |
|
| 143 |
m_state.reset(new EDocParserInternal::State); |
| 144 |
|
| 145 |
// no margins ( ie. the document is a set of picture corresponding to each page ) |
| 146 |
getPageSpan().setMargins(0.01); |
| 147 |
} |
| 148 |
|
| 149 |
MWAWInputStreamPtr EDocParser::rsrcInput() |
| 150 |
{ |
| 151 |
return getRSRCParser()->getInput(); |
| 152 |
} |
| 153 |
|
| 154 |
libmwaw::DebugFile &EDocParser::rsrcAscii() |
| 155 |
{ |
| 156 |
return getRSRCParser()->ascii(); |
| 157 |
} |
| 158 |
|
| 159 |
//////////////////////////////////////////////////////////// |
| 160 |
// new page |
| 161 |
//////////////////////////////////////////////////////////// |
| 162 |
void EDocParser::newPage(int number) |
| 163 |
{ |
| 164 |
if (number <= m_state->m_actPage || number > m_state->m_numPages) |
| 165 |
return; |
| 166 |
|
| 167 |
while (m_state->m_actPage < number) { |
| 168 |
m_state->m_actPage++; |
| 169 |
if (!getTextListener() || m_state->m_actPage == 1) |
| 170 |
continue; |
| 171 |
getTextListener()->insertBreak(MWAWTextListener::PageBreak); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
//////////////////////////////////////////////////////////// |
| 176 |
// the parser |
| 177 |
//////////////////////////////////////////////////////////// |
| 178 |
void EDocParser::parse(librevenge::RVNGTextInterface *docInterface) |
| 179 |
{ |
| 180 |
if (!getInput().get() || !getRSRCParser() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 181 |
bool ok = false; |
| 182 |
try { |
| 183 |
checkHeader(nullptr); |
| 184 |
ok = createZones(); |
| 185 |
if (ok) { |
| 186 |
createDocument(docInterface); |
| 187 |
sendContents(); |
| 188 |
#ifdef DEBUG |
| 189 |
flushExtra(); |
| 190 |
#endif |
| 191 |
} |
| 192 |
ascii().reset(); |
| 193 |
} |
| 194 |
catch (...) { |
| 195 |
MWAW_DEBUG_MSG(("EDocParser::parse: exception catched when parsing\n")); |
| 196 |
ok = false; |
| 197 |
} |
| 198 |
|
| 199 |
resetTextListener(); |
| 200 |
if (!ok) throw(libmwaw::ParseException()); |
| 201 |
} |
| 202 |
|
| 203 |
//////////////////////////////////////////////////////////// |
| 204 |
// create the document |
| 205 |
//////////////////////////////////////////////////////////// |
| 206 |
void EDocParser::createDocument(librevenge::RVNGTextInterface *documentInterface) |
| 207 |
{ |
| 208 |
if (!documentInterface) return; |
| 209 |
if (getTextListener()) { |
| 210 |
MWAW_DEBUG_MSG(("EDocParser::createDocument: listener already exist\n")); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
// update the page |
| 215 |
m_state->m_actPage = 0; |
| 216 |
|
| 217 |
// create the page list |
| 218 |
int numPages = m_state->m_maxPictId; |
| 219 |
if (!m_state->m_indexList.empty()) |
| 220 |
numPages++; |
| 221 |
if (numPages <= 0) numPages=1; |
| 222 |
m_state->m_numPages=numPages; |
| 223 |
|
| 224 |
MWAWPageSpan ps(getPageSpan()); |
| 225 |
ps.setPageSpan(numPages+1); |
| 226 |
std::vector<MWAWPageSpan> pageList(1,ps); |
| 227 |
// |
| 228 |
MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface)); |
| 229 |
setTextListener(listen); |
| 230 |
listen->startDocument(); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
//////////////////////////////////////////////////////////// |
| 235 |
// |
| 236 |
// Intermediate level |
| 237 |
// |
| 238 |
//////////////////////////////////////////////////////////// |
| 239 |
bool EDocParser::createZones() |
| 240 |
{ |
| 241 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 242 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 243 |
|
| 244 |
// the 128 zone |
| 245 |
auto it = entryMap.lower_bound("eDcF"); |
| 246 |
while (it != entryMap.end()) { |
| 247 |
if (it->first != "eDcF") |
| 248 |
break; |
| 249 |
MWAWEntry const &entry = it++->second; |
| 250 |
readFontsName(entry); |
| 251 |
} |
| 252 |
it = entryMap.lower_bound("eIdx"); |
| 253 |
while (it != entryMap.end()) { |
| 254 |
if (it->first != "eIdx") |
| 255 |
break; |
| 256 |
MWAWEntry const &entry = it++->second; |
| 257 |
readIndex(entry); |
| 258 |
} |
| 259 |
it = entryMap.lower_bound("Info"); |
| 260 |
while (it != entryMap.end()) { |
| 261 |
if (it->first != "Info") |
| 262 |
break; |
| 263 |
MWAWEntry const &entry = it++->second; |
| 264 |
readInfo(entry); |
| 265 |
} |
| 266 |
bool res=findContents(); |
| 267 |
#ifdef DEBUG_WITH_FILES |
| 268 |
// get rid of the default application resource |
| 269 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 270 |
static char const *appliRsrc[]= { |
| 271 |
// default, Dialog (3000: DLOG,DITL,DLGX,dctb","ictb","STR ") |
| 272 |
"ALRT","BNDL","CNTL","CURS","CDEF", "DLOG","DLGX","DITL","FREF","ICON", |
| 273 |
"ICN#","MENU","SIZE","WIND", |
| 274 |
"cicn","crsr","dctb","icl4","icl8", "ics4","ics8","ics#","ictb","mstr", |
| 275 |
"snd ", |
| 276 |
"eSRD" |
| 277 |
}; |
| 278 |
for (int r=0; r < 14+11+1; r++) { |
| 279 |
it = entryMap.lower_bound(appliRsrc[r]); |
| 280 |
while (it != entryMap.end()) { |
| 281 |
if (it->first != appliRsrc[r]) |
| 282 |
break; |
| 283 |
MWAWEntry const &entry = it++->second; |
| 284 |
if (entry.isParsed()) continue; |
| 285 |
entry.setParsed(true); |
| 286 |
ascFile.skipZone(entry.begin()-4,entry.end()-1); |
| 287 |
} |
| 288 |
} |
| 289 |
#endif |
| 290 |
return res; |
| 291 |
} |
| 292 |
|
| 293 |
bool EDocParser::findContents() |
| 294 |
{ |
| 295 |
MWAWRSRCParserPtr rsrcParser = getRSRCParser(); |
| 296 |
auto const &entryMap = rsrcParser->getEntriesMap(); |
| 297 |
|
| 298 |
/* if the data is compress, we must look for cPIC zone ; if not, |
| 299 |
we look for the first PICT zone. |
| 300 |
Note: maybe we can also find text in TEXT zone, but I never see that |
| 301 |
*/ |
| 302 |
char const *wh[2]= {"cPIC", "PICT"}; |
| 303 |
for (int st = 0; st < 2; st++) { |
| 304 |
std::map<int, MWAWEntry> &map=st==0 ? m_state->m_idCPICMap : m_state->m_idPICTMap; |
| 305 |
std::set<int> seens; |
| 306 |
auto it = entryMap.lower_bound(wh[st]); |
| 307 |
while (it != entryMap.end()) { |
| 308 |
if (it->first != wh[st]) |
| 309 |
break; |
| 310 |
MWAWEntry const &entry = it++->second; |
| 311 |
map[entry.id()]= entry; |
| 312 |
seens.insert(entry.id()); |
| 313 |
} |
| 314 |
if (seens.empty() || m_state->m_maxPictId) |
| 315 |
continue; |
| 316 |
auto sIt=seens.lower_bound(1); |
| 317 |
if (sIt==seens.end()|| *sIt>10) |
| 318 |
continue; |
| 319 |
int maxId=*sIt; |
| 320 |
while (sIt!=seens.end() && *sIt<maxId+5) |
| 321 |
maxId=*(sIt++); |
| 322 |
m_state->m_maxPictId=maxId; |
| 323 |
m_state->m_compressed=(st==0); |
| 324 |
} |
| 325 |
|
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
bool EDocParser::sendContents() |
| 330 |
{ |
| 331 |
bool compressed=m_state->m_compressed; |
| 332 |
int actPage=0; |
| 333 |
for (int i=1; i <= m_state->m_maxPictId; i++) { |
| 334 |
newPage(++actPage); |
| 335 |
sendPicture(i, compressed); |
| 336 |
} |
| 337 |
if (!m_state->m_indexList.empty()) { |
| 338 |
newPage(++actPage); |
| 339 |
sendIndex(); |
| 340 |
} |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
bool EDocParser::sendPicture(int pictId, bool compressed) |
| 345 |
{ |
| 346 |
if (!getTextListener()) { |
| 347 |
MWAW_DEBUG_MSG(("EDocParser::sendPicture: can not find the listener\n")); |
| 348 |
return false; |
| 349 |
} |
| 350 |
std::map<int, MWAWEntry>::const_iterator it; |
| 351 |
librevenge::RVNGBinaryData data; |
| 352 |
if (compressed) { |
| 353 |
it = m_state->m_idCPICMap.find(pictId); |
| 354 |
if (it==m_state->m_idCPICMap.end() || !decodeZone(it->second,data)) |
| 355 |
return false; |
| 356 |
} |
| 357 |
else { |
| 358 |
it = m_state->m_idPICTMap.find(pictId); |
| 359 |
if (it==m_state->m_idPICTMap.end() || |
| 360 |
!getRSRCParser()->parsePICT(it->second, data)) |
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
auto dataSz=int(data.size()); |
| 365 |
if (!dataSz) |
| 366 |
return false; |
| 367 |
MWAWInputStreamPtr pictInput=MWAWInputStream::get(data, false); |
| 368 |
if (!pictInput) { |
| 369 |
MWAW_DEBUG_MSG(("EDocParser::sendPicture: oops can not find an input\n")); |
| 370 |
return false; |
| 371 |
} |
| 372 |
MWAWBox2f box; |
| 373 |
auto res = MWAWPictData::check(pictInput, dataSz,box); |
| 374 |
if (res == MWAWPict::MWAW_R_BAD) { |
| 375 |
MWAW_DEBUG_MSG(("EDocParser::sendPicture: can not find the picture\n")); |
| 376 |
return false; |
| 377 |
} |
| 378 |
pictInput->seek(0,librevenge::RVNG_SEEK_SET); |
| 379 |
std::shared_ptr<MWAWPict> thePict(MWAWPictData::get(pictInput, dataSz)); |
| 380 |
MWAWPosition pictPos=MWAWPosition(MWAWVec2f(0,0),box.size(), librevenge::RVNG_POINT); |
| 381 |
pictPos.setRelativePosition(MWAWPosition::Char); |
| 382 |
if (thePict) { |
| 383 |
MWAWEmbeddedObject picture; |
| 384 |
if (thePict->getBinary(picture)) |
| 385 |
getTextListener()->insertPicture(pictPos, picture); |
| 386 |
} |
| 387 |
return true; |
| 388 |
} |
| 389 |
|
| 390 |
void EDocParser::flushExtra() |
| 391 |
{ |
| 392 |
#ifdef DEBUG |
| 393 |
for (auto const &rIt : m_state->m_idCPICMap) { |
| 394 |
MWAWEntry const &entry = rIt.second; |
| 395 |
if (entry.isParsed()) continue; |
| 396 |
sendPicture(entry.id(), true); |
| 397 |
} |
| 398 |
for (auto const &rIt : m_state->m_idPICTMap) { |
| 399 |
MWAWEntry const &entry = rIt.second; |
| 400 |
if (entry.isParsed()) continue; |
| 401 |
sendPicture(entry.id(), false); |
| 402 |
} |
| 403 |
#endif |
| 404 |
} |
| 405 |
|
| 406 |
//////////////////////////////////////////////////////////// |
| 407 |
// |
| 408 |
// Low level |
| 409 |
// |
| 410 |
//////////////////////////////////////////////////////////// |
| 411 |
|
| 412 |
|
| 413 |
// the font name |
| 414 |
bool EDocParser::readFontsName(MWAWEntry const &entry) |
| 415 |
{ |
| 416 |
long length = entry.length(); |
| 417 |
if (!entry.valid() || (length%0x100)!=2) { |
| 418 |
MWAW_DEBUG_MSG(("EDocParser::readFontsName: the entry seems very short\n")); |
| 419 |
return false; |
| 420 |
} |
| 421 |
|
| 422 |
entry.setParsed(true); |
| 423 |
long pos = entry.begin(); |
| 424 |
MWAWInputStreamPtr input = rsrcInput(); |
| 425 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 426 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 427 |
libmwaw::DebugStream f; |
| 428 |
f << "Entries(FontsName):"; |
| 429 |
if (entry.id()!=128) |
| 430 |
f << "#id=" << entry.id() << ","; |
| 431 |
auto N=static_cast<int>(input->readULong(2)); |
| 432 |
f << "N=" << N << ","; |
| 433 |
ascFile.addPos(pos-4); |
| 434 |
ascFile.addNote(f.str().c_str()); |
| 435 |
if (N*0x100+2!=length) { |
| 436 |
MWAW_DEBUG_MSG(("EDocParser::readFontsName: the number of elements seems bad\n")); |
| 437 |
return false; |
| 438 |
} |
| 439 |
for (int i = 0; i < N; i++) { |
| 440 |
pos = input->tell(); |
| 441 |
|
| 442 |
f.str(""); |
| 443 |
f << "FontsName-" << i << ":"; |
| 444 |
auto fSz=static_cast<int>(input->readULong(1)); |
| 445 |
if (!fSz || fSz >= 255) { |
| 446 |
f << "##" << fSz << ","; |
| 447 |
MWAW_DEBUG_MSG(("EDocParser::readFontsName: the font name %d seems bad\n", i)); |
| 448 |
} |
| 449 |
else { |
| 450 |
std::string name(""); |
| 451 |
for (int c=0; c < fSz; c++) |
| 452 |
name += char(input->readULong(1)); |
| 453 |
f << "\"" << name << "\","; |
| 454 |
} |
| 455 |
input->seek(pos+32, librevenge::RVNG_SEEK_SET); |
| 456 |
for (int j = 0; j < 112; j++) { // always 0 . |
| 457 |
auto val = static_cast<int>(input->readLong(2)); |
| 458 |
if (val) f << "f" << j << "=" << val << ","; |
| 459 |
} |
| 460 |
ascFile.addPos(pos); |
| 461 |
ascFile.addNote(f.str().c_str()); |
| 462 |
input->seek(pos+0x100, librevenge::RVNG_SEEK_SET); |
| 463 |
} |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
// the index |
| 468 |
bool EDocParser::sendIndex() |
| 469 |
{ |
| 470 |
if (!getTextListener()) { |
| 471 |
MWAW_DEBUG_MSG(("EDocParser::sendIndex: can not find the listener\n")); |
| 472 |
return false; |
| 473 |
} |
| 474 |
if (!m_state->m_indexList.size()) |
| 475 |
return true; |
| 476 |
|
| 477 |
double w = getPageWidth(); |
| 478 |
MWAWParagraph para; |
| 479 |
MWAWTabStop tab; |
| 480 |
tab.m_alignment = MWAWTabStop::RIGHT; |
| 481 |
tab.m_leaderCharacter='.'; |
| 482 |
tab.m_position = w-0.3; |
| 483 |
|
| 484 |
para.m_tabs->push_back(tab); |
| 485 |
para.m_marginsUnit=librevenge::RVNG_INCH; |
| 486 |
|
| 487 |
MWAWFont cFont(3,10); |
| 488 |
cFont.setFlags(MWAWFont::boldBit); |
| 489 |
MWAWFont actFont(3,12); |
| 490 |
|
| 491 |
getTextListener()->insertEOL(); |
| 492 |
std::stringstream ss; |
| 493 |
for (auto const &index : m_state->m_indexList) { |
| 494 |
para.m_margins[0] = 0.3*double(index.m_levelId+1); |
| 495 |
getTextListener()->setParagraph(para); |
| 496 |
getTextListener()->setFont(actFont); |
| 497 |
for (char c : index.m_text) |
| 498 |
getTextListener()->insertCharacter(static_cast<unsigned char>(c)); |
| 499 |
|
| 500 |
if (index.m_page >= 0) { |
| 501 |
getTextListener()->setFont(cFont); |
| 502 |
getTextListener()->insertTab(); |
| 503 |
ss.str(""); |
| 504 |
ss << index.m_page; |
| 505 |
getTextListener()->insertUnicodeString(librevenge::RVNGString(ss.str().c_str())); |
| 506 |
} |
| 507 |
getTextListener()->insertEOL(); |
| 508 |
} |
| 509 |
return true; |
| 510 |
} |
| 511 |
|
| 512 |
bool EDocParser::readIndex(MWAWEntry const &entry) |
| 513 |
{ |
| 514 |
long length = entry.length(); |
| 515 |
if (!entry.valid() || length < 20) { |
| 516 |
MWAW_DEBUG_MSG(("EDocParser::readIndex: the entry seems very short\n")); |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
entry.setParsed(true); |
| 521 |
long pos = entry.begin(); |
| 522 |
long endPos = entry.end(); |
| 523 |
MWAWInputStreamPtr input = rsrcInput(); |
| 524 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 525 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 526 |
libmwaw::DebugStream f; |
| 527 |
f << "Entries(Index):"; |
| 528 |
if (entry.id()!=128) |
| 529 |
f << "#id=" << entry.id() << ","; |
| 530 |
auto val=static_cast<int>(input->readULong(2)); |
| 531 |
if (val) // 100 ? |
| 532 |
f << "f0=" << std::hex << val << std::dec << ","; |
| 533 |
auto N=static_cast<int>(input->readULong(2)); |
| 534 |
f << "N=" << N << ","; |
| 535 |
for (int i = 0; i < 8; i++) { // always 0 |
| 536 |
val=static_cast<int>(input->readLong(2)); |
| 537 |
if (val) |
| 538 |
f << "f" << i << "=" << val << ","; |
| 539 |
} |
| 540 |
ascFile.addPos(pos-4); |
| 541 |
ascFile.addNote(f.str().c_str()); |
| 542 |
if (N*14+20>length) { |
| 543 |
MWAW_DEBUG_MSG(("EDocParser::readIndex: the number of elements seems bad\n")); |
| 544 |
return false; |
| 545 |
} |
| 546 |
|
| 547 |
for (int i = 0; i < N; i++) { |
| 548 |
pos = input->tell(); |
| 549 |
f.str(""); |
| 550 |
if (pos+14 > endPos) { |
| 551 |
f << "Index-" << i << ":###"; |
| 552 |
ascFile.addPos(pos); |
| 553 |
ascFile.addNote(f.str().c_str()); |
| 554 |
|
| 555 |
MWAW_DEBUG_MSG(("EDocParser::readIndex: can not read index %d\n", i)); |
| 556 |
return false; |
| 557 |
} |
| 558 |
EDocParserInternal::Index index; |
| 559 |
val = static_cast<int>(input->readULong(1)); // 0|80 |
| 560 |
if (val) f << "fl=" << std::hex << val << std::dec << ","; |
| 561 |
index.m_levelId = static_cast<int>(input->readULong(1)); |
| 562 |
index.m_page = static_cast<int>(input->readLong(2)); |
| 563 |
// f1: y pos, other 0 |
| 564 |
for (int j = 0; j < 4; j++) { |
| 565 |
val = static_cast<int>(input->readLong(2)); |
| 566 |
if (val) |
| 567 |
f << "f" << j << "=" << val << ","; |
| 568 |
} |
| 569 |
auto fSz = static_cast<int>(input->readULong(1)); |
| 570 |
if (pos+13+fSz > endPos) { |
| 571 |
index.m_extra=f.str(); |
| 572 |
f.str(""); |
| 573 |
f << "Index-" << i << ":" << index << "###"; |
| 574 |
ascFile.addPos(pos); |
| 575 |
ascFile.addNote(f.str().c_str()); |
| 576 |
|
| 577 |
MWAW_DEBUG_MSG(("EDocParser::readIndex: can not read index %d text\n", i)); |
| 578 |
return false; |
| 579 |
} |
| 580 |
std::string text(""); |
| 581 |
for (int j = 0; j < fSz; j++) |
| 582 |
text += char(input->readULong(1)); |
| 583 |
index.m_text=text; |
| 584 |
index.m_extra=f.str(); |
| 585 |
m_state->m_indexList.push_back(index); |
| 586 |
f.str(""); |
| 587 |
f << "Index-" << i << ":" << index; |
| 588 |
if ((fSz%2)==0) // |
| 589 |
input->seek(1,librevenge::RVNG_SEEK_CUR); |
| 590 |
ascFile.addPos(pos); |
| 591 |
ascFile.addNote(f.str().c_str()); |
| 592 |
} |
| 593 |
return true; |
| 594 |
} |
| 595 |
|
| 596 |
// the document information |
| 597 |
bool EDocParser::readInfo(MWAWEntry const &entry) |
| 598 |
{ |
| 599 |
long length = entry.length(); |
| 600 |
if (!entry.valid() || length < 0x68) { |
| 601 |
MWAW_DEBUG_MSG(("EDocParser::readInfo: the entry seems very short\n")); |
| 602 |
return false; |
| 603 |
} |
| 604 |
|
| 605 |
entry.setParsed(true); |
| 606 |
long pos = entry.begin(); |
| 607 |
long endPos = entry.end(); |
| 608 |
MWAWInputStreamPtr input = rsrcInput(); |
| 609 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 610 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 611 |
libmwaw::DebugStream f; |
| 612 |
f << "Entries(Info):"; |
| 613 |
if (entry.id()!=128) |
| 614 |
f << "#id=" << entry.id() << ","; |
| 615 |
int val; |
| 616 |
for (int i = 0; i < 4; i++) { // f0=0, other big number |
| 617 |
val = static_cast<int>(input->readULong(2)); |
| 618 |
if (val) |
| 619 |
f << "f" << i << "=" << std::hex << val << std::dec << ","; |
| 620 |
} |
| 621 |
// creator, file name |
| 622 |
for (int i = 0; i < 2; i++) { |
| 623 |
auto sz=static_cast<int>(input->readULong(1)); |
| 624 |
if (sz > 31) { |
| 625 |
MWAW_DEBUG_MSG(("EDocParser::readInfo: can not read string %d\n", i)); |
| 626 |
f << "###,"; |
| 627 |
} |
| 628 |
else { |
| 629 |
std::string name(""); |
| 630 |
for (int c=0; c < sz; c++) |
| 631 |
name += char(input->readULong(1)); |
| 632 |
f << name << ","; |
| 633 |
} |
| 634 |
input->seek(pos+8+(i+1)*32, librevenge::RVNG_SEEK_SET); |
| 635 |
} |
| 636 |
for (int i = 0; i < 5; i++) { // always 4, 0, 210, 0, 0 ? |
| 637 |
val = static_cast<int>(input->readLong(2)); |
| 638 |
if (val) |
| 639 |
f << "g" << i << "=" << val << ","; |
| 640 |
} |
| 641 |
int dim[2]; |
| 642 |
for (auto &d : dim) d = static_cast<int>(input->readLong(2)); |
| 643 |
f << "dim?=" << dim[1] << "x" << dim[0] << ","; |
| 644 |
if (dim[1]>100 && dim[1]<2000 && dim[0]>100 && dim[0]< 2000) { |
| 645 |
getPageSpan().setFormLength(double(dim[0])/72.); |
| 646 |
getPageSpan().setFormWidth(double(dim[1])/72.); |
| 647 |
} |
| 648 |
else { |
| 649 |
MWAW_DEBUG_MSG(("EDocParser::readInfo: the page dimension seems bad\n")); |
| 650 |
f << "###,"; |
| 651 |
} |
| 652 |
auto N=static_cast<int>(input->readLong(2)); |
| 653 |
f << "numPict?=" << N << ","; // seems ok in eDcR, but no in eSRD |
| 654 |
for (int i = 0; i < 2; i++) { // fl0=hasIndex ?, fl1=0 |
| 655 |
val = static_cast<int>(input->readLong(1)); |
| 656 |
if (val) |
| 657 |
f << "fl" << i << "=" << val << ","; |
| 658 |
} |
| 659 |
val = static_cast<int>(input->readLong(2)); // 0 or bf |
| 660 |
if (val) |
| 661 |
f << "g5=" << val << ","; |
| 662 |
for (int i = 0; i < 3; i++) { // 3 big number: some size? |
| 663 |
val = static_cast<int>(input->readULong(4)); |
| 664 |
if (val) |
| 665 |
f << "f" << i << "=" << std::hex << val << std::dec << ","; |
| 666 |
} |
| 667 |
ascFile.addDelimiter(input->tell(),'|'); |
| 668 |
ascFile.addPos(pos-4); |
| 669 |
ascFile.addNote(f.str().c_str()); |
| 670 |
if (input->tell() != endPos) { |
| 671 |
ascFile.addPos(input->tell()); |
| 672 |
ascFile.addNote("Info(II)"); |
| 673 |
} |
| 674 |
return true; |
| 675 |
} |
| 676 |
|
| 677 |
// code to uncompress data ( very low level) |
| 678 |
namespace EDocParserInternal |
| 679 |
{ |
| 680 |
//! very low structure to help uncompress data |
| 681 |
struct DeflateStruct { |
| 682 |
//! constructor |
| 683 |
DeflateStruct(long size, long initSize) |
| 684 |
: m_toWrite(size) |
| 685 |
, m_data() |
| 686 |
, m_circQueue(0x2000,0) |
| 687 |
, m_circQueuePos(0) |
| 688 |
, m_numDelayed(0) |
| 689 |
, m_delayedChar('\0') |
| 690 |
{ |
| 691 |
m_data.reserve(size_t(initSize)); |
| 692 |
} |
| 693 |
//! true if we have build of the data |
| 694 |
bool isEnd() const |
| 695 |
{ |
| 696 |
return m_toWrite <= 0; |
| 697 |
} |
| 698 |
//! push a new character |
| 699 |
bool push(unsigned char c) |
| 700 |
{ |
| 701 |
if (m_toWrite <= 0) return false; |
| 702 |
m_circQueue[m_circQueuePos++]=c; |
| 703 |
if (m_circQueuePos==0x2000) |
| 704 |
m_circQueuePos=0; |
| 705 |
if (m_numDelayed) |
| 706 |
return treatDelayed(c); |
| 707 |
if (c==0x81 && m_toWrite!=1) { |
| 708 |
m_numDelayed++; |
| 709 |
return true; |
| 710 |
} |
| 711 |
m_delayedChar=c; |
| 712 |
m_data.push_back(c); |
| 713 |
m_toWrite--; |
| 714 |
return true; |
| 715 |
} |
| 716 |
//! send a duplicated part of the data |
| 717 |
bool sendDuplicated(int num, int depl); |
| 718 |
//! check if there is delayed char, if so treat them |
| 719 |
bool treatDelayed(unsigned char c); |
| 720 |
//! return the content of the block in dt |
| 721 |
bool getBinaryData(librevenge::RVNGBinaryData &dt) const |
| 722 |
{ |
| 723 |
dt.clear(); |
| 724 |
if (m_data.empty()) return false; |
| 725 |
unsigned char const *firstPos=&m_data[0]; |
| 726 |
dt.append(firstPos, static_cast<unsigned long>(m_data.size())); |
| 727 |
return true; |
| 728 |
} |
| 729 |
protected: |
| 730 |
//! the number of data that we need to write |
| 731 |
long m_toWrite; |
| 732 |
//! the resulting data |
| 733 |
std::vector<unsigned char> m_data; |
| 734 |
|
| 735 |
//! a circular queue |
| 736 |
std::vector<unsigned char> m_circQueue; |
| 737 |
//! the position in the circular queue |
| 738 |
size_t m_circQueuePos; |
| 739 |
//! the number of character delayed |
| 740 |
int m_numDelayed; |
| 741 |
//! the delayed character |
| 742 |
unsigned char m_delayedChar; |
| 743 |
private: |
| 744 |
DeflateStruct(DeflateStruct const &orig) = delete; |
| 745 |
DeflateStruct &operator=(DeflateStruct const &orig) = delete; |
| 746 |
}; |
| 747 |
|
| 748 |
bool DeflateStruct::sendDuplicated(int num, int depl) |
| 749 |
{ |
| 750 |
int readPos=int(m_circQueuePos)+depl; |
| 751 |
while (readPos < 0) readPos+=0x2000; |
| 752 |
while (readPos >= 0x2000) readPos-=0x2000; |
| 753 |
|
| 754 |
while (num-->0) { |
| 755 |
push(m_circQueue[size_t(readPos++)]); |
| 756 |
if (readPos==0x2000) |
| 757 |
readPos=0; |
| 758 |
} |
| 759 |
return true; |
| 760 |
} |
| 761 |
bool DeflateStruct::treatDelayed(unsigned char c) |
| 762 |
{ |
| 763 |
if (m_toWrite <= 0) |
| 764 |
return false; |
| 765 |
if (m_numDelayed==1) { |
| 766 |
if (c==0x82) { |
| 767 |
m_numDelayed++; |
| 768 |
return true; |
| 769 |
} |
| 770 |
m_delayedChar=0x81; |
| 771 |
m_data.push_back(m_delayedChar); |
| 772 |
if (--m_toWrite==0) return true; |
| 773 |
if (c==0x81 && m_toWrite==1) |
| 774 |
return true; |
| 775 |
m_numDelayed=0; |
| 776 |
m_delayedChar=c; |
| 777 |
m_data.push_back(c); |
| 778 |
m_toWrite--; |
| 779 |
return true; |
| 780 |
} |
| 781 |
|
| 782 |
m_numDelayed=0; |
| 783 |
if (c==0) { |
| 784 |
m_data.push_back(0x81); |
| 785 |
if (--m_toWrite==0) return true; |
| 786 |
m_delayedChar=0x82; |
| 787 |
m_data.push_back(m_delayedChar); |
| 788 |
m_toWrite--; |
| 789 |
return true; |
| 790 |
} |
| 791 |
if (c-1 > m_toWrite) return false; |
| 792 |
for (int i = 0; i < int(c-1); i++) |
| 793 |
m_data.push_back(m_delayedChar); |
| 794 |
m_toWrite -= (c-1); |
| 795 |
return true; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
bool EDocParser::decodeZone(MWAWEntry const &entry, librevenge::RVNGBinaryData &data) |
| 800 |
{ |
| 801 |
data.clear(); |
| 802 |
long length = entry.length(); |
| 803 |
if (!entry.valid() || length<0x21+12) { |
| 804 |
MWAW_DEBUG_MSG(("EDocParser::decodeZone: the entry seems very short\n")); |
| 805 |
return false; |
| 806 |
} |
| 807 |
entry.setParsed(true); |
| 808 |
long pos = entry.begin(); |
| 809 |
long endPos = entry.end(); |
| 810 |
MWAWInputStreamPtr input = rsrcInput(); |
| 811 |
libmwaw::DebugFile &ascFile = rsrcAscii(); |
| 812 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 813 |
|
| 814 |
libmwaw::DebugStream f; |
| 815 |
f << "Entries(CompressZone):"; |
| 816 |
if (long(input->readULong(4))!=length) { |
| 817 |
MWAW_DEBUG_MSG(("EDocParser::decodeZone: unexpected zone size\n")); |
| 818 |
return false; |
| 819 |
} |
| 820 |
auto zoneSize=long(input->readULong(4)); |
| 821 |
f << "sz[final]=" << std::hex << zoneSize << std::dec << ","; |
| 822 |
|
| 823 |
if (!zoneSize) { |
| 824 |
MWAW_DEBUG_MSG(("EDocParser::decodeZone: unexpected final zone size\n")); |
| 825 |
return false; |
| 826 |
} |
| 827 |
f << "checkSum=" << std::hex << input->readULong(4) << std::dec << ","; |
| 828 |
|
| 829 |
ascFile.addPos(pos-4); |
| 830 |
ascFile.addNote(f.str().c_str()); |
| 831 |
|
| 832 |
// make an initial size estimate to avoid big allocation in case zoneSize is damaged |
| 833 |
const long maxInputSize = input->size() - input->tell(); |
| 834 |
const long initSize = (zoneSize / 4 > maxInputSize) ? 4 * maxInputSize : zoneSize; |
| 835 |
EDocParserInternal::DeflateStruct deflate(zoneSize, initSize); |
| 836 |
int const maxData[]= {0x80, 0x20, 0x40}; |
| 837 |
int val; |
| 838 |
|
| 839 |
while (!deflate.isEnd() && input->tell() < endPos-3) { |
| 840 |
// only find a simple compress zone but seems ok to have more |
| 841 |
std::vector<unsigned char> vectors32K[3]; |
| 842 |
std::vector<unsigned char> originalValues[3]; |
| 843 |
for (int st=0; st < 3; st++) { |
| 844 |
pos=input->tell(); |
| 845 |
f.str(""); |
| 846 |
f << "CompressZone[data" << st << "]:"; |
| 847 |
auto num=static_cast<int>(input->readULong(1)); |
| 848 |
f << "num=" << num << ","; |
| 849 |
if (num > maxData[st] || pos+1+num > endPos) { |
| 850 |
MWAW_DEBUG_MSG(("EDocParser::decodeZone: find unexpected num of data : %d for zone %d\n", num, st)); |
| 851 |
f << "###"; |
| 852 |
|
| 853 |
ascFile.addPos(pos); |
| 854 |
ascFile.addNote(f.str().c_str()); |
| 855 |
return false; |
| 856 |
} |
| 857 |
std::multimap<int,int> mapData; |
| 858 |
originalValues[st].resize(size_t(maxData[st])*2, 0); |
| 859 |
for (int i = 0; i < num; i++) { |
| 860 |
val=static_cast<int>(input->readULong(1)); |
| 861 |
for (int b=0; b < 2; b++) { |
| 862 |
int byte= b==0 ? (val>>4) : (val&0xF); |
| 863 |
originalValues[st][size_t(2*i+b)]=static_cast<unsigned char>(byte); |
| 864 |
if (byte==0) |
| 865 |
continue; |
| 866 |
mapData.insert(std::multimap<int,int>::value_type(byte,2*i+b)); |
| 867 |
} |
| 868 |
} |
| 869 |
vectors32K[st].resize(0x8000,0); |
| 870 |
int writePos=0; |
| 871 |
for (auto const &it : mapData) { |
| 872 |
int n=0x8000>>(it.first); |
| 873 |
if (writePos+n>0x8000) { |
| 874 |
MWAW_DEBUG_MSG(("EDocParser::decodeZone: find unexpected value writePos=%x for zone %d\n",static_cast<unsigned int>(writePos+n), st)); |
| 875 |
|
| 876 |
f << "###"; |
| 877 |
|
| 878 |
ascFile.addPos(pos); |
| 879 |
ascFile.addNote(f.str().c_str()); |
| 880 |
return false; |
| 881 |
} |
| 882 |
for (int j = 0; j < n; j++) |
| 883 |
vectors32K[st][size_t(writePos++)]=static_cast<unsigned char>(it.second); |
| 884 |
} |
| 885 |
|
| 886 |
ascFile.addPos(pos); |
| 887 |
ascFile.addNote(f.str().c_str()); |
| 888 |
} |
| 889 |
pos = input->tell(); |
| 890 |
int byte=0; |
| 891 |
long maxBlockSz=0xFFF0; |
| 892 |
unsigned int value=(static_cast<unsigned int>(input->readULong(2)))<<16; |
| 893 |
while (maxBlockSz) { |
| 894 |
if (deflate.isEnd() || input->tell()>endPos) break; |
| 895 |
int ind0=(value>>16); |
| 896 |
if (ind0 & 0x8000) { |
| 897 |
auto ind1 = static_cast<int>(vectors32K[0][size_t(ind0&0x7FFF)]); |
| 898 |
int byt1=originalValues[0][size_t(ind1)]+1; |
| 899 |
if (byte<byt1) { |
| 900 |
value = (value<<byte); |
| 901 |
byt1 -= byte; |
| 902 |
value |= static_cast<unsigned int>(input->readULong(2)); |
| 903 |
byte=16; |
| 904 |
} |
| 905 |
value=(value<<byt1); |
| 906 |
byte-=byt1; |
| 907 |
|
| 908 |
deflate.push(static_cast<unsigned char>(ind1)); |
| 909 |
maxBlockSz-=2; |
| 910 |
continue; |
| 911 |
} |
| 912 |
|
| 913 |
auto ind1 = static_cast<int>(vectors32K[1][size_t(ind0)]); |
| 914 |
int byt1 = originalValues[1][size_t(ind1)]+1; |
| 915 |
if (byte<byt1) { |
| 916 |
value = (value<<byte); |
| 917 |
byt1 -= byte; |
| 918 |
value |= static_cast<unsigned int>(input->readULong(2)); |
| 919 |
byte=16; |
| 920 |
} |
| 921 |
value=(value<<byt1); |
| 922 |
byte-=byt1; |
| 923 |
auto ind2 = static_cast<int>(vectors32K[2][size_t(value>>17)]); |
| 924 |
int byt2=originalValues[2][size_t(ind2)]; |
| 925 |
if (byte<byt2) { |
| 926 |
value = (value<<byte); |
| 927 |
byt2 -= byte; |
| 928 |
value |= static_cast<unsigned int>(input->readULong(2)); |
| 929 |
byte=16; |
| 930 |
} |
| 931 |
value=(value<<byt2); |
| 932 |
byte-=byt2; |
| 933 |
|
| 934 |
ind2=int(value>>26) | (ind2<<6); |
| 935 |
int byt3=6; |
| 936 |
if (byte<byt3) { |
| 937 |
value = (value<<byte); |
| 938 |
byt3 -= byte; |
| 939 |
value |= static_cast<unsigned int>(input->readULong(2)); |
| 940 |
byte=16; |
| 941 |
} |
| 942 |
value=(value<<byt3); |
| 943 |
byte-=byt3; |
| 944 |
deflate.sendDuplicated(ind1, -ind2); |
| 945 |
maxBlockSz-=3; |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
if (input->tell()!=endPos) { |
| 950 |
MWAW_DEBUG_MSG(("EDocParser::decodeZone: unexpected end of data\n")); |
| 951 |
ascFile.addPos(input->tell()); |
| 952 |
ascFile.addNote("CompressZone[after]"); |
| 953 |
} |
| 954 |
bool res = deflate.getBinaryData(data); |
| 955 |
ascFile.skipZone(pos,input->tell()-1); |
| 956 |
#if defined(DEBUG_WITH_FILES) |
| 957 |
if (res) { |
| 958 |
static int volatile cPictName = 0; |
| 959 |
libmwaw::DebugStream f2; |
| 960 |
f2 << "CPICT" << ++cPictName << ".pct"; |
| 961 |
libmwaw::Debug::dumpFile(data, f2.str().c_str()); |
| 962 |
} |
| 963 |
#endif |
| 964 |
return res; |
| 965 |
} |
| 966 |
|
| 967 |
//////////////////////////////////////////////////////////// |
| 968 |
// read the header |
| 969 |
//////////////////////////////////////////////////////////// |
| 970 |
bool EDocParser::checkHeader(MWAWHeader *header, bool strict) |
| 971 |
{ |
| 972 |
*m_state = EDocParserInternal::State(); |
| 973 |
/** no data fork, may be ok, but this means |
| 974 |
that the file contains no text, so... */ |
| 975 |
MWAWInputStreamPtr input = getInput(); |
| 976 |
if (!input || !getRSRCParser()) |
| 977 |
return false; |
| 978 |
if (input->hasDataFork()) { |
| 979 |
MWAW_DEBUG_MSG(("EDocParser::checkHeader: find a datafork, odd!!!\n")); |
| 980 |
} |
| 981 |
if (strict) { |
| 982 |
// check that the fontname zone exists |
| 983 |
auto const &entryMap = getRSRCParser()->getEntriesMap(); |
| 984 |
if (entryMap.find("eDcF") == entryMap.end()) |
| 985 |
return false; |
| 986 |
} |
| 987 |
if (header) |
| 988 |
header->reset(MWAWDocument::MWAW_T_EDOC, version()); |
| 989 |
|
| 990 |
return true; |
| 991 |
} |
| 992 |
|
| 993 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |