| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* libwps |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2009, 2011 Alonso Laurent ([email protected]) |
| 11 | * Copyright (C) 2006, 2007 Andrew Ziem |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 14 | * Copyright (C) 2003-2005 William Lachance ([email protected]) |
| 15 | * |
| 16 | * For minor contributions see the git repository. |
| 17 | * |
| 18 | * Alternatively, the contents of this file may be used under the terms |
| 19 | * of the GNU Lesser General Public License Version 2.1 or later |
| 20 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 21 | * applicable instead of those above. |
| 22 | * |
| 23 | * For further information visit http://libwps.sourceforge.net |
| 24 | */ |
| 25 | |
| 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | |
| 29 | #include <librevenge/librevenge.h> |
| 30 | |
| 31 | #include "WPSOLEObject.h" |
| 32 | |
| 33 | #include "WPSDebug.h" |
| 34 | #include "WPSStream.h" |
| 35 | #include "WPSStringStream.h" |
| 36 | |
| 37 | using namespace libwps; |
| 38 | |
| 39 | |
| 40 | //////////////////////////////////////////////////////////// |
| 41 | // read the OLE object |
| 42 | //////////////////////////////////////////////////////////// |
| 43 | bool WPSOLEObject::readMetafile(std::shared_ptr<WPSStream> stream, WPSEmbeddedObject &object, long endPos, bool strict) |
| 44 | { |
| 45 | if (!stream) return false; |
| 46 | RVNGInputStreamPtr &input=stream->m_input; |
| 47 | libwps::DebugFile &ascFile=stream->m_ascii; |
| 48 | libwps::DebugStream f; |
| 49 | |
| 50 | if (endPos<=0) endPos=stream->m_eof; |
| 51 | else if (endPos>stream->m_eof) endPos=stream->m_eof; |
| 52 | |
| 53 | long pos = input->tell(); |
| 54 | if (pos+8+4>endPos) |
| 55 | return false; |
| 56 | f << "Entries(Metafile):"; |
| 57 | f << "type=" << libwps::readU16(input) << ","; |
| 58 | float fDim[2]; |
| 59 | for (auto &d : fDim) d=float(libwps::read16(input)/1440.); |
| 60 | if (fDim[0]<0||fDim[1]<0) |
| 61 | { |
| 62 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 63 | return false; |
| 64 | } |
| 65 | if (object.m_size==Vec2f() && fDim[0]>0 && fDim[1]>0) |
| 66 | { |
| 67 | object.m_size=Vec2f(fDim[0],fDim[1]); |
| 68 | f << "sz=" << object.m_size << ","; |
| 69 | } |
| 70 | input->seek(2, librevenge::RVNG_SEEK_CUR); // seek handle |
| 71 | if (strict) |
| 72 | { |
| 73 | if (!checkIsWMF(stream, endPos)) |
| 74 | { |
| 75 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 76 | return false; |
| 77 | } |
| 78 | input->seek(pos+8, librevenge::RVNG_SEEK_SET); |
| 79 | } |
| 80 | librevenge::RVNGBinaryData data; |
| 81 | if (!libwps::readData(input, static_cast<unsigned long>(endPos-pos-8), data)) |
| 82 | { |
| 83 | WPS_DEBUG_MSG(("WPSOLEObject::readMetafile: I can not find the picture\n")); |
| 84 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 85 | return false; |
| 86 | } |
| 87 | object.add(data,"application/x-wmf"); |
| 88 | #ifdef DEBUG_WITH_FILES |
| 89 | ascFile.skipZone(pos+8, endPos-1); |
| 90 | std::stringstream s; |
| 91 | static int fileId=0; |
| 92 | s << "PictMeta" << ++fileId << ".wmf"; |
| 93 | libwps::Debug::dumpFile(data, s.str().c_str()); |
| 94 | #endif |
| 95 | |
| 96 | input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 97 | ascFile.addPos(pos); |
| 98 | ascFile.addNote(f.str().c_str()); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool WPSOLEObject::readWMF(std::shared_ptr<WPSStream> stream, WPSEmbeddedObject &object,long endPos) |
| 103 | { |
| 104 | if (!stream) return false; |
| 105 | RVNGInputStreamPtr &input=stream->m_input; |
| 106 | libwps::DebugFile &ascFile=stream->m_ascii; |
| 107 | long pos=input->tell(); |
| 108 | |
| 109 | long lastPos=endPos<=0 ? stream->m_eof : endPos; |
| 110 | if (lastPos>stream->m_eof) lastPos=stream->m_eof; |
| 111 | if (!checkIsWMF(stream,lastPos)) return false; |
| 112 | |
| 113 | input->seek(pos+6, librevenge::RVNG_SEEK_SET); |
| 114 | auto fSize = long(libwps::read32(input)); |
| 115 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 116 | librevenge::RVNGBinaryData data; |
| 117 | if (!libwps::readData(input, static_cast<unsigned long>(2*fSize), data)) |
| 118 | { |
| 119 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 120 | return false; |
| 121 | } |
| 122 | object.add(data,"application/x-wmf"); |
| 123 | #ifdef DEBUG_WITH_FILES |
| 124 | std::stringstream f; |
| 125 | static volatile int actPict = 0; |
| 126 | f << "WMF" << actPict++ << ".wmf"; |
| 127 | libwps::Debug::dumpFile(data, f.str().c_str()); |
| 128 | ascFile.skipZone(pos,pos+2*fSize-1); |
| 129 | #endif |
| 130 | if (endPos>0 && input->tell()!=endPos) |
| 131 | { |
| 132 | ascFile.addPos(input->tell()); |
| 133 | ascFile.addNote("_"); |
| 134 | input->seek(endPos, librevenge::RVNG_SEEK_SET); |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | bool WPSOLEObject::readOLE(std::shared_ptr<WPSStream> stream, WPSEmbeddedObject &object, long endPos) |
| 140 | { |
| 141 | if (!stream) return false; |
| 142 | RVNGInputStreamPtr &input=stream->m_input; |
| 143 | libwps::DebugStream f; |
| 144 | |
| 145 | if (endPos<=0) endPos=stream->m_eof; |
| 146 | else if (endPos>stream->m_eof) endPos=stream->m_eof; |
| 147 | |
| 148 | long pos = input->tell(); |
| 149 | if (pos+24>endPos || libwps::read32(input)!=0x501) |
| 150 | { |
| 151 | WPS_DEBUG_MSG(("WPSOLEObject::readOLE: not a picture header\n")); |
| 152 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 153 | return false; |
| 154 | } |
| 155 | f.str(""); |
| 156 | f << "Entries(OLEObject):"; |
| 157 | auto type=int(readU32(input)); |
| 158 | f << "type=" << type << ","; |
| 159 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 160 | bool ok=false; |
| 161 | switch (type) |
| 162 | { |
| 163 | case 1: |
| 164 | WPS_DEBUG_MSG(("WPSOLEObject::readOLE: find a link ole\n")); |
| 165 | f << "link,"; |
| 166 | break; |
| 167 | case 2: |
| 168 | ok=readEmbeddedOLE(stream, object, endPos); |
| 169 | break; |
| 170 | case 3: |
| 171 | case 5: |
| 172 | ok=readStaticOLE(stream, object, endPos); |
| 173 | break; |
| 174 | default: |
| 175 | WPS_DEBUG_MSG(("WPSOLEObject::readOLE: find a unknown type\n")); |
| 176 | f << "unknown,"; |
| 177 | break; |
| 178 | } |
| 179 | if (!ok) |
| 180 | { |
| 181 | libwps::DebugFile &ascFile=stream->m_ascii; |
| 182 | f << "###"; |
| 183 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 184 | ascFile.addPos(pos); |
| 185 | ascFile.addNote(f.str().c_str()); |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | bool WPSOLEObject::readEmbeddedOLE(std::shared_ptr<WPSStream> stream, WPSEmbeddedObject &object, long endPos) |
| 191 | { |
| 192 | if (!stream) return false; |
| 193 | RVNGInputStreamPtr &input=stream->m_input; |
| 194 | libwps::DebugFile &ascFile=stream->m_ascii; |
| 195 | libwps::DebugStream f; |
| 196 | |
| 197 | long pos = input->tell(); |
| 198 | if (pos+24+4>endPos || libwps::readU32(input)!=0x501) |
| 199 | { |
| 200 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 201 | return false; |
| 202 | } |
| 203 | auto type=int(libwps::readU32(input)); |
| 204 | if (type!=2) |
| 205 | { |
| 206 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 207 | return false; |
| 208 | } |
| 209 | f << "Entries(OLEObject)[embedded]:"; |
| 210 | std::string names[3]; |
| 211 | for (auto &name : names) |
| 212 | { |
| 213 | if (!readString(stream, name, endPos) || input->tell()+4>endPos) |
| 214 | { |
| 215 | WPS_DEBUG_MSG(("WPSOLEObject::readEmbeddedOLE: can not read the name\n")); |
| 216 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 217 | return false; |
| 218 | } |
| 219 | if (name.empty()) continue; |
| 220 | f << name << ","; |
| 221 | } |
| 222 | // find Paint.Picture, WangImage.Document |
| 223 | unsigned long dSz=libwps::readU32(input); |
| 224 | long actPos=input->tell(); |
| 225 | if (dSz>0x40000000 || dSz<10 || dSz>static_cast<unsigned long>(endPos-actPos)) |
| 226 | { |
| 227 | WPS_DEBUG_MSG(("WPSOLEObject::readOLE: pict size seems bad\n")); |
| 228 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | bool ok=true; |
| 233 | if (names[0]=="METAFILEPICT") |
| 234 | ok=readMetafile(stream, object, actPos+long(dSz)); |
| 235 | else |
| 236 | { |
| 237 | librevenge::RVNGBinaryData data; |
| 238 | if (!libwps::readData(input, dSz, data)) |
| 239 | ok=false; |
| 240 | else |
| 241 | { |
| 242 | object.add(data); |
| 243 | #ifdef DEBUG_WITH_FILES |
| 244 | ascFile.skipZone(actPos, actPos+long(dSz)-1); |
| 245 | std::stringstream s; |
| 246 | static int fileId=0; |
| 247 | s << "PictOLEEmbedded" << ++fileId << ".pct"; |
| 248 | libwps::Debug::dumpFile(data, s.str().c_str()); |
| 249 | #endif |
| 250 | } |
| 251 | } |
| 252 | if (!ok) |
| 253 | { |
| 254 | WPS_DEBUG_MSG(("WPSOLEObject::readEmbeddedOLE: I can not find the picture\n")); |
| 255 | f << "###"; |
| 256 | } |
| 257 | input->seek(actPos+long(dSz), librevenge::RVNG_SEEK_SET); |
| 258 | ascFile.addPos(pos); |
| 259 | ascFile.addNote(f.str().c_str()); |
| 260 | if (input->tell()<endPos) // normally followed by a static OLE |
| 261 | readStaticOLE(stream, object, endPos); |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | bool WPSOLEObject::readStaticOLE(std::shared_ptr<WPSStream> stream, WPSEmbeddedObject &object, long endPos) |
| 266 | { |
| 267 | if (!stream) return false; |
| 268 | RVNGInputStreamPtr &input=stream->m_input; |
| 269 | libwps::DebugFile &ascFile=stream->m_ascii; |
| 270 | libwps::DebugStream f; |
| 271 | |
| 272 | long pos = input->tell(); |
| 273 | if (pos+24+4>endPos || libwps::readU32(input)!=0x501) |
| 274 | { |
| 275 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 276 | return false; |
| 277 | } |
| 278 | auto type=int(libwps::readU32(input)); |
| 279 | if (type!=3 && type!=5) |
| 280 | { |
| 281 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 282 | return false; |
| 283 | } |
| 284 | f << "Entries(OLEObject)[static]:"; |
| 285 | f << "type=" << type << ","; |
| 286 | std::string name; |
| 287 | if (!readString(stream, name, endPos) || input->tell()+12>endPos) |
| 288 | { |
| 289 | WPS_DEBUG_MSG(("WPSOLEObject::readStaticOLE: can not read the name\n")); |
| 290 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 291 | return false; |
| 292 | } |
| 293 | // find METAFILEPICT |
| 294 | f << name << ","; |
| 295 | for (int i=0; i<2; ++i) // g0+g1~0, some application id? |
| 296 | { |
| 297 | auto val=long(libwps::read32(input)); |
| 298 | if (val) f << "g" << i << "=" << val << ","; |
| 299 | } |
| 300 | unsigned long dSz=libwps::readU32(input); |
| 301 | long actPos=input->tell(); |
| 302 | if (dSz>0x40000000 || dSz<10 || dSz>static_cast<unsigned long>(endPos-actPos)) |
| 303 | { |
| 304 | WPS_DEBUG_MSG(("WPSOLEObject::readOLE: pict size seems bad\n")); |
| 305 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | bool ok=true; |
| 310 | if (name=="METAFILEPICT") |
| 311 | ok=readMetafile(stream, object, actPos+long(dSz)); |
| 312 | else |
| 313 | { |
| 314 | librevenge::RVNGBinaryData data; |
| 315 | if (!libwps::readData(input, dSz, data)) |
| 316 | ok=false; |
| 317 | else |
| 318 | { |
| 319 | object.add(data); |
| 320 | #ifdef DEBUG_WITH_FILES |
| 321 | ascFile.skipZone(actPos, actPos+long(dSz)-1); |
| 322 | std::stringstream s; |
| 323 | static int fileId=0; |
| 324 | s << "PictOLEStatic" << ++fileId << ".pct"; |
| 325 | libwps::Debug::dumpFile(data, s.str().c_str()); |
| 326 | #endif |
| 327 | } |
| 328 | } |
| 329 | if (!ok) |
| 330 | { |
| 331 | WPS_DEBUG_MSG(("WPSOLEObject::readStaticOLE: I can not find the picture\n")); |
| 332 | f << "###"; |
| 333 | } |
| 334 | input->seek(actPos+long(dSz), librevenge::RVNG_SEEK_SET); |
| 335 | ascFile.addPos(pos); |
| 336 | ascFile.addNote(f.str().c_str()); |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | bool WPSOLEObject::readString(std::shared_ptr<WPSStream> stream, std::string &name, long endPos) |
| 341 | { |
| 342 | if (!stream) return false; |
| 343 | name=""; |
| 344 | RVNGInputStreamPtr &input=stream->m_input; |
| 345 | long pos=input->tell(); |
| 346 | if (pos+4>endPos) return false; |
| 347 | auto sSz=long(libwps::readU32(input)); |
| 348 | if (sSz<0 || sSz>endPos-pos-4) |
| 349 | { |
| 350 | WPS_DEBUG_MSG(("WPSOLEObject::readString: name size seems bad\n")); |
| 351 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 352 | return false; |
| 353 | } |
| 354 | if (sSz==0) return true; |
| 355 | for (long i=0; i+1<sSz; ++i) |
| 356 | { |
| 357 | auto c=char(libwps::readU8(input)); |
| 358 | if (c) |
| 359 | name+=c; |
| 360 | else |
| 361 | return false; |
| 362 | } |
| 363 | return libwps::readU8(input)==0; |
| 364 | } |
| 365 | |
| 366 | bool WPSOLEObject::checkIsWMF(std::shared_ptr<WPSStream> stream, long endPos) |
| 367 | { |
| 368 | RVNGInputStreamPtr &input=stream->m_input; |
| 369 | long pos=input->tell(); |
| 370 | if (pos+18>endPos) return false; |
| 371 | auto fType = int(libwps::read16(input)); |
| 372 | if ((fType!=1 && fType!=2) || libwps::read16(input)<9) |
| 373 | { |
| 374 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 375 | return false; |
| 376 | } |
| 377 | // seek version |
| 378 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 379 | auto fSize = long(libwps::read32(input)); // size in words |
| 380 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 381 | return (2*fSize>18 && 2*fSize<=endPos-pos); |
| 382 | } |
| 383 | |
| 384 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |