xref: /optee_os/lib/libutils/ext/include/string_ext.h (revision b7da54b3f039908dacf92a4719b9206ffc5644d3)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 /*
7  * This file provides extensions for functions not defined in <string.h>
8  */
9 
10 #ifndef STRING_EXT_H
11 #define STRING_EXT_H
12 
13 #include <stddef.h>
14 #include <sys/cdefs.h>
15 
16 /*
17  * Copy src to string dst of siz size.  At most siz-1 characters
18  * will be copied.  Always NUL terminates (unless siz == 0).
19  * Returns strlen(src); if retval >= siz, truncation occurred.
20  */
21 size_t strlcpy(char *dst, const char *src, size_t size);
22 size_t strlcat(char *dst, const char *src, size_t size);
23 
24 /*
25  * This memory compare function will compare two buffers in a constant time.
26  *
27  * Note that this function will not have same kind of return values as the
28  * traditional libc memcmp which return either less than or greater than zero
29  * depending on which string that is lexically greater. This function will
30  * return 0 if it is a match, otherwise it will return a non-zero value.
31  */
32 int buf_compare_ct(const void *s1, const void *s2, size_t n);
33 
34 /*
35  * A constant-time version of memcmp(). Might be slightly slower than
36  * buf_compare_ct(), but complies with the memcmp() specification which
37  * requires three possible outcomes for the comparison (< 0, 0 and > 0).
38  */
39 int consttime_memcmp(const void *p1, const void *p2, size_t nb);
40 
41 /* Variant of strdup() that uses nex_malloc() instead of malloc() */
42 char *nex_strdup(const char *s);
43 
44 #endif /* STRING_EXT_H */
45