winegstreamer: Set 'max_threads' to 4 for 32-bit processors.
[wine.git] / include / wine / unixlib.h
blob9a342fada73297d257b741c54b91704541a5a258
1 /*
2 * Definitions for Unix libraries
4 * Copyright (C) 2021 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_WINE_UNIXLIB_H
22 #define __WINE_WINE_UNIXLIB_H
24 #include "winternl.h"
26 typedef UINT64 unixlib_handle_t;
28 #ifdef WINE_UNIX_LIB
30 typedef NTSTATUS (*unixlib_entry_t)( void *args );
32 extern DECLSPEC_EXPORT const unixlib_entry_t __wine_unix_call_funcs[];
33 extern DECLSPEC_EXPORT const unixlib_entry_t __wine_unix_call_wow64_funcs[];
35 /* some useful helpers from ntdll */
36 NTSYSAPI const char *ntdll_get_build_dir(void);
37 NTSYSAPI const char *ntdll_get_data_dir(void);
38 NTSYSAPI DWORD ntdll_umbstowcs( const char *src, DWORD srclen, WCHAR *dst, DWORD dstlen );
39 NTSYSAPI int ntdll_wcstoumbs( const WCHAR *src, DWORD srclen, char *dst, DWORD dstlen, BOOL strict );
40 NTSYSAPI int ntdll_wcsicmp( const WCHAR *str1, const WCHAR *str2 );
41 NTSYSAPI int ntdll_wcsnicmp( const WCHAR *str1, const WCHAR *str2, int n );
43 /* exception handling */
45 #include <setjmp.h>
47 NTSYSAPI void ntdll_set_exception_jmp_buf( jmp_buf jmp );
49 #define __TRY \
50 do { jmp_buf __jmp; \
51 int __first = 1; \
52 for (;;) if (!__first) \
53 { \
54 do {
56 #define __EXCEPT \
57 } while(0); \
58 ntdll_set_exception_jmp_buf( NULL ); \
59 break; \
60 } else { \
61 if (setjmp( __jmp )) { \
62 do {
64 #define __ENDTRY \
65 } while (0); \
66 break; \
67 } \
68 ntdll_set_exception_jmp_buf( __jmp ); \
69 __first = 0; \
70 } \
71 } while (0);
73 NTSYSAPI BOOLEAN KeAddSystemServiceTable( ULONG_PTR *funcs, ULONG_PTR *counters, ULONG limit,
74 BYTE *arguments, ULONG index );
75 NTSYSAPI NTSTATUS KeUserModeCallback( ULONG id, const void *args, ULONG len, void **ret_ptr, ULONG *ret_len );
77 /* wide char string functions */
79 static inline int ntdll_iswspace( WCHAR wc )
81 return ('\t' <= wc && wc <= '\r') || wc == ' ' || wc == 0xa0;
84 static inline size_t ntdll_wcslen( const WCHAR *str )
86 const WCHAR *s = str;
87 while (*s) s++;
88 return s - str;
91 static inline WCHAR *ntdll_wcscpy( WCHAR *dst, const WCHAR *src )
93 WCHAR *p = dst;
94 while ((*p++ = *src++));
95 return dst;
98 static inline WCHAR *ntdll_wcscat( WCHAR *dst, const WCHAR *src )
100 ntdll_wcscpy( dst + ntdll_wcslen(dst), src );
101 return dst;
104 static inline int ntdll_wcscmp( const WCHAR *str1, const WCHAR *str2 )
106 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
107 return *str1 - *str2;
110 static inline int ntdll_wcsncmp( const WCHAR *str1, const WCHAR *str2, int n )
112 if (n <= 0) return 0;
113 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
114 return *str1 - *str2;
117 static inline WCHAR *ntdll_wcschr( const WCHAR *str, WCHAR ch )
119 do { if (*str == ch) return (WCHAR *)(ULONG_PTR)str; } while (*str++);
120 return NULL;
123 static inline WCHAR *ntdll_wcsrchr( const WCHAR *str, WCHAR ch )
125 WCHAR *ret = NULL;
126 do { if (*str == ch) ret = (WCHAR *)(ULONG_PTR)str; } while (*str++);
127 return ret;
130 static inline WCHAR *ntdll_wcspbrk( const WCHAR *str, const WCHAR *accept )
132 for ( ; *str; str++) if (ntdll_wcschr( accept, *str )) return (WCHAR *)(ULONG_PTR)str;
133 return NULL;
136 static inline SIZE_T ntdll_wcsspn( const WCHAR *str, const WCHAR *accept )
138 const WCHAR *ptr;
139 for (ptr = str; *ptr; ptr++) if (!ntdll_wcschr( accept, *ptr )) break;
140 return ptr - str;
143 static inline SIZE_T ntdll_wcscspn( const WCHAR *str, const WCHAR *reject )
145 const WCHAR *ptr;
146 for (ptr = str; *ptr; ptr++) if (ntdll_wcschr( reject, *ptr )) break;
147 return ptr - str;
150 static inline LONG ntdll_wcstol( const WCHAR *s, WCHAR **end, int base )
152 BOOL negative = FALSE, empty = TRUE;
153 LONG ret = 0;
155 if (base < 0 || base == 1 || base > 36) return 0;
156 if (end) *end = (WCHAR *)s;
157 while (ntdll_iswspace(*s)) s++;
159 if (*s == '-')
161 negative = TRUE;
162 s++;
164 else if (*s == '+') s++;
166 if ((base == 0 || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
168 base = 16;
169 s += 2;
171 if (base == 0) base = s[0] != '0' ? 10 : 8;
173 while (*s)
175 int v;
177 if ('0' <= *s && *s <= '9') v = *s - '0';
178 else if ('A' <= *s && *s <= 'Z') v = *s - 'A' + 10;
179 else if ('a' <= *s && *s <= 'z') v = *s - 'a' + 10;
180 else break;
181 if (v >= base) break;
182 if (negative) v = -v;
183 s++;
184 empty = FALSE;
186 if (!negative && (ret > MAXLONG / base || ret * base > MAXLONG - v))
187 ret = MAXLONG;
188 else if (negative && (ret < (LONG)MINLONG / base || ret * base < (LONG)(MINLONG - v)))
189 ret = MINLONG;
190 else
191 ret = ret * base + v;
194 if (end && !empty) *end = (WCHAR *)s;
195 return ret;
198 static inline ULONG ntdll_wcstoul( const WCHAR *s, WCHAR **end, int base )
200 BOOL negative = FALSE, empty = TRUE;
201 ULONG ret = 0;
203 if (base < 0 || base == 1 || base > 36) return 0;
204 if (end) *end = (WCHAR *)s;
205 while (ntdll_iswspace(*s)) s++;
207 if (*s == '-')
209 negative = TRUE;
210 s++;
212 else if (*s == '+') s++;
214 if ((base == 0 || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
216 base = 16;
217 s += 2;
219 if (base == 0) base = s[0] != '0' ? 10 : 8;
221 while (*s)
223 int v;
225 if ('0' <= *s && *s <= '9') v = *s - '0';
226 else if ('A' <= *s && *s <= 'Z') v = *s - 'A' + 10;
227 else if ('a' <= *s && *s <= 'z') v = *s - 'a' + 10;
228 else break;
229 if (v >= base) break;
230 s++;
231 empty = FALSE;
233 if (ret > MAXDWORD / base || ret * base > MAXDWORD - v)
234 ret = MAXDWORD;
235 else
236 ret = ret * base + v;
239 if (end && !empty) *end = (WCHAR *)s;
240 return negative ? -ret : ret;
243 #define iswspace(ch) ntdll_iswspace(ch)
244 #define wcslen(str) ntdll_wcslen(str)
245 #define wcscpy(dst,src) ntdll_wcscpy(dst,src)
246 #define wcscat(dst,src) ntdll_wcscat(dst,src)
247 #define wcscmp(s1,s2) ntdll_wcscmp(s1,s2)
248 #define wcsncmp(s1,s2,n) ntdll_wcsncmp(s1,s2,n)
249 #define wcschr(str,ch) ntdll_wcschr(str,ch)
250 #define wcsrchr(str,ch) ntdll_wcsrchr(str,ch)
251 #define wcspbrk(str,ac) ntdll_wcspbrk(str,ac)
252 #define wcsspn(str,ac) ntdll_wcsspn(str,ac)
253 #define wcscspn(str,rej) ntdll_wcscspn(str,rej)
254 #define wcsicmp(s1, s2) ntdll_wcsicmp(s1,s2)
255 #define wcsnicmp(s1, s2,n) ntdll_wcsnicmp(s1,s2,n)
256 #define wcstol(str,e,b) ntdll_wcstol(str,e,b)
257 #define wcstoul(str,e,b) ntdll_wcstoul(str,e,b)
259 #else /* WINE_UNIX_LIB */
261 extern unixlib_handle_t __wine_unixlib_handle;
262 extern NTSTATUS (WINAPI *__wine_unix_call_dispatcher)( unixlib_handle_t, unsigned int, void * );
263 extern NTSTATUS WINAPI __wine_init_unix_call(void);
265 #ifdef __arm64ec__
266 NTSTATUS __wine_unix_call_arm64ec( unixlib_handle_t handle, unsigned int code, void *args );
267 static inline NTSTATUS __wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args )
269 return __wine_unix_call_arm64ec( handle, code, args );
271 #else
272 static inline NTSTATUS __wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args )
274 return __wine_unix_call_dispatcher( handle, code, args );
276 #endif
278 #define WINE_UNIX_CALL(code,args) __wine_unix_call( __wine_unixlib_handle, (code), (args) )
280 #endif /* WINE_UNIX_LIB */
282 #endif /* __WINE_WINE_UNIXLIB_H */