]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/src/gzipoutputstreambuf.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / src / gzipoutputstreambuf.cpp
1
2 #include <time.h>
3
4 #include "zipios++/zipios-config.h"
5
6 #include <algorithm>
7 #include "zipios++/meta-iostreams.h"
8
9 #include <zlib.h>
10
11 #include "zipios++/gzipoutputstreambuf.h"
12
13 namespace zipios {
14
15 using std::ios ;
16 using std::cerr ;
17 using std::endl ;
18
19 GZIPOutputStreambuf::GZIPOutputStreambuf( streambuf *outbuf, bool del_outbuf )
20     : DeflateOutputStreambuf( outbuf, true, del_outbuf ),
21       _open      ( false ) 
22 {
23 }
24
25 void GZIPOutputStreambuf::setFilename( const string &filename ) {
26     _filename = filename ;
27 }
28
29 void GZIPOutputStreambuf::setComment( const string &comment ) {
30     _comment = comment ;
31 }
32
33 void GZIPOutputStreambuf::close() {
34     finish() ;
35 }
36
37 void GZIPOutputStreambuf::finish() {
38     if( ! _open )
39         return ;
40     
41     closeStream();  
42     writeTrailer();
43   
44     _open = false ;
45 }
46
47 GZIPOutputStreambuf::~GZIPOutputStreambuf() {
48     finish() ;
49 }
50
51 int GZIPOutputStreambuf::overflow( int c ) {
52     if (!_open) {
53         writeHeader();
54         _open = true;
55     }
56     return DeflateOutputStreambuf::overflow( c ) ;
57 }
58
59 int GZIPOutputStreambuf::sync() {
60     return DeflateOutputStreambuf::sync() ;
61 }
62
63 void GZIPOutputStreambuf::writeHeader() {
64     unsigned char flg = 0x00;
65     flg |= (_filename == "") ? 0x00 : 0x08;
66     flg |= (_comment  == "") ? 0x00 : 0x10;
67
68     ostream os( _outbuf ) ;
69     os << (unsigned char)0x1f;  // Magic #
70     os << (unsigned char)0x8b;  // Magic #
71     os << (unsigned char)0x08;  // Deflater.DEFLATED
72     os << flg;                  // FLG
73     os << (unsigned char)0x00;  // MTIME
74     os << (unsigned char)0x00;  // MTIME
75     os << (unsigned char)0x00;  // MTIME
76     os << (unsigned char)0x00;  // MTIME
77     os << (unsigned char)0x00;  // XFLG
78     os << (unsigned char)0x00;  // OS
79         
80     if (_filename != "") {
81         os << _filename.c_str();// Filename
82         os << (unsigned char)0x00;
83     }
84     
85     if (_comment != "") {
86         os << _comment.c_str(); // Comment
87         os << (unsigned char)0x00;
88     }
89 }
90
91 void GZIPOutputStreambuf::writeTrailer() {
92     writeInt(getCrc32());
93     writeInt(getCount());
94 }
95
96 void GZIPOutputStreambuf::writeInt(uint32 i) {
97     ostream os( _outbuf ) ;
98     os << (unsigned char)( i        & 0xFF);
99     os << (unsigned char)((i >>  8) & 0xFF);
100     os << (unsigned char)((i >> 16) & 0xFF);
101     os << (unsigned char)((i >> 24) & 0xFF);
102 }
103
104 } // namespace
105
106 /** \file
107     Implementation of GZIPOutputStreambuf.
108 */
109
110 /*
111   Zipios++ - a small C++ library that provides easy access to .zip files.
112   Copyright (C) 2000  Thomas Søndergaard
113
114   This library is free software; you can redistribute it and/or
115   modify it under the terms of the GNU Lesser General Public
116   License as published by the Free Software Foundation; either
117   version 2 of the License, or (at your option) any later version.
118
119   This library is distributed in the hope that it will be useful,
120   but WITHOUT ANY WARRANTY; without even the implied warranty of
121   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
122   Lesser General Public License for more details.
123
124   You should have received a copy of the GNU Lesser General Public
125   License along with this library; if not, write to the Free Software
126   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
127 */