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.

wcscpy_s.c 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 SecDoWcscpy(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
  19. {
  20. size_t srcStrLen;
  21. SECUREC_CALC_WSTR_LEN(strSrc, destMax, &srcStrLen);
  22. if (srcStrLen == destMax) {
  23. strDest[0] = '\0';
  24. SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
  25. return ERANGE_AND_RESET;
  26. }
  27. if (strDest == strSrc) {
  28. return EOK;
  29. }
  30. if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, srcStrLen)) {
  31. /* performance optimization srcStrLen include '\0' */
  32. SecDoMemcpy(strDest, strSrc, (srcStrLen + 1) * sizeof(wchar_t)); /* single character length include \0 */
  33. return EOK;
  34. } else {
  35. strDest[0] = L'\0';
  36. SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s");
  37. return EOVERLAP_AND_RESET;
  38. }
  39. }
  40. /*
  41. * <FUNCTION DESCRIPTION>
  42. * The wcscpy_s function copies the wide string pointed to by strSrc
  43. * (including theterminating null wide character) into the array pointed to by strDest
  44. * <INPUT PARAMETERS>
  45. * strDest Destination string buffer
  46. * destMax Size of the destination string buffer.
  47. * strSrc Null-terminated source string buffer.
  48. *
  49. * <OUTPUT PARAMETERS>
  50. * strDest is updated.
  51. *
  52. * <RETURN VALUE>
  53. * EOK Success
  54. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
  55. * EINVAL_AND_RESET strDest != NULL and strSrc is NULLL and destMax != 0
  56. * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
  57. * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
  58. * ERANGE_AND_RESET destMax <= length of strSrc and strDest != strSrc
  59. * and strDest != NULL and strSrc != NULL and destMax != 0
  60. * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap
  61. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and destMax != 0
  62. * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
  63. * and strDest != NULL and strSrc !=NULL and strDest != strSrc
  64. *
  65. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  66. */
  67. errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
  68. {
  69. if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
  70. SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
  71. return ERANGE;
  72. }
  73. if (strDest == NULL || strSrc == NULL) {
  74. SECUREC_ERROR_INVALID_PARAMTER("wcscpy_s");
  75. if (strDest != NULL) {
  76. strDest[0] = L'\0';
  77. return EINVAL_AND_RESET;
  78. }
  79. return EINVAL;
  80. }
  81. return SecDoWcscpy(strDest, destMax, strSrc);
  82. }