├── Makefile ├── README ├── adc_demo └── adc_demo.sh ├── blink ├── Makefile ├── blink.c ├── ignore_handler_s.c ├── safe_str_constraint.c └── snprintf_support.c ├── doc └── README ├── hello ├── Makefile └── hello.c ├── include ├── safe_lib.h ├── safe_lib_errno.h ├── safe_lib_errno.h.in ├── safe_mem_lib.h ├── safe_str_lib.h ├── safe_types.h ├── safe_types.h.in └── snprintf_s.h ├── led_control ├── Makefile ├── led_control.c └── led_control.h ├── pio-interrupt ├── Makefile └── gpio_interrupt.c ├── pio_ilc_demo ├── Makefile ├── README └── intel_pio_ilc.c ├── safeclib ├── abort_handler_s.c ├── ignore_handler_s.c ├── mem_primitives_lib.c ├── mem_primitives_lib.h ├── memcmp16_s.c ├── memcmp32_s.c ├── memcmp_s.c ├── memcpy16_s.c ├── memcpy32_s.c ├── memcpy_s.c ├── memmove16_s.c ├── memmove32_s.c ├── memmove_s.c ├── memset16_s.c ├── memset32_s.c ├── memset_s.c ├── memzero16_s.c ├── memzero32_s.c ├── memzero_s.c ├── safe_mem_constraint.c ├── safe_mem_constraint.h ├── safe_str_constraint.c ├── safe_str_constraint.h ├── safeclib_private.h ├── snprintf_support.c ├── stpcpy_s.c ├── stpncpy_s.c ├── strcasecmp_s.c ├── strcasestr_s.c ├── strcat_s.c ├── strcmp_s.c ├── strcmpfld_s.c ├── strcpy_s.c ├── strcpyfld_s.c ├── strcpyfldin_s.c ├── strcpyfldout_s.c ├── strcspn_s.c ├── strfirstchar_s.c ├── strfirstdiff_s.c ├── strfirstsame_s.c ├── strisalphanumeric_s.c ├── strisascii_s.c ├── strisdigit_s.c ├── strishex_s.c ├── strislowercase_s.c ├── strismixedcase_s.c ├── strispassword_s.c ├── strisuppercase_s.c ├── strlastchar_s.c ├── strlastdiff_s.c ├── strlastsame_s.c ├── strljustify_s.c ├── strncat_s.c ├── strncpy_s.c ├── strnlen_s.c ├── strnterminate_s.c ├── strpbrk_s.c ├── strprefix_s.c ├── strremovews_s.c ├── strspn_s.c ├── strstr_s.c ├── strtok_s.c ├── strtolowercase_s.c ├── strtouppercase_s.c ├── strzero_s.c ├── wcpcpy_s.c ├── wcscat_s.c ├── wcscpy_s.c ├── wcsncat_s.c ├── wcsncpy_s.c ├── wcsnlen_s.c ├── wmemcmp_s.c ├── wmemcpy_s.c ├── wmemmove_s.c └── wmemset_s.c ├── scroll_client ├── Makefile └── scroll_client.c ├── scroll_server ├── Makefile ├── ignore_handler_s.c ├── safe_str_constraint.c ├── scroll_server.c └── snprintf_support.c ├── syschk ├── Makefile ├── ignore_handler_s.c ├── safe_str_constraint.c ├── strcat_s.c ├── strcpy_s.c ├── strnlen_s.c └── syschk.c └── toggle ├── Makefile └── toggle.c /Makefile: -------------------------------------------------------------------------------- 1 | DIRS=led_control hello blink toggle scroll_server scroll_client syschk 2 | 3 | all: 4 | @for subd in $(DIRS); do \ 5 | $(MAKE) -C $$subd; \ 6 | done 7 | 8 | clean: 9 | @for subd in $(DIRS); do \ 10 | $(MAKE) -C $$subd clean; \ 11 | done 12 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is the repository for the source code necessary to run reference designs. 2 | 3 | Each reference design is on its own branch. 4 | 5 | This repository includes software from several sources under a collection of compatible open source licenses; refer to the specific copyright/license embedded in each source file. 6 | -------------------------------------------------------------------------------- /adc_demo/adc_demo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Copyright (C) 2015 Altera Corporation. All rights reserved. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms and conditions of the GNU General Public License, 7 | # version 2, as published by the Free Software Foundation. 8 | # 9 | # This program is distributed in the hope it will be useful, but WITHOUT 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 | # more details. 13 | # 14 | # You should have received a copy of the GNU General Public License along with 15 | # this program. If not, see . 16 | 17 | # Internals 18 | SELF="$(basename $0)" 19 | 20 | # Constants 21 | readonly BAR_COUNT=24 # Total count of display bars 22 | readonly QUANTUM_LEVEL=4096 # Quantum levels for 12bits ADC 23 | readonly ADC_DEFAULT_SYSFS="/sys/bus/iio/devices/iio:device0/in_voltage0_adc1-ch7_raw" 24 | 25 | # Variables 26 | RAW= # Raw Value read from ADC 27 | VAR= # Nominalize ADC value to Bar Count 28 | BAR= # Number of bars to be displayed 29 | ADC_SYSFS= # ADC sysfs to read 30 | 31 | # Display help 32 | help() { 33 | echo "ADC Demonstration to display ADC reading" 34 | echo "usage: ${SELF} -t " 35 | echo "-t file: specifies ADC sysfs. Default points to," 36 | echo " ${ADC_DEFAULT_SYSFS}" 37 | echo "-h : this message." 38 | return 0 39 | } 40 | 41 | # Display function 42 | display() { 43 | RAW="$(cat ${ADC_SYSFS})" 44 | VAR="$(expr ${RAW} \* ${BAR_COUNT} / ${QUANTUM_LEVEL})" 45 | BAR="$(printf '%0.s#' $(seq 0 ${VAR}))" 46 | printf "\r\033[2JADC Demo: Press CTRL-C to quit.\n" 47 | printf "Raw Value: %-4s [%-24s] \033[1;44f" ${RAW} ${BAR} 48 | return 0 49 | } 50 | 51 | # Script starts here 52 | # Option parsing 53 | while [ $# -gt 0 ]; do 54 | case $1 in 55 | -h) help; exit 0 ;; 56 | -t) ADC_SYSFS=$2 ; shift ;; 57 | *) echo "${SELF}: error: unknown option ${1}" ; help ; exit 1 ;; 58 | esac 59 | shift 60 | done 61 | 62 | # Error checks 63 | if [ -z "${ADC_SYSFS}" ]; then 64 | ADC_SYSFS=${ADC_DEFAULT_SYSFS} 65 | fi 66 | 67 | if [ ! -f "${ADC_SYSFS}" ]; then 68 | echo "${SELF}: error: ${ADC_SYSFS}: no such file." 69 | exit 1 70 | fi 71 | 72 | # Start display 73 | while [ true ]; do 74 | display 75 | done 76 | -------------------------------------------------------------------------------- /blink/Makefile: -------------------------------------------------------------------------------- 1 | C_SRC := blink.c snprintf_support.c safe_str_constraint.c ignore_handler_s.c 2 | CFLAGS := -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security -Werror -Wall -fstack-protector-all -O -I../led_control -I../include -I../safeclib 3 | 4 | ifeq ($(or $(COMSPEC),$(ComSpec)),) 5 | RM := rm -rf 6 | else 7 | RM := cs-rm -rf 8 | endif 9 | 10 | ELF ?= $(basename $(firstword $(C_SRC))) 11 | OBJ := $(patsubst %.c,%.o,$(C_SRC)) 12 | 13 | .PHONY: all 14 | all: $(ELF) $(OBJ) 15 | 16 | .PHONY: 17 | clean: 18 | $(RM) $(ELF) *.o 19 | 20 | $(OBJ): %.o: %.c 21 | $(CC) $(CFLAGS) -c $< -o $@ 22 | 23 | $(ELF): $(OBJ) 24 | $(CC) $(CFLAGS) $(OBJ) ../led_control/led_control.a -o $@ -lpthread 25 | 26 | -------------------------------------------------------------------------------- /blink/blink.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014, Altera Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * - Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * - Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * - Neither the name of the Altera Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "led_control.h" 36 | 37 | #define STRSIZE 256 38 | 39 | int main(int argc, char** argv) 40 | { 41 | FILE *fp; 42 | int led, delay_ms, i; 43 | char trigger[] = "timer"; 44 | char dir[STRSIZE]; 45 | char delay[STRSIZE]; 46 | int blink_interval_ms; 47 | 48 | for (i = 1; i < argc; i++) { 49 | if (strcmp("--help", argv[i]) == 0) { 50 | printf("Usage %s: \n" 51 | , argv[0]); 52 | return -1; 53 | } 54 | } 55 | 56 | if (argc != 3) { 57 | printf("Usage %s: \n" 58 | , argv[0]); 59 | return -1; 60 | } 61 | else { 62 | led = atoi(argv[1]); 63 | delay_ms = atoi(argv[2]); 64 | if (led < 0 || led > 3) { 65 | printf("Invalid LED number. Valid LED range is 0-3\n"); 66 | return -1; 67 | } 68 | if (delay_ms <= 0) { 69 | printf("Invalid blink delay.\n"); 70 | return -1; 71 | } 72 | } 73 | 74 | /* Checking if scrolling is still happening 75 | if it is, we will inform user, and bail 76 | */ 77 | char get_fifo_scroll[STRSIZE], seconds_bet_toggle[10]; 78 | snprintf(get_fifo_scroll, STRSIZE-1, "0"); 79 | fp = fopen("/home/root/.intelFPGA/frequency_fifo_scroll", "w"); 80 | if (fp == NULL) { 81 | printf("Failed opening fifo frequency_fifo_scroll\n"); 82 | return -1; 83 | } 84 | fputs(get_fifo_scroll, fp); 85 | fclose(fp); 86 | fp = fopen("/home/root/.intelFPGA/get_scroll_fifo", "r"); 87 | if (fp == NULL) { 88 | printf("Failed opening fifo get_scroll_fifo\n"); 89 | return -1; 90 | } 91 | 92 | if (fgets(seconds_bet_toggle, 10, fp) == NULL) 93 | { 94 | printf("Failed opening fifo frequency_fifo_scroll\n"); 95 | return -1; 96 | } 97 | 98 | fclose(fp); 99 | if (atoi(seconds_bet_toggle) > 0) { 100 | printf("LED is scrolling.\n"); 101 | printf("Disable scroll_client before changing blink delay\n"); 102 | return -1; 103 | } 104 | 105 | /* Blinking LED */ 106 | clear_led(led); 107 | blink_interval_ms = delay_ms; 108 | 109 | /* Setting the trigger to blinking */ 110 | setLEDtrigger(led, trigger, sizeof(trigger)); 111 | /* Setting the delay */ 112 | snprintf(dir, STRSIZE-1, "/sys/class/leds/fpga_led%d/delay_on", led); 113 | if ((fp = fopen(dir, "w")) == NULL) { 114 | printf("Failed to open the file %s\n" , dir); 115 | } 116 | else { 117 | snprintf(delay, STRSIZE-1,"%d", blink_interval_ms); 118 | if (fwrite(delay, 1, sizeof(delay), fp) == 0) 119 | { 120 | fclose(fp); 121 | return -1; 122 | } 123 | fclose(fp); 124 | } 125 | snprintf(dir, STRSIZE-1,"/sys/class/leds/fpga_led%d/delay_off", led); 126 | if ((fp = fopen(dir, "w")) == NULL) { 127 | printf("Failed to open the file %s\n" , dir); 128 | } 129 | else { 130 | snprintf(delay, STRSIZE-1,"%d", blink_interval_ms); 131 | if (fwrite(delay, 1, sizeof(delay), fp) == 0) 132 | { 133 | fclose(fp); 134 | return -1; 135 | } 136 | fclose(fp); 137 | } 138 | 139 | return 0; 140 | } 141 | -------------------------------------------------------------------------------- /blink/ignore_handler_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * ignore_handler_s.c 3 | * 4 | * 2012, Jonathan Toppins 5 | * 6 | * Copyright (c) 2012 Cisco Systems 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | 34 | /** 35 | * NAME 36 | * ignore_handler_s 37 | * 38 | * SYNOPSIS 39 | * #include "safe_lib.h" 40 | * void ignore_handler_s(const char *msg, void *ptr, errno_t error) 41 | * 42 | * DESCRIPTION 43 | * This function simply returns to the caller. 44 | * 45 | * SPECIFIED IN 46 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 47 | * and system software interfaces, Extensions to the C Library, 48 | * Part I: Bounds-checking interfaces 49 | * 50 | * INPUT PARAMETERS 51 | * msg Pointer to the message describing the error 52 | * 53 | * ptr Pointer to aassociated data. Can be NULL. 54 | * 55 | * error The error code encountered. 56 | * 57 | * RETURN VALUE 58 | * Returns no value. 59 | * 60 | * ALSO SEE 61 | * abort_handler_s() 62 | * 63 | */ 64 | 65 | void ignore_handler_s(const char *msg, void *ptr, errno_t error) 66 | { 67 | 68 | sldebug_printf("IGNORE CONSTRAINT HANDLER: (%u) %s\n", error, 69 | (msg) ? msg : "Null message"); 70 | return; 71 | } 72 | EXPORT_SYMBOL(ignore_handler_s) 73 | -------------------------------------------------------------------------------- /hello/Makefile: -------------------------------------------------------------------------------- 1 | C_SRC := hello.c 2 | CFLAGS := -Werror -Wall -fstack-protector-all -O 3 | 4 | ifeq ($(or $(COMSPEC),$(ComSpec)),) 5 | RM := rm -rf 6 | else 7 | RM := cs-rm -rf 8 | endif 9 | 10 | ELF ?= $(basename $(firstword $(C_SRC))) 11 | OBJ := $(patsubst %.c,%.o,$(C_SRC)) 12 | 13 | .PHONY: all 14 | all: $(ELF) $(OBJ) 15 | 16 | .PHONY: 17 | clean: 18 | $(RM) $(ELF) *.o 19 | 20 | $(OBJ): $(C_SRC) 21 | $(CC) $(CFLAGS) -c $< -o $@ 22 | 23 | $(ELF): $(OBJ) 24 | $(CC) $(CFLAGS) $(OBJ) ../led_control/led_control.a -o $@ -lpthread 25 | 26 | -------------------------------------------------------------------------------- /hello/hello.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014, Altera Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * - Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * - Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * - Neither the name of the Altera Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | 30 | int main(int argc, char** argv) { 31 | printf("Hello SoC FPGA!\n"); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /include/safe_lib.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_lib.h -- Safe C Library 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2012 Jonathan Toppins 6 | * Copyright (c) 2008-2013 by Cisco Systems, Inc 7 | * Copyright (c) 2021-2022 by Intel Corp 8 | * All rights reserved. 9 | * 10 | * Permission is hereby granted, free of charge, to any person 11 | * obtaining a copy of this software and associated documentation 12 | * files (the "Software"), to deal in the Software without 13 | * restriction, including without limitation the rights to use, 14 | * copy, modify, merge, publish, distribute, sublicense, and/or 15 | * sell copies of the Software, and to permit persons to whom the 16 | * Software is furnished to do so, subject to the following 17 | * conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 24 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 26 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 27 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 29 | * OTHER DEALINGS IN THE SOFTWARE. 30 | *------------------------------------------------------------------ 31 | */ 32 | 33 | #ifndef __SAFE_LIB_H__ 34 | #define __SAFE_LIB_H__ 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | /* Define safe_lib version number */ 41 | #define SAFEC_VERSION_MAJOR 1 42 | #define SAFEC_VERSION_MINOR 2 43 | #define SAFEC_VERSION_PATCH 0 44 | #define SAFEC_VERSION_STRING "1.2.0" 45 | 46 | #define SAFEC_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c)) 47 | #define SAFEC_VERSION \ 48 | SAFEC_VERSION_NUM(SAFEC_VERSION_MAJOR, \ 49 | SAFEC_VERSION_MINOR, \ 50 | SAFEC_VERSION_PATCH) 51 | 52 | #include "safe_types.h" 53 | #include "safe_lib_errno.h" 54 | 55 | /* C11 appendix K types - specific for bounds checking */ 56 | typedef size_t rsize_t; 57 | 58 | /* 59 | * We depart from the standard and allow memory and string operations to 60 | * have different max sizes. See the respective safe_mem_lib.h or 61 | * safe_str_lib.h files. 62 | */ 63 | /* #define RSIZE_MAX (~(rsize_t)0) - leave here for completeness */ 64 | 65 | typedef void (*constraint_handler_t) (const char * /* msg */, 66 | void * /* ptr */, 67 | errno_t /* error */); 68 | 69 | extern void abort_handler_s(const char *msg, void *ptr, errno_t error); 70 | extern void ignore_handler_s(const char *msg, void *ptr, errno_t error); 71 | 72 | #define sl_default_handler ignore_handler_s 73 | 74 | 75 | #ifdef __cplusplus 76 | } 77 | #endif 78 | #endif /* __SAFE_LIB_H__ */ 79 | -------------------------------------------------------------------------------- /include/safe_lib_errno.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_lib_errno.h -- Safe C Lib Error codes 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2012 Jonathan Toppins 6 | * Copyright (c) 2008-2013 by Cisco Systems, Inc 7 | * Copyright (c) 2021 by Intel Corp 8 | * All rights reserved. 9 | * 10 | * Permission is hereby granted, free of charge, to any person 11 | * obtaining a copy of this software and associated documentation 12 | * files (the "Software"), to deal in the Software without 13 | * restriction, including without limitation the rights to use, 14 | * copy, modify, merge, publish, distribute, sublicense, and/or 15 | * sell copies of the Software, and to permit persons to whom the 16 | * Software is furnished to do so, subject to the following 17 | * conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 24 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 26 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 27 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 29 | * OTHER DEALINGS IN THE SOFTWARE. 30 | *------------------------------------------------------------------ 31 | */ 32 | 33 | #ifndef __SAFE_LIB_ERRNO_H__ 34 | #define __SAFE_LIB_ERRNO_H__ 35 | 36 | #ifdef __KERNEL__ 37 | # include 38 | #else 39 | #include 40 | #endif /* __KERNEL__ */ 41 | 42 | /* 43 | * Safe Lib specific errno codes. These can be added to the errno.h file 44 | * if desired. 45 | */ 46 | #ifndef ESNULLP 47 | #define ESNULLP ( 400 ) /* null ptr */ 48 | #endif 49 | 50 | #ifndef ESZEROL 51 | #define ESZEROL ( 401 ) /* length is zero */ 52 | #endif 53 | 54 | #ifndef ESLEMIN 55 | #define ESLEMIN ( 402 ) /* length is below min */ 56 | #endif 57 | 58 | #ifndef ESLEMAX 59 | #define ESLEMAX ( 403 ) /* length exceeds max */ 60 | #endif 61 | 62 | #ifndef ESOVRLP 63 | #define ESOVRLP ( 404 ) /* overlap undefined */ 64 | #endif 65 | 66 | #ifndef ESEMPTY 67 | #define ESEMPTY ( 405 ) /* empty string */ 68 | #endif 69 | 70 | #ifndef ESNOSPC 71 | #define ESNOSPC ( 406 ) /* not enough space for s2 */ 72 | #endif 73 | 74 | #ifndef ESUNTERM 75 | #define ESUNTERM ( 407 ) /* unterminated string */ 76 | #endif 77 | 78 | #ifndef ESNODIFF 79 | #define ESNODIFF ( 408 ) /* no difference */ 80 | #endif 81 | 82 | #ifndef ESNOTFND 83 | #define ESNOTFND ( 409 ) /* not found */ 84 | #endif 85 | 86 | /* Additional for safe snprintf_s interfaces */ 87 | #ifndef ESBADFMT 88 | #define ESBADFMT ( 410 ) /* bad format string */ 89 | #endif 90 | 91 | #ifndef ESFMTTYP 92 | #define ESFMTTYP ( 411 ) /* bad format type */ 93 | #endif 94 | 95 | /* EOK may or may not be defined in errno.h */ 96 | #ifndef EOK 97 | #define EOK ( 0 ) 98 | #endif 99 | 100 | #endif /* __SAFE_LIB_ERRNO_H__ */ 101 | -------------------------------------------------------------------------------- /include/safe_lib_errno.h.in: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_lib_errno.h -- Safe C Lib Error codes 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2012 Jonathan Toppins 6 | * Copyright (c) 2008-2013 by Cisco Systems, Inc 7 | * Copyright (c) 2021 by Intel Corp 8 | * All rights reserved. 9 | * 10 | * Permission is hereby granted, free of charge, to any person 11 | * obtaining a copy of this software and associated documentation 12 | * files (the "Software"), to deal in the Software without 13 | * restriction, including without limitation the rights to use, 14 | * copy, modify, merge, publish, distribute, sublicense, and/or 15 | * sell copies of the Software, and to permit persons to whom the 16 | * Software is furnished to do so, subject to the following 17 | * conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 24 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 26 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 27 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 29 | * OTHER DEALINGS IN THE SOFTWARE. 30 | *------------------------------------------------------------------ 31 | */ 32 | 33 | #ifndef __SAFE_LIB_ERRNO_H__ 34 | #define __SAFE_LIB_ERRNO_H__ 35 | 36 | #ifdef __KERNEL__ 37 | # include 38 | #else 39 | @INSERT_ERRNO_H@ 40 | #endif /* __KERNEL__ */ 41 | 42 | /* 43 | * Safe Lib specific errno codes. These can be added to the errno.h file 44 | * if desired. 45 | */ 46 | #ifndef ESNULLP 47 | #define ESNULLP ( 400 ) /* null ptr */ 48 | #endif 49 | 50 | #ifndef ESZEROL 51 | #define ESZEROL ( 401 ) /* length is zero */ 52 | #endif 53 | 54 | #ifndef ESLEMIN 55 | #define ESLEMIN ( 402 ) /* length is below min */ 56 | #endif 57 | 58 | #ifndef ESLEMAX 59 | #define ESLEMAX ( 403 ) /* length exceeds max */ 60 | #endif 61 | 62 | #ifndef ESOVRLP 63 | #define ESOVRLP ( 404 ) /* overlap undefined */ 64 | #endif 65 | 66 | #ifndef ESEMPTY 67 | #define ESEMPTY ( 405 ) /* empty string */ 68 | #endif 69 | 70 | #ifndef ESNOSPC 71 | #define ESNOSPC ( 406 ) /* not enough space for s2 */ 72 | #endif 73 | 74 | #ifndef ESUNTERM 75 | #define ESUNTERM ( 407 ) /* unterminated string */ 76 | #endif 77 | 78 | #ifndef ESNODIFF 79 | #define ESNODIFF ( 408 ) /* no difference */ 80 | #endif 81 | 82 | #ifndef ESNOTFND 83 | #define ESNOTFND ( 409 ) /* not found */ 84 | #endif 85 | 86 | /* EOK may or may not be defined in errno.h */ 87 | #ifndef EOK 88 | #define EOK ( 0 ) 89 | #endif 90 | 91 | #endif /* __SAFE_LIB_ERRNO_H__ */ 92 | -------------------------------------------------------------------------------- /include/safe_mem_lib.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_mem_lib.h -- Safe C Library Memory APIs 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2012 Jonathan Toppins 6 | * Copyright (c) 2008-2012 by Cisco Systems, Inc 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #ifndef __SAFE_MEM_LIB_H__ 33 | #define __SAFE_MEM_LIB_H__ 34 | 35 | #include "safe_lib.h" 36 | #include 37 | 38 | #define RSIZE_MAX_MEM ( 256UL << 20 ) /* 256MB */ 39 | #define RSIZE_MAX_MEM16 ( RSIZE_MAX_MEM/2 ) 40 | #define RSIZE_MAX_MEM32 ( RSIZE_MAX_MEM/4 ) 41 | 42 | /* set memory constraint handler */ 43 | extern constraint_handler_t 44 | set_mem_constraint_handler_s(constraint_handler_t handler); 45 | 46 | 47 | /* compare memory */ 48 | extern errno_t memcmp_s(const void *dest, rsize_t dmax, 49 | const void *src, rsize_t slen, int *diff); 50 | 51 | /* compare uint16_t memory */ 52 | extern errno_t memcmp16_s(const uint16_t *dest, rsize_t dmax, 53 | const uint16_t *src, rsize_t slen, int *diff); 54 | 55 | /* compare uint32_t memory */ 56 | extern errno_t memcmp32_s(const uint32_t *dest, rsize_t dmax, 57 | const uint32_t *src, rsize_t slen, int *diff); 58 | 59 | /* wide compare memory */ 60 | extern errno_t wmemcmp_s(const wchar_t *dest, rsize_t dmax, 61 | const wchar_t *src, rsize_t smax, int *diff); 62 | 63 | 64 | /* copy memory */ 65 | extern errno_t memcpy_s(void *dest, rsize_t dmax, 66 | const void *src, rsize_t slen); 67 | 68 | /* copy uint16_t memory */ 69 | extern errno_t memcpy16_s(uint16_t *dest, rsize_t dmax, 70 | const uint16_t *src, rsize_t slen); 71 | 72 | /* copy uint32_t memory */ 73 | extern errno_t memcpy32_s(uint32_t *dest, rsize_t dmax, 74 | const uint32_t *src, rsize_t slen); 75 | 76 | /* copy wchar_t memory */ 77 | extern errno_t wmemcpy_s(wchar_t *dest, rsize_t dmax, 78 | const wchar_t *src, rsize_t slen); 79 | 80 | 81 | /* move memory, including overlapping memory */ 82 | extern errno_t memmove_s(void *dest, rsize_t dmax, 83 | const void *src, rsize_t slen); 84 | 85 | /* uint16_t move memory, including overlapping memory */ 86 | extern errno_t memmove16_s(uint16_t *dest, rsize_t dmax, 87 | const uint16_t *src, rsize_t slen); 88 | 89 | /* uint32_t move memory, including overlapping memory */ 90 | extern errno_t memmove32_s(uint32_t *dest, rsize_t dmax, 91 | const uint32_t *src, rsize_t slen); 92 | 93 | /* copy wchar_t memory, including overlapping memory */ 94 | extern errno_t wmemmove_s(wchar_t *dest, rsize_t dmax, 95 | const wchar_t *src, rsize_t slen); 96 | 97 | 98 | /* set bytes */ 99 | extern errno_t memset_s(void *dest, rsize_t dmax, uint8_t value); 100 | 101 | /* set uint16_t */ 102 | extern errno_t memset16_s(uint16_t *dest, rsize_t dmax, uint16_t value); 103 | 104 | /* set uint32_t */ 105 | extern errno_t memset32_s(uint32_t *dest, rsize_t dmax, uint32_t value); 106 | 107 | /* wide set bytes */ 108 | extern errno_t wmemset_s(wchar_t *dest, wchar_t value, rsize_t len); 109 | 110 | 111 | /* byte zero */ 112 | extern errno_t memzero_s(void *dest, rsize_t dmax); 113 | 114 | /* uint16_t zero */ 115 | extern errno_t memzero16_s(uint16_t *dest, rsize_t dmax); 116 | 117 | /* uint32_t zero */ 118 | extern errno_t memzero32_s(uint32_t *dest, rsize_t dmax); 119 | 120 | 121 | #endif /* __SAFE_MEM_LIB_H__ */ 122 | -------------------------------------------------------------------------------- /include/safe_types.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_types.h - C99 std types & defs or Linux kernel equivalents 3 | * 4 | * Copyright (c) 2007 Bo Berry 5 | * Copyright (c) 2012 Jonathan Toppins 6 | * Copyright (c) 2007-2013 by Cisco Systems, Inc 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #ifndef __SAFE_TYPES_H__ 33 | #define __SAFE_TYPES_H__ 34 | 35 | #ifdef __KERNEL__ 36 | /* linux kernel environment */ 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | /* errno_t isn't defined in the kernel */ 43 | typedef int errno_t; 44 | 45 | #else 46 | 47 | #include 48 | 49 | /* For systems without sys/types.h, prefer to get size_t from stdlib.h */ 50 | /* some armcc environments don't have a sys/types.h in the environment */ 51 | #ifdef _USE_STDLIB 52 | #include 53 | #include 54 | #else 55 | #include 56 | #endif 57 | 58 | #include 59 | #include 60 | #include 61 | 62 | typedef int errno_t; 63 | 64 | #include 65 | 66 | #endif /* __KERNEL__ */ 67 | #endif /* __SAFE_TYPES_H__ */ 68 | -------------------------------------------------------------------------------- /include/safe_types.h.in: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_types.h - C99 std types & defs or Linux kernel equivalents 3 | * 4 | * Copyright (c) 2007 Bo Berry 5 | * Copyright (c) 2012 Jonathan Toppins 6 | * Copyright (c) 2007-2013 by Cisco Systems, Inc 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #ifndef __SAFE_TYPES_H__ 33 | #define __SAFE_TYPES_H__ 34 | 35 | #ifdef __KERNEL__ 36 | /* linux kernel environment */ 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | /* errno_t isn't defined in the kernel */ 43 | typedef int errno_t; 44 | 45 | #else 46 | 47 | #include 48 | @INSERT_SYS_TYPES_H@ 49 | @INSERT_INTTYPES_H@ 50 | @INSERT_STDINT_H@ 51 | @INSERT_ERRNO_H@ 52 | 53 | @FALLBACK_ERRNO_T@ 54 | 55 | @INSERT_BOOL_SUPPORT@ 56 | 57 | #endif /* __KERNEL__ */ 58 | #endif /* __SAFE_TYPES_H__ */ 59 | -------------------------------------------------------------------------------- /include/snprintf_s.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * sprintf_s.h -- Safe Sprintf Interfaces 3 | * 4 | * August 2014, D Wheeler 5 | * 6 | * Copyright (c) 2014 by Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | #ifndef SPRINTF_S_H_ 32 | #define SPRINTF_S_H_ 33 | 34 | #include 35 | 36 | 37 | #define SNPRFNEGATE(x) (-1*(x)) 38 | 39 | 40 | 41 | int snprintf_s_i(char *dest, size_t dmax, const char *format, int a); 42 | int snprintf_s_si(char *dest, size_t dmax, const char *format, char *s, int a); 43 | int snprintf_s_l(char *dest, size_t dmax, const char *format, long a); 44 | int snprintf_s_sl(char *dest, size_t dmax, const char *format, char *s, long a); 45 | 46 | 47 | 48 | #endif /* SPRINTF_S_H_ */ 49 | 50 | -------------------------------------------------------------------------------- /led_control/Makefile: -------------------------------------------------------------------------------- 1 | C_SRC := led_control.c 2 | CFLAGS := -Werror -Wall -fstack-protector-all -O -I../led_control 3 | 4 | ifeq ($(or $(COMSPEC),$(ComSpec)),) 5 | RM := rm -rf 6 | else 7 | RM := cs-rm -rf 8 | endif 9 | 10 | LIB ?= $(basename $(firstword $(C_SRC))).a 11 | OBJ := $(patsubst %.c,%.o,$(C_SRC)) 12 | 13 | .PHONY: all 14 | all: $(LIB) $(OBJ) 15 | 16 | .PHONY: 17 | clean: 18 | $(RM) $(LIB) *.o 19 | 20 | $(OBJ): $(C_SRC) 21 | $(CC) $(CFLAGS) -c $< -o $@ 22 | 23 | $(LIB): $(OBJ) 24 | $(AR) cr $(LIB) $(OBJ) 25 | 26 | -------------------------------------------------------------------------------- /led_control/led_control.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014, Altera Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * - Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * - Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * - Neither the name of the Altera Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #define STRSIZE 256 36 | 37 | void setLEDtrigger(int ledno, char* trigger, int size) 38 | { 39 | FILE *fp; 40 | char dir[STRSIZE]; 41 | 42 | snprintf(dir, STRSIZE-1, "/sys/class/leds/fpga_led%d/trigger", ledno); 43 | 44 | if ((fp = fopen(dir, "w")) == NULL) { 45 | printf("Failed to open the file %s\n", dir); 46 | } 47 | else { 48 | fwrite(trigger, 1, size, fp); 49 | fclose(fp); 50 | } 51 | } 52 | 53 | void setLEDBrightness(int ledno, int brightness) 54 | { 55 | FILE *fp; 56 | char dir[STRSIZE]; 57 | char brightness_char[STRSIZE]; 58 | 59 | snprintf(dir, STRSIZE-1, "/sys/class/leds/fpga_led%d/brightness", ledno); 60 | snprintf(brightness_char, STRSIZE-1, "%d", brightness); 61 | 62 | if ((fp = fopen(dir, "w")) == NULL) { 63 | printf("Failed to open the file %s\n", dir); 64 | } 65 | else { 66 | fwrite(brightness_char, 1, sizeof(brightness_char), fp); 67 | fclose(fp); 68 | } 69 | } 70 | 71 | void clear_led(int led) 72 | { 73 | char trigger[] = "none"; 74 | setLEDtrigger(led, trigger, sizeof(trigger)); 75 | setLEDBrightness(led, 0); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /led_control/led_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014, Altera Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * - Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * - Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * - Neither the name of the Altera Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | void setLEDtrigger(int ledno, char* trigger, int size); 29 | void setLEDBrightness(int ledno, int brightness); 30 | void clear_led(int led); 31 | void clear_leds(); 32 | -------------------------------------------------------------------------------- /pio-interrupt/Makefile: -------------------------------------------------------------------------------- 1 | obj-m := gpio_interrupt.o 2 | 3 | SRC := $(shell pwd) 4 | 5 | all: 6 | $(MAKE) -C $(KERNEL_SRC) M=$(SRC) 7 | 8 | modules_install: 9 | $(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install 10 | 11 | clean: 12 | rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c 13 | rm -f Module.markers Module.symvers modules.order 14 | rm -rf .tmp_versions Modules.symvers 15 | -------------------------------------------------------------------------------- /pio-interrupt/gpio_interrupt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * GPIO interrupt handling example 3 | * 4 | * This module serves as kernel module example that can be added via 5 | * Angstrom recipe. 6 | * 7 | * Copyright Altera Corporation (C) 2013-2014. All rights reserved 8 | * This program is free software; you can redistribute it and/or modify it 9 | * under the terms and conditions of the GNU General Public License, 10 | * version 2, as published by the Free Software Foundation. 11 | * 12 | * This program is distributed in the hope it will be useful, but WITHOUT 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 | * more details. 16 | * 17 | * You should have received a copy of the GNU General Public License along with 18 | * this program. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | static int gpio_number = -1; 28 | static int intr_type = -1; 29 | 30 | irqreturn_t gpio_isr(int this_irq, void *dev_id) 31 | { 32 | pr_info("Interrupt happened at gpio:%d\n", gpio_number); 33 | return IRQ_HANDLED; 34 | } 35 | 36 | static void __exit gpio_interrupt_exit(void) 37 | { 38 | free_irq(gpio_to_irq(gpio_number), 0); 39 | gpio_free(gpio_number); 40 | return; 41 | } 42 | 43 | static int __init gpio_interrupt_init(void) 44 | { 45 | int r; 46 | int req; 47 | int irq_number; 48 | 49 | if (gpio_number < 0) { 50 | pr_err("Please specify a valid gpio_number\n"); 51 | return -1; 52 | } 53 | 54 | if (intr_type < 0) { 55 | pr_err("Please specify a valid intr_type. Button gpio: intr_type=2, " 56 | "Dipsw gpio: intr_type=3\n"); 57 | return -1; 58 | } 59 | 60 | req = gpio_request(gpio_number, "pio interrupt"); 61 | 62 | if (req != 0) { 63 | pr_err("Invalid gpio_number specified\n"); 64 | return -1; 65 | } 66 | 67 | irq_number = gpio_to_irq(gpio_number); 68 | 69 | r = request_irq(irq_number, gpio_isr, intr_type, 0, 0); 70 | if (r) { 71 | pr_err("Failure requesting irq %i\n", irq_number); 72 | return r; 73 | } 74 | 75 | pr_info("Interrupt for GPIO:%d\n registered", gpio_number); 76 | return 0; 77 | } 78 | 79 | MODULE_LICENSE("GPL"); 80 | 81 | module_param(gpio_number, int, 0); 82 | module_param(intr_type, int, 0); 83 | 84 | module_init(gpio_interrupt_init); 85 | module_exit(gpio_interrupt_exit); 86 | -------------------------------------------------------------------------------- /pio_ilc_demo/Makefile: -------------------------------------------------------------------------------- 1 | obj-m := intel_pio_ilc.o 2 | 3 | SRC := $(shell pwd) 4 | 5 | all: 6 | $(MAKE) -C $(KERNEL_SRC) M=$(SRC) 7 | 8 | modules_install: 9 | $(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install 10 | 11 | clean: 12 | rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c 13 | rm -f Module.markers Module.symvers modules.order 14 | rm -rf .tmp_versions Modules.symvers 15 | -------------------------------------------------------------------------------- /pio_ilc_demo/README: -------------------------------------------------------------------------------- 1 | 2 | Table of Contents 3 | ================= 4 | 1. Overview 5 | 2. Write SD image 6 | 3. u-boot commands 7 | 4. Linux commands 8 | 5. Demonstrating ILC functionality 9 | 6. Conclusion 10 | 11 | 1. Overview 12 | =========== 13 | We are showcasing the ILC functionality using custom gpio driver. To do this, 14 | we need minimal modifications to the existing GSRD images. We are utilizing 15 | the Agilex SoC DK production board for this demonstration. 16 | 17 | 2. Write SD image 18 | ================= 19 | Download sdimage.tar.gz file from given below link. 20 | 21 | https://releases.rocketboards.org/2023.04/gsrd/agilex_fm_61_gsrd/sdimage.tar.gz 22 | 23 | Write sdimage.tar.gz file to the SD card. Once the SD card image has been 24 | created, insert the card into the SD slot of the Micro SD daughter card. 25 | And power on the board, stop u-boot at the 'autoboot' and execute these 26 | commands on u-boot command line. 27 | 28 | 3. u-boot command 29 | ================= 30 | Once you arrive at the u-boot console prompt, perform the following 31 | actions to boot into our new kernel environment. 32 | 33 | Configure the FPGA core.rbf: 34 | 35 | $ load mmc 0:1 ${loadaddr} kernel.itb 36 | 37 | $ imxtract ${loadaddr} fpga-4 01000000 38 | 39 | $ fpga load 0 01000000 ${filesize} 40 | 41 | $ bridge enable 42 | 43 | Load the kernel and devicetree into memory: 44 | 45 | $ fdt addr ${loadaddr} 46 | 47 | $ fdt get addr KERNEL_ADDR /images/kernel data 48 | 49 | $ lzmadec ${KERNEL_ADDR} 08000000 50 | 51 | $ imxtract ${loadaddr} fdt-4 ${fdt_addr_r} 52 | 53 | $ fdt addr ${fdt_addr_r} 54 | 55 | $ fdt set /soc/gpio@f9001060 compatible "intel,pio-ilc" 56 | 57 | $ fdt set /soc/ilc@f9001100 status "okay" 58 | 59 | Configure the kernel boot parameters and boot the kernel: 60 | 61 | $ setenv bootargs "earlycon panic=-1 root=${mmcroot} rw rootwait" 62 | 63 | $ booti 08000000 - ${fdt_addr_r} 64 | 65 | 4. Linux commands 66 | ================= 67 | Once you arrive at the Linux console prompt, the following commands 68 | may be of interest. 69 | 70 | We should also see evidence of the Interrupt Latency Counter driver as well 71 | as our pio_ilc driver that we added, both modules should have loaded 72 | and installed IRQ handlers: 73 | 74 | $ lsmod | grep ilc 75 | intel_pio_ilc 16384 0 76 | altera_ilc 16384 0 77 | 78 | $ cat /proc/interrupts | grep ilc 79 | 39: 0 0 0 0 GICv2 50 Level ilc_0, pio_ilc_0 80 | 81 | We can dump the irq count of our pio_ilc driver like this: 82 | 83 | $ cat /sys/devices/platform/soc/f9001060.gpio/pio_ilc_data/39 84 | 0 85 | 86 | We can dump the ILC driver count FIFO like this: 87 | 88 | $ cat /sys/devices/platform/soc/f9001100.ilc/ilc_data/39 89 | [ 387.952999] altera_ilc f9001100.ilc: Fifo for interrupt 39 is empty 90 | 91 | 5. Demonstrating ILC functionality 92 | ================================== 93 | If our system appears to be working properly, we can demonstrate the 94 | ILC functionality on our general interrupts like this: 95 | 96 | Press the FPGA PB0 push button on the board 4 times(User FPGA push buttons 97 | pin 0 -- S1). 98 | 99 | Dump the IRQ counts. We expect to see the pio_ilc driver register 4 events 100 | and we expect the kernel to report 4 IRQs being handled by CPU0: 101 | 102 | $ cat /sys/devices/platform/soc/f9001060.gpio/pio_ilc_data/39 103 | 4 104 | 105 | $ cat /proc/interrupts | grep 'CPU[0-3]\|ilc' 106 | CPU0 CPU1 CPU2 CPU3 107 | 39: 4 0 0 0 GICv2 50 Level pio_ilc_0, ilc_0 108 | 109 | Dump the ILC latency count FIFO. These values represent clock cycles 110 | at 100MHz, or 10ns: 111 | 112 | $ cat /sys/devices/platform/soc/f9001100.ilc/ilc_data/39 113 | 628 114 | 336 115 | 354 116 | 338 117 | 118 | 6. Conclusion 119 | ============= 120 | This demonstrates the ILC functionality with the custom gpio driver. 121 | User can check the latency data values from the ilc counter. 122 | -------------------------------------------------------------------------------- /pio_ilc_demo/intel_pio_ilc.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0-only 2 | /* 3 | * Copyright (C) 2023 Intel Corporation 4 | * Copyright (C) 2025 Altera Corporation 5 | * 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define DRV_NAME "intel_pio_ilc" 14 | #define DATA_REG (0 << 2) 15 | #define DIRECTION_REG BIT(2) 16 | #define INTERRUPTMASK_REG (2 << 2) 17 | #define EDGECAPTURE_REG (3 << 2) 18 | 19 | struct intel_pio_ilc { 20 | struct platform_device *pdev; 21 | void __iomem *regs; 22 | unsigned int irq_count; 23 | unsigned int interrupt; 24 | struct device_attribute dev_attr; 25 | char sysfs[10]; 26 | }; 27 | 28 | static ssize_t pio_ilc_show_counter(struct device *dev, 29 | struct device_attribute *attr, 30 | char *buf) 31 | { 32 | struct intel_pio_ilc *pio_ilc = dev_get_drvdata(dev); 33 | 34 | sprintf(buf, "%u\n", pio_ilc->irq_count); 35 | strcat(buf, "\0"); 36 | 37 | return strlen(buf); 38 | } 39 | 40 | static struct attribute *intel_pio_ilc_attrs[2]; 41 | 42 | struct attribute_group intel_pio_ilc_attr_group = { 43 | .name = "pio_ilc_data", 44 | .attrs = intel_pio_ilc_attrs, 45 | }; 46 | 47 | static irqreturn_t pio_ilc_interrupt_handler(int irq, void *p) 48 | { 49 | struct intel_pio_ilc *pio_ilc = (struct intel_pio_ilc *)p; 50 | 51 | /* Increment IRQ counter */ 52 | pio_ilc->irq_count++; 53 | 54 | /* Clear all pending IRQs in gpio */ 55 | writel(0xFFFFFFFF, pio_ilc->regs + EDGECAPTURE_REG); 56 | 57 | return IRQ_RETVAL(IRQ_HANDLED); 58 | } 59 | 60 | static int intel_pio_ilc_probe(struct platform_device *pdev) 61 | { 62 | struct intel_pio_ilc *pio_ilc; 63 | struct resource *regs; 64 | int ret; 65 | 66 | pio_ilc = devm_kzalloc(&pdev->dev, sizeof(struct intel_pio_ilc), 67 | GFP_KERNEL); 68 | if (!pio_ilc) 69 | return -ENOMEM; 70 | 71 | pio_ilc->pdev = pdev; 72 | 73 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 74 | if (!regs) 75 | return -ENXIO; 76 | 77 | pio_ilc->regs = devm_ioremap_resource(&pdev->dev, regs); 78 | if (!pio_ilc->regs) 79 | return -EADDRNOTAVAIL; 80 | 81 | /* Register IRQ */ 82 | pio_ilc->interrupt = platform_get_irq(pdev, 0); 83 | 84 | ret = devm_request_irq(&pdev->dev, (pio_ilc->interrupt), 85 | pio_ilc_interrupt_handler, IRQF_SHARED, 86 | "pio_ilc_0", (void *)(pio_ilc)); 87 | 88 | if (ret < 0) 89 | dev_warn(&pdev->dev, "Failed to register interrupt handler"); 90 | 91 | /* Setup sysfs interface */ 92 | sprintf(pio_ilc->sysfs, "%d", (pio_ilc->interrupt)); 93 | pio_ilc->dev_attr.attr.name = pio_ilc->sysfs; 94 | pio_ilc->dev_attr.attr.mode = 0444; 95 | pio_ilc->dev_attr.show = pio_ilc_show_counter; 96 | intel_pio_ilc_attrs[0] = &pio_ilc->dev_attr.attr; 97 | intel_pio_ilc_attrs[1] = NULL; 98 | 99 | ret = sysfs_create_group(&pdev->dev.kobj, &intel_pio_ilc_attr_group); 100 | 101 | /* Clear any pending IRQs and Enable IRQ-0 in gpio */ 102 | writel(0xFFFFFFFF, pio_ilc->regs + EDGECAPTURE_REG); 103 | writel(0x00000001, pio_ilc->regs + INTERRUPTMASK_REG); 104 | 105 | platform_set_drvdata(pdev, pio_ilc); 106 | 107 | dev_info(&pdev->dev, "Driver successfully loaded\n"); 108 | 109 | return 0; 110 | } 111 | 112 | static void intel_pio_ilc_remove(struct platform_device *pdev) 113 | { 114 | struct intel_pio_ilc *pio_ilc = platform_get_drvdata(pdev); 115 | 116 | /* Disable IRQs in gpio */ 117 | writel(0x00000000, pio_ilc->regs + INTERRUPTMASK_REG); 118 | 119 | /* Remove sysfs interface */ 120 | sysfs_remove_group(&pdev->dev.kobj, &intel_pio_ilc_attr_group); 121 | 122 | platform_set_drvdata(pdev, NULL); 123 | } 124 | 125 | static const struct of_device_id intel_pio_ilc_match[] = { 126 | { .compatible = "intel,pio-ilc" }, 127 | { /* Sentinel */ } 128 | }; 129 | 130 | MODULE_DEVICE_TABLE(of, intel_pio_ilc_match); 131 | 132 | static struct platform_driver intel_pio_ilc_platform_driver = { 133 | .driver = { 134 | .name = DRV_NAME, 135 | .owner = THIS_MODULE, 136 | .of_match_table = of_match_ptr(intel_pio_ilc_match), 137 | }, 138 | .remove = intel_pio_ilc_remove, 139 | }; 140 | 141 | static int __init intel_pio_ilc_init(void) 142 | { 143 | return platform_driver_probe(&intel_pio_ilc_platform_driver, 144 | intel_pio_ilc_probe); 145 | } 146 | 147 | static void __exit intel_pio_ilc_exit(void) 148 | { 149 | platform_driver_unregister(&intel_pio_ilc_platform_driver); 150 | } 151 | 152 | module_init(intel_pio_ilc_init); 153 | module_exit(intel_pio_ilc_exit); 154 | 155 | MODULE_AUTHOR("Rod Frazer"); 156 | MODULE_LICENSE("GPL"); 157 | MODULE_DESCRIPTION("Altera Avalon GPIO driver for Interrupt Latency Counter use"); 158 | MODULE_ALIAS("platform:" DRV_NAME); 159 | -------------------------------------------------------------------------------- /safeclib/abort_handler_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * abort_handler_s.c 3 | * 4 | * Copyright (c) 2012 Jonathan Toppins 5 | * Copyright (c) 2012 Cisco Systems 6 | * Copyright (c) 2018-2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | 34 | /** 35 | * NAME 36 | * abort_handler_s 37 | * 38 | * SYNOPSIS 39 | * #include "safe_lib.h" 40 | * void abort_handler_s(const char *msg, void *ptr, errno_t error) 41 | * 42 | * DESCRIPTION 43 | * This function writes a message on the standard error stream in 44 | * an implementation-defined format. The message shall include the 45 | * string pointed to by msg. The abort_handler_s function then calls 46 | * the abort function. 47 | * 48 | * SPECIFIED IN 49 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 50 | * and system software interfaces, Extensions to the C Library, 51 | * Part I: Bounds-checking interfaces 52 | * 53 | * INPUT PARAMETERS 54 | * msg Pointer to the message describing the error 55 | * 56 | * ptr Pointer to aassociated data. Can be NULL. 57 | * 58 | * error The error code encountered. 59 | * 60 | * RETURN VALUE 61 | * Does not return to caller. 62 | * 63 | * ALSO SEE 64 | * ignore_handler_s() 65 | * 66 | */ 67 | 68 | void abort_handler_s(const char *msg, void *ptr, errno_t error) 69 | { 70 | slprintf("ABORT CONSTRAINT HANDLER: (%u) %s\n", error, 71 | (msg) ? msg : "Null message"); 72 | slabort(); 73 | } 74 | EXPORT_SYMBOL(abort_handler_s) 75 | -------------------------------------------------------------------------------- /safeclib/ignore_handler_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * ignore_handler_s.c 3 | * 4 | * Copyright (c) 2012 Jonathan Toppins 5 | * Copyright (c) 2012 Cisco Systems 6 | * Copyright (c) 2018-2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | 34 | /** 35 | * NAME 36 | * ignore_handler_s 37 | * 38 | * SYNOPSIS 39 | * #include "safe_lib.h" 40 | * void ignore_handler_s(const char *msg, void *ptr, errno_t error) 41 | * 42 | * DESCRIPTION 43 | * This function simply returns to the caller. 44 | * 45 | * SPECIFIED IN 46 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 47 | * and system software interfaces, Extensions to the C Library, 48 | * Part I: Bounds-checking interfaces 49 | * 50 | * INPUT PARAMETERS 51 | * msg Pointer to the message describing the error 52 | * 53 | * ptr Pointer to aassociated data. Can be NULL. 54 | * 55 | * error The error code encountered. 56 | * 57 | * RETURN VALUE 58 | * Returns no value. 59 | * 60 | * ALSO SEE 61 | * abort_handler_s() 62 | * 63 | */ 64 | 65 | void ignore_handler_s(const char *msg, void *ptr, errno_t error) 66 | { 67 | 68 | sldebug_printf("IGNORE CONSTRAINT HANDLER: (%u) %s\n", error, 69 | (msg) ? msg : "Null message"); 70 | return; 71 | } 72 | EXPORT_SYMBOL(ignore_handler_s) 73 | -------------------------------------------------------------------------------- /safeclib/mem_primitives_lib.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * mem_primitives_lib.h - Unguarded Memory Copy Routines 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * Copyright (c) 2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #ifndef __MEM_PRIMITIVES_LIB_H__ 33 | #define __MEM_PRIMITIVES_LIB_H__ 34 | 35 | #include "safeclib_private.h" 36 | 37 | /* 38 | * These are prototypes for _unguarded_ memory routines. The caller must 39 | * validate all parameters prior to invocation. Useful for diagnostics 40 | * and system initialization processing. 41 | */ 42 | 43 | /* moves (handles overlap) memory */ 44 | extern void 45 | mem_prim_move(void *dest, const void *src, uint32_t length); 46 | 47 | 48 | /* uint8_t moves (handles overlap) memory */ 49 | extern void 50 | mem_prim_move8(uint8_t *dest, const uint8_t *src, uint32_t length); 51 | 52 | /* uint16_t moves (handles overlap) memory */ 53 | extern void 54 | mem_prim_move16(uint16_t *dest, const uint16_t *src, uint32_t length); 55 | 56 | /* uint32_t moves (handles overlap) memory */ 57 | extern void 58 | mem_prim_move32(uint32_t *dest, const uint32_t *src, uint32_t length); 59 | 60 | 61 | /* set bytes */ 62 | extern void 63 | mem_prim_set(void *dest, uint32_t dmax, uint8_t value); 64 | 65 | /* set uint16_ts */ 66 | extern void 67 | mem_prim_set16(uint16_t *dest, uint32_t dmax, uint16_t value); 68 | 69 | /* set uint32_ts */ 70 | extern void 71 | mem_prim_set32(uint32_t *dest, uint32_t dmax, uint32_t value); 72 | 73 | 74 | #endif /* __MEM_PRIMITIVES_LIB_H__ */ 75 | -------------------------------------------------------------------------------- /safeclib/memset16_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * memset16_s 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_mem_constraint.h" 33 | #include "mem_primitives_lib.h" 34 | #include "safe_mem_lib.h" 35 | 36 | /** 37 | * NAME 38 | * memset16_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_mem_lib.h" 42 | * errno_t 43 | * memset16_s(uint16_t *dest, rsize_t len, uint16_t value) 44 | * 45 | * DESCRIPTION 46 | * Sets len uint16_t starting at dest to the specified value. 47 | * 48 | * EXTENSION TO 49 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 50 | * and system software interfaces, Extensions to the C Library, 51 | * Part I: Bounds-checking interfaces 52 | * 53 | * INPUT PARAMETERS 54 | * dest pointer to memory that will be set to the value 55 | * 56 | * len number of uint16_t to be set 57 | * 58 | * value uint16_t value to be written 59 | * 60 | * OUTPUT PARAMETERS 61 | * dest is updated 62 | * 63 | * RUNTIME CONSTRAINTS 64 | * dest shall not be a null pointer. 65 | * len shall not be 0 nor greater than RSIZE_MAX_MEM16. 66 | * If there is a runtime constraint, the operation is not performed. 67 | * 68 | * RETURN VALUE 69 | * EOK successful operation 70 | * ESNULLP NULL pointer 71 | * ESZEROL zero length 72 | * ESLEMAX length exceeds max limit 73 | * 74 | * ALSO SEE 75 | * memset_s(), memset32_s() 76 | * 77 | */ 78 | errno_t 79 | memset16_s (uint16_t *dest, rsize_t len, uint16_t value) 80 | { 81 | if (dest == NULL) { 82 | invoke_safe_mem_constraint_handler("memset16_s: dest is null", 83 | NULL, ESNULLP); 84 | return (RCNEGATE(ESNULLP)); 85 | } 86 | 87 | if (len == 0) { 88 | invoke_safe_mem_constraint_handler("memset16_s: len is 0", 89 | NULL, ESZEROL); 90 | return (RCNEGATE(ESZEROL)); 91 | } 92 | 93 | if (len > RSIZE_MAX_MEM16) { 94 | invoke_safe_mem_constraint_handler("memset16_s: len exceeds max", 95 | NULL, ESLEMAX); 96 | return (RCNEGATE(ESLEMAX)); 97 | } 98 | 99 | mem_prim_set16(dest, len, value); 100 | 101 | return (RCNEGATE(EOK)); 102 | } 103 | EXPORT_SYMBOL(memset16_s) 104 | -------------------------------------------------------------------------------- /safeclib/memset32_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * memset32_s 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_mem_constraint.h" 33 | #include "mem_primitives_lib.h" 34 | #include "safe_mem_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * memset32_s - Sets a block of memory to value 40 | * 41 | * SYNOPSIS 42 | * #include "safe_mem_lib.h" 43 | * errno_t 44 | * memset32_s(uint32_t *dest, rsize_t len, uint32_t value) 45 | * 46 | * DESCRIPTION 47 | * Sets len uint32_t starting at dest to the specified value. 48 | * 49 | * EXTENSION TO 50 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest pointer to memory that will be set to the value 56 | * 57 | * len number of uint32_t to be set 58 | * 59 | * value uint32_t value to be written 60 | * 61 | * OUTPUT PARAMETERS 62 | * dest is updated 63 | * 64 | * RUNTIME CONSTRAINTS 65 | * dest shall not be a null pointer. 66 | * len shall not be 0 nor greater than RSIZE_MAX_MEM32. 67 | * If there is a runtime constraint, the operation is not performed. 68 | * 69 | * RETURN VALUE 70 | * EOK successful operation 71 | * ESNULLP NULL pointer 72 | * ESZEROL zero length 73 | * ESLEMAX length exceeds max limit 74 | * 75 | * ALSO SEE 76 | * memset_s(), memset16_s() 77 | * 78 | */ 79 | errno_t 80 | memset32_s (uint32_t *dest, rsize_t len, uint32_t value) 81 | { 82 | if (dest == NULL) { 83 | invoke_safe_mem_constraint_handler("memset32_s: dest is null", 84 | NULL, ESNULLP); 85 | return (RCNEGATE(ESNULLP)); 86 | } 87 | 88 | if (len == 0) { 89 | invoke_safe_mem_constraint_handler("memset32_s: len is 0", 90 | NULL, ESZEROL); 91 | return (RCNEGATE(ESZEROL)); 92 | } 93 | 94 | if (len > RSIZE_MAX_MEM32) { 95 | invoke_safe_mem_constraint_handler("memset32_s: len exceeds max", 96 | NULL, ESLEMAX); 97 | return (RCNEGATE(ESLEMAX)); 98 | } 99 | 100 | mem_prim_set32(dest, len, value); 101 | 102 | return (RCNEGATE(EOK)); 103 | } 104 | EXPORT_SYMBOL(memset32_s) 105 | -------------------------------------------------------------------------------- /safeclib/memset_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * memset_s 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_mem_constraint.h" 33 | #include "mem_primitives_lib.h" 34 | #include "safe_mem_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * memset_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_mem_lib.h" 43 | * errno_t 44 | * memset_s(void *dest, rsize_t len, uint8_t value) 45 | * 46 | * DESCRIPTION 47 | * Sets len bytes starting at dest to the specified value. 48 | * 49 | * SPECIFIED IN 50 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest pointer to memory that will be set to the value 56 | * 57 | * len number of bytes to be set 58 | * 59 | * value byte value 60 | * 61 | * OUTPUT PARAMETERS 62 | * dest is updated 63 | * 64 | * RUNTIME CONSTRAINTS 65 | * dest shall not be a null pointer. 66 | * len shall not be 0 nor greater than RSIZE_MAX_MEM. 67 | * If there is a runtime constraint, the operation is not performed. 68 | * 69 | * RETURN VALUE 70 | * EOK successful operation 71 | * ESNULLP NULL pointer 72 | * ESZEROL zero length 73 | * ESLEMAX length exceeds max limit 74 | * 75 | * ALSO SEE 76 | * memset16_s(), memset32_s() 77 | * 78 | */ 79 | errno_t 80 | memset_s (void *dest, rsize_t len, uint8_t value) 81 | { 82 | if (dest == NULL) { 83 | invoke_safe_mem_constraint_handler("memset_s: dest is null", 84 | NULL, ESNULLP); 85 | return (RCNEGATE(ESNULLP)); 86 | } 87 | 88 | if (len == 0) { 89 | invoke_safe_mem_constraint_handler("memset_s: len is 0", 90 | NULL, ESZEROL); 91 | return (RCNEGATE(ESZEROL)); 92 | } 93 | 94 | if (len > RSIZE_MAX_MEM) { 95 | invoke_safe_mem_constraint_handler("memset_s: len exceeds max", 96 | NULL, ESLEMAX); 97 | return (RCNEGATE(ESLEMAX)); 98 | } 99 | 100 | mem_prim_set(dest, len, value); 101 | 102 | return (RCNEGATE(EOK)); 103 | } 104 | EXPORT_SYMBOL(memset_s) 105 | -------------------------------------------------------------------------------- /safeclib/memzero16_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * memzero16_s - zeros memory 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_mem_constraint.h" 33 | #include "mem_primitives_lib.h" 34 | #include "safe_mem_lib.h" 35 | 36 | /** 37 | * NAME 38 | * memzero16_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_mem_lib.h" 42 | * errno_t 43 | * memzero16_s(uint16_t *dest, rsize_t len) 44 | * 45 | * DESCRIPTION 46 | * Zeros len uint16_ts starting at dest. 47 | * 48 | * EXTENSION TO 49 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 50 | * and system software interfaces, Extensions to the C Library, 51 | * Part I: Bounds-checking interfaces 52 | * 53 | * INPUT PARAMETERS 54 | * dest pointer to memory to be zeroed. 55 | * 56 | * len number of uint16_ts to be zeroed 57 | * 58 | * OUTPUT PARAMETERS 59 | * dest is updated 60 | * 61 | * RUNTIME CONSTRAINTS 62 | * dest shall not be a null pointer. 63 | * len shall not be 0 nor greater than RSIZE_MAX_MEM16. 64 | * If there is a runtime constraint, the operation is not performed. 65 | * 66 | * RETURN VALUE 67 | * EOK successful operation 68 | * ESNULLP NULL pointer 69 | * ESZEROL zero length 70 | * ESLEMAX length exceeds max limit 71 | * 72 | * ALSO SEE 73 | * memzero_s(), memzero32_s() 74 | * 75 | */ 76 | errno_t 77 | memzero16_s (uint16_t *dest, rsize_t len) 78 | { 79 | if (dest == NULL) { 80 | invoke_safe_mem_constraint_handler("memzero16_s: dest is null", 81 | NULL, ESNULLP); 82 | return (RCNEGATE(ESNULLP)); 83 | } 84 | 85 | if (len == 0) { 86 | invoke_safe_mem_constraint_handler("memzero16_s: len is 0", 87 | NULL, ESZEROL); 88 | return (RCNEGATE(ESZEROL)); 89 | } 90 | 91 | if (len > RSIZE_MAX_MEM16) { 92 | invoke_safe_mem_constraint_handler("memzero16_s: len exceeds max", 93 | NULL, ESLEMAX); 94 | return (RCNEGATE(ESLEMAX)); 95 | } 96 | 97 | /* 98 | * mem_prim_set16(dest, len, 0xDEAD); 99 | * mem_prim_set16(dest, len, 0xBEEF); 100 | */ 101 | mem_prim_set16(dest, len, 0); 102 | 103 | return (RCNEGATE(EOK)); 104 | } 105 | EXPORT_SYMBOL(memzero16_s) 106 | -------------------------------------------------------------------------------- /safeclib/memzero32_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * memzero32_s - zeros memory 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_mem_constraint.h" 33 | #include "mem_primitives_lib.h" 34 | #include "safe_mem_lib.h" 35 | 36 | /** 37 | * NAME 38 | * memzero32_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_mem_lib.h" 42 | * errno_t 43 | * memzero32_s(uint32_t *dest, rsize_t len) 44 | * 45 | * DESCRIPTION 46 | * Zeros len uint32_ts starting at dest. 47 | * 48 | * EXTENSION TO 49 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 50 | * and system software interfaces, Extensions to the C Library, 51 | * Part I: Bounds-checking interfaces 52 | * 53 | * INPUT PARAMETERS 54 | * dest pointer to memory to be zeroed. 55 | * 56 | * len number of uint32_ts to be zeroed 57 | * 58 | * OUTPUT PARAMETERS 59 | * dest is updated 60 | * 61 | * RUNTIME CONSTRAINTS 62 | * dest shall not be a null pointer. 63 | * len shall not be 0 nor greater than RSIZE_MAX_MEM32. 64 | * If there is a runtime constraint, the operation is not performed. 65 | * 66 | * RETURN VALUE 67 | * EOK successful operation 68 | * ESNULLP NULL pointer 69 | * ESZEROL zero length 70 | * ESLEMAX length exceeds max limit 71 | * 72 | * ALSO SEE 73 | * memzero_s(), memzero16_s() 74 | * 75 | */ 76 | errno_t 77 | memzero32_s (uint32_t *dest, rsize_t len) 78 | { 79 | 80 | if (dest == NULL) { 81 | invoke_safe_mem_constraint_handler("memzero32_s: dest is null", 82 | NULL, ESNULLP); 83 | return (RCNEGATE(ESNULLP)); 84 | } 85 | 86 | if (len == 0) { 87 | invoke_safe_mem_constraint_handler("memzero32_s: len is 0", 88 | NULL, ESZEROL); 89 | return (RCNEGATE(ESZEROL)); 90 | } 91 | 92 | if (len > RSIZE_MAX_MEM32) { 93 | invoke_safe_mem_constraint_handler("memzero32_s: len exceeds max", 94 | NULL, ESLEMAX); 95 | return (RCNEGATE(ESLEMAX)); 96 | } 97 | 98 | /* 99 | * mem_prim_set32(dest, len, 0xDEADBEEF); 100 | * mem_prim_set32(dest, len, 0xBA5EBA11); 101 | */ 102 | mem_prim_set32(dest, len, 0); 103 | 104 | return (RCNEGATE(EOK)); 105 | } 106 | EXPORT_SYMBOL(memzero32_s) 107 | -------------------------------------------------------------------------------- /safeclib/memzero_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * memzero_s - zeros memory 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_mem_constraint.h" 33 | #include "mem_primitives_lib.h" 34 | #include "safe_mem_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * memzero_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_mem_lib.h" 43 | * errno_t 44 | * memzero_s(void *dest, rsize_t len) 45 | * 46 | * DESCRIPTION 47 | * Zeros len bytes starting at dest. 48 | * 49 | * EXTENSION TO 50 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest pointer to memory to be zeroed. 56 | * 57 | * len number of bytes to be zeroed 58 | * 59 | * OUTPUT PARAMETERS 60 | * dest is updated 61 | * 62 | * RUNTIME CONSTRAINTS 63 | * dest shall not be a null pointer. 64 | * len shall not be 0 nor greater than RSIZE_MAX_MEM. 65 | * If there is a runtime constraint, the operation is not performed. 66 | * 67 | * RETURN VALUE 68 | * EOK successful operation 69 | * ESNULLP NULL pointer 70 | * ESZEROL zero length 71 | * ESLEMAX length exceeds max limit 72 | * 73 | * ALSO SEE 74 | * memzero16_s(), memzero32_s() 75 | * 76 | */ 77 | errno_t 78 | memzero_s (void *dest, rsize_t len) 79 | { 80 | if (dest == NULL) { 81 | invoke_safe_mem_constraint_handler("memzero_s: dest is null", 82 | NULL, ESNULLP); 83 | return (RCNEGATE(ESNULLP)); 84 | } 85 | 86 | if (len == 0) { 87 | invoke_safe_mem_constraint_handler("memzero_s: len is 0", 88 | NULL, ESZEROL); 89 | return (RCNEGATE(ESZEROL)); 90 | } 91 | 92 | if (len > RSIZE_MAX_MEM) { 93 | invoke_safe_mem_constraint_handler("memzero_s: len exceeds max", 94 | NULL, ESLEMAX); 95 | return (RCNEGATE(ESLEMAX)); 96 | } 97 | 98 | /* 99 | * mem_prim_set(dest, len, 0xA5); 100 | * mem_prim_set(dest, len, 0x5A); 101 | */ 102 | mem_prim_set(dest, len, 0); 103 | 104 | return (RCNEGATE(EOK)); 105 | } 106 | EXPORT_SYMBOL(memzero_s) 107 | -------------------------------------------------------------------------------- /safeclib/safe_mem_constraint.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_mem_constraint.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2012 Jonathan Toppins 6 | * Copyright (c) 2008-2012 Cisco Systems 7 | * Copyright (c) 2018 Intel Corp 8 | * All rights reserved. 9 | * 10 | * Permission is hereby granted, free of charge, to any person 11 | * obtaining a copy of this software and associated documentation 12 | * files (the "Software"), to deal in the Software without 13 | * restriction, including without limitation the rights to use, 14 | * copy, modify, merge, publish, distribute, sublicense, and/or 15 | * sell copies of the Software, and to permit persons to whom the 16 | * Software is furnished to do so, subject to the following 17 | * conditions: 18 | * 19 | * The above copyright notice and this permission notice shall be 20 | * included in all copies or substantial portions of the Software. 21 | * 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 24 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 26 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 27 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 29 | * OTHER DEALINGS IN THE SOFTWARE. 30 | *------------------------------------------------------------------ 31 | */ 32 | 33 | #include "safeclib_private.h" 34 | #include "safe_mem_constraint.h" 35 | #include "safe_mem_lib.h" 36 | 37 | static constraint_handler_t mem_handler = NULL; 38 | 39 | /** 40 | * NAME 41 | * set_mem_constraint_handler_s 42 | * 43 | * SYNOPSIS 44 | * #include "safe_mem_lib.h" 45 | * constraint_handler_t 46 | * set_mem_constraint_handler_straint_handler_t handler) 47 | * 48 | * DESCRIPTION 49 | * The set_mem_constraint_handler_s function sets the runtime-constraint 50 | * handler to be handler. The runtime-constraint handler is the function to 51 | * be called when a library function detects a runtime-constraint 52 | * order: 53 | * 1. A pointer to a character string describing the 54 | * runtime-constraint violation. 55 | * 2. A null pointer or a pointer to an implementation defined 56 | * object. 57 | * 3. If the function calling the handler has a return type declared 58 | * as errno_t, the return value of the function is passed. 59 | * Otherwise, a positive value of type errno_t is passed. 60 | * The implementation has a default constraint handler that is used if no 61 | * calls to the set_constraint_handler_s function have been made. The 62 | * behavior of the default handler is implementation-defined, and it may 63 | * cause the program to exit or abort. If the handler argument to 64 | * set_constraint_handler_s is a null pointer, the implementation default 65 | * handler becomes the current constraint handler. 66 | * 67 | * SPECIFIED IN 68 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 69 | * and system software interfaces, Extensions to the C Library, 70 | * Part I: Bounds-checking interfaces 71 | * 72 | * INPUT PARAMETERS 73 | * *msg Pointer to the message describing the error 74 | * 75 | * *ptr Pointer to aassociated data. Can be NULL. 76 | * 77 | * error The error code encountered. 78 | * 79 | * OUTPUT PARAMETERS 80 | * none 81 | * 82 | * RETURN VALUE 83 | * none 84 | * 85 | * ALSO SEE 86 | * set_str_constraint_handler_s() 87 | */ 88 | constraint_handler_t 89 | set_mem_constraint_handler_s (constraint_handler_t handler) 90 | { 91 | constraint_handler_t prev_handler = mem_handler; 92 | if (NULL == handler) { 93 | mem_handler = sl_default_handler; 94 | } else { 95 | mem_handler = handler; 96 | } 97 | return prev_handler; 98 | } 99 | EXPORT_SYMBOL(set_mem_constraint_handler_s) 100 | 101 | 102 | /** 103 | * NAME 104 | * invoke_safe_mem_constraint_handler 105 | * 106 | * SYNOPSIS 107 | * #include "safe_mem_constraint.h" 108 | * void 109 | * invoke_safe_mem_constraint_handler(const char *msg, 110 | * void *ptr, 111 | * errno_t error) 112 | * 113 | * DESCRIPTION 114 | * Invokes the currently set constraint handler or the default. 115 | * 116 | * INPUT PARAMETERS 117 | * *msg Pointer to the message describing the error 118 | * 119 | * *ptr Pointer to aassociated data. Can be NULL. 120 | * 121 | * error The error code encountered. 122 | * 123 | * OUTPUT PARAMETERS 124 | * none 125 | * 126 | * RETURN VALUE 127 | * none 128 | * 129 | */ 130 | void 131 | invoke_safe_mem_constraint_handler (const char *msg, 132 | void *ptr, 133 | errno_t error) 134 | { 135 | if (NULL != mem_handler) { 136 | mem_handler(msg, ptr, error); 137 | } else { 138 | sl_default_handler(msg, ptr, error); 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /safeclib/safe_mem_constraint.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_mem_constraint.h 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2009 Cisco Systems 6 | * Copyright (c) 2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #ifndef __SAFE_MEM_CONSTRAINT_H__ 33 | #define __SAFE_MEM_CONSTRAINT_H__ 34 | 35 | #include "safeclib_private.h" 36 | 37 | /* 38 | * Function used by the libraries to invoke the registered 39 | * runtime-constraint handler. Always needed. 40 | */ 41 | extern void invoke_safe_mem_constraint_handler( 42 | const char *msg, 43 | void *ptr, 44 | errno_t error); 45 | 46 | #endif /* __SAFE_MEM_CONSTRAINT_H__ */ 47 | -------------------------------------------------------------------------------- /safeclib/safe_str_constraint.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_str_constraint.h 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * Copyright (c) 2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #ifndef __SAFE_STR_CONSTRAINT_H__ 33 | #define __SAFE_STR_CONSTRAINT_H__ 34 | 35 | #include "safeclib_private.h" 36 | 37 | /* 38 | * Function used by the libraries to invoke the registered 39 | * runtime-constraint handler. Always needed. 40 | */ 41 | extern void invoke_safe_str_constraint_handler( 42 | const char *msg, 43 | void *ptr, 44 | errno_t error); 45 | 46 | 47 | /* 48 | * Safe C Lib internal string routine to consolidate error handling 49 | */ 50 | static inline void handle_error(char *orig_dest, rsize_t orig_dmax, 51 | char *err_msg, errno_t err_code) 52 | { 53 | #ifdef SAFECLIB_STR_NULL_SLACK 54 | /* null string to eliminate partial copy */ 55 | while (orig_dmax) { *orig_dest = '\0'; orig_dmax--; orig_dest++; } 56 | #else 57 | *orig_dest = '\0'; 58 | #endif 59 | 60 | invoke_safe_str_constraint_handler(err_msg, NULL, err_code); 61 | return; 62 | } 63 | 64 | static inline void handle_wc_error(wchar_t *orig_dest, rsize_t orig_dmax, 65 | char *err_msg, errno_t err_code) 66 | { 67 | #ifdef SAFECLIB_STR_NULL_SLACK 68 | /* null string to eliminate partial copy */ 69 | while (orig_dmax) { *orig_dest = L'\0'; orig_dmax--; orig_dest++; } 70 | #else 71 | *orig_dest = L'\0'; 72 | #endif 73 | 74 | invoke_safe_str_constraint_handler(err_msg, NULL, err_code); 75 | return; 76 | } 77 | 78 | #endif /* __SAFE_STR_CONSTRAINT_H__ */ 79 | -------------------------------------------------------------------------------- /safeclib/safeclib_private.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safeclib_private.h - Internal library references 3 | * 4 | * Copyright (c) 2012 Jonathan Toppins 5 | * Copyright (c) 2012-2013 Cisco Systems, Inc 6 | * Copyright (c) 2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #ifndef __SAFECLIB_PRIVATE_H__ 33 | #define __SAFECLIB_PRIVATE_H__ 34 | 35 | #ifdef __KERNEL__ 36 | /* linux kernel environment */ 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | #define RCNEGATE(x) ( -(x) ) 43 | 44 | #define slprintf(...) printk(KERN_EMERG __VA_ARGS__) 45 | #define slabort() 46 | #ifdef DEBUG 47 | #define sldebug_printf(...) printk(KERN_DEBUG __VA_ARGS__) 48 | #endif 49 | 50 | #else /* !__KERNEL__ */ 51 | 52 | #if HAVE_CONFIG_H 53 | #include "config.h" 54 | #endif 55 | 56 | #include 57 | #ifdef STDC_HEADERS 58 | # include 59 | # include 60 | # include 61 | #else 62 | # ifdef HAVE_STDLIB_H 63 | # include 64 | # endif 65 | #endif 66 | #ifdef HAVE_STRING_H 67 | # if !defined STDC_HEADERS && defined HAVE_MEMORY_H 68 | # include 69 | # endif 70 | # include 71 | #endif 72 | #ifdef HAVE_LIMITS_H 73 | # include 74 | #endif 75 | 76 | #define EXPORT_SYMBOL(sym) 77 | #define RCNEGATE(x) (x) 78 | 79 | #define slprintf(...) fprintf(stderr, __VA_ARGS__) 80 | #define slabort() abort() 81 | #ifdef DEBUG 82 | #define sldebug_printf(...) printf(__VA_ARGS__) 83 | #endif 84 | 85 | #endif /* __KERNEL__ */ 86 | 87 | #ifndef sldebug_printf 88 | #define sldebug_printf(...) 89 | #endif 90 | 91 | #include "safe_lib.h" 92 | #include "safe_mem_lib.h" 93 | #include "safe_str_lib.h" 94 | 95 | #endif /* __SAFECLIB_PRIVATE_H__ */ 96 | -------------------------------------------------------------------------------- /safeclib/strcasecmp_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strcasecmp_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | /** 36 | * NAME 37 | * strcasecmp_s 38 | * 39 | * SYNOPSIS 40 | * #include "safe_str_lib.h" 41 | * errno_t 42 | * strcasecmp_s(const char *dest, rsize_t dmax, 43 | * const char *src, int *indicator) 44 | * 45 | * DESCRIPTION 46 | * Case insensitive string comparison by converting 47 | * to uppercase prior to the compare. 48 | * 49 | * EXTENSION TO 50 | * ISO/IEC TR 24731, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest pointer to string to compare against 56 | * 57 | * dmax restricted maximum length of string dest 58 | * 59 | * src pointer to the string to be compared to dest 60 | * 61 | * indicator pointer to result indicator, greater than 0, 62 | * equal to 0 or less than 0, if the string pointed 63 | * to by dest is greater than, equal to or less 64 | * than the string pointed to by src respectively. 65 | * 66 | * OUTPUT PARAMETERS 67 | * none 68 | * 69 | * RUNTIME CONSTRAINTS 70 | * Neither dest nor src shall be a null pointer. 71 | * indicator shall not be a null pointer. 72 | * dmax shall not be 0 73 | * dmax shall not be greater than RSIZE_MAX_STR 74 | * 75 | * RETURN VALUE 76 | * indicator, when the return code is OK 77 | * >0 dest greater than src 78 | * 0 strings the same 79 | * <0 dest less than src 80 | * 81 | * EOK comparison complete 82 | * ESNULLP pointer was null 83 | * ESZEROL length was zero 84 | * ESLEMAX length exceeded max 85 | * 86 | * ALSO SEE 87 | * strcmp_s() 88 | * 89 | */ 90 | errno_t 91 | strcasecmp_s (const char *dest, rsize_t dmax, 92 | const char *src, int *indicator) 93 | { 94 | const unsigned char *udest = (const unsigned char *) dest; 95 | const unsigned char *usrc = (const unsigned char *) src; 96 | 97 | if (indicator == NULL) { 98 | invoke_safe_str_constraint_handler("strcasecmp_s: indicator is null", 99 | NULL, ESNULLP); 100 | return RCNEGATE(ESNULLP); 101 | } 102 | *indicator = 0; 103 | 104 | if (dest == NULL) { 105 | invoke_safe_str_constraint_handler("strcasecmp_s: dest is null", 106 | NULL, ESNULLP); 107 | return RCNEGATE(ESNULLP); 108 | } 109 | 110 | if (src == NULL) { 111 | invoke_safe_str_constraint_handler("strcasecmp_s: src is null", 112 | NULL, ESNULLP); 113 | return RCNEGATE(ESNULLP); 114 | } 115 | 116 | if (dmax == 0) { 117 | invoke_safe_str_constraint_handler("strcasecmp_s: dmax is 0", 118 | NULL, ESZEROL); 119 | return RCNEGATE(ESZEROL); 120 | } 121 | 122 | if (dmax > RSIZE_MAX_STR) { 123 | invoke_safe_str_constraint_handler("strcasecmp_s: dmax exceeds max", 124 | NULL, ESLEMAX); 125 | return RCNEGATE(ESLEMAX); 126 | } 127 | 128 | while (*udest && *usrc && dmax) { 129 | 130 | if (toupper(*udest) != toupper(*usrc)) { 131 | break; 132 | } 133 | 134 | udest++; 135 | usrc++; 136 | dmax--; 137 | } 138 | 139 | *indicator = (toupper(*udest) - toupper(*usrc)); 140 | return RCNEGATE(EOK); 141 | } 142 | EXPORT_SYMBOL(strcasecmp_s) 143 | -------------------------------------------------------------------------------- /safeclib/strcmp_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strcmp_s.c -- string compare 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * Copyright (c) 2018-2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | #include "safe_str_constraint.h" 34 | #include "safe_str_lib.h" 35 | 36 | /** 37 | * NAME 38 | * strcmp_s 39 | * 40 | * Synpsos 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strcmp_s(const char *dest, rsize_t dmax, 44 | * const char *src, int *indicator) 45 | * 46 | * DESCRIPTION 47 | * Compares string src to string dest. 48 | * 49 | * EXTENSION TO 50 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest pointer to string to compare against 56 | * 57 | * dmax restricted maximum length of string dest 58 | * 59 | * src pointer to the string to be compared to dest 60 | * 61 | * indicator pointer to result indicator, greater than, 62 | * equal to or less than 0, if the string pointed 63 | * to by dest is greater than, equal to or less 64 | * than the string pointed to by src respectively. 65 | * 66 | * OUTPUT PARAMETERS 67 | * indicator updated result indicator 68 | * 69 | * RUNTIME CONSTRAINTS 70 | * Neither dest nor src shall be a null pointer. 71 | * indicator shall not be a null pointer. 72 | * dmax shall not be 0 73 | * dmax shall not be greater than RSIZE_MAX_STR 74 | * 75 | * RETURN VALUE 76 | * indicator, when the return code is OK 77 | * >0 dest greater than src 78 | * 0 strings the same 79 | * <0 dest less than src 80 | * 81 | * EOK 82 | * ESNULLP pointer was null 83 | * ESZEROL length was zero 84 | * ESLEMAX length exceeded max 85 | * 86 | * ALSO SEE 87 | * strcasecmp_s() 88 | * 89 | */ 90 | errno_t 91 | strcmp_s (const char *dest, rsize_t dmax, 92 | const char *src, int *indicator) 93 | { 94 | if (indicator == NULL) { 95 | invoke_safe_str_constraint_handler("strcmp_s: indicator is null", 96 | NULL, ESNULLP); 97 | return RCNEGATE(ESNULLP); 98 | } 99 | *indicator = 0; 100 | 101 | if (dest == NULL) { 102 | invoke_safe_str_constraint_handler("strcmp_s: dest is null", 103 | NULL, ESNULLP); 104 | return RCNEGATE(ESNULLP); 105 | } 106 | 107 | if (src == NULL) { 108 | invoke_safe_str_constraint_handler("strcmp_s: src is null", 109 | NULL, ESNULLP); 110 | return RCNEGATE(ESNULLP); 111 | } 112 | 113 | if (dmax == 0) { 114 | invoke_safe_str_constraint_handler("strcmp_s: dmax is 0", 115 | NULL, ESZEROL); 116 | return RCNEGATE(ESZEROL); 117 | } 118 | 119 | if (dmax > RSIZE_MAX_STR) { 120 | invoke_safe_str_constraint_handler("strcmp_s: dmax exceeds max", 121 | NULL, ESLEMAX); 122 | return RCNEGATE(ESLEMAX); 123 | } 124 | 125 | while (*dest && *src && dmax) { 126 | 127 | if (*dest != *src) { 128 | break; 129 | } 130 | 131 | dest++; 132 | src++; 133 | dmax--; 134 | } 135 | 136 | *indicator = *dest - *src; 137 | return RCNEGATE(EOK); 138 | } 139 | EXPORT_SYMBOL(strcmp_s) 140 | -------------------------------------------------------------------------------- /safeclib/strcmpfld_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strcmpfld_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * Copyright (c) 2018-2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | #include "safe_str_constraint.h" 34 | #include "safe_str_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * strcmpfld_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_str_lib.h" 43 | * errno_t 44 | * strcmpfld_s(const char *dest, rsize_t dmax, 45 | * const char *src, int *indicator) 46 | * 47 | * DESCRIPTION 48 | * Compares the character array pointed to by src to the character array 49 | * pointed to by dest for dmax characters. The null terminator does not 50 | * stop the comparison. 51 | * 52 | * EXTENSION TO 53 | * ISO/IEC TR 24731, Programming languages, environments 54 | * and system software interfaces, Extensions to the C Library, 55 | * Part I: Bounds-checking interfaces 56 | * 57 | * INPUT PARAMETERS 58 | * dest pointer to character array to compare against 59 | * 60 | * dmax restricted maximum length of dest. The length is 61 | * used for the comparison of src against dest. 62 | * 63 | * src pointer to the character array to be compared to dest 64 | * 65 | * indicator pointer to result indicator, greater than, equal 66 | * to or less than 0, if the character array pointed 67 | * to by dest is greater than, equal to or less 68 | * than the character array pointed to by src. 69 | * OUTPUT 70 | * indicator updated result indicator 71 | * 72 | * RUNTIME CONSTRAINTS 73 | * Neither dest nor src shall be a null pointer. 74 | * indicator shall not be a null pointer. 75 | * dmax shall not be 0 76 | * dmax shall not be greater than RSIZE_MAX_STR 77 | * 78 | * RETURN VALUE 79 | * indicator, when the return code is OK 80 | * >0 dest greater than src 81 | * 0 strings the same 82 | * <0 dest less than src 83 | * 84 | * EOK 85 | * ESNULLP pointer was null 86 | * ESZEROL length was zero 87 | * ESLEMAX length exceeded max 88 | * 89 | * ALSO SEE 90 | * strcpyfld_s(), strcpyfldin_s(), strcpyfldout_s() 91 | * 92 | */ 93 | errno_t 94 | strcmpfld_s (const char *dest, rsize_t dmax, 95 | const char *src, int *indicator) 96 | { 97 | if (indicator == NULL) { 98 | invoke_safe_str_constraint_handler("strcmpfld_s: indicator is null", 99 | NULL, ESNULLP); 100 | return (ESNULLP); 101 | } 102 | *indicator = 0; 103 | 104 | if (dest == NULL) { 105 | invoke_safe_str_constraint_handler("strcmpfld_s: dest is null", 106 | NULL, ESNULLP); 107 | return (ESNULLP); 108 | } 109 | 110 | if (src == NULL) { 111 | invoke_safe_str_constraint_handler("strcmpfld_s: src is null", 112 | NULL, ESNULLP); 113 | return (ESNULLP); 114 | } 115 | 116 | if (dmax == 0) { 117 | invoke_safe_str_constraint_handler("strcmpfld_s: dmax is 0", 118 | NULL, ESZEROL); 119 | return (ESZEROL); 120 | } 121 | 122 | if (dmax > RSIZE_MAX_STR) { 123 | invoke_safe_str_constraint_handler("strcmpfld_s: dmax exceeds max", 124 | NULL, ESLEMAX); 125 | return (ESLEMAX); 126 | } 127 | 128 | /* compare for dmax charactrers, not the null! */ 129 | while (dmax) { 130 | 131 | if (*dest != *src) { 132 | break; 133 | } 134 | 135 | dest++; 136 | src++; 137 | dmax--; 138 | } 139 | 140 | *indicator = *dest - *src; 141 | return (EOK); 142 | } 143 | EXPORT_SYMBOL(strcmpfld_s) 144 | -------------------------------------------------------------------------------- /safeclib/strfirstchar_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strfirstchar_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strfirstchar_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strfirstchar_s(char *dest, rsize_t dmax, char c, char **first) 44 | * 45 | * DESCRIPTION 46 | * This function returns a pointer to the first occurrence 47 | * of character c in dest. The scanning stops at the first null 48 | * or after dmax characters. 49 | * 50 | * EXTENSION TO 51 | * ISO/IEC TR 24731, Programming languages, environments 52 | * and system software interfaces, Extensions to the C Library, 53 | * Part I: Bounds-checking interfaces 54 | * 55 | * INPUT PARAMETERS 56 | * dest pointer to string to compare against 57 | * 58 | * dmax restricted maximum length of string 59 | * 60 | * c character to locate 61 | * 62 | * first returned pointer to first occurrence of c 63 | * 64 | * OUTPUT PARAMETERS 65 | * first updated pointer to first occurrence of c 66 | * 67 | * RUNTIME CONSTRAINTS 68 | * dest shall not be a null pointer. 69 | * first shall not be a null pointer. 70 | * dmax shall not be 0 71 | * dmax shall not be greater than RSIZE_MAX_STR 72 | * 73 | * RETURN VALUE 74 | * pointer to first occurence of c, NULL if not found 75 | * 76 | * EOK pointer to first occurrence is returned 77 | * ESNULLP NULL pointer 78 | * ESZEROL zero length 79 | * ESLEMAX length exceeds max limit 80 | * 81 | * ALSO SEE 82 | * strlastchar_s(), strfirstdiff_s(), strfirstsame_s(), 83 | * strlastdiff_s(), strlastsame_s(), 84 | * 85 | */ 86 | errno_t 87 | strfirstchar_s (char *dest, rsize_t dmax, char c, char **first) 88 | { 89 | 90 | if (first == NULL) { 91 | invoke_safe_str_constraint_handler("strfirstchar_s: index is null", 92 | NULL, ESNULLP); 93 | return (ESNULLP); 94 | } 95 | *first = NULL; 96 | 97 | if (dest == NULL) { 98 | invoke_safe_str_constraint_handler("strfirstchar_s: dest is null", 99 | NULL, ESNULLP); 100 | return (ESNULLP); 101 | } 102 | 103 | if (dmax == 0 ) { 104 | invoke_safe_str_constraint_handler("strfirstchar_s: dmax is 0", 105 | NULL, ESZEROL); 106 | return (ESZEROL); 107 | } 108 | 109 | if (dmax > RSIZE_MAX_STR) { 110 | invoke_safe_str_constraint_handler("strfirstchar_s: dmax exceeds max", 111 | NULL, ESLEMAX); 112 | return (ESLEMAX); 113 | } 114 | 115 | while (dmax && *dest) { 116 | 117 | if (*dest == c) { 118 | *first = dest; 119 | return (EOK); 120 | } 121 | dest++; 122 | dmax--; 123 | } 124 | 125 | return (ESNOTFND); 126 | } 127 | EXPORT_SYMBOL(strfirstchar_s) 128 | -------------------------------------------------------------------------------- /safeclib/strfirstdiff_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strfirstdiff_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strfirstdiff_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strfirstdiff_s(const char *dest, rsize_t dmax, 44 | * const char *src, rsize_t *index) 45 | * 46 | * DESCRIPTION 47 | * Returns the index of the first character that is different 48 | * between dest and src. Index is valid only for OK. 49 | * The scanning stops at the first null in dest or src, or 50 | * after dmax characters. 51 | * 52 | * EXTENSION TO 53 | * ISO/IEC TR 24731, Programming languages, environments 54 | * and system software interfaces, Extensions to the C Library, 55 | * Part I: Bounds-checking interfaces 56 | * 57 | * INPUT PARAMETERS 58 | * dest pointer to string to compare against 59 | * 60 | * dmax restricted maximum length of string dest 61 | * 62 | * src pointer to the string to be compared to dest 63 | * 64 | * index pointer to returned index to first difference 65 | * 66 | * OUTPUT PARAMETERS 67 | * index returned index to first difference 68 | * 69 | * RUNTIME CONSTRAINTS 70 | * Neither dest nor src shall be a null pointer. 71 | * indicator shall not be a null pointer. 72 | * dmax shall not be 0. 73 | * dmax shall not be greater than RSIZE_MAX_STR. 74 | * 75 | * RETURN VALUE 76 | * index to first difference, when the return code is OK 77 | * 78 | * EOK index to first diff is returned 79 | * ESNODIFF no difference 80 | * ESNULLP NULL pointer 81 | * ESZEROL zero length 82 | * ESLEMAX length exceeds max limit 83 | * 84 | * ALSO SEE 85 | * strfirstchar_s(), strfirstsame_s(), strlastchar_s(), 86 | * strlastdiff_s(), strlastsame_s() 87 | * 88 | */ 89 | errno_t 90 | strfirstdiff_s (const char *dest, rsize_t dmax, 91 | const char *src, rsize_t *index) 92 | { 93 | const char *rp; 94 | 95 | if (index == NULL) { 96 | invoke_safe_str_constraint_handler("strfirstdiff_s: index is null", 97 | NULL, ESNULLP); 98 | return (ESNULLP); 99 | } 100 | *index = 0; 101 | 102 | if (dest == NULL) { 103 | invoke_safe_str_constraint_handler("strfirstdiff_s: dest is null", 104 | NULL, ESNULLP); 105 | return (ESNULLP); 106 | } 107 | 108 | if (src == NULL) { 109 | invoke_safe_str_constraint_handler("strfirstdiff_s: src is null", 110 | NULL, ESNULLP); 111 | return (ESNULLP); 112 | } 113 | 114 | if (dmax == 0 ) { 115 | invoke_safe_str_constraint_handler("strfirstdiff_s: dmax is 0", 116 | NULL, ESZEROL); 117 | return (ESZEROL); 118 | } 119 | 120 | if (dmax > RSIZE_MAX_STR) { 121 | invoke_safe_str_constraint_handler("strfirstdiff_s: dmax exceeds max", 122 | NULL, ESLEMAX); 123 | return (ESLEMAX); 124 | } 125 | 126 | /* hold reference point */ 127 | rp = dest; 128 | 129 | while (*dest && *src && dmax) { 130 | 131 | if (*dest != *src) { 132 | *index = dest - rp; 133 | return (EOK); 134 | } 135 | dmax--; 136 | dest++; 137 | src++; 138 | } 139 | 140 | return (ESNODIFF); 141 | } 142 | EXPORT_SYMBOL(strfirstdiff_s) 143 | -------------------------------------------------------------------------------- /safeclib/strfirstsame_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strfirstsame_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strfirstsame_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strfirstsame_s(const char *dest, rsize_t dmax, 44 | * const char *src, rsize_t *index) 45 | * 46 | * DESCRIPTION 47 | * Returns the index of the first character that is the 48 | * same between dest and src. The scanning stops at the 49 | * fisrt null in dest or src, or after dmax characters. 50 | * 51 | * EXTENSION TO 52 | * ISO/IEC TR 24731, Programming languages, environments 53 | * and system software interfaces, Extensions to the C Library, 54 | * Part I: Bounds-checking interfaces 55 | * 56 | * INPUT PARAMETERS 57 | * dest pointer to string to compare against 58 | * 59 | * dmax restricted maximum length of string dest 60 | * 61 | * src pointer to the string to be compared to dest 62 | * 63 | * index pointer to returned index 64 | * 65 | * OUTPUT PARAMETERS 66 | * index updated index 67 | * 68 | * RUNTIME CONSTRAINTS 69 | * Neither dest nor src shall be a null pointer. 70 | * indicator shall not be a null pointer. 71 | * dmax shall not be 0 72 | * dmax shall not be greater than RSIZE_MAX_STR 73 | * 74 | * RETURN VALUE 75 | * index to first same char, when the return code is OK 76 | * 77 | * EOK index to first same char is returned 78 | * ESNULLP NULL pointer 79 | * ESZEROL zero length 80 | * ESLEMAX length exceeds max limit 81 | * ESNOTFND not found 82 | * 83 | * ALSO SEE 84 | * strfirstchar_s(), strfirstdiff_s(), strlastchar_s(), 85 | * strlastdiff_s(), strlastsame_s() 86 | * 87 | */ 88 | errno_t 89 | strfirstsame_s (const char *dest, rsize_t dmax, 90 | const char *src, rsize_t *index) 91 | { 92 | const char *rp = 0; 93 | 94 | if (index == NULL) { 95 | invoke_safe_str_constraint_handler("strfirstsame_s: index is null", 96 | NULL, ESNULLP); 97 | return (ESNULLP); 98 | } 99 | *index = 0; 100 | 101 | if (dest == NULL) { 102 | invoke_safe_str_constraint_handler("strfirstsame_s: dest is null", 103 | NULL, ESNULLP); 104 | return (ESNULLP); 105 | } 106 | 107 | if (src == NULL) { 108 | invoke_safe_str_constraint_handler("strfirstsame_s: src is null", 109 | NULL, ESNULLP); 110 | return (ESNULLP); 111 | } 112 | 113 | if (dmax == 0 ) { 114 | invoke_safe_str_constraint_handler("strfirstsame_s: dmax is 0", 115 | NULL, ESZEROL); 116 | return (ESZEROL); 117 | } 118 | 119 | if (dmax > RSIZE_MAX_STR) { 120 | invoke_safe_str_constraint_handler("strfirstsame_s: dmax exceeds max", 121 | NULL, ESLEMAX); 122 | return (ESLEMAX); 123 | } 124 | 125 | /* hold reference point */ 126 | rp = dest; 127 | 128 | /* 129 | * find the offset 130 | */ 131 | while (*dest && *src && dmax) { 132 | 133 | if (*dest == *src) { 134 | *index = (uint32_t)(dest - rp); 135 | return (EOK); 136 | } 137 | 138 | dest++; 139 | src++; 140 | dmax--; 141 | } 142 | 143 | return (ESNOTFND); 144 | } 145 | EXPORT_SYMBOL(strfirstsame_s) 146 | -------------------------------------------------------------------------------- /safeclib/strisalphanumeric_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strisalphanumeric_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strisalphanumeric_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_dest_lib.h" 42 | * bool 43 | * strisalphanumeric_s(const char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * This function checks if the entire string contains 47 | * alphanumerics. The scanning stops at the first null 48 | * or after dmax characters. 49 | * 50 | * EXTENSION TO 51 | * ISO/IEC TR 24731, Programming languages, environments 52 | * and system software interfaces, Extensions to the C Library, 53 | * Part I: Bounds-checking interfaces 54 | * 55 | * INPUT PARAMETERS 56 | * dest pointer to string 57 | * 58 | * dmax maximum length of string 59 | * 60 | * OUTPUT PARAMETERS 61 | * none 62 | * 63 | * Runtime-condestaints 64 | * dest shall not be a null pointer. 65 | * dmax shall not equal zero. 66 | * dmax shall not be greater than RSIZE_MAX_STR. 67 | * 68 | * RETURN VALUE 69 | * true dest is alphanumeric 70 | * false dest is not alphanumeric or an error occurred 71 | * 72 | * ALSO SEE 73 | * strisascii_s(), strisdigit_s(), strishex_s(), strislowercase_s(), 74 | * strismixedcase_s(), strisuppercase_s() 75 | * 76 | */ 77 | bool 78 | strisalphanumeric_s (const char *dest, rsize_t dmax) 79 | { 80 | if (!dest) { 81 | invoke_safe_str_constraint_handler("strisalphanumeric_s: " 82 | "dest is null", 83 | NULL, ESNULLP); 84 | return (false); 85 | } 86 | 87 | if (dmax == 0) { 88 | invoke_safe_str_constraint_handler("strisalphanumeric_s: " 89 | "dmax is 0", 90 | NULL, ESZEROL); 91 | return (false); 92 | } 93 | 94 | if (dmax > RSIZE_MAX_STR) { 95 | invoke_safe_str_constraint_handler("strisalphanumeric_s: " 96 | "dmax exceeds max", 97 | NULL, ESLEMAX); 98 | return (false); 99 | } 100 | 101 | if (*dest == '\0') { 102 | return (false); 103 | } 104 | 105 | while (dmax && *dest) { 106 | 107 | if (( (*dest >= '0') && (*dest <= '9') ) || 108 | ( (*dest >= 'a') && (*dest <= 'z') ) || 109 | ( (*dest >= 'A') && (*dest <= 'Z') )) { 110 | dest++; 111 | dmax--; 112 | } else { 113 | return (false); 114 | } 115 | } 116 | 117 | return (true); 118 | } 119 | EXPORT_SYMBOL(strisalphanumeric_s) 120 | -------------------------------------------------------------------------------- /safeclib/strisascii_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strisascii_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /* 37 | *- 38 | * NAME 39 | * strisascii_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_str_lib.h" 43 | * bool 44 | * strisascii_s(const char *dest, rsize_t dmax) 45 | * 46 | * DESCRIPTION 47 | * This function checks if the entire string contains ascii 48 | * characters. The scanning stops at the first null or 49 | * at most dmax characters. 50 | * 51 | * EXTENSION TO 52 | * ISO/IEC TR 24731, Programming languages, environments 53 | * and system software interfaces, Extensions to the C Library, 54 | * Part I: Bounds-checking interfaces 55 | * 56 | * INPUT PARAMETERS 57 | * dest pointer to string 58 | * 59 | * dmax maximum length of string 60 | * 61 | * OUTPUT PARAMETERS 62 | * none 63 | * 64 | * RUNTIME CONSTRAINTS 65 | * dest shall not be a null pointer. 66 | * dmax shall not equal zero. 67 | * dmax shall not be greater than RSIZE_MAX_STR. 68 | * 69 | * RETURN VALUE 70 | * true, string is ascii 71 | * false, string contains one or more non-ascii or an error occurred 72 | * 73 | * ALSO SEE 74 | * strisalphanumeric_s(), strisdigit_s(), strishex_s(), 75 | * strislowercase_s(), strismixedcase_s(), strisuppercase_s() 76 | *- 77 | */ 78 | bool 79 | strisascii_s (const char *dest, rsize_t dmax) 80 | { 81 | if (!dest) { 82 | invoke_safe_str_constraint_handler("strisascii_s: dest is null", 83 | NULL, ESNULLP); 84 | return (false); 85 | } 86 | 87 | if (dmax == 0) { 88 | invoke_safe_str_constraint_handler("strisascii_s: dmax is 0", 89 | NULL, ESZEROL); 90 | return (false); 91 | } 92 | 93 | if (dmax > RSIZE_MAX_STR) { 94 | invoke_safe_str_constraint_handler("strisascii_s: dmax " 95 | "exceeds max", 96 | NULL, ESLEMAX); 97 | return (false); 98 | } 99 | 100 | while (dmax && *dest) { 101 | if ((unsigned char)*dest > 127) { 102 | return (false); 103 | } 104 | dest++; 105 | dmax--; 106 | } 107 | 108 | return (true); 109 | } 110 | EXPORT_SYMBOL(strisascii_s) 111 | -------------------------------------------------------------------------------- /safeclib/strisdigit_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strisdigit_s 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * Copyright (c) 2022 by Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | #include "safe_str_constraint.h" 34 | #include "safe_str_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * strisdigit_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_str_lib.h" 43 | * bool 44 | * strisdigit_s(const char *dest, rsize_t dmax) 45 | * 46 | * DESCRIPTION 47 | * This function checks that the entire string contains digits. 48 | * The scanning stops at the first null or after dmax characters. 49 | * 50 | * EXTENSION TO 51 | * ISO/IEC TR 24731, Programming languages, environments 52 | * and system software interfaces, Extensions to the C Library, 53 | * Part I: Bounds-checking interfaces 54 | * 55 | * INPUT PARAMETERS 56 | * dest pointer to string 57 | * 58 | * dmax maximum length of string 59 | * 60 | * OUTPUT PARAMETERS 61 | * none 62 | * 63 | * RUNTIME CONSTRAINTS 64 | * dest shall not be a null pointer. 65 | * dmax shall not equal zero. 66 | * dmax shall not be greater than RSIZE_MAX_STR. 67 | * 68 | * RETURN VALUE 69 | * true string is digit 70 | * false string is not digit or an error occurred 71 | * 72 | * ALSO SEE 73 | * strisalphanumeric_s(), strisascii_s(), strishex_s(), 74 | * strislowercase_s(), strismixedcase_s(), strisuppercase_s() 75 | * 76 | */ 77 | bool 78 | strisdigit_s (const char *dest, rsize_t dmax) 79 | { 80 | if (!dest) { 81 | invoke_safe_str_constraint_handler("strisdigit_s: dest is null", 82 | NULL, ESNULLP); 83 | return (false); 84 | } 85 | 86 | if (dmax == 0) { 87 | invoke_safe_str_constraint_handler("strisdigit_s: dmax is 0", 88 | NULL, ESZEROL); 89 | return (false); 90 | } 91 | 92 | if (dmax > RSIZE_MAX_STR) { 93 | invoke_safe_str_constraint_handler("strisdigit_s: dmax exceeds max", 94 | NULL, ESLEMAX); 95 | return (false); 96 | } 97 | 98 | if (*dest == '\0') { 99 | return (false); 100 | } 101 | 102 | while (dmax && *dest) { 103 | 104 | if ((*dest < '0') || (*dest > '9')) { 105 | return (false); 106 | } 107 | dest++; 108 | dmax--; 109 | } 110 | 111 | return (true); 112 | } 113 | EXPORT_SYMBOL(strisdigit_s) 114 | -------------------------------------------------------------------------------- /safeclib/strishex_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strishex_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strishex_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * bool 43 | * strishex_s(const char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * This function checks that the entire string contains 47 | * hex characters. The scanning stops at the first null 48 | * or after dmax characters. 49 | * 50 | * EXTENSION TO 51 | * ISO/IEC TR 24731, Programming languages, environments 52 | * and system software interfaces, Extensions to the C Library, 53 | * Part I: Bounds-checking interfaces 54 | * 55 | * INPUT PARAMETERS 56 | * dest pointer to string 57 | * 58 | * dmax maximum length of string 59 | * 60 | * OUTPUT PARAMETERS 61 | * none 62 | * 63 | * RUNTIME CONSTRAINTS 64 | * dest shall not be a null pointer. 65 | * dmax shall not equal zero. 66 | * dmax shall not be greater than RSIZE_MAX_STR. 67 | * 68 | * RETURN VALUE 69 | * true string is hex 70 | * false string is not hex or an error occurred 71 | * 72 | * ALSO SEE 73 | * strisalphanumeric_s(), strisascii_s(), strisdigit_s(), 74 | * strislowercase_s(), strismixedcase_s(), 75 | * strisuppercase_s() 76 | * 77 | */ 78 | bool 79 | strishex_s (const char *dest, rsize_t dmax) 80 | { 81 | if (!dest) { 82 | invoke_safe_str_constraint_handler("strishex_s: dest is null", 83 | NULL, ESNULLP); 84 | return (false); 85 | } 86 | 87 | if (dmax == 0) { 88 | invoke_safe_str_constraint_handler("strishex_s: dmax is 0", 89 | NULL, ESZEROL); 90 | return (false); 91 | } 92 | 93 | if (dmax > RSIZE_MAX_STR) { 94 | invoke_safe_str_constraint_handler("strishex_s: dmax exceeds max", 95 | NULL, ESLEMAX); 96 | return (false); 97 | } 98 | 99 | if (*dest == '\0') { 100 | return (false); 101 | } 102 | 103 | while (dmax && *dest) { 104 | 105 | if (((*dest >= '0') && (*dest <= '9')) || 106 | ((*dest >= 'a') && (*dest <= 'f')) || 107 | ((*dest >= 'A') && (*dest <= 'F'))) { 108 | dest++; 109 | dmax--; 110 | 111 | } else { 112 | return (false); 113 | } 114 | } 115 | 116 | return (true); 117 | } 118 | EXPORT_SYMBOL(strishex_s) 119 | -------------------------------------------------------------------------------- /safeclib/strislowercase_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strislowercase_s.c 3 | * 4 | * Copyright (c) 2005 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strislowercase_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * bool 43 | * strislowercase_s(const char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * This function checks if entire string is lowercase. 47 | * The scanning stops at the first null or after dmax 48 | * characters. 49 | * 50 | * EXTENSION TO 51 | * ISO/IEC TR 24731, Programming languages, environments 52 | * and system software interfaces, Extensions to the C Library, 53 | * Part I: Bounds-checking interfaces 54 | * 55 | * INPUT PARAMETERS 56 | * dest pointer to string 57 | * 58 | * dmax maximum length of string 59 | * 60 | * OUTPUT PARAMETERS 61 | * none 62 | * 63 | * RUNTIME CONSTRAINTS 64 | * dest shall not be a null pointer. 65 | * dest shal be null terminated. 66 | * dmax shall not equal zero. 67 | * dmax shall not be greater than RSIZE_MAX_STR. 68 | * 69 | * RETURN VALUE 70 | * true string is lowercase 71 | * false string is not lowercase or an error occurred 72 | * 73 | * ALSO SEE 74 | * strisalphanumeric_s(), strisascii_s(), strisdigit_s(), 75 | * strishex_s(), strismixedcase_s(), 76 | * strisuppercase_s() 77 | * 78 | */ 79 | bool 80 | strislowercase_s (const char *dest, rsize_t dmax) 81 | { 82 | if (!dest) { 83 | invoke_safe_str_constraint_handler("strislowercase_s: " 84 | "dest is null", 85 | NULL, ESNULLP); 86 | return (false); 87 | } 88 | 89 | if (dmax == 0) { 90 | invoke_safe_str_constraint_handler("strislowercase_s: " 91 | "dmax is 0", 92 | NULL, ESZEROL); 93 | return (false); 94 | } 95 | 96 | if (dmax > RSIZE_MAX_STR) { 97 | invoke_safe_str_constraint_handler("strislowercase_s: " 98 | "dmax exceeds max", 99 | NULL, ESLEMAX); 100 | return (false); 101 | } 102 | 103 | if (*dest == '\0') { 104 | return (false); 105 | } 106 | 107 | while (dmax && *dest) { 108 | 109 | if ((*dest < 'a') || (*dest > 'z')) { 110 | return (false); 111 | } 112 | dest++; 113 | dmax--; 114 | } 115 | 116 | return (true); 117 | } 118 | EXPORT_SYMBOL(strislowercase_s) 119 | -------------------------------------------------------------------------------- /safeclib/strismixedcase_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strismixedcase_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * Copyright (c) 2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | #include "safe_str_constraint.h" 34 | #include "safe_str_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * strismixedcase_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_str_lib.h" 43 | * bool 44 | * strismixedcase_s(const char *dest, rsize_t dmax) 45 | * 46 | * DESCRIPTION 47 | * This function checks that the entire string is mixed 48 | * case. The scanning stops at the first null or after 49 | * dmax characters. 50 | * 51 | * EXTENSION TO 52 | * ISO/IEC TR 24731, Programming languages, environments 53 | * and system software interfaces, Extensions to the C Library, 54 | * Part I: Bounds-checking interfaces 55 | * 56 | * INPUT PARAMETERS 57 | * dest pointer to string 58 | * 59 | * dmax maximum length of string 60 | * 61 | * OUTPUT PARAMETERS 62 | * none 63 | * 64 | * RUNTIME CONSTRAINTS 65 | * dest shall not be a null pointer. 66 | * dmax shall not equal zero. 67 | * dmax shall not be greater than RSIZE_MAX_STR. 68 | * 69 | * RETURN VALUE 70 | * true string is mixed case 71 | * false string is not mixed case or error 72 | * 73 | * ALSO SEE 74 | * strisalphanumeric_s(), strisascii_s(), strisdigit_s(), 75 | * strishex_s(), strislowercase_s(), 76 | * strisuppercase_s() 77 | * 78 | */ 79 | bool 80 | strismixedcase_s (const char *dest, rsize_t dmax) 81 | { 82 | if (!dest) { 83 | invoke_safe_str_constraint_handler("strismixedcase_s: " 84 | "dest is null", 85 | NULL, ESNULLP); 86 | return (false); 87 | } 88 | 89 | if (dmax == 0) { 90 | invoke_safe_str_constraint_handler("strismixedcase_s: " 91 | "dmax is 0", 92 | NULL, ESZEROL); 93 | return (false); 94 | } 95 | 96 | if (dmax > RSIZE_MAX_STR) { 97 | invoke_safe_str_constraint_handler("strismixedcase_s: " 98 | "dmax exceeds max", 99 | NULL, ESLEMAX); 100 | return (false); 101 | } 102 | 103 | if (*dest == '\0') { 104 | return (false); 105 | } 106 | 107 | while (dmax && *dest) { 108 | 109 | if (((*dest >= 'a') && (*dest <= 'z')) || 110 | ((*dest >= 'A') && (*dest <= 'Z'))) { 111 | dest++; 112 | dmax--; 113 | } else { 114 | return (false); 115 | } 116 | } 117 | 118 | return (true); 119 | } 120 | EXPORT_SYMBOL(strismixedcase_s) 121 | -------------------------------------------------------------------------------- /safeclib/strisuppercase_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strisuppercase_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * Copyright (c) 2022 Intel Corp 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | #include "safe_str_constraint.h" 34 | #include "safe_str_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * strisuppercase_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_str_lib.h" 43 | * bool 44 | * strisuppercase_s(const char *dest, rsize_t dmax) 45 | * 46 | * DESCRIPTION 47 | * This function checks if entire string is uppercase 48 | * The scanning stops at the first null or after dmax 49 | * characters. 50 | * 51 | * EXTENSION TO 52 | * ISO/IEC TR 24731, Programming languages, environments 53 | * and system software interfaces, Extensions to the C Library, 54 | * Part I: Bounds-checking interfaces 55 | * 56 | * INPUT PARAMETERS 57 | * dest pointer to string 58 | * 59 | * dmax maximum length of string 60 | * 61 | * OUTPUT PARAMETERS 62 | * none 63 | * 64 | * RUNTIME CONSTRAINTS 65 | * dest shall not be a null pointer. 66 | * dmax shall not equal zero. 67 | * dmax shall not be greater than RSIZE_MAX_STR. 68 | * 69 | * RETURN VALUE 70 | * true string is uppercase 71 | * false string is not uppercase or an error occurred 72 | * 73 | * ALSO SEE 74 | * strisalphanumeric_s(), strisascii_s(), strisdigit_s(), 75 | * strishex_s(), strislowercase_s(), strismixedcase_s(), 76 | * 77 | */ 78 | bool 79 | strisuppercase_s (const char *dest, rsize_t dmax) 80 | { 81 | 82 | if (!dest) { 83 | invoke_safe_str_constraint_handler("strisuppercase_s: " 84 | "dest is null", 85 | NULL, ESNULLP); 86 | return (false); 87 | } 88 | 89 | if (dmax == 0) { 90 | invoke_safe_str_constraint_handler("strisuppercase_s: " 91 | "dmax is 0", 92 | NULL, ESZEROL); 93 | return (false); 94 | } 95 | 96 | if (dmax > RSIZE_MAX_STR) { 97 | invoke_safe_str_constraint_handler("strisuppercase_s: " 98 | "dmax exceeds max", 99 | NULL, ESLEMAX); 100 | return (false); 101 | } 102 | 103 | if (*dest == '\0') { 104 | return (false); 105 | } 106 | 107 | while (dmax && *dest) { 108 | 109 | if ((*dest < 'A') || (*dest > 'Z')) { 110 | return (false); 111 | } 112 | dest++; 113 | dmax--; 114 | } 115 | 116 | return (true); 117 | } 118 | EXPORT_SYMBOL(strisuppercase_s) 119 | -------------------------------------------------------------------------------- /safeclib/strlastchar_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strlastchar_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strlastchar_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strlastchar_s(char *dest, rsize_t dmax, char c, char **last) 44 | * 45 | * DESCRIPTION 46 | * Returns a pointer to the last occurrence of character c in 47 | * dest. The scanning stops at the first null or after dmax 48 | * characters. 49 | * 50 | * EXTENSION TO 51 | * ISO/IEC TR 24731, Programming languages, environments 52 | * and system software interfaces, Extensions to the C Library, 53 | * Part I: Bounds-checking interfaces 54 | * 55 | * INPUT PARAMETERS 56 | * dest pointer to string 57 | * 58 | * dmax restricted maximum length of string 59 | * 60 | * c character to locate 61 | * 62 | * last returned pointer to last occurrence 63 | * 64 | * OUTPUT PARAMETERS 65 | * last updated pointer to last occurrence 66 | * 67 | * RUNTIME CONSTRAINTS 68 | * dest shall not be a null pointer. 69 | * last shall not be a null pointer. 70 | * dmax shall not be 0 71 | * dmax shall not be greater than RSIZE_MAX_STR 72 | * 73 | * RETURN VALUE 74 | * pointer to the last occurrence, when the return code is OK 75 | * 76 | * EOK pointer to the last occurence is returned 77 | * ESNOTFND c not found in dest 78 | * ESNULLP NULL pointer 79 | * ESZEROL zero length 80 | * ESLEMAX length exceeds max limit 81 | * 82 | * ALSO SEE 83 | * strfirstchar_s(), strfirstdiff_s(), strfirstsame_s(), 84 | * strlastdiff_s(), strlastsame_s() 85 | * 86 | */ 87 | errno_t 88 | strlastchar_s(char *dest, rsize_t dmax, char c, char **last) 89 | { 90 | if (last == NULL) { 91 | invoke_safe_str_constraint_handler("strlastchar_s: last is null", 92 | NULL, ESNULLP); 93 | return (ESNULLP); 94 | } 95 | *last = NULL; 96 | 97 | if (dest == NULL) { 98 | invoke_safe_str_constraint_handler("strlastchar_s: dest is null", 99 | NULL, ESNULLP); 100 | return (ESNULLP); 101 | } 102 | 103 | if (dmax == 0 ) { 104 | invoke_safe_str_constraint_handler("strlastchar_s: dmax is 0", 105 | NULL, ESZEROL); 106 | return (ESZEROL); 107 | } 108 | 109 | if (dmax > RSIZE_MAX_STR) { 110 | invoke_safe_str_constraint_handler("strlastchar_s: dmax exceeds max", 111 | NULL, ESLEMAX); 112 | return (ESLEMAX); 113 | } 114 | 115 | while (dmax && *dest) { 116 | 117 | if (*dest == c) { 118 | *last = dest; 119 | } 120 | 121 | dest++; 122 | dmax--; 123 | } 124 | 125 | if (*last == NULL) { 126 | return (ESNOTFND); 127 | } else { 128 | return (EOK); 129 | } 130 | } 131 | EXPORT_SYMBOL(strlastchar_s) 132 | -------------------------------------------------------------------------------- /safeclib/strlastdiff_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strlastdiff_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011, 2013 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strlastdiff_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strlastdiff_s(const char *dest, rsize_t dmax, 44 | * const char *src, rsize_t *index) 45 | * 46 | * DESCRIPTION 47 | * Returns the index of the last character that is different 48 | * between dest and src. Index is valid only for EOK. 49 | * The scanning stops at the first null in dest or src, or 50 | * after dmax characters. 51 | * 52 | * EXTENSION TO 53 | * ISO/IEC TR 24731, Programming languages, environments 54 | * and system software interfaces, Extensions to the C Library, 55 | * Part I: Bounds-checking interfaces 56 | * 57 | * INPUT PARAMETERS 58 | * dest pointer to string to compare against 59 | * 60 | * dmax restricted maximum length of string dest 61 | * 62 | * src pointer to the string to be compared to dest 63 | * 64 | * index pointer to returned index of last difference 65 | * 66 | * OUTPUT PARAMETERS 67 | * index updated index of last difference 68 | * 69 | * RUNTIME CONSTRAINTS 70 | * Neither dest nor src shall be a null pointer. 71 | * indicator shall not be a null pointer. 72 | * dmax shall not be 0 73 | * dmax shall not be greater than RSIZE_MAX_STR 74 | * 75 | * RETURN VALUE 76 | * index to last difference, when the return code is OK 77 | * 78 | * EOK index to last diff is returned 79 | * ESNODIFF no difference 80 | * ESNULLP NULL pointer 81 | * ESZEROL zero length 82 | * ESLEMAX length exceeds max limit 83 | * 84 | * ALSO SEE 85 | * strfirstchar_s(), strfirstdiff_s(), strfirstsame_s(), 86 | * strlastchar_s(), strlastsame_s() 87 | * 88 | */ 89 | errno_t 90 | strlastdiff_s(const char *dest, rsize_t dmax, 91 | const char *src, rsize_t *index) 92 | { 93 | const char *rp; 94 | bool there_is_a_diff = false; 95 | 96 | if (index == NULL) { 97 | invoke_safe_str_constraint_handler("strlastdiff_s: index is null", 98 | NULL, ESNULLP); 99 | return (ESNULLP); 100 | } 101 | *index = 0; 102 | 103 | if (dest == NULL) { 104 | invoke_safe_str_constraint_handler("strlastdiff_s: dest is null", 105 | NULL, ESNULLP); 106 | return (ESNULLP); 107 | } 108 | 109 | if (src == NULL) { 110 | invoke_safe_str_constraint_handler("strlastdiff_s: src is null", 111 | NULL, ESNULLP); 112 | return (ESNULLP); 113 | } 114 | 115 | if (dmax == 0 ) { 116 | invoke_safe_str_constraint_handler("strlastdiff_s: dmax is 0", 117 | NULL, ESZEROL); 118 | return (ESZEROL); 119 | } 120 | 121 | if (dmax > RSIZE_MAX_STR) { 122 | invoke_safe_str_constraint_handler("strlastdiff_s: dmax exceeds max", 123 | NULL, ESLEMAX); 124 | return (ESLEMAX); 125 | } 126 | 127 | /* hold reference point */ 128 | rp = dest; 129 | 130 | /* 131 | * find the last diff 132 | */ 133 | while (*dest && *src && dmax) { 134 | 135 | if (*dest != *src) { 136 | there_is_a_diff = true; 137 | *index = dest - rp; 138 | } 139 | 140 | dest++; 141 | src++; 142 | dmax--; 143 | } 144 | 145 | if (there_is_a_diff) { 146 | return (EOK); 147 | } else { 148 | return (ESNODIFF); 149 | } 150 | } 151 | EXPORT_SYMBOL(strlastdiff_s) 152 | -------------------------------------------------------------------------------- /safeclib/strlastsame_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strlastsame_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strlastsame_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strlastsame_s(const char *dest, rsize_t dmax, 44 | * const char *src, rsize_t *index) 45 | * 46 | * DESCRIPTION 47 | * Returns the index of the last character that is the 48 | * same between dest and src. The scanning stops at the 49 | * first nul in dest or src, or after dmax characters. 50 | * 51 | * EXTENSION TO 52 | * ISO/IEC TR 24731, Programming languages, environments 53 | * and system software interfaces, Extensions to the C Library, 54 | * Part I: Bounds-checking interfaces 55 | * 56 | * INPUT PARAMETERS 57 | * dest pointer to string to compare against 58 | * 59 | * dmax restricted maximum length of string dest 60 | * 61 | * src pointer to the string to be compared to dest 62 | * 63 | * index pointer to returned index 64 | * 65 | * OUTPUT PARAMETERS 66 | * index updated index 67 | * 68 | * RUNTIME CONSTRAINTS 69 | * Neither dest nor src shall not be a null pointer. 70 | * indicator shall not be a null pointer. 71 | * dmax shall not be 0 72 | * dmax shall not be greater than RSIZE_MAX_STR 73 | * 74 | * RETURN VALUE 75 | * index to last same char, when the return code is OK 76 | * 77 | * EOK index to last same char is returned 78 | * ESNULLP NULL pointer 79 | * ESZEROL zero length 80 | * ESLEMAX length exceeds max limit 81 | * ESNOTFND not found 82 | * ESUNTERM string unterminated 83 | * 84 | * ALSO SEE 85 | * strfirstchar_s(), strfirstdiff_s(), strfirstsame_s(), 86 | * strlastchar_s(), strlastdiff_s() 87 | * 88 | */ 89 | errno_t 90 | strlastsame_s (const char *dest, rsize_t dmax, 91 | const char *src, rsize_t *index) 92 | { 93 | const char *rp; 94 | bool similarity; 95 | 96 | if (index == NULL) { 97 | invoke_safe_str_constraint_handler("strlastsame_s: index is null", 98 | NULL, ESNULLP); 99 | return (ESNULLP); 100 | } 101 | *index = 0; 102 | 103 | if (dest == NULL) { 104 | invoke_safe_str_constraint_handler("strlastsame_s: dest is null", 105 | NULL, ESNULLP); 106 | return (ESNULLP); 107 | } 108 | 109 | if (src == NULL) { 110 | invoke_safe_str_constraint_handler("strlastsame_s: src is null", 111 | NULL, ESNULLP); 112 | return (ESNULLP); 113 | } 114 | 115 | if (dmax == 0 ) { 116 | invoke_safe_str_constraint_handler("strlastsame_s: dmax is 0", 117 | NULL, ESZEROL); 118 | return (ESZEROL); 119 | } 120 | 121 | if (dmax > RSIZE_MAX_STR) { 122 | invoke_safe_str_constraint_handler("strlastsame_s: dmax exceeds max", 123 | NULL, ESLEMAX); 124 | return (ESLEMAX); 125 | } 126 | 127 | /* hold reference point */ 128 | rp = dest; 129 | 130 | /* 131 | * find the last offset 132 | */ 133 | similarity = false; 134 | while (*dest && *src && dmax) { 135 | 136 | if (*dest == *src) { 137 | similarity = true; 138 | *index = (uint32_t)(dest - rp); 139 | } 140 | 141 | dest++; 142 | src++; 143 | dmax--; 144 | } 145 | 146 | if (similarity) { 147 | return (EOK); 148 | } else { 149 | return (ESNOTFND); 150 | } 151 | } 152 | EXPORT_SYMBOL(strlastsame_s) 153 | -------------------------------------------------------------------------------- /safeclib/strljustify_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strljustify_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reseved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strljustify_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strljustify_s(char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * Removes beginning whitespace from the string pointed to by 47 | * dest by shifting the text left over writting the beginning 48 | * whitespace, left justifying the text. The left justified 49 | * text is null terminated. 50 | * 51 | * The text is shifted so the original pointer can continue 52 | * to be used. 53 | * 54 | * EXTENSION TO 55 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 56 | * and system software interfaces, Extensions to the C Library, 57 | * Part I: Bounds-checking interfaces 58 | * 59 | * INPUT PARAMETERS 60 | * dest pointer to string to left justify 61 | * 62 | * dmax restricted maximum length of string 63 | * 64 | * OUTPUT PARAMETERS 65 | * dest left justified 66 | * 67 | * RUNTIME CONSTRAINTS 68 | * dest shall not be a null pointer. 69 | * dmax shall not be 0 70 | * dmax shall not be greater than RSIZE_MAX_STR 71 | * dest shall be null terminated 72 | * 73 | * RETURN VALUE 74 | * EOK 75 | * ESNULLP NULL pointer 76 | * ESZEROL zero length 77 | * ESLEMAX length exceeds max limit 78 | * ESUNTERM dest was not null terminated 79 | * 80 | * ALSO SEE 81 | * strremovews_s(), 82 | * 83 | */ 84 | errno_t 85 | strljustify_s (char *dest, rsize_t dmax) 86 | { 87 | char *orig_dest; 88 | rsize_t orig_dmax; 89 | 90 | if (dest == NULL) { 91 | invoke_safe_str_constraint_handler("strljustify_s_s: " 92 | "dest is null", 93 | NULL, ESNULLP); 94 | return (ESNULLP); 95 | } 96 | 97 | if (dmax == 0 ) { 98 | invoke_safe_str_constraint_handler("strljustify_s_s: " 99 | "dmax is 0", 100 | NULL, ESZEROL); 101 | return (ESZEROL); 102 | } 103 | 104 | if (dmax > RSIZE_MAX_STR) { 105 | invoke_safe_str_constraint_handler("strljustify_s_s: " 106 | "dmax exceeds max", 107 | NULL, ESLEMAX); 108 | return (ESLEMAX); 109 | } 110 | 111 | /* 112 | * corner case, a dmax of one allows only for a null 113 | */ 114 | if (*dest == '\0' || dmax <= RSIZE_MIN_STR) { 115 | *dest = '\0'; 116 | return (EOK); 117 | } 118 | 119 | orig_dmax = dmax; 120 | orig_dest = dest; 121 | 122 | /* 123 | * scan the string to be sure it is properly terminated 124 | */ 125 | while (*dest) { 126 | if (dmax == 0) { 127 | while (orig_dmax) { *orig_dest++ = '\0'; orig_dmax--; } 128 | 129 | invoke_safe_str_constraint_handler( 130 | "strljustify_s: dest is unterminated", 131 | NULL, ESUNTERM); 132 | return (ESUNTERM); 133 | } 134 | dmax--; 135 | dest++; 136 | } 137 | 138 | /* 139 | * find first non-white space char 140 | */ 141 | dest = orig_dest; 142 | while ((*dest == ' ') || (*dest == '\t')) { 143 | dest++; 144 | } 145 | 146 | /* 147 | * shift text, removing spaces, to left justify 148 | */ 149 | if (orig_dest != dest && *dest) { 150 | while (*dest) { 151 | *orig_dest++ = *dest; 152 | *dest++ = ' '; 153 | } 154 | *orig_dest = '\0'; 155 | } 156 | 157 | return (EOK); 158 | } 159 | EXPORT_SYMBOL(strljustify_s) 160 | -------------------------------------------------------------------------------- /safeclib/strnlen_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strnlen_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strnlen_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * rsize_t 43 | * strnlen_s(const char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * The strnlen_s function computes the length of the string pointed 47 | * to by dest. 48 | * 49 | * SPECIFIED IN 50 | * ISO/IEC TR 24731-1, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest pointer to string 56 | * 57 | * dmax restricted maximum length. 58 | * 59 | * OUTPUT PARAMETERS 60 | * none 61 | * 62 | * RUNTIME CONSTRAINTS 63 | * dest shall not be a null pointer 64 | * dmax shall not be greater than RSIZE_MAX_STR 65 | * dmax shall not equal zero 66 | * 67 | * RETURN VALUE 68 | * The function returns the string length, excluding the terminating 69 | * null character. If dest is NULL, then strnlen_s returns 0. 70 | * 71 | * Otherwise, the strnlen_s function returns the number of characters 72 | * that precede the terminating null character. If there is no null 73 | * character in the first dmax characters of dest then strnlen_s returns 74 | * dmax. At most the first dmax characters of dest are accessed 75 | * by strnlen_s. 76 | * 77 | * ALSO SEE 78 | * strnterminate_s() 79 | * 80 | */ 81 | rsize_t 82 | strnlen_s (const char *dest, rsize_t dmax) 83 | { 84 | rsize_t count; 85 | 86 | if (dest == NULL) { 87 | return RCNEGATE(0); 88 | } 89 | 90 | if (dmax == 0) { 91 | invoke_safe_str_constraint_handler("strnlen_s: dmax is 0", 92 | NULL, ESZEROL); 93 | return RCNEGATE(0); 94 | } 95 | 96 | if (dmax > RSIZE_MAX_STR) { 97 | invoke_safe_str_constraint_handler("strnlen_s: dmax exceeds max", 98 | NULL, ESLEMAX); 99 | return RCNEGATE(0); 100 | } 101 | 102 | count = 0; 103 | while (dmax && *dest) { 104 | count++; 105 | dmax--; 106 | dest++; 107 | } 108 | 109 | return RCNEGATE(count); 110 | } 111 | EXPORT_SYMBOL(strnlen_s) 112 | -------------------------------------------------------------------------------- /safeclib/strnterminate_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strnterminate_s.c 3 | * 4 | * Copyright (c) 2011 Bo Berry 5 | * Copyright (c) 2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strnterminate_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * rsize_t 43 | * strnterminate_s(char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * The strnterminate_s function will terminate the string if a 47 | * null is not encountered before dmax characters. 48 | * 49 | * EXTENSION TO 50 | * ISO/IEC TR 24731-1, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest - pointer to string 56 | * 57 | * dmax - restricted maximum length 58 | * 59 | * OUTPUT PARAMETERS 60 | * dest - dest is terminated if needed 61 | * 62 | * RUNTIME CONSTRAINTS 63 | * dest shall not be a null pointer 64 | * dmax shall not be greater than RSIZE_MAX_STR 65 | * dmax shall not equal zero 66 | * 67 | * RETURN VALUE 68 | * The function returns a terminated string. If a null is not 69 | * encountered prior to dmax characters, the dmax character is 70 | * set to null terminating the string. The string length is 71 | * also returned. 72 | * 73 | * ALSO SEE 74 | * strnlen_s() 75 | * 76 | */ 77 | rsize_t 78 | strnterminate_s (char *dest, rsize_t dmax) 79 | { 80 | rsize_t count; 81 | 82 | if (dest == NULL) { 83 | return (0); 84 | } 85 | 86 | if (dmax == 0) { 87 | invoke_safe_str_constraint_handler("strnterminate_s: dmax is 0", 88 | NULL, ESZEROL); 89 | return (0); 90 | } 91 | 92 | if (dmax > RSIZE_MAX_STR) { 93 | invoke_safe_str_constraint_handler("strnterminate_s: dmax exceeds max", 94 | NULL, ESLEMAX); 95 | return (0); 96 | } 97 | 98 | count = 0; 99 | while (dmax > 1) { 100 | if (*dest) { 101 | count++; 102 | dmax--; 103 | dest++; 104 | } else { 105 | break; 106 | } 107 | } 108 | *dest = '\0'; 109 | 110 | return (count); 111 | } 112 | EXPORT_SYMBOL(strnterminate_s) 113 | -------------------------------------------------------------------------------- /safeclib/strprefix_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strprefix_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strprefix_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strprefix_s(const char *dest, rsize_t dmax, const char *src) 44 | * 45 | * DESCRIPTION 46 | * Determines if the prefix pointed to by src is at the 47 | * beginning of string pointed to by dest. The prefix 48 | * must be a complete match in dest. Useful for command 49 | * or user input parsing. The scanning stops at the first 50 | * null in dest or src, or after dmax characters. 51 | * 52 | * EXTENSION TO 53 | * ISO/IEC TR 24731-1, Programming languages, environments 54 | * and system software interfaces, Extensions to the C Library, 55 | * Part I: Bounds-checking interfaces 56 | * 57 | * INPUT PARAMETERS 58 | * dest pointer to string to compare against 59 | * 60 | * dmax restricted maximum length of dest 61 | * 62 | * src pointer to the prefix 63 | * 64 | * OUTPUT PARAMETERS 65 | * none 66 | * 67 | * RUNTIME CONSTRAINTS 68 | * Neither dest nor src shall be a null pointer. 69 | * dmax shall not equal zero. 70 | * dmax shall not be greater than RSIZE_MAX_STR. 71 | * 72 | * RETURN VALUE 73 | * EOK successful operation, prefix present in dest 74 | * ESNULLP NULL pointer 75 | * ESZEROL zero length 76 | * ESLEMAX length exceeds max limit 77 | * ESNOTFND prefix not found in dest 78 | * 79 | * ALSO SEE 80 | * strspn_s(), strcspn_s(), strpbrk_s(), strstr_s() 81 | * 82 | */ 83 | errno_t 84 | strprefix_s (const char *dest, rsize_t dmax, const char *src) 85 | { 86 | if (dest == NULL) { 87 | invoke_safe_str_constraint_handler("strprefix_s: dest is null", 88 | NULL, ESNULLP); 89 | return (ESNULLP); 90 | } 91 | 92 | if (src == NULL) { 93 | invoke_safe_str_constraint_handler("strprefix_s: src is null", 94 | NULL, ESNULLP); 95 | return (ESNULLP); 96 | } 97 | 98 | if (dmax == 0) { 99 | invoke_safe_str_constraint_handler("strprefix_s: dmax is 0", 100 | NULL, ESZEROL); 101 | return (ESZEROL); 102 | } 103 | 104 | if (dmax > RSIZE_MAX_STR) { 105 | invoke_safe_str_constraint_handler("strprefix_s: dmax exceeds max", 106 | NULL, ESLEMAX); 107 | return (ESLEMAX); 108 | } 109 | 110 | if (*src == '\0') { 111 | return (ESNOTFND); 112 | } 113 | 114 | while (*src && dmax) { 115 | 116 | if (*dest != *src) { 117 | return (ESNOTFND); 118 | } 119 | 120 | dmax--; 121 | dest++; 122 | src++; 123 | } 124 | 125 | return (EOK); 126 | } 127 | EXPORT_SYMBOL(strprefix_s) 128 | -------------------------------------------------------------------------------- /safeclib/strremovews_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strremovews_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights resevered. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strremovews_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strremovews_s(char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * Removes beginning and trailing whitespace from the string pointed to by 47 | * dest by shifting the text left over writting the beginning whitespace. 48 | * The shifted-trimmed text is null terminated. 49 | * 50 | * The text is shifted so the original pointer can continue to be used. This 51 | * is useful when the memory was malloc'ed and will need to be freed. 52 | * 53 | * EXTENSION TO 54 | * ISO/IEC TR 24731, Programming languages, environments 55 | * and system software interfaces, Extensions to the C Library, 56 | * Part I: Bounds-checking interfaces 57 | * 58 | * INPUT PARAMETERS 59 | * dest pointer to string to remove whitespace 60 | * 61 | * dmax restricted maximum length of string 62 | * 63 | * RUNTIME CONSTRAINTS 64 | * dest shall not be a null pointer. 65 | * dmax shall not be 0 66 | * dmax shall not be greater than RSIZE_MAX_STR 67 | * dest shall be null terminated 68 | * 69 | * RETURN VALUE 70 | * EOK 71 | * ESNULLP NULL pointer 72 | * ESZEROL zero length 73 | * ESLEMAX length exceeds max limit 74 | * ESUNTERM dest was not null terminated 75 | * 76 | * SEE ALSO 77 | * strljustify_s(), 78 | * 79 | */ 80 | errno_t 81 | strremovews_s (char *dest, rsize_t dmax) 82 | { 83 | char *orig_dest; 84 | char *orig_end; 85 | rsize_t orig_dmax; 86 | 87 | if (dest == NULL) { 88 | invoke_safe_str_constraint_handler("strremovews_s: dest is null", 89 | NULL, ESNULLP); 90 | return (ESNULLP); 91 | } 92 | 93 | if (dmax == 0 ) { 94 | invoke_safe_str_constraint_handler("strremovews_s: dmax is 0", 95 | NULL, ESZEROL); 96 | return (ESZEROL); 97 | } 98 | 99 | if (dmax > RSIZE_MAX_STR) { 100 | invoke_safe_str_constraint_handler("strremovews_s: dmax exceeds max", 101 | NULL, ESLEMAX); 102 | return (ESLEMAX); 103 | } 104 | 105 | /* 106 | * corner case, a dmax of one requires a null 107 | */ 108 | if (*dest == '\0' || dmax <= RSIZE_MIN_STR) { 109 | *dest = '\0'; 110 | return (EOK); 111 | } 112 | 113 | orig_dest = dest; 114 | orig_dmax = dmax; 115 | 116 | /* 117 | * scan the string to be sure it is properly terminated 118 | */ 119 | while (*dest) { 120 | if (dmax == 0) { 121 | while (orig_dmax) { *orig_dest++ = '\0'; orig_dmax--; } 122 | 123 | invoke_safe_str_constraint_handler( 124 | "strremovews_s: dest is unterminated", 125 | NULL, ESUNTERM); 126 | return (ESUNTERM); 127 | } 128 | dmax--; 129 | dest++; 130 | } 131 | 132 | /* 133 | * find first non-white space char 134 | */ 135 | orig_end = dest-1; 136 | dest = orig_dest; 137 | while ((*dest == ' ') || (*dest == '\t')) { 138 | dest++; 139 | } 140 | 141 | /* 142 | * shift the text over the leading spaces 143 | */ 144 | if (orig_dest != dest && *dest) { 145 | while (*dest) { 146 | *orig_dest++ = *dest; 147 | *dest++ = ' '; 148 | } 149 | *dest = '\0'; 150 | } 151 | 152 | /* 153 | * strip trailing whitespace 154 | */ 155 | dest = orig_end; 156 | while ((*dest == ' ') || (*dest == '\t')) { 157 | *dest = '\0'; 158 | dest--; 159 | } 160 | 161 | return (EOK); 162 | } 163 | EXPORT_SYMBOL(strremovews_s) 164 | -------------------------------------------------------------------------------- /safeclib/strtolowercase_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strtolowercase_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strtolowercase_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strlolowercase_s(char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * Scans the string converting uppercase characters to 47 | * lowercase, leaving all other characters unchanged. 48 | * The scanning stops at the first null or after dmax 49 | * characters. 50 | * 51 | * Extenstion to: 52 | * ISO/IEC TR 24731, Programming languages, environments 53 | * and system software interfaces, Extensions to the C Library, 54 | * Part I: Bounds-checking interfaces 55 | * 56 | * INPUT PARAMETERS 57 | * dest pointer to string 58 | * 59 | * dmax maximum length of string 60 | * 61 | * OUTPUT PARAMETERS 62 | * dest updated string 63 | * 64 | * RUNTIME CONSTRAINTS 65 | * dest shall not be a null pointer. 66 | * dmax shall not equal zero. 67 | * dmax shall not be greater than RSIZE_MAX_STR. 68 | * 69 | * RETURN VALUE 70 | * EOK successful operation 71 | * ESNULLP NULL pointer 72 | * ESZEROL zero length 73 | * ESLEMAX length exceeds max limit 74 | * 75 | * ALSO SEE 76 | * strtouppercase_s() 77 | * 78 | */ 79 | errno_t 80 | strtolowercase_s (char *dest, rsize_t dmax) 81 | { 82 | if (!dest) { 83 | invoke_safe_str_constraint_handler("strtolowercase_s: " 84 | "dest is null", 85 | NULL, ESNULLP); 86 | return (ESNULLP); 87 | } 88 | 89 | if (dmax == 0) { 90 | invoke_safe_str_constraint_handler("strtolowercase_s: " 91 | "dmax is 0", 92 | NULL, ESZEROL); 93 | return (ESZEROL); 94 | } 95 | 96 | if (dmax > RSIZE_MAX_STR) { 97 | invoke_safe_str_constraint_handler("strtolowercase_s: " 98 | "dmax exceeds max", 99 | NULL, ESLEMAX); 100 | return (ESLEMAX); 101 | } 102 | 103 | while (dmax && *dest) { 104 | 105 | if ( (*dest >= 'A') && (*dest <= 'Z')) { 106 | *dest = (char)(*dest + (char)32); 107 | } 108 | dest++; 109 | dmax--; 110 | } 111 | 112 | return (EOK); 113 | } 114 | EXPORT_SYMBOL(strtolowercase_s) 115 | -------------------------------------------------------------------------------- /safeclib/strtouppercase_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * safe_str_touppercase.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strtouppercase_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strlouppercase_s(char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * Scans the string converting lowercase characters to 47 | * uppercase, leaving all other characters unchanged. 48 | * The scanning stops at the first null or after dmax 49 | * characters. 50 | * 51 | * Extenstion to: 52 | * ISO/IEC TR 24731, Programming languages, environments 53 | * and system software interfaces, Extensions to the C Library, 54 | * Part I: Bounds-checking interfaces 55 | * 56 | * INPUT PARAMETERS 57 | * dest pointer to string 58 | * 59 | * dmax maximum length of string 60 | * 61 | * OUTPUT PARAMETERS 62 | * dest updated string 63 | * 64 | * RUNTIME CONSTRAINTS 65 | * dest shall not be a null pointer. 66 | * dmax shall not equal zero. 67 | * dmax shall not be greater than RSIZE_MAX_STR. 68 | * 69 | * RETURN VALUE 70 | * EOK successful operation 71 | * ESNULLP NULL pointer 72 | * ESZEROL zero length 73 | * ESLEMAX length exceeds max limit 74 | * 75 | * ALSO SEE 76 | * strtolowercase_s() 77 | * 78 | */ 79 | errno_t 80 | strtouppercase_s (char *dest, rsize_t dmax) 81 | { 82 | if (!dest) { 83 | invoke_safe_str_constraint_handler("strtouppercase_s: " 84 | "dest is null", 85 | NULL, ESNULLP); 86 | return (ESNULLP); 87 | } 88 | 89 | if (dmax == 0) { 90 | invoke_safe_str_constraint_handler("strtouppercase_s: " 91 | "dmax is 0", 92 | NULL, ESZEROL); 93 | return (ESZEROL); 94 | } 95 | 96 | if (dmax > RSIZE_MAX_STR) { 97 | invoke_safe_str_constraint_handler("strtouppercase_s: " 98 | "dmax exceeds max", 99 | NULL, ESLEMAX); 100 | return (ESLEMAX); 101 | } 102 | 103 | while (dmax && *dest) { 104 | 105 | if ((*dest >= 'a') && (*dest <= 'z')) { 106 | *dest = (char)(*dest - 32); 107 | } 108 | dest++; 109 | dmax--; 110 | } 111 | 112 | return (EOK); 113 | } 114 | EXPORT_SYMBOL(strtouppercase_s) 115 | -------------------------------------------------------------------------------- /safeclib/strzero_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strzero_s.c 3 | * 4 | * Copyright (c) 2008 Bo Berry 5 | * Copyright (c) 2008-2011 Cisco Systems 6 | * All rights reserved. 7 | * 8 | * Permission is hereby granted, free of charge, to any person 9 | * obtaining a copy of this software and associated documentation 10 | * files (the "Software"), to deal in the Software without 11 | * restriction, including without limitation the rights to use, 12 | * copy, modify, merge, publish, distribute, sublicense, and/or 13 | * sell copies of the Software, and to permit persons to whom the 14 | * Software is furnished to do so, subject to the following 15 | * conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be 18 | * included in all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | * OTHER DEALINGS IN THE SOFTWARE. 28 | *------------------------------------------------------------------ 29 | */ 30 | 31 | #include "safeclib_private.h" 32 | #include "safe_str_constraint.h" 33 | #include "safe_str_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * strzero_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_str_lib.h" 42 | * errno_t 43 | * strzero_s(char *dest, rsize_t dmax) 44 | * 45 | * DESCRIPTION 46 | * Nulls dmax characters of dest. This function can be used 47 | * to clear strings that contained sensitive data. 48 | * 49 | * EXTENSION TO 50 | * ISO/IEC TR 24731, Programming languages, environments 51 | * and system software interfaces, Extensions to the C Library, 52 | * Part I: Bounds-checking interfaces 53 | * 54 | * INPUT PARAMETERS 55 | * dest pointer to string that will be nulled. 56 | * 57 | * dmax restricted maximum length of dest 58 | * 59 | * OUTPUT PARAMETERS 60 | * dest updated string 61 | * 62 | * RETURN VALUE 63 | * EOK successful operation 64 | * ESNULLP NULL pointer 65 | * ESZEROL zero length 66 | * ESLEMAX length exceeds max limit 67 | * 68 | * ALSO SEE 69 | * strispassword_s() 70 | * 71 | */ 72 | errno_t 73 | strzero_s (char *dest, rsize_t dmax) 74 | { 75 | if (dest == NULL) { 76 | invoke_safe_str_constraint_handler("strzero_s: dest is null", 77 | NULL, ESNULLP); 78 | return (ESNULLP); 79 | } 80 | 81 | if (dmax == 0) { 82 | invoke_safe_str_constraint_handler("strzero_s: dmax is 0", 83 | NULL, ESZEROL); 84 | return (ESZEROL); 85 | } 86 | 87 | if (dmax > RSIZE_MAX_STR) { 88 | invoke_safe_str_constraint_handler("strzero_s: dmax exceeds max", 89 | NULL, ESLEMAX); 90 | return (ESLEMAX); 91 | } 92 | 93 | /* null string to eliminate data */ 94 | while (dmax) { 95 | *dest = '\0'; 96 | dmax--; 97 | dest++; 98 | } 99 | 100 | return (EOK); 101 | } 102 | EXPORT_SYMBOL(strzero_s) 103 | -------------------------------------------------------------------------------- /safeclib/wcsnlen_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * wcsnlen_s.c 3 | * 4 | * Copyright (c) 2014 by Intel Corp 5 | * All rights reserved. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, 11 | * copy, modify, merge, publish, distribute, sublicense, and/or 12 | * sell copies of the Software, and to permit persons to whom the 13 | * Software is furnished to do so, subject to the following 14 | * conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 | * OTHER DEALINGS IN THE SOFTWARE. 27 | *------------------------------------------------------------------ 28 | */ 29 | 30 | #include "safeclib_private.h" 31 | #include "safe_str_constraint.h" 32 | #include "safe_str_lib.h" 33 | 34 | 35 | /** 36 | * NAME 37 | * wcsnlen_s 38 | * 39 | * SYNOPSIS 40 | * #include "safe_str_lib.h" 41 | * rsize_t 42 | * wcsnlen_s(const wchar_t *dest, rsize_t dmax) 43 | * 44 | * DESCRIPTION 45 | * The wcsnlen_s function computes the length of the wide character string pointed 46 | * to by dest. 47 | * 48 | * SPECIFIED IN 49 | * ISO/IEC TR 24731-1, Programming languages, environments 50 | * and system software interfaces, Extensions to the C Library, 51 | * Part I: Bounds-checking interfaces 52 | * 53 | * INPUT PARAMETERS 54 | * dest pointer to wide character string 55 | * 56 | * dmax restricted maximum length. 57 | * 58 | * OUTPUT PARAMETERS 59 | * none 60 | * 61 | * RUNTIME CONSTRAINTS 62 | * dest shall not be a null pointer 63 | * dmax shall not be greater than RSIZE_MAX_STR 64 | * dmax shall not equal zero 65 | * 66 | * RETURN VALUE 67 | * The function returns the number of wide characters in the string 68 | * pointed to by dest, excluding the terminating null character. 69 | * If dest is NULL, then wcsnlen_s returns 0. 70 | * 71 | * Otherwise, the wcsnlen_s function returns the number of wide characters 72 | * that precede the terminating null character. If there is no null 73 | * character in the first dmax characters of dest then wcsnlen_s returns 74 | * dmax. At most the first dmax characters of dest are accessed 75 | * by wcsnlen_s. 76 | * 77 | * ALSO SEE 78 | * strnlen_s, strnterminate_s() 79 | * 80 | */ 81 | rsize_t 82 | wcsnlen_s (const wchar_t *dest, rsize_t dmax) 83 | { 84 | rsize_t count; 85 | 86 | if (dest == NULL) { 87 | return RCNEGATE(0); 88 | } 89 | 90 | if (dmax == 0) { 91 | invoke_safe_str_constraint_handler("wcsnlen_s: dmax is 0", 92 | NULL, ESZEROL); 93 | return RCNEGATE(0); 94 | } 95 | 96 | if (dmax*sizeof(wchar_t) > RSIZE_MAX_STR) { 97 | invoke_safe_str_constraint_handler("wcsnlen_s: dmax exceeds max", 98 | NULL, ESLEMAX); 99 | return RCNEGATE(0); 100 | } 101 | 102 | count = 0; 103 | while (dmax && *dest) { 104 | count++; 105 | dmax--; 106 | dest++; 107 | } 108 | 109 | return RCNEGATE(count); 110 | } 111 | EXPORT_SYMBOL(wcsnlen_s) 112 | -------------------------------------------------------------------------------- /safeclib/wmemset_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * wmemset_s 3 | * 4 | * Copyright (c) 2014 Intel Corp 5 | * All rights reserved. 6 | * 7 | * Permission is hereby granted, free of charge, to any person 8 | * obtaining a copy of this software and associated documentation 9 | * files (the "Software"), to deal in the Software without 10 | * restriction, including without limitation the rights to use, 11 | * copy, modify, merge, publish, distribute, sublicense, and/or 12 | * sell copies of the Software, and to permit persons to whom the 13 | * Software is furnished to do so, subject to the following 14 | * conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 | * OTHER DEALINGS IN THE SOFTWARE. 27 | *------------------------------------------------------------------ 28 | */ 29 | 30 | #include "safeclib_private.h" 31 | #include "safe_mem_constraint.h" 32 | #include "mem_primitives_lib.h" 33 | #include "safe_mem_lib.h" 34 | 35 | 36 | /** 37 | * NAME 38 | * wmemset_s 39 | * 40 | * SYNOPSIS 41 | * #include "safe_mem_lib.h" 42 | * errno_t 43 | * wmemset_s(wchar_t *dest, wchar_t value, rsize_t len) 44 | * 45 | * DESCRIPTION 46 | * Sets len number of wide characters starting at dest to the specified value. 47 | * 48 | * SPECIFIED IN 49 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 50 | * and system software interfaces, Extensions to the C Library, 51 | * Part I: Bounds-checking interfaces 52 | * 53 | * INPUT PARAMETERS 54 | * dest pointer to memory that will be set to the value 55 | * 56 | * value byte value 57 | * 58 | * len number of wide characters to be set 59 | * 60 | * OUTPUT PARAMETERS 61 | * dest is updated 62 | * 63 | * RUNTIME CONSTRAINTS 64 | * dest shall not be a null pointer. 65 | * len shall not be 0 nor greater than RSIZE_MAX_MEM/sizeof(wchar_t). 66 | * If there is a runtime constraint, the operation is not performed. 67 | * 68 | * RETURN VALUE 69 | * EOK successful operation 70 | * ESNULLP NULL pointer 71 | * ESZEROL zero length 72 | * ESLEMAX length exceeds max limit 73 | * 74 | * ALSO SEE 75 | * memset_s, memset16_s(), memset32_s() 76 | * 77 | */ 78 | errno_t 79 | wmemset_s (wchar_t *dest, wchar_t value, rsize_t len) 80 | { 81 | if (dest == NULL) { 82 | invoke_safe_mem_constraint_handler("wmemset_s: dest is null", 83 | NULL, ESNULLP); 84 | return (RCNEGATE(ESNULLP)); 85 | } 86 | 87 | if (len == 0) { 88 | invoke_safe_mem_constraint_handler("wmemset_s: len is 0", 89 | NULL, ESZEROL); 90 | return (RCNEGATE(ESZEROL)); 91 | } 92 | 93 | if (len*sizeof(wchar_t) > RSIZE_MAX_MEM) { 94 | invoke_safe_mem_constraint_handler("wmemset_s: len exceeds max", 95 | NULL, ESLEMAX); 96 | return (RCNEGATE(ESLEMAX)); 97 | } 98 | 99 | if (sizeof(wchar_t) == sizeof(uint32_t)) { 100 | mem_prim_set32((uint32_t *)dest, len, value); 101 | return (RCNEGATE(EOK)); 102 | } 103 | 104 | if (sizeof(wchar_t) == sizeof(uint16_t)) { 105 | mem_prim_set16((uint16_t *)dest, len, value); 106 | return (RCNEGATE(EOK)); 107 | } 108 | 109 | return (RCNEGATE(ESNOSPC)); 110 | } 111 | EXPORT_SYMBOL(wmemset_s) 112 | -------------------------------------------------------------------------------- /scroll_client/Makefile: -------------------------------------------------------------------------------- 1 | C_SRC := scroll_client.c 2 | CFLAGS := -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security -Werror -Wall -fstack-protector-all -O -I../include -I../safeclib 3 | 4 | ifeq ($(or $(COMSPEC),$(ComSpec)),) 5 | RM := rm -rf 6 | else 7 | RM := cs-rm -rf 8 | endif 9 | 10 | ELF ?= $(basename $(firstword $(C_SRC))) 11 | OBJ := $(patsubst %.c,%.o,$(C_SRC)) 12 | 13 | .PHONY: all 14 | all: $(ELF) $(OBJ) 15 | 16 | .PHONY: 17 | clean: 18 | $(RM) $(ELF) *.o 19 | 20 | $(OBJ): %.o: %.c 21 | $(CC) $(CFLAGS) -c $< -o $@ 22 | 23 | $(ELF): $(OBJ) 24 | $(CC) $(CFLAGS) $(OBJ) ../led_control/led_control.a -o $@ -lpthread 25 | 26 | -------------------------------------------------------------------------------- /scroll_client/scroll_client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014, Altera Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * - Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * - Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * - Neither the name of the Altera Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | 37 | int main(int argc, char** argv) 38 | { 39 | char ms_bet_toggle[100]; 40 | 41 | if (argc != 2) { 42 | printf("Usage: %s \n", argv[0]); 43 | printf(" If is set to 0,\n"); 44 | printf(" application will return the current toggling delay.\n"); 45 | printf(" If is set to negative value,\n"); 46 | printf(" application will stop the LED scrolling.\n"); 47 | return -1; 48 | } 49 | 50 | if (EOF == sscanf(argv[1], "%99s", ms_bet_toggle)) { 51 | printf("Failed reading \n."); 52 | return -1; 53 | } 54 | else { 55 | FILE *fp; 56 | fp = fopen("/home/root/.intelFPGA/frequency_fifo_scroll", "w"); 57 | if (fp == NULL) { 58 | printf("Failed opening fifo frequency_fifo_scroll\n"); 59 | return -1; 60 | } 61 | fputs(ms_bet_toggle, fp); 62 | fclose(fp); 63 | if(atoi(ms_bet_toggle) == 0) { 64 | fp = fopen("/home/root/.intelFPGA/get_scroll_fifo", "r"); 65 | if (fp == NULL) { 66 | printf("Failed opening fifo get_scroll_fifo\n"); 67 | return -1; 68 | } 69 | 70 | if (fgets(ms_bet_toggle, 10, fp) == NULL) 71 | { 72 | printf("Failed opening read\n"); 73 | fclose(fp); 74 | return -1; 75 | } 76 | fclose(fp); 77 | printf("%d", atoi(ms_bet_toggle)); 78 | return 0; 79 | } 80 | } 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /scroll_server/Makefile: -------------------------------------------------------------------------------- 1 | C_SRC := scroll_server.c snprintf_support.c safe_str_constraint.c ignore_handler_s.c 2 | CFLAGS := -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security -Werror -Wall -fstack-protector-all -O -I../led_control -I../include -I../safeclib 3 | 4 | ifeq ($(or $(COMSPEC),$(ComSpec)),) 5 | RM := rm -rf 6 | else 7 | RM := cs-rm -rf 8 | endif 9 | 10 | ELF ?= $(basename $(firstword $(C_SRC))) 11 | OBJ := $(patsubst %.c,%.o,$(C_SRC)) 12 | 13 | .PHONY: all 14 | all: $(ELF) $(OBJ) 15 | 16 | .PHONY: 17 | clean: 18 | $(RM) $(ELF) *.o 19 | 20 | $(OBJ): %.o: %.c 21 | $(CC) $(CFLAGS) -c $< -o $@ 22 | 23 | $(ELF): $(OBJ) 24 | $(CC) $(CFLAGS) $(OBJ) ../led_control/led_control.a -o $@ -lpthread 25 | 26 | -------------------------------------------------------------------------------- /scroll_server/ignore_handler_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * ignore_handler_s.c 3 | * 4 | * 2012, Jonathan Toppins 5 | * 6 | * Copyright (c) 2012 Cisco Systems 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | 34 | /** 35 | * NAME 36 | * ignore_handler_s 37 | * 38 | * SYNOPSIS 39 | * #include "safe_lib.h" 40 | * void ignore_handler_s(const char *msg, void *ptr, errno_t error) 41 | * 42 | * DESCRIPTION 43 | * This function simply returns to the caller. 44 | * 45 | * SPECIFIED IN 46 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 47 | * and system software interfaces, Extensions to the C Library, 48 | * Part I: Bounds-checking interfaces 49 | * 50 | * INPUT PARAMETERS 51 | * msg Pointer to the message describing the error 52 | * 53 | * ptr Pointer to aassociated data. Can be NULL. 54 | * 55 | * error The error code encountered. 56 | * 57 | * RETURN VALUE 58 | * Returns no value. 59 | * 60 | * ALSO SEE 61 | * abort_handler_s() 62 | * 63 | */ 64 | 65 | void ignore_handler_s(const char *msg, void *ptr, errno_t error) 66 | { 67 | 68 | sldebug_printf("IGNORE CONSTRAINT HANDLER: (%u) %s\n", error, 69 | (msg) ? msg : "Null message"); 70 | return; 71 | } 72 | EXPORT_SYMBOL(ignore_handler_s) 73 | -------------------------------------------------------------------------------- /syschk/Makefile: -------------------------------------------------------------------------------- 1 | C_SRC := syschk.c strnlen_s.c safe_str_constraint.c ignore_handler_s.c strcat_s.c strcpy_s.c 2 | CFLAGS := -g -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security -Werror -Wall -fstack-protector-all -O -I../include -I../safeclib 3 | 4 | RM := rm -rf 5 | 6 | ELF ?= $(basename $(firstword $(C_SRC))) 7 | OBJ := $(patsubst %.c,%.o,$(C_SRC)) 8 | 9 | .PHONY: all 10 | all: $(ELF) 11 | 12 | .PHONY: 13 | clean: 14 | $(RM) $(ELF) $(OBJ) 15 | 16 | $(OBJ): %.o: %.c 17 | $(CC) $(CFLAGS) -c $< -o $@ 18 | 19 | $(ELF): $(OBJ) 20 | $(CC) $(CFLAGS) $(OBJ) -o $@ -l curses 21 | 22 | -------------------------------------------------------------------------------- /syschk/ignore_handler_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * ignore_handler_s.c 3 | * 4 | * 2012, Jonathan Toppins 5 | * 6 | * Copyright (c) 2012 Cisco Systems 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | 34 | /** 35 | * NAME 36 | * ignore_handler_s 37 | * 38 | * SYNOPSIS 39 | * #include "safe_lib.h" 40 | * void ignore_handler_s(const char *msg, void *ptr, errno_t error) 41 | * 42 | * DESCRIPTION 43 | * This function simply returns to the caller. 44 | * 45 | * SPECIFIED IN 46 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments 47 | * and system software interfaces, Extensions to the C Library, 48 | * Part I: Bounds-checking interfaces 49 | * 50 | * INPUT PARAMETERS 51 | * msg Pointer to the message describing the error 52 | * 53 | * ptr Pointer to aassociated data. Can be NULL. 54 | * 55 | * error The error code encountered. 56 | * 57 | * RETURN VALUE 58 | * Returns no value. 59 | * 60 | * ALSO SEE 61 | * abort_handler_s() 62 | * 63 | */ 64 | 65 | void ignore_handler_s(const char *msg, void *ptr, errno_t error) 66 | { 67 | 68 | sldebug_printf("IGNORE CONSTRAINT HANDLER: (%u) %s\n", error, 69 | (msg) ? msg : "Null message"); 70 | return; 71 | } 72 | EXPORT_SYMBOL(ignore_handler_s); 73 | -------------------------------------------------------------------------------- /syschk/strnlen_s.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------ 2 | * strnlen_s.c 3 | * 4 | * October 2008, Bo Berry 5 | * 6 | * Copyright (c) 2008-2011 by Cisco Systems, Inc 7 | * All rights reserved. 8 | * 9 | * Permission is hereby granted, free of charge, to any person 10 | * obtaining a copy of this software and associated documentation 11 | * files (the "Software"), to deal in the Software without 12 | * restriction, including without limitation the rights to use, 13 | * copy, modify, merge, publish, distribute, sublicense, and/or 14 | * sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following 16 | * conditions: 17 | * 18 | * The above copyright notice and this permission notice shall be 19 | * included in all copies or substantial portions of the Software. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 | * OTHER DEALINGS IN THE SOFTWARE. 29 | *------------------------------------------------------------------ 30 | */ 31 | 32 | #include "safeclib_private.h" 33 | #include "safe_str_constraint.h" 34 | #include "safe_str_lib.h" 35 | 36 | 37 | /** 38 | * NAME 39 | * strnlen_s 40 | * 41 | * SYNOPSIS 42 | * #include "safe_str_lib.h" 43 | * rsize_t 44 | * strnlen_s(const char *dest, rsize_t dmax) 45 | * 46 | * DESCRIPTION 47 | * The strnlen_s function computes the length of the string pointed 48 | * to by dest. 49 | * 50 | * SPECIFIED IN 51 | * ISO/IEC TR 24731-1, Programming languages, environments 52 | * and system software interfaces, Extensions to the C Library, 53 | * Part I: Bounds-checking interfaces 54 | * 55 | * INPUT PARAMETERS 56 | * dest pointer to string 57 | * 58 | * dmax restricted maximum length. 59 | * 60 | * OUTPUT PARAMETERS 61 | * none 62 | * 63 | * RUNTIME CONSTRAINTS 64 | * dest shall not be a null pointer 65 | * dmax shall not be greater than RSIZE_MAX_STR 66 | * dmax shall not equal zero 67 | * 68 | * RETURN VALUE 69 | * The function returns the string length, excluding the terminating 70 | * null character. If dest is NULL, then strnlen_s returns 0. 71 | * 72 | * Otherwise, the strnlen_s function returns the number of characters 73 | * that precede the terminating null character. If there is no null 74 | * character in the first dmax characters of dest then strnlen_s returns 75 | * dmax. At most the first dmax characters of dest are accessed 76 | * by strnlen_s. 77 | * 78 | * ALSO SEE 79 | * strnterminate_s() 80 | * 81 | */ 82 | rsize_t 83 | strnlen_s (const char *dest, rsize_t dmax) 84 | { 85 | rsize_t count; 86 | 87 | if (dest == NULL) { 88 | return RCNEGATE(0); 89 | } 90 | 91 | if (dmax == 0) { 92 | invoke_safe_str_constraint_handler("strnlen_s: dmax is 0", 93 | NULL, ESZEROL); 94 | return RCNEGATE(0); 95 | } 96 | 97 | if (dmax > RSIZE_MAX_STR) { 98 | invoke_safe_str_constraint_handler("strnlen_s: dmax exceeds max", 99 | NULL, ESLEMAX); 100 | return RCNEGATE(0); 101 | } 102 | 103 | count = 0; 104 | while (*dest && dmax) { 105 | count++; 106 | dmax--; 107 | dest++; 108 | } 109 | 110 | return RCNEGATE(count); 111 | } 112 | EXPORT_SYMBOL(strnlen_s); 113 | -------------------------------------------------------------------------------- /toggle/Makefile: -------------------------------------------------------------------------------- 1 | C_SRC := toggle.c 2 | CFLAGS := -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security -Werror -Wall -fstack-protector-all -O -I../led_control 3 | 4 | ifeq ($(or $(COMSPEC),$(ComSpec)),) 5 | RM := rm -rf 6 | else 7 | RM := cs-rm -rf 8 | endif 9 | 10 | ELF ?= $(basename $(firstword $(C_SRC))) 11 | OBJ := $(patsubst %.c,%.o,$(C_SRC)) 12 | 13 | .PHONY: all 14 | all: $(ELF) $(OBJ) 15 | 16 | .PHONY: 17 | clean: 18 | $(RM) $(ELF) *.o 19 | 20 | $(OBJ): $(C_SRC) 21 | $(CC) $(CFLAGS) -c $< -o $@ 22 | 23 | $(ELF): $(OBJ) 24 | $(CC) $(CFLAGS) $(OBJ) ../led_control/led_control.a -o $@ -lpthread 25 | 26 | -------------------------------------------------------------------------------- /toggle/toggle.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2014, Altera Corporation 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * - Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * - Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * - Neither the name of the Altera Corporation nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "led_control.h" 36 | 37 | #define STRSIZE 2 38 | 39 | int main(int argc, char** argv) 40 | { 41 | int led, on_off, i; 42 | FILE *fp; 43 | char get_fifo_scroll[STRSIZE], seconds_bet_toggle[10]; 44 | 45 | for (i = 1; i < argc; i++) { 46 | if (strcmp("--help", argv[i]) == 0) { 47 | printf("Usage %s: <0|1> ; 0:off, 1:on\n" 48 | , argv[0]); 49 | return -1; 50 | } 51 | } 52 | 53 | if (argc != 3) { 54 | printf("Usage %s: <0|1> ; 0:off, 1:on\n" 55 | , argv[0]); 56 | return -1; 57 | } 58 | else { 59 | led = atoi(argv[1]); 60 | on_off = atoi(argv[2]); 61 | if (led < 0 || led > 3) { 62 | printf("Invalid LED number. Valid LED range is 0-3\n"); 63 | return -1; 64 | } 65 | if (on_off != 0 && on_off != 1) { 66 | printf("Invalid toggling. Valid input is 0 or 1\n"); 67 | return -1; 68 | } 69 | } 70 | 71 | /* Checking if scrolling is still happening 72 | * if it is, we will inform user, and bail 73 | */ 74 | snprintf(get_fifo_scroll, STRSIZE, "%d", 0); 75 | fp = fopen("/home/root/.intelFPGA/frequency_fifo_scroll", "w"); 76 | if (fp == NULL) { 77 | printf("Failed opening fifo frequency_fifo_scroll\n"); 78 | return -1; 79 | } 80 | fputs(get_fifo_scroll, fp); 81 | fclose(fp); 82 | fp = fopen("/home/root/.intelFPGA/get_scroll_fifo", "r"); 83 | if (fp == NULL) { 84 | printf("Failed opening fifo get_scroll_fifo\n"); 85 | return -1; 86 | } 87 | 88 | if (fgets(seconds_bet_toggle, 10, fp) == NULL) 89 | { 90 | printf("Failed opening toggle\n"); 91 | return -1; 92 | } 93 | 94 | fclose(fp); 95 | if (atoi(seconds_bet_toggle) > 0) { 96 | printf("LED is scrolling.\n"); 97 | printf("Disable scroll_client before changing blink delay\n"); 98 | return -1; 99 | } 100 | 101 | /* 102 | * Toggle the GPIO pin 103 | */ 104 | clear_led(led); 105 | char trigger[] = "none"; 106 | 107 | /* Setting the trigger to not blinking */ 108 | setLEDtrigger(led, trigger, sizeof(trigger)); 109 | setLEDBrightness(led, on_off); 110 | 111 | return 0; 112 | } 113 | --------------------------------------------------------------------------------