Skip to content

Commit 877ca9a

Browse files
committed
wasmtime: Add helper for wasm header interpretation
Signed-off-by: Maximilian Hüter <[email protected]>
1 parent cf36a28 commit 877ca9a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/libcrun/handlers/handler-utils.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define _GNU_SOURCE
2020

2121
#include <config.h>
22+
#include <string.h>
2223
#include "../container.h"
2324
#include "../utils.h"
2425
#include "handler-utils.h"
@@ -69,3 +70,20 @@ wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err
6970

7071
return 0;
7172
}
73+
74+
wasm_encoding_t
75+
wasm_interpete_header (const char *header)
76+
{
77+
// Check for the WebAssembly magic bytes
78+
if (strncmp (header, "\0asm", 4))
79+
return WASM_ENC_INVALID;
80+
81+
// We don't care for the specific WebAssembly version
82+
// so we only read the value of the `layer` field.
83+
if (header[6] == '\0' && header[7] == '\0')
84+
return WASM_ENC_MODULE;
85+
86+
// `layer` does not equal 0x00 0x00 so we are working
87+
// with a component.
88+
return WASM_ENC_COMPONENT;
89+
}

src/libcrun/handlers/handler-utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
#include "../container.h"
2222
#include <unistd.h>
2323

24+
typedef enum
25+
{
26+
WASM_ENC_INVALID,
27+
WASM_ENC_MODULE,
28+
WASM_ENC_COMPONENT
29+
} wasm_encoding_t;
30+
2431
int wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err);
2532

33+
wasm_encoding_t wasm_interpete_header (const char *header);
34+
2635
#endif

0 commit comments

Comments
 (0)