summaryrefslogtreecommitdiffstats
path: root/tinySIGCOMP/src/tcomp_compressordisp.c
blob: da7bcc48d4acef748330d0a7484bf3f2cfb70a63 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* 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_compressordisp.h
 * @brief  Entity that receives application messages, invokes a compressor,and forwards the resulting SigComp compressed messages to a remote
 * endpoint.
 *
 * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
 *

 */
#include "tcomp_compressordisp.h"
#include "tcomp_compressor_dummy.h"
#include "tcomp_compressor_deflate.h"

#include "tsk_memory.h"
#include "tsk_debug.h"

#include <string.h>

/**Checks whether NACK (RFC 4077) handling is supported
*/
#define TCOMP_NACK_SUPPORTED (dispatcher->stateHandler->sigcomp_parameters->SigComp_version >= 0x02)


/**Creates new compressor dispatcher.
*/
tcomp_compressordisp_t* tcomp_compressordisp_create(const tcomp_statehandler_t* statehandler)
{
    return tsk_object_new(tcomp_compressordisp_def_t, statehandler);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// Compress a message.
///
/// @param [in,out]	dispatcher	The compressor dispatcher.
/// @param	compartmentId		The compartment to use to compress the message.
/// @param [in,out]	input_ptr	The message to compress.
/// @param	input_size			The size of the input buffer.
/// @param [in,out]	output_ptr	The output buffer to copy the compressed data.
/// @param [in,out]	output_size	The size of the output buffer.
/// @param	stream				Indicates whether it's a stream buffer or not.
///
/// @return	@a tsk_true if succeed and @a tsk_false otherwize.
////////////////////////////////////////////////////////////////////////////////////////////////////
tsk_bool_t tcomp_compressordisp_compress(tcomp_compressordisp_t *dispatcher, uint64_t compartmentId, const void *input_ptr, tsk_size_t input_size, void *output_ptr, tsk_size_t *output_size, tsk_bool_t stream)
{
    tsk_bool_t ret = tsk_true;
    int i = 0;

    /* For each compartment id create/retrieve one compressor instance */
    tcomp_compartment_t *lpCompartment = tcomp_statehandler_getCompartment(dispatcher->stateHandler, compartmentId);

    if(!lpCompartment) {
        TSK_DEBUG_ERROR("You must provide a valid compartment to perform compression.");
        return tsk_false;
    }


    /*
    *	Performs compression.
    */
    tsk_safeobj_lock(dispatcher);

    for( i = 0; (i < TCOMP_MAX_COMPRESSORS && dispatcher->compressors[i]); i++ ) {
        if((ret = dispatcher->compressors[i](lpCompartment, input_ptr, input_size, output_ptr, output_size, stream))) {
            break;
        }
    }

    tsk_safeobj_unlock(dispatcher);


    /*
    *	STREAM case. FIXME:Because I'm a lazy man I only support 0xFF00 case
    */
    if(stream) {
        uint8_t* escapedBuffer;
        tsk_size_t i, j;
        tsk_size_t escapedBufferSize = (*output_size + 2); /* 2 = strlen(0xffff) */

        for(i = 0; i < *output_size ; i++) {
            escapedBufferSize += ((uint8_t*)output_ptr)[i] == 0xff ? 1 : 0;
        }
        escapedBuffer = (uint8_t*)tsk_calloc(escapedBufferSize, sizeof(uint8_t));

        for(i = 0, j = 0; i < *output_size; i++, j++) {
            escapedBuffer[j] = ((uint8_t*)output_ptr)[i];
            if(escapedBuffer[j] == 0xff) {
                escapedBuffer[++j] = 0x00;
            }
        }

        /* End stream */
        escapedBuffer[escapedBufferSize-1] = escapedBuffer[escapedBufferSize-2] = 0xff;
        memcpy(output_ptr, escapedBuffer, escapedBufferSize);

        *output_size = escapedBufferSize; /* Update size */
        TSK_FREE(escapedBuffer); /* free */
    }

    /*
    * NACK
    */
    if(ret && TCOMP_NACK_SUPPORTED) {
        /* store nack for later retrieval in case of errors */
        uint8_t nackId[TSK_SHA1_DIGEST_SIZE];
        tsk_sha1context_t sha;

        tsk_sha1reset(&sha);
        tsk_sha1input(&sha, (const uint8_t*)output_ptr, *output_size);
        tsk_sha1result(&sha, (uint8_t*)nackId);
        tcomp_compartment_addNack(lpCompartment, nackId);
    }

    return ret;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// Adds new compressor the list of the compressors.
///
///
/// @param [in,out]	dispatcher	The compressor dispatcher.
/// @param	compressor			A function pointer to the new compressor.
////////////////////////////////////////////////////////////////////////////////////////////////////
int tcomp_compressordisp_addCompressor(tcomp_compressordisp_t *dispatcher, tcomp_compressor_compress_f compressor)
{
    tsk_size_t i;

    if(!dispatcher) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    for(i = 0; i < TCOMP_MAX_COMPRESSORS; i++) {
        if(!dispatcher->compressors[i]) {
            dispatcher->compressors[i] = compressor;
            return 0;
        }
    }
    return -2;
}

/** Removes a compressor
*/
int tcomp_compressordisp_removeCompressor(tcomp_compressordisp_t *dispatcher, tcomp_compressor_compress_f compressor)
{
    tsk_size_t i;

    if(!dispatcher) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    for(i = 0; i < TCOMP_MAX_COMPRESSORS; i++) {
        if(dispatcher->compressors[i] == compressor) {
            dispatcher->compressors[i] = tsk_null;
            return 0;
        }
    }
    return -2;
}








//========================================================
//	SigComp compressor dispatcher object definition
//
static tsk_object_t* tcomp_compressordisp_ctor(tsk_object_t * self, va_list * app)
{
    tcomp_compressordisp_t *compressordisp = self;
    if(compressordisp) {
        int i = 0;
        compressordisp->stateHandler = va_arg(*app, const tcomp_statehandler_t*);

        compressordisp->compressors[0] = tcomp_compressor_deflate_compress;	/* If you don't want deflate compressor then remove this line. */
        compressordisp->compressors[1] = tcomp_compressor_dummy_compress;	/* RFC 4896 [11. Uncompressed Bytecode]. */

        /* Initialize safeobject */
        tsk_safeobj_init(compressordisp);
    }
    else {
        TSK_DEBUG_ERROR("Failed to create new compressor dispatcher.");
    }

    return self;
}

static tsk_object_t* tcomp_compressordisp_dtor(tsk_object_t *self)
{
    tcomp_compressordisp_t *compressordisp = self;
    if(compressordisp) {
        /* Deinitialize safeobject */
        tsk_safeobj_deinit(compressordisp);

        memset(compressordisp->compressors, 0 , sizeof(compressordisp->compressors));
        compressordisp->stateHandler = tsk_null; // not owner
    }
    else {
        TSK_DEBUG_ERROR("Null dispatcher.");
    }

    return self;
}

static const tsk_object_def_t tcomp_compressordisp_def_s = {
    sizeof(tcomp_compressordisp_t),
    tcomp_compressordisp_ctor,
    tcomp_compressordisp_dtor,
    tsk_null
};
const tsk_object_def_t *tcomp_compressordisp_def_t = &tcomp_compressordisp_def_s;
OpenPOWER on IntegriCloud