| 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 <iomanip> |
| 35 |
#include <iostream> |
| 36 |
#include <limits> |
| 37 |
#include <map> |
| 38 |
#include <set> |
| 39 |
#include <sstream> |
| 40 |
#include <string> |
| 41 |
|
| 42 |
#include <librevenge/librevenge.h> |
| 43 |
|
| 44 |
#include "MWAWCell.hxx" |
| 45 |
#include "MWAWTextListener.hxx" |
| 46 |
#include "MWAWFont.hxx" |
| 47 |
#include "MWAWFontConverter.hxx" |
| 48 |
#include "MWAWHeader.hxx" |
| 49 |
#include "MWAWList.hxx" |
| 50 |
#include "MWAWParagraph.hxx" |
| 51 |
#include "MWAWPosition.hxx" |
| 52 |
#include "MWAWPictMac.hxx" |
| 53 |
#include "MWAWPrinter.hxx" |
| 54 |
#include "MWAWSubDocument.hxx" |
| 55 |
|
| 56 |
#include "HanMacWrdJGraph.hxx" |
| 57 |
#include "HanMacWrdJText.hxx" |
| 58 |
|
| 59 |
#include "HanMacWrdJParser.hxx" |
| 60 |
|
| 61 |
/** Internal: the structures of a HanMacWrdJParser */ |
| 62 |
namespace HanMacWrdJParserInternal |
| 63 |
{ |
| 64 |
//////////////////////////////////////// |
| 65 |
//! Internal: the state of a HanMacWrdJParser |
| 66 |
struct State { |
| 67 |
//! constructor |
| 68 |
State() |
| 69 |
: m_zonesListBegin(-1) |
| 70 |
, m_zonesMap() |
| 71 |
, m_zonesIdList() |
| 72 |
, m_actPage(0) |
| 73 |
, m_numPages(0) |
| 74 |
, m_headerHeight(0) |
| 75 |
, m_footerHeight(0) |
| 76 |
, m_headerId(0) |
| 77 |
, m_footerId(0) |
| 78 |
{ |
| 79 |
} |
| 80 |
|
| 81 |
//! the list of zone begin |
| 82 |
long m_zonesListBegin; |
| 83 |
//! a map of entry: filepos->zone |
| 84 |
std::map<long, MWAWEntry> m_zonesMap; |
| 85 |
//! an internal flag, used to know the actual id of a zone |
| 86 |
std::vector<int> m_zonesIdList; |
| 87 |
|
| 88 |
int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */; |
| 89 |
int m_headerHeight /** the header height if known */, |
| 90 |
m_footerHeight /** the footer height if known */; |
| 91 |
/** the header text zone id or 0*/ |
| 92 |
long m_headerId; |
| 93 |
/** the footer text zone id or 0*/ |
| 94 |
long m_footerId; |
| 95 |
}; |
| 96 |
|
| 97 |
//////////////////////////////////////// |
| 98 |
//! Internal: the subdocument of a HanMacWrdJParser |
| 99 |
class SubDocument final : public MWAWSubDocument |
| 100 |
{ |
| 101 |
public: |
| 102 |
SubDocument(HanMacWrdJParser &pars, MWAWInputStreamPtr const &input, long zoneId) |
| 103 |
: MWAWSubDocument(&pars, input, MWAWEntry()) |
| 104 |
, m_id(zoneId) {} |
| 105 |
|
| 106 |
//! destructor |
| 107 |
~SubDocument() final {} |
| 108 |
|
| 109 |
//! operator!= |
| 110 |
bool operator!=(MWAWSubDocument const &doc) const final |
| 111 |
{ |
| 112 |
if (MWAWSubDocument::operator!=(doc)) return true; |
| 113 |
auto const *sDoc = dynamic_cast<SubDocument const *>(&doc); |
| 114 |
if (!sDoc) return true; |
| 115 |
if (m_id != sDoc->m_id) return true; |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
//! returns the subdocument \a id |
| 120 |
long getId() const |
| 121 |
{ |
| 122 |
return m_id; |
| 123 |
} |
| 124 |
//! sets the subdocument \a id |
| 125 |
void setId(long vid) |
| 126 |
{ |
| 127 |
m_id = vid; |
| 128 |
} |
| 129 |
|
| 130 |
//! the parser function |
| 131 |
void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final; |
| 132 |
|
| 133 |
protected: |
| 134 |
//! the subdocument id |
| 135 |
long m_id; |
| 136 |
}; |
| 137 |
|
| 138 |
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) |
| 139 |
{ |
| 140 |
if (!listener.get()) { |
| 141 |
MWAW_DEBUG_MSG(("HanMacWrdJParserInternal::SubDocument::parse: no listener\n")); |
| 142 |
return; |
| 143 |
} |
| 144 |
if (type != libmwaw::DOC_HEADER_FOOTER) { |
| 145 |
MWAW_DEBUG_MSG(("HanMacWrdJParserInternal::SubDocument::parse: unexpected document type\n")); |
| 146 |
return; |
| 147 |
} |
| 148 |
auto *parser=dynamic_cast<HanMacWrdJParser *>(m_parser); |
| 149 |
if (!parser) { |
| 150 |
MWAW_DEBUG_MSG(("HanMacWrdJParserInternal::SubDocument::parse: no parser\n")); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
long pos = m_input->tell(); |
| 155 |
parser->sendText(m_id, 0); |
| 156 |
m_input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
//////////////////////////////////////////////////////////// |
| 161 |
// constructor/destructor + basic interface ... |
| 162 |
//////////////////////////////////////////////////////////// |
| 163 |
HanMacWrdJParser::HanMacWrdJParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header) |
| 164 |
: MWAWTextParser(input, rsrcParser, header) |
| 165 |
, m_state() |
| 166 |
, m_graphParser() |
| 167 |
, m_textParser() |
| 168 |
{ |
| 169 |
init(); |
| 170 |
} |
| 171 |
|
| 172 |
HanMacWrdJParser::~HanMacWrdJParser() |
| 173 |
{ |
| 174 |
} |
| 175 |
|
| 176 |
void HanMacWrdJParser::init() |
| 177 |
{ |
| 178 |
resetTextListener(); |
| 179 |
setAsciiName("main-1"); |
| 180 |
|
| 181 |
m_state.reset(new HanMacWrdJParserInternal::State); |
| 182 |
|
| 183 |
// reduce the margin (in case, the page is not defined) |
| 184 |
getPageSpan().setMargins(0.1); |
| 185 |
|
| 186 |
m_graphParser.reset(new HanMacWrdJGraph(*this)); |
| 187 |
m_textParser.reset(new HanMacWrdJText(*this)); |
| 188 |
} |
| 189 |
|
| 190 |
bool HanMacWrdJParser::sendText(long id, long cPos, MWAWListenerPtr listener) |
| 191 |
{ |
| 192 |
return m_textParser->sendText(id, cPos, listener); |
| 193 |
} |
| 194 |
|
| 195 |
bool HanMacWrdJParser::canSendTextAsGraphic(long id, long cPos) |
| 196 |
{ |
| 197 |
return m_textParser->canSendTextAsGraphic(id, cPos); |
| 198 |
} |
| 199 |
|
| 200 |
bool HanMacWrdJParser::sendZone(long zId) |
| 201 |
{ |
| 202 |
MWAWPosition pos(MWAWVec2f(0,0), MWAWVec2f(0,0), librevenge::RVNG_POINT); |
| 203 |
pos.setRelativePosition(MWAWPosition::Char); |
| 204 |
return m_graphParser->sendFrame(zId, pos); |
| 205 |
} |
| 206 |
|
| 207 |
bool HanMacWrdJParser::getColor(int colId, int patternId, MWAWColor &color) const |
| 208 |
{ |
| 209 |
return m_graphParser->getColor(colId, patternId, color); |
| 210 |
} |
| 211 |
|
| 212 |
//////////////////////////////////////////////////////////// |
| 213 |
// position and height |
| 214 |
//////////////////////////////////////////////////////////// |
| 215 |
MWAWVec2f HanMacWrdJParser::getPageLeftTop() const |
| 216 |
{ |
| 217 |
return MWAWVec2f(float(getPageSpan().getMarginLeft()), |
| 218 |
float(getPageSpan().getMarginTop()+m_state->m_headerHeight/72.0)); |
| 219 |
} |
| 220 |
|
| 221 |
//////////////////////////////////////////////////////////// |
| 222 |
// new page |
| 223 |
//////////////////////////////////////////////////////////// |
| 224 |
void HanMacWrdJParser::newPage(int number) |
| 225 |
{ |
| 226 |
if (number <= m_state->m_actPage || number > m_state->m_numPages) |
| 227 |
return; |
| 228 |
|
| 229 |
while (m_state->m_actPage < number) { |
| 230 |
m_state->m_actPage++; |
| 231 |
if (!getTextListener() || m_state->m_actPage == 1) |
| 232 |
continue; |
| 233 |
getTextListener()->insertBreak(MWAWTextListener::PageBreak); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
bool HanMacWrdJParser::readClassicHeader(HanMacWrdJZoneHeader &header, long endPos) |
| 238 |
{ |
| 239 |
header=HanMacWrdJZoneHeader(header.m_isMain); |
| 240 |
MWAWInputStreamPtr input = getInput(); |
| 241 |
long pos = input->tell(); |
| 242 |
header.m_length = long(input->readULong(4)); |
| 243 |
long headerEnd=pos+4+header.m_length; |
| 244 |
|
| 245 |
if ((endPos>0&&headerEnd>endPos) || (endPos<0&&!input->checkPosition(headerEnd))) |
| 246 |
return false; |
| 247 |
header.m_n = static_cast<int>(input->readLong(2)); |
| 248 |
header.m_values[0]=static_cast<int>(input->readLong(2)); |
| 249 |
header.m_fieldSize=static_cast<int>(input->readLong(2)); |
| 250 |
if (header.m_length < 16+header.m_n*header.m_fieldSize) |
| 251 |
return false; |
| 252 |
for (int i = 0; i < 3; i++) |
| 253 |
header.m_values[i+1]=static_cast<int>(input->readLong(2)); |
| 254 |
header.m_id=long(input->readULong(4)); |
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
//////////////////////////////////////////////////////////// |
| 259 |
// the parser |
| 260 |
//////////////////////////////////////////////////////////// |
| 261 |
void HanMacWrdJParser::parse(librevenge::RVNGTextInterface *docInterface) |
| 262 |
{ |
| 263 |
if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException()); |
| 264 |
bool ok = true; |
| 265 |
try { |
| 266 |
// create the asciiFile |
| 267 |
ascii().setStream(getInput()); |
| 268 |
ascii().open(asciiName()); |
| 269 |
|
| 270 |
checkHeader(nullptr); |
| 271 |
ok = createZones(); |
| 272 |
if (ok) { |
| 273 |
createDocument(docInterface); |
| 274 |
std::vector<long> tokenIds = m_textParser->getTokenIdList(); |
| 275 |
m_graphParser->sendPageGraphics(tokenIds); |
| 276 |
m_textParser->sendMainText(); |
| 277 |
#ifdef DEBUG |
| 278 |
m_textParser->flushExtra(); |
| 279 |
m_graphParser->flushExtra(); |
| 280 |
#endif |
| 281 |
} |
| 282 |
ascii().reset(); |
| 283 |
} |
| 284 |
catch (...) { |
| 285 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::parse: exception catched when parsing\n")); |
| 286 |
ok = false; |
| 287 |
} |
| 288 |
|
| 289 |
resetTextListener(); |
| 290 |
if (!ok) throw(libmwaw::ParseException()); |
| 291 |
} |
| 292 |
|
| 293 |
//////////////////////////////////////////////////////////// |
| 294 |
// create the document |
| 295 |
//////////////////////////////////////////////////////////// |
| 296 |
void HanMacWrdJParser::createDocument(librevenge::RVNGTextInterface *documentInterface) |
| 297 |
{ |
| 298 |
if (!documentInterface) return; |
| 299 |
if (getTextListener()) { |
| 300 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::createDocument: listener already exist\n")); |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
// update the page |
| 305 |
m_state->m_actPage = 0; |
| 306 |
|
| 307 |
int numPage = m_textParser->numPages(); |
| 308 |
if (m_graphParser->numPages() > numPage) |
| 309 |
numPage = m_graphParser->numPages(); |
| 310 |
m_state->m_numPages = numPage; |
| 311 |
|
| 312 |
// create the page list |
| 313 |
MWAWPageSpan ps(getPageSpan()); |
| 314 |
if (m_state->m_headerId) { |
| 315 |
MWAWHeaderFooter header(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL); |
| 316 |
header.m_subDocument.reset(new HanMacWrdJParserInternal::SubDocument(*this, getInput(), m_state->m_headerId)); |
| 317 |
ps.setHeaderFooter(header); |
| 318 |
} |
| 319 |
if (m_state->m_footerId) { |
| 320 |
MWAWHeaderFooter footer(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL); |
| 321 |
footer.m_subDocument.reset(new HanMacWrdJParserInternal::SubDocument(*this, getInput(), m_state->m_footerId)); |
| 322 |
ps.setHeaderFooter(footer); |
| 323 |
} |
| 324 |
ps.setPageSpan(m_state->m_numPages+1); |
| 325 |
std::vector<MWAWPageSpan> pageList(1,ps); |
| 326 |
|
| 327 |
// |
| 328 |
MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface)); |
| 329 |
setTextListener(listen); |
| 330 |
listen->startDocument(); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
//////////////////////////////////////////////////////////// |
| 335 |
// |
| 336 |
// Intermediate level |
| 337 |
// |
| 338 |
//////////////////////////////////////////////////////////// |
| 339 |
bool HanMacWrdJParser::createZones() |
| 340 |
{ |
| 341 |
MWAWInputStreamPtr input = getInput(); |
| 342 |
long pos = input->tell(); |
| 343 |
if (!readHeaderEnd()) |
| 344 |
input->seek(pos+34, librevenge::RVNG_SEEK_SET); |
| 345 |
if (!readZonesList()) |
| 346 |
return false; |
| 347 |
m_state->m_zonesIdList.clear(); |
| 348 |
m_state->m_zonesIdList.resize(16,0); |
| 349 |
/* some zones do not seem to appear in this list, so we must track them */ |
| 350 |
std::vector<MWAWEntry> newEntriesList; |
| 351 |
for (auto const &it : m_state->m_zonesMap) { |
| 352 |
auto const &entry=it.second; |
| 353 |
if (!entry.valid()) continue; |
| 354 |
if (m_state->m_zonesMap.find(entry.end()) != m_state->m_zonesMap.end()) |
| 355 |
continue; |
| 356 |
|
| 357 |
MWAWEntry newEntry; |
| 358 |
newEntry.setBegin(entry.end()); |
| 359 |
while (checkEntry(newEntry)) { |
| 360 |
if (!newEntry.valid()) break; |
| 361 |
newEntriesList.push_back(newEntry); |
| 362 |
|
| 363 |
long newBeginPos=newEntry.end(); |
| 364 |
if (m_state->m_zonesMap.find(newBeginPos) != m_state->m_zonesMap.end()) |
| 365 |
break; |
| 366 |
newEntry=MWAWEntry(); |
| 367 |
newEntry.setBegin(newBeginPos); |
| 368 |
} |
| 369 |
} |
| 370 |
for (MWAWEntry const &zone : newEntriesList) { |
| 371 |
if (!zone.valid()) |
| 372 |
continue; |
| 373 |
m_state->m_zonesMap.insert |
| 374 |
(std::map<long,MWAWEntry>::value_type(zone.begin(),zone)); |
| 375 |
} |
| 376 |
|
| 377 |
// now parse the different zones |
| 378 |
for (auto &it : m_state->m_zonesMap) { |
| 379 |
if (it.second.begin()<=0) continue; |
| 380 |
readZone(it.second); |
| 381 |
} |
| 382 |
|
| 383 |
// retrieve the text type, look for header/footer and pass information to text parser |
| 384 |
std::map<long,int> idTypeMap = m_graphParser->getTextFrameInformations(); |
| 385 |
for (auto const &typeIt : idTypeMap) { |
| 386 |
if (typeIt.second==1) |
| 387 |
m_state->m_headerId = typeIt.first; |
| 388 |
else if (typeIt.second==2) |
| 389 |
m_state->m_footerId = typeIt.first; |
| 390 |
} |
| 391 |
m_textParser->updateTextZoneTypes(idTypeMap); |
| 392 |
|
| 393 |
// and the footnote |
| 394 |
long fntTextId; |
| 395 |
std::vector<long> fntFirstPosList; |
| 396 |
if (m_graphParser->getFootnoteInformations(fntTextId, fntFirstPosList)) |
| 397 |
m_textParser->updateFootnoteInformations(fntTextId, fntFirstPosList); |
| 398 |
|
| 399 |
// finish graphparser preparation |
| 400 |
m_graphParser->prepareStructures(); |
| 401 |
|
| 402 |
libmwaw::DebugStream f; |
| 403 |
for (auto const &it : m_state->m_zonesMap) { |
| 404 |
MWAWEntry const &zone = it.second; |
| 405 |
if (zone.begin()<=0) continue; |
| 406 |
if (zone.isParsed()) continue; |
| 407 |
f.str(""); |
| 408 |
f << "Entries(" << zone.name() << "):"; |
| 409 |
ascii().addPos(zone.begin()); |
| 410 |
ascii().addNote(f.str().c_str()); |
| 411 |
} |
| 412 |
|
| 413 |
return m_state->m_zonesMap.size(); |
| 414 |
} |
| 415 |
|
| 416 |
//////////////////////////////////////////////////////////// |
| 417 |
// |
| 418 |
// Low level |
| 419 |
// |
| 420 |
//////////////////////////////////////////////////////////// |
| 421 |
bool HanMacWrdJParser::checkEntry(MWAWEntry &entry) |
| 422 |
{ |
| 423 |
MWAWInputStreamPtr input = getInput(); |
| 424 |
if (entry.begin()<=0 || !input->checkPosition(entry.begin())) |
| 425 |
return false; |
| 426 |
long pos = input->tell(); |
| 427 |
input->seek(entry.begin(), librevenge::RVNG_SEEK_SET); |
| 428 |
|
| 429 |
auto type = static_cast<int>(input->readULong(2)); |
| 430 |
long val = input->readLong(2); // always 0? |
| 431 |
auto length = long(input->readULong(4)); |
| 432 |
if (type >= 32 || length < 8 || !input->checkPosition(entry.begin()+length)) { |
| 433 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
entry.setId(type); |
| 438 |
entry.setLength(length); |
| 439 |
|
| 440 |
if (type>=0 && type <= 15) { |
| 441 |
static char const *what[] = { |
| 442 |
"FontDef", "Ruler", "Style", "FrameDef", "TZoneList", |
| 443 |
"TextZone", "Picture", "Table", "GraphData", "GroupData", |
| 444 |
"ZoneA", "ZoneB", "Section", "FtnDef", "ZoneE", "FontsName" |
| 445 |
}; |
| 446 |
entry.setName(what[type]); |
| 447 |
} |
| 448 |
else { |
| 449 |
std::stringstream s; |
| 450 |
s << "Zone" << std::hex << type << std::dec; |
| 451 |
entry.setName(s.str()); |
| 452 |
} |
| 453 |
|
| 454 |
libmwaw::DebugStream f; |
| 455 |
f << "Entries(" << entry.name() << ":"; |
| 456 |
if (val) f << "#unkn=" << val << ","; |
| 457 |
ascii().addPos(entry.begin()); |
| 458 |
ascii().addNote(f.str().c_str()); |
| 459 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
bool HanMacWrdJParser::readZonesList() |
| 464 |
{ |
| 465 |
MWAWInputStreamPtr input = getInput(); |
| 466 |
long pos = input->tell(); |
| 467 |
if (!input->checkPosition(pos+82)) |
| 468 |
return false; |
| 469 |
|
| 470 |
libmwaw::DebugStream f; |
| 471 |
f << "Entries(Zones):"; |
| 472 |
for (int i = 0; i < 7; i++) { // f0=a000 |
| 473 |
auto val = long(input->readULong(2)); |
| 474 |
if (val) f << "f" << i << "=" << std::hex << val << std::dec << ","; |
| 475 |
} |
| 476 |
ascii().addPos(pos); |
| 477 |
ascii().addNote(f.str().c_str()); |
| 478 |
|
| 479 |
pos=input->tell(); |
| 480 |
f.str(""); |
| 481 |
f << "Zones(A):"; |
| 482 |
|
| 483 |
for (int i = 0; i < 20; i++) { |
| 484 |
// sure up to Zonesb=FontNames Zones19=EOF |
| 485 |
auto ptr = long(input->readULong(4)); |
| 486 |
if (!ptr) continue; |
| 487 |
if (!input->checkPosition(ptr)) |
| 488 |
f << "###"; |
| 489 |
else if (i != 19) { // i==19: is end of file |
| 490 |
MWAWEntry zone; |
| 491 |
zone.setBegin(ptr); |
| 492 |
if (checkEntry(zone)) |
| 493 |
m_state->m_zonesMap.insert |
| 494 |
(std::map<long,MWAWEntry>::value_type(zone.begin(),zone)); |
| 495 |
else |
| 496 |
f << "###"; |
| 497 |
} |
| 498 |
f << "Zone" << i << "=" << std::hex << ptr << std::dec << ","; |
| 499 |
} |
| 500 |
ascii().addPos(pos); |
| 501 |
ascii().addNote(f.str().c_str()); |
| 502 |
|
| 503 |
ascii().addPos(input->tell()); |
| 504 |
ascii().addNote("_"); |
| 505 |
|
| 506 |
return m_state->m_zonesMap.size(); |
| 507 |
} |
| 508 |
|
| 509 |
bool HanMacWrdJParser::readZone(MWAWEntry &entry) |
| 510 |
{ |
| 511 |
if (entry.begin()<=0) { |
| 512 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZone: can not find the zone\n")); |
| 513 |
return false; |
| 514 |
} |
| 515 |
|
| 516 |
int localId = 0; |
| 517 |
if (entry.id() >= 0 && entry.id() <= 15) |
| 518 |
localId = m_state->m_zonesIdList[size_t(entry.id())]++; |
| 519 |
MWAWInputStreamPtr input = getInput(); |
| 520 |
libmwaw::DebugStream f; |
| 521 |
long pos = entry.begin(); |
| 522 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 523 |
|
| 524 |
f << "Entries(" << entry.name() << "):"; |
| 525 |
auto type = static_cast<int>(input->readULong(2)); // number between 0 and f |
| 526 |
f << "type=" << type << ","; |
| 527 |
long val = input->readLong(2); |
| 528 |
if (val) f << "f0=" << val << ","; |
| 529 |
entry.setLength(long(input->readULong(4))); |
| 530 |
if (entry.length() < 12 || !input->checkPosition(entry.end())) { |
| 531 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZone: header seems to short\n")); |
| 532 |
return false; |
| 533 |
} |
| 534 |
entry.setParsed(true); |
| 535 |
|
| 536 |
ascii().addPos(pos); |
| 537 |
ascii().addNote(f.str().c_str()); |
| 538 |
|
| 539 |
bool done = false; |
| 540 |
switch (entry.id()) { |
| 541 |
case 0: |
| 542 |
done = m_textParser->readFonts(entry); |
| 543 |
break; |
| 544 |
case 1: |
| 545 |
done = m_textParser->readParagraphs(entry); |
| 546 |
break; |
| 547 |
case 2: |
| 548 |
done = m_textParser->readStyles(entry); |
| 549 |
break; |
| 550 |
case 3: |
| 551 |
done = m_graphParser->readFrames(entry); |
| 552 |
break; |
| 553 |
case 4: |
| 554 |
done = m_textParser->readTextZonesList(entry); |
| 555 |
break; |
| 556 |
case 5: |
| 557 |
done = m_textParser->readTextZone(entry, localId); |
| 558 |
break; |
| 559 |
case 6: |
| 560 |
done = m_graphParser->readPicture(entry, localId); |
| 561 |
break; |
| 562 |
case 7: |
| 563 |
done = m_graphParser->readTable(entry, localId); |
| 564 |
break; |
| 565 |
case 8: |
| 566 |
done = m_graphParser->readGraphData(entry, localId); |
| 567 |
break; |
| 568 |
case 9: |
| 569 |
done = m_graphParser->readGroupData(entry, localId); |
| 570 |
break; |
| 571 |
case 10: // always 5 zones with N=0? the preference ? |
| 572 |
done = readZoneA(entry); |
| 573 |
break; |
| 574 |
case 11: |
| 575 |
done = readZoneB(entry); |
| 576 |
break; |
| 577 |
case 12: |
| 578 |
done = m_textParser->readSections(entry); |
| 579 |
break; |
| 580 |
case 13: |
| 581 |
done = m_textParser->readFtnPos(entry); |
| 582 |
break; |
| 583 |
case 15: |
| 584 |
done = m_textParser->readFontNames(entry); |
| 585 |
break; |
| 586 |
default: |
| 587 |
break; |
| 588 |
} |
| 589 |
|
| 590 |
if (done) return true; |
| 591 |
|
| 592 |
f.str(""); |
| 593 |
f << entry.name() << "[data]:"; |
| 594 |
ascii().addPos(pos+8); |
| 595 |
ascii().addNote(f.str().c_str()); |
| 596 |
|
| 597 |
return true; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
// read the print info data |
| 602 |
bool HanMacWrdJParser::readPrintInfo(MWAWEntry const &entry) |
| 603 |
{ |
| 604 |
MWAWInputStreamPtr input = getInput(); |
| 605 |
libmwaw::DebugFile &asciiFile = ascii(); |
| 606 |
long pos = entry.begin(); |
| 607 |
|
| 608 |
if (!input->checkPosition(entry.end())) { |
| 609 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readPrintInfo: the zone seems too short\n")); |
| 610 |
return false; |
| 611 |
} |
| 612 |
|
| 613 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 614 |
libmwaw::DebugStream f; |
| 615 |
entry.setParsed(true); |
| 616 |
|
| 617 |
f << "Entries(PrintInfo):"; |
| 618 |
long val; |
| 619 |
|
| 620 |
val = long(input->readULong(2)); |
| 621 |
if (val != 1) f << "firstSectNumber=" << val << ","; |
| 622 |
val = long(input->readULong(2)); |
| 623 |
if (val) f << "f0=" << val << ","; |
| 624 |
int dim[4] = {0,0,0,0}; |
| 625 |
for (auto &d : dim) d=int(input->readLong(2)); |
| 626 |
f << "paper=[" << dim[1] << "x" << dim[0] << " " << dim[3] << "x" << dim[2] << "],"; |
| 627 |
f << "margins?=["; |
| 628 |
float margins[4] = {0,0,0,0}; // L, T, R, B |
| 629 |
for (auto &margin : margins) { |
| 630 |
margin = float(input->readLong(4))/65536.f; |
| 631 |
f << margin << ","; |
| 632 |
} |
| 633 |
f << "],"; |
| 634 |
|
| 635 |
// after unknown |
| 636 |
asciiFile.addDelimiter(input->tell(),'|'); |
| 637 |
asciiFile.addPos(pos); |
| 638 |
asciiFile.addNote(f.str().c_str()); |
| 639 |
pos += 44; |
| 640 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 641 |
f.str(""); |
| 642 |
f << "PrintInfo(B):"; |
| 643 |
|
| 644 |
// print info |
| 645 |
libmwaw::PrinterInfo info; |
| 646 |
if (!info.read(input)) { |
| 647 |
f << "###"; |
| 648 |
asciiFile.addPos(pos); |
| 649 |
asciiFile.addNote(f.str().c_str()); |
| 650 |
return false; |
| 651 |
} |
| 652 |
|
| 653 |
MWAWVec2i paperSize = info.paper().size(); |
| 654 |
MWAWVec2i pageSize = info.page().size(); |
| 655 |
|
| 656 |
bool useDocInfo = (float(dim[3]-dim[1])>margins[2]+margins[0]) && |
| 657 |
(float(dim[2]-dim[0])>margins[3]+margins[1]); |
| 658 |
bool usePrintInfo = pageSize.x() > 0 && pageSize.y() > 0 && |
| 659 |
paperSize.x() > 0 && paperSize.y() > 0; |
| 660 |
|
| 661 |
MWAWVec2f lTopMargin(margins[0],margins[1]), rBotMargin(margins[2],margins[3]); |
| 662 |
// define margin from print info |
| 663 |
if (useDocInfo) |
| 664 |
paperSize = MWAWVec2i(dim[3]-dim[1],dim[2]-dim[0]); |
| 665 |
else if (usePrintInfo) { |
| 666 |
lTopMargin= MWAWVec2f(-float(info.paper().pos(0)[0]), -float(info.paper().pos(0)[1])); |
| 667 |
rBotMargin=MWAWVec2f(info.paper().pos(1) - info.page().pos(1)); |
| 668 |
|
| 669 |
// move margin left | top |
| 670 |
float decalX = lTopMargin.x() > 14 ? 14 : 0; |
| 671 |
float decalY = lTopMargin.y() > 14 ? 14 : 0; |
| 672 |
lTopMargin -= MWAWVec2f(decalX, decalY); |
| 673 |
rBotMargin += MWAWVec2f(decalX, decalY); |
| 674 |
} |
| 675 |
|
| 676 |
// decrease right | bottom |
| 677 |
float rightMarg = rBotMargin.x() -10; |
| 678 |
if (rightMarg < 0) rightMarg=0; |
| 679 |
float botMarg = rBotMargin.y() -50; |
| 680 |
if (botMarg < 0) botMarg=0; |
| 681 |
|
| 682 |
if (useDocInfo || usePrintInfo) { |
| 683 |
getPageSpan().setMarginTop(double(lTopMargin.y())/72.0); |
| 684 |
getPageSpan().setMarginBottom(double(botMarg)/72.0); |
| 685 |
getPageSpan().setMarginLeft(double(lTopMargin.x())/72.0); |
| 686 |
getPageSpan().setMarginRight(double(rightMarg)/72.0); |
| 687 |
getPageSpan().setFormLength(paperSize.y()/72.); |
| 688 |
getPageSpan().setFormWidth(paperSize.x()/72.); |
| 689 |
|
| 690 |
f << info; |
| 691 |
} |
| 692 |
else |
| 693 |
f << "###"; |
| 694 |
|
| 695 |
asciiFile.addPos(pos); |
| 696 |
asciiFile.addNote(f.str().c_str()); |
| 697 |
if (input->tell()!=entry.end()) { |
| 698 |
asciiFile.addDelimiter(input->tell(),'|'); |
| 699 |
input->seek(entry.end(), librevenge::RVNG_SEEK_SET); |
| 700 |
} |
| 701 |
return true; |
| 702 |
} |
| 703 |
|
| 704 |
/* a unknown zone find always with N=0, so probably bad when N\neq 0 */ |
| 705 |
bool HanMacWrdJParser::readZoneA(MWAWEntry const &entry) |
| 706 |
{ |
| 707 |
if (!entry.valid()) { |
| 708 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: called without any entry\n")); |
| 709 |
return false; |
| 710 |
} |
| 711 |
if (entry.length() == 8) { |
| 712 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: find an empty zone\n")); |
| 713 |
entry.setParsed(true); |
| 714 |
return true; |
| 715 |
} |
| 716 |
if (entry.length() < 12) { |
| 717 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: the entry seems too short\n")); |
| 718 |
return false; |
| 719 |
} |
| 720 |
long pos = entry.begin()+8; // skip header |
| 721 |
long endPos = entry.end(); |
| 722 |
|
| 723 |
MWAWInputStreamPtr input = getInput(); |
| 724 |
libmwaw::DebugFile &asciiFile = ascii(); |
| 725 |
libmwaw::DebugStream f; |
| 726 |
entry.setParsed(true); |
| 727 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 728 |
// first read the header (supposing that fieldSize=4) |
| 729 |
f << entry.name() << "[header]:"; |
| 730 |
HanMacWrdJZoneHeader mainHeader(true); |
| 731 |
if (!readClassicHeader(mainHeader,endPos) || |
| 732 |
(mainHeader.m_n && mainHeader.m_fieldSize!=4)) { |
| 733 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: can not read an entry\n")); |
| 734 |
f << "###sz=" << mainHeader.m_length; |
| 735 |
asciiFile.addPos(pos); |
| 736 |
asciiFile.addNote(f.str().c_str()); |
| 737 |
return false; |
| 738 |
} |
| 739 |
if (mainHeader.m_n != 0) { |
| 740 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: Arggh, find unexpected N\n")); |
| 741 |
f << "###"; |
| 742 |
} |
| 743 |
long headerEnd=pos+4+mainHeader.m_length; |
| 744 |
f << mainHeader; |
| 745 |
f << "listId=[" << std::hex; |
| 746 |
std::vector<long> listIds; |
| 747 |
for (int i = 0; i < mainHeader.m_n; i++) { |
| 748 |
auto val = long(input->readULong(4)); |
| 749 |
listIds.push_back(val); |
| 750 |
f << val << ","; |
| 751 |
} |
| 752 |
f << std::dec << "],"; |
| 753 |
asciiFile.addPos(pos); |
| 754 |
asciiFile.addNote(f.str().c_str()); |
| 755 |
if (input->tell()!=headerEnd) { |
| 756 |
asciiFile.addDelimiter(input->tell(),'|'); |
| 757 |
input->seek(headerEnd, librevenge::RVNG_SEEK_SET); |
| 758 |
} |
| 759 |
|
| 760 |
// find now 5 zone with size 2a, 10, 24, 1ea, 10 or 0x38 |
| 761 |
/* ZoneA-4: |
| 762 |
000000100000000000040000000a000100000000 |
| 763 |
000000380002000800040000000a000100000000 |
| 764 |
0000000400000006000a0000000000000000022c00000034004e15c400000000004e2100018d00b4 |
| 765 |
size, N, ...??? |
| 766 |
*/ |
| 767 |
long const expectedSize[]= {0x2a, 0x10, 0x24, 0x1ea, 0x10}; |
| 768 |
for (int i = 0; i < 5; i++) { |
| 769 |
pos = input->tell(); |
| 770 |
if (pos==endPos) |
| 771 |
return true; |
| 772 |
f.str(""); |
| 773 |
f << entry.name() << "-" << i << ":"; |
| 774 |
auto dataSz = long(input->readULong(4)); |
| 775 |
long zoneEnd=pos+4+dataSz; |
| 776 |
|
| 777 |
if (zoneEnd>endPos) { |
| 778 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: can not read an entry\n")); |
| 779 |
f << "###sz=" << dataSz; |
| 780 |
asciiFile.addPos(pos); |
| 781 |
asciiFile.addNote(f.str().c_str()); |
| 782 |
return false; |
| 783 |
} |
| 784 |
if (dataSz != expectedSize[i] && dataSz!=0) { |
| 785 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: find unexpected size for zone %d\n", i)); |
| 786 |
f << "###sz=" << dataSz; |
| 787 |
} |
| 788 |
asciiFile.addPos(pos); |
| 789 |
asciiFile.addNote(f.str().c_str()); |
| 790 |
input->seek(zoneEnd, librevenge::RVNG_SEEK_SET); |
| 791 |
} |
| 792 |
pos = input->tell(); |
| 793 |
if (pos!=endPos) { |
| 794 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneA: find unexpected end data\n")); |
| 795 |
f.str(""); |
| 796 |
f << entry.name() << "###:"; |
| 797 |
asciiFile.addPos(pos); |
| 798 |
asciiFile.addNote(f.str().c_str()); |
| 799 |
} |
| 800 |
|
| 801 |
return true; |
| 802 |
} |
| 803 |
|
| 804 |
/* a unknown zone */ |
| 805 |
bool HanMacWrdJParser::readZoneB(MWAWEntry const &entry) |
| 806 |
{ |
| 807 |
if (!entry.valid()) { |
| 808 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneB: called without any entry\n")); |
| 809 |
return false; |
| 810 |
} |
| 811 |
if (entry.length() == 8) { |
| 812 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneB: find an empty zone\n")); |
| 813 |
entry.setParsed(true); |
| 814 |
return true; |
| 815 |
} |
| 816 |
if (entry.length() < 12) { |
| 817 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneB: the entry seems too short\n")); |
| 818 |
return false; |
| 819 |
} |
| 820 |
long pos = entry.begin()+8; // skip header |
| 821 |
long endPos = entry.end(); |
| 822 |
|
| 823 |
MWAWInputStreamPtr input = getInput(); |
| 824 |
libmwaw::DebugFile &asciiFile = ascii(); |
| 825 |
libmwaw::DebugStream f; |
| 826 |
entry.setParsed(true); |
| 827 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 828 |
// first read the header (supposing that fieldSize=4) |
| 829 |
f << entry.name() << "[header]:"; |
| 830 |
HanMacWrdJZoneHeader mainHeader(true); |
| 831 |
if (!readClassicHeader(mainHeader,endPos) || |
| 832 |
(mainHeader.m_n && mainHeader.m_fieldSize!=44)) { |
| 833 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneB: can not read an entry\n")); |
| 834 |
f << "###sz=" << mainHeader.m_length; |
| 835 |
asciiFile.addPos(pos); |
| 836 |
asciiFile.addNote(f.str().c_str()); |
| 837 |
return false; |
| 838 |
} |
| 839 |
long headerEnd=pos+4+mainHeader.m_length; |
| 840 |
f << mainHeader; |
| 841 |
asciiFile.addPos(pos); |
| 842 |
asciiFile.addNote(f.str().c_str()); |
| 843 |
|
| 844 |
for (int i = 0; i < mainHeader.m_n; i++) { |
| 845 |
pos = input->tell(); |
| 846 |
f.str(""); |
| 847 |
f << entry.name() << "-data" << i << ":"; |
| 848 |
asciiFile.addPos(pos); |
| 849 |
asciiFile.addNote(f.str().c_str()); |
| 850 |
input->seek(pos+44, librevenge::RVNG_SEEK_SET); |
| 851 |
} |
| 852 |
if (input->tell()!=headerEnd) { |
| 853 |
asciiFile.addDelimiter(input->tell(),'|'); |
| 854 |
input->seek(headerEnd, librevenge::RVNG_SEEK_SET); |
| 855 |
} |
| 856 |
|
| 857 |
for (int i = 0; i < mainHeader.m_n; i++) { |
| 858 |
pos = input->tell(); |
| 859 |
f.str(""); |
| 860 |
f << entry.name() << "-" << i << ":"; |
| 861 |
auto dataSz = long(input->readULong(4)); |
| 862 |
if (pos+4+dataSz>endPos) { |
| 863 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneB: can not read an entry\n")); |
| 864 |
f << "###sz=" << dataSz; |
| 865 |
asciiFile.addPos(pos); |
| 866 |
asciiFile.addNote(f.str().c_str()); |
| 867 |
return false; |
| 868 |
} |
| 869 |
asciiFile.addPos(pos); |
| 870 |
asciiFile.addNote(f.str().c_str()); |
| 871 |
if (dataSz) input->seek(dataSz, librevenge::RVNG_SEEK_CUR); |
| 872 |
} |
| 873 |
pos = input->tell(); |
| 874 |
if (pos!=endPos) { |
| 875 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneB: find unexpected end data\n")); |
| 876 |
f.str(""); |
| 877 |
f << entry.name() << "###:"; |
| 878 |
asciiFile.addPos(pos); |
| 879 |
asciiFile.addNote(f.str().c_str()); |
| 880 |
} |
| 881 |
return true; |
| 882 |
} |
| 883 |
|
| 884 |
// a small unknown zone |
| 885 |
bool HanMacWrdJParser::readHeaderEnd() |
| 886 |
{ |
| 887 |
MWAWInputStreamPtr input = getInput(); |
| 888 |
libmwaw::DebugFile &asciiFile = ascii(); |
| 889 |
long pos=input->tell(); |
| 890 |
long endPos=pos+34; |
| 891 |
|
| 892 |
if (!input->checkPosition(endPos)) { |
| 893 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readHeaderEnd: the zone seems too short\n")); |
| 894 |
return false; |
| 895 |
} |
| 896 |
|
| 897 |
libmwaw::DebugStream f; |
| 898 |
f << "Entries(HeaderEnd):"; |
| 899 |
|
| 900 |
long val = input->readLong(4); // 1c58b4 |
| 901 |
f << "dim?=" << float(val)/65536.f << ","; |
| 902 |
|
| 903 |
for (int i = 0; i < 4; i++) { // always 7,7,0,0 |
| 904 |
val = input->readLong(2); |
| 905 |
if (!val) continue; |
| 906 |
f << "f" << i << "=" << val << ","; |
| 907 |
} |
| 908 |
val = input->readLong(4); // 2d5ab ~dim/10. |
| 909 |
f << "dim2?=" << float(val)/65536.f << ","; |
| 910 |
for (int i = 0; i < 4; i++) { // 0,4,0, 0 |
| 911 |
val = long(input->readULong(2)); |
| 912 |
if (!val) continue; |
| 913 |
f << "g" << i << "=" << val << ","; |
| 914 |
} |
| 915 |
for (int i = 0; i < 4; i++) { // 1,1,1,0 |
| 916 |
val = input->readLong(1); |
| 917 |
if (!val) continue; |
| 918 |
f << "h" << i << "=" << val << ","; |
| 919 |
} |
| 920 |
for (int i = 0; i < 3; i++) { // always 6,0,0 |
| 921 |
val = input->readLong(2); |
| 922 |
if (!val) continue; |
| 923 |
f << "j" << i << "=" << val << ","; |
| 924 |
} |
| 925 |
|
| 926 |
asciiFile.addPos(pos); |
| 927 |
asciiFile.addNote(f.str().c_str()); |
| 928 |
if (input->tell()!=endPos) { |
| 929 |
asciiFile.addDelimiter(input->tell(),'|'); |
| 930 |
input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 931 |
} |
| 932 |
return true; |
| 933 |
} |
| 934 |
|
| 935 |
#ifdef DEBUG |
| 936 |
bool HanMacWrdJParser::readZoneWithHeader(MWAWEntry const &entry) |
| 937 |
{ |
| 938 |
if (!entry.valid()) { |
| 939 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneWithHeader: called without any entry\n")); |
| 940 |
return false; |
| 941 |
} |
| 942 |
if (entry.length() < 12) { |
| 943 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneWithHeader: the entry seems too short\n")); |
| 944 |
return false; |
| 945 |
} |
| 946 |
|
| 947 |
MWAWInputStreamPtr input = getInput(); |
| 948 |
libmwaw::DebugFile &asciiFile = ascii(); |
| 949 |
libmwaw::DebugStream f; |
| 950 |
entry.setParsed(true); |
| 951 |
|
| 952 |
long pos = entry.begin()+8; // skip header |
| 953 |
long endPos = entry.end(); |
| 954 |
input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 955 |
|
| 956 |
// first read the header |
| 957 |
f << entry.name() << "[header]:"; |
| 958 |
HanMacWrdJZoneHeader mainHeader(false); |
| 959 |
if (!readClassicHeader(mainHeader,endPos)) { |
| 960 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneWithHeader: can not read an entry\n")); |
| 961 |
f << "###sz=" << mainHeader.m_length; |
| 962 |
asciiFile.addPos(pos); |
| 963 |
asciiFile.addNote(f.str().c_str()); |
| 964 |
return false; |
| 965 |
} |
| 966 |
long headerEnd=pos+4+mainHeader.m_length; |
| 967 |
f << mainHeader; |
| 968 |
|
| 969 |
int val; |
| 970 |
f << "unk=["; |
| 971 |
for (int i = 0; i < mainHeader.m_n; i++) { |
| 972 |
f << "["; |
| 973 |
for (int j=0; j < mainHeader.m_fieldSize; j++) { |
| 974 |
val = static_cast<int>(input->readULong(1)); |
| 975 |
if (val) f << std::hex << val << std::dec << ","; |
| 976 |
else f << "_,"; |
| 977 |
} |
| 978 |
f << "]"; |
| 979 |
} |
| 980 |
f << "],"; |
| 981 |
if (input->tell()!=headerEnd) { |
| 982 |
asciiFile.addDelimiter(input->tell(),'|'); |
| 983 |
input->seek(headerEnd, librevenge::RVNG_SEEK_SET); |
| 984 |
} |
| 985 |
asciiFile.addPos(pos); |
| 986 |
asciiFile.addNote(f.str().c_str()); |
| 987 |
|
| 988 |
for (int i = 0; i < mainHeader.m_n; i++) { |
| 989 |
pos = input->tell(); |
| 990 |
f.str(""); |
| 991 |
f << entry.name() << "-" << i << ":"; |
| 992 |
auto dataSz = long(input->readULong(4)); |
| 993 |
if (pos+4+dataSz>endPos) { |
| 994 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneWithHeader: can not read an entry\n")); |
| 995 |
f << "###sz=" << dataSz; |
| 996 |
asciiFile.addPos(pos); |
| 997 |
asciiFile.addNote(f.str().c_str()); |
| 998 |
return false; |
| 999 |
} |
| 1000 |
asciiFile.addPos(pos); |
| 1001 |
asciiFile.addNote(f.str().c_str()); |
| 1002 |
if (dataSz) input->seek(dataSz, librevenge::RVNG_SEEK_CUR); |
| 1003 |
} |
| 1004 |
asciiFile.addPos(endPos); |
| 1005 |
asciiFile.addNote("_"); |
| 1006 |
if (input->tell()==endPos) |
| 1007 |
return true; |
| 1008 |
|
| 1009 |
// often another list of data |
| 1010 |
int i=0; |
| 1011 |
while (!input->isEnd()) { |
| 1012 |
pos = input->tell(); |
| 1013 |
if (pos==endPos) |
| 1014 |
return true; |
| 1015 |
f.str(""); |
| 1016 |
f << entry.name() << "-A" << i++ << ":"; |
| 1017 |
auto dataSz = long(input->readULong(4)); |
| 1018 |
if (pos+4+dataSz>endPos) { |
| 1019 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::readZoneWithHeader: can not read an entry\n")); |
| 1020 |
f << "###sz=" << dataSz; |
| 1021 |
asciiFile.addPos(pos); |
| 1022 |
asciiFile.addNote(f.str().c_str()); |
| 1023 |
return false; |
| 1024 |
} |
| 1025 |
asciiFile.addPos(pos); |
| 1026 |
asciiFile.addNote(f.str().c_str()); |
| 1027 |
if (dataSz) input->seek(dataSz, librevenge::RVNG_SEEK_CUR); |
| 1028 |
} |
| 1029 |
return true; |
| 1030 |
} |
| 1031 |
#endif |
| 1032 |
|
| 1033 |
//////////////////////////////////////////////////////////// |
| 1034 |
// read the header |
| 1035 |
//////////////////////////////////////////////////////////// |
| 1036 |
bool HanMacWrdJParser::checkHeader(MWAWHeader *header, bool strict) |
| 1037 |
{ |
| 1038 |
*m_state = HanMacWrdJParserInternal::State(); |
| 1039 |
|
| 1040 |
MWAWInputStreamPtr input = getInput(); |
| 1041 |
if (!input || !input->hasDataFork()) |
| 1042 |
return false; |
| 1043 |
libmwaw::DebugStream f; |
| 1044 |
f << "FileHeader:"; |
| 1045 |
long const headerSize=0x33c; |
| 1046 |
if (!input->checkPosition(headerSize)) { |
| 1047 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::checkHeader: file is too short\n")); |
| 1048 |
return false; |
| 1049 |
} |
| 1050 |
input->seek(0,librevenge::RVNG_SEEK_SET); |
| 1051 |
int head[3]; |
| 1052 |
for (auto &h : head) h = static_cast<int>(input->readULong(2)); |
| 1053 |
if (head[0] != 0x594c || head[1] != 0x5953 || head[2] != 0x100) |
| 1054 |
return false; |
| 1055 |
auto val = static_cast<int>(input->readLong(1)); |
| 1056 |
if (val==1) f << "hasPassword,"; |
| 1057 |
else if (val) { |
| 1058 |
if (strict) return false; |
| 1059 |
f << "#hasPassword=" << val << ","; |
| 1060 |
} |
| 1061 |
val = static_cast<int>(input->readLong(1)); |
| 1062 |
if (val) { |
| 1063 |
if (strict && (val<0||val>2)) return false; |
| 1064 |
f << "f0=" << val << ","; |
| 1065 |
} |
| 1066 |
|
| 1067 |
m_state->m_zonesListBegin = 0x460; |
| 1068 |
for (int i = 0; i < 4; i++) { // always 0? |
| 1069 |
val = static_cast<int>(input->readLong(2)); |
| 1070 |
if (val) f << "f" << i+1 << "=" << val << ","; |
| 1071 |
} |
| 1072 |
ascii().addPos(0); |
| 1073 |
ascii().addNote(f.str().c_str()); |
| 1074 |
|
| 1075 |
long pos; |
| 1076 |
// title, subject, author, revision, remark, [2 documents tags], mail: |
| 1077 |
int const fieldSizes[] = { 128, 128, 32, 32, 256, 36, 64, 64, 64 }; |
| 1078 |
for (int i = 0; i < 9; i++) { |
| 1079 |
pos=input->tell(); |
| 1080 |
if (i == 5) { |
| 1081 |
ascii().addPos(pos); |
| 1082 |
ascii().addNote("FileHeader[DocTags]:"); |
| 1083 |
input->seek(pos+fieldSizes[i], librevenge::RVNG_SEEK_SET); |
| 1084 |
pos=input->tell(); |
| 1085 |
MWAWEntry printInfo; |
| 1086 |
printInfo.setBegin(pos); |
| 1087 |
printInfo.setLength(164); |
| 1088 |
if (!readPrintInfo(printInfo)) |
| 1089 |
input->seek(pos+164, librevenge::RVNG_SEEK_SET); |
| 1090 |
|
| 1091 |
pos=input->tell(); |
| 1092 |
ascii().addPos(pos); |
| 1093 |
ascii().addNote("FileHeader[DocEnd]"); |
| 1094 |
input->seek(pos+60, librevenge::RVNG_SEEK_SET); |
| 1095 |
continue; |
| 1096 |
} |
| 1097 |
auto fSz = static_cast<int>(input->readULong(1)); |
| 1098 |
if (fSz >= fieldSizes[i]) { |
| 1099 |
if (strict) |
| 1100 |
return false; |
| 1101 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::checkHeader: can not read field size %i\n", i)); |
| 1102 |
ascii().addPos(pos); |
| 1103 |
ascii().addNote("FileHeader#"); |
| 1104 |
input->seek(pos+fieldSizes[i], librevenge::RVNG_SEEK_SET); |
| 1105 |
continue; |
| 1106 |
} |
| 1107 |
f.str(""); |
| 1108 |
if (fSz == 0) |
| 1109 |
f << "_"; |
| 1110 |
else { |
| 1111 |
std::string name(""); |
| 1112 |
for (int c = 0; c < fSz; c++) |
| 1113 |
name+=char(input->readULong(1)); |
| 1114 |
f.str(""); |
| 1115 |
f << "FileHeader[field"<<i<< "]:" << name; |
| 1116 |
} |
| 1117 |
ascii().addPos(pos); |
| 1118 |
ascii().addNote(f.str().c_str()); |
| 1119 |
input->seek(pos+fieldSizes[i], librevenge::RVNG_SEEK_SET); |
| 1120 |
} |
| 1121 |
|
| 1122 |
pos=input->tell(); |
| 1123 |
f.str(""); |
| 1124 |
f << "FileHeader(B):"; // unknown 76 bytes |
| 1125 |
ascii().addPos(pos); |
| 1126 |
ascii().addNote(f.str().c_str()); |
| 1127 |
|
| 1128 |
input->seek(m_state->m_zonesListBegin, librevenge::RVNG_SEEK_SET); |
| 1129 |
if (header) |
| 1130 |
header->reset(MWAWDocument::MWAW_T_HANMACWORDJ, 1); |
| 1131 |
|
| 1132 |
return true; |
| 1133 |
} |
| 1134 |
|
| 1135 |
//////////////////////////////////////////////////////////// |
| 1136 |
// code to uncompress a zone |
| 1137 |
//////////////////////////////////////////////////////////// |
| 1138 |
/* implementation of a basic splay tree to decode a block |
| 1139 |
freely inspired from: ftp://ftp.cs.uiowa.edu/pub/jones/compress/minunsplay.c : |
| 1140 |
|
| 1141 |
Author: Douglas Jones, Dept. of Comp. Sci., U. of Iowa, Iowa City, IA 52242. |
| 1142 |
Date: Nov. 5, 1990. |
| 1143 |
(derived from the Feb. 14 1990 version by stripping out irrelevancies) |
| 1144 |
(minor revision of Feb. 20, 1989 to add exit(0) at end of program). |
| 1145 |
(minor revision of Nov. 14, 1988 to detect corrupt input better). |
| 1146 |
(minor revision of Aug. 8, 1988 to eliminate unused vars, fix -c). |
| 1147 |
Copyright: This material is derived from code Copyrighted 1988 by |
| 1148 |
Jeffrey Chilton and Douglas Jones. That code contained a copyright |
| 1149 |
notice allowing copying for personal or research purposes, so long |
| 1150 |
as copies of the code were not sold for direct commercial advantage. |
| 1151 |
This version of the code has been stripped of most of the material |
| 1152 |
added by Jeff Chilton, and this release of the code may be used or |
| 1153 |
copied for any purpose, public or private. |
| 1154 |
Patents: The algorithm central to this code is entirely the invention of |
| 1155 |
Douglas Jones, and it has not been patented. Any patents claiming |
| 1156 |
to cover this material are invalid. |
| 1157 |
Exportability: Splay-tree based compression algorithms may be used for |
| 1158 |
cryptography, and when used as such, they may not be exported from |
| 1159 |
the United States without appropriate approval. All cryptographic |
| 1160 |
features of the original version of this code have been removed. |
| 1161 |
Language: C |
| 1162 |
Purpose: Data uncompression program, a companion to minsplay.c |
| 1163 |
Algorithm: Uses a splay-tree based prefix code. For a full understanding |
| 1164 |
of the operation of this data compression scheme, refer to the paper |
| 1165 |
"Applications of Splay Trees to Data Compression" by Douglas W. Jones |
| 1166 |
in Communications of the ACM, Aug. 1988, pages 996-1007. |
| 1167 |
*/ |
| 1168 |
bool HanMacWrdJParser::decodeZone(MWAWEntry const &entry, librevenge::RVNGBinaryData &dt) |
| 1169 |
{ |
| 1170 |
if (!entry.valid() || entry.length() <= 4) { |
| 1171 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::decodeZone: called with an invalid zone\n")); |
| 1172 |
return false; |
| 1173 |
} |
| 1174 |
short const maxChar=256; |
| 1175 |
short const maxSucc=maxChar+1; |
| 1176 |
short const twoMaxChar=2*maxChar+1; |
| 1177 |
short const twoMaxSucc=2*maxSucc; |
| 1178 |
|
| 1179 |
// first build the tree data |
| 1180 |
short left[maxSucc]; |
| 1181 |
short right[maxSucc]; |
| 1182 |
short up[twoMaxSucc]; |
| 1183 |
for (short i = 0; i <= twoMaxChar; ++i) |
| 1184 |
up[i] = i/2; |
| 1185 |
for (short j = 0; j <= maxChar; ++j) { |
| 1186 |
left[j] = short(2 * j); |
| 1187 |
right[j] = short(2 * j + 1); |
| 1188 |
} |
| 1189 |
|
| 1190 |
short const root = 0; |
| 1191 |
short const sizeBit = 8; |
| 1192 |
short const highBit=128; /* mask for the most sig bit of 8 bit byte */ |
| 1193 |
|
| 1194 |
short bitbuffer = 0; /* buffer to hold a byte for unpacking bits */ |
| 1195 |
short bitcounter = 0; /* count of remaining bits in buffer */ |
| 1196 |
|
| 1197 |
MWAWInputStreamPtr input = getInput(); |
| 1198 |
input->seek(entry.begin()+4, librevenge::RVNG_SEEK_SET); |
| 1199 |
dt.clear(); |
| 1200 |
while (!input->isEnd() && input->tell() < entry.end()) { |
| 1201 |
short a = root; |
| 1202 |
bool ok = true; |
| 1203 |
do { /* once for each bit on path */ |
| 1204 |
if (bitcounter == 0) { |
| 1205 |
if (input->isEnd() || input->tell() >= entry.end()) { |
| 1206 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::decodeZone: find some uncomplete data for zone %lx\n", static_cast<long unsigned int>(entry.begin()))); |
| 1207 |
dt.append(static_cast<unsigned char>(a)); |
| 1208 |
ok = false; |
| 1209 |
break; |
| 1210 |
} |
| 1211 |
|
| 1212 |
bitbuffer = short(input->readULong(1)); |
| 1213 |
bitcounter = sizeBit; |
| 1214 |
} |
| 1215 |
--bitcounter; |
| 1216 |
if ((bitbuffer & highBit) != 0) |
| 1217 |
a = right[a]; |
| 1218 |
else |
| 1219 |
a = left[a]; |
| 1220 |
bitbuffer = short(bitbuffer << 1); |
| 1221 |
} |
| 1222 |
while (a <= maxChar); |
| 1223 |
if (!ok) |
| 1224 |
break; |
| 1225 |
dt.append(static_cast<unsigned char>(a - maxSucc)); |
| 1226 |
|
| 1227 |
/* now splay tree about leaf a */ |
| 1228 |
do { /* walk up the tree semi-rotating pairs of nodes */ |
| 1229 |
short c; |
| 1230 |
if ((c = up[a]) != root) { /* a pair remains */ |
| 1231 |
short d = up[c]; |
| 1232 |
short b = left[d]; |
| 1233 |
if (c == b) { |
| 1234 |
b = right[d]; |
| 1235 |
right[d] = a; |
| 1236 |
} |
| 1237 |
else |
| 1238 |
left[d] = a; |
| 1239 |
if (left[c] == a) |
| 1240 |
left[c] = b; |
| 1241 |
else |
| 1242 |
right[c] = b; |
| 1243 |
up[a] = d; |
| 1244 |
up[b] = c; |
| 1245 |
a = d; |
| 1246 |
} |
| 1247 |
else |
| 1248 |
a = c; |
| 1249 |
} |
| 1250 |
while (a != root); |
| 1251 |
} |
| 1252 |
if (dt.size()==0) { |
| 1253 |
MWAW_DEBUG_MSG(("HanMacWrdJParser::decodeZone: oops an empty zone\n")); |
| 1254 |
return false; |
| 1255 |
} |
| 1256 |
|
| 1257 |
ascii().skipZone(entry.begin()+4, entry.end()-1); |
| 1258 |
return true; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |