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 Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT(). If Length > 0 and Source is not align
| 310 | @retval RETURN_ACCESS_DENIED If Source and Destination overlap. |
| 311 | **/ |
| 312 | RETURN_STATUS |
| 313 | EFIAPI |
| 314 | StrnCpyS ( |
| 315 | OUT CHAR16 *Destination, |
| 316 | IN UINTN DestMax, |
| 317 | IN CONST CHAR16 *Source, |
| 318 | IN UINTN Length |
| 319 | ) |
| 320 | { |
| 321 | UINTN SourceLen; |
| 322 | |
| 323 | ASSERT (((UINTN) Destination & BIT0) == 0); |
| 324 | ASSERT (((UINTN) Source & BIT0) == 0); |
| 325 | |
| 326 | // |
| 327 | // 1. Neither Destination nor Source shall be a null pointer. |
| 328 | // |
| 329 | SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER); |
| 330 | SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER); |
| 331 | |
| 332 | // |
| 333 | // 2. Neither DestMax nor Length shall be greater than RSIZE_MAX |
| 334 | // |
| 335 | // if (RSIZE_MAX != 0) { |
| 336 | // SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER); |
| 337 | // SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER); |
| 338 | // } |
| 339 | |
| 340 | // |
| 341 | // 3. DestMax shall not equal zero. |
| 342 | // |
| 343 | SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER); |
| 344 | |
| 345 | // |
| 346 | // 4. If Length is not less than DestMax, then DestMax shall be greater than StrnLenS(Source, DestMax). |
| 347 | // |
| 348 | SourceLen = StrnLenS (Source, MIN (DestMax, Length)); //Slice: SourceLen <= DestMax |
| 349 | // if (Length >= DestMax) { |
| 350 | // SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL); |
| 351 | // } |
| 352 | |
| 353 | // |
| 354 | // 5. Copying shall not take place between objects that overlap. |
| 355 | // |
| 356 | if (SourceLen > Length) { |
| 357 | SourceLen = Length; |
| 358 | } |
| 359 | // SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED); |
| 360 | |
| 361 | // |
| 362 | // The StrnCpyS function copies not more than Length successive characters (characters that |
| 363 | // follow a null character are not copied) from the array pointed to by Source to the array |
| 364 | // pointed to by Destination. If no null character was copied from Source, then Destination[Length] is set to a null |
| 365 | // character. |
| 366 | // |
| 367 | while ((SourceLen > 0) && (*Source != 0) && (--DestMax > 0)) { |
| 368 | *(Destination++) = *(Source++); |
| 369 | SourceLen--; |
no test coverage detected