-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGZip.h
More file actions
104 lines (78 loc) · 2.27 KB
/
GZip.h
File metadata and controls
104 lines (78 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#ifndef GZIP_H
#define GZIP_H
#include <cstdint>
#include "zlib/zlib.h"
class GZipUnpack
{
public:
GZipUnpack() : m_strm({nullptr,0,0,nullptr,0,0,nullptr,nullptr,nullptr,nullptr,nullptr,0,0,0})
{
}
virtual ~GZipUnpack() {
inflateEnd(&m_strm);
}
int Init()
{
/* allocate inflate state */
const int iRet = inflateInit2(&m_strm, 15 + 32);
inflateReset(&m_strm);
return iRet;
}
void InitBuffer(unsigned char* const pIn, uint32_t nInCount)
{
m_strm.avail_in = nInCount;
m_strm.next_in = pIn;
}
int Deflate(unsigned char* pOut, uint32_t* pnOutCount)
{
m_strm.avail_out = *pnOutCount;
m_strm.next_out = pOut;
const int iRet = inflate(&m_strm, Z_NO_FLUSH);
*pnOutCount -= m_strm.avail_out;
return iRet;
}
private:
z_stream m_strm;
};
class GZipPack
{
public:
GZipPack() : m_strm({nullptr,0,0,nullptr,0,0,nullptr,nullptr,nullptr,nullptr,nullptr,0,0,0})
{
}
virtual ~GZipPack() {
deflateEnd(&m_strm);
}
int Init(bool bUseDeflate = false)
{
/* allocate inflate state */
const int iRet = deflateInit2(&m_strm, Z_BEST_SPEED/*Z_DEFAULT_COMPRESSION*//*Z_BEST_COMPRESSION*/, Z_DEFLATED, 15 + (bUseDeflate == false ? 16 : 0), 9, Z_DEFAULT_STRATEGY);
//deflateReset(&m_strm);
//int iRet = deflateInit(&m_strm, Z_BEST_COMPRESSION);
return iRet;
}
void InitBuffer(const uint8_t* pIn, uint32_t nInCount)
{
m_strm.avail_in = nInCount;
m_strm.next_in = reinterpret_cast<const Bytef*>(pIn);
}
int Inflate(unsigned char* pOut, size_t* pnOutCount, int nFlush)
{
m_strm.avail_out = static_cast<uint32_t>(*pnOutCount);
m_strm.next_out = pOut;
const int iRet = deflate(&m_strm, nFlush);
*pnOutCount = m_strm.avail_out;
return iRet;
}
private:
z_stream m_strm;
};
#endif // GZIP_H