13d58139fSopenharmony_ci/* 23d58139fSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved. 33d58139fSopenharmony_ci * Licensed under Mulan PSL v2. 43d58139fSopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2. 53d58139fSopenharmony_ci * You may obtain a copy of Mulan PSL v2 at: 63d58139fSopenharmony_ci * http://license.coscl.org.cn/MulanPSL2 73d58139fSopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 83d58139fSopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 93d58139fSopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 103d58139fSopenharmony_ci * See the Mulan PSL v2 for more details. 113d58139fSopenharmony_ci * Description: snprintf_s function 123d58139fSopenharmony_ci * Create: 2014-02-25 133d58139fSopenharmony_ci */ 143d58139fSopenharmony_ci 153d58139fSopenharmony_ci#include "securec.h" 163d58139fSopenharmony_ci 173d58139fSopenharmony_ci#if SECUREC_ENABLE_SNPRINTF 183d58139fSopenharmony_ci/* 193d58139fSopenharmony_ci * <FUNCTION DESCRIPTION> 203d58139fSopenharmony_ci * The snprintf_s function is equivalent to the snprintf function 213d58139fSopenharmony_ci * except for the parameter destMax/count and the explicit runtime-constraints violation 223d58139fSopenharmony_ci * The snprintf_s function formats and stores count or fewer characters in 233d58139fSopenharmony_ci * strDest and appends a terminating null. Each argument (if any) is converted 243d58139fSopenharmony_ci * and output according to the corresponding format specification in format. 253d58139fSopenharmony_ci * The formatting is consistent with the printf family of functions; If copying 263d58139fSopenharmony_ci * occurs between strings that overlap, the behavior is undefined. 273d58139fSopenharmony_ci * 283d58139fSopenharmony_ci * <INPUT PARAMETERS> 293d58139fSopenharmony_ci * strDest Storage location for the output. 303d58139fSopenharmony_ci * destMax The size of the storage location for output. Size 313d58139fSopenharmony_ci * in bytes for snprintf_s or size in words for snwprintf_s. 323d58139fSopenharmony_ci * count Maximum number of character to store. 333d58139fSopenharmony_ci * format Format-control string. 343d58139fSopenharmony_ci * ... Optional arguments. 353d58139fSopenharmony_ci * 363d58139fSopenharmony_ci * <OUTPUT PARAMETERS> 373d58139fSopenharmony_ci * strDest is updated 383d58139fSopenharmony_ci * 393d58139fSopenharmony_ci * <RETURN VALUE> 403d58139fSopenharmony_ci * return the number of characters written, not including the terminating null 413d58139fSopenharmony_ci * return -1 if an error occurs. 423d58139fSopenharmony_ci * return -1 if count < destMax and the output string has been truncated 433d58139fSopenharmony_ci * 443d58139fSopenharmony_ci * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid 453d58139fSopenharmony_ci * 463d58139fSopenharmony_ci */ 473d58139fSopenharmony_ciint snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...) 483d58139fSopenharmony_ci{ 493d58139fSopenharmony_ci int ret; /* If initialization causes e838 */ 503d58139fSopenharmony_ci va_list argList; 513d58139fSopenharmony_ci 523d58139fSopenharmony_ci va_start(argList, format); 533d58139fSopenharmony_ci ret = vsnprintf_s(strDest, destMax, count, format, argList); 543d58139fSopenharmony_ci va_end(argList); 553d58139fSopenharmony_ci (void)argList; /* To clear e438 last value assigned not used , the compiler will optimize this code */ 563d58139fSopenharmony_ci 573d58139fSopenharmony_ci return ret; 583d58139fSopenharmony_ci} 593d58139fSopenharmony_ci#if SECUREC_EXPORT_KERNEL_SYMBOL 603d58139fSopenharmony_ciEXPORT_SYMBOL(snprintf_s); 613d58139fSopenharmony_ci#endif 623d58139fSopenharmony_ci#endif 633d58139fSopenharmony_ci 643d58139fSopenharmony_ci#if SECUREC_SNPRINTF_TRUNCATED 653d58139fSopenharmony_ci/* 663d58139fSopenharmony_ci * <FUNCTION DESCRIPTION> 673d58139fSopenharmony_ci * The snprintf_truncated_s function is equivalent to the snprintf function 683d58139fSopenharmony_ci * except for the parameter destMax/count and the explicit runtime-constraints violation 693d58139fSopenharmony_ci * The snprintf_truncated_s function formats and stores count or fewer characters in 703d58139fSopenharmony_ci * strDest and appends a terminating null. Each argument (if any) is converted 713d58139fSopenharmony_ci * and output according to the corresponding format specification in format. 723d58139fSopenharmony_ci * The formatting is consistent with the printf family of functions; If copying 733d58139fSopenharmony_ci * occurs between strings that overlap, the behavior is undefined. 743d58139fSopenharmony_ci * 753d58139fSopenharmony_ci * <INPUT PARAMETERS> 763d58139fSopenharmony_ci * strDest Storage location for the output. 773d58139fSopenharmony_ci * destMax The size of the storage location for output. Size 783d58139fSopenharmony_ci * in bytes for snprintf_truncated_s or size in words for snwprintf_s. 793d58139fSopenharmony_ci * format Format-control string. 803d58139fSopenharmony_ci * ... Optional arguments. 813d58139fSopenharmony_ci * 823d58139fSopenharmony_ci * <OUTPUT PARAMETERS> 833d58139fSopenharmony_ci * strDest is updated 843d58139fSopenharmony_ci * 853d58139fSopenharmony_ci * <RETURN VALUE> 863d58139fSopenharmony_ci * return the number of characters written, not including the terminating null 873d58139fSopenharmony_ci * return -1 if an error occurs. 883d58139fSopenharmony_ci * return destMax-1 if output string has been truncated 893d58139fSopenharmony_ci * 903d58139fSopenharmony_ci * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid 913d58139fSopenharmony_ci * 923d58139fSopenharmony_ci */ 933d58139fSopenharmony_ciint snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) 943d58139fSopenharmony_ci{ 953d58139fSopenharmony_ci int ret; /* If initialization causes e838 */ 963d58139fSopenharmony_ci va_list argList; 973d58139fSopenharmony_ci 983d58139fSopenharmony_ci va_start(argList, format); 993d58139fSopenharmony_ci ret = vsnprintf_truncated_s(strDest, destMax, format, argList); 1003d58139fSopenharmony_ci va_end(argList); 1013d58139fSopenharmony_ci (void)argList; /* To clear e438 last value assigned not used , the compiler will optimize this code */ 1023d58139fSopenharmony_ci 1033d58139fSopenharmony_ci return ret; 1043d58139fSopenharmony_ci} 1053d58139fSopenharmony_ci#if SECUREC_EXPORT_KERNEL_SYMBOL 1063d58139fSopenharmony_ciEXPORT_SYMBOL(snprintf_truncated_s); 1073d58139fSopenharmony_ci#endif 1083d58139fSopenharmony_ci 1093d58139fSopenharmony_ci#endif 1103d58139fSopenharmony_ci 111