1 /*------------------------------------------------------------------
2 * strpbrk_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 * strpbrk_s
40 *
41 * SYNOPSIS
42 * #include "safe_str_lib.h"
43 * errno_t
44 * strpbrk_s(char *dest, rsize_t dmax,
45 * char *src, rsize_t slen, char **first)
46 *
47 * DESCRIPTION
48 * Returns a pointer, first, to the first ocurrence of any character
49 * in src which is contained in dest.
50 *
51 * EXTENSION TO
52 * ISO/IEC TR 24731, Programming languages, environments
53 * and system software interfaces, Extensions to the C Library,
54 * Part I: Bounds-checking interfaces
55 *
56 * INPUT PARAMETERS
57 * dest pointer to string
58 *
59 * dmax restricted maximum length of string dest
60 *
61 * src pointer to string
62 *
63 * slen restricted length of string src
64 *
65 * first returned pointer to first occurence
66 *
67 * OUTPUT PARAMETERS
68 * none
69 *
70 * RUNTIME CONSTRAINTS
71 * Neither dest nor src shall be a null pointer.
72 * first shall not be a null pointer.
73 * dmax shall not be 0
74 * dmax shall not be greater than RSIZE_MAX_STR
75 *
76 * RETURN VALUE
77 * pointer to the first ocurrence of any character
78 * contained in src
79 *
80 * EOK count
81 * ESNULLP NULL pointer
82 * ESZEROL zero length
83 * ESLEMAX length exceeds max limit
84 *
85 * ALSO SEE
86 * strfirstchar_s(), strlastchar_s(), strfirstdiff_s(),
87 * strfirstsame_s(), strlastdiff_s(), strlastsame_s()
88 *
89 */
90 errno_t
strpbrk_s(char * dest,rsize_t dmax,char * src,rsize_t slen,char ** first)91 strpbrk_s (char *dest, rsize_t dmax,
92 char *src, rsize_t slen, char **first)
93 {
94 char *ps;
95 rsize_t len;
96
97 if (first == NULL) {
98 invoke_safe_str_constraint_handler("strpbrk_s: count is null",
99 NULL, ESNULLP);
100 return RCNEGATE(ESNULLP);
101 }
102 *first = NULL;
103
104 if (dest == NULL) {
105 invoke_safe_str_constraint_handler("strpbrk_s: dest is null",
106 NULL, ESNULLP);
107 return RCNEGATE(ESNULLP);
108 }
109
110 if (src == NULL) {
111 invoke_safe_str_constraint_handler("strpbrk_s: src is null",
112 NULL, ESNULLP);
113 return RCNEGATE(ESNULLP);
114 }
115
116 if (dmax == 0 ) {
117 invoke_safe_str_constraint_handler("strpbrk_s: dmax is 0",
118 NULL, ESZEROL);
119 return RCNEGATE(ESZEROL);
120 }
121
122 if (dmax > RSIZE_MAX_STR) {
123 invoke_safe_str_constraint_handler("strpbrk_s: dmax exceeds max",
124 NULL, ESLEMAX);
125 return RCNEGATE(ESLEMAX);
126 }
127
128 if (slen == 0 ) {
129 invoke_safe_str_constraint_handler("strpbrk_s: slen is 0",
130 NULL, ESZEROL);
131 return RCNEGATE(ESZEROL);
132 }
133
134 if (slen > RSIZE_MAX_STR) {
135 invoke_safe_str_constraint_handler("strpbrk_s: slen exceeds max",
136 NULL, ESLEMAX);
137 return RCNEGATE(ESLEMAX);
138 }
139
140 /*
141 * look for a matching char in the substring src
142 */
143 while (*dest && dmax) {
144
145 ps = src;
146 len = slen;
147 while (*ps) {
148
149 /* check for a match with the substring */
150 if (*dest == *ps) {
151 *first = dest;
152 return RCNEGATE(EOK);
153 }
154 ps++;
155 len--;
156 }
157 dest++;
158 dmax--;
159 }
160
161 return RCNEGATE(ESNOTFND);
162 }
163 EXPORT_SYMBOL(strpbrk_s)
164