Commit 1bee4d4e authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

decoding of typedefs implemented in C and java

parent 1359b683
......@@ -570,11 +570,7 @@ aspect Java_Class {
// env.println();
env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
env.indent();
env.println("e.begin(Constant.TYPE_DEF);");
env.println("e.encodePacked32(index);");
env.println("e.encodeString(getName());");
env.println("emitSignature(e);");
env.println("e.end(null);");
env.unindent();
env.println("}");
env.println();
......
......@@ -47,19 +47,19 @@
UNWRAP_ac( __VA_ARGS__) = UNWRAP_ac(__VA_ARGS__)->next; \
}
int labcomm_reader_alloc(struct labcomm_reader *r,
int labcomm_reader_alloc(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context)
{
UNWRAP(alloc, r, action_context);
}
int labcomm_reader_free(struct labcomm_reader *r,
int labcomm_reader_free(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context)
{
UNWRAP(free, r, action_context);
}
int labcomm_reader_start(struct labcomm_reader *r,
int labcomm_reader_start(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
int local_index, int remote_index,
const struct labcomm_signature *signature,
......@@ -68,41 +68,41 @@ int labcomm_reader_start(struct labcomm_reader *r,
UNWRAP(start, r, action_context, local_index, remote_index, signature, value);
}
int labcomm_reader_end(struct labcomm_reader *r,
int labcomm_reader_end(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context)
{
UNWRAP(end, r, action_context);
}
int labcomm_reader_fill(struct labcomm_reader *r,
int labcomm_reader_fill(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context)
{
UNWRAP(fill, r, action_context);
}
int labcomm_reader_ioctl(struct labcomm_reader *r,
int labcomm_reader_ioctl(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context,
int local_index, int remote_index,
const struct labcomm_signature *signature,
const struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args)
{
UNWRAP(ioctl, r, action_context,
UNWRAP(ioctl, r, action_context,
local_index, remote_index, signature, ioctl_action, args);
}
int labcomm_writer_alloc(struct labcomm_writer *w,
int labcomm_writer_alloc(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context)
{
UNWRAP(alloc, w, action_context);
}
int labcomm_writer_free(struct labcomm_writer *w,
int labcomm_writer_free(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context)
{
UNWRAP(free, w, action_context);
}
int labcomm_writer_start(struct labcomm_writer *w,
int labcomm_writer_start(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
int index, const struct labcomm_signature *signature,
void *value)
......@@ -110,26 +110,26 @@ int labcomm_writer_start(struct labcomm_writer *w,
UNWRAP(start, w, action_context, index, signature, value);
}
int labcomm_writer_end(struct labcomm_writer *w,
int labcomm_writer_end(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context)
{
UNWRAP(end, w, action_context);
}
}
int labcomm_writer_flush(struct labcomm_writer *w,
int labcomm_writer_flush(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context)
{
UNWRAP(flush, w, action_context);
}
}
int labcomm_writer_ioctl(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
int index,
const struct labcomm_signature *signature,
int labcomm_writer_ioctl(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context,
int index,
const struct labcomm_signature *signature,
uint32_t ioctl_action, va_list args)
{
UNWRAP(ioctl, w, action_context, index, signature, ioctl_action, args);
}
}
#undef UNWRAP
#undef UNWRAP_ac
......@@ -137,12 +137,12 @@ int labcomm_writer_ioctl(struct labcomm_writer *w,
static const char *labcomm_error_string[] = {
static const char *labcomm_error_string[] = {
#define LABCOMM_ERROR(name, description) description ,
#include "labcomm_error.h"
#undef LABCOMM_ERROR
};
static const int labcomm_error_string_count = (sizeof(labcomm_error_string) /
static const int labcomm_error_string_count = (sizeof(labcomm_error_string) /
sizeof(labcomm_error_string[0]));
......@@ -175,7 +175,7 @@ void labcomm2014_on_error_fprintf(enum labcomm_error error_id, size_t nbr_va_arg
fprintf(stderr, "}\n");
va_end(arg_pointer);
}
}
#else
; // If labcomm can't be compiled with stdio the user will have to make an own error callback functionif he/she needs error reporting.
#endif
......@@ -207,8 +207,8 @@ void *labcomm_signature_array_ref(struct labcomm_memory *memory,
*first = index;
*last = index + 1;
*data = labcomm_memory_alloc(memory, 0, size);
if (*data) {
memset(*data, 0, size);
if (*data) {
memset(*data, 0, size);
}
} else if (index < *first || *last <= index) {
void *old_data = *data;
......@@ -221,8 +221,8 @@ void *labcomm_signature_array_ref(struct labcomm_memory *memory,
*data = labcomm_memory_alloc(memory, 0, n * size);
if (*data) {
memset(*data, 0, n * size);
memcpy(*data + (old_first - *first) * size,
old_data,
memcpy(*data + (old_first - *first) * size,
old_data,
(old_last - old_first) * size);
}
// dump(old_data, size, old_first, old_last);
......
......@@ -166,6 +166,72 @@ static int handle_sample_ref(struct labcomm_decoder *d, int remote_index,
return result;
}
static int decoder_skip(struct labcomm_decoder *d, int len, int tag)
{
int i;
printf("got tag 0x%x, skipping %d bytes\n", tag, len);
for(i = 0; i <len; i++){
fprintf(stderr,".");
labcomm_read_byte(d->reader);
if (d->reader->error < 0) {
fprintf(stderr, "\nerror while skipping: %d\n", d->reader->error);
return d->reader->error;
}
}
fprintf(stderr, "\n");
return tag;
}
static int decode_type_binding(struct labcomm_decoder *d, int kind)
{
int sample_index = labcomm_read_packed32(d->reader);
int typedef_index = labcomm_read_packed32(d->reader);
printf("type_binding: 0x%x -> 0x%x\n", sample_index, typedef_index);
int result = kind;
return result;
}
static int decode_type_def(struct labcomm_decoder *d, int kind){
int i, remote_index, result;
char *name;
int size;
remote_index = labcomm_read_packed32(d->reader);
result = remote_index;
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
}
name = labcomm_read_string(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
}
size = labcomm_read_packed32(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto free_signature_name;
}
//printf("got typedef 0x%x : %s, skipping %d signature bytes", remote_index, name, size);
#if 0
signature.signature = labcomm_memory_alloc(d->memory, 1, signature.size);
if (d->reader->error < 0) {
result = d->reader->error;
goto free_signature_name;
}
#endif
for (i = 0 ; i < size ; i++) {
/*signature.signature[i] =*/ labcomm_read_byte(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto free_signature_signature;
}
}
free_signature_signature:
// labcomm_memory_free(d->memory, 1, signature.signature);
free_signature_name:
labcomm_memory_free(d->memory, 0, name);
out:
return result;
}
static int decode_sample_def_or_ref(struct labcomm_decoder *d, int kind)
{
......@@ -200,16 +266,31 @@ static int decode_sample_def_or_ref(struct labcomm_decoder *d, int kind)
goto free_signature_signature;
}
}
if (kind == LABCOMM_SAMPLE_DEF) {
result = handle_sample_def(d, remote_index, &signature);
} else if (kind == LABCOMM_SAMPLE_REF) {
result = handle_sample_ref(d, remote_index, &signature);
if (result == -ENOENT) {
/* Dummy value to silently continue */
result = LABCOMM_SAMPLE_REF;
switch (kind) {
case LABCOMM_SAMPLE_DEF: {
result = handle_sample_def(d, remote_index, &signature);
break;
}
case LABCOMM_SAMPLE_REF: {
result = handle_sample_ref(d, remote_index, &signature);
if (result == -ENOENT) {
/* Dummy value to silently continue */
result = LABCOMM_SAMPLE_REF;
}
break;
}
case LABCOMM_TYPE_DEF: {
int i;
printf("labcomm_decoder: ignoring TYPE_DEF 0x%x\n", remote_index);
for(i = 0; i < signature.size; i++){
printf("%x ",signature.signature[i]);
}
printf("\n");
result = LABCOMM_SAMPLE_DEF;
break;
}
} else {
result = -EINVAL;
default:
result = -EINVAL;
}
free_signature_signature:
labcomm_memory_free(d->memory, 1, signature.signature);
......@@ -249,18 +330,6 @@ static void reader_alloc(struct labcomm_decoder *d)
}
}
static int decoder_skip(struct labcomm_decoder *d, int len, int tag)
{
int i;
printf("got tag 0x%x, skipping %d bytes\n", tag, len);
for(i = 0; i <len; i++){
labcomm_read_byte(d->reader);
if (d->reader->error < 0) {
return d->reader->error;
}
}
return tag;
}
/* d - decoder to read from
registry - decoder to lookup signatures (registry != d only if
nesting decoders, e.g., when decoding pragma)
......@@ -375,9 +444,9 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
} else if (remote_index == LABCOMM_SAMPLE_REF) {
result = decode_sample_def_or_ref(d, LABCOMM_SAMPLE_REF);
} else if (remote_index == LABCOMM_TYPE_DEF) {
result = decoder_skip(d, length, remote_index);
result = decode_type_def(d, LABCOMM_TYPE_DEF);
} else if (remote_index == LABCOMM_TYPE_BINDING) {
result = decoder_skip(d, length, remote_index);
result = decode_type_binding(d, LABCOMM_TYPE_BINDING);
} else if (remote_index == LABCOMM_PRAGMA) {
result = decode_pragma(d, d, length);
} else if (remote_index < LABCOMM_USER) {
......
......@@ -240,6 +240,36 @@ static void do_write_signature(struct labcomm_encoder * e, const struct labcomm_
#endif
}
#ifdef TEST_MAP
static void sig_size(char b, const struct labcomm_signature *signature,
void *context)
{
int *result = context;
int diff;
if(signature) {
int idx = labcomm_get_local_index(signature);
diff = labcomm_size_packed32(idx);
//printf("== diff = %d, idx = 0x%d\n",diff, idx);
}else {
diff = 1;
//printf("== diff = %d, byte = 0x%d\n",diff, b);
}
(*result)+=diff;
}
#endif
static int calc_sig_encoded_size(struct labcomm_encoder *e,
const struct labcomm_signature *sig)
{
int result=0;
#ifdef TEST_MAP
map_signature(sig_size, &result, sig, FALSE);
#else
fprintf("warning! calc_sig_encoded_size not implemented without map...\n");
#endif
//printf("calc_sig_encoded_size: %s == %d\n",sig->name,result);
return result;
}
static int internal_reg_type(
struct labcomm_encoder *e,
const struct labcomm_signature *signature,
......@@ -263,6 +293,7 @@ static int internal_reg_type(
labcomm_write_string(e->writer, signature->name);
//XXX flush for debugging, can be removed when working
// labcomm_writer_flush(e->writer, e->writer->action_context);
labcomm_write_packed32(e->writer, calc_sig_encoded_size(e, signature));
do_write_signature(e, signature, FALSE);
// for (i = 0 ; i < signature->size ; i++) {
// if (e->writer->pos >= e->writer->count) {
......
......@@ -125,6 +125,8 @@ int test_decode_one(struct labcomm_decoder *decoder)
for (reader.count = 0 ; reader.count < writer.pos ; reader.count++) {
reader.error = 0;
reader.pos = 0;
labcomm_decoder_decode_one(decoder);
labcomm_decoder_decode_one(decoder);
result = labcomm_decoder_decode_one(decoder);
if (result >= 0 ) {
fprintf(stderr, "Got result from buffer with bogus length (%d)\n",
......
......@@ -199,6 +199,7 @@ int main(int argc, char **argv)
while (labcomm_decoder_decode_one(decoder) > 0) ;
printf("cache_s1.i = %d, s1.i = %d\n", cache_s1.i, s1.i);
assert(cache_s1.i == s1.i);
puts("S1 copied ok");
......
......@@ -9,4 +9,5 @@ all:
.PHONY: distclean
distclean:
rm *.o *.so *.so.1 *.a
for v in $(VERSIONS) ; do $(MAKE) -C $${v} distclean ; done
......@@ -10,15 +10,18 @@ ifeq ($(UNAME_S),Linux)
LDLIBS=-llabcomm$(LIBVERSION) -lrt
MAKESHARED=gcc -o $1 -shared -Wl,-soname,$2 $3 -lc -lrt
else ifeq ($(UNAME_S),Darwin)
CC=$(CROSS_COMPILE)clang
LD=$(CROSS_COMPILE)ld
#CC=$(CROSS_COMPILE)clang
#LD=$(CROSS_COMPILE)ld
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)gcc
CFLAGS=-g -Wall -Werror -O3 -I. -Itest \
-DLABCOMM_COMPAT=\"labcomm_compat_osx.h\" \
-DLABCOMM_OS_DARWIN=1\
-Wno-tautological-compare -Wno-unused-function
LDFLAGS=-L..
-DLABCOMM_OS_DARWIN=1
# -Wno-tautological-compare -Wno-unused-function
CFLAGS+=-std=c99
LDFLAGS=-L..
LDLIBS=-llabcomm$(LIBVERSION)
MAKESHARED=clang -o $1 -shared -Wl,-install_name,$2 $3 -lc
MAKESHARED=clang -o $1 -shared -Wl,-install_name,$2 $3 -lc
else ifneq ($(findstring CYGWIN,$(UNAME_S)),)
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld
......
......@@ -35,10 +35,18 @@ public class DecoderChannel implements Decoder {
}
private void processTypeDef(int len) throws IOException {
System.err.println("Got TypeDef: skipping "+len+" bytes");
for(int i=0; i<len; i++) {
decodeByte();
//System.err.println("Got TypeDef: skipping "+len+" bytes");
int idx = decodePacked32();
String name = decodeString();
System.err.println("Got TypeDef: "+idx+" "+name);
int siglen = decodePacked32();
System.err.println("siglen="+siglen);
for(int i=0; i<siglen; i++) {
byte b = decodeByte();
System.out.print(b);
System.out.print(" ");
}
System.out.println();
}
private void processTypeBinding(int len) throws IOException {
......
......@@ -15,18 +15,26 @@ public class EncoderChannel implements Encoder {
private EncoderRegistry type_def_registry = new EncoderRegistry();
private int current_tag;
public EncoderChannel(Writer writer) throws IOException {
private EncoderChannel(Writer writer, boolean emitVersion) throws IOException {
this.writer = writer;
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
if(emitVersion){
begin(Constant.VERSION);
encodeString(Constant.CURRENT_VERSION);
end(null);
}
}
public EncoderChannel(Writer writer) throws IOException {
this(writer, true);
}
public EncoderChannel(OutputStream writer) throws IOException {
this(new WriterWrapper(writer));
this(new WriterWrapper(writer), true);
}
private EncoderChannel(OutputStream writer, boolean emitVersion) throws IOException {
this(new WriterWrapper(writer), emitVersion);
}
private void bindType(int sampleId, int typeId) throws IOException {
begin(Constant.TYPE_BINDING);
encodePacked32(sampleId);
......@@ -50,6 +58,19 @@ public class EncoderChannel implements Encoder {
}
private static class WrappedEncoder extends EncoderChannel{
private Encoder wrapped;
public WrappedEncoder(Encoder e, OutputStream s, boolean emitVersion) throws IOException {
super(s,emitVersion);
this.wrapped = e;
}
public int getTypeId(Class<? extends SampleType> c) throws IOException{
return wrapped.getTypeId(c);
}
}
private int registerTypeDef(SampleDispatcher dispatcher) throws IOException {
//XXX A bit crude; maybe add boolean registry.contains(...) and check
// if already registered
......@@ -57,11 +78,24 @@ public class EncoderChannel implements Encoder {
return type_def_registry.getTag(dispatcher);
} catch (IOException e) {
int index = type_def_registry.add(dispatcher);
begin(dispatcher.getTypeDeclTag());
System.out.println("registered "+dispatcher.getName());
//
//wrap encoder to get encoded length of signature
ByteArrayOutputStream baos = new ByteArrayOutputStream();
EncoderChannel wrapped = new WrappedEncoder(this, baos, false);
dispatcher.encodeTypeDef(wrapped, index);
wrapped.flush();
byte b[] = baos.toByteArray();
begin(Constant.TYPE_DEF);
encodePacked32(index);
encodeString(dispatcher.getName());
dispatcher.encodeTypeDef(this, index);
encodePacked32(b.length);
for(int i = 0; i<b.length; i++) {
encodeByte(b[i]);
}
end(null);
return index;
}
}
......@@ -104,6 +138,12 @@ public class EncoderChannel implements Encoder {
begin(sample_def_registry.getTag(c));
}
/* temporary(?) fix to allow nesting encoders to find encoded size */
private void flush() throws IOException{
data.flush();
writer.write(bytes.toByteArray());
bytes.reset();
}
public void end(Class<? extends SampleType> c) throws IOException {
data.flush();
WritePacked32(writer, current_tag);
......@@ -112,8 +152,8 @@ public class EncoderChannel implements Encoder {
bytes.reset();
}
/**
* @return the id of a TYPE_DEF
/**
* @return the id of a TYPE_DEF
*/
public int getTypeId(Class<? extends SampleType> c) throws IOException {
return type_def_registry.getTag(c);
......
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