1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: Implements layer to compress/decompress Zlib compressed data
7
////////////////////////////////////////////////////////////////////////////
9
#include "IOLayerZlib.h"
14
IOLayerZlib::IOLayerZlib()
18
m_bInitialised = false;
19
m_nPackLevel = Z_DEFAULT_COMPRESSION;
20
memset(&m_stream, 0, sizeof(z_stream));
23
IOLayerZlib::~IOLayerZlib()
27
void IOLayerZlib::Initialize(int nMode)
29
IOLayerBase::Initialize(nMode);
31
//stream object initialization
34
if(MODE_WRITE == m_nMode)
35
m_err = deflateInit(&m_stream, m_nPackLevel); //compression
37
m_err = inflateInit(&m_stream); //decompression
39
ASSERT(m_err == Z_OK); // check state
40
m_stream.avail_in = 0;
41
m_stream.avail_out = m_nOutLength;
42
m_stream.next_out = m_szOutBuffer;
44
m_bInitialised = true;
48
int IOLayerZlib::Process(char *szBuffer, int nLen)
52
//if stream length is known in advance, process only up to X bytes
53
if(m_nTotalInBytes >= 0)
55
if((m_nProcessedInBytes + nLen) > m_nTotalInBytes){
56
TRACE(" IOLayerZlib::Process Reached end of stream!!\n");
57
nLen = m_nTotalInBytes - m_nProcessedInBytes;
61
if(nLen <= 0) return 0;
64
if(MODE_WRITE == m_nMode)
65
nBytes = Write(szBuffer, nLen);
67
nBytes = Read(szBuffer, nLen);
69
//store output size for this operation
70
m_nLastOutBytes = nBytes;
71
m_nProcessedInBytes += nLen;
72
m_nProcessedOutBytes += nBytes;
77
int IOLayerZlib::Read(char *szBuffer, int nLen)
80
TRACE("IOLayerZlib::Read (size %d bytes)\n", nLen);
85
ASSERT(m_stream.avail_in == 0);
86
m_stream.next_in = (unsigned char *)szBuffer;
87
m_stream.avail_in = nLen;
91
bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
96
int IOLayerZlib::Write(char *szBuffer, int nLen)
99
TRACE("IOLayerZlib::Write (%d bytes)\n", nLen);
104
ASSERT(m_stream.avail_in == 0);
105
m_stream.next_in = (unsigned char *)szBuffer;
106
m_stream.avail_in = nLen;
109
bool bSucess = false;
110
bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
111
// TOFIX err handling
115
int IOLayerZlib::Finalize()
118
bool bSucess = false;
119
TRACE("IOLayerZlib::Finalize start\n");
120
bSucess = DoProcessing(Z_FINISH, nTotalBytes);
121
TRACE("IOLayerZlib::Finalize (%d bytes)\n", nTotalBytes);
123
// cleanup stream memory
124
if(MODE_WRITE == m_nMode)
125
deflateEnd(&m_stream);
127
inflateEnd(&m_stream);
129
//call next layer attached?
130
if(NULL != m_pNextLayer)
131
return m_pNextLayer->Finalize();
136
bool IOLayerZlib::DoProcessing(int nFlag, int &nTotal)
139
int nTotalBytesExt = 0;
145
m_stream.avail_out = m_nOutLength;
146
m_stream.next_out = m_szOutBuffer;
148
if(MODE_WRITE == m_nMode)
149
m_err = deflate(&m_stream, nFlag);
151
m_err = inflate(&m_stream, nFlag);
153
unsigned int nBytes = m_nOutLength - m_stream.avail_out;
154
TRACE(" IOLayerZlib::DoProcessing zlib output: %d bytes, zlib stream state: %s\n", nBytes, m_stream.msg);
156
nTotalBytes += nBytes;
157
m_nLastOutBytes = nBytes;
158
m_nProcessedOutBytes += nBytes;
160
//call next layer attached?
161
if(NULL != m_pNextLayer && nBytes > 0)
162
nTotalBytesExt += m_pNextLayer->Process((char *)m_szOutBuffer, nBytes);
164
//control looping here
167
if(nFlag == Z_NO_FLUSH && m_stream.avail_in == 0)
171
//TRACE(" IOLayerZlib::DoProcessing end (zlib unprocessed: %d bytes)\n", m_stream.avail_in);
172
nTotal = nTotalBytes;
1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: Implements layer to compress/decompress Zlib compressed data
7
////////////////////////////////////////////////////////////////////////////
9
#include "IOLayerZlib.h"
14
IOLayerZlib::IOLayerZlib()
18
m_bInitialised = false;
19
m_nPackLevel = Z_DEFAULT_COMPRESSION;
20
memset(&m_stream, 0, sizeof(z_stream));
23
IOLayerZlib::~IOLayerZlib()
27
void IOLayerZlib::Initialize(int nMode)
29
IOLayerBase::Initialize(nMode);
31
//stream object initialization
34
if(MODE_WRITE == m_nMode)
35
m_err = deflateInit(&m_stream, m_nPackLevel); //compression
37
m_err = inflateInit(&m_stream); //decompression
39
ASSERT(m_err == Z_OK); // check state
40
m_stream.avail_in = 0;
41
m_stream.avail_out = m_nOutLength;
42
m_stream.next_out = m_szOutBuffer;
44
m_bInitialised = true;
48
int IOLayerZlib::Process(char *szBuffer, int nLen)
52
//if stream length is known in advance, process only up to X bytes
53
if(m_nTotalInBytes >= 0)
55
if((m_nProcessedInBytes + nLen) > m_nTotalInBytes){
56
TRACE(" IOLayerZlib::Process Reached end of stream!!\n");
57
nLen = m_nTotalInBytes - m_nProcessedInBytes;
61
if(nLen <= 0) return 0;
64
if(MODE_WRITE == m_nMode)
65
nBytes = Write(szBuffer, nLen);
67
nBytes = Read(szBuffer, nLen);
69
//store output size for this operation
70
m_nLastOutBytes = nBytes;
71
m_nProcessedInBytes += nLen;
72
m_nProcessedOutBytes += nBytes;
77
int IOLayerZlib::Read(char *szBuffer, int nLen)
80
TRACE("IOLayerZlib::Read (size %d bytes)\n", nLen);
85
ASSERT(m_stream.avail_in == 0);
86
m_stream.next_in = (unsigned char *)szBuffer;
87
m_stream.avail_in = nLen;
91
bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
96
int IOLayerZlib::Write(char *szBuffer, int nLen)
99
TRACE("IOLayerZlib::Write (%d bytes)\n", nLen);
104
ASSERT(m_stream.avail_in == 0);
105
m_stream.next_in = (unsigned char *)szBuffer;
106
m_stream.avail_in = nLen;
109
bool bSucess = false;
110
bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
111
// TOFIX err handling
115
int IOLayerZlib::Finalize()
118
bool bSucess = false;
119
TRACE("IOLayerZlib::Finalize start\n");
120
bSucess = DoProcessing(Z_FINISH, nTotalBytes);
121
TRACE("IOLayerZlib::Finalize (%d bytes)\n", nTotalBytes);
123
// cleanup stream memory
124
if(MODE_WRITE == m_nMode)
125
deflateEnd(&m_stream);
127
inflateEnd(&m_stream);
129
//call next layer attached?
130
if(NULL != m_pNextLayer)
131
return m_pNextLayer->Finalize();
136
bool IOLayerZlib::DoProcessing(int nFlag, int &nTotal)
139
int nTotalBytesExt = 0;
145
m_stream.avail_out = m_nOutLength;
146
m_stream.next_out = m_szOutBuffer;
148
if(MODE_WRITE == m_nMode)
149
m_err = deflate(&m_stream, nFlag);
151
m_err = inflate(&m_stream, nFlag);
153
unsigned int nBytes = m_nOutLength - m_stream.avail_out;
154
TRACE(" IOLayerZlib::DoProcessing zlib output: %d bytes, zlib stream state: %s\n", nBytes, m_stream.msg);
156
nTotalBytes += nBytes;
157
m_nLastOutBytes = nBytes;
158
m_nProcessedOutBytes += nBytes;
160
//call next layer attached?
161
if(NULL != m_pNextLayer && nBytes > 0)
162
nTotalBytesExt += m_pNextLayer->Process((char *)m_szOutBuffer, nBytes);
164
//control looping here
167
if(nFlag == Z_NO_FLUSH && m_stream.avail_in == 0)
171
//TRACE(" IOLayerZlib::DoProcessing end (zlib unprocessed: %d bytes)\n", m_stream.avail_in);
172
nTotal = nTotalBytes;