Turn 'Filename' into a dynamically allocated type with no arbitrary
[u/mdw/putty] / windows / winmisc.c
CommitLineData
e0e7dff8 1/*
f4ff9455 2 * winmisc.c: miscellaneous Windows-specific things
e0e7dff8 3 */
4
e0e7dff8 5#include <stdio.h>
6#include <stdlib.h>
7#include "putty.h"
4c26bb50 8#include <security.h>
e0e7dff8 9
4c48c989 10OSVERSIONINFO osVersion;
11
46ed7b64 12char *platform_get_x_display(void) {
13 /* We may as well check for DISPLAY in case it's useful. */
14 return dupstr(getenv("DISPLAY"));
15}
16
962468d4 17Filename *filename_from_str(const char *str)
9a30e26b 18{
962468d4 19 Filename *ret = snew(Filename);
20 ret->path = dupstr(str);
9a30e26b 21 return ret;
22}
23
962468d4 24Filename *filename_copy(const Filename *fn)
25{
26 return filename_from_str(fn->path);
27}
28
9fab77dc 29const char *filename_to_str(const Filename *fn)
9a30e26b 30{
9fab77dc 31 return fn->path;
9a30e26b 32}
33
962468d4 34int filename_equal(const Filename *f1, const Filename *f2)
35{
36 return !strcmp(f1->path, f2->path);
37}
38
39int filename_is_null(const Filename *fn)
9a30e26b 40{
962468d4 41 return !*fn->path;
9a30e26b 42}
43
962468d4 44void filename_free(Filename *fn)
45{
46 sfree(fn->path);
47 sfree(fn);
48}
49
50int filename_serialise(const Filename *f, void *vdata)
51{
52 char *data = (char *)vdata;
53 int len = strlen(f->path) + 1; /* include trailing NUL */
54 if (data) {
55 strcpy(data, f->path);
56 }
57 return len;
58}
59Filename *filename_deserialise(void *vdata, int maxsize, int *used)
9a30e26b 60{
962468d4 61 char *data = (char *)vdata;
62 char *end;
63 end = memchr(data, '\0', maxsize);
64 if (!end)
65 return NULL;
66 end++;
67 *used = end - data;
68 return filename_from_str(data);
9a30e26b 69}
d0912d1f 70
799dfcfa 71char *get_username(void)
72{
73 DWORD namelen;
74 char *user;
4c26bb50 75 int got_username = FALSE;
76 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
77 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
799dfcfa 78
4c26bb50 79 {
80 static int tried_usernameex = FALSE;
81 if (!tried_usernameex) {
82 /* Not available on Win9x, so load dynamically */
bda368a5 83 HMODULE secur32 = load_system32_dll("secur32.dll");
4c26bb50 84 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
85 tried_usernameex = TRUE;
86 }
87 }
88
89 if (p_GetUserNameExA) {
05581745 90 /*
4c26bb50 91 * If available, use the principal -- this avoids the problem
92 * that the local username is case-insensitive but Kerberos
93 * usernames are case-sensitive.
05581745 94 */
4c26bb50 95
96 /* Get the length */
97 namelen = 0;
98 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
99
100 user = snewn(namelen, char);
101 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
102 if (got_username) {
103 char *p = strchr(user, '@');
104 if (p) *p = 0;
105 } else {
106 sfree(user);
107 }
05581745 108 }
799dfcfa 109
4c26bb50 110 if (!got_username) {
111 /* Fall back to local user name */
112 namelen = 0;
113 if (GetUserName(NULL, &namelen) == FALSE) {
114 /*
115 * Apparently this doesn't work at least on Windows XP SP2.
116 * Thus assume a maximum of 256. It will fail again if it
117 * doesn't fit.
118 */
119 namelen = 256;
120 }
121
122 user = snewn(namelen, char);
123 got_username = GetUserName(user, &namelen);
124 if (!got_username) {
125 sfree(user);
126 }
127 }
799dfcfa 128
4c26bb50 129 return got_username ? user : NULL;
799dfcfa 130}
131
4c48c989 132BOOL init_winver(void)
133{
134 ZeroMemory(&osVersion, sizeof(osVersion));
135 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
136 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
137}
138
bda368a5 139HMODULE load_system32_dll(const char *libname)
140{
141 /*
142 * Wrapper function to load a DLL out of c:\windows\system32
143 * without going through the full DLL search path. (Hence no
144 * attack is possible by placing a substitute DLL earlier on that
145 * path.)
146 */
147 static char *sysdir = NULL;
148 char *fullpath;
149 HMODULE ret;
150
151 if (!sysdir) {
152 int size = 0, len;
153 do {
154 size = 3*size/2 + 512;
155 sysdir = sresize(sysdir, size, char);
156 len = GetSystemDirectory(sysdir, size);
157 } while (len >= size);
158 }
159
160 fullpath = dupcat(sysdir, "\\", libname, NULL);
161 ret = LoadLibrary(fullpath);
162 sfree(fullpath);
163 return ret;
164}
165
d0912d1f 166#ifdef DEBUG
167static FILE *debug_fp = NULL;
168static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
169static int debug_got_console = 0;
170
171void dputs(char *buf)
172{
173 DWORD dw;
174
175 if (!debug_got_console) {
176 if (AllocConsole()) {
177 debug_got_console = 1;
178 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
179 }
180 }
181 if (!debug_fp) {
182 debug_fp = fopen("debug.log", "w");
183 }
184
185 if (debug_hdl != INVALID_HANDLE_VALUE) {
186 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
187 }
188 fputs(buf, debug_fp);
189 fflush(debug_fp);
190}
191#endif
192
193#ifdef MINEFIELD
194/*
195 * Minefield - a Windows equivalent for Electric Fence
196 */
197
198#define PAGESIZE 4096
199
200/*
201 * Design:
202 *
203 * We start by reserving as much virtual address space as Windows
204 * will sensibly (or not sensibly) let us have. We flag it all as
205 * invalid memory.
206 *
207 * Any allocation attempt is satisfied by committing one or more
208 * pages, with an uncommitted page on either side. The returned
209 * memory region is jammed up against the _end_ of the pages.
210 *
211 * Freeing anything causes instantaneous decommitment of the pages
212 * involved, so stale pointers are caught as soon as possible.
213 */
214
215static int minefield_initialised = 0;
216static void *minefield_region = NULL;
217static long minefield_size = 0;
218static long minefield_npages = 0;
219static long minefield_curpos = 0;
220static unsigned short *minefield_admin = NULL;
221static void *minefield_pages = NULL;
222
223static void minefield_admin_hide(int hide)
224{
225 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
226 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
227}
228
229static void minefield_init(void)
230{
231 int size;
232 int admin_size;
233 int i;
234
235 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
236 minefield_region = VirtualAlloc(NULL, size,
237 MEM_RESERVE, PAGE_NOACCESS);
238 if (minefield_region)
239 break;
240 }
241 minefield_size = size;
242
243 /*
244 * Firstly, allocate a section of that to be the admin block.
245 * We'll need a two-byte field for each page.
246 */
247 minefield_admin = minefield_region;
248 minefield_npages = minefield_size / PAGESIZE;
249 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
250 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
251 minefield_pages = (char *) minefield_region + admin_size;
252
253 /*
254 * Commit the admin region.
255 */
256 VirtualAlloc(minefield_admin, minefield_npages * 2,
257 MEM_COMMIT, PAGE_READWRITE);
258
259 /*
260 * Mark all pages as unused (0xFFFF).
261 */
262 for (i = 0; i < minefield_npages; i++)
263 minefield_admin[i] = 0xFFFF;
264
265 /*
266 * Hide the admin region.
267 */
268 minefield_admin_hide(1);
269
270 minefield_initialised = 1;
271}
272
273static void minefield_bomb(void)
274{
275 div(1, *(int *) minefield_pages);
276}
277
278static void *minefield_alloc(int size)
279{
280 int npages;
281 int pos, lim, region_end, region_start;
282 int start;
283 int i;
284
285 npages = (size + PAGESIZE - 1) / PAGESIZE;
286
287 minefield_admin_hide(0);
288
289 /*
290 * Search from current position until we find a contiguous
291 * bunch of npages+2 unused pages.
292 */
293 pos = minefield_curpos;
294 lim = minefield_npages;
295 while (1) {
296 /* Skip over used pages. */
297 while (pos < lim && minefield_admin[pos] != 0xFFFF)
298 pos++;
299 /* Count unused pages. */
300 start = pos;
301 while (pos < lim && pos - start < npages + 2 &&
302 minefield_admin[pos] == 0xFFFF)
303 pos++;
304 if (pos - start == npages + 2)
305 break;
306 /* If we've reached the limit, reset the limit or stop. */
307 if (pos >= lim) {
308 if (lim == minefield_npages) {
309 /* go round and start again at zero */
310 lim = minefield_curpos;
311 pos = 0;
312 } else {
313 minefield_admin_hide(1);
314 return NULL;
315 }
316 }
317 }
318
319 minefield_curpos = pos - 1;
320
321 /*
322 * We have npages+2 unused pages starting at start. We leave
323 * the first and last of these alone and use the rest.
324 */
325 region_end = (start + npages + 1) * PAGESIZE;
326 region_start = region_end - size;
327 /* FIXME: could align here if we wanted */
328
329 /*
330 * Update the admin region.
331 */
332 for (i = start + 2; i < start + npages + 1; i++)
333 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
334 minefield_admin[start + 1] = region_start % PAGESIZE;
335
336 minefield_admin_hide(1);
337
338 VirtualAlloc((char *) minefield_pages + region_start, size,
339 MEM_COMMIT, PAGE_READWRITE);
340 return (char *) minefield_pages + region_start;
341}
342
343static void minefield_free(void *ptr)
344{
345 int region_start, i, j;
346
347 minefield_admin_hide(0);
348
349 region_start = (char *) ptr - (char *) minefield_pages;
350 i = region_start / PAGESIZE;
351 if (i < 0 || i >= minefield_npages ||
352 minefield_admin[i] != region_start % PAGESIZE)
353 minefield_bomb();
354 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
355 minefield_admin[j] = 0xFFFF;
356 }
357
358 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
359
360 minefield_admin_hide(1);
361}
362
363static int minefield_get_size(void *ptr)
364{
365 int region_start, i, j;
366
367 minefield_admin_hide(0);
368
369 region_start = (char *) ptr - (char *) minefield_pages;
370 i = region_start / PAGESIZE;
371 if (i < 0 || i >= minefield_npages ||
372 minefield_admin[i] != region_start % PAGESIZE)
373 minefield_bomb();
374 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
375
376 minefield_admin_hide(1);
377
378 return j * PAGESIZE - region_start;
379}
380
381void *minefield_c_malloc(size_t size)
382{
383 if (!minefield_initialised)
384 minefield_init();
385 return minefield_alloc(size);
386}
387
388void minefield_c_free(void *p)
389{
390 if (!minefield_initialised)
391 minefield_init();
392 minefield_free(p);
393}
394
395/*
396 * realloc _always_ moves the chunk, for rapid detection of code
397 * that assumes it won't.
398 */
399void *minefield_c_realloc(void *p, size_t size)
400{
401 size_t oldsize;
402 void *q;
403 if (!minefield_initialised)
404 minefield_init();
405 q = minefield_alloc(size);
406 oldsize = minefield_get_size(p);
407 memcpy(q, p, (oldsize < size ? oldsize : size));
408 minefield_free(p);
409 return q;
410}
411
412#endif /* MINEFIELD */
ae62eaeb 413
414FontSpec *fontspec_new(const char *name,
415 int bold, int height, int charset)
416{
417 FontSpec *f = snew(FontSpec);
418 f->name = dupstr(name);
419 f->isbold = bold;
420 f->height = height;
421 f->charset = charset;
422 return f;
423}
424FontSpec *fontspec_copy(const FontSpec *f)
425{
426 return fontspec_new(f->name, f->isbold, f->height, f->charset);
427}
428void fontspec_free(FontSpec *f)
429{
430 sfree(f->name);
431 sfree(f);
432}
433int fontspec_serialise(FontSpec *f, void *vdata)
434{
435 char *data = (char *)vdata;
436 int len = strlen(f->name) + 1; /* include trailing NUL */
437 if (data) {
438 strcpy(data, f->name);
439 PUT_32BIT_MSB_FIRST(data + len, f->isbold);
440 PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
441 PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
442 }
443 return len + 12; /* also include three 4-byte ints */
444}
445FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
446{
447 char *data = (char *)vdata;
448 char *end;
449 if (maxsize < 13)
450 return NULL;
451 end = memchr(data, '\0', maxsize-12);
452 if (!end)
453 return NULL;
454 end++;
455 *used = end - data + 12;
456 return fontspec_new(data,
457 GET_32BIT_MSB_FIRST(end),
458 GET_32BIT_MSB_FIRST(end + 4),
459 GET_32BIT_MSB_FIRST(end + 8));
460}