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 <librevenge/librevenge.h>
00041 #include <librevenge-stream/librevenge-stream.h>
00042 #include "libmwaw_internal.hxx"
00043 
00053 class MWAWInputStream
00054 {
00055 public:
00060   MWAWInputStream(shared_ptr<librevenge::RVNGInputStream> input, bool inverted);
00061 
00066   MWAWInputStream(librevenge::RVNGInputStream *input, bool inverted, bool checkCompression=false);
00068   ~MWAWInputStream();
00069 
00071   shared_ptr<librevenge::RVNGInputStream> input()
00072   {
00073     return m_stream;
00074   }
00076   static shared_ptr<MWAWInputStream> get(librevenge::RVNGBinaryData const &data, bool inverted);
00077 
00079   bool readInverted() const
00080   {
00081     return m_inverseRead;
00082   }
00084   void setReadInverted(bool newVal)
00085   {
00086     m_inverseRead = newVal;
00087   }
00088   //
00089   // Position: access
00090   //
00091 
00096   int seek(long offset, librevenge::RVNG_SEEK_TYPE seekType);
00098   long tell();
00100   long size() const
00101   {
00102     return m_streamSize;
00103   }
00105   bool checkPosition(long pos) const
00106   {
00107     if (pos < 0) return false;
00108     if (m_readLimit > 0 && pos > m_readLimit) return false;
00109     return pos<=m_streamSize;
00110   }
00112   bool isEnd();
00113 
00117   void pushLimit(long newLimit)
00118   {
00119     m_prevLimits.push_back(m_readLimit);
00120     m_readLimit = newLimit > m_streamSize ? m_streamSize : newLimit;
00121   }
00123   void popLimit()
00124   {
00125     if (m_prevLimits.size()) {
00126       m_readLimit = m_prevLimits.back();
00127       m_prevLimits.pop_back();
00128     }
00129     else m_readLimit = -1;
00130   }
00131 
00132   //
00133   // get data
00134   //
00135 
00137   unsigned long readULong(int num)
00138   {
00139     return readULong(m_stream.get(), num, 0, m_inverseRead);
00140   }
00142   long readLong(int num);
00144   bool readDouble8(double &res, bool &isNotANumber);
00146   bool readDoubleReverted8(double &res, bool &isNotANumber);
00148   bool readDouble10(double &res, bool &isNotANumber);
00149 
00153   const uint8_t *read(size_t numBytes, unsigned long &numBytesRead);
00157   static unsigned long readULong(librevenge::RVNGInputStream *stream, int num, unsigned long a, bool inverseRead);
00158 
00160   bool readDataBlock(long size, librevenge::RVNGBinaryData &data);
00162   bool readEndDataBlock(librevenge::RVNGBinaryData &data);
00163 
00164   //
00165   // OLE/Zip access
00166   //
00167 
00169   bool isStructured();
00171   unsigned subStreamCount();
00173   std::string subStreamName(unsigned id);
00174 
00176   shared_ptr<MWAWInputStream> getSubStreamByName(std::string const &name);
00178   shared_ptr<MWAWInputStream> getSubStreamById(unsigned id);
00179 
00180   //
00181   // Finder Info access
00182   //
00184   bool getFinderInfo(std::string &type, std::string &creator) const
00185   {
00186     if (!m_fInfoType.length() || !m_fInfoCreator.length()) {
00187       type = creator = "";
00188       return false;
00189     }
00190     type = m_fInfoType;
00191     creator = m_fInfoCreator;
00192     return true;
00193   }
00194 
00195   //
00196   // Resource Fork access
00197   //
00198 
00200   bool hasDataFork() const
00201   {
00202     return bool(m_stream);
00203   }
00205   bool hasResourceFork() const
00206   {
00207     return bool(m_resourceFork);
00208   }
00210   shared_ptr<MWAWInputStream> getResourceForkStream()
00211   {
00212     return m_resourceFork;
00213   }
00214 
00215 
00216 protected:
00218   void updateStreamSize();
00220   static uint8_t readU8(librevenge::RVNGInputStream *stream);
00221 
00223   bool unBinHex();
00225   bool unzipStream();
00227   bool unMacMIME();
00229   bool unMacMIME(MWAWInputStream *input,
00230                  shared_ptr<librevenge::RVNGInputStream> &dataInput,
00231                  shared_ptr<librevenge::RVNGInputStream> &rsrcInput) const;
00232 
00233 private:
00234   MWAWInputStream(MWAWInputStream const &orig);
00235   MWAWInputStream &operator=(MWAWInputStream const &orig);
00236 
00237 protected:
00239   shared_ptr<librevenge::RVNGInputStream> m_stream;
00241   long m_streamSize;
00242 
00244   bool m_inverseRead;
00245 
00247   long m_readLimit;
00249   std::vector<long> m_prevLimits;
00250 
00252   mutable std::string m_fInfoType;
00254   mutable std::string m_fInfoCreator;
00256   shared_ptr<MWAWInputStream> m_resourceFork;
00257 };
00258 
00259 #endif
00260 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: