labcomm_sig_parser.c 15.5 KB
Newer Older
Sven Robertz's avatar
Sven Robertz committed
1
/* labcomm_sig_parser.c:
2
3
4
5
6
7
8
9
10
11
 * an example parser for labcomm signatures, illustrating how to skip samples
 * based on their signature. Intended as an embryo for introducing this 
 * functionality into the lib to allow a channel to survive types with no
 * registered handler.
 */

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

12
#include "labcomm_sig_parser.h"
Sven Robertz's avatar
Sven Robertz committed
13

14
static void error(char *s) {
15
16
17
18
19
20
21
22
	fprintf(stderr, "%s", s);
	fprintf(stderr, "\nexiting\n");
	exit(1);
}

#define STACK_SIZE 16

/* aux method for reading a big endian uint32 from a char* (i.e. ntohl but for explicit char*) */
Sven Robertz's avatar
Sven Robertz committed
23
24
25
26
27
28
29
30
static unsigned int unpack32(unsigned char *c, unsigned int idx) {
       unsigned int b0=(c[idx]) << 3 ; 
       unsigned int b1=(c[idx+1]) << 2 ;
       unsigned int b2=(c[idx+2]) << 1 ;
       unsigned int b3=c[idx+3];

       return  b0 | b1 | b2 | b3;
}
31

Sven Robertz's avatar
Sven Robertz committed
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
static inline unsigned int get32(buffer *b) {
       unsigned int res = unpack32(b->c, b->idx);
       b->idx+=4;
       return res;
}


/* aux method for reading labcomm varint from a char* 
   size is an out parameter: if not NULL the number of bytes read will be written here
*/
static int unpack_varint(unsigned char *buf, unsigned int idx, unsigned char *size) {
        unsigned int res = 0;
        unsigned int i=0;
        unsigned char cont = TRUE;

        do {
                unsigned char c = buf[idx+i];
                res |= (c & 0x7f) << 7*i;
                cont = c & 0x80;
                i++;
        } while(cont);

	if(size != NULL)
		*size = i; 
        return res;
57
58
59
60
61
62
63
64
65
66
67
68
69
}

void dumpStack(buffer *b) {
#ifdef DEBUG_STACK
	int i;
	printf("=== stack: ");
	for(i=0; i<STACK_SIZE; i++) { //HERE BE DRAGONS
		printf("%2.2x ", b->stack[i]);
	}
	printf("    top==%d\n", b->top);
#endif
}

Sven Robertz's avatar
Sven Robertz committed
70
void push(buffer *b, void* e) {
71
72
73
74
	b->stack[b->top]=e;
	b->top=b->top-1;
	dumpStack(b);
}
Sven Robertz's avatar
Sven Robertz committed
75
void* pop(buffer *b) {
76
77
78
79
80
81
82
	b->top=b->top+1;
	return b->stack[b->top];
}
	
int init_buffer(buffer *b, size_t size, size_t stacksize) {
	b->c = malloc(size);
	b->capacity = size;
83
	b->size = 0;
84
85
86
	b->idx = 0;

	b->stack = calloc(stacksize, sizeof(b->stack));
87
	b->stacksize = stacksize;
88
89
90
91
92
	b->top = stacksize-1;

	return b->c == NULL || b->stack == NULL;
}

93
94
95
96
97
98
99
int read_file(FILE *f, buffer *b) {
        int s = fread(b->c, sizeof(char), b->capacity, f);
        b->size = s;
        b->idx=0;
        return s;
}

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
int more(buffer *b) 
{
	return b->idx < b->size;
}

unsigned char get(buffer *b) {
	return b->c[b->idx++];
}

unsigned char peek(buffer *b) {
	return b->c[b->idx];
}

void advance(buffer *b) {
	b->idx++;
}

void advancen(buffer *b, size_t n) {
	b->idx+=n;
}

Sven Robertz's avatar
Sven Robertz committed
121
122
unsigned int peek_varint(buffer *b, unsigned char *size) {
        return unpack_varint(b->c, b->idx, size);
123
124
}

Sven Robertz's avatar
Sven Robertz committed
125
126
127
128
129
unsigned int get_varint(buffer *b) {
        unsigned char size;
        unsigned int res = peek_varint(b, &size);
        advancen(b, size);
        return res;
130
}
Sven Robertz's avatar
Sven Robertz committed
131
132
133
134
unsigned int get_varint_size(buffer *b, unsigned char *size) {
        unsigned int res = peek_varint(b, size);
        advancen(b, *size);
        return res;
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}

void getStr(buffer *b, char *dest, size_t size) {
	int rem = b->size - b->idx;
	if( size > rem ) 
		size = rem;
	strncpy(dest, &b->c[b->idx], size);
	b->idx += size;
}

unsigned int signatures_length[MAX_SIGNATURES];
unsigned char signatures_name[MAX_SIGNATURES][MAX_NAME_LEN]; //HERE BE DRAGONS: add range checks
unsigned char signatures[MAX_SIGNATURES][MAX_SIG_LEN]; 

unsigned int get_signature_len(unsigned int uid){
Sven Robertz's avatar
Sven Robertz committed
150
	return signatures_length[uid-LABCOMM_USER];
151
152
}
unsigned char* get_signature_name(unsigned int uid){
Sven Robertz's avatar
Sven Robertz committed
153
	return &signatures_name[uid-LABCOMM_USER][1];
154
155
}
unsigned char* get_signature(unsigned int uid){
Sven Robertz's avatar
Sven Robertz committed
156
	return signatures[uid-LABCOMM_USER];
157
158
159
160
161
162
163
164
165
}

void dump_signature(unsigned int uid){
	int i;
	unsigned int len = get_signature_len(uid);
	printf("signature for uid %x : %s (len=%d):\n", uid, get_signature_name(uid), len);
	unsigned char* sig = get_signature(uid);
	for(i=0; i<len; i++) {
		printf("%2.2x ",sig[i]);
166
		if( i < len-1 && (i+1)%8==0 ) printf("\n");
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	}
	printf("\n");
}

int labcomm_sizeof(unsigned int type)
{
	switch(type) {
		case TYPE_BOOLEAN :
		case TYPE_BYTE : 
			return 1;
		case TYPE_SHORT : 
			return 2;
		case TYPE_INTEGER :
		case TYPE_FLOAT : 
			return 4;
		case TYPE_LONG :
		case TYPE_DOUBLE : 
			return 8;
		default:
			printf("labcomm_sizeof(%x)\n", type);
		 	error("labcomm_sizeof should only be called for primitive types");
	}
}

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
static int accept_packet(buffer *d);
static int accept_type_decl(buffer *d);
static int accept_sample_decl(buffer *d);
static int accept_user_id(buffer *d);
static int accept_string(buffer *d);
static int accept_string_length(buffer *d);
static int accept_char(buffer *d);
static int accept_type(buffer *d);
static int accept_boolean_type(buffer *d);
static int accept_byte_type(buffer *d);
static int accept_short_type(buffer *d);
static int accept_integer_type(buffer *d);
static int accept_long_type(buffer *d);
static int accept_float_type(buffer *d);
static int accept_long_type(buffer *d);
static int accept_string_type(buffer *d);
static int accept_array_decl(buffer *d);
static int accept_number_of_indices(buffer *d);
static int accept_indices(buffer *d);
static int accept_variable_index(buffer *d);
static int accept_fixed_index(buffer *d);
static int accept_struct_decl(buffer *d);
static int accept_number_of_fields(buffer *d);
static int accept_field(buffer *d);
static int accept_sample_data(buffer *d);
static int accept_packed_sample_data(buffer *d);
217

Sven Robertz's avatar
Sven Robertz committed
218
219
220
221
222
223
224
225
226
227
228
229
static unsigned char labcomm_varint_sizeof(unsigned int i)
{
	if(i < 128) {
		return 1;
	} else {
		unsigned char res = 1;
		while (i >>= 7) ++res;

		return res;
	}
}

230
int do_parse(buffer *d) {
Sven Robertz's avatar
Sven Robertz committed
231
232
        unsigned char nbytes;
        unsigned int type = peek_varint(d, &nbytes) ;
233
	if(type == TYPE_DECL ) {
Sven Robertz's avatar
Sven Robertz committed
234
		advancen(d, nbytes);
235
		accept_user_id(d);
Sven Robertz's avatar
Sven Robertz committed
236
		unsigned int uid = (unsigned int) (unsigned long) pop(d);
237
		VERBOSE_PRINTF(", name = ");
238
		accept_string(d);
Sven Robertz's avatar
Sven Robertz committed
239
240
241
242
243
		pop(d); // ignore, for now. 
#ifdef RETURN_STRINGS
		char *str = (char *) pop(d);
		free(str);
#endif
244
		VERBOSE_PRINTF(" : ");
245
		accept_type(d);
Sven Robertz's avatar
Sven Robertz committed
246
		unsigned int type = (unsigned int) (unsigned long) pop(d);
247
248

		//push(d, type);
249
		VERBOSE_PRINTF("\n");
250
251
		return TRUE;
	} else if (type == SAMPLE_DECL) {
Sven Robertz's avatar
Sven Robertz committed
252
		advancen(d, nbytes);
253
		VERBOSE_PRINTF("sample_decl ");
254
255
		accept_user_id(d);
		unsigned int nstart = d->idx;
Sven Robertz's avatar
Sven Robertz committed
256
		unsigned int uid = (unsigned int) (unsigned long) pop(d);
257
		VERBOSE_PRINTF(", name = ");
258
259
		accept_string(d);
		unsigned int start = d->idx;
Sven Robertz's avatar
Sven Robertz committed
260
261
262
263
264
265
		unsigned int nlen = (unsigned int) (unsigned long) pop(d);
#ifdef RETURN_STRINGS
		char *str = (char *) pop(d);
		free(str);
#endif
		unsigned char lenlen = labcomm_varint_sizeof(nlen);
266
		accept_type(d);
267
		//printf(" : ");
268
269
270
		//unsigned int dt = pop(d);
		unsigned int end = d->idx;
		unsigned int len = end-start;
271

272
		if(len <= MAX_SIG_LEN) {
Sven Robertz's avatar
Sven Robertz committed
273
274
			signatures_length[uid-LABCOMM_USER] = len;
			memcpy(signatures[uid-LABCOMM_USER], &d->c[start], len);
275
276
277
		} else {
			error("sig longer than max length (this ought to be dynamic...)");
		}
278
279

		if(nlen < MAX_NAME_LEN) { // leave 1 byte for terminating NULL
Sven Robertz's avatar
Sven Robertz committed
280
281
282
			signatures_name[uid-LABCOMM_USER][0] = nlen;
			memcpy(signatures_name[uid-LABCOMM_USER], &d->c[nstart+lenlen-1], nlen+1);
			signatures_name[uid-LABCOMM_USER][nlen+1]=0;
283
284
285
		} else {
			error("sig name longer than max length (this ought to be dynamic...");
		}
286
		VERBOSE_PRINTF("signature for uid %x: %s (start=%x,end=%x, nlen=%d,len=%d)\n", uid, get_signature_name(uid), start,end, nlen, len);
287
	} else {
288
#ifdef EXIT_WHEN_RECEIVING_DATA
289
290
		printf("*** got sample data, exiting\n");
		exit(0);
291
292
293
#else
		accept_sample_data(d);
#endif
294
295
296
	}
}

297
static int accept_user_id(buffer *d){
Sven Robertz's avatar
Sven Robertz committed
298
299
300
301
        unsigned char nbytes;
	unsigned int uid = peek_varint(d, &nbytes);
	if(uid >= LABCOMM_USER) {
		advancen(d, nbytes);
302
		VERBOSE_PRINTF("uid = %x ", uid);
Sven Robertz's avatar
Sven Robertz committed
303
		push(d, (void *) (unsigned long) uid);
304
305
306
307
308
309
		return TRUE;
	} else {
		return FALSE;
	}
}

310
static int accept_string(buffer *d){
Sven Robertz's avatar
Sven Robertz committed
311
	unsigned int len = get_varint(d);
312
	char *str=malloc(len);
313
314
	getStr(d, str, len); 
	VERBOSE_PRINTF("%s", str);
315
316
317
318
319
#ifdef RETURN_STRINGS
	push(d, str);
#else
	free(str);
#endif
Sven Robertz's avatar
Sven Robertz committed
320
	push(d, (void *) (unsigned long) len);
321
322
	return TRUE;
}
323
static int accept_type(buffer *d){
Sven Robertz's avatar
Sven Robertz committed
324
325
	unsigned char nbytes;
        unsigned int type = peek_varint(d, &nbytes) ;
326
327
	switch(type) {
		case TYPE_BOOLEAN :
328
			VERBOSE_PRINTF("boolean");
Sven Robertz's avatar
Sven Robertz committed
329
			advancen(d, nbytes);
330
331
			break;
		case TYPE_BYTE :
332
			VERBOSE_PRINTF("byte");
Sven Robertz's avatar
Sven Robertz committed
333
			advancen(d, nbytes);
334
335
			break;
		case TYPE_SHORT :
336
			VERBOSE_PRINTF("short");
Sven Robertz's avatar
Sven Robertz committed
337
			advancen(d, nbytes);
338
339
			break;
		case TYPE_INTEGER :
340
			VERBOSE_PRINTF("integer");
Sven Robertz's avatar
Sven Robertz committed
341
			advancen(d, nbytes);
342
343
			break;
		case TYPE_LONG :
344
			VERBOSE_PRINTF("long");
Sven Robertz's avatar
Sven Robertz committed
345
			advancen(d, nbytes);
346
347
			break;
		case TYPE_FLOAT :
348
			VERBOSE_PRINTF("float");
Sven Robertz's avatar
Sven Robertz committed
349
			advancen(d, nbytes);
350
351
			break;
		case TYPE_DOUBLE :
352
			VERBOSE_PRINTF("double");
Sven Robertz's avatar
Sven Robertz committed
353
			advancen(d, nbytes);
354
355
			break;
		case TYPE_STRING :
356
			VERBOSE_PRINTF("string");
Sven Robertz's avatar
Sven Robertz committed
357
			advancen(d, nbytes);
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
			break;
		case ARRAY_DECL :
			accept_array_decl(d);
			break;
		case STRUCT_DECL :
			accept_struct_decl(d);
			break;
		default :
			printf("accept_basic_type default (type==%x) should not happen\n", type);
			return FALSE;
	}
	//push(d,type);
	return TRUE;
}

373
static int accept_array_decl(buffer *d){
Sven Robertz's avatar
Sven Robertz committed
374
375
        unsigned char nbytes;
        unsigned int tid = peek_varint(d, &nbytes) ;
376
	if(tid == ARRAY_DECL) {
Sven Robertz's avatar
Sven Robertz committed
377
378
		advancen(d, nbytes);
		unsigned int nidx = get_varint(d);
379
		VERBOSE_PRINTF("%d dim array", nidx);
380
381
382
383
		int i;
		unsigned int numVar=0;
		unsigned int size=1;
		for(i=0; i<nidx; i++) {
Sven Robertz's avatar
Sven Robertz committed
384
			unsigned int idx = get_varint(d);
385
386
			if(idx == 0) {
				numVar++;
387
				VERBOSE_PRINTF("[_] ");
388
			} else {
389
				VERBOSE_PRINTF("[%d] ", idx);
390
391
392
				size*=idx;
			}
		}
393
		VERBOSE_PRINTF(" of ");
394
		unsigned int et=accept_type(d);
395
		//pop(d);
396
397
398
399
400
401
402
		//push(d,tid);
		return TRUE;
	} else {
		printf("accept_array_decl: type=%x, should not happen\n",tid);
		return FALSE;
	}
}
403
static int accept_struct_decl(buffer *d){
Sven Robertz's avatar
Sven Robertz committed
404
405
	unsigned char nbytes;
        unsigned int tid = peek_varint(d, &nbytes) ;
406
	if(tid == STRUCT_DECL) {
Sven Robertz's avatar
Sven Robertz committed
407
408
		advancen(d, nbytes);
		unsigned int nf = get_varint(d);
409
		VERBOSE_PRINTF(", %d field struct:\n", nf);
410
411
412
413
		int i;
		int numVar=0;
		int size=0;
		for(i=0; i<nf; i++) {
414
			VERBOSE_PRINTF("\t");
415
416
417
418
419
420
421
422
423
			accept_field(d);
		}
//		push(d,tid);
		return TRUE;
	} else {
		printf("accept_struct_decl: type=%x, should not happen\n",tid);
		return FALSE;
	}
}
424
static int accept_field(buffer *d){
425
	VERBOSE_PRINTF("field ");
426
	accept_string(d);
427
	pop(d); // ignore, for now
Sven Robertz's avatar
Sven Robertz committed
428
429
430
431
#ifdef RETURN_STRINGS
		char *str = (char *) pop(d);
		free(str);
#endif
432
	VERBOSE_PRINTF(" : ");
433
	accept_type(d);
434
	VERBOSE_PRINTF("\n");
435
}
436
static int accept_sample_data(buffer *d){
437
	accept_user_id(d);
Sven Robertz's avatar
Sven Robertz committed
438
	unsigned int uid = (unsigned int) (unsigned long) pop(d);	
439
440
441
442
443
444
445
446
447
448
449
	printf("sample data... %x\n", uid);
#ifdef DEBUG
	dump_signature(uid);
#endif
	unsigned int siglen = get_signature_len(uid);
	unsigned char *sig = get_signature(uid);
	skip_packed_sample_data(d, sig, siglen);
	return TRUE;
}
//int accept_packed_sample_data(buffer *d){

450
static int skip_type(unsigned int,buffer*,unsigned char*,unsigned int,unsigned int*) ;
451

452
static int skip_array(buffer *d, unsigned char *sig, unsigned int len, unsigned int *pos) {
453
454
	unsigned int skip = 0;
	unsigned int tot_nbr_elem_tmp = 1;
Sven Robertz's avatar
Sven Robertz committed
455
456
	unsigned char nbytes;
	unsigned int nIdx = unpack_varint(sig, *pos, &nbytes);
457
	VERBOSE_PRINTF("skip_array: nIdx = %d (from sig)\n", nIdx);
Sven Robertz's avatar
Sven Robertz committed
458
	*pos +=nbytes;
459
460
461
462
463
464
	unsigned int idx[nIdx];
	unsigned int nVar=0;

	unsigned int i;

	for(i=0; i<nIdx; i++) {
Sven Robertz's avatar
Sven Robertz committed
465
466
		idx[i] = unpack_varint(sig, *pos, &nbytes);
		*pos += nbytes;
467
		VERBOSE_PRINTF("skip_array: idx[%d]=%d (from sig)\n", i, idx[i]);
468
469
470
471
472
473
474
475
		if(idx[i] == 0) {
			nVar++;
		} else {
			tot_nbr_elem_tmp *= idx[i];
		}
	}
	unsigned int var[nVar];

Sven Robertz's avatar
Sven Robertz committed
476
	unsigned char varSize=0; // total number of bytes required for var size fields
477
	for(i=0; i<nVar; i++) {
Sven Robertz's avatar
Sven Robertz committed
478
479
		var[i] = get_varint_size(d, &nbytes);	
		varSize += nbytes;
480
		VERBOSE_PRINTF("skip_array: var[%d]=%d (from sample)\n", i, var[i]);
Sven Robertz's avatar
Sven Robertz committed
481
		tot_nbr_elem_tmp *= var[i];
482
483
	}

Sven Robertz's avatar
Sven Robertz committed
484
485
	unsigned int type = unpack_varint(sig, *pos, &nbytes);
	*pos+=nbytes;
486
487
488
489
490

	unsigned int elemSize = labcomm_sizeof(type);

	skip = elemSize * tot_nbr_elem_tmp;

491
	VERBOSE_PRINTF("skip_array: skip: %d * %d = %d\n", tot_nbr_elem_tmp, elemSize ,skip);
492
493
494
	
	advancen(d, skip);

Sven Robertz's avatar
Sven Robertz committed
495
496
	//return skip + 4*nVar;
	return skip + varSize;
497
498
499
}

int skip_struct(buffer *d, unsigned char *sig, unsigned int len, unsigned int *pos) {
Sven Robertz's avatar
Sven Robertz committed
500
501
502
	unsigned char nbytes;
	unsigned int nFields = unpack_varint(sig,*pos, &nbytes);
	*pos += nbytes;
503
504
	unsigned int i;
	unsigned int skipped=0;
505
	VERBOSE_PRINTF("skip_struct (%d fields)\n", nFields);
506
507
	for(i=0; i<nFields; i++) {
		//skip name 
Sven Robertz's avatar
Sven Robertz committed
508
		unsigned int namelen = unpack_varint(sig, *pos, &nbytes);
509
#ifdef DEBUG
510
		VERBOSE_PRINTF("namelen==%d",namelen);
Sven Robertz's avatar
Sven Robertz committed
511
		char name[namelen+1];
Sven Robertz's avatar
Sven Robertz committed
512
		strncpy(name, sig+*pos+nbytes, namelen);
513
514
		name[namelen]=0;
		VERBOSE_PRINTF(", name = %s",name);
515
#endif
Sven Robertz's avatar
Sven Robertz committed
516
		*pos += (nbytes+namelen); // 32bit len + actual string
Sven Robertz's avatar
Sven Robertz committed
517

Sven Robertz's avatar
Sven Robertz committed
518
519
		unsigned int type = unpack_varint(sig, *pos, &nbytes);
		*pos += nbytes;
520
#ifdef DEBUG
521
		VERBOSE_PRINTF(": type == %x\n", type);
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#endif
		skipped += skip_type(type, d, sig, len, pos);
	}
	return skipped;
}
#ifndef QUIET
/* print and skip */
int skip_type(unsigned int type, buffer *d, 
		unsigned char *sig, unsigned int len, unsigned int *pos) 
{
	int skipped=0;
	printf("skip_type %x:", type);
	switch(type) {
		case TYPE_BOOLEAN :
			printf("boolean [%d]\n", get(d));
			skipped++;
			break;
		case TYPE_BYTE : 
			printf("byte [%d]\n", get(d));
			skipped++;
			break;
		case TYPE_SHORT : 
			//XXX not supported
			advancen(d,2); 
			skipped+=2;
			break;
		case TYPE_INTEGER :
			printf("integer [%d]\n", get32(d));
			skipped +=4;
			break;
		case TYPE_FLOAT : 
			//XXX not supported
			advancen(d,4); 
			skipped+=4;
			break;
		case TYPE_LONG :
		case TYPE_DOUBLE : 
			//XXX not supported
			advancen(d,8); 
			skipped+=8;
			break;
		case TYPE_STRING :
Sven Robertz's avatar
Sven Robertz committed
564
565
566
			{
			unsigned char nbytes;
			unsigned int len = get_varint_size(d, &nbytes);
567
568
569
570
571
			int i;
			printf("string [");
			for(i=0; i<len; i++)
				printf("%c", get(d));
			printf("]\n");
Sven Robertz's avatar
Sven Robertz committed
572
			skipped+=len+nbytes;
573
574
575
576
			break;}
		case ARRAY_DECL :
			printf("array\n");
			skipped += skip_array(d, sig, len, pos);
Sven Robertz's avatar
Sven Robertz committed
577
			break;
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
		case STRUCT_DECL :
			printf("struct\n");
			skipped += skip_struct(d, sig, len, pos);
			break;
		default:
			printf("ERROR: skip_type: type = %x\n", type);
			exit(1);
	}
	return skipped;
}
#else
int skip_type(unsigned int type, buffer *d, 
		unsigned char *sig, unsigned int len, unsigned int *pos) 
{
	int skipped=0;
593
	VERBOSE_PRINTF("skip_type %x\n", type);
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
	switch(type) {
		case TYPE_BOOLEAN :
		case TYPE_BYTE : 
			advancen(d,1); 
			skipped++;
			break;
		case TYPE_SHORT : 
			advancen(d,2); 
			skipped+=2;
			break;
		case TYPE_INTEGER :
		case TYPE_FLOAT : 
			advancen(d,4); 
			skipped+=4;
			break;
		case TYPE_LONG :
		case TYPE_DOUBLE : 
			advancen(d,8); 
			skipped+=8;
			break;
		case TYPE_STRING :
Sven Robertz's avatar
Sven Robertz committed
615
616
617
			{
			unsigned char nbytes;
			unsigned int len = get_varint_size(d, &nbytes);
618
			advancen(d,len);
Sven Robertz's avatar
Sven Robertz committed
619
			skipped+=len+nbytes;
620
621
622
			break;}
		case ARRAY_DECL :
			skipped += skip_array(d, sig, len, pos);
Sven Robertz's avatar
Sven Robertz committed
623
			break;
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
		case STRUCT_DECL :
			skipped += skip_struct(d, sig, len, pos);
			break;
		default:
			printf("ERROR: skip_type: type = %x\n", type);
			exit(1);
	}
	return skipped;
}
#endif

/* parse signature and skip the corresponding bytes in the buffer 
 */
int skip_packed_sample_data(buffer *d, unsigned char *sig, unsigned int siglen) {
	unsigned int pos = 0; 		//current position in signature
	unsigned int skipped = 0;	//skipped byte counter
	while(pos < siglen) {
Sven Robertz's avatar
Sven Robertz committed
641
642
643
		unsigned char nbytes;
		unsigned int type = unpack_varint(sig,pos, &nbytes);
		pos+=nbytes;
644
645
646
647
648
		skipped += skip_type(type, d, sig, siglen, &pos);
	}	
	printf("skipped %d bytes\n", skipped);
	return TRUE;
}