| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*** |
| 6 | *tcsncpy_s.inl - general implementation of _tcsncpy_s |
| 7 | * |
| 8 | |
| 9 | * |
| 10 | *Purpose: |
| 11 | * This file contains the general algorithm for strncpy_s and its variants. |
| 12 | * |
| 13 | ****/ |
| 14 | |
| 15 | _FUNC_PROLOGUE |
| 16 | errno_t __cdecl _FUNC_NAME(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC, size_t _COUNT) |
| 17 | { |
| 18 | _CHAR *p; |
| 19 | size_t available; |
| 20 | |
| 21 | if (_COUNT == 0 && _DEST == NULL && _SIZE == 0) |
| 22 | { |
| 23 | /* this case is allowed; nothing to do */ |
| 24 | _RETURN_NO_ERROR; |
| 25 | } |
| 26 | |
| 27 | /* validation section */ |
| 28 | _VALIDATE_STRING(_DEST, _SIZE); |
| 29 | if (_COUNT == 0) |
| 30 | { |
| 31 | /* notice that the source string pointer can be NULL in this case */ |
| 32 | _RESET_STRING(_DEST, _SIZE); |
| 33 | _RETURN_NO_ERROR; |
| 34 | } |
| 35 | _VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE); |
| 36 | |
| 37 | p = _DEST; |
| 38 | available = _SIZE; |
| 39 | if (_COUNT == _TRUNCATE) |
| 40 | { |
| 41 | while ((*p++ = *_SRC++) != 0 && --available > 0) |
| 42 | { |
| 43 | } |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | _ASSERT_EXPR((!_CrtGetCheckCount() || _COUNT < _SIZE), "Buffer is too small" ); |
| 48 | |
| 49 | while ((*p++ = *_SRC++) != 0 && --available > 0 && --_COUNT > 0) |
| 50 | { |
| 51 | } |
| 52 | if (_COUNT == 0) |
| 53 | { |
| 54 | *p = 0; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (available == 0) |
| 59 | { |
| 60 | if (_COUNT == _TRUNCATE) |
| 61 | { |
| 62 | _DEST[_SIZE - 1] = 0; |
| 63 | _RETURN_TRUNCATE; |
| 64 | } |
| 65 | _RESET_STRING(_DEST, _SIZE); |
| 66 | _RETURN_BUFFER_TOO_SMALL(_DEST, _SIZE); |
| 67 | } |
| 68 | _FILL_STRING(_DEST, _SIZE, _SIZE - available + 1); |
| 69 | _RETURN_NO_ERROR; |
| 70 | } |
| 71 | |
| 72 | |