aboutsummaryrefslogtreecommitdiff
path: root/x86_64-w64-mingw32/include/stralign.h
blob: 9b5637d6d6f4c91297c2de66553adbc12fa70d73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is part of the mingw-w64 runtime package.
 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 */
#ifndef __STRALIGN_H_
#define __STRALIGN_H_

#ifndef _STRALIGN_USE_SECURE_CRT
#define _STRALIGN_USE_SECURE_CRT 0
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifndef WSTR_ALIGNED
#if defined (__amd64__) || defined (__arm__)
#define WSTR_ALIGNED(s) TRUE
#else
#define WSTR_ALIGNED(s) (((DWORD_PTR)(s) & 1) == 0)
#endif
#endif

#if defined(_X86_)
#define ua_CharUpperW CharUpperW
#define ua_lstrcmpiW lstrcmpiW
#define ua_lstrcmpW lstrcmpW
#define ua_lstrlenW lstrlenW
#define ua_wcschr wcschr
#define ua_wcsicmp wcsicmp
#define ua_wcslen wcslen
#define ua_wcsrchr wcsrchr

  PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source);
#if !defined (__CRT__NO_INLINE) && !defined (__CYGWIN__)
  __CRT_INLINE PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source) { return wcscpy(Destination,Source); }
#else
#define ua_wcscpy wcscpy
#endif

#else /* not _X86_ : */

#ifndef WSTR_ALIGNED
#define WSTR_ALIGNED(s) (((DWORD_PTR)(s) & (sizeof(WCHAR)-1))==0)
#endif

  /* TODO: This method seems to be not present for amd64.  */
  LPUWSTR WINAPI uaw_CharUpperW(LPUWSTR String);
  int WINAPI uaw_lstrcmpW(PCUWSTR String1,PCUWSTR String2);
  int WINAPI uaw_lstrcmpiW(PCUWSTR String1,PCUWSTR String2);
  int WINAPI uaw_lstrlenW(LPCUWSTR String);
  PUWSTR __cdecl uaw_wcschr(PCUWSTR String,WCHAR Character);
  PUWSTR __cdecl uaw_wcscpy(PUWSTR Destination,PCUWSTR Source);
  int __cdecl uaw_wcsicmp(PCUWSTR String1,PCUWSTR String2);
  size_t __cdecl uaw_wcslen(PCUWSTR String);
  PUWSTR __cdecl uaw_wcsrchr(PCUWSTR String,WCHAR Character);
#ifdef CharUpper
  LPUWSTR ua_CharUpperW(LPUWSTR String);
#ifndef __CRT__NO_INLINE
  __CRT_INLINE LPUWSTR ua_CharUpperW(LPUWSTR String) {
    if(WSTR_ALIGNED(String)) return CharUpperW((PWSTR)String);
    return uaw_CharUpperW(String);
  }
#endif /* !__CRT__NO_INLINE */
#endif /* CharUpper */

#ifdef lstrcmp
  int ua_lstrcmpW(LPCUWSTR String1,LPCUWSTR String2);
#endif
#ifdef lstrcmpi
  int ua_lstrcmpiW(LPCUWSTR String1,LPCUWSTR String2);
#endif
#ifdef lstrlen
  int ua_lstrlenW(LPCUWSTR String);
#endif

#ifndef __CRT__NO_INLINE
#ifdef lstrcmp
  __CRT_INLINE int ua_lstrcmpW(LPCUWSTR String1,LPCUWSTR String2) {
    if(WSTR_ALIGNED(String1) && WSTR_ALIGNED(String2))
      return lstrcmpW((LPCWSTR)String1,(LPCWSTR)String2);
    return uaw_lstrcmpW(String1,String2);
  }
#endif

#ifdef lstrcmpi
  __CRT_INLINE int ua_lstrcmpiW(LPCUWSTR String1,LPCUWSTR String2) {
    if(WSTR_ALIGNED(String1) && WSTR_ALIGNED(String2))
      return lstrcmpiW((LPCWSTR)String1,(LPCWSTR)String2);
    return uaw_lstrcmpiW(String1,String2);
  }
#endif

#ifdef lstrlen
  __CRT_INLINE int ua_lstrlenW(LPCUWSTR String) {
    if(WSTR_ALIGNED(String)) return lstrlenW((PCWSTR)String);
    return uaw_lstrlenW(String);
  }
#endif
#endif /* !__CRT__NO_INLINE */

#if defined(_WSTRING_DEFINED)
#ifdef _WConst_return
  typedef _WConst_return WCHAR UNALIGNED *PUWSTR_C;
#else
  typedef WCHAR UNALIGNED *PUWSTR_C;
#endif

  PUWSTR_C ua_wcschr(PCUWSTR String,WCHAR Character);
  PUWSTR_C ua_wcsrchr(PCUWSTR String,WCHAR Character);
#if defined(__cplusplus) && defined(_WConst_Return)
  PUWSTR ua_wcschr(PUWSTR String,WCHAR Character);
  PUWSTR ua_wcsrchr(PUWSTR String,WCHAR Character);
#endif
  PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source);
  size_t ua_wcslen(PCUWSTR String);

#ifndef __CRT__NO_INLINE
  __CRT_INLINE PUWSTR_C ua_wcschr(PCUWSTR String,WCHAR Character) {
    if(WSTR_ALIGNED(String)) return wcschr((PCWSTR)String,Character);
    return (PUWSTR_C)uaw_wcschr(String,Character);
  }
  __CRT_INLINE PUWSTR_C ua_wcsrchr(PCUWSTR String,WCHAR Character) {
    if(WSTR_ALIGNED(String)) return wcsrchr((PCWSTR)String,Character);
    return (PUWSTR_C)uaw_wcsrchr(String,Character);
  }
#if defined(__cplusplus) && defined(_WConst_Return)
  __CRT_INLINE PUWSTR ua_wcschr(PUWSTR String,WCHAR Character) {
    if(WSTR_ALIGNED(String)) return wcscpy((PWSTR)Destination,(PCWSTR)Source);
    return uaw_wcscpy(Destination,Source);
  }
  __CRT_INLINE PUWSTR ua_wcsrchr(PUWSTR String,WCHAR Character) {
    if(WSTR_ALIGNED(String)) return wcsrchr(String,Character);
    return uaw_wcsrchr((PCUWSTR)String,Character);
  }
#endif

  __CRT_INLINE PUWSTR ua_wcscpy(PUWSTR Destination,PCUWSTR Source) {
    if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination))
      return wcscpy((PWSTR)Destination,(PCWSTR)Source);
    return uaw_wcscpy(Destination,Source);
  }
  __CRT_INLINE size_t ua_wcslen(PCUWSTR String) {
    if(WSTR_ALIGNED(String)) return wcslen((PCWSTR)String);
    return uaw_wcslen(String);
  }
#endif /* !__CRT__NO_INLINE */
#endif /* _X86_ */
  int ua_wcsicmp(LPCUWSTR String1,LPCUWSTR String2);

#ifndef __CRT__NO_INLINE
  __CRT_INLINE int ua_wcsicmp(LPCUWSTR String1,LPCUWSTR String2) {
    if(WSTR_ALIGNED(String1) && WSTR_ALIGNED(String2))
      return _wcsicmp((LPCWSTR)String1,(LPCWSTR)String2);
    return uaw_wcsicmp(String1,String2);
  }
#endif /* !__CRT__NO_INLINE */
#endif /* _WSTRING_DEFINED */

#ifndef __UA_WCSLEN
#define __UA_WCSLEN ua_wcslen
#endif

#define __UA_WSTRSIZE(s) ((__UA_WCSLEN(s)+1)*sizeof(WCHAR))
#define __UA_STACKCOPY(p,s) memcpy(_alloca(s),p,s)

#if defined (__amd64__) || defined (__arm__) || defined (_X86_)
#define WSTR_ALIGNED_STACK_COPY(d,s) (*(d) = (PCWSTR)(s))
#else
#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; }
#endif

#define ASTR_ALIGNED_STACK_COPY(d,s) (*(d) = (PCSTR)(s))

#if !defined (_X86_) && !defined (__amd64__) && !defined (__arm__)
#define __UA_STRUC_ALIGNED(t,s) (((DWORD_PTR)(s) & (TYPE_ALIGNMENT(t)-1))==0)
#define STRUC_ALIGNED_STACK_COPY(t,s) __UA_STRUC_ALIGNED(t,s) ? ((t const *)(s)) : ((t const *)__UA_STACKCOPY((s),sizeof(t)))
#else
#define STRUC_ALIGNED_STACK_COPY(t,s) ((CONST t *)(s))
#endif

#if defined(UNICODE)
#define TSTR_ALIGNED_STACK_COPY(d,s) WSTR_ALIGNED_STACK_COPY(d,s)
#define TSTR_ALIGNED(x) WSTR_ALIGNED(x)
#define ua_CharUpper ua_CharUpperW
#define ua_lstrcmp ua_lstrcmpW
#define ua_lstrcmpi ua_lstrcmpiW
#define ua_lstrlen ua_lstrlenW
#define ua_tcscpy ua_wcscpy
#else
#define TSTR_ALIGNED_STACK_COPY(d,s) ASTR_ALIGNED_STACK_COPY(d,s)
#define TSTR_ALIGNED(x) TRUE
#define ua_CharUpper CharUpperA
#define ua_lstrcmp lstrcmpA
#define ua_lstrcmpi lstrcmpiA
#define ua_lstrlen lstrlenA
#define ua_tcscpy strcpy
#endif

#ifdef __cplusplus
}
#endif

#include <sec_api/stralign_s.h>
#endif