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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* 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(Ghost).
*
* @author Mamadou Diop <diopmamadou(at)yahoo.fr>
*
*/
#include "tcomp_deflatedata.h"
#include "tsk_binaryutils.h"
#include "tsk_debug.h"
#include <string.h>
/*
|<----------------------------DMS--------------------------------->|
|<-----SigComp message---->|<------------UDVM memory size--------->|
+-+----------+-------------+-----+----------+----------------------+
| | bytecode | comp msg | | bytecode | circular buffer |
+-+----------+-------------+-----+----------+----------------------+
^ ^
| |
SigComp header Low bytes of UDVM
I suppose we would like to compress this message "libsigcomp":
GHOST STRUCTURE:
0x02bb --> Circular Buffer size (DEFLATE_UDVM_CIRCULAR_START_INDEX = 699)
0x2000 --> DMS (8192)
0x0005 --> See 'libsigcomp/asm/deflate.asm' (MULTILOAD param number five)
0x02c5 --> 709 (DEFLATE_UDVM_CIRCULAR_START_INDEX + strlen("libsigcomp"))
...... --> Remaining MULTILOAD words
0x0682 --> 1666 (HASH_LEN = SMS+8)
0x067a --> 1658 (SMS)
0x1902 --> [CPB+DMS+SMS]+[SigComp version]
------------------------------------------------------------
0x0003 --> DEFLATE_NO_DICT
...... --> Dictionnary words
0x0000 --> FIXME
0x0000 --> FIXME
...... --> Deflate byte code (DeflateCompressor::deflate_bytecode)
...... --> no compressed message
*/
#define GHOST_CB_START_INDEX (0)
#define GHOST_DMS_INDEX (GHOST_CB_START_INDEX + 2)
#define GHOST_0x0005_INDEX (GHOST_DMS_INDEX + 2)
#define GHOST_CB_END_INDEX (GHOST_0x0005_INDEX + 2)
#define GHOST_HASH_LEN_INDEX (GHOST_CB_END_INDEX + 2 + 236)
#define GHOST_SMS_INDEX (GHOST_HASH_LEN_INDEX + 2)
#define GHOST_CPB_DMS_SMS_INDEX (GHOST_SMS_INDEX + 2)
#define GHOST_VERSION_INDEX (GHOST_CPB_DMS_SMS_INDEX + 1)
#define GHOST_BYTECODE1_SIZE (GHOST_VERSION_INDEX + 1)
static const char* DeflateData_deflate_bytecode1_ghost =
{
"\xff\xff" // Circular buffer Size (CBS)
"\xff\xff" // State Memory Size (DMS)
"\x00\x05" // See 'libsigcomp/asm/deflate.asm'
"\xff\xff" // (CBS + strlen(input))
"\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00\x08\x00\x00\x00\x09\x00\x00"
"\x00\x0a\x00\x01\x00\x0b\x00\x01\x00\x0d\x00\x01\x00\x0f\x00\x01\x00\x11\x00\x02\x00\x13\x00\x02\x00\x17\x00\x02\x00\x1b"
"\x00\x02\x00\x1f\x00\x03\x00\x23\x00\x03\x00\x2b\x00\x03\x00\x33\x00\x03\x00\x3b\x00\x04\x00\x43\x00\x04\x00\x53\x00\x04"
"\x00\x63\x00\x04\x00\x73\x00\x05\x00\x83\x00\x05\x00\xa3\x00\x05\x00\xc3\x00\x05\x00\xe3\x00\x00\x01\x02\x00\x00\x00\x01"
"\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x01\x00\x05\x00\x01\x00\x07\x00\x02\x00\x09\x00\x02\x00\x0d\x00\x03"
"\x00\x11\x00\x03\x00\x19\x00\x04\x00\x21\x00\x04\x00\x31\x00\x05\x00\x41\x00\x05\x00\x61\x00\x06\x00\x81\x00\x06\x00\xc1"
"\x00\x07\x01\x01\x00\x07\x01\x81\x00\x08\x02\x01\x00\x08\x03\x01\x00\x09\x04\x01\x00\x09\x06\x01\x00\x0a\x08\x01\x00\x0a"
"\x0c\x01\x00\x0b\x10\x01\x00\x0b\x18\x01\x00\x0c\x20\x01\x00\x0c\x30\x01\x00\x0d\x40\x01\x00\x0d\x60\x01"
"\xff\xff" // HASH_LEN
"\xff\xff" // SMS
"\xff" // [CPB+DMS+SMS]
"\xff" // SigComp Version
};
#define GHOST_STATE_ADDRESS 64
#define GHOST_STATE_INSTRUCTION 492
#define GHOST_STATE_MIN_ACCESS_LEN 6
#define GHOST_STATE_RETENTION_PRIORITY 0
/**Creates a Ghost state.
*/
void tcomp_deflatedata_createGhost(tcomp_deflatedata_t *deflatedata, uint32_t state_length, tcomp_params_t *params)
{
uint8_t *ghostvalue_ptr = 0;
#define GHOSTVALUE_AT(position) (ghostvalue_ptr + position)
if(!deflatedata){
TSK_DEBUG_ERROR("NULL defalte data.");
return;
}
if(deflatedata->ghostState){
TSK_DEBUG_ERROR("The defalte data already have a ghost state. This MUST never happen.");
return;
}
tsk_safeobj_lock(deflatedata);
deflatedata->ghostState = tcomp_state_create(state_length, GHOST_STATE_ADDRESS, GHOST_STATE_INSTRUCTION, GHOST_STATE_MIN_ACCESS_LEN, GHOST_STATE_RETENTION_PRIORITY);
tcomp_buffer_allocBuff(deflatedata->ghostState->value, state_length);
ghostvalue_ptr = tcomp_buffer_getBuffer(deflatedata->ghostState->value);
memcpy(ghostvalue_ptr, DeflateData_deflate_bytecode1_ghost, GHOST_BYTECODE1_SIZE);
TSK_BINARY_SET_2BYTES( GHOSTVALUE_AT(GHOST_CB_START_INDEX), DEFLATE_UDVM_CIRCULAR_START_INDEX );
TSK_BINARY_SET_2BYTES( GHOSTVALUE_AT(GHOST_DMS_INDEX), state_length+GHOST_STATE_ADDRESS );
TSK_BINARY_SET_2BYTES( GHOSTVALUE_AT(GHOST_CB_END_INDEX), (DEFLATE_UDVM_CIRCULAR_START_INDEX + 0/*input_size*/) );
// ......
TSK_BINARY_SET_2BYTES( GHOSTVALUE_AT(GHOST_HASH_LEN_INDEX), (state_length+8) );
TSK_BINARY_SET_2BYTES( GHOSTVALUE_AT(GHOST_SMS_INDEX), state_length );
TSK_BINARY_SET_2BYTES( GHOSTVALUE_AT(GHOST_CPB_DMS_SMS_INDEX), tcomp_params_getParameters(params) ); // [cpb+dms+sms]+[version]
// ------------------------------------------------------------
#define GHOST_DICT_CODE_INDEX (GHOST_BYTECODE1_SIZE)
#define GHOST_DICT_WORDS_INDEX (GHOST_DICT_CODE_INDEX + 2)
#if USE_DICTS_FOR_COMPRESSION
# define GHOST_FIXME2_INDEX (GHOST_DICT_WORDS_INDEX + 4/*Sip dict id len*/)
#else
# define GHOST_FIXME2_INDEX (GHOST_DICT_WORDS_INDEX + 0)
#endif
#define GHOST_DEFLATE_BYTECODE_INDEX (GHOST_FIXME2_INDEX + 4)
#define GHOST_INPUT_INDEX (GHOST_DEFLATE_BYTECODE_INDEX + DEFLATE_BYTECODE_LEN)
#if USE_DICTS_FOR_COMPRESSION
BINARY_SET_2BYTES( ghostState->getStateValue()->getBuffer(GHOST_DICT_CODE_INDEX), DEFLATE_FIXME_DICT );
if( DEFLATE_FIXME_DICT == DEFLATE_NO_DICT ){
// Nothing to append
}else{
if(DEFLATE_FIXME_DICT == DEFLATE_SIP_DICT_ONLY){
::memmove( ghostState->getStateValue()->getBuffer(GHOST_DICT_WORDS_INDEX), RFC3485_DICTIONARY_SIP_IDENTIFIER, 4 );
}else{
assert(1==0);
}
}
#endif
memcpy( GHOSTVALUE_AT(GHOST_DEFLATE_BYTECODE_INDEX), (const char*)DEFLATEDATA_DEFLATE_BYTECODE, DEFLATE_BYTECODE_LEN );
tsk_safeobj_unlock(deflatedata);
#undef GHOSTVALUE_AT
}
/**Acknowledge a Ghost state.
*/
void tcomp_deflatedata_ackGhost(tcomp_compressordata_t *data, const tcomp_buffer_handle_t *stateid)
{
tcomp_deflatedata_t *deflatedata = data;
if(!deflatedata){
TSK_DEBUG_ERROR("NULL defalte data.");
return;
}
tsk_safeobj_lock(deflatedata);
if(deflatedata->ghostState){
/* Update ghost */
if(tcomp_buffer_startsWith(deflatedata->ghostState->identifier, stateid)){
/* END() + COPY() */
tcomp_deflateStream_end(&(deflatedata->stream_acked));
tcomp_deflateStream_copy(&(deflatedata->stream_acked), &(deflatedata->stream_1));
deflatedata->stream_acked.stateful = 1;
deflatedata->stream_acked.dataWaitingAck = 0;
TSK_DEBUG_INFO("SigComp - ACKed State id=");
tcomp_buffer_print(stateid);
}
}
tsk_safeobj_unlock(deflatedata);
}
/**Updates a Ghost state.
*/
void tcomp_deflatedata_updateGhost(tcomp_deflatedata_t *deflatedata, const uint8_t *input_ptr, tsk_size_t input_size)
{
uint32_t i;
uint8_t *ghostvalue_ptr = 0;
#define GHOSTVALUE_AT(position) (ghostvalue_ptr + position)
if(!deflatedata){
TSK_DEBUG_ERROR("NULL defalte data.");
return;
}
if(!deflatedata->ghostState){
TSK_DEBUG_ERROR("NULL ghost state.");
return;
}
tsk_safeobj_lock(deflatedata);
ghostvalue_ptr = tcomp_buffer_getBuffer(deflatedata->ghostState->value);
#define ZBUFF_LEN (0x0001 << deflatedata->zWindowBits)
for(i = 0; i < input_size; i++){
#if 0
*GHOSTVALUE_AT(GHOST_INPUT_INDEX + ghost_copy_offset) = 0x00;
#else
*GHOSTVALUE_AT(GHOST_INPUT_INDEX + deflatedata->ghost_copy_offset) = input_ptr[i];
#endif
deflatedata->ghost_copy_offset = (deflatedata->ghost_copy_offset + 1) % ZBUFF_LEN;
}
/* Update Circular Buffer Index */
TSK_BINARY_SET_2BYTES( GHOSTVALUE_AT(GHOST_CB_END_INDEX), (DEFLATE_UDVM_CIRCULAR_START_INDEX + deflatedata->ghost_copy_offset) );
/* Compute State Identifier (20 bytes) */
tcomp_state_makeValid(deflatedata->ghostState);
// new state identifer not acked yet
if(deflatedata->useOnlyACKedStates){
deflatedata->stream_acked.stateful = 0;
deflatedata->stream_acked.dataWaitingAck = 1;
}
TSK_DEBUG_INFO("SigComp - Making Ghost state valid with id = ");
tcomp_buffer_print(deflatedata->ghostState->identifier);
tsk_safeobj_unlock(deflatedata);
#undef GHOSTVALUE_AT
}
uint32_t* tcomp_deflatedata_getGhostCopyOffset(tcomp_deflatedata_t *deflatedata)
{
if(!deflatedata){
TSK_DEBUG_ERROR("NULL defalte data.");
return 0;
}
return &(deflatedata->ghost_copy_offset);
}
void tcomp_deflatedata_freeGhostState(tcomp_compressordata_t *data)
{
tcomp_deflatedata_t *deflatedata = data;
if(!deflatedata){
TSK_DEBUG_ERROR("NULL defalte data.");
return;
}
TSK_OBJECT_SAFE_FREE(deflatedata->ghostState);
}
|