1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define SECUREC_INLINE_DO_MEMCPY 1
18 #define SECUREC_FORMAT_OUTPUT_INPUT 1
19 #ifdef SECUREC_FOR_WCHAR
20 #undef SECUREC_FOR_WCHAR
21 #endif
22
23 #include "secureprintoutput.h"
24
25 #define SECUREC_CHAR(x) x
26 #define SECUREC_WRITE_MULTI_CHAR SecWriteMultiChar
27 #define SECUREC_WRITE_STRING SecWriteString
28
29 #ifndef EOF
30 #define EOF (-1)
31 #endif
32
33 /* put a char to output */
34 #define SECUREC_PUTC(c, outStream) ((--(outStream)->count >= 0) ? \
35 (int)((unsigned int)(unsigned char)(*((outStream)->cur++) = (char)(c)) & 0xff) : EOF)
36 /* to clear e835 */
37 #define SECUREC_PUTC_ZERO(outStream) ((--(outStream)->count >= 0) ? \
38 ((*((outStream)->cur++) = (char)('\0'))) : EOF)
39
40 static void SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten);
41 static void SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten);
42
43 #include "output.inl"
44
45 /*
46 * Wide character formatted output implementation
47 */
SecVsnprintfImpl(char * string,size_t count,const char * format,va_list argList)48 int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list argList)
49 {
50 SecPrintfStream str;
51 int retVal;
52
53 str.count = (int)count; /* this count include \0 character, Must be greater than zero */
54 str.cur = string;
55
56 retVal = SecOutputS(&str, format, argList);
57 if ((retVal >= 0) && (SECUREC_PUTC_ZERO(&str) != EOF)) {
58 return retVal;
59 } else if (str.count < 0) {
60 /* the buffer was too small; we return truncation */
61 string[count - 1] = '\0';
62 return SECUREC_PRINTF_TRUNCATE;
63 }
64 string[0] = '\0'; /* empty the dest strDest */
65 return -1;
66 }
67
68 /*
69 * Sec write Wide character
70 */
SecWriteMultiChar(char ch,int num,SecPrintfStream * f,int * pnumwritten)71 static void SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten)
72 {
73 int count = num;
74 while (count-- > 0) {
75 if (SECUREC_PUTC(ch, f) == EOF) {
76 *pnumwritten = -1;
77 break;
78 } else {
79 *pnumwritten = *pnumwritten + 1;
80 }
81 }
82 }
83
84 /*
85 * Sec write string function
86 */
SecWriteString(const char * string,int len,SecPrintfStream * f,int * pnumwritten)87 static void SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten)
88 {
89 const char *str = string;
90 int count = len;
91 while (count-- > 0) {
92 if (SECUREC_PUTC(*str, f) == EOF) {
93 *pnumwritten = -1;
94 break;
95 } else {
96 *pnumwritten = *pnumwritten + 1;
97 ++str;
98 }
99 }
100 }
101
102