mirror of
https://github.com/zaerl/mojibake.git
synced 2026-12-01 16:56:40 +01:00
Update mjb_confusable_* to mjb_text (#121)
This commit is contained in:
@@ -3149,9 +3149,7 @@ Compute a Unicode confusable skeleton (Unicode 18.0.0 UTS #39 Section 4).
|
||||
|
||||
```c
|
||||
mjb_status mjb_confusable_skeleton(
|
||||
const char *buffer,
|
||||
size_t byte_length,
|
||||
mjb_encoding encoding,
|
||||
const mjb_text *text,
|
||||
mjb_encoding output_encoding,
|
||||
mjb_result *result
|
||||
);
|
||||
@@ -3159,16 +3157,14 @@ mjb_status mjb_confusable_skeleton(
|
||||
|
||||
Compute the UTS #39 `bidiSkeleton(LTR, input)`: apply the Unicode Bidirectional Algorithm through L4, then NFD, remove default-ignorables, substitute prototypes from `confusables.txt`, and reapply NFD. Skeletons can be stored or indexed so future confusable checks can compare them directly. If `MJB_FEATURE_SECURITY=0` the function always returns `MJB_STATUS_FEATURE_NOT_ENABLED`.
|
||||
|
||||
- `buffer` - The string to transform
|
||||
- `byte_length` - The length of the string in bytes, or `MJB_NUL_TERMINATED` to determine it from an encoding-aware NUL code unit
|
||||
- `encoding` - The encoding of the string
|
||||
- `text` - The text to transform
|
||||
- `output_encoding` - The output encoding of the skeleton
|
||||
- `result` - The pointer to store the result. If `result->owned` is true, `result->output` is library-allocated and must be freed with `mjb_result_free(result)`. Otherwise it points into the input, which must outlive the result
|
||||
|
||||
**Returns**
|
||||
|
||||
- `MJB_STATUS_OK` - The confusable skeleton was returned
|
||||
- `MJB_STATUS_INVALID_ARGUMENT` - `result` is NULL, or `buffer` is NULL with a non-zero size
|
||||
- `MJB_STATUS_INVALID_ARGUMENT` - `result` or `text` is NULL, or `text->buffer` is NULL with a non-zero size
|
||||
- `MJB_STATUS_INVALID_ENCODING` - An encoding is invalid
|
||||
- `MJB_STATUS_UNSUPPORTED` - The output encoding cannot represent a skeleton codepoint
|
||||
- `MJB_STATUS_OVERFLOW` - The output size would overflow
|
||||
@@ -3178,11 +3174,11 @@ Compute the UTS #39 `bidiSkeleton(LTR, input)`: apply the Unicode Bidirectional
|
||||
**Example**
|
||||
|
||||
```c
|
||||
const char *input = "h\xD0\xB5llo"; // Cyrillic U+0435 in place of e
|
||||
// Cyrillic U+0435 in place of e
|
||||
mjb_text input = mjb_text_utf8("h\xD0\xB5llo", MJB_NUL_TERMINATED);
|
||||
mjb_result result;
|
||||
|
||||
if(mjb_confusable_skeleton(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
||||
&result) != MJB_STATUS_OK) {
|
||||
if(mjb_confusable_skeleton(&input, MJB_ENC_UTF_8, &result) != MJB_STATUS_OK) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -3201,28 +3197,20 @@ Compute a Unicode confusable skeleton into a caller-provided buffer.
|
||||
|
||||
```c
|
||||
mjb_status mjb_confusable_skeleton_into(
|
||||
const char *buffer,
|
||||
size_t byte_length,
|
||||
mjb_encoding encoding,
|
||||
mjb_encoding output_encoding,
|
||||
void *output,
|
||||
size_t *output_size
|
||||
const mjb_text *text,
|
||||
mjb_buffer *output
|
||||
);
|
||||
```
|
||||
|
||||
Compute the same UTS #39 `bidiSkeleton(LTR, input)` as `mjb_confusable_skeleton` without allocating the final output buffer. Set `output` to NULL to query the required size. If `output` is non-NULL, `*output_size` supplies its capacity; on return it contains the required size when the buffer is too small, or the written size on success. Terminators are excluded and are not written. If capacity is insufficient, or another error occurs, the output buffer may hold a partial prefix of the result, but nothing is written past its capacity. Bidirectional resolution, normalization, and skeleton mapping still require temporary allocations, including during a size query. If `MJB_FEATURE_SECURITY=0` the function always returns `MJB_STATUS_FEATURE_NOT_ENABLED`.
|
||||
Compute the same UTS #39 `bidiSkeleton(LTR, input)` as `mjb_confusable_skeleton` without allocating the final output buffer. `output->encoding` selects the output encoding. Set `output->data` to NULL to query the required size. If `output->data` is non-NULL, `output->size` supplies its capacity; on return it contains the required size when the buffer is too small, or the written size on success. Terminators are excluded and are not written. If capacity is insufficient, or another error occurs, the output buffer may hold a partial prefix of the result, but nothing is written past its capacity. Bidirectional resolution, normalization, and skeleton mapping still require temporary allocations, including during a size query. If `MJB_FEATURE_SECURITY=0` the function always returns `MJB_STATUS_FEATURE_NOT_ENABLED`.
|
||||
|
||||
- `buffer` - The string to transform
|
||||
- `byte_length` - The length of the string in bytes, or `MJB_NUL_TERMINATED` to determine it from an encoding-aware NUL code unit
|
||||
- `encoding` - The encoding of the string
|
||||
- `output_encoding` - The output encoding of the skeleton
|
||||
- `output` - The caller-provided output buffer, or NULL to query the required size. The caller retains ownership
|
||||
- `output_size` - The input capacity and output required or written byte count
|
||||
- `text` - The text to transform
|
||||
- `output` - The output storage, its capacity, and its encoding. The caller retains ownership of `output->data`
|
||||
|
||||
**Returns**
|
||||
|
||||
- `MJB_STATUS_OK` - The required size was returned or the skeleton was written
|
||||
- `MJB_STATUS_INVALID_ARGUMENT` - `output_size` is NULL, or `buffer` is NULL with a non-zero size
|
||||
- `MJB_STATUS_INVALID_ARGUMENT` - `output` or `text` is NULL, or `text->buffer` is NULL with a non-zero size
|
||||
- `MJB_STATUS_INVALID_ENCODING` - An encoding is invalid
|
||||
- `MJB_STATUS_MALFORMED_INPUT` - The input contains an ill-formed code-unit sequence
|
||||
- `MJB_STATUS_UNSUPPORTED` - The output encoding cannot represent a skeleton codepoint
|
||||
@@ -3234,23 +3222,29 @@ Compute the same UTS #39 `bidiSkeleton(LTR, input)` as `mjb_confusable_skeleton`
|
||||
**Example**
|
||||
|
||||
```c
|
||||
const char *input = "h\xD0\xB5llo"; // Cyrillic U+0435 in place of e
|
||||
size_t output_size = 0;
|
||||
// Cyrillic U+0435 in place of e
|
||||
mjb_text input = mjb_text_utf8("h\xD0\xB5llo", MJB_NUL_TERMINATED);
|
||||
mjb_buffer output = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
||||
|
||||
if(mjb_confusable_skeleton_into(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
||||
NULL, &output_size) != MJB_STATUS_OK) {
|
||||
// Query the required size
|
||||
if(mjb_confusable_skeleton_into(&input, &output) != MJB_STATUS_OK) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
char output[5];
|
||||
char storage[5];
|
||||
|
||||
if(output_size > sizeof(output) || mjb_confusable_skeleton_into(input, strlen(input),
|
||||
MJB_ENC_UTF_8, MJB_ENC_UTF_8, output, &output_size) != MJB_STATUS_OK) {
|
||||
if(output.size > sizeof(storage)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
output = mjb_buffer_make(storage, sizeof(storage), MJB_ENC_UTF_8);
|
||||
|
||||
if(mjb_confusable_skeleton_into(&input, &output) != MJB_STATUS_OK) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Skeleton payload (no terminator): hello
|
||||
printf("Skeleton payload (no terminator): %.*s", (int)output_size, output);
|
||||
printf("Skeleton payload (no terminator): %.*s", (int)output.size, storage);
|
||||
```
|
||||
|
||||
See also: [`mjb_confusable_skeleton`](#mjb_confusable_skeleton), [`mjb_confusable_match`](#mjb_confusable_match), [`mjb_is_identifier`](#mjb_is_identifier).
|
||||
|
||||
@@ -50,6 +50,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
of the buffer, byte length, and encoding: `mjb_idna_to_ascii(text, output_encoding, info,
|
||||
result)`. Their `_into` variants also take a `mjb_buffer *` instead of the output encoding,
|
||||
output buffer, and output size: `mjb_idna_to_ascii_into(text, info, output)`
|
||||
- **Breaking**: `mjb_confusable_skeleton` takes a `const mjb_text *` instead of the buffer, byte
|
||||
length, and encoding: `mjb_confusable_skeleton(text, output_encoding, result)`, and
|
||||
`mjb_confusable_skeleton_into(text, output)` also takes a `mjb_buffer *` instead of the output
|
||||
encoding, output buffer, and output size
|
||||
- **Breaking**: the `_into` functions now write in a single pass instead of measuring first, so a
|
||||
call with enough capacity runs about as fast as the allocating variant instead of nearly twice
|
||||
as slow. When capacity is insufficient, or another error occurs, the output buffer may now hold
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Test coverage
|
||||
|
||||
Mojibake runs a total of **4,624,791** C assertions and **161** JavaScript assertions, including all
|
||||
Mojibake runs a total of **4,624,794** C assertions and **162** JavaScript assertions, including all
|
||||
the official tests included in the standard:
|
||||
|
||||
1. [auxiliary/GraphemeBreakTest.txt](https://www.unicode.org/Public/18.0.0/ucd/auxiliary/GraphemeBreakTest.txt)
|
||||
@@ -47,12 +47,12 @@ the official tests included in the standard:
|
||||
| `mjb_convert_encoding` | 155 |
|
||||
| `mjb_collation_key` | 137 |
|
||||
| `mjb_codepoint_count` | 135 |
|
||||
| `mjb_confusable_skeleton_into` | 115 |
|
||||
| `mjb_confusable_skeleton_into` | 117 |
|
||||
| `mjb_codepoint_encode` | 106 |
|
||||
| `mjb_caseless_match` | 104 |
|
||||
| `mjb_resolved_script_set` | 97 |
|
||||
| `mjb_locale_parse` | 91 |
|
||||
| `mjb_confusable_skeleton` | 87 |
|
||||
| `mjb_confusable_skeleton` | 88 |
|
||||
| `mjb_terminal_width` | 80 |
|
||||
| `mjb_collation_key_into` | 76 |
|
||||
| `mjb_filter_into` | 73 |
|
||||
@@ -131,7 +131,7 @@ the official tests included in the standard:
|
||||
| `mjb_version` | 2 |
|
||||
| `mjb_version_number` | 2 |
|
||||
| `mjb_set_allocator` | 1 |
|
||||
| **Total** | **4624791** |
|
||||
| **Total** | **4624794** |
|
||||
|
||||
## JavaScript
|
||||
|
||||
@@ -156,6 +156,7 @@ the official tests included in the standard:
|
||||
| `Mojibake.normalizationQuickCheck` | 3 |
|
||||
| `Mojibake.sentenceCount` | 3 |
|
||||
| `Mojibake.codepointPropertyInt` | 2 |
|
||||
| `Mojibake.confusableSkeleton` | 2 |
|
||||
| `Mojibake.setLocale` | 2 |
|
||||
| `Mojibake.bidiResolve` | 1 |
|
||||
| `Mojibake.categoryIsCombining` | 1 |
|
||||
@@ -191,7 +192,6 @@ the official tests included in the standard:
|
||||
| `Mojibake.codepointPlane` | 1 |
|
||||
| `Mojibake.codepointScript` | 1 |
|
||||
| `Mojibake.codepointScriptExtensions` | 1 |
|
||||
| `Mojibake.confusableSkeleton` | 1 |
|
||||
| `Mojibake.create` | 1 |
|
||||
| `Mojibake.detectEncoding` | 1 |
|
||||
| `Mojibake.forEachCodepoint` | 1 |
|
||||
@@ -223,4 +223,4 @@ the official tests included in the standard:
|
||||
| `Mojibake.emojiSequenceInfo` | 0 |
|
||||
| `Mojibake.localeCode` | 0 |
|
||||
| `Mojibake.scriptCode` | 0 |
|
||||
| **Total** | **161** |
|
||||
| **Total** | **162** |
|
||||
|
||||
+6
-9
@@ -599,20 +599,17 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
}
|
||||
|
||||
case 12: { // Confusable skeleton and pairwise detection
|
||||
if(mjb_confusable_skeleton(buffer, size, encoding, MJB_ENC_UTF_8, &result) ==
|
||||
MJB_STATUS_OK) {
|
||||
mjb_text text = mjb_text_make(buffer, size, encoding);
|
||||
if(mjb_confusable_skeleton(&text, MJB_ENC_UTF_8, &result) == MJB_STATUS_OK) {
|
||||
mjb_result_free(&result);
|
||||
}
|
||||
|
||||
size_t required = 0;
|
||||
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
||||
|
||||
if(mjb_confusable_skeleton_into(buffer, size, encoding, MJB_ENC_UTF_8, NULL,
|
||||
&required) == MJB_STATUS_OK &&
|
||||
required <= 4096) {
|
||||
if(mjb_confusable_skeleton_into(&text, &into) == MJB_STATUS_OK && into.size <= 4096) {
|
||||
char output[4096];
|
||||
size_t capacity = required;
|
||||
fuzz_sink += (size_t)mjb_confusable_skeleton_into(buffer, size, encoding,
|
||||
MJB_ENC_UTF_8, output, &capacity);
|
||||
into.data = output;
|
||||
fuzz_sink += (size_t)mjb_confusable_skeleton_into(&text, &into);
|
||||
}
|
||||
|
||||
bool confusable;
|
||||
|
||||
+5
-4
@@ -1394,17 +1394,17 @@ export class Mojibake {
|
||||
return this.decodeString(ptr, null, Encoding.UTF_8).output;
|
||||
}
|
||||
|
||||
// mjb_status mjb_confusable_skeleton(const char *buffer, size_t byte_length,
|
||||
// mjb_encoding encoding, mjb_encoding output_encoding, mjb_result *result)
|
||||
// mjb_status mjb_confusable_skeleton(const mjb_text *text, mjb_encoding output_encoding,
|
||||
// mjb_result *result)
|
||||
confusableSkeleton(input: MojibakeInput, options: TextInputOptions = {}): Result | null {
|
||||
const wasmInput = this.copyInput(input, options.encoding);
|
||||
const textPtr = this.copyText(wasmInput);
|
||||
const outputEncoding = this.resolveEncoding(options.outputEncoding ?? wasmInput.encoding);
|
||||
const resultPtr = this.malloc(RESULT_SIZE);
|
||||
let result: RawResult | null = null;
|
||||
|
||||
try {
|
||||
const status = this.module._mjb_confusable_skeleton(wasmInput.ptr, wasmInput.size,
|
||||
wasmInput.encoding, outputEncoding, resultPtr);
|
||||
const status = this.module._mjb_confusable_skeleton(textPtr, outputEncoding, resultPtr);
|
||||
|
||||
if(status !== Status.OK) {
|
||||
return null;
|
||||
@@ -1418,6 +1418,7 @@ export class Mojibake {
|
||||
}
|
||||
|
||||
this.free(wasmInput.ptr);
|
||||
this.free(textPtr);
|
||||
this.free(resultPtr);
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -138,7 +138,7 @@ export type MojibakeWasmModule = {
|
||||
// Return the name of a property, NULL if the property specified is not valid.
|
||||
_mjb_property_name: (property: number) => Pointer;
|
||||
// Compute a Unicode confusable skeleton (Unicode 18.0.0 UTS #39 Section 4).
|
||||
_mjb_confusable_skeleton: (buffer: Pointer, byte_length: number, encoding: number, output_encoding: number, result: Pointer) => number;
|
||||
_mjb_confusable_skeleton: (text: Pointer, output_encoding: number, result: Pointer) => number;
|
||||
// Determine whether two strings are visually confusable (Unicode 18.0.0 UTS #39 Section 4): skeleton(s1) == skeleton(s2).
|
||||
_mjb_confusable_match: (s1: Pointer, s1_byte_length: number, s1_encoding: number, s2: Pointer, s2_byte_length: number, s2_encoding: number, confusable: Pointer) => number;
|
||||
// Return the emoji properties.
|
||||
|
||||
@@ -284,6 +284,8 @@ ATT_ASSERT(mojibake.confusableMatch(new Uint8Array([0x80]), 'A'), null,
|
||||
'areConfusable malformed input');
|
||||
ATT_ASSERT(mojibake.confusableSkeleton('h\u0435llo')?.output, 'hello',
|
||||
'confusableSkeleton');
|
||||
ATT_ASSERT(mojibake.confusableSkeleton('h\u0435llo', { encoding: Encoding.UTF_16BE,
|
||||
outputEncoding: Encoding.UTF_32LE })?.output, 'hello', 'confusableSkeleton UTF-16BE to UTF-32LE');
|
||||
ATT_ASSERT(mojibake.codepointEmojiProperties(0x23)?.component, true, 'codepointEmojiProperties');
|
||||
ATT_ASSERT(mojibake.codepointIsEmoji(0x23), true, 'codepointIsEmoji');
|
||||
ATT_ASSERT(mojibake.codepointIsEmojiPresentation(0x23), false, 'codepointIsEmojiPresentation');
|
||||
|
||||
@@ -766,8 +766,9 @@ struct NumericValue {
|
||||
[[nodiscard]] inline TextResult confusable_skeleton_result(std::string_view input,
|
||||
mjb_encoding input_encoding = MJB_ENC_UTF_8, mjb_encoding output_encoding = MJB_ENC_UTF_8) {
|
||||
TextResult result = detail::ResultAccess::create();
|
||||
const mjb_status status = mjb_confusable_skeleton(input.data(), input.size(), input_encoding,
|
||||
output_encoding, detail::ResultAccess::out(result));
|
||||
const mjb_text text = mjb_text_make(input.data(), input.size(), input_encoding);
|
||||
const mjb_status status = mjb_confusable_skeleton(&text, output_encoding,
|
||||
detail::ResultAccess::out(result));
|
||||
|
||||
return detail::ResultAccess::checked(std::move(result), status,
|
||||
"Confusable skeleton generation failed");
|
||||
|
||||
+2
-2
@@ -919,10 +919,10 @@ MJB_EXPORT MJB_NODISCARD mjb_status mjb_resolved_script_set(const char *buffer,
|
||||
MJB_EXPORT MJB_CONST const char *mjb_property_name(mjb_property property);
|
||||
|
||||
// Compute a Unicode confusable skeleton (Unicode 18.0.0 UTS #39 Section 4).
|
||||
MJB_EXPORT MJB_NODISCARD mjb_status mjb_confusable_skeleton(const char *buffer, size_t byte_length, mjb_encoding encoding, mjb_encoding output_encoding, mjb_result *result);
|
||||
MJB_EXPORT MJB_NODISCARD mjb_status mjb_confusable_skeleton(const mjb_text *text, mjb_encoding output_encoding, mjb_result *result);
|
||||
|
||||
// Compute a Unicode confusable skeleton into a caller-provided buffer.
|
||||
MJB_EXPORT MJB_NODISCARD mjb_status mjb_confusable_skeleton_into(const char *buffer, size_t byte_length, mjb_encoding encoding, mjb_encoding output_encoding, void *output, size_t *output_size);
|
||||
MJB_EXPORT MJB_NODISCARD mjb_status mjb_confusable_skeleton_into(const mjb_text *text, mjb_buffer *output);
|
||||
|
||||
// Determine whether two strings are visually confusable (Unicode 18.0.0 UTS #39 Section 4): skeleton(s1) == skeleton(s2).
|
||||
MJB_EXPORT MJB_NODISCARD mjb_status mjb_confusable_match(const char *s1, size_t s1_byte_length, mjb_encoding s1_encoding, const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding, bool *confusable);
|
||||
|
||||
+21
-28
@@ -461,9 +461,9 @@ static mjb_status mjb_confusable_skeleton_process(const char *buffer, size_t byt
|
||||
}
|
||||
|
||||
// Compute the Unicode security skeleton of a string (UTS #39 Section 4).
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton(const char *buffer, size_t byte_length,
|
||||
mjb_encoding encoding, mjb_encoding output_encoding, mjb_result *result) {
|
||||
if(result == NULL || (buffer == NULL && byte_length > 0)) {
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton(const mjb_text *text, mjb_encoding output_encoding,
|
||||
mjb_result *result) {
|
||||
if(result == NULL || text == NULL || (text->buffer == NULL && text->byte_length > 0)) {
|
||||
return MJB_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
@@ -471,27 +471,26 @@ MJB_EXPORT mjb_status mjb_confusable_skeleton(const char *buffer, size_t byte_le
|
||||
result->output_size = 0;
|
||||
result->owned = false;
|
||||
|
||||
return mjb_confusable_skeleton_process(buffer, byte_length, encoding, output_encoding, result,
|
||||
NULL, NULL);
|
||||
return mjb_confusable_skeleton_process(text->buffer, text->byte_length, text->encoding,
|
||||
output_encoding, result, NULL, NULL);
|
||||
}
|
||||
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton_into(const char *buffer, size_t byte_length,
|
||||
mjb_encoding encoding, mjb_encoding output_encoding, void *output, size_t *output_size) {
|
||||
if(output_size == NULL) {
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton_into(const mjb_text *text, mjb_buffer *output) {
|
||||
if(output == NULL) {
|
||||
return MJB_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if(buffer == NULL && byte_length > 0) {
|
||||
*output_size = 0;
|
||||
if(text == NULL || (text->buffer == NULL && text->byte_length > 0)) {
|
||||
output->size = 0;
|
||||
|
||||
return MJB_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
mjb_status status = mjb_confusable_skeleton_process(buffer, byte_length, encoding,
|
||||
output_encoding, NULL, output, output_size);
|
||||
mjb_status status = mjb_confusable_skeleton_process(text->buffer, text->byte_length,
|
||||
text->encoding, output->encoding, NULL, output->data, &output->size);
|
||||
|
||||
if(status != MJB_STATUS_OK && status != MJB_STATUS_OUTPUT_TOO_SMALL) {
|
||||
*output_size = 0;
|
||||
output->size = 0;
|
||||
}
|
||||
|
||||
return status;
|
||||
@@ -512,16 +511,17 @@ MJB_EXPORT mjb_status mjb_confusable_match(const char *s1, size_t s1_byte_length
|
||||
return MJB_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
mjb_text text1 = mjb_text_make(s1, s1_byte_length, s1_encoding);
|
||||
mjb_text text2 = mjb_text_make(s2, s2_byte_length, s2_encoding);
|
||||
mjb_result skel1;
|
||||
mjb_result skel2;
|
||||
mjb_status status = mjb_confusable_skeleton(s1, s1_byte_length, s1_encoding, MJB_ENC_UTF_8,
|
||||
&skel1);
|
||||
mjb_status status = mjb_confusable_skeleton(&text1, MJB_ENC_UTF_8, &skel1);
|
||||
|
||||
if(status != MJB_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = mjb_confusable_skeleton(s2, s2_byte_length, s2_encoding, MJB_ENC_UTF_8, &skel2);
|
||||
status = mjb_confusable_skeleton(&text2, MJB_ENC_UTF_8, &skel2);
|
||||
|
||||
if(status != MJB_STATUS_OK) {
|
||||
if(skel1.owned) {
|
||||
@@ -558,24 +558,17 @@ MJB_EXPORT mjb_status mjb_resolved_script_set(const char *buffer, size_t byte_le
|
||||
return MJB_STATUS_FEATURE_NOT_ENABLED;
|
||||
}
|
||||
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton(const char *buffer, size_t byte_length,
|
||||
mjb_encoding encoding, mjb_encoding output_encoding, mjb_result *result) {
|
||||
(void)buffer;
|
||||
(void)byte_length;
|
||||
(void)encoding;
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton(const mjb_text *text, mjb_encoding output_encoding,
|
||||
mjb_result *result) {
|
||||
(void)text;
|
||||
(void)output_encoding;
|
||||
(void)result;
|
||||
return MJB_STATUS_FEATURE_NOT_ENABLED;
|
||||
}
|
||||
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton_into(const char *buffer, size_t byte_length,
|
||||
mjb_encoding encoding, mjb_encoding output_encoding, void *output, size_t *output_size) {
|
||||
(void)buffer;
|
||||
(void)byte_length;
|
||||
(void)encoding;
|
||||
(void)output_encoding;
|
||||
MJB_EXPORT mjb_status mjb_confusable_skeleton_into(const mjb_text *text, mjb_buffer *output) {
|
||||
(void)text;
|
||||
(void)output;
|
||||
(void)output_size;
|
||||
return MJB_STATUS_FEATURE_NOT_ENABLED;
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -286,19 +286,19 @@ static mjb_status sweep_idna_to_unicode_into(bool *cleared) {
|
||||
#if MJB_FEATURE_SECURITY
|
||||
static mjb_status sweep_confusable_skeleton(bool *cleared) {
|
||||
mjb_result result = { NULL, 0, false };
|
||||
mjb_status status = mjb_confusable_skeleton(sweep_text, sweep_text_size, MJB_ENC_UTF_8,
|
||||
MJB_ENC_UTF_8, &result);
|
||||
mjb_text text = mjb_text_utf8(sweep_text, sweep_text_size);
|
||||
mjb_status status = mjb_confusable_skeleton(&text, MJB_ENC_UTF_8, &result);
|
||||
*cleared = result_done(status, &result);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static mjb_status sweep_confusable_skeleton_into(bool *cleared) {
|
||||
char output[SWEEP_OUTPUT_SIZE];
|
||||
size_t output_size = sizeof(output);
|
||||
mjb_status status = mjb_confusable_skeleton_into(sweep_text, sweep_text_size, MJB_ENC_UTF_8,
|
||||
MJB_ENC_UTF_16LE, output, &output_size);
|
||||
*cleared = into_done(status, output_size);
|
||||
char storage[SWEEP_OUTPUT_SIZE];
|
||||
mjb_text text = mjb_text_utf8(sweep_text, sweep_text_size);
|
||||
mjb_buffer output = mjb_buffer_make(storage, sizeof(storage), MJB_ENC_UTF_16LE);
|
||||
mjb_status status = mjb_confusable_skeleton_into(&text, &output);
|
||||
*cleared = into_done(status, output.size);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
+19
-12
@@ -1179,11 +1179,11 @@ ATT_TEST(example) {
|
||||
{
|
||||
// Example for mjb_confusable_skeleton
|
||||
MJB_TEST_COVERAGE(mjb_confusable_skeleton); // Added by the script
|
||||
const char *input = "h\xD0\xB5llo"; // Cyrillic U+0435 in place of e
|
||||
// Cyrillic U+0435 in place of e
|
||||
mjb_text input = mjb_text_utf8("h\xD0\xB5llo", MJB_NUL_TERMINATED);
|
||||
mjb_result result;
|
||||
|
||||
if(mjb_confusable_skeleton(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
||||
&result) != MJB_STATUS_OK) {
|
||||
if(mjb_confusable_skeleton(&input, MJB_ENC_UTF_8, &result) != MJB_STATUS_OK) {
|
||||
ATT_ASSERT(0, 1, "mjb_confusable_skeleton test failed"); // Added by the script
|
||||
return;
|
||||
}
|
||||
@@ -1200,26 +1200,33 @@ ATT_TEST(example) {
|
||||
{
|
||||
// Example for mjb_confusable_skeleton_into
|
||||
MJB_TEST_COVERAGE(mjb_confusable_skeleton_into); // Added by the script
|
||||
const char *input = "h\xD0\xB5llo"; // Cyrillic U+0435 in place of e
|
||||
size_t output_size = 0;
|
||||
// Cyrillic U+0435 in place of e
|
||||
mjb_text input = mjb_text_utf8("h\xD0\xB5llo", MJB_NUL_TERMINATED);
|
||||
mjb_buffer output = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
||||
|
||||
if(mjb_confusable_skeleton_into(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
||||
NULL, &output_size) != MJB_STATUS_OK) {
|
||||
// Query the required size
|
||||
if(mjb_confusable_skeleton_into(&input, &output) != MJB_STATUS_OK) {
|
||||
ATT_ASSERT(0, 1, "mjb_confusable_skeleton_into test failed"); // Added by the script
|
||||
return;
|
||||
}
|
||||
|
||||
char output[5];
|
||||
char storage[5];
|
||||
|
||||
if(output_size > sizeof(output) || mjb_confusable_skeleton_into(input, strlen(input),
|
||||
MJB_ENC_UTF_8, MJB_ENC_UTF_8, output, &output_size) != MJB_STATUS_OK) {
|
||||
if(output.size > sizeof(storage)) {
|
||||
ATT_ASSERT(0, 1, "mjb_confusable_skeleton_into test failed"); // Added by the script
|
||||
return;
|
||||
}
|
||||
|
||||
output = mjb_buffer_make(storage, sizeof(storage), MJB_ENC_UTF_8);
|
||||
|
||||
if(mjb_confusable_skeleton_into(&input, &output) != MJB_STATUS_OK) {
|
||||
ATT_ASSERT(0, 1, "mjb_confusable_skeleton_into test failed"); // Added by the script
|
||||
return;
|
||||
}
|
||||
|
||||
// Skeleton payload (no terminator): hello
|
||||
// printf("Skeleton payload (no terminator): %.*s", (int)output_size, output);
|
||||
snprintf(test_buffer, sizeof(test_buffer), "Skeleton payload (no terminator): %.*s", (int)output_size, output); // Added by the script
|
||||
// printf("Skeleton payload (no terminator): %.*s", (int)output.size, storage);
|
||||
snprintf(test_buffer, sizeof(test_buffer), "Skeleton payload (no terminator): %.*s", (int)output.size, storage); // Added by the script
|
||||
ATT_ASSERT(test_buffer, "Skeleton payload (no terminator): hello", "mjb_confusable_skeleton_into test failed"); // Added by the script
|
||||
}
|
||||
#endif // MJB_FEATURE_SECURITY
|
||||
|
||||
+67
-58
@@ -150,30 +150,27 @@ static void check_skeleton(const char *input, size_t input_size, const char *exp
|
||||
mjb_result skeleton;
|
||||
|
||||
MJB_TEST_COVERAGE(mjb_confusable_skeleton);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(input, input_size, MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
||||
mjb_text text = mjb_text_utf8(input, input_size);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, MJB_ENC_UTF_8,
|
||||
&skeleton), MJB_STATUS_OK, name);
|
||||
ATT_ASSERT(skeleton.output_size, expected_size, name);
|
||||
ATT_ASSERT((int)memcmp(skeleton.output, expected, expected_size), 0, name);
|
||||
|
||||
size_t required = 0;
|
||||
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
||||
MJB_TEST_COVERAGE(mjb_confusable_skeleton_into);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(input, input_size, MJB_ENC_UTF_8,
|
||||
MJB_ENC_UTF_8, NULL, &required),
|
||||
MJB_STATUS_OK, name);
|
||||
ATT_ASSERT(required, expected_size, name);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK, name);
|
||||
ATT_ASSERT(into.size, expected_size, name);
|
||||
|
||||
char output[128];
|
||||
ATT_ASSERT((int)(required < sizeof(output)), true, name);
|
||||
ATT_ASSERT((int)(into.size < sizeof(output)), true, name);
|
||||
|
||||
if(required < sizeof(output)) {
|
||||
if(into.size < sizeof(output)) {
|
||||
memset(output, '#', sizeof(output));
|
||||
size_t output_size = required;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(input, input_size, MJB_ENC_UTF_8,
|
||||
MJB_ENC_UTF_8, output, &output_size),
|
||||
MJB_STATUS_OK, name);
|
||||
ATT_ASSERT(output_size, expected_size, name);
|
||||
into = mjb_buffer_make(output, into.size, MJB_ENC_UTF_8);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK, name);
|
||||
ATT_ASSERT(into.size, expected_size, name);
|
||||
ATT_ASSERT((int)memcmp(output, expected, expected_size), 0, name);
|
||||
ATT_ASSERT(output[output_size], '#', name);
|
||||
ATT_ASSERT(output[into.size], '#', name);
|
||||
}
|
||||
|
||||
ATT_ASSERT_STATUS(mjb_result_free(&skeleton), MJB_STATUS_OK, name);
|
||||
@@ -372,43 +369,55 @@ ATT_TEST(security) {
|
||||
"Resolved scripts leave a small output buffer untouched");
|
||||
|
||||
mjb_result skeleton;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(NULL, 1, enc, enc, &skeleton),
|
||||
mjb_text text = mjb_text_make(NULL, 1, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, &skeleton),
|
||||
MJB_STATUS_INVALID_ARGUMENT, "Skeleton rejects NULL input");
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton("A", 1, enc, enc, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||||
text = mjb_text_make("A", 1, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||||
"Skeleton rejects NULL result");
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton("A", 1, MJB_ENC_UNKNOWN, enc, &skeleton),
|
||||
text = mjb_text_make("A", 1, MJB_ENC_UNKNOWN);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, &skeleton),
|
||||
MJB_STATUS_INVALID_ENCODING, "Skeleton rejects invalid input encoding");
|
||||
|
||||
size_t into_size = 9;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(NULL, 1, enc, enc, NULL, &into_size),
|
||||
MJB_STATUS_INVALID_ARGUMENT, "Skeleton into rejects NULL input");
|
||||
ATT_ASSERT(into_size, (size_t)0, "Skeleton into clears size after invalid input");
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into("A", 1, enc, enc, NULL, NULL),
|
||||
MJB_STATUS_INVALID_ARGUMENT, "Skeleton into rejects NULL size");
|
||||
text = mjb_text_make(NULL, 1, enc);
|
||||
mjb_buffer into = mjb_buffer_make(NULL, 9, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_INVALID_ARGUMENT,
|
||||
"Skeleton into rejects NULL input");
|
||||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into clears size after invalid input");
|
||||
into = mjb_buffer_make(NULL, 9, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(NULL, &into), MJB_STATUS_INVALID_ARGUMENT,
|
||||
"Skeleton into rejects NULL text");
|
||||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into clears size after NULL text");
|
||||
text = mjb_text_make("A", 1, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||||
"Skeleton into rejects NULL output");
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(NULL, enc, &skeleton), MJB_STATUS_INVALID_ARGUMENT,
|
||||
"Skeleton rejects NULL text");
|
||||
|
||||
into_size = 9;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into("", 0, enc, enc, NULL, &into_size),
|
||||
MJB_STATUS_OK, "Skeleton into measures empty input");
|
||||
ATT_ASSERT(into_size, (size_t)0, "Skeleton into empty size");
|
||||
text = mjb_text_make("", 0, enc);
|
||||
into = mjb_buffer_make(NULL, 9, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||||
"Skeleton into measures empty input");
|
||||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into empty size");
|
||||
|
||||
const char *into_input = "h\xD0\xB5llo";
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(into_input, 6, enc, enc, NULL, &into_size),
|
||||
MJB_STATUS_OK, "Skeleton into queries required size");
|
||||
ATT_ASSERT(into_size, (size_t)5, "Skeleton into required size");
|
||||
text = mjb_text_make(into_input, 6, enc);
|
||||
into = mjb_buffer_make(NULL, 0, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||||
"Skeleton into queries required size");
|
||||
ATT_ASSERT(into.size, (size_t)5, "Skeleton into required size");
|
||||
|
||||
char into_output[6] = { '#', '#', '#', '#', '#', '#' };
|
||||
into_size = 4;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(into_input, 6, enc, enc, into_output,
|
||||
&into_size),
|
||||
MJB_STATUS_OUTPUT_TOO_SMALL, "Skeleton into reports a small output buffer");
|
||||
ATT_ASSERT(into_size, (size_t)5, "Skeleton into preserves required size");
|
||||
ATT_ASSERT((int)memcmp(into_output, "######", sizeof(into_output)), 0,
|
||||
"Skeleton into leaves a small buffer untouched");
|
||||
into = mjb_buffer_make(into_output, 4, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OUTPUT_TOO_SMALL,
|
||||
"Skeleton into reports a small output buffer");
|
||||
ATT_ASSERT(into.size, (size_t)5, "Skeleton into preserves required size");
|
||||
ATT_ASSERT((int)memcmp(into_output + 4, "##", 2), 0,
|
||||
"Skeleton into is not written past its capacity");
|
||||
|
||||
into_size = 5;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(into_input, 6, enc, enc, into_output,
|
||||
&into_size),
|
||||
MJB_STATUS_OK, "Skeleton into writes into exact capacity");
|
||||
into = mjb_buffer_make(into_output, 5, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||||
"Skeleton into writes into exact capacity");
|
||||
ATT_ASSERT((int)memcmp(into_output, "hello", 5), 0, "Skeleton into output");
|
||||
ATT_ASSERT(into_output[5], '#', "Skeleton into does not write a terminator");
|
||||
|
||||
@@ -423,18 +432,19 @@ ATT_TEST(security) {
|
||||
"\xD9\x88\xD8\xB3\xD9\x84\xD9\x85",
|
||||
30, "Skeleton preserves full 18-codepoint expansion");
|
||||
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton("A", 1, enc, MJB_ENC_UTF_16LE, &skeleton),
|
||||
text = mjb_text_make("A", 1, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, MJB_ENC_UTF_16LE, &skeleton),
|
||||
MJB_STATUS_OK, "Skeleton supports UTF-16 output");
|
||||
ATT_ASSERT(skeleton.output_size, (size_t)2, "Skeleton UTF-16 output size");
|
||||
ATT_ASSERT((int)memcmp(skeleton.output, "A\0", 2), 0, "Skeleton UTF-16 output");
|
||||
ATT_ASSERT_STATUS(mjb_result_free(&skeleton), MJB_STATUS_OK, "Free UTF-16 skeleton");
|
||||
|
||||
char utf16_output[3] = { '#', '#', '#' };
|
||||
into_size = sizeof(utf16_output) - 1;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into("A", 1, enc, MJB_ENC_UTF_16LE, utf16_output,
|
||||
&into_size),
|
||||
MJB_STATUS_OK, "Skeleton into supports UTF-16 output");
|
||||
ATT_ASSERT(into_size, (size_t)2, "Skeleton into UTF-16 output size");
|
||||
text = mjb_text_make("A", 1, enc);
|
||||
into = mjb_buffer_make(utf16_output, sizeof(utf16_output) - 1, MJB_ENC_UTF_16LE);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||||
"Skeleton into supports UTF-16 output");
|
||||
ATT_ASSERT(into.size, (size_t)2, "Skeleton into UTF-16 output size");
|
||||
ATT_ASSERT((int)memcmp(utf16_output, "A\0", 2), 0, "Skeleton into UTF-16 output");
|
||||
ATT_ASSERT(utf16_output[2], '#', "Skeleton into UTF-16 output has no terminator");
|
||||
|
||||
@@ -447,14 +457,13 @@ ATT_TEST(security) {
|
||||
|
||||
const unsigned char malformed_utf8[] = { 0x80 };
|
||||
const char *malformed_utf8_bytes = (const char *)malformed_utf8;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(
|
||||
malformed_utf8_bytes, sizeof(malformed_utf8), enc, enc, &skeleton),
|
||||
text = mjb_text_make(malformed_utf8_bytes, sizeof(malformed_utf8), enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, &skeleton),
|
||||
MJB_STATUS_MALFORMED_INPUT, "Skeleton reports malformed input");
|
||||
into_size = 9;
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(malformed_utf8_bytes,
|
||||
sizeof(malformed_utf8), enc, enc, NULL, &into_size),
|
||||
MJB_STATUS_MALFORMED_INPUT, "Skeleton into reports malformed input");
|
||||
ATT_ASSERT(into_size, (size_t)0, "Skeleton into clears size after malformed input");
|
||||
into = mjb_buffer_make(NULL, 9, enc);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_MALFORMED_INPUT,
|
||||
"Skeleton into reports malformed input");
|
||||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into clears size after malformed input");
|
||||
ATT_ASSERT_STATUS(mjb_confusable_match(malformed_utf8_bytes, sizeof(malformed_utf8), enc, "A", 1,
|
||||
enc, &confusable),
|
||||
MJB_STATUS_MALFORMED_INPUT, "Confusable reports malformed input");
|
||||
@@ -547,18 +556,18 @@ ATT_TEST(security) {
|
||||
mjb_script_set_kind kind = MJB_SCRIPT_SET_EMPTY;
|
||||
mjb_result result = { NULL, 0, false };
|
||||
unsigned char output[16] = { 0 };
|
||||
size_t output_size = sizeof(output);
|
||||
mjb_buffer into = mjb_buffer_make(output, sizeof(output), MJB_ENC_UTF_8);
|
||||
bool confusable = false;
|
||||
|
||||
MJB_TEST_COVERAGE(mjb_resolved_script_set);
|
||||
ATT_ASSERT_STATUS(mjb_resolved_script_set("a", 1, MJB_ENC_UTF_8, &script, &count, &kind),
|
||||
MJB_STATUS_FEATURE_NOT_ENABLED, "Disabled resolved scripts report feature status");
|
||||
MJB_TEST_COVERAGE(mjb_confusable_skeleton);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton("a", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_8, &result),
|
||||
mjb_text text = mjb_text_utf8("a", 1);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, MJB_ENC_UTF_8, &result),
|
||||
MJB_STATUS_FEATURE_NOT_ENABLED, "Disabled skeleton reports feature status");
|
||||
MJB_TEST_COVERAGE(mjb_confusable_skeleton_into);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into("a", 1, MJB_ENC_UTF_8, MJB_ENC_UTF_8, output,
|
||||
&output_size),
|
||||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into),
|
||||
MJB_STATUS_FEATURE_NOT_ENABLED, "Disabled skeleton into reports feature status");
|
||||
MJB_TEST_COVERAGE(mjb_confusable_match);
|
||||
ATT_ASSERT_STATUS(mjb_confusable_match("a", 1, MJB_ENC_UTF_8, "b", 1, MJB_ENC_UTF_8,
|
||||
|
||||
+28
-37
@@ -2879,9 +2879,7 @@ printf("Property: %s", name);`,
|
||||
name: 'mjb_confusable_skeleton',
|
||||
attributes: ['MJB_NODISCARD'],
|
||||
args: [
|
||||
buffer('The string to transform'),
|
||||
byte_length(),
|
||||
encoding(),
|
||||
text('The text to transform'),
|
||||
encoding('The output encoding of the skeleton', 'output_encoding'),
|
||||
result()
|
||||
],
|
||||
@@ -2895,7 +2893,8 @@ printf("Property: %s", name);`,
|
||||
'always returns `MJB_STATUS_FEATURE_NOT_ENABLED`.',
|
||||
returns: [
|
||||
{ value: 'MJB_STATUS_OK', description: 'The confusable skeleton was returned' },
|
||||
{ value: 'MJB_STATUS_INVALID_ARGUMENT', description: '`result` is NULL, or `buffer` is NULL with a non-zero size' },
|
||||
{ value: 'MJB_STATUS_INVALID_ARGUMENT', description:
|
||||
'`result` or `text` is NULL, or `text->buffer` is NULL with a non-zero size' },
|
||||
{ value: 'MJB_STATUS_INVALID_ENCODING', description: 'An encoding is invalid' },
|
||||
{ value: 'MJB_STATUS_UNSUPPORTED', description:
|
||||
'The output encoding cannot represent a skeleton codepoint' },
|
||||
@@ -2904,11 +2903,11 @@ printf("Property: %s", name);`,
|
||||
{ value: 'MJB_STATUS_FEATURE_NOT_ENABLED', description:
|
||||
'The library was compiled with `MJB_FEATURE_SECURITY=0`' }
|
||||
],
|
||||
example: `const char *input = "h\\xD0\\xB5llo"; // Cyrillic U+0435 in place of e
|
||||
example: `// Cyrillic U+0435 in place of e
|
||||
mjb_text input = mjb_text_utf8("h\\xD0\\xB5llo", MJB_NUL_TERMINATED);
|
||||
mjb_result result;
|
||||
|
||||
if(mjb_confusable_skeleton(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
||||
&result) != MJB_STATUS_OK) {
|
||||
if(mjb_confusable_skeleton(&input, MJB_ENC_UTF_8, &result) != MJB_STATUS_OK) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -2924,32 +2923,18 @@ mjb_result_free(&result);`,
|
||||
name: 'mjb_confusable_skeleton_into',
|
||||
attributes: ['MJB_NODISCARD'],
|
||||
args: [
|
||||
buffer('The string to transform'),
|
||||
byte_length(),
|
||||
encoding(),
|
||||
encoding('The output encoding of the skeleton', 'output_encoding'),
|
||||
{
|
||||
name: 'output',
|
||||
type: 'void *',
|
||||
description: 'The caller-provided output buffer, or NULL to query the required size',
|
||||
wasm_generated: false,
|
||||
ownership: 'The caller retains ownership'
|
||||
},
|
||||
{
|
||||
name: 'output_size',
|
||||
type: 'size_t *',
|
||||
description: 'The input capacity and output required or written byte count',
|
||||
wasm_generated: false
|
||||
}
|
||||
text('The text to transform'),
|
||||
outputBuffer()
|
||||
],
|
||||
wasm: false,
|
||||
section: Section.Security,
|
||||
exampleFeature: 'MJB_FEATURE_SECURITY',
|
||||
details: 'Compute the same UTS #39 `bidiSkeleton(LTR, input)` as ' +
|
||||
'`mjb_confusable_skeleton` without allocating the final output buffer. Set `output` to ' +
|
||||
'NULL to query the required size. If `output` is non-NULL, `*output_size` supplies its ' +
|
||||
'capacity; on return it contains the required size when the buffer is too small, or the ' +
|
||||
'written size on success. Terminators are excluded and are not written. If capacity is ' +
|
||||
'`mjb_confusable_skeleton` without allocating the final output buffer. ' +
|
||||
'`output->encoding` selects the output encoding. Set `output->data` to NULL to query the ' +
|
||||
'required size. If `output->data` is non-NULL, `output->size` supplies its capacity; on ' +
|
||||
'return it contains the required size when the buffer is too small, or the written size ' +
|
||||
'on success. Terminators are excluded and are not written. If capacity is ' +
|
||||
'insufficient, or another error occurs, the output buffer may hold a partial prefix of ' +
|
||||
'the result, but nothing is written past its capacity. Bidirectional resolution, ' +
|
||||
'normalization, and ' +
|
||||
@@ -2960,7 +2945,7 @@ mjb_result_free(&result);`,
|
||||
{ value: 'MJB_STATUS_OK', description:
|
||||
'The required size was returned or the skeleton was written' },
|
||||
{ value: 'MJB_STATUS_INVALID_ARGUMENT', description:
|
||||
'`output_size` is NULL, or `buffer` is NULL with a non-zero size' },
|
||||
'`output` or `text` is NULL, or `text->buffer` is NULL with a non-zero size' },
|
||||
{ value: 'MJB_STATUS_INVALID_ENCODING', description: 'An encoding is invalid' },
|
||||
{ value: 'MJB_STATUS_MALFORMED_INPUT', description:
|
||||
'The input contains an ill-formed code-unit sequence' },
|
||||
@@ -2973,23 +2958,29 @@ mjb_result_free(&result);`,
|
||||
{ value: 'MJB_STATUS_FEATURE_NOT_ENABLED', description:
|
||||
'The library was compiled with `MJB_FEATURE_SECURITY=0`' }
|
||||
],
|
||||
example: `const char *input = "h\\xD0\\xB5llo"; // Cyrillic U+0435 in place of e
|
||||
size_t output_size = 0;
|
||||
example: `// Cyrillic U+0435 in place of e
|
||||
mjb_text input = mjb_text_utf8("h\\xD0\\xB5llo", MJB_NUL_TERMINATED);
|
||||
mjb_buffer output = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
||||
|
||||
if(mjb_confusable_skeleton_into(input, strlen(input), MJB_ENC_UTF_8, MJB_ENC_UTF_8,
|
||||
NULL, &output_size) != MJB_STATUS_OK) {
|
||||
// Query the required size
|
||||
if(mjb_confusable_skeleton_into(&input, &output) != MJB_STATUS_OK) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
char output[5];
|
||||
char storage[5];
|
||||
|
||||
if(output_size > sizeof(output) || mjb_confusable_skeleton_into(input, strlen(input),
|
||||
MJB_ENC_UTF_8, MJB_ENC_UTF_8, output, &output_size) != MJB_STATUS_OK) {
|
||||
if(output.size > sizeof(storage)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
output = mjb_buffer_make(storage, sizeof(storage), MJB_ENC_UTF_8);
|
||||
|
||||
if(mjb_confusable_skeleton_into(&input, &output) != MJB_STATUS_OK) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Skeleton payload (no terminator): hello
|
||||
printf("Skeleton payload (no terminator): %.*s", (int)output_size, output);`,
|
||||
printf("Skeleton payload (no terminator): %.*s", (int)output.size, storage);`,
|
||||
related: ['mjb_confusable_skeleton', 'mjb_confusable_match', 'mjb_is_identifier'],
|
||||
specs: [uts(39, 'Unicode Security Mechanisms')]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user