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