xbintsc 0.6.10 → 0.6.13
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.
- package/dist/src/ast/expressions.d.ts +6 -0
- package/dist/src/checker/globals.js +2 -1
- package/dist/src/checker/globals.js.map +1 -1
- package/dist/src/checker/type-infer.js +1 -1
- package/dist/src/checker/type-infer.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/calls/literals.js +4 -1
- package/dist/src/codegen/generator/calls/literals.js.map +1 -1
- package/dist/src/codegen/generator/expressions/assignment.js +3 -1
- package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
- package/dist/src/codegen/generator/expressions/operators.js +2 -6
- package/dist/src/codegen/generator/expressions/operators.js.map +1 -1
- package/dist/src/codegen/generator/expressions/primary.d.ts +9 -0
- package/dist/src/codegen/generator/expressions/primary.js +44 -7
- package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
- package/dist/src/codegen/generator/state.js +3 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +3 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/commonjs.js +1 -1
- package/dist/src/driver/bundler/commonjs.js.map +1 -1
- package/dist/src/driver/bundler/symbols.js +6 -0
- package/dist/src/driver/bundler/symbols.js.map +1 -1
- package/dist/src/driver/compiler.js +1 -1
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +2 -0
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/node/crypto/index.js +12 -1
- package/dist/src/extensions/node/crypto/index.js.map +1 -1
- package/dist/src/extensions/node/index.js +2 -0
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/readline/index.d.ts +12 -0
- package/dist/src/extensions/node/readline/index.js +27 -0
- package/dist/src/extensions/node/readline/index.js.map +1 -0
- package/dist/src/parser/declarations/functions.js +6 -0
- package/dist/src/parser/declarations/functions.js.map +1 -1
- package/dist/src/parser/literals.js +1 -1
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +26 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-modules.test.js +22 -0
- package/dist/tests/e2e/compile-modules.test.js.map +1 -1
- package/dist/tests/e2e/compile-node.test.js +37 -1
- package/dist/tests/e2e/compile-node.test.js.map +1 -1
- package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
- package/dist/tests/e2e/crypto-random.test.js +63 -0
- package/dist/tests/e2e/crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/date-parse.test.js +44 -1
- package/dist/tests/e2e/date-parse.test.js.map +1 -1
- package/dist/tests/e2e/gui-browser.test.js +61 -0
- package/dist/tests/e2e/gui-browser.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-coverage.test.js +40 -1
- package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-features.test.js +100 -0
- package/dist/tests/e2e/gui-css-features.test.js.map +1 -1
- package/dist/tests/e2e/gui-dom.test.js +63 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-errors.test.d.ts +11 -0
- package/dist/tests/e2e/gui-errors.test.js +68 -0
- package/dist/tests/e2e/gui-errors.test.js.map +1 -0
- package/dist/tests/e2e/gui-form.test.d.ts +9 -0
- package/dist/tests/e2e/gui-form.test.js +359 -0
- package/dist/tests/e2e/gui-form.test.js.map +1 -0
- package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
- package/dist/tests/e2e/gui-style.test.js +82 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -1
- package/dist/tests/e2e/gui-unsupported.test.js +4 -3
- package/dist/tests/e2e/gui-unsupported.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js +7 -1
- package/dist/tests/e2e/gui-webgl2-shadow-sampler.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +4 -0
- package/dist/tests/e2e/harness.js +2 -0
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
- package/dist/tests/e2e/json-parse-errors.test.js +48 -0
- package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
- package/dist/tests/e2e/readline.test.d.ts +7 -0
- package/dist/tests/e2e/readline.test.js +73 -0
- package/dist/tests/e2e/readline.test.js.map +1 -0
- package/doc/ai/language-support.md +2 -1
- package/doc/ai/zh-CN/language-support.md +2 -1
- package/doc/gui/api.md +24 -3
- package/doc/gui/dom.md +28 -14
- package/doc/gui/input.md +74 -3
- package/doc/gui/known-issues.md +2 -1
- package/doc/gui/layout.md +5 -1
- package/doc/implemented/node/crypto.md +8 -0
- package/doc/implemented/node/fs.md +1 -1
- package/doc/implemented/node/readline.md +34 -0
- package/doc/implemented/node.md +1 -0
- package/doc/implemented/stdlib.md +10 -2
- package/doc/unimplemented/node/modules.md +2 -3
- package/doc/unimplemented/node.md +2 -2
- package/doc/zh-CN/gui/api.md +20 -3
- package/doc/zh-CN/gui/dom.md +23 -13
- package/doc/zh-CN/gui/input.md +65 -4
- package/doc/zh-CN/gui/known-issues.md +2 -1
- package/doc/zh-CN/gui/layout.md +4 -1
- package/doc/zh-CN/implemented/node/crypto.md +6 -0
- package/doc/zh-CN/implemented/node/fs.md +1 -1
- package/doc/zh-CN/implemented/node/readline.md +33 -0
- package/doc/zh-CN/implemented/node.md +1 -0
- package/doc/zh-CN/implemented/stdlib.md +10 -2
- package/doc/zh-CN/unimplemented/node/modules.md +1 -2
- package/doc/zh-CN/unimplemented/node.md +2 -2
- package/package.json +7 -7
- package/runtime/ext_gui/browser_abort.cpp +2 -2
- package/runtime/ext_gui/browser_loop.cpp +1 -0
- package/runtime/ext_gui/browser_media.cpp +1 -1
- package/runtime/ext_gui/css_match.cpp +21 -1
- package/runtime/ext_gui/css_selector.cpp +2 -2
- package/runtime/ext_gui/document.cpp +12 -0
- package/runtime/ext_gui/dom.h +41 -2
- package/runtime/ext_gui/dom_api_canvas_image.cpp +2 -1
- package/runtime/ext_gui/dom_api_document.cpp +28 -0
- package/runtime/ext_gui/dom_api_events.cpp +18 -15
- package/runtime/ext_gui/dom_api_form.cpp +567 -0
- package/runtime/ext_gui/dom_api_internal.h +48 -214
- package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
- package/runtime/ext_gui/dom_api_node.cpp +28 -15
- package/runtime/ext_gui/dom_api_node_read.cpp +28 -1
- package/runtime/ext_gui/dom_api_proto.cpp +47 -0
- package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
- package/runtime/ext_gui/dom_parse.cpp +114 -1
- package/runtime/ext_gui/error_report.cpp +166 -0
- package/runtime/ext_gui/form_focus.cpp +293 -0
- package/runtime/ext_gui/form_input.cpp +448 -0
- package/runtime/ext_gui/form_internal.h +30 -0
- package/runtime/ext_gui/form_shared.cpp +126 -0
- package/runtime/ext_gui/form_state.cpp +536 -0
- package/runtime/ext_gui/form_state.h +97 -0
- package/runtime/ext_gui/gui.cpp +16 -0
- package/runtime/ext_gui/gui_engine.h +53 -4
- package/runtime/ext_gui/gui_events.cpp +67 -49
- package/runtime/ext_gui/layout.cpp +6 -0
- package/runtime/ext_gui/layout.h +4 -0
- package/runtime/ext_gui/layout_flow.cpp +23 -1
- package/runtime/ext_gui/layout_inline.cpp +46 -14
- package/runtime/ext_gui/paint.cpp +26 -1
- package/runtime/ext_gui/paint_form.cpp +389 -0
- package/runtime/ext_gui/paint_internal.h +6 -0
- package/runtime/ext_gui/script.cpp +1 -1
- package/runtime/ext_gui/style.cpp +1 -0
- package/runtime/ext_gui/style.h +13 -0
- package/runtime/ext_gui/style_print.cpp +129 -0
- package/runtime/ext_gui/style_text.cpp +79 -0
- package/runtime/ext_gui/style_ua.cpp +42 -0
- package/runtime/ext_gui/warnings.cpp +5 -7
- package/runtime/ext_gui/webgl_texture.cpp +0 -8
- package/runtime/ext_gui/window.cpp +52 -62
- package/runtime/ext_gui/window_diagnostics.cpp +58 -0
- package/runtime/ext_gui/window_emit.cpp +51 -0
- package/runtime/ext_node/crypto/crypto.c +3 -2
- package/runtime/ext_node/crypto/crypto_hash.h +13 -0
- package/runtime/ext_node/crypto/crypto_random.c +339 -0
- package/runtime/ext_node/fs/async_worker.c +1 -1
- package/runtime/ext_node/fs/fs_ops.c +1 -1
- package/runtime/ext_node/fs/parts/platform.inc +67 -0
- package/runtime/ext_node/readline/readline.c +572 -0
- package/runtime/rt.h +42 -0
- package/runtime/rt_internal.h +3 -2
- package/runtime/xt_crypto.c +132 -0
- package/runtime/xt_io/exception.inc +29 -6
- package/runtime/xt_loop.c +36 -1
- package/runtime/xt_promise.c +26 -2
- package/runtime/xt_random.c +143 -0
- package/runtime/xt_regex/matcher.inc +9 -28
- package/runtime/xt_regex/parser.inc +65 -2
- package/runtime/xt_regex/types.inc +11 -1
- package/runtime/xt_regex.h +6 -2
- package/runtime/xt_stdlib/dispatch.inc +2 -0
- package/runtime/xt_stdlib/math.inc +1 -1
- package/runtime/xt_stdlib2/date.inc +206 -6
- package/runtime/xt_stdlib2/json.inc +4 -546
- package/runtime/xt_stdlib2/json_parse.inc +347 -0
- package/runtime/xt_stdlib2/json_stringify.inc +368 -0
- package/runtime/xt_stdlib2/regexp-match.inc +6 -11
- package/runtime/xt_stdlib2.c +0 -4
- package/runtime/xt_typed_array/elements.inc +2 -6
- package/runtime/xt_url.c +0 -6
- package/runtime/xt_url_object.c +1 -1
- package/src/ast/expressions.ts +6 -0
- package/src/checker/globals.ts +2 -0
- package/src/checker/type-infer.ts +1 -1
- package/src/codegen/generator/calls/literals.ts +4 -1
- package/src/codegen/generator/expressions/assignment.ts +3 -1
- package/src/codegen/generator/expressions/operators.ts +2 -6
- package/src/codegen/generator/expressions/primary.ts +58 -6
- package/src/codegen/generator/state.ts +3 -0
- package/src/codegen/generator/tables.ts +3 -0
- package/src/driver/bundler/commonjs.ts +1 -1
- package/src/driver/bundler/symbols.ts +6 -0
- package/src/driver/compiler.ts +1 -1
- package/src/driver/runtime-objects.ts +2 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/crypto/index.ts +12 -1
- package/src/extensions/node/index.ts +2 -0
- package/src/extensions/node/readline/index.ts +31 -0
- package/src/parser/declarations/functions.ts +6 -0
- package/src/parser/literals.ts +1 -1
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
#include "rt.h"
|
|
2
|
+
#include "crypto_hash.h"
|
|
3
|
+
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
#include <stdio.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
/* -- random and password hashing ----------------------------------------- */
|
|
10
|
+
|
|
11
|
+
/* `Buffer.alloc`/`from` live in the same extension archive (buffer.c); this
|
|
12
|
+
* module only needs the static dispatcher to hand a byte array back as a
|
|
13
|
+
* Buffer. */
|
|
14
|
+
xt_value xt_buffer_static(xt_value name, int32_t argc, xt_value *argv);
|
|
15
|
+
|
|
16
|
+
static xt_value xt_crypto_buffer(const uint8_t *bytes, size_t length) {
|
|
17
|
+
xt_value values = xt_array_new(0, NULL);
|
|
18
|
+
for (size_t i = 0; i < length; i++) xt_array_push(values, xt_number((double)bytes[i]));
|
|
19
|
+
xt_value name = xt_string_from_cstr("from");
|
|
20
|
+
xt_value args[1] = {values};
|
|
21
|
+
return xt_buffer_static(name, 1, args);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static void xt_crypto_fail(const char *constructor, const char *message) {
|
|
25
|
+
xt_value text = xt_string_from_cstr(message);
|
|
26
|
+
xt_throw(xt_error_ctor_by_name(xt_string_from_cstr(constructor), 1, &text));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Read a string / Buffer / typed array as raw bytes. On success `out` is
|
|
30
|
+
* malloc'ed and must be freed by the caller. */
|
|
31
|
+
static int xt_crypto_bytes(xt_value value, uint8_t **out, size_t *length) {
|
|
32
|
+
*out = NULL;
|
|
33
|
+
*length = 0;
|
|
34
|
+
if (XT_IS_STRING(value)) {
|
|
35
|
+
const char *text = xt_string_data(value);
|
|
36
|
+
int32_t count = xt_string_length_value(value);
|
|
37
|
+
if (count < 0) count = 0;
|
|
38
|
+
uint8_t *bytes = (uint8_t *)malloc(count ? (size_t)count : 1);
|
|
39
|
+
if (!bytes) return 0;
|
|
40
|
+
if (count > 0) memcpy(bytes, text, (size_t)count);
|
|
41
|
+
*out = bytes;
|
|
42
|
+
*length = (size_t)count;
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
if (xt_is_typed_array(value)) {
|
|
46
|
+
uint32_t view_length = xt_typed_array_length(value);
|
|
47
|
+
/* A `Uint8Array`-style byte view is common; for other element widths the
|
|
48
|
+
* caller's byte count is the view's byte length, which is not exposed.
|
|
49
|
+
* Reading elements keeps this correct for the password/salt use case. */
|
|
50
|
+
uint8_t *bytes = (uint8_t *)malloc(view_length ? view_length : 1);
|
|
51
|
+
if (!bytes) return 0;
|
|
52
|
+
for (uint32_t i = 0; i < view_length; i++) {
|
|
53
|
+
xt_value item = xt_typed_array_read(value, i);
|
|
54
|
+
bytes[i] = (uint8_t)((int)xt_to_number(item) & 0xff);
|
|
55
|
+
}
|
|
56
|
+
*out = bytes;
|
|
57
|
+
*length = view_length;
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
if (XT_IS_OBJECT(value)) {
|
|
61
|
+
xt_value length_value = xt_object_get_cstr(value, "length");
|
|
62
|
+
if (!XT_IS_NUMBER(length_value)) return 0;
|
|
63
|
+
size_t count = (size_t)xt_to_number(length_value);
|
|
64
|
+
uint8_t *bytes = (uint8_t *)malloc(count ? count : 1);
|
|
65
|
+
if (!bytes) return 0;
|
|
66
|
+
for (size_t i = 0; i < count; i++) {
|
|
67
|
+
char key[24];
|
|
68
|
+
snprintf(key, sizeof(key), "%zu", i);
|
|
69
|
+
bytes[i] = (uint8_t)((int)xt_to_number(xt_get(value, xt_string_from_cstr(key))) & 0xff);
|
|
70
|
+
}
|
|
71
|
+
*out = bytes;
|
|
72
|
+
*length = count;
|
|
73
|
+
return 1;
|
|
74
|
+
}
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static xt_value xt_crypto_random_bytes_impl(int32_t argc, xt_value *argv) {
|
|
79
|
+
if (argc < 1) return xt_undefined();
|
|
80
|
+
double size_value = xt_to_number(argv[0]);
|
|
81
|
+
if (!(size_value >= 0) || size_value > 2147483647.0) {
|
|
82
|
+
xt_crypto_fail("RangeError", "The value of \"size\" is out of range");
|
|
83
|
+
}
|
|
84
|
+
size_t size = (size_t)size_value;
|
|
85
|
+
uint8_t *bytes = (uint8_t *)malloc(size ? size : 1);
|
|
86
|
+
if (!bytes) xt_crypto_fail("Error", "out of memory");
|
|
87
|
+
if (xt_secure_random(bytes, size) != 0) {
|
|
88
|
+
free(bytes);
|
|
89
|
+
xt_crypto_fail("Error", "the operating system entropy source is unavailable");
|
|
90
|
+
}
|
|
91
|
+
xt_value buffer = xt_crypto_buffer(bytes, size);
|
|
92
|
+
free(bytes);
|
|
93
|
+
return buffer;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
xt_value xt_crypto_random_bytes(int32_t argc, xt_value *argv) {
|
|
97
|
+
xt_value buffer = xt_crypto_random_bytes_impl(argc, argv);
|
|
98
|
+
if (argc > 1 && XT_IS_FUNCTION(argv[1])) {
|
|
99
|
+
xt_value args[2] = {XT_NULL, buffer};
|
|
100
|
+
xt_call_with_this(argv[1], XT_UNDEFINED, 2, args);
|
|
101
|
+
return xt_undefined();
|
|
102
|
+
}
|
|
103
|
+
return buffer;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
xt_value xt_crypto_random_uuid(int32_t argc, xt_value *argv) {
|
|
107
|
+
(void)argc;
|
|
108
|
+
(void)argv;
|
|
109
|
+
return xt_crypto_static(xt_string_from_cstr("randomUUID"), 0, NULL);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
xt_value xt_crypto_random_int(int32_t argc, xt_value *argv) {
|
|
113
|
+
int32_t callback_index = -1;
|
|
114
|
+
if (argc > 0 && XT_IS_FUNCTION(argv[argc - 1])) callback_index = argc - 1;
|
|
115
|
+
int32_t count = callback_index >= 0 ? callback_index : argc;
|
|
116
|
+
double minimum = 0.0;
|
|
117
|
+
double maximum = 0.0;
|
|
118
|
+
if (count == 1) {
|
|
119
|
+
maximum = xt_to_number(argv[0]);
|
|
120
|
+
} else if (count >= 2) {
|
|
121
|
+
minimum = xt_to_number(argv[0]);
|
|
122
|
+
maximum = xt_to_number(argv[1]);
|
|
123
|
+
} else {
|
|
124
|
+
xt_crypto_fail("TypeError", "randomInt requires a range");
|
|
125
|
+
}
|
|
126
|
+
if (!(maximum > minimum)) {
|
|
127
|
+
xt_crypto_fail("RangeError", "The value of \"max\" must be greater than \"min\"");
|
|
128
|
+
}
|
|
129
|
+
double span = maximum - minimum;
|
|
130
|
+
double value = minimum + (double)xt_random_below((uint64_t)span);
|
|
131
|
+
if (callback_index >= 0) {
|
|
132
|
+
xt_value args[2] = {XT_NULL, xt_number(value)};
|
|
133
|
+
xt_call_with_this(argv[callback_index], XT_UNDEFINED, 2, args);
|
|
134
|
+
return xt_undefined();
|
|
135
|
+
}
|
|
136
|
+
return xt_number(value);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static int xt_crypto_fill_buffer(xt_value target, size_t offset, size_t size) {
|
|
140
|
+
if (xt_is_typed_array(target)) {
|
|
141
|
+
uint32_t length = xt_typed_array_length(target);
|
|
142
|
+
static const int kind_bytes[] = {1, 1, 1, 2, 2, 4, 4, 4, 8};
|
|
143
|
+
int kind = xt_typed_array_kind_of(target);
|
|
144
|
+
size_t total = kind >= 0 ? (size_t)length * (size_t)kind_bytes[kind] : 0;
|
|
145
|
+
if (offset > total) offset = total;
|
|
146
|
+
if (size > total - offset) size = total - offset;
|
|
147
|
+
if (size == 0) return 1;
|
|
148
|
+
uint8_t *bytes = (uint8_t *)malloc(size);
|
|
149
|
+
if (!bytes) return 0;
|
|
150
|
+
int ok = xt_secure_random(bytes, size) == 0;
|
|
151
|
+
if (ok) {
|
|
152
|
+
xt_array_buffer_copy_bytes_in(xt_typed_array_backing_buffer(target),
|
|
153
|
+
xt_typed_array_view_offset(target) + (uint32_t)offset, bytes,
|
|
154
|
+
(uint32_t)size);
|
|
155
|
+
}
|
|
156
|
+
free(bytes);
|
|
157
|
+
return ok;
|
|
158
|
+
}
|
|
159
|
+
xt_value length_value = xt_object_get_cstr(target, "length");
|
|
160
|
+
if (!XT_IS_NUMBER(length_value)) return 0;
|
|
161
|
+
size_t total = (size_t)xt_to_number(length_value);
|
|
162
|
+
if (offset > total) offset = total;
|
|
163
|
+
if (size > total - offset) size = total - offset;
|
|
164
|
+
if (size == 0) return 1;
|
|
165
|
+
uint8_t *bytes = (uint8_t *)malloc(size);
|
|
166
|
+
if (!bytes) return 0;
|
|
167
|
+
int ok = xt_secure_random(bytes, size) == 0;
|
|
168
|
+
if (ok) {
|
|
169
|
+
for (size_t i = 0; i < size; i++) {
|
|
170
|
+
char key[24];
|
|
171
|
+
snprintf(key, sizeof(key), "%zu", offset + i);
|
|
172
|
+
xt_set(target, xt_string_from_cstr(key), xt_number((double)bytes[i]));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
free(bytes);
|
|
176
|
+
return ok;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
xt_value xt_crypto_random_fill_sync(int32_t argc, xt_value *argv) {
|
|
180
|
+
if (argc < 1) return xt_undefined();
|
|
181
|
+
xt_value target = argv[0];
|
|
182
|
+
size_t total = 0;
|
|
183
|
+
if (xt_is_typed_array(target)) {
|
|
184
|
+
static const int kind_bytes[] = {1, 1, 1, 2, 2, 4, 4, 4, 8};
|
|
185
|
+
int kind = xt_typed_array_kind_of(target);
|
|
186
|
+
total = kind >= 0 ? (size_t)xt_typed_array_length(target) * (size_t)kind_bytes[kind] : 0;
|
|
187
|
+
} else {
|
|
188
|
+
xt_value length_value = xt_object_get_cstr(target, "length");
|
|
189
|
+
if (!XT_IS_NUMBER(length_value)) {
|
|
190
|
+
xt_crypto_fail("TypeError", "randomFillSync requires a Buffer or TypedArray");
|
|
191
|
+
}
|
|
192
|
+
total = (size_t)xt_to_number(length_value);
|
|
193
|
+
}
|
|
194
|
+
size_t offset = argc > 1 ? (size_t)xt_to_number(argv[1]) : 0;
|
|
195
|
+
size_t size = argc > 2 ? (size_t)xt_to_number(argv[2]) : total - (offset < total ? offset : total);
|
|
196
|
+
if (!xt_crypto_fill_buffer(target, offset, size)) {
|
|
197
|
+
xt_crypto_fail("Error", "the operating system entropy source is unavailable");
|
|
198
|
+
}
|
|
199
|
+
return target;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
xt_value xt_crypto_random_fill(int32_t argc, xt_value *argv) {
|
|
203
|
+
int32_t callback_index = argc > 1 && XT_IS_FUNCTION(argv[argc - 1]) ? argc - 1 : -1;
|
|
204
|
+
int32_t count = callback_index >= 0 ? callback_index : argc;
|
|
205
|
+
xt_value target = xt_crypto_random_fill_sync(count, argv);
|
|
206
|
+
if (callback_index >= 0) {
|
|
207
|
+
xt_value args[2] = {XT_NULL, target};
|
|
208
|
+
xt_call_with_this(argv[callback_index], XT_UNDEFINED, 2, args);
|
|
209
|
+
return xt_undefined();
|
|
210
|
+
}
|
|
211
|
+
return target;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* -- PBKDF2 -------------------------------------------------------------- */
|
|
215
|
+
|
|
216
|
+
static size_t xt_hmac(const char *algorithm, const uint8_t *key, size_t key_length,
|
|
217
|
+
const uint8_t *data, size_t data_length, uint8_t *out) {
|
|
218
|
+
uint8_t zero = 0;
|
|
219
|
+
size_t digest_length = xt_crypto_hash_compute(algorithm, &zero, 0, out);
|
|
220
|
+
if (digest_length == 0) return 0;
|
|
221
|
+
uint8_t block[64];
|
|
222
|
+
memset(block, 0, sizeof(block));
|
|
223
|
+
if (key_length > sizeof(block)) {
|
|
224
|
+
uint8_t key_hash[64];
|
|
225
|
+
if (xt_crypto_hash_compute(algorithm, key, key_length, key_hash) == 0) return 0;
|
|
226
|
+
memcpy(block, key_hash, digest_length);
|
|
227
|
+
} else {
|
|
228
|
+
memcpy(block, key, key_length);
|
|
229
|
+
}
|
|
230
|
+
uint8_t inner[64 + 4096];
|
|
231
|
+
if (data_length > 4096) return 0;
|
|
232
|
+
for (size_t i = 0; i < sizeof(block); i++) inner[i] = (uint8_t)(block[i] ^ 0x36);
|
|
233
|
+
memcpy(inner + sizeof(block), data, data_length);
|
|
234
|
+
uint8_t inner_hash[64];
|
|
235
|
+
size_t inner_length = xt_crypto_hash_compute(algorithm, inner, sizeof(block) + data_length, inner_hash);
|
|
236
|
+
if (inner_length == 0) return 0;
|
|
237
|
+
uint8_t outer[64];
|
|
238
|
+
for (size_t i = 0; i < sizeof(outer); i++) outer[i] = (uint8_t)(block[i] ^ 0x5c);
|
|
239
|
+
uint8_t outer_input[64 + 64];
|
|
240
|
+
memcpy(outer_input, outer, sizeof(outer));
|
|
241
|
+
memcpy(outer_input + sizeof(outer), inner_hash, inner_length);
|
|
242
|
+
if (xt_crypto_hash_compute(algorithm, outer_input, sizeof(outer) + inner_length, out) == 0) return 0;
|
|
243
|
+
return digest_length;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
xt_value xt_crypto_pbkdf2_sync(int32_t argc, xt_value *argv) {
|
|
247
|
+
if (argc < 4) {
|
|
248
|
+
xt_crypto_fail("TypeError", "pbkdf2Sync requires password, salt, iterations and keylen");
|
|
249
|
+
}
|
|
250
|
+
uint8_t *password = NULL;
|
|
251
|
+
uint8_t *salt = NULL;
|
|
252
|
+
size_t password_length = 0;
|
|
253
|
+
size_t salt_length = 0;
|
|
254
|
+
if (!xt_crypto_bytes(argv[0], &password, &password_length) ||
|
|
255
|
+
!xt_crypto_bytes(argv[1], &salt, &salt_length)) {
|
|
256
|
+
free(password);
|
|
257
|
+
free(salt);
|
|
258
|
+
xt_crypto_fail("TypeError", "password and salt must be strings or Buffers");
|
|
259
|
+
}
|
|
260
|
+
int iterations = (int)xt_to_number(argv[2]);
|
|
261
|
+
size_t key_length = (size_t)xt_to_number(argv[3]);
|
|
262
|
+
const char *algorithm = argc > 4 && !xt_truthy(xt_is_nullish(argv[4]))
|
|
263
|
+
? xt_string_data(xt_to_string(argv[4]))
|
|
264
|
+
: "sha256";
|
|
265
|
+
if (iterations < 1) {
|
|
266
|
+
free(password);
|
|
267
|
+
free(salt);
|
|
268
|
+
xt_crypto_fail("RangeError", "iterations must be a positive number");
|
|
269
|
+
}
|
|
270
|
+
uint8_t zero = 0;
|
|
271
|
+
uint8_t probe[64];
|
|
272
|
+
size_t digest_length = xt_crypto_hash_compute(algorithm, &zero, 0, probe);
|
|
273
|
+
if (digest_length == 0) {
|
|
274
|
+
free(password);
|
|
275
|
+
free(salt);
|
|
276
|
+
xt_crypto_fail("Error", "unsupported PBKDF2 digest");
|
|
277
|
+
}
|
|
278
|
+
uint8_t *output = (uint8_t *)malloc(key_length ? key_length : 1);
|
|
279
|
+
if (!output) {
|
|
280
|
+
free(password);
|
|
281
|
+
free(salt);
|
|
282
|
+
xt_crypto_fail("Error", "out of memory");
|
|
283
|
+
}
|
|
284
|
+
size_t done = 0;
|
|
285
|
+
uint32_t block_index = 1;
|
|
286
|
+
uint8_t *block_input = (uint8_t *)malloc(salt_length + 4);
|
|
287
|
+
if (!block_input) {
|
|
288
|
+
free(password);
|
|
289
|
+
free(salt);
|
|
290
|
+
free(output);
|
|
291
|
+
xt_crypto_fail("Error", "out of memory");
|
|
292
|
+
}
|
|
293
|
+
while (done < key_length) {
|
|
294
|
+
memcpy(block_input, salt, salt_length);
|
|
295
|
+
block_input[salt_length + 0] = (uint8_t)(block_index >> 24);
|
|
296
|
+
block_input[salt_length + 1] = (uint8_t)(block_index >> 16);
|
|
297
|
+
block_input[salt_length + 2] = (uint8_t)(block_index >> 8);
|
|
298
|
+
block_input[salt_length + 3] = (uint8_t)block_index;
|
|
299
|
+
uint8_t u[64];
|
|
300
|
+
uint8_t t[64];
|
|
301
|
+
if (xt_hmac(algorithm, password, password_length, block_input, salt_length + 4, u) == 0) break;
|
|
302
|
+
memcpy(t, u, digest_length);
|
|
303
|
+
for (int round = 1; round < iterations; round++) {
|
|
304
|
+
uint8_t next[64];
|
|
305
|
+
if (xt_hmac(algorithm, password, password_length, u, digest_length, next) == 0) break;
|
|
306
|
+
memcpy(u, next, digest_length);
|
|
307
|
+
for (size_t i = 0; i < digest_length; i++) t[i] ^= u[i];
|
|
308
|
+
}
|
|
309
|
+
size_t copy = key_length - done < digest_length ? key_length - done : digest_length;
|
|
310
|
+
memcpy(output + done, t, copy);
|
|
311
|
+
done += copy;
|
|
312
|
+
block_index++;
|
|
313
|
+
}
|
|
314
|
+
free(block_input);
|
|
315
|
+
free(password);
|
|
316
|
+
free(salt);
|
|
317
|
+
xt_value buffer = xt_crypto_buffer(output, done);
|
|
318
|
+
free(output);
|
|
319
|
+
return buffer;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
xt_value xt_crypto_pbkdf2(int32_t argc, xt_value *argv) {
|
|
323
|
+
int32_t callback_index = argc > 0 && XT_IS_FUNCTION(argv[argc - 1]) ? argc - 1 : -1;
|
|
324
|
+
int32_t count = callback_index >= 0 ? callback_index : argc;
|
|
325
|
+
xt_value result = xt_crypto_pbkdf2_sync(count, argv);
|
|
326
|
+
if (callback_index >= 0) {
|
|
327
|
+
xt_value args[2] = {XT_NULL, result};
|
|
328
|
+
xt_call_with_this(argv[callback_index], XT_UNDEFINED, 2, args);
|
|
329
|
+
return xt_undefined();
|
|
330
|
+
}
|
|
331
|
+
return result;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
xt_value xt_crypto_random_fill_callback(int32_t argc, xt_value *argv) {
|
|
335
|
+
return xt_crypto_random_fill(argc, argv);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
@@ -333,7 +333,7 @@ void xt_fs_job_run(xt_async_job *base) {
|
|
|
333
333
|
if (!job->path || xt_fs_rmdir_one(job->path) != 0) xt_fs_job_fail(job, errno, "rmdir", job->path);
|
|
334
334
|
break;
|
|
335
335
|
case XT_FS_JOB_RENAME:
|
|
336
|
-
if (!job->path || !job->path2 ||
|
|
336
|
+
if (!job->path || !job->path2 || xt_fs_rename_atomic(job->path, job->path2) != 0) {
|
|
337
337
|
xt_fs_job_fail(job, errno, "rename", job->path);
|
|
338
338
|
}
|
|
339
339
|
break;
|
|
@@ -238,7 +238,7 @@ xt_value xt_node_rename(int32_t argc, xt_value *argv) {
|
|
|
238
238
|
const char *from = xt_string_data(xt_to_string(argv[0]));
|
|
239
239
|
const char *to = xt_string_data(xt_to_string(argv[1]));
|
|
240
240
|
if (!from || !to) return xt_undefined();
|
|
241
|
-
if (
|
|
241
|
+
if (xt_fs_rename_atomic(from, to) != 0) xt_fs_error("rename", from);
|
|
242
242
|
return xt_undefined();
|
|
243
243
|
}
|
|
244
244
|
|
|
@@ -118,6 +118,8 @@ static inline size_t xt_fs_path_root(const char *path) {
|
|
|
118
118
|
while (xt_fs_is_sep(path[i])) i++;
|
|
119
119
|
return i;
|
|
120
120
|
}
|
|
121
|
+
#else
|
|
122
|
+
(void)path;
|
|
121
123
|
#endif
|
|
122
124
|
return 0;
|
|
123
125
|
}
|
|
@@ -150,3 +152,68 @@ static inline int xt_fs_mkdir_parents(const char *path, int mode) {
|
|
|
150
152
|
free(copy);
|
|
151
153
|
return status;
|
|
152
154
|
}
|
|
155
|
+
|
|
156
|
+
/* -- atomic rename -------------------------------------------------------- */
|
|
157
|
+
/* POSIX `rename` replaces an existing destination atomically. The Windows CRT
|
|
158
|
+
* `rename` fails with EEXIST instead, so `renameSync` could not overwrite a
|
|
159
|
+
* file (the usual write-to-temp-then-rename pattern broke on Windows).
|
|
160
|
+
* `MoveFileExW` with `MOVEFILE_REPLACE_EXISTING` has the POSIX semantics and
|
|
161
|
+
* takes UTF-8 paths through a wide-string conversion. */
|
|
162
|
+
#if defined(_WIN32)
|
|
163
|
+
#ifndef WIN32_LEAN_AND_MEAN
|
|
164
|
+
#define WIN32_LEAN_AND_MEAN
|
|
165
|
+
#endif
|
|
166
|
+
#include <windows.h>
|
|
167
|
+
|
|
168
|
+
static inline wchar_t *xt_fs_utf8_to_wide(const char *text) {
|
|
169
|
+
int length = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0);
|
|
170
|
+
if (length <= 0) return NULL;
|
|
171
|
+
wchar_t *wide = (wchar_t *)malloc((size_t)length * sizeof(wchar_t));
|
|
172
|
+
if (!wide) return NULL;
|
|
173
|
+
if (MultiByteToWideChar(CP_UTF8, 0, text, -1, wide, length) <= 0) {
|
|
174
|
+
free(wide);
|
|
175
|
+
return NULL;
|
|
176
|
+
}
|
|
177
|
+
return wide;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static inline int xt_fs_rename_atomic(const char *from, const char *to) {
|
|
181
|
+
wchar_t *wide_from = xt_fs_utf8_to_wide(from);
|
|
182
|
+
wchar_t *wide_to = xt_fs_utf8_to_wide(to);
|
|
183
|
+
if (!wide_from || !wide_to) {
|
|
184
|
+
free(wide_from);
|
|
185
|
+
free(wide_to);
|
|
186
|
+
errno = EINVAL;
|
|
187
|
+
return -1;
|
|
188
|
+
}
|
|
189
|
+
BOOL ok = MoveFileExW(wide_from, wide_to, MOVEFILE_REPLACE_EXISTING);
|
|
190
|
+
DWORD error = ok ? ERROR_SUCCESS : GetLastError();
|
|
191
|
+
free(wide_from);
|
|
192
|
+
free(wide_to);
|
|
193
|
+
if (ok) return 0;
|
|
194
|
+
switch (error) {
|
|
195
|
+
case ERROR_FILE_NOT_FOUND:
|
|
196
|
+
case ERROR_PATH_NOT_FOUND:
|
|
197
|
+
errno = ENOENT;
|
|
198
|
+
break;
|
|
199
|
+
case ERROR_ACCESS_DENIED:
|
|
200
|
+
case ERROR_SHARING_VIOLATION:
|
|
201
|
+
errno = EACCES;
|
|
202
|
+
break;
|
|
203
|
+
case ERROR_ALREADY_EXISTS:
|
|
204
|
+
errno = EEXIST;
|
|
205
|
+
break;
|
|
206
|
+
case ERROR_NOT_SAME_DEVICE:
|
|
207
|
+
errno = EXDEV;
|
|
208
|
+
break;
|
|
209
|
+
default:
|
|
210
|
+
errno = EIO;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
return -1;
|
|
214
|
+
}
|
|
215
|
+
#else
|
|
216
|
+
static inline int xt_fs_rename_atomic(const char *from, const char *to) {
|
|
217
|
+
return rename(from, to);
|
|
218
|
+
}
|
|
219
|
+
#endif
|