You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

wcsncpy_s.c 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define SECUREC_INLINE_DO_MEMCPY 1
  17. #include "securecutil.h"
  18. static errno_t SecDoWcsncpy(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
  19. {
  20. size_t srcStrLen;
  21. if (count < destMax) {
  22. SECUREC_CALC_WSTR_LEN(strSrc, count, &srcStrLen);
  23. } else {
  24. SECUREC_CALC_WSTR_LEN(strSrc, destMax, &srcStrLen);
  25. }
  26. if (srcStrLen == destMax) {
  27. strDest[0] = '\0';
  28. SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
  29. return ERANGE_AND_RESET;
  30. }
  31. if (strDest == strSrc) {
  32. return EOK;
  33. }
  34. if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, srcStrLen)) {
  35. /* performance optimization srcStrLen not include '\0' */
  36. SecDoMemcpy(strDest, strSrc, srcStrLen * sizeof(wchar_t));
  37. *(strDest + srcStrLen) = L'\0';
  38. return EOK;
  39. } else {
  40. strDest[0] = L'\0';
  41. SECUREC_ERROR_BUFFER_OVERLAP("wcsncpy_s");
  42. return EOVERLAP_AND_RESET;
  43. }
  44. }
  45. /*
  46. * <FUNCTION DESCRIPTION>
  47. * The wcsncpy_s function copies not more than n successive wide characters
  48. * (not including the terminating null wide character)
  49. * from the array pointed to by strSrc to the array pointed to by strDest
  50. *
  51. * <INPUT PARAMETERS>
  52. * strDest Destination string.
  53. * destMax The size of the destination string, in characters.
  54. * strSrc Source string.
  55. * count Number of characters to be copied.
  56. *
  57. * <OUTPUT PARAMETERS>
  58. * strDest is updated
  59. *
  60. * <RETURN VALUE>
  61. * EOK Success
  62. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
  63. * EINVAL_AND_RESET strDest != NULL and strSrc is NULLL and destMax != 0
  64. * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
  65. * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
  66. * ERANGE_AND_RESET count > SECUREC_WCHAR_STRING_MAX_LEN or
  67. * (destMax <= length of strSrc and destMax <= count and strDest != strSrc
  68. * and strDest != NULL and strSrc != NULL and destMax != 0 and
  69. * destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap)
  70. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
  71. *
  72. *
  73. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  74. */
  75. errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
  76. {
  77. if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
  78. SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
  79. return ERANGE;
  80. }
  81. if (strDest == NULL || strSrc == NULL) {
  82. SECUREC_ERROR_INVALID_PARAMTER("wcsncpy_s");
  83. if (strDest != NULL) {
  84. strDest[0] = '\0';
  85. return EINVAL_AND_RESET;
  86. }
  87. return EINVAL;
  88. }
  89. if (count > SECUREC_WCHAR_STRING_MAX_LEN) {
  90. #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
  91. if (count == (size_t)(-1)) {
  92. return SecDoWcsncpy(strDest, destMax, strSrc, destMax - 1);
  93. }
  94. #endif
  95. strDest[0] = '\0'; /* clear dest string */
  96. SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
  97. return ERANGE_AND_RESET;
  98. }
  99. if (count == 0) {
  100. strDest[0] = '\0';
  101. return EOK;
  102. }
  103. return SecDoWcsncpy(strDest, destMax, strSrc, count);
  104. }