summaryrefslogtreecommitdiffstats
path: root/tinySIGCOMP/src/tcomp_deflatedata.zlib.c
blob: 6d8c6284ede96a2d72be7821f04c6024c88cf7eb (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tcomp_compressor.h
 * @brief  SiComp Deflate compressor (zlib).
 *
 * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
 *

 */
#include "tcomp_deflatedata.h"
#include "tsk_debug.h"


tsk_bool_t tcomp_deflateStream_end(tcomp_deflateStream_t *stream)
{
	if(!stream){
		TSK_DEBUG_ERROR("NULL defalte stream.");
		return tsk_false;
	}

	return deflateEnd(&(stream->zs));
}

tsk_bool_t tcomp_deflateStream_copy(tcomp_deflateStream_t *stream, tcomp_deflateStream_t *source)
{
	if(!stream){
		TSK_DEBUG_ERROR("NULL defalte stream.");
		return tsk_false;
	}

	return deflateCopy(&(stream->zs), &(source->zs));
}

tsk_bool_t tcomp_deflatedata_zInit(tcomp_deflatedata_t *deflatedata)
{
	if(!deflatedata){
		TSK_DEBUG_ERROR("NULL defalte data.");
		return tsk_false;
	}

	/* Already initialized? */
	if(deflatedata->isInitialized) {
		return tsk_true;
	}

	/* allocate deflate state */
    deflatedata->stream_1.zs.zalloc = deflatedata->stream_acked.zs.zalloc = Z_NULL;
    deflatedata->stream_1.zs.zfree = deflatedata->stream_acked.zs.zfree = Z_NULL;
    deflatedata->stream_1.zs.opaque = deflatedata->stream_acked.zs.opaque = Z_NULL;
#ifndef __SYMBIAN32__
	deflatedata->stream_1.zs.data_type = deflatedata->stream_acked.zs.data_type = Z_TEXT;
#endif

    //bool ret  =  (deflateInit(&this->zStream, this->zLevel) == Z_OK);
	if( deflateInit2(&deflatedata->stream_1.zs, deflatedata->zLevel, Z_DEFLATED, -deflatedata->zWindowBits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK 
		|| deflateInit2(&deflatedata->stream_acked.zs, deflatedata->zLevel, Z_DEFLATED, -deflatedata->zWindowBits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK )
	{
		return tsk_false;
	}
#if USE_DICTS_FOR_COMPRESSION
	if( deflateSetDictionary(this->stream_1.zs, (const Bytef*)RFC3485_DICTIONARY_SIP_VALUE, RFC3485_DICTIONARY_SIP_VALUE_LENGTH) != Z_OK 
		|| deflateSetDictionary(this->stream_acked.zs, (const Bytef*)RFC3485_DICTIONARY_SIP_VALUE, RFC3485_DICTIONARY_SIP_VALUE_LENGTH) != Z_OK )
	{
		return false;
	}
#endif

	deflatedata->stream_1.stateful = deflatedata->stream_acked.stateful = 0;
	deflatedata->stream_1.dataWaitingAck = deflatedata->stream_acked.dataWaitingAck = 0;
	deflatedata->isInitialized = tsk_true;

	return tsk_true;
}

tsk_bool_t tcomp_deflatedata_zUnInit(tcomp_deflatedata_t *deflatedata)
{
	if(!deflatedata){
		TSK_DEBUG_ERROR("NULL defalte data.");
		return tsk_false;
	}

	if(deflatedata->isInitialized){
		deflatedata->isInitialized = tsk_false;
		
		deflatedata->stream_1.dataWaitingAck = deflatedata->stream_acked.dataWaitingAck = 0;
		deflatedata->stream_1.stateful = deflatedata->stream_acked.stateful = 0;

		return (tcomp_deflateStream_end(&deflatedata->stream_1) != Z_STREAM_ERROR) && (tcomp_deflateStream_end(&deflatedata->stream_acked) != Z_STREAM_ERROR);
	}
	return tsk_true;
}

tsk_bool_t tcomp_deflatedata_zReset(tcomp_deflatedata_t *deflatedata)
{
	tsk_bool_t ret;

	if(!deflatedata){
		TSK_DEBUG_ERROR("NULL defalte data.");
		return tsk_false;
	}

	ret = deflatedata->isInitialized ? tcomp_deflatedata_zUnInit(deflatedata) : tsk_true; 
	ret &= tcomp_deflatedata_zInit(deflatedata);
	
	return ret;
}

tsk_bool_t tcomp_deflatedata_zCompress(tcomp_deflatedata_t *deflatedata, const void* in, tsk_size_t inLen, void* out, tsk_size_t* outLen, tsk_bool_t *stateChanged)
{
	int ret = tsk_false;
/*
	Two streams [1] and [2]
	
	* ZINIT/ZUNINIT/ZRESET
		XXX([1])
		XXX([2])
	
	* COMPRESSION
		[1]->END()
		[1]<-COPY-[2]
		[1]->COMPRESS()
	
	* ACK
		[2]->END()
		[2]<-COPY-[1]
		updateGhost([1])
*/
	if(!deflatedata){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_false;
	}

	tsk_safeobj_lock(deflatedata);
	
	/* Initialized? */
	if(!deflatedata->isInitialized){
		if(!tcomp_deflatedata_zInit(deflatedata)){
			TSK_DEBUG_ERROR("Failed to initialize zlib resources..");
			tsk_safeobj_unlock(deflatedata);
			return tsk_false;
		}
	}
	
	if(deflatedata->useOnlyACKedStates){
		if(!deflatedata->stream_acked.dataWaitingAck){
			deflatedata->stream_acked.dataWaitingAck = 1;
			*stateChanged = tsk_true;
		}
		else{
			*stateChanged = tsk_false;
		}

		/* END() + COPY() ==> use acked state */
		tcomp_deflateStream_end(&(deflatedata->stream_1));
		tcomp_deflateStream_copy(&(deflatedata->stream_1), &(deflatedata->stream_acked));
	}
	else{
		*stateChanged = tsk_true;
	}
	
	// IN
	deflatedata->stream_1.zs.next_in = (Bytef*)in;
    deflatedata->stream_1.zs.avail_in = (uInt)inLen;
	
	// OUT
	deflatedata->stream_1.zs.next_out = (Bytef*)out;
    deflatedata->stream_1.zs.avail_out = (uInt)*outLen;
	
	ret = deflate(&(deflatedata->stream_1.zs), Z_SYNC_FLUSH);
	
	*outLen -= deflatedata->stream_1.zs.avail_out;
	
	tsk_safeobj_unlock(deflatedata);
	
	return (ret == Z_OK);
}

int tcomp_deflatedata_zGetWindowBits(tcomp_deflatedata_t *deflatedata)
{
	if(!deflatedata){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	return deflatedata->zWindowBits;
}

void tcomp_deflatedata_zSetWindowBits(tcomp_deflatedata_t *deflatedata, int windowSize)
{
	if(!deflatedata){
		TSK_DEBUG_ERROR("Invalid parameter");
		return;
	}

	deflatedata->zWindowBits = windowSize;
}

OpenPOWER on IntegriCloud