8 /* ----------------------------------------------------------------------
9 * String handling routines.
12 char *dupstr(const char *s)
17 p = snewn(len + 1, char);
23 /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
24 char *dupcat(const char *s1, ...)
33 sn = va_arg(ap, char *);
40 p = snewn(len + 1, char);
46 sn = va_arg(ap, char *);
58 * Do an sprintf(), but into a custom-allocated buffer.
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
68 char *dupprintf(const char *fmt, ...)
73 ret = dupvprintf(fmt, ap);
77 char *dupvprintf(const char *fmt, va_list ap)
82 buf = snewn(512, char);
87 #define vsnprintf _vsnprintf
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. */
95 /* This is the C99 error condition: the returned length is
96 * the required buffer size not counting the NUL. */
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. */
103 buf = sresize(buf, size, char);
107 /* ----------------------------------------------------------------------
108 * Base64 encoding routine. This is required in public-key writing
109 * but also in HTTP proxy handling, so it's centralised here.
112 void base64_encode_atom(unsigned char *data, int n, char *out)
114 static const char base64_chars[] =
115 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
119 word = data[0] << 16;
121 word |= data[1] << 8;
124 out[0] = base64_chars[(word >> 18) & 0x3F];
125 out[1] = base64_chars[(word >> 12) & 0x3F];
127 out[2] = base64_chars[(word >> 6) & 0x3F];
131 out[3] = base64_chars[word & 0x3F];
136 /* ----------------------------------------------------------------------
137 * Generic routines to deal with send buffers: a linked list of
138 * smallish blocks, with the operations
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
145 * - retrieve a larger amount of initial data from the list
146 * - return the current size of the buffer chain in bytes
149 #define BUFFER_GRANULE 512
151 struct bufchain_granule {
152 struct bufchain_granule *next;
154 char buf[BUFFER_GRANULE];
157 void bufchain_init(bufchain *ch)
159 ch->head = ch->tail = NULL;
163 void bufchain_clear(bufchain *ch)
165 struct bufchain_granule *b;
168 ch->head = ch->head->next;
175 int bufchain_size(bufchain *ch)
177 return ch->buffersize;
180 void bufchain_add(bufchain *ch, const void *data, int len)
182 const char *buf = (const char *)data;
184 ch->buffersize += len;
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);
191 ch->tail->buflen += copylen;
194 int grainlen = min(len, BUFFER_GRANULE);
195 struct bufchain_granule *newbuf;
196 newbuf = snew(struct bufchain_granule);
198 newbuf->buflen = grainlen;
199 memcpy(newbuf->buf, buf, grainlen);
203 ch->tail->next = newbuf;
205 ch->head = ch->tail = newbuf;
211 void bufchain_consume(bufchain *ch, int len)
213 struct bufchain_granule *tmp;
215 assert(ch->buffersize >= len);
218 assert(ch->head != NULL);
219 if (remlen >= ch->head->buflen - ch->head->bufpos) {
220 remlen = ch->head->buflen - ch->head->bufpos;
222 ch->head = tmp->next;
227 ch->head->bufpos += remlen;
228 ch->buffersize -= remlen;
233 void bufchain_prefix(bufchain *ch, void **data, int *len)
235 *len = ch->head->buflen - ch->head->bufpos;
236 *data = ch->head->buf + ch->head->bufpos;
239 void bufchain_fetch(bufchain *ch, void *data, int len)
241 struct bufchain_granule *tmp;
242 char *data_c = (char *)data;
246 assert(ch->buffersize >= len);
251 if (remlen >= tmp->buflen - tmp->bufpos)
252 remlen = tmp->buflen - tmp->bufpos;
253 memcpy(data_c, tmp->buf + tmp->bufpos, remlen);
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
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);
278 static FILE *fp = NULL;
280 static char *mlog_file = NULL;
281 static int mlog_line = 0;
283 void mlog(char *file, int line)
288 fp = fopen("putty_mem.log", "w");
289 setvbuf(fp, NULL, _IONBF, BUFSIZ);
292 fprintf(fp, "%s:%d: ", file, line);
296 void *safemalloc(size_t size)
300 p = minefield_c_malloc(size);
307 sprintf(str, "Out of memory! (%s:%d, size=%d)",
308 mlog_file, mlog_line, size);
309 fprintf(fp, "*** %s\n", str);
312 strcpy(str, "Out of memory!");
318 fprintf(fp, "malloc(%d) returns %p\n", size, p);
323 void *saferealloc(void *ptr, size_t size)
328 p = minefield_c_malloc(size);
334 p = minefield_c_realloc(ptr, size);
336 p = realloc(ptr, size);
342 sprintf(str, "Out of memory! (%s:%d, size=%d)",
343 mlog_file, mlog_line, size);
344 fprintf(fp, "*** %s\n", str);
347 strcpy(str, "Out of memory!");
353 fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
358 void safefree(void *ptr)
363 fprintf(fp, "free(%p)\n", ptr);
366 minefield_c_free(ptr);
373 fprintf(fp, "freeing null pointer - no action taken\n");
377 /* ----------------------------------------------------------------------
378 * Debugging routines.
382 extern void dputs(char *); /* defined in per-platform *misc.c */
384 void debug_printf(char *fmt, ...)
390 buf = dupvprintf(fmt, ap);
397 void debug_memdump(void *buf, int len, int L)
400 unsigned char *p = buf;
404 debug_printf("\t%d (0x%x) bytes:\n", len, len);
405 delta = 15 & (int) p;
409 for (; 0 < len; p += 16, len -= 16) {
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 */
419 debug_printf("%c%02.2x",
420 &p[i] != (unsigned char *) buf
421 && i % 4 ? '.' : ' ', p[i]
423 if (p[i] >= ' ' && p[i] <= '~')
424 foo[i] = (char) p[i];
428 debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
432 #endif /* def DEBUG */