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.

wcstok_s.c 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "securec.h"
  17. /*
  18. * FindBegin Wide character postion function
  19. */
  20. static wchar_t *SecFindBeginW(wchar_t *strToken, const wchar_t *strDelimit)
  21. {
  22. /* Find beginning of token (skip over leading delimiters). Note that
  23. * there is no token if this loop sets string to point to the terminal null.
  24. */
  25. wchar_t *token = strToken;
  26. while (*token != L'\0') {
  27. const wchar_t *ctl = strDelimit;
  28. while (*ctl != L'\0' && *ctl != *token) {
  29. ++ctl;
  30. }
  31. if (*ctl == L'\0') {
  32. break;
  33. }
  34. ++token;
  35. }
  36. return token;
  37. }
  38. /*
  39. * FindBegin rest Wide character postion function
  40. */
  41. static wchar_t *SecFindRestW(wchar_t *strToken, const wchar_t *strDelimit)
  42. {
  43. /* Find the end of the token. If it is not the end of the string,
  44. * put a null there.
  45. */
  46. wchar_t *token = strToken;
  47. while (*token != L'\0') {
  48. const wchar_t *ctl = strDelimit;
  49. while (*ctl != L'\0' && *ctl != *token) {
  50. ++ctl;
  51. }
  52. if (*ctl != L'\0') {
  53. *token++ = L'\0';
  54. break;
  55. }
  56. ++token;
  57. }
  58. return token;
  59. }
  60. /*
  61. * Update Token wide character function
  62. */
  63. static wchar_t *SecUpdateTokenW(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context)
  64. {
  65. /* point to updated position */
  66. wchar_t *token = SecFindRestW(strToken, strDelimit);
  67. /* Update the context */
  68. *context = token;
  69. /* Determine if a token has been found. */
  70. if (token == strToken) {
  71. return NULL;
  72. }
  73. return strToken;
  74. }
  75. /*
  76. * <NAME>
  77. * wcstok_s
  78. *
  79. *
  80. * <FUNCTION DESCRIPTION>
  81. * The wcstok_s function is the wide-character equivalent of the strtok_s function
  82. *
  83. * <INPUT PARAMETERS>
  84. * strToken String containing token or tokens.
  85. * strDelimit Set of delimiter characters.
  86. * context Used to store position information between calls to
  87. * wcstok_s.
  88. *
  89. * <OUTPUT PARAMETERS>
  90. * context is updated
  91. * <RETURN VALUE>
  92. * The wcstok_s function is the wide-character equivalent of the strtok_s function
  93. */
  94. wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context)
  95. {
  96. wchar_t *orgToken = strToken;
  97. /* validation section */
  98. if (context == NULL || strDelimit == NULL) {
  99. return NULL;
  100. }
  101. if (orgToken == NULL && (*context) == NULL) {
  102. return NULL;
  103. }
  104. /* If string==NULL, continue with previous string */
  105. if (orgToken == NULL) {
  106. orgToken = *context;
  107. }
  108. orgToken = SecFindBeginW(orgToken, strDelimit);
  109. return SecUpdateTokenW(orgToken, strDelimit, context);
  110. }