Commit 58b63c23 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

sig-parser working as before 2014 reorganization. Still WiP

parent 68cb6c1f
......@@ -54,6 +54,8 @@ ifeq ($(LABCOMM_EXPERIMENTAL),true)
experimental/labcomm_sig_parser.o \
experimental/test_sig_parser.o
experimental/test_sig_parser : experimental/labcomm_sig_parser.o experimental/test_sig_parser.c
endif
## Targets
......
......@@ -38,17 +38,17 @@ static void error(char *s) {
}
/* aux method for reading a big endian uint32 from a char* (i.e. ntohl but for explicit char*) */
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];
static int unpack32(unsigned char *c, int idx) {
int b0=(c[idx]) << 3 ;
int b1=(c[idx+1]) << 2 ;
int b2=(c[idx+2]) << 1 ;
int b3=c[idx+3];
return b0 | b1 | b2 | b3;
}
static inline unsigned int get32(labcomm_sig_parser_t *b) {
unsigned int res = unpack32(b->c, b->idx);
static inline int get32(labcomm_sig_parser_t *b) {
int res = unpack32(b->c, b->idx);
b->idx+=4;
return res;
}
......@@ -57,16 +57,16 @@ static inline unsigned int get32(labcomm_sig_parser_t *b) {
/* 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 unsigned int unpack_varint(unsigned char *buf,
unsigned int idx,
unsigned char *size)
static int unpack_varint(unsigned char *buf,
int idx,
size_t *size)
{
unsigned int res = 0;
int res = 0;
unsigned int i=0;
unsigned char cont = TRUE;
do {
unsigned char c = buf[idx+i];
char c = buf[idx+i];
res = (res << 7) | (c & 0x7f);
cont = c & 0x80;
i++;
......@@ -78,9 +78,9 @@ static unsigned int unpack_varint(unsigned char *buf,
}
void dumpValStack(labcomm_sig_parser_t *b) {
int i;
printf("=== value stack: ");
#ifdef DEBUG_STACK_VERBOSE
int i;
for(i=0; i<STACK_SIZE; i++) { //HERE BE DRAGONS
printf("%2.2x ", b->val_stack[i]);
}
......@@ -88,9 +88,9 @@ void dumpValStack(labcomm_sig_parser_t *b) {
printf(" top==%d\n", b->val_top);
}
void dumpPtrStack(labcomm_sig_parser_t *b) {
int i;
printf("=== pointer stack: ");
#ifdef DEBUG_STACK_VERBOSE
int i;
for(i=0; i<STACK_SIZE; i++) { //HERE BE DRAGONS
printf("%2.2x ", b->ptr_stack[i]);
}
......@@ -197,27 +197,27 @@ void advancen(labcomm_sig_parser_t *b, size_t n) {
b->idx+=n;
}
unsigned int peek_varint(labcomm_sig_parser_t *b, unsigned char *size) {
int peek_varint(labcomm_sig_parser_t *b, size_t *size) {
return unpack_varint(b->c, b->idx, size);
}
unsigned int get_varint(labcomm_sig_parser_t *b) {
unsigned char size;
unsigned int res = peek_varint(b, &size);
int get_varint(labcomm_sig_parser_t *b) {
size_t size;
int res = peek_varint(b, &size);
advancen(b, size);
return res;
}
unsigned int get_varint_size(labcomm_sig_parser_t *b, unsigned char *size) {
int get_varint_size(labcomm_sig_parser_t *b, size_t *size) {
unsigned int res = peek_varint(b, size);
advancen(b, *size);
return res;
}
void getStr(labcomm_sig_parser_t *b, char *dest, size_t size) {
void getStr(labcomm_sig_parser_t *b, unsigned char *dest, size_t size) {
int rem = b->size - b->idx;
if( size > rem )
size = rem;
strncpy(dest, &b->c[b->idx], size);
strncpy((char *)dest, (char *)&b->c[b->idx], size);
b->idx += size;
}
......@@ -240,7 +240,7 @@ unsigned char* get_signature(labcomm_sig_parser_t *p, unsigned int uid){
// return signatures_name_length[uid-LABCOMM_USER];
//}
unsigned char* get_signature_name(labcomm_sig_parser_t *p, unsigned int uid){
char* get_signature_name(labcomm_sig_parser_t *p, unsigned int uid){
//return signatures_name[uid-LABCOMM_USER];
return p->sig_ts[uid-LABCOMM_USER].name;
}
......@@ -278,6 +278,7 @@ static size_t labcomm_sizeof_primitive(unsigned int type)
default:
printf("labcomm_sizeof_primitive(%x)\n", type);
error("labcomm_sizeof_primitive should only be called for primitive types");
return 0;
}
}
......@@ -286,16 +287,19 @@ static size_t labcomm_sizeof_primitive(unsigned int type)
//static int accept_sample_decl(labcomm_sig_parser_t *d);
static int accept_user_id(labcomm_sig_parser_t *d);
static int accept_string(labcomm_sig_parser_t *d);
static int accept_string_length(labcomm_sig_parser_t *d);
static int accept_char(labcomm_sig_parser_t *d);
static int accept_type(labcomm_sig_parser_t *d);
static int accept_array_decl(labcomm_sig_parser_t *d);
#if 0
// UNUSED declarations
static int accept_string_length(labcomm_sig_parser_t *d);
static int accept_char(labcomm_sig_parser_t *d);
static int accept_number_of_indices(labcomm_sig_parser_t *d);
static int accept_indices(labcomm_sig_parser_t *d);
static int accept_variable_index(labcomm_sig_parser_t *d);
static int accept_fixed_index(labcomm_sig_parser_t *d);
static int accept_struct_decl(labcomm_sig_parser_t *d);
static int accept_number_of_fields(labcomm_sig_parser_t *d);
#endif
static int accept_struct_decl(labcomm_sig_parser_t *d);
static int accept_field(labcomm_sig_parser_t *d);
static int accept_sample_data(labcomm_sig_parser_t *d);
......@@ -350,12 +354,18 @@ static int accept_signature(labcomm_sig_parser_t *d)
free(str);
#endif
unsigned char lenlen = labcomm_varint_sizeof(nlen);
get_varint(d); // ignore sig len
VERBOSE_PRINTF("\ntype = ");
accept_type(d);
//printf(" : ");
//unsigned int dt = pop(d);
#ifdef USE_TYPE_AND_SIZE
unsigned int type = pop_val(d);
unsigned int enc_size = pop_val(d);
#else
pop_val(d); // unsigned int type
pop_val(d); // unsigned int enc_size
#endif
unsigned int end = d->idx;
unsigned int len = end-start;
......@@ -400,14 +410,18 @@ static int accept_signature(labcomm_sig_parser_t *d)
// HERE BE DRAGONS! what does the return value mean?
int accept_packet(labcomm_sig_parser_t *d) {
unsigned char nbytes;
size_t nbytes;
unsigned int type = peek_varint(d, &nbytes) ;
if(type == VERSION ) {
//XXX is this used? If so, is it correct?
advancen(d, nbytes);
VERBOSE_PRINTF("ignoring version ");
int len = get_varint(d); //ignore length field
advancen(d, len);
advancen(d, nbytes);//consume type field
get_varint(d); //ignore length field
VERBOSE_PRINTF("got version.\n");
accept_string(d);
pop_val(d); // ignore length, for now
#ifdef RETURN_STRINGS
char *str = (char *) pop_ptr(d);
free(str);
#endif
}else if(type == TYPE_DECL ) {
//XXX is this used? If so, is it correct?
advancen(d, nbytes);
......@@ -429,17 +443,24 @@ int accept_packet(labcomm_sig_parser_t *d) {
accept_sample_data(d);
#endif
} else {
#ifdef EXIT_ON_UNKNOWN_TAG
error("got unknown type (<LABCOMM_USER)");
exit(1);
#else
int len = get_varint(d); //ignore length field
printf("got unknown tag: 0x%x, skipping %d bytes\n",type, len);
advancen(d, len);
#endif
}
return TRUE;
}
static int accept_user_id(labcomm_sig_parser_t *d){
unsigned char nbytes;
unsigned int uid = peek_varint(d, &nbytes);
size_t nbytes;
int uid = peek_varint(d, &nbytes);
if(uid >= LABCOMM_USER) {
advancen(d, nbytes);
VERBOSE_PRINTF("uid = %x ", uid);
VERBOSE_PRINTF("uid = 0x%x ", uid);
push_val(d, uid);
return TRUE;
} else {
......@@ -449,7 +470,7 @@ static int accept_user_id(labcomm_sig_parser_t *d){
static int accept_string(labcomm_sig_parser_t *d){
unsigned int len = get_varint(d);
char *str=malloc(len);
unsigned char *str=malloc(len);
getStr(d, str, len);
VERBOSE_PRINTF("%s", str);
#ifdef RETURN_STRINGS
......@@ -462,7 +483,7 @@ static int accept_string(labcomm_sig_parser_t *d){
}
/* pushes size and type id */
static int accept_type(labcomm_sig_parser_t *d){
unsigned char nbytes;
size_t nbytes;
unsigned int type = peek_varint(d, &nbytes) ;
switch(type) {
case TYPE_BOOLEAN :
......@@ -527,8 +548,8 @@ static int accept_type(labcomm_sig_parser_t *d){
/* pushes size and element type */
static int accept_array_decl(labcomm_sig_parser_t *d){
unsigned char nbytes;
unsigned int tid = peek_varint(d, &nbytes) ;
size_t nbytes;
int tid = peek_varint(d, &nbytes) ;
if(tid == ARRAY_DECL) {
advancen(d, nbytes);
unsigned int nidx = get_varint(d);
......@@ -575,15 +596,17 @@ static int accept_array_decl(labcomm_sig_parser_t *d){
/* pushes size */
static int accept_struct_decl(labcomm_sig_parser_t *d){
unsigned char nbytes;
unsigned int tid = peek_varint(d, &nbytes) ;
size_t nbytes;
int tid = peek_varint(d, &nbytes) ;
if(tid == STRUCT_DECL) {
advancen(d, nbytes);
unsigned int nf = get_varint(d);
VERBOSE_PRINTF("%d field struct:\n", nf);
int i;
#ifdef USE_UNUSED_VARS
int numVar=0;
int size=0;
#endif
unsigned int fieldsizes=0;
for(i=0; i<nf; i++) {
accept_field(d);
......@@ -612,6 +635,7 @@ static int accept_field(labcomm_sig_parser_t *d){
pop_val(d); // ignore type, for now
// push(pop() is really a NOP , leave size on the stack when debugging done
VERBOSE_PRINTF("\n");
return TRUE;
}
static int accept_sample_data(labcomm_sig_parser_t *d){
accept_user_id(d);
......@@ -633,12 +657,12 @@ static int accept_sample_data(labcomm_sig_parser_t *d){
return TRUE;
}
static int skip_type(unsigned int,labcomm_sig_parser_t*,unsigned char*,unsigned int,unsigned int*) ;
static int skip_type(unsigned int,labcomm_sig_parser_t*,unsigned char*,unsigned int,int*) ;
static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, unsigned int *pos) {
static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, int len, int *pos) {
unsigned int skip = 0;
unsigned int tot_nbr_elem_tmp = 1;
unsigned char nbytes;
size_t nbytes;
unsigned int nIdx = unpack_varint(sig, *pos, &nbytes);
VERBOSE_PRINTF("skip_array: nIdx = %d (from sig)\n", nIdx);
*pos +=nbytes;
......@@ -657,7 +681,7 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int
tot_nbr_elem_tmp *= idx[i];
}
}
unsigned int var[nVar];
int var[nVar];
unsigned char varSize=0; // total number of bytes required for var size fields
for(i=0; i<nVar; i++) {
......@@ -667,7 +691,7 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int
tot_nbr_elem_tmp *= var[i];
}
unsigned int type = unpack_varint(sig, *pos, &nbytes);
int type = unpack_varint(sig, *pos, &nbytes);
*pos+=nbytes;
unsigned int elemSize = labcomm_sizeof_primitive(type);
......@@ -682,8 +706,8 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int
return skip + varSize;
}
int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, unsigned int *pos) {
unsigned char nbytes;
int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, int *pos) {
size_t nbytes;
unsigned int nFields = unpack_varint(sig,*pos, &nbytes);
*pos += nbytes;
unsigned int i;
......@@ -713,7 +737,7 @@ int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, u
#ifndef QUIET
/* print and skip */
int skip_type(unsigned int type, labcomm_sig_parser_t *d,
unsigned char *sig, unsigned int len, unsigned int *pos)
unsigned char *sig, unsigned int len, int *pos)
{
int skipped=0;
printf("skip_type %x:", type);
......@@ -748,8 +772,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
break;
case TYPE_STRING :
{
unsigned char nbytes;
unsigned int len = get_varint_size(d, &nbytes);
size_t nbytes;
int len = get_varint_size(d, &nbytes);
int i;
printf("string [");
for(i=0; i<len; i++)
......@@ -773,7 +797,7 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
}
#else
int skip_type(unsigned int type, labcomm_sig_parser_t *d,
unsigned char *sig, unsigned int len, unsigned int *pos)
unsigned char *sig, unsigned int len, int *pos)
{
int skipped=0;
VERBOSE_PRINTF("skip_type %x\n", type);
......@@ -799,8 +823,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
break;
case TYPE_STRING :
{
unsigned char nbytes;
unsigned int len = get_varint_size(d, &nbytes);
size_t nbytes;
int len = get_varint_size(d, &nbytes);
advancen(d,len);
skipped+=len+nbytes;
break;}
......@@ -821,10 +845,10 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
/* parse signature and skip the corresponding bytes in the labcomm_sig_parser_t
*/
int skip_packed_sample_data(labcomm_sig_parser_t *d, struct labcomm_signature *sig) {
unsigned int pos = 0; //current position in signature
int pos = 0; //current position in signature
unsigned int skipped = 0; //skipped byte counter
while(pos < sig->size) {
unsigned char nbytes;
size_t nbytes;
unsigned int type = unpack_varint(sig->signature,pos, &nbytes);
pos+=nbytes;
skipped += skip_type(type, d, sig->signature, sig->size, &pos);
......
......@@ -15,7 +15,7 @@
#undef DEBUG_STACK_VERBOSE // dump stack, otherwise just print value of top
#undef QUIET //just print type and size when skipping data
#undef VERBOSE // print in great detail
// #undef VERBOSE // print in great detail
#undef STATIC_ALLOCATION //dynamic allocation not completely implemented
......@@ -29,10 +29,10 @@
/* internal type: stack &c. for the parser */
typedef struct {
char* c;
unsigned char* c;
size_t size;
size_t capacity;
unsigned int idx;
int idx;
int val_top;
int * val_stack;
int ptr_top;
......@@ -57,7 +57,7 @@ typedef struct {
unsigned char **signatures; // [MAX_SIGNATURES][MAX_SIG_LEN];
unsigned int *signatures_name_length; // [MAX_SIGNATURES]
unsigned char **signatures_name; // [MAX_SIGNATURES][MAX_NAME_LEN];
char **signatures_name; // [MAX_SIGNATURES][MAX_NAME_LEN];
#endif
} labcomm_sig_parser_t;
......@@ -73,7 +73,7 @@ int accept_packet(labcomm_sig_parser_t *p);
struct labcomm_signature *get_sig_t(labcomm_sig_parser_t *p,unsigned int uid);
unsigned int get_signature_len(labcomm_sig_parser_t *p,unsigned int uid);
unsigned char* get_signature_name(labcomm_sig_parser_t *p,unsigned int uid);
char* get_signature_name(labcomm_sig_parser_t *p,unsigned int uid);
unsigned char* get_signature(labcomm_sig_parser_t *p,unsigned int uid);
void dump_signature(labcomm_sig_parser_t *p,unsigned int uid);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment