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