forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwasi-simdutf-stubs.h
More file actions
205 lines (172 loc) Β· 6.86 KB
/
wasi-simdutf-stubs.h
File metadata and controls
205 lines (172 loc) Β· 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef WASI_SIMDUTF_STUBS_H_
#define WASI_SIMDUTF_STUBS_H_
#ifdef __wasi__
#include <cstddef>
#include <cstdint>
#include "wasi-simdutf-compat.h" // Include this to get the result struct
namespace simdutf {
// Character encoding validation functions
inline result_with_error validate_ascii_with_errors(const char* buf, size_t len) {
// Simple ASCII validation
for (size_t i = 0; i < len; i++) {
if (static_cast<unsigned char>(buf[i]) > 127) {
return result_with_error(false, error_code::INVALID_BASE64_CHARACTER);
}
}
return result_with_error(true, error_code::SUCCESS);
}
// UTF conversion length calculation functions
inline size_t utf8_length_from_utf16(const char16_t* input, size_t length) {
// Simple approximation: UTF-8 can be up to 3 bytes per UTF-16 unit
return length * 3;
}
inline size_t utf8_length_from_latin1(const char* input, size_t length) {
// Latin1 to UTF-8 can be up to 2 bytes per character
return length * 2;
}
inline size_t utf16_length_from_utf8(const char* input, size_t length) {
// UTF-8 to UTF-16: worst case is 1 UTF-16 unit per UTF-8 byte
return length;
}
// UTF conversion functions
inline size_t convert_utf16_to_utf8(const char16_t* input, size_t length, char* output) {
// Basic stub implementation - copy as-is (incorrect but compilable)
for (size_t i = 0; i < length; i++) {
output[i] = static_cast<char>(input[i] & 0xFF);
}
return length;
}
// Convert UTF16 to Latin1
inline size_t convert_utf16_to_latin1(const char16_t* input, size_t length, char* output) {
// Simple conversion - just copy lower bytes (Latin1 is 8-bit)
for (size_t i = 0; i < length; i++) {
output[i] = static_cast<char>(input[i] & 0xFF);
}
return length;
}
// Convert Latin1 to UTF16 little-endian
inline size_t convert_latin1_to_utf16le(const char* input, size_t length, char16_t* output) {
// Latin1 maps directly to first 256 Unicode code points
for (size_t i = 0; i < length; i++) {
output[i] = static_cast<char16_t>(static_cast<unsigned char>(input[i]));
}
return length;
}
// Convert UTF8 to UTF16 little-endian
inline size_t convert_utf8_to_utf16le(const char* input, size_t length, char16_t* output) {
// Simple stub - just treat as Latin1
return convert_latin1_to_utf16le(input, length, output);
}
// Base64 encoding/decoding functions
// base64_type is defined in wasi-simdutf-compat.h
inline size_t base64_length_from_binary(size_t length, base64_type type = base64_default) {
// Base64 encoding: 4 characters for every 3 bytes, rounded up
return ((length + 2) / 3) * 4;
}
inline size_t maximal_binary_length_from_base64(const char* input, size_t length) {
// Base64 decoding: 3 bytes for every 4 characters
return (length / 4) * 3;
}
inline size_t maximal_binary_length_from_base64(const char16_t* input, size_t length) {
// Base64 decoding: 3 bytes for every 4 characters (same as char version)
return (length / 4) * 3;
}
inline size_t binary_to_base64(const char* input, size_t length, char* output, base64_type type = base64_default) {
// Stub implementation - just return expected length
return base64_length_from_binary(length, type);
}
// Error codes are defined in wasi-simdutf-compat.h
// Base64 decoding with safety checks
inline result base64_to_binary_safe(const char* input, size_t length, char* output, base64_type type = base64_default) {
// Simple stub implementation
size_t output_len = maximal_binary_length_from_base64(input, length);
if (output_len > 0) {
// Simulate successful decoding
return result(error_code::SUCCESS, output_len);
}
return result(error_code::INVALID_BASE64_CHARACTER, 0);
}
// Overload that takes output length
inline result base64_to_binary_safe(const char* input, size_t length, char* output, size_t& output_length, base64_type type = base64_default) {
// Simple stub implementation
output_length = maximal_binary_length_from_base64(input, length);
if (output_length > 0) {
// Simulate successful decoding
return result(error_code::SUCCESS, output_length);
}
return result(error_code::INVALID_BASE64_CHARACTER, 0);
}
// Overload for char16_t input
inline result base64_to_binary_safe(const char16_t* input, size_t length, char* output, size_t& output_length, base64_type type = base64_default) {
// Simple stub implementation
output_length = maximal_binary_length_from_base64(static_cast<const char*>(nullptr), length);
if (output_length > 0) {
// Simulate successful decoding
return result(error_code::SUCCESS, output_length);
}
return result(error_code::INVALID_BASE64_CHARACTER, 0);
}
// Overload for char16_t without output_length
inline result base64_to_binary_safe(const char16_t* input, size_t length, char* output, base64_type type = base64_default) {
// Simple stub implementation
size_t output_len = maximal_binary_length_from_base64(static_cast<const char*>(nullptr), length);
if (output_len > 0) {
// Simulate successful decoding
return result(error_code::SUCCESS, output_len);
}
return result(error_code::INVALID_BASE64_CHARACTER, 0);
}
// Convert Latin-1 to UTF-8
inline size_t convert_latin1_to_utf8(const char* src, size_t len, char* dst) {
size_t dst_len = 0;
for (size_t i = 0; i < len; i++) {
unsigned char c = static_cast<unsigned char>(src[i]);
if (c < 0x80) {
dst[dst_len++] = static_cast<char>(c);
} else {
dst[dst_len++] = static_cast<char>(0xC0 | (c >> 6));
dst[dst_len++] = static_cast<char>(0x80 | (c & 0x3F));
}
}
return dst_len;
}
// UTF-8 length from Latin-1 - removed duplicate, already defined above
// Additional simdutf functions
inline size_t utf8_length_from_utf16le(const char16_t* input, size_t length) {
// UTF-16LE is same as UTF-16 for length calculation
return utf8_length_from_utf16(input, length);
}
inline size_t convert_utf16le_to_utf8(const char16_t* input, size_t length, char* output) {
// UTF-16LE is same as UTF-16 for conversion
return convert_utf16_to_utf8(input, length, output);
}
inline bool validate_utf8(const char* input, size_t length) {
// Simple UTF-8 validation stub
return true;
}
inline result_with_error validate_utf8_with_errors(const char* input, size_t length) {
// Simple validation - always succeed for stub
return result_with_error(true, error_code::SUCCESS);
}
inline bool validate_ascii(const char* input, size_t length) {
// Check if all bytes are ASCII
for (size_t i = 0; i < length; i++) {
if (static_cast<unsigned char>(input[i]) > 127) {
return false;
}
}
return true;
}
inline size_t convert_utf8_to_utf16(const char* input, size_t length, char16_t* output) {
// Basic stub implementation
size_t output_len = 0;
for (size_t i = 0; i < length && input[i]; i++) {
output[output_len++] = static_cast<char16_t>(static_cast<unsigned char>(input[i]));
}
return output_len;
}
// Version macro
#define SIMDUTF_VERSION "5.7.0"
} // namespace simdutf
#endif // __wasi__
#endif // WASI_SIMDUTF_STUBS_H_