diff --git a/winpr/include/winpr/string.h b/winpr/include/winpr/string.h index 9f7c487c1..579fc02ad 100644 --- a/winpr/include/winpr/string.h +++ b/winpr/include/winpr/string.h @@ -427,9 +427,23 @@ extern "C" WINPR_API INT64 GetLine(char** lineptr, size_t* size, FILE* stream); #if !defined(WINPR_HAVE_STRNDUP) + WINPR_ATTR_MALLOC(free, 1) WINPR_API char* strndup(const char* s, size_t n); #endif + /** @brief WCHAR version of \b strndup + * creates a copy of a \b WCHAR string of \b n characters length. The copy will always be \b \0 + * terminated + * + * @param s The \b WCHAR string to copy + * @param n The numer of WCHAR to copy + * + * @return An allocated copy of \b s, always \b \0 terminated + * @since version 3.10.0 + */ + WINPR_ATTR_MALLOC(free, 1) + WINPR_API WCHAR* wcsndup(const WCHAR* s, size_t n); + #ifdef __cplusplus } #endif diff --git a/winpr/libwinpr/crt/string.c b/winpr/libwinpr/crt/string.c index f5427a4ee..b8049d5b2 100644 --- a/winpr/libwinpr/crt/string.c +++ b/winpr/libwinpr/crt/string.c @@ -834,3 +834,15 @@ const WCHAR* InitializeConstWCharFromUtf8(const char* str, WCHAR* buffer, size_t (void)ConvertUtf8ToWChar(str, buffer, len); return buffer; } + +WCHAR* wcsndup(const WCHAR* s, size_t n) +{ + if (!s) + return NULL; + + WCHAR* copy = calloc(n + 1, sizeof(WCHAR)); + if (!copy) + return NULL; + memcpy(copy, s, n * sizeof(WCHAR)); + return copy; +}