Add random commentary to SOCKS code.
[u/mdw/putty] / misc.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdarg.h>
4#include <ctype.h>
5#include <assert.h>
6#include "putty.h"
7
8/* ----------------------------------------------------------------------
9 * String handling routines.
10 */
11
12char *dupstr(const char *s)
13{
14 char *p = NULL;
15 if (s) {
16 int len = strlen(s);
17 p = snewn(len + 1, char);
18 strcpy(p, s);
19 }
20 return p;
21}
22
23/* Allocate the concatenation of N strings. Terminate arg list with NULL. */
24char *dupcat(const char *s1, ...)
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
40 p = snewn(len + 1, char);
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
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
82 buf = snewn(512, char);
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 }
103 buf = sresize(buf, size, char);
104 }
105}
106
107/* ----------------------------------------------------------------------
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/* ----------------------------------------------------------------------
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
145 * - retrieve a larger amount of initial data from the list
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
180void bufchain_add(bufchain *ch, const void *data, int len)
181{
182 const char *buf = (const char *)data;
183
184 ch->buffersize += len;
185
186 if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) {
187 int copylen = min(len, BUFFER_GRANULE - ch->tail->buflen);
188 memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen);
189 buf += copylen;
190 len -= copylen;
191 ch->tail->buflen += copylen;
192 }
193 while (len > 0) {
194 int grainlen = min(len, BUFFER_GRANULE);
195 struct bufchain_granule *newbuf;
196 newbuf = snew(struct bufchain_granule);
197 newbuf->bufpos = 0;
198 newbuf->buflen = grainlen;
199 memcpy(newbuf->buf, buf, grainlen);
200 buf += grainlen;
201 len -= grainlen;
202 if (ch->tail)
203 ch->tail->next = newbuf;
204 else
205 ch->head = ch->tail = newbuf;
206 newbuf->next = NULL;
207 ch->tail = newbuf;
208 }
209}
210
211void bufchain_consume(bufchain *ch, int len)
212{
213 struct bufchain_granule *tmp;
214
215 assert(ch->buffersize >= len);
216 while (len > 0) {
217 int remlen = len;
218 assert(ch->head != NULL);
219 if (remlen >= ch->head->buflen - ch->head->bufpos) {
220 remlen = ch->head->buflen - ch->head->bufpos;
221 tmp = ch->head;
222 ch->head = tmp->next;
223 sfree(tmp);
224 if (!ch->head)
225 ch->tail = NULL;
226 } else
227 ch->head->bufpos += remlen;
228 ch->buffersize -= remlen;
229 len -= remlen;
230 }
231}
232
233void bufchain_prefix(bufchain *ch, void **data, int *len)
234{
235 *len = ch->head->buflen - ch->head->bufpos;
236 *data = ch->head->buf + ch->head->bufpos;
237}
238
239void bufchain_fetch(bufchain *ch, void *data, int len)
240{
241 struct bufchain_granule *tmp;
242 char *data_c = (char *)data;
243
244 tmp = ch->head;
245
246 assert(ch->buffersize >= len);
247 while (len > 0) {
248 int remlen = len;
249
250 assert(tmp != NULL);
251 if (remlen >= tmp->buflen - tmp->bufpos)
252 remlen = tmp->buflen - tmp->bufpos;
253 memcpy(data_c, tmp->buf + tmp->bufpos, remlen);
254
255 tmp = tmp->next;
256 len -= remlen;
257 data_c += remlen;
258 }
259}
260
261/* ----------------------------------------------------------------------
262 * My own versions of malloc, realloc and free. Because I want
263 * malloc and realloc to bomb out and exit the program if they run
264 * out of memory, realloc to reliably call malloc if passed a NULL
265 * pointer, and free to reliably do nothing if passed a NULL
266 * pointer. We can also put trace printouts in, if we need to; and
267 * we can also replace the allocator with an ElectricFence-like
268 * one.
269 */
270
271#ifdef MINEFIELD
272void *minefield_c_malloc(size_t size);
273void minefield_c_free(void *p);
274void *minefield_c_realloc(void *p, size_t size);
275#endif
276
277#ifdef MALLOC_LOG
278static FILE *fp = NULL;
279
280static char *mlog_file = NULL;
281static int mlog_line = 0;
282
283void mlog(char *file, int line)
284{
285 mlog_file = file;
286 mlog_line = line;
287 if (!fp) {
288 fp = fopen("putty_mem.log", "w");
289 setvbuf(fp, NULL, _IONBF, BUFSIZ);
290 }
291 if (fp)
292 fprintf(fp, "%s:%d: ", file, line);
293}
294#endif
295
296void *safemalloc(size_t size)
297{
298 void *p;
299#ifdef MINEFIELD
300 p = minefield_c_malloc(size);
301#else
302 p = malloc(size);
303#endif
304 if (!p) {
305 char str[200];
306#ifdef MALLOC_LOG
307 sprintf(str, "Out of memory! (%s:%d, size=%d)",
308 mlog_file, mlog_line, size);
309 fprintf(fp, "*** %s\n", str);
310 fclose(fp);
311#else
312 strcpy(str, "Out of memory!");
313#endif
314 modalfatalbox(str);
315 }
316#ifdef MALLOC_LOG
317 if (fp)
318 fprintf(fp, "malloc(%d) returns %p\n", size, p);
319#endif
320 return p;
321}
322
323void *saferealloc(void *ptr, size_t size)
324{
325 void *p;
326 if (!ptr) {
327#ifdef MINEFIELD
328 p = minefield_c_malloc(size);
329#else
330 p = malloc(size);
331#endif
332 } else {
333#ifdef MINEFIELD
334 p = minefield_c_realloc(ptr, size);
335#else
336 p = realloc(ptr, size);
337#endif
338 }
339 if (!p) {
340 char str[200];
341#ifdef MALLOC_LOG
342 sprintf(str, "Out of memory! (%s:%d, size=%d)",
343 mlog_file, mlog_line, size);
344 fprintf(fp, "*** %s\n", str);
345 fclose(fp);
346#else
347 strcpy(str, "Out of memory!");
348#endif
349 modalfatalbox(str);
350 }
351#ifdef MALLOC_LOG
352 if (fp)
353 fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
354#endif
355 return p;
356}
357
358void safefree(void *ptr)
359{
360 if (ptr) {
361#ifdef MALLOC_LOG
362 if (fp)
363 fprintf(fp, "free(%p)\n", ptr);
364#endif
365#ifdef MINEFIELD
366 minefield_c_free(ptr);
367#else
368 free(ptr);
369#endif
370 }
371#ifdef MALLOC_LOG
372 else if (fp)
373 fprintf(fp, "freeing null pointer - no action taken\n");
374#endif
375}
376
377/* ----------------------------------------------------------------------
378 * Debugging routines.
379 */
380
381#ifdef DEBUG
382extern void dputs(char *); /* defined in per-platform *misc.c */
383
384void debug_printf(char *fmt, ...)
385{
386 char *buf;
387 va_list ap;
388
389 va_start(ap, fmt);
390 buf = dupvprintf(fmt, ap);
391 dputs(buf);
392 sfree(buf);
393 va_end(ap);
394}
395
396
397void debug_memdump(void *buf, int len, int L)
398{
399 int i;
400 unsigned char *p = buf;
401 char foo[17];
402 if (L) {
403 int delta;
404 debug_printf("\t%d (0x%x) bytes:\n", len, len);
405 delta = 15 & (int) p;
406 p -= delta;
407 len += delta;
408 }
409 for (; 0 < len; p += 16, len -= 16) {
410 dputs(" ");
411 if (L)
412 debug_printf("%p: ", p);
413 strcpy(foo, "................"); /* sixteen dots */
414 for (i = 0; i < 16 && i < len; ++i) {
415 if (&p[i] < (unsigned char *) buf) {
416 dputs(" "); /* 3 spaces */
417 foo[i] = ' ';
418 } else {
419 debug_printf("%c%02.2x",
420 &p[i] != (unsigned char *) buf
421 && i % 4 ? '.' : ' ', p[i]
422 );
423 if (p[i] >= ' ' && p[i] <= '~')
424 foo[i] = (char) p[i];
425 }
426 }
427 foo[i] = '\0';
428 debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
429 }
430}
431
432#endif /* def DEBUG */