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.

vswscanf_s.c 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "secinput.h"
  17. static size_t SecWcslen(const wchar_t *s)
  18. {
  19. const wchar_t *end = s;
  20. while (*end != L'\0') {
  21. ++end;
  22. }
  23. return ((size_t)((end - s)));
  24. }
  25. /*
  26. * <FUNCTION DESCRIPTION>
  27. * The vswscanf_s function is the wide-character equivalent of the vsscanf_s function
  28. * The vsscanf_s function reads data from buffer into the location given by
  29. * each argument. Every argument must be a pointer to a variable with a type
  30. * that corresponds to a type specifier in format.
  31. * The format argument controls the interpretation of the input fields and
  32. * has the same form and function as the format argument for the scanf function.
  33. * If copying takes place between strings that overlap, the behavior is undefined.
  34. *
  35. * <INPUT PARAMETERS>
  36. * buffer Stored data
  37. * format Format control string, see Format Specifications.
  38. * argList pointer to list of arguments
  39. *
  40. * <OUTPUT PARAMETERS>
  41. * argList the converted value stored in user assigned address
  42. *
  43. * <RETURN VALUE>
  44. * Each of these functions returns the number of fields successfully converted
  45. * and assigned; the return value does not include fields that were read but
  46. * not assigned. A return value of 0 indicates that no fields were assigned.
  47. * return -1 if an error occurs.
  48. */
  49. int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list argList)
  50. {
  51. size_t count; /* If initialization causes e838 */
  52. SecFileStream fStr;
  53. int retVal;
  54. /* validation section */
  55. if (buffer == NULL || format == NULL) {
  56. SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
  57. return SECUREC_SCANF_EINVAL;
  58. }
  59. count = SecWcslen(buffer);
  60. if (count == 0 || count > SECUREC_WCHAR_STRING_MAX_LEN) {
  61. SecClearDestBufW(buffer, format, argList);
  62. SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
  63. return SECUREC_SCANF_EINVAL;
  64. }
  65. SECUREC_INIT_SEC_FILE_STREAM(fStr, SECUREC_MEM_STR_FLAG, NULL, 0,\
  66. (const char *)buffer, (int)count * ((int)sizeof(wchar_t)));
  67. retVal = SecInputSW(&fStr, format, argList);
  68. if (retVal < 0) {
  69. SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
  70. return SECUREC_SCANF_EINVAL;
  71. }
  72. return retVal;
  73. }