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