#ifndef _NTSTRSAFE_H_INCLUDED_
#ifndef NTSTRSAFE_LIB_IMPL
#ifndef NTSTRSAFE_NO_UNICODE_STRING_FUNCTIONS
#ifndef NTSTRSAFE_NO_CB_FUNCTIONS
/*++
NTSTATUS
RtlUnicodeStringCbCatStringN(
_Inout_ PUNICODE_STRING DestinationString,
_In_ LPCTSTR pszSrc,
_In_ size_t cbToAppend
);
Routine Description:
This routine is a safer version of the C built-in function 'strncat' for
PUNICODE_STRINGs.
This function returns an NTSTATUS value, and not a pointer. It returns
STATUS_SUCCESS if all of pszSrc or the first cbToAppend bytes were
appended to the destination string, otherwise it will return a failure
code. In failure cases as much of pszSrc will be appended to DestinationString as
possible.
Arguments:
DestinationString - pointer to the counted unicode destination string
pszSrc - source string
cbToAppend - maximum number of bytes to append
Notes:
Behavior is undefined if source and destination strings overlap.
DestinationString and pszSrc should not be NULL. See RtlUnicodeStringCbCatStringNEx if
you require the handling of NULL values.
Return Value:
STATUS_SUCCESS - if all of pszSrc or the first cbToAppend bytes were
concatenated to pszDest
failure - the operation did not succeed
STATUS_BUFFER_OVERFLOW
Note: This status has the severity class Warning - IRPs completed with this
status do have their data copied back to user mode
- this return value is an indication that the
operation failed due to insufficient space. When this
error occurs, the destination buffer is modified to
contain a truncated version of the ideal result. This is
useful for situations where truncation is ok
It is strongly recommended to use the NT_SUCCESS() macro to test the
return value of this function
--*/
NTSTRSAFEDDI
RtlUnicodeStringCbCatStringN(
_Inout_ PUNICODE_STRING DestinationString,
_In_ NTSTRSAFE_PCWSTR pszSrc,
_In_ size_t cbToAppend)
{
NTSTATUS status;
wchar_t* pszDest;
size_t cchDest;
size_t cchDestLength;
status = RtlUnicodeStringValidateDestWorker(DestinationString,
&pszDest,
&cchDest,
&cchDestLength,
NTSTRSAFE_UNICODE_STRING_MAX_CCH,
0);
if (NT_SUCCESS(status))
{
size_t cchToAppend = cbToAppend / sizeof(wchar_t);
if (cchToAppend > NTSTRSAFE_UNICODE_STRING_MAX_CCH)
{
status = STATUS_INVALID_PARAMETER;
}
else
{
size_t cchCopied = 0;
status = RtlWideCharArrayCopyStringWorker(pszDest + cchDestLength,
cchDest - cchDestLength,
&cchCopied,
pszSrc,
cchToAppend);
// safe to multiply (cchDestLength + cchCopied) * sizeof(wchar_t) since (cchDestLength + cchCopied) < NTSTRSAFE_UNICODE_STRING_MAX_CCH and sizeof(wchar_t) is 2
DestinationString->Length = (USHORT)((cchDestLength + cchCopied) * sizeof(wchar_t));
}
}
return status;
}
View code on GitHub// ntstrsafe.h
NTSTRSAFEDDI RtlUnicodeStringCbCatStringN(
[in, out] PUNICODE_STRING DestinationString,
[in] NTSTRSAFE_PCWSTR pszSrc,
[in] size_t cbToAppend
);
View the official Windows Driver Kit DDI referenceNo description available.
The RtlUnicodeStringCbCatStringN function concatenates two strings when the destination string is contained in a UNICODE_STRING structure, while limiting the size of the appended string.
DestinationString [in, out]A pointer to a UNICODE_STRING structure. This structure includes a buffer that, on input, contains a destination string to which the source string will be concatenated. On output, this buffer is the destination buffer that contains the entire resultant string. The source string (excluding the terminating null) is added to the end of the destination string. The maximum number of bytes in the structure's string buffer is NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR).
pszSrc [in]A caller-supplied pointer to a null-terminated string. This string will be concatenated to the end of the destination string that is contained in the UNICODE_STRING structure that DestinationString points to.
cbToAppend [in]The maximum number of bytes to append to the string that the DestinationString parameter describes.
RtlUnicodeStringCbCatStringN returns one of the following NTSTATUS values.
| Return code | Description |
|---|---|
| STATUS_SUCCESS | This success status means that source data was present, the strings were concatenated without truncation, and the resultant destination buffer is null-terminated. |
| STATUS_BUFFER_OVERFLOW | This warning status means that the concatenation operation did not complete because of insufficient buffer space. The destination buffer contains a truncated, null-terminated version of the intended result. |
| STATUS_INVALID_PARAMETER | This error status means that the function received an invalid input parameter. For more information, see the following list. |
RtlUnicodeStringCbCatStringN returns the STATUS_INVALID_PARAMETER value when one of the following occurs:
For information about how to test NTSTATUS values, see Using NTSTATUS Values.
The RtlUnicodeStringCbCatStringN function uses the destination buffer's size to ensure that the concatenation operation does not write past the end of the buffer. The function does not terminate the resultant string with a null character value (that is, with zero).
If the source and destination strings overlap, the behavior of the function is undefined.
The pszSrc and DestinationString pointers cannot be NULL. If you need to handle NULL pointer values, use the RtlUnicodeStringCbCatStringNEx function.
For more information about the safe string functions, see Using Safe String Functions.
RtlUnicodeStringCbCatStringNEx