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 |
12 | char *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 |
24 | char *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 | */ |
68 | char *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 | } |
77 | char *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 | |
112 | void 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 | |
151 | struct bufchain_granule { |
152 | struct bufchain_granule *next; |
153 | int buflen, bufpos; |
154 | char buf[BUFFER_GRANULE]; |
155 | }; |
156 | |
157 | void bufchain_init(bufchain *ch) |
158 | { |
159 | ch->head = ch->tail = NULL; |
160 | ch->buffersize = 0; |
161 | } |
162 | |
163 | void 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 | |
175 | int bufchain_size(bufchain *ch) |
176 | { |
177 | return ch->buffersize; |
178 | } |
179 | |
e0e7dff8 |
180 | void bufchain_add(bufchain *ch, const void *data, int len) |
5471d09a |
181 | { |
e0e7dff8 |
182 | const char *buf = (const char *)data; |
5471d09a |
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; |
3d88e64d |
196 | newbuf = snew(struct bufchain_granule); |
5471d09a |
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 | |
211 | void bufchain_consume(bufchain *ch, int len) |
212 | { |
7983f47e |
213 | struct bufchain_granule *tmp; |
214 | |
5471d09a |
215 | assert(ch->buffersize >= len); |
7983f47e |
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; |
5471d09a |
230 | } |
231 | } |
232 | |
233 | void 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 | |
7983f47e |
239 | void 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 | |
03f64569 |
261 | /* ---------------------------------------------------------------------- |
b191636d |
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 |
d0912d1f |
272 | void *minefield_c_malloc(size_t size); |
273 | void minefield_c_free(void *p); |
274 | void *minefield_c_realloc(void *p, size_t size); |
275 | #endif |
374330e2 |
276 | |
277 | #ifdef MALLOC_LOG |
278 | static FILE *fp = NULL; |
279 | |
d7da76ca |
280 | static char *mlog_file = NULL; |
281 | static int mlog_line = 0; |
282 | |
32874aea |
283 | void mlog(char *file, int line) |
284 | { |
d7da76ca |
285 | mlog_file = file; |
286 | mlog_line = line; |
c662dbc0 |
287 | if (!fp) { |
374330e2 |
288 | fp = fopen("putty_mem.log", "w"); |
c662dbc0 |
289 | setvbuf(fp, NULL, _IONBF, BUFSIZ); |
290 | } |
374330e2 |
291 | if (fp) |
32874aea |
292 | fprintf(fp, "%s:%d: ", file, line); |
374330e2 |
293 | } |
294 | #endif |
295 | |
32874aea |
296 | void *safemalloc(size_t size) |
297 | { |
b191636d |
298 | void *p; |
299 | #ifdef MINEFIELD |
32874aea |
300 | p = minefield_c_malloc(size); |
b191636d |
301 | #else |
32874aea |
302 | p = malloc(size); |
b191636d |
303 | #endif |
374330e2 |
304 | if (!p) { |
d7da76ca |
305 | char str[200]; |
306 | #ifdef MALLOC_LOG |
307 | sprintf(str, "Out of memory! (%s:%d, size=%d)", |
308 | mlog_file, mlog_line, size); |
1b2ef365 |
309 | fprintf(fp, "*** %s\n", str); |
310 | fclose(fp); |
d7da76ca |
311 | #else |
312 | strcpy(str, "Out of memory!"); |
313 | #endif |
1709795f |
314 | modalfatalbox(str); |
374330e2 |
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 | |
32874aea |
323 | void *saferealloc(void *ptr, size_t size) |
324 | { |
374330e2 |
325 | void *p; |
b191636d |
326 | if (!ptr) { |
327 | #ifdef MINEFIELD |
32874aea |
328 | p = minefield_c_malloc(size); |
b191636d |
329 | #else |
32874aea |
330 | p = malloc(size); |
b191636d |
331 | #endif |
332 | } else { |
333 | #ifdef MINEFIELD |
32874aea |
334 | p = minefield_c_realloc(ptr, size); |
b191636d |
335 | #else |
32874aea |
336 | p = realloc(ptr, size); |
b191636d |
337 | #endif |
338 | } |
374330e2 |
339 | if (!p) { |
d7da76ca |
340 | char str[200]; |
341 | #ifdef MALLOC_LOG |
342 | sprintf(str, "Out of memory! (%s:%d, size=%d)", |
343 | mlog_file, mlog_line, size); |
1b2ef365 |
344 | fprintf(fp, "*** %s\n", str); |
345 | fclose(fp); |
d7da76ca |
346 | #else |
347 | strcpy(str, "Out of memory!"); |
348 | #endif |
1709795f |
349 | modalfatalbox(str); |
374330e2 |
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 | |
32874aea |
358 | void safefree(void *ptr) |
359 | { |
374330e2 |
360 | if (ptr) { |
361 | #ifdef MALLOC_LOG |
362 | if (fp) |
363 | fprintf(fp, "free(%p)\n", ptr); |
364 | #endif |
b191636d |
365 | #ifdef MINEFIELD |
32874aea |
366 | minefield_c_free(ptr); |
b191636d |
367 | #else |
32874aea |
368 | free(ptr); |
b191636d |
369 | #endif |
374330e2 |
370 | } |
371 | #ifdef MALLOC_LOG |
372 | else if (fp) |
373 | fprintf(fp, "freeing null pointer - no action taken\n"); |
374 | #endif |
375 | } |
c82bda52 |
376 | |
03f64569 |
377 | /* ---------------------------------------------------------------------- |
378 | * Debugging routines. |
379 | */ |
380 | |
c82bda52 |
381 | #ifdef DEBUG |
d0912d1f |
382 | extern void dputs(char *); /* defined in per-platform *misc.c */ |
db9c0f86 |
383 | |
d0912d1f |
384 | void debug_printf(char *fmt, ...) |
32874aea |
385 | { |
57356d63 |
386 | char *buf; |
db9c0f86 |
387 | va_list ap; |
388 | |
389 | va_start(ap, fmt); |
57356d63 |
390 | buf = dupvprintf(fmt, ap); |
32874aea |
391 | dputs(buf); |
57356d63 |
392 | sfree(buf); |
c82bda52 |
393 | va_end(ap); |
394 | } |
db9c0f86 |
395 | |
396 | |
32874aea |
397 | void debug_memdump(void *buf, int len, int L) |
398 | { |
db9c0f86 |
399 | int i; |
400 | unsigned char *p = buf; |
765c4200 |
401 | char foo[17]; |
db9c0f86 |
402 | if (L) { |
403 | int delta; |
d0912d1f |
404 | debug_printf("\t%d (0x%x) bytes:\n", len, len); |
db9c0f86 |
405 | delta = 15 & (int) p; |
406 | p -= delta; |
407 | len += delta; |
408 | } |
409 | for (; 0 < len; p += 16, len -= 16) { |
32874aea |
410 | dputs(" "); |
411 | if (L) |
d0912d1f |
412 | debug_printf("%p: ", p); |
32874aea |
413 | strcpy(foo, "................"); /* sixteen dots */ |
db9c0f86 |
414 | for (i = 0; i < 16 && i < len; ++i) { |
415 | if (&p[i] < (unsigned char *) buf) { |
32874aea |
416 | dputs(" "); /* 3 spaces */ |
765c4200 |
417 | foo[i] = ' '; |
db9c0f86 |
418 | } else { |
d0912d1f |
419 | debug_printf("%c%02.2x", |
32874aea |
420 | &p[i] != (unsigned char *) buf |
421 | && i % 4 ? '.' : ' ', p[i] |
422 | ); |
765c4200 |
423 | if (p[i] >= ' ' && p[i] <= '~') |
32874aea |
424 | foo[i] = (char) p[i]; |
db9c0f86 |
425 | } |
426 | } |
765c4200 |
427 | foo[i] = '\0'; |
d0912d1f |
428 | debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo); |
db9c0f86 |
429 | } |
430 | } |
431 | |
32874aea |
432 | #endif /* def DEBUG */ |