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.

strtok_s.c 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Find beginning of token (skip over leading delimiters).Note that
  19. * there is no token if this loop sets string to point to the terminal null.
  20. */
  21. static char *SecFindBegin(char *strToken, const char *strDelimit)
  22. {
  23. char *token = strToken;
  24. while (*token != '\0') {
  25. const char *ctl = strDelimit;
  26. while (*ctl != '\0' && *ctl != *token) {
  27. ++ctl;
  28. }
  29. if (*ctl == '\0') { /* don't find any delimiter in string header, break the loop */
  30. break;
  31. }
  32. ++token;
  33. }
  34. return token;
  35. }
  36. /*
  37. * Find rest of token
  38. */
  39. static char *SecFindRest(char *strToken, const char *strDelimit)
  40. {
  41. /* Find the rest of the token. If it is not the end of the string,
  42. * put a null there.
  43. */
  44. char *token = strToken;
  45. while (*token != '\0') {
  46. const char *ctl = strDelimit;
  47. while (*ctl != '\0' && *ctl != *token) {
  48. ++ctl;
  49. }
  50. if (*ctl != '\0') { /* find a delimiter */
  51. *token++ = '\0'; /* set string termintor */
  52. break;
  53. }
  54. ++token;
  55. }
  56. return token;
  57. }
  58. /*
  59. * Find the final position pointer
  60. */
  61. static char *SecUpdateToken(char *strToken, const char *strDelimit, char **context)
  62. {
  63. /* point to updated position */
  64. char *token = SecFindRest(strToken, strDelimit);
  65. /* record string position for next search in the context */
  66. *context = token;
  67. /* Determine if a token has been found. */
  68. if (token == strToken) {
  69. return NULL;
  70. }
  71. return strToken;
  72. }
  73. /*
  74. * <FUNCTION DESCRIPTION>
  75. * The strtok_s function parses a string into a sequence of strToken,
  76. * replace all characters in strToken string that match to strDelimit set with 0.
  77. * On the first call to strtok_s the string to be parsed should be specified in strToken.
  78. * In each subsequent call that should parse the same string, strToken should be NULL
  79. * <INPUT PARAMETERS>
  80. * strToken String containing token or tokens.
  81. * strDelimit Set of delimiter characters.
  82. * context Used to store position information between calls
  83. * to strtok_s
  84. * <OUTPUT PARAMETERS>
  85. * context is updated
  86. * <RETURN VALUE>
  87. * On the first call returns the address of the first non \0 character, otherwise NULL is returned.
  88. * In subsequent calls, the strtoken is set to NULL, and the context set is the same as the previous call,
  89. * return NULL if the *context string length is equal 0, otherwise return *context.
  90. */
  91. char *strtok_s(char *strToken, const char *strDelimit, char **context)
  92. {
  93. char *orgToken = strToken;
  94. /* validate delimiter and string context */
  95. if (context == NULL || strDelimit == NULL) {
  96. return NULL;
  97. }
  98. /* valid input string and string pointer from where to search */
  99. if (orgToken == NULL && (*context) == NULL) {
  100. return NULL;
  101. }
  102. /* If string is null, continue searching from previous string position stored in context */
  103. if (orgToken == NULL) {
  104. orgToken = *context;
  105. }
  106. orgToken = SecFindBegin(orgToken, strDelimit);
  107. return SecUpdateToken(orgToken, strDelimit, context);
  108. }
  109. #if SECUREC_IN_KERNEL
  110. EXPORT_SYMBOL(strtok_s);
  111. #endif