• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef __SECURECUTIL_H__46C86578_F8FF_4E49_8E64_9B175241761F
17 #define __SECURECUTIL_H__46C86578_F8FF_4E49_8E64_9B175241761F
18 #include "securec.h"
19 
20 #ifndef SECUREC_HAVE_STRNLEN
21 #if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L)
22 #if SECUREC_IN_KERNEL
23 #define SECUREC_HAVE_STRNLEN 0
24 #else
25 #if defined(__GLIBC__) && __GLIBC__ >= 2 && defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ >= 10
26 #define SECUREC_HAVE_STRNLEN 1
27 #else
28 #define SECUREC_HAVE_STRNLEN 0
29 #endif
30 #endif
31 #else
32 #define SECUREC_HAVE_STRNLEN 0
33 #endif
34 #endif
35 
36 #if SECUREC_IN_KERNEL == 0
37 
38 #if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) || defined(__ARMCC_VERSION)
39 typedef __int64 SecInt64;
40 typedef unsigned __int64 SecUnsignedInt64;
41 #if defined(__ARMCC_VERSION)
42 typedef int SecInt32;
43 typedef unsigned int SecUnsignedInt32;
44 #else
45 typedef __int32 SecInt32;
46 typedef unsigned __int32 SecUnsignedInt32;
47 #endif
48 #else
49 typedef int SecInt32;
50 typedef unsigned int SecUnsignedInt32;
51 typedef long long SecInt64;
52 typedef unsigned long long SecUnsignedInt64;
53 #endif
54 
55 /* struct for performance */
56 typedef struct {
57     unsigned char buf[1];
58 } SecStrBuf1;
59 typedef struct {
60     unsigned char buf[2];
61 } SecStrBuf2;
62 typedef struct {
63     unsigned char buf[3];
64 } SecStrBuf3;
65 typedef struct {
66     unsigned char buf[4];
67 } SecStrBuf4;
68 typedef struct {
69     unsigned char buf[5];
70 } SecStrBuf5;
71 typedef struct {
72     unsigned char buf[6];
73 } SecStrBuf6;
74 typedef struct {
75     unsigned char buf[7];
76 } SecStrBuf7;
77 typedef struct {
78     unsigned char buf[8];
79 } SecStrBuf8;
80 typedef struct {
81     unsigned char buf[9];
82 } SecStrBuf9;
83 typedef struct {
84     unsigned char buf[10];
85 } SecStrBuf10;
86 typedef struct {
87     unsigned char buf[11];
88 } SecStrBuf11;
89 typedef struct {
90     unsigned char buf[12];
91 } SecStrBuf12;
92 typedef struct {
93     unsigned char buf[13];
94 } SecStrBuf13;
95 typedef struct {
96     unsigned char buf[14];
97 } SecStrBuf14;
98 typedef struct {
99     unsigned char buf[15];
100 } SecStrBuf15;
101 typedef struct {
102     unsigned char buf[16];
103 } SecStrBuf16;
104 typedef struct {
105     unsigned char buf[17];
106 } SecStrBuf17;
107 typedef struct {
108     unsigned char buf[18];
109 } SecStrBuf18;
110 typedef struct {
111     unsigned char buf[19];
112 } SecStrBuf19;
113 typedef struct {
114     unsigned char buf[20];
115 } SecStrBuf20;
116 typedef struct {
117     unsigned char buf[21];
118 } SecStrBuf21;
119 typedef struct {
120     unsigned char buf[22];
121 } SecStrBuf22;
122 typedef struct {
123     unsigned char buf[23];
124 } SecStrBuf23;
125 typedef struct {
126     unsigned char buf[24];
127 } SecStrBuf24;
128 typedef struct {
129     unsigned char buf[25];
130 } SecStrBuf25;
131 typedef struct {
132     unsigned char buf[26];
133 } SecStrBuf26;
134 typedef struct {
135     unsigned char buf[27];
136 } SecStrBuf27;
137 typedef struct {
138     unsigned char buf[28];
139 } SecStrBuf28;
140 typedef struct {
141     unsigned char buf[29];
142 } SecStrBuf29;
143 typedef struct {
144     unsigned char buf[30];
145 } SecStrBuf30;
146 typedef struct {
147     unsigned char buf[31];
148 } SecStrBuf31;
149 typedef struct {
150     unsigned char buf[32];
151 } SecStrBuf32;
152 typedef struct {
153     unsigned char buf[33];
154 } SecStrBuf33;
155 typedef struct {
156     unsigned char buf[34];
157 } SecStrBuf34;
158 typedef struct {
159     unsigned char buf[35];
160 } SecStrBuf35;
161 typedef struct {
162     unsigned char buf[36];
163 } SecStrBuf36;
164 typedef struct {
165     unsigned char buf[37];
166 } SecStrBuf37;
167 typedef struct {
168     unsigned char buf[38];
169 } SecStrBuf38;
170 typedef struct {
171     unsigned char buf[39];
172 } SecStrBuf39;
173 typedef struct {
174     unsigned char buf[40];
175 } SecStrBuf40;
176 typedef struct {
177     unsigned char buf[41];
178 } SecStrBuf41;
179 typedef struct {
180     unsigned char buf[42];
181 } SecStrBuf42;
182 typedef struct {
183     unsigned char buf[43];
184 } SecStrBuf43;
185 typedef struct {
186     unsigned char buf[44];
187 } SecStrBuf44;
188 typedef struct {
189     unsigned char buf[45];
190 } SecStrBuf45;
191 typedef struct {
192     unsigned char buf[46];
193 } SecStrBuf46;
194 typedef struct {
195     unsigned char buf[47];
196 } SecStrBuf47;
197 typedef struct {
198     unsigned char buf[48];
199 } SecStrBuf48;
200 typedef struct {
201     unsigned char buf[49];
202 } SecStrBuf49;
203 typedef struct {
204     unsigned char buf[50];
205 } SecStrBuf50;
206 typedef struct {
207     unsigned char buf[51];
208 } SecStrBuf51;
209 typedef struct {
210     unsigned char buf[52];
211 } SecStrBuf52;
212 typedef struct {
213     unsigned char buf[53];
214 } SecStrBuf53;
215 typedef struct {
216     unsigned char buf[54];
217 } SecStrBuf54;
218 typedef struct {
219     unsigned char buf[55];
220 } SecStrBuf55;
221 typedef struct {
222     unsigned char buf[56];
223 } SecStrBuf56;
224 typedef struct {
225     unsigned char buf[57];
226 } SecStrBuf57;
227 typedef struct {
228     unsigned char buf[58];
229 } SecStrBuf58;
230 typedef struct {
231     unsigned char buf[59];
232 } SecStrBuf59;
233 typedef struct {
234     unsigned char buf[60];
235 } SecStrBuf60;
236 typedef struct {
237     unsigned char buf[61];
238 } SecStrBuf61;
239 typedef struct {
240     unsigned char buf[62];
241 } SecStrBuf62;
242 typedef struct {
243     unsigned char buf[63];
244 } SecStrBuf63;
245 typedef struct {
246     unsigned char buf[64];
247 } SecStrBuf64;
248 
249 
250 
251 #if (defined(_MSC_VER)) && (_MSC_VER >= 1400)
252 #define SECUREC_MASK_MSVC_CRT_WARNING __pragma(warning(push)) \
253     __pragma(warning(disable:4996 4127))
254 #define SECUREC_END_MASK_MSVC_CRT_WARNING  __pragma(warning(pop))
255 #else
256 #define SECUREC_MASK_MSVC_CRT_WARNING
257 #define SECUREC_END_MASK_MSVC_CRT_WARNING
258 #endif
259 
260 #define SECUREC_WHILE_ZERO SECUREC_MASK_MSVC_CRT_WARNING while (0) SECUREC_END_MASK_MSVC_CRT_WARNING
261 
262 #define SECUREC_PRINTF_TRUNCATE (-2)
263 typedef struct {
264     int count;
265     char *cur;
266 } SecPrintfStream;
267 
268 #endif
269 
270 #define SECUREC_ADDR_ALIGNED_8(addr) (!((size_t)(addr) & 7))
271 #define SECUREC_IN_REGISTER register
272 
273 /* User can change the error handler by modify the following definition,
274  * such as logging the detail error in file.
275  */
276 #if defined(_DEBUG) || defined(DEBUG)
277 #if defined(SECUREC_ERROR_HANDLER_BY_ASSERT)
278 #define SECUREC_ERROR_INVALID_PARAMTER(msg) assert( msg "invalid argument" == NULL)
279 #define SECUREC_ERROR_INVALID_RANGE(msg)    assert( msg "invalid dest buffer size" == NULL)
280 #define SECUREC_ERROR_BUFFER_OVERLAP(msg)   assert( msg "buffer overlap" == NULL)
281 
282 #elif defined(SECUREC_ERROR_HANDLER_BY_PRINTF)
283 #if SECUREC_IN_KERNEL
284 #define SECUREC_ERROR_INVALID_PARAMTER(msg) printk( "%s invalid argument\n",msg)
285 #define SECUREC_ERROR_INVALID_RANGE(msg)    printk( "%s invalid dest buffer size\n", msg)
286 #define SECUREC_ERROR_BUFFER_OVERLAP(msg)   printk( "%s buffer overlap\n",msg)
287 #else
288 #define SECUREC_ERROR_INVALID_PARAMTER(msg) printf( "%s invalid argument\n",msg)
289 #define SECUREC_ERROR_INVALID_RANGE(msg)    printf( "%s invalid dest buffer size\n", msg)
290 #define SECUREC_ERROR_BUFFER_OVERLAP(msg)   printf( "%s buffer overlap\n",msg)
291 #endif
292 #elif defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
293 
294 #define SECUREC_ERROR_INVALID_PARAMTER(msg) LogSecureCRuntimeError(msg " EINVAL\n")
295 #define SECUREC_ERROR_INVALID_RANGE(msg)    LogSecureCRuntimeError(msg " ERANGE\n")
296 #define SECUREC_ERROR_BUFFER_OVERLAP(msg)   LogSecureCRuntimeError(msg " EOVERLAP\n")
297 #else
298 
299 #define SECUREC_ERROR_INVALID_PARAMTER(msg) ((void)0)
300 #define SECUREC_ERROR_INVALID_RANGE(msg)    ((void)0)
301 #define SECUREC_ERROR_BUFFER_OVERLAP(msg)   ((void)0)
302 #endif
303 
304 #else
305 #define SECUREC_ERROR_INVALID_PARAMTER(msg) ((void)0)
306 #define SECUREC_ERROR_INVALID_RANGE(msg)    ((void)0)
307 #define SECUREC_ERROR_BUFFER_OVERLAP(msg)   ((void)0)
308 #endif
309 
310 #ifdef __cplusplus
311 extern "C" {
312 #endif
313 
314 #if SECUREC_IN_KERNEL == 0
315     extern int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list arglist);
316     extern int SecPutWcharStrEndingZero(SecPrintfStream *str, int zerosNum);
317     extern void SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten);
318     extern void SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten);
319 #endif
320 
321 /* assembly language memcpy memset for X86 or MIPS ... */
322 #ifdef SECUREC_USE_ASM
323     extern void *memcpy_opt(void *, const void *, size_t);
324     extern void *memset_opt(void *, int, size_t);
325 #endif
326 
327 #if defined(SECUREC_VXWORKS_VERSION_5_4)
328     extern int SecU64Div32(SecUnsignedInt32 dividendHigh, SecUnsignedInt32 dividendLow, SecUnsignedInt32 divisor,
329                 SecUnsignedInt32 *pQuotientHigh, SecUnsignedInt32 *pQuotientLow, SecUnsignedInt32 *puiRemainder);
330 
331 #endif
332 
333 #if defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
334     extern void LogSecureCRuntimeError(const char *errDetail);
335 #endif
336 
337 #ifdef __cplusplus
338 }
339 #endif                          /* __cplusplus */
340 #endif
341 
342 
343