1 /***
2 *corecrt_internal_securecrt.h - contains declarations of internal routines and variables for securecrt
3 *
4 *       Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 *       Declares routines and variables used internally in the SecureCRT implementation.
8 *       In this include file we define the macros needed to implement the secure functions
9 *       inlined in the *.inl files like tcscpy_s.inl, etc.
10 *
11 *       [Internal]
12 *
13 ****/
14 
15 #pragma once
16 
17 #ifndef _INC_INTERNAL_SECURECRT
18 #define _INC_INTERNAL_SECURECRT
19 
20 #include <corecrt_internal.h>
21 #include <errno.h>
22 
23 /* string resetting */
24 #define _FILL_STRING _SECURECRT__FILL_STRING
25 
26 #define _FILL_BYTE _SECURECRT__FILL_BYTE
27 
28 #define _RESET_STRING(_String, _Size) \
29     *(_String) = 0; \
30     _FILL_STRING((_String), (_Size), 1);
31 
32 /* validations */
33 #define _VALIDATE_STRING_ERROR(_String, _Size, _Ret) \
34     _VALIDATE_RETURN((_String) != NULL && (_Size) > 0, EINVAL, (_Ret))
35 
36 #define _VALIDATE_STRING(_String, _Size) \
37     _VALIDATE_STRING_ERROR((_String), (_Size), EINVAL)
38 
39 #define _VALIDATE_POINTER_ERROR_RETURN(_Pointer, _ErrorCode, _Ret) \
40     _VALIDATE_RETURN((_Pointer) != NULL, (_ErrorCode), (_Ret))
41 
42 #define _VALIDATE_POINTER_ERROR(_Pointer, _Ret) \
43     _VALIDATE_POINTER_ERROR_RETURN((_Pointer), EINVAL, (_Ret))
44 
45 #define _VALIDATE_POINTER(_Pointer) \
46     _VALIDATE_POINTER_ERROR((_Pointer), EINVAL)
47 
48 #define _VALIDATE_CONDITION_ERROR_RETURN(_Condition, _ErrorCode, _Ret) \
49     _VALIDATE_RETURN((_Condition), (_ErrorCode), (_Ret))
50 
51 #define _VALIDATE_CONDITION_ERROR(_Condition, _Ret) \
52     _VALIDATE_CONDITION_ERROR_RETURN((_Condition), EINVAL, (_Ret))
53 
54 #define _VALIDATE_POINTER_RESET_STRING_ERROR(_Pointer, _String, _Size, _Ret) \
55     if ((_Pointer) == NULL) \
56     { \
57         _RESET_STRING((_String), (_Size)); \
58         _VALIDATE_POINTER_ERROR_RETURN((_Pointer), EINVAL, (_Ret)) \
59     }
60 
61 #define _VALIDATE_POINTER_RESET_STRING(_Pointer, _String, _Size) \
62     _VALIDATE_POINTER_RESET_STRING_ERROR((_Pointer), (_String), (_Size), EINVAL)
63 
64 #define _RETURN_BUFFER_TOO_SMALL_ERROR(_String, _Size, _Ret) \
65     _VALIDATE_RETURN((L"Buffer is too small" && 0), ERANGE, _Ret)
66 
67 #define _RETURN_BUFFER_TOO_SMALL(_String, _Size) \
68     _RETURN_BUFFER_TOO_SMALL_ERROR((_String), (_Size), ERANGE)
69 
70 #define _RETURN_DEST_NOT_NULL_TERMINATED(_String, _Size) \
71     _VALIDATE_RETURN((L"String is not null terminated" && 0), EINVAL, EINVAL)
72 
73 #define _RETURN_EINVAL \
74     _VALIDATE_RETURN((L"Invalid parameter", 0), EINVAL, EINVAL)
75 
76 #define _RETURN_ERROR(_Msg, _Ret) \
77     _VALIDATE_RETURN(((_Msg), 0), EINVAL, _Ret)
78 
79 /* returns without calling _invalid_parameter */
80 #define _RETURN_NO_ERROR \
81     return 0
82 
83 /* Note that _RETURN_TRUNCATE does not set errno */
84 #define _RETURN_TRUNCATE \
85     return STRUNCATE
86 
87 #define _SET_MBCS_ERROR \
88     (errno = EILSEQ)
89 
90 #define _RETURN_MBCS_ERROR \
91     return _SET_MBCS_ERROR
92 
93 /* locale dependent */
94 #define _LOCALE_ARG \
95     _LocInfo
96 
97 #define _LOCALE_ARG_DECL \
98     _locale_t _LOCALE_ARG
99 
100 #define _LOCALE_UPDATE \
101     _LocaleUpdate _LocUpdate(_LOCALE_ARG)
102 
103 #define _ISMBBLEAD(_Character) \
104     _ismbblead_l((_Character), _LocUpdate.GetLocaleT())
105 
106 #define _ISMBBLEADPREFIX(_Result, _StringStart, _BytePtr)               \
107     {                                                                   \
108         unsigned char *_Tmp_VAR, *_StringStart_VAR, *_BytePtr_VAR;      \
109                                                                         \
110         _StringStart_VAR = (_StringStart);                              \
111         _BytePtr_VAR = (_BytePtr);                                      \
112         _Tmp_VAR = _BytePtr_VAR;                                        \
113         while ((_Tmp_VAR >= _StringStart_VAR) && _ISMBBLEAD(*_Tmp_VAR)) \
114         {                                                               \
115             _Tmp_VAR--;                                                 \
116         }                                                               \
117         (_Result) = ((_BytePtr_VAR - _Tmp_VAR) & 1) != 0;               \
118     }
119 
120 #define _LOCALE_SHORTCUT_TEST \
121     _LocUpdate.GetLocaleT()->mbcinfo->ismbcodepage == 0
122 
123 /* misc */
124 #define _ASSIGN_IF_NOT_NULL(_Pointer, _Value) \
125     if ((_Pointer) != NULL) \
126     { \
127         *(_Pointer) = (_Value); \
128     }
129 
130 #endif  /* _INC_INTERNAL_SECURECRT */
131