aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPekka Enberg <penberg@kernel.org>2013-03-10 17:09:36 +0000
committerPekka Enberg <penberg@kernel.org>2013-03-10 17:11:09 +0000
commit043b91f622fb1ce357fe3de9314626c696f44dff (patch)
tree2f0107e4cc90b6adbcaad4f71dc988cdefad231d
parent4c43f40e263abd54e5e3523ed8f463c820e0eef5 (diff)
downloadjato-043b91f622fb1ce357fe3de9314626c696f44dff.tar.gz
vm/jar: Clean up code
Reading vm/jar.c can feel like pulling teeth at times... Signed-off-by: Pekka Enberg <penberg@kernel.org>
-rw-r--r--vm/jar.c103
1 files changed, 71 insertions, 32 deletions
diff --git a/vm/jar.c b/vm/jar.c
index 3e8652c1..e5947184 100644
--- a/vm/jar.c
+++ b/vm/jar.c
@@ -14,12 +14,15 @@
#include "vm/jar.h"
struct parse_buffer {
- const char *data;
- unsigned int i;
+ const char *data;
+ unsigned int i;
};
-/* The following is an implementation of the Jar file format as specified in:
- * http://download.oracle.com/javase/1.3/docs/guide/jar/jar.html */
+/*
+ * The following is an implementation of the Jar file format as specified in:
+ *
+ * http://download.oracle.com/javase/1.3/docs/guide/jar/jar.html
+ */
static struct jar_header *jar_header_alloc(void)
{
@@ -49,10 +52,13 @@ static bool parse_alphanum(struct parse_buffer *b, char *alphanum_result)
static bool parse_headerchar(struct parse_buffer *b, char *headerchar_result)
{
+ char ch;
+
if (parse_alphanum(b, headerchar_result))
return true;
- char ch = b->data[b->i];
+ ch = b->data[b->i];
+
if (ch == '-' || ch == '_') {
*headerchar_result = ch;
++b->i;
@@ -91,20 +97,23 @@ static bool parse_otherchar(struct parse_buffer *b, char *otherchar_result)
*otherchar_result = c;
++b->i;
+
return true;
}
static bool parse_continuation(struct parse_buffer *b,
char **continuation_result)
{
+ struct string *continuation;
+ char otherchar;
+
if (b->data[b->i] != ' ')
return false;
++b->i;
- struct string *continuation = alloc_str();
+ continuation = alloc_str();
- char otherchar;
while (parse_otherchar(b, &otherchar))
str_append(continuation, "%c", otherchar);
@@ -112,66 +121,81 @@ static bool parse_continuation(struct parse_buffer *b,
goto out_free_continuation;
*continuation_result = strdup(continuation->value);
+
free_str(continuation);
+
return true;
out_free_continuation:
free_str(continuation);
+
return false;
}
static bool parse_value(struct parse_buffer *b, char **value_result)
{
+ struct string *value;
+ char *continuation;
+ char otherchar;
+
if (b->data[b->i] != ' ')
return false;
++b->i;
- struct string *value = alloc_str();
+ value = alloc_str();
- char otherchar;
while (parse_otherchar(b, &otherchar))
str_append(value, "%c", otherchar);
if (!parse_newline(b))
goto out_free_value;
- char *continuation;
while (parse_continuation(b, &continuation))
str_append(value, "%s", continuation);
*value_result = strdup(value->value);
+
free_str(value);
+
return true;
out_free_value:
free_str(value);
+
return false;
}
static bool parse_name(struct parse_buffer *b, char **name_result)
{
+ struct string *name;
+ char headerchar;
char alphanum;
+
if (!parse_alphanum(b, &alphanum))
return false;
- struct string *name = alloc_str();
+ name = alloc_str();
str_append(name, "%c", alphanum);
- char headerchar;
while (parse_headerchar(b, &headerchar))
str_append(name, "%c", headerchar);
*name_result = strdup(name->value);
+
free_str(name);
+
return true;
}
static bool parse_header(struct parse_buffer *b,
struct jar_header **header_result)
{
+ struct jar_header *header;
+ char *value;
char *name;
+
if (!parse_name(b, &name))
return false;
@@ -180,19 +204,21 @@ static bool parse_header(struct parse_buffer *b,
++b->i;
- char *value;
if (!parse_value(b, &value))
goto out_free_name;
- struct jar_header *header = jar_header_alloc();
- header->name = name;
- header->value = value;
+ header = jar_header_alloc();
+
+ header->name = name;
+ header->value = value;
+
*header_result = header;
return true;
out_free_name:
free(name);
+
return false;
}
@@ -210,9 +236,10 @@ static struct jar_manifest *jar_manifest_alloc(void)
static void jar_manifest_free(struct jar_manifest *jm)
{
+ struct jar_individual_section *section, *tmp_section;
+
free(jm->main_section);
- struct jar_individual_section *section, *tmp_section;
list_for_each_entry_safe(section, tmp_section,
&jm->individual_sections, node) {
free(section);
@@ -247,9 +274,10 @@ static struct jar_individual_section *jar_individual_section_alloc(void)
static void jar_individual_section_free(struct jar_individual_section *jis)
{
+ struct jar_header *header, *tmp_header;
+
free(jis->name_header);
- struct jar_header *header, *tmp_header;
list_for_each_entry_safe(header, tmp_header,
&jis->perentry_attributes, node) {
free(header);
@@ -261,8 +289,10 @@ static void jar_individual_section_free(struct jar_individual_section *jis)
static struct jar_individual_section *
parse_individual_section(struct parse_buffer *b)
{
- struct jar_individual_section *individual_section
- = jar_individual_section_alloc();
+ struct jar_individual_section *individual_section;
+ struct jar_header *perentry_attribute;
+
+ individual_section = jar_individual_section_alloc();
if (!parse_header(b, &individual_section->name_header))
goto out_free_individual_section;
@@ -270,7 +300,6 @@ parse_individual_section(struct parse_buffer *b)
if (strcmp(individual_section->name_header->name, "Name"))
goto out_free_individual_section;
- struct jar_header *perentry_attribute;
while (parse_header(b, &perentry_attribute)) {
list_add_tail(&perentry_attribute->node,
&individual_section->perentry_attributes);
@@ -300,15 +329,16 @@ static bool parse_version_number(struct parse_buffer *b,
unsigned int *version_number_result)
{
unsigned int version_number = 0;
+ unsigned int digit;
if (!parse_digit(b, &version_number))
return false;
- unsigned int digit;
while (parse_digit(b, &digit))
version_number = 10 * version_number + digit;
*version_number_result = version_number;
+
return true;
}
@@ -317,15 +347,16 @@ static bool parse_version_info(struct parse_buffer *b,
unsigned int *minor_version_result)
{
struct jar_header *header;
+ struct parse_buffer pb;
+
if (!parse_header(b, &header))
return false;
if (strcmp(header->name, "Manifest-Version"))
goto out_free_header;
- struct parse_buffer pb;
- pb.data = header->value;
- pb.i = 0;
+ pb.data = header->value;
+ pb.i = 0;
if (!parse_version_number(&pb, major_version_result))
goto out_free_header;
@@ -343,10 +374,12 @@ static bool parse_version_info(struct parse_buffer *b,
goto out_free_header;
jar_header_free(header);
+
return true;
out_free_header:
jar_header_free(header);
+
return false;
}
@@ -354,6 +387,7 @@ static bool parse_main_section(struct parse_buffer *b,
struct jar_main_section **main_section_result)
{
struct jar_main_section *main_section = jar_main_section_alloc();
+ struct jar_header *attribute;
if (!parse_version_info(b,
&main_section->major_version,
@@ -368,15 +402,16 @@ static bool parse_main_section(struct parse_buffer *b,
goto out_free_main_section;
#endif
- struct jar_header *attribute;
while (parse_header(b, &attribute))
list_add_tail(&attribute->node, &main_section->main_attributes);
*main_section_result = main_section;
+
return true;
out_free_main_section:
jar_main_section_free(main_section);
+
return false;
}
@@ -384,6 +419,7 @@ static bool parse_manifest_file(struct parse_buffer *b,
struct jar_manifest **manifest_result)
{
struct jar_manifest *manifest = jar_manifest_alloc();
+ struct jar_individual_section *section;
if (!parse_main_section(b, &manifest->main_section))
goto out_free_manifest;
@@ -391,16 +427,17 @@ static bool parse_manifest_file(struct parse_buffer *b,
if (!parse_newline(b))
goto out_free_manifest;
- struct jar_individual_section *section;
while ((section = parse_individual_section(b)) != NULL) {
list_add_tail(&section->node, &manifest->individual_sections);
}
*manifest_result = manifest;
+
return true;
out_free_manifest:
jar_manifest_free(manifest);
+
return false;
}
@@ -408,15 +445,15 @@ static struct jar_manifest *read_manifest(struct zip *zip)
{
struct zip_entry *zip_entry;
struct parse_buffer pb;
+ struct jar_manifest *manifest;
zip_entry = zip_entry_find(zip, "META-INF/MANIFEST.MF");
if (!zip_entry)
return NULL;
- pb.data = zip_entry_data(zip, zip_entry);
- pb.i = 0;
+ pb.data = zip_entry_data(zip, zip_entry);
+ pb.i = 0;
- struct jar_manifest *manifest;
if (!parse_manifest_file(&pb, &manifest)) {
NOT_IMPLEMENTED;
printf("parse error, byte offset %d. oops\n", pb.i);
@@ -455,6 +492,9 @@ error_out:
const char *vm_jar_get_main_class(const struct vm_jar *jar)
{
+ struct jar_main_section *section;
+ struct jar_header *header;
+
if (!jar)
return NULL;
@@ -464,9 +504,8 @@ const char *vm_jar_get_main_class(const struct vm_jar *jar)
if (!jar->manifest->main_section)
return NULL;
- struct jar_main_section *section = jar->manifest->main_section;
+ section = jar->manifest->main_section;
- struct jar_header *header;
list_for_each_entry(header, &section->main_attributes, node) {
if (!strcmp(header->name, "Main-Class"))
return header->value;