| 1 | /*------------------------------------------------------------------ |
| 2 | * strtok_s.c |
| 3 | * |
| 4 | * October 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 | * strtok_s |
| 40 | * |
| 41 | * SYNOPSIS |
| 42 | * #include "safe_str_lib.h" |
| 43 | * char * |
| 44 | * strtok_s(char *dest, rsize_t *dmax, char *src, char **ptr) |
| 45 | * |
| 46 | * DESCRIPTION |
| 47 | * A sequence of calls to the strtok_s function breaks the string |
| 48 | * pointed to by dest into a sequence of tokens, each of which is |
| 49 | * delimited by a character from the string pointed to by src. The |
| 50 | * fourth argument points to a caller-provided char pointer into |
| 51 | * which the strtok_s function stores information necessary for |
| 52 | * it to continue scanning the same string. |
| 53 | * |
| 54 | * The first call in a sequence has a non-null first argument and |
| 55 | * dmax points to an object whose value is the number of elements |
| 56 | * in the character array pointed to by the first argument. The |
| 57 | * first call stores an initial value in the object pointed to by |
| 58 | * ptr and updates the value pointed to by dmax to reflect the |
| 59 | * number of elements that remain in relation to ptr. Subsequent |
| 60 | * calls in the sequence have a null first argument and the objects |
| 61 | * pointed to by dmax and ptr are required to have the values |
| 62 | * stored by the previous call in the sequence, which are then |
| 63 | * updated. The separator string pointed to by src may be different |
| 64 | * from call to call. |
| 65 | * |
| 66 | * The first call in the sequence searches the string pointed to |
| 67 | * by dest for the first character that is not contained in the |
| 68 | * current separator string pointed to by src. If no such character |
| 69 | * is found, then there are no tokens in the string pointed to |
| 70 | * by dest and the strtok_s function returns a null pointer. If |
| 71 | * such a character is found, it is the start of the first token. |
| 72 | * |
| 73 | * The strtok_s function then searches from there for the first |
| 74 | * character in dest that is contained in the current separator |
| 75 | * string. If no such character is found, the current token |
| 76 | * extends to the end of the string pointed to by dest, and |
| 77 | * subsequent searches in the same string for a token return |
| 78 | * a null pointer. If such a character is found, it is |
| 79 | * overwritten by a null character, which terminates the |
| 80 | * current token. |
| 81 | * |
| 82 | * In all cases, the strtok_s function stores sufficient information |
| 83 | * in the pointer pointed to by ptr so that subsequent calls, |
| 84 | * with a null pointer for dest and the unmodified pointer value |
| 85 | * for ptr, shall start searching just past the element overwritten |
| 86 | * by a null character (if any). |
| 87 | * |
| 88 | * SPECIFIED IN |
| 89 | * ISO/IEC TR 24731-1, Programming languages, environments |
| 90 | * and system software interfaces, Extensions to the C Library, |
| 91 | * Part I: Bounds-checking interfaces |
| 92 | * |
| 93 | * INPUT PARAMETERS |
| 94 | * dest pointer to string to tokenize |
| 95 | * |
| 96 | * dmax restricted maximum length of dest string |
| 97 | * |
| 98 | * src pointer to delimiter string (len < 255) |
| 99 | * |
| 100 | * ptr returned pointer to token |
| 101 | * |
| 102 | * OUTPUT PARAMETERS |
| 103 | * dmax update length |
| 104 | * |
| 105 | * ptr update pointer to token |
| 106 | * |
| 107 | * RUNTIME CONSTRAINTS |
| 108 | * src shall not be a null pointer. |
| 109 | * ptr shall not be a null pointer. |
| 110 | * dmax shall not be a null pointer. |
| 111 | * *dmax shall not be 0. |
| 112 | * |
| 113 | * If dest is a null pointer, then *ptr shall not be a null pointer. |
| 114 | * |
| 115 | * dest must not be unterminated. |
| 116 | * |
| 117 | * The value of *dmax shall not be greater than RSIZE_MAX_STR. The |
| 118 | * end of the token found shall occur within the first *dmax |
| 119 | * characters of dest for the first call, and shall occur within |
| 120 | * the first *dmax characters of where searching resumes on |
| 121 | * subsequent calls. |
| 122 | * |
| 123 | * RETURN VALUE |
| 124 | * The strtok_s function returns a pointer to the first character |
| 125 | * of a token; or a null pointer if there is no token or there |
| 126 | * is a runtime-constraint violation. |
| 127 | * |
| 128 | * EOK |
| 129 | * ESNULLP NULL pointer |
| 130 | * ESZEROL zero length |
| 131 | * ESLEMAX length exceeds max limit |
| 132 | * ESUNTERM unterminated string |
| 133 | * |
| 134 | * EXAMPLES |
| 135 | * [1] Sequencial strtok_s() calls to tokenize a string |
| 136 | * |
| 137 | * String to tokenize str1 = ",.:*one,two;three,;four*.*.five-six***" |
| 138 | * len=38 |
| 139 | * String of delimiters str2 = ",.;*" |
| 140 | * |
| 141 | * p2tok = strtok_s(str1, &len, str2, &p2str); |
| 142 | * token -one- remaining -two;three,;four*.*.five-six***- len=30 |
| 143 | * |
| 144 | * p2tok = strtok_s(NULL, &len, str2, &p2str); |
| 145 | * token -two- remaining -three,;four*.*.five-six***- len=26 |
| 146 | * |
| 147 | * p2tok = strtok_s(NULL, &len, str2, &p2str); |
| 148 | * token -three- remaining -;four*.*.five-six***- len=20 |
| 149 | * |
| 150 | * p2tok = strtok_s(NULL, &len, str2, &p2str); |
| 151 | * token -four- remaining -.*.five-six***- len=14 |
| 152 | * |
| 153 | * p2tok = strtok_s(NULL, &len, str2, &p2str); |
| 154 | * token -five-six- remaining -**- len=2 |
| 155 | * |
| 156 | * p2tok = strtok_s(NULL, &len, str2, &p2str); |
| 157 | * token -(null)- remaining -**- len=0 |
| 158 | * |
| 159 | * |
| 160 | * [2] While loop with same entry data as [1] |
| 161 | * |
| 162 | * p2tok = str1; |
| 163 | * while (p2tok && len) { |
| 164 | * p2tok = strtok_s(NULL, &len, str2, &p2str); |
| 165 | * printf(" token -- remaining -- len=0 \n", |
| 166 | * p2tok, p2str, (int)len ); |
| 167 | * } |
| 168 | * |
| 169 | *- |
| 170 | */ |
| 171 | char * |
| 172 | strtok_s(char *dest, rsize_t *dmax, const char *src, char **ptr) |
| 173 | { |
| 174 | |
| 175 | /* |
| 176 | * CONFIGURE: The spec does not call out a maximum for the src |
| 177 | * string, so one is defined here. |
| 178 | */ |
| 179 | #define STRTOK_DELIM_MAX_LEN ( 16 ) |
| 180 | |
| 181 | |
| 182 | const char *pt; |
| 183 | char *ptoken; |
| 184 | rsize_t dlen; |
| 185 | rsize_t slen; |
| 186 | |
| 187 | if (dmax == NULL) { |
| 188 | invoke_safe_str_constraint_handler("strtok_s: dmax is NULL", |
| 189 | NULL, ESNULLP); |
| 190 | return (NULL); |
| 191 | } |
| 192 | |
| 193 | if (*dmax == 0) { |
| 194 | invoke_safe_str_constraint_handler("strtok_s: dmax is 0", |
| 195 | NULL, ESZEROL); |
| 196 | return (NULL); |
| 197 | } |
| 198 | |
| 199 | if (*dmax > RSIZE_MAX_STR) { |
| 200 | invoke_safe_str_constraint_handler("strtok_s: dmax exceeds max", |
| 201 | NULL, ESLEMAX); |
| 202 | return (NULL); |
| 203 | } |
| 204 | |
| 205 | if (src == NULL) { |
| 206 | invoke_safe_str_constraint_handler("strtok_s: src is null", |
| 207 | NULL, ESNULLP); |
| 208 | return (NULL); |
| 209 | } |
| 210 | |
| 211 | if (ptr == NULL) { |
| 212 | invoke_safe_str_constraint_handler("strtok_s: ptr is null", |
| 213 | NULL, ESNULLP); |
| 214 | return (NULL); |
| 215 | } |
| 216 | |
| 217 | /* if the source was NULL, use the tokenizer context */ |
| 218 | if (dest == NULL) { |
| 219 | dest = *ptr; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * scan dest for a delimiter |
| 224 | */ |
| 225 | dlen = *dmax; |
| 226 | ptoken = NULL; |
| 227 | while (*dest != '\0' && !ptoken) { |
| 228 | |
| 229 | if (dlen == 0) { |
| 230 | *ptr = NULL; |
| 231 | invoke_safe_str_constraint_handler( |
| 232 | "strtok_s: dest is unterminated", |
| 233 | NULL, ESUNTERM); |
| 234 | return (NULL); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * must scan the entire delimiter list |
| 239 | * ISO should have included a delimiter string limit!! |
| 240 | */ |
| 241 | slen = STRTOK_DELIM_MAX_LEN; |
| 242 | pt = src; |
| 243 | while (*pt != '\0') { |
| 244 | |
| 245 | if (slen == 0) { |
| 246 | *ptr = NULL; |
| 247 | invoke_safe_str_constraint_handler( |
| 248 | "strtok_s: src is unterminated", |
| 249 | NULL, ESUNTERM); |
| 250 | return (NULL); |
| 251 | } |
| 252 | slen--; |
| 253 | |
| 254 | if (*dest == *pt) { |
| 255 | ptoken = NULL; |
| 256 | break; |
| 257 | } else { |
| 258 | pt++; |
| 259 | ptoken = dest; |
| 260 | } |
| 261 | } |
| 262 | dest++; |
| 263 | dlen--; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * if the beginning of a token was not found, then no |
| 268 | * need to continue the scan. |
| 269 | */ |
| 270 | if (ptoken == NULL) { |
| 271 | *dmax = dlen; |
| 272 | return (ptoken); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Now we need to locate the end of the token |
| 277 | */ |
| 278 | while (*dest != '\0') { |
| 279 | |
| 280 | if (dlen == 0) { |
| 281 | *ptr = NULL; |
| 282 | invoke_safe_str_constraint_handler( |
| 283 | "strtok_s: dest is unterminated", |
| 284 | NULL, ESUNTERM); |
| 285 | return (NULL); |
| 286 | } |
| 287 | |
| 288 | slen = STRTOK_DELIM_MAX_LEN; |
| 289 | pt = src; |
| 290 | while (*pt != '\0') { |
| 291 | |
| 292 | if (slen == 0) { |
| 293 | *ptr = NULL; |
| 294 | invoke_safe_str_constraint_handler( |
| 295 | "strtok_s: src is unterminated", |
| 296 | NULL, ESUNTERM); |
| 297 | return (NULL); |
| 298 | } |
| 299 | slen--; |
| 300 | |
| 301 | if (*dest == *pt) { |
| 302 | /* |
| 303 | * found a delimiter, set to null |
| 304 | * and return context ptr to next char |
| 305 | */ |
| 306 | *dest = '\0'; |
| 307 | *ptr = (dest + 1); /* return pointer for next scan */ |
| 308 | *dmax = dlen - 1; /* account for the nulled delimiter */ |
| 309 | return (ptoken); |
| 310 | } else { |
| 311 | /* |
| 312 | * simply scanning through the delimiter string |
| 313 | */ |
| 314 | pt++; |
| 315 | } |
| 316 | } |
| 317 | dest++; |
| 318 | dlen--; |
| 319 | } |
| 320 | |
| 321 | *dmax = dlen; |
| 322 | return (ptoken); |
| 323 | } |
| 324 | EXPORT_SYMBOL(strtok_s) |
| 325 |