• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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             0X01
26 #define SECUREC_FILE_STREAM_FLAG         0X02
27 #define SECUREC_FROM_STDIN_FLAG          0X04
28 #define SECUREC_LOAD_FILE_TO_MEM_FLAG    0X08
29 
30 #define SECUREC_UNINITIALIZED_FILE_POS   (-1)
31 #define SECUREC_BOM_HEADER_SIZE          2
32 #define SECUREC_BOM_HEADER_BE_1ST        0xFEU
33 #define SECUREC_BOM_HEADER_BE_2ST        0xFFU
34 #define SECUREC_BOM_HEADER_LE_1ST        0xFFU
35 #define SECUREC_BOM_HEADER_LE_2ST        0xFEU
36 #define SECUREC_UTF8_BOM_HEADER_SIZE     3
37 #define SECUREC_UTF8_BOM_HEADER_1ST      0xEFU
38 #define SECUREC_UTF8_BOM_HEADER_2ND      0xBBU
39 #define SECUREC_UTF8_BOM_HEADER_3RD      0xBFU
40 #define SECUREC_UTF8_LEAD_1ST            0xE0
41 #define SECUREC_UTF8_LEAD_2ND            0x80
42 
43 typedef struct {
44     unsigned int flag;          /* mark the properties of input stream */
45     int count;                  /* the size of buffered string in bytes */
46     const char *cur;            /* the pointer to next read position */
47     char *base;                 /* the pointer to the header of buffered string */
48 #if SECUREC_ENABLE_SCANF_FILE
49     FILE *pf;                   /* the file pointer */
50     long oriFilePos;            /* the original position of file offset when fscanf is called */
51     int fileRealRead;
52 #if defined(SECUREC_NO_STD_UNGETC)
53     unsigned int lastChar;      /* the char code of last input */
54     int fUnget;                 /* the boolean flag of pushing a char back to read stream */
55 #endif
56 #endif
57 } SecFileStream;
58 
59 
60 #define SECUREC_INIT_SEC_FILE_STREAM_COMMON(fileStream, streamFlag, curPtr, strCount) do { \
61     (fileStream).flag = (streamFlag); \
62     (fileStream).count = (strCount); \
63     (fileStream).cur = (curPtr); \
64     (fileStream).base = NULL; \
65 } SECUREC_WHILE_ZERO
66 
67 #if SECUREC_ENABLE_SCANF_FILE
68 #if defined(SECUREC_NO_STD_UNGETC)
69 /* This initialization for eliminating redundant initialization.
70  * Compared with the previous version initialization 0,
71  * the current code causes the binary size to increase by some bytes
72  */
73 #define SECUREC_INIT_SEC_FILE_STREAM(fileStream, streamFlag, stream, filePos, curPtr, strCount) do { \
74     SECUREC_INIT_SEC_FILE_STREAM_COMMON((fileStream), (streamFlag), (curPtr), (strCount)); \
75     (fileStream).pf = (stream); \
76     (fileStream).oriFilePos = (filePos); \
77     (fileStream).fileRealRead = 0; \
78     (fileStream).lastChar = 0; \
79     (fileStream).fUnget = 0; \
80 } SECUREC_WHILE_ZERO
81 #else
82 #define SECUREC_INIT_SEC_FILE_STREAM(fileStream, streamFlag, stream, filePos, curPtr, strCount) do { \
83     SECUREC_INIT_SEC_FILE_STREAM_COMMON((fileStream), (streamFlag), (curPtr), (strCount)); \
84     (fileStream).pf = (stream); \
85     (fileStream).oriFilePos = (filePos); \
86     (fileStream).fileRealRead = 0; \
87 } SECUREC_WHILE_ZERO
88 #endif
89 #else /* No SECUREC_ENABLE_SCANF_FILE */
90 #define SECUREC_INIT_SEC_FILE_STREAM(fileStream, streamFlag, stream, filePos, curPtr, strCount) do { \
91     SECUREC_INIT_SEC_FILE_STREAM_COMMON((fileStream), (streamFlag), (curPtr), (strCount)); \
92 } SECUREC_WHILE_ZERO
93 #endif
94 
95 #ifdef __cplusplus
96 extern "C" {
97 #endif
98 
99     extern int SecInputS(SecFileStream *stream, const char *cFormat, va_list argList);
100     extern void SecClearDestBuf(const char *buffer, const char *format, va_list argList);
101 #if SECUREC_IN_KERNEL == 0
102     extern int SecInputSW(SecFileStream *stream, const wchar_t *cFormat, va_list argList);
103     extern void SecClearDestBufW(const wchar_t *buffer, const wchar_t *format, va_list argList);
104 #endif
105 /* 20150105 For software and hardware decoupling,such as UMG */
106 #if defined(SECUREC_SYSAPI4VXWORKS)
107 #ifdef feof
108 #undef feof
109 #endif
110     extern int feof(FILE *stream);
111 #endif
112 
113 #if defined(SECUREC_SYSAPI4VXWORKS) || defined(SECUREC_CTYPE_MACRO_ADAPT)
114 #ifndef isspace
115 #define isspace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n'))
116 #endif
117 #ifndef iswspace
118 #define iswspace(c) (((c) == L' ') || ((c) == L'\t') || ((c) == L'\r') || ((c) == L'\n'))
119 #endif
120 #ifndef isascii
121 #define isascii(c) (((unsigned char)(c)) <= 0x7f)
122 #endif
123 #ifndef isupper
124 #define isupper(c) ((c) >= 'A' && (c) <= 'Z')
125 #endif
126 #ifndef islower
127 #define islower(c) ((c) >= 'a' && (c) <= 'z')
128 #endif
129 #ifndef isalpha
130 #define isalpha(c) (isupper(c) || (islower(c)))
131 #endif
132 #ifndef isdigit
133 #define isdigit(c) ((c) >= '0' && (c) <= '9')
134 #endif
135 #ifndef isxupper
136 #define isxupper(c) ((c) >= 'A' && (c) <= 'F')
137 #endif
138 #ifndef isxlower
139 #define isxlower(c) ((c) >= 'a' && (c) <= 'f')
140 #endif
141 #ifndef isxdigit
142 #define isxdigit(c) (isdigit(c) || isxupper(c) || isxlower(c))
143 #endif
144 #endif
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 /* Reserved file operation macro interface */
150 #define SECUREC_LOCK_FILE(s)
151 #define SECUREC_UNLOCK_FILE(s)
152 #define SECUREC_LOCK_STDIN(i, s)
153 #define SECUREC_UNLOCK_STDIN(i, s)
154 #endif
155 
156 
157