Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • labcomm2014
  • master
  • pragma
  • python_sig_hash
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
  • v2014.5
  • v2014.6
  • v2015.0
20 results

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • master
  • pragma
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
15 results
Show changes
Showing
with 1367 additions and 29 deletions
all: SUBDIRS=c csharp java python
cd c ; make
cd csharp ; make .PHONY: all
cd java ; make all: $(SUBDIRS:%=make-%)
test: .PHONY: make-%
$(MAKE) -C c -e run-test make-%:
$(MAKE) -C $*
clean:
cd c ; make clean .PHONY: test
cd csharp ; make clean test: $(SUBDIRS:%=test-%)
cd java ; make clean
.PHONY: test-%
distclean: test-%:
cd c ; make distclean $(MAKE) -C $* test
cd csharp ; make clean
cd java ; make clean .PHONY: clean
clean: $(SUBDIRS:%=clean-%)
.PHONY: clean-%
clean-%:
$(MAKE) -C $* clean
.PHONY: distclean
distclean: clean $(SUBDIRS:%=distclean-%)
.PHONY: distclean-%
distclean-%:
$(MAKE) -C $* distclean
...@@ -34,7 +34,7 @@ or ...@@ -34,7 +34,7 @@ or
$ VAR Encoder e; $ VAR Encoder e;
$ Init_Encoder e, st; $ Init_Encoder e, st;
This will read/write the version of LabComm to ensure compatibility, current This will read/write the version of LabComm to ensure compatibility, current
version is "LabComm2013". version is "LabComm2014".
# Initiate the labcomm samples: # Initiate the labcomm samples:
LabComm trusts the application to manage each sample. It requests the samples, LabComm trusts the application to manage each sample. It requests the samples,
......
liblabcomm.a
liblabcomm.so
liblabcomm.so.1
liblabcomm2006.so.1
liblabcomm2006.so
liblabcomm2006.a
liblabcomm2014.so.1
liblabcomm2014.so
liblabcomm2014.a
## Macros
VERSION=2014
LIBVERSION=2014
include ../os_compat.mk
ALL_DEPS=../liblabcomm$(LIBVERSION).a ../liblabcomm$(LIBVERSION).so
# TODO: Support for Codesourcery ARM toolchain.
OBJS=labcomm$(VERSION).o \
labcomm$(VERSION)_memory.o \
labcomm$(VERSION)_error.o \
labcomm$(VERSION)_default_error_handler.o \
labcomm$(VERSION)_default_memory.o \
labcomm$(VERSION)_default_scheduler.o \
labcomm$(VERSION)_time.o \
labcomm$(VERSION)_scheduler.o \
labcomm$(VERSION)_encoder.o \
labcomm$(VERSION)_decoder.o \
labcomm$(VERSION)_dynamic_buffer_writer.o \
labcomm$(VERSION)_fd_reader.o \
labcomm$(VERSION)_type_signature.o \
labcomm$(VERSION)_fd_writer.o \
labcomm$(VERSION)_pthread_scheduler.o \
labcomm$(VERSION)_renaming.o \
labcomm$(VERSION)_renaming_registry.o \
labcomm$(VERSION)_renaming_encoder.o \
labcomm$(VERSION)_renaming_decoder.o
# Enable experimental objects by `make LABCOMM_EXPERIMENTAL=true`
ifeq ($(LABCOMM_EXPERIMENTAL),true)
OBJS += experimental/udp_hack.o experimental/ethaddr.o \
experimental/labcomm_thr_reader_writer.o \
experimental/ThrottleDrv/ethernet_drv.o \
experimental/ThrottleDrv/throttle_drv.o \
experimental/labcomm_udp_reader_writer.o
endif
# Enable experimental objects by `make LABCOMM_SIG_PARSER=true`
ifeq ($(LABCOMM_SIG_PARSER),true)
OBJS += experimental/labcomm2014_sig_parser.o
endif
LABCOMM_JAR=../../../compiler/labcomm$(LIBVERSION)_compiler.jar
LABCOMM=java -jar $(LABCOMM_JAR)
TESTS=test_labcomm_basic_type_encoding \
test_labcomm_generated_encoding \
test_signature_numbers \
test_labcomm \
test_labcomm_pthread_scheduler \
test_labcomm_copy \
test_labcomm_renaming_registry \
test_labcomm_renaming_encoder \
test_labcomm_renaming_decoder
#FIXME: test_labcomm_errors
TEST_DIR=test
## Targets
.PHONY: all
all: $(ALL_DEPS)
.PHONY: test
test: all $(TESTS:%=run-test-%)
.PHONY: clean
clean:
$(RM) *.o
$(RM) experimental/*.o
$(RM) experimental/ThrottleDrv/*.o
$(RM) test/*.o
$(RM) test/*.gch
$(RM) test/test_labcomm_errors
$(RM) test/testdata/gen/*.[cho]
$(RM) test/gen/*.[cho]
$(RM) -rf test/gen
.PHONY: distclean
distclean: clean
$(RM) ../liblabcomm$(LIBVERSION).so.1
$(RM) ../liblabcomm$(LIBVERSION).a
# rules invoked by 'all'
../liblabcomm$(LIBVERSION).so: ../liblabcomm$(LIBVERSION).so.1
if [ -h $@ ] ; then rm $@ ; fi
ln -s $(<:../%=%) $@
../liblabcomm$(LIBVERSION).so.1: $(OBJS:%.o=%.pic.o)
$(call MAKESHARED,$@,$(@:../%=%),$^)
../liblabcomm$(LIBVERSION).a: $(OBJS)
ar -r $@ $^
# Enable sig parser objects by `make labcomm2014_sig_PARSER=true`
ifeq ($(LABCOMM_SIG_PARSER),true)
experimental/test_sig_parser : experimental/labcomm2014_sig_parser.o experimental/test_sig_parser.c
endif
# compilation rules
%.pic.o: %.c
$(CC) -fPIC $(CFLAGS) -c -o $@ $<
%.o: %.c %.h
$(CC) $(CFLAGS) -c -o $@ $<
# rules invoked by 'test'
.PHONY: run-test-%
run-test-%: $(TEST_DIR)/gen/% | $(TEST_DIR)/gen
$(VALGRIND) $<
.PRECIOUS: $(TEST_DIR)/gen/%
$(TEST_DIR)/gen/%: $(TEST_DIR)/gen/%.o | $(TEST_DIR)/gen
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(TEST_DIR)/gen/%.o: $(TEST_DIR)/%.c | $(TEST_DIR)/gen
$(CC) $(CFLAGS_TEST) -o $@ -c $<
.PRECIOUS: $(TEST_DIR)/gen/%.c
.PRECIOUS: $(TEST_DIR)/gen/%.h
$(TEST_DIR)/gen/%.c $(TEST_DIR)/gen/%.h: $(TEST_DIR)/%.lc | $(TEST_DIR)/gen
$(LABCOMM) \
--c=$(TEST_DIR)/gen/$*.c \
--h=$(TEST_DIR)/gen/$*.h \
$<
$(LABCOMM_JAR):
@echo "======Building LabComm compiler======"
cd $(shell dirname $(LABCOMM_JAR)); ant jar
@echo "======End building LabComm compiler======"
$(TEST_DIR)/gen:
mkdir -p $@
# Extra compilation dependencies
labcomm$(VERSION).o: \
labcomm$(VERSION).c \
labcomm$(VERSION).h \
labcomm$(VERSION)_private.h
labcomm$(VERSION)_fd_reader.o: \
labcomm$(VERSION)_private.h
labcomm$(VERSION)_fd_writer.o: \
labcomm$(VERSION)_private.h
labcomm$(VERSION)_dynamic_buffer_writer.o: \
labcomm$(VERSION)_private.h
$(TEST_DIR)/gen/test_labcomm_basic_type_encoding.o: \
labcomm$(VERSION)_private.h
$(TEST_DIR)/gen/test_labcomm_generated_encoding.o: \
labcomm$(VERSION)_private.h \
$(TEST_DIR)/gen/generated_encoding.h
$(TEST_DIR)/gen/test_labcomm_generated_encoding: \
$(TEST_DIR)/gen/generated_encoding.o
$(TEST_DIR)/gen/test_signature_numbers.c: \
$(TEST_DIR)/gen/another_encoding.h \
$(TEST_DIR)/gen/generated_encoding.h
$(TEST_DIR)/gen/test_signature_numbers: \
$(TEST_DIR)/gen/another_encoding.o \
$(TEST_DIR)/gen/generated_encoding.o
$(TEST_DIR)/gen/test_labcomm: \
$(TEST_DIR)/gen/test_sample.o
$(TEST_DIR)/gen/test_labcomm_copy: \
$(TEST_DIR)/gen/generated_encoding.o \
$(TEST_DIR)/gen/test_sample.o \
$(TEST_DIR)/gen/more_types.o
$(TEST_DIR)/gen/test_labcomm_renaming_registry: \
$(TEST_DIR)/gen/generated_encoding.o
$(TEST_DIR)/gen/test_labcomm_renaming_encoder: \
$(TEST_DIR)/gen/generated_encoding.o
$(TEST_DIR)/gen/test_labcomm_renaming_decoder: \
$(TEST_DIR)/gen/generated_encoding.o
/* labcomm_sig_parser.c: /* labcomm2014_sig_parser.c:
* an example parser for labcomm signatures, illustrating how to skip samples * an example parser for labcomm signatures, illustrating how to skip samples
* based on their signature. Intended as an embryo for introducing this * based on their signature. Intended as an embryo for introducing this
* functionality into the lib to allow a channel to survive types with no * functionality into the lib to allow a channel to survive types with no
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
* - 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 not used * is currently not used
* *
* - TYPE_DECL is not tested (is it ever sent?)
*
* - The dynamic allocation of the parser is not quite dynamic, the sizes are * - The dynamic allocation of the parser is not quite dynamic, the sizes are
* set through the init function, and are then static. * set through the init function, and are then static.
* This should be adapted when allocation is parameterized/user-definable * This should be adapted when allocation is parameterized/user-definable
...@@ -29,7 +27,7 @@ ...@@ -29,7 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "labcomm_sig_parser.h" #include "labcomm2014_sig_parser.h"
static void error(char *s) { static void error(char *s) {
fprintf(stderr, "ERROR: %s", s); fprintf(stderr, "ERROR: %s", s);
...@@ -38,17 +36,17 @@ static void error(char *s) { ...@@ -38,17 +36,17 @@ static void error(char *s) {
} }
/* aux method for reading a big endian uint32 from a char* (i.e. ntohl but for explicit char*) */ /* 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) { static int unpack32(unsigned char *c, int idx) {
unsigned int b0=(c[idx]) << 3 ; int b0=(c[idx]) << 3 ;
unsigned int b1=(c[idx+1]) << 2 ; int b1=(c[idx+1]) << 2 ;
unsigned int b2=(c[idx+2]) << 1 ; int b2=(c[idx+2]) << 1 ;
unsigned int b3=c[idx+3]; int b3=c[idx+3];
return b0 | b1 | b2 | b3; return b0 | b1 | b2 | b3;
} }
static inline unsigned int get32(labcomm_sig_parser_t *b) { static inline int get32(labcomm2014_sig_parser_t *b) {
unsigned int res = unpack32(b->c, b->idx); int res = unpack32(b->c, b->idx);
b->idx+=4; b->idx+=4;
return res; return res;
} }
...@@ -57,13 +55,13 @@ static inline unsigned int get32(labcomm_sig_parser_t *b) { ...@@ -57,13 +55,13 @@ static inline unsigned int get32(labcomm_sig_parser_t *b) {
/* aux method for reading labcomm varint from a char* /* 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 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, static int unpack_varint(unsigned char *buf,
unsigned int idx, int idx,
unsigned char *size) size_t *size)
{ {
unsigned int res = 0; int res = 0;
unsigned int i=0; unsigned int i=0;
unsigned char cont = TRUE; unsigned char cont = LABCOMM2014_TRUE;
do { do {
unsigned char c = buf[idx+i]; unsigned char c = buf[idx+i];
...@@ -77,20 +75,20 @@ static unsigned int unpack_varint(unsigned char *buf, ...@@ -77,20 +75,20 @@ static unsigned int unpack_varint(unsigned char *buf,
return res; return res;
} }
void dumpValStack(labcomm_sig_parser_t *b) { void dumpValStack(labcomm2014_sig_parser_t *b) {
int i;
printf("=== value stack: "); printf("=== value stack: ");
#ifdef DEBUG_STACK_VERBOSE #ifdef DEBUG_STACK_VERBOSE
int i;
for(i=0; i<STACK_SIZE; i++) { //HERE BE DRAGONS for(i=0; i<STACK_SIZE; i++) { //HERE BE DRAGONS
printf("%2.2x ", b->val_stack[i]); printf("%2.2x ", b->val_stack[i]);
} }
#endif #endif
printf(" top==%d\n", b->val_top); printf(" top==%d\n", b->val_top);
} }
void dumpPtrStack(labcomm_sig_parser_t *b) { void dumpPtrStack(labcomm2014_sig_parser_t *b) {
int i;
printf("=== pointer stack: "); printf("=== pointer stack: ");
#ifdef DEBUG_STACK_VERBOSE #ifdef DEBUG_STACK_VERBOSE
int i;
for(i=0; i<STACK_SIZE; i++) { //HERE BE DRAGONS for(i=0; i<STACK_SIZE; i++) { //HERE BE DRAGONS
printf("%2.2x ", b->ptr_stack[i]); printf("%2.2x ", b->ptr_stack[i]);
} }
...@@ -98,7 +96,7 @@ void dumpPtrStack(labcomm_sig_parser_t *b) { ...@@ -98,7 +96,7 @@ void dumpPtrStack(labcomm_sig_parser_t *b) {
printf(" top==%d\n", b->ptr_top); printf(" top==%d\n", b->ptr_top);
} }
void push_val(labcomm_sig_parser_t *b, unsigned int e) { void push_val(labcomm2014_sig_parser_t *b, unsigned int e) {
b->val_stack[b->val_top]=e; b->val_stack[b->val_top]=e;
b->val_top=b->val_top-1; b->val_top=b->val_top-1;
if(b->val_top<0) error("value stack overrun"); if(b->val_top<0) error("value stack overrun");
...@@ -106,7 +104,7 @@ void push_val(labcomm_sig_parser_t *b, unsigned int e) { ...@@ -106,7 +104,7 @@ void push_val(labcomm_sig_parser_t *b, unsigned int e) {
dumpValStack(b); dumpValStack(b);
#endif #endif
} }
unsigned int pop_val(labcomm_sig_parser_t *b) { unsigned int pop_val(labcomm2014_sig_parser_t *b) {
b->val_top=b->val_top+1; b->val_top=b->val_top+1;
if(b->val_top>b->stacksize) error("value stack underrun"); if(b->val_top>b->stacksize) error("value stack underrun");
#if defined DEBUG && !defined QUIET_STACK #if defined DEBUG && !defined QUIET_STACK
...@@ -114,7 +112,7 @@ unsigned int pop_val(labcomm_sig_parser_t *b) { ...@@ -114,7 +112,7 @@ unsigned int pop_val(labcomm_sig_parser_t *b) {
#endif #endif
return b->val_stack[b->val_top]; return b->val_stack[b->val_top];
} }
void push_ptr(labcomm_sig_parser_t *b, void* e) { void push_ptr(labcomm2014_sig_parser_t *b, void* e) {
b->ptr_stack[b->ptr_top]=e; b->ptr_stack[b->ptr_top]=e;
b->ptr_top=b->ptr_top-1; b->ptr_top=b->ptr_top-1;
if(b->ptr_top<0) error("pointer stack overrun"); if(b->ptr_top<0) error("pointer stack overrun");
...@@ -122,7 +120,7 @@ void push_ptr(labcomm_sig_parser_t *b, void* e) { ...@@ -122,7 +120,7 @@ void push_ptr(labcomm_sig_parser_t *b, void* e) {
dumpPtrStack(b); dumpPtrStack(b);
#endif #endif
} }
void* pop_ptr(labcomm_sig_parser_t *b) { void* pop_ptr(labcomm2014_sig_parser_t *b) {
b->ptr_top=b->ptr_top+1; b->ptr_top=b->ptr_top+1;
if(b->ptr_top>b->stacksize) error("pointer stack underrun"); if(b->ptr_top>b->stacksize) error("pointer stack underrun");
#ifdef DEBUG #ifdef DEBUG
...@@ -131,7 +129,7 @@ void* pop_ptr(labcomm_sig_parser_t *b) { ...@@ -131,7 +129,7 @@ void* pop_ptr(labcomm_sig_parser_t *b) {
return b->ptr_stack[b->ptr_top]; return b->ptr_stack[b->ptr_top];
} }
int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size, int labcomm2014_sig_parser_init(labcomm2014_sig_parser_t *b, size_t buffer_size,
size_t stacksize, size_t num_signatures, size_t stacksize, size_t num_signatures,
size_t max_name_len, size_t max_sig_len) size_t max_name_len, size_t max_sig_len)
{ {
...@@ -153,9 +151,9 @@ int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size, ...@@ -153,9 +151,9 @@ int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size,
b->max_sig_len = max_sig_len; b->max_sig_len = max_sig_len;
#ifdef STATIC_ALLOCATION #ifdef STATIC_ALLOCATION
printf("warning: labcomm_sig_parser_t_init: size params ignored, using defaults from .h file \n"); printf("warning: labcomm2014_sig_parser_t_init: size params ignored, using defaults from .h file \n");
#else #else
b->sig_ts=calloc(num_signatures, sizeof(labcomm_signature_t)); b->sig_ts=calloc(num_signatures, sizeof(struct labcomm2014_signature));
b->signatures_length=calloc(num_signatures, sizeof(int)); b->signatures_length=calloc(num_signatures, sizeof(int));
b->signatures_name_length=calloc(num_signatures, sizeof(int)); b->signatures_name_length=calloc(num_signatures, sizeof(int));
b->signatures_name=calloc(num_signatures, sizeof(void *)); //HERE BE DRAGONS: add range checks b->signatures_name=calloc(num_signatures, sizeof(void *)); //HERE BE DRAGONS: add range checks
...@@ -169,83 +167,105 @@ int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size, ...@@ -169,83 +167,105 @@ int labcomm_sig_parser_init(labcomm_sig_parser_t *b, size_t buffer_size,
return b->c == NULL || b->val_stack == NULL || b->ptr_stack == NULL; return b->c == NULL || b->val_stack == NULL || b->ptr_stack == NULL;
} }
int labcomm_sig_parser_read_file(labcomm_sig_parser_t *b, FILE *f) { /* free the objects allocated by labcomm_sig_parser_init(b)
* NB! does not free b itself */
void labcomm2014_sig_parser_free(labcomm2014_sig_parser_t *b)
{
int i;
free(b->c);
free(b->val_stack);
free(b->ptr_stack);
#ifndef STATIC_ALLOCATION
for(i = 0; i<b->max_signatures; i++) {
free(b->signatures[i]);
free(b->signatures_name[i]);
}
free(b->signatures);
free(b->signatures_name);
free(b->signatures_name_length);
free(b->signatures_length);
free(b->sig_ts);
#endif
}
int labcomm2014_sig_parser_read_file(labcomm2014_sig_parser_t *b, FILE *f) {
int s = fread(b->c, sizeof(char), b->capacity, f); int s = fread(b->c, sizeof(char), b->capacity, f);
b->size = s; b->size = s;
b->idx=0; b->idx=0;
return s; return s;
} }
int more(labcomm_sig_parser_t *b) int more(labcomm2014_sig_parser_t *b)
{ {
return b->idx < b->size; return b->idx < b->size;
} }
unsigned char get(labcomm_sig_parser_t *b) { unsigned char get(labcomm2014_sig_parser_t *b) {
return b->c[b->idx++]; return b->c[b->idx++];
} }
unsigned char peek(labcomm_sig_parser_t *b) { unsigned char peek(labcomm2014_sig_parser_t *b) {
return b->c[b->idx]; return b->c[b->idx];
} }
void advance(labcomm_sig_parser_t *b) { void advance(labcomm2014_sig_parser_t *b) {
b->idx++; b->idx++;
} }
void advancen(labcomm_sig_parser_t *b, size_t n) { void advancen(labcomm2014_sig_parser_t *b, size_t n) {
b->idx+=n; b->idx+=n;
} }
unsigned int peek_varint(labcomm_sig_parser_t *b, unsigned char *size) { int peek_varint(labcomm2014_sig_parser_t *b, size_t *size) {
return unpack_varint(b->c, b->idx, size); return unpack_varint(b->c, b->idx, size);
} }
unsigned int get_varint(labcomm_sig_parser_t *b) { int get_varint(labcomm2014_sig_parser_t *b) {
unsigned char size; size_t size;
unsigned int res = peek_varint(b, &size); int res = peek_varint(b, &size);
advancen(b, size); advancen(b, size);
return res; return res;
} }
unsigned int get_varint_size(labcomm_sig_parser_t *b, unsigned char *size) { int get_varint_size(labcomm2014_sig_parser_t *b, size_t *size) {
unsigned int res = peek_varint(b, size); unsigned int res = peek_varint(b, size);
advancen(b, *size); advancen(b, *size);
return res; return res;
} }
void getStr(labcomm_sig_parser_t *b, char *dest, size_t size) { void getStr(labcomm2014_sig_parser_t *b, unsigned char *dest, size_t size) {
int rem = b->size - b->idx; int rem = b->size - b->idx;
if( size > rem ) if( size > rem )
size = rem; size = rem;
strncpy(dest, &b->c[b->idx], size); strncpy((char *)dest, (char *)&b->c[b->idx], size);
dest[size] = 0;
b->idx += size; b->idx += size;
} }
labcomm_signature_t *get_sig_t(labcomm_sig_parser_t *p, unsigned int uid) struct labcomm2014_signature *get_sig_t(labcomm2014_sig_parser_t *p, unsigned int uid)
{ {
return &(p->sig_ts[uid-LABCOMM_USER]); return &(p->sig_ts[uid-LABCOMM_USER]);
} }
unsigned int get_signature_len(labcomm_sig_parser_t *p, unsigned int uid){ unsigned int get_signature_len(labcomm2014_sig_parser_t *p, unsigned int uid){
//return signatures_length[uid-LABCOMM_USER]; //return signatures_length[uid-LABCOMM_USER];
return p->sig_ts[uid-LABCOMM_USER].size; return p->sig_ts[uid-LABCOMM_USER].size;
} }
unsigned char* get_signature(labcomm_sig_parser_t *p, unsigned int uid){ unsigned char* get_signature(labcomm2014_sig_parser_t *p, unsigned int uid){
//return signatures[uid-LABCOMM_USER]; //return signatures[uid-LABCOMM_USER];
return p->sig_ts[uid-LABCOMM_USER].signature; return p->sig_ts[uid-LABCOMM_USER].signature;
} }
//is this needed? //is this needed?
//unsigned int get_signature_name_len(labcomm_sig_parser_t *p, unsigned int uid){ //unsigned int get_signature_name_len(labcomm2014_sig_parser_t *p, unsigned int uid){
// return signatures_name_length[uid-LABCOMM_USER]; // return signatures_name_length[uid-LABCOMM_USER];
//} //}
unsigned char* get_signature_name(labcomm_sig_parser_t *p, unsigned int uid){ char* get_signature_name(labcomm2014_sig_parser_t *p, unsigned int uid){
//return signatures_name[uid-LABCOMM_USER]; //return signatures_name[uid-LABCOMM_USER];
return p->sig_ts[uid-LABCOMM_USER].name; return p->sig_ts[uid-LABCOMM_USER].name;
} }
void dump_signature(labcomm_sig_parser_t *p, unsigned int uid){ void dump_signature(labcomm2014_sig_parser_t *p, unsigned int uid){
int i; int i;
unsigned int len = get_signature_len(p, uid); unsigned int len = get_signature_len(p, uid);
printf("signature for uid %x : %s (len=%d):\n", uid, get_signature_name(p, uid), len); printf("signature for uid %x : %s (len=%d):\n", uid, get_signature_name(p, uid), len);
...@@ -257,11 +277,11 @@ void dump_signature(labcomm_sig_parser_t *p, unsigned int uid){ ...@@ -257,11 +277,11 @@ void dump_signature(labcomm_sig_parser_t *p, unsigned int uid){
printf("\n"); printf("\n");
} }
static inline void labcomm_sig_parser_t_set_varsize(labcomm_sig_parser_t *b) static inline void labcomm2014_sig_parser_t_set_varsize(labcomm2014_sig_parser_t *b)
{ {
b->current_decl_is_varsize = TRUE; b->current_decl_is_varsize = LABCOMM2014_TRUE;
} }
static size_t labcomm_sizeof_primitive(unsigned int type) static size_t labcomm2014_sizeof_primitive(unsigned int type)
{ {
switch(type) { switch(type) {
case TYPE_BOOLEAN : case TYPE_BOOLEAN :
...@@ -271,35 +291,41 @@ static size_t labcomm_sizeof_primitive(unsigned int type) ...@@ -271,35 +291,41 @@ static size_t labcomm_sizeof_primitive(unsigned int type)
return 2; return 2;
case TYPE_INTEGER : case TYPE_INTEGER :
case TYPE_FLOAT : case TYPE_FLOAT :
case TYPE_SAMPLE_REF :
return 4; return 4;
case TYPE_LONG : case TYPE_LONG :
case TYPE_DOUBLE : case TYPE_DOUBLE :
return 8; return 8;
default: default:
printf("labcomm_sizeof_primitive(%x)\n", type); printf("labcomm2014_sizeof_primitive(%x)\n", type);
error("labcomm_sizeof_primitive should only be called for primitive types"); error("labcomm2014_sizeof_primitive should only be called for primitive types");
return 0;
} }
} }
//these are inlined in do_accept_packet //these are inlined in do_accept_packet
//static int accept_type_decl(labcomm_sig_parser_t *d); //static int accept_type_decl(labcomm2014_sig_parser_t *d);
//static int accept_sample_decl(labcomm_sig_parser_t *d); //static int accept_sample_decl(labcomm2014_sig_parser_t *d);
static int accept_user_id(labcomm_sig_parser_t *d); static int accept_user_id(labcomm2014_sig_parser_t *d);
static int accept_string(labcomm_sig_parser_t *d); static int accept_string(labcomm2014_sig_parser_t *d);
static int accept_string_length(labcomm_sig_parser_t *d); static int accept_type(labcomm2014_sig_parser_t *d);
static int accept_char(labcomm_sig_parser_t *d); static int accept_array_decl(labcomm2014_sig_parser_t *d);
static int accept_type(labcomm_sig_parser_t *d); static int accept_intentions(labcomm2014_sig_parser_t *d);
static int accept_array_decl(labcomm_sig_parser_t *d); #if 0
static int accept_number_of_indices(labcomm_sig_parser_t *d); // UNUSED declarations
static int accept_indices(labcomm_sig_parser_t *d); static int accept_string_length(labcomm2014_sig_parser_t *d);
static int accept_variable_index(labcomm_sig_parser_t *d); static int accept_char(labcomm2014_sig_parser_t *d);
static int accept_fixed_index(labcomm_sig_parser_t *d); static int accept_number_of_indices(labcomm2014_sig_parser_t *d);
static int accept_struct_decl(labcomm_sig_parser_t *d); static int accept_indices(labcomm2014_sig_parser_t *d);
static int accept_number_of_fields(labcomm_sig_parser_t *d); static int accept_variable_index(labcomm2014_sig_parser_t *d);
static int accept_field(labcomm_sig_parser_t *d); static int accept_fixed_index(labcomm2014_sig_parser_t *d);
static int accept_sample_data(labcomm_sig_parser_t *d); static int accept_number_of_fields(labcomm2014_sig_parser_t *d);
#endif
static unsigned char labcomm_varint_sizeof(unsigned int i) static int accept_struct_decl(labcomm2014_sig_parser_t *d);
static int accept_field(labcomm2014_sig_parser_t *d);
static int accept_sample_data(labcomm2014_sig_parser_t *d);
static unsigned char labcomm2014_varint_sizeof(unsigned int i)
{ {
if(i < 128) { if(i < 128) {
return 1; return 1;
...@@ -310,13 +336,17 @@ static unsigned char labcomm_varint_sizeof(unsigned int i) ...@@ -310,13 +336,17 @@ static unsigned char labcomm_varint_sizeof(unsigned int i)
return res; return res;
} }
} }
int encoded_size_static(struct labcomm2014_signature *sig, void *unused)
int encoded_size_static(labcomm_signature_t *sig, void *unused)
{ {
#ifdef LABCOMM_EXPERIMENTAL_CACHED_ENCODED_SIZE
if(sig->cached_encoded_size == -1) { if(sig->cached_encoded_size == -1) {
error("encoded_size_static called for var_size sample or uninitialized signature"); error("encoded_size_static called for var_size sample or uninitialized signature");
} }
return sig->cached_encoded_size; return sig->cached_encoded_size;
#else
printf("Warning: encoded_size_static currently broken\n");
return -1;
#endif
} }
/* This function probably never will be implemented, as it would be /* This function probably never will be implemented, as it would be
...@@ -326,56 +356,58 @@ int encoded_size_static(labcomm_signature_t *sig, void *unused) ...@@ -326,56 +356,58 @@ int encoded_size_static(labcomm_signature_t *sig, void *unused)
on the receiver side, is to skip unhandled samples. on the receiver side, is to skip unhandled samples.
*/ */
int encoded_size_parse_sig(labcomm_signature_t *sig, void *sample) int encoded_size_parse_sig(struct labcomm2014_signature *sig, void *sample)
{ {
printf("Warning: encoded_size_parse_sig not implemented\n"); printf("Warning: encoded_size_parse_sig not implemented\n");
return -1; return -1;
} }
static int accept_signature(labcomm_sig_parser_t *d) static int accept_signature(labcomm2014_sig_parser_t *d,
labcomm2014_type type,
unsigned int start,
unsigned int uid, char *name)
{ {
if(accept_user_id(d)) { get_varint(d); // ignore sig len
unsigned int uid = pop_val(d);
unsigned int nstart = d->idx;
VERBOSE_PRINTF(", name = ");
accept_string(d);
unsigned int start = d->idx;
unsigned int nlen = pop_val(d);
#ifdef RETURN_STRINGS
char *str = (char *) pop_ptr(d);
free(str);
#endif
unsigned char lenlen = labcomm_varint_sizeof(nlen);
VERBOSE_PRINTF("\ntype = "); VERBOSE_PRINTF("\ntype = ");
accept_type(d); accept_type(d);
//printf(" : "); //printf(" : ");
//unsigned int dt = pop(d); //unsigned int dt = pop(d);
#ifdef USE_TYPE_AND_SIZE
unsigned int type = pop_val(d); unsigned int type = pop_val(d);
unsigned int enc_size = 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
if(type != PKG_SAMPLE_DECL) {
if(type == PKG_SAMPLE_REF) {
INFO_PRINTF("accept_signature: ignoring sample ref\n");
return LABCOMM2014_TRUE;
} else if (type == PKG_TYPE_DECL) {
INFO_PRINTF("accept_signature: ignoring typedef\n");
return LABCOMM2014_TRUE;
} else {
error("decl is neither sample, ref, or typedef???");
return LABCOMM2014_FALSE;
}
}
unsigned int end = d->idx; unsigned int end = d->idx;
unsigned int len = end-start; unsigned int len = end-start;
labcomm_signature_t *newsig = get_sig_t(d, uid); struct labcomm2014_signature *newsig = get_sig_t(d, uid);
newsig->type = type; // newsig->type = type;
if(len <= d->max_sig_len) { if(len <= d->max_sig_len) {
d->signatures_length[uid-LABCOMM_USER] = len; d->signatures_length[uid-LABCOMM_USER] = len;
memcpy(d->signatures[uid-LABCOMM_USER], &d->c[start], len); memcpy(d->signatures[uid-LABCOMM_USER], &d->c[start], len);
newsig->size = len; newsig->size = len;
newsig->signature = d->signatures[uid-LABCOMM_USER]; newsig->signature = d->signatures[uid-LABCOMM_USER];
newsig->name = name;
} else { } else {
error("sig longer than max length (this ought to be dynamic...)"); error("sig longer than max length (this ought to be dynamic...)");
} }
VERBOSE_PRINTF("signature for uid %x: %s (start=%x,end=%x,len=%d)\n", uid, get_signature_name(d, uid), start,end, len);
if(nlen < d->max_name_len) { // leave 1 byte for terminating NULL INFO_PRINTF("accept_signature: %s\n", newsig->name);
d->signatures_name_length[uid-LABCOMM_USER] = nlen; #ifdef LABCOMM_EXPERIMENTAL_CACHED_ENCODED_SIZE
memcpy(d->signatures_name[uid-LABCOMM_USER], &d->c[nstart+lenlen], nlen);
d->signatures_name[uid-LABCOMM_USER][nlen]=0;
newsig->name = d->signatures_name[uid-LABCOMM_USER];
} else {
error("sig name longer than max length (this ought to be dynamic...");
}
VERBOSE_PRINTF("signature for uid %x: %s (start=%x,end=%x, nlen=%d,len=%d)\n", uid, get_signature_name(d, uid), start,end, nlen, len);
INFO_PRINTF("SIG: %s\n", newsig->name);
if(! d->current_decl_is_varsize) { if(! d->current_decl_is_varsize) {
newsig->cached_encoded_size = enc_size; newsig->cached_encoded_size = enc_size;
newsig->encoded_size = encoded_size_static; newsig->encoded_size = encoded_size_static;
...@@ -385,28 +417,92 @@ static int accept_signature(labcomm_sig_parser_t *d) ...@@ -385,28 +417,92 @@ static int accept_signature(labcomm_sig_parser_t *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");
} }
return TRUE; #endif
return LABCOMM2014_TRUE;
}
static int accept_decl(labcomm2014_sig_parser_t *d, labcomm2014_type type)
{
if(accept_user_id(d)) {
unsigned int uid = pop_val(d);
VERBOSE_PRINTF(", intentions = ");
accept_intentions(d);
unsigned int start = d->idx;
unsigned int nstart = pop_val(d);
unsigned int nlen = pop_val(d);
#ifdef RETURN_STRINGS
char *str = (char *) pop_ptr(d);
free(str);
#endif
unsigned char lenlen = labcomm2014_varint_sizeof(nlen);
if(type != PKG_SAMPLE_DECL) {
// don't record typedefs and samplerefs (for now)
// to avoid number clashes with sample defs
// in the parser struct
return accept_signature(d, type, start, uid, (char *) &d->c[nstart+lenlen]);
}
if(nlen < d->max_name_len) { // leave 1 byte for terminating NULL
char *name;
d->signatures_name_length[uid-LABCOMM_USER] = nlen;
memcpy(d->signatures_name[uid-LABCOMM_USER], &d->c[nstart+lenlen], nlen);
d->signatures_name[uid-LABCOMM_USER][nlen]=0;
name = d->signatures_name[uid-LABCOMM_USER];
return accept_signature(d, type, start, uid, name);
} else {
error("sig name longer than max length (this ought to be dynamic...");
return LABCOMM2014_FALSE;
}
} else { } else {
error("sample_decl with uid < LABCOMM_USER"); error("sample_decl with uid < LABCOMM_USER");
return FALSE; return LABCOMM2014_FALSE;
} }
} }
// HERE BE DRAGONS! what does the return value mean? // HERE BE DRAGONS! what does the return value mean?
int accept_packet(labcomm_sig_parser_t *d) { int accept_packet(labcomm2014_sig_parser_t *d) {
unsigned char nbytes; size_t nbytes;
unsigned int type = peek_varint(d, &nbytes) ; unsigned int type = peek_varint(d, &nbytes) ;
if(type == TYPE_DECL ) { if(type == PKG_VERSION ) {
//XXX is this used? If so, is it correct? 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 == PKG_SAMPLE_DECL) {
d->current_decl_is_varsize = LABCOMM2014_FALSE; // <-- a conveniance flag in labcomm2014_sig_parser_t
advancen(d, nbytes); advancen(d, nbytes);
d->current_decl_is_varsize = FALSE; // <-- a conveniance flag in labcomm_sig_parser_t VERBOSE_PRINTF("sample_decl ");
get_varint(d); //ignore length field
accept_decl(d, type);
} else if (type == PKG_SAMPLE_REF) {
d->current_decl_is_varsize = LABCOMM2014_FALSE; // <-- a conveniance flag in labcomm2014_sig_parser_t
advancen(d, nbytes);
VERBOSE_PRINTF("sample_ref ");
get_varint(d); //ignore length field
accept_decl(d, type);
}else if(type == PKG_TYPE_DECL ) {
d->current_decl_is_varsize = LABCOMM2014_FALSE; // <-- a conveniance flag in labcomm2014_sig_parser_t
advancen(d, nbytes);//consume type field
VERBOSE_PRINTF("type_decl "); VERBOSE_PRINTF("type_decl ");
accept_signature(d); get_varint(d); //ignore length field
} else if (type == SAMPLE_DECL) { accept_decl(d, type);
d->current_decl_is_varsize = FALSE; // <-- a conveniance flag in labcomm_sig_parser_t } else if (type == PKG_TYPE_BINDING) {
VERBOSE_PRINTF("type_binding ");
advancen(d, nbytes); advancen(d, nbytes);
VERBOSE_PRINTF("sample_decl "); get_varint(d); //ignore length field
accept_signature(d); #ifdef VERBOSE
int sid =
#endif
get_varint(d); //ignore sample id field
#ifdef VERBOSE
int tid =
#endif
get_varint(d); //ignore type id field
VERBOSE_PRINTF("sid=0x%x, tid=0x%x\n ", sid, tid);
} 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");
...@@ -415,27 +511,87 @@ int accept_packet(labcomm_sig_parser_t *d) { ...@@ -415,27 +511,87 @@ int accept_packet(labcomm_sig_parser_t *d) {
accept_sample_data(d); accept_sample_data(d);
#endif #endif
} else { } else {
#ifdef EXIT_ON_UNKNOWN_TAG
error("got unknown type (<LABCOMM_USER)"); error("got unknown type (<LABCOMM_USER)");
exit(1); exit(1);
#else
int len = get_varint(d); // length field
printf("got unknown tag: 0x%x, skipping %d bytes\n",type, len);
advancen(d, len);
#endif
} }
return LABCOMM2014_TRUE;
} }
static int accept_user_id(labcomm_sig_parser_t *d){ static int accept_user_id(labcomm2014_sig_parser_t *d){
unsigned char nbytes; size_t nbytes;
unsigned int uid = peek_varint(d, &nbytes); int uid = peek_varint(d, &nbytes);
if(uid >= LABCOMM_USER) { if(uid >= LABCOMM_USER) {
advancen(d, nbytes); advancen(d, nbytes);
VERBOSE_PRINTF("uid = %x ", uid); VERBOSE_PRINTF("uid = 0x%x ", uid);
push_val(d, uid); push_val(d, uid);
return TRUE; return LABCOMM2014_TRUE;
} else { } else {
return FALSE; error("uid < LABCOMM_USER");
return LABCOMM2014_FALSE;
} }
} }
static int accept_string(labcomm_sig_parser_t *d){ /** pushes (index in stream of name) and (length of name) */
static int accept_intentions(labcomm2014_sig_parser_t *d){
unsigned int num = get_varint(d);
int i;
int npos = 0;
int nlen = 0;
for(i=0; i<num; i++) {
int klen, vlen;
printf("( ");
#ifdef RETURN_STRINGS
char *key;
char *val;
#endif
accept_string(d);
klen = pop_val(d);
if(klen==0) {
npos = d->idx;
}
#ifdef RETURN_STRINGS
key = (char *) pop_ptr(d);
if(klen!=0) {
printf("%s] : ",key);
}
free(key);
#else
if(klen!=0) {
printf(": ");
} else {
printf("name: ");
}
#endif
accept_string(d);
printf(" ");
vlen = pop_val(d);
if(klen==0) {
nlen = vlen;
}
#ifdef RETURN_STRINGS
val = (char *) pop_ptr(d);
printf("%s %s",(klen?",val : ":""), val);
free(val);
#endif
printf(") ");
}
push_val(d, nlen);
push_val(d, npos);
return LABCOMM2014_TRUE;
}
static int accept_string(labcomm2014_sig_parser_t *d){
unsigned int len = get_varint(d); unsigned int len = get_varint(d);
char *str=malloc(len); unsigned char *str=malloc(len+1); // len is without terminating null
getStr(d, str, len); getStr(d, str, len);
VERBOSE_PRINTF("%s", str); VERBOSE_PRINTF("%s", str);
#ifdef RETURN_STRINGS #ifdef RETURN_STRINGS
...@@ -444,11 +600,11 @@ static int accept_string(labcomm_sig_parser_t *d){ ...@@ -444,11 +600,11 @@ static int accept_string(labcomm_sig_parser_t *d){
free(str); free(str);
#endif #endif
push_val(d, len); push_val(d, len);
return TRUE; return LABCOMM2014_TRUE;
} }
/* pushes size and type id */ /* pushes size and type id */
static int accept_type(labcomm_sig_parser_t *d){ static int accept_type(labcomm2014_sig_parser_t *d){
unsigned char nbytes; size_t nbytes;
unsigned int type = peek_varint(d, &nbytes) ; unsigned int type = peek_varint(d, &nbytes) ;
switch(type) { switch(type) {
case TYPE_BOOLEAN : case TYPE_BOOLEAN :
...@@ -489,9 +645,14 @@ static int accept_type(labcomm_sig_parser_t *d){ ...@@ -489,9 +645,14 @@ static int accept_type(labcomm_sig_parser_t *d){
case TYPE_STRING : case TYPE_STRING :
VERBOSE_PRINTF("string\n"); VERBOSE_PRINTF("string\n");
advancen(d, nbytes); advancen(d, nbytes);
labcomm_sig_parser_t_set_varsize(d); labcomm2014_sig_parser_t_set_varsize(d);
push_val(d, 0); push_val(d, 0);
break; break;
case TYPE_SAMPLE_REF :
VERBOSE_PRINTF("sample\n");
advancen(d, nbytes);
push_val(d, 4);
break;
case ARRAY_DECL : case ARRAY_DECL :
accept_array_decl(d); accept_array_decl(d);
pop_val(d); // ignore element type pop_val(d); // ignore element type
...@@ -502,19 +663,22 @@ static int accept_type(labcomm_sig_parser_t *d){ ...@@ -502,19 +663,22 @@ static int accept_type(labcomm_sig_parser_t *d){
// push(d, pop(d) is a NOP --> leave size on stack // push(d, pop(d) is a NOP --> leave size on stack
break; break;
default : default :
printf("accept_type default (type==%x) should not happen\n", type); //should we distinguish between SAMPLE_DEF and TYPE_DEF here?
//printf("accept_type default (type==%x) should not happen\n", type);
VERBOSE_PRINTF("user type 0x%x\n",type);
advancen(d, nbytes);
push_val(d, 0); push_val(d, 0);
push_val(d, type); push_val(d, type);
return FALSE; return LABCOMM2014_FALSE;
} }
push_val(d, type); push_val(d, type);
return TRUE; return LABCOMM2014_TRUE;
} }
/* pushes size and element type */ /* pushes size and element type */
static int accept_array_decl(labcomm_sig_parser_t *d){ static int accept_array_decl(labcomm2014_sig_parser_t *d){
unsigned char nbytes; size_t nbytes;
unsigned int tid = peek_varint(d, &nbytes) ; int tid = peek_varint(d, &nbytes) ;
if(tid == ARRAY_DECL) { if(tid == ARRAY_DECL) {
advancen(d, nbytes); advancen(d, nbytes);
unsigned int nidx = get_varint(d); unsigned int nidx = get_varint(d);
...@@ -527,7 +691,7 @@ static int accept_array_decl(labcomm_sig_parser_t *d){ ...@@ -527,7 +691,7 @@ static int accept_array_decl(labcomm_sig_parser_t *d){
if(idx == 0) { if(idx == 0) {
numVar++; numVar++;
VERBOSE_PRINTF("[_] "); VERBOSE_PRINTF("[_] ");
labcomm_sig_parser_t_set_varsize(d); labcomm2014_sig_parser_t_set_varsize(d);
} else { } else {
VERBOSE_PRINTF("[%d] ", idx); VERBOSE_PRINTF("[%d] ", idx);
size*=idx; size*=idx;
...@@ -550,45 +714,52 @@ static int accept_array_decl(labcomm_sig_parser_t *d){ ...@@ -550,45 +714,52 @@ static int accept_array_decl(labcomm_sig_parser_t *d){
push_val(d, 0); push_val(d, 0);
} }
push_val(d, et); push_val(d, et);
return TRUE; return LABCOMM2014_TRUE;
} else { } else {
printf("accept_array_decl: type=%x, should not happen\n",tid); printf("accept_array_decl: type=%x, should not happen\n",tid);
push_val(d, 0); push_val(d, 0);
push_val(d, tid); push_val(d, tid);
return FALSE; return LABCOMM2014_FALSE;
} }
} }
/* pushes size */ /* pushes size */
static int accept_struct_decl(labcomm_sig_parser_t *d){ static int accept_struct_decl(labcomm2014_sig_parser_t *d){
unsigned char nbytes; size_t nbytes;
unsigned int tid = peek_varint(d, &nbytes) ; int tid = peek_varint(d, &nbytes) ;
if(tid == STRUCT_DECL) { if(tid == STRUCT_DECL) {
advancen(d, nbytes); advancen(d, nbytes);
unsigned int nf = get_varint(d); unsigned int nf = get_varint(d);
if(nf == 0) {
VERBOSE_PRINTF("void\n");
} else {
VERBOSE_PRINTF("%d field struct:\n", nf); VERBOSE_PRINTF("%d field struct:\n", nf);
}
int i; int i;
#ifdef USE_UNUSED_VARS
int numVar=0; int numVar=0;
int size=0; int size=0;
#endif
unsigned int fieldsizes=0; unsigned int fieldsizes=0;
for(i=0; i<nf; i++) { for(i=0; i<nf; i++) {
accept_field(d); accept_field(d);
fieldsizes += pop_val(d); fieldsizes += pop_val(d);
} }
push_val(d, fieldsizes); push_val(d, fieldsizes);
return TRUE; return LABCOMM2014_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);
return FALSE; return LABCOMM2014_FALSE;
} }
} }
/* pushes field size */ /* pushes field size */
static int accept_field(labcomm_sig_parser_t *d){ static int accept_field(labcomm2014_sig_parser_t *d){
VERBOSE_PRINTF("\tfield name: "); VERBOSE_PRINTF("\tfield: ");
accept_string(d); accept_intentions(d);
pop_val(d); // ignore length, for now pop_val(d); // ignore name pos, for now
pop_val(d); // ignore name length, for now
#ifdef RETURN_STRINGS #ifdef RETURN_STRINGS
char *str = (char *) pop_ptr(d); char *str = (char *) pop_ptr(d);
free(str); free(str);
...@@ -598,28 +769,34 @@ static int accept_field(labcomm_sig_parser_t *d){ ...@@ -598,28 +769,34 @@ static int accept_field(labcomm_sig_parser_t *d){
pop_val(d); // ignore type, for now pop_val(d); // ignore type, for now
// push(pop() is really a NOP , leave size on the stack when debugging done // push(pop() is really a NOP , leave size on the stack when debugging done
VERBOSE_PRINTF("\n"); VERBOSE_PRINTF("\n");
return LABCOMM2014_TRUE;
} }
static int accept_sample_data(labcomm_sig_parser_t *d){ static int accept_sample_data(labcomm2014_sig_parser_t *d){
accept_user_id(d); accept_user_id(d);
unsigned int uid = pop_val(d); unsigned int uid = pop_val(d);
printf("sample data... uid=0x%x\n", uid); printf("sample data... uid=0x%x\n", uid);
int len = get_varint(d); //length field
#ifdef DEBUG #ifdef DEBUG
dump_signature(d, uid); dump_signature(d, uid);
#endif #endif
labcomm_signature_t *sigt = get_sig_t(d, uid); #ifdef SKIP_BY_PARSING
int encoded_size = sigt->encoded_size(sigt, NULL); struct labcomm2014_signature *sigt = get_sig_t(d, uid);
int encoded_size = sigt->encoded_size(NULL);
INFO_PRINTF("encoded_size from sig: %d\n", encoded_size); INFO_PRINTF("encoded_size from sig: %d\n", encoded_size);
labcomm_signature_t *sig = get_sig_t(d, uid); struct labcomm2014_signature *sig = get_sig_t(d, uid);
skip_packed_sample_data(d, sig); skip_packed_sample_data(d, sig);
return TRUE; #else
advancen(d, len);
#endif
return LABCOMM2014_TRUE;
} }
static int skip_type(unsigned int,labcomm_sig_parser_t*,unsigned char*,unsigned int,unsigned int*) ; static int skip_type(unsigned int,labcomm2014_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(labcomm2014_sig_parser_t *d, unsigned char *sig, int len, int *pos) {
unsigned int skip = 0; unsigned int skip = 0;
unsigned int tot_nbr_elem_tmp = 1; unsigned int tot_nbr_elem_tmp = 1;
unsigned char nbytes; size_t nbytes;
unsigned int nIdx = unpack_varint(sig, *pos, &nbytes); unsigned int nIdx = unpack_varint(sig, *pos, &nbytes);
VERBOSE_PRINTF("skip_array: nIdx = %d (from sig)\n", nIdx); VERBOSE_PRINTF("skip_array: nIdx = %d (from sig)\n", nIdx);
*pos +=nbytes; *pos +=nbytes;
...@@ -638,7 +815,7 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int ...@@ -638,7 +815,7 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int
tot_nbr_elem_tmp *= idx[i]; 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 unsigned char varSize=0; // total number of bytes required for var size fields
for(i=0; i<nVar; i++) { for(i=0; i<nVar; i++) {
...@@ -648,10 +825,10 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int ...@@ -648,10 +825,10 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int
tot_nbr_elem_tmp *= var[i]; tot_nbr_elem_tmp *= var[i];
} }
unsigned int type = unpack_varint(sig, *pos, &nbytes); int type = unpack_varint(sig, *pos, &nbytes);
*pos+=nbytes; *pos+=nbytes;
unsigned int elemSize = labcomm_sizeof_primitive(type); unsigned int elemSize = labcomm2014_sizeof_primitive(type);
skip = elemSize * tot_nbr_elem_tmp; skip = elemSize * tot_nbr_elem_tmp;
...@@ -663,9 +840,9 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int ...@@ -663,9 +840,9 @@ static int skip_array(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int
return skip + varSize; return skip + varSize;
} }
int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, unsigned int *pos) { int skip_struct(labcomm2014_sig_parser_t *d, unsigned char *sig, unsigned int len, int *pos) {
unsigned char nbytes; size_t nbytes;
unsigned int nFields = unpack_varint(sig,*pos, &nbytes); int nFields = unpack_varint(sig,*pos, &nbytes);
*pos += nbytes; *pos += nbytes;
unsigned int i; unsigned int i;
unsigned int skipped=0; unsigned int skipped=0;
...@@ -676,7 +853,7 @@ int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, u ...@@ -676,7 +853,7 @@ int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, u
#ifdef DEBUG #ifdef DEBUG
VERBOSE_PRINTF("field #%d:\n----namelen==%d\n",i,namelen); VERBOSE_PRINTF("field #%d:\n----namelen==%d\n",i,namelen);
char name[namelen+1]; //HERE BE DRAGONS. alloca? char name[namelen+1]; //HERE BE DRAGONS. alloca?
strncpy(name, sig+*pos+nbytes, namelen); strncpy(name, (const char *)sig+*pos+nbytes, namelen);
name[namelen]=0; name[namelen]=0;
VERBOSE_PRINTF("----name = %s\n",name); VERBOSE_PRINTF("----name = %s\n",name);
#endif #endif
...@@ -693,8 +870,8 @@ int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, u ...@@ -693,8 +870,8 @@ int skip_struct(labcomm_sig_parser_t *d, unsigned char *sig, unsigned int len, u
} }
#ifndef QUIET #ifndef QUIET
/* print and skip */ /* print and skip */
int skip_type(unsigned int type, labcomm_sig_parser_t *d, int skip_type(unsigned int type, labcomm2014_sig_parser_t *d,
unsigned char *sig, unsigned int len, unsigned int *pos) unsigned char *sig, unsigned int len, int *pos)
{ {
int skipped=0; int skipped=0;
printf("skip_type %x:", type); printf("skip_type %x:", type);
...@@ -729,8 +906,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d, ...@@ -729,8 +906,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
break; break;
case TYPE_STRING : case TYPE_STRING :
{ {
unsigned char nbytes; size_t nbytes;
unsigned int len = get_varint_size(d, &nbytes); int len = get_varint_size(d, &nbytes);
int i; int i;
printf("string ["); printf("string [");
for(i=0; i<len; i++) for(i=0; i<len; i++)
...@@ -753,8 +930,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d, ...@@ -753,8 +930,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
return skipped; return skipped;
} }
#else #else
int skip_type(unsigned int type, labcomm_sig_parser_t *d, int skip_type(unsigned int type, labcomm2014_sig_parser_t *d,
unsigned char *sig, unsigned int len, unsigned int *pos) const char *sig, unsigned int len, int *pos)
{ {
int skipped=0; int skipped=0;
VERBOSE_PRINTF("skip_type %x\n", type); VERBOSE_PRINTF("skip_type %x\n", type);
...@@ -780,8 +957,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d, ...@@ -780,8 +957,8 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
break; break;
case TYPE_STRING : case TYPE_STRING :
{ {
unsigned char nbytes; size_t nbytes;
unsigned int len = get_varint_size(d, &nbytes); int len = get_varint_size(d, &nbytes);
advancen(d,len); advancen(d,len);
skipped+=len+nbytes; skipped+=len+nbytes;
break;} break;}
...@@ -799,17 +976,17 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d, ...@@ -799,17 +976,17 @@ int skip_type(unsigned int type, labcomm_sig_parser_t *d,
} }
#endif #endif
/* parse signature and skip the corresponding bytes in the labcomm_sig_parser_t /* parse signature and skip the corresponding bytes in the labcomm2014_sig_parser_t
*/ */
int skip_packed_sample_data(labcomm_sig_parser_t *d, labcomm_signature_t *sig) { int skip_packed_sample_data(labcomm2014_sig_parser_t *d, struct labcomm2014_signature *sig) {
unsigned int pos = 0; //current position in signature int pos = 0; //current position in signature
unsigned int skipped = 0; //skipped byte counter unsigned int skipped = 0; //skipped byte counter
while(pos < sig->size) { while(pos < sig->size) {
unsigned char nbytes; size_t nbytes;
unsigned int type = unpack_varint(sig->signature,pos, &nbytes); int type = unpack_varint(sig->signature,pos, &nbytes);
pos+=nbytes; pos+=nbytes;
skipped += skip_type(type, d, sig->signature, sig->size, &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 LABCOMM2014_TRUE;
} }
/* labcomm_sig_parser.h: /* labcomm2014_sig_parser.h:
* an example parser for labcomm signatures, illustrating how to skip samples * an example parser for labcomm signatures, illustrating how to skip samples
* based on their signature. Intended as an embryo for introducing this * based on their signature. Intended as an embryo for introducing this
* functionality into the lib to allow a channel to survive types with no * functionality into the lib to allow a channel to survive types with no
...@@ -8,22 +8,27 @@ ...@@ -8,22 +8,27 @@
#ifndef LABCOMM_SIG_PARSER_H #ifndef LABCOMM_SIG_PARSER_H
#define LABCOMM_SIG_PARSER_H #define LABCOMM_SIG_PARSER_H
#include "../labcomm_private.h" #include "labcomm2014.h"
#include "labcomm2014_private.h"
#undef DEBUG #undef DEBUG
#undef QUIET_STACK // don't print anything for push/pop #define QUIET_STACK // don't print anything for push/pop
#undef DEBUG_STACK_VERBOSE // dump stack, otherwise just print value of top #undef DEBUG_STACK_VERBOSE // dump stack, otherwise just print value of top
#undef QUIET //just print type and size when skipping data #undef QUIET //just print type and size when skipping data
#undef VERBOSE // print in great detail #define VERBOSE // print in great detail
#undef SKIP_BY_PARSING
#undef STATIC_ALLOCATION //dynamic allocation not completely implemented #undef STATIC_ALLOCATION //dynamic allocation not completely implemented
#ifdef STATIC_ALLOCATION #ifdef STATIC_ALLOCATION
#define MAX_SIGNATURES 16 #define MAX_SIGNATURES 100
#define MAX_NAME_LEN 32 #define MAX_NAME_LEN 32
#define MAX_SIG_LEN 128 #define MAX_SIG_LEN 128
#define TYPEDEF_BASE MAX_SIGNATURES
#endif #endif
...@@ -32,7 +37,7 @@ typedef struct { ...@@ -32,7 +37,7 @@ typedef struct {
unsigned char* c; unsigned char* c;
size_t size; size_t size;
size_t capacity; size_t capacity;
unsigned int idx; int idx;
int val_top; int val_top;
int * val_stack; int * val_stack;
int ptr_top; int ptr_top;
...@@ -43,44 +48,51 @@ typedef struct { ...@@ -43,44 +48,51 @@ typedef struct {
size_t max_signatures; // set by init(...) size_t max_signatures; // set by init(...)
size_t max_name_len; size_t max_name_len;
size_t max_sig_len; size_t max_sig_len;
// arrays for signatures and typedefs
// signatures start at index 0
// typedefs start at index MAX_SIGNATURES
#ifdef STATIC_ALLOCATION #ifdef STATIC_ALLOCATION
labcomm_signature_t sig_ts[MAX_SIGNATURES]; struct labcomm2014_signature sig_ts[2*MAX_SIGNATURES];
unsigned int signatures_length[MAX_SIGNATURES]; unsigned int signatures_length[2*MAX_SIGNATURES];
unsigned int signatures_name_length[MAX_SIGNATURES]; unsigned int signatures_name_length[2*MAX_SIGNATURES];
unsigned char signatures_name[MAX_SIGNATURES][MAX_NAME_LEN]; unsigned char signatures_name[2*MAX_SIGNATURES][MAX_NAME_LEN];
unsigned char signatures[MAX_SIGNATURES][MAX_SIG_LEN]; unsigned char signatures[2*MAX_SIGNATURES][MAX_SIG_LEN];
#else #else
labcomm_signature_t *sig_ts; // [MAX_SIGNATURES] struct labcomm2014_signature *sig_ts; // [2*MAX_SIGNATURES]
unsigned int *signatures_length; // [MAX_SIGNATURES] unsigned int *signatures_length; // [2*MAX_SIGNATURES]
unsigned char **signatures; // [MAX_SIGNATURES][MAX_SIG_LEN]; unsigned char **signatures; // [2*MAX_SIGNATURES][MAX_SIG_LEN];
unsigned int *signatures_name_length; // [MAX_SIGNATURES] unsigned int *signatures_name_length; // [2*MAX_SIGNATURES]
unsigned char **signatures_name; // [MAX_SIGNATURES][MAX_NAME_LEN]; char **signatures_name; // [2*MAX_SIGNATURES][MAX_NAME_LEN];
#endif #endif
} labcomm_sig_parser_t; } labcomm2014_sig_parser_t;
int labcomm_sig_parser_init(labcomm_sig_parser_t *p, size_t size, int labcomm2014_sig_parser_init(labcomm2014_sig_parser_t *p, size_t size,
size_t stacksize, size_t max_num_signatures, size_t stacksize, size_t max_num_signatures,
size_t max_name_len, size_t max_sig_len); size_t max_name_len, size_t max_sig_len);
int labcomm_sig_parser_read_file(labcomm_sig_parser_t *p, FILE *f); void labcomm2014_sig_parser_free(labcomm2014_sig_parser_t *b);
int labcomm2014_sig_parser_read_file(labcomm2014_sig_parser_t *p, FILE *f);
int accept_packet(labcomm2014_sig_parser_t *p);
struct labcomm2014_signature *get_sig_t(labcomm2014_sig_parser_t *p,unsigned int uid);
int accept_packet(labcomm_sig_parser_t *p); unsigned int get_signature_len(labcomm2014_sig_parser_t *p,unsigned int uid);
char* get_signature_name(labcomm2014_sig_parser_t *p,unsigned int uid);
unsigned char* get_signature(labcomm2014_sig_parser_t *p,unsigned int uid);
void dump_signature(labcomm2014_sig_parser_t *p,unsigned int uid);
labcomm_signature_t *get_sig_t(labcomm_sig_parser_t *p,unsigned int uid);
unsigned int get_signature_len(labcomm_sig_parser_t *p,unsigned int uid); int more(labcomm2014_sig_parser_t *b);
unsigned 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);
/* parse signature and skip the corresponding bytes in the labcomm_sig_parser /* parse signature and skip the corresponding bytes in the labcomm2014_sig_parser
*/ */
int skip_packed_sample_data(labcomm_sig_parser_t *p, labcomm_signature_t *sig); int skip_packed_sample_data(labcomm2014_sig_parser_t *p, struct labcomm2014_signature *sig);
#ifdef QUIET #ifdef QUIET
#define INFO_PRINTF(format, args...) #define INFO_PRINTF(format, args...)
...@@ -101,16 +113,12 @@ int skip_packed_sample_data(labcomm_sig_parser_t *p, labcomm_signature_t *sig); ...@@ -101,16 +113,12 @@ int skip_packed_sample_data(labcomm_sig_parser_t *p, labcomm_signature_t *sig);
#undef RETURN_STRINGS // not really tested #undef RETURN_STRINGS // not really tested
#ifndef TRUE
#define FALSE 0
#define TRUE 1
#endif
typedef enum{ typedef enum{
TYPE_DECL = LABCOMM_TYPEDEF, PKG_VERSION = LABCOMM_VERSION,
SAMPLE_DECL = LABCOMM_SAMPLE, PKG_SAMPLE_DECL = LABCOMM_SAMPLE_DEF,
PKG_SAMPLE_REF = LABCOMM_SAMPLE_REF,
PKG_TYPE_DECL = LABCOMM_TYPE_DEF,
PKG_TYPE_BINDING = LABCOMM_TYPE_BINDING,
ARRAY_DECL = LABCOMM_ARRAY, ARRAY_DECL = LABCOMM_ARRAY,
STRUCT_DECL = LABCOMM_STRUCT, STRUCT_DECL = LABCOMM_STRUCT,
...@@ -122,6 +130,7 @@ typedef enum{ ...@@ -122,6 +130,7 @@ typedef enum{
TYPE_LONG = LABCOMM_LONG, TYPE_LONG = LABCOMM_LONG,
TYPE_FLOAT = LABCOMM_FLOAT, TYPE_FLOAT = LABCOMM_FLOAT,
TYPE_DOUBLE = LABCOMM_DOUBLE, TYPE_DOUBLE = LABCOMM_DOUBLE,
TYPE_STRING = LABCOMM_STRING TYPE_STRING = LABCOMM_STRING,
} labcomm_type ; TYPE_SAMPLE_REF = LABCOMM_REF
} labcomm2014_type ;
#endif #endif
/* labcomm_sig_parser.c: /* labcomm2014_sig_parser.c:
* a main program for the example labcomm signatures parser * a main program for the example labcomm signatures parser
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "labcomm_sig_parser.h" #include "experimental/labcomm2014_sig_parser.h"
#undef DEBUG_READ #define DEBUG_READ
#define BUF_SIZE 1024 #define BUF_SIZE 1024
#define STACK_SIZE 16 #define STACK_SIZE 16
#define MAX_NUM_SIGNATURES 10 #define MAX_NUM_SIGNATURES 10
#define MAX_SIGNATURES 16 #define MAX_SIGNATURES 100
#define MAX_NAME_LEN 32 #define MAX_NAME_LEN 64
#define MAX_SIG_LEN 128 #define MAX_SIG_LEN 512
void test_read(labcomm_sig_parser_t *p) { void test_read(labcomm2014_sig_parser_t *p) {
int r = labcomm_sig_parser_read_file(p, stdin); int r = labcomm2014_sig_parser_read_file(p, stdin);
#ifdef DEBUG_READ #ifdef DEBUG_READ
printf("read %d bytes:\n\n", r); printf("read %d bytes:\n\n", r);
int i; int i;
...@@ -28,9 +29,9 @@ void test_read(labcomm_sig_parser_t *p) { ...@@ -28,9 +29,9 @@ void test_read(labcomm_sig_parser_t *p) {
#endif #endif
} }
int main() { int main() {
labcomm_sig_parser_t p; labcomm2014_sig_parser_t p;
if(labcomm_sig_parser_init(&p, BUF_SIZE, STACK_SIZE, if(labcomm2014_sig_parser_init(&p, BUF_SIZE, STACK_SIZE,
MAX_NUM_SIGNATURES, MAX_NAME_LEN, MAX_SIG_LEN) ) { MAX_NUM_SIGNATURES, MAX_NAME_LEN, MAX_SIG_LEN) ) {
printf("failed to init buffer\n"); printf("failed to init buffer\n");
exit(1); exit(1);
...@@ -40,5 +41,6 @@ int main() { ...@@ -40,5 +41,6 @@ int main() {
printf("--------------------------------------------- new packet: \n"); printf("--------------------------------------------- new packet: \n");
} while(more(&p) && accept_packet(&p)); } while(more(&p) && accept_packet(&p));
printf("EOF\n"); printf("EOF\n");
labcomm2014_sig_parser_free(&p);
} }