summaryrefslogtreecommitdiffstats
path: root/dcopc/marshal.c
blob: a17aa2ffebf381d3df3c5033dc4208900eb80459 (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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Copyright (c) 2000 Simon Hausmann <hausmann@kde.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "config.h"
#include "marshal.h"

#include <glib.h>

#include <assert.h>
#include <stdlib.h>
#include <string.h>

/* The code in previous revisions was broken on little endian system, also see KDE bug #32463 */
/* The current code was not tested on big endian system, so it could be another issue and big */
/* endian systems would need the same code as the little endian ones                          */
#include <endian.h>

dcop_data *dcop_data_new()
{
    dcop_data *res = g_new( dcop_data, 1 );

    res->ptr = 0;
    res->size = 0;
    res->cur = 0;
    res->ref = 0;

    return res;
}

void dcop_data_destroy( dcop_data *data )
{
    g_assert( data->ref == 0 );

    g_free( data->ptr );

    g_free( data );
}

dcop_data *dcop_data_copy( dcop_data *data )
{
    dcop_data *res = dcop_data_new();
    res->ptr = (char *)g_malloc( data->size );
    res->size = data->size;
    dcop_data_reset( res );
    memcpy( res->ptr, data->ptr, data->size );
    return res;
}

static gboolean dcop_data_check_size( dcop_data *data, unsigned int size )
{
    if ( data->size - ( data->cur - data->ptr ) < size )
	return FALSE;
    return TRUE;
}

gboolean dcop_marshal_raw( dcop_data *data, const void *ptr, unsigned int size )
{
    unsigned int relptr = data->cur - data->ptr;

    data->ptr = (char *)g_realloc( data->ptr, data->size + size );

    if ( data->ptr == 0 )
	return FALSE;

    data->cur = data->ptr + relptr;

    memcpy( data->cur, ptr, size );

    data->cur += size;
    data->size += size;

    return TRUE;
}

gboolean dcop_marshal_uint32( dcop_data *data, unsigned int val )
{
    unsigned char buf[4];

    g_assert( sizeof( unsigned int ) == 4 );

#ifdef WORDS_BIGENDIAN
    buf[0] = val;
    buf[1] = val >> 8;
    buf[2] = val >> 16;
    buf[3] = val >> 24;
#else
    buf[3] = val;
    buf[2] = val >> 8;
    buf[1] = val >> 16;
    buf[0] = val >> 24;
#endif

    return dcop_marshal_raw( data, buf, 4 );
}

gboolean dcop_demarshal_uint32( dcop_data *data, unsigned int *val )
{
    g_assert( sizeof( unsigned int ) == 4 );

    if ( !dcop_data_check_size( data, 4 ) )
	return FALSE;

#ifdef WORDS_BIGENDIAN
    *val = (data->cur[3] << 24) |
           (data->cur[2] << 16) |
           (data->cur[1] << 8) |
           data->cur[0]; 
#else
    *val = (data->cur[0] << 24) |
           (data->cur[1] << 16) |
           (data->cur[2] << 8) |
           data->cur[3]; 
#endif
    data->cur += 4;

    return TRUE;
}

gboolean dcop_marshal_string( dcop_data *data, const gchar *str )
{
    size_t l = 0;

    if ( str )
	l = strlen( str ) + 1;

    if( !dcop_marshal_uint32( data, l ) )
	return FALSE;

    if ( str )
	return dcop_marshal_raw( data, str, l );
    else
	return TRUE;
}

gboolean dcop_demarshal_string( dcop_data *data, gchar **str )
{
    unsigned int l = 0;
    gchar *res = 0;

    g_assert( str );

    if ( !dcop_demarshal_uint32( data, &l ) )
	return FALSE;

    if ( !dcop_data_check_size( data, l ) )
	return FALSE;

    res = (char *)g_malloc( l );
    memcpy( res, data->cur, l );
    data->cur += l;

    *str = res;

    return TRUE;
}


gboolean dcop_marshal_string16( dcop_data *data, const gchar *str )
{
    size_t l = 0;
    size_t c = 0;
    char *tmp = 0;
    const char *src = str;
    char *dst = 0;

    if ( str )
	l = strlen( str ) * 2;
    else
    {
	/* null marker*/
	guint32 s = 0xffffffff;
	return dcop_marshal_uint32( data, s );
    }

    if( !dcop_marshal_uint32( data, l ) )
	return FALSE;

    if ( str )
    {
	dst = tmp = (char *)g_malloc( l );
	c = strlen( str );

	while ( c-- )
	{
	    *dst++ = 0;
	    *dst++ = *src++;
	}

	dcop_marshal_raw( data, tmp, l );

	g_free( tmp );
    }
    return TRUE;
}

gboolean dcop_demarshal_string16( dcop_data *data, gchar **str )
{
    unsigned int l = 0;
    unsigned int size = 0;
    char *res = 0;
    char *p = 0;

    assert( str );

    if ( !dcop_demarshal_uint32( data, &l ) )
	return FALSE;

    /* null marker*/
    if ( l == 0xffffffff )
    {
	*str = 0;
	return TRUE;
    }

    if ( !dcop_data_check_size( data, l ) )
	return FALSE;

    size = ( l / 2 );

    p = res = (char *)g_malloc( size + 1 );

    while( size-- )
    {
	data->cur++;
	*p++ = *data->cur++;
    }

    *p = '\0';

    *str = res;

    return TRUE;
}

gboolean dcop_marshal_bytearray( dcop_data *data, const gchar *str,  size_t size )
{
    if( !dcop_marshal_uint32( data, size ) )
	return FALSE;

    if ( str )
	return dcop_marshal_raw( data, str, size );
    else
	return TRUE;
}

gboolean dcop_demarshal_bytearray( dcop_data *data, gchar **str, size_t *size )
{
    unsigned int l = 0;
    gchar *res = 0;

    g_assert( str );

    if ( !dcop_demarshal_uint32( data, &l ) )
	return FALSE;

    if ( !dcop_data_check_size( data, l ) )
	return FALSE;

    res = (char *)g_malloc( l );
    memcpy( res, data->cur, l );
    data->cur += l;

    *str = res;
    *size = l;

    return TRUE;
}



gboolean dcop_marshal_data( dcop_data *data, dcop_data *other )
{
    if ( !dcop_marshal_uint32( data, other->size ) )
	return FALSE;

    return dcop_marshal_raw( data, other->ptr, other->size );
}

gboolean dcop_demarshal_data( dcop_data *data, dcop_data **other )
{
    dcop_data *res = dcop_data_new();
    char *tmp = 0;
    unsigned int l = 0;

    if ( !dcop_demarshal_uint32( data, &l ) )
	return FALSE;

    if ( !dcop_data_check_size( data, l ) )
	return FALSE;

    tmp = (char *)malloc( l );

    memcpy( tmp, data->cur, l );
    data->cur += l;

    dcop_data_assign( res, tmp, l );
    dcop_data_ref( res );

    *other = res;
    return True;
}

gboolean dcop_marshal_stringlist( dcop_data *data, GList *list )
{
    GList *it = g_list_first( list );

    if ( !dcop_marshal_uint32( data, g_list_length( list ) ) )
	return FALSE;

    while ( it )
    {
	if ( !dcop_marshal_string( data, (gchar *)it->data ) )
	    return FALSE;
	it = g_list_next( it );
    }
    return TRUE;
}

gboolean dcop_demarshal_stringlist( dcop_data *data, GList**list )
{
    unsigned int count = 0;
    GList *res = 0;
    unsigned int i = 0;
    gchar *str = 0;

    *list = 0;

    if ( !dcop_demarshal_uint32( data, &count ) )
	return FALSE;

    for ( i = 0; i < count; ++i )
    {
	if ( !dcop_demarshal_string( data, &str ) )
	{
	    dcop_list_free( res );
	    return FALSE;
	}
	res = g_list_append( res, str );
    }
    *list = res;
    return TRUE;
}

gboolean dcop_marshal_stringlist16( dcop_data *data, GList *list )
{
    GList *it = g_list_first( list );

    if ( !dcop_marshal_uint32( data, g_list_length( list ) ) )
	return False;

    while ( it )
    {
	if ( !dcop_marshal_string16( data, (gchar *)it->data ) )
	    return FALSE;
	it = g_list_next( it );
    }
    return TRUE;
}

gboolean dcop_demarshal_stringlist16( dcop_data *data, GList **list )
{
    unsigned int count = 0;
    GList *res = 0;
    unsigned int i = 0;
    char *str = 0;

    *list = 0;

    if ( !dcop_demarshal_uint32( data, &count ) )
	return FALSE;

    for ( i = 0; i < count; ++i )
    {
	if ( !dcop_demarshal_string16( data, &str ) )
	{
	    dcop_list_free( res );
	    return FALSE;
	}
	res = g_list_append( res, str );
    }
    *list = res;
    return TRUE;
}

void dcop_data_assign( dcop_data *data, char *d, unsigned int size )
{ 
    data->ptr = data->cur = d; 
    data->size = size; 
}

gboolean dcop_marshal_boolean( dcop_data *data, gboolean val )
{
    guint8 i = (guint8)val;
    return dcop_marshal_uint8( data, i );
}

gboolean dcop_demarshal_boolean( dcop_data *data, gboolean *val )
{
    guint8 i;
    if ( !dcop_demarshal_uint8( data, &i ) )
	return FALSE;
    *val = (gboolean)i;
    return TRUE;
}

gboolean dcop_marshal_uint8( dcop_data *data, guint8 val )
{
    return dcop_marshal_raw( data, &val, 1 );
}

gboolean dcop_demarshal_uint8( dcop_data *data, guint8 *val )
{
    if ( !dcop_data_check_size( data, 1 ) )
	return FALSE;

    *val = *data->cur++;
    return TRUE;
}

dcop_data *dcop_data_ref( dcop_data *data ) { data->ref++; return data; }
void dcop_data_deref( dcop_data *data ) { if ( !--data->ref ) dcop_data_destroy( data ); }
void dcop_data_reset( dcop_data *data ) { data->cur = data->ptr; }