#ifndef _NTSTRSAFE_H_INCLUDED_
#ifndef NTSTRSAFE_LIB_IMPL
#ifndef NTSTRSAFE_NO_UNICODE_STRING_FUNCTIONS
#ifndef NTSTRSAFE_NO_CB_FUNCTIONS
/*++
NTSTATUS
RtlUnicodeStringCbCopyN(
_Inout_ PUNICODE_STRING DestinationString,
_In_ PCUNICODE_STRING SourceString,
_In_ size_t cbToCopy
);
Routine Description:
This routine is a safer version of the C built-in function 'strncpy' for
PUNICODE_STRINGs.
This function returns an NTSTATUS value, and not a pointer. It returns
STATUS_SUCCESS if the entire string or the first cbToCopy bytes were
copied without truncation, otherwise it will return a failure code. In
failure cases as much of SourceString will be copied to DestinationString as possible.
Arguments:
DestinationString - pointer to the counted unicode destination string
SourceString - pointer to the counted unicode source string
cbToCopy - maximum number of bytes to copy from source string,
not including the null terminator.
Notes:
Behavior is undefined if source and destination strings overlap.
DestinationString and SourceString should not be NULL. See RtlUnicodeStringCbCopyNEx
if you require the handling of NULL values.
Return Value:
STATUS_SUCCESS - if there was source data and it was all copied
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 copy
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
RtlUnicodeStringCbCopyN(
_Inout_ PUNICODE_STRING DestinationString,
_In_ PCUNICODE_STRING SourceString,
_In_ size_t cbToCopy)
{
NTSTATUS status;
wchar_t* pszDest;
size_t cchDest;
status = RtlUnicodeStringValidateDestWorker(DestinationString,
&pszDest,
&cchDest,
NULL,
NTSTRSAFE_UNICODE_STRING_MAX_CCH,
0);
if (NT_SUCCESS(status))
{
wchar_t* pszSrc;
size_t cchSrcLength;
size_t cchNewDestLength = 0;
status = RtlUnicodeStringValidateSrcWorker(SourceString,
&pszSrc,
&cchSrcLength,
NTSTRSAFE_UNICODE_STRING_MAX_CCH,
0);
if (NT_SUCCESS(status))
{
size_t cchToCopy = cbToCopy / sizeof(wchar_t);
if (cchToCopy > NTSTRSAFE_UNICODE_STRING_MAX_CCH)
{
status = STATUS_INVALID_PARAMETER;
}
else
{
if (cchSrcLength < cchToCopy)
{
cchToCopy = cchSrcLength;
}
status = RtlWideCharArrayCopyWorker(pszDest,
cchDest,
&cchNewDestLength,
pszSrc,
cchToCopy);
}
}
// safe to multiply cchNewDestLength * sizeof(wchar_t) since cchDest < NTSTRSAFE_UNICODE_STRING_MAX_CCH and sizeof(wchar_t) is 2
DestinationString->Length = (USHORT)(cchNewDestLength * sizeof(wchar_t));
}
return status;
}
View code on GitHub// ntstrsafe.h
NTSTRSAFEDDI RtlUnicodeStringCbCopyN(
[out] PUNICODE_STRING DestinationString,
[in] PCUNICODE_STRING SourceString,
[in] size_t cbToCopy
);
View the official Windows Driver Kit DDI referenceNo description available.
The RtlUnicodeStringCbCopyN function copies a string from one UNICODE_STRING structure to another while limiting the size of the copied string.
DestinationString [out]A pointer to a UNICODE_STRING structure that receives the copied string. The string that the SourceString parameter's UNICODE_STRING structure points to is copied to the buffer that the DestinationString parameter's UNICODE_STRING structure points to. The maximum number of bytes in the DestinationString structure's string buffer is NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR).
SourceString [in]A pointer to a UNICODE_STRING structure that contains the string to be copied. The maximum number of bytes in the structure's string buffer is NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR).
cbToCopy [in]The number of bytes to be copied from the source to the destination.
RtlUnicodeStringCbCopyN returns one of the following NTSTATUS values.
| Return code | Description |
|---|---|
| STATUS_SUCCESS | This success status means source data was present, the string was copied without truncation. |
| STATUS_BUFFER_OVERFLOW | This warning status means the copy operation did not complete due to insufficient buffer space. The destination buffer contains a truncated version of the intended result. |
| STATUS_INVALID_PARAMETER | This error status means the function received an invalid input parameter. For more information, see the following paragraph. |
RtlUnicodeStringCbCopyN 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 RtlUnicodeStringCbCopyN function uses the destination buffer's size to ensure that the copy 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 SourceString and DestinationString pointers cannot be NULL. If you need to handle NULL pointer values, use the RtlUnicodeStringCbCopyNEx function.
For more information about the safe string functions, see Using Safe String Functions.