High Performance OPC UA Server SDK
1.7.1.383
|
This encapsulates a UTF-8 encoded OPC UA string. More...
#include <string.h>
Public Member Functions | |
static char * | ua_string_strndup (const char *s, size_t n) |
Creates a duplicate of the string s . More... | |
void | ua_string_init (struct ua_string *s) |
Initializes s with a null string, so it is safe to clear or perform further operations. More... | |
void | ua_string_clear (struct ua_string *s) |
Release allocated resources for s . More... | |
void | ua_string_clear_s (struct ua_string *s) |
Securely clears a string. More... | |
int | ua_string_allocate (struct ua_string *s, size_t size) |
Allocate a string with length len bytes. More... | |
int | ua_string_set (struct ua_string *s, const char *src) |
Initializes s with a copy of src . More... | |
int | ua_string_setn (struct ua_string *s, const char *src, size_t len) |
Initializes s with a copy of src . More... | |
void | ua_string_attach (struct ua_string *s, char *src) |
Initializes s with src . More... | |
void | ua_string_attachn (struct ua_string *s, char *src, size_t len) |
Initializes s with src . More... | |
void | ua_string_attach_const (struct ua_string *s, const char *src) |
Initializes s with string constant src . More... | |
void | ua_string_attachn_const (struct ua_string *s, const char *src, size_t len) |
Initializes s with string constant src . More... | |
int | ua_string_smart_attach_const (struct ua_string *s, const char *src) |
Simple wrapper for ua_string_attach_const or ua_string_set. More... | |
int | ua_string_smart_attachn_const (struct ua_string *s, const char *src, size_t len) |
Simple wrapper for ua_string_attachn_const or ua_string_setn. More... | |
int | ua_string_compare_lex (const struct ua_string *a, const struct ua_string *b) |
The compare function returns zero if both strings are equal. More... | |
int | ua_string_compare (const struct ua_string *a, const struct ua_string *b) |
The compare function returns zero if both strings are equal. More... | |
int | ua_string_compare_const (const struct ua_string *a, const char *b) |
Compares a UA string with a string constant. More... | |
int | ua_string_comparen_const (const struct ua_string *a, const char *b, size_t len) |
Compares a UA string with a string constant of known length. More... | |
int | ua_string_copy (struct ua_string *dst, const struct ua_string *src) |
Creates a copy of the string src including the terminating null byte. More... | |
int | ua_string_cat (struct ua_string *dst, size_t size, const struct ua_string *src) |
Appends a copy of src to the end of dst . More... | |
int | ua_string_catf (struct ua_string *dst, size_t size, const char *fmt,...) |
Appends a formatted string to the end of dst . More... | |
int | ua_string_snprintf (struct ua_string *s, size_t size, const char *fmt,...) |
Replaces the given string s with a formatted string. More... | |
size_t | ua_string_length (const struct ua_string *s) |
Returns the length of the string in bytes excluding the zero terminator. More... | |
char * | ua_string_data (struct ua_string *s) |
Returns the raw string data of s . More... | |
const char * | ua_string_const_data (const struct ua_string *s) |
Returns the raw string data of s . More... | |
bool | ua_string_is_null (const struct ua_string *s) |
Returns true if the given string is a NULL string. More... | |
bool | ua_string_is_empty (const struct ua_string *s) |
Returns true if the given string is a valid empty string. More... | |
const char * | ua_string_printable (const struct ua_string *s) |
Return a printable representation of a ua_string. More... | |
int | ua_string_index_of (const struct ua_string *s, const char *substr) |
Searches for the substring substr in the given ua string s . More... | |
int | ua_string_replace (struct ua_string *s, const char *search, const char *replace) |
Replaces the first occurance of search in s with replace . More... | |
int | ua_string_trim (struct ua_string *s) |
Removes leading and trailing white spaces from string. More... | |
int | ua_string_from_file (struct ua_string *s, const char *path) |
Read all bytes from a file and write it into a string. More... | |
int | ua_string_to_file (struct ua_string *s, const char *path) |
Write all characters from string into a file. More... | |
bool | ua_string_is_zeroterminated (const struct ua_string *s) |
Returns true if the string is zero terminated. More... | |
Data Fields | |
uint32_t | len: 31 |
Length of the string. More... | |
uint32_t | free: 1 |
Bit to indicate if the data pointer must be freed. More... | |
union { | |
const char * cdata | |
Const pointer to the data. More... | |
char * data | |
Non-const pointer to the data. More... | |
} | d |
The string data. More... | |
Related Functions | |
#define | UA_STRING_INITIALIZER { 0, 0, { NULL } } |
Initializer for null strings. | |
#define | UA_STRING_FROM_C_STRING(s) { sizeof(s)-1, 0, { s } } |
Initializer for ua_strings from string constants. | |
This encapsulates a UTF-8 encoded OPC UA string.
Note, on the wire OPC UA String are not zero terminated, in memory we always add a zero terminator to avoid problems with C functions that expect zero terminated strings.
int ua_string_allocate | ( | struct ua_string * | s, |
size_t | size | ||
) |
Allocate a string with length len
bytes.
This creates an empty string, but with an internal buffer of len
bytes. Note that one byte is reserved for the null terminator. If you want to store a string with length n you need to allocate n+1 bytes.
Example:
void ua_string_attach | ( | struct ua_string * | s, |
char * | src | ||
) |
Initializes s
with src
.
This function takes ownership of src
which means the memory of src
will be freed when this string gets cleared using ua_string_clear. If src
is NULL, s
will be initialized as null string.
s | The string to initialize. |
src | The source string to attach. The string must be zero terminated and the memory must be allocated using ipc_malloc. When clearing the string ua_string using ua_string_clear the memory will be freed. |
void ua_string_attach_const | ( | struct ua_string * | s, |
const char * | src | ||
) |
Initializes s
with string constant src
.
This way s
will refer to an external string src
and will not free it when ua_string_clear() is called. This may be useful to initialize a UA string with string constants. If src
is NULL, s
will be initialized as null string.
s | The string to initialize. |
src | The source string to attach. The string must be zero terminated. |
Example:
void ua_string_attachn | ( | struct ua_string * | s, |
char * | src, | ||
size_t | len | ||
) |
Initializes s
with src
.
This function behaves like ua_string_attach, but takes an additional argument len
to avoid a strlen() call if the length is already known. Nevertheless the src
string must be zero terminated. If not, use ua_string_setn to create a terminated copy. If src
is NULL s
is initialized as null string.
s | The string to initialize. |
src | The source string to attach. The string must be zero terminated and the memory must be allocated using ipc_malloc. When clearing the string ua_string using ua_string_clear the memory will be freed. |
len | Length of src in bytes. |
void ua_string_attachn_const | ( | struct ua_string * | s, |
const char * | src, | ||
size_t | len | ||
) |
Initializes s
with string constant src
.
This function behaves like ua_string_attach_const but takes an additional argument len
to avoid a strlen() call. If src
is NULL s
is initialized as null string.
s | The string to initialize. |
src | The source string to attach. The string must be zero terminated. If not, use ua_string_setn to create a terminated copy. |
len | Length of string in bytes. |
Appends a copy of src
to the end of dst
.
dst | An initialized ua_string. This can be a also a Null string, but it must not be a constant string created with ua_string_attach(n)_const().. |
size | Maximum size the concatenated string can reach (including null byte). If size is too small dst will be truncated and UA_EBADTRUNCATED is returned. |
src | The string to be concatenated to dst . |
int ua_string_catf | ( | struct ua_string * | dst, |
size_t | size, | ||
const char * | fmt, | ||
... | |||
) |
Appends a formatted string to the end of dst
.
dst | An initialized ua_string. This can be a also a Null string, but it must not be a constant string created with ua_string_attach(n)_const(). |
size | Maximum size the concatenated string can reach (including null byte). If size is too small dst will be truncated and UA_EBADTRUNCATED is returned. |
fmt | format string according to the printf-format. |
void ua_string_clear | ( | struct ua_string * | s | ) |
Release allocated resources for s
.
It is safe to pass a null string or a string attached from a constant string. So clear every ua_string you don't need any longer!
s | string to be cleared, will be a null string afterwards. |
void ua_string_clear_s | ( | struct ua_string * | s | ) |
Securely clears a string.
This function calls ua_string_clear() internally, but first erases the string contents securely. Use this to clear credentials like e.g. passwords from memory.
s | string to be cleared, will be a null string afterwards. |
The compare function returns zero if both strings are equal.
If not equal an integer value less or greater then zero is returned, so that the return value can be used as a sorting criteria (e.g. qsort).
Strings are compared by their length field first, so shorter strings are considered smaller. Strings with the same length are compared using ua_memcmp.
This function is intended for maximum performance, however if strings are presented to a human ua_string_compare_lex should be considered.
int ua_string_compare_const | ( | const struct ua_string * | a, |
const char * | b | ||
) |
Compares a UA string with a string constant.
This function is provided for convenience and creates a ua_string from b
and then calls ua_string_compare
The compare function returns zero if both strings are equal.
If not equal an integer value less or greater then zero is returned, so that the return value can be used as a sorting criteria (e.g. qsort).
Strings are compared byte by byte, so when sorting the result is sorted lexicographical. The comparison is case sensitive and all capital letters are considered smaller than all lowercase letters, similar to the C99 strcmp() function.
For a more performant comparison see ua_string_compare.
int ua_string_comparen_const | ( | const struct ua_string * | a, |
const char * | b, | ||
size_t | len | ||
) |
Compares a UA string with a string constant of known length.
This function is provided for convenience.
It sorts the same way as ua_string_compare.
a | ua_string as first string comparison operand |
b | const char* string as second comparison operand |
len | Length if b in bytes. This function is used if b is not zero terminated. Note that the purpose of len is only to define the length of b, it is not like the n argument of strncmp. So if a is longer than b , and len bytes of a are identical to b , this function will return "not equal", because the strings have different lengths. To compare substrings you must reduce the length of a and b . |
const char * ua_string_const_data | ( | const struct ua_string * | s | ) |
Returns the raw string data of s
.
This can be used to pass the string to other C functions which expects a normal C string.
Creates a copy of the string src
including the terminating null byte.
dst | destination string |
src | source string |
char * ua_string_data | ( | struct ua_string * | s | ) |
Returns the raw string data of s
.
This pointer can be used to access and modify the characters that compose the string.
int ua_string_from_file | ( | struct ua_string * | s, |
const char * | path | ||
) |
Read all bytes from a file and write it into a string.
The file is opened in text mode. If the file is empty and empty string will be returned.
s | String to write to. |
path | Path of the file to read. |
int ua_string_index_of | ( | const struct ua_string * | s, |
const char * | substr | ||
) |
Searches for the substring substr
in the given ua string s
.
s | UA String to be searched |
substr | Substring to search for. Must be a zero terminated C string. |
void ua_string_init | ( | struct ua_string * | s | ) |
Initializes s
with a null string, so it is safe to clear or perform further operations.
s | an uninitialized ua_string. |
bool ua_string_is_empty | ( | const struct ua_string * | s | ) |
Returns true if the given string is a valid empty string.
This means the string has length of zero, but the data pointer is valid and contains a zero terminator.
bool ua_string_is_null | ( | const struct ua_string * | s | ) |
Returns true if the given string is a NULL string.
In OPC UA len
is set to -1 for NULL strings, however we only check the data
pointer. If data
is NULL the string is a NULL string and len
is ignored.
bool ua_string_is_zeroterminated | ( | const struct ua_string * | s | ) |
Returns true if the string is zero terminated.
This is typically true, as ua_string_set or the decoder always creates zero terminated strings. But it is possible that somebody passes a non-terminated string using ua_string_attachn_const or ua_string_attachn. Using such strings can be dangerous, because most C functions expect zero terminated strings. If any function needs to ensure the string is terminated to work without issues it can use this function to test it. This function checks if the last byte is a zero terminator. If the string was terminated earlier this is ignored. Note that this check would technically be an of-by-one error if the string is not terminated in the allocated memory is exactly len
bytes. Because this is not allowed by definition, this is OK. It is the best we can do to stop reading more bytes.
s | String to test |
size_t ua_string_length | ( | const struct ua_string * | s | ) |
Returns the length of the string in bytes excluding the zero terminator.
A value of 0 indicates an empty or null string, or an invalid argument.
const char * ua_string_printable | ( | const struct ua_string * | s | ) |
Return a printable representation of a ua_string.
The returned string is intended for printing/tracing the ua_string, it is purely informative and you cannot rely on the format of the returned value.
This function always returns a zero terminated string, which can be directly used for printing. It is only valid as long as the underlying ua_string is not modified.
int ua_string_replace | ( | struct ua_string * | s, |
const char * | search, | ||
const char * | replace | ||
) |
Replaces the first occurance of search
in s
with replace
.
s | UA String to be changed |
search | search string to be replaced |
replace | replacement string |
int ua_string_set | ( | struct ua_string * | s, |
const char * | src | ||
) |
Initializes s
with a copy of src
.
src
must be a zero terminated C string or NULL.
int ua_string_setn | ( | struct ua_string * | s, |
const char * | src, | ||
size_t | len | ||
) |
Initializes s
with a copy of src
.
Unlike ua_string_set this function takes an additional len
argument. This avoids calling strlen() which is faster when the length is already known and it can be used to create substrings of src
. s
is initialized as null string if src
is NULL.
int ua_string_smart_attach_const | ( | struct ua_string * | s, |
const char * | src | ||
) |
Simple wrapper for ua_string_attach_const or ua_string_set.
When compiled without MEMORY_USE_SHM this simply calls ua_string_attach_const. When compiled with MEMORY_USE_SHM this calls ua_string_set which will create a copy in shared memory, which is necessary to work across memory boundaries. ua_string_attach_const is faster than ua_string_set, so we try to avoid the later one if possible.
int ua_string_smart_attachn_const | ( | struct ua_string * | s, |
const char * | src, | ||
size_t | len | ||
) |
Simple wrapper for ua_string_attachn_const or ua_string_setn.
When compiled without MEMORY_USE_SHM this simply calls ua_string_attachn_const. When compiled with MEMORY_USE_SHM this calls ua_string_setn which will create a copy in shared memory, which is necessary to work across memory boundaries. ua_string_attachn_const is faster than ua_string_setn, so we try to avoid the later one if possible.
int ua_string_snprintf | ( | struct ua_string * | s, |
size_t | size, | ||
const char * | fmt, | ||
... | |||
) |
Replaces the given string s
with a formatted string.
s | An initialized ua_string. This can be a also a Null string, but it must not be a constant string created with ua_string_attach(n)_const(). |
size | Maximum size the output string can reach (including null byte). If size is too small s will be truncated and depending on the platform either UA_EBAD or UA_EBADTRUNCATED is returned. |
fmt | format string according to the printf-format. |
static char * ua_string_strndup | ( | const char * | s, |
size_t | n | ||
) |
Creates a duplicate of the string s
.
This function copies at most n-1
characters from s
and always adds a zero terminator. So it is safe to call this function from non-terminated string as long as n-1 characters are readable. It also stops copying when it encounters a zero terminator in the sources before reaching n
.
s | source string |
n | size of new string including the zero terminator. |
int ua_string_to_file | ( | struct ua_string * | s, |
const char * | path | ||
) |
Write all characters from string into a file.
The file is opened in text mode.
s | String to be written, must be at least one byte. |
path | Path of the file to create. |
int ua_string_trim | ( | struct ua_string * | s | ) |
Removes leading and trailing white spaces from string.
This functions required internal memory allocation and memcpy to removed the leading white spaces.
s | The string to trim. |
const char* cdata |
Const pointer to the data.
union { ... } d |
The string data.
char* data |
Non-const pointer to the data.
uint32_t free |
Bit to indicate if the data pointer must be freed.
uint32_t len |
Length of the string.