Merge pull request #1456 from robertbaldyga/add-missing-safeclib-files

casadm: Add missing safeclib files
This commit is contained in:
Robert Baldyga 2023-11-29 14:16:17 +01:00 committed by GitHub
commit fd39e912cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 4578 additions and 0 deletions

View File

@ -66,7 +66,37 @@ OBJS += safeclib/safe_str_constraint.o
OBJS += safeclib/ignore_handler_s.o OBJS += safeclib/ignore_handler_s.o
OBJS += safeclib/safe_mem_constraint.o OBJS += safeclib/safe_mem_constraint.o
OBJS += safeclib/mem_primitives_lib.o OBJS += safeclib/mem_primitives_lib.o
OBJS += safeclib/strcat_s.o
OBJS += safeclib/strcmpfld_s.o
OBJS += safeclib/strcpy_s.o
OBJS += safeclib/strcpyfld_s.o
OBJS += safeclib/strcpyfldin_s.o
OBJS += safeclib/strcpyfldout_s.o
OBJS += safeclib/strcspn_s.o
OBJS += safeclib/strfirstchar_s.o
OBJS += safeclib/strfirstdiff_s.o
OBJS += safeclib/strfirstsame_s.o
OBJS += safeclib/strisalphanumeric_s.o
OBJS += safeclib/strisascii_s.o
OBJS += safeclib/strisdigit_s.o
OBJS += safeclib/strishex_s.o
OBJS += safeclib/strislowercase_s.o
OBJS += safeclib/strismixedcase_s.o
OBJS += safeclib/strispassword_s.o
OBJS += safeclib/strisuppercase_s.o
OBJS += safeclib/strlastchar_s.o
OBJS += safeclib/strlastdiff_s.o
OBJS += safeclib/strlastsame_s.o
OBJS += safeclib/strljustify_s.o
OBJS += safeclib/strncat_s.o
OBJS += safeclib/strnlen_s.o OBJS += safeclib/strnlen_s.o
OBJS += safeclib/strnterminate_s.o
OBJS += safeclib/strpbrk_s.o
OBJS += safeclib/strprefix_s.o
OBJS += safeclib/strremovews_s.o
OBJS += safeclib/strspn_s.o
OBJS += safeclib/strstr_s.o
OBJS += safeclib/strzero_s.o
# #
# Flags for C compilation # Flags for C compilation

232
casadm/safeclib/strcat_s.c Normal file
View File

@ -0,0 +1,232 @@
/*------------------------------------------------------------------
* strcat_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strcat_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strcat_s(char *dest, rsize_t dmax, const char *src)
*
* DESCRIPTION
* The strcat_s function appends a copy of the string pointed
* to by src (including the terminating null character) to the
* end of the string pointed to by dest. The initial character
* from src overwrites the null character at the end ofdest.
*
* All elements following the terminating null character (if
* any) written by strcat_s in the array of dmax characters
* pointed to by dest take unspecified values when strcat_s
* returns.
*
* SPECIFIED IN
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string that will be extended by src
* if dmax allows. The string is null terminated.
* If the resulting concatenated string is less
* than dmax, the remaining slack space is nulled.
*
* dmax restricted maximum length of the resulting dest,
* including the null
*
* src pointer to the string that will be concatenaed
* to string dest
*
* OUTPUT PARAMETERS
* dest is updated
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer
* dmax shall not equal zero
* dmax shall not be greater than RSIZE_MAX_STR
* dmax shall be greater than strnlen_s(src,m).
* Copying shall not takeplace between objects that overlap
* If there is a runtime-constraint violation, then if dest is
* not a null pointer and dmax is greater than zero and not
* greater than RSIZE_MAX_STR, then strcat_s nulls dest.
*
* RETURN VALUE
* EOK successful operation, all the characters from src
* were appended to dest and the result in dest is
* null terminated.
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max
* ESUNTERM dest not terminated
*
* ALSO SEE
* strncat_s(), strcpy_s(), strncpy_s()
*
*/
errno_t
strcat_s (char *dest, rsize_t dmax, const char *src)
{
rsize_t orig_dmax;
char *orig_dest;
const char *overlap_bumper;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strcat_s: dest is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strcat_s: src is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strcat_s: dmax is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcat_s: dmax exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
/* hold base of dest in case src was not copied */
orig_dmax = dmax;
orig_dest = dest;
if (dest < src) {
overlap_bumper = src;
/* Find the end of dest */
while (*dest != '\0') {
if (dest == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strcat_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
dest++;
dmax--;
if (dmax == 0) {
handle_error(orig_dest, orig_dmax, "strcat_s: "
"dest unterminated",
ESUNTERM);
return RCNEGATE(ESUNTERM);
}
}
while (dmax > 0) {
if (dest == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strcat_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
*dest = *src;
if (*dest == '\0') {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null slack to clear any data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#endif
return RCNEGATE(EOK);
}
dmax--;
dest++;
src++;
}
} else {
overlap_bumper = dest;
/* Find the end of dest */
while (*dest != '\0') {
/*
* NOTE: no need to check for overlap here since src comes first
* in memory and we're not incrementing src here.
*/
dest++;
dmax--;
if (dmax == 0) {
handle_error(orig_dest, orig_dmax, "strcat_s: "
"dest unterminated",
ESUNTERM);
return RCNEGATE(ESUNTERM);
}
}
while (dmax > 0) {
if (src == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strcat_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
*dest = *src;
if (*dest == '\0') {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null slack to clear any data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#endif
return RCNEGATE(EOK);
}
dmax--;
dest++;
src++;
}
}
/*
* the entire src was not copied, so null the string
*/
handle_error(orig_dest, orig_dmax, "strcat_s: not enough "
"space for src",
ESNOSPC);
return RCNEGATE(ESNOSPC);
}
EXPORT_SYMBOL(strcat_s);

View File

@ -0,0 +1,142 @@
/*------------------------------------------------------------------
* strcmpfld_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strcmpfld_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strcmpfld_s(const char *dest, rsize_t dmax,
* const char *src, int *indicator)
*
* DESCRIPTION
* Compares the character array pointed to by src to the character array
* pointed to by dest for dmax characters. The null terminator does not
* stop the comparison.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to character array to compare against
*
* dmax restricted maximum length of dest. The length is
* used for the comparison of src against dest.
*
* src pointer to the character array to be compared to dest
*
* indicator pointer to result indicator, greater than, equal
* to or less than 0, if the character array pointed
* to by dest is greater than, equal to or less
* than the character array pointed to by src.
* OUTPUT
* indicator updated result indicator
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* indicator shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* indicator, when the return code is OK
* >0 dest greater than src
* 0 strings the same
* <0 dest less than src
*
* EOK
* ESNULLP pointer was null
* ESZEROL length was zero
* ESLEMAX length exceeded max
*
* ALSO SEE
* strcpyfld_s(), strcpyfldin_s(), strcpyfldout_s()
*
*/
errno_t
strcmpfld_s (const char *dest, rsize_t dmax,
const char *src, int *indicator)
{
if (indicator == NULL) {
invoke_safe_str_constraint_handler("strcmpfld_s: indicator is null",
NULL, ESNULLP);
return (ESNULLP);
}
*indicator = 0;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strcmpfld_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strcmpfld_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strcmpfld_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcmpfld_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* compare for dmax charactrers, not the null! */
while (dmax) {
if (*dest != *src) {
break;
}
dest++;
src++;
dmax--;
}
*indicator = *dest - *src;
return (EOK);
}

198
casadm/safeclib/strcpy_s.c Normal file
View File

@ -0,0 +1,198 @@
/*------------------------------------------------------------------
* strcpy_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strcpy_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strcpy_s(char *dest, rsize_t dmax, const char *src)
*
* DESCRIPTION
* The strcpy_s function copies the string pointed to by src
* (including the terminating null character) into the array
* pointed to by dest. All elements following the terminating
* null character (if any) written by strcpy_s in the array
* of dmax characters pointed to by dest are nulled when
* strcpy_s returns.
*
* SPECIFIED IN
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string that will be replaced by src.
*
* dmax restricted maximum length of dest
*
* src pointer to the string that will be copied
* to dest
*
* OUTPUT PARAMETERS
* dest updated
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* dmax shall not be greater than RSIZE_MAX_STR.
* dmax shall not equal zero.
* dmax shall be greater than strnlen_s(src, dmax).
* Copying shall not take place between objects that overlap.
* If there is a runtime-constraint violation, then if dest
* is not a null pointer and destmax is greater than zero and
* not greater than RSIZE_MAX_STR, then strcpy_s nulls dest.
*
* RETURN VALUE
* EOK successful operation, the characters in src were
* copied into dest and the result is null terminated.
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESOVRLP strings overlap
* ESNOSPC not enough space to copy src
*
* ALSO SEE
* strcat_s(), strncat_s(), strncpy_s()
*
*/
errno_t
strcpy_s (char *dest, rsize_t dmax, const char *src)
{
rsize_t orig_dmax;
char *orig_dest;
const char *overlap_bumper;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strcpy_s: dest is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strcpy_s: dmax is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcpy_s: dmax exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
if (src == NULL) {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#else
*dest = '\0';
#endif
invoke_safe_str_constraint_handler("strcpy_s: src is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (dest == src) {
return RCNEGATE(EOK);
}
/* hold base of dest in case src was not copied */
orig_dmax = dmax;
orig_dest = dest;
if (dest < src) {
overlap_bumper = src;
while (dmax > 0) {
if (dest == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strcpy_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
*dest = *src;
if (*dest == '\0') {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null slack to clear any data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#endif
return RCNEGATE(EOK);
}
dmax--;
dest++;
src++;
}
} else {
overlap_bumper = dest;
while (dmax > 0) {
if (src == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strcpy_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
*dest = *src;
if (*dest == '\0') {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null slack to clear any data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#endif
return RCNEGATE(EOK);
}
dmax--;
dest++;
src++;
}
}
/*
* the entire src must have been copied, if not reset dest
* to null the string.
*/
handle_error(orig_dest, orig_dmax, "strcpy_s: not "
"enough space for src",
ESNOSPC);
return RCNEGATE(ESNOSPC);
}
EXPORT_SYMBOL(strcpy_s);

View File

@ -0,0 +1,199 @@
/*------------------------------------------------------------------
* strcpyfld_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strcpyfld_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strcpyfld_s(char *dest, rsize_t dmax, const char *src, rsize_t slen)
*
* DESCRIPTION
* The strcpyfld_s function copies slen characters from the character
* array pointed to by src into the character array pointed to by dest.
* The copy operation does not stop on the null character as the
* function copies slen characters.
*
* EXTENSION TO
* ISO/IEC TR 24731-1, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to character array that will be replaced by src.
*
* dmax restricted maximum length of dest
*
* src pointer to the character array that will be copied
* to dest
*
* slen maximum length of src
*
* OUTPUT PARAMETERS
* dest updated
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
* slen shall not equal zero.
* slen shall not exceed dmax
* Copying shall not take place between objects that overlap.
* If there is a runtime-constraint violation, then if dest
* is not a null pointer and destmax is greater than zero and
* not greater than RSIZE_MAX_STR, then strcpyfld_s nulls dest.
*
* RETURN VALUE
* EOK successful operation
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESOVRLP strings overlap
*
* ALSO SEE
* strcpyfldin_s(), strcpyfldout_s()
*
*/
errno_t
strcpyfld_s (char *dest, rsize_t dmax, const char *src, rsize_t slen)
{
rsize_t orig_dmax;
char *orig_dest;
const char *overlap_bumper;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strcpyfld_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strcpyfld_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcpyfld_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
if (src == NULL) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfld_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (slen == 0) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfld_s: slen is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (slen > dmax) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfld_s: src exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* hold base of dest in case src was not copied */
orig_dmax = dmax;
orig_dest = dest;
if (dest < src) {
overlap_bumper = src;
while (slen > 0) {
if (dest == overlap_bumper) {
dmax = orig_dmax;
dest = orig_dest;
/* null string to eliminate partial copy */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler(
"strcpyfld_s: overlapping objects",
NULL, ESOVRLP);
return (ESOVRLP);
}
*dest++ = *src++;
slen--;
dmax--;
}
} else {
overlap_bumper = dest;
while (slen > 0) {
if (src == overlap_bumper) {
dmax = orig_dmax;
dest = orig_dest;
/* null string to eliminate partial copy */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler(
"strcpyfld_s: overlapping objects",
NULL, ESOVRLP);
return (ESOVRLP);
}
*dest++ = *src++;
slen--;
dmax--;
}
}
/* null slack space in the field */
while (dmax) { *dest = '\0'; dmax--; dest++; }
return (EOK);
}

View File

@ -0,0 +1,202 @@
/*------------------------------------------------------------------
* strcpyfldin_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strcpyfldin_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strcpyfldin_s(char *dest, rsize_t dmax,
* const char *src, rsize_t slen)
*
* DESCRIPTION
* The strcpyfldin_s function copies at most slen characters from the
* null terminated string pointed to by src into the fixed character
* array pointed to by dest. The copy operation stops on the null
* character if encountered and then continues to fill the field
* with nulls up to dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731-1, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to character array that will be replaced by src.
*
* dmax restricted maximum length of dest
*
* src pointer to the null terminated string that will be copied
* into the character array pointed to by dest
*
* slen length of source
*
* OUTPUT PARAMETERS
* dest updated
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
* slen shall not equal zero.
* slen shall not exceed dmax
* Copying shall not take place between objects that overlap.
* If there is a runtime-constraint violation, then if dest
* is not a null pointer and dmax is greater than zero and
* not greater than RSIZE_MAX_STR, then strcpyfldin_s nulls dest.
*
* RETURN VALUE
* EOK successful operation
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESOVRLP strings overlap
*
* ALSO SEE
* strcpyfld_s(), strcpyfldout_s(),
*
*/
errno_t
strcpyfldin_s (char *dest, rsize_t dmax, const char *src, rsize_t slen)
{
rsize_t orig_dmax;
char *orig_dest;
const char *overlap_bumper;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strcpyfldin_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strcpyfldin_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcpyfldin_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
if (src == NULL) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfldin_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (slen == 0) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfldin_s: slen is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (slen > dmax) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfldin_s: slen exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* hold base of dest in case src was not copied */
orig_dmax = dmax;
orig_dest = dest;
if (dest < src) {
overlap_bumper = src;
while (dmax > 0 && *src) {
if (dest == overlap_bumper) {
dmax = orig_dmax;
dest = orig_dest;
/* null string to eliminate partial copy */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler(
"strcpyfldin_s: overlapping objects",
NULL, ESOVRLP);
return (ESOVRLP);
}
dmax--;
*dest++ = *src++;
}
} else {
overlap_bumper = dest;
while (dmax > 0 && *src) {
if (src == overlap_bumper) {
dmax = orig_dmax;
dest = orig_dest;
/* null string to eliminate partial copy */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler(
"strcpyfldin_s: overlapping objects",
NULL, ESOVRLP);
return (ESOVRLP);
}
dmax--;
*dest++ = *src++;
}
}
/*
* finish filling in the field with nulls if there is slack space
*/
while (dmax) { *dest = '\0'; dmax--; dest++; }
return (EOK);
}

View File

@ -0,0 +1,204 @@
/*------------------------------------------------------------------
* strcpyfldout_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strcpyfldout_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strcpyfldout_s(char *dest, rsize_t dmax,
* const char *src, rsize_t slen)
*
* DESCRIPTION
* The strcpyfldout_s function copies slen characters from
* the character array pointed to by src into the string
* pointed to by dest. A null is included to properly
* termiante the dest string. The copy operation does not
* stop on the null character as function copies dmax
* characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string that will be replaced by src.
*
* dmax restricted maximum length of dest
*
* src pointer to the character array to be copied
* to dest and null terminated.
*
* slen the maximum number of characters that will be
* copied from the src field into the dest string.
*
* OUTPUT PARAMETERS
* dest updated
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
* slen shall not equal zero.
* slen shall not exceed dmax
* Copying shall not take place between objects that overlap.
* If there is a runtime-constraint violation, then if dest
* is not a null pointer and dmax is greater than zero and
* not greater than RSIZE_MAX_STR, then strcpyfldout_s nulls dest.
*
* RETURN VALUE
* EOK successful operation
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESOVRLP strings overlap
*
* ALSO SEE
* strcpyfld_s(), strcpyfldin_s()
*
*/
errno_t
strcpyfldout_s (char *dest, rsize_t dmax, const char *src, rsize_t slen)
{
rsize_t orig_dmax;
char *orig_dest;
const char *overlap_bumper;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strcpyfldout_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strcpyfldout_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcpyfldout_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
if (src == NULL) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfldout_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (slen == 0) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfldout_s: slen is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (slen > dmax) {
/* null string to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler("strcpyfldout_s: slen exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* hold base of dest in case src was not copied */
orig_dmax = dmax;
orig_dest = dest;
if (dest < src) {
overlap_bumper = src;
while (dmax > 1 && slen) {
if (dest == overlap_bumper) {
dmax = orig_dmax;
dest = orig_dest;
/* null string to eliminate partial copy */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler(
"strcpyfldout_s: overlapping objects",
NULL, ESOVRLP);
return (ESOVRLP);
}
dmax--;
slen--;
*dest++ = *src++;
}
} else {
overlap_bumper = dest;
while (dmax > 1 && slen) {
if (src == overlap_bumper) {
dmax = orig_dmax;
dest = orig_dest;
/* null string to eliminate partial copy */
while (dmax) { *dest = '\0'; dmax--; dest++; }
invoke_safe_str_constraint_handler(
"strcpyfldout_s: overlapping objects",
NULL, ESOVRLP);
return (ESOVRLP);
}
dmax--;
slen--;
*dest++ = *src++;
}
}
/* null slack space */
while (dmax) { *dest = '\0'; dmax--; dest++; }
return (EOK);
}

165
casadm/safeclib/strcspn_s.c Normal file
View File

@ -0,0 +1,165 @@
/*------------------------------------------------------------------
* strcspn_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strcspn_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strcspn_s(const char *dest, rsize_t dmax,
* const char *src, rsize_t slen, rsize_t *count)
*
* DESCRIPTION
* This function computes the prefix length of the string pointed
* to by dest which consists entirely of characters that are
* excluded from the string pointed to by src. The scanning stops
* at the first null in dest or after dmax characters. The
* exclusion string is checked to the null or after slen
* characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to determine the prefix
*
* dmax restricted maximum length of string dest
*
* src pointer to exclusion string
*
* slen restricted maximum length of string src
*
* count pointer to a count variable that will be updated
* with the dest substring length
*
* OUTPUT PARAMETERS
* count updated count variable
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* count shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* EOK count
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strspn_s(), strpbrk_s(), strstr_s()
*
*/
errno_t
strcspn_s (const char *dest, rsize_t dmax,
const char *src, rsize_t slen, rsize_t *count)
{
const char *scan2;
rsize_t smax;
if (count== NULL) {
invoke_safe_str_constraint_handler("strcspn_s: count is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
*count = 0;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strcspn_s: dest is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strcspn_s: src is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strcspn_s: dmax is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcspn_s: dmax exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
if (slen == 0 ) {
invoke_safe_str_constraint_handler("strcspn_s: slen is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (slen > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strcspn_s: slen exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
while (*dest && dmax) {
/*
* Scanning for exclusions, so if there is a match,
* we're done!
*/
smax = slen;
scan2 = src;
while (*scan2 && smax) {
if (*dest == *scan2) {
return RCNEGATE(EOK);
}
scan2++;
smax--;
}
(*count)++;
dest++;
dmax--;
}
return RCNEGATE(EOK);
}
EXPORT_SYMBOL(strcspn_s);

View File

@ -0,0 +1,127 @@
/*------------------------------------------------------------------
* strfirstchar_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strfirstchar_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strfirstchar_s(char *dest, rsize_t dmax, char c, char **first)
*
* DESCRIPTION
* This function returns a pointer to the first occurrence
* of character c in dest. The scanning stops at the first null
* or after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to compare against
*
* dmax restricted maximum length of string
*
* c character to locate
*
* first returned pointer to first occurrence of c
*
* OUTPUT PARAMETERS
* first updated pointer to first occurrence of c
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* first shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* pointer to first occurence of c, NULL if not found
*
* EOK pointer to first occurrence is returned
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strlastchar_s(), strfirstdiff_s(), strfirstsame_s(),
* strlastdiff_s(), strlastsame_s(),
*
*/
errno_t
strfirstchar_s (char *dest, rsize_t dmax, char c, char **first)
{
if (first == NULL) {
invoke_safe_str_constraint_handler("strfirstchar_s: index is null",
NULL, ESNULLP);
return (ESNULLP);
}
*first = NULL;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strfirstchar_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strfirstchar_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strfirstchar_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
while (*dest && dmax) {
if (*dest == c) {
*first = dest;
return (EOK);
}
dest++;
dmax--;
}
return (ESNOTFND);
}

View File

@ -0,0 +1,142 @@
/*------------------------------------------------------------------
* strfirstdiff_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strfirstdiff_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strfirstdiff_s(const char *dest, rsize_t dmax,
* const char *src, rsize_t *index)
*
* DESCRIPTION
* Returns the index of the first character that is different
* between dest and src. Index is valid only for OK.
* The scanning stops at the first null in dest or src, or
* after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to compare against
*
* dmax restricted maximum length of string dest
*
* src pointer to the string to be compared to dest
*
* index pointer to returned index to first difference
*
* OUTPUT PARAMETERS
* index returned index to first difference
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* indicator shall not be a null pointer.
* dmax shall not be 0.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* index to first difference, when the return code is OK
*
* EOK index to first diff is returned
* ESNODIFF no difference
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strfirstchar_s(), strfirstsame_s(), strlastchar_s(),
* strlastdiff_s(), strlastsame_s()
*
*/
errno_t
strfirstdiff_s (const char *dest, rsize_t dmax,
const char *src, rsize_t *index)
{
const char *rp;
if (index == NULL) {
invoke_safe_str_constraint_handler("strfirstdiff_s: index is null",
NULL, ESNULLP);
return (ESNULLP);
}
*index = 0;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strfirstdiff_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strfirstdiff_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strfirstdiff_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strfirstdiff_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* hold reference point */
rp = dest;
while (*dest && *src && dmax) {
if (*dest != *src) {
*index = dest - rp;
return (EOK);
}
dmax--;
dest++;
src++;
}
return (ESNODIFF);
}

View File

@ -0,0 +1,145 @@
/*------------------------------------------------------------------
* strfirstsame_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strfirstsame_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strfirstsame_s(const char *dest, rsize_t dmax,
* const char *src, rsize_t *index)
*
* DESCRIPTION
* Returns the index of the first character that is the
* same between dest and src. The scanning stops at the
* fisrt null in dest or src, or after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to compare against
*
* dmax restricted maximum length of string dest
*
* src pointer to the string to be compared to dest
*
* index pointer to returned index
*
* OUTPUT PARAMETERS
* index updated index
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* indicator shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* index to first same char, when the return code is OK
*
* EOK index to first same char is returned
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESNOTFND not found
*
* ALSO SEE
* strfirstchar_s(), strfirstdiff_s(), strlastchar_s(),
* strlastdiff_s(), strlastsame_s()
*
*/
errno_t
strfirstsame_s (const char *dest, rsize_t dmax,
const char *src, rsize_t *index)
{
const char *rp = 0;
if (index == NULL) {
invoke_safe_str_constraint_handler("strfirstsame_s: index is null",
NULL, ESNULLP);
return (ESNULLP);
}
*index = 0;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strfirstsame_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strfirstsame_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strfirstsame_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strfirstsame_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* hold reference point */
rp = dest;
/*
* find the offset
*/
while (*dest && *src && dmax) {
if (*dest == *src) {
*index = (uint32_t)(dest - rp);
return (EOK);
}
dest++;
src++;
dmax--;
}
return (ESNOTFND);
}

View File

@ -0,0 +1,119 @@
/*------------------------------------------------------------------
* strisalphanumeric_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strisalphanumeric_s
*
* SYNOPSIS
* #include "safe_dest_lib.h"
* bool
* strisalphanumeric_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function checks if the entire string contains
* alphanumerics. The scanning stops at the first null
* or after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax maximum length of string
*
* OUTPUT PARAMETERS
* none
*
* Runtime-condestaints
* dest shall not be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* true dest is alphanumeric
* false dest is not alphanumeric or an error occurred
*
* ALSO SEE
* strisascii_s(), strisdigit_s(), strishex_s(), strislowercase_s(),
* strismixedcase_s(), strisuppercase_s()
*
*/
bool
strisalphanumeric_s (const char *dest, rsize_t dmax)
{
if (!dest) {
invoke_safe_str_constraint_handler("strisalphanumeric_s: "
"dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strisalphanumeric_s: "
"dmax is 0",
NULL, ESZEROL);
return (false);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strisalphanumeric_s: "
"dmax exceeds max",
NULL, ESLEMAX);
return (false);
}
if (*dest == '\0') {
return (false);
}
while (*dest && dmax) {
if (( (*dest >= '0') && (*dest <= '9') ) ||
( (*dest >= 'a') && (*dest <= 'z') ) ||
( (*dest >= 'A') && (*dest <= 'Z') )) {
dest++;
dmax--;
} else {
return (false);
}
}
return (true);
}

View File

@ -0,0 +1,110 @@
/*------------------------------------------------------------------
* strisascii_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/*
*-
* NAME
* strisascii_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* bool
* strisascii_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function checks if the entire string contains ascii
* characters. The scanning stops at the first null or
* at most dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax maximum length of string
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* true, string is ascii
* false, string contains one or more non-ascii or an error occurred
*
* ALSO SEE
* strisalphanumeric_s(), strisdigit_s(), strishex_s(),
* strislowercase_s(), strismixedcase_s(), strisuppercase_s()
*-
*/
bool
strisascii_s (const char *dest, rsize_t dmax)
{
if (!dest) {
invoke_safe_str_constraint_handler("strisascii_s: dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strisascii_s: dmax is 0",
NULL, ESZEROL);
return (false);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strisascii_s: dmax "
"exceeds max",
NULL, ESLEMAX);
return (false);
}
while (*dest && dmax) {
if ((unsigned char)*dest > 127) {
return (false);
}
dest++;
dmax--;
}
return (true);
}

View File

@ -0,0 +1,112 @@
/*------------------------------------------------------------------
* strisdigit_s
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strisdigit_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* bool
* strisdigit_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function checks that the entire string contains digits.
* The scanning stops at the first null or after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax maximum length of string
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* true string is digit
* false string is not digit or an error occurred
*
* ALSO SEE
* strisalphanumeric_s(), strisascii_s(), strishex_s(),
* strislowercase_s(), strismixedcase_s(), strisuppercase_s()
*
*/
bool
strisdigit_s (const char *dest, rsize_t dmax)
{
if (!dest) {
invoke_safe_str_constraint_handler("strisdigit_s: dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strisdigit_s: dmax is 0",
NULL, ESZEROL);
return (false);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strisdigit_s: dmax exceeds max",
NULL, ESLEMAX);
return (false);
}
if (*dest == '\0') {
return (false);
}
while (*dest) {
if ((*dest < '0') || (*dest > '9')) {
return (false);
}
dest++;
dmax--;
}
return (true);
}

View File

@ -0,0 +1,118 @@
/*------------------------------------------------------------------
* strishex_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strishex_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* bool
* strishex_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function checks that the entire string contains
* hex characters. The scanning stops at the first null
* or after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax maximum length of string
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* true string is hex
* false string is not hex or an error occurred
*
* ALSO SEE
* strisalphanumeric_s(), strisascii_s(), strisdigit_s(),
* strislowercase_s(), strismixedcase_s(),
* strisuppercase_s()
*
*/
bool
strishex_s (const char *dest, rsize_t dmax)
{
if (!dest) {
invoke_safe_str_constraint_handler("strishex_s: dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strishex_s: dmax is 0",
NULL, ESZEROL);
return (false);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strishex_s: dmax exceeds max",
NULL, ESLEMAX);
return (false);
}
if (*dest == '\0') {
return (false);
}
while (*dest && dmax) {
if (((*dest >= '0') && (*dest <= '9')) ||
((*dest >= 'a') && (*dest <= 'f')) ||
((*dest >= 'A') && (*dest <= 'F'))) {
dest++;
dmax--;
} else {
return (false);
}
}
return (true);
}

View File

@ -0,0 +1,118 @@
/*------------------------------------------------------------------
* strislowercase_s.c
*
* February 2005, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strislowercase_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* bool
* strislowercase_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function checks if entire string is lowercase.
* The scanning stops at the first null or after dmax
* characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax maximum length of string
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dest shal be null terminated.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* true string is lowercase
* false string is not lowercase or an error occurred
*
* ALSO SEE
* strisalphanumeric_s(), strisascii_s(), strisdigit_s(),
* strishex_s(), strismixedcase_s(),
* strisuppercase_s()
*
*/
bool
strislowercase_s (const char *dest, rsize_t dmax)
{
if (!dest) {
invoke_safe_str_constraint_handler("strislowercase_s: "
"dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strislowercase_s: "
"dmax is 0",
NULL, ESZEROL);
return (false);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strislowercase_s: "
"dmax exceeds max",
NULL, ESLEMAX);
return (false);
}
if (*dest == '\0') {
return (false);
}
while (*dest && dmax) {
if ((*dest < 'a') || (*dest > 'z')) {
return (false);
}
dest++;
dmax--;
}
return (true);
}

View File

@ -0,0 +1,119 @@
/*------------------------------------------------------------------
* strismixedcase_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strismixedcase_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* bool
* strismixedcase_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function checks that the entire string is mixed
* case. The scanning stops at the first null or after
* dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax maximum length of string
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* true string is mixed case
* false string is not mixed case or error
*
* ALSO SEE
* strisalphanumeric_s(), strisascii_s(), strisdigit_s(),
* strishex_s(), strislowercase_s(),
* strisuppercase_s()
*
*/
bool
strismixedcase_s (const char *dest, rsize_t dmax)
{
if (!dest) {
invoke_safe_str_constraint_handler("strismixedcase_s: "
"dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strismixedcase_s: "
"dmax is 0",
NULL, ESZEROL);
return (false);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strismixedcase_s: "
"dmax exceeds max",
NULL, ESLEMAX);
return (false);
}
if (*dest == '\0') {
return (false);
}
while (*dest) {
if (((*dest >= 'a') && (*dest <= 'z')) ||
((*dest >= 'A') && (*dest <= 'Z'))) {
dest++;
dmax--;
} else {
return (false);
}
}
return (true);
}

View File

@ -0,0 +1,168 @@
/*------------------------------------------------------------------
* strispassword_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strispassword_s
*
* SYNOPSIS
* #include "strlib.h"
* bool
* strispassword_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function validates the make-up of a password string.
* -SAFE_STR_PASSWORD_MIN_LENGTH character minimum
* -SAFE_STR_PASSWORD_MAX_LENGTH character maximum
* -at least SAFE_STR_MIN_LOWERCASE lower case characters
* -at least SAFE_STR_MIN_UPPERCASE upper case characters
* -at least SAFE_STR_MIN_NUMBERS number
* -at least SAFE_STR_MIN_SPECIALS special
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax length of password string
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* length > SAFE_STR_PASSWORD_MIN_LENGTH
* length < SAFE_STR_PASSWORD_MAX_LENGTH
* dest shall not be unterminated
*
* RETURN VALUE
* true, string has valid password makeup
* false, string does not meet requirements or an error occurred
*
* ALSO SEE
* strzero_s()
*
*/
bool
strispassword_s (const char *dest, rsize_t dmax)
{
uint32_t cnt_all;
uint32_t cnt_lowercase;
uint32_t cnt_uppercase;
uint32_t cnt_numbers;
uint32_t cnt_specials;
if (!dest) {
invoke_safe_str_constraint_handler("strispassword_s: "
"dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax < SAFE_STR_PASSWORD_MIN_LENGTH) {
invoke_safe_str_constraint_handler("strispassword_s: "
"dest is too short",
NULL, ESLEMIN);
return (false);
}
if (dmax > SAFE_STR_PASSWORD_MAX_LENGTH) {
invoke_safe_str_constraint_handler("strispassword_s: "
"dest exceeds max",
NULL, ESLEMAX);
return (false);
}
if (*dest == '\0') {
return (false);
}
cnt_all = cnt_lowercase = cnt_uppercase = 0;
cnt_numbers = cnt_specials = 0;
while (*dest) {
if (dmax == 0) {
invoke_safe_str_constraint_handler(
"strispassword_s: dest is unterminated",
NULL, ESUNTERM);
return (false);
}
dmax--;
cnt_all++;
if ((*dest >= '0') && (*dest <= '9')) {
cnt_numbers++;
} else if ((*dest >= 'a') && (*dest <= 'z')) {
cnt_lowercase++;
} else if ((*dest >= 'A') && (*dest <= 'Z')) {
cnt_uppercase++;
/* allow all specials */
} else if ((*dest >= 33) && (*dest <= 47)) {
cnt_specials++;
} else if ((*dest >= 58) && (*dest <= 64)) {
cnt_specials++;
} else if ((*dest >= 91) && (*dest <= 94)) {
cnt_specials++;
} else if ((*dest >= 95) && (*dest <= 96)) {
cnt_specials++;
} else if ((*dest >= 123) && (*dest <= 126)) {
cnt_specials++;
} else {
/* illegal char in password string */
return (false);
}
dest++;
}
if (cnt_all < SAFE_STR_PASSWORD_MAX_LENGTH &&
cnt_numbers >= SAFE_STR_MIN_NUMBERS &&
cnt_lowercase >= SAFE_STR_MIN_LOWERCASE &&
cnt_uppercase >= SAFE_STR_MIN_UPPERCASE &&
cnt_specials >= SAFE_STR_MIN_SPECIALS ) {
return (true);
} else {
return (false);
}
}

View File

@ -0,0 +1,117 @@
/*------------------------------------------------------------------
* strisuppercase_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strisuppercase_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* bool
* strisuppercase_s(const char *dest, rsize_t dmax)
*
* DESCRIPTION
* This function checks if entire string is uppercase
* The scanning stops at the first null or after dmax
* characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax maximum length of string
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* true string is uppercase
* false string is not uppercase or an error occurred
*
* ALSO SEE
* strisalphanumeric_s(), strisascii_s(), strisdigit_s(),
* strishex_s(), strislowercase_s(), strismixedcase_s(),
*
*/
bool
strisuppercase_s (const char *dest, rsize_t dmax)
{
if (!dest) {
invoke_safe_str_constraint_handler("strisuppercase_s: "
"dest is null",
NULL, ESNULLP);
return (false);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strisuppercase_s: "
"dmax is 0",
NULL, ESZEROL);
return (false);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strisuppercase_s: "
"dmax exceeds max",
NULL, ESLEMAX);
return (false);
}
if (*dest == '\0') {
return (false);
}
while (*dest) {
if ((*dest < 'A') || (*dest > 'Z')) {
return (false);
}
dest++;
dmax--;
}
return (true);
}

View File

@ -0,0 +1,131 @@
/*------------------------------------------------------------------
* strlastchar_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strlastchar_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strlastchar_s(char *dest, rsize_t dmax, char c, char **last)
*
* DESCRIPTION
* Returns a pointer to the last occurrence of character c in
* dest. The scanning stops at the first null or after dmax
* characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax restricted maximum length of string
*
* c character to locate
*
* last returned pointer to last occurrence
*
* OUTPUT PARAMETERS
* last updated pointer to last occurrence
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* last shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* pointer to the last occurrence, when the return code is OK
*
* EOK pointer to the last occurence is returned
* ESNOTFND c not found in dest
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strfirstchar_s(), strfirstdiff_s(), strfirstsame_s(),
* strlastdiff_s(), strlastsame_s()
*
*/
errno_t
strlastchar_s(char *dest, rsize_t dmax, char c, char **last)
{
if (last == NULL) {
invoke_safe_str_constraint_handler("strlastchar_s: last is null",
NULL, ESNULLP);
return (ESNULLP);
}
*last = NULL;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strlastchar_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strlastchar_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strlastchar_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
while (*dest && dmax) {
if (*dest == c) {
*last = dest;
}
dest++;
dmax--;
}
if (*last == NULL) {
return (ESNOTFND);
} else {
return (EOK);
}
}

View File

@ -0,0 +1,151 @@
/*------------------------------------------------------------------
* strlastdiff_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strlastdiff_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strlastdiff_s(const char *dest, rsize_t dmax,
* const char *src, rsize_t *index)
*
* DESCRIPTION
* Returns the index of the last character that is different
* between dest and src. Index is valid only for EOK.
* The scanning stops at the first null in dest or src, or
* after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to compare against
*
* dmax restricted maximum length of string dest
*
* src pointer to the string to be compared to dest
*
* index pointer to returned index of last difference
*
* OUTPUT PARAMETERS
* index updated index of last difference
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* indicator shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* index to last difference, when the return code is OK
*
* EOK index to last diff is returned
* ESNODIFF no difference
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strfirstchar_s(), strfirstdiff_s(), strfirstsame_s(),
* strlastchar_s(), strlastsame_s()
*
*/
errno_t
strlastdiff_s(const char *dest, rsize_t dmax,
const char *src, rsize_t *index)
{
const char *rp;
bool there_is_a_diff = false;
if (index == NULL) {
invoke_safe_str_constraint_handler("strlastdiff_s: index is null",
NULL, ESNULLP);
return (ESNULLP);
}
*index = 0;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strlastdiff_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strlastdiff_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strlastdiff_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strlastdiff_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* hold reference point */
rp = dest;
/*
* find the last diff
*/
while (*dest && *src && dmax) {
if (*dest != *src) {
there_is_a_diff = true;
*index = dest - rp;
}
dest++;
src++;
dmax--;
}
if (there_is_a_diff) {
return (EOK);
} else {
return (ESNODIFF);
}
}

View File

@ -0,0 +1,152 @@
/*------------------------------------------------------------------
* strlastsame_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strlastsame_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strlastsame_s(const char *dest, rsize_t dmax,
* const char *src, rsize_t *index)
*
* DESCRIPTION
* Returns the index of the last character that is the
* same between dest and src. The scanning stops at the
* first nul in dest or src, or after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to compare against
*
* dmax restricted maximum length of string dest
*
* src pointer to the string to be compared to dest
*
* index pointer to returned index
*
* OUTPUT PARAMETERS
* index updated index
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall not be a null pointer.
* indicator shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* index to last same char, when the return code is OK
*
* EOK index to last same char is returned
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESNOTFND not found
* ESUNTERM string unterminated
*
* ALSO SEE
* strfirstchar_s(), strfirstdiff_s(), strfirstsame_s(),
* strlastchar_s(), strlastdiff_s()
*
*/
errno_t
strlastsame_s (const char *dest, rsize_t dmax,
const char *src, rsize_t *index)
{
const char *rp;
bool similarity;
if (index == NULL) {
invoke_safe_str_constraint_handler("strlastsame_s: index is null",
NULL, ESNULLP);
return (ESNULLP);
}
*index = 0;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strlastsame_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strlastsame_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strlastsame_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strlastsame_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* hold reference point */
rp = dest;
/*
* find the last offset
*/
similarity = false;
while (*dest && *src && dmax) {
if (*dest == *src) {
similarity = true;
*index = (uint32_t)(dest - rp);
}
dest++;
src++;
dmax--;
}
if (similarity) {
return (EOK);
} else {
return (ESNOTFND);
}
}

View File

@ -0,0 +1,159 @@
/*------------------------------------------------------------------
* strljustify_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reseved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strljustify_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strljustify_s(char *dest, rsize_t dmax)
*
* DESCRIPTION
* Removes beginning whitespace from the string pointed to by
* dest by shifting the text left over writting the beginning
* whitespace, left justifying the text. The left justified
* text is null terminated.
*
* The text is shifted so the original pointer can continue
* to be used.
*
* EXTENSION TO
* ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to left justify
*
* dmax restricted maximum length of string
*
* OUTPUT PARAMETERS
* dest left justified
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
* dest shall be null terminated
*
* RETURN VALUE
* EOK
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESUNTERM dest was not null terminated
*
* ALSO SEE
* strremovews_s(),
*
*/
errno_t
strljustify_s (char *dest, rsize_t dmax)
{
char *orig_dest;
rsize_t orig_dmax;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strljustify_s_s: "
"dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strljustify_s_s: "
"dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strljustify_s_s: "
"dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/*
* corner case, a dmax of one allows only for a null
*/
if (*dest == '\0' || dmax <= RSIZE_MIN_STR) {
*dest = '\0';
return (EOK);
}
orig_dmax = dmax;
orig_dest = dest;
/*
* scan the string to be sure it is properly terminated
*/
while (*dest) {
if (dmax == 0) {
while (orig_dmax) { *orig_dest++ = '\0'; orig_dmax--; }
invoke_safe_str_constraint_handler(
"strljustify_s: dest is unterminated",
NULL, ESUNTERM);
return (ESUNTERM);
}
dmax--;
dest++;
}
/*
* find first non-white space char
*/
dest = orig_dest;
while ((*dest == ' ') || (*dest == '\t')) {
dest++;
}
/*
* shift text, removing spaces, to left justify
*/
if (orig_dest != dest && *dest) {
while (*dest) {
*orig_dest++ = *dest;
*dest++ = ' ';
}
*orig_dest = '\0';
}
return (EOK);
}

202
casadm/safeclib/strncat_s.c Normal file
View File

@ -0,0 +1,202 @@
/*------------------------------------------------------------------
* strncat_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
errno_t
strncat_s (char * restrict dest, rsize_t dmax, const char * restrict src, rsize_t slen)
{
rsize_t orig_dmax;
char *orig_dest;
const char *overlap_bumper;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strncat_s: dest is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strncat_s: src is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (slen > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strncat_s: slen exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strncat_s: dmax is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strncat_s: dmax exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
/* hold base of dest in case src was not copied */
orig_dmax = dmax;
orig_dest = dest;
if (dest < src) {
overlap_bumper = src;
/* Find the end of dest */
while (*dest != '\0') {
if (dest == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strncat_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
dest++;
dmax--;
if (dmax == 0) {
handle_error(orig_dest, orig_dmax, "strncat_s: "
"dest unterminated",
ESUNTERM);
return RCNEGATE(ESUNTERM);
}
}
while (dmax > 0) {
if (dest == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strncat_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
/*
* Copying truncated before the source null is encountered
*/
if (slen == 0) {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null remaining string */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#else
*dest = '\0';
#endif
return RCNEGATE(EOK);
}
*dest = *src;
if (*dest == '\0') {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null slack to clear data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#endif
return RCNEGATE(EOK);
}
dmax--;
slen--;
dest++;
src++;
}
} else {
overlap_bumper = dest;
/* Find the end of dest */
while (*dest != '\0') {
/*
* NOTE: no need to check for overlap here since src comes first
* in memory and we're not incrementing src here.
*/
dest++;
dmax--;
if (dmax == 0) {
handle_error(orig_dest, orig_dmax, "strncat_s: "
"dest unterminated",
ESUNTERM);
return RCNEGATE(ESUNTERM);
}
}
while (dmax > 0) {
if (src == overlap_bumper) {
handle_error(orig_dest, orig_dmax, "strncat_s: "
"overlapping objects",
ESOVRLP);
return RCNEGATE(ESOVRLP);
}
/*
* Copying truncated
*/
if (slen == 0) {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null remaining string */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#else
*dest = '\0';
#endif
return RCNEGATE(EOK);
}
*dest = *src;
if (*dest == '\0') {
#ifdef SAFECLIB_STR_NULL_SLACK
/* null slack to clear any data */
while (dmax) { *dest = '\0'; dmax--; dest++; }
#endif
return RCNEGATE(EOK);
}
dmax--;
slen--;
dest++;
src++;
}
}
/*
* the entire src was not copied, so the string will be nulled.
*/
handle_error(orig_dest, orig_dmax, "strncat_s: not enough "
"space for src",
ESNOSPC);
return RCNEGATE(ESNOSPC);
}
EXPORT_SYMBOL(strncat_s)

View File

@ -0,0 +1,112 @@
/*------------------------------------------------------------------
* strnterminate_s.c
*
* February 2011, Bo Berry
*
* Copyright (c) 2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strnterminate_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* rsize_t
* strnterminate_s(char *dest, rsize_t dmax)
*
* DESCRIPTION
* The strnterminate_s function will terminate the string if a
* null is not encountered before dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731-1, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest - pointer to string
*
* dmax - restricted maximum length
*
* OUTPUT PARAMETERS
* dest - dest is terminated if needed
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer
* dmax shall not be greater than RSIZE_MAX_STR
* dmax shall not equal zero
*
* RETURN VALUE
* The function returns a terminated string. If a null is not
* encountered prior to dmax characters, the dmax character is
* set to null terminating the string. The string length is
* also returned.
*
* ALSO SEE
* strnlen_s()
*
*/
rsize_t
strnterminate_s (char *dest, rsize_t dmax)
{
rsize_t count;
if (dest == NULL) {
return (0);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strnterminate_s: dmax is 0",
NULL, ESZEROL);
return (0);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strnterminate_s: dmax exceeds max",
NULL, ESLEMAX);
return (0);
}
count = 0;
while (dmax > 1) {
if (*dest) {
count++;
dmax--;
dest++;
} else {
break;
}
}
*dest = '\0';
return (count);
}

163
casadm/safeclib/strpbrk_s.c Normal file
View File

@ -0,0 +1,163 @@
/*------------------------------------------------------------------
* strpbrk_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strpbrk_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strpbrk_s(char *dest, rsize_t dmax,
* char *src, rsize_t slen, char **first)
*
* DESCRIPTION
* Returns a pointer, first, to the first ocurrence of any character
* in src which is contained in dest.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string
*
* dmax restricted maximum length of string dest
*
* src pointer to string
*
* slen restricted length of string src
*
* first returned pointer to first occurence
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* first shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* pointer to the first ocurrence of any character
* contained in src
*
* EOK count
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strfirstchar_s(), strlastchar_s(), strfirstdiff_s(),
* strfirstsame_s(), strlastdiff_s(), strlastsame_s()
*
*/
errno_t
strpbrk_s (char *dest, rsize_t dmax,
char *src, rsize_t slen, char **first)
{
char *ps;
rsize_t len;
if (first == NULL) {
invoke_safe_str_constraint_handler("strpbrk_s: count is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
*first = NULL;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strpbrk_s: dest is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strpbrk_s: src is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strpbrk_s: dmax is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strpbrk_s: dmax exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
if (slen == 0 ) {
invoke_safe_str_constraint_handler("strpbrk_s: slen is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (slen > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strpbrk_s: slen exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
/*
* look for a matching char in the substring src
*/
while (*dest && dmax) {
ps = src;
len = slen;
while (*ps) {
/* check for a match with the substring */
if (*dest == *ps) {
*first = dest;
return RCNEGATE(EOK);
}
ps++;
len--;
}
dest++;
dmax--;
}
return RCNEGATE(ESNOTFND);
}
EXPORT_SYMBOL(strpbrk_s);

View File

@ -0,0 +1,127 @@
/*------------------------------------------------------------------
* strprefix_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strprefix_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strprefix_s(const char *dest, rsize_t dmax, const char *src)
*
* DESCRIPTION
* Determines if the prefix pointed to by src is at the
* beginning of string pointed to by dest. The prefix
* must be a complete match in dest. Useful for command
* or user input parsing. The scanning stops at the first
* null in dest or src, or after dmax characters.
*
* EXTENSION TO
* ISO/IEC TR 24731-1, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to compare against
*
* dmax restricted maximum length of dest
*
* src pointer to the prefix
*
* OUTPUT PARAMETERS
* none
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* dmax shall not equal zero.
* dmax shall not be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* EOK successful operation, prefix present in dest
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESNOTFND prefix not found in dest
*
* ALSO SEE
* strspn_s(), strcspn_s(), strpbrk_s(), strstr_s()
*
*/
errno_t
strprefix_s (const char *dest, rsize_t dmax, const char *src)
{
if (dest == NULL) {
invoke_safe_str_constraint_handler("strprefix_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strprefix_s: src is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strprefix_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strprefix_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
if (*src == '\0') {
return (ESNOTFND);
}
while (*src && dmax) {
if (*dest != *src) {
return (ESNOTFND);
}
dmax--;
dest++;
src++;
}
return (EOK);
}

View File

@ -0,0 +1,163 @@
/*------------------------------------------------------------------
* strremovews_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights resevered.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strremovews_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strremovews_s(char *dest, rsize_t dmax)
*
* DESCRIPTION
* Removes beginning and trailing whitespace from the string pointed to by
* dest by shifting the text left over writting the beginning whitespace.
* The shifted-trimmed text is null terminated.
*
* The text is shifted so the original pointer can continue to be used. This
* is useful when the memory was malloc'ed and will need to be freed.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to remove whitespace
*
* dmax restricted maximum length of string
*
* RUNTIME CONSTRAINTS
* dest shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
* dest shall be null terminated
*
* RETURN VALUE
* EOK
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESUNTERM dest was not null terminated
*
* SEE ALSO
* strljustify_s(),
*
*/
errno_t
strremovews_s (char *dest, rsize_t dmax)
{
char *orig_dest;
char *orig_end;
rsize_t orig_dmax;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strremovews_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strremovews_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strremovews_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/*
* corner case, a dmax of one requires a null
*/
if (*dest == '\0' || dmax <= RSIZE_MIN_STR) {
*dest = '\0';
return (EOK);
}
orig_dest = dest;
orig_dmax = dmax;
/*
* scan the string to be sure it is properly terminated
*/
while (*dest) {
if (dmax == 0) {
while (orig_dmax) { *orig_dest++ = '\0'; orig_dmax--; }
invoke_safe_str_constraint_handler(
"strremovews_s: dest is unterminated",
NULL, ESUNTERM);
return (ESUNTERM);
}
dmax--;
dest++;
}
/*
* find first non-white space char
*/
orig_end = dest-1;
dest = orig_dest;
while ((*dest == ' ') || (*dest == '\t')) {
dest++;
}
/*
* shift the text over the leading spaces
*/
if (orig_dest != dest && *dest) {
while (*dest) {
*orig_dest++ = *dest;
*dest++ = ' ';
}
*dest = '\0';
}
/*
* strip trailing whitespace
*/
dest = orig_end;
while ((*dest == ' ') || (*dest == '\t')) {
*dest = '\0';
dest--;
}
return (EOK);
}

170
casadm/safeclib/strspn_s.c Normal file
View File

@ -0,0 +1,170 @@
/*------------------------------------------------------------------
* strspn_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011, 2013 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strspn_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strspn_s(const char *dest, rsize_t dmax,
* const char *src, rsize_t slen, rsize_t *count)
*
* DESCRIPTION
* This function computes the prefix length of the string
* pointed to by dest which consists entirely of characters
* that are included from the string pointed to by src.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to determine the prefix
*
* dmax restricted maximum length of string dest
*
* src pointer to exclusion string
*
* slen restricted maximum length of string src
*
* count pointer to a count variable that will be updated
* with the dest substring length
*
* OUTPUT PARAMETERS
* count updated count
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* count shall not be a null pointer.
* dmax shall not be 0
* dmax shall not be greater than RSIZE_MAX_STR
*
* RETURN VALUE
* EOK count
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strcspn_s(), strpbrk_s(), strstr_s(), strprefix_s()
*
*/
errno_t
strspn_s (const char *dest, rsize_t dmax,
const char *src, rsize_t slen, rsize_t *count)
{
const char *scan2;
rsize_t smax;
bool match_found;
if (count== NULL) {
invoke_safe_str_constraint_handler("strspn_s: count is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
*count = 0;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strspn_s: dest is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strspn_s: src is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (dmax == 0 ) {
invoke_safe_str_constraint_handler("strspn_s: dmax is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strspn_s: dmax exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
if (slen == 0 ) {
invoke_safe_str_constraint_handler("strspn_s: slen is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (slen > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strspn_s: slen exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
while (*dest && dmax) {
/*
* Scan the entire src string for each dest character, counting
* inclusions.
*/
match_found = false;
smax = slen;
scan2 = src;
while (*scan2 && smax) {
if (*dest == *scan2) {
match_found = true;
break;
}
scan2++;
smax--;
}
if (match_found) {
(*count)++;
} else {
break;
}
dest++;
dmax--;
}
return RCNEGATE(EOK);
}
EXPORT_SYMBOL(strspn_s);

179
casadm/safeclib/strstr_s.c Normal file
View File

@ -0,0 +1,179 @@
/*------------------------------------------------------------------
* strstr_s.c
*
* November 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strstr_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strstr_s(char *dest, rsize_t dmax,
* const char *src, rsize_t slen, char **substring)
*
* DESCRIPTION
* The strstr_s() function locates the first occurrence of the
* substring pointed to by src which would be located in the
* string pointed to by dest.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string to be searched for the substring
*
* dmax restricted maximum length of dest string
*
* src pointer to the sub string
*
* slen the maximum number of characters to copy from src
*
* substring the returned substring pointer
*
* OUTPUT PARAMETERS
* substring returned substring pointer
*
* RUNTIME CONSTRAINTS
* Neither dest nor src shall be a null pointer.
* Meither dmax nor slen shall be zero.
* Neither dmax nor slen shall be greater than RSIZE_MAX_STR.
*
* RETURN VALUE
* EOK successful operation, substring found.
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
* ESNOTFND substring not found
*
* ALSO SEE
* strprefix_s(), strspn_s(), strcspn_s(), strpbrk_s()
*
*/
errno_t
strstr_s (char *dest, rsize_t dmax,
const char *src, rsize_t slen, char **substring)
{
rsize_t len;
rsize_t dlen;
int i;
if (substring == NULL) {
invoke_safe_str_constraint_handler("strstr_s: substring is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
*substring = NULL;
if (dest == NULL) {
invoke_safe_str_constraint_handler("strstr_s: dest is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strstr_s: dmax is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strstr_s: dmax exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
if (src == NULL) {
invoke_safe_str_constraint_handler("strstr_s: src is null",
NULL, ESNULLP);
return RCNEGATE(ESNULLP);
}
if (slen == 0) {
invoke_safe_str_constraint_handler("strstr_s: slen is 0",
NULL, ESZEROL);
return RCNEGATE(ESZEROL);
}
if (slen > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strstr_s: slen exceeds max",
NULL, ESLEMAX);
return RCNEGATE(ESLEMAX);
}
/*
* src points to a string with zero length, or
* src equals dest, return dest
*/
if (*src == '\0' || dest == src) {
*substring = dest;
return RCNEGATE(EOK);
}
while (*dest && dmax) {
i = 0;
len = slen;
dlen = dmax;
while (src[i] && dlen) {
/* not a match, not a substring */
if (dest[i] != src[i]) {
break;
}
/* move to the next char */
i++;
len--;
dlen--;
if (src[i] == '\0' || !len) {
*substring = dest;
return RCNEGATE(EOK);
}
}
dest++;
dmax--;
}
/*
* substring was not found, return NULL
*/
*substring = NULL;
return RCNEGATE(ESNOTFND);
}
EXPORT_SYMBOL(strstr_s);

102
casadm/safeclib/strzero_s.c Normal file
View File

@ -0,0 +1,102 @@
/*------------------------------------------------------------------
* strzero_s.c
*
* October 2008, Bo Berry
*
* Copyright (c) 2008-2011 by Cisco Systems, Inc
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
/**
* NAME
* strzero_s
*
* SYNOPSIS
* #include "safe_str_lib.h"
* errno_t
* strzero_s(char *dest, rsize_t dmax)
*
* DESCRIPTION
* Nulls dmax characters of dest. This function can be used
* to clear strings that contained sensitive data.
*
* EXTENSION TO
* ISO/IEC TR 24731, Programming languages, environments
* and system software interfaces, Extensions to the C Library,
* Part I: Bounds-checking interfaces
*
* INPUT PARAMETERS
* dest pointer to string that will be nulled.
*
* dmax restricted maximum length of dest
*
* OUTPUT PARAMETERS
* dest updated string
*
* RETURN VALUE
* EOK successful operation
* ESNULLP NULL pointer
* ESZEROL zero length
* ESLEMAX length exceeds max limit
*
* ALSO SEE
* strispassword_s()
*
*/
errno_t
strzero_s (char *dest, rsize_t dmax)
{
if (dest == NULL) {
invoke_safe_str_constraint_handler("strzero_s: dest is null",
NULL, ESNULLP);
return (ESNULLP);
}
if (dmax == 0) {
invoke_safe_str_constraint_handler("strzero_s: dmax is 0",
NULL, ESZEROL);
return (ESZEROL);
}
if (dmax > RSIZE_MAX_STR) {
invoke_safe_str_constraint_handler("strzero_s: dmax exceeds max",
NULL, ESLEMAX);
return (ESLEMAX);
}
/* null string to eliminate data */
while (dmax) {
*dest = '\0';
dmax--;
dest++;
}
return (EOK);
}