summaryrefslogtreecommitdiffstats
path: root/tinySIGCOMP/src/tcomp_compressor_deflate.c
blob: fcce528c6da5c1b67ad52b1a528579711d5345bc (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
218
/*
* 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  SigComp Deflate compressor.
 *
 * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
 *

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

#include "tsk_debug.h"

#include <string.h>

#define TCOMP_MIN(a, b) (a < b ? a : b)

////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// @brief	Compress SIgComp message using deflate algorithm. 
///
/// @param [in,out]	lpCompartment	If non-null, the pointer to a compartment. 
/// @param [in,out]	input_ptr		If non-null, the input pointer. 
/// @param	input_size				Size of the input. 
/// @param [in,out]	output_ptr		If non-null, the output pointer. 
/// @param [in,out]	output_size		If non-null, size of the output. 
/// @param	stream					The stream. 
///
/// @return	. 
////////////////////////////////////////////////////////////////////////////////////////////////////

tsk_bool_t tcomp_compressor_deflate_compress(tcomp_compartment_t *lpCompartment, const void *input_ptr, tsk_size_t input_size, void *output_ptr, tsk_size_t *output_size, tsk_bool_t stream)
{
#define GET_OUTPUT_BUFFER_AT(position) (((uint8_t*)output_ptr) + position)

	tsk_bool_t result = tsk_true;
	int stateChanged, stateful, windowBits, zret;
	tcomp_deflatedata_t *deflatedata = 0;
	tsk_size_t pointer = 0, state_len_index = 0, compressedDataLen;
	uint8_t smsCode, *header;

	tsk_safeobj_lock(lpCompartment);
	
	/* Compression Data */
	if(!lpCompartment->compressorData){
		lpCompartment->compressorData = tcomp_deflatedata_create(stream, lpCompartment->useOnlyACKedStates);
		if(!lpCompartment->compressorData){
			TSK_DEBUG_ERROR("Failed to create deflate compressor data.");
			result = tsk_false;
			goto bail;
		}
		else{
			lpCompartment->ackGhost = tcomp_deflatedata_ackGhost;
			lpCompartment->freeGhostState = tcomp_deflatedata_freeGhostState;
			lpCompartment->compressorData_isStream  = stream;
		}
	}
		
	deflatedata = lpCompartment->compressorData;

	/* State memory size code */
	smsCode = TCOMP_MIN(lpCompartment->remote_parameters->smsCode, lpCompartment->remote_parameters->dmsCode);
	if(lpCompartment->useOnlyACKedStates){
		stateful = (deflatedata->ghostState && tcomp_deflatedata_isStateful(deflatedata));
	}
	else{
		stateful = !!deflatedata->ghostState;
	}

	/*
	*	Init zLIB
	*/
	windowBits = ( smsCode - (stream ? 2 : 1) ) + 10;
	windowBits = TSK_CLAMP(8, windowBits, 15); /* Because of zlib limitation (windowsize MUST be between 8 and 15) */
	if(windowBits != deflatedata->zWindowBits){
		/* Window size changed */
		tcomp_deflatedata_freeGhostState(deflatedata);
		tcomp_deflatedata_zSetWindowBits(deflatedata, windowBits);

		if( !(result = tcomp_deflatedata_zReset(deflatedata)) ) {
			goto bail;
		}
	}
	else if(!deflatedata->ghostState){
		/* No ghost --> reset zlib */
		deflatedata->ghost_copy_offset = 0;
		if( !(result = tcomp_deflatedata_zReset(deflatedata)) ) {
			goto bail;
		}
	}

	stateful &= !!deflatedata->ghostState;

	/*
	*	SigComp headers
	*/
	header = GET_OUTPUT_BUFFER_AT(pointer++);

	
	/* SigComp Header */
	if(lpCompartment->lpReqFeedback && tcomp_buffer_getSize(lpCompartment->lpReqFeedback)){
		/* Return the requested feedback */
		*header = 0xfc; /* T=1 */
		memcpy(GET_OUTPUT_BUFFER_AT(pointer), tcomp_buffer_getBuffer(lpCompartment->lpReqFeedback), tcomp_buffer_getSize(lpCompartment->lpReqFeedback));
		pointer += tcomp_buffer_getSize(lpCompartment->lpReqFeedback);
	}
	else{
		*header = 0xf8;
	}

	/*
	* Stateless or stateful?
	*/
	if(stateful){
		TSK_DEBUG_INFO("SigComp - Compressing message with state id = ");
		tcomp_buffer_print(deflatedata->ghostState->identifier);
		memcpy(GET_OUTPUT_BUFFER_AT(pointer), tcomp_buffer_getBuffer(deflatedata->ghostState->identifier), TCOMP_PARTIAL_ID_LEN_VALUE);

		pointer += TCOMP_PARTIAL_ID_LEN_VALUE; 
		*header |= TCOMP_PARTIAL_ID_LEN_CODE;
	}
	else{
		uint32_t codeLen = DEFLATE_BYTECODE_LEN;
		/* first byte for codelen */
		*GET_OUTPUT_BUFFER_AT(pointer++) = ((codeLen>>4)& 0x00ff);
		/* last 4 bits for codelen */
		*GET_OUTPUT_BUFFER_AT(pointer) = ((codeLen & 0x000f)<<4); 
		/* first and last 4 bits for destination */
		*GET_OUTPUT_BUFFER_AT(pointer++) |= DEFLATE_BYTECODE_DESTINATION_CODE;

		/*
		*	Upload UDVM bytecode
		*/
		memcpy(GET_OUTPUT_BUFFER_AT(pointer), (const uint8_t*)DEFLATEDATA_DEFLATE_BYTECODE, codeLen);
		pointer += codeLen;

		//////////////////////////////////////////////////
		// FIXME: check for overflow and >=320
		//
		// [DMS]+[Req. Fed. Loc.]+[state_len]+[cpb||dms||sms]+[Sigcomp_version]+[states]
		//*output_buffer.getBuffer(pointer++) = 0x04; //reserved=0, Q=1, S=0, I=0
		//*output_buffer.getBuffer(pointer++) = (this->req_feedback_item++); //requested feedback item
		
		state_len_index  = pointer;
		*GET_OUTPUT_BUFFER_AT(pointer) = 0x00, pointer += 4; /* [hash_len]+[state_len] */
		*GET_OUTPUT_BUFFER_AT(pointer++) = (tcomp_params_getParameters(lpCompartment->local_parameters)>>8); // [cpb||dms||sms]
		*GET_OUTPUT_BUFFER_AT(pointer++) = (tcomp_params_getParameters(lpCompartment->local_parameters)&0x00ff); // [Sigcomp_version]
#if USE_DICTS_FOR_COMPRESSION
		*output_buffer.getBuffer(pointer++) = 0x00; // First dict byte	// FIXME
		*output_buffer.getBuffer(pointer++) = DEFLATE_FIXME_DICT; // FIXME: also change ghost
#endif
	}

	/*
	*	Compress data using ZLIB
	*/
	compressedDataLen = (*output_size - pointer);
	zret = tcomp_deflatedata_zCompress(deflatedata, input_ptr, input_size, GET_OUTPUT_BUFFER_AT(pointer), &compressedDataLen, &stateChanged);
	if(!zret){
		result = tsk_false;
		goto bail;
	}
	pointer += compressedDataLen;
	*output_size = (pointer);

	/*
	* Update state length
	*/
	if(!stateful){	
		uint32_t state_len = ( (1<<(deflatedata->zWindowBits)) + DEFLATE_UDVM_CIRCULAR_START_INDEX - 64 );
		uint32_t hash_len = (state_len + 8);
		
		// FIXME: 131072  could not go in 2-bytes
		*GET_OUTPUT_BUFFER_AT(state_len_index) = (hash_len >> 8);
		*GET_OUTPUT_BUFFER_AT(state_len_index+1) = (hash_len & 0x00ff);
		*GET_OUTPUT_BUFFER_AT(state_len_index+2) = (state_len >> 8);
		*GET_OUTPUT_BUFFER_AT(state_len_index+3) = (state_len & 0x00ff);

		/*	First time or synchronize failure (NACK reason=STATE_NOT_FOUND) */
		if(!deflatedata->ghostState){
			tcomp_deflatedata_createGhost(deflatedata, state_len, lpCompartment->local_parameters);
		}
	}
	if(!lpCompartment->useOnlyACKedStates || (lpCompartment->useOnlyACKedStates && stateChanged))
	{
		tcomp_deflatedata_updateGhost(deflatedata, (const uint8_t*)input_ptr, input_size);
	}

	//output_buffer.print(2000);
	
bail:
	tsk_safeobj_unlock(lpCompartment);

	return result;

#undef GET_OUTPUT_BUFFER_AT
}
OpenPOWER on IntegriCloud