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.

wcscat_s.c 4.3 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. /*
  19. * Befor this function, the basic parameter checking has been done
  20. */
  21. static errno_t SecDoWcscat(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
  22. {
  23. size_t destLen;
  24. size_t srcLen;
  25. size_t maxCount; /* Store the maximum available count */
  26. /* To calculate the length of a wide character, the parameter must be a wide character */
  27. SECUREC_CALC_WSTR_LEN(strDest, destMax, &destLen);
  28. maxCount = destMax - destLen;
  29. SECUREC_CALC_WSTR_LEN(strSrc, maxCount, &srcLen);
  30. if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
  31. strDest[0] = L'\0';
  32. if (strDest + destLen <= strSrc && destLen == destMax) {
  33. SECUREC_ERROR_INVALID_PARAMTER("wcscat_s");
  34. return EINVAL_AND_RESET;
  35. }
  36. SECUREC_ERROR_BUFFER_OVERLAP("wcscat_s");
  37. return EOVERLAP_AND_RESET;
  38. }
  39. if (srcLen + destLen >= destMax || strDest == strSrc) {
  40. strDest[0] = L'\0';
  41. if (destLen == destMax) {
  42. SECUREC_ERROR_INVALID_PARAMTER("wcscat_s");
  43. return EINVAL_AND_RESET;
  44. }
  45. SECUREC_ERROR_INVALID_RANGE("wcscat_s");
  46. return ERANGE_AND_RESET;
  47. }
  48. SecDoMemcpy(strDest + destLen, strSrc, (srcLen + 1) * sizeof(wchar_t)); /* single character length include \0 */
  49. return EOK;
  50. }
  51. /*
  52. * <FUNCTION DESCRIPTION>
  53. * The wcscat_s function appends a copy of the wide string pointed to by strSrc
  54. * (including the terminating null wide character)
  55. * to the end of the wide string pointed to by strDest.
  56. * The arguments and return value of wcscat_s are wide-character strings.
  57. *
  58. * The wcscat_s function appends strSrc to strDest and terminates the resulting
  59. * string with a null character. The initial character of strSrc overwrites the
  60. * terminating null character of strDest. wcscat_s will return EOVERLAP_AND_RESET if the
  61. * source and destination strings overlap.
  62. *
  63. * Note that the second parameter is the total size of the buffer, not the
  64. * remaining size.
  65. *
  66. * <INPUT PARAMETERS>
  67. * strDest Null-terminated destination string buffer.
  68. * destMax Size of the destination string buffer.
  69. * strSrc Null-terminated source string buffer.
  70. *
  71. * <OUTPUT PARAMETERS>
  72. * strDest is updated
  73. *
  74. * <RETURN VALUE>
  75. * EOK Success
  76. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
  77. * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid) or
  78. * (strDest != NULL and strSrc is NULLL and destMax != 0
  79. * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN)
  80. * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
  81. * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
  82. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
  83. *
  84. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  85. */
  86. errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
  87. {
  88. if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
  89. SECUREC_ERROR_INVALID_RANGE("wcscat_s");
  90. return ERANGE;
  91. }
  92. if (strDest == NULL || strSrc == NULL) {
  93. SECUREC_ERROR_INVALID_PARAMTER("wcscat_s");
  94. if (strDest != NULL) {
  95. strDest[0] = L'\0';
  96. return EINVAL_AND_RESET;
  97. }
  98. return EINVAL;
  99. }
  100. return SecDoWcscat(strDest, destMax, strSrc);
  101. }