aboutsummaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-09-07 00:04:38 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-10-05 08:17:21 +0200
commit144b7fe1415ff97497fa4ea3b95e8dd6b95d069e (patch)
tree68391f6ec2dba2879ca8255ee25ce39ebaa7d4ad /src/io
First commit1st
Diffstat (limited to 'src/io')
-rw-r--r--src/io/CMakeLists.txt18
-rw-r--r--src/io/leb128.c47
-rw-r--r--src/io/read.c29
-rw-r--r--src/io/varint1.c27
-rw-r--r--src/io/varint64.c27
-rw-r--r--src/io/varint7.c26
-rw-r--r--src/io/varuint1.c27
-rw-r--r--src/io/varuint32.c27
-rw-r--r--src/io/varuint64.c27
-rw-r--r--src/io/varuint7.c27
10 files changed, 282 insertions, 0 deletions
diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt
new file mode 100644
index 0000000..a9f97ca
--- /dev/null
+++ b/src/io/CMakeLists.txt
@@ -0,0 +1,18 @@
+# nanowasm, a tiny WebAssembly/Wasm interpreter
+# Copyright (C) 2023-2024 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
+ read.c
+ varint1.c
+ varint7.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..a57d5c7
--- /dev/null
+++ b/src/io/leb128.c
@@ -0,0 +1,47 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <nw/log.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+enum nw_state nwp_leb128(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, const unsigned maxbits, const bool sign)
+{
+ uint8_t byte;
+
+ for (;;)
+ {
+ const int n = cfg->read(&byte, sizeof byte, cfg->user);
+
+ if (n < 0)
+ return NW_FATAL;
+ else if (!n)
+ return NW_AGAIN;
+
+ l->result |= (unsigned long long)(byte & 0x7f) << l->shift;
+ l->shift += 7;
+
+ if (!(byte & 0x80))
+ break;
+ else if (++l->bcnt > (maxbits + 7u - 1u) / 7u)
+ {
+ LOG("%s: overflow\n", __func__);
+ return NW_FATAL;
+ }
+ }
+
+ if (sign && (l->shift < maxbits) && (byte & 0x40))
+ l->result |= -1ll << l->shift;
+
+ return NW_OK;
+}
diff --git a/src/io/read.c b/src/io/read.c
new file mode 100644
index 0000000..2d64257
--- /dev/null
+++ b/src/io/read.c
@@ -0,0 +1,29 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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>
+#include <stdint.h>
+
+enum nw_state nwp_io_read(const struct nw_io_cfg *const cfg,
+ struct nw_sm_io *const io)
+{
+ void *const buf = (uint8_t *)io->buf + io->read;
+ const size_t rem = io->n - io->read;
+ const int n = cfg->read(buf, rem, cfg->user);
+
+ if (n < 0)
+ return NW_FATAL;
+
+ if ((io->read += n) >= io->n)
+ return NW_OK;
+
+ return NW_AGAIN;
+}
diff --git a/src/io/varint1.c b/src/io/varint1.c
new file mode 100644
index 0000000..74a577f
--- /dev/null
+++ b/src/io/varint1.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint1(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varint1 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 1, true);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varint64.c b/src/io/varint64.c
new file mode 100644
index 0000000..f0faa5b
--- /dev/null
+++ b/src/io/varint64.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint64(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint32 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 64, true);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varint7.c b/src/io/varint7.c
new file mode 100644
index 0000000..77b9006
--- /dev/null
+++ b/src/io/varint7.c
@@ -0,0 +1,26 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varint7(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varint7 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 7, true);
+
+ if (ret)
+ return ret;
+
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ return ret;
+}
diff --git a/src/io/varuint1.c b/src/io/varuint1.c
new file mode 100644
index 0000000..3760f9d
--- /dev/null
+++ b/src/io/varuint1.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint1(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint1 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 1, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint32.c b/src/io/varuint32.c
new file mode 100644
index 0000000..40e2030
--- /dev/null
+++ b/src/io/varuint32.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint32(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint32 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 32, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint64.c b/src/io/varuint64.c
new file mode 100644
index 0000000..e01ce69
--- /dev/null
+++ b/src/io/varuint64.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint64(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint64 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 64, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}
diff --git a/src/io/varuint7.c b/src/io/varuint7.c
new file mode 100644
index 0000000..b20c00c
--- /dev/null
+++ b/src/io/varuint7.c
@@ -0,0 +1,27 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2024 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 <nanowasm/leb128.h>
+#include <nw/io.h>
+#include <stdbool.h>
+
+enum nw_state nwp_varuint7(const struct nw_io_cfg *const cfg,
+ struct nw_sm_leb128 *const l, nw_varuint7 *const out)
+{
+ const enum nw_state ret = nwp_leb128(cfg, l, 7, false);
+
+ if (!ret)
+ {
+ *out = l->result;
+ *l = (const struct nw_sm_leb128){0};
+ }
+
+ return ret;
+}