2166f4530ab6d9abb970580a48e6c22c15872a9b
2 * winmisc.c: miscellaneous Windows-specific things
10 OSVERSIONINFO osVersion
;
12 char *platform_get_x_display(void) {
13 /* We may as well check for DISPLAY in case it's useful. */
14 return dupstr(getenv("DISPLAY"));
17 Filename
*filename_from_str(const char *str
)
19 Filename
*ret
= snew(Filename
);
20 ret
->path
= dupstr(str
);
24 Filename
*filename_copy(const Filename
*fn
)
26 return filename_from_str(fn
->path
);
29 const char *filename_to_str(const Filename
*fn
)
34 int filename_equal(const Filename
*f1
, const Filename
*f2
)
36 return !strcmp(f1
->path
, f2
->path
);
39 int filename_is_null(const Filename
*fn
)
44 void filename_free(Filename
*fn
)
50 int filename_serialise(const Filename
*f
, void *vdata
)
52 char *data
= (char *)vdata
;
53 int len
= strlen(f
->path
) + 1; /* include trailing NUL */
55 strcpy(data
, f
->path
);
59 Filename
*filename_deserialise(void *vdata
, int maxsize
, int *used
)
61 char *data
= (char *)vdata
;
63 end
= memchr(data
, '\0', maxsize
);
68 return filename_from_str(data
);
72 * Windows implementation of smemclr (see misc.c) using SecureZeroMemory.
74 void smemclr(void *b
, size_t n
) {
76 SecureZeroMemory(b
, n
);
79 char *get_username(void)
83 int got_username
= FALSE
;
84 DECL_WINDOWS_FUNCTION(static, BOOLEAN
, GetUserNameExA
,
85 (EXTENDED_NAME_FORMAT
, LPSTR
, PULONG
));
88 static int tried_usernameex
= FALSE
;
89 if (!tried_usernameex
) {
90 /* Not available on Win9x, so load dynamically */
91 HMODULE secur32
= load_system32_dll("secur32.dll");
92 GET_WINDOWS_FUNCTION(secur32
, GetUserNameExA
);
93 tried_usernameex
= TRUE
;
97 if (p_GetUserNameExA
) {
99 * If available, use the principal -- this avoids the problem
100 * that the local username is case-insensitive but Kerberos
101 * usernames are case-sensitive.
106 (void) p_GetUserNameExA(NameUserPrincipal
, NULL
, &namelen
);
108 user
= snewn(namelen
, char);
109 got_username
= p_GetUserNameExA(NameUserPrincipal
, user
, &namelen
);
111 char *p
= strchr(user
, '@');
119 /* Fall back to local user name */
121 if (GetUserName(NULL
, &namelen
) == FALSE
) {
123 * Apparently this doesn't work at least on Windows XP SP2.
124 * Thus assume a maximum of 256. It will fail again if it
130 user
= snewn(namelen
, char);
131 got_username
= GetUserName(user
, &namelen
);
137 return got_username ? user
: NULL
;
140 BOOL
init_winver(void)
142 ZeroMemory(&osVersion
, sizeof(osVersion
));
143 osVersion
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
144 return GetVersionEx ( (OSVERSIONINFO
*) &osVersion
);
147 HMODULE
load_system32_dll(const char *libname
)
150 * Wrapper function to load a DLL out of c:\windows\system32
151 * without going through the full DLL search path. (Hence no
152 * attack is possible by placing a substitute DLL earlier on that
155 static char *sysdir
= NULL
;
162 size
= 3*size
/2 + 512;
163 sysdir
= sresize(sysdir
, size
, char);
164 len
= GetSystemDirectory(sysdir
, size
);
165 } while (len
>= size
);
168 fullpath
= dupcat(sysdir
, "\\", libname
, NULL
);
169 ret
= LoadLibrary(fullpath
);
175 static FILE *debug_fp
= NULL
;
176 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
177 static int debug_got_console
= 0;
179 void dputs(char *buf
)
183 if (!debug_got_console
) {
184 if (AllocConsole()) {
185 debug_got_console
= 1;
186 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
190 debug_fp
= fopen("debug.log", "w");
193 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
194 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
196 fputs(buf
, debug_fp
);
203 * Minefield - a Windows equivalent for Electric Fence
206 #define PAGESIZE 4096
211 * We start by reserving as much virtual address space as Windows
212 * will sensibly (or not sensibly) let us have. We flag it all as
215 * Any allocation attempt is satisfied by committing one or more
216 * pages, with an uncommitted page on either side. The returned
217 * memory region is jammed up against the _end_ of the pages.
219 * Freeing anything causes instantaneous decommitment of the pages
220 * involved, so stale pointers are caught as soon as possible.
223 static int minefield_initialised
= 0;
224 static void *minefield_region
= NULL
;
225 static long minefield_size
= 0;
226 static long minefield_npages
= 0;
227 static long minefield_curpos
= 0;
228 static unsigned short *minefield_admin
= NULL
;
229 static void *minefield_pages
= NULL
;
231 static void minefield_admin_hide(int hide
)
233 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
234 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
237 static void minefield_init(void)
243 for (size
= 0x40000000; size
> 0; size
= ((size
>> 3) * 7) & ~0xFFF) {
244 minefield_region
= VirtualAlloc(NULL
, size
,
245 MEM_RESERVE
, PAGE_NOACCESS
);
246 if (minefield_region
)
249 minefield_size
= size
;
252 * Firstly, allocate a section of that to be the admin block.
253 * We'll need a two-byte field for each page.
255 minefield_admin
= minefield_region
;
256 minefield_npages
= minefield_size
/ PAGESIZE
;
257 admin_size
= (minefield_npages
* 2 + PAGESIZE
- 1) & ~(PAGESIZE
- 1);
258 minefield_npages
= (minefield_size
- admin_size
) / PAGESIZE
;
259 minefield_pages
= (char *) minefield_region
+ admin_size
;
262 * Commit the admin region.
264 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
265 MEM_COMMIT
, PAGE_READWRITE
);
268 * Mark all pages as unused (0xFFFF).
270 for (i
= 0; i
< minefield_npages
; i
++)
271 minefield_admin
[i
] = 0xFFFF;
274 * Hide the admin region.
276 minefield_admin_hide(1);
278 minefield_initialised
= 1;
281 static void minefield_bomb(void)
283 div(1, *(int *) minefield_pages
);
286 static void *minefield_alloc(int size
)
289 int pos
, lim
, region_end
, region_start
;
293 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
295 minefield_admin_hide(0);
298 * Search from current position until we find a contiguous
299 * bunch of npages+2 unused pages.
301 pos
= minefield_curpos
;
302 lim
= minefield_npages
;
304 /* Skip over used pages. */
305 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
307 /* Count unused pages. */
309 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
310 minefield_admin
[pos
] == 0xFFFF)
312 if (pos
- start
== npages
+ 2)
314 /* If we've reached the limit, reset the limit or stop. */
316 if (lim
== minefield_npages
) {
317 /* go round and start again at zero */
318 lim
= minefield_curpos
;
321 minefield_admin_hide(1);
327 minefield_curpos
= pos
- 1;
330 * We have npages+2 unused pages starting at start. We leave
331 * the first and last of these alone and use the rest.
333 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
334 region_start
= region_end
- size
;
335 /* FIXME: could align here if we wanted */
338 * Update the admin region.
340 for (i
= start
+ 2; i
< start
+ npages
+ 1; i
++)
341 minefield_admin
[i
] = 0xFFFE; /* used but no region starts here */
342 minefield_admin
[start
+ 1] = region_start
% PAGESIZE
;
344 minefield_admin_hide(1);
346 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
347 MEM_COMMIT
, PAGE_READWRITE
);
348 return (char *) minefield_pages
+ region_start
;
351 static void minefield_free(void *ptr
)
353 int region_start
, i
, j
;
355 minefield_admin_hide(0);
357 region_start
= (char *) ptr
- (char *) minefield_pages
;
358 i
= region_start
/ PAGESIZE
;
359 if (i
< 0 || i
>= minefield_npages
||
360 minefield_admin
[i
] != region_start
% PAGESIZE
)
362 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
363 minefield_admin
[j
] = 0xFFFF;
366 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
368 minefield_admin_hide(1);
371 static int minefield_get_size(void *ptr
)
373 int region_start
, i
, j
;
375 minefield_admin_hide(0);
377 region_start
= (char *) ptr
- (char *) minefield_pages
;
378 i
= region_start
/ PAGESIZE
;
379 if (i
< 0 || i
>= minefield_npages
||
380 minefield_admin
[i
] != region_start
% PAGESIZE
)
382 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
384 minefield_admin_hide(1);
386 return j
* PAGESIZE
- region_start
;
389 void *minefield_c_malloc(size_t size
)
391 if (!minefield_initialised
)
393 return minefield_alloc(size
);
396 void minefield_c_free(void *p
)
398 if (!minefield_initialised
)
404 * realloc _always_ moves the chunk, for rapid detection of code
405 * that assumes it won't.
407 void *minefield_c_realloc(void *p
, size_t size
)
411 if (!minefield_initialised
)
413 q
= minefield_alloc(size
);
414 oldsize
= minefield_get_size(p
);
415 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
420 #endif /* MINEFIELD */
422 FontSpec
*fontspec_new(const char *name
,
423 int bold
, int height
, int charset
)
425 FontSpec
*f
= snew(FontSpec
);
426 f
->name
= dupstr(name
);
429 f
->charset
= charset
;
432 FontSpec
*fontspec_copy(const FontSpec
*f
)
434 return fontspec_new(f
->name
, f
->isbold
, f
->height
, f
->charset
);
436 void fontspec_free(FontSpec
*f
)
441 int fontspec_serialise(FontSpec
*f
, void *vdata
)
443 char *data
= (char *)vdata
;
444 int len
= strlen(f
->name
) + 1; /* include trailing NUL */
446 strcpy(data
, f
->name
);
447 PUT_32BIT_MSB_FIRST(data
+ len
, f
->isbold
);
448 PUT_32BIT_MSB_FIRST(data
+ len
+ 4, f
->height
);
449 PUT_32BIT_MSB_FIRST(data
+ len
+ 8, f
->charset
);
451 return len
+ 12; /* also include three 4-byte ints */
453 FontSpec
*fontspec_deserialise(void *vdata
, int maxsize
, int *used
)
455 char *data
= (char *)vdata
;
459 end
= memchr(data
, '\0', maxsize
-12);
463 *used
= end
- data
+ 12;
464 return fontspec_new(data
,
465 GET_32BIT_MSB_FIRST(end
),
466 GET_32BIT_MSB_FIRST(end
+ 4),
467 GET_32BIT_MSB_FIRST(end
+ 8));