7 /* ----------------------------------------------------------------------
8 * String handling routines.
14 char *p = smalloc(len + 1);
19 /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
20 char *dupcat(char *s1, ...)
29 sn = va_arg(ap, char *);
42 sn = va_arg(ap, char *);
53 /* ----------------------------------------------------------------------
54 * Base64 encoding routine. This is required in public-key writing
55 * but also in HTTP proxy handling, so it's centralised here.
58 void base64_encode_atom(unsigned char *data, int n, char *out)
60 static const char base64_chars[] =
61 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
70 out[0] = base64_chars[(word >> 18) & 0x3F];
71 out[1] = base64_chars[(word >> 12) & 0x3F];
73 out[2] = base64_chars[(word >> 6) & 0x3F];
77 out[3] = base64_chars[word & 0x3F];
82 /* ----------------------------------------------------------------------
83 * Generic routines to deal with send buffers: a linked list of
84 * smallish blocks, with the operations
86 * - add an arbitrary amount of data to the end of the list
87 * - remove the first N bytes from the list
88 * - return a (pointer,length) pair giving some initial data in
89 * the list, suitable for passing to a send or write system
91 * - retrieve a larger amount of initial data from the list
92 * - return the current size of the buffer chain in bytes
95 #define BUFFER_GRANULE 512
97 struct bufchain_granule {
98 struct bufchain_granule *next;
100 char buf[BUFFER_GRANULE];
103 void bufchain_init(bufchain *ch)
105 ch->head = ch->tail = NULL;
109 void bufchain_clear(bufchain *ch)
111 struct bufchain_granule *b;
114 ch->head = ch->head->next;
121 int bufchain_size(bufchain *ch)
123 return ch->buffersize;
126 void bufchain_add(bufchain *ch, void *data, int len)
128 char *buf = (char *)data;
130 ch->buffersize += len;
132 if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) {
133 int copylen = min(len, BUFFER_GRANULE - ch->tail->buflen);
134 memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen);
137 ch->tail->buflen += copylen;
140 int grainlen = min(len, BUFFER_GRANULE);
141 struct bufchain_granule *newbuf;
142 newbuf = smalloc(sizeof(struct bufchain_granule));
144 newbuf->buflen = grainlen;
145 memcpy(newbuf->buf, buf, grainlen);
149 ch->tail->next = newbuf;
151 ch->head = ch->tail = newbuf;
157 void bufchain_consume(bufchain *ch, int len)
159 struct bufchain_granule *tmp;
161 assert(ch->buffersize >= len);
164 assert(ch->head != NULL);
165 if (remlen >= ch->head->buflen - ch->head->bufpos) {
166 remlen = ch->head->buflen - ch->head->bufpos;
168 ch->head = tmp->next;
173 ch->head->bufpos += remlen;
174 ch->buffersize -= remlen;
179 void bufchain_prefix(bufchain *ch, void **data, int *len)
181 *len = ch->head->buflen - ch->head->bufpos;
182 *data = ch->head->buf + ch->head->bufpos;
185 void bufchain_fetch(bufchain *ch, void *data, int len)
187 struct bufchain_granule *tmp;
188 char *data_c = (char *)data;
192 assert(ch->buffersize >= len);
197 if (remlen >= tmp->buflen - tmp->bufpos)
198 remlen = tmp->buflen - tmp->bufpos;
199 memcpy(data_c, tmp->buf + tmp->bufpos, remlen);
207 /* ----------------------------------------------------------------------
208 * My own versions of malloc, realloc and free. Because I want
209 * malloc and realloc to bomb out and exit the program if they run
210 * out of memory, realloc to reliably call malloc if passed a NULL
211 * pointer, and free to reliably do nothing if passed a NULL
212 * pointer. We can also put trace printouts in, if we need to; and
213 * we can also replace the allocator with an ElectricFence-like
219 * Minefield - a Windows equivalent for Electric Fence
222 #define PAGESIZE 4096
227 * We start by reserving as much virtual address space as Windows
228 * will sensibly (or not sensibly) let us have. We flag it all as
231 * Any allocation attempt is satisfied by committing one or more
232 * pages, with an uncommitted page on either side. The returned
233 * memory region is jammed up against the _end_ of the pages.
235 * Freeing anything causes instantaneous decommitment of the pages
236 * involved, so stale pointers are caught as soon as possible.
239 static int minefield_initialised = 0;
240 static void *minefield_region = NULL;
241 static long minefield_size = 0;
242 static long minefield_npages = 0;
243 static long minefield_curpos = 0;
244 static unsigned short *minefield_admin = NULL;
245 static void *minefield_pages = NULL;
247 static void minefield_admin_hide(int hide)
249 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
250 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
253 static void minefield_init(void)
259 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
260 minefield_region = VirtualAlloc(NULL, size,
261 MEM_RESERVE, PAGE_NOACCESS);
262 if (minefield_region)
265 minefield_size = size;
268 * Firstly, allocate a section of that to be the admin block.
269 * We'll need a two-byte field for each page.
271 minefield_admin = minefield_region;
272 minefield_npages = minefield_size / PAGESIZE;
273 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
274 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
275 minefield_pages = (char *) minefield_region + admin_size;
278 * Commit the admin region.
280 VirtualAlloc(minefield_admin, minefield_npages * 2,
281 MEM_COMMIT, PAGE_READWRITE);
284 * Mark all pages as unused (0xFFFF).
286 for (i = 0; i < minefield_npages; i++)
287 minefield_admin[i] = 0xFFFF;
290 * Hide the admin region.
292 minefield_admin_hide(1);
294 minefield_initialised = 1;
297 static void minefield_bomb(void)
299 div(1, *(int *) minefield_pages);
302 static void *minefield_alloc(int size)
305 int pos, lim, region_end, region_start;
309 npages = (size + PAGESIZE - 1) / PAGESIZE;
311 minefield_admin_hide(0);
314 * Search from current position until we find a contiguous
315 * bunch of npages+2 unused pages.
317 pos = minefield_curpos;
318 lim = minefield_npages;
320 /* Skip over used pages. */
321 while (pos < lim && minefield_admin[pos] != 0xFFFF)
323 /* Count unused pages. */
325 while (pos < lim && pos - start < npages + 2 &&
326 minefield_admin[pos] == 0xFFFF)
328 if (pos - start == npages + 2)
330 /* If we've reached the limit, reset the limit or stop. */
332 if (lim == minefield_npages) {
333 /* go round and start again at zero */
334 lim = minefield_curpos;
337 minefield_admin_hide(1);
343 minefield_curpos = pos - 1;
346 * We have npages+2 unused pages starting at start. We leave
347 * the first and last of these alone and use the rest.
349 region_end = (start + npages + 1) * PAGESIZE;
350 region_start = region_end - size;
351 /* FIXME: could align here if we wanted */
354 * Update the admin region.
356 for (i = start + 2; i < start + npages + 1; i++)
357 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
358 minefield_admin[start + 1] = region_start % PAGESIZE;
360 minefield_admin_hide(1);
362 VirtualAlloc((char *) minefield_pages + region_start, size,
363 MEM_COMMIT, PAGE_READWRITE);
364 return (char *) minefield_pages + region_start;
367 static void minefield_free(void *ptr)
369 int region_start, i, j;
371 minefield_admin_hide(0);
373 region_start = (char *) ptr - (char *) minefield_pages;
374 i = region_start / PAGESIZE;
375 if (i < 0 || i >= minefield_npages ||
376 minefield_admin[i] != region_start % PAGESIZE)
378 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
379 minefield_admin[j] = 0xFFFF;
382 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
384 minefield_admin_hide(1);
387 static int minefield_get_size(void *ptr)
389 int region_start, i, j;
391 minefield_admin_hide(0);
393 region_start = (char *) ptr - (char *) minefield_pages;
394 i = region_start / PAGESIZE;
395 if (i < 0 || i >= minefield_npages ||
396 minefield_admin[i] != region_start % PAGESIZE)
398 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
400 minefield_admin_hide(1);
402 return j * PAGESIZE - region_start;
405 static void *minefield_c_malloc(size_t size)
407 if (!minefield_initialised)
409 return minefield_alloc(size);
412 static void minefield_c_free(void *p)
414 if (!minefield_initialised)
420 * realloc _always_ moves the chunk, for rapid detection of code
421 * that assumes it won't.
423 static void *minefield_c_realloc(void *p, size_t size)
427 if (!minefield_initialised)
429 q = minefield_alloc(size);
430 oldsize = minefield_get_size(p);
431 memcpy(q, p, (oldsize < size ? oldsize : size));
436 #endif /* MINEFIELD */
439 static FILE *fp = NULL;
441 static char *mlog_file = NULL;
442 static int mlog_line = 0;
444 void mlog(char *file, int line)
449 fp = fopen("putty_mem.log", "w");
450 setvbuf(fp, NULL, _IONBF, BUFSIZ);
453 fprintf(fp, "%s:%d: ", file, line);
457 void *safemalloc(size_t size)
461 p = minefield_c_malloc(size);
468 sprintf(str, "Out of memory! (%s:%d, size=%d)",
469 mlog_file, mlog_line, size);
470 fprintf(fp, "*** %s\n", str);
473 strcpy(str, "Out of memory!");
475 MessageBox(NULL, str, "PuTTY Fatal Error",
476 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
481 fprintf(fp, "malloc(%d) returns %p\n", size, p);
486 void *saferealloc(void *ptr, size_t size)
491 p = minefield_c_malloc(size);
497 p = minefield_c_realloc(ptr, size);
499 p = realloc(ptr, size);
505 sprintf(str, "Out of memory! (%s:%d, size=%d)",
506 mlog_file, mlog_line, size);
507 fprintf(fp, "*** %s\n", str);
510 strcpy(str, "Out of memory!");
512 MessageBox(NULL, str, "PuTTY Fatal Error",
513 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
518 fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
523 void safefree(void *ptr)
528 fprintf(fp, "free(%p)\n", ptr);
531 minefield_c_free(ptr);
538 fprintf(fp, "freeing null pointer - no action taken\n");
542 /* ----------------------------------------------------------------------
543 * Debugging routines.
547 static FILE *debug_fp = NULL;
548 static int debug_got_console = 0;
550 static void dputs(char *buf)
554 if (!debug_got_console) {
556 debug_got_console = 1;
559 debug_fp = fopen("debug.log", "w");
562 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw,
564 fputs(buf, debug_fp);
569 void dprintf(char *fmt, ...)
575 vsprintf(buf, fmt, ap);
581 void debug_memdump(void *buf, int len, int L)
584 unsigned char *p = buf;
588 dprintf("\t%d (0x%x) bytes:\n", len, len);
589 delta = 15 & (int) p;
593 for (; 0 < len; p += 16, len -= 16) {
597 strcpy(foo, "................"); /* sixteen dots */
598 for (i = 0; i < 16 && i < len; ++i) {
599 if (&p[i] < (unsigned char *) buf) {
600 dputs(" "); /* 3 spaces */
604 &p[i] != (unsigned char *) buf
605 && i % 4 ? '.' : ' ', p[i]
607 if (p[i] >= ' ' && p[i] <= '~')
608 foo[i] = (char) p[i];
612 dprintf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
616 #endif /* def DEBUG */