Remove all the "assert(len>0)" which forbade zero-length writes across the
[u/mdw/putty] / misc.c
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
12 char *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. */
24 char *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 */
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
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
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 /* ----------------------------------------------------------------------
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
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
180 void bufchain_add(bufchain *ch, const void *data, int len)
181 {
182 const char *buf = (const char *)data;
183
184 if (len == 0) return;
185
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;
198 newbuf = snew(struct bufchain_granule);
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
213 void bufchain_consume(bufchain *ch, int len)
214 {
215 struct bufchain_granule *tmp;
216
217 assert(ch->buffersize >= len);
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;
232 }
233 }
234
235 void 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
241 void 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
263 /* ----------------------------------------------------------------------
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
274 void *minefield_c_malloc(size_t size);
275 void minefield_c_free(void *p);
276 void *minefield_c_realloc(void *p, size_t size);
277 #endif
278
279 #ifdef MALLOC_LOG
280 static FILE *fp = NULL;
281
282 static char *mlog_file = NULL;
283 static int mlog_line = 0;
284
285 void mlog(char *file, int line)
286 {
287 mlog_file = file;
288 mlog_line = line;
289 if (!fp) {
290 fp = fopen("putty_mem.log", "w");
291 setvbuf(fp, NULL, _IONBF, BUFSIZ);
292 }
293 if (fp)
294 fprintf(fp, "%s:%d: ", file, line);
295 }
296 #endif
297
298 void *safemalloc(size_t size)
299 {
300 void *p;
301 #ifdef MINEFIELD
302 p = minefield_c_malloc(size);
303 #else
304 p = malloc(size);
305 #endif
306 if (!p) {
307 char str[200];
308 #ifdef MALLOC_LOG
309 sprintf(str, "Out of memory! (%s:%d, size=%d)",
310 mlog_file, mlog_line, size);
311 fprintf(fp, "*** %s\n", str);
312 fclose(fp);
313 #else
314 strcpy(str, "Out of memory!");
315 #endif
316 modalfatalbox(str);
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
325 void *saferealloc(void *ptr, size_t size)
326 {
327 void *p;
328 if (!ptr) {
329 #ifdef MINEFIELD
330 p = minefield_c_malloc(size);
331 #else
332 p = malloc(size);
333 #endif
334 } else {
335 #ifdef MINEFIELD
336 p = minefield_c_realloc(ptr, size);
337 #else
338 p = realloc(ptr, size);
339 #endif
340 }
341 if (!p) {
342 char str[200];
343 #ifdef MALLOC_LOG
344 sprintf(str, "Out of memory! (%s:%d, size=%d)",
345 mlog_file, mlog_line, size);
346 fprintf(fp, "*** %s\n", str);
347 fclose(fp);
348 #else
349 strcpy(str, "Out of memory!");
350 #endif
351 modalfatalbox(str);
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
360 void safefree(void *ptr)
361 {
362 if (ptr) {
363 #ifdef MALLOC_LOG
364 if (fp)
365 fprintf(fp, "free(%p)\n", ptr);
366 #endif
367 #ifdef MINEFIELD
368 minefield_c_free(ptr);
369 #else
370 free(ptr);
371 #endif
372 }
373 #ifdef MALLOC_LOG
374 else if (fp)
375 fprintf(fp, "freeing null pointer - no action taken\n");
376 #endif
377 }
378
379 /* ----------------------------------------------------------------------
380 * Debugging routines.
381 */
382
383 #ifdef DEBUG
384 extern void dputs(char *); /* defined in per-platform *misc.c */
385
386 void debug_printf(char *fmt, ...)
387 {
388 char *buf;
389 va_list ap;
390
391 va_start(ap, fmt);
392 buf = dupvprintf(fmt, ap);
393 dputs(buf);
394 sfree(buf);
395 va_end(ap);
396 }
397
398
399 void debug_memdump(void *buf, int len, int L)
400 {
401 int i;
402 unsigned char *p = buf;
403 char foo[17];
404 if (L) {
405 int delta;
406 debug_printf("\t%d (0x%x) bytes:\n", len, len);
407 delta = 15 & (int) p;
408 p -= delta;
409 len += delta;
410 }
411 for (; 0 < len; p += 16, len -= 16) {
412 dputs(" ");
413 if (L)
414 debug_printf("%p: ", p);
415 strcpy(foo, "................"); /* sixteen dots */
416 for (i = 0; i < 16 && i < len; ++i) {
417 if (&p[i] < (unsigned char *) buf) {
418 dputs(" "); /* 3 spaces */
419 foo[i] = ' ';
420 } else {
421 debug_printf("%c%02.2x",
422 &p[i] != (unsigned char *) buf
423 && i % 4 ? '.' : ' ', p[i]
424 );
425 if (p[i] >= ' ' && p[i] <= '~')
426 foo[i] = (char) p[i];
427 }
428 }
429 foo[i] = '\0';
430 debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
431 }
432 }
433
434 #endif /* def DEBUG */