| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 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) 2006 Ariya Hidayat ([email protected]) |
| 11 | * Copyright (C) 2007 Fridrich Strba ([email protected]) |
| 12 | * |
| 13 | * For minor contributions see the git repository. |
| 14 | * |
| 15 | * Alternatively, the contents of this file may be used under the terms |
| 16 | * of the GNU Lesser General Public License Version 2.1 or later |
| 17 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 18 | * applicable instead of those above. |
| 19 | */ |
| 20 | |
| 21 | #include <librevenge-stream/librevenge-stream.h> |
| 22 | |
| 23 | #include <limits> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | |
| 32 | #include "RVNGOLEStream.h" |
| 33 | #include "RVNGZipStream.h" |
| 34 | |
| 35 | #ifndef S_ISREG |
| 36 | #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 37 | #endif |
| 38 | |
| 39 | namespace librevenge |
| 40 | { |
| 41 | |
| 42 | enum RVNGStreamType { UNKNOWN, FLAT, OLE2, ZIP }; |
| 43 | |
| 44 | class RVNGFileStreamPrivate |
| 45 | { |
| 46 | public: |
| 47 | RVNGFileStreamPrivate(); |
| 48 | ~RVNGFileStreamPrivate(); |
| 49 | FILE *file; |
| 50 | unsigned long streamSize; |
| 51 | unsigned char *readBuffer; |
| 52 | unsigned long readBufferLength; |
| 53 | unsigned long readBufferPos; |
| 54 | RVNGStreamType streamType; |
| 55 | std::vector<std::string> streamNameList; |
| 56 | private: |
| 57 | RVNGFileStreamPrivate(const RVNGFileStreamPrivate &); |
| 58 | RVNGFileStreamPrivate &operator=(const RVNGFileStreamPrivate &); |
| 59 | }; |
| 60 | |
| 61 | class RVNGStringStreamPrivate |
| 62 | { |
| 63 | public: |
| 64 | RVNGStringStreamPrivate(const unsigned char *data, unsigned dataSize); |
| 65 | ~RVNGStringStreamPrivate(); |
| 66 | std::vector<unsigned char> buffer; |
| 67 | volatile long offset; |
| 68 | RVNGStreamType streamType; |
| 69 | std::vector<std::string> streamNameList; |
| 70 | private: |
| 71 | RVNGStringStreamPrivate(const RVNGStringStreamPrivate &); |
| 72 | RVNGStringStreamPrivate &operator=(const RVNGStringStreamPrivate &); |
| 73 | }; |
| 74 | |
| 75 | RVNGFileStreamPrivate::RVNGFileStreamPrivate() : |
| 76 | file(nullptr), |
| 77 | streamSize(0), |
| 78 | readBuffer(nullptr), |
| 79 | readBufferLength(0), |
| 80 | readBufferPos(0), |
| 81 | streamType(UNKNOWN), |
| 82 | streamNameList() |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | RVNGFileStreamPrivate::~RVNGFileStreamPrivate() |
| 87 | { |
| 88 | if (file) |
| 89 | fclose(file); |
| 90 | if (readBuffer) |
| 91 | delete [] readBuffer; |
| 92 | } |
| 93 | |
| 94 | RVNGStringStreamPrivate::RVNGStringStreamPrivate(const unsigned char *data, unsigned dataSize) : |
| 95 | buffer(dataSize), |
| 96 | offset(0), |
| 97 | streamType(UNKNOWN), |
| 98 | streamNameList() |
| 99 | |
| 100 | { |
| 101 | if (dataSize != 0) |
| 102 | memcpy(buffer.data(), data, dataSize); |
| 103 | } |
| 104 | |
| 105 | RVNGStringStreamPrivate::~RVNGStringStreamPrivate() |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | RVNGFileStream::RVNGFileStream(const char *filename) : |
| 110 | RVNGInputStream(), |
| 111 | d(new RVNGFileStreamPrivate()) |
| 112 | { |
| 113 | d->file = fopen(filename, "rb"); |
| 114 | if (!d->file || ferror(d->file)) |
| 115 | { |
| 116 | delete d; |
| 117 | d = nullptr; |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | struct stat status; |
| 122 | const int retval = stat(filename, &status); |
| 123 | if ((0 != retval) || !S_ISREG(status.st_mode)) |
| 124 | { |
| 125 | delete d; |
| 126 | d = nullptr; |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | fseek(d->file, 0, SEEK_END); |
| 131 | |
| 132 | d->streamSize = (unsigned long) ftell(d->file); |
| 133 | if (d->streamSize == (unsigned long)-1) |
| 134 | d->streamSize = 0; |
| 135 | // preventing possible unsigned/signed issues later by truncating the file |
| 136 | if (d->streamSize > (std::numeric_limits<unsigned long>::max)() / 2) |
| 137 | d->streamSize = (std::numeric_limits<unsigned long>::max)() / 2; |
| 138 | fseek(d->file, 0, SEEK_SET); |
| 139 | } |
| 140 | |
| 141 | RVNGFileStream::~RVNGFileStream() |
| 142 | { |
| 143 | delete d; |
| 144 | } |
| 145 | |
| 146 | #define BUFFER_MAX 65536 |
| 147 | |
| 148 | const unsigned char *RVNGFileStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
| 149 | { |
| 150 | numBytesRead = 0; |
| 151 | |
| 152 | if (!d) |
| 153 | return nullptr; |
| 154 | |
| 155 | if (numBytes == 0 || numBytes > (std::numeric_limits<unsigned long>::max)()/2 |
| 156 | || ferror(d->file)) |
| 157 | return nullptr; |
| 158 | |
| 159 | // can we read from the buffer? |
| 160 | if (d->readBuffer && (d->readBufferPos + numBytes > d->readBufferPos) |
| 161 | && (d->readBufferPos + numBytes <= d->readBufferLength)) |
| 162 | { |
| 163 | const unsigned char *pTmp = d->readBuffer + d->readBufferPos; |
| 164 | d->readBufferPos += numBytes; |
| 165 | numBytesRead = numBytes; |
| 166 | return pTmp; |
| 167 | } |
| 168 | |
| 169 | // hmm, we cannot: go back by the bytes we read ahead && invalidate the buffer |
| 170 | if (d->readBuffer) |
| 171 | { |
| 172 | fseek(d->file, (long)ftell(d->file) - (long)d->readBufferLength, SEEK_SET); |
| 173 | fseek(d->file, (long)d->readBufferPos, SEEK_CUR); |
| 174 | delete [] d->readBuffer; |
| 175 | d->readBuffer = nullptr; |
| 176 | d->readBufferPos = 0; |
| 177 | d->readBufferLength = 0; |
| 178 | } |
| 179 | |
| 180 | auto curpos = (unsigned long) tell(); |
| 181 | if (curpos == (unsigned long)-1) // tellg() returned ERROR |
| 182 | return nullptr; |
| 183 | |
| 184 | if ((curpos + numBytes < curpos) /*overflow*/ || |
| 185 | (curpos + numBytes >= d->streamSize)) /*reading more than available*/ |
| 186 | { |
| 187 | numBytes = d->streamSize - curpos; |
| 188 | } |
| 189 | |
| 190 | if (numBytes == 0) |
| 191 | return nullptr; |
| 192 | |
| 193 | if (numBytes < BUFFER_MAX) |
| 194 | { |
| 195 | if (BUFFER_MAX < d->streamSize - curpos) |
| 196 | d->readBufferLength = BUFFER_MAX; |
| 197 | else /* BUFFER_MAX >= d->streamSize - curpos */ |
| 198 | d->readBufferLength = d->streamSize - curpos; |
| 199 | } |
| 200 | else |
| 201 | d->readBufferLength = numBytes; |
| 202 | |
| 203 | fseek(d->file, (long) d->readBufferLength, SEEK_CUR); |
| 204 | fseek(d->file, (long) curpos, SEEK_SET); |
| 205 | |
| 206 | d->readBuffer = new unsigned char[d->readBufferLength]; |
| 207 | unsigned long tmpNumBytes; |
| 208 | if ((tmpNumBytes = fread((void *)(d->readBuffer), 1, d->readBufferLength, d->file)) != d->readBufferLength) |
| 209 | d->readBufferLength = tmpNumBytes; |
| 210 | |
| 211 | d->readBufferPos = 0; |
| 212 | if (!d->readBufferLength) |
| 213 | return nullptr; |
| 214 | |
| 215 | numBytesRead = numBytes; |
| 216 | |
| 217 | d->readBufferPos += numBytesRead; |
| 218 | return const_cast<const unsigned char *>(d->readBuffer); |
| 219 | } |
| 220 | |
| 221 | long RVNGFileStream::tell() |
| 222 | { |
| 223 | if (!d) |
| 224 | return -1L; |
| 225 | return ferror(d->file) ? -1L : (long)ftell(d->file) - (long)d->readBufferLength + (long) d->readBufferPos; |
| 226 | } |
| 227 | |
| 228 | int RVNGFileStream::seek(long offset, RVNG_SEEK_TYPE seekType) |
| 229 | { |
| 230 | if (!d) |
| 231 | return -1; |
| 232 | if (seekType == RVNG_SEEK_CUR) |
| 233 | offset += tell(); |
| 234 | if (seekType == RVNG_SEEK_END) |
| 235 | offset += (long)d->streamSize; |
| 236 | |
| 237 | if (offset < 0) |
| 238 | offset = 0; |
| 239 | if (offset > (long)d->streamSize) |
| 240 | offset = (long)d->streamSize; |
| 241 | |
| 242 | if (!ferror(d->file) && offset < ftell(d->file) && (unsigned long)offset >= (unsigned long)ftell(d->file) - d->readBufferLength) |
| 243 | { |
| 244 | d->readBufferPos = (unsigned long)(offset + (long) d->readBufferLength - (long) ftell(d->file)); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | if (d->readBuffer) // seeking outside of the buffer, so invalidate the buffer |
| 249 | { |
| 250 | fseek(d->file, (long)ftell(d->file) - (long)d->readBufferLength, SEEK_SET); |
| 251 | fseek(d->file, (long) d->readBufferPos, SEEK_CUR); |
| 252 | delete [] d->readBuffer; |
| 253 | d->readBuffer = nullptr; |
| 254 | d->readBufferPos = 0; |
| 255 | d->readBufferLength = 0; |
| 256 | } |
| 257 | |
| 258 | if (!ferror(d->file)) |
| 259 | { |
| 260 | fseek(d->file, offset, SEEK_SET); |
| 261 | return (int)((long)ftell(d->file) == -1) ; |
| 262 | } |
| 263 | else |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | bool RVNGFileStream::isEnd() |
| 268 | { |
| 269 | if (!d) |
| 270 | return true; |
| 271 | return (tell() >= (long)d->streamSize); |
| 272 | } |
| 273 | |
| 274 | bool RVNGFileStream::isStructured() |
| 275 | { |
| 276 | if (!d) |
| 277 | return false; |
| 278 | if (ferror(d->file)) |
| 279 | return false; |
| 280 | if (d->streamType == UNKNOWN) |
| 281 | { |
| 282 | seek(0, RVNG_SEEK_SET); |
| 283 | |
| 284 | // Check whether it is OLE2 storage |
| 285 | Storage tmpStorage(this); |
| 286 | if (tmpStorage.isStructured()) |
| 287 | { |
| 288 | d->streamType = OLE2; |
| 289 | d->streamNameList = tmpStorage.getSubStreamNamesList(); |
| 290 | seek(0, RVNG_SEEK_SET); |
| 291 | return true; |
| 292 | } |
| 293 | seek(0, RVNG_SEEK_SET); |
| 294 | if (RVNGZipStream::isZipFile(this)) |
| 295 | { |
| 296 | d->streamType = ZIP; |
| 297 | d->streamNameList = RVNGZipStream::getSubStreamNamesList(this); |
| 298 | seek(0, RVNG_SEEK_SET); |
| 299 | return true; |
| 300 | } |
| 301 | d->streamType = FLAT; |
| 302 | return false; |
| 303 | } |
| 304 | else if (d->streamType == FLAT) |
| 305 | return false; |
| 306 | else |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | unsigned RVNGFileStream::subStreamCount() |
| 311 | { |
| 312 | if (!isStructured()||!d) return 0; |
| 313 | return (unsigned) d->streamNameList.size(); |
| 314 | } |
| 315 | |
| 316 | const char *RVNGFileStream::subStreamName(unsigned id) |
| 317 | { |
| 318 | if (!isStructured() ||!d || id>=(unsigned) d->streamNameList.size()) |
| 319 | return nullptr; |
| 320 | return d->streamNameList[size_t(id)].c_str(); |
| 321 | } |
| 322 | |
| 323 | bool RVNGFileStream::existsSubStream(const char *name) |
| 324 | { |
| 325 | if (!name || !d) |
| 326 | return false; |
| 327 | if (ferror(d->file)) |
| 328 | return false; |
| 329 | if (d->streamType == UNKNOWN && !isStructured()) |
| 330 | return false; |
| 331 | if (d->streamType == FLAT) |
| 332 | return false; |
| 333 | for (const auto &i : d->streamNameList) |
| 334 | { |
| 335 | if (i == name) |
| 336 | return true; |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | RVNGInputStream *RVNGFileStream::getSubStreamById(unsigned id) |
| 342 | { |
| 343 | return getSubStreamByName(subStreamName(id)); |
| 344 | } |
| 345 | |
| 346 | RVNGInputStream *RVNGFileStream::getSubStreamByName(const char *name) |
| 347 | { |
| 348 | if (!name || !d) |
| 349 | return nullptr; |
| 350 | if (ferror(d->file)) |
| 351 | return nullptr; |
| 352 | if (d->streamType == UNKNOWN && !isStructured()) |
| 353 | return nullptr; |
| 354 | if (d->streamType == OLE2) |
| 355 | { |
| 356 | seek(0, RVNG_SEEK_SET); |
| 357 | Storage tmpStorage(this); |
| 358 | Stream tmpStream(&tmpStorage, name); |
| 359 | if (tmpStorage.result() != Storage::Ok || !tmpStream.size()) |
| 360 | return (RVNGInputStream *)nullptr; |
| 361 | |
| 362 | std::vector<unsigned char> buf(tmpStream.size()); |
| 363 | unsigned long tmpLength; |
| 364 | tmpLength = tmpStream.read(buf.data(), tmpStream.size()); |
| 365 | |
| 366 | // sanity check |
| 367 | if (tmpLength != tmpStream.size()) |
| 368 | /* something went wrong here and we do not trust the |
| 369 | resulting buffer */ |
| 370 | return (RVNGInputStream *)nullptr; |
| 371 | |
| 372 | return new RVNGStringStream(buf.data(), (unsigned)tmpLength); |
| 373 | } |
| 374 | else if (d->streamType == ZIP) |
| 375 | { |
| 376 | seek(0, RVNG_SEEK_SET); |
| 377 | return RVNGZipStream::getSubstream(this, name); |
| 378 | } |
| 379 | return nullptr; |
| 380 | } |
| 381 | |
| 382 | RVNGStringStream::RVNGStringStream(const unsigned char *data, const unsigned int dataSize) : |
| 383 | RVNGInputStream(), |
| 384 | d(new RVNGStringStreamPrivate(data, dataSize)) |
| 385 | { |
| 386 | } |
| 387 | |
| 388 | RVNGStringStream::~RVNGStringStream() |
| 389 | { |
| 390 | delete d; |
| 391 | } |
| 392 | |
| 393 | const unsigned char *RVNGStringStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
| 394 | { |
| 395 | numBytesRead = 0; |
| 396 | |
| 397 | if (numBytes == 0) |
| 398 | return nullptr; |
| 399 | |
| 400 | long numBytesToRead; |
| 401 | |
| 402 | if ((unsigned long)d->offset+numBytes < d->buffer.size()) |
| 403 | numBytesToRead = (long) numBytes; |
| 404 | else |
| 405 | numBytesToRead = (long) d->buffer.size() - d->offset; |
| 406 | |
| 407 | numBytesRead = (unsigned long) numBytesToRead; // about as paranoid as we can be.. |
| 408 | |
| 409 | if (numBytesToRead == 0) |
| 410 | return nullptr; |
| 411 | |
| 412 | long oldOffset = d->offset; |
| 413 | d->offset += numBytesToRead; |
| 414 | |
| 415 | return &d->buffer[size_t(oldOffset)]; |
| 416 | |
| 417 | } |
| 418 | |
| 419 | long RVNGStringStream::tell() |
| 420 | { |
| 421 | return d->offset; |
| 422 | } |
| 423 | |
| 424 | int RVNGStringStream::seek(long offset, RVNG_SEEK_TYPE seekType) |
| 425 | { |
| 426 | if (seekType == RVNG_SEEK_CUR) |
| 427 | d->offset += offset; |
| 428 | else if (seekType == RVNG_SEEK_SET) |
| 429 | d->offset = offset; |
| 430 | else if (seekType == RVNG_SEEK_END) |
| 431 | d->offset = offset+(long) d->buffer.size(); |
| 432 | |
| 433 | if (d->offset < 0) |
| 434 | { |
| 435 | d->offset = 0; |
| 436 | return -1; |
| 437 | } |
| 438 | if ((long)d->offset > (long)d->buffer.size()) |
| 439 | { |
| 440 | d->offset = (long) d->buffer.size(); |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | bool RVNGStringStream::isEnd() |
| 448 | { |
| 449 | if ((long)d->offset >= (long)d->buffer.size()) |
| 450 | return true; |
| 451 | |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | bool RVNGStringStream::isStructured() |
| 456 | { |
| 457 | if (d->buffer.empty()) |
| 458 | return false; |
| 459 | |
| 460 | if (d->streamType == UNKNOWN) |
| 461 | { |
| 462 | seek(0, RVNG_SEEK_SET); |
| 463 | |
| 464 | // Check whether it is OLE2 storage |
| 465 | Storage tmpStorage(this); |
| 466 | if (tmpStorage.isStructured()) |
| 467 | { |
| 468 | d->streamType = OLE2; |
| 469 | d->streamNameList = tmpStorage.getSubStreamNamesList(); |
| 470 | return true; |
| 471 | } |
| 472 | seek(0, RVNG_SEEK_SET); |
| 473 | if (RVNGZipStream::isZipFile(this)) |
| 474 | { |
| 475 | d->streamType = ZIP; |
| 476 | d->streamNameList = RVNGZipStream::getSubStreamNamesList(this); |
| 477 | return true; |
| 478 | } |
| 479 | d->streamType = FLAT; |
| 480 | return false; |
| 481 | } |
| 482 | else if (d->streamType == FLAT) |
| 483 | return false; |
| 484 | else |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | unsigned RVNGStringStream::subStreamCount() |
| 489 | { |
| 490 | if (!isStructured()||!d) return 0; |
| 491 | return (unsigned) d->streamNameList.size(); |
| 492 | } |
| 493 | |
| 494 | const char *RVNGStringStream::subStreamName(unsigned id) |
| 495 | { |
| 496 | if (!isStructured() ||!d || id>=(unsigned) d->streamNameList.size()) |
| 497 | return nullptr; |
| 498 | return d->streamNameList[size_t(id)].c_str(); |
| 499 | } |
| 500 | |
| 501 | bool RVNGStringStream::existsSubStream(const char *name) |
| 502 | { |
| 503 | if (!name || !d) |
| 504 | return false; |
| 505 | if (d->buffer.empty()) |
| 506 | return false; |
| 507 | if (d->streamType == UNKNOWN && !isStructured()) |
| 508 | return false; |
| 509 | if (d->streamType == FLAT) |
| 510 | return false; |
| 511 | for (const auto &i : d->streamNameList) |
| 512 | { |
| 513 | if (i == name) |
| 514 | return true; |
| 515 | } |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | RVNGInputStream *RVNGStringStream::getSubStreamById(unsigned id) |
| 520 | { |
| 521 | return getSubStreamByName(subStreamName(id)); |
| 522 | } |
| 523 | |
| 524 | RVNGInputStream *RVNGStringStream::getSubStreamByName(const char *name) |
| 525 | { |
| 526 | if (!name || d->buffer.empty()) |
| 527 | return nullptr; |
| 528 | if (d->streamType == UNKNOWN && !isStructured()) |
| 529 | return nullptr; |
| 530 | |
| 531 | if (d->streamType == OLE2) |
| 532 | { |
| 533 | seek(0, RVNG_SEEK_SET); |
| 534 | Storage tmpStorage(this); |
| 535 | Stream tmpStream(&tmpStorage, name); |
| 536 | if (tmpStorage.result() != Storage::Ok || !tmpStream.size()) |
| 537 | return (RVNGInputStream *)nullptr; |
| 538 | |
| 539 | std::vector<unsigned char> buf(tmpStream.size()); |
| 540 | unsigned long tmpLength; |
| 541 | tmpLength = tmpStream.read(buf.data(), tmpStream.size()); |
| 542 | |
| 543 | // sanity check |
| 544 | if (tmpLength != tmpStream.size()) |
| 545 | /* something went wrong here and we do not trust the |
| 546 | resulting buffer */ |
| 547 | return (RVNGInputStream *)nullptr; |
| 548 | |
| 549 | return new RVNGStringStream(buf.data(), (unsigned)tmpLength); |
| 550 | } |
| 551 | else if (d->streamType == ZIP) |
| 552 | return RVNGZipStream::getSubstream(this, name); |
| 553 | return nullptr; |
| 554 | } |
| 555 | |
| 556 | } |
| 557 | |
| 558 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |