summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCSDUMMI <csdummi.misquality@simplelogin.co>2025-05-10 19:47:03 +0200
committerCSDUMMI <csdummi.misquality@simplelogin.co>2025-05-10 19:47:03 +0200
commit64f9409f9c46a72d0cc804683db06d02c8ea96fa (patch)
tree786c3cc51af8cd2d5fe9265d52aa91aed934d0a3
parent2b3c39d18c4c751d36a0904f00efbf015fda7d4b (diff)
Add parsing of export table
-rw-r--r--wai.c41
1 files changed, 32 insertions, 9 deletions
diff --git a/wai.c b/wai.c
index 28bad13..aac65ca 100644
--- a/wai.c
+++ b/wai.c
@@ -28,6 +28,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
+#include <inttypes.h>
enum section {
Section_Custom,
@@ -51,10 +52,24 @@ struct stack {
size_t count;
};
+#define MAX_FUNCTIONS 128
+enum export_desc {
+ Export_Func,
+ Export_Table,
+ Export_Mem,
+ Export_Global,
+};
+
+struct export_t {
+ u_char name[128];
+ size_t name_length;
+ uint32_t index;
+ enum export_desc description;
+};
struct module {
struct type_t *types;
- u_char *funcs[128];
+ u_char *funcs[MAX_FUNCTIONS];
struct table_t *tables;
struct mem_t *mems;
struct global_t *globals;
@@ -62,7 +77,7 @@ struct module {
struct data_t *datas;
struct start_t *start;
struct import_t *imports;
- struct export_t *exports;
+ struct export_t exports[MAX_FUNCTIONS];
u_char *binary;
struct stack stack;
int scope;
@@ -271,19 +286,27 @@ int parse_section(struct module *module, u_char *binary, int len) {
case Section_Export:
printf("section: exports\n");
int num_exports = binary[i];
+
+ if(num_exports > MAX_FUNCTIONS) {
+ printf("Number of exports exceeds maximum number of functions in a module (%d)", MAX_FUNCTIONS);
+ return -1;
+ }
+
incr(i, len);
for (int exports_i = 0; exports_i < num_exports; exports_i++) {
- int string_len = binary[i];
+ struct export_t *export = &module->exports[i];
+
+ export->name_length = binary[i];
incr(i, len);
- printf("export name: ");
- for (int si = 0; si < string_len; si++) {
- putchar(binary[i]);
+
+ for (int si = 0; si < export->name_length; si++) {
+ export->name[si] = binary[i];
incr(i, len);
}
- putchar('\n');
- // export kind
+ export->description = (int) binary[i];
incr(i, len);
- // export func index
+ export->index = (uint32_t) binary[i];
+ printf("export name: %s of type %d\n", export->name, export->description);
incr(i, len);
}
break;