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