• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "securec.h"
17 
SecFindBeginW(wchar_t * strToken,const wchar_t * strDelimit)18 static wchar_t *SecFindBeginW(wchar_t *strToken, const wchar_t *strDelimit)
19 {
20     /* Find beginning of token (skip over leading delimiters). Note that
21      * there is no token if this loop sets string to point to the terminal null.
22      */
23     wchar_t *token = strToken;
24     while (*token != 0) {
25         const wchar_t *ctl = strDelimit;
26         while (*ctl != 0 && *ctl != *token) {
27             ++ctl;
28         }
29         if (*ctl == 0) {
30             break;
31         }
32         ++token;
33     }
34     return token;
35 }
36 
SecFindRestW(wchar_t * strToken,const wchar_t * strDelimit)37 static wchar_t *SecFindRestW(wchar_t *strToken, const wchar_t *strDelimit)
38 {
39 
40     /* Find the end of the token. If it is not the end of the string,
41      * put a null there.
42      */
43     wchar_t *token = strToken;
44     while (*token != 0) {
45         const wchar_t *ctl = strDelimit;
46         while (*ctl != 0 && *ctl != *token) {
47             ++ctl;
48         }
49         if (*ctl != 0) {
50             *token++ = 0;
51             break;
52         }
53         ++token;
54     }
55     return token;
56 }
57 
58 /*******************************************************************************
59  * <NAME>
60  *    wcstok_s
61  *
62  *
63  * <FUNCTION DESCRIPTION>
64  *   The  wcstok_s  function  is  the  wide-character  equivalent  of the strtok_s function
65  *
66  * <INPUT PARAMETERS>
67  *    strToken               String containing token or tokens.
68  *    strDelimit             Set of delimiter characters.
69  *    context                Used to store position information between calls to
70  *                               wcstok_s.
71  *
72  * <OUTPUT PARAMETERS>
73  *    context               is updated
74  * <RETURN VALUE>
75  *    Returns a pointer to the next token found in strToken.
76  *    They return NULL when no more tokens are found.
77  *    Each call modifies strToken by substituting a NULL character for the first
78  *    delimiter that occurs after the returned token.
79  *
80  *    return value          condition
81  *    NULL                  context is NULL, strDelimit is NULL, strToken is NULL
82  *                          and  (*context) is NULL, or no token is found.
83  *******************************************************************************
84  */
wcstok_s(wchar_t * strToken,const wchar_t * strDelimit,wchar_t ** context)85 wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context)
86 {
87     wchar_t *orgToken = strToken;
88     /* validation section */
89     if (context == NULL || strDelimit == NULL) {
90         return NULL;
91     }
92     if (orgToken == NULL && (*context) == NULL) {
93         return NULL;
94     }
95 
96     /* If string==NULL, continue with previous string */
97     if (orgToken == NULL) {
98         orgToken = *context;
99     }
100 
101     orgToken = SecFindBeginW(orgToken, strDelimit);
102     {
103         wchar_t *token = orgToken;
104 
105         orgToken = SecFindRestW(orgToken, strDelimit);
106 
107         /* Update the context */
108         *context = orgToken;
109 
110         /* Determine if a token has been found. */
111         if (token == orgToken) {
112             token = NULL;
113         }
114 
115         return token;
116     }
117 }
118 
119 
120