| 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 |
/* This header contains code specific to a small picture |
| 35 |
*/ |
| 36 |
#include <iostream> |
| 37 |
#include <sstream> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include <stack> |
| 41 |
|
| 42 |
#include "libmwaw_internal.hxx" |
| 43 |
|
| 44 |
#include <librevenge/librevenge.h> |
| 45 |
#include <librevenge-stream/librevenge-stream.h> |
| 46 |
#include <libmwaw/libmwaw.hxx> |
| 47 |
|
| 48 |
#include "MWAWPropertyHandler.hxx" |
| 49 |
|
| 50 |
//////////////////////////////////////////////////// |
| 51 |
// |
| 52 |
// MWAWPropertyHandlerEncoder |
| 53 |
// |
| 54 |
//////////////////////////////////////////////////// |
| 55 |
MWAWPropertyHandlerEncoder::MWAWPropertyHandlerEncoder() |
| 56 |
: m_f(std::ios::in | std::ios::out | std::ios::binary) |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
MWAWPropertyHandler::~MWAWPropertyHandler() |
| 61 |
{ |
| 62 |
} |
| 63 |
|
| 64 |
void MWAWPropertyHandlerEncoder::insertElement(const char *psName) |
| 65 |
{ |
| 66 |
m_f << 'E'; |
| 67 |
writeString(librevenge::RVNGString(psName)); |
| 68 |
} |
| 69 |
|
| 70 |
void MWAWPropertyHandlerEncoder::insertElement |
| 71 |
(const char *psName, const librevenge::RVNGPropertyList &xPropList) |
| 72 |
{ |
| 73 |
m_f << 'S'; |
| 74 |
writeString(librevenge::RVNGString(psName)); |
| 75 |
writePropertyList(xPropList); |
| 76 |
} |
| 77 |
|
| 78 |
void MWAWPropertyHandlerEncoder::characters(librevenge::RVNGString const &sCharacters) |
| 79 |
{ |
| 80 |
if (sCharacters.len()==0) return; |
| 81 |
m_f << 'T'; |
| 82 |
writeString(sCharacters); |
| 83 |
} |
| 84 |
|
| 85 |
void MWAWPropertyHandlerEncoder::writeString(const librevenge::RVNGString &string) |
| 86 |
{ |
| 87 |
unsigned long sz = string.size()+1; |
| 88 |
writeLong(long(sz)); |
| 89 |
m_f.write(string.cstr(), static_cast<int>(sz)); |
| 90 |
} |
| 91 |
|
| 92 |
void MWAWPropertyHandlerEncoder::writeLong(long val) |
| 93 |
{ |
| 94 |
auto value=static_cast<int32_t>(val); |
| 95 |
unsigned char const allValue[]= {static_cast<unsigned char>(value&0xFF), static_cast<unsigned char>((value>>8)&0xFF), static_cast<unsigned char>((value>>16)&0xFF), static_cast<unsigned char>((value>>24)&0xFF)}; |
| 96 |
m_f.write(reinterpret_cast<const char *>(allValue), 4); |
| 97 |
} |
| 98 |
|
| 99 |
void MWAWPropertyHandlerEncoder::writeProperty(const char *key, const librevenge::RVNGProperty &prop) |
| 100 |
{ |
| 101 |
if (!key) { |
| 102 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerEncoder::writeProperty: key is NULL\n")); |
| 103 |
return; |
| 104 |
} |
| 105 |
writeString(librevenge::RVNGString(key)); |
| 106 |
writeString(prop.getStr()); |
| 107 |
} |
| 108 |
|
| 109 |
void MWAWPropertyHandlerEncoder::writePropertyList(const librevenge::RVNGPropertyList &xPropList) |
| 110 |
{ |
| 111 |
librevenge::RVNGPropertyList::Iter i(xPropList); |
| 112 |
long numElt = 0; |
| 113 |
for (i.rewind(); i.next();) numElt++; |
| 114 |
writeLong(numElt); |
| 115 |
for (i.rewind(); i.next();) { |
| 116 |
auto const *child=xPropList.child(i.key()); |
| 117 |
if (!child) { |
| 118 |
m_f << 'p'; |
| 119 |
writeProperty(i.key(),*i()); |
| 120 |
continue; |
| 121 |
} |
| 122 |
m_f << 'v'; |
| 123 |
writeString(librevenge::RVNGString(i.key())); |
| 124 |
writePropertyListVector(*child); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void MWAWPropertyHandlerEncoder::writePropertyListVector(const librevenge::RVNGPropertyListVector &vect) |
| 129 |
{ |
| 130 |
writeLong(long(vect.count())); |
| 131 |
for (unsigned long i=0; i < vect.count(); i++) |
| 132 |
writePropertyList(vect[i]); |
| 133 |
} |
| 134 |
|
| 135 |
bool MWAWPropertyHandlerEncoder::getData(librevenge::RVNGBinaryData &data) |
| 136 |
{ |
| 137 |
data.clear(); |
| 138 |
std::string d=m_f.str(); |
| 139 |
if (d.length() == 0) return false; |
| 140 |
data.append(reinterpret_cast<const unsigned char *>(d.c_str()), d.length()); |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
/* \brief Internal: the property decoder |
| 145 |
* |
| 146 |
* \note see MWAWPropertyHandlerEncoder for the format |
| 147 |
*/ |
| 148 |
class MWAWPropertyHandlerDecoder |
| 149 |
{ |
| 150 |
public: |
| 151 |
//! constructor given a MWAWPropertyHandler |
| 152 |
explicit MWAWPropertyHandlerDecoder(MWAWPropertyHandler *hdl=nullptr) |
| 153 |
: m_handler(hdl) |
| 154 |
{ |
| 155 |
} |
| 156 |
|
| 157 |
//! tries to read the data |
| 158 |
bool readData(librevenge::RVNGBinaryData const &encoded) |
| 159 |
{ |
| 160 |
try { |
| 161 |
auto *inp = const_cast<librevenge::RVNGInputStream *>(encoded.getDataStream()); |
| 162 |
if (!inp) return false; |
| 163 |
|
| 164 |
while (!inp->isEnd()) { |
| 165 |
unsigned const char *c; |
| 166 |
unsigned long numRead; |
| 167 |
|
| 168 |
c = inp->read(1,numRead); |
| 169 |
if (!c || numRead != 1) { |
| 170 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder: can not read data type \n")); |
| 171 |
return false; |
| 172 |
} |
| 173 |
switch (*c) { |
| 174 |
case 'E': |
| 175 |
if (!readInsertElement(*inp)) return false; |
| 176 |
break; |
| 177 |
case 'S': |
| 178 |
if (!readInsertElementWithList(*inp)) return false; |
| 179 |
break; |
| 180 |
case 'T': |
| 181 |
if (!readCharacters(*inp)) return false; |
| 182 |
break; |
| 183 |
default: |
| 184 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder: unknown type='%c' \n", *c)); |
| 185 |
return false; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
catch (...) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
protected: |
| 196 |
//! reads an simple element |
| 197 |
bool readInsertElement(librevenge::RVNGInputStream &input) |
| 198 |
{ |
| 199 |
librevenge::RVNGString s; |
| 200 |
if (!readString(input, s)) return false; |
| 201 |
|
| 202 |
if (s.empty()) { |
| 203 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readInsertElement find empty tag\n")); |
| 204 |
return false; |
| 205 |
} |
| 206 |
if (m_handler) m_handler->insertElement(s.cstr()); |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
//! reads an element with a property list |
| 211 |
bool readInsertElementWithList(librevenge::RVNGInputStream &input) |
| 212 |
{ |
| 213 |
librevenge::RVNGString s; |
| 214 |
if (!readString(input, s)) return false; |
| 215 |
|
| 216 |
if (s.empty()) { |
| 217 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readInsertElementWithProperty: find empty tag\n")); |
| 218 |
return false; |
| 219 |
} |
| 220 |
librevenge::RVNGPropertyList lists; |
| 221 |
if (!readPropertyList(input, lists)) { |
| 222 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readInsertElementWithProperty: can not read propertyList for tag %s\n", |
| 223 |
s.cstr())); |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
if (m_handler) m_handler->insertElement(s.cstr(), lists); |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
//! reads a set of characters |
| 232 |
bool readCharacters(librevenge::RVNGInputStream &input) |
| 233 |
{ |
| 234 |
librevenge::RVNGString s; |
| 235 |
if (!readString(input, s)) return false; |
| 236 |
if (!s.size()) return true; |
| 237 |
if (m_handler) m_handler->characters(s); |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
// |
| 242 |
// low level |
| 243 |
// |
| 244 |
|
| 245 |
//! low level: reads a property vector: number of properties list followed by list of properties list |
| 246 |
bool readPropertyListVector(librevenge::RVNGInputStream &input, librevenge::RVNGPropertyListVector &vect) |
| 247 |
{ |
| 248 |
long numElt; |
| 249 |
if (!readLong(input, numElt)) return false; |
| 250 |
|
| 251 |
if (numElt < 0) { |
| 252 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readPropertyListVector: can not read numElt=%ld\n", |
| 253 |
numElt)); |
| 254 |
return false; |
| 255 |
} |
| 256 |
for (long i = 0; i < numElt; i++) { |
| 257 |
librevenge::RVNGPropertyList lists; |
| 258 |
if (readPropertyList(input, lists)) { |
| 259 |
vect.append(lists); |
| 260 |
continue; |
| 261 |
} |
| 262 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readPropertyListVector: can not read property list %ld\n", i)); |
| 263 |
return false; |
| 264 |
} |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
//! low level: reads a property list: number of properties followed by list of properties |
| 269 |
bool readPropertyList(librevenge::RVNGInputStream &input, librevenge::RVNGPropertyList &lists) |
| 270 |
{ |
| 271 |
long numElt; |
| 272 |
if (!readLong(input, numElt)) return false; |
| 273 |
|
| 274 |
if (numElt < 0) { |
| 275 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readPropertyList: can not read numElt=%ld\n", |
| 276 |
numElt)); |
| 277 |
return false; |
| 278 |
} |
| 279 |
for (long i = 0; i < numElt; i++) { |
| 280 |
unsigned const char *c; |
| 281 |
unsigned long numRead; |
| 282 |
c = input.read(1,numRead); |
| 283 |
if (!c || numRead != 1) { |
| 284 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder:readPropertyList can not read data type for child %ld\n", i)); |
| 285 |
return false; |
| 286 |
} |
| 287 |
switch (*c) { |
| 288 |
case 'p': |
| 289 |
if (readProperty(input, lists)) break; |
| 290 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readPropertyList: can not read property %ld\n", i)); |
| 291 |
return false; |
| 292 |
case 'v': { |
| 293 |
librevenge::RVNGString key; |
| 294 |
librevenge::RVNGPropertyListVector vect; |
| 295 |
if (!readString(input, key) || key.empty() || !readPropertyListVector(input, vect)) { |
| 296 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readPropertyList: can not read propertyVector for child %ld\n", i)); |
| 297 |
return false; |
| 298 |
} |
| 299 |
lists.insert(key.cstr(),vect); |
| 300 |
break; |
| 301 |
} |
| 302 |
default: |
| 303 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder:readPropertyList find unknown type %c for child %ld\n", char(*c), i)); |
| 304 |
return false; |
| 305 |
} |
| 306 |
} |
| 307 |
return true; |
| 308 |
} |
| 309 |
|
| 310 |
//! low level: reads a property and its value, adds it to \a list |
| 311 |
bool readProperty(librevenge::RVNGInputStream &input, librevenge::RVNGPropertyList &list) |
| 312 |
{ |
| 313 |
librevenge::RVNGString key, val; |
| 314 |
if (!readString(input, key)) return false; |
| 315 |
if (!readString(input, val)) return false; |
| 316 |
|
| 317 |
list.insert(key.cstr(), val); |
| 318 |
librevenge::RVNGProperty const *prop=list[key.cstr()]; |
| 319 |
if (!prop) return true; |
| 320 |
librevenge::RVNGUnit unit=prop->getUnit(); |
| 321 |
if (unit==librevenge::RVNG_POINT) |
| 322 |
list.insert(key.cstr(), prop->getDouble()/72., librevenge::RVNG_INCH); |
| 323 |
else if (unit==librevenge::RVNG_TWIP) |
| 324 |
list.insert(key.cstr(), prop->getDouble()/1440., librevenge::RVNG_INCH); |
| 325 |
return true; |
| 326 |
} |
| 327 |
|
| 328 |
//! low level: reads a string : size and string |
| 329 |
bool readString(librevenge::RVNGInputStream &input, librevenge::RVNGString &s) |
| 330 |
{ |
| 331 |
long numC = 0; |
| 332 |
if (!readLong(input, numC)) return false; |
| 333 |
if (numC==0) { |
| 334 |
s = librevenge::RVNGString(""); |
| 335 |
return true; |
| 336 |
} |
| 337 |
unsigned long numRead; |
| 338 |
const unsigned char *dt = input.read(static_cast<unsigned long>(numC), numRead); |
| 339 |
if (dt == nullptr || numRead != static_cast<unsigned long>(numC)) { |
| 340 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readString: can not read a string\n")); |
| 341 |
return false; |
| 342 |
} |
| 343 |
s = librevenge::RVNGString(reinterpret_cast<const char *>(dt)); |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
//! low level: reads an long value |
| 348 |
static bool readLong(librevenge::RVNGInputStream &input, long &val) |
| 349 |
{ |
| 350 |
unsigned long numRead = 0; |
| 351 |
const unsigned char *dt = input.read(4, numRead); |
| 352 |
if (dt == nullptr || numRead != 4) { |
| 353 |
MWAW_DEBUG_MSG(("MWAWPropertyHandlerDecoder::readLong: can not read long\n")); |
| 354 |
return false; |
| 355 |
} |
| 356 |
val = long((dt[3]<<24)|(dt[2]<<16)|(dt[1]<<8)|dt[0]); |
| 357 |
return true; |
| 358 |
} |
| 359 |
private: |
| 360 |
MWAWPropertyHandlerDecoder(MWAWPropertyHandlerDecoder const &orig) = delete; |
| 361 |
MWAWPropertyHandlerDecoder &operator=(MWAWPropertyHandlerDecoder const &) = delete; |
| 362 |
|
| 363 |
protected: |
| 364 |
//! the streamfile |
| 365 |
MWAWPropertyHandler *m_handler; |
| 366 |
}; |
| 367 |
|
| 368 |
//////////////////////////////////////////////////// |
| 369 |
// |
| 370 |
// MWAWPropertyHandler |
| 371 |
// |
| 372 |
//////////////////////////////////////////////////// |
| 373 |
bool MWAWPropertyHandler::checkData(librevenge::RVNGBinaryData const &encoded) |
| 374 |
{ |
| 375 |
MWAWPropertyHandlerDecoder decod; |
| 376 |
return decod.readData(encoded); |
| 377 |
} |
| 378 |
|
| 379 |
bool MWAWPropertyHandler::readData(librevenge::RVNGBinaryData const &encoded) |
| 380 |
{ |
| 381 |
MWAWPropertyHandlerDecoder decod(this); |
| 382 |
return decod.readData(encoded); |
| 383 |
} |
| 384 |
|
| 385 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |