From 6aab3c7b99f41aa3a3270a67b8fafb20ec2eec1e Mon Sep 17 00:00:00 2001 From: huangyicong Date: Wed, 19 Jan 2022 16:25:13 +0800 Subject: [PATCH] add symbolic link --- base/include/securec.h | 539 +------ base/include/securectype.h | 280 +--- base/src/securec/fscanf_s.c | 56 +- base/src/securec/fwscanf_s.c | 56 +- base/src/securec/gets_s.c | 75 +- base/src/securec/input.inl | 1911 +----------------------- base/src/securec/memcpy_s.c | 328 +--- base/src/securec/memmove_s.c | 122 +- base/src/securec/memset_s.c | 323 +--- base/src/securec/output.inl | 1222 +-------------- base/src/securec/scanf_s.c | 56 +- base/src/securec/secinput.h | 105 +- base/src/securec/securecutil.c | 355 +---- base/src/securec/securecutil.h | 343 +---- base/src/securec/secureinput_a.c | 30 +- base/src/securec/secureinput_w.c | 55 +- base/src/securec/secureprintoutput.h | 88 +- base/src/securec/secureprintoutput_a.c | 28 +- base/src/securec/secureprintoutput_w.c | 100 +- base/src/securec/snprintf_s.c | 80 +- base/src/securec/sprintf_s.c | 60 +- base/src/securec/sscanf_s.c | 60 +- base/src/securec/strcat_s.c | 218 +-- base/src/securec/strcpy_s.c | 214 +-- base/src/securec/strncat_s.c | 193 +-- base/src/securec/strncpy_s.c | 160 +- base/src/securec/strtok_s.c | 121 +- base/src/securec/swprintf_s.c | 53 +- base/src/securec/swscanf_s.c | 59 +- base/src/securec/vfscanf_s.c | 72 +- base/src/securec/vfwscanf_s.c | 72 +- base/src/securec/vscanf_s.c | 71 +- base/src/securec/vsnprintf_s.c | 119 +- base/src/securec/vsprintf_s.c | 72 +- base/src/securec/vsscanf_s.c | 86 +- base/src/securec/vswprintf_s.c | 87 +- base/src/securec/vswscanf_s.c | 83 +- base/src/securec/vwscanf_s.c | 70 +- base/src/securec/wcscat_s.c | 140 +- base/src/securec/wcscpy_s.c | 104 +- base/src/securec/wcsncat_s.c | 155 +- base/src/securec/wcsncpy_s.c | 125 +- base/src/securec/wcstok_s.c | 120 +- base/src/securec/wmemcpy_s.c | 74 +- base/src/securec/wmemmove_s.c | 73 +- base/src/securec/wscanf_s.c | 56 +- 46 files changed, 46 insertions(+), 8823 deletions(-) mode change 100755 => 120000 base/include/securec.h mode change 100755 => 120000 base/include/securectype.h mode change 100755 => 120000 base/src/securec/fscanf_s.c mode change 100755 => 120000 base/src/securec/fwscanf_s.c mode change 100755 => 120000 base/src/securec/gets_s.c mode change 100755 => 120000 base/src/securec/input.inl mode change 100755 => 120000 base/src/securec/memcpy_s.c mode change 100755 => 120000 base/src/securec/memmove_s.c mode change 100755 => 120000 base/src/securec/memset_s.c mode change 100755 => 120000 base/src/securec/output.inl mode change 100755 => 120000 base/src/securec/scanf_s.c mode change 100755 => 120000 base/src/securec/secinput.h mode change 100755 => 120000 base/src/securec/securecutil.c mode change 100755 => 120000 base/src/securec/securecutil.h mode change 100755 => 120000 base/src/securec/secureinput_a.c mode change 100755 => 120000 base/src/securec/secureinput_w.c mode change 100755 => 120000 base/src/securec/secureprintoutput.h mode change 100755 => 120000 base/src/securec/secureprintoutput_a.c mode change 100755 => 120000 base/src/securec/secureprintoutput_w.c mode change 100755 => 120000 base/src/securec/snprintf_s.c mode change 100755 => 120000 base/src/securec/sprintf_s.c mode change 100755 => 120000 base/src/securec/sscanf_s.c mode change 100755 => 120000 base/src/securec/strcat_s.c mode change 100755 => 120000 base/src/securec/strcpy_s.c mode change 100755 => 120000 base/src/securec/strncat_s.c mode change 100755 => 120000 base/src/securec/strncpy_s.c mode change 100755 => 120000 base/src/securec/strtok_s.c mode change 100755 => 120000 base/src/securec/swprintf_s.c mode change 100755 => 120000 base/src/securec/swscanf_s.c mode change 100755 => 120000 base/src/securec/vfscanf_s.c mode change 100755 => 120000 base/src/securec/vfwscanf_s.c mode change 100755 => 120000 base/src/securec/vscanf_s.c mode change 100755 => 120000 base/src/securec/vsnprintf_s.c mode change 100755 => 120000 base/src/securec/vsprintf_s.c mode change 100755 => 120000 base/src/securec/vsscanf_s.c mode change 100755 => 120000 base/src/securec/vswprintf_s.c mode change 100755 => 120000 base/src/securec/vswscanf_s.c mode change 100755 => 120000 base/src/securec/vwscanf_s.c mode change 100755 => 120000 base/src/securec/wcscat_s.c mode change 100755 => 120000 base/src/securec/wcscpy_s.c mode change 100755 => 120000 base/src/securec/wcsncat_s.c mode change 100755 => 120000 base/src/securec/wcsncpy_s.c mode change 100755 => 120000 base/src/securec/wcstok_s.c mode change 100755 => 120000 base/src/securec/wmemcpy_s.c mode change 100755 => 120000 base/src/securec/wmemmove_s.c mode change 100755 => 120000 base/src/securec/wscanf_s.c diff --git a/base/include/securec.h b/base/include/securec.h deleted file mode 100755 index 553eaf4..0000000 --- a/base/include/securec.h +++ /dev/null @@ -1,538 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 -#define __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 - -/* Compile in kernel under macro control */ -#ifndef SECUREC_IN_KERNEL -#ifdef __KERNEL__ -#define SECUREC_IN_KERNEL 1 -#else -#define SECUREC_IN_KERNEL 0 -#endif -#endif - -/* If you need high performance, enable the SECUREC_WITH_PERFORMANCE_ADDONS macro, default is enable . - * The macro is automatically closed on the windows platform in securectyp.h. - */ -#ifndef SECUREC_WITH_PERFORMANCE_ADDONS -#if SECUREC_IN_KERNEL -#define SECUREC_WITH_PERFORMANCE_ADDONS 0 -#else -#define SECUREC_WITH_PERFORMANCE_ADDONS 1 -#endif -#endif - -#include "securectype.h" -#include - -#ifndef SECUREC_HAVE_ERRNO_H -#if SECUREC_IN_KERNEL -#define SECUREC_HAVE_ERRNO_H 0 -#else -#define SECUREC_HAVE_ERRNO_H 1 -#endif -#endif - -/* EINVAL ERANGE may defined in errno.h */ -#if SECUREC_HAVE_ERRNO_H -#include -#endif - -/* If stack size on some embedded platform is limited, you can define the following macro - * which will put some variables on heap instead of stack. - * SECUREC_STACK_SIZE_LESS_THAN_1K - */ - -/* define error code */ -#if !defined(__STDC_WANT_LIB_EXT1__) || (defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ == 0)) -#ifndef SECUREC_DEFINED_ERRNO_TYPE -#define SECUREC_DEFINED_ERRNO_TYPE -/* just check whether macrodefinition exists. */ -#ifndef errno_t -typedef int errno_t; -#endif -#endif -#endif - -/* success */ -#ifndef EOK -#define EOK (0) -#endif - -#ifndef EINVAL -/* The src buffer is not correct and destination buffer can't not be reset */ -#define EINVAL (22) -#endif - -#ifndef EINVAL_AND_RESET -/* Once the error is detected, the dest buffer must be rest! */ -#define EINVAL_AND_RESET (22 | 128) -#endif - -#ifndef ERANGE -/* The destination buffer is not long enough and destination buffer can not be reset */ -#define ERANGE (34) -#endif - -#ifndef ERANGE_AND_RESET -/* Once the error is detected, the dest buffer must be rest! */ -#define ERANGE_AND_RESET (34 | 128) -#endif - -#ifndef EOVERLAP_AND_RESET -/* Once the buffer overlap is detected, the dest buffer must be rest! */ -#define EOVERLAP_AND_RESET (54 | 128) -#endif - -/* if you need export the function of this library in Win32 dll, use __declspec(dllexport) */ -#ifdef SECUREC_IS_DLL_LIBRARY -#ifdef SECUREC_DLL_EXPORT -#define SECUREC_API __declspec(dllexport) -#else -#define SECUREC_API __declspec(dllimport) -#endif -#else -/* Standardized function declaration . If a security function is declared in the your code, - * it may cause a compilation alarm,Please delete the security function you declared - */ -#define SECUREC_API extern -#endif - -#ifndef SECUREC_SNPRINTF_TRUNCATED -#define SECUREC_SNPRINTF_TRUNCATED 1 -#endif - -#ifdef __cplusplus -extern "C" { -#endif - - /** - * @Description:The memset_s function copies the value of c (converted to an unsigned char) into each of the first count characters of the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param c - the value to be copied - * @param count -copies first count characters of dest - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memset_s(void *dest, size_t destMax, int c, size_t count); - - /* The memset_s security function is not provided in Windows system, but other security functions are provided. In this case, can only use the memset_s function */ -#ifndef SECUREC_ONLY_DECLARE_MEMSET - - /** - * @Description:The wmemcpy_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ -#if SECUREC_IN_KERNEL == 0 - SECUREC_API errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); -#endif - /** - * @Description:The memmove_s function copies n characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memmove_s(void *dest, size_t destMax, const void *src, size_t count); -#if SECUREC_IN_KERNEL == 0 - /** - * @Description:The wmemmove_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); - - /** - * @Description:The wcscpy_s function copies the wide string pointed to by strSrc (including theterminating null wide character) into the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); - - /** - * @Description:The wcsncpy_s function copies not more than n successive wide characters (not including the terminating null wide character) - * from the array pointed to by strSrc to the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); - - /** - * @Description:The wcscat_s function appends a copy of the wide string pointed to by strSrc (including the terminating null wide character) - * to the end of the wide string pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); - - /** - * @Description:The wcsncat_s function appends not more than n successive wide characters (not including the terminating null wide character) - * from the array pointed to by strSrc to the end of the wide string pointed to by strDest. - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); - - /** - * @Description: The strtok_s function parses a string into a sequence of tokens,On the first call to strtok_s the string to be parsed should be specified in strToken. - * In each subsequent call that should parse the same string, strToken should be NULL - * @param strToken - the string to be delimited - * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string - * @param context -is a pointer to a char * variable that is used internally by strtok_s function - * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. - */ - SECUREC_API char *strtok_s(char *strToken, const char *strDelimit, char **context); - - /** - * @Description: The wcstok_s function is the wide-character equivalent of the strtok_s function - * @param strToken - the string to be delimited - * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string - * @param context -is a pointer to a char * variable that is used internally by strtok_s function - * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. - */ - SECUREC_API wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context); - - /** - * @Description: The sprintf_s function is equivalent to the sprintf function except for the parameter destMax and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @return:success the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. - */ - SECUREC_API int sprintf_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); - - /** - * @Description: The swprintf_s function is the wide-character equivalent of the sprintf_s function - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null ) - * @param format - format string - * @return:success the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. - */ - SECUREC_API int swprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, ...); - - /** - * @Description: The vsprintf_s function is equivalent to the vsprintf function except for the parameter destMax and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null wide characte) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. - */ - SECUREC_API int vsprintf_s(char *strDest, size_t destMax, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(3, 0); - - /** - * @Description: The vswprintf_s function is the wide-character equivalent of the vsprintf_s function - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null ) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. - */ - SECUREC_API int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list arglist); - - /** - * @Description: The vsnprintf_s function is equivalent to the vsnprintf function except for the parameter destMax/count and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs - */ - SECUREC_API int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(4, 0); - - /** - * @Description: The snprintf_s function is equivalent to the snprintf function except for the parameter destMax/count and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) - * @param format - format string - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs - */ - SECUREC_API int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, - ...) SECUREC_ATTRIBUTE(4, 5); - -#if SECUREC_SNPRINTF_TRUNCATED - /** - * @Description: The vsnprintf_truncated_s function is equivalent to the vsnprintf_s function except no count parameter and return value - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs - */ - SECUREC_API int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(3, 0); - - /** - * @Description: The snprintf_truncated_s function is equivalent to the snprintf_2 function except no count parameter and return value - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs - */ - SECUREC_API int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); -#endif - /** - * @Description: The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int scanf_s(const char *format, ...); - - /** - * @Description: The wscanf_s function is the wide-character equivalent of the scanf_s function - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int wscanf_s(const wchar_t *format, ...); - - /** - * @Description: The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by arglist - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vscanf_s(const char *format, va_list arglist); - - /** - * @Description: The vwscanf_s function is the wide-character equivalent of the vscanf_s function - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vwscanf_s(const wchar_t *format, va_list arglist); - - /** - * @Description: The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a*) - * @param stream - stdio file stream - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int fscanf_s(FILE *stream, const char *format, ...); - - /** - * @Description: The fwscanf_s function is the wide-character equivalent of the fscanf_s function - * @param stream - stdio file stream - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int fwscanf_s(FILE *stream, const wchar_t *format, ...); - - /** - * @Description: The vfscanf_s function is equivalent to fscanf_s, with the variable argument list replaced by arglist - * @param stream - stdio file stream - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vfscanf_s(FILE *stream, const char *format, va_list arglist); - - /** - * @Description: The vfwscanf_s function is the wide-character equivalent of the vfscanf_s function - * @param stream - stdio file stream - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vfwscanf_s(FILE *stream, const wchar_t *format, va_list arglist); - - /** - * @Description: The sscanf_s function is equivalent to fscanf_s, except that input is obtained from a string (specified by the argument buffer) rather than from a stream - * @param buffer - read character from buffer - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int sscanf_s(const char *buffer, const char *format, ...); - - /** - * @Description: The swscanf_s function is the wide-character equivalent of the sscanf_s function - * @param buffer - read character from buffer - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int swscanf_s(const wchar_t *buffer, const wchar_t *format, ...); - - /** - * @Description: The vsscanf_s function is equivalent to sscanf_s, with the variable argument list replaced by arglist - * @param buffer - read character from buffer - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vsscanf_s(const char *buffer, const char *format, va_list arglist); - - /** - * @Description: The vswscanf_s function is the wide-character equivalent of the vsscanf_s function - * @param buffer - read character from buffer - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list arglist); - - /** - * @Description:The gets_s function reads at most one less than the number of characters specified by destMax from the stream pointed to by stdin, into the array pointed to by buffer - * @param buffer - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @return buffer if there was no runtime-constraint violation,If an error occurred return NULL. - */ - SECUREC_API char *gets_s(char *buffer, size_t destMax); -#endif - - /** - * @Description:The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count); - - /** - * @Description:The strcpy_s function copies the string pointed to by strSrc (including the terminating null character) into the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strcpy_s(char *strDest, size_t destMax, const char *strSrc); - - /** - * @Description:The strncpy_s function copies not more than n successive characters (not including the terminating null character) - * from the array pointed to by strSrc to the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count); - - /** - * @Description:The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character) - * to the end of the string pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null wide character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc); - - /** - * @Description:The strncat_s function appends not more than n successive characters (not including the terminating null character) - * from the array pointed to by strSrc to the end of the string pointed to by strDest. - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count); -#if SECUREC_IN_KERNEL == 0 - /* those functions are used by macro ,must declare hare , also for without function declaration warning */ - extern errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count); - extern errno_t strcpy_error(char *strDest, size_t destMax, const char *strSrc); -#endif -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - /* those functions are used by macro */ - extern errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count); - extern errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count); - extern errno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count); - extern errno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count); - -/* strcpy_sp is a macro, NOT a function in performance optimization mode. */ -#define strcpy_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRCPY_SM((dest), (destMax), (src)) : \ - strcpy_s((dest), (destMax), (src))) - -/* strncpy_sp is a macro, NOT a function in performance optimization mode. */ -#define strncpy_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ - __builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRNCPY_SM((dest), (destMax), (src), (count)) : \ - strncpy_s((dest), (destMax), (src), (count))) - -/* strcat_sp is a macro, NOT a function in performance optimization mode. */ -#define strcat_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRCAT_SM((dest), (destMax), (src)) : \ - strcat_s((dest), (destMax), (src))) - - /* strncat_sp is a macro, NOT a function in performance optimization mode. */ -#define strncat_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ - __builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRNCAT_SM((dest), (destMax), (src), (count)) : \ - strncat_s((dest), (destMax), (src), (count))) - -/* memcpy_sp is a macro, NOT a function in performance optimization mode. */ -#define memcpy_sp(dest, destMax, src, count) (__builtin_constant_p((count)) ? \ - (SECUREC_MEMCPY_SM((dest), (destMax), (src), (count))) : \ - (__builtin_constant_p((destMax)) ? \ - (((size_t)(destMax) > 0 && \ - (((unsigned long long)(destMax) & \ - (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ - memcpy_sOptTc((dest), (destMax), (src), (count)) : ERANGE ) : \ - memcpy_sOptAsm((dest), (destMax), (src), (count)))) - -/* memset_sp is a macro, NOT a function in performance optimization mode. */ -#define memset_sp(dest, destMax, c, count) (__builtin_constant_p((count)) ? \ - (SECUREC_MEMSET_SM((dest), (destMax), (c), (count))) : \ - (__builtin_constant_p((destMax)) ? \ - (((size_t)(destMax) > 0 && \ - (((unsigned long long)(destMax) & \ - (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ - memset_sOptTc((dest), (destMax), (c), (count)) : ERANGE ) : \ - memset_sOptAsm((dest), (destMax), (c), (count)))) -#else -#define strcpy_sp strcpy_s -#define strncpy_sp strncpy_s -#define strcat_sp strcat_s -#define strncat_sp strncat_s -#define memcpy_sp memcpy_s -#define memset_sp memset_s -#endif - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */ diff --git a/base/include/securec.h b/base/include/securec.h new file mode 120000 index 0000000..0a28bfe --- /dev/null +++ b/base/include/securec.h @@ -0,0 +1 @@ +../../../../third_party/bounds_checking_function/include/securec.h \ No newline at end of file diff --git a/base/include/securectype.h b/base/include/securectype.h deleted file mode 100755 index 564c9f4..0000000 --- a/base/include/securectype.h +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions]: Performance-sensitive - * [reason]: Strict parameter verification has been done before use - */ - -#ifndef __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 -#define __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 - -#ifndef SECUREC_ONLY_DECLARE_MEMSET -/* Shielding VC symbol redefinition warning */ -#if defined(_MSC_VER) && (_MSC_VER >= 1400) -#ifdef __STDC_WANT_SECURE_LIB__ -#undef __STDC_WANT_SECURE_LIB__ -#endif -#define __STDC_WANT_SECURE_LIB__ 0 -#ifdef _CRTIMP_ALTERNATIVE -#undef _CRTIMP_ALTERNATIVE -#endif -#define _CRTIMP_ALTERNATIVE //comment microsoft *_s function -#endif -#endif - -#if SECUREC_IN_KERNEL -#include -#include -#else -#include -#include -#include -#endif - -/* if enable SECUREC_COMPATIBLE_WIN_FORMAT, the output format will be compatible to Windows. */ -#if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) -#define SECUREC_COMPATIBLE_WIN_FORMAT -#endif - -#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) -/* in windows platform, can't use optimized function for there is no __builtin_constant_p like function */ -/* If need optimized macro, can define this: define __builtin_constant_p(x) 0 */ -#ifdef SECUREC_WITH_PERFORMANCE_ADDONS -#undef SECUREC_WITH_PERFORMANCE_ADDONS -#define SECUREC_WITH_PERFORMANCE_ADDONS 0 -#endif -#endif - -#if defined(__VXWORKS__) || defined(__vxworks) || defined(__VXWORKS) || defined(_VXWORKS_PLATFORM_) || \ - defined(SECUREC_VXWORKS_VERSION_5_4) -#if !defined(SECUREC_VXWORKS_PLATFORM) -#define SECUREC_VXWORKS_PLATFORM -#endif -#endif - -/* if enable SECUREC_COMPATIBLE_LINUX_FORMAT, the output format will be compatible to Linux. */ -#if !(defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) -#define SECUREC_COMPATIBLE_LINUX_FORMAT -#endif -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT -#include -#endif - -/* add the -DSECUREC_SUPPORT_FORMAT_WARNING compiler option to supoort -Wformat. - * default does not check the format is that the same data type in the actual code - * in the product is different in the original data type definition of VxWorks and Linux. - */ -#ifndef SECUREC_SUPPORT_FORMAT_WARNING -#define SECUREC_SUPPORT_FORMAT_WARNING 0 -#endif - -/* SECUREC_PCLINT for tool do not recognize __attribute__ just for pclint */ -#if SECUREC_SUPPORT_FORMAT_WARNING && !defined(SECUREC_PCLINT) -#define SECUREC_ATTRIBUTE(x,y) __attribute__((format(printf, (x), (y)))) -#else -#define SECUREC_ATTRIBUTE(x,y) -#endif - -/* SECUREC_PCLINT for tool do not recognize __builtin_expect ,just for pclint */ -#if defined(__GNUC__) && \ - ((__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3 /* above 3.4 */ ))) && \ - !defined(SECUREC_PCLINT) -/* This is a built-in function that can be used without a declaration, if you encounter an undeclared compilation alarm, - * you can add -DSECUREC_NEED_BUILTIN_EXPECT_DECLARE to compiler options - */ -#if defined(SECUREC_NEED_BUILTIN_EXPECT_DECLARE) -long __builtin_expect(long exp, long c); -#endif -#define SECUREC_LIKELY(x) __builtin_expect(!!(x), 1) -#define SECUREC_UNLIKELY(x) __builtin_expect(!!(x), 0) -#else -#define SECUREC_LIKELY(x) (x) -#define SECUREC_UNLIKELY(x) (x) -#endif - -/* define the max length of the string */ -#define SECUREC_STRING_MAX_LEN (0x7fffffffUL) -#define SECUREC_WCHAR_STRING_MAX_LEN (SECUREC_STRING_MAX_LEN / sizeof(wchar_t)) - -/* add SECUREC_MEM_MAX_LEN for memcpy and memmove */ -#define SECUREC_MEM_MAX_LEN (0x7fffffffUL) -#define SECUREC_WCHAR_MEM_MAX_LEN (SECUREC_MEM_MAX_LEN / sizeof(wchar_t)) - -#if SECUREC_STRING_MAX_LEN > 0x7fffffff -#error "max string is 2G" -#endif - -#if (defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) -#if (__SIZEOF_POINTER__ != 4) && (__SIZEOF_POINTER__ != 8) -#error "unsupported system" -#endif -#endif - -#ifndef SECUREC_MALLOC -#define SECUREC_MALLOC(x) malloc((size_t)(x)) -#endif - -#ifndef SECUREC_FREE -#define SECUREC_FREE(x) free((void *)(x)) -#endif - -#if defined(_WIN64) || defined(WIN64) || defined(__LP64__) || defined(_LP64) -#define SECUREC_ON_64BITS -#endif - -#if (!defined(SECUREC_ON_64BITS) && defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) -#if __SIZEOF_POINTER__ == 8 -#define SECUREC_ON_64BITS -#endif -#endif - -#if defined(__SVR4) || defined(__svr4__) -#define SECUREC_ON_SOLARIS -#endif - -#if (defined(__hpux) || defined(_AIX) || defined(SECUREC_ON_SOLARIS)) -#define SECUREC_ON_UNIX -#endif - -/* codes should run under the macro SECUREC_COMPATIBLE_LINUX_FORMAT in unknown system on default, - * and strtold. The function - * strtold is referenced first at ISO9899:1999(C99), and some old compilers can - * not support these functions. Here provides a macro to open these functions: - * SECUREC_SUPPORT_STRTOLD -- if defined, strtold will be used - */ -#ifndef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 0 -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) -#if defined(__USE_ISOC99) || \ - (defined(_AIX) && defined(_ISOC99_SOURCE)) || \ - (defined(__hpux) && defined(__ia64)) || \ - (defined(SECUREC_ON_SOLARIS) && (!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \ - defined(_STDC_C99) || defined(__EXTENSIONS__)) -#undef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 1 -#endif -#endif -#if ((defined(SECUREC_WRLINUX_BELOW4) || defined(_WRLINUX_BELOW4_))) -#undef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 0 -#endif -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - -#ifndef SECUREC_TWO_MIN -#define SECUREC_TWO_MIN(a, b) ((a) < (b) ? (a) : (b)) -#endif - -/* for strncpy_s performance optimization */ -#define SECUREC_STRNCPY_SM(dest, destMax, src, count) \ - (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (SECUREC_TWO_MIN(count , strlen(src)) + 1) <= (size_t)destMax ) ? \ - ((count < strlen(src))? (memcpy(dest, src, count), *((char *)dest + count) = '\0', EOK): \ - (memcpy(dest, src, strlen(src) + 1), EOK ) ) :(strncpy_error(dest, destMax, src, count))) - -#define SECUREC_STRCPY_SM(dest, destMax, src) \ - (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (strlen(src) + 1) <= (size_t)destMax )? (memcpy(dest, src, strlen(src) + 1), EOK): \ - (strcpy_error(dest, destMax, src))) - -/* for strcat_s performance optimization */ -#if defined(__GNUC__) -#define SECUREC_STRCAT_SM(dest, destMax, src) \ - ({ \ - int catRet = EOK; \ - if ((void *)dest != NULL && (void *)src != NULL && (size_t)(destMax) >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ - char *catTmpDst = (dest); \ - size_t catRestSize = (destMax); \ - while(catRestSize > 0 && *catTmpDst) { \ - ++catTmpDst; \ - --catRestSize; \ - } \ - if (catRestSize == 0) { \ - catRet = EINVAL; \ - } else if ((strlen(src) + 1) <= catRestSize) { \ - memcpy(catTmpDst, (src), strlen(src) + 1); \ - catRet = EOK; \ - } else { \ - catRet = ERANGE; \ - } \ - if (catRet != EOK) { \ - catRet = strcat_s((dest), (destMax), (src)); \ - } \ - } else { \ - catRet = strcat_s((dest), (destMax), (src)); \ - } \ - catRet; \ - }) -#else -#define SECUREC_STRCAT_SM(dest, destMax, src) strcat_s(dest, destMax, src) -#endif - -/* for strncat_s performance optimization */ -#if defined(__GNUC__) -#define SECUREC_STRNCAT_SM(dest, destMax, src, count) \ - ({ \ - int ncatRet = EOK; \ - if ((void *)dest != NULL && (void *)src != NULL && (size_t)destMax > 0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (((unsigned long long)(count) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ - char *ncatTmpDest = (dest); \ - size_t ncatRestSize = (destMax); \ - while(ncatRestSize > 0 && *ncatTmpDest) { \ - ++ncatTmpDest; \ - --ncatRestSize; \ - } \ - if (ncatRestSize == 0) { \ - ncatRet = EINVAL; \ - } else if ((SECUREC_TWO_MIN((count) , strlen(src)) + 1) <= ncatRestSize ) { \ - if ((count) < strlen(src)) { \ - memcpy(ncatTmpDest, (src), (count)); \ - *(ncatTmpDest + (count)) = '\0'; \ - } else { \ - memcpy(ncatTmpDest, (src), strlen(src) + 1); \ - } \ - } else { \ - ncatRet = ERANGE; \ - } \ - if (ncatRet != EOK) { \ - ncatRet = strncat_s((dest), (destMax), (src), (count)); \ - } \ - } else { \ - ncatRet = strncat_s((dest), (destMax), (src), (count)); \ - } \ - ncatRet; \ - }) -#else -#define SECUREC_STRNCAT_SM(dest, destMax, src, count) strncat_s(dest, destMax, src, count) -#endif - -/* SECUREC_MEMCPY_SM do NOT check buffer overlap by default */ -#define SECUREC_MEMCPY_SM(dest, destMax, src, count) \ - (!(((size_t)destMax == 0 )||(((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ - ((size_t)count > (size_t)destMax) || ((void *)dest) == NULL || ((void *)src == NULL))? \ - (memcpy(dest, src, count), EOK) : \ - (memcpy_s(dest, destMax, src, count))) - -#define SECUREC_MEMSET_SM(dest, destMax, c, count) \ - (!(((size_t)destMax == 0 ) || (((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ - ((void *)dest == NULL) || ((size_t)count > (size_t)destMax)) ? \ - (memset(dest, c, count), EOK) : \ - ( memset_s(dest, destMax, c, count))) - -#endif -#endif /* __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 */ diff --git a/base/include/securectype.h b/base/include/securectype.h new file mode 120000 index 0000000..e1ba8dc --- /dev/null +++ b/base/include/securectype.h @@ -0,0 +1 @@ +../../../../third_party/bounds_checking_function/include/securectype.h \ No newline at end of file diff --git a/base/src/securec/fscanf_s.c b/base/src/securec/fscanf_s.c deleted file mode 100755 index 28b2e9d..0000000 --- a/base/src/securec/fscanf_s.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The fscanf_s function is equivalent to fscanf except that the c, s, - * and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a*) - * The fscanf function reads data from the current position of stream into - * the locations given by argument (if any). Each argument must be a pointer - * to a variable of a type that corresponds to a type specifier in format. - * format controls the interpretation of the input fields and has the same - * form and function as the format argument for scanf; see scanf for a - * description of format. - * - * - * stream Pointer to FILE structure. - * format Format control string, see Format Specifications. - * ... Optional arguments. - * - * - * ... The covered value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; the return value does not include fields that were read but - * not assigned. A return value of 0 indicates that no fields were assigned. - * A return value of -1 indicates an error. - ******************************************************************************* - */ -int fscanf_s(FILE *stream, const char *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - va_start(arglist, format); - ret = vfscanf_s(stream, format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} - diff --git a/base/src/securec/fscanf_s.c b/base/src/securec/fscanf_s.c new file mode 120000 index 0000000..dab1a65 --- /dev/null +++ b/base/src/securec/fscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/fscanf_s.c \ No newline at end of file diff --git a/base/src/securec/fwscanf_s.c b/base/src/securec/fwscanf_s.c deleted file mode 100755 index a183bb5..0000000 --- a/base/src/securec/fwscanf_s.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The fwscanf_s function is the wide-character equivalent of the fscanf_s function - * The fwscanf_s function reads data from the current position of stream into - * the locations given by argument (if any). Each argument must be a pointer - * to a variable of a type that corresponds to a type specifier in format. - * format controls the interpretation of the input fields and has the same - * form and function as the format argument for scanf; see scanf for a - * description of format. - * - * - * stream Pointer to FILE structure. - * format Format control string, see Format Specifications. - * ... Optional arguments. - * - * - * ... The converted value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; the return value does not include fields that were read but - * not assigned. A return value of 0 indicates that no fields were assigned. - * A value of -1 indicates that an error has occurred. - ******************************************************************************* - */ -int fwscanf_s(FILE *stream, const wchar_t *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vfwscanf_s(stream, format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} - diff --git a/base/src/securec/fwscanf_s.c b/base/src/securec/fwscanf_s.c new file mode 120000 index 0000000..8016e06 --- /dev/null +++ b/base/src/securec/fwscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/fwscanf_s.c \ No newline at end of file diff --git a/base/src/securec/gets_s.c b/base/src/securec/gets_s.c deleted file mode 100755 index 340c7ea..0000000 --- a/base/src/securec/gets_s.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -static void SecTrimCRLF(char *buffer, const size_t len) -{ - int i; - for (i = (int)(len - 1); i >= 0 && (buffer[i] == '\r' || buffer[i] == '\n'); --i) { - buffer[i] = '\0'; - } - return; -} - -/******************************************************************************* - * - * The gets_s function reads at most one less than the number of characters - * specified by destMax from the stream pointed to by stdin, into the array pointed to by buffer - * The line consists of all characters up to and including - * the first newline character ('\n'). gets_s then replaces the newline - * character with a null character ('\0') before returning the line. - * If the first character read is the end-of-file character, a null character - * is stored at the beginning of buffer and NULL is returned. - * - * - * buffer Storage location for input string. - * numberOfElements The size of the buffer. - * - * - * buffer is updated - * - * - * buffer Successful operation - * NULL Improper parameter or read failure - ******************************************************************************* - */ -char *gets_s(char *buffer, size_t numberOfElements) -{ - size_t len; -#ifdef SECUREC_COMPATIBLE_WIN_FORMAT - size_t bufferSize = ((numberOfElements == (size_t)-1) ? SECUREC_STRING_MAX_LEN : numberOfElements); -#else - size_t bufferSize = numberOfElements; -#endif - - if (buffer == NULL || bufferSize == 0 || bufferSize > SECUREC_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_PARAMTER("gets_s"); - return NULL; - } - - if (fgets(buffer, (int)bufferSize, stdin) == NULL) { - return NULL; - } - - len = strlen(buffer); - if (len > 0 && len < bufferSize) { - SecTrimCRLF(buffer, len); - } - - return buffer; -} - diff --git a/base/src/securec/gets_s.c b/base/src/securec/gets_s.c new file mode 120000 index 0000000..872ccd8 --- /dev/null +++ b/base/src/securec/gets_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/gets_s.c \ No newline at end of file diff --git a/base/src/securec/input.inl b/base/src/securec/input.inl deleted file mode 100755 index b62c121..0000000 --- a/base/src/securec/input.inl +++ /dev/null @@ -1,1910 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -/* [Standardize-exceptions] Use unsafe function: Performance-sensitive - * [reason] Always used in the performance critical path, - * and sufficient input validation is performed before calling - */ - -#ifndef __INPUT_INL__5D13A042_DC3F_4ED9_A8D1_882811274C27 -#define __INPUT_INL__5D13A042_DC3F_4ED9_A8D1_882811274C27 - -#if !defined(SECUREC_SYSAPI4VXWORKS) -#include -#ifdef SECUREC_FOR_WCHAR -#include /* for iswspace */ -#endif -#endif - -#define SECUREC_NUM_WIDTH_SHORT 0 -#define SECUREC_NUM_WIDTH_INT 1 -#define SECUREC_NUM_WIDTH_LONG 2 -#define SECUREC_NUM_WIDTH_LONG_LONG 3 /* also long double */ - -#define SECUREC_BUF_EXT_MUL (2) -#define SECUREC_BUFFERED_BLOK_SIZE 1024 - -#if defined(SECUREC_SYSAPI4VXWORKS) -#define SECUREC_DECIMAL_POINT_PTR "." -#else -#include /* if this file NOT exist, you can remove it */ -#define SECUREC_DECIMAL_POINT_PTR (localeconv()->decimal_point) -#endif - -#if defined(SECUREC_VXWORKS_PLATFORM) && !defined(va_copy) && !defined(__va_copy) -/* the name is the same as system macro. */ -#define __va_copy(d,s) do { \ - size_t size_of_d = (size_t)sizeof(d); \ - size_t size_of_s = (size_t)sizeof(s); \ - if (size_of_d != size_of_s) { \ - (void)memcpy((d), (s), sizeof(va_list)); \ - } else { \ - (void)memcpy(&(d), &(s), sizeof(va_list)); \ - } \ - } SECUREC_WHILE_ZERO -#endif - -#define SECUREC_MUL10(x) ((((x) << 2) + (x)) << 1) -#define SECUREC_MULTI_BYTE_MAX_LEN (6) -#define SECUREC_INT_MAX_DIV_TEN 21474836 - -/* Compatibility macro name cannot be modifie */ -#ifndef UNALIGNED -#if !(defined(_M_IA64)) && !(defined(_M_AMD64)) -#define UNALIGNED -#else -#define UNALIGNED __unaligned -#endif -#endif - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) -#define SECUREC_MAX_64BITS_VALUE 18446744073709551615ULL -#define SECUREC_MAX_64BITS_VALUE_DIV_TEN 1844674407370955161ULL -#define SECUREC_MAX_64BITS_VALUE_CUT_LAST_DIGIT 18446744073709551610ULL -#define SECUREC_MIN_64BITS_NEG_VALUE 9223372036854775808ULL -#define SECUREC_MAX_64BITS_POS_VALUE 9223372036854775807ULL -#define SECUREC_MIN_32BITS_NEG_VALUE 2147483648ULL -#define SECUREC_MAX_32BITS_POS_VALUE 2147483647ULL -#define SECUREC_MAX_32BITS_VALUE 4294967295ULL -#define SECUREC_MAX_32BITS_VALUE_INC 4294967296ULL -#define SECUREC_MAX_32BITS_VALUE_DIV_TEN 429496729ULL -#define SECUREC_LONG_BIT_NUM ((unsigned int)(sizeof(long) << 3U)) -#endif - -#define SECUREC_CHAR(x) (x) -#define SECUREC_BRACE ('{') /* [ to { */ - -#ifdef SECUREC_FOR_WCHAR -#define SECUREC_EOF WEOF -#define SECUREC_MB_LEN 16 /* max. # bytes in multibyte char ,see MB_LEN_MAX */ -#define SECUREC_GET_CHAR() (++charCount, SecGetCharW(stream)) -/* un get char marco do not set char count ,The reason is to avoid warning that variables are not used */ -#define SECUREC_UN_GET_CHAR(chr) (--charCount,SecUnGetCharW((chr), stream)) -#define SECUREC_IS_DIGIT(chr) (!((chr) & 0xff00) && isdigit(((chr) & 0x00ff))) -#define SECUREC_IS_XDIGIT(chr) (!((chr) & 0xff00) && isxdigit(((chr) & 0x00ff))) -static void SecUnGetCharW(SecInt chr, SecFileStream *str); -static SecInt SecGetCharW(SecFileStream *str); -#else -#define SECUREC_EOF EOF -#define SECUREC_GET_CHAR() (++charCount, SecGetChar(stream)) -#define SECUREC_UN_GET_CHAR(chr) (--charCount,SecUnGetChar((chr), stream)) -#define SECUREC_IS_DIGIT(chr) isdigit((unsigned char)(chr)& 0x00ff) -#define SECUREC_IS_XDIGIT(chr) isxdigit((unsigned char)(chr)& 0x00ff) -static SecInt SecGetChar(SecFileStream *str); -static void SecUnGetChar(SecInt chr, SecFileStream *str); -#endif - -#define SECUREC_SKIP_SPACE_CHAR() SecSkipSpaceChar(&charCount, stream) - -static SecInt SecSkipSpaceChar(int *, SecFileStream *); - -typedef struct { -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - SecUnsignedInt64 number64As; - int beyondMax; -#endif - SecUnsignedInt64 number64; - unsigned long number; - int negative; -} SecNumberSpce; - -static int SecCheckFloatDecPoint(char dec_point) -{ - /* don't support multi-language decimal point */ - if (dec_point == '.') { - return 1; - } - return 0; -} - -static int SecIs64BitPtr(size_t ptrSize) -{ - /* point size is 4 or 8 , Under the 64 bit system, the value not 0 */ - /* to clear e778 */ - return (int)(ptrSize & sizeof(SecInt64)); -} - -static void SecAssignFloat(int numberWidth, void *argument, const char *number) -{ - char *endptr = NULL; - double d; -#if SECUREC_SUPPORT_STRTOLD - if (numberWidth == SECUREC_NUM_WIDTH_LONG_LONG) { - long double d2 = strtold(number, &endptr); - *(long double UNALIGNED *)argument = d2; - return; - } -#endif - d = strtod(number, &endptr); - if (numberWidth > SECUREC_NUM_WIDTH_INT) { - *(double UNALIGNED *)argument = (double)d; - } else { - *(float UNALIGNED *)argument = (float)d; - } -} - -static int SecUpdateFloatString(size_t usedLen, - size_t *floatStrSize, - SecChar **floatStr, const SecChar *floatStrBuf, SecChar **allocFlag) -{ - if (usedLen == (*floatStrSize)) { - if ((*floatStr) == floatStrBuf) { - /* add 1 to clear ZERO LENGTH ALLOCATIONS warning */ - size_t oriBufSize = (*floatStrSize) * SECUREC_BUF_EXT_MUL * sizeof(SecChar) + 1; - void *tmpPointer = (void *)SECUREC_MALLOC(oriBufSize); - if (tmpPointer == NULL) { - return 0; - } - if (memcpy_s(tmpPointer, oriBufSize, (*floatStr), (*floatStrSize) * sizeof(SecChar)) != EOK) { - SECUREC_FREE(tmpPointer); /* This is a dead code, just to meet the coding requirements */ - return 0; - } - (*floatStr) = (SecChar *) (tmpPointer); - (*allocFlag) = (SecChar *) (tmpPointer); /* use to clear free on stack warning */ - (*floatStrSize) *= SECUREC_BUF_EXT_MUL; /* this is OK, oriBufSize plus 1 just clear warning */ - } else { - /* LSD 2014.3.6 fix, replace realloc to malloc to avoid heap injection */ - size_t oriBufSize = (*floatStrSize) * sizeof(SecChar); - size_t nextSize = oriBufSize * SECUREC_BUF_EXT_MUL + 1; /* add 1 to clear ZERO LENGTH ALLOCATIONS warning */ - /* Prevents integer overflow when calculating the wide character length. - * The maximum length of 2G/2 is enough - */ - if (nextSize <= (SECUREC_WCHAR_MEM_MAX_LEN) / 2) { - void *tmpPointer = (void *)SECUREC_MALLOC(nextSize); - if (tmpPointer == NULL) { - return 0; - } - if (memcpy_s(tmpPointer, nextSize, (*floatStr), oriBufSize) != EOK) { - SECUREC_FREE(tmpPointer); /* This is a dead code, just to meet the coding requirements */ - return 0; - } - if (memset_s((*floatStr), oriBufSize, 0, oriBufSize) != EOK) { - SECUREC_FREE(tmpPointer); /* This is a dead code, just to meet the coding requirements */ - return 0; - } - SECUREC_FREE((*floatStr)); - - (*floatStr) = (SecChar *) (tmpPointer); - (*allocFlag) = (SecChar *) (tmpPointer); /* use to clear free on stack warning */ - (*floatStrSize) *= SECUREC_BUF_EXT_MUL; /* this is OK, oriBufSize plus 1 just clear warning */ - return 1; - } - return 0; - } - } - return 1; -} - -#ifndef SECUREC_FOR_WCHAR - -/* LSD only multi-bytes string need isleadbyte() function */ -static int SecIsleadbyte(SecInt ch) -{ - unsigned int c = (unsigned int)ch; -#if !(defined(_WIN32) || defined(_INC_WCTYPE)) - return (int)(c & 0x80); -#else - return (int)isleadbyte((int)(c & 0xff)); -#endif -} - -#endif - -static void SecUpdateWcharFlagByType(SecUnsignedChar chr, signed char *isWchar) -{ -#if defined(SECUREC_FOR_WCHAR) && (defined(SECUREC_COMPATIBLE_WIN_FORMAT)) - signed char flagForUpperType = -1; - signed char flagForLowerType = 1; -#else - signed char flagForUpperType = 1; - signed char flagForLowerType = -1; -#endif - - if ((*isWchar) == 0) { - if ((chr == SECUREC_CHAR('C')) || (chr == SECUREC_CHAR('S'))) { - (*isWchar) = flagForUpperType; - } else { - (*isWchar) = flagForLowerType; - } - } - return; -} - -#define SECUREC_FLOAT_BUFSIZE (309+40) /* digits in max. dp value + slop */ -#ifdef SECUREC_FOR_WCHAR -#define SECUREC_BRACKET_TABLE_SIZE (32 * 256) -#else -#define SECUREC_BRACKET_TABLE_SIZE (32) -#endif - -#ifdef SECUREC_FOR_WCHAR -#define SECUREC_GETC fgetwc -#define SECUREC_UN_GETC ungetwc -#define SECUREC_CHAR_MASK 0xffff -#else -#define SECUREC_GETC fgetc -#define SECUREC_UN_GETC ungetc -#define SECUREC_CHAR_MASK 0xff -#endif - -/* LSD 2014 1 24 add to protect NULL pointer access */ -#define SECUREC_CHECK_INPUT_ADDR(p) do { \ - if ((p) == NULL) { \ - paraIsNull = 1; \ - goto ERR_RET; \ - } \ - } SECUREC_WHILE_ZERO - -#ifdef SECUREC_FOR_WCHAR -void SecClearDestBufW(const wchar_t *buffer, const wchar_t *cformat, va_list arglist) -#else -void SecClearDestBuf(const char *buffer, const char *cformat, va_list arglist) -#endif -{ - const SecUnsignedChar *fmt = (const SecUnsignedChar *)cformat; - void *pDestBuf = NULL; - va_list arglistSave; /* backup for arglist value, this variable don't need initialized */ - size_t bufSize = 0; - int spec = 0; - signed char isWchar = 0; - char doneFlag = 0; - - if (fmt != NULL) { - while (*fmt) { - if (*fmt == SECUREC_CHAR('%')) { - doneFlag = 0; - isWchar = 0; - - while (doneFlag == 0) { - spec = *(++fmt); - - if (SECUREC_IS_DIGIT((SecUnsignedChar) spec)) { - continue; - } else if (spec == SECUREC_CHAR('h')) { - isWchar = -1; - continue; - } else if (spec == SECUREC_CHAR('l') || spec == SECUREC_CHAR('w')) { - isWchar = 1; - continue; - } - doneFlag = 1; - } - - /* if no l or h flag */ - SecUpdateWcharFlagByType(*fmt, &isWchar); - - spec = *fmt | (SECUREC_CHAR('a') - SECUREC_CHAR('A')); - - if (!(spec == SECUREC_CHAR('c') || spec == SECUREC_CHAR('s') || spec == SECUREC_BRACE)) { - return; /* first argument is not a string type */ - } - - if ((buffer != NULL) && (*buffer != SECUREC_CHAR('\0')) && (spec != SECUREC_CHAR('s'))) { - /* when buffer not empty just clear %s. - * example call sscanf by argument of (" \n","%s",str,sizeof(str)) - */ - return; - } - - if (spec == SECUREC_BRACE) { -#if !(defined(SECUREC_COMPATIBLE_WIN_FORMAT)) - if (*fmt == SECUREC_CHAR('{')) { - return; - } -#endif - ++fmt; - - if (*fmt == SECUREC_CHAR('^')) { - ++fmt; - } - - if (*fmt == SECUREC_CHAR(']')) { - ++fmt; - } - - while ((*fmt != SECUREC_CHAR('\0')) && (*fmt != SECUREC_CHAR(']'))) { - ++fmt; - } - if (*fmt == SECUREC_CHAR('\0')) { - return; /* trunc'd format string */ - } - } - - (void)memset(&arglistSave, 0, sizeof(arglistSave)); /* to clear e530 arglistSave not initialized */ -#if defined(va_copy) - va_copy(arglistSave, arglist); -#elif defined(__va_copy) /* for vxworks */ - __va_copy(arglistSave, arglist); -#else - arglistSave = arglist; -#endif - pDestBuf = (void *)va_arg(arglistSave, void *); - /* Get the next argument - size of the array in characters */ - bufSize = ((size_t)(va_arg(arglistSave, size_t))) & 0xFFFFFFFFUL; - - va_end(arglistSave); - /* to clear e438 last value assigned not used , the compiler will optimize this code */ - (void)arglistSave; - - if (bufSize == 0 || bufSize > SECUREC_STRING_MAX_LEN || pDestBuf == NULL) { - return; - } - - *(char *)pDestBuf = '\0'; - - if (isWchar > 0 && bufSize >= sizeof(wchar_t)) { - *(wchar_t UNALIGNED *)pDestBuf = L'\0'; - } - - return; - } - ++fmt; /* skip to next char */ - } - } - return; -} - -static void SecAssignNumber(void *argPtr, const int numberWidth, const unsigned long number) -{ - if (numberWidth > SECUREC_NUM_WIDTH_INT) { - *(long UNALIGNED *)argPtr = (long)number; /* take number as unsigned number */ - } else if (numberWidth == SECUREC_NUM_WIDTH_INT) { - *(int UNALIGNED *)argPtr = (int)number; - } else if (numberWidth == SECUREC_NUM_WIDTH_SHORT) { - *(short UNALIGNED *)argPtr = (short)number; /* take number as unsigned number */ - } else { /* < 0 for hh format modifier */ - *(char UNALIGNED *)argPtr = (char)number; /* take number as unsigned number */ - } -} - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) -static int SecIsLongBitEqual(const int bitNum) -{ - return (unsigned int)bitNum == SECUREC_LONG_BIT_NUM; -} -#endif -static SecInt SecHex2Dec(const SecInt ch) -{ - /* use isdigt Causing tool false alarms */ - return (SecInt) ((ch >= '0' && ch <= '9') ? (unsigned char)ch : - ((((unsigned char)ch | (unsigned char)('a' - 'A')) - ('a')) + 10 + '0')); -} - -static char SecDecodeNumber(const int comChr, const SecInt ch, SecNumberSpce *spec) -{ - char doneFlag = 0; -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - unsigned long decimalEdge = SECUREC_MAX_32BITS_VALUE_DIV_TEN; -#endif - if (comChr == SECUREC_CHAR('x') || comChr == SECUREC_CHAR('p')) { - if (SECUREC_IS_XDIGIT(ch)) { - SecInt ch2; -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if ((spec->number >> (SECUREC_LONG_BIT_NUM - 4)) > 0) { - spec->beyondMax = 1; - } -#endif - spec->number = (spec->number << 4); - ch2 = SecHex2Dec(ch); - spec->number += (unsigned long)((SecUnsignedInt) ch2 - SECUREC_CHAR('0')); - } else { - doneFlag = 1; - } - } else if (SECUREC_IS_DIGIT(ch)) { - - if (comChr == SECUREC_CHAR('o')) { - if (ch < SECUREC_CHAR('8')) { -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if ((spec->number >> (SECUREC_LONG_BIT_NUM - 3)) > 0) { - spec->beyondMax = 1; - } -#endif - spec->number = (spec->number << 3); - spec->number += (unsigned long)((SecUnsignedInt) ch - SECUREC_CHAR('0')); - } else { - doneFlag = 1; - } - } else { /* SECUREC_CHAR('d') == comChr */ -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) -#ifdef SECUREC_ON_64BITS - if (SecIsLongBitEqual(64)) { - decimalEdge = (unsigned long)SECUREC_MAX_64BITS_VALUE_DIV_TEN; - } -#else - if (SecIsLongBitEqual(32)) { - decimalEdge = SECUREC_MAX_32BITS_VALUE_DIV_TEN; - } -#endif - if (spec->number > decimalEdge) { - spec->beyondMax = 1; - } -#endif - spec->number = SECUREC_MUL10(spec->number); -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if (spec->number == SECUREC_MUL10(decimalEdge)) { - spec->number64As = (unsigned long)SECUREC_MAX_64BITS_VALUE - spec->number; - if (spec->number64As < (SecUnsignedInt64)((SecUnsignedInt) ch - SECUREC_CHAR('0'))) { - spec->beyondMax = 1; - } - } -#endif - spec->number += (unsigned long)((SecUnsignedInt) ch - SECUREC_CHAR('0')); - } - } else { - doneFlag = 1; - } - return doneFlag; -} - -static void SecFinishNumber(const int comChr, const int numberWidth, SecNumberSpce *spec) -{ - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if (spec->negative != 0) { - if (numberWidth == SECUREC_NUM_WIDTH_INT) { - if ((comChr == SECUREC_CHAR('d')) || (comChr == SECUREC_CHAR('i'))) { -#ifdef SECUREC_ON_64BITS - if (SecIsLongBitEqual(64)) { - if ((spec->number > SECUREC_MIN_64BITS_NEG_VALUE)) { - spec->number = 0; - } else { - spec->number = (unsigned int)(-(int)spec->number); - } - } -#else - if (SecIsLongBitEqual(32)) { - if ((spec->number > SECUREC_MIN_32BITS_NEG_VALUE)) { - spec->number = SECUREC_MIN_32BITS_NEG_VALUE; - } else { - spec->number = (unsigned int)(-(int)spec->number); - } - } -#endif - if (spec->beyondMax) { -#ifdef SECUREC_ON_64BITS - if (SecIsLongBitEqual(64)) { - spec->number = 0; - } -#else - if (SecIsLongBitEqual(32)) { - spec->number = SECUREC_MIN_32BITS_NEG_VALUE; - } -#endif - } - } else { /* o, u, x, X ,p */ -#ifdef SECUREC_ON_64BITS - if (spec->number > SECUREC_MAX_32BITS_VALUE_INC) { - spec->number = SECUREC_MAX_32BITS_VALUE; - } else { - spec->number = (unsigned int)(-(int)spec->number); - } -#else - spec->number = (unsigned int)(-(int)spec->number); -#endif - if (spec->beyondMax) { - spec->number |= (unsigned long)0xffffffffffffffffULL; - } - } - } else { - if ((comChr == SECUREC_CHAR('d')) || (comChr == SECUREC_CHAR('i'))) { - if (spec->number > (unsigned long)(1ULL << (SECUREC_LONG_BIT_NUM - 1))) { - spec->number = (unsigned long)(1ULL << (SECUREC_LONG_BIT_NUM - 1)); - } else { - spec->number = (unsigned long)(-(long)spec->number); - } - } else { - spec->number = (unsigned long)(-(long)spec->number); - if (spec->beyondMax) { - spec->number |= (unsigned long)0xffffffffffffffffULL; - } - } - } - if ((comChr == SECUREC_CHAR('d')) || (comChr == SECUREC_CHAR('i'))) { - if (((spec->beyondMax) && (numberWidth < SECUREC_NUM_WIDTH_SHORT)) || - ((spec->beyondMax) && (numberWidth == SECUREC_NUM_WIDTH_SHORT)) || - ((spec->beyondMax) && (numberWidth == SECUREC_NUM_WIDTH_INT) && (SecIsLongBitEqual(64)))) { - spec->number = 0; - } - if ((spec->beyondMax) && (numberWidth == SECUREC_NUM_WIDTH_LONG)) { - spec->number = ((unsigned long)(1UL << (SECUREC_LONG_BIT_NUM - 1))); - } - } else { /* o, u, x, X, p */ - if (spec->beyondMax) { - spec->number |= (unsigned long)0xffffffffffffffffULL; - } - } - } else { - if (numberWidth == SECUREC_NUM_WIDTH_INT) { - if ((comChr == SECUREC_CHAR('d')) || (comChr == SECUREC_CHAR('i'))) { -#ifdef SECUREC_ON_64BITS - if (SecIsLongBitEqual(64)) { - if (spec->number > SECUREC_MAX_64BITS_POS_VALUE) { - spec->number |= (unsigned long)0xffffffffffffffffULL; - } - } - if ((spec->beyondMax) && (SecIsLongBitEqual(64))) { - spec->number |= (unsigned long)0xffffffffffffffffULL; - } -#else - if (SecIsLongBitEqual(32)) { - if (spec->number > SECUREC_MAX_32BITS_POS_VALUE) { - spec->number = SECUREC_MAX_32BITS_POS_VALUE; - } - } - if ((spec->beyondMax) && (SecIsLongBitEqual(32))) { - spec->number = SECUREC_MAX_32BITS_POS_VALUE; - } -#endif - } else { /* o,u,x,X,p */ - if (spec->beyondMax) { - spec->number = SECUREC_MAX_32BITS_VALUE; - } - } - - } else { - if ((comChr == SECUREC_CHAR('d')) || (comChr == SECUREC_CHAR('i'))) { - if (spec->number > ((unsigned long)(1UL << (SECUREC_LONG_BIT_NUM - 1)) - 1)) { - spec->number = ((unsigned long)(1UL << (SECUREC_LONG_BIT_NUM - 1)) - 1); - } - if (((spec->beyondMax) && (numberWidth < SECUREC_NUM_WIDTH_SHORT)) || - ((spec->beyondMax) && (numberWidth == SECUREC_NUM_WIDTH_SHORT))) { - spec->number |= (unsigned long)0xffffffffffffffffULL; - } - if ((spec->beyondMax) && (numberWidth == SECUREC_NUM_WIDTH_LONG)) { - spec->number = ((unsigned long)(1UL << (SECUREC_LONG_BIT_NUM - 1)) - 1); - } - } else { - if (spec->beyondMax) { - spec->number |= (unsigned long)0xffffffffffffffffULL; - } - } - } - } -#else - if (spec->negative != 0) { -#if defined(__hpux) - if (comChr != SECUREC_CHAR('p')) { - spec->number = (unsigned long)(-(long)spec->number); - } -#else - spec->number = (unsigned long)(-(long)spec->number); -#endif - } -#endif - - (void)numberWidth; /* clear compile warning */ - (void)comChr; /* clear compile warning */ - return; -} - -static char SecDecodeNumber64(const int comChr, const SecInt ch, SecNumberSpce *spec) -{ - char doneFlag = 0; - - if (comChr == SECUREC_CHAR('x') || comChr == SECUREC_CHAR('p')) { - if (SECUREC_IS_XDIGIT(ch)) { - SecInt ch2; -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if ((spec->number64 >> 60) > 0) { - spec->beyondMax = 1; - } - spec->number64As = 16; -#endif - spec->number64 <<= 4; - ch2 = SecHex2Dec(ch); - spec->number64 += (SecUnsignedInt64)((SecUnsignedInt) ch2 - SECUREC_CHAR('0')); - } else { - doneFlag = 1; - } - } else if (SECUREC_IS_DIGIT(ch)) { - if (comChr == SECUREC_CHAR('o')) { - if (ch < SECUREC_CHAR('8')) { -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if ((spec->number64 >> 61) > 0) { - spec->beyondMax = 1; - } - spec->number64As = 8; -#endif - spec->number64 <<= 3; - spec->number64 += (SecUnsignedInt64)((SecUnsignedInt) ch - SECUREC_CHAR('0')); - } else { - doneFlag = 1; - } - } else { /* SECUREC_CHAR('d') == comChr */ -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if (spec->number64 > SECUREC_MAX_64BITS_VALUE_DIV_TEN) { - spec->beyondMax = 1; - } -#endif - spec->number64 = SECUREC_MUL10(spec->number64); -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if (spec->number64 == SECUREC_MAX_64BITS_VALUE_CUT_LAST_DIGIT) { - spec->number64As = SECUREC_MAX_64BITS_VALUE - spec->number64; - if (spec->number64As < (SecUnsignedInt64)((SecUnsignedInt) ch - SECUREC_CHAR('0'))) { - spec->beyondMax = 1; - } - } -#endif - spec->number64 += (SecUnsignedInt64)((SecUnsignedInt) ch - SECUREC_CHAR('0')); - } - - } else { - doneFlag = 1; - } - - return doneFlag; -} - -static void SecFinishNumber64(const int comChr, SecNumberSpce *spec) -{ -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - if (spec->negative != 0) { - if (comChr == (SECUREC_CHAR('d')) || (comChr == SECUREC_CHAR('i'))) { - if (spec->number64 > SECUREC_MIN_64BITS_NEG_VALUE) { - spec->number64 = SECUREC_MIN_64BITS_NEG_VALUE; - } else { - spec->number64 = (SecUnsignedInt64)(-(SecInt64)spec->number64); - } - if (spec->beyondMax) { - spec->number64 = SECUREC_MIN_64BITS_NEG_VALUE; - } - } else { /* o, u, x, X, p */ - spec->number64 = (SecUnsignedInt64)(-(SecInt64)spec->number64); - if (spec->beyondMax) { - spec->number64 = SECUREC_MAX_64BITS_VALUE; - } - } - } else { - if ((comChr == SECUREC_CHAR('d')) || (comChr == SECUREC_CHAR('i'))) { - if (spec->number64 > SECUREC_MAX_64BITS_POS_VALUE) { - spec->number64 = SECUREC_MAX_64BITS_POS_VALUE; - } - if (spec->beyondMax) { - spec->number64 = SECUREC_MAX_64BITS_POS_VALUE; - } - } else { - if (spec->beyondMax) { - spec->number64 = SECUREC_MAX_64BITS_VALUE; - } - } - } -#else - if (spec->negative != 0) { -#if defined(__hpux) - if (comChr != SECUREC_CHAR('p')) { - spec->number64 = (SecUnsignedInt64)(-(SecInt64)spec->number64); - } -#else - spec->number64 = (SecUnsignedInt64)(-(SecInt64)spec->number64); -#endif - } -#endif - (void)comChr; /* clear compile warning */ - return; -} - -static void SecAdjustStream(SecFileStream *stream) -{ - if (stream != NULL && (stream->flag & SECUREC_FILE_STREAM_FLAG) && stream->base != NULL) { - - if ((0 == stream->count) && feof(stream->pf)) { - /* file pointer at the end of file, don't need to seek back */ - stream->base[0] = '\0'; - } else { - /* LSD seek to original position, bug fix 2014 1 21 */ - - if (fseek(stream->pf, stream->oriFilePos, SEEK_SET)) { - /* seek failed, ignore it */ - stream->oriFilePos = 0; - } else { - if (stream->fileRealRead > 0) { /* LSD bug fix. when file reach to EOF, don't seek back */ -#if (defined(SECUREC_COMPATIBLE_WIN_FORMAT)) - int loops; - for (loops = 0; loops < (stream->fileRealRead / SECUREC_BUFFERED_BLOK_SIZE); ++loops) { - if (fread(stream->base, (size_t)1, (size_t)SECUREC_BUFFERED_BLOK_SIZE, - stream->pf) != SECUREC_BUFFERED_BLOK_SIZE) { - break; - } - } - if ((stream->fileRealRead % SECUREC_BUFFERED_BLOK_SIZE) != 0) { - size_t ret = fread(stream->base, - (size_t)((unsigned int)stream->fileRealRead % SECUREC_BUFFERED_BLOK_SIZE), - (size_t)1, stream->pf); - if (ret == 1 || ret == 0) { - if (ftell(stream->pf) < stream->oriFilePos + stream->fileRealRead) { - (void)fseek(stream->pf, stream->oriFilePos + stream->fileRealRead, SEEK_SET); - } - } - } - -#else - /* in linux like system */ - if (fseek(stream->pf, stream->oriFilePos + stream->fileRealRead, SEEK_SET)) { - /* seek failed, ignore it */ - stream->oriFilePos = 0; - } -#endif - } - } - } - - SECUREC_FREE(stream->base); - stream->base = NULL; - } - return; -} - -#ifdef SECUREC_FOR_WCHAR -int SecInputSW(SecFileStream *stream, const wchar_t *cformat, va_list arglist) -#else -int SecInputS(SecFileStream *stream, const char *cformat, va_list arglist) -#endif -{ - SecChar floatStrBuffer[SECUREC_FLOAT_BUFSIZE + 1]; - SecChar *pFloatStr = floatStrBuffer; - SecChar *pAllocedFloatStr = NULL; - const SecUnsignedChar *format = (const SecUnsignedChar *)cformat; - - size_t arrayWidth = 0; - size_t floatStrUsedLen = 0; - size_t floatStrSize = sizeof(floatStrBuffer) / sizeof(floatStrBuffer[0]); - -#ifdef SECUREC_FOR_WCHAR - unsigned char *bracketTable = NULL; -#else - unsigned char bracketTable[SECUREC_BRACKET_TABLE_SIZE] = { 0 }; -#endif - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - SecNumberSpce numberSpec = { 0, 0, 0, 0, 0 }; -#else - SecNumberSpce numberSpec = { 0, 0, 0 }; -#endif - - void *argPtr = NULL; /* points to receiving data addr */ - void *startPtr = NULL; - - SecInt ch = 0; - int charCount; - int comChr = 0; - int oricomChr = 0; - - int doneCount; - - int started = 0; - int width = 0; - int widthSet = 0; - int errNoMem = 0; - int formatError = 0; - int paraIsNull = 0; - int numberWidth = SECUREC_NUM_WIDTH_INT; /* 0 = SHORT, 1 = int, > 1 long or L_DOUBLE */ - int isInt64Arg = 0; /* 1 for 64-bit integer, 0 otherwise */ - - va_list arglistSave; /* backup for arglist value, this variable don't need initialized */ - -#if defined(va_copy) || defined(__va_copy) - int arglistBeenCopied = 0; -#endif - - SecChar decimal; - SecUnsignedChar expCh; - SecUnsignedChar last; - SecUnsignedChar prevChar; - - signed char isWchar; /* -1/0 not wchar, 1 for wchar */ - unsigned char tableMask; - char suppress; - char match; - char doneFlag; - - doneCount = 0; - charCount = 0; - match = 0; - (void)memset(&arglistSave, 0, sizeof(arglistSave)); - - while (format != NULL && *format) { - if (isspace((SecUnsignedChar) * format)) { - SecUnsignedChar tch; - /* eat all space chars and put fist no space char backup */ - SECUREC_UN_GET_CHAR(SECUREC_SKIP_SPACE_CHAR()); - do { - tch = (SecUnsignedChar) * (++format); - } while (isspace(tch)); - continue; - } - - if (*format == SECUREC_CHAR('%')) { - numberSpec.number = 0; - numberSpec.negative = 0; - prevChar = 0; - width = 0; - widthSet = 0; - started = 0; - arrayWidth = 0; - errNoMem = 0; - doneFlag = 0; - suppress = 0; - tableMask = 0; - isWchar = 0; - numberWidth = SECUREC_NUM_WIDTH_INT; -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && !(defined(SECUREC_ON_UNIX))) - numberSpec.beyondMax = 0; -#endif - isInt64Arg = 0; - numberSpec.number64 = 0; - - while (doneFlag == 0) { - comChr = *(++format); - if (SECUREC_IS_DIGIT((SecUnsignedChar) comChr)) { - widthSet = 1; - if (width > SECUREC_INT_MAX_DIV_TEN) { - formatError = 1; - goto ERR_RET; - } - width = (int)SECUREC_MUL10((unsigned int)width) + (comChr - SECUREC_CHAR('0')); - } else { - switch (comChr) { - case SECUREC_CHAR('F'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('N'): - break; - case SECUREC_CHAR('h'): - --numberWidth; /* h for SHORT , hh for CHAR */ - isWchar = -1; - break; -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT - case SECUREC_CHAR('j'): - numberWidth = SECUREC_NUM_WIDTH_LONG_LONG; /* intmax_t or uintmax_t */ - isInt64Arg = 1; - break; - case SECUREC_CHAR('t'): /* fall-through */ /* FALLTHRU */ -#endif - case SECUREC_CHAR('z'): -#ifdef SECUREC_ON_64BITS - numberWidth = SECUREC_NUM_WIDTH_LONG_LONG; - isInt64Arg = 1; -#else - numberWidth = SECUREC_NUM_WIDTH_LONG; -#endif - break; - case SECUREC_CHAR('L'): /* long double */ /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('q'): - numberWidth = SECUREC_NUM_WIDTH_LONG_LONG; - isInt64Arg = 1; - break; - case SECUREC_CHAR('l'): - if (*(format + 1) == SECUREC_CHAR('l')) { - isInt64Arg = 1; - numberWidth = SECUREC_NUM_WIDTH_LONG_LONG; - ++format; - } else { - numberWidth = SECUREC_NUM_WIDTH_LONG; -#ifdef SECUREC_ON_64BITS -#if !(defined(SECUREC_COMPATIBLE_WIN_FORMAT)) /* on window 64 system sizeof long is 32bit */ - isInt64Arg = 1; -#endif -#endif - isWchar = 1; - } - break; - case SECUREC_CHAR('w'): - isWchar = 1; - break; - - case SECUREC_CHAR('*'): - suppress = 1; - break; - - case SECUREC_CHAR('I'): - if ((*(format + 1) == SECUREC_CHAR('6')) && - (*(format + 2) == SECUREC_CHAR('4'))) { - isInt64Arg = 1; - format += 2; - break; - } else if ((*(format + 1) == SECUREC_CHAR('3')) && - (*(format + 2) == SECUREC_CHAR('2'))) { - format += 2; - break; - } else if ((*(format + 1) == SECUREC_CHAR('d')) || - (*(format + 1) == SECUREC_CHAR('i')) || - (*(format + 1) == SECUREC_CHAR('o')) || - (*(format + 1) == SECUREC_CHAR('x')) || - (*(format + 1) == SECUREC_CHAR('X'))) { - isInt64Arg = SecIs64BitPtr(sizeof(void *)); - break; - } - isInt64Arg = SecIs64BitPtr(sizeof(void *)); - doneFlag = 1; - break; - default: - doneFlag = 1; - break; - } /* end of switch (comChr) ... */ - } - } - - if (suppress == 0) { - /* LSD change, for gcc compile Assign arglist to arglistSave */ -#if defined(va_copy) - va_copy(arglistSave, arglist); -#elif defined(__va_copy) /* for vxworks */ - __va_copy(arglistSave, arglist); -#else - arglistSave = arglist; -#endif - argPtr = (void *)va_arg(arglist, void *); - SECUREC_CHECK_INPUT_ADDR(argPtr); - } else { - /* "argPtr = NULL" is safe, in suppress mode we don't use argPtr to store data */ - argPtr = NULL; /* doesn't matter what value we use here - we're only using it as a flag */ - } - - doneFlag = 0; - - SecUpdateWcharFlagByType(*format, &isWchar); - - comChr = *format | (SECUREC_CHAR('a') - SECUREC_CHAR('A')); /* to lowercase */ - - if (comChr != SECUREC_CHAR('n')) { - if (comChr != SECUREC_CHAR('c') && comChr != SECUREC_BRACE) { - ch = SECUREC_SKIP_SPACE_CHAR(); - } else { - ch = SECUREC_GET_CHAR(); - } - } - - if (comChr != SECUREC_CHAR('n')) { - if (ch == SECUREC_EOF) { - goto ERR_RET; - } - } - - if (widthSet == 0 || width != 0) { - if (suppress == 0 && (comChr == SECUREC_CHAR('c') || - comChr == SECUREC_CHAR('s') || - comChr == SECUREC_BRACE)) { - -#if defined(va_copy) - va_copy(arglist, arglistSave); - va_end(arglistSave); - arglistBeenCopied = 1; -#elif defined(__va_copy) /* for vxworks */ - __va_copy(arglist, arglistSave); - va_end(arglistSave); - arglistBeenCopied = 1; -#else - arglist = arglistSave; -#endif - argPtr = (void *)va_arg(arglist, void *); - SECUREC_CHECK_INPUT_ADDR(argPtr); - -#if defined(va_copy) - va_copy(arglistSave, arglist); -#elif defined(__va_copy) /* for vxworks */ - __va_copy(arglistSave, arglist); -#else - arglistSave = arglist; -#endif - /* Get the next argument - size of the array in characters */ -#ifdef SECUREC_ON_64BITS - arrayWidth = ((size_t)(va_arg(arglist, size_t))) & 0xFFFFFFFFUL; -#else /* !SECUREC_ON_64BITS */ - arrayWidth = (size_t)va_arg(arglist, size_t); -#endif - - if (arrayWidth < 1) { - - if (isWchar > 0) { - *(wchar_t UNALIGNED *)argPtr = L'\0'; - } else { - *(char *)argPtr = '\0'; - } - - goto ERR_RET; - } - - /* LSD add string maxi width protection */ - if (isWchar > 0) { - if (arrayWidth > SECUREC_WCHAR_STRING_MAX_LEN) { - goto ERR_RET; - } - } else { - /* for char *buffer */ - if (arrayWidth > SECUREC_STRING_MAX_LEN) { - goto ERR_RET; - } - } - - } - - oricomChr = comChr; - - switch (comChr) { - case SECUREC_CHAR('c'): - /* also case 'C' */ /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('s'): - /* also case 'S': */ /* fall-through */ /* FALLTHRU */ - case SECUREC_BRACE: - if (comChr == 'c') { - if (widthSet == 0) { - widthSet = 1; - width = 1; - } - } else if (comChr == 's') { - /* empty */ - } else { /* for [ */ - const SecUnsignedChar *bracketFmtPtr = (const SecUnsignedChar *)(format); -#if !(defined(SECUREC_COMPATIBLE_WIN_FORMAT)) - if (*format == SECUREC_CHAR('{')) { - goto ERR_RET; - } -#endif - /* for building "table" data */ - ++bracketFmtPtr; - - if (*bracketFmtPtr == SECUREC_CHAR('^')) { - ++bracketFmtPtr; - tableMask = (unsigned char)0xff; - } - - /* malloc when first %[ is meet for wchar version */ -#ifdef SECUREC_FOR_WCHAR - if (bracketTable == NULL) { - /* LSD the table will be freed after ERR_RET label of this function */ - bracketTable = (unsigned char *)SECUREC_MALLOC(SECUREC_BRACKET_TABLE_SIZE); - if (bracketTable == NULL) { - goto ERR_RET; - } - } -#endif - (void)memset(bracketTable, 0, (size_t)SECUREC_BRACKET_TABLE_SIZE); - - if (*bracketFmtPtr == SECUREC_CHAR(']')) { - prevChar = SECUREC_CHAR(']'); - ++bracketFmtPtr; - - bracketTable[SECUREC_CHAR(']') >> 3] = 1 << (SECUREC_CHAR(']') & 7); - - } - - while (*bracketFmtPtr != SECUREC_CHAR('\0') && *bracketFmtPtr != SECUREC_CHAR(']')) { - unsigned int tmpIndex; /* to clear warning */ - expCh = *bracketFmtPtr++; - - if (expCh != SECUREC_CHAR('-') || !prevChar || /* first char */ - *bracketFmtPtr == SECUREC_CHAR(']')) { /* last char */ - prevChar = expCh; - /* only supports wide characters with a maximum length of two bytes */ - tmpIndex = (unsigned int)(int)expCh & SECUREC_CHAR_MASK; - /* Do not use |= optimize this code, it will cause compiling warning */ - bracketTable[(tmpIndex) >> 3] = (unsigned char)(bracketTable[(tmpIndex) >> 3] | - (unsigned char)(1 << (tmpIndex & 7))); - } else { - /* for %[a-z] */ - expCh = *bracketFmtPtr++; /* get end of range */ - - if (prevChar < expCh) { /* %[a-z] */ - last = expCh; - } else { - -#if (defined(SECUREC_COMPATIBLE_WIN_FORMAT)) - /* %[z-a] */ - last = prevChar; - prevChar = expCh; -#else - prevChar = expCh; - /* only supports wide characters with a maximum length of two bytes */ - tmpIndex = (unsigned int)(int)expCh & SECUREC_CHAR_MASK; - bracketTable['-' >> 3] |= (unsigned char)(1 << ('-' & 7)); - bracketTable[(tmpIndex) >> 3] = (unsigned char)(bracketTable[(tmpIndex) >> 3] | - (unsigned char)(1 << (tmpIndex & 7))); - continue; -#endif - } - /* format %[a-\xff] last is 0xFF, condition (rnch <= last) cause dead loop */ - for (expCh = prevChar; expCh < last; ++expCh) { - /* only supports wide characters with a maximum length of two bytes */ - tmpIndex = (unsigned int)(int)expCh & SECUREC_CHAR_MASK; - bracketTable[tmpIndex >> 3] = (unsigned char)(bracketTable[tmpIndex >> 3] | - (unsigned char)(1 << (tmpIndex & 7))); - } - /* only supports wide characters with a maximum length of two bytes */ - tmpIndex = (unsigned int)(int)last & SECUREC_CHAR_MASK; - bracketTable[tmpIndex >> 3] = (unsigned char)(bracketTable[tmpIndex >> 3] | - (unsigned char)(1 << (tmpIndex & 7))); - prevChar = 0; - } - } - - if (*bracketFmtPtr == SECUREC_CHAR('\0')) { - if (arrayWidth >= sizeof(SecChar) && argPtr) { - *(SecChar *) argPtr = SECUREC_CHAR('\0'); - } - goto ERR_RET; /* trunc'd format string */ - } - format = bracketFmtPtr; - } - /* scanset completed. Now read string */ - - startPtr = argPtr; - - SECUREC_UN_GET_CHAR(ch); - - /* One element is needed for '\0' for %s & %[ */ - if (comChr != SECUREC_CHAR('c')) { - --arrayWidth; - } - while (widthSet == 0 || width-- != 0) { - - ch = SECUREC_GET_CHAR(); - if ((SECUREC_EOF != ch) && - /* char conditions */ - ((comChr == SECUREC_CHAR('c')) || - /* string conditions !isspace() */ - ((comChr == SECUREC_CHAR('s') && - (!(ch >= SECUREC_CHAR('\t') && ch <= SECUREC_CHAR('\r')) && ch != SECUREC_CHAR(' ')))) || - /* BRACKET conditions */ - ((comChr == SECUREC_BRACE) && -#ifdef SECUREC_FOR_WCHAR - bracketTable && /* only supports wide characters with a maximum length of two bytes */ - ((bracketTable[((unsigned int)(int)ch & SECUREC_CHAR_MASK) >> 3] ^ tableMask) & - (1 << (ch & 7))) -#else - ((bracketTable[((unsigned char)ch & 0xff) >> 3] ^ tableMask) & (1 << (ch & 7))) -#endif - ))) { - if (suppress == 0) { - if (arrayWidth == 0) { - /* We have exhausted the user's buffer */ - - errNoMem = 1; - break; - } - SECUREC_CHECK_INPUT_ADDR(argPtr); - -#ifdef SECUREC_FOR_WCHAR - if (isWchar > 0) { - *(wchar_t UNALIGNED *)argPtr = (wchar_t)ch; - argPtr = (wchar_t *)argPtr + 1; - --arrayWidth; - } else { - int temp = 0; - if (arrayWidth >= ((size_t)MB_CUR_MAX)) { - SECUREC_MASK_MSVC_CRT_WARNING - temp = wctomb((char *)argPtr, (wchar_t)ch); - SECUREC_END_MASK_MSVC_CRT_WARNING - } else { - char tmpBuf[SECUREC_MB_LEN + 1]; - SECUREC_MASK_MSVC_CRT_WARNING temp = wctomb(tmpBuf, (wchar_t)ch); - SECUREC_END_MASK_MSVC_CRT_WARNING - if (temp > 0 && ((size_t)(unsigned int)temp) > arrayWidth) { - errNoMem = 1; - break; - } - if (temp > 0 && ((size_t)(unsigned int)temp) <= sizeof(tmpBuf)) { - if (EOK != memcpy_s(argPtr, arrayWidth, - tmpBuf, (size_t)(unsigned int)temp)) { - errNoMem = 1; - break; - } - } - } - if (temp > 0) { - /* if wctomb error, then ignore character */ - argPtr = (char *)argPtr + temp; - arrayWidth -= (size_t)(unsigned int)temp; - } - } -#else - - if (isWchar > 0) { - wchar_t tempWchar = L'?'; /* set default char as ? */ - char temp[SECUREC_MULTI_BYTE_MAX_LEN + 1]; - temp[0] = (char)ch; - temp[1] = '\0'; -#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) - if (SecIsleadbyte(ch)) { - temp[1] = (char)SECUREC_GET_CHAR(); - temp[2] = '\0'; - } - if (mbtowc(&tempWchar, temp, sizeof(temp)) <= 0) { - /* no string termination error for tool */ - tempWchar = L'?'; - } -#else - if (SecIsleadbyte(ch)) { - int convRes = 0; - int di = 1; - /* in Linux like system, the string is encoded in UTF-8 */ - while (di < (int)MB_CUR_MAX && di < SECUREC_MULTI_BYTE_MAX_LEN) { - temp[di++] = (char)SECUREC_GET_CHAR(); - temp[di] = '\0'; - convRes = mbtowc(&tempWchar, temp, sizeof(temp)); - if (convRes > 0) { - break; /* convert succeed */ - } - } - if (convRes <= 0) { - tempWchar = L'?'; - } - } else { - if (mbtowc(&tempWchar, temp, sizeof(temp)) <= 0) { - /* no string termination error for tool */ - tempWchar = L'?'; - } - } -#endif - - *(wchar_t UNALIGNED *)argPtr = tempWchar; - /* just copy L'?' if mbtowc fails, errno is set by mbtowc */ - argPtr = (wchar_t *)argPtr + 1; - --arrayWidth; - - } else { - *(char *)argPtr = (char)ch; - argPtr = (char *)argPtr + 1; - --arrayWidth; - } -#endif - } else { - /* suppress */ - /* this is OK ,Used to identify processed data for %* ,use size_t just clear e613 */ - startPtr = (SecChar *) (size_t)1 + (size_t)argPtr; - } - } else { - SECUREC_UN_GET_CHAR(ch); - break; - } - } - - if (errNoMem != 0) { - /* In case of error, blank out the input buffer */ - if (isWchar > 0) { - if (startPtr != NULL) { - *(wchar_t UNALIGNED *)startPtr = 0; - } - } else { - if (startPtr != NULL) { - *(char *)startPtr = 0; - } - } - - goto ERR_RET; - } - - if (startPtr != argPtr) { - if (suppress == 0) { - - SECUREC_CHECK_INPUT_ADDR(argPtr); - - if (comChr != 'c') { - /* null-terminate strings */ - if (isWchar > 0) { - *(wchar_t UNALIGNED *)argPtr = L'\0'; - } else { - *(char *)argPtr = '\0'; - } - } - ++doneCount; - } - - } else { - goto ERR_RET; - } - - break; - case SECUREC_CHAR('p'): - /* make %hp same as %p */ - numberWidth = SECUREC_NUM_WIDTH_INT; -#ifdef SECUREC_ON_64BITS - isInt64Arg = 1; -#endif - /* fall-through */ - /* FALLTHRU */ - case SECUREC_CHAR('o'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('u'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('d'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('i'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('x'): - if (ch == SECUREC_CHAR('+') || ch == SECUREC_CHAR('-')) { - if (ch == SECUREC_CHAR('-')) { - numberSpec.negative = 1; - } - if (--width == 0 && widthSet != 0) { - doneFlag = 1; - } else { - ch = SECUREC_GET_CHAR(); - } - } - - if (comChr == SECUREC_CHAR('x') || comChr == SECUREC_CHAR('i')) { - if (comChr == SECUREC_CHAR('i')) { - /* i could be d, o, or x, use d as default */ - comChr = SECUREC_CHAR('d'); - } - if (ch == SECUREC_CHAR('0')) { - ch = SECUREC_GET_CHAR(); - if ((SecChar) (ch) == SECUREC_CHAR('x') || (SecChar) ch == SECUREC_CHAR('X')) { - ch = SECUREC_GET_CHAR(); - if (widthSet != 0) { - width -= 2; - if (width < 1) { - doneFlag = 1; - } - } - comChr = SECUREC_CHAR('x'); - } else { - started = 1; - if (comChr != SECUREC_CHAR('x')) { - if (widthSet != 0 && --width == 0) { - doneFlag = 1; - } - comChr = SECUREC_CHAR('o'); - } else { - SECUREC_UN_GET_CHAR(ch); - ch = SECUREC_CHAR('0'); - } - } - } - } - - /* scanNumber: */ - if (isInt64Arg != 0) { - while (doneFlag == 0) { - /* decode ch to number64 */ - doneFlag = SecDecodeNumber64(comChr, ch, &numberSpec); - - if (doneFlag == 0) { - started = 1; - - if (widthSet != 0 && --width == 0) { - doneFlag = 1; - } else { - ch = SECUREC_GET_CHAR(); - } - } else { - SECUREC_UN_GET_CHAR(ch); - } - - } /* end of WHILE loop */ - - /* Handling integer negative numbers and beyond max */ - - SecFinishNumber64(oricomChr, &numberSpec); - - } - /* do not use else , Otherwise, the vxworks55 arm926ej compiler will crash. */ - if (isInt64Arg == 0) { - while (doneFlag == 0) { - /* decode ch to number */ - doneFlag = SecDecodeNumber(comChr, ch, &numberSpec); - if (doneFlag == 0) { - started = 1; - - if (widthSet != 0 && --width == 0) { - doneFlag = 1; - } else { - ch = SECUREC_GET_CHAR(); - } - } else { - SECUREC_UN_GET_CHAR(ch); - } - } /* end of WHILE loop */ - - /* Handling integer negative numbers and beyond max */ - - SecFinishNumber(oricomChr, numberWidth, &numberSpec); - - } - - if (comChr == SECUREC_CHAR('F')) { /* expected ':' in long pointer */ - started = 0; - } - - if (started != 0) { - if (suppress == 0) { - SECUREC_CHECK_INPUT_ADDR(argPtr); - - if (isInt64Arg != 0) { -#if defined(SECUREC_VXWORKS_PLATFORM) - /* take number64 as unsigned number */ - *(SecInt64 UNALIGNED *)argPtr = *(SecUnsignedInt64 *)(&numberSpec.number64); -#else - /* take number64 as unsigned number */ - *(SecInt64 UNALIGNED *)argPtr = (SecInt64)numberSpec.number64; -#endif - } else { - SecAssignNumber(argPtr, numberWidth, numberSpec.number); - } - ++doneCount; - } - /* remove blank else */ - } else { - goto ERR_RET; - } - break; - - case SECUREC_CHAR('n'): /* char count */ - if (suppress == 0) { - SECUREC_CHECK_INPUT_ADDR(argPtr); - SecAssignNumber(argPtr, numberWidth, (unsigned long)(unsigned int)charCount); - } - break; - - case SECUREC_CHAR('e'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('f'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('g'): /* scan a float */ - - floatStrUsedLen = 0; - - if (ch == SECUREC_CHAR('-')) { - pFloatStr[floatStrUsedLen++] = SECUREC_CHAR('-'); - --width; - ch = SECUREC_GET_CHAR(); - - } else if (ch == SECUREC_CHAR('+')) { - --width; - ch = SECUREC_GET_CHAR(); - } - - if (widthSet == 0) { /* must care width */ - width = -1; - } - - /* now get integral part */ - while (SECUREC_IS_DIGIT(ch) && width-- != 0) { - started = 1; - pFloatStr[floatStrUsedLen++] = (SecChar) ch; /* ch must be '0' - '9' */ - if (SecUpdateFloatString(floatStrUsedLen, - &floatStrSize, &pFloatStr, floatStrBuffer, &pAllocedFloatStr) == 0) { - goto ERR_RET; - } - ch = SECUREC_GET_CHAR(); - } - -#ifdef SECUREC_FOR_WCHAR - /* convert decimal point(.) to wide-char */ - decimal = L'.'; - if (mbtowc(&decimal, SECUREC_DECIMAL_POINT_PTR, (size_t)MB_CUR_MAX) <= 0) { - decimal = L'.'; - } -#else - decimal = *SECUREC_DECIMAL_POINT_PTR; /* if locale.h NOT exist, let decimal = '.' */ -#endif - - if (SecCheckFloatDecPoint((char)decimal) != 1) { - goto ERR_RET; - } - - /* now check for decimal */ - if (decimal == (char)ch && width-- != 0) { - ch = SECUREC_GET_CHAR(); - pFloatStr[floatStrUsedLen++] = decimal; - if (SecUpdateFloatString(floatStrUsedLen, - &floatStrSize, &pFloatStr, floatStrBuffer, &pAllocedFloatStr) == 0) { - goto ERR_RET; - } - while (SECUREC_IS_DIGIT(ch) && width-- != 0) { - started = 1; - pFloatStr[floatStrUsedLen++] = (SecChar) ch; - if (SecUpdateFloatString(floatStrUsedLen, - &floatStrSize, - &pFloatStr, floatStrBuffer, &pAllocedFloatStr) == 0) { - goto ERR_RET; - } - ch = SECUREC_GET_CHAR(); - } - } - - /* now check for exponent */ - - if (started != 0 && (ch == SECUREC_CHAR('e') || ch == SECUREC_CHAR('E')) && width-- != 0) { - pFloatStr[floatStrUsedLen++] = SECUREC_CHAR('e'); - if (SecUpdateFloatString(floatStrUsedLen, - &floatStrSize, &pFloatStr, floatStrBuffer, &pAllocedFloatStr) == 0) { - goto ERR_RET; - } - - ch = SECUREC_GET_CHAR(); - if (ch == SECUREC_CHAR('+') || ch == SECUREC_CHAR('-')) { - if (ch == SECUREC_CHAR('-')) { - - pFloatStr[floatStrUsedLen++] = SECUREC_CHAR('-'); - if (SecUpdateFloatString(floatStrUsedLen, - &floatStrSize, - &pFloatStr, floatStrBuffer, &pAllocedFloatStr) == 0) { - goto ERR_RET; - } - } - - if (width != 0) { - ch = SECUREC_GET_CHAR(); - --width; - } - } - - while (SECUREC_IS_DIGIT(ch) && width-- != 0) { - pFloatStr[floatStrUsedLen++] = (SecChar) ch; - if (SecUpdateFloatString(floatStrUsedLen, - &floatStrSize, - &pFloatStr, floatStrBuffer, &pAllocedFloatStr) == 0) { - goto ERR_RET; - } - ch = SECUREC_GET_CHAR(); - } - - } - - SECUREC_UN_GET_CHAR(ch); - - if (started != 0) { - if (suppress == 0) { - SECUREC_CHECK_INPUT_ADDR(argPtr); - - /* Make sure have a string terminator */ - pFloatStr[floatStrUsedLen] = SECUREC_CHAR('\0'); -#ifdef SECUREC_FOR_WCHAR - { - /* convert float string */ - size_t mbsLen; - size_t tempFloatStrLen = (size_t)(floatStrSize + 1) * sizeof(wchar_t); - char *tempFloatStr = (char *)SECUREC_MALLOC(tempFloatStrLen); - - if (tempFloatStr == NULL) { - goto ERR_RET; - } - tempFloatStr[0] = '\0'; - SECUREC_MASK_MSVC_CRT_WARNING - mbsLen = wcstombs(tempFloatStr, pFloatStr, tempFloatStrLen - 1); - SECUREC_END_MASK_MSVC_CRT_WARNING - if (mbsLen != (size_t)-1) { - tempFloatStr[mbsLen] = '\0'; - SecAssignFloat(numberWidth, argPtr, tempFloatStr); - } else { - SECUREC_FREE(tempFloatStr); - goto ERR_RET; - } - SECUREC_FREE(tempFloatStr); - } -#else - SecAssignFloat(numberWidth, argPtr, pFloatStr); -#endif - ++doneCount; - } - /* remove blank else */ /* NULL */ - } else { - goto ERR_RET; - } - - break; - default: - if ((int)*format != (int)ch) { - SECUREC_UN_GET_CHAR(ch); - /* to clear e438 last value assigned not used , the compiler will optimize this code */ - (void)charCount; - formatError = 1; - goto ERR_RET; - } else { - --match; - } - - if (suppress == 0) { -#if defined(va_copy) - va_copy(arglist, arglistSave); - arglistBeenCopied = 1; - va_end(arglistSave); -#elif defined(__va_copy) /* for vxworks */ - __va_copy(arglist, arglistSave); - arglistBeenCopied = 1; - va_end(arglistSave); -#else - arglist = arglistSave; -#endif - } - } - - ++match; - - } - - else { - /* 0 width in format */ - SECUREC_UN_GET_CHAR(ch); - /* to clear e438 last value assigned not used , the compiler will optimize this code */ - (void)charCount; - goto ERR_RET; - } - - ++format; - } else { - ch = SECUREC_GET_CHAR(); - if ((int)*format++ != (int)(ch)) { - SECUREC_UN_GET_CHAR(ch); - /* to clear e438 last value assigned not used , the compiler will optimize this code */ - (void)charCount; - goto ERR_RET; - } -#ifndef SECUREC_FOR_WCHAR - /* The purpose of type conversion is to avoid warnings */ - if (SecIsleadbyte(ch)) { - char temp[SECUREC_MULTI_BYTE_MAX_LEN]; - wchar_t tempWchar = L'\0'; - int ch2 = SECUREC_GET_CHAR(); - - if ((int)*format++ != (ch2)) { - SECUREC_UN_GET_CHAR(ch2); /* LSD in console mode, ungetc twice will cause problem */ - SECUREC_UN_GET_CHAR(ch); - /* to clear e438 last value assigned not used , the compiler will optimize this code */ - (void)charCount; - goto ERR_RET; - } - if (MB_CUR_MAX > 2 && (((unsigned char)ch & 0xE0) == 0xE0) && (((unsigned char)ch2 & 0x80) == 0x80)) { - /* this char is very likely to be a UTF-8 char */ - int ch3 = SECUREC_GET_CHAR(); - temp[0] = (char)ch; - temp[1] = (char)ch2; - temp[2] = (char)ch3; - temp[3] = '\0'; - - if (mbtowc(&tempWchar, temp, sizeof(temp)) > 0) { - /* succeed */ - if ((int)*format++ != (int)ch3) { - SECUREC_UN_GET_CHAR(ch3); - /* to clear e438 last value assigned not used , the compiler will optimize this code */ - (void)charCount; - goto ERR_RET; - } - --charCount; - } else { - SECUREC_UN_GET_CHAR(ch3); - } - } - --charCount; /* only count as one character read */ - } -#endif /* SECUREC_FOR_WCHAR */ - } - - if ((ch == SECUREC_EOF) && ((*format != SECUREC_CHAR('%')) || (*(format + 1) != SECUREC_CHAR('n')))) { - break; - } - - } - -ERR_RET: -#ifdef SECUREC_FOR_WCHAR - if (bracketTable != NULL) { - SECUREC_FREE(bracketTable); - bracketTable = NULL; - (void)bracketTable; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - } -#endif - -#if defined(va_copy) || defined(__va_copy) - if (arglistBeenCopied != 0) { - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - } -#endif - va_end(arglistSave); - (void)arglistSave; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - /* LSD 2014.3.6 add, clear the stack data */ - if (memset_s(floatStrBuffer, sizeof(floatStrBuffer), 0, sizeof(floatStrBuffer)) != EOK) { - doneCount = 0; /* This is a dead code, just to meet the coding requirements */ - } - if (pAllocedFloatStr != NULL) { - /* pFloatStr can be allocated in SecUpdateFloatString function, clear and free it */ - if (memset_s(pAllocedFloatStr, floatStrSize * sizeof(SecChar), 0, floatStrSize * sizeof(SecChar)) != EOK) { - doneCount = 0; /* This is a dead code, just to meet the coding requirements */ - } - SECUREC_FREE(pAllocedFloatStr); - pAllocedFloatStr = NULL; - (void)pAllocedFloatStr; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - } - - SecAdjustStream(stream); - - if (ch == SECUREC_EOF) { - return ((doneCount || match) ? doneCount : EOF); - } else if (formatError != 0 || paraIsNull != 0) { - /* Invalid Input Format or parameter */ - return SECUREC_SCANF_ERROR_PARA; - } - - return doneCount; -} - -#ifdef SECUREC_FOR_WCHAR -static SecInt SecGetCharW(SecFileStream *str) -#else -static SecInt SecGetChar(SecFileStream *str) -#endif -{ - - SecInt ch = 0; - int firstReadOnFile = 0; - do { - if ((str->flag & SECUREC_FROM_STDIN_FLAG) > 0) { -#if defined(SECUREC_NO_STD_UNGETC) - if (str->fUnget == 1) { - ch = (SecInt) str->lastChar; - str->fUnget = 0; - - } else { - ch = SECUREC_GETC(str->pf); - str->lastChar = (unsigned int)ch; - - } -#else - ch = SECUREC_GETC(str->pf); -#endif - break; - } else if ((str->flag & SECUREC_FILE_STREAM_FLAG) > 0 && str->count == 0) { - /* load file to buffer */ - if (str->base == NULL) { - str->base = (char *)SECUREC_MALLOC(SECUREC_BUFFERED_BLOK_SIZE + 1); - if (str->base == NULL) { - ch = SECUREC_EOF; - break; - } - str->base[SECUREC_BUFFERED_BLOK_SIZE] = '\0'; /* for tool Warning string null */ - } - /* LSD add 2014.3.21 */ - if (str->oriFilePos == SECUREC_UNINITIALIZED_FILE_POS) { - str->oriFilePos = ftell(str->pf); /* save original file read position */ - firstReadOnFile = 1; - } - str->count = (int)fread(str->base, (size_t)1, (size_t)SECUREC_BUFFERED_BLOK_SIZE, str->pf); - str->base[SECUREC_BUFFERED_BLOK_SIZE] = '\0'; /* for tool Warning string null */ - if (str->count == 0 || str->count > SECUREC_BUFFERED_BLOK_SIZE) { - ch = SECUREC_EOF; - break; - } - str->cur = str->base; - str->flag |= SECUREC_LOAD_FILE_TO_MEM_FLAG; - if (firstReadOnFile != 0) { -#ifdef SECUREC_FOR_WCHAR - - if (str->count > 1 - && (((unsigned char)(str->base[0]) == 0xFFU && (unsigned char)(str->base[1]) == 0xFEU) - || ((unsigned char)(str->base[0]) == 0xFEU && (unsigned char)(str->base[1]) == 0xFFU))) { - /* it's BOM header, UNICODE little endian */ - str->count -= SECUREC_BOM_HEADER_SIZE; - if (memmove_s(str->base, (size_t)SECUREC_BUFFERED_BLOK_SIZE, - str->base + SECUREC_BOM_HEADER_SIZE, (size_t)(unsigned int)str->count) != EOK) { - ch = SECUREC_EOF; - break; - } - - if (str->count % (int)sizeof(SecChar)) { - /* the str->count must be a multiple of sizeof(SecChar), - * otherwise this function will return SECUREC_EOF when read the last character - */ - int ret = (int)fread(str->base + str->count, (size_t)1, - (size_t)SECUREC_BOM_HEADER_SIZE, str->pf); - if (ret > 0 && ret <= SECUREC_BUFFERED_BLOK_SIZE) { - str->count += ret; - } - } - } - -#else - if (str->count > 2 && (unsigned char)(str->base[0]) == 0xEFU && - (unsigned char)(str->base[1]) == 0xBBU && (unsigned char)(str->base[2]) == 0xBFU) { - /* it's BOM header, little endian */ - str->count -= SECUREC_UTF8_BOM_HEADER_SIZE; - str->cur += SECUREC_UTF8_BOM_HEADER_SIZE; - } -#endif - } - } - - if ((str->flag & SECUREC_MEM_STR_FLAG) > 0 || (str->flag & SECUREC_LOAD_FILE_TO_MEM_FLAG) > 0) { - /* according wchar_t has two bytes */ - ch = (SecInt)((str->count -= (int)sizeof(SecChar)) >= 0 ? \ - (SecInt) (SECUREC_CHAR_MASK & *((const SecChar *)(const void *)str->cur)) : SECUREC_EOF); - str->cur += sizeof(SecChar); - } - /* use break in do-while to skip some code */ - } SECUREC_WHILE_ZERO; - - - if (ch != SECUREC_EOF && (str->flag & SECUREC_FILE_STREAM_FLAG) > 0 && str->base) { - str->fileRealRead += (int)sizeof(SecChar); - } - return ch; - -} - -static void SecUnGetCharImpl(SecInt chr, SecFileStream *str) -{ - if ((str->flag & SECUREC_FROM_STDIN_FLAG) > 0) { -#if defined(SECUREC_NO_STD_UNGETC) - str->lastChar = (unsigned int)chr; - str->fUnget = 1; -#else - (void)SECUREC_UN_GETC(chr, str->pf); -#endif - } else if ((str->flag & SECUREC_MEM_STR_FLAG) || (str->flag & SECUREC_LOAD_FILE_TO_MEM_FLAG) > 0) { - if (str->cur > str->base) { - str->cur -= sizeof(SecChar); - str->count += (int)sizeof(SecChar); - } - } - - if ((str->flag & SECUREC_FILE_STREAM_FLAG) > 0 && str->base) { - /* LSD fix, change from -- str->fileRealRead to str->fileRealRead -= sizeof(SecChar). 2014.2.21 */ - str->fileRealRead -= (int)sizeof(SecChar); - } - -} - -#ifdef SECUREC_FOR_WCHAR -static void SecUnGetCharW(SecInt chr, SecFileStream *str) -#else -static void SecUnGetChar(SecInt chr, SecFileStream *str) -#endif -{ - if (chr != SECUREC_EOF) { - SecUnGetCharImpl(chr, str); - } -} - -static SecInt SecSkipSpaceChar(int *counter, SecFileStream *fileptr) -{ - SecInt ch; - - do { - ++(*counter); -#ifdef SECUREC_FOR_WCHAR - ch = SecGetCharW(fileptr); -#else - ch = SecGetChar(fileptr); -#endif - if (ch == SECUREC_EOF) { - break; - } - } -#ifdef SECUREC_FOR_WCHAR - while (iswspace((wint_t) ch)); -#else - while (isspace((SecUnsignedChar) ch)); -#endif - return ch; -} - -#endif /* __INPUT_INL__5D13A042_DC3F_4ED9_A8D1_882811274C27 */ - - diff --git a/base/src/securec/input.inl b/base/src/securec/input.inl new file mode 120000 index 0000000..092c8f6 --- /dev/null +++ b/base/src/securec/input.inl @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/input.inl \ No newline at end of file diff --git a/base/src/securec/memcpy_s.c b/base/src/securec/memcpy_s.c deleted file mode 100755 index bf4e271..0000000 --- a/base/src/securec/memcpy_s.c +++ /dev/null @@ -1,327 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions] Use unsafe function: Portability - * [reason] Use unsafe function to implement security function to maintain platform compatibility. - * And sufficient input validation is performed before calling - */ - -#include "securecutil.h" - -#ifndef SECUREC_MEMCOPY_WITH_PERFORMANCE -#define SECUREC_MEMCOPY_WITH_PERFORMANCE 0 -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS || SECUREC_MEMCOPY_WITH_PERFORMANCE -#ifndef SECUREC_MEMCOPY_THRESHOLD_SIZE -#define SECUREC_MEMCOPY_THRESHOLD_SIZE (64UL) -#endif - -#define SECUREC_COPY_STRUCT(num) case num:*(SecStrBuf##num *)dest=*(const SecStrBuf##num *) src;break; - -#define SECUREC_SMALL_MEM_COPY do { \ - if (SECUREC_ADDR_ALIGNED_8(dest) && SECUREC_ADDR_ALIGNED_8(src)) { \ - /* use struct assignment */ \ - switch (count) { \ - SECUREC_COPY_STRUCT(1) \ - SECUREC_COPY_STRUCT(2) \ - SECUREC_COPY_STRUCT(3) \ - SECUREC_COPY_STRUCT(4) \ - SECUREC_COPY_STRUCT(5) \ - SECUREC_COPY_STRUCT(6) \ - SECUREC_COPY_STRUCT(7) \ - SECUREC_COPY_STRUCT(8) \ - SECUREC_COPY_STRUCT(9) \ - SECUREC_COPY_STRUCT(10) \ - SECUREC_COPY_STRUCT(11) \ - SECUREC_COPY_STRUCT(12) \ - SECUREC_COPY_STRUCT(13) \ - SECUREC_COPY_STRUCT(14) \ - SECUREC_COPY_STRUCT(15) \ - SECUREC_COPY_STRUCT(16) \ - SECUREC_COPY_STRUCT(17) \ - SECUREC_COPY_STRUCT(18) \ - SECUREC_COPY_STRUCT(19) \ - SECUREC_COPY_STRUCT(20) \ - SECUREC_COPY_STRUCT(21) \ - SECUREC_COPY_STRUCT(22) \ - SECUREC_COPY_STRUCT(23) \ - SECUREC_COPY_STRUCT(24) \ - SECUREC_COPY_STRUCT(25) \ - SECUREC_COPY_STRUCT(26) \ - SECUREC_COPY_STRUCT(27) \ - SECUREC_COPY_STRUCT(28) \ - SECUREC_COPY_STRUCT(29) \ - SECUREC_COPY_STRUCT(30) \ - SECUREC_COPY_STRUCT(31) \ - SECUREC_COPY_STRUCT(32) \ - SECUREC_COPY_STRUCT(33) \ - SECUREC_COPY_STRUCT(34) \ - SECUREC_COPY_STRUCT(35) \ - SECUREC_COPY_STRUCT(36) \ - SECUREC_COPY_STRUCT(37) \ - SECUREC_COPY_STRUCT(38) \ - SECUREC_COPY_STRUCT(39) \ - SECUREC_COPY_STRUCT(40) \ - SECUREC_COPY_STRUCT(41) \ - SECUREC_COPY_STRUCT(42) \ - SECUREC_COPY_STRUCT(43) \ - SECUREC_COPY_STRUCT(44) \ - SECUREC_COPY_STRUCT(45) \ - SECUREC_COPY_STRUCT(46) \ - SECUREC_COPY_STRUCT(47) \ - SECUREC_COPY_STRUCT(48) \ - SECUREC_COPY_STRUCT(49) \ - SECUREC_COPY_STRUCT(50) \ - SECUREC_COPY_STRUCT(51) \ - SECUREC_COPY_STRUCT(52) \ - SECUREC_COPY_STRUCT(53) \ - SECUREC_COPY_STRUCT(54) \ - SECUREC_COPY_STRUCT(55) \ - SECUREC_COPY_STRUCT(56) \ - SECUREC_COPY_STRUCT(57) \ - SECUREC_COPY_STRUCT(58) \ - SECUREC_COPY_STRUCT(59) \ - SECUREC_COPY_STRUCT(60) \ - SECUREC_COPY_STRUCT(61) \ - SECUREC_COPY_STRUCT(62) \ - SECUREC_COPY_STRUCT(63) \ - SECUREC_COPY_STRUCT(64) \ - default:break; \ - } /* END switch */ \ - } else { \ - char *tmpDest = (char *)dest; \ - const char *tmpSrc = (const char *)src; \ - switch (count) { \ - case 64: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 63: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 62: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 61: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 60: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 59: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 58: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 57: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 56: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 55: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 54: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 53: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 52: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 51: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 50: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 49: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 48: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 47: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 46: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 45: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 44: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 43: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 42: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 41: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 40: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 39: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 38: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 37: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 36: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 35: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 34: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 33: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 32: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 31: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 30: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 29: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 28: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 27: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 26: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 25: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 24: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 23: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 22: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 21: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 20: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 19: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 18: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 17: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 16: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 15: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 14: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 13: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 12: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 11: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 10: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 9: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 8: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 7: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 6: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 5: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 4: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 3: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 2: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - case 1: *tmpDest++ = *tmpSrc++; /* fall-through */ /* FALLTHRU */ \ - default:break; \ - } \ - } \ - } SECUREC_WHILE_ZERO -#endif - -static errno_t SecMemcpyError(void *dest, size_t destMax, const void *src, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_MEM_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("memcpy_s"); - return ERANGE; - } - if (dest == NULL || src == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("memcpy_s"); - if (dest != NULL) { - (void)memset(dest, 0, destMax); - return EINVAL_AND_RESET; - } - return EINVAL; - } - if (count > destMax) { - (void)memset(dest, 0, destMax); - SECUREC_ERROR_INVALID_RANGE("memcpy_s"); - return ERANGE_AND_RESET; - } - if (dest == src) { - return EOK; - } - if ((dest > src && dest < (const void *)((const unsigned char *)src + count)) || - (src > dest && src < (void *)((unsigned char *)dest + count))) { - (void)memset(dest, 0, destMax); - SECUREC_ERROR_BUFFER_OVERLAP("memcpy_s"); - return EOVERLAP_AND_RESET; - } - /* count == 0 also return EOK */ - return EOK; -} - -#if SECUREC_WITH_PERFORMANCE_ADDONS || SECUREC_MEMCOPY_WITH_PERFORMANCE -static void SecDoMemcpyOpt(void *dest, const void *src, size_t count) -{ - if (count > SECUREC_MEMCOPY_THRESHOLD_SIZE) { -/* - * if SECUREC_USE_ASM macro is enabled, it will call assembly language function to improve performance. - */ -#ifdef SECUREC_USE_ASM - (void)memcpy_opt(dest, src, count); -#else - /* large enough, let system API do it */ - (void)memcpy(dest, src, count); -#endif - - } else { - SECUREC_SMALL_MEM_COPY; - } - return; -} -#endif - -/******************************************************************************* - * - * The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest - * - * - * dest Destination buffer. - * destMax Size of the destination buffer. - * src Buffer to copy from. - * count Number of characters to copy - * - * - * dest buffer is updated. - * - * - * EOK Success - * EINVAL dest is NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN - * EINVAL_AND_RESET dest != NULL and src is NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN - * ERANGE destMax > SECUREC_MEM_MAX_LEN or destMax is 0 - * ERANGE_AND_RESET count > destMax and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN - * and dest != NULL and src != NULL - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and - * count <= destMax destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN and dest != NULL - * and src != NULL and dest != src - * - * if an error occurred, dest will be filled with 0. - * If the source and destination overlap, the behavior of memcpy_s is undefined. - * Use memmove_s to handle overlapping regions. - ******************************************************************************* - */ -errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count) -{ -#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) - /* fread API in windows will call memcpy_s and pass 0xffffffff to destMax. - * To avoid the failure of fread, we don't check desMax limit. - */ - if (SECUREC_LIKELY(count <= destMax && dest != NULL && src != NULL && - count > 0 && - ((dest > src && (void *)((const unsigned char *)src + count) <= dest) || - (src > dest && (void *)((unsigned char *)dest + count) <= src)))) { -#else - if (SECUREC_LIKELY(count <= destMax && dest != NULL && src != NULL && - destMax <= SECUREC_MEM_MAX_LEN && - count > 0 && - ((dest > src && (const void *)((const unsigned char *)src + count) <= dest) || - (src > dest && (void *)((unsigned char *)dest + count) <= src)))) { -#endif -#if SECUREC_MEMCOPY_WITH_PERFORMANCE - SecDoMemcpyOpt(dest, src, count); -#else -#ifdef SECUREC_USE_ASM - (void)memcpy_opt(dest, src, count); -#else - (void)memcpy(dest, src, count); -#endif -#endif - return EOK; - } - - /* meet some runtime violation, return error code */ - return SecMemcpyError(dest, destMax, src, count); - -} - -#if SECUREC_IN_KERNEL -EXPORT_SYMBOL(memcpy_s); -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - -errno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count) -{ - if (SECUREC_LIKELY(count <= destMax && dest != NULL && src != NULL && - destMax <= SECUREC_MEM_MAX_LEN && - count > 0 && - ((dest > src && (const void *)((const unsigned char *)src + count) <= dest) || - (src > dest && (const void *)((const unsigned char *)dest + count) <= src)))) { - SecDoMemcpyOpt(dest, src, count); - return EOK; - } - /* meet some runtime violation, return error code */ - return SecMemcpyError(dest, destMax, src, count); -} - -/* trim judgement on "destMax <= SECUREC_MEM_MAX_LEN" */ -errno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count) -{ - if (SECUREC_LIKELY(count <= destMax && dest != NULL && src != NULL && - count > 0 && - ((dest > src && (const void *)((const unsigned char *)src + count) <= dest) || - (src > dest && (void *)((unsigned char *)dest + count) <= src)))) { - SecDoMemcpyOpt(dest, src, count); - return EOK; - } - /* meet some runtime violation, return error code */ - return SecMemcpyError(dest, destMax, src, count); -} -#endif - diff --git a/base/src/securec/memcpy_s.c b/base/src/securec/memcpy_s.c new file mode 120000 index 0000000..39749ea --- /dev/null +++ b/base/src/securec/memcpy_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/memcpy_s.c \ No newline at end of file diff --git a/base/src/securec/memmove_s.c b/base/src/securec/memmove_s.c deleted file mode 100755 index ac2c859..0000000 --- a/base/src/securec/memmove_s.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions] Use unsafe function: Portability - * [reason] Use unsafe function to implement security function to maintain platform compatibility. - * And sufficient input validation is performed before calling - */ - -#include "securecutil.h" - -#ifdef SECUREC_NOT_CALL_LIBC_CORE_API -static void SecUtilMemmove(void *dst, const void *src, size_t count) -{ - unsigned char *pDest = (unsigned char *)dst; - const unsigned char *pSrc = (const unsigned char *)src; - size_t maxCount = count; - - if (dst <= src || pDest >= (pSrc + maxCount)) { - /* - * Non-Overlapping Buffers - * copy from lower addresses to higher addresses - */ - while (maxCount--) { - *pDest = *pSrc; - ++pDest; - ++pSrc; - } - } else { - /* - * Overlapping Buffers - * copy from higher addresses to lower addresses - */ - pDest = pDest + maxCount - 1; - pSrc = pSrc + maxCount - 1; - - while (maxCount--) { - *pDest = *pSrc; - - --pDest; - --pSrc; - } - } -} -#endif - -/******************************************************************************* - * - * Copies count bytes of characters from src to dest. - * - * - * dest Destination object. - * destMax Size of the destination buffer. - * src Source object. - * count Number of characters to copy. - * - * - * dest buffer is updated. - * - * - * EOK Success - * EINVAL dest is NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN - * EINVAL_AND_RESET dest != NULL and src is NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN - * ERANGE destMax > SECUREC_MEM_MAX_LEN or destMax is 0 - * ERANGE_AND_RESET count > destMax and dest != NULL and src != NULL and destMax != 0 - * and destMax <= SECUREC_MEM_MAX_LEN - * - * If an error occurred, dest will be filled with 0 when dest and destMax valid. - * If some regions of the source area and the destination overlap, memmove_s - * ensures that the original source bytes in the overlapping region are copied - * before being overwritten. - ******************************************************************************* - */ -errno_t memmove_s(void *dest, size_t destMax, const void *src, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_MEM_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("memmove_s"); - return ERANGE; - } - if (dest == NULL || src == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("memmove_s"); - if (dest != NULL) { - (void)memset(dest, 0, destMax); - return EINVAL_AND_RESET; - } - return EINVAL; - } - if (count > destMax) { - (void)memset(dest, 0, destMax); - SECUREC_ERROR_INVALID_RANGE("memmove_s"); - return ERANGE_AND_RESET; - } - if (dest == src) { - return EOK; - } - - if (count > 0) { -#ifdef SECUREC_NOT_CALL_LIBC_CORE_API - SecUtilMemmove(dest, src, count); -#else - /* use underlying memmove for performance consideration */ - (void)memmove(dest, src, count); -#endif - } - return EOK; -} - -#if SECUREC_IN_KERNEL -EXPORT_SYMBOL(memmove_s); -#endif - diff --git a/base/src/securec/memmove_s.c b/base/src/securec/memmove_s.c new file mode 120000 index 0000000..1ba502c --- /dev/null +++ b/base/src/securec/memmove_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/memmove_s.c \ No newline at end of file diff --git a/base/src/securec/memset_s.c b/base/src/securec/memset_s.c deleted file mode 100755 index f915589..0000000 --- a/base/src/securec/memset_s.c +++ /dev/null @@ -1,322 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions] Use unsafe function: Portability - * [reason] Use unsafe function to implement security function to maintain platform compatibility. - * And sufficient input validation is performed before calling - */ - -#include "securecutil.h" - -#ifndef SECUREC_MEMSET_WITH_PERFORMANCE -#define SECUREC_MEMSET_WITH_PERFORMANCE 0 -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS || SECUREC_MEMSET_WITH_PERFORMANCE - -/* use union to clear strict-aliasing warning */ -typedef union { - SecStrBuf32 buf32; - SecStrBuf31 buf31; - SecStrBuf30 buf30; - SecStrBuf29 buf29; - SecStrBuf28 buf28; - SecStrBuf27 buf27; - SecStrBuf26 buf26; - SecStrBuf25 buf25; - SecStrBuf24 buf24; - SecStrBuf23 buf23; - SecStrBuf22 buf22; - SecStrBuf21 buf21; - SecStrBuf20 buf20; - SecStrBuf19 buf19; - SecStrBuf18 buf18; - SecStrBuf17 buf17; - SecStrBuf16 buf16; - SecStrBuf15 buf15; - SecStrBuf14 buf14; - SecStrBuf13 buf13; - SecStrBuf12 buf12; - SecStrBuf11 buf11; - SecStrBuf10 buf10; - SecStrBuf9 buf9; - SecStrBuf8 buf8; - SecStrBuf7 buf7; - SecStrBuf6 buf6; - SecStrBuf5 buf5; - SecStrBuf4 buf4; - SecStrBuf3 buf3; - SecStrBuf2 buf2; - SecStrBuf1 buf1; -} SecStrBuf32Union; -/* C standard initializes the first member of the consortium. */ -static const SecStrBuf32 g_allZero = {{'\0','\0','\0','\0','\0','\0','\0','\0', - '\0','\0','\0','\0','\0','\0','\0','\0', - '\0','\0','\0','\0','\0','\0','\0','\0', - '\0','\0','\0','\0','\0','\0','\0','\0'}}; -static const SecStrBuf32 g_allFF = {{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, - 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, - 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, - 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}}; - -static const SecStrBuf32Union *SecStrictAliasingCast(const SecStrBuf32 *buf) -{ - return (const SecStrBuf32Union *)buf; -} - -#ifndef SECUREC_MEMSET_THRESHOLD_SIZE -#define SECUREC_MEMSET_THRESHOLD_SIZE (32UL) -#endif - -#define SECUREC_COPY_STRUCT_4_ZERO(num) \ - case num: \ - *(SecStrBuf##num *)dest = *(const SecStrBuf##num *)(&((SecStrictAliasingCast(&g_allZero))->buf##num)); \ - break; - -#define SECUREC_COPY_STRUCT_4_FF(num) \ - case num: \ - *(SecStrBuf##num *)dest = *(const SecStrBuf##num *)(&((SecStrictAliasingCast(&g_allFF))->buf##num)); \ - break; - -#define SECUREC_UNALIGNED_SET do { \ - char *pcDest = (char *)dest; \ - switch (count) { \ - case 32: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 31: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 30: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 29: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 28: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 27: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 26: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 25: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 24: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 23: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 22: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 21: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 20: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 19: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 18: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 17: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 16: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 15: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 14: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 13: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 12: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 11: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 10: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 9: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 8: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 7: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 6: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 5: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 4: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 3: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 2: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - case 1: *pcDest++ = (char)c; /* fall-through */ /* FALLTHRU */ \ - default:break; \ - } \ - } SECUREC_WHILE_ZERO - -#define SECUREC_ALIGNED_SET_OPT_ZERO_FF do { \ - switch (c) { \ - case 0: \ - switch (count) { \ - SECUREC_COPY_STRUCT_4_ZERO(1) \ - SECUREC_COPY_STRUCT_4_ZERO(2) \ - SECUREC_COPY_STRUCT_4_ZERO(3) \ - SECUREC_COPY_STRUCT_4_ZERO(4) \ - SECUREC_COPY_STRUCT_4_ZERO(5) \ - SECUREC_COPY_STRUCT_4_ZERO(6) \ - SECUREC_COPY_STRUCT_4_ZERO(7) \ - SECUREC_COPY_STRUCT_4_ZERO(8) \ - SECUREC_COPY_STRUCT_4_ZERO(9) \ - SECUREC_COPY_STRUCT_4_ZERO(10) \ - SECUREC_COPY_STRUCT_4_ZERO(11) \ - SECUREC_COPY_STRUCT_4_ZERO(12) \ - SECUREC_COPY_STRUCT_4_ZERO(13) \ - SECUREC_COPY_STRUCT_4_ZERO(14) \ - SECUREC_COPY_STRUCT_4_ZERO(15) \ - SECUREC_COPY_STRUCT_4_ZERO(16) \ - SECUREC_COPY_STRUCT_4_ZERO(17) \ - SECUREC_COPY_STRUCT_4_ZERO(18) \ - SECUREC_COPY_STRUCT_4_ZERO(19) \ - SECUREC_COPY_STRUCT_4_ZERO(20) \ - SECUREC_COPY_STRUCT_4_ZERO(21) \ - SECUREC_COPY_STRUCT_4_ZERO(22) \ - SECUREC_COPY_STRUCT_4_ZERO(23) \ - SECUREC_COPY_STRUCT_4_ZERO(24) \ - SECUREC_COPY_STRUCT_4_ZERO(25) \ - SECUREC_COPY_STRUCT_4_ZERO(26) \ - SECUREC_COPY_STRUCT_4_ZERO(27) \ - SECUREC_COPY_STRUCT_4_ZERO(28) \ - SECUREC_COPY_STRUCT_4_ZERO(29) \ - SECUREC_COPY_STRUCT_4_ZERO(30) \ - SECUREC_COPY_STRUCT_4_ZERO(31) \ - SECUREC_COPY_STRUCT_4_ZERO(32) \ - default:break; \ - } \ - break; \ - case 0xFF: \ - switch (count) { \ - SECUREC_COPY_STRUCT_4_FF(1) \ - SECUREC_COPY_STRUCT_4_FF(2) \ - SECUREC_COPY_STRUCT_4_FF(3) \ - SECUREC_COPY_STRUCT_4_FF(4) \ - SECUREC_COPY_STRUCT_4_FF(5) \ - SECUREC_COPY_STRUCT_4_FF(6) \ - SECUREC_COPY_STRUCT_4_FF(7) \ - SECUREC_COPY_STRUCT_4_FF(8) \ - SECUREC_COPY_STRUCT_4_FF(9) \ - SECUREC_COPY_STRUCT_4_FF(10) \ - SECUREC_COPY_STRUCT_4_FF(11) \ - SECUREC_COPY_STRUCT_4_FF(12) \ - SECUREC_COPY_STRUCT_4_FF(13) \ - SECUREC_COPY_STRUCT_4_FF(14) \ - SECUREC_COPY_STRUCT_4_FF(15) \ - SECUREC_COPY_STRUCT_4_FF(16) \ - SECUREC_COPY_STRUCT_4_FF(17) \ - SECUREC_COPY_STRUCT_4_FF(18) \ - SECUREC_COPY_STRUCT_4_FF(19) \ - SECUREC_COPY_STRUCT_4_FF(20) \ - SECUREC_COPY_STRUCT_4_FF(21) \ - SECUREC_COPY_STRUCT_4_FF(22) \ - SECUREC_COPY_STRUCT_4_FF(23) \ - SECUREC_COPY_STRUCT_4_FF(24) \ - SECUREC_COPY_STRUCT_4_FF(25) \ - SECUREC_COPY_STRUCT_4_FF(26) \ - SECUREC_COPY_STRUCT_4_FF(27) \ - SECUREC_COPY_STRUCT_4_FF(28) \ - SECUREC_COPY_STRUCT_4_FF(29) \ - SECUREC_COPY_STRUCT_4_FF(30) \ - SECUREC_COPY_STRUCT_4_FF(31) \ - SECUREC_COPY_STRUCT_4_FF(32) \ - default:break; \ - } \ - break; \ - default: \ - SECUREC_UNALIGNED_SET; \ - } /* END switch */ \ - } SECUREC_WHILE_ZERO -#endif - -static errno_t SecMemsetError(void *dest, size_t destMax, int c, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_MEM_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("memset_s"); - return ERANGE; - } - if (dest == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("memset_s"); - return EINVAL; - } - if (count > destMax) { - (void)memset(dest, c, destMax); /* set entire buffer to value c */ - SECUREC_ERROR_INVALID_RANGE("memset_s"); - return ERANGE_AND_RESET; - } - return EOK; -} - -#if SECUREC_WITH_PERFORMANCE_ADDONS || SECUREC_MEMSET_WITH_PERFORMANCE -static void SecDoMemsetOpt(void *dest, int c, size_t count) -{ - if (count > SECUREC_MEMSET_THRESHOLD_SIZE) { -#ifdef SECUREC_USE_ASM - (void)memset_opt(dest, c, count); -#else - (void)memset(dest, c, count); -#endif - } else { - if (SECUREC_ADDR_ALIGNED_8(dest)) { - /* use struct assignment */ - SECUREC_ALIGNED_SET_OPT_ZERO_FF; - } else { - SECUREC_UNALIGNED_SET; - } - } - return; -} -#endif - -/******************************************************************************* - * - * The memset_s function copies the value of c (converted to an unsigned char) - * into each of the first count characters of the object pointed to by dest. - * - * - * dest Pointer to destination. - * destMax The size of the buffer. - * c Character to set. - * count Number of characters. - * - * - * dest buffer is updated. - * - * - * EOK Success - * EINVAL dest == NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN - * ERANGE destMax is 0 or destMax > SECUREC_MEM_MAX_LEN - * ERANGE_AND_RESET count > destMax and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN and dest != NULL - * - * if return ERANGE_AND_RESET then fill dest to c ,fill length is destMax - ******************************************************************************* - */ -errno_t memset_s(void *dest, size_t destMax, int c, size_t count) -{ - if (SECUREC_LIKELY(count <= destMax && dest != NULL && destMax <= SECUREC_MEM_MAX_LEN)) { -#if SECUREC_MEMSET_WITH_PERFORMANCE - SecDoMemsetOpt(dest, c, count); -#else -#ifdef SECUREC_USE_ASM - (void)memset_opt(dest, c, count); -#else - (void)memset(dest, c, count); -#endif -#endif - return EOK; - } else { - /* meet some runtime violation, return error code */ - return SecMemsetError(dest, destMax, c, count); - } -} - -#if SECUREC_IN_KERNEL -EXPORT_SYMBOL(memset_s); -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - -errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count) -{ - if (SECUREC_LIKELY(count <= destMax && dest != NULL && destMax <= SECUREC_MEM_MAX_LEN)) { - SecDoMemsetOpt(dest, c, count); - return EOK; - } - - /* meet some runtime violation, return error code */ - return SecMemsetError(dest, destMax, c, count); -} - -errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count) -{ - if (SECUREC_LIKELY(count <= destMax && dest != NULL)) { - SecDoMemsetOpt(dest, c, count); - return EOK; - } - /* meet some runtime violation, return error code */ - return SecMemsetError(dest, destMax, c, count); - -} -#endif - diff --git a/base/src/securec/memset_s.c b/base/src/securec/memset_s.c new file mode 120000 index 0000000..91d71c9 --- /dev/null +++ b/base/src/securec/memset_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/memset_s.c \ No newline at end of file diff --git a/base/src/securec/output.inl b/base/src/securec/output.inl deleted file mode 100755 index 9b5ecf3..0000000 --- a/base/src/securec/output.inl +++ /dev/null @@ -1,1221 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -/* [Standardize-exceptions] Use unsafe function: Portability - * [reason] Use unsafe function to implement security function to maintain platform compatibility. - * And sufficient input validation is performed before calling - */ - -#ifndef OUTPUT_INL_2B263E9C_43D8_44BB_B17A_6D2033DECEE5 -#define OUTPUT_INL_2B263E9C_43D8_44BB_B17A_6D2033DECEE5 - -#define SECUREC_FLOAT_BUFSIZE (309+40) /* max float point value */ -#define SECUREC_FLOAT_BUFSIZE_LB (4932+40) /* max long double value */ - -#define SECUREC_INT_MAX 2147483647 - -#define SECUREC_MUL10(x) ((((x) << 2) + (x)) << 1) -#define SECUREC_INT_MAX_DIV_TEN 21474836 -#define SECUREC_MUL10_ADD_BEYOND_MAX(val) (((val) > SECUREC_INT_MAX_DIV_TEN)) - -#ifdef SECUREC_STACK_SIZE_LESS_THAN_1K -#define SECUREC_FMT_STR_LEN (8) -#else -#define SECUREC_FMT_STR_LEN (16) -#endif - -typedef struct { - unsigned int flags; - int fldWidth; - int precision; - int bufferIsWide; /* flag for buffer contains wide chars */ - int dynWidth; /* %* 1 width from variable parameter ;0 not */ - int dynPrecision; /* %.* 1 precision from variable parameter ;0 not */ -} SecFormatAttr; - -typedef union { - char *str; /* not a null terminated string */ - wchar_t *wStr; -} SecFormatBuf; - -typedef union { - char str[SECUREC_BUFFER_SIZE + 1]; -#ifdef SECUREC_FOR_WCHAR - wchar_t wStr[SECUREC_BUFFER_SIZE + 1]; -#endif -} SecBuffer; - -static int SecIndirectSprintf(char *strDest, const char *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - SECUREC_MASK_MSVC_CRT_WARNING - ret = vsprintf(strDest, format, arglist); - SECUREC_END_MASK_MSVC_CRT_WARNING - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} - -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT -/* to clear e506 warning */ -static int SecIsSameSize(size_t sizeA, size_t sizeB) -{ - return sizeA == sizeB; -} -#endif - -#define SECUREC_SPECIAL(_val, Base) \ - case Base: \ - do { \ - *--formatBuf.str = digits[_val % Base]; \ - }while ((_val /= Base) != 0) - -#define SECUREC_SAFE_WRITE_PREFIX(src, txtLen, _stream, outChars) do { \ - for (ii = 0; ii < txtLen; ++ii) { \ - *((SecChar *)(void *)(_stream->cur)) = *(src); \ - _stream->cur += sizeof(SecChar); \ - ++(src); \ - } \ - _stream->count -= txtLen * (int)(sizeof(SecChar)); \ - *(outChars) = *(outChars) + (txtLen); \ - } SECUREC_WHILE_ZERO - -#define SECUREC_SAFE_WRITE_STR(src, txtLen, _stream, outChars) do { \ - if (txtLen < 12 /* for mobile number length */) { \ - for (ii = 0; ii < txtLen; ++ii) { \ - *((SecChar *)(void *)(_stream->cur)) = *(src); \ - _stream->cur += sizeof(SecChar); \ - ++(src); \ - } \ - } else { \ - (void)memcpy(_stream->cur, src, ((size_t)(unsigned int)txtLen * (sizeof(SecChar)))); \ - _stream->cur += (size_t)(unsigned int)txtLen * (sizeof(SecChar)); \ - } \ - _stream->count -= txtLen * (int)(sizeof(SecChar)); \ - *(outChars) = *(outChars) + (txtLen); \ - } SECUREC_WHILE_ZERO - -#define SECUREC_SAFE_WRITE_CHAR(_ch, _stream, outChars) do { \ - *((SecChar *)(void *)(_stream->cur)) = (SecChar)_ch; \ - _stream->cur += sizeof(SecChar); \ - _stream->count -= (int)(sizeof(SecChar)); \ - *(outChars) = *(outChars) + 1; \ - } SECUREC_WHILE_ZERO - -#define SECUREC_SAFE_PADDING(padChar, padLen, _stream, outChars) do { \ - for (ii = 0; ii < padLen; ++ii) { \ - *((SecChar *)(void *)(_stream->cur)) = (SecChar)padChar; \ - _stream->cur += sizeof(SecChar); \ - } \ - _stream->count -= padLen * (int)(sizeof(SecChar)); \ - *(outChars) = *(outChars) + (padLen); \ - } SECUREC_WHILE_ZERO - -/* The count variable can be reduced to 0, and the external function complements the \0 terminator. */ -#define SECUREC_IS_REST_BUF_ENOUGH(needLen) ((int)(stream->count - (int)needLen * (int)(sizeof(SecChar))) >= 0) - -#define SECUREC_FMT_STATE_OFFSET 256 -#ifdef SECUREC_FOR_WCHAR -#define SECUREC_FMT_TYPE(c,fmtTable) ((((unsigned int)(int)(c)) <= (unsigned int)(int)SECUREC_CHAR('~')) ? \ - (fmtTable[(unsigned char)(c)]) : 0) -#define SECUREC_DECODE_STATE(c,fmtTable,laststate) (SecFmtState)(((fmtTable[(SECUREC_FMT_TYPE(c,fmtTable)) * \ - ((unsigned char)STAT_INVALID + 1) + \ - (unsigned char)(laststate) + \ - SECUREC_FMT_STATE_OFFSET]))) -#else -#define SECUREC_DECODE_STATE(c,fmtTable,laststate) (SecFmtState)((fmtTable[(fmtTable[(unsigned char)(c)]) * \ - ((unsigned char)STAT_INVALID + 1) + \ - (unsigned char)(laststate) + \ - SECUREC_FMT_STATE_OFFSET])) -#endif - -static void SecDecodeFlags(SecChar ch, SecFormatAttr *attr) -{ - switch (ch) { - case SECUREC_CHAR(' '): - attr->flags |= SECUREC_FLAG_SIGN_SPACE; - break; - case SECUREC_CHAR('+'): - attr->flags |= SECUREC_FLAG_SIGN; - break; - case SECUREC_CHAR('-'): - attr->flags |= SECUREC_FLAG_LEFT; - break; - case SECUREC_CHAR('0'): - attr->flags |= SECUREC_FLAG_LEADZERO; /* add zero th the front */ - break; - case SECUREC_CHAR('#'): - attr->flags |= SECUREC_FLAG_ALTERNATE; /* output %x with 0x */ - break; - default: - break; - } - return; -} - -static int SecDecodeSize(SecChar ch, SecFormatAttr *attr, const SecChar **format) -{ - switch (ch) { -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT - case SECUREC_CHAR('j'): - attr->flags |= SECUREC_FLAG_INTMAX; - break; -#endif - case SECUREC_CHAR('q'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('L'): - attr->flags |= SECUREC_FLAG_LONGLONG | SECUREC_FLAG_LONG_DOUBLE; - break; - case SECUREC_CHAR('l'): - if (**format == SECUREC_CHAR('l')) { - ++(*format); - attr->flags |= SECUREC_FLAG_LONGLONG; /* long long */ - } else { - attr->flags |= SECUREC_FLAG_LONG; /* long int or wchar_t */ - } - break; - case SECUREC_CHAR('t'): - attr->flags |= SECUREC_FLAG_PTRDIFF; - break; -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT - case SECUREC_CHAR('z'): - attr->flags |= SECUREC_FLAG_SIZE; - break; - case SECUREC_CHAR('Z'): - attr->flags |= SECUREC_FLAG_SIZE; - break; -#endif - - case SECUREC_CHAR('I'): -#ifdef SECUREC_ON_64BITS - attr->flags |= SECUREC_FLAG_I64; /* %I to INT64 */ -#endif - if ((**format == SECUREC_CHAR('6')) && (*((*format) + 1) == SECUREC_CHAR('4'))) { - (*format) += 2; - attr->flags |= SECUREC_FLAG_I64; /* %I64 to INT64 */ - } else if ((**format == SECUREC_CHAR('3')) && (*((*format) + 1) == SECUREC_CHAR('2'))) { - (*format) += 2; - attr->flags &= ~SECUREC_FLAG_I64; /* %I64 to INT32 */ - } else if ((**format == SECUREC_CHAR('d')) || (**format == SECUREC_CHAR('i')) || - (**format == SECUREC_CHAR('o')) || (**format == SECUREC_CHAR('u')) || - (**format == SECUREC_CHAR('x')) || (**format == SECUREC_CHAR('X'))) { - /* do nothing */ - } else { - /* Compatibility code for "%I" just print I */ - return -1; - } - break; - - case SECUREC_CHAR('h'): - if (**format == SECUREC_CHAR('h')) - attr->flags |= SECUREC_FLAG_CHAR; /* char */ - else - attr->flags |= SECUREC_FLAG_SHORT; /* short int */ - break; - - case SECUREC_CHAR('w'): - attr->flags |= SECUREC_FLAG_WIDECHAR; /* wide char */ - break; - default: - break; - - } - - return 0; -} - -static int SecDecodeTypeC(SecFormatAttr *attr, unsigned int cValue, SecFormatBuf *formatBuf, SecBuffer *buffer) -{ - int textLen; - wchar_t wchar; - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) && !(defined(__hpux)) && !(defined(SECUREC_ON_SOLARIS)) - attr->flags &= ~SECUREC_FLAG_LEADZERO; -#endif - -#ifdef SECUREC_FOR_WCHAR - attr->bufferIsWide = 1; - wchar = (wchar_t)cValue; - if (attr->flags & SECUREC_FLAG_SHORT) { - /* multibyte character to wide character */ - char tempchar[2]; - tempchar[0] = (char)(wchar & 0x00ff); - tempchar[1] = '\0'; - - if (mbtowc(buffer->wStr, tempchar, sizeof(tempchar)) < 0) { - return -1; - } - } else { - buffer->wStr[0] = wchar; - } - formatBuf->wStr = buffer->wStr; - textLen = 1; /* only 1 wide character */ -#else - attr->bufferIsWide = 0; - if (attr->flags & (SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR)) { - wchar = (wchar_t)cValue; - /* wide character to multibyte character */ - SECUREC_MASK_MSVC_CRT_WARNING - textLen = wctomb(buffer->str, wchar); - SECUREC_END_MASK_MSVC_CRT_WARNING - if (textLen < 0) { - return -1; - } - } else { - /* get multibyte character from argument */ - unsigned short temp; - temp = (unsigned short)cValue; - buffer->str[0] = (char)temp; - textLen = 1; - } - formatBuf->str = buffer->str; -#endif - - return textLen; -} - -static int SecDecodeTypeS(SecFormatAttr *attr, char *argPtr, SecFormatBuf *formatBuf) -{ - /* literal string to print null ptr, define it on stack rather than const text area - is to avoid gcc warning with pointing const text with variable */ - static char strNullString[8] = "(null)"; - static wchar_t wStrNullString[8] = { L'(', L'n', L'u', L'l', L'l', L')', L'\0', L'\0' }; - - int finalPrecision; - char *strEnd = NULL; - wchar_t *wStrEnd = NULL; - int textLen; - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) && (!defined(SECUREC_ON_UNIX)) - attr->flags &= ~SECUREC_FLAG_LEADZERO; -#endif - finalPrecision = (attr->precision == -1) ? SECUREC_INT_MAX : attr->precision; - formatBuf->str = argPtr; - -#ifdef SECUREC_FOR_WCHAR -#if defined(SECUREC_COMPATIBLE_LINUX_FORMAT) - if (!(attr->flags & SECUREC_FLAG_LONG)) { - attr->flags |= SECUREC_FLAG_SHORT; - } -#endif - if (attr->flags & SECUREC_FLAG_SHORT) { - if (formatBuf->str == NULL) { /* NULL passed, use special string */ - formatBuf->str = strNullString; - } - strEnd = formatBuf->str; - for (textLen = 0; textLen < finalPrecision && *strEnd; textLen++) { - ++strEnd; - } - /* textLen now contains length in multibyte chars */ - } else { - if (formatBuf->wStr == NULL) { /* NULL passed, use special string */ - formatBuf->wStr = wStrNullString; - } - attr->bufferIsWide = 1; - wStrEnd = formatBuf->wStr; - while (finalPrecision-- && *wStrEnd) { - ++wStrEnd; - } - textLen = (int)(wStrEnd - formatBuf->wStr); /* in wchar_ts */ - /* textLen now contains length in wide chars */ - } -#else /* SECUREC_FOR_WCHAR */ - if (attr->flags & (SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR)) { - if (formatBuf->wStr == NULL) { /* NULL passed, use special string */ - formatBuf->wStr = wStrNullString; - } - attr->bufferIsWide = 1; - wStrEnd = formatBuf->wStr; - while (finalPrecision-- && *wStrEnd) { - ++wStrEnd; - } - textLen = (int)(wStrEnd - formatBuf->wStr); - } else { - if (formatBuf->str == NULL) { /* meet NULL, use special string */ - formatBuf->str = strNullString; - } - - if (finalPrecision == SECUREC_INT_MAX) { - /* precision NOT assigned */ - /* The strlen performance is high when the string length is greater than 32 */ - textLen = (int)strlen(formatBuf->str); - } else { - /* precision assigned */ - strEnd = formatBuf->str; - while (finalPrecision-- && *strEnd) { - ++strEnd; - } - textLen = (int)(strEnd - formatBuf->str); /* length of the string */ - } - - } - -#endif /* SECUREC_FOR_WCHAR */ - return textLen; -} - -#ifdef SECUREC_FOR_WCHAR -int SecOutputSW(SecPrintfStream *stream, const wchar_t *cformat, va_list arglist) -#else -int SecOutputS(SecPrintfStream *stream, const char *cformat, va_list arglist) -#endif -{ - const SecChar *format = cformat; - - char *floatBuf = NULL; - SecFormatBuf formatBuf; - static const char *itoaUpperDigits = "0123456789ABCDEFX"; - static const char *itoaLowerDigits = "0123456789abcdefx"; - const char *digits = itoaUpperDigits; - int ii = 0; - - unsigned int radix; - int charsOut; /* characters written */ - - int prefixLen = 0; - int padding = 0; - - int textLen; /* length of the text */ - int bufferSize = 0; /* size of formatBuf.str */ - int noOutput = 0; - - SecFmtState state; - SecFmtState laststate; - - SecChar prefix[2] = { 0 }; - SecChar ch; /* currently read character */ - - static const unsigned char fmtCharTable[337] = { - /* type 0: nospecial meaning; - 1: '%'; - 2: '.' - 3: '*' - 4: '0' - 5: '1' ... '9' - 6: ' ', '+', '-', '#' - 7: 'h', 'l', 'L', 'F', 'w' , 'N','z','q','t','j' - 8: 'd','o','u','i','x','X','e','f','g' - */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x06, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x06, 0x00, 0x06, 0x02, 0x00, - 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x08, 0x08, 0x00, 0x07, 0x00, 0x00, 0x07, 0x00, 0x07, 0x00, - 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x08, 0x07, 0x00, 0x07, 0x00, 0x00, 0x08, - 0x08, 0x07, 0x00, 0x08, 0x07, 0x08, 0x00, 0x07, 0x08, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, - /* fill zero for normal char 128 byte for 0x80 - 0xff */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - /* state 0: normal - 1: percent - 2: flag - 3: width - 4: dot - 5: precis - 6: size - 7: type - 8: invalid - */ - 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x01, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x01, 0x00, 0x00, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x03, 0x03, 0x08, 0x05, - 0x08, 0x08, 0x00, 0x00, 0x00, 0x02, 0x02, 0x03, 0x05, 0x05, 0x08, 0x00, 0x00, 0x00, 0x03, 0x03, - 0x03, 0x05, 0x05, 0x08, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, - 0x00 - }; - - SecFormatAttr formatAttr; - SecBuffer buffer; - formatAttr.flags = 0; - formatAttr.bufferIsWide = 0; /* flag for buffer contains wide chars */ - formatAttr.fldWidth = 0; - formatAttr.precision = 0; - formatAttr.dynWidth = 0; - formatAttr.dynPrecision = 0; - - charsOut = 0; - textLen = 0; - state = STAT_NORMAL; /* starting state */ - formatBuf.str = NULL; - - /* loop each format character */ - /* remove format != NULL */ - while ((ch = *format++) != SECUREC_CHAR('\0') && charsOut >= 0) { - laststate = state; - state = SECUREC_DECODE_STATE(ch, fmtCharTable, laststate); - - switch (state) { - case STAT_NORMAL: - -NORMAL_CHAR: - - /* normal state, write character */ - if (SECUREC_IS_REST_BUF_ENOUGH(1 /* only one char */ )) { - SECUREC_SAFE_WRITE_CHAR(ch, stream, &charsOut); /* char * cast to wchar * */ - } else { -#ifdef SECUREC_FOR_WCHAR - SECUREC_WRITE_CHAR(ch, stream, &charsOut); -#else - /* optimize function call to code */ - charsOut = -1; - stream->count = -1; -#endif - } - - continue; - - case STAT_PERCENT: - /* set default values */ - prefixLen = 0; - noOutput = 0; - formatAttr.flags = 0; - formatAttr.fldWidth = 0; - formatAttr.precision = -1; - formatAttr.bufferIsWide = 0; - break; - - case STAT_FLAG: - /* set flag based on which flag character */ - SecDecodeFlags(ch, &formatAttr); - break; - - case STAT_WIDTH: - /* update width value */ - if (ch == SECUREC_CHAR('*')) { - /* get width */ - formatAttr.fldWidth = (int)va_arg(arglist, int); - if (formatAttr.fldWidth < 0) { - formatAttr.flags |= SECUREC_FLAG_LEFT; - formatAttr.fldWidth = -formatAttr.fldWidth; - } - formatAttr.dynWidth = 1; - } else { - if (laststate != STAT_WIDTH) { - formatAttr.fldWidth = 0; - } - if (SECUREC_MUL10_ADD_BEYOND_MAX(formatAttr.fldWidth)) { - return -1; - } - formatAttr.fldWidth = (int)SECUREC_MUL10((unsigned int)formatAttr.fldWidth) + (ch - SECUREC_CHAR('0')); - formatAttr.dynWidth = 0; - } - break; - - case STAT_DOT: - formatAttr.precision = 0; - break; - - case STAT_PRECIS: - /* update precision value */ - if (ch == SECUREC_CHAR('*')) { - /* get precision from arg list */ - formatAttr.precision = (int)va_arg(arglist, int); - if (formatAttr.precision < 0) { - formatAttr.precision = -1; - } - formatAttr.dynPrecision = 1; - } else { - /* add digit to current precision */ - if (SECUREC_MUL10_ADD_BEYOND_MAX(formatAttr.precision)) { - return -1; - } - formatAttr.precision = - (int)SECUREC_MUL10((unsigned int)formatAttr.precision) + (ch - SECUREC_CHAR('0')); - formatAttr.dynPrecision = 0; - } - break; - - case STAT_SIZE: - /* read a size specifier, set the formatAttr.flags based on it */ - if (SecDecodeSize(ch, &formatAttr, &format) != 0) { - /* Compatibility code for "%I" just print I */ - state = STAT_NORMAL; - goto NORMAL_CHAR; - } - break; - - case STAT_TYPE: - - switch (ch) { - - case SECUREC_CHAR('C'): - /* wide char */ - if (!(formatAttr.flags & (SECUREC_FLAG_SHORT | SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR))) { - -#ifdef SECUREC_FOR_WCHAR - formatAttr.flags |= SECUREC_FLAG_SHORT; -#else - formatAttr.flags |= SECUREC_FLAG_WIDECHAR; -#endif - } - /* fall-through */ - /* FALLTHRU */ - case SECUREC_CHAR('c'): - { - unsigned int cValue = (unsigned int)va_arg(arglist, int); - textLen = SecDecodeTypeC(&formatAttr, cValue, &formatBuf, &buffer); - if (textLen < 0) { - noOutput = 1; - } - } - break; - case SECUREC_CHAR('S'): /* wide char string */ -#ifndef SECUREC_FOR_WCHAR - if (!(formatAttr.flags & (SECUREC_FLAG_SHORT | SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR))) { - formatAttr.flags |= SECUREC_FLAG_WIDECHAR; - } -#else - if (!(formatAttr.flags & (SECUREC_FLAG_SHORT | SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR))) { - formatAttr.flags |= SECUREC_FLAG_SHORT; - } -#endif - /* fall-through */ - /* FALLTHRU */ - case SECUREC_CHAR('s'): - { - char *argPtr = (char *)va_arg(arglist, char *); - textLen = SecDecodeTypeS(&formatAttr, argPtr, &formatBuf); - } - break; - - case SECUREC_CHAR('n'): - /* higher risk disable it */ - return -1; - - case SECUREC_CHAR('E'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('F'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('G'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('A'): /* fall-through */ /* FALLTHRU */ - /* convert format char to lower , use Explicit conversion to clean up compilation warning */ - ch = (SecChar) (ch + ((SecChar) (SECUREC_CHAR('a')) - (SECUREC_CHAR('A')))); - /* fall-through */ - /* FALLTHRU */ - case SECUREC_CHAR('e'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('f'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('g'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('a'): - { - /* floating point conversion */ - formatBuf.str = buffer.str; /* output buffer for float string with default size */ - - /* compute the precision value */ - if (formatAttr.precision < 0) { - formatAttr.precision = 6; - } else if (formatAttr.precision == 0 && ch == SECUREC_CHAR('g')) { - formatAttr.precision = 1; - } - - /* calc buffer size to store long double value */ - if (formatAttr.flags & SECUREC_FLAG_LONG_DOUBLE) { - if (formatAttr.precision > (SECUREC_INT_MAX - SECUREC_FLOAT_BUFSIZE_LB)) { - noOutput = 1; - break; - } - bufferSize = SECUREC_FLOAT_BUFSIZE_LB + formatAttr.precision; - } else { - if (formatAttr.precision > (SECUREC_INT_MAX - SECUREC_FLOAT_BUFSIZE)) { - noOutput = 1; - break; - } - bufferSize = SECUREC_FLOAT_BUFSIZE + formatAttr.precision; - } - if (formatAttr.fldWidth > bufferSize) { - bufferSize = formatAttr.fldWidth; - } - - if (bufferSize >= SECUREC_BUFFER_SIZE) { - /* the current value of SECUREC_BUFFER_SIZE could NOT store the formatted float string */ - /* size include '+' and '\0' */ - floatBuf = (char *)SECUREC_MALLOC(((size_t)(unsigned int)bufferSize + (size_t)2)); - if (floatBuf != NULL) { - formatBuf.str = floatBuf; - } else { - noOutput = 1; - break; - } - } - - { - /* add following code to call system sprintf API for float number */ - const SecChar *pFltFmt = format - 2; /* point to the position before 'f' or 'g' */ - int k; - int fltFmtStrLen; - char fltFmtBuf[SECUREC_FMT_STR_LEN]; - char *fltFmtStr = fltFmtBuf; - char *fltFmtHeap = NULL; /* to clear warning */ - - while (SECUREC_CHAR('%') != *pFltFmt) { /* must meet '%' */ - --pFltFmt; - } - fltFmtStrLen = (int)((format - pFltFmt) + 1); /* with ending terminator */ - if (fltFmtStrLen > SECUREC_FMT_STR_LEN) { - /* if SECUREC_FMT_STR_LEN is NOT enough, alloc a new buffer */ - fltFmtHeap = (char *)SECUREC_MALLOC((size_t)((unsigned int)fltFmtStrLen)); - if (fltFmtHeap == NULL) { - noOutput = 1; - break; - } else { - for (k = 0; k < fltFmtStrLen - 1; ++k) { - /* convert wchar to char */ - fltFmtHeap[k] = (char)(pFltFmt[k]); /* copy the format string */ - } - fltFmtHeap[k] = '\0'; - - fltFmtStr = fltFmtHeap; - } - } else { - /* purpose of the repeat code is to solve the tool alarm Redundant_Null_Check */ - for (k = 0; k < fltFmtStrLen - 1; ++k) { - /* convert wchar to char */ - fltFmtBuf[k] = (char)(pFltFmt[k]); /* copy the format string */ - } - fltFmtBuf[k] = '\0'; - } - -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT - if (formatAttr.flags & SECUREC_FLAG_LONG_DOUBLE) { - long double tmp = (long double)va_arg(arglist, long double); - /* call system sprintf to format float value */ - if (formatAttr.dynWidth && formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, - formatAttr.fldWidth,formatAttr.precision, tmp); - } else if (formatAttr.dynWidth) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, - formatAttr.fldWidth, tmp); - } else if (formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, - formatAttr.precision, tmp); - } else { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, tmp); - } - } else -#endif - { - double tmp = (double)va_arg(arglist, double); - if (formatAttr.dynWidth && formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, formatAttr.fldWidth, - formatAttr.precision, tmp); - } else if (formatAttr.dynWidth) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, formatAttr.fldWidth, - tmp); - } else if (formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, formatAttr.precision, - tmp); - } else { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, tmp); - } - } - - if (fltFmtHeap != NULL) { - /* if buffer is allocated on heap, free it */ - SECUREC_FREE(fltFmtHeap); - fltFmtHeap = NULL; - /* to clear e438 last value assigned not used , the compiler will optimize this code */ - (void)fltFmtHeap; - } - if (textLen < 0) { - /* bufferSize is large enough,just validation the return value */ - noOutput = 1; - break; - } - - formatAttr.fldWidth = textLen; /* no padding ,this variable to calculate amount of padding */ - prefixLen = 0; /* no padding ,this variable to calculate amount of padding */ - formatAttr.flags = 0; /* clear all internal formatAttr.flags */ - break; - } - } - - case SECUREC_CHAR('p'): - /* print a pointer */ -#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) - formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; -#else - formatAttr.flags |= SECUREC_FLAG_POINTER; -#endif - -#ifdef SECUREC_ON_64BITS - formatAttr.flags |= SECUREC_FLAG_I64; /* converting an int64 */ -#else - formatAttr.flags |= SECUREC_FLAG_LONG; /* converting a long */ -#endif - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) && (!defined(SECUREC_ON_UNIX)) - -#if defined(SECUREC_VXWORKS_PLATFORM) - formatAttr.precision = 1; -#else - formatAttr.precision = 0; -#endif - formatAttr.flags |= SECUREC_FLAG_ALTERNATE; /* "0x" is not default prefix in UNIX */ - digits = itoaLowerDigits; - goto OUTPUT_HEX; -#else -/* not linux vxwoks */ -#if defined(_AIX) || defined(SECUREC_ON_SOLARIS) - formatAttr.precision = 1; -#else - formatAttr.precision = 2 * sizeof(void *); -#endif - -#endif - -#if defined(SECUREC_ON_UNIX) - digits = itoaLowerDigits; - goto OUTPUT_HEX; -#endif - - /* fall-through */ - /* FALLTHRU */ - case SECUREC_CHAR('X'): - /* unsigned upper hex output */ - digits = itoaUpperDigits; - goto OUTPUT_HEX; - - case SECUREC_CHAR('x'): - /* unsigned lower hex output */ - digits = itoaLowerDigits; - -OUTPUT_HEX: - radix = 16; - if (formatAttr.flags & SECUREC_FLAG_ALTERNATE) { - /* alternate form means '0x' prefix */ - prefix[0] = SECUREC_CHAR('0'); - prefix[1] = (SecChar) (digits[16]); /* 'x' or 'X' */ - -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) - if (ch == 'p') { - prefix[1] = SECUREC_CHAR('x'); - } -#endif -#if defined(_AIX) || defined(SECUREC_ON_SOLARIS) - if (ch == 'p') { - prefixLen = 0; - } else { - prefixLen = 2; - } -#else - prefixLen = 2; -#endif - - } - goto OUTPUT_INT; - case SECUREC_CHAR('i'): /* fall-through */ /* FALLTHRU */ - case SECUREC_CHAR('d'): /* fall-through */ /* FALLTHRU */ - /* signed decimal output */ - formatAttr.flags |= SECUREC_FLAG_SIGNED; - /* fall-through */ - /* FALLTHRU */ - case SECUREC_CHAR('u'): - radix = 10; - goto OUTPUT_INT; - case SECUREC_CHAR('o'): - /* unsigned octal output */ - radix = 8; - if (formatAttr.flags & SECUREC_FLAG_ALTERNATE) { - /* alternate form means force a leading 0 */ - formatAttr.flags |= SECUREC_FLAG_FORCE_OCTAL; - } -OUTPUT_INT: - { - - SecUnsignedInt64 number = 0; /* number to convert */ - SecInt64 l; /* temp long value */ - unsigned char tch; -#if defined(SECUREC_VXWORKS_VERSION_5_4) && !defined(SECUREC_ON_64BITS) - SecUnsignedInt32 digit = 0; /* ascii value of digit */ - SecUnsignedInt32 quotientHigh = 0; - SecUnsignedInt32 quotientLow = 0; -#endif - - /* read argument into variable l */ - if (formatAttr.flags & SECUREC_FLAG_I64) { - l = (SecInt64)va_arg(arglist, SecInt64); - } else if (formatAttr.flags & SECUREC_FLAG_LONGLONG) { - l = (SecInt64)va_arg(arglist, SecInt64); - } else -#ifdef SECUREC_ON_64BITS - if (formatAttr.flags & SECUREC_FLAG_LONG) { - l = (long)va_arg(arglist, long); - } else -#endif /* SECUREC_ON_64BITS */ - if (formatAttr.flags & SECUREC_FLAG_CHAR) { - if (formatAttr.flags & SECUREC_FLAG_SIGNED) { - l = (char)va_arg(arglist, int); /* sign extend */ - if (l >= 128) { /* on some platform, char is always unsigned */ - SecUnsignedInt64 tmpL = (SecUnsignedInt64)l; - formatAttr.flags |= SECUREC_FLAG_NEGATIVE; - tch = (unsigned char)(~(tmpL)); - l = tch + 1; - } - } else { - l = (unsigned char)va_arg(arglist, int); /* zero-extend */ - } - - } else if (formatAttr.flags & SECUREC_FLAG_SHORT) { - if (formatAttr.flags & SECUREC_FLAG_SIGNED) { - l = (short)va_arg(arglist, int); /* sign extend */ - } else { - l = (unsigned short)va_arg(arglist, int); /* zero-extend */ - } - - } -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT - else if (formatAttr.flags & SECUREC_FLAG_PTRDIFF) { - l = (ptrdiff_t)va_arg(arglist, ptrdiff_t); /* sign extend */ - } else if (formatAttr.flags & SECUREC_FLAG_SIZE) { - if (formatAttr.flags & SECUREC_FLAG_SIGNED) { - /* No suitable macros were found to handle the branch */ - if (SecIsSameSize(sizeof(size_t), sizeof(long))) { - l = va_arg(arglist, long); /* sign extend */ - } else if (SecIsSameSize(sizeof(size_t), sizeof(long long))) { - l = va_arg(arglist, long long); /* sign extend */ - } else { - l = va_arg(arglist, int); /* sign extend */ - } - } else { - l = (SecInt64)(size_t)va_arg(arglist, size_t); /* sign extend */ - } - } else if (formatAttr.flags & SECUREC_FLAG_INTMAX) { - if (formatAttr.flags & SECUREC_FLAG_SIGNED) { - l = va_arg(arglist, SecInt64); /* sign extend */ - } else { - l = (SecInt64)(SecUnsignedInt64)va_arg(arglist, SecUnsignedInt64); /* sign extend */ - } - } -#endif - else { - if (formatAttr.flags & SECUREC_FLAG_SIGNED) { - l = va_arg(arglist, int); /* sign extend */ - } else { - l = (unsigned int)va_arg(arglist, int); /* zero-extend */ - } - - } - - /* check for negative; copy into number */ - if ((formatAttr.flags & SECUREC_FLAG_SIGNED) && l < 0) { - number = (SecUnsignedInt64)(-l); - formatAttr.flags |= SECUREC_FLAG_NEGATIVE; - } else { - number = (SecUnsignedInt64)l; - } - - if (((formatAttr.flags & SECUREC_FLAG_I64) == 0) && -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT - ((formatAttr.flags & SECUREC_FLAG_INTMAX) == 0) && -#endif -#ifdef SECUREC_ON_64BITS - ((formatAttr.flags & SECUREC_FLAG_PTRDIFF) == 0) && - ((formatAttr.flags & SECUREC_FLAG_SIZE) == 0) && -#if !defined(SECUREC_COMPATIBLE_WIN_FORMAT) /* on window 64 system sizeof long is 32bit */ - ((formatAttr.flags & SECUREC_FLAG_LONG) == 0) && -#endif -#endif - ((formatAttr.flags & SECUREC_FLAG_LONGLONG) == 0)) { - - number &= 0xffffffff; - } - - /* check precision value for default */ - if (formatAttr.precision < 0) { - formatAttr.precision = 1; /* default precision */ - } else { -#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) - formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; -#else - if (!(formatAttr.flags & SECUREC_FLAG_POINTER)) { - formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; - } -#endif - if (formatAttr.precision > SECUREC_MAX_PRECISION) { - formatAttr.precision = SECUREC_MAX_PRECISION; - } - } - - /* Check if data is 0; if so, turn off hex prefix,if 'p',add 0x prefix,else not add prefix */ - if (number == 0) { -#if !(defined(SECUREC_VXWORKS_PLATFORM)||defined(__hpux)) - prefixLen = 0; -#else - if ((ch == 'p') && (formatAttr.flags & SECUREC_FLAG_ALTERNATE)) - prefixLen = 2; - else - prefixLen = 0; -#endif - } - - /* Convert data to ASCII */ - formatBuf.str = &buffer.str[SECUREC_BUFFER_SIZE]; - - if (number > 0) { -#ifdef SECUREC_ON_64BITS - switch (radix) { - /* the compiler will optimize each one */ - SECUREC_SPECIAL(number, 10); - break; - SECUREC_SPECIAL(number, 16); - break; - SECUREC_SPECIAL(number, 8); - break; - default: - break; - } -#else /* for 32 bits system */ - if (number <= 0xFFFFFFFFUL) { - /* in most case, the value to be converted is small value */ - SecUnsignedInt32 n32Tmp = (SecUnsignedInt32)number; - switch (radix) { - SECUREC_SPECIAL(n32Tmp, 16); - break; - SECUREC_SPECIAL(n32Tmp, 8); - break; - -#ifdef _AIX - /* the compiler will optimize div 10 */ - SECUREC_SPECIAL(n32Tmp, 10); - break; -#else - case 10: - { - /* fast div 10 */ - SecUnsignedInt32 q; - SecUnsignedInt32 r; - do { - *--formatBuf.str = digits[n32Tmp % 10]; - q = (n32Tmp >> 1) + (n32Tmp >> 2); - q = q + (q >> 4); - q = q + (q >> 8); - q = q + (q >> 16); - q = q >> 3; - r = n32Tmp - (((q << 2) + q) << 1); - n32Tmp = (r > 9) ? (q + 1) : q; - } while (n32Tmp != 0); - } - break; -#endif - default: - break; - } /* end switch */ - } else { - /* the value to be converted is greater than 4G */ -#if defined(SECUREC_VXWORKS_VERSION_5_4) - do { - if (0 != SecU64Div32((SecUnsignedInt32)((number >> 16) >> 16), - (SecUnsignedInt32)number, - (SecUnsignedInt32)radix, "ientHigh, "ientLow, &digit)) { - noOutput = 1; - break; - } - *--formatBuf.str = digits[digit]; - - number = (SecUnsignedInt64)quotientHigh; - number = (number << 32) + quotientLow; - } while (number != 0); -#else - switch (radix) { - /* the compiler will optimize div 10 */ - SECUREC_SPECIAL(number, 10); - break; - SECUREC_SPECIAL(number, 16); - break; - SECUREC_SPECIAL(number, 8); - break; - default: - break; - } -#endif - } -#endif - } /* END if (number > 0) */ - /* compute length of number,.if textLen > 0, then formatBuf.str must be in buffer.str */ - textLen = (int)((char *)&buffer.str[SECUREC_BUFFER_SIZE] - formatBuf.str); - if (formatAttr.precision > textLen) { - for (ii = 0; ii < formatAttr.precision - textLen; ++ii) { - *--formatBuf.str = '0'; - } - textLen = formatAttr.precision; - } - - /* Force a leading zero if FORCEOCTAL flag set */ - if ((formatAttr.flags & SECUREC_FLAG_FORCE_OCTAL) && (textLen == 0 || formatBuf.str[0] != '0')) { - *--formatBuf.str = '0'; - ++textLen; /* add a zero */ - } - } - break; - default: - break; - } - - if (noOutput == 0) { - if (formatAttr.flags & SECUREC_FLAG_SIGNED) { - if (formatAttr.flags & SECUREC_FLAG_NEGATIVE) { - /* prefix is a '-' */ - prefix[0] = SECUREC_CHAR('-'); - prefixLen = 1; - } else if (formatAttr.flags & SECUREC_FLAG_SIGN) { - /* prefix is '+' */ - prefix[0] = SECUREC_CHAR('+'); - prefixLen = 1; - } else if (formatAttr.flags & SECUREC_FLAG_SIGN_SPACE) { - /* prefix is ' ' */ - prefix[0] = SECUREC_CHAR(' '); - prefixLen = 1; - } - } - -#if defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && (!defined(SECUREC_ON_UNIX)) - if ((formatAttr.flags & SECUREC_FLAG_POINTER) && (0 == textLen)) { - formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; - formatBuf.str = &buffer.str[SECUREC_BUFFER_SIZE - 1]; - *formatBuf.str-- = '\0'; - *formatBuf.str-- = ')'; - *formatBuf.str-- = 'l'; - *formatBuf.str-- = 'i'; - *formatBuf.str-- = 'n'; - *formatBuf.str = '('; - textLen = 5; - } -#endif - - /* calculate amount of padding */ - padding = (formatAttr.fldWidth - textLen) - prefixLen; - - /* put out the padding, prefix, and text, in the correct order */ - - if (!(formatAttr.flags & (SECUREC_FLAG_LEFT | SECUREC_FLAG_LEADZERO)) && padding > 0) { - /* pad on left with blanks */ - if (SECUREC_IS_REST_BUF_ENOUGH(padding)) { - /* char * cast to wchar * */ - SECUREC_SAFE_PADDING(SECUREC_CHAR(' '), padding, stream, &charsOut); - } else { - SECUREC_WRITE_MULTI_CHAR(SECUREC_CHAR(' '), padding, stream, &charsOut); - } - } - - /* write prefix */ - if (prefixLen > 0) { - SecChar *pPrefix = prefix; - if (SECUREC_IS_REST_BUF_ENOUGH(prefixLen)) { - /* max prefix len is 2, use loop copy */ /* char * cast to wchar * */ - SECUREC_SAFE_WRITE_PREFIX(pPrefix, prefixLen, stream, &charsOut); - } else { - SECUREC_WRITE_STRING(prefix, prefixLen, stream, &charsOut); - } - } - - if ((formatAttr.flags & SECUREC_FLAG_LEADZERO) && !(formatAttr.flags & SECUREC_FLAG_LEFT) - && padding > 0) { - /* write leading zeros */ - if (SECUREC_IS_REST_BUF_ENOUGH(padding)) { - /* char * cast to wchar * */ - SECUREC_SAFE_PADDING(SECUREC_CHAR('0'), padding, stream, &charsOut); - } else { - SECUREC_WRITE_MULTI_CHAR(SECUREC_CHAR('0'), padding, stream, &charsOut); - } - } - - /* write text */ -#ifndef SECUREC_FOR_WCHAR - if (formatAttr.bufferIsWide && (textLen > 0)) { - wchar_t *p = formatBuf.wStr; - int count = textLen; - while (count--) { - char tmpBuf[SECUREC_MB_LEN + 1]; - SECUREC_MASK_MSVC_CRT_WARNING - int retVal = wctomb(tmpBuf, *p++); - SECUREC_END_MASK_MSVC_CRT_WARNING - if (retVal <= 0) { - charsOut = -1; - break; - } - SECUREC_WRITE_STRING(tmpBuf, retVal, stream, &charsOut); - } - } else { - if (SECUREC_IS_REST_BUF_ENOUGH(textLen)) { - SECUREC_SAFE_WRITE_STR(formatBuf.str, textLen, stream, &charsOut); - } else { - SECUREC_WRITE_STRING(formatBuf.str, textLen, stream, &charsOut); - } - } -#else /* SECUREC_FOR_WCHAR */ - if (formatAttr.bufferIsWide == 0 && textLen > 0) { - int count = textLen; - char *p = formatBuf.str; - - while (count > 0) { - wchar_t wchar = L'\0'; - int retVal = mbtowc(&wchar, p, (size_t)MB_CUR_MAX); - if (retVal <= 0) { - charsOut = -1; - break; - } - SECUREC_WRITE_CHAR(wchar, stream, &charsOut); - p += retVal; - count -= retVal; - } - } else { - if (SECUREC_IS_REST_BUF_ENOUGH(textLen)) { - SECUREC_SAFE_WRITE_STR(formatBuf.wStr, textLen, stream, &charsOut); /* char * cast to wchar * */ - } else { - SECUREC_WRITE_STRING(formatBuf.wStr, textLen, stream, &charsOut); - } - } -#endif /* SECUREC_FOR_WCHAR */ - - if (charsOut >= 0 && (formatAttr.flags & SECUREC_FLAG_LEFT) && padding > 0) { - /* pad on right with blanks */ - if (SECUREC_IS_REST_BUF_ENOUGH(padding)) { - /* char * cast to wchar * */ - SECUREC_SAFE_PADDING(SECUREC_CHAR(' '), padding, stream, &charsOut); - } else { - SECUREC_WRITE_MULTI_CHAR(SECUREC_CHAR(' '), padding, stream, &charsOut); - } - } - - /* we're done! */ - } - if (floatBuf != NULL) { - SECUREC_FREE(floatBuf); - floatBuf = NULL; - } - break; - case STAT_INVALID: - return -1; - default: - return -1; /* input format is wrong, directly return */ - } - } - - if (state != STAT_NORMAL && state != STAT_TYPE) { - return -1; - } - - return charsOut; /* the number of characters written */ -} /* arglist must not be declare as const */ -#endif /* OUTPUT_INL_2B263E9C_43D8_44BB_B17A_6D2033DECEE5 */ - diff --git a/base/src/securec/output.inl b/base/src/securec/output.inl new file mode 120000 index 0000000..969ff6f --- /dev/null +++ b/base/src/securec/output.inl @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/output.inl \ No newline at end of file diff --git a/base/src/securec/scanf_s.c b/base/src/securec/scanf_s.c deleted file mode 100755 index 81e922c..0000000 --- a/base/src/securec/scanf_s.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s - * The scanf_s function reads data from the standard input stream stdin and - * writes the data into the location that's given by argument. Each argument - * must be a pointer to a variable of a type that corresponds to a type specifier - * in format. If copying occurs between strings that overlap, the behavior is - * undefined. - * - * - * format Format control string. - * ... Optional arguments. - * - * - * ... The converted value stored in user assigned address - * - * - * Returns the number of fields successfully converted and assigned; - * the return value does not include fields that were read but not assigned. - * A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ - -int scanf_s(const char *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vscanf_s(format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} - - diff --git a/base/src/securec/scanf_s.c b/base/src/securec/scanf_s.c new file mode 120000 index 0000000..734faab --- /dev/null +++ b/base/src/securec/scanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/scanf_s.c \ No newline at end of file diff --git a/base/src/securec/secinput.h b/base/src/securec/secinput.h deleted file mode 100755 index b08281a..0000000 --- a/base/src/securec/secinput.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __SEC_INPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C -#define __SEC_INPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C -#include "securecutil.h" - -#define SECUREC_SCANF_EINVAL (-1) -#define SECUREC_SCANF_ERROR_PARA (-2) - -/* for internal stream flag */ -#define SECUREC_MEM_STR_FLAG 0X01 -#define SECUREC_FILE_STREAM_FLAG 0X02 -#define SECUREC_FROM_STDIN_FLAG 0X04 -#define SECUREC_LOAD_FILE_TO_MEM_FLAG 0X08 - -#define SECUREC_UNINITIALIZED_FILE_POS (-1) -#define SECUREC_BOM_HEADER_SIZE (2) -#define SECUREC_UTF8_BOM_HEADER_SIZE (3) - -typedef struct { - int count; /* the size of buffered string in bytes */ - const char *cur; /* the pointer to next read position */ - char *base; /* the pointer to the header of buffered string */ - unsigned int flag; /* mark the properties of input stream */ - FILE *pf; /* the file pointer */ - int fileRealRead; - long oriFilePos; /* the original position of file offset when fscanf is called */ -#if defined(SECUREC_NO_STD_UNGETC) - unsigned int lastChar; /* the char code of last input */ - int fUnget; /* the boolean flag of pushing a char back to read stream */ -#endif -} SecFileStream; - -#if defined(SECUREC_NO_STD_UNGETC) -#define SECUREC_INIT_SEC_FILE_STREAM {0, NULL, NULL, 0, NULL, 0, 0, 0, 0} -#else -#define SECUREC_INIT_SEC_FILE_STREAM {0, NULL, NULL, 0, NULL, 0, 0} -#endif - -#ifdef __cplusplus -extern "C" { -#endif - - extern int SecInputS(SecFileStream *stream, const char *format, va_list arglist); - extern int SecInputSW(SecFileStream *stream, const wchar_t *format, va_list arglist); - extern void SecClearDestBuf(const char *buffer, const char *cformat, va_list arglist); - extern void SecClearDestBufW(const wchar_t *buffer, const wchar_t *cformat, va_list arglist); - -/* 20150105 For software and hardware decoupling,such as UMG */ -#ifdef SECUREC_SYSAPI4VXWORKS -#ifdef feof -#undef feof -#endif - extern int feof(FILE *stream); - -#ifndef isspace -#define isspace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n')) -#endif -#ifndef isascii -#define isascii(c) (((unsigned char)(c))<=0x7f) -#endif -#ifndef isupper -#define isupper(c) ((c) >= 'A' && (c) <= 'Z') -#endif -#ifndef islower -#define islower(c) ((c) >= 'a' && (c) <= 'z') -#endif -#ifndef isalpha -#define isalpha(c) (isupper(c) || (islower(c))) -#endif -#ifndef isdigit -#define isdigit(c) ((c) >= '0' && (c) <= '9') -#endif -#ifndef isxdigit -#define isxupper(c) ((c) >= 'A' && (c) <= 'F') -#define isxlower(c) ((c) >= 'a' && (c) <= 'f') -#define isxdigit(c) (isdigit(c) || isxupper(c) ||isxlower(c)) -#endif -#endif - -#ifdef __cplusplus -} -#endif -/* Reserved file operation macro interface */ -#define SECUREC_LOCK_FILE(s) -#define SECUREC_UNLOCK_FILE(s) -#define SECUREC_LOCK_STDIN(i,s) -#define SECUREC_UNLOCK_STDIN(i,s) -#endif - - diff --git a/base/src/securec/secinput.h b/base/src/securec/secinput.h new file mode 120000 index 0000000..60bfe01 --- /dev/null +++ b/base/src/securec/secinput.h @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/secinput.h \ No newline at end of file diff --git a/base/src/securec/securecutil.c b/base/src/securec/securecutil.c deleted file mode 100755 index c40467a..0000000 --- a/base/src/securec/securecutil.c +++ /dev/null @@ -1,354 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* Avoid duplicate header files,not include securecutil.h */ -#include "securecutil.h" -#if SECUREC_IN_KERNEL == 0 -#include "secureprintoutput.h" -#endif - - -#if SECUREC_IN_KERNEL == 0 -/* put a char to output */ -#define SECUREC_PUTC(_c,_stream) ((--(_stream)->count >= 0) ? ((*(_stream)->cur++ = (char)(_c)) & 0xff) : EOF) -/* to clear e835 */ -#define SECUREC_PUTC_ZERO(_stream) ((--(_stream)->count >= 0) ? ((*(_stream)->cur++ = (char)('\0'))) : EOF) - -int SecPutWcharStrEndingZero(SecPrintfStream *str, int zeroNum) -{ - int succeed = 0; - int i; - - for (i = 0; i < zeroNum && (SECUREC_PUTC_ZERO(str) != EOF); ++i) { - } - if (i == zeroNum) { - succeed = 1; - } - return succeed; -} - -int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list arglist) -{ - SecPrintfStream str; - int retVal; - - str.count = (int)count; /* this count include \0 character */ - str.cur = string; - - retVal = SecOutputS(&str, format, arglist); - if ((retVal >= 0) && (SECUREC_PUTC_ZERO(&str) != EOF)) { - return (retVal); - } else if (str.count < 0) { - /* the buffer was too small; we return truncation */ - string[count - 1] = 0; - return SECUREC_PRINTF_TRUNCATE; - } - - return -1; -} - -void SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten) -{ - int count = num; - while (count-- > 0) { - if (SECUREC_PUTC(ch, f) == EOF) { - *pnumwritten = -1; - break; - } else { - ++(*pnumwritten); - } - } -} - -void SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten) -{ - const char *str = string; - int count = len; - while (count-- > 0) { - if (SECUREC_PUTC(*str, f) == EOF) { - *pnumwritten = -1; - break; - } else { - ++(*pnumwritten); - ++str; - } - } -} -#endif - -/* Following function "U64Div32" realized the operation of division between an unsigned 64-bits - * number and an unsigned 32-bits number. - * these codes are contributed by Dopra team in syslib. - */ -#if defined(SECUREC_VXWORKS_VERSION_5_4) - -#define SECUREC_MAX_SHIFT_NUM 32 -#define SECUREC_MASK_BIT_ALL 0xFFFFFFFF -#define SECUREC_MASK_BIT_32 0x80000000 -#define SECUREC_MASK_BIT_01 0x00000001 -#define SECUREC_MASK_HI_NBITS(x) (SECUREC_MASK_BIT_ALL << (SECUREC_MAX_SHIFT_NUM - (x))) - -typedef enum { - SEC_BIT64_GREAT, - SEC_BIT64_EQUAL, - SEC_BIT64_LESS -} SecCompareResult; - -static void SecBigIntSub(SecUnsignedInt32 *aHi, SecUnsignedInt32 *aLo, const SecUnsignedInt32 bHi, - const SecUnsignedInt32 bLo) -{ - if (*aLo < bLo) { - *aHi -= (bHi + 1); - } else { - *aHi -= (bHi); - } - *aLo -= bLo; -} - -static SecCompareResult SecBigIntCompare(const SecUnsignedInt32 aHi, const SecUnsignedInt32 aLo, - const SecUnsignedInt32 bHi, const SecUnsignedInt32 bLo) -{ - if (aHi > bHi) { - return SEC_BIT64_GREAT; - } else if ((aHi == bHi) && (aLo > bLo)) { - return SEC_BIT64_GREAT; - } else if ((aHi == bHi) && (aLo == bLo)) { - return SEC_BIT64_EQUAL; - } else { - return SEC_BIT64_LESS; - } -} - -static void SecU64Div64Ret(SecUnsignedInt32 tmpQuoHi, SecUnsignedInt32 tmpQuoLo, - SecUnsignedInt32 tmpDividendHi, SecUnsignedInt32 tmpDividendLo, - SecUnsignedInt32 *pQuotientHigh, SecUnsignedInt32 *pQuotientLow, - SecUnsignedInt32 *pRemainderHigh, SecUnsignedInt32 *pRemainderLow) -{ - *pQuotientHigh = tmpQuoHi; - *pQuotientLow = tmpQuoLo; - - if ((pRemainderHigh != NULL) - && (pRemainderLow != NULL)) { - *pRemainderHigh = tmpDividendHi; - *pRemainderLow = tmpDividendLo; - } - return; -} - -static int SecU64Div64(SecUnsignedInt32 dividendHigh, SecUnsignedInt32 dividendLow, - SecUnsignedInt32 divisorHigh, SecUnsignedInt32 divisorLow, - SecUnsignedInt32 *pQuotientHigh, SecUnsignedInt32 *pQuotientLow, - SecUnsignedInt32 *pRemainderHigh, SecUnsignedInt32 *pRemainderLow) -{ - signed char scShiftNumHi = 0; - signed char scShiftNumLo = 0; - SecUnsignedInt32 tmpQuoHi; - SecUnsignedInt32 tmpQuoLo; - SecUnsignedInt32 tmpDividendHi; - SecUnsignedInt32 tmpDividendLo; - SecUnsignedInt32 tmpDivisorHi; - SecUnsignedInt32 tmpDivisorLo; - SecCompareResult etmpResult; - - if ((pQuotientHigh == NULL) || (pQuotientLow == NULL)) { - return -1; - } - - if (divisorHigh == 0) { - if (divisorLow == 0) { - return -1; - } else if (divisorLow == 1) { - *pQuotientHigh = dividendHigh; - *pQuotientLow = dividendLow; - - if (pRemainderHigh != NULL && pRemainderLow != NULL) { - *pRemainderHigh = 0; - *pRemainderLow = 0; - } - - return 0; - } - } - - tmpQuoHi = tmpQuoLo = 0; - tmpDividendHi = dividendHigh; - tmpDividendLo = dividendLow; - - /* if divisor is larger than dividend, quotient equals to zero, - * remainder equals to dividends */ - etmpResult = SecBigIntCompare(dividendHigh, dividendLow, divisorHigh, divisorLow); - - if (etmpResult == SEC_BIT64_LESS) { - SecU64Div64Ret(tmpQuoHi, tmpQuoLo, - tmpDividendHi, tmpDividendLo, pQuotientHigh, pQuotientLow, pRemainderHigh, pRemainderLow); - return 0; - } - - else if (etmpResult == SEC_BIT64_EQUAL) { - *pQuotientHigh = 0; - *pQuotientLow = 1; - - if ((pRemainderHigh != NULL) && (pRemainderLow != NULL)) { - *pRemainderHigh = 0; - *pRemainderLow = 0; - } - - return 0; - } - - /* get shift number to implement divide arithmetic */ - if (divisorHigh > 0) { - for (scShiftNumHi = 0; scShiftNumHi < SECUREC_MAX_SHIFT_NUM; scShiftNumHi++) { - if ((divisorHigh << (SecUnsignedInt32)(unsigned char)scShiftNumHi) & SECUREC_MASK_BIT_32) { - break; - } - } - } else { - for (scShiftNumLo = 0; scShiftNumLo < SECUREC_MAX_SHIFT_NUM; scShiftNumLo++) { - if ((divisorLow << scShiftNumLo) & SECUREC_MASK_BIT_32) { - break; - } - } - } - - if (divisorHigh > 0) { - /* divisor's high 32 bits doesn't equal to zero */ - - for (; scShiftNumHi >= 0; scShiftNumHi--) { - - if (scShiftNumHi == 0) { - tmpDivisorHi = divisorHigh; - } else { - tmpDivisorHi = (divisorHigh << (SecUnsignedInt32)(unsigned char)scShiftNumHi) - | (divisorLow >> (SECUREC_MAX_SHIFT_NUM - scShiftNumHi)); - } - - tmpDivisorLo = divisorLow << (SecUnsignedInt32)(unsigned char)scShiftNumHi; - - etmpResult = SecBigIntCompare(tmpDividendHi, tmpDividendLo, tmpDivisorHi, tmpDivisorLo); - - if (etmpResult != SEC_BIT64_LESS) { - SecBigIntSub(&tmpDividendHi, &tmpDividendLo, tmpDivisorHi, tmpDivisorLo); - - tmpQuoLo |= (SecUnsignedInt32)(1 << (SecUnsignedInt32)(unsigned char)scShiftNumHi); - - if ((tmpDividendHi == 0) && (tmpDividendLo == 0)) { - SecU64Div64Ret(tmpQuoHi, tmpQuoLo, - tmpDividendHi, tmpDividendLo, - pQuotientHigh, pQuotientLow, pRemainderHigh, pRemainderLow); - return 0; - } - } - if (scShiftNumHi == 0) { - break; - } - } - - } else { - /* divisor's high 32 bits equals to zero */ - - scShiftNumHi = scShiftNumLo; - - for (; scShiftNumHi >= 0; scShiftNumHi--) { - tmpDivisorHi = divisorLow << (SecUnsignedInt32)(unsigned char)scShiftNumHi; - etmpResult = SecBigIntCompare(tmpDividendHi, tmpDividendLo, tmpDivisorHi, 0); - - if (etmpResult != SEC_BIT64_LESS) { - SecUnsignedInt32 tmp = 0; - SecBigIntSub(&tmpDividendHi, &tmpDividendLo, tmpDivisorHi, tmp); - - tmpQuoHi |= (SecUnsignedInt32)(1 << (SecUnsignedInt32)(unsigned char)scShiftNumHi); - - if ((tmpDividendHi == 0) && (tmpDividendLo == 0)) { - SecU64Div64Ret(tmpQuoHi, tmpQuoLo, tmpDividendHi, tmpDividendLo, - pQuotientHigh, pQuotientLow, pRemainderHigh, pRemainderLow); - return 0; - } - } - if (scShiftNumHi == 0) { - break; - } - } - - for (scShiftNumHi = SECUREC_MAX_SHIFT_NUM - 1; scShiftNumHi >= 0; scShiftNumHi--) { - if (scShiftNumHi == 0) { - tmpDivisorHi = 0; - } else { - tmpDivisorHi = divisorLow >> (SECUREC_MAX_SHIFT_NUM - scShiftNumHi); - } - - tmpDivisorLo = divisorLow << (SecUnsignedInt32)(unsigned char)scShiftNumHi; - - etmpResult = SecBigIntCompare(tmpDividendHi, tmpDividendLo, tmpDivisorHi, tmpDivisorLo); - - if (etmpResult != SEC_BIT64_LESS) { - SecBigIntSub(&tmpDividendHi, &tmpDividendLo, tmpDivisorHi, tmpDivisorLo); - - tmpQuoLo |= (SecUnsignedInt32)(1 << (SecUnsignedInt32)(unsigned char)scShiftNumHi); - - if ((tmpDividendHi == 0) && (tmpDividendLo == 0)) { - SecU64Div64Ret(tmpQuoHi, tmpQuoLo, tmpDividendHi, tmpDividendLo, - pQuotientHigh, pQuotientLow, pRemainderHigh, pRemainderLow); - return 0; - } - } - if (scShiftNumHi == 0) { - break; - } - } - - } - - SecU64Div64Ret(tmpQuoHi, tmpQuoLo, - tmpDividendHi, tmpDividendLo, pQuotientHigh, pQuotientLow, pRemainderHigh, pRemainderLow); - return 0; -} - -int SecU64Div32(SecUnsignedInt32 dividendHigh, SecUnsignedInt32 dividendLow, SecUnsignedInt32 divisor, - SecUnsignedInt32 *pQuotientHigh, SecUnsignedInt32 *pQuotientLow, SecUnsignedInt32 *puiRemainder) -{ - SecUnsignedInt32 tmpRemainderHi = 0; - SecUnsignedInt32 tmpRemainderLo = 0; - SecUnsignedInt32 tmpDividendHigh = dividendHigh; - SecUnsignedInt32 tmpDividendLow = dividendLow; - SecUnsignedInt32 tmpDivisor = divisor; - int ret = 0; - - if ((pQuotientHigh == NULL) || (pQuotientLow == NULL) || tmpDivisor == 0 || puiRemainder == NULL) { - return -1; - } - - tmpDividendHigh &= SECUREC_MASK_BIT_ALL; - tmpDividendLow &= SECUREC_MASK_BIT_ALL; - tmpDivisor &= SECUREC_MASK_BIT_ALL; - *pQuotientHigh = 0; - *pQuotientLow = 0; - *puiRemainder = 0; - - ret = SecU64Div64(tmpDividendHigh, - tmpDividendLow, 0, tmpDivisor, pQuotientHigh, pQuotientLow, &tmpRemainderHi, &tmpRemainderLo); - if (ret != 0) { - return ret; - } - - if (tmpRemainderHi != 0) { - return -1; - } - *puiRemainder = tmpRemainderLo; - - return 0; -} -#endif - diff --git a/base/src/securec/securecutil.c b/base/src/securec/securecutil.c new file mode 120000 index 0000000..4bb5fed --- /dev/null +++ b/base/src/securec/securecutil.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/securecutil.c \ No newline at end of file diff --git a/base/src/securec/securecutil.h b/base/src/securec/securecutil.h deleted file mode 100755 index 0243971..0000000 --- a/base/src/securec/securecutil.h +++ /dev/null @@ -1,342 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __SECURECUTIL_H__46C86578_F8FF_4E49_8E64_9B175241761F -#define __SECURECUTIL_H__46C86578_F8FF_4E49_8E64_9B175241761F -#include "securec.h" - -#ifndef SECUREC_HAVE_STRNLEN -#if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L) -#if SECUREC_IN_KERNEL -#define SECUREC_HAVE_STRNLEN 0 -#else -#if defined(__GLIBC__) && __GLIBC__ >= 2 && defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ >= 10 -#define SECUREC_HAVE_STRNLEN 1 -#else -#define SECUREC_HAVE_STRNLEN 0 -#endif -#endif -#else -#define SECUREC_HAVE_STRNLEN 0 -#endif -#endif - -#if SECUREC_IN_KERNEL == 0 - -#if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) || defined(__ARMCC_VERSION) -typedef __int64 SecInt64; -typedef unsigned __int64 SecUnsignedInt64; -#if defined(__ARMCC_VERSION) -typedef int SecInt32; -typedef unsigned int SecUnsignedInt32; -#else -typedef __int32 SecInt32; -typedef unsigned __int32 SecUnsignedInt32; -#endif -#else -typedef int SecInt32; -typedef unsigned int SecUnsignedInt32; -typedef long long SecInt64; -typedef unsigned long long SecUnsignedInt64; -#endif - -/* struct for performance */ -typedef struct { - unsigned char buf[1]; -} SecStrBuf1; -typedef struct { - unsigned char buf[2]; -} SecStrBuf2; -typedef struct { - unsigned char buf[3]; -} SecStrBuf3; -typedef struct { - unsigned char buf[4]; -} SecStrBuf4; -typedef struct { - unsigned char buf[5]; -} SecStrBuf5; -typedef struct { - unsigned char buf[6]; -} SecStrBuf6; -typedef struct { - unsigned char buf[7]; -} SecStrBuf7; -typedef struct { - unsigned char buf[8]; -} SecStrBuf8; -typedef struct { - unsigned char buf[9]; -} SecStrBuf9; -typedef struct { - unsigned char buf[10]; -} SecStrBuf10; -typedef struct { - unsigned char buf[11]; -} SecStrBuf11; -typedef struct { - unsigned char buf[12]; -} SecStrBuf12; -typedef struct { - unsigned char buf[13]; -} SecStrBuf13; -typedef struct { - unsigned char buf[14]; -} SecStrBuf14; -typedef struct { - unsigned char buf[15]; -} SecStrBuf15; -typedef struct { - unsigned char buf[16]; -} SecStrBuf16; -typedef struct { - unsigned char buf[17]; -} SecStrBuf17; -typedef struct { - unsigned char buf[18]; -} SecStrBuf18; -typedef struct { - unsigned char buf[19]; -} SecStrBuf19; -typedef struct { - unsigned char buf[20]; -} SecStrBuf20; -typedef struct { - unsigned char buf[21]; -} SecStrBuf21; -typedef struct { - unsigned char buf[22]; -} SecStrBuf22; -typedef struct { - unsigned char buf[23]; -} SecStrBuf23; -typedef struct { - unsigned char buf[24]; -} SecStrBuf24; -typedef struct { - unsigned char buf[25]; -} SecStrBuf25; -typedef struct { - unsigned char buf[26]; -} SecStrBuf26; -typedef struct { - unsigned char buf[27]; -} SecStrBuf27; -typedef struct { - unsigned char buf[28]; -} SecStrBuf28; -typedef struct { - unsigned char buf[29]; -} SecStrBuf29; -typedef struct { - unsigned char buf[30]; -} SecStrBuf30; -typedef struct { - unsigned char buf[31]; -} SecStrBuf31; -typedef struct { - unsigned char buf[32]; -} SecStrBuf32; -typedef struct { - unsigned char buf[33]; -} SecStrBuf33; -typedef struct { - unsigned char buf[34]; -} SecStrBuf34; -typedef struct { - unsigned char buf[35]; -} SecStrBuf35; -typedef struct { - unsigned char buf[36]; -} SecStrBuf36; -typedef struct { - unsigned char buf[37]; -} SecStrBuf37; -typedef struct { - unsigned char buf[38]; -} SecStrBuf38; -typedef struct { - unsigned char buf[39]; -} SecStrBuf39; -typedef struct { - unsigned char buf[40]; -} SecStrBuf40; -typedef struct { - unsigned char buf[41]; -} SecStrBuf41; -typedef struct { - unsigned char buf[42]; -} SecStrBuf42; -typedef struct { - unsigned char buf[43]; -} SecStrBuf43; -typedef struct { - unsigned char buf[44]; -} SecStrBuf44; -typedef struct { - unsigned char buf[45]; -} SecStrBuf45; -typedef struct { - unsigned char buf[46]; -} SecStrBuf46; -typedef struct { - unsigned char buf[47]; -} SecStrBuf47; -typedef struct { - unsigned char buf[48]; -} SecStrBuf48; -typedef struct { - unsigned char buf[49]; -} SecStrBuf49; -typedef struct { - unsigned char buf[50]; -} SecStrBuf50; -typedef struct { - unsigned char buf[51]; -} SecStrBuf51; -typedef struct { - unsigned char buf[52]; -} SecStrBuf52; -typedef struct { - unsigned char buf[53]; -} SecStrBuf53; -typedef struct { - unsigned char buf[54]; -} SecStrBuf54; -typedef struct { - unsigned char buf[55]; -} SecStrBuf55; -typedef struct { - unsigned char buf[56]; -} SecStrBuf56; -typedef struct { - unsigned char buf[57]; -} SecStrBuf57; -typedef struct { - unsigned char buf[58]; -} SecStrBuf58; -typedef struct { - unsigned char buf[59]; -} SecStrBuf59; -typedef struct { - unsigned char buf[60]; -} SecStrBuf60; -typedef struct { - unsigned char buf[61]; -} SecStrBuf61; -typedef struct { - unsigned char buf[62]; -} SecStrBuf62; -typedef struct { - unsigned char buf[63]; -} SecStrBuf63; -typedef struct { - unsigned char buf[64]; -} SecStrBuf64; - - - -#if (defined(_MSC_VER)) && (_MSC_VER >= 1400) -#define SECUREC_MASK_MSVC_CRT_WARNING __pragma(warning(push)) \ - __pragma(warning(disable:4996 4127)) -#define SECUREC_END_MASK_MSVC_CRT_WARNING __pragma(warning(pop)) -#else -#define SECUREC_MASK_MSVC_CRT_WARNING -#define SECUREC_END_MASK_MSVC_CRT_WARNING -#endif - -#define SECUREC_WHILE_ZERO SECUREC_MASK_MSVC_CRT_WARNING while (0) SECUREC_END_MASK_MSVC_CRT_WARNING - -#define SECUREC_PRINTF_TRUNCATE (-2) -typedef struct { - int count; - char *cur; -} SecPrintfStream; - -#endif - -#define SECUREC_ADDR_ALIGNED_8(addr) (!((size_t)(addr) & 7)) -#define SECUREC_IN_REGISTER register - -/* User can change the error handler by modify the following definition, - * such as logging the detail error in file. - */ -#if defined(_DEBUG) || defined(DEBUG) -#if defined(SECUREC_ERROR_HANDLER_BY_ASSERT) -#define SECUREC_ERROR_INVALID_PARAMTER(msg) assert( msg "invalid argument" == NULL) -#define SECUREC_ERROR_INVALID_RANGE(msg) assert( msg "invalid dest buffer size" == NULL) -#define SECUREC_ERROR_BUFFER_OVERLAP(msg) assert( msg "buffer overlap" == NULL) - -#elif defined(SECUREC_ERROR_HANDLER_BY_PRINTF) -#if SECUREC_IN_KERNEL -#define SECUREC_ERROR_INVALID_PARAMTER(msg) printk( "%s invalid argument\n",msg) -#define SECUREC_ERROR_INVALID_RANGE(msg) printk( "%s invalid dest buffer size\n", msg) -#define SECUREC_ERROR_BUFFER_OVERLAP(msg) printk( "%s buffer overlap\n",msg) -#else -#define SECUREC_ERROR_INVALID_PARAMTER(msg) printf( "%s invalid argument\n",msg) -#define SECUREC_ERROR_INVALID_RANGE(msg) printf( "%s invalid dest buffer size\n", msg) -#define SECUREC_ERROR_BUFFER_OVERLAP(msg) printf( "%s buffer overlap\n",msg) -#endif -#elif defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG) - -#define SECUREC_ERROR_INVALID_PARAMTER(msg) LogSecureCRuntimeError(msg " EINVAL\n") -#define SECUREC_ERROR_INVALID_RANGE(msg) LogSecureCRuntimeError(msg " ERANGE\n") -#define SECUREC_ERROR_BUFFER_OVERLAP(msg) LogSecureCRuntimeError(msg " EOVERLAP\n") -#else - -#define SECUREC_ERROR_INVALID_PARAMTER(msg) ((void)0) -#define SECUREC_ERROR_INVALID_RANGE(msg) ((void)0) -#define SECUREC_ERROR_BUFFER_OVERLAP(msg) ((void)0) -#endif - -#else -#define SECUREC_ERROR_INVALID_PARAMTER(msg) ((void)0) -#define SECUREC_ERROR_INVALID_RANGE(msg) ((void)0) -#define SECUREC_ERROR_BUFFER_OVERLAP(msg) ((void)0) -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#if SECUREC_IN_KERNEL == 0 - extern int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list arglist); - extern int SecPutWcharStrEndingZero(SecPrintfStream *str, int zerosNum); - extern void SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten); - extern void SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten); -#endif - -/* assembly language memcpy memset for X86 or MIPS ... */ -#ifdef SECUREC_USE_ASM - extern void *memcpy_opt(void *, const void *, size_t); - extern void *memset_opt(void *, int, size_t); -#endif - -#if defined(SECUREC_VXWORKS_VERSION_5_4) - extern int SecU64Div32(SecUnsignedInt32 dividendHigh, SecUnsignedInt32 dividendLow, SecUnsignedInt32 divisor, - SecUnsignedInt32 *pQuotientHigh, SecUnsignedInt32 *pQuotientLow, SecUnsignedInt32 *puiRemainder); - -#endif - -#if defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG) - extern void LogSecureCRuntimeError(const char *errDetail); -#endif - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif - - diff --git a/base/src/securec/securecutil.h b/base/src/securec/securecutil.h new file mode 120000 index 0000000..1e72cb2 --- /dev/null +++ b/base/src/securec/securecutil.h @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/securecutil.h \ No newline at end of file diff --git a/base/src/securec/secureinput_a.c b/base/src/securec/secureinput_a.c deleted file mode 100755 index 06081b2..0000000 --- a/base/src/securec/secureinput_a.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secinput.h" - -#ifdef SECUREC_FOR_WCHAR -#undef SECUREC_FOR_WCHAR -#endif - -typedef char SecChar; -typedef unsigned char SecUnsignedChar; -typedef int SecInt; -typedef unsigned int SecUnsignedInt; - -#include "input.inl" - - diff --git a/base/src/securec/secureinput_a.c b/base/src/securec/secureinput_a.c new file mode 120000 index 0000000..b9a19a7 --- /dev/null +++ b/base/src/securec/secureinput_a.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/secureinput_a.c \ No newline at end of file diff --git a/base/src/securec/secureinput_w.c b/base/src/securec/secureinput_w.c deleted file mode 100755 index 755852d..0000000 --- a/base/src/securec/secureinput_w.c +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#if defined(_MSC_VER) && (_MSC_VER >= 1400) -#ifdef __STDC_WANT_SECURE_LIB__ -#undef __STDC_WANT_SECURE_LIB__ -#endif -#ifdef _CRTIMP_ALTERNATIVE -#undef _CRTIMP_ALTERNATIVE -#endif -#define __STDC_WANT_SECURE_LIB__ 0 -#define _CRTIMP_ALTERNATIVE //comment microsoft *_s function -#endif - -/* if some platforms don't have wchar.h, don't include it */ -#if !(defined(SECUREC_VXWORKS_PLATFORM)) -/* This header file is placed below secinput.h, which will cause tool alarm, - * but If there is no macro above, it will cause compiling alarm */ -#include -#endif -#include "secinput.h" - -#ifndef SECUREC_FOR_WCHAR -#define SECUREC_FOR_WCHAR -#endif - -#ifndef WEOF -#define WEOF ((wchar_t)-1) -#endif - -#if defined(SECUREC_VXWORKS_PLATFORM) && !defined(__WINT_TYPE__) -typedef wchar_t wint_t; -#endif - -typedef wint_t SecInt; -typedef wint_t SecUnsignedInt; -typedef wchar_t SecChar; -typedef wchar_t SecUnsignedChar; - -#include "input.inl" - diff --git a/base/src/securec/secureinput_w.c b/base/src/securec/secureinput_w.c new file mode 120000 index 0000000..7714c47 --- /dev/null +++ b/base/src/securec/secureinput_w.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/secureinput_w.c \ No newline at end of file diff --git a/base/src/securec/secureprintoutput.h b/base/src/securec/secureprintoutput.h deleted file mode 100755 index e234111..0000000 --- a/base/src/securec/secureprintoutput.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __SECUREPRINTOUTPUT_H__E950DA2C_902F_4B15_BECD_948E99090D9C -#define __SECUREPRINTOUTPUT_H__E950DA2C_902F_4B15_BECD_948E99090D9C -#include "securecutil.h" - -#if SECUREC_IN_KERNEL == 0 - -/* flag definitions */ -/* Using macros instead of enumerations is because some of the enumerated types under the compiler are 16bit. */ -#define SECUREC_FLAG_SIGN 0x00001U -#define SECUREC_FLAG_SIGN_SPACE 0x00002U -#define SECUREC_FLAG_LEFT 0x00004U -#define SECUREC_FLAG_LEADZERO 0x00008U -#define SECUREC_FLAG_LONG 0x00010U -#define SECUREC_FLAG_SHORT 0x00020U -#define SECUREC_FLAG_SIGNED 0x00040U -#define SECUREC_FLAG_ALTERNATE 0x00080U -#define SECUREC_FLAG_NEGATIVE 0x00100U -#define SECUREC_FLAG_FORCE_OCTAL 0x00200U -#define SECUREC_FLAG_LONG_DOUBLE 0x00400U -#define SECUREC_FLAG_WIDECHAR 0x00800U -#define SECUREC_FLAG_LONGLONG 0x01000U -#define SECUREC_FLAG_CHAR 0x02000U -#define SECUREC_FLAG_POINTER 0x04000U -#define SECUREC_FLAG_I64 0x08000U -#define SECUREC_FLAG_PTRDIFF 0x10000U -#define SECUREC_FLAG_SIZE 0x20000U -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT -#define SECUREC_FLAG_INTMAX 0x40000U -#endif - -/* state definitions */ -typedef enum { - STAT_NORMAL, - STAT_PERCENT, - STAT_FLAG, - STAT_WIDTH, - STAT_DOT, - STAT_PRECIS, - STAT_SIZE, - STAT_TYPE, - STAT_INVALID -} SecFmtState; - -#ifdef SECUREC_STACK_SIZE_LESS_THAN_1K -/* SECUREC_BUFFER_SIZE Can not be less than 23 , - *the length of the octal representation of 64-bit integers with zero lead - */ -#define SECUREC_BUFFER_SIZE 256 -#else -#define SECUREC_BUFFER_SIZE 512 -#endif - -#define SECUREC_MAX_PRECISION SECUREC_BUFFER_SIZE -/* max. # bytes in multibyte char ,see MB_LEN_MAX */ -#define SECUREC_MB_LEN 16 - -#ifdef __cplusplus -extern "C" { -#endif - - extern int SecOutputS(SecPrintfStream *stream, const char *cformat, va_list arglist); - extern int SecOutputSW(SecPrintfStream *stream, const wchar_t *cformat, va_list arglist); - -#ifdef __cplusplus -} -#endif - -#endif - -#endif - - diff --git a/base/src/securec/secureprintoutput.h b/base/src/securec/secureprintoutput.h new file mode 120000 index 0000000..d81eca6 --- /dev/null +++ b/base/src/securec/secureprintoutput.h @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/secureprintoutput.h \ No newline at end of file diff --git a/base/src/securec/secureprintoutput_a.c b/base/src/securec/secureprintoutput_a.c deleted file mode 100755 index 112cb5a..0000000 --- a/base/src/securec/secureprintoutput_a.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "secureprintoutput.h" - -#ifdef SECUREC_FOR_WCHAR -#undef SECUREC_FOR_WCHAR -#endif - -typedef char SecChar; -#define SECUREC_CHAR(x) x - -#define SECUREC_WRITE_MULTI_CHAR SecWriteMultiChar -#define SECUREC_WRITE_STRING SecWriteString - -#include "output.inl" diff --git a/base/src/securec/secureprintoutput_a.c b/base/src/securec/secureprintoutput_a.c new file mode 120000 index 0000000..cc7c851 --- /dev/null +++ b/base/src/securec/secureprintoutput_a.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/secureprintoutput_a.c \ No newline at end of file diff --git a/base/src/securec/secureprintoutput_w.c b/base/src/securec/secureprintoutput_w.c deleted file mode 100755 index a85b49f..0000000 --- a/base/src/securec/secureprintoutput_w.c +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#if defined(_MSC_VER) && (_MSC_VER >= 1400) -#ifdef _CRTIMP_ALTERNATIVE -#undef _CRTIMP_ALTERNATIVE -#endif -#ifdef __STDC_WANT_SECURE_LIB__ -#undef __STDC_WANT_SECURE_LIB__ -#endif -#define _CRTIMP_ALTERNATIVE //comment microsoft *_s function -#define __STDC_WANT_SECURE_LIB__ 0 -#endif - -/* if some platforms don't have wchar.h, don't include it */ -#if !(defined(SECUREC_VXWORKS_PLATFORM)) -/* This header file is placed below secinput.h, which will cause tool alarm, - * but If there is no macro above, it will cause compiling alarm */ -#include -#endif -#include "secureprintoutput.h" - -#ifndef WEOF -#define WEOF ((wchar_t)-1) -#endif - -#ifndef SECUREC_FOR_WCHAR -#define SECUREC_FOR_WCHAR -#endif - -typedef wchar_t SecChar; -#define SECUREC_CHAR(x) L ## x -#define SECUREC_WRITE_CHAR SecWriteCharW -#define SECUREC_WRITE_MULTI_CHAR SecWriteMultiCharW -#define SECUREC_WRITE_STRING SecWriteStringW - - -/* put a wchar to output stream */ -/* LSD change "unsigned short" to wchar_t */ - -static wchar_t SecPutCharW(wchar_t ch, SecPrintfStream *f) -{ - wchar_t wcRet = 0; - if (((f)->count -= (int)sizeof(wchar_t)) >= 0) { - *(wchar_t *)(void *)(f->cur) = ch; - f->cur += sizeof(wchar_t); - wcRet = ch; - } else { - wcRet = (wchar_t)WEOF; - } - return wcRet; -} - -static void SecWriteCharW(wchar_t ch, SecPrintfStream *f, int *pnumwritten) -{ - if (SecPutCharW(ch, f) == (wchar_t)WEOF) { - *pnumwritten = -1; - } else { - ++(*pnumwritten); - } -} - -static void SecWriteMultiCharW(wchar_t ch, int num, SecPrintfStream *f, int *pnumwritten) -{ - int count = num; - while (count-- > 0) { - SecWriteCharW(ch, f, pnumwritten); - if (*pnumwritten == -1) { - break; - } - } -} - -static void SecWriteStringW(const wchar_t *string, int len, SecPrintfStream *f, int *pnumwritten) -{ - const wchar_t *str = string; - int count = len; - while (count-- > 0) { - SecWriteCharW(*str++, f, pnumwritten); - if (*pnumwritten == -1) { - break; - } - } -} - -#include "output.inl" - diff --git a/base/src/securec/secureprintoutput_w.c b/base/src/securec/secureprintoutput_w.c new file mode 120000 index 0000000..ee8711c --- /dev/null +++ b/base/src/securec/secureprintoutput_w.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/secureprintoutput_w.c \ No newline at end of file diff --git a/base/src/securec/snprintf_s.c b/base/src/securec/snprintf_s.c deleted file mode 100755 index 03a6607..0000000 --- a/base/src/securec/snprintf_s.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The snprintf_s function is equivalent to the snprintf function - * except for the parameter destMax/count and the explicit runtime-constraints violation - * The snprintf_s function formats and stores count or fewer characters in - * strDest and appends a terminating null. Each argument (if any) is converted - * and output according to the corresponding format specification in format. - * The formatting is consistent with the printf family of functions; If copying - * occurs between strings that overlap, the behavior is undefined. - * - * - * strDest Storage location for the output. - * destMax The size of the storage location for output. Size - * in bytes for snprintf_s or size in words for snwprintf_s. - * count Maximum number of character to store. - * format Format-control string. - * ... Optional arguments. - * - * - * strDest is updated - * - * - * return the number of characters written, not including the terminating null - * return -1 if an error occurs. - * return -1 if count < destMax and the output string has been truncated - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); -#ifndef __MINGW32__ - ret = vsnprintf_s(strDest, destMax, count, format, arglist); -#else - ret = vsnprintf_truncated_s(strDest, destMax, format, arglist); -#endif - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} - -#if SECUREC_SNPRINTF_TRUNCATED -int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vsnprintf_truncated_s(strDest, destMax, format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} -#endif - - diff --git a/base/src/securec/snprintf_s.c b/base/src/securec/snprintf_s.c new file mode 120000 index 0000000..5008c3e --- /dev/null +++ b/base/src/securec/snprintf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/snprintf_s.c \ No newline at end of file diff --git a/base/src/securec/sprintf_s.c b/base/src/securec/sprintf_s.c deleted file mode 100755 index 08cd73f..0000000 --- a/base/src/securec/sprintf_s.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The sprintf_s function is equivalent to the sprintf function - * except for the parameter destMax and the explicit runtime-constraints violation - * The sprintf_s function formats and stores a series of characters and values - * in strDest. Each argument (if any) is converted and output according to - * the corresponding format specification in format. The format consists of - * ordinary characters and has the same form and function as the format argument - * for printf. A null character is appended after the last character written. - * If copying occurs between strings that overlap, the behavior is undefined. - * - * - * strDest Storage location for output. - * destMax Maximum number of characters to store. - * format Format-control string. - * ... Optional arguments - * - * - * strDest is updated - * - * - * return the number of bytes stored in strDest, not counting the terminating null character. - * return -1 if an error occurred. - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -#ifndef __MINGW32__ -int sprintf_s(char *strDest, size_t destMax, const char *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vsprintf_s(strDest, destMax, format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} -#endif - diff --git a/base/src/securec/sprintf_s.c b/base/src/securec/sprintf_s.c new file mode 120000 index 0000000..1b633ce --- /dev/null +++ b/base/src/securec/sprintf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/sprintf_s.c \ No newline at end of file diff --git a/base/src/securec/sscanf_s.c b/base/src/securec/sscanf_s.c deleted file mode 100755 index c28bd59..0000000 --- a/base/src/securec/sscanf_s.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The sscanf_s function is equivalent to fscanf_s, - * except that input is obtained from a string (specified by the argument buffer) rather than from a stream - * The sscanf function reads data from buffer into the location given by each - * argument. Every argument must be a pointer to a variable with a type that - * corresponds to a type specifier in format. The format argument controls the - * interpretation of the input fields and has the same form and function as - * the format argument for the scanf function. - * If copying takes place between strings that overlap, the behavior is undefined. - * - * - * buffer Stored data. - * format Format control string, see Format Specifications. - * ... Optional arguments. - * - * - * ... The converted value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; the return value does not include fields that were read but - * not assigned. - * A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -#ifndef __MINGW32__ -int sscanf_s(const char *buffer, const char *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vsscanf_s(buffer, format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} -#endif - diff --git a/base/src/securec/sscanf_s.c b/base/src/securec/sscanf_s.c new file mode 120000 index 0000000..53bee07 --- /dev/null +++ b/base/src/securec/sscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/sscanf_s.c \ No newline at end of file diff --git a/base/src/securec/strcat_s.c b/base/src/securec/strcat_s.c deleted file mode 100755 index d4a9443..0000000 --- a/base/src/securec/strcat_s.c +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -#if SECUREC_HAVE_STRNLEN -#define SECUREC_STRCAT_LEN_THRESHOLD 8 - -#define SECUREC_CALC_STR_LEN(str,maxLen,len) do { \ - if (*((str) + 0) == '\0') { \ - len = 0; \ - } else if (*((str) + 1) == '\0') { \ - len = 1; \ - } else if (*((str) + 2) == '\0') { \ - len = 2; \ - } else if (*((str) + 3) == '\0') { \ - len = 3; \ - } else if (*((str) + 4) == '\0') { \ - len = 4; \ - } else if (*((str) + 5) == '\0') { \ - len = 5; \ - } else if (*((str) + 6) == '\0') { \ - len = 6; \ - } else if (*((str) + 7) == '\0') { \ - len = 7; \ - } else if (*((str) + 8) == '\0') { \ - /* Optimization with a length of 8 */ \ - len = 8; \ - } else { \ - /* The offset is 8 because the performance of 8 byte alignment is high */ \ - len = SECUREC_STRCAT_LEN_THRESHOLD + \ - strnlen((str) + SECUREC_STRCAT_LEN_THRESHOLD, \ - (maxLen) - SECUREC_STRCAT_LEN_THRESHOLD); \ - } \ - } SECUREC_WHILE_ZERO - -/* The function compiler will be inlined and not placed in other files */ -static size_t SecMinStrLenOpt(const char *str, size_t maxLen) -{ - size_t len; - if (maxLen > SECUREC_STRCAT_LEN_THRESHOLD) { - /* Just to reduce the code complexity */ - SECUREC_CALC_STR_LEN(str, maxLen, len); - } else { - const char *strEnd = str; - len = 0; - /* use count as boundary checker */ - while (len < maxLen && *(strEnd) != '\0') { - ++strEnd; - ++len; /* no ending terminator */ - } - } - return len; -} - -static errno_t SecDoStrcat(char *strDest, size_t destMax, const char *strSrc) -{ - size_t destLen = strnlen(strDest, destMax); - /* Only optimize strSrc, do not apply this function to strDest */ - size_t srcLen = SecMinStrLenOpt(strSrc, destMax - destLen); - - if ((strDest < strSrc && strDest + destLen + srcLen >= strSrc) || \ - (strSrc < strDest && strSrc + srcLen >= strDest)) { - strDest[0] = '\0'; - if (strDest + destLen <= strSrc && destLen == destMax) { - SECUREC_ERROR_INVALID_PARAMTER("strcat_s"); - return EINVAL_AND_RESET; - } - SECUREC_ERROR_BUFFER_OVERLAP("strcat_s"); - return EOVERLAP_AND_RESET; - } - if (srcLen + destLen >= destMax || strDest == strSrc) { - strDest[0] = '\0'; - if (destLen == destMax) { - SECUREC_ERROR_INVALID_PARAMTER("strcat_s"); - return EINVAL_AND_RESET; - } - SECUREC_ERROR_INVALID_RANGE("strcat_s"); - return ERANGE_AND_RESET; - } - (void)memcpy(strDest + destLen, strSrc, srcLen + 1); /* copy terminator */ - return EOK; -} -#else -static errno_t SecDoStrcat(char *strDest, size_t destMax, const char *strSrc) -{ - char *tmpDest = strDest; - const char *tmpSrc = strSrc; - size_t availableSize = destMax; - SECUREC_IN_REGISTER const char *overlapGuard = NULL; - - if (tmpDest < tmpSrc) { - overlapGuard = tmpSrc; - while (availableSize > 0 && *tmpDest != 0) { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strcat_s"); - return EOVERLAP_AND_RESET; - } - /* seek to string end */ - ++tmpDest; - --availableSize; - } - - /* strDest unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("strcat_s"); - return EINVAL_AND_RESET; - } - /* if availableSize > 0, then execute the strcat operation */ - while ((*tmpDest++ = *tmpSrc++) != 0 && --availableSize > 0) { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strcat_s"); - return EOVERLAP_AND_RESET; - } - } - } else { - overlapGuard = tmpDest; - while (availableSize > 0 && *tmpDest != '\0') { - /* seek to string end, and no need to check overlap */ - ++tmpDest; - --availableSize; - } - - /* strDest unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("strcat_s"); - return EINVAL_AND_RESET; - } - while ((*tmpDest++ = *tmpSrc++) != '\0' && --availableSize > 0) { - if (tmpSrc == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strcat_s"); - return EOVERLAP_AND_RESET; - } - } - } - - /* strDest have not enough space, return error */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("strcat_s"); - return ERANGE_AND_RESET; - } - return EOK; -} -#endif -/******************************************************************************* - * - * The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character) - * to the end of the string pointed to by strDest. - * The initial character of strSrc overwrites the terminating null character of strDest. - * strcat_s will return EOVERLAP_AND_RESET if the source and destination strings overlap. - * - * Note that the second parameter is the total size of the buffer, not the - * remaining size. - * - * - * strDest Null-terminated destination string buffer. - * destMax Size of the destination string buffer. - * strSrc Null-terminated source string buffer. - * - * - * strDest is updated - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN - * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid)or - * (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN) - * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN - * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ - -errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc) -{ - - if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("strcat_s"); - return ERANGE; - } - - if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("strcat_s"); - if (strDest != NULL) { - strDest[0] = '\0'; - return EINVAL_AND_RESET; - } - return EINVAL; - } - - return SecDoStrcat(strDest, destMax, strSrc); -} - -#if SECUREC_IN_KERNEL -EXPORT_SYMBOL(strcat_s); -#endif - diff --git a/base/src/securec/strcat_s.c b/base/src/securec/strcat_s.c new file mode 120000 index 0000000..52d0eb3 --- /dev/null +++ b/base/src/securec/strcat_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/strcat_s.c \ No newline at end of file diff --git a/base/src/securec/strcpy_s.c b/base/src/securec/strcpy_s.c deleted file mode 100755 index 39642cb..0000000 --- a/base/src/securec/strcpy_s.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions] Use unsafe function: Performance-sensitive - * [reason] Always used in the performance critical path, - * and sufficient input validation is performed before calling - */ - -#include "securecutil.h" - -#ifndef SECUREC_STRCOPY_THRESHOLD_SIZE -#define SECUREC_STRCOPY_THRESHOLD_SIZE (32UL) -#endif - -/* The purpose of converting to void is to clean up the alarm */ -#define SECUREC_STRCPY_BY_STRUCT(num) \ - case num: \ - *(SecStrBuf##num *)(void *)strDest = *(const SecStrBuf##num *)(const void *)strSrc; \ - break; - -#define SECUREC_SMALL_STR_COPY do { \ - if (SECUREC_ADDR_ALIGNED_8(strDest) && SECUREC_ADDR_ALIGNED_8(strSrc)) { \ - /* use struct assignment */ \ - switch (srcStrLen) { \ - SECUREC_STRCPY_BY_STRUCT(1) \ - SECUREC_STRCPY_BY_STRUCT(2) \ - SECUREC_STRCPY_BY_STRUCT(3) \ - SECUREC_STRCPY_BY_STRUCT(4) \ - SECUREC_STRCPY_BY_STRUCT(5) \ - SECUREC_STRCPY_BY_STRUCT(6) \ - SECUREC_STRCPY_BY_STRUCT(7) \ - SECUREC_STRCPY_BY_STRUCT(8) \ - SECUREC_STRCPY_BY_STRUCT(9) \ - SECUREC_STRCPY_BY_STRUCT(10) \ - SECUREC_STRCPY_BY_STRUCT(11) \ - SECUREC_STRCPY_BY_STRUCT(12) \ - SECUREC_STRCPY_BY_STRUCT(13) \ - SECUREC_STRCPY_BY_STRUCT(14) \ - SECUREC_STRCPY_BY_STRUCT(15) \ - SECUREC_STRCPY_BY_STRUCT(16) \ - SECUREC_STRCPY_BY_STRUCT(17) \ - SECUREC_STRCPY_BY_STRUCT(18) \ - SECUREC_STRCPY_BY_STRUCT(19) \ - SECUREC_STRCPY_BY_STRUCT(20) \ - SECUREC_STRCPY_BY_STRUCT(21) \ - SECUREC_STRCPY_BY_STRUCT(22) \ - SECUREC_STRCPY_BY_STRUCT(23) \ - SECUREC_STRCPY_BY_STRUCT(24) \ - SECUREC_STRCPY_BY_STRUCT(25) \ - SECUREC_STRCPY_BY_STRUCT(26) \ - SECUREC_STRCPY_BY_STRUCT(27) \ - SECUREC_STRCPY_BY_STRUCT(28) \ - SECUREC_STRCPY_BY_STRUCT(29) \ - SECUREC_STRCPY_BY_STRUCT(30) \ - SECUREC_STRCPY_BY_STRUCT(31) \ - SECUREC_STRCPY_BY_STRUCT(32) \ - default:break; \ - } /* END switch */ \ - } else { \ - char *tmpStrDest = (char *)strDest; \ - const char *tmpStrSrc = (const char *)strSrc; \ - switch (srcStrLen) { \ - case 32: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 31: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 30: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 29: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 28: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 27: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 26: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 25: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 24: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 23: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 22: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 21: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 20: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 19: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 18: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 17: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 16: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 15: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 14: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 13: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 12: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 11: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 10: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 9: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 8: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 7: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 6: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 5: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 4: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 3: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 2: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - case 1: *tmpStrDest++ = *tmpStrSrc++; /* fall-through */ /* FALLTHRU */ \ - default:break; \ - } \ - } \ - } SECUREC_WHILE_ZERO - -static errno_t CheckSrcRange(char *strDest, size_t destMax, const char *strSrc) -{ - size_t tmpDestMax = destMax; - const char *tmpSrc = strSrc; - /* use destMax as boundary checker and destMax must be greater than zero */ - while (*(tmpSrc) != '\0' && tmpDestMax > 0) { - ++tmpSrc; - --tmpDestMax; - } - if (tmpDestMax == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("strcpy_s"); - return ERANGE_AND_RESET; - } - return EOK; -} - -errno_t strcpy_error(char *strDest, size_t destMax, const char *strSrc) -{ - if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("strcpy_s"); - return ERANGE; - } else if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("strcpy_s"); - if (strDest != NULL) { - strDest[0] = '\0'; - return EINVAL_AND_RESET; - } - return EINVAL; - } - return CheckSrcRange(strDest, destMax, strSrc); -} - -/******************************************************************************* - * - * The strcpy_s function copies the string pointed to strSrc - * (including the terminating null character) into the array pointed to by strDest - * The destination string must be large enough to hold the source string, - * including the terminating null character. strcpy_s will return EOVERLAP_AND_RESET - * if the source and destination strings overlap. - * - * - * strDest Location of destination string buffer - * destMax Size of the destination string buffer. - * strSrc Null-terminated source string buffer. - * - * - * strDest is updated. - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN - * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN - * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN - * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -errno_t strcpy_s(char *strDest, size_t destMax, const char *strSrc) -{ - if ((destMax > 0 && destMax <= SECUREC_STRING_MAX_LEN && strDest != NULL && strSrc != NULL && strDest != strSrc)) { -#if SECUREC_HAVE_STRNLEN - size_t srcStrLen = strnlen(strSrc, destMax) + 1; /* len include \0 */ -#else - size_t srcStrLen = destMax; /* use it to store the max length limit */ - const char *endPos = strSrc; - while (*(endPos) != '\0' && srcStrLen > 0) { /* use srcStrLen as boundary checker */ - ++endPos; - --srcStrLen; - } - srcStrLen = (size_t)(endPos - strSrc) + 1; /* len include \0 */ -#endif - /* le is high performance to lt */ - if (srcStrLen <= destMax) { - if ((strDest < strSrc && strDest + srcStrLen <= strSrc) || - (strSrc < strDest && strSrc + srcStrLen <= strDest)) { -#if SECUREC_IN_KERNEL - (void)memcpy(strDest, strSrc, srcStrLen); -#else - if (srcStrLen > SECUREC_STRCOPY_THRESHOLD_SIZE) { - (void)memcpy(strDest, strSrc, srcStrLen); - } else { - SECUREC_SMALL_STR_COPY; - } -#endif - return EOK; - } else { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strcpy_s"); - return EOVERLAP_AND_RESET; - } - - } - } - return strcpy_error(strDest, destMax, strSrc); -} - -#if SECUREC_IN_KERNEL -EXPORT_SYMBOL(strcpy_s); -#endif - diff --git a/base/src/securec/strcpy_s.c b/base/src/securec/strcpy_s.c new file mode 120000 index 0000000..bd84f05 --- /dev/null +++ b/base/src/securec/strcpy_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/strcpy_s.c \ No newline at end of file diff --git a/base/src/securec/strncat_s.c b/base/src/securec/strncat_s.c deleted file mode 100755 index f5dbc6c..0000000 --- a/base/src/securec/strncat_s.c +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -#if SECUREC_HAVE_STRNLEN -static errno_t SecDoStrncat(char *strDest, size_t destMax, const char *strSrc, size_t count) -{ - size_t destLen = strnlen(strDest, destMax); - /* The strSrc is no longer optimized. The reason is that when count is small, - * the efficiency of strnlen is higher than that of self realization. - */ - size_t srcLen = strnlen(strSrc, count); - - if ((strDest < strSrc && strDest + destLen + srcLen >= strSrc) || \ - (strSrc < strDest && strSrc + srcLen >= strDest)) { - strDest[0] = '\0'; - if (strDest + destLen <= strSrc && destLen == destMax) { - SECUREC_ERROR_INVALID_PARAMTER("strncat_s"); - return EINVAL_AND_RESET; - } - SECUREC_ERROR_BUFFER_OVERLAP("strncat_s"); - return EOVERLAP_AND_RESET; - } - if (srcLen + destLen >= destMax || strDest == strSrc) { - strDest[0] = '\0'; - if (destLen == destMax) { - SECUREC_ERROR_INVALID_PARAMTER("strncat_s"); - return EINVAL_AND_RESET; - } - SECUREC_ERROR_INVALID_RANGE("strncat_s"); - return ERANGE_AND_RESET; - } - (void)memcpy(strDest + destLen, strSrc, srcLen); /* no terminator */ - *(strDest + destLen + srcLen) = '\0'; - return EOK; -} -#else -static errno_t SecDoStrncat(char *strDest, size_t destMax, const char *strSrc, size_t count) -{ - char *tmpDest = strDest; - const char *tmpSrc = strSrc; - size_t availableSize = destMax; - size_t maxCount = count; - SECUREC_IN_REGISTER const char *overlapGuard = NULL; - - if (tmpDest < tmpSrc) { - overlapGuard = tmpSrc; - while (availableSize > 0 && *tmpDest != '\0') { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strncat_s"); - return EOVERLAP_AND_RESET; - } - ++tmpDest; - --availableSize; - } - - /* strDestination unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("strncat_s"); - return EINVAL_AND_RESET; - } - while (maxCount > 0 && (*tmpDest++ = *tmpSrc++) != '\0' && --availableSize > 0) { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strncat_s"); - return EOVERLAP_AND_RESET; - } - --maxCount; - } - } else { - overlapGuard = tmpDest; - while (availableSize > 0 && *tmpDest != '\0') { - /* seek to string end, and no need to check overlap */ - ++tmpDest; - --availableSize; - } - - /* strDest unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("strncat_s"); - return EINVAL_AND_RESET; - } - while (maxCount > 0 && (*tmpDest++ = *tmpSrc++) != '\0' && --availableSize > 0) { - if (tmpSrc == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strncat_s"); - return EOVERLAP_AND_RESET; - } - --maxCount; - } - } - if (maxCount == 0) { - *tmpDest = 0; /* add terminator to strDest */ - } - - /* strDest have not enough space,return error */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("strncat_s"); - return ERANGE_AND_RESET; - } - - return EOK; -} -#endif - -/******************************************************************************* - * - * The strncat_s function appends not more than n successive characters - * (not including the terminating null character) - * from the array pointed to by strSrc to the end of the string pointed to by strDest - * The strncat_s function try to append the first D characters of strSrc to - * the end of strDest, where D is the lesser of count and the length of strSrc. - * If appending those D characters will fit within strDest (whose size is given - * as destMax) and still leave room for a null terminator, then those characters - * are appended, starting at the original terminating null of strDest, and a - * new terminating null is appended; otherwise, strDest[0] is set to the null - * character. - * - * - * strDest Null-terminated destination string. - * destMax Size of the destination buffer. - * strSrc Null-terminated source string. - * count Number of character to append, or truncate. - * - * - * strDest is updated - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN - * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid)or - * (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN) - * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN - * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************** - */ -errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("strncat_s"); - return ERANGE; - } - - if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("strncat_s"); - if (strDest != NULL) { - strDest[0] = '\0'; - return EINVAL_AND_RESET; - } - return EINVAL; - } -#ifdef SECUREC_COMPATIBLE_WIN_FORMAT - if (count > SECUREC_STRING_MAX_LEN && count != (size_t)-1) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("strncat_s"); - return ERANGE_AND_RESET; - } -#else - if (count > SECUREC_STRING_MAX_LEN) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("strncat_s"); - return ERANGE_AND_RESET; - } -#endif - - return SecDoStrncat(strDest, destMax, strSrc, count); -} - -#if SECUREC_IN_KERNEL -EXPORT_SYMBOL(strncat_s); -#endif - diff --git a/base/src/securec/strncat_s.c b/base/src/securec/strncat_s.c new file mode 120000 index 0000000..fc42ddf --- /dev/null +++ b/base/src/securec/strncat_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/strncat_s.c \ No newline at end of file diff --git a/base/src/securec/strncpy_s.c b/base/src/securec/strncpy_s.c deleted file mode 100755 index 4685324..0000000 --- a/base/src/securec/strncpy_s.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions] Use unsafe function: Performance-sensitive - * [reason] Always used in the performance critical path, - * and sufficient input validation is performed before calling - */ - -#include "securecutil.h" - -static errno_t CheckSrcCountRange(char *strDest, size_t destMax, const char *strSrc, size_t count) -{ - size_t tmpDestMax = destMax; - size_t tmpCount = count; - const char *endPos = strSrc; - - /* use destMax and count as boundary checker and destMax must be greater than zero */ - while (*(endPos) != '\0' && tmpDestMax > 0 && tmpCount > 0) { - ++endPos; - --tmpCount; - --tmpDestMax; - } - if (tmpDestMax == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("strncpy_s"); - return ERANGE_AND_RESET; - } - return EOK; -} - -errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("strncpy_s"); - return ERANGE; - } else if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("strncpy_s"); - if (strDest != NULL) { - strDest[0] = '\0'; - return EINVAL_AND_RESET; - } - return EINVAL; - } else if (count > SECUREC_STRING_MAX_LEN) { - strDest[0] = '\0'; /* clear dest string */ - SECUREC_ERROR_INVALID_RANGE("strncpy_s"); - return ERANGE_AND_RESET; - } else if (count == 0) { - strDest[0] = '\0'; - return EOK; - } - - return CheckSrcCountRange(strDest, destMax, strSrc, count); -} - -/* The function compiler will be inlined and not placed in other files */ -static size_t SecMinStrLen(const char *str, size_t maxLen) -{ -#if SECUREC_HAVE_STRNLEN - size_t len = strnlen(str, maxLen); /* no ending terminator */ -#else - size_t len = 0; - /* use maxLen as boundary checker */ - const char *strEnd = str; - while (len < maxLen && *(strEnd) != '\0') { - ++strEnd; - ++len; /* no ending terminator */ - } -#endif - return len; -} - -/******************************************************************************* - * - * The strncpy_s function copies not more than n successive characters (not including the terminating null character) - * from the array pointed to by strSrc to the array pointed to by strDest. - * - * - * strDest Destination string. - * destMax The size of the destination string, in characters. - * strSrc Source string. - * count Number of characters to be copied. - * - * - * strDest is updated - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN - * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN - * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN - * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count) -{ -#ifdef SECUREC_COMPATIBLE_WIN_FORMAT - if ((destMax > 0 && destMax <= SECUREC_STRING_MAX_LEN && strDest != NULL && strSrc != NULL && - (count <= SECUREC_STRING_MAX_LEN || count == ((size_t)-1)) && count > 0)) { -#else - if ((destMax > 0 && destMax <= SECUREC_STRING_MAX_LEN && strDest != NULL && strSrc != NULL && - count <= SECUREC_STRING_MAX_LEN && count > 0)) { -#endif - size_t minCpLen; /* use it to store the maxi length limit */ - - if (count < destMax) { - minCpLen = SecMinStrLen(strSrc, count); /* no ending terminator */ - } else { - minCpLen = SecMinStrLen(strSrc, destMax); - if (minCpLen == destMax) { -#ifdef SECUREC_COMPATIBLE_WIN_FORMAT - if (count == ((size_t)-1)) { - /* Compatible with windows security function features */ - minCpLen = destMax - 1; - } else { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strncpy_s"); - return ERANGE_AND_RESET; - } -#else - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strncpy_s"); - return ERANGE_AND_RESET; -#endif - } - } - - if ((strDest < strSrc && strDest + minCpLen < strSrc) || - (strSrc < strDest && strSrc + minCpLen < strDest) || strDest == strSrc) { - /* Not overlap */ - (void)memcpy(strDest, strSrc, minCpLen); /* copy string without terminator */ - strDest[minCpLen] = '\0'; - return EOK; - } else { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("strncpy_s"); - return EOVERLAP_AND_RESET; - } - } - return strncpy_error(strDest, destMax, strSrc, count); -} - -#if SECUREC_IN_KERNEL -EXPORT_SYMBOL(strncpy_s); -#endif - diff --git a/base/src/securec/strncpy_s.c b/base/src/securec/strncpy_s.c new file mode 120000 index 0000000..3924424 --- /dev/null +++ b/base/src/securec/strncpy_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/strncpy_s.c \ No newline at end of file diff --git a/base/src/securec/strtok_s.c b/base/src/securec/strtok_s.c deleted file mode 100755 index 9ab5f2f..0000000 --- a/base/src/securec/strtok_s.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -static char *SecFindBegin(char *strToken, const char *strDelimit) -{ - /* Find beginning of token (skip over leading delimiters). Note that - * there is no token if this loop sets string to point to the terminal null. - */ - char *token = strToken; - while (*token != 0) { - const char *ctl = strDelimit; - while (*ctl != 0 && *ctl != *token) { - ++ctl; - } - - if (*ctl == 0) { /* don't find any delimiter in string header, break the loop */ - break; - } - ++token; - } - return token; -} - -static char *SecFindRest(char *strToken, const char *strDelimit) -{ - - /* Find the rest of the token. If it is not the end of the string, - * put a null there. - */ - char *token = strToken; - while (*token != 0) { - const char *ctl = strDelimit; - while (*ctl != 0 && *ctl != *token) { - ++ctl; - } - if (*ctl != 0) { /* find a delimiter */ - *token++ = 0; /* set string termintor */ - break; - } - ++token; - } - return token; -} - -/******************************************************************************* - * - * The strtok_s function parses a string into a sequence of tokens, - * On the first call to strtok_s the string to be parsed should be specified in strToken. - * In each subsequent call that should parse the same string, strToken should be NULL - * - * - * strToken String containing token or tokens. - * strDelimit Set of delimiter characters. - * context Used to store position information between calls - * to strtok_s - * - * - * context is updated - * - * Returns a pointer to the next token found in strToken. - * They return NULL when no more tokens are found. - * Each call modifies strToken by substituting a NULL character for the first - * delimiter that occurs after the returned token. - * - * return value condition - * NULL context is NULL, strDelimit is NULL, strToken is NULL - * and (*context) is NULL, or no token is found. - ******************************************************************************* - */ -char *strtok_s(char *strToken, const char *strDelimit, char **context) -{ - char *orgToken = strToken; - /* validate delimiter and string context */ - if (context == NULL || strDelimit == NULL) { - return NULL; - } - - /* valid input string and string pointer from where to search */ - if (orgToken == NULL && (*context) == NULL) { - return NULL; - } - - /* If string is null, continue searching from previous string position stored in context */ - if (orgToken == NULL) { - orgToken = *context; - } - - orgToken = SecFindBegin(orgToken, strDelimit); - - { - char *token = orgToken; /* point to updated position */ - - orgToken = SecFindRest(orgToken, strDelimit); - - /* record string position for next search in the context */ - *context = orgToken; - - /* Determine if a token has been found. */ - if (token == orgToken) { - token = NULL; - } - return token; - } -} - - diff --git a/base/src/securec/strtok_s.c b/base/src/securec/strtok_s.c new file mode 120000 index 0000000..af89a59 --- /dev/null +++ b/base/src/securec/strtok_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/strtok_s.c \ No newline at end of file diff --git a/base/src/securec/swprintf_s.c b/base/src/securec/swprintf_s.c deleted file mode 100755 index 61eff40..0000000 --- a/base/src/securec/swprintf_s.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The swprintf_s function is the wide-character equivalent of the sprintf_s function - * - * - * strDest Storage location for the output. - * destMax Maximum number of characters to store. - * format Format-control string. - * ... Optional arguments - * - * - * strDest is updated - * - * - * return the number of wide characters stored in strDest, not counting the terminating null wide character. - * return -1 if an error occurred. - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -#ifndef __MINGW32__ -int swprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vswprintf_s(strDest, destMax, format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} -#endif - diff --git a/base/src/securec/swprintf_s.c b/base/src/securec/swprintf_s.c new file mode 120000 index 0000000..196d51e --- /dev/null +++ b/base/src/securec/swprintf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/swprintf_s.c \ No newline at end of file diff --git a/base/src/securec/swscanf_s.c b/base/src/securec/swscanf_s.c deleted file mode 100755 index ab3850a..0000000 --- a/base/src/securec/swscanf_s.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * The swscanf_s function is the wide-character equivalent of the sscanf_s function - * The swscanf_s function reads data from buffer into the location given by - * each argument. Every argument must be a pointer to a variable with a type - * that corresponds to a type specifier in format. The format argument controls - * the interpretation of the input fields and has the same form and function - * as the format argument for the scanf function. If copying takes place between - * strings that overlap, the behavior is undefined. - * - * - * buffer Stored data. - * format Format control string, see Format Specifications. - * ... Optional arguments. - * - * - * ... the converted value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; The return value does not include fields that were read but not - * assigned. - * A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -#ifndef __MINGW32__ -int swscanf_s(const wchar_t *buffer, const wchar_t *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vswscanf_s(buffer, format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} -#endif - diff --git a/base/src/securec/swscanf_s.c b/base/src/securec/swscanf_s.c new file mode 120000 index 0000000..e787252 --- /dev/null +++ b/base/src/securec/swscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/swscanf_s.c \ No newline at end of file diff --git a/base/src/securec/vfscanf_s.c b/base/src/securec/vfscanf_s.c deleted file mode 100755 index 20a610d..0000000 --- a/base/src/securec/vfscanf_s.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secinput.h" - -/******************************************************************************* - * - * The vfscanf_s function is equivalent to fscanf_s, with the variable argument list replaced by arglist - * The vfscanf_s function reads data from the current position of stream into - * the locations given by argument (if any). Each argument must be a pointer - * to a variable of a type that corresponds to a type specifier in format. - * format controls the interpretation of the input fields and has the same - * form and function as the format argument for scanf. - * - * - * stream Pointer to FILE structure. - * format Format control string, see Format Specifications. - * arglist pointer to list of arguments - * - * - * arglist the converted value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; the return value does not include fields that were read but - * not assigned. A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -int vfscanf_s(FILE *stream, const char *format, va_list arglist) -{ - int retVal; /* If initialization causes e838 */ - SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM; - - if ((stream == NULL) || (format == NULL)) { - SECUREC_ERROR_INVALID_PARAMTER("vfscanf_s"); - return SECUREC_SCANF_EINVAL; - } - if (stream == stdin) { - return vscanf_s(format, arglist); - } - - SECUREC_LOCK_FILE(stream); - fStr.pf = stream; - fStr.flag = SECUREC_FILE_STREAM_FLAG; - fStr.oriFilePos = SECUREC_UNINITIALIZED_FILE_POS; - - retVal = SecInputS(&fStr, format, arglist); - - SECUREC_UNLOCK_FILE(stream); - if (retVal < 0) { - SECUREC_ERROR_INVALID_PARAMTER("vfscanf_s"); - return SECUREC_SCANF_EINVAL; - } - - return retVal; -} - - diff --git a/base/src/securec/vfscanf_s.c b/base/src/securec/vfscanf_s.c new file mode 120000 index 0000000..0caf9b2 --- /dev/null +++ b/base/src/securec/vfscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vfscanf_s.c \ No newline at end of file diff --git a/base/src/securec/vfwscanf_s.c b/base/src/securec/vfwscanf_s.c deleted file mode 100755 index 6bd179a..0000000 --- a/base/src/securec/vfwscanf_s.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secinput.h" - -/******************************************************************************* - * - * The vfwscanf_s function is the wide-character equivalent of the vfscanf_s function - * The vfwscanf_s function reads data from the current position of stream into - * the locations given by argument (if any). Each argument must be a pointer - * to a variable of a type that corresponds to a type specifier in format. - * format controls the interpretation of the input fields and has the same form - * and function as the format argument for scanf; see scanf for a description - * of format. - * - * - * stream Pointer to FILE structure. - * format Format control string, see Format Specifications. - * arglist pointer to list of arguments - * - * - * arglist the converted value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; the return value does not include fields that were read but - * not assigned. A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -int vfwscanf_s(FILE *stream, const wchar_t *format, va_list arglist) -{ - int retVal; /* If initialization causes e838 */ - SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM; - - if ((stream == NULL) || (format == NULL)) { - SECUREC_ERROR_INVALID_PARAMTER("vfwscanf_s"); - return SECUREC_SCANF_EINVAL; - } - if (stream == stdin) { - return vwscanf_s(format, arglist); - } - - SECUREC_LOCK_FILE(stream); - - fStr.pf = stream; - fStr.flag = SECUREC_FILE_STREAM_FLAG; - fStr.oriFilePos = SECUREC_UNINITIALIZED_FILE_POS; - - retVal = SecInputSW(&fStr, format, arglist); - SECUREC_UNLOCK_FILE(stream); - if (retVal < 0) { - SECUREC_ERROR_INVALID_PARAMTER("vfwscanf_s"); - return SECUREC_SCANF_EINVAL; - } - return retVal; -} - - diff --git a/base/src/securec/vfwscanf_s.c b/base/src/securec/vfwscanf_s.c new file mode 120000 index 0000000..56035ca --- /dev/null +++ b/base/src/securec/vfwscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vfwscanf_s.c \ No newline at end of file diff --git a/base/src/securec/vscanf_s.c b/base/src/securec/vscanf_s.c deleted file mode 100755 index 5418495..0000000 --- a/base/src/securec/vscanf_s.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secinput.h" - -/******************************************************************************* - * - * The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by arglist, - * The vscanf_s function reads data from the standard input stream stdin and - * writes the data into the location that's given by argument. Each argument - * must be a pointer to a variable of a type that corresponds to a type specifier - * in format. If copying occurs between strings that overlap, the behavior is - * undefined. - * - * - * format Format control string. - * arglist pointer to list of arguments - * - * - * arglist the converted value stored in user assigned address - * - * - * Returns the number of fields successfully converted and assigned; - * the return value does not include fields that were read but not assigned. - * A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -int vscanf_s(const char *format, va_list arglist) -{ - int retVal; /* If initialization causes e838 */ - SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM; - /* - * "va_list" has different definition on different platform, so we can't use arglist == NULL - * to determine it's invalid. If you has fixed platform, you can check some fields to validate it, - * such as "arglist == NULL" or arglist.xxx != NULL or *(size_t *)&arglist != 0. - */ - fStr.pf = stdin; - if (format == NULL || fStr.pf == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("vscanf_s"); - return SECUREC_SCANF_EINVAL; - } - - fStr.flag = SECUREC_FROM_STDIN_FLAG; - - SECUREC_LOCK_STDIN(0, fStr.pf); - - retVal = SecInputS(&fStr, format, arglist); - - SECUREC_UNLOCK_STDIN(0, fStr.pf); - if (retVal < 0) { - SECUREC_ERROR_INVALID_PARAMTER("vscanf_s"); - return SECUREC_SCANF_EINVAL; - } - return retVal; -} - - diff --git a/base/src/securec/vscanf_s.c b/base/src/securec/vscanf_s.c new file mode 120000 index 0000000..21d8c7c --- /dev/null +++ b/base/src/securec/vscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vscanf_s.c \ No newline at end of file diff --git a/base/src/securec/vsnprintf_s.c b/base/src/securec/vsnprintf_s.c deleted file mode 100755 index eee4152..0000000 --- a/base/src/securec/vsnprintf_s.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -/******************************************************************************* - * - * The vsnprintf_s function is equivalent to the vsnprintf function - * except for the parameter destMax/count and the explicit runtime-constraints violation - * The vsnprintf_s function takes a pointer to an argument list, then formats - * and writes up to count characters of the given data to the memory pointed - * to by strDest and appends a terminating null. - * - * - * strDest Storage location for the output. - * destMax The size of the strDest for output. - * count Maximum number of character to write(not including - * the terminating NULL) - * format Format-control string. - * arglist pointer to list of arguments. - * - * - * strDest is updated - * - * - * return the number of characters written, not including the terminating null - * return -1 if an error occurs. - * return -1 if count < destMax and the output string has been truncated - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -#ifndef __MINGW32__ -int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format, va_list arglist) -{ - int retVal; - - if (format == NULL || strDest == NULL || destMax == 0 || destMax > SECUREC_STRING_MAX_LEN || - (count > (SECUREC_STRING_MAX_LEN - 1) && count != (size_t)-1)) { - if (strDest != NULL && destMax > 0) { - strDest[0] = '\0'; - } - SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_s"); - return -1; - } - - if (destMax > count) { - retVal = SecVsnprintfImpl(strDest, count + 1, format, arglist); - if (retVal == SECUREC_PRINTF_TRUNCATE) { /* lsd add to keep dest buffer not destroyed 2014.2.18 */ - /* the string has been truncated, return -1 */ - return -1; /* to skip error handler, return strlen(strDest) or -1 */ - } - } else { /* destMax <= count */ - retVal = SecVsnprintfImpl(strDest, destMax, format, arglist); -#ifdef SECUREC_COMPATIBLE_WIN_FORMAT - if (retVal == SECUREC_PRINTF_TRUNCATE && count == (size_t)-1) { - return -1; - } -#endif - } - - if (retVal < 0) { - strDest[0] = '\0'; /* empty the dest strDest */ - - if (retVal == SECUREC_PRINTF_TRUNCATE) { - /* Buffer too small */ - SECUREC_ERROR_INVALID_RANGE("vsnprintf_s"); - } - - SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_s"); - return -1; - } - - return retVal; -} -#endif - -#if defined(__MINGW32__) || defined(SECUREC_SNPRINTF_TRUNCATED) -int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format, va_list arglist) -{ - int retVal; - - if (format == NULL || strDest == NULL || destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) { - if (strDest != NULL && destMax > 0) { - strDest[0] = '\0'; - } - SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_ex_s"); - return -1; - } - - retVal = SecVsnprintfImpl(strDest, destMax, format, arglist); - - if (retVal < 0) { - if (retVal == SECUREC_PRINTF_TRUNCATE) { - return (int)(destMax - 1); /* to skip error handler, return strlen(strDest) */ - } - strDest[0] = '\0'; /* empty the dest strDest */ - SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_ex_s"); - return -1; - } - - return retVal; -} -#endif - - diff --git a/base/src/securec/vsnprintf_s.c b/base/src/securec/vsnprintf_s.c new file mode 120000 index 0000000..21bb462 --- /dev/null +++ b/base/src/securec/vsnprintf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vsnprintf_s.c \ No newline at end of file diff --git a/base/src/securec/vsprintf_s.c b/base/src/securec/vsprintf_s.c deleted file mode 100755 index 624dce6..0000000 --- a/base/src/securec/vsprintf_s.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -/******************************************************************************* - * - * The vsprintf_s function is equivalent to the vsprintf function - * except for the parameter destMax and the explicit runtime-constraints violation - * The vsprintf_s function takes a pointer to an argument list, and then formats - * and writes the given data to the memory pointed to by strDest. - * The function differ from the non-secure versions only in that the secure - * versions support positional parameters. - * - * - * strDest Storage location for the output. - * destMax Size of strDest - * format Format specification. - * arglist pointer to list of arguments - * - * - * strDest is updated - * - * - * return the number of characters written, not including the terminating null character, - * return -1 if an error occurs. - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -#ifndef __MINGW32__ -int vsprintf_s(char *strDest, size_t destMax, const char *format, va_list arglist) -{ - int retVal; /* If initialization causes e838 */ - - if (format == NULL || strDest == NULL || destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) { - if (strDest != NULL && destMax > 0) { - strDest[0] = '\0'; - } - SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s"); - return -1; - } - - retVal = SecVsnprintfImpl(strDest, destMax, format, arglist); - - if (retVal < 0) { - strDest[0] = '\0'; - if (retVal == SECUREC_PRINTF_TRUNCATE) { - /* Buffer is too small */ - SECUREC_ERROR_INVALID_RANGE("vsprintf_s"); - } - SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s"); - return -1; - } - - return retVal; -} -#endif - diff --git a/base/src/securec/vsprintf_s.c b/base/src/securec/vsprintf_s.c new file mode 120000 index 0000000..576d332 --- /dev/null +++ b/base/src/securec/vsprintf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vsprintf_s.c \ No newline at end of file diff --git a/base/src/securec/vsscanf_s.c b/base/src/securec/vsscanf_s.c deleted file mode 100755 index dda7b0f..0000000 --- a/base/src/securec/vsscanf_s.c +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secinput.h" -#if defined(SECUREC_VXWORKS_PLATFORM) && !defined(SECUREC_SYSAPI4VXWORKS) -#include -#endif - -/******************************************************************************* - * - * vsscanf_s - * - * - * - * The vsscanf_s function is equivalent to sscanf_s, with the variable argument list replaced by arglist - * The vsscanf_s function reads data from buffer into the location given by - * each argument. Every argument must be a pointer to a variable with a type - * that corresponds to a type specifier in format. The format argument controls - * the interpretation of the input fields and has the same form and function - * as the format argument for the scanf function. - * If copying takes place between strings that overlap, the behavior is undefined. - * - * - * buffer Stored data - * format Format control string, see Format Specifications. - * arglist pointer to list of arguments - * - * - * arglist the converted value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; the return value does not include fields that were read but - * not assigned. A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -#ifndef __MINGW32__ -int vsscanf_s(const char *buffer, const char *format, va_list arglist) -{ - SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM; - int retVal; /* If initialization causes e838 */ - size_t count; /* If initialization causes e838 */ - - /* validation section */ - if (buffer == NULL || format == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("vsscanf_s"); - return SECUREC_SCANF_EINVAL; - } - count = strlen(buffer); - if (count == 0 || count > SECUREC_STRING_MAX_LEN) { - SecClearDestBuf(buffer, format, arglist); - SECUREC_ERROR_INVALID_PARAMTER("vsscanf_s"); - return SECUREC_SCANF_EINVAL; - } -#ifdef SECUREC_VXWORKS_PLATFORM - if (isspace((int)buffer[0]) && isspace((int)buffer[count - 1])) { - SecClearDestBuf(buffer, format, arglist); - } -#endif - fStr.flag = SECUREC_MEM_STR_FLAG; - fStr.cur = (const char *)buffer; - fStr.count = (int)count; - retVal = SecInputS(&fStr, format, arglist); - if (retVal < 0) { - SECUREC_ERROR_INVALID_PARAMTER("vsscanf_s"); - return SECUREC_SCANF_EINVAL; - } - - return retVal; -} -#endif - diff --git a/base/src/securec/vsscanf_s.c b/base/src/securec/vsscanf_s.c new file mode 120000 index 0000000..c6b3c51 --- /dev/null +++ b/base/src/securec/vsscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vsscanf_s.c \ No newline at end of file diff --git a/base/src/securec/vswprintf_s.c b/base/src/securec/vswprintf_s.c deleted file mode 100755 index eaf3f7f..0000000 --- a/base/src/securec/vswprintf_s.c +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secureprintoutput.h" -#ifndef __MINGW32__ -static int SecVswprintfImpl(wchar_t *string, size_t sizeInWchar, const wchar_t *format, va_list arglist) -{ - SecPrintfStream str; - int retVal; /* If initialization causes e838 */ - - str.cur = (char *)string; - str.count = (int)(sizeInWchar * sizeof(wchar_t)); - - retVal = SecOutputSW(&str, format, arglist); - if ((retVal >= 0) && SecPutWcharStrEndingZero(&str, (int)sizeof(wchar_t))) { - return (retVal); - } else if (str.count < 0) { - /* the buffer was too small; we return truncation */ - string[sizeInWchar - 1] = 0; - return SECUREC_PRINTF_TRUNCATE; - } - return -1; - -} - -/******************************************************************************* - * - * The vswprintf_s function is the wide-character equivalent of the vsprintf_s function - * - * - * strDest Storage location for the output. - * destMax Size of strDest - * format Format specification. - * arglist pointer to list of arguments - * - * - * strDest is updated - * - * - * return the number of wide characters stored in strDest, not counting the terminating null wide character. - * return -1 if an error occurred. - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ - -int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list arglist) -{ - int retVal; /* If initialization causes e838 */ - - if (format == NULL || strDest == NULL || destMax == 0 || destMax > (SECUREC_WCHAR_STRING_MAX_LEN)) { - if (strDest != NULL && destMax > 0) { - strDest[0] = '\0'; - } - SECUREC_ERROR_INVALID_PARAMTER("vswprintf_s"); - return -1; - } - - retVal = SecVswprintfImpl(strDest, destMax, format, arglist); - - if (retVal < 0) { - strDest[0] = '\0'; - if (retVal == SECUREC_PRINTF_TRUNCATE) { - /* Buffer too small */ - SECUREC_ERROR_INVALID_RANGE("vswprintf_s"); - } - SECUREC_ERROR_INVALID_PARAMTER("vswprintf_s"); - return -1; - } - - return retVal; -} -#endif - diff --git a/base/src/securec/vswprintf_s.c b/base/src/securec/vswprintf_s.c new file mode 120000 index 0000000..22023a8 --- /dev/null +++ b/base/src/securec/vswprintf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vswprintf_s.c \ No newline at end of file diff --git a/base/src/securec/vswscanf_s.c b/base/src/securec/vswscanf_s.c deleted file mode 100755 index f917774..0000000 --- a/base/src/securec/vswscanf_s.c +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secinput.h" - -static size_t SecWcslen(const wchar_t *s) -{ - const wchar_t *end = s; - while (*end != L'\0') { - ++end; - } - return ((size_t)((end - s))); -} - -/******************************************************************************* - * - * The vswscanf_s function is the wide-character equivalent of the vsscanf_s function - * The vsscanf_s function reads data from buffer into the location given by - * each argument. Every argument must be a pointer to a variable with a type - * that corresponds to a type specifier in format. - * The format argument controls the interpretation of the input fields and - * has the same form and function as the format argument for the scanf function. - * If copying takes place between strings that overlap, the behavior is undefined. - * - * - * buffer Stored data - * format Format control string, see Format Specifications. - * arglist pointer to list of arguments - * - * - * arglist the converted value stored in user assigned address - * - * - * Each of these functions returns the number of fields successfully converted - * and assigned; the return value does not include fields that were read but - * not assigned. A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list arglist) -{ - SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM; - int retVal; /* If initialization causes e838 */ - size_t count; /* If initialization causes e838 */ - - /* validation section */ - if (buffer == NULL || format == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s"); - return SECUREC_SCANF_EINVAL; - } - count = SecWcslen(buffer); - if (count == 0 || count > SECUREC_WCHAR_STRING_MAX_LEN) { - SecClearDestBufW(buffer, format, arglist); - SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s"); - return SECUREC_SCANF_EINVAL; - } - - fStr.flag = SECUREC_MEM_STR_FLAG; - fStr.cur = (const char *)buffer; - fStr.count = (int)count *((int)sizeof(wchar_t)); - - retVal = SecInputSW(&fStr, format, arglist); - if (retVal < 0) { - SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s"); - return SECUREC_SCANF_EINVAL; - } - return retVal; -} - - diff --git a/base/src/securec/vswscanf_s.c b/base/src/securec/vswscanf_s.c new file mode 120000 index 0000000..ad40dc0 --- /dev/null +++ b/base/src/securec/vswscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vswscanf_s.c \ No newline at end of file diff --git a/base/src/securec/vwscanf_s.c b/base/src/securec/vwscanf_s.c deleted file mode 100755 index 401a28e..0000000 --- a/base/src/securec/vwscanf_s.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "secinput.h" - -/******************************************************************************* - * - * The vwscanf_s function is the wide-character equivalent of the vscanf_s function - * The vwscanf_s function is the wide-character version of vscanf_s. The - * function reads data from the standard input stream stdin and writes the - * data into the location that's given by argument. Each argument must be a - * pointer to a variable of a type that corresponds to a type specifier in - * format. If copying occurs between strings that overlap, the behavior is - * undefined. - * - * - * format Format control string. - * arglist pointer to list of arguments - * - * - * arglist the converted value stored in user assigned address - * - * - * Returns the number of fields successfully converted and assigned; - * the return value does not include fields that were read but not assigned. - * A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ -int vwscanf_s(const wchar_t *format, va_list arglist) -{ - int retVal; /* If initialization causes e838 */ - SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM; - - fStr.pf = stdin; - if (format == NULL || fStr.pf == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("vwscanf_s"); - return SECUREC_SCANF_EINVAL; - } - - fStr.flag = SECUREC_FROM_STDIN_FLAG; - - SECUREC_LOCK_STDIN(0, fStr.pf); - - retVal = SecInputSW(&fStr, format, arglist); - - SECUREC_UNLOCK_STDIN(0, fStr.pf); - - if (retVal < 0) { - SECUREC_ERROR_INVALID_PARAMTER("vwscanf_s"); - return SECUREC_SCANF_EINVAL; - } - - return retVal; -} - - diff --git a/base/src/securec/vwscanf_s.c b/base/src/securec/vwscanf_s.c new file mode 120000 index 0000000..bff8ed3 --- /dev/null +++ b/base/src/securec/vwscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/vwscanf_s.c \ No newline at end of file diff --git a/base/src/securec/wcscat_s.c b/base/src/securec/wcscat_s.c deleted file mode 100755 index d8b9ba9..0000000 --- a/base/src/securec/wcscat_s.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -static errno_t SecDoWcscat(wchar_t *strDest, size_t destMax, const wchar_t *strSrc) -{ - wchar_t *tmpDest = strDest; - const wchar_t *tmpSrc = strSrc; - size_t availableSize = destMax; - SECUREC_IN_REGISTER const wchar_t *overlapGuard = NULL; - - if (tmpDest < tmpSrc) { - overlapGuard = tmpSrc; - while (availableSize > 0 && *tmpDest != '\0') { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcscat_s"); - return EOVERLAP_AND_RESET; - } - /* seek to string end */ - ++tmpDest; - --availableSize; - } - - /* strDest unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("wcscat_s"); - return EINVAL_AND_RESET; - } - - /* if 2014-2018 > 0, then execute the strcat operation */ - while ((*tmpDest++ = *tmpSrc++) != '\0' && --availableSize > 0) { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcscat_s"); - return EOVERLAP_AND_RESET; - } - } - } else { - overlapGuard = tmpDest; - while (availableSize > 0 && *tmpDest != '\0') { - /* seek to string end, and no need to check overlap */ - ++tmpDest; - --availableSize; - } - /* strDest unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("wcscat_s"); - return EINVAL_AND_RESET; - } - while ((*tmpDest++ = *tmpSrc++) != '\0' && --availableSize > 0) { - if (tmpSrc == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcscat_s"); - return EOVERLAP_AND_RESET; - } - } - } - - /* strDest have not enough space, return error */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("wcscat_s"); - return ERANGE_AND_RESET; - } - return EOK; -} - -/******************************************************************************* - * - * The wcscat_s function appends a copy of the wide string pointed to by strSrc -* (including the terminating null wide character) - * to the end of the wide string pointed to by strDest. - * The arguments and return value of wcscat_s are wide-character strings. - * - * The wcscat_s function appends strSrc to strDest and terminates the resulting - * string with a null character. The initial character of strSrc overwrites the - * terminating null character of strDest. wcscat_s will return EOVERLAP_AND_RESET if the - * source and destination strings overlap. - * - * Note that the second parameter is the total size of the buffer, not the - * remaining size. - * - * - * strDest Null-terminated destination string buffer. - * destMax Size of the destination string buffer. - * strSrc Null-terminated source string buffer. - * - * - * strDest is updated - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN - * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid) or - * (strDest != NULL and strSrc is NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN) - * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0 - * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc) -{ - if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("wcscat_s"); - return ERANGE; - } - - if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("wcscat_s"); - if (strDest != NULL) { - strDest[0] = '\0'; - return EINVAL_AND_RESET; - } - return EINVAL; - } - - return SecDoWcscat(strDest, destMax, strSrc); -} - - diff --git a/base/src/securec/wcscat_s.c b/base/src/securec/wcscat_s.c new file mode 120000 index 0000000..43a3f10 --- /dev/null +++ b/base/src/securec/wcscat_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wcscat_s.c \ No newline at end of file diff --git a/base/src/securec/wcscpy_s.c b/base/src/securec/wcscpy_s.c deleted file mode 100755 index 726f0ef..0000000 --- a/base/src/securec/wcscpy_s.c +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -static errno_t SecDoWcscpy(wchar_t *strDest, size_t destMax, const wchar_t *strSrc) -{ - wchar_t *tmpDest = strDest; - const wchar_t *tmpSrc = strSrc; - size_t maxSize = destMax; - SECUREC_IN_REGISTER const wchar_t *overlapGuard = NULL; - - if (tmpDest < tmpSrc) { - overlapGuard = tmpSrc; - while ((*(tmpDest++) = *(tmpSrc++)) != '\0' && --maxSize > 0) { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s"); - return EOVERLAP_AND_RESET; - } - } - } else { - overlapGuard = tmpDest; - while ((*(tmpDest++) = *(tmpSrc++)) != '\0' && --maxSize > 0) { - if (tmpSrc == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s"); - return EOVERLAP_AND_RESET; - } - } - } - - if (maxSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("wcscpy_s"); - return ERANGE_AND_RESET; - } - return EOK; -} - -/******************************************************************************* - * - * The wcscpy_s function copies the wide string pointed to by strSrc - * (including theterminating null wide character) into the array pointed to by strDest - - * - * strDest Destination string buffer - * destMax Size of the destination string buffer. - * strSrc Null-terminated source string buffer. - * - * - * strDest is updated. - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN - * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN - * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0 - * ERANGE_AND_RESET destMax <= length of strSrc and strDest != strSrc - * and strDest != NULL and strSrc != NULL and destMax != 0 and - * destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and destMax != 0 - * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN and - * strDest != NULL and strSrc !=NULL and strDest != strSrc - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc) -{ - if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("wcscpy_s"); - return ERANGE; - } - if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("wcscpy_s"); - if (strDest != NULL) { - strDest[0] = L'\0'; - return EINVAL_AND_RESET; - } - - return EINVAL; - } - if (strDest == strSrc) { - return EOK; - } - return SecDoWcscpy(strDest, destMax, strSrc); -} - - diff --git a/base/src/securec/wcscpy_s.c b/base/src/securec/wcscpy_s.c new file mode 120000 index 0000000..30881ff --- /dev/null +++ b/base/src/securec/wcscpy_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wcscpy_s.c \ No newline at end of file diff --git a/base/src/securec/wcsncat_s.c b/base/src/securec/wcsncat_s.c deleted file mode 100755 index 8116fd9..0000000 --- a/base/src/securec/wcsncat_s.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -static errno_t SecDoWcsncat(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count) -{ - wchar_t *tmpDest = strDest; - const wchar_t *tmpSrc = strSrc; - size_t availableSize = destMax; - size_t maxCount = count; - SECUREC_IN_REGISTER const wchar_t *overlapGuard = NULL; - - if (tmpDest < tmpSrc) { - overlapGuard = tmpSrc; - while (availableSize > 0 && *tmpDest != '\0') { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcsncat_s"); - return EOVERLAP_AND_RESET; - } - ++tmpDest; - --availableSize; - } - /* strDestination unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s"); - return EINVAL_AND_RESET; - } - while (maxCount > 0 && (*tmpDest++ = *tmpSrc++) != '\0' && --availableSize > 0) { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcsncat_s"); - return EOVERLAP_AND_RESET; - } - --maxCount; - } - } else { - overlapGuard = tmpDest; - while (availableSize > 0 && *tmpDest != '\0') { - /* seek to string end, and no need to check overlap */ - ++tmpDest; - --availableSize; - } - - /* strDest unterminated, return error. */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s"); - return EINVAL_AND_RESET; - } - while (maxCount > 0 && (*tmpDest++ = *tmpSrc++) != '\0' && --availableSize > 0) { - if (tmpSrc == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcsncat_s"); - return EOVERLAP_AND_RESET; - } - --maxCount; - } - } - if (maxCount == 0) { - *tmpDest = '\0'; /* add terminator to strDest */ - } - - /* strDest have no space to store the terminator, return error */ - if (availableSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("wcsncat_s"); - return ERANGE_AND_RESET; - } - return EOK; -} - -/******************************************************************************* - * - * The wcsncat_s function appends not more than n successive wide characters - * (not including the terminating null wide character) - * from the array pointed to by strSrc to the end of the wide string pointed to by strDest. - * - * The wcsncat_s function try to append the first D characters of strSrc to - * the end of strDest, where D is the lesser of count and the length of strSrc. - * If appending those D characters will fit within strDest (whose size is - * given as destMax) and still leave room for a null terminator, then those - * characters are appended, starting at the original terminating null of - * strDest, and a new terminating null is appended; otherwise, strDest[0] is - * set to the null character. - * - * - * strDest Null-terminated destination string. - * destMax Size of the destination buffer. - * strSrc Null-terminated source string. - * count Number of character to append, or truncate. - * - * - * strDest is updated - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN - * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid) or - * (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN) - * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0 - * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("wcsncat_s"); - return ERANGE; - } - - if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s"); - if (strDest != NULL) { - strDest[0] = '\0'; - return EINVAL_AND_RESET; - } - return EINVAL; - } -#ifdef SECUREC_COMPATIBLE_WIN_FORMAT - if (count > SECUREC_WCHAR_STRING_MAX_LEN && count != ((size_t)-1)) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("wcsncat_s"); - return ERANGE_AND_RESET; - } -#else - if (count > SECUREC_WCHAR_STRING_MAX_LEN) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("wcsncat_s"); - return ERANGE_AND_RESET; - } -#endif - - return SecDoWcsncat(strDest, destMax, strSrc, count); -} - - diff --git a/base/src/securec/wcsncat_s.c b/base/src/securec/wcsncat_s.c new file mode 120000 index 0000000..8954cff --- /dev/null +++ b/base/src/securec/wcsncat_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wcsncat_s.c \ No newline at end of file diff --git a/base/src/securec/wcsncpy_s.c b/base/src/securec/wcsncpy_s.c deleted file mode 100755 index 702fc05..0000000 --- a/base/src/securec/wcsncpy_s.c +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securecutil.h" - -static errno_t SecDoWcsncpy(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count) -{ - wchar_t *tmpDest = strDest; - const wchar_t *tmpSrc = strSrc; - size_t maxSize = destMax; - size_t maxCount = count; - SECUREC_IN_REGISTER const wchar_t *overlapGuard = NULL; - - if (tmpDest < tmpSrc) { - overlapGuard = tmpSrc; - while ((*(tmpDest++) = *(tmpSrc++)) != '\0' && --maxSize > 0 && --maxCount > 0) { - if (tmpDest == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcsncpy_s"); - return EOVERLAP_AND_RESET; - } - } - } else { - overlapGuard = tmpDest; - while ((*(tmpDest++) = *(tmpSrc++)) != '\0' && --maxSize > 0 && --maxCount > 0) { - if (tmpSrc == overlapGuard) { - strDest[0] = '\0'; - SECUREC_ERROR_BUFFER_OVERLAP("wcsncpy_s"); - return EOVERLAP_AND_RESET; - } - } - } - if (maxCount == 0) { - *tmpDest = '\0'; - } - - if (maxSize == 0) { - strDest[0] = '\0'; - SECUREC_ERROR_INVALID_RANGE("wcsncpy_s"); - return ERANGE_AND_RESET; - } - return EOK; -} - -/******************************************************************************* - * - * The wcsncpy_s function copies not more than n successive wide characters - * (not including the terminating null wide character) - * from the array pointed to by strSrc to the array pointed to by strDest - * - * - * strDest Destination string. - * destMax The size of the destination string, in characters. - * strSrc Source string. - * count Number of characters to be copied. - * - * - * strDest is updated - * - * - * EOK Success - * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN - * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN - * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0 - * ERANGE_AND_RESET count > SECUREC_WCHAR_STRING_MAX_LEN or - * (destMax <= length of strSrc and destMax <= count and strDest != strSrc - * and strDest != NULL and strSrc != NULL and destMax != 0 and - * destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap) - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid - * - * - * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid - ******************************************************************************* - */ -errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) { - SECUREC_ERROR_INVALID_RANGE("wcsncpy_s"); - return ERANGE; - } - - if (strDest == NULL || strSrc == NULL) { - SECUREC_ERROR_INVALID_PARAMTER("wcsncpy_s"); - if (strDest != NULL) { - strDest[0] = '\0'; - return EINVAL_AND_RESET; - } - return EINVAL; - } -#ifdef SECUREC_COMPATIBLE_WIN_FORMAT - if (count > SECUREC_WCHAR_STRING_MAX_LEN && count != (size_t)-1) { - strDest[0] = '\0'; /* clear dest string */ - SECUREC_ERROR_INVALID_RANGE("wcsncpy_s"); - return ERANGE_AND_RESET; - } -#else - if (count > SECUREC_WCHAR_STRING_MAX_LEN) { - strDest[0] = '\0'; /* clear dest string */ - SECUREC_ERROR_INVALID_RANGE("wcsncpy_s"); - return ERANGE_AND_RESET; - } -#endif - if (count == 0) { - strDest[0] = '\0'; - return EOK; - } - - return SecDoWcsncpy(strDest, destMax, strSrc, count); -} - - diff --git a/base/src/securec/wcsncpy_s.c b/base/src/securec/wcsncpy_s.c new file mode 120000 index 0000000..b5a2b58 --- /dev/null +++ b/base/src/securec/wcsncpy_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wcsncpy_s.c \ No newline at end of file diff --git a/base/src/securec/wcstok_s.c b/base/src/securec/wcstok_s.c deleted file mode 100755 index a23d600..0000000 --- a/base/src/securec/wcstok_s.c +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -static wchar_t *SecFindBeginW(wchar_t *strToken, const wchar_t *strDelimit) -{ - /* Find beginning of token (skip over leading delimiters). Note that - * there is no token if this loop sets string to point to the terminal null. - */ - wchar_t *token = strToken; - while (*token != 0) { - const wchar_t *ctl = strDelimit; - while (*ctl != 0 && *ctl != *token) { - ++ctl; - } - if (*ctl == 0) { - break; - } - ++token; - } - return token; -} - -static wchar_t *SecFindRestW(wchar_t *strToken, const wchar_t *strDelimit) -{ - - /* Find the end of the token. If it is not the end of the string, - * put a null there. - */ - wchar_t *token = strToken; - while (*token != 0) { - const wchar_t *ctl = strDelimit; - while (*ctl != 0 && *ctl != *token) { - ++ctl; - } - if (*ctl != 0) { - *token++ = 0; - break; - } - ++token; - } - return token; -} - -/******************************************************************************* - * - * wcstok_s - * - * - * - * The wcstok_s function is the wide-character equivalent of the strtok_s function - * - * - * strToken String containing token or tokens. - * strDelimit Set of delimiter characters. - * context Used to store position information between calls to - * wcstok_s. - * - * - * context is updated - * - * Returns a pointer to the next token found in strToken. - * They return NULL when no more tokens are found. - * Each call modifies strToken by substituting a NULL character for the first - * delimiter that occurs after the returned token. - * - * return value condition - * NULL context is NULL, strDelimit is NULL, strToken is NULL - * and (*context) is NULL, or no token is found. - ******************************************************************************* - */ -wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context) -{ - wchar_t *orgToken = strToken; - /* validation section */ - if (context == NULL || strDelimit == NULL) { - return NULL; - } - if (orgToken == NULL && (*context) == NULL) { - return NULL; - } - - /* If string==NULL, continue with previous string */ - if (orgToken == NULL) { - orgToken = *context; - } - - orgToken = SecFindBeginW(orgToken, strDelimit); - { - wchar_t *token = orgToken; - - orgToken = SecFindRestW(orgToken, strDelimit); - - /* Update the context */ - *context = orgToken; - - /* Determine if a token has been found. */ - if (token == orgToken) { - token = NULL; - } - - return token; - } -} - - diff --git a/base/src/securec/wcstok_s.c b/base/src/securec/wcstok_s.c new file mode 120000 index 0000000..92d3a62 --- /dev/null +++ b/base/src/securec/wcstok_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wcstok_s.c \ No newline at end of file diff --git a/base/src/securec/wmemcpy_s.c b/base/src/securec/wmemcpy_s.c deleted file mode 100755 index bde47a3..0000000 --- a/base/src/securec/wmemcpy_s.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions] Use unsafe function: Portability - * [reason] Use unsafe function to implement security function to maintain platform compatibility. - * And sufficient input validation is performed before calling - */ - -#include "securecutil.h" - -/******************************************************************************* - * - * The wmemcpy_s function copies n successive wide characters - * from the object pointed to by src into the object pointed to by dest.t. - * - * - * dest Destination buffer. - * destMax Size of the destination buffer. - * src Buffer to copy from. - * count Number of characters to copy. - * - * - * dest buffer is updated. - * - * - * EOK Success - * EINVAL dest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_MEM_MAX_LEN - * and count <= destMax - * EINVAL_AND_RESET dest != NULL and src is NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_MEM_MAX_LEN and count <= destMax - * ERANGE destMax > SECUREC_WCHAR_MEM_MAX_LEN or destMax is 0 or - * (count > destMax and dest is NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_MEM_MAX_LEN) - * ERANGE_AND_RESET count > destMax and dest != NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_MEM_MAX_LEN - * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and - * count <= destMax destMax != 0 and destMax <= SECUREC_WCHAR_MEM_MAX_LEN - * and dest != NULL and src != NULL and dest != src - * - * if an error occurred, dest will be filled with 0 when dest and destMax valid . - * If the source and destination overlap, the behavior of wmemcpy_s is undefined. - * Use wmemmove_s to handle overlapping regions. - ******************************************************************************* - */ -errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_WCHAR_MEM_MAX_LEN) { - SECUREC_ERROR_INVALID_PARAMTER("wmemcpy_s"); - return ERANGE; - } - if (count > destMax) { - SECUREC_ERROR_INVALID_PARAMTER("wmemcpy_s"); - if (dest != NULL) { - (void)memset(dest, 0, destMax * sizeof(wchar_t)); - return ERANGE_AND_RESET; - } - return ERANGE; - } - return memcpy_s(dest, destMax * sizeof(wchar_t), src, count * sizeof(wchar_t)); -} - - diff --git a/base/src/securec/wmemcpy_s.c b/base/src/securec/wmemcpy_s.c new file mode 120000 index 0000000..13f272a --- /dev/null +++ b/base/src/securec/wmemcpy_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wmemcpy_s.c \ No newline at end of file diff --git a/base/src/securec/wmemmove_s.c b/base/src/securec/wmemmove_s.c deleted file mode 100755 index 0cd7fd5..0000000 --- a/base/src/securec/wmemmove_s.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions] Use unsafe function: Portability - * [reason] Use unsafe function to implement security function to maintain platform compatibility. - * And sufficient input validation is performed before calling - */ - -#include "securecutil.h" - -/******************************************************************************* - * - * The wmemmove_s function copies n successive wide characters from the object pointed - * to by src into the object pointed to by dest. - * - * - * dest Destination buffer. - * destMax Size of the destination buffer. - * src Source object. - * count Number of bytes or character to copy. - * - * - * dest is updated. - * - * - * EOK Success - * EINVAL dest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_MEM_MAX_LEN - * and count <= destMax - * EINVAL_AND_RESET dest != NULL and src is NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_MEM_MAX_LEN and count <= destMax - * ERANGE destMax > SECUREC_WCHAR_MEM_MAX_LEN or destMax is 0 or - * (count > destMax and dest is NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_MEM_MAX_LEN) - * ERANGE_AND_RESET count > destMax and dest != NULL and destMax != 0 - * and destMax <= SECUREC_WCHAR_MEM_MAX_LEN - * - * - * If an error occurred, dest will be filled with 0 when dest and destMax valid. - * If some regions of the source area and the destination overlap, wmemmove_s - * ensures that the original source bytes in the overlapping region are copied - * before being overwritten - ******************************************************************************** - */ -errno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count) -{ - if (destMax == 0 || destMax > SECUREC_WCHAR_MEM_MAX_LEN) { - SECUREC_ERROR_INVALID_PARAMTER("wmemmove_s"); - return ERANGE; - } - if (count > destMax) { - SECUREC_ERROR_INVALID_PARAMTER("wmemmove_s"); - if (dest != NULL) { - (void)memset(dest, 0, destMax * sizeof(wchar_t)); - return ERANGE_AND_RESET; - } - return ERANGE; - } - return memmove_s(dest, destMax * sizeof(wchar_t), src, count * sizeof(wchar_t)); -} - - diff --git a/base/src/securec/wmemmove_s.c b/base/src/securec/wmemmove_s.c new file mode 120000 index 0000000..cc65ce3 --- /dev/null +++ b/base/src/securec/wmemmove_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wmemmove_s.c \ No newline at end of file diff --git a/base/src/securec/wscanf_s.c b/base/src/securec/wscanf_s.c deleted file mode 100755 index 436cd82..0000000 --- a/base/src/securec/wscanf_s.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "securec.h" - -/******************************************************************************* - * - * - * The wscanf_s function is the wide-character equivalent of the scanf_s function - * The wscanf_s function reads data from the standard input stream stdin and - * writes the data into the location that's given by argument. Each argument - * must be a pointer to a variable of a type that corresponds to a type specifier - * in format. If copying occurs between strings that overlap, the behavior is - * undefined. - * - * - * format Format control string. - * ... Optional arguments. - * - * - * ... the converted value stored in user assigned address - * - * - * Returns the number of fields successfully converted and assigned; - * the return value does not include fields that were read but not assigned. - * A return value of 0 indicates that no fields were assigned. - * return -1 if an error occurs. - ******************************************************************************* - */ - -int wscanf_s(const wchar_t *format, ...) -{ - int ret; /* If initialization causes e838 */ - va_list arglist; - - va_start(arglist, format); - ret = vwscanf_s(format, arglist); - va_end(arglist); - (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ - - return ret; -} - diff --git a/base/src/securec/wscanf_s.c b/base/src/securec/wscanf_s.c new file mode 120000 index 0000000..3b39ff8 --- /dev/null +++ b/base/src/securec/wscanf_s.c @@ -0,0 +1 @@ +../../../../../third_party/bounds_checking_function/src/wscanf_s.c \ No newline at end of file -- Gitee