@deroll/cmio 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/AUTHORS +1 -0
  2. package/LICENSE +202 -0
  3. package/README.md +122 -0
  4. package/binding.gyp +53 -0
  5. package/deps/machine-guest-tools/AUTHORS +7 -0
  6. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/abi.h +589 -0
  7. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/buf.h +82 -0
  8. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/io.h +168 -0
  9. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/keccak.h +131 -0
  10. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/merkle.h +118 -0
  11. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/rollup.h +260 -0
  12. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/util.h +34 -0
  13. package/deps/machine-guest-tools/sys-utils/libcmt/src/abi.c +315 -0
  14. package/deps/machine-guest-tools/sys-utils/libcmt/src/buf.c +121 -0
  15. package/deps/machine-guest-tools/sys-utils/libcmt/src/io-mock.c +319 -0
  16. package/deps/machine-guest-tools/sys-utils/libcmt/src/io.c +164 -0
  17. package/deps/machine-guest-tools/sys-utils/libcmt/src/keccak.c +156 -0
  18. package/deps/machine-guest-tools/sys-utils/libcmt/src/merkle.c +202 -0
  19. package/deps/machine-guest-tools/sys-utils/libcmt/src/rollup.c +455 -0
  20. package/deps/machine-guest-tools/sys-utils/libcmt/src/util.c +52 -0
  21. package/lib/index.d.mts +18 -0
  22. package/lib/index.d.ts +162 -0
  23. package/lib/index.js +230 -0
  24. package/lib/index.mjs +19 -0
  25. package/package.json +70 -0
  26. package/prebuilds/darwin-arm64/@deroll+cmio.node +0 -0
  27. package/src/addon.cc +411 -0
@@ -0,0 +1,34 @@
1
+ #ifndef CMT_UTIL_H
2
+ #define CMT_UTIL_H
3
+ #include <stdbool.h>
4
+
5
+ /**
6
+ */
7
+ bool cmt_util_debug_enabled(void);
8
+
9
+ /** Read whole file `name` contents into `data` and set `length`.
10
+ * @param name[in] - file path
11
+ * @param max[in] - size of `data` in bytes
12
+ * @param data[out] - file contents
13
+ * @param length[out] - actual size in `bytes` written to `data`
14
+ *
15
+ * @return
16
+ * | | |
17
+ * |-----|--------------------|
18
+ * | 0 |success |
19
+ * | < 0 |negative errno value| */
20
+ int cmt_util_read_whole_file(const char *name, size_t max, void *data, size_t *length);
21
+
22
+ /** Write the contents of `data` into file `name`.
23
+ * @param name[in] - file path
24
+ * @param length[in] - size of `data` in bytes
25
+ * @param data[out] - file contents
26
+ *
27
+ * @return
28
+ * | | |
29
+ * |-----|--------------------|
30
+ * | 0 |success |
31
+ * | < 0 |negative errno value| */
32
+ int cmt_util_write_whole_file(const char *name, size_t length, const void *data);
33
+
34
+ #endif /* CMT_UTIL_H */
@@ -0,0 +1,315 @@
1
+ /* Copyright Cartesi and individual authors (see AUTHORS)
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ #include "libcmt/abi.h"
17
+
18
+ #include <errno.h>
19
+ #include <string.h>
20
+
21
+ static uintptr_t align_forward(uintptr_t p, size_t a) {
22
+ return (p + (a - 1)) & ~(a - 1);
23
+ }
24
+
25
+ uint32_t cmt_abi_funsel(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
26
+ return CMT_ABI_FUNSEL(a, b, c, d);
27
+ }
28
+
29
+ int cmt_abi_mark_frame(const cmt_buf_t *me, cmt_buf_t *frame) {
30
+ if (!me || !frame) {
31
+ return -EINVAL;
32
+ }
33
+ *frame = *me;
34
+ return 0;
35
+ }
36
+
37
+ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel) {
38
+ cmt_buf_t x[1];
39
+ int rc = cmt_buf_split(me, sizeof(funsel), x, me);
40
+ if (rc) {
41
+ return rc;
42
+ }
43
+ // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
44
+ memcpy(x->begin, &funsel, sizeof(funsel));
45
+ return 0;
46
+ }
47
+
48
+ int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]) {
49
+ if (n > CMT_ABI_U256_LENGTH) {
50
+ return -EDOM;
51
+ }
52
+ for (size_t i = 0; i < n; ++i) {
53
+ out[CMT_ABI_U256_LENGTH - 1 - i] = data[i];
54
+ }
55
+ for (size_t i = n; i < CMT_ABI_U256_LENGTH; ++i) {
56
+ out[CMT_ABI_U256_LENGTH - 1 - i] = 0;
57
+ }
58
+ return 0;
59
+ }
60
+
61
+ int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]) {
62
+ if (n > CMT_ABI_U256_LENGTH) {
63
+ return -EDOM;
64
+ }
65
+ for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) {
66
+ out[i] = 0;
67
+ }
68
+ for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) {
69
+ out[i] = data[i - CMT_ABI_U256_LENGTH + n];
70
+ }
71
+ return 0;
72
+ }
73
+
74
+ int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_ABI_U256_LENGTH]) {
75
+ #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
76
+ return cmt_abi_encode_uint_nn(n, data, out);
77
+ #else
78
+ return cmt_abi_encode_uint_nr(n, data, out);
79
+ #endif
80
+ }
81
+
82
+ int cmt_abi_decode_uint_nr(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) {
83
+ if (n > CMT_ABI_U256_LENGTH) {
84
+ return -EDOM;
85
+ }
86
+ for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) {
87
+ if (data[i]) {
88
+ return -EDOM;
89
+ }
90
+ }
91
+ for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) {
92
+ out[CMT_ABI_U256_LENGTH - 1 - i] = data[i];
93
+ }
94
+ return 0;
95
+ }
96
+
97
+ int cmt_abi_decode_uint_nn(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) {
98
+ if (n > CMT_ABI_U256_LENGTH) {
99
+ return -EDOM;
100
+ }
101
+ for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) {
102
+ if (data[i]) {
103
+ return -EDOM;
104
+ }
105
+ }
106
+ for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) {
107
+ out[i - CMT_ABI_U256_LENGTH + n] = data[i];
108
+ }
109
+ return 0;
110
+ }
111
+
112
+ int cmt_abi_decode_uint(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) {
113
+ #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
114
+ return cmt_abi_decode_uint_nn(data, n, out);
115
+ #else
116
+ return cmt_abi_decode_uint_nr(data, n, out);
117
+ #endif
118
+ }
119
+
120
+ int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data) {
121
+ cmt_buf_t x[1];
122
+ if (data_length > CMT_ABI_U256_LENGTH) {
123
+ return -EDOM;
124
+ }
125
+ if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
126
+ return -ENOBUFS;
127
+ }
128
+ return cmt_abi_encode_uint(data_length, data, x->begin);
129
+ }
130
+
131
+ int cmt_abi_put_uint_be(cmt_buf_t *me, size_t data_length, const void *data) {
132
+ cmt_buf_t x[1];
133
+ if (data_length > CMT_ABI_U256_LENGTH) {
134
+ return -EDOM;
135
+ }
136
+ if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
137
+ return -ENOBUFS;
138
+ }
139
+ return cmt_abi_encode_uint_nn(data_length, data, x->begin);
140
+ }
141
+ int cmt_abi_put_uint256(cmt_buf_t *me, const cmt_abi_u256_t *value) {
142
+ cmt_buf_t x[1];
143
+ if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
144
+ return -ENOBUFS;
145
+ }
146
+ return cmt_abi_encode_uint_nn(sizeof(*value), value->data, x->begin);
147
+ }
148
+
149
+ int cmt_abi_put_bool(cmt_buf_t *me, bool value) {
150
+ uint8_t boolean = !!value;
151
+ return cmt_abi_put_uint(me, sizeof(boolean), &boolean);
152
+ }
153
+
154
+ int cmt_abi_put_address(cmt_buf_t *me, const cmt_abi_address_t *address) {
155
+ cmt_buf_t x[1];
156
+ if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
157
+ return -ENOBUFS;
158
+ }
159
+ return cmt_abi_encode_uint_nn(sizeof(*address), address->data, x->begin);
160
+ }
161
+
162
+ int cmt_abi_put_bytes_s(cmt_buf_t *me, cmt_buf_t *offset) {
163
+ return cmt_buf_split(me, CMT_ABI_U256_LENGTH, offset, me);
164
+ }
165
+
166
+ int cmt_abi_reserve_bytes_d(cmt_buf_t *me, cmt_buf_t *of, size_t n, cmt_buf_t *out, const void *start) {
167
+ int rc = 0;
168
+ cmt_buf_t tmp[1];
169
+ cmt_buf_t sz[1];
170
+ size_t n32 = align_forward(n, CMT_ABI_U256_LENGTH);
171
+
172
+ rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, sz, tmp);
173
+ if (rc) {
174
+ return rc;
175
+ }
176
+ rc = cmt_buf_split(tmp, n32, out, tmp);
177
+ if (rc) {
178
+ return rc;
179
+ }
180
+
181
+ size_t offset = sz->begin - (uint8_t *) start;
182
+ rc = cmt_abi_encode_uint(sizeof(offset), &offset, of->begin);
183
+ if (rc) {
184
+ return rc;
185
+ }
186
+ rc = cmt_abi_encode_uint(sizeof(n), &n, sz->begin);
187
+ if (rc) {
188
+ return rc;
189
+ }
190
+
191
+ *me = *tmp; // commit the buffer changes
192
+ // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
193
+ memset(out->begin + n, 0, n32 - n); // zero out the padding
194
+ return 0;
195
+ }
196
+
197
+ int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, const cmt_buf_t *frame, const cmt_abi_bytes_t *payload) {
198
+ cmt_buf_t res[1];
199
+ int rc = cmt_abi_reserve_bytes_d(me, offset, payload->length, res, frame->begin);
200
+ if (rc) {
201
+ return rc;
202
+ }
203
+ // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
204
+ memcpy(res->begin, payload->data, payload->length);
205
+ return 0;
206
+ }
207
+
208
+ uint32_t cmt_abi_peek_funsel(cmt_buf_t *me) {
209
+ if (cmt_buf_length(me) < 4) {
210
+ return 0;
211
+ }
212
+ return CMT_ABI_FUNSEL(me->begin[0], me->begin[1], me->begin[2], me->begin[3]);
213
+ }
214
+
215
+ int cmt_abi_check_funsel(cmt_buf_t *me, uint32_t expected) {
216
+ if (cmt_buf_length(me) < 4) {
217
+ return -ENOBUFS;
218
+ }
219
+
220
+ if (cmt_abi_peek_funsel(me) != expected) {
221
+ return -EBADMSG;
222
+ }
223
+
224
+ me->begin += 4;
225
+ return 0;
226
+ }
227
+
228
+ int cmt_abi_get_uint(cmt_buf_t *me, size_t n, void *data) {
229
+ cmt_buf_t x[1];
230
+
231
+ if (n > CMT_ABI_U256_LENGTH) {
232
+ return -EDOM;
233
+ }
234
+ int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me);
235
+ if (rc) {
236
+ return rc;
237
+ }
238
+
239
+ return cmt_abi_decode_uint(x->begin, n, data);
240
+ }
241
+
242
+ int cmt_abi_get_uint_be(cmt_buf_t *me, size_t n, void *data) {
243
+ cmt_buf_t x[1];
244
+
245
+ if (n > CMT_ABI_U256_LENGTH) {
246
+ return -EDOM;
247
+ }
248
+ int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me);
249
+ if (rc) {
250
+ return rc;
251
+ }
252
+
253
+ return cmt_abi_decode_uint_nn(x->begin, n, data);
254
+ }
255
+
256
+ int cmt_abi_get_uint256(cmt_buf_t *me, cmt_abi_u256_t *value) {
257
+ cmt_buf_t x[1];
258
+ if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
259
+ return -ENOBUFS;
260
+ }
261
+ return cmt_abi_decode_uint_nn(x->begin, sizeof(*value), value->data);
262
+ }
263
+
264
+ int cmt_abi_get_bool(cmt_buf_t *me, bool *value) {
265
+ bool boolean = 0;
266
+ int rc = cmt_abi_get_uint(me, sizeof(boolean), &boolean);
267
+ if (rc) {
268
+ return rc;
269
+ }
270
+ *value = boolean;
271
+ return 0;
272
+ }
273
+
274
+ int cmt_abi_get_address(cmt_buf_t *me, cmt_abi_address_t *address) {
275
+ cmt_buf_t x[1];
276
+
277
+ int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me);
278
+ if (rc) {
279
+ return rc;
280
+ }
281
+ return cmt_abi_decode_uint_nn(x->begin, sizeof(*address), address->data);
282
+ }
283
+
284
+ int cmt_abi_get_bytes_s(cmt_buf_t *me, cmt_buf_t of[1]) {
285
+ return cmt_buf_split(me, CMT_ABI_U256_LENGTH, of, me);
286
+ }
287
+
288
+ int cmt_abi_peek_bytes_d(const cmt_buf_t *start, cmt_buf_t of[1], cmt_buf_t *bytes) {
289
+ int rc = 0;
290
+ uint64_t offset = 0;
291
+ uint64_t size = 0;
292
+ rc = cmt_abi_get_uint(of, sizeof(offset), &offset);
293
+ if (rc) {
294
+ return rc;
295
+ }
296
+
297
+ /* from the beginning, after funsel */
298
+ cmt_buf_t it[1] = {{start->begin + offset, start->end}};
299
+ rc = cmt_abi_get_uint(it, sizeof(size), &size);
300
+ if (rc) {
301
+ return rc;
302
+ }
303
+ return cmt_buf_split(it, size, bytes, it);
304
+ }
305
+
306
+ int cmt_abi_get_bytes_d(const cmt_buf_t *start, cmt_buf_t of[1], size_t *n, void **data) {
307
+ cmt_buf_t bytes[1];
308
+ int rc = cmt_abi_peek_bytes_d(start, of, bytes);
309
+ if (rc) {
310
+ return rc;
311
+ }
312
+ *n = cmt_buf_length(bytes);
313
+ *data = bytes->begin;
314
+ return 0;
315
+ }
@@ -0,0 +1,121 @@
1
+ /* Copyright Cartesi and individual authors (see AUTHORS)
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ #include "libcmt/buf.h"
17
+
18
+ #include <errno.h>
19
+ #include <stdbool.h>
20
+ #include <stdio.h>
21
+
22
+ static inline int is_pow2(int l) {
23
+ return !(l & (l - 1));
24
+ }
25
+
26
+ void cmt_buf_init(cmt_buf_t *me, size_t length, void *data) {
27
+ if (!me) {
28
+ return;
29
+ }
30
+ me->begin = (uint8_t *) data;
31
+ me->end = (uint8_t *) data + length;
32
+ }
33
+
34
+ int cmt_buf_split(const cmt_buf_t *me, size_t lhs_length, cmt_buf_t *lhs, cmt_buf_t *rhs) {
35
+ if (!me) {
36
+ return -EINVAL;
37
+ }
38
+ if (!lhs) {
39
+ return -EINVAL;
40
+ }
41
+ if (!rhs) {
42
+ return -EINVAL;
43
+ }
44
+
45
+ uint8_t *begin = me->begin;
46
+ uint8_t *split = me->begin + lhs_length;
47
+ uint8_t *end = me->end;
48
+
49
+ if (split < begin || end < split) {
50
+ return -ENOBUFS;
51
+ }
52
+
53
+ lhs->begin = begin;
54
+ lhs->end = split;
55
+ rhs->begin = split;
56
+ rhs->end = end;
57
+
58
+ return 0;
59
+ }
60
+
61
+ static int comma(const uint8_t *s, const uint8_t *end) {
62
+ return s < end && *s == ',';
63
+ }
64
+
65
+ static bool cmt_buf_split_by(cmt_buf_t *x, cmt_buf_t *xs, int (*nxt)(const uint8_t *s, const uint8_t *end)) {
66
+ if (xs->begin == xs->end) {
67
+ return false;
68
+ }
69
+ x->begin = xs->begin;
70
+ for (; *xs->begin && xs->begin < xs->end && !nxt(xs->begin, xs->end); ++xs->begin) {
71
+ ;
72
+ }
73
+ x->end = xs->begin;
74
+ xs->begin = xs->begin + nxt(xs->begin, xs->end);
75
+ return *x->begin;
76
+ }
77
+
78
+ bool cmt_buf_split_by_comma(cmt_buf_t *x, cmt_buf_t *xs) {
79
+ return cmt_buf_split_by(x, xs, comma);
80
+ }
81
+
82
+ size_t cmt_buf_length(const cmt_buf_t *me) {
83
+ if (!me) {
84
+ return 0;
85
+ }
86
+ return me->end - me->begin;
87
+ }
88
+
89
+ static void xxd(const uint8_t *p, const uint8_t *q, size_t mask) {
90
+ if (q < p) {
91
+ return;
92
+ }
93
+
94
+ for (size_t i = 0U, n = q - p; i < n; ++i) {
95
+ bool is_line_start = (i & mask) == 0;
96
+ bool is_line_end = (i & mask) == mask || (i + 1 == n);
97
+ char separator = is_line_end ? '\n' : ' ';
98
+
99
+ if (is_line_start) {
100
+ printf("%p %4zu: ", (void *) (p + i), i);
101
+ }
102
+ printf("%02x%c", p[i], separator);
103
+ }
104
+ }
105
+
106
+ void cmt_buf_xxd(void *begin, void *end, int bytes_per_line) {
107
+ if (!begin) {
108
+ return;
109
+ }
110
+ if (!end) {
111
+ return;
112
+ }
113
+ if (end <= begin) {
114
+ return;
115
+ }
116
+ if (!is_pow2(bytes_per_line)) {
117
+ return;
118
+ }
119
+
120
+ xxd(begin, end, bytes_per_line - 1);
121
+ }