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.

wcsncat_s.c 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /*
  19. * Befor this function, the basic parameter checking has been done
  20. */
  21. static errno_t SecDoWcsncat(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
  22. {
  23. size_t destLen;
  24. size_t srcLen;
  25. /* To calculate the length of a wide character, the parameter must be a wide character */
  26. SECUREC_CALC_WSTR_LEN(strDest, destMax, &destLen);
  27. SECUREC_CALC_WSTR_LEN(strSrc, count, &srcLen);
  28. if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
  29. strDest[0] = L'\0';
  30. if (strDest + destLen <= strSrc && destLen == destMax) {
  31. SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s");
  32. return EINVAL_AND_RESET;
  33. }
  34. SECUREC_ERROR_BUFFER_OVERLAP("wcsncat_s");
  35. return EOVERLAP_AND_RESET;
  36. }
  37. if (srcLen + destLen >= destMax || strDest == strSrc) {
  38. strDest[0] = L'\0';
  39. if (destLen == destMax) {
  40. SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s");
  41. return EINVAL_AND_RESET;
  42. }
  43. SECUREC_ERROR_INVALID_RANGE("wcsncat_s");
  44. return ERANGE_AND_RESET;
  45. }
  46. SecDoMemcpy(strDest + destLen, strSrc, srcLen * sizeof(wchar_t)); /* no terminator */
  47. *(strDest + destLen + srcLen) = L'\0';
  48. return EOK;
  49. }
  50. /*
  51. * <FUNCTION DESCRIPTION>
  52. * The wcsncat_s function appends not more than n successive wide characters
  53. * (not including the terminating null wide character)
  54. * from the array pointed to by strSrc to the end of the wide string pointed to by strDest.
  55. *
  56. * The wcsncat_s function try to append the first D characters of strSrc to
  57. * the end of strDest, where D is the lesser of count and the length of strSrc.
  58. * If appending those D characters will fit within strDest (whose size is
  59. * given as destMax) and still leave room for a null terminator, then those
  60. * characters are appended, starting at the original terminating null of
  61. * strDest, and a new terminating null is appended; otherwise, strDest[0] is
  62. * set to the null character.
  63. *
  64. * <INPUT PARAMETERS>
  65. * strDest Null-terminated destination string.
  66. * destMax Size of the destination buffer.
  67. * strSrc Null-terminated source string.
  68. * count Number of character to append, or truncate.
  69. *
  70. * <OUTPUT PARAMETERS>
  71. * strDest is updated
  72. *
  73. * <RETURN VALUE>
  74. * EOK Success
  75. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
  76. * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid) or
  77. * (strDest != NULL and strSrc is NULLL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN)
  78. * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
  79. * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
  80. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
  81. *
  82. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  83. */
  84. errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
  85. {
  86. if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
  87. SECUREC_ERROR_INVALID_RANGE("wcsncat_s");
  88. return ERANGE;
  89. }
  90. if (strDest == NULL || strSrc == NULL) {
  91. SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s");
  92. if (strDest != NULL) {
  93. strDest[0] = L'\0';
  94. return EINVAL_AND_RESET;
  95. }
  96. return EINVAL;
  97. }
  98. if (count > SECUREC_WCHAR_STRING_MAX_LEN) {
  99. #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
  100. if (count == ((size_t)-1)) {
  101. /* Windows internal functions may pass in -1 when calling this function */
  102. return SecDoWcsncat(strDest, destMax, strSrc, destMax);
  103. }
  104. #endif
  105. strDest[0] = L'\0';
  106. SECUREC_ERROR_INVALID_RANGE("wcsncat_s");
  107. return ERANGE_AND_RESET;
  108. }
  109. return SecDoWcsncat(strDest, destMax, strSrc, count);
  110. }