• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "secureprintoutput.h"
17 
18 static inline int SecVsnprintfPImpl(char *string, size_t count, int priv, const char *format, va_list arglist);
19 
20 /*******************************************************************************
21  * <FUNCTION DESCRIPTION>
22  *    The vsnprintf_s function is equivalent to the vsnprintf function
23  *     except for the parameter destMax/count and the explicit runtime-constraints violation
24  *    The vsnprintf_s function takes a pointer to an argument list, then formats
25  *    and writes up to count characters of the given data to the memory pointed
26  *    to by strDest and appends a terminating null.
27  *
28  * <INPUT PARAMETERS>
29  *    strDest                  Storage location for the output.
30  *    destMax                The size of the strDest for output.
31  *    count                    Maximum number of character to write(not including
32  *                                the terminating NULL)
33  *    priv_on               whether print <private> for not-public args
34  *    format                   Format-control string.
35  *    arglist                     pointer to list of arguments.
36  *
37  * <OUTPUT PARAMETERS>
38  *    strDest                is updated
39  *
40  * <RETURN VALUE>
41  *    return  the number of characters written, not including the terminating null
42  *    return -1 if an  error occurs.
43  *    return -1 if count < destMax and the output string  has been truncated
44  *
45  * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
46  *******************************************************************************
47  */
vsnprintfp_s(char * strDest,size_t destMax,size_t count,int priv,const char * format,va_list arglist)48 int vsnprintfp_s(char *strDest, size_t destMax, size_t count, int priv,  const char *format, va_list arglist)
49 {
50     int retVal;
51 
52     if (format == NULL || strDest == NULL || destMax == 0 || destMax > SECUREC_STRING_MAX_LEN ||
53         (count > (SECUREC_STRING_MAX_LEN - 1) && count != (size_t)-1)) {
54         if (strDest != NULL && destMax > 0) {
55             strDest[0] = '\0';
56         }
57         SECUREC_ERROR_INVALID_PARAMTER("vsnprintfp_s");
58         return -1;
59     }
60 
61     if (destMax > count) {
62         retVal = SecVsnprintfPImpl(strDest, count + 1, priv, format, arglist);
63         if (retVal == SECUREC_PRINTF_TRUNCATE) {  /* lsd add to keep dest buffer not destroyed 2014.2.18 */
64             /* the string has been truncated, return  -1 */
65             return -1;          /* to skip error handler,  return strlen(strDest) or -1 */
66         }
67     } else {                    /* destMax <= count */
68         retVal = SecVsnprintfPImpl(strDest, destMax, priv, format, arglist);
69 #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
70         if (retVal == SECUREC_PRINTF_TRUNCATE && count == (size_t)-1) {
71             return -1;
72         }
73 #endif
74     }
75 
76     if (retVal < 0) {
77         strDest[0] = '\0';      /* empty the dest strDest */
78 
79         if (retVal == SECUREC_PRINTF_TRUNCATE) {
80             /* Buffer too small */
81             SECUREC_ERROR_INVALID_RANGE("vsnprintfp_s");
82         }
83 
84         SECUREC_ERROR_INVALID_PARAMTER("vsnprintfp_s");
85         return -1;
86     }
87 
88     return retVal;
89 }
90 
91 #ifdef SECUREC_FOR_WCHAR
92 #undef SECUREC_FOR_WCHAR
93 #endif
94 
95 typedef char SecChar;
96 #define SECUREC_CHAR(x) x
97 
98 #define SECUREC_WRITE_MULTI_CHAR  SecWriteMultiChar
99 #define SECUREC_WRITE_STRING      SecWriteString
100 #include "output_p.inl"
101 
102 /* put a char to output */
103 #define SECUREC_PUTC(_c,_stream)    ((--(_stream)->count >= 0) ? ((*(_stream)->cur++ = (char)(_c)) & 0xff) : EOF)
104 /* to clear e835 */
105 #define SECUREC_PUTC_ZERO(_stream)    ((--(_stream)->count >= 0) ? ((*(_stream)->cur++ = (char)('\0'))) : EOF)
106 
SecVsnprintfPImpl(char * string,size_t count,int priv,const char * format,va_list arglist)107 static inline int SecVsnprintfPImpl(char *string, size_t count, int priv, const char *format, va_list arglist)
108 {
109     SecPrintfStream str;
110     int retVal;
111 
112     str.count = (int)count;     /* this count include \0 character */
113     str.cur = string;
114 
115     retVal = SecOutputPS(&str, priv, format, arglist);
116     if ((retVal >= 0) && (SECUREC_PUTC_ZERO(&str) != EOF)) {
117         return (retVal);
118     } else if (str.count < 0) {
119         /* the buffer was too small; we return truncation */
120         string[count - 1] = 0;
121         return SECUREC_PRINTF_TRUNCATE;
122     }
123 
124     return -1;
125 }
126