• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. 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  * Create: 2014-02-25
14  */
15 
16 #ifndef SEC_INPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
17 #define SEC_INPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
18 #include "securecutil.h"
19 
20 #define SECUREC_SCANF_EINVAL             (-1)
21 #define SECUREC_SCANF_ERROR_PARA         (-2)
22 
23 /* For internal stream flag */
24 #define SECUREC_MEM_STR_FLAG             0x01U
25 #define SECUREC_FILE_STREAM_FLAG         0x02U
26 #define SECUREC_PIPE_STREAM_FLAG         0x04U
27 #define SECUREC_LOAD_FILE_TO_MEM_FLAG    0x08U
28 
29 #define SECUREC_UCS_BOM_HEADER_SIZE      2U
30 #define SECUREC_UCS_BOM_HEADER_BE_1ST    0xfeU
31 #define SECUREC_UCS_BOM_HEADER_BE_2ST    0xffU
32 #define SECUREC_UCS_BOM_HEADER_LE_1ST    0xffU
33 #define SECUREC_UCS_BOM_HEADER_LE_2ST    0xfeU
34 #define SECUREC_UTF8_BOM_HEADER_SIZE     3U
35 #define SECUREC_UTF8_BOM_HEADER_1ST      0xefU
36 #define SECUREC_UTF8_BOM_HEADER_2ND      0xbbU
37 #define SECUREC_UTF8_BOM_HEADER_3RD      0xbfU
38 #define SECUREC_UTF8_LEAD_1ST            0xe0U
39 #define SECUREC_UTF8_LEAD_2ND            0x80U
40 
41 #define SECUREC_BEGIN_WITH_UCS_BOM(s, len) ((len) == SECUREC_UCS_BOM_HEADER_SIZE && \
42     (((unsigned char)((s)[0]) == SECUREC_UCS_BOM_HEADER_LE_1ST && \
43     (unsigned char)((s)[1]) == SECUREC_UCS_BOM_HEADER_LE_2ST) || \
44     ((unsigned char)((s)[0]) == SECUREC_UCS_BOM_HEADER_BE_1ST && \
45     (unsigned char)((s)[1]) == SECUREC_UCS_BOM_HEADER_BE_2ST)))
46 
47 #define SECUREC_BEGIN_WITH_UTF8_BOM(s, len) ((len) == SECUREC_UTF8_BOM_HEADER_SIZE && \
48     (unsigned char)((s)[0]) == SECUREC_UTF8_BOM_HEADER_1ST && \
49     (unsigned char)((s)[1]) == SECUREC_UTF8_BOM_HEADER_2ND && \
50     (unsigned char)((s)[2]) == SECUREC_UTF8_BOM_HEADER_3RD)
51 
52 #ifdef SECUREC_FOR_WCHAR
53 #define SECUREC_BOM_HEADER_SIZE SECUREC_UCS_BOM_HEADER_SIZE
54 #define SECUREC_BEGIN_WITH_BOM(s, len) SECUREC_BEGIN_WITH_UCS_BOM((s), (len))
55 #else
56 #define SECUREC_BOM_HEADER_SIZE SECUREC_UTF8_BOM_HEADER_SIZE
57 #define SECUREC_BEGIN_WITH_BOM(s, len) SECUREC_BEGIN_WITH_UTF8_BOM((s), (len))
58 #endif
59 
60 typedef struct {
61     unsigned int flag;          /* Mark the properties of input stream */
62     char *base;                 /* The pointer to the header of buffered string */
63     const char *cur;            /* The pointer to next read position */
64     size_t count;               /* The size of buffered string in bytes */
65 #if SECUREC_ENABLE_SCANF_FILE
66     FILE *pf;                   /* The file pointer */
67     size_t fileRealRead;
68     long oriFilePos;            /* The original position of file offset when fscanf is called */
69 #endif
70 } SecFileStream;
71 
72 #if SECUREC_ENABLE_SCANF_FILE
73 #define SECUREC_FILE_STREAM_INIT_FILE(stream, fp) do { \
74     (stream)->pf = (fp); \
75     (stream)->fileRealRead = 0; \
76     (stream)->oriFilePos = 0; \
77 } SECUREC_WHILE_ZERO
78 #else
79 /* Disable file */
80 #define SECUREC_FILE_STREAM_INIT_FILE(stream, fp)
81 #endif
82 
83 /* This initialization for eliminating redundant initialization. */
84 #define SECUREC_FILE_STREAM_FROM_STRING(stream, buf, cnt) do { \
85     (stream)->flag = SECUREC_MEM_STR_FLAG; \
86     (stream)->base = NULL; \
87     (stream)->cur = (buf); \
88     (stream)->count = (cnt); \
89     SECUREC_FILE_STREAM_INIT_FILE((stream), NULL); \
90 } SECUREC_WHILE_ZERO
91 
92 /* This initialization for eliminating redundant initialization. */
93 #define SECUREC_FILE_STREAM_FROM_FILE(stream, fp) do { \
94     (stream)->flag = SECUREC_FILE_STREAM_FLAG; \
95     (stream)->base = NULL; \
96     (stream)->cur = NULL; \
97     (stream)->count = 0; \
98     SECUREC_FILE_STREAM_INIT_FILE((stream), (fp)); \
99 } SECUREC_WHILE_ZERO
100 
101 /* This initialization for eliminating redundant initialization. */
102 #define SECUREC_FILE_STREAM_FROM_STDIN(stream) do { \
103     (stream)->flag = SECUREC_PIPE_STREAM_FLAG; \
104     (stream)->base = NULL; \
105     (stream)->cur = NULL; \
106     (stream)->count = 0; \
107     SECUREC_FILE_STREAM_INIT_FILE((stream), SECUREC_STREAM_STDIN); \
108 } SECUREC_WHILE_ZERO
109 
110 #ifdef __cplusplus
111 extern "C" {
112 #endif
113 int SecInputS(SecFileStream *stream, const char *cFormat, va_list argList);
114 void SecClearDestBuf(const char *buffer, const char *format, va_list argList);
115 #ifdef SECUREC_FOR_WCHAR
116 int SecInputSW(SecFileStream *stream, const wchar_t *cFormat, va_list argList);
117 void SecClearDestBufW(const wchar_t *buffer, const wchar_t *format, va_list argList);
118 #endif
119 
120 /* 20150105 For software and hardware decoupling,such as UMG */
121 #ifdef SECUREC_SYSAPI4VXWORKS
122 #ifdef feof
123 #undef feof
124 #endif
125 extern int feof(FILE *stream);
126 #endif
127 
128 #if defined(SECUREC_SYSAPI4VXWORKS) || defined(SECUREC_CTYPE_MACRO_ADAPT)
129 #ifndef isspace
130 #define isspace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n'))
131 #endif
132 #ifndef iswspace
133 #define iswspace(c) (((c) == L' ') || ((c) == L'\t') || ((c) == L'\r') || ((c) == L'\n'))
134 #endif
135 #ifndef isascii
136 #define isascii(c) (((unsigned char)(c)) <= 0x7f)
137 #endif
138 #ifndef isupper
139 #define isupper(c) ((c) >= 'A' && (c) <= 'Z')
140 #endif
141 #ifndef islower
142 #define islower(c) ((c) >= 'a' && (c) <= 'z')
143 #endif
144 #ifndef isalpha
145 #define isalpha(c) (isupper(c) || (islower(c)))
146 #endif
147 #ifndef isdigit
148 #define isdigit(c) ((c) >= '0' && (c) <= '9')
149 #endif
150 #ifndef isxupper
151 #define isxupper(c) ((c) >= 'A' && (c) <= 'F')
152 #endif
153 #ifndef isxlower
154 #define isxlower(c) ((c) >= 'a' && (c) <= 'f')
155 #endif
156 #ifndef isxdigit
157 #define isxdigit(c) (isdigit(c) || isxupper(c) || isxlower(c))
158 #endif
159 #endif
160 
161 #ifdef __cplusplus
162 }
163 #endif
164 /* Reserved file operation macro interface, s is FILE *, i is fileno zero. */
165 #ifndef SECUREC_LOCK_FILE
166 #define SECUREC_LOCK_FILE(s)
167 #endif
168 
169 #ifndef SECUREC_UNLOCK_FILE
170 #define SECUREC_UNLOCK_FILE(s)
171 #endif
172 
173 #ifndef SECUREC_LOCK_STDIN
174 #define SECUREC_LOCK_STDIN(i, s)
175 #endif
176 
177 #ifndef SECUREC_UNLOCK_STDIN
178 #define SECUREC_UNLOCK_STDIN(i, s)
179 #endif
180 #endif
181 
182