MWAWInputStream.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
00002 
00003 /* libmwaw
00004 * Version: MPL 2.0 / LGPLv2+
00005 *
00006 * The contents of this file are subject to the Mozilla Public License Version
00007 * 2.0 (the "License"); you may not use this file except in compliance with
00008 * the License or as specified alternatively below. You may obtain a copy of
00009 * the License at http://www.mozilla.org/MPL/
00010 *
00011 * Software distributed under the License is distributed on an "AS IS" basis,
00012 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013 * for the specific language governing rights and limitations under the
00014 * License.
00015 *
00016 * Major Contributor(s):
00017 * Copyright (C) 2002 William Lachance (wrlach@gmail.com)
00018 * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
00019 * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
00020 * Copyright (C) 2006, 2007 Andrew Ziem
00021 * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
00022 *
00023 *
00024 * All Rights Reserved.
00025 *
00026 * For minor contributions see the git repository.
00027 *
00028 * Alternatively, the contents of this file may be used under the terms of
00029 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
00030 * in which case the provisions of the LGPLv2+ are applicable
00031 * instead of those above.
00032 */
00033 
00034 #ifndef MWAW_INPUT_STREAM_H
00035 #define MWAW_INPUT_STREAM_H
00036 
00037 #include <string>
00038 #include <vector>
00039 
00040 #include <libwpd/libwpd.h>
00041 #include <libwpd-stream/libwpd-stream.h>
00042 #include "libmwaw_internal.hxx"
00043 
00044 namespace libmwawOLE
00045 {
00046 class Storage;
00047 }
00048 
00049 class WPXBinaryData;
00050 
00060 class MWAWInputStream
00061 {
00062 public:
00067   MWAWInputStream(shared_ptr<WPXInputStream> inp, bool inverted);
00068 
00073   MWAWInputStream(WPXInputStream *input, bool inverted, bool checkCompression=false);
00075   ~MWAWInputStream();
00076 
00078   shared_ptr<WPXInputStream> input() {
00079     return m_stream;
00080   }
00082   static shared_ptr<MWAWInputStream> get(WPXBinaryData const &data, bool inverted);
00083 
00085   bool readInverted() const {
00086     return m_inverseRead;
00087   }
00089   void setReadInverted(bool newVal) {
00090     m_inverseRead = newVal;
00091   }
00092   //
00093   // Position: access
00094   //
00095 
00100   int seek(long offset, WPX_SEEK_TYPE seekType);
00102   long tell();
00104   long size() const {
00105     return m_streamSize;
00106   }
00108   bool checkPosition(long pos) const {
00109     if (pos < 0) return false;
00110     if (m_readLimit > 0 && pos > m_readLimit) return false;
00111     return pos<=m_streamSize;
00112   }
00114   bool atEOS();
00115 
00119   void pushLimit(long newLimit) {
00120     m_prevLimits.push_back(m_readLimit);
00121     m_readLimit = newLimit > m_streamSize ? m_streamSize : newLimit;
00122   }
00124   void popLimit() {
00125     if (m_prevLimits.size()) {
00126       m_readLimit = m_prevLimits.back();
00127       m_prevLimits.pop_back();
00128     } else m_readLimit = -1;
00129   }
00130 
00131   //
00132   // get data
00133   //
00134 
00136   unsigned long readULong(int num) {
00137     return readULong(m_stream.get(), num, 0, m_inverseRead);
00138   }
00140   long readLong(int num);
00142   bool readDouble(double &res);
00143 
00147   const uint8_t *read(size_t numBytes, unsigned long &numBytesRead);
00151   static unsigned long readULong(WPXInputStream *stream, int num, unsigned long a, bool inverseRead);
00152 
00154   bool readDataBlock(long size, WPXBinaryData &data);
00156   bool readEndDataBlock(WPXBinaryData &data);
00157 
00158   //
00159   // OLE access
00160   //
00161 
00163   bool isOLEStream();
00165   std::vector<std::string> getOLENames();
00167   shared_ptr<MWAWInputStream> getDocumentOLEStream(std::string name);
00168 
00169   //
00170   // Finder Info access
00171   //
00173   bool getFinderInfo(std::string &type, std::string &creator) const {
00174     if (!m_fInfoType.length() || !m_fInfoCreator.length()) {
00175       type = creator = "";
00176       return false;
00177     }
00178     type = m_fInfoType;
00179     creator = m_fInfoCreator;
00180     return true;
00181   }
00182 
00183   //
00184   // Resource Fork access
00185   //
00186 
00188   bool hasDataFork() const {
00189     return bool(m_stream);
00190   }
00192   bool hasResourceFork() const {
00193     return bool(m_resourceFork);
00194   }
00196   shared_ptr<MWAWInputStream> getResourceForkStream() {
00197     return m_resourceFork;
00198   }
00199 
00200 
00201 protected:
00203   void updateStreamSize();
00205   static uint8_t readU8(WPXInputStream *stream);
00206 
00208   bool createStorageOLE();
00209 
00211   bool unBinHex();
00213   bool unzipStream();
00215   bool unMacMIME();
00217   bool unMacMIME(MWAWInputStream *input,
00218                  shared_ptr<WPXInputStream> &dataInput,
00219                  shared_ptr<WPXInputStream> &rsrcInput) const;
00220 
00221 private:
00222   MWAWInputStream(MWAWInputStream const &orig);
00223   MWAWInputStream &operator=(MWAWInputStream const &orig);
00224 
00225 protected:
00227   shared_ptr<WPXInputStream> m_stream;
00229   long m_streamSize;
00230 
00232   bool m_inverseRead;
00233 
00235   long m_readLimit;
00237   std::vector<long> m_prevLimits;
00238 
00240   mutable std::string m_fInfoType;
00242   mutable std::string m_fInfoCreator;
00244   shared_ptr<MWAWInputStream> m_resourceFork;
00246   shared_ptr<libmwawOLE::Storage> m_storageOLE;
00247 };
00248 
00250 class MWAWStringStream: public WPXInputStream
00251 {
00252 public:
00254   MWAWStringStream(const unsigned char *data, const unsigned long dataSize);
00256   ~MWAWStringStream() { }
00257 
00261   const unsigned char *read(unsigned long numBytes, unsigned long &numBytesRead);
00263   long tell() {
00264     return m_offset;
00265   }
00270   int seek(long offset, WPX_SEEK_TYPE seekType);
00272   bool atEOS() {
00273     return ((long)m_offset >= (long)m_buffer.size());
00274   }
00275 
00280   bool isStructuredDocument() {
00281     return false;
00282   }
00287   WPXInputStream *getSubStream(const char *) {
00288     return 0;
00289   }
00290 
00295   bool isOLEStream() {
00296     return isStructuredDocument();
00297   }
00302   WPXInputStream *getDocumentOLEStream(const char *name) {
00303     return getSubStream(name);
00304   }
00305 
00306 private:
00308   std::vector<unsigned char> m_buffer;
00310   volatile long m_offset;
00311 
00312   MWAWStringStream(const MWAWStringStream &);
00313   MWAWStringStream &operator=(const MWAWStringStream &);
00314 };
00315 
00316 #endif
00317 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: