1 /*------------------------------------------------------------------
2  * strspn_s.c
3  *
4  * November 2008, Bo Berry
5  *
6  * Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or
14  * sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  *------------------------------------------------------------------
30  */
31 
32 #include "safeclib_private.h"
33 #include "safe_str_constraint.h"
34 #include "safe_str_lib.h"
35 
36 
37 /**
38  * NAME
39  *    strspn_s
40  *
41  * SYNOPSIS
42  *    #include "safe_str_lib.h"
43  *    errno_t
44  *    strspn_s(const char *dest, rsize_t dmax,
45  *             const char *src,  rsize_t slen, rsize_t *count)
46  *
47  * DESCRIPTION
48  *    This function computes the prefix length of the string
49  *    pointed to by dest which consists entirely of characters
50  *    that are included from the string pointed to by src.
51  *
52  * EXTENSION TO
53  *    ISO/IEC TR 24731, Programming languages, environments
54  *    and system software interfaces, Extensions to the C Library,
55  *    Part I: Bounds-checking interfaces
56  *
57  * INPUT PARAMETERS
58  *    dest     pointer to string to determine the prefix
59  *
60  *    dmax     restricted maximum length of string dest
61  *
62  *    src      pointer to exclusion string
63  *
64  *    slen     restricted maximum length of string src
65  *
66  *    count    pointer to a count variable that will be updated
67  *              with the dest substring length
68  *
69  * OUTPUT PARAMETERS
70  *    count    updated count
71  *
72  * RUNTIME CONSTRAINTS
73  *    Neither dest nor src shall be a null pointer.
74  *    count shall not be a null pointer.
75  *    dmax shall not be 0
76  *    dmax shall not be greater than RSIZE_MAX_STR
77  *
78  * RETURN VALUE
79  *    EOK         count
80  *    ESNULLP     NULL pointer
81  *    ESZEROL     zero length
82  *    ESLEMAX     length exceeds max limit
83  *
84  * ALSO SEE
85  *    strcspn_s(), strpbrk_s(), strstr_s(), strprefix_s()
86  *
87  */
88 errno_t
strspn_s(const char * dest,rsize_t dmax,const char * src,rsize_t slen,rsize_t * count)89 strspn_s (const char *dest, rsize_t dmax,
90           const char *src,  rsize_t slen, rsize_t *count)
91 {
92     const char *scan2;
93     rsize_t smax;
94     bool match_found;
95 
96     if (count== NULL) {
97         invoke_safe_str_constraint_handler("strspn_s: count is null",
98                    NULL, ESNULLP);
99         return RCNEGATE(ESNULLP);
100     }
101     *count = 0;
102 
103     if (dest == NULL) {
104         invoke_safe_str_constraint_handler("strspn_s: dest is null",
105                    NULL, ESNULLP);
106         return RCNEGATE(ESNULLP);
107     }
108 
109     if (src == NULL) {
110         invoke_safe_str_constraint_handler("strspn_s: src is null",
111                    NULL, ESNULLP);
112         return RCNEGATE(ESNULLP);
113     }
114 
115     if (dmax == 0 ) {
116         invoke_safe_str_constraint_handler("strspn_s: dmax is 0",
117                    NULL, ESZEROL);
118         return RCNEGATE(ESZEROL);
119     }
120 
121     if (dmax > RSIZE_MAX_STR) {
122         invoke_safe_str_constraint_handler("strspn_s: dmax exceeds max",
123                    NULL, ESLEMAX);
124         return RCNEGATE(ESLEMAX);
125     }
126 
127     if (slen == 0 ) {
128         invoke_safe_str_constraint_handler("strspn_s: slen is 0",
129                    NULL, ESZEROL);
130         return RCNEGATE(ESZEROL);
131     }
132 
133     if (slen > RSIZE_MAX_STR) {
134         invoke_safe_str_constraint_handler("strspn_s: slen exceeds max",
135                    NULL, ESLEMAX);
136         return RCNEGATE(ESLEMAX);
137     }
138 
139     while (*dest && dmax) {
140 
141         /*
142          * Scan the entire src string for each dest character, counting
143          * inclusions.
144          */
145         match_found = false;
146         smax = slen;
147         scan2 = src;
148         while (*scan2 && smax) {
149 
150             if (*dest == *scan2) {
151                 match_found = true;
152                 break;
153             }
154             scan2++;
155             smax--;
156         }
157 
158         if (match_found) {
159             (*count)++;
160         } else {
161             break;
162         }
163 
164         dest++;
165         dmax--;
166     }
167 
168     return RCNEGATE(EOK);
169 }
170 EXPORT_SYMBOL(strspn_s)
171