zipios  2.2.0
Zipios -- a small C++ library that provides easy access to .zip files.
zipinputstreambuf.cpp
Go to the documentation of this file.
1 /*
2  Zipios -- a small C++ library that provides easy access to .zip files.
3 
4  Copyright (C) 2000-2007 Thomas Sondergaard
5  Copyright (C) 2015-2019 Made to Order Software Corporation
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
28 #include "zipinputstreambuf.hpp"
29 
31 
32 
33 namespace zipios
34 {
35 
36 
55 ZipInputStreambuf::ZipInputStreambuf(std::streambuf *inbuf, offset_t start_pos)
56  : InflateInputStreambuf(inbuf, start_pos)
57  //, m_current_entry() -- auto-init
58  //, m_remain(0) -- auto-init
59 {
60  // read the zip local header
61  std::istream is(m_inbuf); // istream does not destroy the streambuf.
62  is.exceptions(std::ios::eofbit | std::ios::failbit | std::ios::badbit);
63 
64  // if the read fails in any way it will throw
67  {
68  throw FileCollectionException("Trailing data descriptor in zip file not supported");
69  }
70 
71  switch(m_current_entry.getMethod())
72  {
73  case StorageMethod::DEFLATED:
74  reset() ; // reset inflatestream data structures
75 //std::cerr << "deflated" << std::endl;
76  break;
77 
78  case StorageMethod::STORED:
80  // Force underflow on first read:
81  setg(&m_outvec[0], &m_outvec[0] + getBufferSize(), &m_outvec[0] + getBufferSize());
82 //std::cerr << "stored" << std::endl;
83  break;
84 
85  default:
86  // file not supported... sorry!
87  throw FileCollectionException("Unsupported compression format");
88 
89  }
90 }
91 
92 
109 {
110 }
111 
112 
122 std::streambuf::int_type ZipInputStreambuf::underflow()
123 {
124  switch(m_current_entry.getMethod())
125  {
126  case StorageMethod::DEFLATED:
127  // inflate class takes care of it in this case
129 
130  case StorageMethod::STORED:
131  {
132  // Ok, we are STORED, so we handle it ourselves.
133  offset_t const num_b(std::min(m_remain, static_cast<offset_t>(getBufferSize())));
134  std::streamsize const g(m_inbuf->sgetn(&m_outvec[0], num_b));
135  setg(&m_outvec[0], &m_outvec[0], &m_outvec[0] + g);
136  m_remain -= g;
137  if(g > 0)
138  {
139  // we got some data, return it
140  return traits_type::to_int_type(*gptr());
141  }
142 
143  // documentation says to return EOF if no data available
144  return traits_type::eof();
145  }
146 
147  default:
148  // This should NEVER be reached or the constructor let something
149  // go through that should not have gone through
150  throw std::logic_error("ZipInputStreambuf::underflow(): unknown storage method"); // LCOV_EXCL_LINE
151 
152  }
153 }
154 
155 
156 } // namespace
157 
158 // Local Variables:
159 // mode: cpp
160 // indent-tabs-mode: nil
161 // c-basic-offset: 4
162 // tab-width: 4
163 // End:
164 
165 // vim: ts=4 sw=4 et
zipios::InflateInputStreambuf::m_outvec
std::vector< char > m_outvec
Definition: inflateinputstreambuf.hpp:62
zipios::FileEntry::getMethod
virtual StorageMethod getMethod() const
Return the method used to create this entry.
Definition: fileentry.cpp:281
zipios::ZipLocalEntry::hasTrailingDataDescriptor
bool hasTrailingDataDescriptor() const
Is there a trailing data descriptor?
Definition: ziplocalentry.cpp:312
zipinputstreambuf.hpp
Define the zipios::ZipInputStreambuf class.
zipiosexceptions.hpp
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
zipios::FilterInputStreambuf::m_inbuf
std::streambuf * m_inbuf
Definition: filterinputstreambuf.hpp:46
zipios::InflateInputStreambuf::underflow
virtual std::streambuf::int_type underflow() override
Called when more data is required.
Definition: inflateinputstreambuf.cpp:130
zipios::InflateInputStreambuf
A stream buffer to inflate data previous compressed with zlib.
Definition: inflateinputstreambuf.hpp:48
zipios::FileCollectionException
FileCollectionException is used to signal a FileCollection problem.
Definition: zipiosexceptions.hpp:95
zipios::ZipLocalEntry::read
virtual void read(std::istream &is) override
Read one local entry from is.
Definition: ziplocalentry.cpp:335
zipios::ZipInputStreambuf::m_remain
offset_t m_remain
Definition: zipinputstreambuf.hpp:55
zipios::FileEntry::isValid
virtual bool isValid() const
Check whether this entry is valid.
Definition: fileentry.cpp:453
zipios::ZipInputStreambuf::m_current_entry
ZipLocalEntry m_current_entry
Definition: zipinputstreambuf.hpp:54
zipios::getBufferSize
size_t getBufferSize()
Definition: zipios-config.hpp.in:62
zipios::ZipInputStreambuf::underflow
virtual std::streambuf::int_type underflow() override
Called when more data is required.
Definition: zipinputstreambuf.cpp:122
zipios::InflateInputStreambuf::reset
bool reset(offset_t stream_position=-1)
Initializes the stream buffer.
Definition: inflateinputstreambuf.cpp:215
zipios::offset_t
std::streamoff offset_t
Definition: zipios-config.hpp.in:59
zipios::FileEntry::getSize
virtual size_t getSize() const
Retrieve the size of the file when uncompressed.
Definition: fileentry.cpp:331
zipios::ZipInputStreambuf::~ZipInputStreambuf
virtual ~ZipInputStreambuf() override
Clean up a ZipInputStreambuf object.
Definition: zipinputstreambuf.cpp:108
zipios
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:36
zipios::ZipInputStreambuf::ZipInputStreambuf
ZipInputStreambuf(std::streambuf *inbuf, offset_t start_pos=-1)
Initialize a ZipInputStreambuf.
Definition: zipinputstreambuf.cpp:55