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
26 typedef UINT64 unixlib_handle_t
;
30 typedef NTSTATUS (*unixlib_entry_t
)( void *args
);
32 /* some useful helpers from ntdll */
33 extern const char *ntdll_get_build_dir(void);
34 extern const char *ntdll_get_data_dir(void);
35 extern DWORD
ntdll_umbstowcs( const char *src
, DWORD srclen
, WCHAR
*dst
, DWORD dstlen
);
36 extern int ntdll_wcstoumbs( const WCHAR
*src
, DWORD srclen
, char *dst
, DWORD dstlen
, BOOL strict
);
37 extern int ntdll_wcsicmp( const WCHAR
*str1
, const WCHAR
*str2
);
38 extern int ntdll_wcsnicmp( const WCHAR
*str1
, const WCHAR
*str2
, int n
);
39 extern NTSTATUS
ntdll_init_syscalls( SYSTEM_SERVICE_TABLE
*table
, void **dispatcher
);
41 /* exception handling */
44 typedef struct { int reg
[16]; } __wine_jmp_buf
;
45 #elif defined(__x86_64__)
46 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part
[2]; } reg
[16]; } __wine_jmp_buf
;
47 #elif defined(__arm__)
48 typedef struct { int reg
[28]; } __wine_jmp_buf
;
49 #elif defined(__aarch64__)
50 typedef struct { __int64 reg
[24]; } __wine_jmp_buf
;
52 typedef struct { int reg
; } __wine_jmp_buf
;
55 extern int __attribute__ ((__nothrow__
,__returns_twice__
)) __wine_setjmpex( __wine_jmp_buf
*buf
,
56 EXCEPTION_REGISTRATION_RECORD
*frame
);
57 extern void DECLSPEC_NORETURN
__wine_longjmp( __wine_jmp_buf
*buf
, int retval
);
58 extern void ntdll_set_exception_jmp_buf( __wine_jmp_buf
*jmp
);
61 do { __wine_jmp_buf __jmp; \
63 for (;;) if (!__first) \
69 ntdll_set_exception_jmp_buf( NULL ); \
72 if (__wine_setjmpex( &__jmp, NULL )) { \
79 ntdll_set_exception_jmp_buf( &__jmp ); \
84 NTSTATUS
KeUserModeCallback( ULONG id
, const void *args
, ULONG len
, void **ret_ptr
, ULONG
*ret_len
);
86 /* wide char string functions */
88 static inline int ntdll_iswspace( WCHAR wc
)
90 return ('\t' <= wc
&& wc
<= '\r') || wc
== ' ' || wc
== 0xa0;
93 static inline size_t ntdll_wcslen( const WCHAR
*str
)
100 static inline WCHAR
*ntdll_wcscpy( WCHAR
*dst
, const WCHAR
*src
)
103 while ((*p
++ = *src
++));
107 static inline WCHAR
*ntdll_wcscat( WCHAR
*dst
, const WCHAR
*src
)
109 ntdll_wcscpy( dst
+ ntdll_wcslen(dst
), src
);
113 static inline int ntdll_wcscmp( const WCHAR
*str1
, const WCHAR
*str2
)
115 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
116 return *str1
- *str2
;
119 static inline int ntdll_wcsncmp( const WCHAR
*str1
, const WCHAR
*str2
, int n
)
121 if (n
<= 0) return 0;
122 while ((--n
> 0) && *str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
123 return *str1
- *str2
;
126 static inline WCHAR
*ntdll_wcschr( const WCHAR
*str
, WCHAR ch
)
128 do { if (*str
== ch
) return (WCHAR
*)(ULONG_PTR
)str
; } while (*str
++);
132 static inline WCHAR
*ntdll_wcsrchr( const WCHAR
*str
, WCHAR ch
)
135 do { if (*str
== ch
) ret
= (WCHAR
*)(ULONG_PTR
)str
; } while (*str
++);
139 static inline WCHAR
*ntdll_wcspbrk( const WCHAR
*str
, const WCHAR
*accept
)
141 for ( ; *str
; str
++) if (ntdll_wcschr( accept
, *str
)) return (WCHAR
*)(ULONG_PTR
)str
;
145 static inline SIZE_T
ntdll_wcsspn( const WCHAR
*str
, const WCHAR
*accept
)
148 for (ptr
= str
; *ptr
; ptr
++) if (!ntdll_wcschr( accept
, *ptr
)) break;
152 static inline SIZE_T
ntdll_wcscspn( const WCHAR
*str
, const WCHAR
*reject
)
155 for (ptr
= str
; *ptr
; ptr
++) if (ntdll_wcschr( reject
, *ptr
)) break;
159 static inline LONG
ntdll_wcstol( const WCHAR
*s
, WCHAR
**end
, int base
)
161 BOOL negative
= FALSE
, empty
= TRUE
;
164 if (base
< 0 || base
== 1 || base
> 36) return 0;
165 if (end
) *end
= (WCHAR
*)s
;
166 while (ntdll_iswspace(*s
)) s
++;
173 else if (*s
== '+') s
++;
175 if ((base
== 0 || base
== 16) && s
[0] == '0' && (s
[1] == 'x' || s
[1] == 'X'))
180 if (base
== 0) base
= s
[0] != '0' ? 10 : 8;
186 if ('0' <= *s
&& *s
<= '9') v
= *s
- '0';
187 else if ('A' <= *s
&& *s
<= 'Z') v
= *s
- 'A' + 10;
188 else if ('a' <= *s
&& *s
<= 'z') v
= *s
- 'a' + 10;
190 if (v
>= base
) break;
191 if (negative
) v
= -v
;
195 if (!negative
&& (ret
> MAXLONG
/ base
|| ret
* base
> MAXLONG
- v
))
197 else if (negative
&& (ret
< (LONG
)MINLONG
/ base
|| ret
* base
< (LONG
)(MINLONG
- v
)))
200 ret
= ret
* base
+ v
;
203 if (end
&& !empty
) *end
= (WCHAR
*)s
;
207 static inline ULONG
ntdll_wcstoul( const WCHAR
*s
, WCHAR
**end
, int base
)
209 BOOL negative
= FALSE
, empty
= TRUE
;
212 if (base
< 0 || base
== 1 || base
> 36) return 0;
213 if (end
) *end
= (WCHAR
*)s
;
214 while (ntdll_iswspace(*s
)) s
++;
221 else if (*s
== '+') s
++;
223 if ((base
== 0 || base
== 16) && s
[0] == '0' && (s
[1] == 'x' || s
[1] == 'X'))
228 if (base
== 0) base
= s
[0] != '0' ? 10 : 8;
234 if ('0' <= *s
&& *s
<= '9') v
= *s
- '0';
235 else if ('A' <= *s
&& *s
<= 'Z') v
= *s
- 'A' + 10;
236 else if ('a' <= *s
&& *s
<= 'z') v
= *s
- 'a' + 10;
238 if (v
>= base
) break;
242 if (ret
> MAXDWORD
/ base
|| ret
* base
> MAXDWORD
- v
)
245 ret
= ret
* base
+ v
;
248 if (end
&& !empty
) *end
= (WCHAR
*)s
;
249 return negative
? -ret
: ret
;
252 #define iswspace(ch) ntdll_iswspace(ch)
253 #define wcslen(str) ntdll_wcslen(str)
254 #define wcscpy(dst,src) ntdll_wcscpy(dst,src)
255 #define wcscat(dst,src) ntdll_wcscat(dst,src)
256 #define wcscmp(s1,s2) ntdll_wcscmp(s1,s2)
257 #define wcsncmp(s1,s2,n) ntdll_wcsncmp(s1,s2,n)
258 #define wcschr(str,ch) ntdll_wcschr(str,ch)
259 #define wcsrchr(str,ch) ntdll_wcsrchr(str,ch)
260 #define wcspbrk(str,ac) ntdll_wcspbrk(str,ac)
261 #define wcsspn(str,ac) ntdll_wcsspn(str,ac)
262 #define wcscspn(str,rej) ntdll_wcscspn(str,rej)
263 #define wcsicmp(s1, s2) ntdll_wcsicmp(s1,s2)
264 #define wcsnicmp(s1, s2,n) ntdll_wcsnicmp(s1,s2,n)
265 #define wcstol(str,e,b) ntdll_wcstol(str,e,b)
266 #define wcstoul(str,e,b) ntdll_wcstoul(str,e,b)
268 #else /* WINE_UNIX_LIB */
270 extern NTSTATUS WINAPI
__wine_unix_call( unixlib_handle_t handle
, unsigned int code
, void *args
);
271 extern unixlib_handle_t __wine_unixlib_handle DECLSPEC_HIDDEN
;
272 extern NTSTATUS (WINAPI
*__wine_unix_call_dispatcher
)( unixlib_handle_t
, unsigned int, void * ) DECLSPEC_HIDDEN
;
273 extern NTSTATUS WINAPI
__wine_init_unix_call(void) DECLSPEC_HIDDEN
;
275 #define WINE_UNIX_CALL(code,args) __wine_unix_call_dispatcher( __wine_unixlib_handle, (code), (args) )
277 #endif /* WINE_UNIX_LIB */
279 #endif /* __WINE_WINE_UNIXLIB_H */