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