This also adds support for reading SourceFile class attribute Signed-off-by: Tomek Grabiec <tgrab...@gmail.com> --- Makefile | 1 + cafebabe/include/cafebabe/class.h | 1 + cafebabe/include/cafebabe/sourcefile_attribute.h | 51 +++++++++++++++++++ cafebabe/src/cafebabe/class.c | 32 ++++++++++++ cafebabe/src/cafebabe/sourcefile_attribute.c | 57 ++++++++++++++++++++++ 5 files changed, 142 insertions(+), 0 deletions(-) create mode 100644 cafebabe/include/cafebabe/sourcefile_attribute.h create mode 100644 cafebabe/src/cafebabe/sourcefile_attribute.c
diff --git a/Makefile b/Makefile index 44a7768..f1d42db 100644 --- a/Makefile +++ b/Makefile @@ -129,6 +129,7 @@ CAFEBABE_OBJS := \ error.o \ field_info.o \ method_info.o \ + sourcefile_attribute.o \ stream.o CAFEBABE_OBJS := $(addprefix cafebabe/src/cafebabe/,$(CAFEBABE_OBJS)) diff --git a/cafebabe/include/cafebabe/class.h b/cafebabe/include/cafebabe/class.h index b29d7a1..e7a9a96 100644 --- a/cafebabe/include/cafebabe/class.h +++ b/cafebabe/include/cafebabe/class.h @@ -97,5 +97,6 @@ int cafebabe_class_get_field(const struct cafebabe_class *c, int cafebabe_class_get_method(const struct cafebabe_class *c, const char *name, const char *descriptor, unsigned int *r); +char *cafebabe_class_get_source_file_name(const struct cafebabe_class *class); #endif diff --git a/cafebabe/include/cafebabe/sourcefile_attribute.h b/cafebabe/include/cafebabe/sourcefile_attribute.h new file mode 100644 index 0000000..540d91b --- /dev/null +++ b/cafebabe/include/cafebabe/sourcefile_attribute.h @@ -0,0 +1,51 @@ +/* + * cafebabe - the class loader library in C + * Copyright (C) 2009 Tomasz Grabiec <tgrab...@gmail.com> + * + * This file is released under the GPL version 2 with the following + * clarification and special exception: + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give you + * permission to link this library with independent modules to produce an + * executable, regardless of the license terms of these independent + * modules, and to copy and distribute the resulting executable under terms + * of your choice, provided that you also meet, for each linked independent + * module, the terms and conditions of the license of that module. An + * independent module is a module which is not derived from or based on + * this library. If you modify this library, you may extend this exception + * to your version of the library, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. + * + * Please refer to the file LICENSE for details. + */ + +#pragma once + +#ifndef CAFEBABE__SOURCEFILE_ATTRIBUTE_H +#define CAFEBABE__SOURCEFILE_ATTRIBUTE_H + +#include <stdint.h> + +#include "cafebabe/attribute_array.h" +#include "cafebabe/attribute_info.h" + +/** + * The Code attribute. + * + * See also section 4.7.7 of The Java Virtual Machine Specification. + */ +struct cafebabe_sourcefile_attribute { + uint16_t sourcefile_index; +}; + +int cafebabe_sourcefile_attribute_init(struct cafebabe_sourcefile_attribute *a, + struct cafebabe_stream *s); +void cafebabe_sourcefile_attribute_deinit(struct cafebabe_sourcefile_attribute *a); + +#endif diff --git a/cafebabe/src/cafebabe/class.c b/cafebabe/src/cafebabe/class.c index eac7cea..c43fefe 100644 --- a/cafebabe/src/cafebabe/class.c +++ b/cafebabe/src/cafebabe/class.c @@ -27,6 +27,7 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "cafebabe/attribute_array.h" #include "cafebabe/attribute_info.h" @@ -35,6 +36,7 @@ #include "cafebabe/error.h" #include "cafebabe/field_info.h" #include "cafebabe/method_info.h" +#include "cafebabe/sourcefile_attribute.h" #include "cafebabe/stream.h" int @@ -366,3 +368,33 @@ cafebabe_class_get_method(const struct cafebabe_class *c, /* Not found */ return 1; } + +char *cafebabe_class_get_source_file_name(const struct cafebabe_class *class) +{ + unsigned int sourcefile_attrib_index = 0; + if (cafebabe_attribute_array_get(&class->attributes, "SourceFile", + class, &sourcefile_attrib_index)) + return NULL; + + const struct cafebabe_attribute_info *attribute + = &class->attributes.array[sourcefile_attrib_index]; + + struct cafebabe_stream stream; + cafebabe_stream_open_buffer(&stream, + attribute->info, attribute->attribute_length); + + struct cafebabe_sourcefile_attribute sourcefile_attribute; + if (cafebabe_sourcefile_attribute_init(&sourcefile_attribute, &stream)) + return NULL; + + cafebabe_stream_close_buffer(&stream); + + const struct cafebabe_constant_info_utf8 *file_name; + if (cafebabe_class_constant_get_utf8(class, + sourcefile_attribute.sourcefile_index, &file_name)) + { + return NULL; + } + + return strndup((char*)file_name->bytes, file_name->length); +} diff --git a/cafebabe/src/cafebabe/sourcefile_attribute.c b/cafebabe/src/cafebabe/sourcefile_attribute.c new file mode 100644 index 0000000..6185a72 --- /dev/null +++ b/cafebabe/src/cafebabe/sourcefile_attribute.c @@ -0,0 +1,57 @@ +/* + * cafebabe - the class loader library in C + * Copyright (C) 2009 Tomasz Grabiec <tgrab...@gmail.com> + * + * This file is released under the GPL version 2 with the following + * clarification and special exception: + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give you + * permission to link this library with independent modules to produce an + * executable, regardless of the license terms of these independent + * modules, and to copy and distribute the resulting executable under terms + * of your choice, provided that you also meet, for each linked independent + * module, the terms and conditions of the license of that module. An + * independent module is a module which is not derived from or based on + * this library. If you modify this library, you may extend this exception + * to your version of the library, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. + * + * Please refer to the file LICENSE for details. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "cafebabe/attribute_info.h" +#include "cafebabe/sourcefile_attribute.h" +#include "cafebabe/stream.h" + +int +cafebabe_sourcefile_attribute_init(struct cafebabe_sourcefile_attribute *a, + struct cafebabe_stream *s) +{ + if (cafebabe_stream_read_uint16(s, &a->sourcefile_index)) + goto out; + + if (!cafebabe_stream_eof(s)) { + s->cafebabe_errno = CAFEBABE_ERROR_EXPECTED_EOF; + goto out; + } + + /* Success */ + return 0; + +out: + return 1; +} + +void +cafebabe_sourcefile_attribute_deinit(struct cafebabe_sourcefile_attribute *a) +{ +} -- 1.6.0.6 ------------------------------------------------------------------------------ _______________________________________________ Jatovm-devel mailing list Jatovm-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jatovm-devel