1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5  */
6 #ifndef __STRALIGN_H_
7 #define __STRALIGN_H_
8 
9 #ifndef _STRALIGN_USE_SECURE_CRT
10 #define _STRALIGN_USE_SECURE_CRT 0
11 #endif
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #ifndef WSTR_ALIGNED
18 #if defined (__x86_64__) || defined (__arm__)
19 #define WSTR_ALIGNED(s) TRUE
20 #else
21 #define WSTR_ALIGNED(s) (((DWORD_PTR)(s) & 1) == 0)
22 #endif
23 #endif
24 
25 #if defined(_X86_)
26 #define ua_CharUpperW CharUpperW
27 #define ua_lstrcmpiW lstrcmpiW
28 #define ua_lstrcmpW lstrcmpW
29 #define ua_lstrlenW lstrlenW
30 #define ua_wcschr wcschr
31 #define ua_wcsicmp wcsicmp
32 #define ua_wcslen wcslen
33 #define ua_wcsrchr wcsrchr
34 
35   PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source);
36 #if !defined (__CRT__NO_INLINE) && !defined (__CYGWIN__)
ua_wcscpy(PUWSTR Destination,PCUWSTR Source)37   __CRT_INLINE PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source) { return wcscpy(Destination,Source); }
38 #else
39 #define ua_wcscpy wcscpy
40 #endif
41 
42 #else /* not _X86_ : */
43 
44 #ifndef WSTR_ALIGNED
45 #define WSTR_ALIGNED(s) (((DWORD_PTR)(s) & (sizeof(WCHAR)-1))==0)
46 #endif
47 
48   /* TODO: This method seems to be not present for x86-64.  */
49   LPUWSTR WINAPI uaw_CharUpperW(LPUWSTR String);
50   int WINAPI uaw_lstrcmpW(PCUWSTR String1,PCUWSTR String2);
51   int WINAPI uaw_lstrcmpiW(PCUWSTR String1,PCUWSTR String2);
52   int WINAPI uaw_lstrlenW(LPCUWSTR String);
53   PUWSTR __cdecl uaw_wcschr(PCUWSTR String,WCHAR Character);
54   PUWSTR __cdecl uaw_wcscpy(PUWSTR Destination,PCUWSTR Source);
55   int __cdecl uaw_wcsicmp(PCUWSTR String1,PCUWSTR String2);
56   size_t __cdecl uaw_wcslen(PCUWSTR String);
57   PUWSTR __cdecl uaw_wcsrchr(PCUWSTR String,WCHAR Character);
58 #ifdef CharUpper
59   LPUWSTR ua_CharUpperW(LPUWSTR String);
60 #ifndef __CRT__NO_INLINE
61   __CRT_INLINE LPUWSTR ua_CharUpperW(LPUWSTR String) {
62     if(WSTR_ALIGNED(String)) return CharUpperW((PWSTR)String);
63     return uaw_CharUpperW(String);
64   }
65 #endif /* !__CRT__NO_INLINE */
66 #endif /* CharUpper */
67 
68 #ifdef lstrcmp
69   int ua_lstrcmpW(LPCUWSTR String1,LPCUWSTR String2);
70 #endif
71 #ifdef lstrcmpi
72   int ua_lstrcmpiW(LPCUWSTR String1,LPCUWSTR String2);
73 #endif
74 #ifdef lstrlen
75   int ua_lstrlenW(LPCUWSTR String);
76 #endif
77 
78 #ifndef __CRT__NO_INLINE
79 #ifdef lstrcmp
80   __CRT_INLINE int ua_lstrcmpW(LPCUWSTR String1,LPCUWSTR String2) {
81     if(WSTR_ALIGNED(String1) && WSTR_ALIGNED(String2))
82       return lstrcmpW((LPCWSTR)String1,(LPCWSTR)String2);
83     return uaw_lstrcmpW(String1,String2);
84   }
85 #endif
86 
87 #ifdef lstrcmpi
88   __CRT_INLINE int ua_lstrcmpiW(LPCUWSTR String1,LPCUWSTR String2) {
89     if(WSTR_ALIGNED(String1) && WSTR_ALIGNED(String2))
90       return lstrcmpiW((LPCWSTR)String1,(LPCWSTR)String2);
91     return uaw_lstrcmpiW(String1,String2);
92   }
93 #endif
94 
95 #ifdef lstrlen
96   __CRT_INLINE int ua_lstrlenW(LPCUWSTR String) {
97     if(WSTR_ALIGNED(String)) return lstrlenW((PCWSTR)String);
98     return uaw_lstrlenW(String);
99   }
100 #endif
101 #endif /* !__CRT__NO_INLINE */
102 
103 #if defined(_WSTRING_DEFINED)
104 #ifdef _WConst_return
105   typedef _WConst_return WCHAR UNALIGNED *PUWSTR_C;
106 #else
107   typedef WCHAR UNALIGNED *PUWSTR_C;
108 #endif
109 
110   PUWSTR_C ua_wcschr(PCUWSTR String,WCHAR Character);
111   PUWSTR_C ua_wcsrchr(PCUWSTR String,WCHAR Character);
112 #if defined(__cplusplus) && defined(_WConst_Return)
113   PUWSTR ua_wcschr(PUWSTR String,WCHAR Character);
114   PUWSTR ua_wcsrchr(PUWSTR String,WCHAR Character);
115 #endif
116   PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source);
117   size_t ua_wcslen(PCUWSTR String);
118 
119 #ifndef __CRT__NO_INLINE
120   __CRT_INLINE PUWSTR_C ua_wcschr(PCUWSTR String,WCHAR Character) {
121     if(WSTR_ALIGNED(String)) return (PUWSTR_C)wcschr((PCWSTR)String,Character);
122     return (PUWSTR_C)uaw_wcschr(String,Character);
123   }
124   __CRT_INLINE PUWSTR_C ua_wcsrchr(PCUWSTR String,WCHAR Character) {
125     if(WSTR_ALIGNED(String)) return (PUWSTR_C)wcsrchr((PCWSTR)String,Character);
126     return (PUWSTR_C)uaw_wcsrchr(String,Character);
127   }
128 #if defined(__cplusplus) && defined(_WConst_Return)
129   __CRT_INLINE PUWSTR ua_wcschr(PUWSTR String,WCHAR Character) {
130     if(WSTR_ALIGNED(String)) return wcscpy((PWSTR)Destination,(PCWSTR)Source);
131     return uaw_wcscpy(Destination,Source);
132   }
133   __CRT_INLINE PUWSTR ua_wcsrchr(PUWSTR String,WCHAR Character) {
134     if(WSTR_ALIGNED(String)) return wcsrchr(String,Character);
135     return uaw_wcsrchr((PCUWSTR)String,Character);
136   }
137 #endif
138 
139   __CRT_INLINE PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source) {
140     if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination))
141       return wcscpy((PWSTR)Destination,(PCWSTR)Source);
142     return uaw_wcscpy(Destination,Source);
143   }
144   __CRT_INLINE size_t ua_wcslen(PCUWSTR String) {
145     if(WSTR_ALIGNED(String)) return wcslen((PCWSTR)String);
146     return uaw_wcslen(String);
147   }
148 #endif /* !__CRT__NO_INLINE */
149 #endif /* _X86_ */
150   int ua_wcsicmp(LPCUWSTR String1,LPCUWSTR String2);
151 
152 #ifndef __CRT__NO_INLINE
153   __CRT_INLINE int ua_wcsicmp(LPCUWSTR String1,LPCUWSTR String2) {
154     if(WSTR_ALIGNED(String1) && WSTR_ALIGNED(String2))
155       return _wcsicmp((LPCWSTR)String1,(LPCWSTR)String2);
156     return uaw_wcsicmp(String1,String2);
157   }
158 #endif /* !__CRT__NO_INLINE */
159 #endif /* _WSTRING_DEFINED */
160 
161 #ifndef __UA_WCSLEN
162 #define __UA_WCSLEN ua_wcslen
163 #endif
164 
165 #define __UA_WSTRSIZE(s) ((__UA_WCSLEN(s)+1)*sizeof(WCHAR))
166 #define __UA_STACKCOPY(p,s) memcpy(_alloca(s),p,s)
167 
168 #if defined (__x86_64__) || defined (__arm__) || defined (_X86_)
169 #define WSTR_ALIGNED_STACK_COPY(d,s) (*(d) = (PCWSTR)(s))
170 #else
171 #define WSTR_ALIGNED_STACK_COPY(d,s) { PCUWSTR __ua_src; ULONG __ua_size; PWSTR __ua_dst; __ua_src = (s); if(WSTR_ALIGNED(__ua_src)) { __ua_dst = (PWSTR)__ua_src; } else { __ua_size = __UA_WSTRSIZE(__ua_src); __ua_dst = (PWSTR)_alloca(__ua_size); memcpy(__ua_dst,__ua_src,__ua_size); } *(d) = (PCWSTR)__ua_dst; }
172 #endif
173 
174 #define ASTR_ALIGNED_STACK_COPY(d,s) (*(d) = (PCSTR)(s))
175 
176 #if !defined (_X86_) && !defined (__x86_64__) && !defined (__arm__)
177 #define __UA_STRUC_ALIGNED(t,s) (((DWORD_PTR)(s) & (TYPE_ALIGNMENT(t)-1))==0)
178 #define STRUC_ALIGNED_STACK_COPY(t,s) __UA_STRUC_ALIGNED(t,s) ? ((t const *)(s)) : ((t const *)__UA_STACKCOPY((s),sizeof(t)))
179 #else
180 #define STRUC_ALIGNED_STACK_COPY(t,s) ((CONST t *)(s))
181 #endif
182 
183 #if defined(UNICODE)
184 #define TSTR_ALIGNED_STACK_COPY(d,s) WSTR_ALIGNED_STACK_COPY(d,s)
185 #define TSTR_ALIGNED(x) WSTR_ALIGNED(x)
186 #define ua_CharUpper ua_CharUpperW
187 #define ua_lstrcmp ua_lstrcmpW
188 #define ua_lstrcmpi ua_lstrcmpiW
189 #define ua_lstrlen ua_lstrlenW
190 #define ua_tcscpy ua_wcscpy
191 #else
192 #define TSTR_ALIGNED_STACK_COPY(d,s) ASTR_ALIGNED_STACK_COPY(d,s)
193 #define TSTR_ALIGNED(x) TRUE
194 #define ua_CharUpper CharUpperA
195 #define ua_lstrcmp lstrcmpA
196 #define ua_lstrcmpi lstrcmpiA
197 #define ua_lstrlen lstrlenA
198 #define ua_tcscpy strcpy
199 #endif
200 
201 #ifdef __cplusplus
202 }
203 #endif
204 
205 #include <sec_api/stralign_s.h>
206 #endif
207