| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | |
| 4 | Original source: POLE - Portable C++ library to access OLE Storage |
| 5 | Copyright (C) 2002-2005 Ariya Hidayat <[email protected]> |
| 6 | |
| 7 | Performance optimization: Dmitry Fedorov |
| 8 | Copyright 2009 <www.bioimage.ucsb.edu> <www.dimin.net> |
| 9 | |
| 10 | Fix for more than 236 mbat block entries : Michel Boudinot |
| 11 | Copyright 2010 <[email protected]> |
| 12 | |
| 13 | Version: 0.4 |
| 14 | |
| 15 | Redistribution and use in source and binary forms, with or without |
| 16 | modification, are permitted provided that the following conditions |
| 17 | are met: |
| 18 | * Redistributions of source code must retain the above copyright notice, |
| 19 | this list of conditions and the following disclaimer. |
| 20 | * Redistributions in binary form must reproduce the above copyright notice, |
| 21 | this list of conditions and the following disclaimer in the documentation |
| 22 | and/or other materials provided with the distribution. |
| 23 | * Neither the name of the authors nor the names of its contributors may be |
| 24 | used to endorse or promote products derived from this software without |
| 25 | specific prior written permission. |
| 26 | |
| 27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 28 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 30 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 31 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 32 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 33 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 34 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 35 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 36 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 37 | THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | This file taken from libwpg RVNGOLEStream.cpp 1.5 Thu Aug 17 21:21:30 2006 |
| 42 | */ |
| 43 | |
| 44 | #include "RVNGOLEStream.h" |
| 45 | |
| 46 | #include <string.h> |
| 47 | #include <ctype.h> |
| 48 | |
| 49 | #include <cassert> |
| 50 | #include <sstream> |
| 51 | #include <iostream> |
| 52 | #include <list> |
| 53 | #include <locale> |
| 54 | #include <set> |
| 55 | #include <string> |
| 56 | #include <vector> |
| 57 | |
| 58 | #include <librevenge-stream/librevenge-stream.h> |
| 59 | |
| 60 | #include "librevenge_internal.h" |
| 61 | |
| 62 | namespace librevenge |
| 63 | { |
| 64 | enum { Avail = 0xffffffff, Eof = 0xfffffffe, Bat = 0xfffffffd, MetaBat = 0xfffffffc, NotFound=0xfffffff0 }; |
| 65 | |
| 66 | static inline unsigned short readU16(const unsigned char *ptr) |
| 67 | { |
| 68 | return (unsigned short)(ptr[0]+(ptr[1]<<8)); |
| 69 | } |
| 70 | |
| 71 | static inline unsigned readU32(const unsigned char *ptr) |
| 72 | { |
| 73 | return (unsigned)(ptr[0]+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24)); |
| 74 | } |
| 75 | |
| 76 | static inline void writeU16(unsigned char *ptr, unsigned long data) |
| 77 | { |
| 78 | ptr[0] = (unsigned char)(data & 0xff); |
| 79 | ptr[1] = (unsigned char)((data >> 8) & 0xff); |
| 80 | } |
| 81 | |
| 82 | static inline void writeU32(unsigned char *ptr, unsigned long data) |
| 83 | { |
| 84 | ptr[0] = (unsigned char)(data & 0xff); |
| 85 | ptr[1] = (unsigned char)((data >> 8) & 0xff); |
| 86 | ptr[2] = (unsigned char)((data >> 16) & 0xff); |
| 87 | ptr[3] = (unsigned char)((data >> 24) & 0xff); |
| 88 | } |
| 89 | |
| 90 | static unsigned long getLength(RVNGInputStream *const input) |
| 91 | { |
| 92 | assert(input); |
| 93 | |
| 94 | const long pos = input->tell(); |
| 95 | if (0 > pos) |
| 96 | return 0; |
| 97 | |
| 98 | long end = 0; |
| 99 | |
| 100 | if (0 == input->seek(0, librevenge::RVNG_SEEK_END)) |
| 101 | end = input->tell(); |
| 102 | if ((0 != input->seek(pos, librevenge::RVNG_SEEK_SET)) || (0 > end)) |
| 103 | return 0; |
| 104 | |
| 105 | assert(pos <= end); |
| 106 | |
| 107 | return static_cast<unsigned long>(end); |
| 108 | } |
| 109 | |
| 110 | class Header |
| 111 | { |
| 112 | public: |
| 113 | unsigned char m_magic[8]; // signature, or magic identifier |
| 114 | unsigned m_revision; // the file revision |
| 115 | unsigned m_num_bat; // blocks allocated for big bat |
| 116 | unsigned m_start_dirent; // starting block for directory info |
| 117 | unsigned m_threshold; // switch from small to big file (usually 4K) |
| 118 | unsigned m_start_sbat; // starting block index to store small bat |
| 119 | unsigned m_num_sbat; // blocks allocated for small bat |
| 120 | unsigned m_shift_sbat; // sbat->blockSize = 1 << m_shift_sbat |
| 121 | unsigned m_size_sbat; // the small block size, default 0x40 |
| 122 | unsigned m_shift_bbat; // bbat->blockSize = 1 << m_shift_bbat |
| 123 | unsigned m_size_bbat; // the big block size, default 0x200 |
| 124 | unsigned m_start_mbat; // starting block to store meta bat |
| 125 | unsigned m_num_mbat; // blocks allocated for meta bat |
| 126 | unsigned long m_blocks_bbat[109]; |
| 127 | |
| 128 | Header(); |
| 129 | void compute_block_size() |
| 130 | { |
| 131 | assert(m_shift_bbat < 31); |
| 132 | assert(m_shift_sbat < 31); |
| 133 | m_size_bbat = 1 << m_shift_bbat; |
| 134 | m_size_sbat = 1 << m_shift_sbat; |
| 135 | } |
| 136 | bool valid_signature() const |
| 137 | { |
| 138 | for (unsigned i = 0; i < 8; i++) |
| 139 | if (m_magic[i] != s_ole_magic[i]) return false; |
| 140 | return true; |
| 141 | } |
| 142 | bool valid(unsigned long fileSize) const; |
| 143 | void load(const unsigned char *buffer, unsigned long size); |
| 144 | void save(unsigned char *buffer); |
| 145 | protected: |
| 146 | static const unsigned char s_ole_magic[]; |
| 147 | }; |
| 148 | |
| 149 | const unsigned char Header::s_ole_magic[] = |
| 150 | { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 }; |
| 151 | |
| 152 | class AllocTable |
| 153 | { |
| 154 | public: |
| 155 | unsigned m_blockSize; |
| 156 | AllocTable() : m_blockSize(4096), m_data() |
| 157 | { |
| 158 | resize(128); // initial size |
| 159 | } |
| 160 | void clear(); |
| 161 | unsigned long count() const |
| 162 | { |
| 163 | return (unsigned long) m_data.size(); |
| 164 | } |
| 165 | void resize(unsigned long newsize) |
| 166 | { |
| 167 | m_data.resize(size_t(newsize), Avail); |
| 168 | } |
| 169 | void set(unsigned long index, unsigned long val) |
| 170 | { |
| 171 | if (index >= count()) |
| 172 | resize(index+1); |
| 173 | m_data[size_t(index)] = val; |
| 174 | } |
| 175 | std::vector<unsigned long> follow(unsigned long start) const; |
| 176 | unsigned long operator[](unsigned long index) const |
| 177 | { |
| 178 | return m_data[size_t(index)]; |
| 179 | } |
| 180 | void load(const unsigned char *buffer, unsigned len) |
| 181 | { |
| 182 | resize(len / 4); |
| 183 | for (unsigned long i = 0; i < count(); i++) |
| 184 | set(i, readU32(buffer + i*4)); |
| 185 | } |
| 186 | |
| 187 | // write part |
| 188 | void setChain(std::vector<unsigned long> chain, unsigned end); |
| 189 | void save(unsigned char *buffer) const |
| 190 | { |
| 191 | auto cnt=(unsigned) count(); |
| 192 | unsigned i = 0; |
| 193 | for (i = 0; i < cnt; i++) |
| 194 | writeU32(buffer + i*4, m_data[i]); |
| 195 | unsigned lastFree = 128-(cnt%128); |
| 196 | if (lastFree==128) return; |
| 197 | for (i = 0; i < lastFree; i++) |
| 198 | writeU32(buffer + (cnt+i)*4, Avail); |
| 199 | } |
| 200 | // return space required to save the allocation table |
| 201 | unsigned saveSize() const |
| 202 | { |
| 203 | auto cnt=(unsigned)(((count()+127)/128)*128); |
| 204 | return cnt * 4; |
| 205 | } |
| 206 | private: |
| 207 | std::vector<unsigned long> m_data; |
| 208 | AllocTable(const AllocTable &); |
| 209 | AllocTable &operator=(const AllocTable &); |
| 210 | }; |
| 211 | |
| 212 | class DirInfo |
| 213 | { |
| 214 | public: |
| 215 | //! constructor |
| 216 | DirInfo() |
| 217 | { |
| 218 | int i = 0; |
| 219 | for (i=0; i < 4; i++) |
| 220 | m_time[i]=0; |
| 221 | for (i=0; i < 4; i++) |
| 222 | m_clsid[i]=0; |
| 223 | } |
| 224 | //! returns true if the clsid field is filed |
| 225 | bool hasCLSId() const |
| 226 | { |
| 227 | for (unsigned int i : m_clsid) |
| 228 | if (i) return true; |
| 229 | return false; |
| 230 | } |
| 231 | /** four uint32_t : the first two used for creation, the last for modification time */ |
| 232 | unsigned m_time[4]; |
| 233 | /** four uint32_t: the clsid data */ |
| 234 | unsigned m_clsid[4]; |
| 235 | }; |
| 236 | |
| 237 | class DirEntry |
| 238 | { |
| 239 | public: |
| 240 | enum { End= 0xffffffff }; |
| 241 | //! constructor |
| 242 | DirEntry() : m_valid(false), m_macRootEntry(false), m_type(0), m_colour(0), m_size(0), m_start(0), |
| 243 | m_right(End), m_left(End), m_child(End), m_info(), m_name("") |
| 244 | { |
| 245 | } |
| 246 | //! returns true for a directory |
| 247 | bool is_dir() const |
| 248 | { |
| 249 | return m_type==1 || m_type==5; |
| 250 | } |
| 251 | //! returns the simplified file name |
| 252 | std::string name() const |
| 253 | { |
| 254 | if (m_name.length() && m_name[0]<32) |
| 255 | return m_name.substr(1); |
| 256 | return m_name; |
| 257 | } |
| 258 | /** returns the string which was store inside the file. |
| 259 | |
| 260 | \note: either name() or a index (unknown) followed by name() */ |
| 261 | std::string const &filename() const |
| 262 | { |
| 263 | return m_name; |
| 264 | } |
| 265 | /** sets the file name */ |
| 266 | void setName(std::string const &nm) |
| 267 | { |
| 268 | m_name=nm; |
| 269 | } |
| 270 | /** reads a entry content in buffer */ |
| 271 | void load(unsigned char *buffer, unsigned len); |
| 272 | //! saves a entry content in buffer */ |
| 273 | void save(unsigned char *buffer) const; |
| 274 | //! returns space required to save a dir entry |
| 275 | static unsigned saveSize() |
| 276 | { |
| 277 | return 128; |
| 278 | } |
| 279 | |
| 280 | bool m_valid; /** false if invalid (should be skipped) */ |
| 281 | bool m_macRootEntry; /** true if this is a classic mac directory entry */ |
| 282 | unsigned m_type; /** the type */ |
| 283 | unsigned m_colour; /** the red/black color: 0 means red */ |
| 284 | unsigned long m_size; /** size (not valid if directory) */ |
| 285 | unsigned long m_start; /** starting block */ |
| 286 | unsigned m_right; /** previous sibling */ |
| 287 | unsigned m_left; /** next sibling */ |
| 288 | unsigned m_child; /** first child */ |
| 289 | |
| 290 | DirInfo m_info; //! the file information |
| 291 | protected: |
| 292 | std::string m_name; /** the name, not in unicode anymore */ |
| 293 | }; |
| 294 | |
| 295 | class DirTree |
| 296 | { |
| 297 | public: |
| 298 | /** constructor */ |
| 299 | DirTree() : m_entries() |
| 300 | { |
| 301 | clear(); |
| 302 | } |
| 303 | /** clear all entries, leaving only a root entries */ |
| 304 | void clear(); |
| 305 | /** set the root to a mac/pc root */ |
| 306 | void setRootType(bool pc=true); |
| 307 | /** returns true if it is a pc file */ |
| 308 | bool hasRootTypePc() const |
| 309 | { |
| 310 | return !m_entries.size() || !m_entries[0].m_macRootEntry; |
| 311 | } |
| 312 | /** returns the number of entries */ |
| 313 | unsigned count() const |
| 314 | { |
| 315 | return unsigned(m_entries.size()); |
| 316 | } |
| 317 | /** returns the entry with a given index */ |
| 318 | DirEntry const *entry(unsigned ind) const |
| 319 | { |
| 320 | if (ind >= count()) return nullptr; |
| 321 | return &m_entries[ size_t(ind) ]; |
| 322 | } |
| 323 | /** returns the entry with a given index */ |
| 324 | DirEntry *entry(unsigned ind) |
| 325 | { |
| 326 | if (ind >= count()) return nullptr; |
| 327 | return &m_entries[ size_t(ind) ]; |
| 328 | } |
| 329 | /** returns the entry with a given name */ |
| 330 | DirEntry *entry(const std::string &name) |
| 331 | { |
| 332 | return entry(index(name)); |
| 333 | } |
| 334 | /** given a fullname (e.g "/ObjectPool/_1020961869"), find the entry */ |
| 335 | unsigned index(const std::string &name, bool create=false); |
| 336 | /** tries to find a child of ind with a given name */ |
| 337 | unsigned find_child(unsigned ind, const std::string &name) const |
| 338 | { |
| 339 | DirEntry const *p = entry(ind); |
| 340 | if (!p || !p->m_valid) return 0; |
| 341 | std::vector<unsigned> siblingsList = get_siblings(p->m_child); |
| 342 | for (unsigned s : siblingsList) |
| 343 | { |
| 344 | p = entry(s); |
| 345 | if (!p) continue; |
| 346 | if (p->name()==name) |
| 347 | return s; |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | /** returns the list of ind substream */ |
| 352 | std::vector<std::string> getSubStreamList(unsigned ind=0, bool retrieveAll=false) |
| 353 | { |
| 354 | std::vector<std::string> res; |
| 355 | std::set<unsigned> seens; |
| 356 | getSubStreamList(ind, retrieveAll, "", res, seens, true); |
| 357 | return res; |
| 358 | } |
| 359 | /** tries to read the different entries */ |
| 360 | void load(unsigned char *buffer, unsigned len); |
| 361 | |
| 362 | // write part: |
| 363 | //! check/update so that the sibling are store with a red black tree |
| 364 | void setInRedBlackTreeForm() |
| 365 | { |
| 366 | std::set<unsigned> seens; |
| 367 | setInRedBlackTreeForm(0, seens); |
| 368 | } |
| 369 | //! return space required to save a dir entry |
| 370 | unsigned saveSize() const |
| 371 | { |
| 372 | unsigned cnt = count(); |
| 373 | cnt = 4*((cnt+3)/4); |
| 374 | return cnt*DirEntry::saveSize(); |
| 375 | } |
| 376 | //! save the list of direntry in buffer |
| 377 | void save(unsigned char *buffer) const |
| 378 | { |
| 379 | unsigned entrySize=DirEntry::saveSize(); |
| 380 | size_t cnt = count(); |
| 381 | for (size_t i = 0; i < cnt; i++) |
| 382 | m_entries[i].save(buffer+entrySize*i); |
| 383 | if ((cnt%4)==0) return; |
| 384 | DirEntry empty; |
| 385 | while (cnt%4) |
| 386 | empty.save(buffer+entrySize*cnt++); |
| 387 | } |
| 388 | protected: |
| 389 | //! returns a list of siblings corresponding to a node |
| 390 | std::vector<unsigned> get_siblings(unsigned ind) const |
| 391 | { |
| 392 | std::set<unsigned> seens; |
| 393 | get_siblings(ind, seens); |
| 394 | std::vector<unsigned> retVal; |
| 395 | for (unsigned seen : seens) |
| 396 | retVal.push_back(seen); |
| 397 | return retVal; |
| 398 | } |
| 399 | //! constructs the list of siblings ( by filling the seens set ) |
| 400 | void get_siblings(unsigned ind, std::set<unsigned> &seens) const |
| 401 | { |
| 402 | if (seens.find(ind) != seens.end()) |
| 403 | return; |
| 404 | seens.insert(ind); |
| 405 | DirEntry const *e = entry(ind); |
| 406 | if (!e) return; |
| 407 | unsigned cnt = count(); |
| 408 | if (e->m_left>0&& e->m_left < cnt) |
| 409 | get_siblings(e->m_left, seens); |
| 410 | if (e->m_right>0 && e->m_right < cnt) |
| 411 | get_siblings(e->m_right, seens); |
| 412 | } |
| 413 | /** returns a substream list */ |
| 414 | void getSubStreamList(unsigned ind, bool all, const std::string &prefix, |
| 415 | std::vector<std::string> &res, std::set<unsigned> &seen, |
| 416 | bool isRoot=false) const; |
| 417 | |
| 418 | /** check that the subtrees of index is a red black tree, if not rebuild it */ |
| 419 | void setInRedBlackTreeForm(unsigned id, std::set<unsigned> &seen); |
| 420 | /** rebuild all the childs m_left, m_right index as a red black |
| 421 | tree, returns the root index. |
| 422 | |
| 423 | \note: this function supposes that the childs list is already sorted |
| 424 | */ |
| 425 | unsigned setInRBTForm(std::vector<unsigned> const &childList, |
| 426 | unsigned firstInd, unsigned lastInd, |
| 427 | unsigned height); |
| 428 | |
| 429 | /** a comparaison funcion of DirTree used to sort the entry by name */ |
| 430 | struct CompareEntryName |
| 431 | { |
| 432 | //! constructor |
| 433 | CompareEntryName(DirTree &tree) : m_tree(tree) |
| 434 | { |
| 435 | } |
| 436 | //! comparaison function |
| 437 | bool operator()(unsigned ind1, unsigned ind2) const |
| 438 | { |
| 439 | DirEntry const *e1=m_tree.entry(ind1); |
| 440 | DirEntry const *e2=m_tree.entry(ind2); |
| 441 | if (!e1 && !e2) return false; |
| 442 | if (!e1) return true; |
| 443 | if (!e2) return false; |
| 444 | std::string name1(e1->name()), name2(e2->name()); |
| 445 | size_t len1=name1.length(); |
| 446 | size_t len2=name2.length(); |
| 447 | if (len1 != len2) return len1 < len2; |
| 448 | for (size_t c=0; c < len1; c++) |
| 449 | { |
| 450 | if (tolower(name1[c]) != tolower(name2[c])) |
| 451 | return tolower(name1[c]) < tolower(name2[c]); |
| 452 | } |
| 453 | return ind1 < ind2; |
| 454 | } |
| 455 | |
| 456 | //! the main tree |
| 457 | DirTree &m_tree; |
| 458 | }; |
| 459 | |
| 460 | private: |
| 461 | std::vector<DirEntry> m_entries; |
| 462 | DirTree(const DirTree &); |
| 463 | DirTree &operator=(const DirTree &); |
| 464 | }; |
| 465 | |
| 466 | class IStorage |
| 467 | { |
| 468 | public: |
| 469 | RVNGInputStream *m_input; |
| 470 | librevenge::Storage::Result m_result; // result of operation |
| 471 | |
| 472 | Header m_header; // storage header |
| 473 | DirTree m_dirtree; // directory tree |
| 474 | AllocTable m_bbat; // allocation table for big blocks |
| 475 | AllocTable m_sbat; // allocation table for small blocks |
| 476 | |
| 477 | std::vector<unsigned long> m_sb_blocks; // blocks for "small" files |
| 478 | |
| 479 | IStorage(RVNGInputStream *is); |
| 480 | ~IStorage() {} |
| 481 | |
| 482 | //! returns a directory entry corresponding to a index |
| 483 | DirEntry *entry(unsigned ind) |
| 484 | { |
| 485 | load(); |
| 486 | return m_dirtree.entry(ind); |
| 487 | } |
| 488 | //! returns a directory entry corresponding to a name |
| 489 | DirEntry *entry(const std::string &name) |
| 490 | { |
| 491 | if (!name.length()) return nullptr; |
| 492 | load(); |
| 493 | return m_dirtree.entry(name); |
| 494 | } |
| 495 | //! returns a directory entry corresponding to a index |
| 496 | unsigned index(const std::string &name) |
| 497 | { |
| 498 | if (!name.length()) return NotFound; |
| 499 | load(); |
| 500 | return m_dirtree.index(name); |
| 501 | } |
| 502 | /** returns the OLE revision */ |
| 503 | unsigned revision() const |
| 504 | { |
| 505 | return m_header.m_revision; |
| 506 | } |
| 507 | /** returns true if it is a pc file */ |
| 508 | bool hasRootTypePc() const |
| 509 | { |
| 510 | return m_dirtree.hasRootTypePc(); |
| 511 | } |
| 512 | //! returns true if the entry exists in the OLE, if so fills isDir |
| 513 | bool isSubStream(const std::string &name, bool &isDir); |
| 514 | //! returns the list of subStream given a dirEntry index |
| 515 | std::vector<std::string> getSubStreamList(unsigned ind=0, bool retrieveAll=false) |
| 516 | { |
| 517 | load(); |
| 518 | return m_dirtree.getSubStreamList(ind, retrieveAll); |
| 519 | } |
| 520 | |
| 521 | bool isStructured(); |
| 522 | std::vector<std::string> getSubStreamNamesList(); |
| 523 | void load(); |
| 524 | |
| 525 | bool use_big_block_for(unsigned long size) const |
| 526 | { |
| 527 | return size >= m_header.m_threshold; |
| 528 | } |
| 529 | unsigned long loadBigBlocks(std::vector<unsigned long> const &blocks, unsigned char *buffer, unsigned long maxlen) const; |
| 530 | |
| 531 | unsigned long loadBigBlock(unsigned long block, unsigned char *buffer, unsigned long maxlen) const; |
| 532 | |
| 533 | unsigned long loadSmallBlocks(std::vector<unsigned long> const &blocks, unsigned char *buffer, unsigned long maxlen); |
| 534 | |
| 535 | unsigned long loadSmallBlock(unsigned long block, unsigned char *buffer, unsigned long maxlen); |
| 536 | |
| 537 | protected: |
| 538 | bool m_isLoad; |
| 539 | private: |
| 540 | // no copy or assign |
| 541 | IStorage(const IStorage &); |
| 542 | IStorage &operator=(const IStorage &); |
| 543 | |
| 544 | }; |
| 545 | |
| 546 | class OStorage |
| 547 | { |
| 548 | public: |
| 549 | //! constructor |
| 550 | OStorage(unsigned long minSize=0) : m_header(), m_dirtree(), m_bbat(), m_num_bbat(0), m_sbat(), m_num_sbat(0), m_sb_blocks(), m_data() |
| 551 | { |
| 552 | // update data to contain at least, header |
| 553 | m_data.resize(minSize>512 ? minSize : 512); |
| 554 | } |
| 555 | //! destructor |
| 556 | ~OStorage() { } |
| 557 | |
| 558 | //! function to retrieve the list of actual direntry |
| 559 | std::vector<std::string> getSubStreamList(unsigned ind=0, bool all=false) |
| 560 | { |
| 561 | return m_dirtree.getSubStreamList(ind, all); |
| 562 | } |
| 563 | /** set the OLE revision */ |
| 564 | void setRevision(unsigned rev) |
| 565 | { |
| 566 | m_header.m_revision=rev; |
| 567 | } |
| 568 | //! set the root to a mac/pc root */ |
| 569 | void setRootType(bool pc) |
| 570 | { |
| 571 | m_dirtree.setRootType(pc); |
| 572 | } |
| 573 | //! add a new stream knowing its data |
| 574 | bool addStream(std::string const &name, unsigned char const *buffer, unsigned long len); |
| 575 | //! add a new directory (usefull to create empty leaf dir ) |
| 576 | bool addDirectory(std::string const &dir); |
| 577 | //! set a node information |
| 578 | void setInformation(std::string const &name, DirInfo const &info) |
| 579 | { |
| 580 | DirEntry *e = m_dirtree.entry(name); |
| 581 | if (!e) |
| 582 | { |
| 583 | RVNG_DEBUG_MSG(("librevengeOLE::OStorage::setInformation: can not find entry %s!!!\n", name.c_str())); |
| 584 | return; |
| 585 | } |
| 586 | e->m_info = info; |
| 587 | } |
| 588 | //! try to return in data the contain of the file |
| 589 | bool getFileData(std::vector<unsigned char> &data) |
| 590 | { |
| 591 | try |
| 592 | { |
| 593 | if (!updateToSave()) |
| 594 | return false; |
| 595 | data = m_data; |
| 596 | } |
| 597 | catch (...) |
| 598 | { |
| 599 | return false; |
| 600 | } |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | protected: |
| 605 | //! add stream data in a file, returns the first index |
| 606 | unsigned insertData(unsigned char const *buffer, unsigned long len, bool useBigBlock, unsigned end=Eof); |
| 607 | |
| 608 | //! return true if we need to use big block |
| 609 | bool useBigBlockFor(unsigned long size) const |
| 610 | { |
| 611 | return size >= m_header.m_threshold; |
| 612 | } |
| 613 | //! returns the maximum size of a big/small block |
| 614 | static unsigned long getMaximumSize(bool isBig) |
| 615 | { |
| 616 | return isBig ? 512 : 64; |
| 617 | } |
| 618 | //! returns the address of a big/small block |
| 619 | size_t getDataAddress(unsigned block, bool isBig) const |
| 620 | { |
| 621 | if (isBig) return size_t((block+1)*512); |
| 622 | auto bId=size_t(block/8); |
| 623 | if (bId >= m_sb_blocks.size()) throw GenericException(); |
| 624 | return size_t((m_sb_blocks[bId]+1)*512+64*(block%8)); |
| 625 | } |
| 626 | |
| 627 | //! create a new big block, resize m_data; ... and return is identifier |
| 628 | unsigned newBBlock() |
| 629 | { |
| 630 | unsigned res = m_num_bbat++; |
| 631 | if (m_data.size() < (res+2)*512) m_data.resize((res+2)*512, 0); |
| 632 | m_bbat.resize(res+1); |
| 633 | return res; |
| 634 | } |
| 635 | //! create a new small block, ... and returns is identifier |
| 636 | unsigned newSBlock() |
| 637 | { |
| 638 | unsigned res = m_num_sbat++; |
| 639 | if ((res%8)==0) |
| 640 | m_sb_blocks.push_back(newBBlock()); |
| 641 | m_sbat.resize(res+1); |
| 642 | return res; |
| 643 | } |
| 644 | |
| 645 | //! return a new dir entry, if it does not exists |
| 646 | DirEntry *createEntry(std::string const &name) |
| 647 | { |
| 648 | if (name.length()==0) |
| 649 | { |
| 650 | RVNG_DEBUG_MSG(("librevengeOLE::OStorage::createEntry: called with no name\n")); |
| 651 | return nullptr; |
| 652 | } |
| 653 | if (m_dirtree.index(name)!=NotFound) |
| 654 | { |
| 655 | RVNG_DEBUG_MSG(("librevengeOLE::OStorage::createEntry: entry %s already exists\n", name.c_str())); |
| 656 | return nullptr; |
| 657 | } |
| 658 | unsigned index = m_dirtree.index(name,true); |
| 659 | if (index == NotFound) return nullptr; |
| 660 | |
| 661 | return m_dirtree.entry(index); |
| 662 | } |
| 663 | |
| 664 | //! finish to update the file ( note: it is better to call this function only one time ) |
| 665 | bool updateToSave(); |
| 666 | |
| 667 | Header m_header; // storage header |
| 668 | DirTree m_dirtree; // directory tree |
| 669 | AllocTable m_bbat; // allocation table for big blocks |
| 670 | unsigned m_num_bbat; // the number of bbat |
| 671 | AllocTable m_sbat; // allocation table for small blocks |
| 672 | unsigned m_num_sbat; // the number of sbat |
| 673 | |
| 674 | std::vector<unsigned long> m_sb_blocks; // blocks for "small" files |
| 675 | |
| 676 | std::vector<unsigned char> m_data; |
| 677 | private: |
| 678 | // no copy or assign |
| 679 | OStorage(const OStorage &); |
| 680 | OStorage &operator=(const OStorage &); |
| 681 | }; |
| 682 | |
| 683 | class IStream |
| 684 | { |
| 685 | public: |
| 686 | IStorage *m_iStorage; |
| 687 | //! the stream size |
| 688 | unsigned long m_size; |
| 689 | std::string m_name; |
| 690 | |
| 691 | IStream(IStorage *io, std::string const &name); |
| 692 | ~IStream() |
| 693 | { |
| 694 | } |
| 695 | unsigned long size() const |
| 696 | { |
| 697 | return m_size; |
| 698 | } |
| 699 | unsigned long tell() const |
| 700 | { |
| 701 | return m_pos; |
| 702 | } |
| 703 | unsigned long read(unsigned char *data, unsigned long maxlen) |
| 704 | { |
| 705 | if (!m_size) |
| 706 | return 0; |
| 707 | unsigned long bytes; |
| 708 | if (m_data.size()) |
| 709 | bytes = readData(tell(), data, maxlen); |
| 710 | else |
| 711 | bytes = readUsingStorage(tell(), data, maxlen); |
| 712 | m_pos += bytes; |
| 713 | return bytes; |
| 714 | } |
| 715 | |
| 716 | protected: |
| 717 | //! create the data corresponding to a directory |
| 718 | bool createOleFromDirectory(IStorage *io, std::string const &name); |
| 719 | //! try to read maxlen data using m_iStorage |
| 720 | unsigned long readUsingStorage(unsigned long pos, unsigned char *data, unsigned long maxlen); |
| 721 | //! try to read maxlen data using m_data |
| 722 | unsigned long readData(unsigned long pos, unsigned char *data, unsigned long maxlen); |
| 723 | |
| 724 | private: |
| 725 | std::vector<unsigned long> m_blocks; |
| 726 | |
| 727 | // no copy or assign |
| 728 | IStream(const IStream &); |
| 729 | IStream &operator=(const IStream &); |
| 730 | |
| 731 | // pointer for read |
| 732 | unsigned long m_pos; |
| 733 | |
| 734 | // a copy of the data when the entry came for OStorage |
| 735 | std::vector<unsigned char> m_data; |
| 736 | }; |
| 737 | |
| 738 | } // namespace librevenge |
| 739 | // =========== Header ========== |
| 740 | |
| 741 | librevenge::Header::Header() : |
| 742 | m_revision(0x3e), m_num_bat(0), m_start_dirent(0), m_threshold(4096), |
| 743 | m_start_sbat(Eof), m_num_sbat(0), m_shift_sbat(6), m_size_sbat(0), |
| 744 | m_shift_bbat(9), m_size_bbat(0), |
| 745 | m_start_mbat(Eof), m_num_mbat(0) |
| 746 | { |
| 747 | for (unsigned i = 0; i < 8; i++) |
| 748 | m_magic[i] = s_ole_magic[i]; |
| 749 | for (unsigned long &j : m_blocks_bbat) |
| 750 | j = Avail; |
| 751 | compute_block_size(); |
| 752 | } |
| 753 | |
| 754 | bool librevenge::Header::valid(const unsigned long fileSize) const |
| 755 | { |
| 756 | if (m_threshold != 4096) return false; |
| 757 | // there must be at least the header, one bat sector and one dirent sector in the file |
| 758 | if ((fileSize / m_size_bbat) < 3) return false; |
| 759 | // sectors must fit into the file |
| 760 | if ((fileSize / m_size_bbat) < m_num_mbat) return false; |
| 761 | if (m_num_bat == 0) return false; |
| 762 | if ((m_num_bat > 109) && (m_num_bat > (m_num_mbat * (m_size_bbat/4-1)) + 109)) return false; |
| 763 | if ((m_num_bat < 109) && (m_num_mbat != 0)) return false; |
| 764 | if (m_shift_sbat > m_shift_bbat) return false; |
| 765 | if (m_shift_bbat <= 6) return false; |
| 766 | if (m_shift_bbat >=31) return false; |
| 767 | return true; |
| 768 | } |
| 769 | |
| 770 | void librevenge::Header::load(const unsigned char *buffer, unsigned long size) |
| 771 | { |
| 772 | if (size < 512) |
| 773 | return; |
| 774 | m_revision = (unsigned) readU16(buffer+0x18); |
| 775 | m_shift_bbat = (unsigned int) readU16(buffer + 0x1e); |
| 776 | if (m_shift_bbat > 31) |
| 777 | m_shift_bbat = 31; |
| 778 | m_shift_sbat = (unsigned int) readU16(buffer + 0x20); |
| 779 | if (m_shift_sbat > 31) |
| 780 | m_shift_sbat = 31; |
| 781 | m_num_bat = (unsigned int) readU32(buffer + 0x2c); |
| 782 | m_start_dirent = (unsigned int) readU32(buffer + 0x30); |
| 783 | m_threshold = (unsigned int) readU32(buffer + 0x38); |
| 784 | m_start_sbat = (unsigned int) readU32(buffer + 0x3c); |
| 785 | m_num_sbat = (unsigned int) readU32(buffer + 0x40); |
| 786 | m_start_mbat = (unsigned int) readU32(buffer + 0x44); |
| 787 | m_num_mbat = (unsigned int) readU32(buffer + 0x48); |
| 788 | |
| 789 | for (unsigned i = 0; i < 8; i++) |
| 790 | m_magic[i] = buffer[i]; |
| 791 | for (unsigned j=0; j<109; j++) |
| 792 | m_blocks_bbat[j] = readU32(buffer + 0x4C+j*4); |
| 793 | } |
| 794 | |
| 795 | void librevenge::Header::save(unsigned char *buffer) |
| 796 | { |
| 797 | memcpy(buffer, s_ole_magic, 8); // ole signature |
| 798 | memset(buffer + 8, 0, 0x4c - 8); |
| 799 | writeU32(buffer + 8, 0); // unknown |
| 800 | writeU32(buffer + 12, 0); // unknown |
| 801 | writeU32(buffer + 16, 0); // unknown |
| 802 | writeU16(buffer + 24, m_revision); |
| 803 | writeU16(buffer + 26, 3); // version ? |
| 804 | writeU16(buffer + 28, 0xfffe); // unknown |
| 805 | writeU16(buffer + 0x1e, m_shift_bbat); |
| 806 | writeU16(buffer + 0x20, m_shift_sbat); |
| 807 | writeU32(buffer + 0x2c, m_num_bat); |
| 808 | writeU32(buffer + 0x30, m_start_dirent); |
| 809 | writeU32(buffer + 0x38, m_threshold); |
| 810 | writeU32(buffer + 0x3c, m_start_sbat); |
| 811 | writeU32(buffer + 0x40, m_num_sbat); |
| 812 | writeU32(buffer + 0x44, m_start_mbat); |
| 813 | writeU32(buffer + 0x48, m_num_mbat); |
| 814 | |
| 815 | for (unsigned i=0; i<109; i++) |
| 816 | writeU32(buffer + 0x4C+i*4, m_blocks_bbat[i]); |
| 817 | } |
| 818 | |
| 819 | |
| 820 | // =========== AllocTable ========== |
| 821 | |
| 822 | std::vector<unsigned long> librevenge::AllocTable::follow(unsigned long start) const |
| 823 | { |
| 824 | std::vector<unsigned long> chain; |
| 825 | if (start >= count()) return chain; |
| 826 | |
| 827 | std::set<unsigned long> seens; |
| 828 | unsigned long p = start; |
| 829 | while (p < count()) |
| 830 | { |
| 831 | if (p == Eof || p == Bat || p == MetaBat) break; |
| 832 | if (seens.find(p) != seens.end()) break; |
| 833 | seens.insert(p); |
| 834 | chain.push_back(p); |
| 835 | p = m_data[ p ]; |
| 836 | } |
| 837 | |
| 838 | return chain; |
| 839 | } |
| 840 | |
| 841 | void librevenge::AllocTable::setChain(std::vector<unsigned long> chain, unsigned end) |
| 842 | { |
| 843 | if (!chain.size()) return; |
| 844 | |
| 845 | for (size_t i=0; i<chain.size()-1; i++) |
| 846 | set(chain[i], chain[i+1]); |
| 847 | set(chain[ chain.size()-1 ], end); |
| 848 | } |
| 849 | |
| 850 | // =========== DirEntry ========== |
| 851 | void librevenge::DirEntry::load(unsigned char *buffer, unsigned len) |
| 852 | { |
| 853 | if (len != 128) |
| 854 | { |
| 855 | RVNG_DEBUG_MSG(("DirEntry::load: unexpected len for DirEntry::load\n")); |
| 856 | *this=DirEntry(); |
| 857 | return; |
| 858 | } |
| 859 | // 2 = file (aka stream), 1 = directory (aka storage), 5 = root |
| 860 | m_type = buffer[ 0x42]; |
| 861 | m_colour = buffer[0x43]; |
| 862 | |
| 863 | // parse name of this entry, which stored as Unicode 16-bit |
| 864 | m_name=std::string(""); |
| 865 | auto name_len = (unsigned) readU16(buffer + 0x40); |
| 866 | if (name_len > 64) name_len = 64; |
| 867 | if (name_len==2 && m_type==5 && readU16(buffer)==0x5200) |
| 868 | { |
| 869 | // find in some mswork mac 4.0 file |
| 870 | m_name="R"; |
| 871 | m_macRootEntry=true; |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | for (unsigned j=0; (j < name_len) && buffer[j]; j+= 2) |
| 876 | m_name.append(1, char(buffer[j])); |
| 877 | } |
| 878 | |
| 879 | int i = 0; |
| 880 | for (i = 0; i < 4; i++) |
| 881 | m_info.m_clsid[i]=(unsigned) readU32(buffer + 0x50 + 4*i); |
| 882 | for (i = 0; i < 4; i++) |
| 883 | m_info.m_time[i]=(unsigned) readU32(buffer + 0x64 + 4*i); |
| 884 | |
| 885 | m_valid = true; |
| 886 | m_start = (unsigned int) readU32(buffer + 0x74); |
| 887 | m_size = (unsigned int) readU32(buffer + 0x78); |
| 888 | m_left = (unsigned int) readU32(buffer + 0x44); |
| 889 | m_right = (unsigned int) readU32(buffer + 0x48); |
| 890 | m_child = (unsigned int) readU32(buffer + 0x4C); |
| 891 | |
| 892 | // sanity checks |
| 893 | if ((m_type != 2) && (m_type != 1) && (m_type != 5)) m_valid = false; |
| 894 | if (name_len < 1) m_valid = false; |
| 895 | } |
| 896 | |
| 897 | void librevenge::DirEntry::save(unsigned char *buffer) const |
| 898 | { |
| 899 | int i = 0; |
| 900 | for (i = 0; i < 128; i++) buffer[i]=0; |
| 901 | |
| 902 | auto name_len = (unsigned) m_name.length(); |
| 903 | if (name_len>31) name_len = 31; |
| 904 | if (name_len==2 && m_macRootEntry && m_type==5) |
| 905 | buffer[1]='R'; |
| 906 | else |
| 907 | { |
| 908 | for (size_t j = 0; j < name_len; j++) |
| 909 | writeU16(buffer+2*j, (unsigned) m_name[j]); |
| 910 | } |
| 911 | writeU16(buffer+0x40, 2*name_len+2); |
| 912 | |
| 913 | buffer[0x42]=(unsigned char) m_type; |
| 914 | buffer[0x43]=(unsigned char) m_colour; |
| 915 | for (i = 0; i < 4; i++) |
| 916 | writeU32(buffer + 0x50+4*i, m_info.m_clsid[i]); |
| 917 | for (i = 0; i < 4; i++) |
| 918 | writeU32(buffer + 0x64+4*i, m_info.m_time[i]); |
| 919 | writeU32(buffer + 0x74, m_start); |
| 920 | writeU32(buffer + 0x78, m_size); |
| 921 | writeU32(buffer + 0x44, m_left); |
| 922 | writeU32(buffer + 0x48, m_right); |
| 923 | writeU32(buffer + 0x4C, m_child); |
| 924 | } |
| 925 | // =========== DirTree ========== |
| 926 | |
| 927 | void librevenge::DirTree::clear() |
| 928 | { |
| 929 | m_entries.resize(0); |
| 930 | setRootType(true); |
| 931 | } |
| 932 | |
| 933 | void librevenge::DirTree::setRootType(bool pc) |
| 934 | { |
| 935 | if (!m_entries.size()) |
| 936 | { |
| 937 | m_entries.resize(1); |
| 938 | m_entries[0]=DirEntry(); |
| 939 | m_entries[0].m_valid = true; |
| 940 | m_entries[0].setName("Root Entry"); |
| 941 | m_entries[0].m_type = 5; |
| 942 | } |
| 943 | if (pc) |
| 944 | m_entries[0].setName("Root Entry"); |
| 945 | else |
| 946 | { |
| 947 | m_entries[0].setName("R"); |
| 948 | m_entries[0].m_macRootEntry = true; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | unsigned librevenge::DirTree::index(const std::string &name, bool create) |
| 953 | { |
| 954 | |
| 955 | if (name.length()==0) return NotFound; |
| 956 | |
| 957 | // quick check for "/" (that's root) |
| 958 | if (name == "/") return 0; |
| 959 | |
| 960 | // split the names, e.g "/ObjectPool/_1020961869" will become: |
| 961 | // "ObjectPool" and "_1020961869" |
| 962 | std::list<std::string> names; |
| 963 | std::string::size_type start = 0, end = 0; |
| 964 | if (name[0] == '/') start++; |
| 965 | while (start < name.length()) |
| 966 | { |
| 967 | end = name.find_first_of('/', start); |
| 968 | if (end == std::string::npos) end = name.length(); |
| 969 | names.push_back(name.substr(start, end-start)); |
| 970 | start = end+1; |
| 971 | } |
| 972 | |
| 973 | // start from root |
| 974 | unsigned ind = 0 ; |
| 975 | |
| 976 | // trace one by one |
| 977 | std::list<std::string>::const_iterator it; |
| 978 | size_t depth = 0; |
| 979 | |
| 980 | for (it = names.begin(); it != names.end(); ++it, ++depth) |
| 981 | { |
| 982 | std::string childName(*it); |
| 983 | if (childName.length() && childName[0]<32) |
| 984 | childName= it->substr(1); |
| 985 | |
| 986 | unsigned child = find_child(ind, childName); |
| 987 | // traverse to the child |
| 988 | if (child > 0) |
| 989 | { |
| 990 | ind = child; |
| 991 | continue; |
| 992 | } |
| 993 | else if (!create) return NotFound; |
| 994 | |
| 995 | // create a new entry |
| 996 | unsigned parent = ind; |
| 997 | m_entries.push_back(DirEntry()); |
| 998 | ind = count()-1; |
| 999 | DirEntry *e = entry(ind); |
| 1000 | e->m_valid = true; |
| 1001 | e->setName(*it); |
| 1002 | e->m_type = depth+1==names.size() ? 2 : 1; |
| 1003 | // e->m_start = Eof; CHECKME |
| 1004 | e->m_left = entry(parent)->m_child; |
| 1005 | entry(parent)->m_child = ind; |
| 1006 | } |
| 1007 | |
| 1008 | return ind; |
| 1009 | } |
| 1010 | |
| 1011 | void librevenge::DirTree::load(unsigned char *buffer, unsigned size) |
| 1012 | { |
| 1013 | m_entries.clear(); |
| 1014 | |
| 1015 | for (unsigned i = 0; i < size/128; i++) |
| 1016 | { |
| 1017 | DirEntry e; |
| 1018 | e.load(buffer+i*128, 128); |
| 1019 | m_entries.push_back(e); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | void librevenge::DirTree::getSubStreamList(unsigned ind, bool all, const std::string &prefix, |
| 1024 | std::vector<std::string> &res, |
| 1025 | std::set<unsigned> &seen, |
| 1026 | bool isRoot) const |
| 1027 | { |
| 1028 | if (seen.find(ind) != seen.end()) |
| 1029 | return; |
| 1030 | seen.insert(ind); |
| 1031 | unsigned cnt = count(); |
| 1032 | DirEntry const *p = entry(ind); |
| 1033 | if (!p || !p->m_valid) |
| 1034 | return; |
| 1035 | std::string name(prefix); |
| 1036 | if (ind && !isRoot) |
| 1037 | { |
| 1038 | if (p->filename().length()) |
| 1039 | name+= p->filename(); |
| 1040 | else |
| 1041 | return; |
| 1042 | } |
| 1043 | if (!p->is_dir()) |
| 1044 | { |
| 1045 | res.push_back(name); |
| 1046 | return; |
| 1047 | } |
| 1048 | if (ind) |
| 1049 | name += "/"; |
| 1050 | if (all) |
| 1051 | { |
| 1052 | if (ind) |
| 1053 | res.push_back(name); |
| 1054 | else |
| 1055 | res.push_back("/"); |
| 1056 | } |
| 1057 | if (p->m_child >= cnt) |
| 1058 | return; |
| 1059 | std::vector<unsigned> siblings=get_siblings(p->m_child); |
| 1060 | for (unsigned sibling : siblings) |
| 1061 | getSubStreamList(sibling, all, name, res, seen); |
| 1062 | } |
| 1063 | |
| 1064 | void librevenge::DirTree::setInRedBlackTreeForm(unsigned ind, std::set<unsigned> &seen) |
| 1065 | { |
| 1066 | if (seen.find(ind) != seen.end()) |
| 1067 | return; |
| 1068 | seen.insert(ind); |
| 1069 | |
| 1070 | DirEntry *p = entry(ind); |
| 1071 | if (!p || !p->m_valid) |
| 1072 | return; |
| 1073 | |
| 1074 | p->m_colour=1; // set all nodes blacks |
| 1075 | std::vector<unsigned> childs=get_siblings(p->m_child); |
| 1076 | size_t numChild=childs.size(); |
| 1077 | for (size_t s=0; s < numChild; s++) |
| 1078 | setInRedBlackTreeForm(childs[s], seen); |
| 1079 | if (numChild <= 1) |
| 1080 | return; |
| 1081 | CompareEntryName compare(*this); |
| 1082 | std::set<unsigned,CompareEntryName> set(childs.begin(),childs.end(),compare); |
| 1083 | std::vector<unsigned> sortedChildren; |
| 1084 | for (unsigned iter : set) |
| 1085 | sortedChildren.push_back(iter); |
| 1086 | if (sortedChildren.size() != numChild) |
| 1087 | { |
| 1088 | RVNG_DEBUG_MSG(("DirTree::setInRedBlackTreeForm: OOPS pb with numChild\n")); |
| 1089 | return; |
| 1090 | } |
| 1091 | unsigned h=1; |
| 1092 | unsigned hNumChild=1; |
| 1093 | while (2*hNumChild+1<=numChild) |
| 1094 | { |
| 1095 | hNumChild=2*hNumChild+1; |
| 1096 | h++; |
| 1097 | } |
| 1098 | p->m_child=setInRBTForm(sortedChildren, 0, unsigned(numChild-1), h); |
| 1099 | } |
| 1100 | |
| 1101 | unsigned librevenge::DirTree::setInRBTForm(std::vector<unsigned> const &childs, |
| 1102 | unsigned firstInd, unsigned lastInd, |
| 1103 | unsigned height) |
| 1104 | { |
| 1105 | unsigned middle = (firstInd+lastInd)/2; |
| 1106 | unsigned ind=childs[middle]; |
| 1107 | DirEntry *p = entry(ind); |
| 1108 | if (!p) |
| 1109 | { |
| 1110 | RVNG_DEBUG_MSG(("DirTree::setInRedBlackTreeForm: OOPS can not find tree to modified\n")); |
| 1111 | throw GenericException(); |
| 1112 | } |
| 1113 | unsigned newH = height==0 ? 0 : height-1; |
| 1114 | if (height==0) |
| 1115 | { |
| 1116 | p->m_colour = 0; |
| 1117 | if (firstInd!=middle || lastInd!=middle) |
| 1118 | { |
| 1119 | RVNG_DEBUG_MSG(("DirTree::setInRedBlackTreeForm: OOPS problem setting RedBlack colour\n")); |
| 1120 | } |
| 1121 | } |
| 1122 | if (firstInd!=middle) |
| 1123 | p->m_left=setInRBTForm(childs, firstInd,unsigned(middle-1),newH); |
| 1124 | else |
| 1125 | p->m_left=DirEntry::End; |
| 1126 | if (lastInd!=middle) |
| 1127 | p->m_right=setInRBTForm(childs, unsigned(middle+1),lastInd,newH); |
| 1128 | else |
| 1129 | p->m_right=DirEntry::End; |
| 1130 | return ind; |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | // =========== IStorage ========== |
| 1135 | |
| 1136 | librevenge::IStorage::IStorage(RVNGInputStream *is) : |
| 1137 | m_input(is), |
| 1138 | m_result(librevenge::Storage::Ok), |
| 1139 | m_header(), m_dirtree(), |
| 1140 | m_bbat(), m_sbat(), m_sb_blocks(), |
| 1141 | m_isLoad(false) |
| 1142 | { |
| 1143 | m_bbat.m_blockSize = m_header.m_size_bbat; |
| 1144 | m_sbat.m_blockSize = m_header.m_size_sbat; |
| 1145 | } |
| 1146 | |
| 1147 | bool librevenge::IStorage::isStructured() |
| 1148 | { |
| 1149 | load(); |
| 1150 | return (m_result == librevenge::Storage::Ok); |
| 1151 | } |
| 1152 | |
| 1153 | std::vector<std::string> librevenge::IStorage::getSubStreamNamesList() |
| 1154 | { |
| 1155 | load(); |
| 1156 | if (m_result != librevenge::Storage::Ok) |
| 1157 | return std::vector<std::string>(); |
| 1158 | return m_dirtree.getSubStreamList(0, true); |
| 1159 | } |
| 1160 | |
| 1161 | bool librevenge::IStorage::isSubStream(const std::string &name, bool &isDir) |
| 1162 | { |
| 1163 | if (!name.length()) return false; |
| 1164 | load(); |
| 1165 | // search in the entries |
| 1166 | DirEntry *e = m_dirtree.entry(name); |
| 1167 | if (!e) return false; |
| 1168 | isDir = e->is_dir(); |
| 1169 | return true; |
| 1170 | } |
| 1171 | |
| 1172 | void librevenge::IStorage::load() |
| 1173 | { |
| 1174 | if (m_isLoad) |
| 1175 | return; |
| 1176 | m_isLoad = true; |
| 1177 | m_result = librevenge::Storage::NotOLE; |
| 1178 | if (!m_input) |
| 1179 | return; |
| 1180 | |
| 1181 | std::vector<unsigned long> blocks; |
| 1182 | |
| 1183 | // load header |
| 1184 | unsigned long numBytesRead = 0; |
| 1185 | m_input->seek(0, RVNG_SEEK_SET); |
| 1186 | const unsigned char *buf = m_input->read(512, numBytesRead); |
| 1187 | |
| 1188 | if (numBytesRead < 512) |
| 1189 | return; |
| 1190 | |
| 1191 | m_header.load(buf, numBytesRead); |
| 1192 | |
| 1193 | // check OLE magic id |
| 1194 | if (!m_header.valid_signature()) |
| 1195 | return; |
| 1196 | |
| 1197 | // important block size |
| 1198 | m_header.compute_block_size(); |
| 1199 | m_bbat.m_blockSize = m_header.m_size_bbat; |
| 1200 | m_sbat.m_blockSize = m_header.m_size_sbat; |
| 1201 | |
| 1202 | // sanity checks |
| 1203 | m_result = librevenge::Storage::BadOLE; |
| 1204 | if (!m_header.valid(getLength(m_input))) return; |
| 1205 | if (m_header.m_threshold != 4096) return; |
| 1206 | |
| 1207 | // find blocks allocated to store big bat |
| 1208 | // the first 109 blocks are in header, the rest in meta bat |
| 1209 | blocks.clear(); |
| 1210 | blocks.resize(m_header.m_num_bat); |
| 1211 | for (unsigned j = 0; j < 109; j++) |
| 1212 | if (j >= m_header.m_num_bat) break; |
| 1213 | else blocks[j] = m_header.m_blocks_bbat[j]; |
| 1214 | if ((m_header.m_num_bat > 109) && (m_header.m_num_mbat > 0)) |
| 1215 | { |
| 1216 | std::vector<unsigned char> buffer2(m_bbat.m_blockSize); |
| 1217 | unsigned k = 109; |
| 1218 | unsigned long sector; |
| 1219 | for (unsigned r = 0; r < m_header.m_num_mbat && k > 0; r++) |
| 1220 | { |
| 1221 | if (r == 0) // 1st meta bat location is in file header. |
| 1222 | sector = m_header.m_start_mbat; |
| 1223 | else // next meta bat location is the last current block value. |
| 1224 | sector = blocks[--k]; |
| 1225 | unsigned long readData=loadBigBlock(sector, buffer2.data(), m_bbat.m_blockSize); |
| 1226 | for (unsigned s=0; s < m_bbat.m_blockSize; s+=4) |
| 1227 | { |
| 1228 | // check that for maximum block and that the data are correctly read |
| 1229 | if (k >= m_header.m_num_bat || s>=readData) break; |
| 1230 | else blocks[k++] = readU32(&buffer2[s]); |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | // load big bat |
| 1236 | if (blocks.size()*m_bbat.m_blockSize > 0) |
| 1237 | { |
| 1238 | std::vector<unsigned char> buffer(blocks.size()*m_bbat.m_blockSize); |
| 1239 | unsigned long readData=loadBigBlocks(blocks, buffer.data(), buffer.size()); |
| 1240 | m_bbat.load(buffer.data(), (unsigned)readData); |
| 1241 | } |
| 1242 | |
| 1243 | // load small bat |
| 1244 | blocks.clear(); |
| 1245 | blocks = m_bbat.follow(m_header.m_start_sbat); |
| 1246 | if (blocks.size()*m_bbat.m_blockSize > 0) |
| 1247 | { |
| 1248 | std::vector<unsigned char> buffer(blocks.size()*m_bbat.m_blockSize); |
| 1249 | unsigned long readData=loadBigBlocks(blocks, buffer.data(), buffer.size()); |
| 1250 | m_sbat.load(buffer.data(), (unsigned)readData); |
| 1251 | } |
| 1252 | |
| 1253 | // load directory tree |
| 1254 | blocks.clear(); |
| 1255 | blocks = m_bbat.follow(m_header.m_start_dirent); |
| 1256 | if (blocks.size()*m_bbat.m_blockSize > 0) |
| 1257 | { |
| 1258 | std::vector<unsigned char> buffer(blocks.size()*m_bbat.m_blockSize); |
| 1259 | unsigned long readData=loadBigBlocks(blocks, buffer.data(), buffer.size()); |
| 1260 | m_dirtree.load(buffer.data(), (unsigned)readData); |
| 1261 | if (readData >= 0x74 + 4) |
| 1262 | { |
| 1263 | unsigned sb_start = readU32(&buffer[0x74]); |
| 1264 | |
| 1265 | // fetch block chain as data for small-files |
| 1266 | m_sb_blocks = m_bbat.follow(sb_start); // small files |
| 1267 | |
| 1268 | // so far so good |
| 1269 | m_result = librevenge::Storage::Ok; |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | unsigned long librevenge::IStorage::loadBigBlocks(std::vector<unsigned long> const &blocks, |
| 1275 | unsigned char *data, unsigned long maxlen) const |
| 1276 | { |
| 1277 | // sentinel |
| 1278 | if (!data) return 0; |
| 1279 | if (blocks.size() < 1) return 0; |
| 1280 | if (maxlen == 0) return 0; |
| 1281 | |
| 1282 | // read block one by one, seems fast enough |
| 1283 | unsigned long bytes = 0; |
| 1284 | for (unsigned long i=0; (i < blocks.size()) & (bytes<maxlen); i++) |
| 1285 | { |
| 1286 | unsigned long block = blocks[i]; |
| 1287 | unsigned long pos = m_bbat.m_blockSize * (block+1); |
| 1288 | unsigned long p = (m_bbat.m_blockSize < maxlen-bytes) ? m_bbat.m_blockSize : maxlen-bytes; |
| 1289 | |
| 1290 | if (m_input->seek(long(pos), RVNG_SEEK_SET)!=0) |
| 1291 | continue; |
| 1292 | unsigned long numBytesRead = 0; |
| 1293 | const unsigned char *buf = m_input->read(p, numBytesRead); |
| 1294 | memcpy(data+bytes, buf, numBytesRead); |
| 1295 | bytes += numBytesRead; |
| 1296 | } |
| 1297 | |
| 1298 | return bytes; |
| 1299 | } |
| 1300 | |
| 1301 | unsigned long librevenge::IStorage::loadBigBlock(unsigned long block, |
| 1302 | unsigned char *data, unsigned long maxlen) const |
| 1303 | { |
| 1304 | // sentinel |
| 1305 | if (!data) return 0; |
| 1306 | |
| 1307 | // wraps call for loadBigBlocks |
| 1308 | std::vector<unsigned long> blocks; |
| 1309 | blocks.resize(1); |
| 1310 | blocks[ 0 ] = block; |
| 1311 | |
| 1312 | return loadBigBlocks(blocks, data, maxlen); |
| 1313 | } |
| 1314 | |
| 1315 | // return number of bytes which has been read |
| 1316 | unsigned long librevenge::IStorage::loadSmallBlocks(std::vector<unsigned long> const &blocks, |
| 1317 | unsigned char *data, unsigned long maxlen) |
| 1318 | { |
| 1319 | // sentinel |
| 1320 | if (!data || blocks.size() < 1 || maxlen == 0) return 0; |
| 1321 | |
| 1322 | // our own local buffer |
| 1323 | std::vector<unsigned char> tmpBuf(m_bbat.m_blockSize); |
| 1324 | |
| 1325 | // read small block one by one |
| 1326 | unsigned long bytes = 0; |
| 1327 | for (unsigned long i=0; (i<blocks.size()) & (bytes<maxlen); i++) |
| 1328 | { |
| 1329 | unsigned long block = blocks[i]; |
| 1330 | |
| 1331 | // find where the small-block exactly is |
| 1332 | unsigned long pos = block * m_sbat.m_blockSize; |
| 1333 | unsigned long bbindex = pos / m_bbat.m_blockSize; |
| 1334 | if (bbindex >= m_sb_blocks.size()) break; |
| 1335 | |
| 1336 | unsigned long readData=loadBigBlock(m_sb_blocks[ bbindex ], tmpBuf.data(), m_bbat.m_blockSize); |
| 1337 | |
| 1338 | // copy the data |
| 1339 | unsigned long offset = pos % m_bbat.m_blockSize; |
| 1340 | unsigned long p = (maxlen-bytes < m_bbat.m_blockSize-offset) ? maxlen-bytes : m_bbat.m_blockSize-offset; |
| 1341 | // sanety check |
| 1342 | if (m_sbat.m_blockSize<p) p=m_sbat.m_blockSize; |
| 1343 | if (readData < p) p = readData; |
| 1344 | memcpy(data + bytes, &tmpBuf[offset], p); |
| 1345 | bytes += p; |
| 1346 | } |
| 1347 | |
| 1348 | return bytes; |
| 1349 | } |
| 1350 | |
| 1351 | unsigned long librevenge::IStorage::loadSmallBlock(unsigned long block, |
| 1352 | unsigned char *data, unsigned long maxlen) |
| 1353 | { |
| 1354 | // sentinel |
| 1355 | if (!data) return 0; |
| 1356 | |
| 1357 | // wraps call for loadSmallBlocks |
| 1358 | std::vector<unsigned long> blocks; |
| 1359 | blocks.resize(1); |
| 1360 | blocks.assign(1, block); |
| 1361 | |
| 1362 | return loadSmallBlocks(blocks, data, maxlen); |
| 1363 | } |
| 1364 | |
| 1365 | // =========== OStorage ========== |
| 1366 | bool librevenge::OStorage::addDirectory(std::string const &dir) |
| 1367 | { |
| 1368 | DirEntry *e=createEntry(dir); |
| 1369 | if (!e) return false; |
| 1370 | |
| 1371 | e->m_type=1; |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
| 1375 | bool librevenge::OStorage::addStream(std::string const &name, unsigned char const *buffer, unsigned long len) |
| 1376 | { |
| 1377 | DirEntry *e=createEntry(name); |
| 1378 | if (!e) return false; |
| 1379 | |
| 1380 | if (!len) |
| 1381 | { |
| 1382 | RVNG_DEBUG_MSG(("librevengeOLE::OStorage::addStream: call to create an empty file!!!\n")); |
| 1383 | return true; |
| 1384 | } |
| 1385 | e->m_start = insertData(buffer, len, useBigBlockFor(len)); |
| 1386 | e->m_size = len; |
| 1387 | return true; |
| 1388 | } |
| 1389 | |
| 1390 | bool librevenge::OStorage::updateToSave() |
| 1391 | { |
| 1392 | unsigned long dirSize = m_dirtree.saveSize(); |
| 1393 | DirEntry *rEntry = m_dirtree.entry(0); |
| 1394 | if (!dirSize || !rEntry) |
| 1395 | { |
| 1396 | RVNG_DEBUG_MSG(("librevengeOLE::OStorage::updateToSave: can not find dir tree size!!!\n")); |
| 1397 | return false; |
| 1398 | } |
| 1399 | m_dirtree.setInRedBlackTreeForm(); |
| 1400 | |
| 1401 | std::vector<unsigned char> buffer; |
| 1402 | // first add the small block allocation table |
| 1403 | unsigned long sbatSize = m_sbat.saveSize(); |
| 1404 | if (!sbatSize) |
| 1405 | m_header.m_start_sbat = Bat; |
| 1406 | else |
| 1407 | { |
| 1408 | // FIXME: set m_header.m_start_sbat |
| 1409 | buffer.resize(sbatSize); |
| 1410 | m_sbat.save(buffer.data()); |
| 1411 | m_header.m_num_sbat = (unsigned)(sbatSize+511)/512; |
| 1412 | m_header.m_start_sbat = insertData(buffer.data(), sbatSize, true, unsigned(Eof)); |
| 1413 | if (m_sb_blocks.size()) |
| 1414 | { |
| 1415 | rEntry->m_start =(unsigned) m_sb_blocks[0]; |
| 1416 | m_bbat.setChain(m_sb_blocks, unsigned(Eof)); |
| 1417 | } |
| 1418 | rEntry->m_size = 64*m_num_sbat; |
| 1419 | } |
| 1420 | // then add dirtree |
| 1421 | buffer.resize(dirSize); |
| 1422 | m_dirtree.save(buffer.data()); |
| 1423 | m_header.m_start_dirent=insertData(buffer.data(), dirSize, true, unsigned(Eof)); |
| 1424 | |
| 1425 | // now update the big alloc table to add size for self and for the meta data |
| 1426 | unsigned numBBlock = m_num_bbat; |
| 1427 | if (numBBlock==0) |
| 1428 | { |
| 1429 | RVNG_DEBUG_MSG(("librevengeOLE::OStorage::updateToSave: can not find any big block!!!\n")); |
| 1430 | return false; |
| 1431 | } |
| 1432 | unsigned numBAlloc = (numBBlock+127)/128; |
| 1433 | unsigned numMAlloc = (numBAlloc+17)/127; // ie. 109->0, 110->1, ... |
| 1434 | while (numBBlock+numBAlloc+numMAlloc > numBAlloc*128) |
| 1435 | { |
| 1436 | numBAlloc++; |
| 1437 | numMAlloc = (numBAlloc+17)/127; |
| 1438 | } |
| 1439 | |
| 1440 | std::vector<unsigned long> mainBlocks(numBAlloc); |
| 1441 | unsigned b = 0; |
| 1442 | for (b = 0; b < numBAlloc; b++) |
| 1443 | { |
| 1444 | mainBlocks[b]=numBBlock+b; |
| 1445 | m_bbat.set(mainBlocks[b], unsigned(Bat)); |
| 1446 | } |
| 1447 | if (numMAlloc) |
| 1448 | { |
| 1449 | for (b = 0; b < numMAlloc; b++) |
| 1450 | m_bbat.set(numBBlock+numBAlloc+b, unsigned(MetaBat)); |
| 1451 | } |
| 1452 | |
| 1453 | // now add the small block allocation table |
| 1454 | unsigned long bbatSize = m_bbat.saveSize(); |
| 1455 | if (bbatSize) |
| 1456 | { |
| 1457 | buffer.resize(bbatSize); |
| 1458 | m_bbat.save(buffer.data()); |
| 1459 | insertData(buffer.data(), bbatSize, true, unsigned(Bat)); |
| 1460 | } |
| 1461 | |
| 1462 | for (b = 0; b < numBAlloc; b++) |
| 1463 | { |
| 1464 | if (b >= 109) |
| 1465 | break; |
| 1466 | m_header.m_blocks_bbat[b]=mainBlocks[b]; |
| 1467 | } |
| 1468 | if (numMAlloc) |
| 1469 | { |
| 1470 | buffer.resize(numMAlloc*512,0); |
| 1471 | size_t wPos=0; |
| 1472 | for (b=109; b < numBAlloc; b++) |
| 1473 | { |
| 1474 | if ((wPos%512)==508) |
| 1475 | { |
| 1476 | writeU32(&buffer[wPos],numBBlock+numBAlloc+(wPos+4)/512); |
| 1477 | wPos+=4; |
| 1478 | } |
| 1479 | writeU32(&buffer[wPos],mainBlocks[b]); |
| 1480 | wPos+=4; |
| 1481 | } |
| 1482 | while (wPos%512) |
| 1483 | { |
| 1484 | writeU32(&buffer[wPos], Avail); |
| 1485 | wPos+=4; |
| 1486 | } |
| 1487 | m_header.m_start_mbat = insertData((unsigned char const *)buffer.data(), 512*numMAlloc, true, unsigned(Eof)); |
| 1488 | m_header.m_start_mbat = numBBlock+numBAlloc; |
| 1489 | } |
| 1490 | m_header.m_num_bat = (m_num_bbat+127)/128; |
| 1491 | m_header.m_num_mbat = numMAlloc; |
| 1492 | |
| 1493 | m_data.resize((m_num_bbat+1)*512,0); |
| 1494 | // save the header |
| 1495 | m_header.save(m_data.data()); |
| 1496 | return true; |
| 1497 | } |
| 1498 | |
| 1499 | unsigned librevenge::OStorage::insertData(unsigned char const *buffer, unsigned long len, bool useBigBlock, unsigned end) |
| 1500 | { |
| 1501 | if (buffer==nullptr || len == 0) |
| 1502 | { |
| 1503 | RVNG_DEBUG_MSG(("librevengeOLE::OStorage::insertData: call with no data\n")); |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | unsigned long bSize = getMaximumSize(useBigBlock); |
| 1508 | size_t nBlock = ((len+bSize-1)/bSize); |
| 1509 | std::vector<unsigned long> chain; |
| 1510 | for (size_t b=0; b < nBlock; b++) |
| 1511 | { |
| 1512 | unsigned block=useBigBlock ? newBBlock() : newSBlock(); |
| 1513 | chain.push_back(block); |
| 1514 | size_t wPos = getDataAddress(block, useBigBlock); |
| 1515 | unsigned long wSize = len > bSize ? bSize : len; |
| 1516 | memcpy(&m_data[wPos], buffer, wSize); |
| 1517 | buffer += bSize; |
| 1518 | len -= wSize; |
| 1519 | } |
| 1520 | if (useBigBlock) |
| 1521 | m_bbat.setChain(chain, end); |
| 1522 | else |
| 1523 | m_sbat.setChain(chain, end); |
| 1524 | return (unsigned) chain[0]; |
| 1525 | } |
| 1526 | |
| 1527 | // =========== IStream ========== |
| 1528 | |
| 1529 | librevenge::IStream::IStream(librevenge::IStorage *s, std::string const &name) : |
| 1530 | m_iStorage(s), |
| 1531 | m_size(0), |
| 1532 | m_name(name), |
| 1533 | m_blocks(), |
| 1534 | m_pos(0), |
| 1535 | m_data() |
| 1536 | { |
| 1537 | if (!name.length() || !m_iStorage) return; |
| 1538 | m_iStorage->load(); |
| 1539 | DirEntry *entry = m_iStorage->entry(name); |
| 1540 | if (!entry) |
| 1541 | return; |
| 1542 | if (entry->is_dir()) |
| 1543 | { |
| 1544 | createOleFromDirectory(s, name); |
| 1545 | return; |
| 1546 | } |
| 1547 | |
| 1548 | m_size = entry->m_size; |
| 1549 | |
| 1550 | unsigned blockSize = 0; |
| 1551 | |
| 1552 | if (m_iStorage->use_big_block_for(entry->m_size)) |
| 1553 | { |
| 1554 | m_blocks = m_iStorage->m_bbat.follow(entry->m_start); |
| 1555 | blockSize = m_iStorage->m_bbat.m_blockSize; |
| 1556 | } |
| 1557 | else |
| 1558 | { |
| 1559 | m_blocks = m_iStorage->m_sbat.follow(entry->m_start); |
| 1560 | blockSize = m_iStorage->m_sbat.m_blockSize; |
| 1561 | } |
| 1562 | |
| 1563 | // sanity check of stream size |
| 1564 | const auto maxSize = unsigned(blockSize * m_blocks.size()); |
| 1565 | if (m_size > maxSize) |
| 1566 | { |
| 1567 | RVNG_DEBUG_MSG(("librevenge::IStream::IStream: size %lu is wrong, using an approximated value %u\n", m_size, maxSize)); |
| 1568 | m_size = maxSize; |
| 1569 | entry->m_size = m_size; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | unsigned long librevenge::IStream::readData(unsigned long pos, unsigned char *data, unsigned long maxlen) |
| 1574 | { |
| 1575 | // sanity checks |
| 1576 | if (!data || (maxlen == 0) || (unsigned long)m_data.size() != m_size || !m_size) |
| 1577 | return 0; |
| 1578 | |
| 1579 | if (pos >= m_size) |
| 1580 | return 0; |
| 1581 | unsigned long count = m_size - pos; |
| 1582 | if (count > maxlen) count = maxlen; |
| 1583 | memcpy(data, &m_data[size_t(pos)], count); |
| 1584 | return count; |
| 1585 | } |
| 1586 | |
| 1587 | unsigned long librevenge::IStream::readUsingStorage(unsigned long pos, unsigned char *data, unsigned long maxlen) |
| 1588 | { |
| 1589 | // sanity checks |
| 1590 | if (!data || (maxlen == 0) || !m_iStorage || !m_size) return 0; |
| 1591 | |
| 1592 | unsigned long totalbytes = 0; |
| 1593 | |
| 1594 | if (!m_iStorage->use_big_block_for(size())) |
| 1595 | { |
| 1596 | // small file |
| 1597 | unsigned sBlockSize = m_iStorage->m_sbat.m_blockSize; |
| 1598 | unsigned long index = pos / sBlockSize; |
| 1599 | |
| 1600 | if (index >= m_blocks.size()) return 0; |
| 1601 | |
| 1602 | std::vector<unsigned char> buf(sBlockSize); |
| 1603 | unsigned long offset = pos % sBlockSize; |
| 1604 | while (totalbytes < maxlen) |
| 1605 | { |
| 1606 | if (index >= m_blocks.size()) break; |
| 1607 | m_iStorage->loadSmallBlock(m_blocks[index], buf.data(), m_iStorage->m_bbat.m_blockSize); |
| 1608 | unsigned long count = sBlockSize - offset; |
| 1609 | if (count > maxlen-totalbytes) count = maxlen-totalbytes; |
| 1610 | memcpy(data+totalbytes, &buf[offset], count); |
| 1611 | totalbytes += count; |
| 1612 | offset = 0; |
| 1613 | index++; |
| 1614 | } |
| 1615 | } |
| 1616 | else |
| 1617 | { |
| 1618 | // big file |
| 1619 | unsigned bBlockSize = m_iStorage->m_bbat.m_blockSize; |
| 1620 | unsigned long index = pos / bBlockSize; |
| 1621 | |
| 1622 | if (index >= m_blocks.size()) return 0; |
| 1623 | |
| 1624 | std::vector<unsigned char> buf(bBlockSize); |
| 1625 | unsigned long offset = pos % bBlockSize; |
| 1626 | while (totalbytes < maxlen) |
| 1627 | { |
| 1628 | if (index >= m_blocks.size()) break; |
| 1629 | m_iStorage->loadBigBlock(m_blocks[index], buf.data(), bBlockSize); |
| 1630 | unsigned long count = bBlockSize - offset; |
| 1631 | if (count > maxlen-totalbytes) count = maxlen-totalbytes; |
| 1632 | memcpy(data+totalbytes, &buf[offset], count); |
| 1633 | totalbytes += count; |
| 1634 | index++; |
| 1635 | offset = 0; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | return totalbytes; |
| 1640 | } |
| 1641 | |
| 1642 | bool librevenge::IStream::createOleFromDirectory(IStorage *io, std::string const &name) |
| 1643 | { |
| 1644 | // sanety check |
| 1645 | if (!name.length() || !io) return false; |
| 1646 | m_iStorage->load(); |
| 1647 | DirEntry *entry = io->entry(name); |
| 1648 | if (!entry || !entry->is_dir()) return false; |
| 1649 | |
| 1650 | unsigned index = io->index(name); |
| 1651 | if (index==librevenge::NotFound) return false; |
| 1652 | std::vector<std::string> nodes=io->getSubStreamList(index, true); |
| 1653 | if (nodes.size() <= 1) |
| 1654 | { |
| 1655 | RVNG_DEBUG_MSG(("librevenge::IStream::createOleFromDirectory: can not find any child for %s\n", name.c_str())); |
| 1656 | return false; |
| 1657 | } |
| 1658 | try |
| 1659 | { |
| 1660 | std::string dir(name); |
| 1661 | size_t len=dir.length(); |
| 1662 | if (len && dir[len-1]=='/') dir.resize(len-1); |
| 1663 | std::vector<unsigned char> buffer; |
| 1664 | |
| 1665 | // try to compute a minimum data size |
| 1666 | unsigned long minimalSize = 0; |
| 1667 | size_t l=0; |
| 1668 | for (l=0; l < nodes.size(); l++) |
| 1669 | { |
| 1670 | std::string fullName=dir+nodes[l]; |
| 1671 | entry=io->entry(fullName); |
| 1672 | if (!entry || !entry->m_valid) |
| 1673 | continue; |
| 1674 | minimalSize += 128; // DirEntry |
| 1675 | if (!entry->is_dir() && entry->m_size < 50000000) |
| 1676 | minimalSize+=((entry->m_size+63)/64)*64; |
| 1677 | } |
| 1678 | |
| 1679 | librevenge::OStorage storage(minimalSize); |
| 1680 | storage.setRevision(io->revision()); |
| 1681 | if (!io->hasRootTypePc()) |
| 1682 | storage.setRootType(false); |
| 1683 | for (l=0; l < nodes.size(); l++) |
| 1684 | { |
| 1685 | std::string fullName=dir+nodes[l]; |
| 1686 | entry=io->entry(fullName); |
| 1687 | if (!entry) |
| 1688 | { |
| 1689 | RVNG_DEBUG_MSG(("librevenge::IStream::createOleFromDirectory: can not find child for %s\n", fullName.c_str())); |
| 1690 | continue; |
| 1691 | } |
| 1692 | if (entry->is_dir()) |
| 1693 | { |
| 1694 | if (entry->m_child == librevenge::DirEntry::End) |
| 1695 | storage.addDirectory(nodes[l]); |
| 1696 | continue; |
| 1697 | } |
| 1698 | librevenge::IStream leafStream(io,fullName); |
| 1699 | unsigned long sz=leafStream.size(); |
| 1700 | bool ok = true; |
| 1701 | if (sz) |
| 1702 | { |
| 1703 | // a test because empty file are rare but seems to exists |
| 1704 | buffer.resize(sz); |
| 1705 | ok = leafStream.read(buffer.data(), sz) == sz; |
| 1706 | if (ok) |
| 1707 | ok=storage.addStream(nodes[l], buffer.data(), sz); |
| 1708 | } |
| 1709 | else |
| 1710 | { |
| 1711 | ok=storage.addStream(nodes[l], nullptr, 0); |
| 1712 | } |
| 1713 | if (!ok) |
| 1714 | { |
| 1715 | RVNG_DEBUG_MSG(("librevenge::IStream::createOleFromDirectory: can not read %s\n", fullName.c_str())); |
| 1716 | return false; |
| 1717 | } |
| 1718 | } |
| 1719 | // finally try to update the storage info |
| 1720 | std::vector<std::string> resNodes=storage.getSubStreamList(0, true); |
| 1721 | for (l=0; l < resNodes.size(); l++) |
| 1722 | { |
| 1723 | std::string fullName=dir+resNodes[l]; |
| 1724 | entry=io->entry(fullName); |
| 1725 | if (!entry) continue; |
| 1726 | if (resNodes[l].length()) |
| 1727 | storage.setInformation(resNodes[l], entry->m_info); |
| 1728 | } |
| 1729 | |
| 1730 | // retrieve the data |
| 1731 | if (!storage.getFileData(m_data)) |
| 1732 | { |
| 1733 | m_data.resize(0); |
| 1734 | return false; |
| 1735 | } |
| 1736 | // ok, we can update size |
| 1737 | m_size = (unsigned long) m_data.size(); |
| 1738 | return true; |
| 1739 | } |
| 1740 | catch (...) |
| 1741 | { |
| 1742 | } |
| 1743 | return false; |
| 1744 | } |
| 1745 | |
| 1746 | // =========== Storage ========== |
| 1747 | |
| 1748 | librevenge::Storage::Storage(RVNGInputStream *is) : |
| 1749 | m_io(new IStorage(is)) |
| 1750 | { |
| 1751 | } |
| 1752 | |
| 1753 | librevenge::Storage::~Storage() |
| 1754 | { |
| 1755 | } |
| 1756 | |
| 1757 | librevenge::Storage::Result librevenge::Storage::result() |
| 1758 | { |
| 1759 | return m_io->m_result; |
| 1760 | } |
| 1761 | |
| 1762 | bool librevenge::Storage::isStructured() |
| 1763 | { |
| 1764 | return m_io->isStructured(); |
| 1765 | } |
| 1766 | |
| 1767 | std::vector<std::string> librevenge::Storage::getSubStreamNamesList() |
| 1768 | { |
| 1769 | std::vector<std::string> res=m_io->getSubStreamNamesList(); |
| 1770 | |
| 1771 | // time to do some cleaning ( remove ^A, ^B, ... ) |
| 1772 | for (auto &re : res) |
| 1773 | { |
| 1774 | std::string str=re, finalStr(""); |
| 1775 | for (char s : str) |
| 1776 | if (s>=32) |
| 1777 | finalStr+=s; |
| 1778 | re=finalStr; |
| 1779 | } |
| 1780 | return res; |
| 1781 | } |
| 1782 | |
| 1783 | // =========== Stream ========== |
| 1784 | |
| 1785 | librevenge::Stream::Stream(librevenge::Storage *storage, const std::string &name) : |
| 1786 | m_io(new librevenge::IStream(storage->m_io.get(), name)) |
| 1787 | { |
| 1788 | } |
| 1789 | |
| 1790 | // FIXME tell parent we're gone |
| 1791 | librevenge::Stream::~Stream() |
| 1792 | { |
| 1793 | } |
| 1794 | |
| 1795 | unsigned long librevenge::Stream::size() |
| 1796 | { |
| 1797 | return m_io ? m_io->size() : 0; |
| 1798 | } |
| 1799 | |
| 1800 | unsigned long librevenge::Stream::read(unsigned char *data, unsigned long maxlen) |
| 1801 | { |
| 1802 | return m_io ? m_io->read(data, maxlen) : 0; |
| 1803 | } |
| 1804 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |