Asterisk - The Open Source Telephony Project
18.5.0
|
UTF-8 information and validation functions. More...
#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/utf8.h"
#include "asterisk/test.h"
Go to the source code of this file.
Data Structures | |
struct | ast_utf8_validator |
Macros | |
#define | UTF8_ACCEPT 0 |
#define | UTF8_REJECT 12 |
Functions | |
AST_TEST_DEFINE (test_utf8_is_valid) | |
AST_TEST_DEFINE (test_utf8_copy_string) | |
AST_TEST_DEFINE (test_utf8_validator) | |
void | ast_utf8_copy_string (char *dst, const char *src, size_t size) |
Copy a string safely ensuring valid UTF-8. More... | |
int | ast_utf8_init (void) |
Register UTF-8 tests. More... | |
int | ast_utf8_is_valid (const char *src) |
Check if a zero-terminated string is valid UTF-8. More... | |
int | ast_utf8_is_validn (const char *src, size_t size) |
Check if the first size bytes of a string are valid UTF-8. More... | |
void | ast_utf8_validator_destroy (struct ast_utf8_validator *validator) |
Destroy a UTF-8 validator. More... | |
enum ast_utf8_validation_result | ast_utf8_validator_feed (struct ast_utf8_validator *validator, const char *data) |
Feed a zero-terminated string into the UTF-8 validator. More... | |
enum ast_utf8_validation_result | ast_utf8_validator_feedn (struct ast_utf8_validator *validator, const char *data, size_t size) |
Feed a string into the UTF-8 validator. More... | |
int | ast_utf8_validator_new (struct ast_utf8_validator **validator) |
Create a new UTF-8 validator. More... | |
void | ast_utf8_validator_reset (struct ast_utf8_validator *validator) |
Reset the state of a UTF-8 validator. More... | |
enum ast_utf8_validation_result | ast_utf8_validator_state (struct ast_utf8_validator *validator) |
Get the current UTF-8 validator state. More... | |
static uint32_t | decode (uint32_t *state, uint32_t byte) |
static int | test_copy_and_compare (const char *src, size_t dst_len, const char *cmp) |
static void | test_utf8_shutdown (void) |
Variables | |
static const uint8_t | utf8d [] |
UTF-8 information and validation functions.
Definition in file utf8.c.
#define UTF8_ACCEPT 0 |
Definition at line 60 of file utf8.c.
Referenced by ast_utf8_copy_string(), ast_utf8_is_valid(), ast_utf8_is_validn(), ast_utf8_validator_new(), ast_utf8_validator_reset(), and ast_utf8_validator_state().
#define UTF8_REJECT 12 |
Definition at line 61 of file utf8.c.
Referenced by ast_utf8_copy_string(), and ast_utf8_validator_state().
AST_TEST_DEFINE | ( | test_utf8_is_valid | ) |
Definition at line 222 of file utf8.c.
References AST_TEST_NOT_RUN, AST_TEST_PASS, ast_utf8_is_valid(), ast_utf8_is_validn(), sip_to_pjsip::info(), TEST_EXECUTE, and TEST_INIT.
AST_TEST_DEFINE | ( | test_utf8_copy_string | ) |
Definition at line 287 of file utf8.c.
References AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), test_copy_and_compare(), TEST_EXECUTE, and TEST_INIT.
AST_TEST_DEFINE | ( | test_utf8_validator | ) |
Definition at line 316 of file utf8.c.
References AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, AST_UTF8_INVALID, AST_UTF8_UNKNOWN, AST_UTF8_VALID, ast_utf8_validator_destroy(), ast_utf8_validator_feed(), ast_utf8_validator_new(), sip_to_pjsip::info(), TEST_EXECUTE, and TEST_INIT.
void ast_utf8_copy_string | ( | char * | dst, |
const char * | src, | ||
size_t | size | ||
) |
Copy a string safely ensuring valid UTF-8.
This is similar to ast_copy_string, but it will only copy valid UTF-8 sequences from the source string into the destination buffer. If an invalid UTF-8 sequence is encountered, or the available space in the destination buffer is exhausted in the middle of an otherwise valid UTF-8 sequence, the destination buffer will be truncated to ensure that it only contains valid UTF-8.
dst | The destination buffer. |
src | The source string |
size | The size of the destination buffer |
Definition at line 133 of file utf8.c.
References ast_assert, decode(), UTF8_ACCEPT, and UTF8_REJECT.
Referenced by test_copy_and_compare().
int ast_utf8_init | ( | void | ) |
Register UTF-8 tests.
Does nothing unless TEST_FRAMEWORK is defined.
Definition at line 362 of file utf8.c.
References ast_register_cleanup(), AST_TEST_REGISTER, and test_utf8_shutdown().
Referenced by asterisk_daemon().
int ast_utf8_is_valid | ( | const char * | str | ) |
Check if a zero-terminated string is valid UTF-8.
str | The zero-terminated string to check |
0 | if the string is not valid UTF-8 |
Non-zero | if the string is valid UTF-8 |
Definition at line 110 of file utf8.c.
References decode(), and UTF8_ACCEPT.
Referenced by AST_TEST_DEFINE().
int ast_utf8_is_validn | ( | const char * | str, |
size_t | size | ||
) |
Check if the first size bytes of a string are valid UTF-8.
Similar to ast_utf8_is_valid() but checks the first size bytes or until a zero byte is reached, whichever comes first.
str | The string to check |
size | The number of bytes to evaluate |
0 | if the string is not valid UTF-8 |
Non-zero | if the string is valid UTF-8 |
Definition at line 121 of file utf8.c.
References decode(), and UTF8_ACCEPT.
Referenced by AST_TEST_DEFINE().
void ast_utf8_validator_destroy | ( | struct ast_utf8_validator * | validator | ) |
Destroy a UTF-8 validator.
validator | The validator instance to destroy |
Definition at line 215 of file utf8.c.
References ast_free.
Referenced by AST_TEST_DEFINE().
enum ast_utf8_validation_result ast_utf8_validator_feed | ( | struct ast_utf8_validator * | validator, |
const char * | data | ||
) |
Feed a zero-terminated string into the UTF-8 validator.
validator | The validator instance |
data | The zero-terminated string to feed into the validator |
Definition at line 189 of file utf8.c.
References ast_utf8_validator_state(), decode(), and ast_utf8_validator::state.
Referenced by AST_TEST_DEFINE().
enum ast_utf8_validation_result ast_utf8_validator_feedn | ( | struct ast_utf8_validator * | validator, |
const char * | data, | ||
size_t | size | ||
) |
Feed a string into the UTF-8 validator.
Similar to ast_utf8_validator_feed but will stop feeding in data if a zero byte is encountered or size bytes have been read.
validator | The validator instance |
data | The string to feed into the validator |
size | The number of bytes to feed into the validator |
Definition at line 199 of file utf8.c.
References ast_utf8_validator_state(), decode(), and ast_utf8_validator::state.
int ast_utf8_validator_new | ( | struct ast_utf8_validator ** | validator | ) |
Create a new UTF-8 validator.
[out] | validator | The validator instance |
0 | on success |
-1 | on failure |
Definition at line 163 of file utf8.c.
References ast_malloc, ast_utf8_validator::state, tmp(), and UTF8_ACCEPT.
Referenced by AST_TEST_DEFINE().
void ast_utf8_validator_reset | ( | struct ast_utf8_validator * | validator | ) |
Reset the state of a UTF-8 validator.
Resets the provided UTF-8 validator to its initial state so that it can be reused.
validator | The validator instance to reset |
Definition at line 210 of file utf8.c.
References ast_utf8_validator::state, and UTF8_ACCEPT.
enum ast_utf8_validation_result ast_utf8_validator_state | ( | struct ast_utf8_validator * | validator | ) |
Get the current UTF-8 validator state.
validator | The validator instance |
Definition at line 176 of file utf8.c.
References AST_UTF8_INVALID, AST_UTF8_UNKNOWN, AST_UTF8_VALID, ast_utf8_validator::state, UTF8_ACCEPT, and UTF8_REJECT.
Referenced by ast_utf8_validator_feed(), and ast_utf8_validator_feedn().
|
inlinestatic |
Definition at line 98 of file utf8.c.
References state, type, and utf8d.
Referenced by ast_utf8_copy_string(), ast_utf8_is_valid(), ast_utf8_is_validn(), ast_utf8_validator_feed(), and ast_utf8_validator_feedn().
|
static |
Definition at line 280 of file utf8.c.
References ast_utf8_copy_string().
Referenced by AST_TEST_DEFINE().
|
static |
Definition at line 355 of file utf8.c.
References AST_TEST_UNREGISTER.
Referenced by ast_utf8_init().