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 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
17 #ifdef _CRTIMP_ALTERNATIVE
18 #undef _CRTIMP_ALTERNATIVE
19 #endif
20 #ifdef __STDC_WANT_SECURE_LIB__
21 #undef __STDC_WANT_SECURE_LIB__
22 #endif
23 #define _CRTIMP_ALTERNATIVE //comment microsoft *_s function
24 #define __STDC_WANT_SECURE_LIB__ 0
25 #endif
26
27 /* if some platforms don't have wchar.h, don't include it */
28 #if !(defined(SECUREC_VXWORKS_PLATFORM))
29 /* This header file is placed below secinput.h, which will cause tool alarm,
30 * but If there is no macro above, it will cause compiling alarm */
31 #include <wchar.h>
32 #endif
33 #include "secureprintoutput.h"
34
35 #ifndef WEOF
36 #define WEOF ((wchar_t)-1)
37 #endif
38
39 #ifndef SECUREC_FOR_WCHAR
40 #define SECUREC_FOR_WCHAR
41 #endif
42
43 typedef wchar_t SecChar;
44 #define SECUREC_CHAR(x) L ## x
45 #define SECUREC_WRITE_CHAR SecWriteCharW
46 #define SECUREC_WRITE_MULTI_CHAR SecWriteMultiCharW
47 #define SECUREC_WRITE_STRING SecWriteStringW
48
49
50 /* put a wchar to output stream */
51 /* LSD change "unsigned short" to wchar_t */
52
SecPutCharW(wchar_t ch,SecPrintfStream * f)53 static wchar_t SecPutCharW(wchar_t ch, SecPrintfStream *f)
54 {
55 wchar_t wcRet = 0;
56 if (((f)->count -= (int)sizeof(wchar_t)) >= 0) {
57 *(wchar_t *)(void *)(f->cur) = ch;
58 f->cur += sizeof(wchar_t);
59 wcRet = ch;
60 } else {
61 wcRet = (wchar_t)WEOF;
62 }
63 return wcRet;
64 }
65
SecWriteCharW(wchar_t ch,SecPrintfStream * f,int * pnumwritten)66 static void SecWriteCharW(wchar_t ch, SecPrintfStream *f, int *pnumwritten)
67 {
68 if (SecPutCharW(ch, f) == (wchar_t)WEOF) {
69 *pnumwritten = -1;
70 } else {
71 ++(*pnumwritten);
72 }
73 }
74
SecWriteMultiCharW(wchar_t ch,int num,SecPrintfStream * f,int * pnumwritten)75 static void SecWriteMultiCharW(wchar_t ch, int num, SecPrintfStream *f, int *pnumwritten)
76 {
77 int count = num;
78 while (count-- > 0) {
79 SecWriteCharW(ch, f, pnumwritten);
80 if (*pnumwritten == -1) {
81 break;
82 }
83 }
84 }
85
SecWriteStringW(const wchar_t * string,int len,SecPrintfStream * f,int * pnumwritten)86 static void SecWriteStringW(const wchar_t *string, int len, SecPrintfStream *f, int *pnumwritten)
87 {
88 const wchar_t *str = string;
89 int count = len;
90 while (count-- > 0) {
91 SecWriteCharW(*str++, f, pnumwritten);
92 if (*pnumwritten == -1) {
93 break;
94 }
95 }
96 }
97
98 #include "output.inl"
99
100