Skip to content
Snippets Groups Projects
Commit 75a92c86 authored by Sven Robertz's avatar Sven Robertz
Browse files

some cleanup and renaming for clarity

parent 0283a101
Branches
Tags
No related merge requests found
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
* *
* - The RETURN_STRINGS and where/if to allocate strings is to be decided, it * - The RETURN_STRINGS and where/if to allocate strings is to be decided, it
* is currently no longer used * is currently no longer used
*
* - TYPE_DECL is not tested
*/ */
...@@ -225,7 +227,7 @@ static inline void buffer_set_varsize(buffer *b) ...@@ -225,7 +227,7 @@ static inline void buffer_set_varsize(buffer *b)
{ {
b->current_decl_is_varsize = TRUE; b->current_decl_is_varsize = TRUE;
} }
size_t labcomm_sizeof_primitive(unsigned int type) static size_t labcomm_sizeof_primitive(unsigned int type)
{ {
switch(type) { switch(type) {
case TYPE_BOOLEAN : case TYPE_BOOLEAN :
...@@ -245,8 +247,7 @@ size_t labcomm_sizeof_primitive(unsigned int type) ...@@ -245,8 +247,7 @@ size_t labcomm_sizeof_primitive(unsigned int type)
} }
} }
//these are inlined in do_parse //these are inlined in do_accept_packet
//static int accept_packet(buffer *d);
//static int accept_type_decl(buffer *d); //static int accept_type_decl(buffer *d);
//static int accept_sample_decl(buffer *d); //static int accept_sample_decl(buffer *d);
static int accept_user_id(buffer *d); static int accept_user_id(buffer *d);
...@@ -254,14 +255,6 @@ static int accept_string(buffer *d); ...@@ -254,14 +255,6 @@ static int accept_string(buffer *d);
static int accept_string_length(buffer *d); static int accept_string_length(buffer *d);
static int accept_char(buffer *d); static int accept_char(buffer *d);
static int accept_type(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_array_decl(buffer *d);
static int accept_number_of_indices(buffer *d); static int accept_number_of_indices(buffer *d);
static int accept_indices(buffer *d); static int accept_indices(buffer *d);
...@@ -271,7 +264,6 @@ static int accept_struct_decl(buffer *d); ...@@ -271,7 +264,6 @@ static int accept_struct_decl(buffer *d);
static int accept_number_of_fields(buffer *d); static int accept_number_of_fields(buffer *d);
static int accept_field(buffer *d); static int accept_field(buffer *d);
static int accept_sample_data(buffer *d); static int accept_sample_data(buffer *d);
static int accept_packed_sample_data(buffer *d);
static unsigned char labcomm_varint_sizeof(unsigned int i) static unsigned char labcomm_varint_sizeof(unsigned int i)
{ {
...@@ -307,12 +299,12 @@ int encoded_size_parse_sig(labcomm_signature_t *sig, void *sample) ...@@ -307,12 +299,12 @@ int encoded_size_parse_sig(labcomm_signature_t *sig, void *sample)
} }
// HERE BE DRAGONS! what does the return value mean? // HERE BE DRAGONS! what does the return value mean?
int do_parse(buffer *d) { int accept_packet(buffer *d) {
unsigned char nbytes; unsigned char nbytes;
unsigned int type = peek_varint(d, &nbytes) ; unsigned int type = peek_varint(d, &nbytes) ;
if(type == TYPE_DECL ) { if(type == TYPE_DECL ) {
advancen(d, nbytes); advancen(d, nbytes);
accept_user_id(d); if(accept_user_id(d)) {
unsigned int uid = pop_val(d); unsigned int uid = pop_val(d);
VERBOSE_PRINTF(", name = "); VERBOSE_PRINTF(", name = ");
accept_string(d); accept_string(d);
...@@ -324,17 +316,20 @@ int do_parse(buffer *d) { ...@@ -324,17 +316,20 @@ int do_parse(buffer *d) {
VERBOSE_PRINTF(" : "); VERBOSE_PRINTF(" : ");
accept_type(d); accept_type(d);
unsigned int type = pop_val(d); unsigned int type = pop_val(d);
pop_val(d); // ignore size
//push(d, type); //push(d, type);
VERBOSE_PRINTF("\n"); VERBOSE_PRINTF("\n");
return TRUE; return TRUE;
} else {
error("type_decl with uid < LABCOMM_USER\n");
}
} else if (type == SAMPLE_DECL) { } else if (type == SAMPLE_DECL) {
d->current_decl_is_varsize = FALSE; // <-- a conveniance flag in buffer d->current_decl_is_varsize = FALSE; // <-- a conveniance flag in buffer
advancen(d, nbytes); advancen(d, nbytes);
VERBOSE_PRINTF("sample_decl "); VERBOSE_PRINTF("sample_decl ");
accept_user_id(d); if(accept_user_id(d)) {
unsigned int nstart = d->idx;
unsigned int uid = pop_val(d); unsigned int uid = pop_val(d);
unsigned int nstart = d->idx;
labcomm_signature_t *newsig = get_sig_t(uid); labcomm_signature_t *newsig = get_sig_t(uid);
newsig->type = type; newsig->type = type;
VERBOSE_PRINTF(", name = "); VERBOSE_PRINTF(", name = ");
...@@ -382,6 +377,9 @@ int do_parse(buffer *d) { ...@@ -382,6 +377,9 @@ int do_parse(buffer *d) {
newsig->encoded_size = encoded_size_parse_sig; newsig->encoded_size = encoded_size_parse_sig;
INFO_PRINTF(".... is variable size\n"); INFO_PRINTF(".... is variable size\n");
} }
} else {
error("sample_decl with uid < LABCOMM_USER\n");
}
} else if(type >= LABCOMM_USER) { } else if(type >= LABCOMM_USER) {
#ifdef EXIT_WHEN_RECEIVING_DATA #ifdef EXIT_WHEN_RECEIVING_DATA
printf("*** got sample data, exiting\n"); printf("*** got sample data, exiting\n");
...@@ -506,8 +504,8 @@ static int accept_array_decl(buffer *d){ ...@@ -506,8 +504,8 @@ static int accept_array_decl(buffer *d){
} }
VERBOSE_PRINTF(" of "); VERBOSE_PRINTF(" of ");
accept_type(d); accept_type(d);
unsigned int et= pop_val(d); unsigned int et= pop_val(d); // type
unsigned int es= pop_val(d); unsigned int es= pop_val(d); // (encoded) size
#ifdef DEBUG #ifdef DEBUG
printf("accept_array_decl: et = %x\n", et); printf("accept_array_decl: et = %x\n", et);
#endif #endif
...@@ -516,6 +514,8 @@ static int accept_array_decl(buffer *d){ ...@@ -516,6 +514,8 @@ static int accept_array_decl(buffer *d){
printf("size=%d, es=%d\n", size, es); printf("size=%d, es=%d\n", size, es);
#endif #endif
push_val(d, (size*es)); push_val(d, (size*es));
} else {
//HERE BE DRAGONS! shouldn't we push some (non-) size?
} }
//pop(d); //pop(d);
push_val(d, tid); push_val(d, tid);
...@@ -527,6 +527,8 @@ static int accept_array_decl(buffer *d){ ...@@ -527,6 +527,8 @@ static int accept_array_decl(buffer *d){
return FALSE; return FALSE;
} }
} }
/* pushes size */
static int accept_struct_decl(buffer *d){ static int accept_struct_decl(buffer *d){
unsigned char nbytes; unsigned char nbytes;
unsigned int tid = peek_varint(d, &nbytes) ; unsigned int tid = peek_varint(d, &nbytes) ;
...@@ -544,15 +546,15 @@ static int accept_struct_decl(buffer *d){ ...@@ -544,15 +546,15 @@ static int accept_struct_decl(buffer *d){
fieldsizes += pop_val(d); fieldsizes += pop_val(d);
} }
push_val(d, fieldsizes); push_val(d, fieldsizes);
// push_val(d, tid);
return TRUE; return TRUE;
} else { } else {
printf("accept_struct_decl: type=%x, should not happen\n",tid); printf("accept_struct_decl: type=%x, should not happen\n",tid);
push_val(d, 0); push_val(d, 0);
push_val(d, tid);
return FALSE; return FALSE;
} }
} }
/* pushes name (if enabled) and field size */
static int accept_field(buffer *d){ static int accept_field(buffer *d){
VERBOSE_PRINTF("field "); VERBOSE_PRINTF("field ");
accept_string(d); accept_string(d);
...@@ -564,25 +566,23 @@ static int accept_field(buffer *d){ ...@@ -564,25 +566,23 @@ static int accept_field(buffer *d){
VERBOSE_PRINTF(" : "); VERBOSE_PRINTF(" : ");
accept_type(d); accept_type(d);
pop_val(d); // ignore type, for now pop_val(d); // ignore type, for now
//push(d, pop(d) == NOP //push(d, pop(d) == NOP , leave size on the stack
VERBOSE_PRINTF("\n"); VERBOSE_PRINTF("\n");
} }
static int accept_sample_data(buffer *d){ static int accept_sample_data(buffer *d){
accept_user_id(d); accept_user_id(d);
unsigned int uid = pop_val(d); unsigned int uid = pop_val(d);
printf("sample data... %x\n", uid); printf("sample data... uid=0x%x\n", uid);
#ifdef DEBUG #ifdef DEBUG
dump_signature(uid); dump_signature(uid);
#endif #endif
labcomm_signature_t *sigt = get_sig_t(uid); labcomm_signature_t *sigt = get_sig_t(uid);
int encoded_size = sigt->encoded_size(sigt, NULL); int encoded_size = sigt->encoded_size(sigt, NULL);
INFO_PRINTF("encoded_size from sig: %d\n", encoded_size); INFO_PRINTF("encoded_size from sig: %d\n", encoded_size);
unsigned int siglen = get_signature_len(uid); labcomm_signature_t *sig = get_sig_t(uid);
unsigned char *sig = get_signature(uid); skip_packed_sample_data(d, sig);
skip_packed_sample_data(d, sig, siglen);
return TRUE; return TRUE;
} }
//int accept_packed_sample_data(buffer *d){
static int skip_type(unsigned int,buffer*,unsigned char*,unsigned int,unsigned int*) ; static int skip_type(unsigned int,buffer*,unsigned char*,unsigned int,unsigned int*) ;
...@@ -771,14 +771,14 @@ int skip_type(unsigned int type, buffer *d, ...@@ -771,14 +771,14 @@ int skip_type(unsigned int type, buffer *d,
/* parse signature and skip the corresponding bytes in the buffer /* parse signature and skip the corresponding bytes in the buffer
*/ */
int skip_packed_sample_data(buffer *d, unsigned char *sig, unsigned int siglen) { int skip_packed_sample_data(buffer *d, labcomm_signature_t *sig) {
unsigned int pos = 0; //current position in signature unsigned int pos = 0; //current position in signature
unsigned int skipped = 0; //skipped byte counter unsigned int skipped = 0; //skipped byte counter
while(pos < siglen) { while(pos < sig->size) {
unsigned char nbytes; unsigned char nbytes;
unsigned int type = unpack_varint(sig,pos, &nbytes); unsigned int type = unpack_varint(sig->signature,pos, &nbytes);
pos+=nbytes; pos+=nbytes;
skipped += skip_type(type, d, sig, siglen, &pos); skipped += skip_type(type, d, sig->signature, sig->size, &pos);
} }
printf("skipped %d bytes\n", skipped); printf("skipped %d bytes\n", skipped);
return TRUE; return TRUE;
... ...
......
...@@ -76,6 +76,8 @@ typedef struct { ...@@ -76,6 +76,8 @@ typedef struct {
int init_buffer(buffer *b, size_t size, size_t stacksize) ; int init_buffer(buffer *b, size_t size, size_t stacksize) ;
int read_file(FILE *f, buffer *b); int read_file(FILE *f, buffer *b);
int accept_packet(buffer *d);
//XXX experimental //XXX experimental
#define MAX_SIGNATURES 10 #define MAX_SIGNATURES 10
#define MAX_NAME_LEN 32 #define MAX_NAME_LEN 32
...@@ -83,15 +85,16 @@ int read_file(FILE *f, buffer *b); ...@@ -83,15 +85,16 @@ int read_file(FILE *f, buffer *b);
#define STACK_SIZE 16 #define STACK_SIZE 16
labcomm_signature_t *get_sig_t(unsigned int uid);
unsigned int get_signature_len(unsigned int uid); unsigned int get_signature_len(unsigned int uid);
unsigned char* get_signature_name(unsigned int uid); unsigned char* get_signature_name(unsigned int uid);
unsigned char* get_signature(unsigned int uid); unsigned char* get_signature(unsigned int uid);
void dump_signature(unsigned int uid); void dump_signature(unsigned int uid);
int do_parse(buffer *d);
/* parse signature and skip the corresponding bytes in the buffer /* parse signature and skip the corresponding bytes in the buffer
*/ */
int skip_packed_sample_data(buffer *d, unsigned char *sig, unsigned int siglen); int skip_packed_sample_data(buffer *d, labcomm_signature_t *sig);
#endif #endif
...@@ -33,7 +33,7 @@ int main() { ...@@ -33,7 +33,7 @@ int main() {
test_read(&buf); test_read(&buf);
do{ do{
printf("--------------------------------------------- new packet: \n"); printf("--------------------------------------------- new packet: \n");
} while(more(&buf) && do_parse(&buf)); } while(more(&buf) && accept_packet(&buf));
printf("EOF\n"); printf("EOF\n");
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment