win32u: Store the service table entry directly into the ntdll variable.
[wine.git] / include / wine / unixlib.h
blob2a615179e64c683e972c2c54a2916ecda88fef03
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[];
34 extern DECLSPEC_EXPORT SYSTEM_SERVICE_TABLE KeServiceDescriptorTable[4];
36 /* some useful helpers from ntdll */
37 NTSYSAPI const char *ntdll_get_build_dir(void);
38 NTSYSAPI const char *ntdll_get_data_dir(void);
39 NTSYSAPI DWORD ntdll_umbstowcs( const char *src, DWORD srclen, WCHAR *dst, DWORD dstlen );
40 NTSYSAPI int ntdll_wcstoumbs( const WCHAR *src, DWORD srclen, char *dst, DWORD dstlen, BOOL strict );
41 NTSYSAPI int ntdll_wcsicmp( const WCHAR *str1, const WCHAR *str2 );
42 NTSYSAPI int ntdll_wcsnicmp( const WCHAR *str1, const WCHAR *str2, int n );
44 /* exception handling */
46 #ifdef __i386__
47 typedef struct { int reg[16]; } __wine_jmp_buf;
48 #elif defined(__x86_64__)
49 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part[2]; } reg[16]; } __wine_jmp_buf;
50 #elif defined(__arm__)
51 typedef struct { int reg[28]; } __wine_jmp_buf;
52 #elif defined(__aarch64__)
53 typedef struct { __int64 reg[24]; } __wine_jmp_buf;
54 #else
55 typedef struct { int reg; } __wine_jmp_buf;
56 #endif
58 NTSYSAPI int __attribute__ ((__nothrow__,__returns_twice__)) __wine_setjmpex( __wine_jmp_buf *buf,
59 EXCEPTION_REGISTRATION_RECORD *frame );
60 NTSYSAPI void DECLSPEC_NORETURN __wine_longjmp( __wine_jmp_buf *buf, int retval );
61 NTSYSAPI void ntdll_set_exception_jmp_buf( __wine_jmp_buf *jmp );
63 #define __TRY \
64 do { __wine_jmp_buf __jmp; \
65 int __first = 1; \
66 for (;;) if (!__first) \
67 { \
68 do {
70 #define __EXCEPT \
71 } while(0); \
72 ntdll_set_exception_jmp_buf( NULL ); \
73 break; \
74 } else { \
75 if (__wine_setjmpex( &__jmp, NULL )) { \
76 do {
78 #define __ENDTRY \
79 } while (0); \
80 break; \
81 } \
82 ntdll_set_exception_jmp_buf( &__jmp ); \
83 __first = 0; \
84 } \
85 } while (0);
87 NTSYSAPI NTSTATUS KeUserModeCallback( ULONG id, const void *args, ULONG len, void **ret_ptr, ULONG *ret_len );
89 /* wide char string functions */
91 static inline int ntdll_iswspace( WCHAR wc )
93 return ('\t' <= wc && wc <= '\r') || wc == ' ' || wc == 0xa0;
96 static inline size_t ntdll_wcslen( const WCHAR *str )
98 const WCHAR *s = str;
99 while (*s) s++;
100 return s - str;
103 static inline WCHAR *ntdll_wcscpy( WCHAR *dst, const WCHAR *src )
105 WCHAR *p = dst;
106 while ((*p++ = *src++));
107 return dst;
110 static inline WCHAR *ntdll_wcscat( WCHAR *dst, const WCHAR *src )
112 ntdll_wcscpy( dst + ntdll_wcslen(dst), src );
113 return dst;
116 static inline int ntdll_wcscmp( const WCHAR *str1, const WCHAR *str2 )
118 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
119 return *str1 - *str2;
122 static inline int ntdll_wcsncmp( const WCHAR *str1, const WCHAR *str2, int n )
124 if (n <= 0) return 0;
125 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
126 return *str1 - *str2;
129 static inline WCHAR *ntdll_wcschr( const WCHAR *str, WCHAR ch )
131 do { if (*str == ch) return (WCHAR *)(ULONG_PTR)str; } while (*str++);
132 return NULL;
135 static inline WCHAR *ntdll_wcsrchr( const WCHAR *str, WCHAR ch )
137 WCHAR *ret = NULL;
138 do { if (*str == ch) ret = (WCHAR *)(ULONG_PTR)str; } while (*str++);
139 return ret;
142 static inline WCHAR *ntdll_wcspbrk( const WCHAR *str, const WCHAR *accept )
144 for ( ; *str; str++) if (ntdll_wcschr( accept, *str )) return (WCHAR *)(ULONG_PTR)str;
145 return NULL;
148 static inline SIZE_T ntdll_wcsspn( const WCHAR *str, const WCHAR *accept )
150 const WCHAR *ptr;
151 for (ptr = str; *ptr; ptr++) if (!ntdll_wcschr( accept, *ptr )) break;
152 return ptr - str;
155 static inline SIZE_T ntdll_wcscspn( const WCHAR *str, const WCHAR *reject )
157 const WCHAR *ptr;
158 for (ptr = str; *ptr; ptr++) if (ntdll_wcschr( reject, *ptr )) break;
159 return ptr - str;
162 static inline LONG ntdll_wcstol( const WCHAR *s, WCHAR **end, int base )
164 BOOL negative = FALSE, empty = TRUE;
165 LONG ret = 0;
167 if (base < 0 || base == 1 || base > 36) return 0;
168 if (end) *end = (WCHAR *)s;
169 while (ntdll_iswspace(*s)) s++;
171 if (*s == '-')
173 negative = TRUE;
174 s++;
176 else if (*s == '+') s++;
178 if ((base == 0 || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
180 base = 16;
181 s += 2;
183 if (base == 0) base = s[0] != '0' ? 10 : 8;
185 while (*s)
187 int v;
189 if ('0' <= *s && *s <= '9') v = *s - '0';
190 else if ('A' <= *s && *s <= 'Z') v = *s - 'A' + 10;
191 else if ('a' <= *s && *s <= 'z') v = *s - 'a' + 10;
192 else break;
193 if (v >= base) break;
194 if (negative) v = -v;
195 s++;
196 empty = FALSE;
198 if (!negative && (ret > MAXLONG / base || ret * base > MAXLONG - v))
199 ret = MAXLONG;
200 else if (negative && (ret < (LONG)MINLONG / base || ret * base < (LONG)(MINLONG - v)))
201 ret = MINLONG;
202 else
203 ret = ret * base + v;
206 if (end && !empty) *end = (WCHAR *)s;
207 return ret;
210 static inline ULONG ntdll_wcstoul( const WCHAR *s, WCHAR **end, int base )
212 BOOL negative = FALSE, empty = TRUE;
213 ULONG ret = 0;
215 if (base < 0 || base == 1 || base > 36) return 0;
216 if (end) *end = (WCHAR *)s;
217 while (ntdll_iswspace(*s)) s++;
219 if (*s == '-')
221 negative = TRUE;
222 s++;
224 else if (*s == '+') s++;
226 if ((base == 0 || base == 16) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
228 base = 16;
229 s += 2;
231 if (base == 0) base = s[0] != '0' ? 10 : 8;
233 while (*s)
235 int v;
237 if ('0' <= *s && *s <= '9') v = *s - '0';
238 else if ('A' <= *s && *s <= 'Z') v = *s - 'A' + 10;
239 else if ('a' <= *s && *s <= 'z') v = *s - 'a' + 10;
240 else break;
241 if (v >= base) break;
242 s++;
243 empty = FALSE;
245 if (ret > MAXDWORD / base || ret * base > MAXDWORD - v)
246 ret = MAXDWORD;
247 else
248 ret = ret * base + v;
251 if (end && !empty) *end = (WCHAR *)s;
252 return negative ? -ret : ret;
255 #define iswspace(ch) ntdll_iswspace(ch)
256 #define wcslen(str) ntdll_wcslen(str)
257 #define wcscpy(dst,src) ntdll_wcscpy(dst,src)
258 #define wcscat(dst,src) ntdll_wcscat(dst,src)
259 #define wcscmp(s1,s2) ntdll_wcscmp(s1,s2)
260 #define wcsncmp(s1,s2,n) ntdll_wcsncmp(s1,s2,n)
261 #define wcschr(str,ch) ntdll_wcschr(str,ch)
262 #define wcsrchr(str,ch) ntdll_wcsrchr(str,ch)
263 #define wcspbrk(str,ac) ntdll_wcspbrk(str,ac)
264 #define wcsspn(str,ac) ntdll_wcsspn(str,ac)
265 #define wcscspn(str,rej) ntdll_wcscspn(str,rej)
266 #define wcsicmp(s1, s2) ntdll_wcsicmp(s1,s2)
267 #define wcsnicmp(s1, s2,n) ntdll_wcsnicmp(s1,s2,n)
268 #define wcstol(str,e,b) ntdll_wcstol(str,e,b)
269 #define wcstoul(str,e,b) ntdll_wcstoul(str,e,b)
271 #else /* WINE_UNIX_LIB */
273 NTSYSAPI NTSTATUS WINAPI __wine_unix_call( unixlib_handle_t handle, unsigned int code, void *args );
274 extern unixlib_handle_t __wine_unixlib_handle;
275 extern NTSTATUS (WINAPI *__wine_unix_call_dispatcher)( unixlib_handle_t, unsigned int, void * );
276 extern NTSTATUS WINAPI __wine_init_unix_call(void);
278 #define WINE_UNIX_CALL(code,args) __wine_unix_call_dispatcher( __wine_unixlib_handle, (code), (args) )
280 #endif /* WINE_UNIX_LIB */
282 #endif /* __WINE_WINE_UNIXLIB_H */