labcomm_parser.c 14.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/* labcomm_parser.c:
 * 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
13
#define DEBUG 

14
15
16
#define FALSE 0
#define TRUE 1

Sven Robertz's avatar
Sven Robertz committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define	USER_ID_BASE	0x00000080

typedef enum{
	TYPE_DECL = 0x00000001,
	SAMPLE_DECL = 0x00000002,

	ARRAY_DECL = 0x00000010,
	STRUCT_DECL = 0x00000011,

	TYPE_BOOLEAN  = 0x00000020,
	TYPE_BYTE  = 0x00000021,
	TYPE_SHORT  = 0x00000022,
	TYPE_INTEGER  = 0x00000023,
	TYPE_LONG  = 0x00000024,
	TYPE_FLOAT  = 0x00000025,
	TYPE_DOUBLE  = 0x00000026,
	TYPE_STRING  = 0x00000027
} labcomm_type ;

36
void error(char *s) {
Sven Robertz's avatar
Sven Robertz committed
37
38
	fprintf(stderr, "%s", s);
	fprintf(stderr, "\nexiting\n");
39
40
	exit(1);
}
Sven Robertz's avatar
Sven Robertz committed
41

42
43
44
#define BUF_SIZE 1024
#define STACK_SIZE 16

Sven Robertz's avatar
Sven Robertz committed
45
/* internal type: stack for the parser */
46
47
48
49
50
51
52
53
54
typedef struct {
	unsigned char* c;
	unsigned int size;
	unsigned int capacity;
	unsigned int idx;
	unsigned int* stack;
	unsigned int top;
} buffer;

Sven Robertz's avatar
Sven Robertz committed
55
56
57
58
59
60
61
62
63
/* aux method for reading a big endian uint32 from a char* (i.e. ntohl but for explicit char*) */
static 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;
}
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

void dumpStack(buffer *b) {
#ifdef DEBUG
	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
}

void push(buffer *b, unsigned int e) {
	b->stack[b->top]=e;
	b->top=b->top-1;
	dumpStack(b);
}
unsigned int pop(buffer *b) {
	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;
	b->idx = 0;

	b->stack = calloc(stacksize, sizeof(b->stack));
	b->top = stacksize-1;

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

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;
}

unsigned int peek32(buffer *b) {
	return unpack32(b->c, b->idx);
}
Sven Robertz's avatar
Sven Robertz committed
121

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
void advance32(buffer *b) {
	b->idx+=4;
}

unsigned int get32(buffer *b) {
	unsigned int res = peek32(b);
	advance32(b);
	return res;
}

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;
}

//XXX experimental
Sven Robertz's avatar
Sven Robertz committed
141
142
143
144
145
146
#define MAX_SIGNATURES 10
#define MAX_NAME_LEN 32 
#define MAX_SIG_LEN 128
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]; 
147
148
149
150
151
152
153
154
155
156
157
158
159
160

unsigned int get_signature_len(unsigned int uid){
	return signatures_length[uid-USER_ID_BASE];
}
unsigned char* get_signature_name(unsigned int uid){
	return &signatures_name[uid-USER_ID_BASE][1];
}
unsigned char* get_signature(unsigned int uid){
	return signatures[uid-USER_ID_BASE];
}

void dump_signature(unsigned int uid){
	int i;
	unsigned int len = get_signature_len(uid);
Sven Robertz's avatar
Sven Robertz committed
161
	printf("signature for uid %x : %s (len=%d):\n", uid, get_signature_name(uid), len);
162
163
164
165
166
167
168
169
	unsigned char* sig = get_signature(uid);
	for(i=0; i<len; i++) {
		printf("%2.2x ",sig[i]);
		if( (i+1)%8==0 ) printf("\n");
	}
	printf("\n");
}

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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");
	}
}

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

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;
}

void test_read(buffer *buf) {
	int r = read_file(stdin, buf);
	printf("read %d bytes:\n\n", r);
	int i;
	for(i=0; i<r; i++) {
		printf("%x ", buf->c[i]);
		if(i%8 == 7) printf("\n");
	}
	printf("\n");
}
int main() {
	buffer buf;

	if( init_buffer(&buf, BUF_SIZE, STACK_SIZE) ) {
		printf("failed to init buffer\n");
		exit(1);
	}
	test_read(&buf);
	do{
		printf("trying to read another packet\n");
	} while(more(&buf) && accept_packet(&buf)); 
	printf("done\n");
}

	
int accept_packet(buffer *d) {
	if(accept_type_decl(d)) {
		printf("*** got type decl\n");
	} else if(accept_sample_decl(d)) {
		printf("*** got sample decl\n");
	} else if(accept_sample_data(d)) {
		printf("*** got sample data\n");
	} else {
//	error("unexpected %x, expecting packet", d->c[d->idx]); 
		error("packet");
	}
}
int accept_type_decl(buffer *d){
	unsigned int type = peek32(d) ;
	if(type == TYPE_DECL ) {
		advance32(d);
		accept_user_id(d);
		unsigned int uid = pop(d);
		accept_string(d);
269
270
271
272
		// ignore, for now. This should do something as
		// char *name = (char*) pop(d);
		// store or print name
		// free(name)
273
274
275
		accept_type(d);
		unsigned int type = pop(d);

276
		//push(d, type);
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
		return TRUE;
	}else {
		return FALSE;
	}
}

int accept_sample_decl(buffer *d){
	unsigned int type = peek32(d) ;
	if (type == SAMPLE_DECL) {
		advance32(d);
		accept_user_id(d);
		unsigned int nstart = d->idx;
		unsigned int uid = pop(d);
		accept_string(d);
		unsigned int start = d->idx;
		unsigned int nlen = pop(d);
		accept_type(d);
294
		unsigned int dt = pop(d);
295
296
		unsigned int end = d->idx;
		unsigned int len = end-start;
Sven Robertz's avatar
Sven Robertz committed
297
298
299
300
301
302
303
304
305
306
307
308
		if(len <= MAX_SIG_LEN) {
			signatures_length[uid-USER_ID_BASE] = len;
			memcpy(signatures_name[uid-USER_ID_BASE], &d->c[nstart+3], nlen+1);
		} else {
			error("sig longer than max length (this ought to be dynamic...)");
		}
		if(nlen < MAX_NAME_LEN) { // reserve space for terminating NULL
			signatures_name[uid-USER_ID_BASE][nlen+1]=0;
			memcpy(signatures[uid-USER_ID_BASE], &d->c[start], len);
		} else {
			error("sig name longer than max length (this ought to be dynamic...");
		}
309
		printf("signature for uid %x: %s (start=%x,end=%x, nlen=%d,len=%d)\n", uid, get_signature_name(uid), start,end, nlen, len);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
		return TRUE;
	} else {
		return FALSE;
	}
}

int accept_user_id(buffer *d){
	unsigned int uid = peek32(d);
	if(uid >= USER_ID_BASE) {
		push(d, uid);
		advance32(d);
		//printf("uid = %x\n", uid);
		return TRUE;
	} else {
		return FALSE;
	}
}

int accept_string(buffer *d){
	unsigned int len = get32(d);
	push(d, len);
	char *str=malloc(len);
	getStr(d, str, len);
333
334
335
336
	printf("%s\n", str);
#ifdef RETURN_STRINGS
	push(d, str);
#else
337
	free(str);
338
#endif
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
	return TRUE;
}
// included above
// int accept_string_length(buffer *d){
//	unsigned int uid = get32(d);
//	return TRUE;
//}
//int accept_char(buffer *d){
//}

int accept_type(buffer *d){
	if(accept_basic_type(d)) {
	} else if(accept_user_id(d)) {
		//printf("user_id \n");
	} else if(accept_array_decl(d)) {
		//printf("array_decl \n");
	} else if(accept_struct_decl(d)) {
		//printf("struct_decl \n");
	} else {	
		return FALSE;
	}
}
int accept_basic_type(buffer *d){
	unsigned int type = peek32(d);
	switch(type) {
		case TYPE_BOOLEAN :
			//printf("boolean \n");
			break;
		case TYPE_BYTE :
			//printf("byte \n");
			break;
		case TYPE_SHORT :
			//printf("short \n");
			break;
		case TYPE_INTEGER :
			//printf("integer \n");
			break;
		case TYPE_LONG :
			//printf("long \n");
			break;
		case TYPE_FLOAT :
			//printf("float \n");
			break;
		case TYPE_DOUBLE :
			//printf("double \n");
			break;
		case TYPE_STRING :
			//printf("string \n");
			break;
		default :
			return FALSE;
	}
	advance32(d);
	push(d,type);
	return TRUE;
}
#if 0 // handle all basic types above
int accept_boolean_type(buffer *d){
}
int accept_byte_type(buffer *d){
}
int accept_short_type(buffer *d){
}
int accept_integer_type(buffer *d){
}
int accept_long_type(buffer *d){
}
int accept_float_type(buffer *d){
}
int accept_string_type(buffer *d){
}
#endif
int accept_array_decl(buffer *d){
	unsigned int tid = peek32(d);
	if(tid == ARRAY_DECL) {
		advance32(d);
		unsigned int nidx = get32(d);
		printf("%d dim array: ", nidx);
		int i;
		unsigned int numVar=0;
		unsigned int size=1;
		for(i=0; i<nidx; i++) {
			unsigned int idx = get32(d);
			if(idx == 0) {
				numVar++;
424
				printf("variable index (numVar=%d), ",numVar);
425
			} else {
426
				printf("fixed index: %d, ", idx);
427
428
429
430
				size*=idx;
			}
			printf("\n");
		}
431
432
433
434
		unsigned int et=accept_type(d);
		printf("array element type: %x\n", et);
		pop(d);
		//push(d,tid);
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
		return TRUE;
	} else {
		return FALSE;
	}
}
#if 0
int accept_number_of_indices(buffer *d){
}
int accept_indices(buffer *d){
}
int accept_variable_index(buffer *d){
}
int accept_fixed_index(buffer *d){
}
#endif
int accept_struct_decl(buffer *d){
	unsigned int tid = peek32(d);
	if(tid == STRUCT_DECL) {
		advance32(d);
		unsigned int nf = get32(d);
		printf("%d field struct: ", nf);
		int i;
		int numVar=0;
		int size=0;
		for(i=0; i<nf; i++) {
			accept_field(d);
		}
462
		push(d,tid);
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
		return TRUE;
	} else {
		return FALSE;
	}
}
#if 0
int accept_number_of_fields(buffer *d){
}
#endif
int accept_field(buffer *d){
	accept_string(d);
	// ignore, for now
	accept_type(d);
	unsigned int type = pop(d);
	printf("field: %x \n", type);
}
int accept_sample_data(buffer *d){
	accept_user_id(d);
	unsigned int uid = pop(d);	
	printf("sample data... %x\n", uid);
Sven Robertz's avatar
Sven Robertz committed
483
#ifdef DEBUG
484
	dump_signature(uid);
Sven Robertz's avatar
Sven Robertz committed
485
#endif
486
487
488
489
490
491
492
493
494
	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){

int skip_type(unsigned int,buffer*,unsigned char*,unsigned int,unsigned int*) ;

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
int skip_array(buffer *d, unsigned char *sig, unsigned int len, unsigned int *pos) {
	unsigned int skip = 0;
	unsigned int tot_nbr_elem_tmp = 1;
	unsigned int nIdx = unpack32(sig, *pos);
	printf("skip_array: nIdx = %d\n", nIdx);
	*pos +=4;
	unsigned int idx[nIdx];
	unsigned int nVar=0;

	unsigned int i;

	for(i=0; i<nIdx; i++) {
		idx[i] = unpack32(sig, *pos);
		*pos += 4;
		printf("skip_array: idx[%d]=%d\n", i, idx[i]);
		if(idx[i] == 0) {
			nVar++;
		} else {
			tot_nbr_elem_tmp *= idx[i];
		}
	}
	unsigned int var[nVar];

	for(i=0; i<nVar; i++) {
		var[i] = get32(d);	
		printf("skip_array: var[%d]=%d\n", i, var[i]);
	}

	unsigned int type = unpack32(sig, *pos);
	*pos+=4;

	unsigned int elemSize = labcomm_sizeof(type);

	skip = elemSize * tot_nbr_elem_tmp;

	printf("skip_array: skip: %d * %d = %d\n", elemSize, tot_nbr_elem_tmp, skip);
	
	advancen(d, skip);

	skip += nVar;

	return skip;
}

539
540
541
542
543
544
545
546
547
548
int skip_struct(buffer *d, unsigned char *sig, unsigned int len, unsigned int *pos) {
	unsigned int nFields = unpack32(sig,*pos);
	*pos += 4;
	unsigned int i;
	unsigned int skipped=0;
	printf("skip_struct (%d fields)\n", nFields);
	for(i=0; i<nFields; i++) {
		//skip name 
		unsigned int namelen = unpack32(sig, *pos);
		*pos += (4+namelen); // 32bit len + actual string
Sven Robertz's avatar
Sven Robertz committed
549
#ifdef DEBUG
550
		printf("namelen==%d \n",namelen);
Sven Robertz's avatar
Sven Robertz committed
551
#endif
552
553
		unsigned int type = unpack32(sig, *pos);
		*pos += 4;
Sven Robertz's avatar
Sven Robertz committed
554
#ifdef DEBUG
555
		printf("type == %x\n", type);
Sven Robertz's avatar
Sven Robertz committed
556
#endif
557
558
559
560
561
562
563
564
565
566
		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;
Sven Robertz's avatar
Sven Robertz committed
567
	printf("skip_type %x:", type);
568
569
	switch(type) {
		case TYPE_BOOLEAN :
Sven Robertz's avatar
Sven Robertz committed
570
			printf("boolean [%d]\n", get(d));
571
572
573
			skipped++;
			break;
		case TYPE_BYTE : 
Sven Robertz's avatar
Sven Robertz committed
574
			printf("byte [%d]\n", get(d));
575
576
577
578
579
580
581
582
			skipped++;
			break;
		case TYPE_SHORT : 
			//XXX not supported
			advancen(d,2); 
			skipped+=2;
			break;
		case TYPE_INTEGER :
Sven Robertz's avatar
Sven Robertz committed
583
			printf("integer [%d]\n", get32(d));
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
			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 :
			{unsigned int len = get32(d);
			//advancen(d,len);
			int i;
Sven Robertz's avatar
Sven Robertz committed
601
			printf("string [");
602
603
			for(i=0; i<len; i++)
				printf("%c", get(d));
Sven Robertz's avatar
Sven Robertz committed
604
			printf("]\n");
605
606
607
			skipped+=len+4;
			break;}
		case ARRAY_DECL :
608
609
			printf("array\n");
			skipped += skip_array(d, sig, len, pos);
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
		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;
	printf("skip_type %x\n", type);
	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 :
			{unsigned int len = get32(d);
			advancen(d,len);
			skipped+=len+4;
			break;}
		case ARRAY_DECL :
652
653
			printf("array\n");
			skipped += skip_array(d, sig, len, pos);
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
		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) {
		unsigned int type = unpack32(sig,pos);
		pos+=4;
		skipped += skip_type(type, d, sig, siglen, &pos);
	}	
	printf("skipped %d bytes\n", skipped);
	return TRUE;
}