Implement `pscp-select-backend'.
[u/mdw/putty] / misc.c
CommitLineData
374330e2 1#include <stdio.h>
2#include <stdlib.h>
1709795f 3#include <stdarg.h>
57356d63 4#include <ctype.h>
5471d09a 5#include <assert.h>
374330e2 6#include "putty.h"
7
03f64569 8/* ----------------------------------------------------------------------
9 * String handling routines.
10 */
11
57356d63 12char *dupstr(const char *s)
03f64569 13{
6d113886 14 char *p = NULL;
15 if (s) {
16 int len = strlen(s);
17 p = snewn(len + 1, char);
18 strcpy(p, s);
19 }
03f64569 20 return p;
21}
22
23/* Allocate the concatenation of N strings. Terminate arg list with NULL. */
57356d63 24char *dupcat(const char *s1, ...)
03f64569 25{
26 int len;
27 char *p, *q, *sn;
28 va_list ap;
29
30 len = strlen(s1);
31 va_start(ap, s1);
32 while (1) {
33 sn = va_arg(ap, char *);
34 if (!sn)
35 break;
36 len += strlen(sn);
37 }
38 va_end(ap);
39
3d88e64d 40 p = snewn(len + 1, char);
03f64569 41 strcpy(p, s1);
42 q = p + strlen(p);
43
44 va_start(ap, s1);
45 while (1) {
46 sn = va_arg(ap, char *);
47 if (!sn)
48 break;
49 strcpy(q, sn);
50 q += strlen(q);
51 }
52 va_end(ap);
53
54 return p;
55}
56
57356d63 57/*
58 * Do an sprintf(), but into a custom-allocated buffer.
59 *
60 * Irritatingly, we don't seem to be able to do this portably using
61 * vsnprintf(), because there appear to be issues with re-using the
62 * same va_list for two calls, and the excellent C99 va_copy is not
63 * yet widespread. Bah. Instead I'm going to do a horrid, horrid
64 * hack, in which I trawl the format string myself, work out the
65 * maximum length of each format component, and resize the buffer
66 * before printing it.
67 */
68char *dupprintf(const char *fmt, ...)
69{
70 char *ret;
71 va_list ap;
72 va_start(ap, fmt);
73 ret = dupvprintf(fmt, ap);
74 va_end(ap);
75 return ret;
76}
77char *dupvprintf(const char *fmt, va_list ap)
78{
79 char *buf;
80 int len, size;
81
3d88e64d 82 buf = snewn(512, char);
57356d63 83 size = 512;
84
85 while (1) {
86#ifdef _WINDOWS
87#define vsnprintf _vsnprintf
88#endif
89 len = vsnprintf(buf, size, fmt, ap);
90 if (len >= 0 && len < size) {
91 /* This is the C99-specified criterion for snprintf to have
92 * been completely successful. */
93 return buf;
94 } else if (len > 0) {
95 /* This is the C99 error condition: the returned length is
96 * the required buffer size not counting the NUL. */
97 size = len + 1;
98 } else {
99 /* This is the pre-C99 glibc error condition: <0 means the
100 * buffer wasn't big enough, so we enlarge it a bit and hope. */
101 size += 512;
102 }
3d88e64d 103 buf = sresize(buf, size, char);
57356d63 104 }
105}
106
03f64569 107/* ----------------------------------------------------------------------
1549e076 108 * Base64 encoding routine. This is required in public-key writing
109 * but also in HTTP proxy handling, so it's centralised here.
110 */
111
112void base64_encode_atom(unsigned char *data, int n, char *out)
113{
114 static const char base64_chars[] =
115 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
116
117 unsigned word;
118
119 word = data[0] << 16;
120 if (n > 1)
121 word |= data[1] << 8;
122 if (n > 2)
123 word |= data[2];
124 out[0] = base64_chars[(word >> 18) & 0x3F];
125 out[1] = base64_chars[(word >> 12) & 0x3F];
126 if (n > 1)
127 out[2] = base64_chars[(word >> 6) & 0x3F];
128 else
129 out[2] = '=';
130 if (n > 2)
131 out[3] = base64_chars[word & 0x3F];
132 else
133 out[3] = '=';
134}
135
136/* ----------------------------------------------------------------------
5471d09a 137 * Generic routines to deal with send buffers: a linked list of
138 * smallish blocks, with the operations
139 *
140 * - add an arbitrary amount of data to the end of the list
141 * - remove the first N bytes from the list
142 * - return a (pointer,length) pair giving some initial data in
143 * the list, suitable for passing to a send or write system
144 * call
7983f47e 145 * - retrieve a larger amount of initial data from the list
5471d09a 146 * - return the current size of the buffer chain in bytes
147 */
148
149#define BUFFER_GRANULE 512
150
151struct bufchain_granule {
152 struct bufchain_granule *next;
153 int buflen, bufpos;
154 char buf[BUFFER_GRANULE];
155};
156
157void bufchain_init(bufchain *ch)
158{
159 ch->head = ch->tail = NULL;
160 ch->buffersize = 0;
161}
162
163void bufchain_clear(bufchain *ch)
164{
165 struct bufchain_granule *b;
166 while (ch->head) {
167 b = ch->head;
168 ch->head = ch->head->next;
169 sfree(b);
170 }
171 ch->tail = NULL;
172 ch->buffersize = 0;
173}
174
175int bufchain_size(bufchain *ch)
176{
177 return ch->buffersize;
178}
179
e0e7dff8 180void bufchain_add(bufchain *ch, const void *data, int len)
5471d09a 181{
e0e7dff8 182 const char *buf = (const char *)data;
5471d09a 183
bfa5400d 184 if (len == 0) return;
185
5471d09a 186 ch->buffersize += len;
187
188 if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) {
189 int copylen = min(len, BUFFER_GRANULE - ch->tail->buflen);
190 memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen);
191 buf += copylen;
192 len -= copylen;
193 ch->tail->buflen += copylen;
194 }
195 while (len > 0) {
196 int grainlen = min(len, BUFFER_GRANULE);
197 struct bufchain_granule *newbuf;
3d88e64d 198 newbuf = snew(struct bufchain_granule);
5471d09a 199 newbuf->bufpos = 0;
200 newbuf->buflen = grainlen;
201 memcpy(newbuf->buf, buf, grainlen);
202 buf += grainlen;
203 len -= grainlen;
204 if (ch->tail)
205 ch->tail->next = newbuf;
206 else
207 ch->head = ch->tail = newbuf;
208 newbuf->next = NULL;
209 ch->tail = newbuf;
210 }
211}
212
213void bufchain_consume(bufchain *ch, int len)
214{
7983f47e 215 struct bufchain_granule *tmp;
216
5471d09a 217 assert(ch->buffersize >= len);
7983f47e 218 while (len > 0) {
219 int remlen = len;
220 assert(ch->head != NULL);
221 if (remlen >= ch->head->buflen - ch->head->bufpos) {
222 remlen = ch->head->buflen - ch->head->bufpos;
223 tmp = ch->head;
224 ch->head = tmp->next;
225 sfree(tmp);
226 if (!ch->head)
227 ch->tail = NULL;
228 } else
229 ch->head->bufpos += remlen;
230 ch->buffersize -= remlen;
231 len -= remlen;
5471d09a 232 }
233}
234
235void bufchain_prefix(bufchain *ch, void **data, int *len)
236{
237 *len = ch->head->buflen - ch->head->bufpos;
238 *data = ch->head->buf + ch->head->bufpos;
239}
240
7983f47e 241void bufchain_fetch(bufchain *ch, void *data, int len)
242{
243 struct bufchain_granule *tmp;
244 char *data_c = (char *)data;
245
246 tmp = ch->head;
247
248 assert(ch->buffersize >= len);
249 while (len > 0) {
250 int remlen = len;
251
252 assert(tmp != NULL);
253 if (remlen >= tmp->buflen - tmp->bufpos)
254 remlen = tmp->buflen - tmp->bufpos;
255 memcpy(data_c, tmp->buf + tmp->bufpos, remlen);
256
257 tmp = tmp->next;
258 len -= remlen;
259 data_c += remlen;
260 }
261}
262
03f64569 263/* ----------------------------------------------------------------------
b191636d 264 * My own versions of malloc, realloc and free. Because I want
265 * malloc and realloc to bomb out and exit the program if they run
266 * out of memory, realloc to reliably call malloc if passed a NULL
267 * pointer, and free to reliably do nothing if passed a NULL
268 * pointer. We can also put trace printouts in, if we need to; and
269 * we can also replace the allocator with an ElectricFence-like
270 * one.
271 */
272
273#ifdef MINEFIELD
d0912d1f 274void *minefield_c_malloc(size_t size);
275void minefield_c_free(void *p);
276void *minefield_c_realloc(void *p, size_t size);
277#endif
374330e2 278
279#ifdef MALLOC_LOG
280static FILE *fp = NULL;
281
d7da76ca 282static char *mlog_file = NULL;
283static int mlog_line = 0;
284
32874aea 285void mlog(char *file, int line)
286{
d7da76ca 287 mlog_file = file;
288 mlog_line = line;
c662dbc0 289 if (!fp) {
374330e2 290 fp = fopen("putty_mem.log", "w");
c662dbc0 291 setvbuf(fp, NULL, _IONBF, BUFSIZ);
292 }
374330e2 293 if (fp)
32874aea 294 fprintf(fp, "%s:%d: ", file, line);
374330e2 295}
296#endif
297
32874aea 298void *safemalloc(size_t size)
299{
b191636d 300 void *p;
301#ifdef MINEFIELD
32874aea 302 p = minefield_c_malloc(size);
b191636d 303#else
32874aea 304 p = malloc(size);
b191636d 305#endif
374330e2 306 if (!p) {
d7da76ca 307 char str[200];
308#ifdef MALLOC_LOG
309 sprintf(str, "Out of memory! (%s:%d, size=%d)",
310 mlog_file, mlog_line, size);
1b2ef365 311 fprintf(fp, "*** %s\n", str);
312 fclose(fp);
d7da76ca 313#else
314 strcpy(str, "Out of memory!");
315#endif
1709795f 316 modalfatalbox(str);
374330e2 317 }
318#ifdef MALLOC_LOG
319 if (fp)
320 fprintf(fp, "malloc(%d) returns %p\n", size, p);
321#endif
322 return p;
323}
324
32874aea 325void *saferealloc(void *ptr, size_t size)
326{
374330e2 327 void *p;
b191636d 328 if (!ptr) {
329#ifdef MINEFIELD
32874aea 330 p = minefield_c_malloc(size);
b191636d 331#else
32874aea 332 p = malloc(size);
b191636d 333#endif
334 } else {
335#ifdef MINEFIELD
32874aea 336 p = minefield_c_realloc(ptr, size);
b191636d 337#else
32874aea 338 p = realloc(ptr, size);
b191636d 339#endif
340 }
374330e2 341 if (!p) {
d7da76ca 342 char str[200];
343#ifdef MALLOC_LOG
344 sprintf(str, "Out of memory! (%s:%d, size=%d)",
345 mlog_file, mlog_line, size);
1b2ef365 346 fprintf(fp, "*** %s\n", str);
347 fclose(fp);
d7da76ca 348#else
349 strcpy(str, "Out of memory!");
350#endif
1709795f 351 modalfatalbox(str);
374330e2 352 }
353#ifdef MALLOC_LOG
354 if (fp)
355 fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
356#endif
357 return p;
358}
359
32874aea 360void safefree(void *ptr)
361{
374330e2 362 if (ptr) {
363#ifdef MALLOC_LOG
364 if (fp)
365 fprintf(fp, "free(%p)\n", ptr);
366#endif
b191636d 367#ifdef MINEFIELD
32874aea 368 minefield_c_free(ptr);
b191636d 369#else
32874aea 370 free(ptr);
b191636d 371#endif
374330e2 372 }
373#ifdef MALLOC_LOG
374 else if (fp)
375 fprintf(fp, "freeing null pointer - no action taken\n");
376#endif
377}
c82bda52 378
03f64569 379/* ----------------------------------------------------------------------
380 * Debugging routines.
381 */
382
c82bda52 383#ifdef DEBUG
d0912d1f 384extern void dputs(char *); /* defined in per-platform *misc.c */
db9c0f86 385
d0912d1f 386void debug_printf(char *fmt, ...)
32874aea 387{
57356d63 388 char *buf;
db9c0f86 389 va_list ap;
390
391 va_start(ap, fmt);
57356d63 392 buf = dupvprintf(fmt, ap);
32874aea 393 dputs(buf);
57356d63 394 sfree(buf);
c82bda52 395 va_end(ap);
396}
db9c0f86 397
398
32874aea 399void debug_memdump(void *buf, int len, int L)
400{
db9c0f86 401 int i;
402 unsigned char *p = buf;
765c4200 403 char foo[17];
db9c0f86 404 if (L) {
405 int delta;
d0912d1f 406 debug_printf("\t%d (0x%x) bytes:\n", len, len);
db9c0f86 407 delta = 15 & (int) p;
408 p -= delta;
409 len += delta;
410 }
411 for (; 0 < len; p += 16, len -= 16) {
32874aea 412 dputs(" ");
413 if (L)
d0912d1f 414 debug_printf("%p: ", p);
32874aea 415 strcpy(foo, "................"); /* sixteen dots */
db9c0f86 416 for (i = 0; i < 16 && i < len; ++i) {
417 if (&p[i] < (unsigned char *) buf) {
32874aea 418 dputs(" "); /* 3 spaces */
765c4200 419 foo[i] = ' ';
db9c0f86 420 } else {
d0912d1f 421 debug_printf("%c%02.2x",
32874aea 422 &p[i] != (unsigned char *) buf
423 && i % 4 ? '.' : ' ', p[i]
424 );
765c4200 425 if (p[i] >= ' ' && p[i] <= '~')
32874aea 426 foo[i] = (char) p[i];
db9c0f86 427 }
428 }
765c4200 429 foo[i] = '\0';
d0912d1f 430 debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
db9c0f86 431 }
432}
433
32874aea 434#endif /* def DEBUG */