• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2014-2020. All rights reserved.
3  * Licensed under Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *          http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  * Description: Define macro, data struct, and declare function prototype,
12  *              which is used by input.inl, secureinput_a.c and secureinput_w.c.
13  * Author: lishunda
14  * Create: 2014-02-25
15  */
16 
17 #ifndef SEC_INPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
18 #define SEC_INPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
19 #include "securecutil.h"
20 
21 #define SECUREC_SCANF_EINVAL             (-1)
22 #define SECUREC_SCANF_ERROR_PARA         (-2)
23 
24 /* For internal stream flag */
25 #define SECUREC_MEM_STR_FLAG             0x01U
26 #define SECUREC_FILE_STREAM_FLAG         0x02U
27 #define SECUREC_PIPE_STREAM_FLAG         0x04U
28 #define SECUREC_LOAD_FILE_TO_MEM_FLAG    0x08U
29 
30 #define SECUREC_UCS_BOM_HEADER_SIZE      2U
31 #define SECUREC_UCS_BOM_HEADER_BE_1ST    0xfeU
32 #define SECUREC_UCS_BOM_HEADER_BE_2ST    0xffU
33 #define SECUREC_UCS_BOM_HEADER_LE_1ST    0xffU
34 #define SECUREC_UCS_BOM_HEADER_LE_2ST    0xfeU
35 #define SECUREC_UTF8_BOM_HEADER_SIZE     3U
36 #define SECUREC_UTF8_BOM_HEADER_1ST      0xefU
37 #define SECUREC_UTF8_BOM_HEADER_2ND      0xbbU
38 #define SECUREC_UTF8_BOM_HEADER_3RD      0xbfU
39 #define SECUREC_UTF8_LEAD_1ST            0xe0U
40 #define SECUREC_UTF8_LEAD_2ND            0x80U
41 
42 #define SECUREC_BEGIN_WITH_UCS_BOM(s, len) ((len) >= SECUREC_UCS_BOM_HEADER_SIZE && \
43     (((unsigned char)((s)[0]) == SECUREC_UCS_BOM_HEADER_LE_1ST && \
44     (unsigned char)((s)[1]) == SECUREC_UCS_BOM_HEADER_LE_2ST) || \
45     ((unsigned char)((s)[0]) == SECUREC_UCS_BOM_HEADER_BE_1ST && \
46     (unsigned char)((s)[1]) == SECUREC_UCS_BOM_HEADER_BE_2ST)))
47 
48 #define SECUREC_BEGIN_WITH_UTF8_BOM(s, len) ((len) >= SECUREC_UTF8_BOM_HEADER_SIZE && \
49     (unsigned char)((s)[0]) == SECUREC_UTF8_BOM_HEADER_1ST && \
50     (unsigned char)((s)[1]) == SECUREC_UTF8_BOM_HEADER_2ND && \
51     (unsigned char)((s)[2]) == SECUREC_UTF8_BOM_HEADER_3RD)
52 
53 #ifdef SECUREC_FOR_WCHAR
54 #define SECUREC_BOM_HEADER_SIZE SECUREC_UCS_BOM_HEADER_SIZE
55 #define SECUREC_BEGIN_WITH_BOM(s, len) SECUREC_BEGIN_WITH_UCS_BOM((s), (len))
56 #else
57 #define SECUREC_BOM_HEADER_SIZE SECUREC_UTF8_BOM_HEADER_SIZE
58 #define SECUREC_BEGIN_WITH_BOM(s, len) SECUREC_BEGIN_WITH_UTF8_BOM((s), (len))
59 #endif
60 
61 typedef struct {
62     unsigned int flag;          /* Mark the properties of input stream */
63     char *base;                 /* The pointer to the header of buffered string */
64     const char *cur;            /* The pointer to next read position */
65     size_t count;               /* The size of buffered string in bytes */
66 #if SECUREC_ENABLE_SCANF_FILE
67     FILE *pf;                   /* The file pointer */
68     size_t fileRealRead;
69     long oriFilePos;            /* The original position of file offset when fscanf is called */
70 #if !SECUREC_USE_STD_UNGETC
71     unsigned int lastChar;      /* The char code of last input */
72     int fUnGet;                 /* The boolean flag of pushing a char back to read stream */
73 #endif
74 #endif
75 } SecFileStream;
76 
77 #if SECUREC_ENABLE_SCANF_FILE && !SECUREC_USE_STD_UNGETC
78 #define SECUREC_FILE_STREAM_INIT_FILE(stream, fp) do { \
79     (stream)->pf = (fp); \
80     (stream)->fileRealRead = 0; \
81     (stream)->oriFilePos = 0; \
82     (stream)->lastChar = 0; \
83     (stream)->fUnGet = 0; \
84 } SECUREC_WHILE_ZERO
85 #elif SECUREC_ENABLE_SCANF_FILE && SECUREC_USE_STD_UNGETC
86 #define SECUREC_FILE_STREAM_INIT_FILE(stream, fp) do { \
87     (stream)->pf = (fp); \
88     (stream)->fileRealRead = 0; \
89     (stream)->oriFilePos = 0; \
90 } SECUREC_WHILE_ZERO
91 #else
92 /* Disable file */
93 #define SECUREC_FILE_STREAM_INIT_FILE(stream, fp)
94 #endif
95 
96 /* This initialization for eliminating redundant initialization. */
97 #define SECUREC_FILE_STREAM_FROM_STRING(stream, buf, cnt) do { \
98     (stream)->flag = SECUREC_MEM_STR_FLAG; \
99     (stream)->base = NULL; \
100     (stream)->cur = (buf); \
101     (stream)->count = (cnt); \
102     SECUREC_FILE_STREAM_INIT_FILE((stream), NULL); \
103 } SECUREC_WHILE_ZERO
104 
105 /* This initialization for eliminating redundant initialization. */
106 #define SECUREC_FILE_STREAM_FROM_FILE(stream, fp) do { \
107     (stream)->flag = SECUREC_FILE_STREAM_FLAG; \
108     (stream)->base = NULL; \
109     (stream)->cur = NULL; \
110     (stream)->count = 0; \
111     SECUREC_FILE_STREAM_INIT_FILE((stream), (fp)); \
112 } SECUREC_WHILE_ZERO
113 
114 /* This initialization for eliminating redundant initialization. */
115 #define SECUREC_FILE_STREAM_FROM_STDIN(stream) do { \
116     (stream)->flag = SECUREC_PIPE_STREAM_FLAG; \
117     (stream)->base = NULL; \
118     (stream)->cur = NULL; \
119     (stream)->count = 0; \
120     SECUREC_FILE_STREAM_INIT_FILE((stream), SECUREC_STREAM_STDIN); \
121 } SECUREC_WHILE_ZERO
122 
123 #ifdef __cplusplus
124 extern "C" {
125 #endif
126     int SecInputS(SecFileStream *stream, const char *cFormat, va_list argList);
127     void SecClearDestBuf(const char *buffer, const char *format, va_list argList);
128 #ifdef SECUREC_FOR_WCHAR
129     int SecInputSW(SecFileStream *stream, const wchar_t *cFormat, va_list argList);
130     void SecClearDestBufW(const wchar_t *buffer, const wchar_t *format, va_list argList);
131 #endif
132 
133 /* 20150105 For software and hardware decoupling,such as UMG */
134 #ifdef SECUREC_SYSAPI4VXWORKS
135 #ifdef feof
136 #undef feof
137 #endif
138     extern int feof(FILE *stream);
139 #endif
140 
141 #if defined(SECUREC_SYSAPI4VXWORKS) || defined(SECUREC_CTYPE_MACRO_ADAPT)
142 #ifndef isspace
143 #define isspace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n'))
144 #endif
145 #ifndef iswspace
146 #define iswspace(c) (((c) == L' ') || ((c) == L'\t') || ((c) == L'\r') || ((c) == L'\n'))
147 #endif
148 #ifndef isascii
149 #define isascii(c) (((unsigned char)(c)) <= 0x7f)
150 #endif
151 #ifndef isupper
152 #define isupper(c) ((c) >= 'A' && (c) <= 'Z')
153 #endif
154 #ifndef islower
155 #define islower(c) ((c) >= 'a' && (c) <= 'z')
156 #endif
157 #ifndef isalpha
158 #define isalpha(c) (isupper(c) || (islower(c)))
159 #endif
160 #ifndef isdigit
161 #define isdigit(c) ((c) >= '0' && (c) <= '9')
162 #endif
163 #ifndef isxupper
164 #define isxupper(c) ((c) >= 'A' && (c) <= 'F')
165 #endif
166 #ifndef isxlower
167 #define isxlower(c) ((c) >= 'a' && (c) <= 'f')
168 #endif
169 #ifndef isxdigit
170 #define isxdigit(c) (isdigit(c) || isxupper(c) || isxlower(c))
171 #endif
172 #endif
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 /* Reserved file operation macro interface, s is FILE *, i is fileno zero. */
178 #ifndef SECUREC_LOCK_FILE
179 #define SECUREC_LOCK_FILE(s)
180 #endif
181 
182 #ifndef SECUREC_UNLOCK_FILE
183 #define SECUREC_UNLOCK_FILE(s)
184 #endif
185 
186 #ifndef SECUREC_LOCK_STDIN
187 #define SECUREC_LOCK_STDIN(i, s)
188 #endif
189 
190 #ifndef SECUREC_UNLOCK_STDIN
191 #define SECUREC_UNLOCK_STDIN(i, s)
192 #endif
193 #endif
194 
195