diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-09-07 00:04:38 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-06 14:38:40 +0100 |
| commit | 6d9d80362f9932bbc87e162b8ef7df06c73e27e1 (patch) | |
| tree | e3e228c63fe26f07503f226de7fb5086b3dc2286 /src/io | |
| download | nanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz | |
First commit
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/CMakeLists.txt | 23 | ||||
| -rw-r--r-- | src/io/leb128.c | 75 | ||||
| -rw-r--r-- | src/io/leuint32.c | 17 | ||||
| -rw-r--r-- | src/io/leuint64.c | 24 | ||||
| -rw-r--r-- | src/io/read.c | 31 | ||||
| -rw-r--r-- | src/io/toleuint32.c | 19 | ||||
| -rw-r--r-- | src/io/toleuint64.c | 23 | ||||
| -rw-r--r-- | src/io/varint1.c | 27 | ||||
| -rw-r--r-- | src/io/varint32.c | 27 | ||||
| -rw-r--r-- | src/io/varint64.c | 28 | ||||
| -rw-r--r-- | src/io/varint7.c | 27 | ||||
| -rw-r--r-- | src/io/varuint1.c | 27 | ||||
| -rw-r--r-- | src/io/varuint32.c | 27 | ||||
| -rw-r--r-- | src/io/varuint64.c | 27 | ||||
| -rw-r--r-- | src/io/varuint7.c | 27 |
15 files changed, 429 insertions, 0 deletions
diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt new file mode 100644 index 0000000..b7c19cb --- /dev/null +++ b/src/io/CMakeLists.txt @@ -0,0 +1,23 @@ +# nanowasm, a tiny WebAssembly/Wasm interpreter +# Copyright (C) 2023-2025 Xavier Del Campo Romero +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +target_sources(${PROJECT_NAME} PRIVATE + leb128.c + leuint32.c + leuint64.c + read.c + toleuint32.c + toleuint64.c + varint1.c + varint7.c + varint32.c + varint64.c + varuint1.c + varuint7.c + varuint32.c + varuint64.c +) diff --git a/src/io/leb128.c b/src/io/leb128.c new file mode 100644 index 0000000..efb599b --- /dev/null +++ b/src/io/leb128.c @@ -0,0 +1,75 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + + +/* The functions below is a (somewhat heavily) modified version of that + * provided by the wac project: https://github.com/kanaka/wac + * + * Copyright (C) Joel Martin <github@martintribe.org> + * The wac project is licensed under the MPL 2.0 (Mozilla Public License + * 2.0). The text of the MPL 2.0 license is included below and can be + * found at https://www.mozilla.org/MPL/2.0/ + */ + +#include <nanowasm/types.h> +#include <nw/io.h> +#include <nw/log.h> + +enum nw_state nwp_leb128(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, const unsigned maxbits, const int sign, + void *const user) +{ + unsigned char byte; + + for (;;) + { + const int n = cfg->read(&byte, sizeof byte, user); + unsigned long v; + + if (n < 0) + return NW_FATAL; + else if (!n) + return NW_AGAIN; + + v = (unsigned long)(byte & 0x7f) << l->shift;; + + if (l->shift < 32) + l->result.low |= v; + else + l->result.hi |= v; + + l->shift += 7; + + if (!(byte & 0x80)) + break; + else if (++l->bcnt > (maxbits + 7u - 1u) / 7u) + { +#ifdef NW_LOG + long offset; + const enum nw_state n = cfg->tell(&offset, cfg->user); + + if (n) + return n; + + nwp_log("leb128 overflow, offset=%#lx\n", (unsigned long)offset); +#endif + return NW_FATAL; + } + } + + if (sign && (l->shift < maxbits) && (byte & 0x40)) + { + if (l->shift < 32) + l->result.low |= -1l << (l->shift); + else + l->result.hi |= -1l << (l->shift); + } + + return NW_OK; +} diff --git a/src/io/leuint32.c b/src/io/leuint32.c new file mode 100644 index 0000000..41a2e3b --- /dev/null +++ b/src/io/leuint32.c @@ -0,0 +1,17 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/io.h> + +unsigned long nwp_leuint32(const struct nw_leuint32 *const v) +{ + return v->v[0] | (v->v[1] << 8) | ((unsigned long)v->v[2] << 16) + | ((unsigned long)v->v[3] << 24); +} diff --git a/src/io/leuint64.c b/src/io/leuint64.c new file mode 100644 index 0000000..6d3deb0 --- /dev/null +++ b/src/io/leuint64.c @@ -0,0 +1,24 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/io.h> + +void nwp_leuint64(const struct nw_leuint64 *const v, + struct nw_ull *const ull) +{ + ull->low = v->v[0] + | (v->v[1] << 8) + | ((unsigned long)v->v[2] << 16ul) + | ((unsigned long)v->v[3] << 24ul); + ull->hi = v->v[4] + | (v->v[5] << 8) + | ((unsigned long) v->v[6] << 16ul) + | ((unsigned long)v->v[7] << 24ul); +} diff --git a/src/io/read.c b/src/io/read.c new file mode 100644 index 0000000..d4222b5 --- /dev/null +++ b/src/io/read.c @@ -0,0 +1,31 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/io.h> +#include <stddef.h> + +enum nw_state nwp_io_read(const struct nw_io_cfg *const cfg, + struct nw_sm_io *const io, void *const user) +{ + void *const buf = (unsigned char *)io->buf + io->read; + const size_t rem = io->n - io->read; + const int n = cfg->read(buf, rem, user); + + if (n < 0 || (!n && cfg->eof(user))) + return NW_FATAL; + + if ((io->read += n) >= io->n) + { + io->read = 0; + return NW_OK; + } + + return NW_AGAIN; +} diff --git a/src/io/toleuint32.c b/src/io/toleuint32.c new file mode 100644 index 0000000..c1c96c7 --- /dev/null +++ b/src/io/toleuint32.c @@ -0,0 +1,19 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/io.h> + +void nwp_toleuint32(const unsigned long v, struct nw_leuint32 *const r) +{ + r->v[0] = v; + r->v[1] = v >> 8; + r->v[2] = v >> 16; + r->v[3] = v >> 24; +} diff --git a/src/io/toleuint64.c b/src/io/toleuint64.c new file mode 100644 index 0000000..57856c2 --- /dev/null +++ b/src/io/toleuint64.c @@ -0,0 +1,23 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/nw.h> +#include <nw/io.h> + +void nwp_toleuint64(const struct nw_ull *const v, struct nw_leuint64 *const r) +{ + r->v[0] = v->low; + r->v[1] = v->low >> 8; + r->v[2] = v->low >> 16; + r->v[3] = v->low >> 24; + r->v[4] = v->hi; + r->v[5] = v->hi >> 8; + r->v[6] = v->hi >> 16; + r->v[7] = v->hi >> 24; +} diff --git a/src/io/varint1.c b/src/io/varint1.c new file mode 100644 index 0000000..502765b --- /dev/null +++ b/src/io/varint1.c @@ -0,0 +1,27 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varint1(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varint1 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 1, 1, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + *out = l->result.low; + *l = ll; + } + + return ret; +} diff --git a/src/io/varint32.c b/src/io/varint32.c new file mode 100644 index 0000000..f1c58cd --- /dev/null +++ b/src/io/varint32.c @@ -0,0 +1,27 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varint32(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varint32 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 32, 1, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + *out = l->result.low; + *l = ll; + } + + return ret; +} diff --git a/src/io/varint64.c b/src/io/varint64.c new file mode 100644 index 0000000..5d0a3fd --- /dev/null +++ b/src/io/varint64.c @@ -0,0 +1,28 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varint64(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varint64 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 64, 1, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + out->low = l->result.low; + out->hi = l->result.hi; + *l = ll; + } + + return ret; +} diff --git a/src/io/varint7.c b/src/io/varint7.c new file mode 100644 index 0000000..d22d25c --- /dev/null +++ b/src/io/varint7.c @@ -0,0 +1,27 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varint7(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varint7 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 7, 1, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + *out = l->result.low; + *l = ll; + } + + return ret; +} diff --git a/src/io/varuint1.c b/src/io/varuint1.c new file mode 100644 index 0000000..2f9587c --- /dev/null +++ b/src/io/varuint1.c @@ -0,0 +1,27 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varuint1(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varuint1 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 1, 0, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + *out = l->result.low; + *l = ll; + } + + return ret; +} diff --git a/src/io/varuint32.c b/src/io/varuint32.c new file mode 100644 index 0000000..1ad2c35 --- /dev/null +++ b/src/io/varuint32.c @@ -0,0 +1,27 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varuint32(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varuint32 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 32, 0, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + *out = l->result.low; + *l = ll; + } + + return ret; +} diff --git a/src/io/varuint64.c b/src/io/varuint64.c new file mode 100644 index 0000000..5538bb8 --- /dev/null +++ b/src/io/varuint64.c @@ -0,0 +1,27 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varuint64(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varuint64 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 64, 0, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + *out = l->result; + *l = ll; + } + + return ret; +} diff --git a/src/io/varuint7.c b/src/io/varuint7.c new file mode 100644 index 0000000..e3c2e91 --- /dev/null +++ b/src/io/varuint7.c @@ -0,0 +1,27 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include <nanowasm/types.h> +#include <nw/io.h> + +enum nw_state nwp_varuint7(const struct nw_io_cfg *const cfg, + struct nw_sm_leb128 *const l, nw_varuint7 *const out, void *const user) +{ + const enum nw_state ret = nwp_leb128(cfg, l, 7, 0, user); + + if (!ret) + { + static const struct nw_sm_leb128 ll; + + *out = l->result.low; + *l = ll; + } + + return ret; +} |
