1 /*------------------------------------------------------------------
2 * strcspn_s.c
3 *
4 * November 2008, Bo Berry
5 *
6 * Copyright (c) 2008-2011 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 * strcspn_s
40 *
41 * SYNOPSIS
42 * #include "safe_str_lib.h"
43 * errno_t
44 * strcspn_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 pointed
49 * to by dest which consists entirely of characters that are
50 * excluded from the string pointed to by src. The scanning stops
51 * at the first null in dest or after dmax characters. The
52 * exclusion string is checked to the null or after slen
53 * characters.
54 *
55 * EXTENSION TO
56 * ISO/IEC TR 24731, Programming languages, environments
57 * and system software interfaces, Extensions to the C Library,
58 * Part I: Bounds-checking interfaces
59 *
60 * INPUT PARAMETERS
61 * dest pointer to string to determine the prefix
62 *
63 * dmax restricted maximum length of string dest
64 *
65 * src pointer to exclusion string
66 *
67 * slen restricted maximum length of string src
68 *
69 * count pointer to a count variable that will be updated
70 * with the dest substring length
71 *
72 * OUTPUT PARAMETERS
73 * count updated count variable
74 *
75 * RUNTIME CONSTRAINTS
76 * Neither dest nor src shall be a null pointer.
77 * count shall not be a null pointer.
78 * dmax shall not be 0
79 * dmax shall not be greater than RSIZE_MAX_STR
80 *
81 * RETURN VALUE
82 * EOK count
83 * ESNULLP NULL pointer
84 * ESZEROL zero length
85 * ESLEMAX length exceeds max limit
86 *
87 * ALSO SEE
88 * strspn_s(), strpbrk_s(), strstr_s()
89 *
90 */
91 errno_t
strcspn_s(const char * dest,rsize_t dmax,const char * src,rsize_t slen,rsize_t * count)92 strcspn_s (const char *dest, rsize_t dmax,
93 const char *src, rsize_t slen, rsize_t *count)
94 {
95 const char *scan2;
96 rsize_t smax;
97
98 if (count== NULL) {
99 invoke_safe_str_constraint_handler("strcspn_s: count is null",
100 NULL, ESNULLP);
101 return RCNEGATE(ESNULLP);
102 }
103 *count = 0;
104
105 if (dest == NULL) {
106 invoke_safe_str_constraint_handler("strcspn_s: dest is null",
107 NULL, ESNULLP);
108 return RCNEGATE(ESNULLP);
109 }
110
111 if (src == NULL) {
112 invoke_safe_str_constraint_handler("strcspn_s: src is null",
113 NULL, ESNULLP);
114 return RCNEGATE(ESNULLP);
115 }
116
117 if (dmax == 0 ) {
118 invoke_safe_str_constraint_handler("strcspn_s: dmax is 0",
119 NULL, ESZEROL);
120 return RCNEGATE(ESZEROL);
121 }
122
123 if (dmax > RSIZE_MAX_STR) {
124 invoke_safe_str_constraint_handler("strcspn_s: dmax exceeds max",
125 NULL, ESLEMAX);
126 return RCNEGATE(ESLEMAX);
127 }
128
129 if (slen == 0 ) {
130 invoke_safe_str_constraint_handler("strcspn_s: slen is 0",
131 NULL, ESZEROL);
132 return RCNEGATE(ESZEROL);
133 }
134
135 if (slen > RSIZE_MAX_STR) {
136 invoke_safe_str_constraint_handler("strcspn_s: slen exceeds max",
137 NULL, ESLEMAX);
138 return RCNEGATE(ESLEMAX);
139 }
140
141 while (*dest && dmax) {
142
143 /*
144 * Scanning for exclusions, so if there is a match,
145 * we're done!
146 */
147 smax = slen;
148 scan2 = src;
149 while (*scan2 && smax) {
150
151 if (*dest == *scan2) {
152 return RCNEGATE(EOK);
153 }
154 scan2++;
155 smax--;
156 }
157
158 (*count)++;
159 dest++;
160 dmax--;
161 }
162
163 return RCNEGATE(EOK);
164 }
165 EXPORT_SYMBOL(strcspn_s)
166