stralign.h 6.6 KB

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