~ubuntu-branches/ubuntu/raring/notecase/raring

« back to all changes in this revision

Viewing changes to src/lib/IOLayerZlib.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nathan Handler
  • Date: 2008-12-21 13:09:58 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081221130958-0ri77h0x7j1dclkq
Tags: 1.9.8-0ubuntu1
New upstream release (LP: #307752)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////
2
 
// NoteCase notes manager project <http://notecase.sf.net>
3
 
//
4
 
// This code is licensed under BSD license.See "license.txt" for more details.
5
 
//
6
 
// File: Implements layer to compress/decompress Zlib compressed data
7
 
////////////////////////////////////////////////////////////////////////////
8
 
 
9
 
#include "IOLayerZlib.h"
10
 
#include <stdlib.h>
11
 
#include <memory.h>
12
 
#include "debug.h"
13
 
 
14
 
IOLayerZlib::IOLayerZlib()
15
 
{
16
 
        m_err = Z_OK;
17
 
        m_nMode = MODE_UNK;
18
 
        m_bInitialised = false;
19
 
        m_nPackLevel   = Z_DEFAULT_COMPRESSION;
20
 
        memset(&m_stream, 0, sizeof(z_stream));
21
 
}
22
 
 
23
 
IOLayerZlib::~IOLayerZlib()
24
 
{
25
 
}
26
 
 
27
 
void IOLayerZlib::Initialize(int nMode)
28
 
{
29
 
        IOLayerBase::Initialize(nMode);
30
 
 
31
 
        //stream object initialization
32
 
        if(!m_bInitialised)
33
 
        {
34
 
                if(MODE_WRITE == m_nMode)
35
 
                        m_err = deflateInit(&m_stream, m_nPackLevel);   //compression
36
 
                else
37
 
                        m_err = inflateInit(&m_stream);                         //decompression
38
 
 
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;
43
 
 
44
 
                m_bInitialised = true;
45
 
        }
46
 
}
47
 
 
48
 
int IOLayerZlib::Process(char *szBuffer, int nLen)
49
 
{
50
 
        int nBytes = 0;
51
 
 
52
 
        //if stream length is known in advance, process only up to X bytes
53
 
        if(m_nTotalInBytes >= 0)
54
 
        {
55
 
                if((m_nProcessedInBytes + nLen) > m_nTotalInBytes){
56
 
                        TRACE(" IOLayerZlib::Process Reached end of stream!!\n");
57
 
                        nLen = m_nTotalInBytes - m_nProcessedInBytes;
58
 
                }
59
 
        }
60
 
 
61
 
        if(nLen <= 0) return 0;
62
 
 
63
 
        //do the operation
64
 
        if(MODE_WRITE == m_nMode)
65
 
                nBytes = Write(szBuffer, nLen);
66
 
        else
67
 
                nBytes = Read(szBuffer, nLen);
68
 
 
69
 
        //store output size for this operation
70
 
        m_nLastOutBytes = nBytes;
71
 
        m_nProcessedInBytes += nLen;
72
 
        m_nProcessedOutBytes += nBytes;
73
 
 
74
 
        return 0;
75
 
}
76
 
 
77
 
int IOLayerZlib::Read(char *szBuffer, int nLen)
78
 
{
79
 
        //decompress content
80
 
        TRACE("IOLayerZlib::Read (size %d bytes)\n", nLen);
81
 
        ASSERT(nLen > 0);
82
 
        if(nLen < 1)
83
 
                return 0;
84
 
 
85
 
        ASSERT(m_stream.avail_in == 0);
86
 
        m_stream.next_in  = (unsigned char *)szBuffer;
87
 
        m_stream.avail_in = nLen;
88
 
 
89
 
        int nTotalBytes = 0;
90
 
        bool bSucess = false;
91
 
        bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
92
 
        // TOFIX err handling
93
 
        return nTotalBytes;
94
 
}
95
 
 
96
 
int IOLayerZlib::Write(char *szBuffer, int nLen)
97
 
{
98
 
        //compress content
99
 
        TRACE("IOLayerZlib::Write (%d bytes)\n", nLen);
100
 
        ASSERT(nLen > 0);
101
 
        if(nLen < 1)
102
 
                return 0;
103
 
 
104
 
        ASSERT(m_stream.avail_in == 0);
105
 
        m_stream.next_in  = (unsigned char *)szBuffer;
106
 
        m_stream.avail_in = nLen;
107
 
 
108
 
        int nTotalBytes = 0;
109
 
        bool bSucess = false;
110
 
        bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
111
 
        // TOFIX err handling
112
 
        return nTotalBytes;
113
 
}
114
 
 
115
 
int IOLayerZlib::Finalize()
116
 
{
117
 
        int nTotalBytes = 0;
118
 
        bool bSucess = false;
119
 
        TRACE("IOLayerZlib::Finalize start\n");
120
 
        bSucess = DoProcessing(Z_FINISH, nTotalBytes);
121
 
        TRACE("IOLayerZlib::Finalize (%d bytes)\n", nTotalBytes);
122
 
 
123
 
        // cleanup stream memory
124
 
        if(MODE_WRITE == m_nMode)
125
 
                deflateEnd(&m_stream);
126
 
        else
127
 
                inflateEnd(&m_stream);
128
 
 
129
 
        //call next layer attached?
130
 
        if(NULL != m_pNextLayer)
131
 
                return m_pNextLayer->Finalize();
132
 
 
133
 
        return nTotalBytes;
134
 
}
135
 
 
136
 
bool IOLayerZlib::DoProcessing(int nFlag, int &nTotal)
137
 
{
138
 
        int nTotalBytes = 0;
139
 
        int nTotalBytesExt = 0;
140
 
 
141
 
        m_err = Z_OK;
142
 
 
143
 
        for(;;)
144
 
        {
145
 
                m_stream.avail_out = m_nOutLength;
146
 
                m_stream.next_out = m_szOutBuffer;
147
 
 
148
 
                if(MODE_WRITE == m_nMode)
149
 
                        m_err = deflate(&m_stream, nFlag);
150
 
                else
151
 
                        m_err = inflate(&m_stream, nFlag);
152
 
 
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);
155
 
 
156
 
                nTotalBytes += nBytes;
157
 
                m_nLastOutBytes = nBytes;
158
 
                m_nProcessedOutBytes += nBytes;
159
 
 
160
 
                //call next layer attached?
161
 
                if(NULL != m_pNextLayer && nBytes > 0)
162
 
                        nTotalBytesExt += m_pNextLayer->Process((char *)m_szOutBuffer, nBytes);
163
 
 
164
 
                //control looping here
165
 
                if(m_err != Z_OK)
166
 
                        break;
167
 
                if(nFlag == Z_NO_FLUSH && m_stream.avail_in == 0)
168
 
                        break;
169
 
        }
170
 
 
171
 
        //TRACE(" IOLayerZlib::DoProcessing end (zlib unprocessed: %d bytes)\n", m_stream.avail_in);
172
 
        nTotal = nTotalBytes;
173
 
        return true;
174
 
}
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: Implements layer to compress/decompress Zlib compressed data
 
7
////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
#include "IOLayerZlib.h"
 
10
#include <stdlib.h>
 
11
#include <memory.h>
 
12
#include "debug.h"
 
13
 
 
14
IOLayerZlib::IOLayerZlib()
 
15
{
 
16
        m_err = Z_OK;
 
17
        m_nMode = MODE_UNK;
 
18
        m_bInitialised = false;
 
19
        m_nPackLevel   = Z_DEFAULT_COMPRESSION;
 
20
        memset(&m_stream, 0, sizeof(z_stream));
 
21
}
 
22
 
 
23
IOLayerZlib::~IOLayerZlib()
 
24
{
 
25
}
 
26
 
 
27
void IOLayerZlib::Initialize(int nMode)
 
28
{
 
29
        IOLayerBase::Initialize(nMode);
 
30
 
 
31
        //stream object initialization
 
32
        if(!m_bInitialised)
 
33
        {
 
34
                if(MODE_WRITE == m_nMode)
 
35
                        m_err = deflateInit(&m_stream, m_nPackLevel);   //compression
 
36
                else
 
37
                        m_err = inflateInit(&m_stream);                         //decompression
 
38
 
 
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;
 
43
 
 
44
                m_bInitialised = true;
 
45
        }
 
46
}
 
47
 
 
48
int IOLayerZlib::Process(char *szBuffer, int nLen)
 
49
{
 
50
        int nBytes = 0;
 
51
 
 
52
        //if stream length is known in advance, process only up to X bytes
 
53
        if(m_nTotalInBytes >= 0)
 
54
        {
 
55
                if((m_nProcessedInBytes + nLen) > m_nTotalInBytes){
 
56
                        TRACE(" IOLayerZlib::Process Reached end of stream!!\n");
 
57
                        nLen = m_nTotalInBytes - m_nProcessedInBytes;
 
58
                }
 
59
        }
 
60
 
 
61
        if(nLen <= 0) return 0;
 
62
 
 
63
        //do the operation
 
64
        if(MODE_WRITE == m_nMode)
 
65
                nBytes = Write(szBuffer, nLen);
 
66
        else
 
67
                nBytes = Read(szBuffer, nLen);
 
68
 
 
69
        //store output size for this operation
 
70
        m_nLastOutBytes = nBytes;
 
71
        m_nProcessedInBytes += nLen;
 
72
        m_nProcessedOutBytes += nBytes;
 
73
 
 
74
        return 0;
 
75
}
 
76
 
 
77
int IOLayerZlib::Read(char *szBuffer, int nLen)
 
78
{
 
79
        //decompress content
 
80
        TRACE("IOLayerZlib::Read (size %d bytes)\n", nLen);
 
81
        ASSERT(nLen > 0);
 
82
        if(nLen < 1)
 
83
                return 0;
 
84
 
 
85
        ASSERT(m_stream.avail_in == 0);
 
86
        m_stream.next_in  = (unsigned char *)szBuffer;
 
87
        m_stream.avail_in = nLen;
 
88
 
 
89
        int nTotalBytes = 0;
 
90
        bool bSucess = false;
 
91
        bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
 
92
        // TOFIX err handling
 
93
        return nTotalBytes;
 
94
}
 
95
 
 
96
int IOLayerZlib::Write(char *szBuffer, int nLen)
 
97
{
 
98
        //compress content
 
99
        TRACE("IOLayerZlib::Write (%d bytes)\n", nLen);
 
100
        ASSERT(nLen > 0);
 
101
        if(nLen < 1)
 
102
                return 0;
 
103
 
 
104
        ASSERT(m_stream.avail_in == 0);
 
105
        m_stream.next_in  = (unsigned char *)szBuffer;
 
106
        m_stream.avail_in = nLen;
 
107
 
 
108
        int nTotalBytes = 0;
 
109
        bool bSucess = false;
 
110
        bSucess = DoProcessing(Z_NO_FLUSH, nTotalBytes);
 
111
        // TOFIX err handling
 
112
        return nTotalBytes;
 
113
}
 
114
 
 
115
int IOLayerZlib::Finalize()
 
116
{
 
117
        int nTotalBytes = 0;
 
118
        bool bSucess = false;
 
119
        TRACE("IOLayerZlib::Finalize start\n");
 
120
        bSucess = DoProcessing(Z_FINISH, nTotalBytes);
 
121
        TRACE("IOLayerZlib::Finalize (%d bytes)\n", nTotalBytes);
 
122
 
 
123
        // cleanup stream memory
 
124
        if(MODE_WRITE == m_nMode)
 
125
                deflateEnd(&m_stream);
 
126
        else
 
127
                inflateEnd(&m_stream);
 
128
 
 
129
        //call next layer attached?
 
130
        if(NULL != m_pNextLayer)
 
131
                return m_pNextLayer->Finalize();
 
132
 
 
133
        return nTotalBytes;
 
134
}
 
135
 
 
136
bool IOLayerZlib::DoProcessing(int nFlag, int &nTotal)
 
137
{
 
138
        int nTotalBytes = 0;
 
139
        int nTotalBytesExt = 0;
 
140
 
 
141
        m_err = Z_OK;
 
142
 
 
143
        for(;;)
 
144
        {
 
145
                m_stream.avail_out = m_nOutLength;
 
146
                m_stream.next_out = m_szOutBuffer;
 
147
 
 
148
                if(MODE_WRITE == m_nMode)
 
149
                        m_err = deflate(&m_stream, nFlag);
 
150
                else
 
151
                        m_err = inflate(&m_stream, nFlag);
 
152
 
 
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);
 
155
 
 
156
                nTotalBytes += nBytes;
 
157
                m_nLastOutBytes = nBytes;
 
158
                m_nProcessedOutBytes += nBytes;
 
159
 
 
160
                //call next layer attached?
 
161
                if(NULL != m_pNextLayer && nBytes > 0)
 
162
                        nTotalBytesExt += m_pNextLayer->Process((char *)m_szOutBuffer, nBytes);
 
163
 
 
164
                //control looping here
 
165
                if(m_err != Z_OK)
 
166
                        break;
 
167
                if(nFlag == Z_NO_FLUSH && m_stream.avail_in == 0)
 
168
                        break;
 
169
        }
 
170
 
 
171
        //TRACE(" IOLayerZlib::DoProcessing end (zlib unprocessed: %d bytes)\n", m_stream.avail_in);
 
172
        nTotal = nTotalBytes;
 
173
        return true;
 
174
}