1 /*
2  * Copyright (c) 2022 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 #ifndef __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27
17 #define __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27
18 
19 /* Compile in kernel under macro control */
20 #ifndef SECUREC_IN_KERNEL
21 #ifdef __KERNEL__
22 #define SECUREC_IN_KERNEL 1
23 #else
24 #define SECUREC_IN_KERNEL 0
25 #endif
26 #endif
27 
28 /* If you need high performance, enable the SECUREC_WITH_PERFORMANCE_ADDONS macro, default is enable .
29  * The macro is automatically closed on the windows platform in securectyp.h.
30  */
31 #ifndef SECUREC_WITH_PERFORMANCE_ADDONS
32 #if SECUREC_IN_KERNEL
33 #define SECUREC_WITH_PERFORMANCE_ADDONS 0
34 #else
35 #define SECUREC_WITH_PERFORMANCE_ADDONS 1
36 #endif
37 #endif
38 
39 #include <stdarg.h>
40 #include "securectype.h"
41 
42 #ifndef SECUREC_HAVE_ERRNO_H
43 #if SECUREC_IN_KERNEL
44 #define SECUREC_HAVE_ERRNO_H 0
45 #else
46 #define SECUREC_HAVE_ERRNO_H 1
47 #endif
48 #endif
49 
50 /* EINVAL ERANGE may defined in errno.h */
51 #if SECUREC_HAVE_ERRNO_H
52 #include <errno.h>
53 #endif
54 
55 /* If stack size on some embedded platform is limited, you can define the following macro
56  * which will put some variables on heap instead of stack.
57  * SECUREC_STACK_SIZE_LESS_THAN_1K
58  */
59 
60 /* define error code */
61 #if !defined(__STDC_WANT_LIB_EXT1__) || (defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ == 0))
62 #ifndef SECUREC_DEFINED_ERRNO_TYPE
63 #define SECUREC_DEFINED_ERRNO_TYPE
64 /* just check whether macrodefinition exists. */
65 #ifndef errno_t
66 typedef int errno_t;
67 #endif
68 #endif
69 #endif
70 
71 /* success */
72 #ifndef EOK
73 #define EOK (0)
74 #endif
75 
76 #ifndef EINVAL
77 /* The src buffer is not correct and destination buffer can't not be reset */
78 #define EINVAL (22)
79 #endif
80 
81 #ifndef EINVAL_AND_RESET
82 /* Once the error is detected, the dest buffer must be rest! */
83 #define EINVAL_AND_RESET (22 | 128)
84 #endif
85 
86 #ifndef ERANGE
87 /* The destination buffer is not long enough and destination buffer can not be reset */
88 #define ERANGE (34)
89 #endif
90 
91 #ifndef ERANGE_AND_RESET
92 /* Once the error is detected, the dest buffer must be rest! */
93 #define ERANGE_AND_RESET  (34 | 128)
94 #endif
95 
96 #ifndef EOVERLAP_AND_RESET
97 /* Once the buffer overlap is detected, the dest buffer must be rest! */
98 #define EOVERLAP_AND_RESET (54 | 128)
99 #endif
100 
101 /* if you need export the function of this library in Win32 dll, use __declspec(dllexport) */
102 #ifdef SECUREC_IS_DLL_LIBRARY
103 #ifdef SECUREC_DLL_EXPORT
104 #define SECUREC_API __declspec(dllexport)
105 #else
106 #define SECUREC_API __declspec(dllimport)
107 #endif
108 #else
109 /* Standardized function declaration . If a security function is declared in the your code,
110  * it may cause a compilation alarm,Please delete the security function you declared
111  */
112 #define SECUREC_API extern
113 #endif
114 
115 #ifndef SECUREC_SNPRINTF_TRUNCATED
116 #define SECUREC_SNPRINTF_TRUNCATED 1
117 #endif
118 
119 #ifdef __cplusplus
120 extern "C" {
121 #endif
122 
123     SECUREC_API errno_t memset_s(void *dest, size_t destMax, int c, size_t count);
124 
125 #ifndef SECUREC_ONLY_DECLARE_MEMSET
126 
127 #if SECUREC_IN_KERNEL == 0
128     SECUREC_API errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count);
129 #endif
130     SECUREC_API errno_t memmove_s(void *dest, size_t destMax, const void *src, size_t count);
131 #if SECUREC_IN_KERNEL == 0
132     SECUREC_API errno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count);
133 
134     SECUREC_API errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc);
135 
136     SECUREC_API errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count);
137 
138     SECUREC_API errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc);
139 
140     SECUREC_API errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count);
141 
142     SECUREC_API char *strtok_s(char *strToken, const char *strDelimit, char **context);
143 
144     SECUREC_API wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context);
145 
146     SECUREC_API int sprintf_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4);
147 
148     SECUREC_API int swprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, ...);
149 
150     SECUREC_API int vsprintf_s(char *strDest, size_t destMax, const char *format,
151                                va_list arglist) SECUREC_ATTRIBUTE(3, 0);
152 
153     SECUREC_API int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list arglist);
154 
155     SECUREC_API int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format,
156                                 va_list arglist) SECUREC_ATTRIBUTE(4, 0);
157 
158     SECUREC_API int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format,
159                                ...) SECUREC_ATTRIBUTE(4, 5);
160 
161 #if SECUREC_SNPRINTF_TRUNCATED
162     SECUREC_API int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format,
163                                           va_list arglist) SECUREC_ATTRIBUTE(3, 0);
164 
165     SECUREC_API int snprintf_truncated_s(char *strDest, size_t destMax,
166                                          const char *format, ...) SECUREC_ATTRIBUTE(3, 4);
167 #endif
168 
169     SECUREC_API int scanf_s(const char *format, ...);
170 
171     SECUREC_API int wscanf_s(const wchar_t *format, ...);
172 
173     SECUREC_API int vscanf_s(const char *format, va_list arglist);
174 
175     SECUREC_API int vwscanf_s(const wchar_t *format, va_list arglist);
176 
177     SECUREC_API int fscanf_s(FILE *stream, const char *format, ...);
178 
179     SECUREC_API int fwscanf_s(FILE *stream, const wchar_t *format, ...);
180 
181     SECUREC_API int vfscanf_s(FILE *stream, const char *format, va_list arglist);
182 
183     SECUREC_API int vfwscanf_s(FILE *stream, const wchar_t *format, va_list arglist);
184 
185     SECUREC_API int sscanf_s(const char *buffer, const char *format, ...);
186 
187     SECUREC_API int swscanf_s(const wchar_t *buffer, const wchar_t *format, ...);
188 
189     SECUREC_API int vsscanf_s(const char *buffer, const char *format, va_list arglist);
190 
191     SECUREC_API int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list arglist);
192 
193     SECUREC_API char *gets_s(char *buffer, size_t destMax);
194 #endif
195 
196     SECUREC_API errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count);
197 
198     SECUREC_API errno_t strcpy_s(char *strDest, size_t destMax, const char *strSrc);
199 
200     SECUREC_API errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count);
201 
202     SECUREC_API errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc);
203 
204     SECUREC_API errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count);
205 #if SECUREC_IN_KERNEL == 0
206     /* those functions are used by macro ,must declare hare , also for  without function declaration warning */
207     extern errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count);
208     extern errno_t strcpy_error(char *strDest, size_t destMax, const char *strSrc);
209 #endif
210 #endif
211 
212 #if SECUREC_WITH_PERFORMANCE_ADDONS
213     /* those functions are used by macro */
214     extern errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count);
215     extern errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count);
216     extern errno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count);
217     extern errno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count);
218 
219 /* strcpy_sp is a macro, NOT a function in performance optimization mode. */
220 #define strcpy_sp(dest, destMax, src)  ((__builtin_constant_p((destMax)) && \
221                                          __builtin_constant_p((src))) ?  \
222                                         SECUREC_STRCPY_SM((dest), (destMax), (src)) : \
223                                         strcpy_s((dest), (destMax), (src)))
224 
225 /* strncpy_sp is a macro, NOT a function in performance optimization mode. */
226 #define strncpy_sp(dest, destMax, src, count)  ((__builtin_constant_p((count)) && \
227                                                  __builtin_constant_p((destMax)) && \
228                                                  __builtin_constant_p((src))) ?  \
229                                                 SECUREC_STRNCPY_SM((dest), (destMax), (src), (count)) : \
230                                                 strncpy_s((dest), (destMax), (src), (count)))
231 
232 /* strcat_sp is a macro, NOT a function in performance optimization mode. */
233 #define strcat_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \
234                                         __builtin_constant_p((src))) ?  \
235                                        SECUREC_STRCAT_SM((dest), (destMax), (src)) : \
236                                        strcat_s((dest), (destMax), (src)))
237 
238  /* strncat_sp is a macro, NOT a function in performance optimization mode. */
239 #define strncat_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) &&  \
240                                                 __builtin_constant_p((destMax)) && \
241                                                 __builtin_constant_p((src))) ?  \
242                                                SECUREC_STRNCAT_SM((dest), (destMax), (src), (count)) : \
243                                                strncat_s((dest), (destMax), (src), (count)))
244 
245 /* memcpy_sp is a macro, NOT a function in performance optimization mode. */
246 #define memcpy_sp(dest, destMax, src, count)  (__builtin_constant_p((count)) ? \
247                                                (SECUREC_MEMCPY_SM((dest), (destMax),  (src), (count))) : \
248                                                (__builtin_constant_p((destMax)) ? \
249                                                 (((size_t)(destMax) > 0 && \
250                                                  (((unsigned long long)(destMax) & \
251                                                    (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \
252                                                   memcpy_sOptTc((dest), (destMax), (src), (count)) : ERANGE ) : \
253                                                   memcpy_sOptAsm((dest), (destMax), (src), (count))))
254 
255 /* memset_sp is a macro, NOT a function in performance optimization mode. */
256 #define memset_sp(dest, destMax, c, count)  (__builtin_constant_p((count)) ? \
257                                              (SECUREC_MEMSET_SM((dest), (destMax),  (c), (count))) : \
258                                              (__builtin_constant_p((destMax)) ? \
259                                               (((size_t)(destMax) > 0 && \
260                                               (((unsigned long long)(destMax) & \
261                                                (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \
262                                                 memset_sOptTc((dest), (destMax), (c), (count)) : ERANGE ) : \
263                                                 memset_sOptAsm((dest), (destMax), (c), (count))))
264 #else
265 #define strcpy_sp   strcpy_s
266 #define strncpy_sp  strncpy_s
267 #define strcat_sp   strcat_s
268 #define strncat_sp  strncat_s
269 #define memcpy_sp   memcpy_s
270 #define memset_sp   memset_s
271 #endif
272 
273 #ifdef __cplusplus
274 }
275 #endif /* __cplusplus */
276 #endif /* __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */
277