1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
3 * Licensed under Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 * Description: wcstok_s function
12 * Create: 2014-02-25
13 */
14
15 #include "securecutil.h"
16
SecIsInDelimitW(wchar_t ch,const wchar_t * strDelimit)17 SECUREC_INLINE int SecIsInDelimitW(wchar_t ch, const wchar_t *strDelimit)
18 {
19 const wchar_t *ctl = strDelimit;
20 while (*ctl != L'\0' && *ctl != ch) {
21 ++ctl;
22 }
23 return (int)(*ctl != L'\0');
24 }
25
26 /*
27 * Find beginning of token (skip over leading delimiters).
28 * Note that there is no token if this loop sets string to point to the terminal null.
29 */
SecFindBeginW(wchar_t * strToken,const wchar_t * strDelimit)30 SECUREC_INLINE wchar_t *SecFindBeginW(wchar_t *strToken, const wchar_t *strDelimit)
31 {
32 wchar_t *token = strToken;
33 while (*token != L'\0') {
34 if (SecIsInDelimitW(*token, strDelimit) != 0) {
35 ++token;
36 continue;
37 }
38 /* Don't find any delimiter in string header, break the loop */
39 break;
40 }
41 return token;
42 }
43
44 /*
45 * Find the end of the token. If it is not the end of the string, put a null there.
46 */
SecFindRestW(wchar_t * strToken,const wchar_t * strDelimit)47 SECUREC_INLINE wchar_t *SecFindRestW(wchar_t *strToken, const wchar_t *strDelimit)
48 {
49 wchar_t *token = strToken;
50 while (*token != L'\0') {
51 if (SecIsInDelimitW(*token, strDelimit) != 0) {
52 /* Find a delimiter, set string terminator */
53 *token = L'\0';
54 ++token;
55 break;
56 }
57 ++token;
58 }
59 return token;
60 }
61
62 /*
63 * Update Token wide character function
64 */
SecUpdateTokenW(wchar_t * strToken,const wchar_t * strDelimit,wchar_t ** context)65 SECUREC_INLINE wchar_t *SecUpdateTokenW(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context)
66 {
67 /* Point to updated position. Record string position for next search in the context */
68 *context = SecFindRestW(strToken, strDelimit);
69 /* Determine if a token has been found */
70 if (*context == strToken) {
71 return NULL;
72 }
73 return strToken;
74 }
75
76 /*
77 * <NAME>
78 * wcstok_s
79 *
80 *
81 * <FUNCTION DESCRIPTION>
82 * The wcstok_s function is the wide-character equivalent of the strtok_s function
83 *
84 * <INPUT PARAMETERS>
85 * strToken String containing token or tokens.
86 * strDelimit Set of delimiter characters.
87 * context Used to store position information between calls to
88 * wcstok_s.
89 *
90 * <OUTPUT PARAMETERS>
91 * context is updated
92 * <RETURN VALUE>
93 * The wcstok_s function is the wide-character equivalent of the strtok_s function
94 */
wcstok_s(wchar_t * strToken,const wchar_t * strDelimit,wchar_t ** context)95 wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context)
96 {
97 wchar_t *orgToken = strToken;
98 /* Validation section */
99 if (context == NULL || strDelimit == NULL) {
100 return NULL;
101 }
102 if (orgToken == NULL && *context == NULL) {
103 return NULL;
104 }
105 /* If string==NULL, continue with previous string */
106 if (orgToken == NULL) {
107 orgToken = *context;
108 }
109 orgToken = SecFindBeginW(orgToken, strDelimit);
110 return SecUpdateTokenW(orgToken, strDelimit, context);
111 }
112
113