Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
cheprep::DeflateOutputStreamBuffer Class Reference

#include <DeflateOutputStreamBuffer.h>

+ Inheritance diagram for cheprep::DeflateOutputStreamBuffer:

Public Member Functions

 DeflateOutputStreamBuffer (std::streambuf *buffer)
 
void init (bool compress)
 
void finish ()
 
virtual ~DeflateOutputStreamBuffer ()
 

Protected Member Functions

int overflow (int c=EOF)
 
bool flushOut ()
 
void putUI (unsigned int ui)
 
void putUS (unsigned short us)
 
void putUB (unsigned char ub)
 
void putS (const std::string s)
 
std::streampos pos ()
 
unsigned int getSize ()
 
unsigned int getCRC ()
 

Detailed Description

Definition at line 19 of file DeflateOutputStreamBuffer.h.

Constructor & Destructor Documentation

◆ DeflateOutputStreamBuffer()

cheprep::DeflateOutputStreamBuffer::DeflateOutputStreamBuffer ( std::streambuf *  buffer)

Definition at line 79 of file DeflateOutputStreamBuffer.cc.

80 : buffer(aBuffer)
81 , crc(0)
82 , size(0)
83#ifndef CHEPREP_NO_ZLIB
84 , zStreamOpen(false)
85 , in(inSize)
86 , out(outSize)
87#endif // CHEPREP_NO_ZLIB
88 {
89
90#ifndef CHEPREP_NO_ZLIB
91 zStream.zalloc = Z_NULL;
92 zStream.zfree = Z_NULL;
93 zStream.opaque = Z_NULL;
94 zStream.msg = 0;
95 zStream.next_in = 0;
96 zStream.next_out = 0;
97 zStream.state = 0;
98#endif // CHEPREP_NO_ZLIB
99 }
alloc_func zalloc
Definition: zlib.h:98
z_const Bytef * next_in
Definition: zlib.h:87
free_func zfree
Definition: zlib.h:99
voidpf opaque
Definition: zlib.h:100
struct internal_state FAR * state
Definition: zlib.h:96
Bytef * next_out
Definition: zlib.h:91
z_const char * msg
Definition: zlib.h:95
#define Z_NULL
Definition: zlib.h:212

◆ ~DeflateOutputStreamBuffer()

cheprep::DeflateOutputStreamBuffer::~DeflateOutputStreamBuffer ( )
virtual

Definition at line 161 of file DeflateOutputStreamBuffer.cc.

161 {
162 }

Member Function Documentation

◆ finish()

void cheprep::DeflateOutputStreamBuffer::finish ( )

Definition at line 128 of file DeflateOutputStreamBuffer.cc.

128 {
129
130#ifndef CHEPREP_NO_ZLIB
131 if (zStreamOpen) {
132
133 overflow() ;
134
135 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
136 zStream.avail_out = outSize;
137
138 int err = Z_OK ;
139 while ( err == Z_OK ) {
140 if (zStream.avail_out == 0) {
141 flushOut();
142 }
143 err = deflate(&zStream, Z_FINISH);
144 }
145
146 flushOut() ;
147
148 if (err != Z_STREAM_END) {
149 cerr << "ERROR: deflation failed" << endl;
150 }
151
152 if (deflateEnd(&zStream) != Z_OK) {
153 cerr << "ERROR: deflateEnd failed" << endl;
154 }
155
156 zStreamOpen = false ;
157 }
158#endif // CHEPREP_NO_ZLIB
159 }
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1075
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:762
uInt avail_out
Definition: zlib.h:92
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_FINISH
Definition: zlib.h:172
#define Z_OK
Definition: zlib.h:177

Referenced by cheprep::GZIPOutputStreamBuffer::close(), and cheprep::ZipOutputStreamBuffer::closeEntry().

◆ flushOut()

bool cheprep::DeflateOutputStreamBuffer::flushOut ( )
protected

Definition at line 233 of file DeflateOutputStreamBuffer.cc.

233 {
234 int deflatedCount = outSize - zStream.avail_out;
235 int byteCount = buffer->sputn(&(out[0]), deflatedCount);
236
237 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
238 zStream.avail_out = outSize ;
239
240 return deflatedCount == byteCount ;
241 }

Referenced by finish(), and overflow().

◆ getCRC()

unsigned int cheprep::DeflateOutputStreamBuffer::getCRC ( )
inlineprotected

Definition at line 71 of file DeflateOutputStreamBuffer.h.

71 {
72 return crc;
73 }

Referenced by cheprep::ZipOutputStreamBuffer::closeEntry().

◆ getSize()

unsigned int cheprep::DeflateOutputStreamBuffer::getSize ( )
inlineprotected

Definition at line 67 of file DeflateOutputStreamBuffer.h.

67 {
68 return size;
69 }

Referenced by cheprep::ZipOutputStreamBuffer::closeEntry().

◆ init()

void cheprep::DeflateOutputStreamBuffer::init ( bool  compress)

Definition at line 102 of file DeflateOutputStreamBuffer.cc.

102 {
103
104 if (compress) {
105 if (zStreamOpen) return;
106
107 zStream.next_in = reinterpret_cast<unsigned char *>(&(in[0]));
108 zStream.avail_in = 0 ;
109
110 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
111 zStream.avail_out = out.size();
112
113 if (deflateInit2(&zStream, 6, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY ) != Z_OK) {
114 cerr << "ERROR: deflateInit2 failed" << endl;
115 } else {
116 zStreamOpen = true;
117 setp(&(in[0]), &(in[0])+inSize);
118 }
119 }
120#else
121 void DeflateOutputStreamBuffer::init(bool /*compress*/) {
122#endif // CHEPREP_NO_ZLIB
123
124 crc = 0;
125 size = 0;
126 }
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:67
uInt avail_in
Definition: zlib.h:88
#define Z_DEFLATED
Definition: zlib.h:209
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1797

Referenced by cheprep::GZIPOutputStreamBuffer::GZIPOutputStreamBuffer(), init(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ overflow()

int cheprep::DeflateOutputStreamBuffer::overflow ( int  c = EOF)
protected

Definition at line 171 of file DeflateOutputStreamBuffer.cc.

171 {
172
173#ifndef CHEPREP_NO_ZLIB
174 if (zStreamOpen) {
175 zStream.avail_in = pptr() - pbase() ;
176 zStream.next_in = reinterpret_cast<unsigned char *>(&(in[0]));
177
178 int len = zStream.avail_in;
179 unsigned char* buf = zStream.next_in;
180
181 crc = crc ^ 0xffffffffUL;
182 while (len >= 8) {
183 DO8;
184 len -= 8;
185 }
186 if (len) do {
187 DO1;
188 } while (--len);
189 crc = crc ^ 0xffffffffUL;
190
191 size += zStream.avail_in;
192
193 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
194 zStream.avail_out = outSize ;
195
196 int err = Z_OK ;
197 while ((zStream.avail_in > 0 || zStream.avail_out == 0) && err == Z_OK) {
198 if (zStream.avail_out == 0 ) {
199 flushOut();
200 }
201 err = deflate(&zStream, Z_NO_FLUSH);
202 }
203
204 flushOut();
205
206 setp(&(in[0]), &(in[0]) + inSize);
207
208 if ((err != Z_OK) && (err != Z_STREAM_END)) {
209 cerr << "ERROR: deflation failed" << endl;
210 return EOF ;
211 }
212
213 if ( c != EOF ) {
214 *pptr() = c ;
215 pbump(1);
216 }
217
218 return 0 ;
219 } else {
220#endif // CHEPREP_NO_ZLIB
221 crc = crc ^ 0xffffffffUL;
222 crc = crctable[(crc ^ c) & 0xff] ^ (crc >> 8);
223 crc = crc ^ 0xffffffffUL;
224 size++;
225 return buffer->sputc((char)c);
226#ifndef CHEPREP_NO_ZLIB
227 }
228#endif // CHEPREP_NO_ZLIB
229 }
#define DO1(buf, i)
Definition: adler32.c:15
#define DO8(buf, i)
Definition: adler32.c:18
#define Z_NO_FLUSH
Definition: zlib.h:168

Referenced by finish(), cheprep::ZipOutputStreamBuffer::overflow(), and cheprep::GZIPOutputStreamBuffer::overflow().

◆ pos()

std::streampos cheprep::DeflateOutputStreamBuffer::pos ( )
inlineprotected

Definition at line 62 of file DeflateOutputStreamBuffer.h.

62 {
63 std::ostream os(buffer);
64 return os.tellp();
65 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), cheprep::ZipOutputStreamBuffer::closeEntry(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ putS()

void cheprep::DeflateOutputStreamBuffer::putS ( const std::string  s)
inlineprotected

Definition at line 58 of file DeflateOutputStreamBuffer.h.

58 {
59 buffer->sputn(s.c_str(), s.length());
60 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ putUB()

void cheprep::DeflateOutputStreamBuffer::putUB ( unsigned char  ub)
inlineprotected

Definition at line 54 of file DeflateOutputStreamBuffer.h.

54 {
55 buffer->sputc(ub);
56 }

◆ putUI()

void cheprep::DeflateOutputStreamBuffer::putUI ( unsigned int  ui)
inlineprotected

Definition at line 38 of file DeflateOutputStreamBuffer.h.

38 {
39 unsigned char* ucp = reinterpret_cast<unsigned char *>(&ui);
40 unsigned int i = (static_cast<unsigned int>(ucp[ 3 ]) << 24) +
41 (static_cast<unsigned int>(ucp[ 2 ]) << 16) +
42 (static_cast<unsigned int>(ucp[ 1 ]) << 8 ) +
43 (static_cast<unsigned int>(ucp[ 0 ]));
44 buffer->sputn(reinterpret_cast<char *>(&i), sizeof(unsigned int));
45 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), cheprep::ZipOutputStreamBuffer::closeEntry(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ putUS()

void cheprep::DeflateOutputStreamBuffer::putUS ( unsigned short  us)
inlineprotected

Definition at line 47 of file DeflateOutputStreamBuffer.h.

47 {
48 unsigned char* ucp = reinterpret_cast<unsigned char *>(&us);
49 unsigned short s = (static_cast<unsigned short>(ucp[ 1 ]) << 8 ) +
50 (static_cast<unsigned short>(ucp[ 0 ]));
51 buffer->sputn(reinterpret_cast<char *>(&s), sizeof(unsigned short));
52 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), and cheprep::ZipOutputStreamBuffer::putNextEntry().


The documentation for this class was generated from the following files: