3 #include "cheprep/DeflateOutputStreamBuffer.h"
13 unsigned long DeflateOutputStreamBuffer::crctable[] = {
14 0x00000000
L, 0x77073096
L, 0xEE0E612C
L, 0x990951BA
L,
15 0x076DC419
L, 0x706AF48F
L, 0xE963A535
L, 0x9E6495A3
L,
16 0x0EDB8832
L, 0x79DCB8A4
L, 0xE0D5E91E
L, 0x97D2D988
L,
17 0x09B64C2B
L, 0x7EB17CBD
L, 0xE7B82D07
L, 0x90BF1D91
L,
18 0x1DB71064
L, 0x6AB020F2
L, 0xF3B97148
L, 0x84BE41DE
L,
19 0x1ADAD47D
L, 0x6DDDE4EB
L, 0xF4D4B551
L, 0x83D385C7
L,
20 0x136C9856
L, 0x646BA8C0
L, 0xFD62F97A
L, 0x8A65C9EC
L,
21 0x14015C4F
L, 0x63066CD9
L, 0xFA0F3D63
L, 0x8D080DF5
L,
22 0x3B6E20C8
L, 0x4C69105E
L, 0xD56041E4
L, 0xA2677172
L,
23 0x3C03E4D1
L, 0x4B04D447
L, 0xD20D85FD
L, 0xA50AB56B
L,
24 0x35B5A8FA
L, 0x42B2986C
L, 0xDBBBC9D6
L, 0xACBCF940
L,
25 0x32D86CE3
L, 0x45DF5C75
L, 0xDCD60DCF
L, 0xABD13D59
L,
26 0x26D930AC
L, 0x51DE003A
L, 0xC8D75180
L, 0xBFD06116
L,
27 0x21B4F4B5
L, 0x56B3C423
L, 0xCFBA9599
L, 0xB8BDA50F
L,
28 0x2802B89E
L, 0x5F058808
L, 0xC60CD9B2
L, 0xB10BE924
L,
29 0x2F6F7C87
L, 0x58684C11
L, 0xC1611DAB
L, 0xB6662D3D
L,
30 0x76DC4190
L, 0x01DB7106
L, 0x98D220BC
L, 0xEFD5102A
L,
31 0x71B18589
L, 0x06B6B51F
L, 0x9FBFE4A5
L, 0xE8B8D433
L,
32 0x7807C9A2
L, 0x0F00F934
L, 0x9609A88E
L, 0xE10E9818
L,
33 0x7F6A0DBB
L, 0x086D3D2D
L, 0x91646C97
L, 0xE6635C01
L,
34 0x6B6B51F4
L, 0x1C6C6162
L, 0x856530D8
L, 0xF262004E
L,
35 0x6C0695ED
L, 0x1B01A57B
L, 0x8208F4C1
L, 0xF50FC457
L,
36 0x65B0D9C6
L, 0x12B7E950
L, 0x8BBEB8EA
L, 0xFCB9887C
L,
37 0x62DD1DDF
L, 0x15DA2D49
L, 0x8CD37CF3
L, 0xFBD44C65
L,
38 0x4DB26158
L, 0x3AB551CE
L, 0xA3BC0074
L, 0xD4BB30E2
L,
39 0x4ADFA541
L, 0x3DD895D7
L, 0xA4D1C46D
L, 0xD3D6F4FB
L,
40 0x4369E96A
L, 0x346ED9FC
L, 0xAD678846
L, 0xDA60B8D0
L,
41 0x44042D73
L, 0x33031DE5
L, 0xAA0A4C5F
L, 0xDD0D7CC9
L,
42 0x5005713C
L, 0x270241AA
L, 0xBE0B1010
L, 0xC90C2086
L,
43 0x5768B525
L, 0x206F85B3
L, 0xB966D409
L, 0xCE61E49F
L,
44 0x5EDEF90E
L, 0x29D9C998
L, 0xB0D09822
L, 0xC7D7A8B4
L,
45 0x59B33D17
L, 0x2EB40D81
L, 0xB7BD5C3B
L, 0xC0BA6CAD
L,
46 0xEDB88320
L, 0x9ABFB3B6
L, 0x03B6E20C
L, 0x74B1D29A
L,
47 0xEAD54739
L, 0x9DD277AF
L, 0x04DB2615
L, 0x73DC1683
L,
48 0xE3630B12
L, 0x94643B84
L, 0x0D6D6A3E
L, 0x7A6A5AA8
L,
49 0xE40ECF0B
L, 0x9309FF9D
L, 0x0A00AE27
L, 0x7D079EB1
L,
50 0xF00F9344
L, 0x8708A3D2
L, 0x1E01F268
L, 0x6906C2FE
L,
51 0xF762575D
L, 0x806567CB
L, 0x196C3671
L, 0x6E6B06E7
L,
52 0xFED41B76
L, 0x89D32BE0
L, 0x10DA7A5A
L, 0x67DD4ACC
L,
53 0xF9B9DF6F
L, 0x8EBEEFF9
L, 0x17B7BE43
L, 0x60B08ED5
L,
54 0xD6D6A3E8
L, 0xA1D1937E
L, 0x38D8C2C4
L, 0x4FDFF252
L,
55 0xD1BB67F1
L, 0xA6BC5767
L, 0x3FB506DD
L, 0x48B2364B
L,
56 0xD80D2BDA
L, 0xAF0A1B4C
L, 0x36034AF6
L, 0x41047A60
L,
57 0xDF60EFC3
L, 0xA867DF55
L, 0x316E8EEF
L, 0x4669BE79
L,
58 0xCB61B38C
L, 0xBC66831A
L, 0x256FD2A0
L, 0x5268E236
L,
59 0xCC0C7795
L, 0xBB0B4703
L, 0x220216B9
L, 0x5505262F
L,
60 0xC5BA3BBE
L, 0xB2BD0B28
L, 0x2BB45A92
L, 0x5CB36A04
L,
61 0xC2D7FFA7
L, 0xB5D0CF31
L, 0x2CD99E8B
L, 0x5BDEAE1D
L,
62 0x9B64C2B0
L, 0xEC63F226
L, 0x756AA39C
L, 0x026D930A
L,
63 0x9C0906A9
L, 0xEB0E363F
L, 0x72076785
L, 0x05005713
L,
64 0x95BF4A82
L, 0xE2B87A14
L, 0x7BB12BAE
L, 0x0CB61B38
L,
65 0x92D28E9B
L, 0xE5D5BE0D
L, 0x7CDCEFB7
L, 0x0BDBDF21
L,
66 0x86D3D2D4
L, 0xF1D4E242
L, 0x68DDB3F8
L, 0x1FDA836E
L,
67 0x81BE16CD
L, 0xF6B9265B
L, 0x6FB077E1
L, 0x18B74777
L,
68 0x88085AE6
L, 0xFF0F6A70
L, 0x66063BCA
L, 0x11010B5C
L,
69 0x8F659EFF
L, 0xF862AE69
L, 0x616BFFD3
L, 0x166CCF45
L,
70 0xA00AE278
L, 0xD70DD2EE
L, 0x4E048354
L, 0x3903B3C2
L,
71 0xA7672661
L, 0xD06016F7
L, 0x4969474D
L, 0x3E6E77DB
L,
72 0xAED16A4A
L, 0xD9D65ADC
L, 0x40DF0B66
L, 0x37D83BF0
L,
73 0xA9BCAE53
L, 0xDEBB9EC5
L, 0x47B2CF7F
L, 0x30B5FFE9
L,
74 0xBDBDF21C
L, 0xCABAC28A
L, 0x53B39330
L, 0x24B4A3A6
L,
75 0xBAD03605
L, 0xCDD70693
L, 0x54DE5729
L, 0x23D967BF
L,
76 0xB3667A2E
L, 0xC4614AB8
L, 0x5D681B02
L, 0x2A6F2B94
L,
77 0xB40BBE37
L, 0xC30C8EA1
L, 0x5A05DF1B
L, 0x2D02EF8DL
80 DeflateOutputStreamBuffer::DeflateOutputStreamBuffer(streambuf *aBuffer)
84 #ifndef CHEPREP_NO_ZLIB
91 #ifndef CHEPREP_NO_ZLIB
92 zStream.zalloc = Z_NULL;
93 zStream.zfree = Z_NULL;
94 zStream.opaque = Z_NULL;
99 #endif // CHEPREP_NO_ZLIB
102 #ifndef CHEPREP_NO_ZLIB
103 void DeflateOutputStreamBuffer::init(
bool compress) {
106 if (zStreamOpen)
return;
108 zStream.next_in =
reinterpret_cast<unsigned char *
>(&(in[0]));
109 zStream.avail_in = 0 ;
111 zStream.next_out =
reinterpret_cast<unsigned char *
>(&(out[0]));
112 zStream.avail_out = out.size();
114 if (deflateInit2(&zStream, 6, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY ) != Z_OK) {
115 cerr <<
"ERROR: deflateInit2 failed" << endl;
118 setp(&(in[0]), &(in[0])+inSize);
122 void DeflateOutputStreamBuffer::init(
bool ) {
123 #endif // CHEPREP_NO_ZLIB
129 void DeflateOutputStreamBuffer::finish() {
131 #ifndef CHEPREP_NO_ZLIB
136 zStream.next_out =
reinterpret_cast<unsigned char *
>(&(out[0]));
137 zStream.avail_out = outSize;
140 while ( err == Z_OK ) {
141 if (zStream.avail_out == 0) {
144 err =
deflate(&zStream, Z_FINISH);
149 if (err != Z_STREAM_END) {
150 cerr <<
"ERROR: deflation failed" << endl;
154 cerr <<
"ERROR: deflateEnd failed" << endl;
157 zStreamOpen = false ;
159 #endif // CHEPREP_NO_ZLIB
162 DeflateOutputStreamBuffer::~DeflateOutputStreamBuffer() {
166 #ifndef CHEPREP_NO_ZLIB
167 #define DO1 crc = crctable[(crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
168 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
169 #endif // CHEPREP_NO_ZLIB
172 int DeflateOutputStreamBuffer::overflow(
int c) {
174 #ifndef CHEPREP_NO_ZLIB
176 zStream.avail_in = pptr() - pbase() ;
177 zStream.next_in =
reinterpret_cast<unsigned char *
>(&(in[0]));
179 int len = zStream.avail_in;
180 unsigned char* buf = zStream.next_in;
182 crc = crc ^ 0xffffffffUL;
190 crc = crc ^ 0xffffffffUL;
192 size += zStream.avail_in;
194 zStream.next_out =
reinterpret_cast<unsigned char *
>(&(out[0]));
195 zStream.avail_out = outSize ;
198 while ((zStream.avail_in > 0 || zStream.avail_out == 0) && err == Z_OK) {
199 if (zStream.avail_out == 0 ) {
202 err =
deflate(&zStream, Z_NO_FLUSH);
207 setp(&(in[0]), &(in[0]) + inSize);
209 if ((err != Z_OK) && (err != Z_STREAM_END)) {
210 cerr <<
"ERROR: deflation failed" << endl;
221 #endif // CHEPREP_NO_ZLIB
222 crc = crc ^ 0xffffffffUL;
223 crc = crctable[(crc ^ c) & 0xff] ^ (crc >> 8);
224 crc = crc ^ 0xffffffffUL;
226 return buffer->sputc((
char)c);
227 #ifndef CHEPREP_NO_ZLIB
229 #endif // CHEPREP_NO_ZLIB
233 #ifndef CHEPREP_NO_ZLIB
234 bool DeflateOutputStreamBuffer::flushOut() {
235 int deflatedCount = outSize - zStream.avail_out;
236 int byteCount =
buffer->sputn(&(out[0]), deflatedCount);
238 zStream.next_out =
reinterpret_cast<unsigned char *
>(&(out[0]));
239 zStream.avail_out = outSize ;
241 return deflatedCount == byteCount ;
243 #endif // CHEPREP_NO_ZLIB
int ZEXPORT deflateEnd(z_streamp strm)
int ZEXPORT deflate(z_streamp strm, int flush)
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
static constexpr double L