]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/src/zipinputstreambuf.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / src / zipinputstreambuf.cpp
1
2 #include "zipios++/zipios-config.h"
3
4 #include <algorithm>
5 #include "zipios++/meta-iostreams.h"
6
7 #include <zlib.h>
8
9 #include "zipios++/zipinputstreambuf.h"
10 #include "zipios_common.h"
11
12 namespace zipios {
13
14 using std::ios ;
15 using std::cerr ;
16 using std::endl ;
17
18 ZipInputStreambuf::ZipInputStreambuf( streambuf *inbuf, int s_pos, bool del_inbuf ) 
19   : InflateInputStreambuf( inbuf, s_pos, del_inbuf ),
20     _open_entry( false                   ) 
21 {}
22
23 void ZipInputStreambuf::closeEntry() {
24   if ( ! _open_entry )
25     return ;
26   
27   // check if we're positioned correctly, otherwise position us correctly
28   int position = static_cast< int >( _inbuf->pubseekoff(0, ios::cur, 
29                                     ios::in) );
30   if ( position != _data_start + static_cast< int >( _curr_entry.getCompressedSize() ) )
31     _inbuf->pubseekoff(_data_start + _curr_entry.getCompressedSize(), 
32                        ios::beg, ios::in) ;
33
34 }
35
36 void ZipInputStreambuf::close() {
37 }
38
39 ConstEntryPointer ZipInputStreambuf::getNextEntry() {
40   if ( _open_entry )
41     closeEntry() ;
42
43   // read the zip local header
44   istream is( _inbuf ) ; // istream does not destroy the streambuf.
45   is.exceptions( ios::eofbit | ios::failbit | ios::badbit );
46
47   try {
48     is >> _curr_entry ;
49     if ( _curr_entry.isValid() ) {
50       _data_start = static_cast< int >( _inbuf->pubseekoff(0, ios::cur, ios::in) );
51       if ( _curr_entry.getMethod() == DEFLATED ) {
52         _open_entry = true ;
53         reset() ; // reset inflatestream data structures 
54         // cerr << "deflated" << endl ;
55       } else if ( _curr_entry.getMethod() == STORED ) {
56         _open_entry = true ;
57         _remain = _curr_entry.getSize() ;
58         // Force underflow on first read:
59         setg( &( _outvec[ 0 ] ),
60               &( _outvec[ 0 ] ) + _outvecsize,
61               &( _outvec[ 0 ] ) + _outvecsize );
62         // cerr << "stored" << endl ;
63       } else {
64         _open_entry = false ; // Unsupported compression format.
65         throw FCollException( "Unsupported compression format" ) ;
66       }
67     }
68   } catch (...) {
69     _open_entry = false ;
70   }
71
72   if ( _curr_entry.isValid() && _curr_entry.trailingDataDescriptor() )
73     throw FCollException( "Trailing data descriptor in zip file not supported" ) ; 
74   return new ZipLocalEntry( _curr_entry ) ;
75 }
76
77
78 ZipInputStreambuf::~ZipInputStreambuf() {
79 }
80
81
82 int ZipInputStreambuf::underflow() {
83   if ( ! _open_entry )
84     return EOF ; // traits_type::eof() 
85   if ( _curr_entry.getMethod() == DEFLATED )
86     return InflateInputStreambuf::underflow() ;
87
88   // Ok, we're are stored, so we handle it ourselves.
89   int num_b = min( _remain, _outvecsize ) ;
90   int g = static_cast< int >( _inbuf->sgetn( &(_outvec[ 0 ] ) , num_b ) ) ;
91   setg( &( _outvec[ 0 ] ),
92         &( _outvec[ 0 ] ),
93         &( _outvec[ 0 ] ) + g ) ;
94   _remain -= g ;
95   if ( g > 0 )
96     return static_cast< unsigned char >( *gptr() ) ;
97   else
98     return EOF ; // traits_type::eof() 
99 }
100
101
102 // FIXME: We need to check somew
103 //  
104 //    // gp_bitfield bit 3 is one, if the length of the zip entry
105 //    // is stored in a trailer.
106 //    if ( is->good  && ( _curr_entry.gp_bitfield & 4 ) != 1 )
107 //      return true ;
108 //    else {
109 //      is->clear() ;
110 //      return false ;
111 //    }
112
113
114 } // namespace
115
116 /** \file
117     Implementation of ZipInputStreambuf.
118 */
119
120 /*
121   Zipios++ - a small C++ library that provides easy access to .zip files.
122   Copyright (C) 2000  Thomas Søndergaard
123   
124   This library is free software; you can redistribute it and/or
125   modify it under the terms of the GNU Lesser General Public
126   License as published by the Free Software Foundation; either
127   version 2 of the License, or (at your option) any later version.
128   
129   This library is distributed in the hope that it will be useful,
130   but WITHOUT ANY WARRANTY; without even the implied warranty of
131   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
132   Lesser General Public License for more details.
133   
134   You should have received a copy of the GNU Lesser General Public
135   License along with this library; if not, write to the Free Software
136   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
137 */