Copies not more than Length successive char from the string pointed to by Source to the array pointed to by Destination. If no null char is copied from Source, then Destination[Length] is always set to null. This function is similar as strncpy_s defined in C11. If an error would be returned, then the function will also ASSERT(). If an error is returned, then the Destination is
| 1894 | @retval RETURN_ACCESS_DENIED If Source and Destination overlap. |
| 1895 | **/ |
| 1896 | RETURN_STATUS |
| 1897 | EFIAPI |
| 1898 | AsciiStrnCpyS ( |
| 1899 | OUT CHAR8 *Destination, |
| 1900 | IN UINTN DestMax, |
| 1901 | IN CONST CHAR8 *Source, |
| 1902 | IN UINTN Length |
| 1903 | ) |
| 1904 | { |
| 1905 | UINTN SourceLen; |
| 1906 | |
| 1907 | // |
| 1908 | // 1. Neither Destination nor Source shall be a null pointer. |
| 1909 | // |
| 1910 | SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER); |
| 1911 | SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER); |
| 1912 | |
| 1913 | // |
| 1914 | // 2. Neither DestMax nor Length shall be greater than ASCII_RSIZE_MAX |
| 1915 | // |
| 1916 | // if (ASCII_RSIZE_MAX != 0) { |
| 1917 | // SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER); |
| 1918 | // SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER); |
| 1919 | // } |
| 1920 | |
| 1921 | // |
| 1922 | // 3. DestMax shall not equal zero. |
| 1923 | // |
| 1924 | SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER); |
| 1925 | |
| 1926 | // |
| 1927 | // 4. If Length is not less than DestMax, then DestMax shall be greater than AsciiStrnLenS(Source, DestMax). |
| 1928 | // |
| 1929 | SourceLen = AsciiStrnLenS (Source, MIN (DestMax, Length)); |
| 1930 | // if (Length >= DestMax) { |
| 1931 | // SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL); |
| 1932 | // } |
| 1933 | |
| 1934 | // |
| 1935 | // 5. Copying shall not take place between objects that overlap. |
| 1936 | // |
| 1937 | if (SourceLen > Length) { |
| 1938 | SourceLen = Length; |
| 1939 | } |
| 1940 | // SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination, DestMax, (CHAR8 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED); |
| 1941 | |
| 1942 | // |
| 1943 | // The AsciiStrnCpyS function copies not more than Length successive characters (characters that |
| 1944 | // follow a null character are not copied) from the array pointed to by Source to the array |
| 1945 | // pointed to by Destination. If no null character was copied from Source, then Destination[Length] is set to a null |
| 1946 | // character. |
| 1947 | // |
| 1948 | while ((SourceLen > 0) && (*Source != 0) && (--DestMax > 0)) { |
| 1949 | *(Destination++) = *(Source++); |
| 1950 | SourceLen--; |
| 1951 | } |
| 1952 | *Destination = 0; |
| 1953 |
no test coverage detected