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 if (len
== 0) return;
186 ch
->buffersize
+= len
;
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
);
193 ch
->tail
->buflen
+= copylen
;
196 int grainlen
= min(len
, BUFFER_GRANULE
);
197 struct bufchain_granule
*newbuf
;
198 newbuf
= snew(struct bufchain_granule
);
200 newbuf
->buflen
= grainlen
;
201 memcpy(newbuf
->buf
, buf
, grainlen
);
205 ch
->tail
->next
= newbuf
;
207 ch
->head
= ch
->tail
= newbuf
;
213 void bufchain_consume(bufchain
*ch
, int len
)
215 struct bufchain_granule
*tmp
;
217 assert(ch
->buffersize
>= len
);
220 assert(ch
->head
!= NULL
);
221 if (remlen
>= ch
->head
->buflen
- ch
->head
->bufpos
) {
222 remlen
= ch
->head
->buflen
- ch
->head
->bufpos
;
224 ch
->head
= tmp
->next
;
229 ch
->head
->bufpos
+= remlen
;
230 ch
->buffersize
-= remlen
;
235 void bufchain_prefix(bufchain
*ch
, void **data
, int *len
)
237 *len
= ch
->head
->buflen
- ch
->head
->bufpos
;
238 *data
= ch
->head
->buf
+ ch
->head
->bufpos
;
241 void bufchain_fetch(bufchain
*ch
, void *data
, int len
)
243 struct bufchain_granule
*tmp
;
244 char *data_c
= (char *)data
;
248 assert(ch
->buffersize
>= len
);
253 if (remlen
>= tmp
->buflen
- tmp
->bufpos
)
254 remlen
= tmp
->buflen
- tmp
->bufpos
;
255 memcpy(data_c
, tmp
->buf
+ tmp
->bufpos
, remlen
);
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
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
);
280 static FILE *fp
= NULL
;
282 static char *mlog_file
= NULL
;
283 static int mlog_line
= 0;
285 void mlog(char *file
, int line
)
290 fp
= fopen("putty_mem.log", "w");
291 setvbuf(fp
, NULL
, _IONBF
, BUFSIZ
);
294 fprintf(fp
, "%s:%d: ", file
, line
);
298 void *safemalloc(size_t size
)
302 p
= minefield_c_malloc(size
);
309 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
310 mlog_file
, mlog_line
, size
);
311 fprintf(fp
, "*** %s\n", str
);
314 strcpy(str
, "Out of memory!");
320 fprintf(fp
, "malloc(%d) returns %p\n", size
, p
);
325 void *saferealloc(void *ptr
, size_t size
)
330 p
= minefield_c_malloc(size
);
336 p
= minefield_c_realloc(ptr
, size
);
338 p
= realloc(ptr
, size
);
344 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
345 mlog_file
, mlog_line
, size
);
346 fprintf(fp
, "*** %s\n", str
);
349 strcpy(str
, "Out of memory!");
355 fprintf(fp
, "realloc(%p,%d) returns %p\n", ptr
, size
, p
);
360 void safefree(void *ptr
)
365 fprintf(fp
, "free(%p)\n", ptr
);
368 minefield_c_free(ptr
);
375 fprintf(fp
, "freeing null pointer - no action taken\n");
379 /* ----------------------------------------------------------------------
380 * Debugging routines.
384 extern void dputs(char *); /* defined in per-platform *misc.c */
386 void debug_printf(char *fmt
, ...)
392 buf
= dupvprintf(fmt
, ap
);
399 void debug_memdump(void *buf
, int len
, int L
)
402 unsigned char *p
= buf
;
406 debug_printf("\t%d (0x%x) bytes:\n", len
, len
);
407 delta
= 15 & (int) p
;
411 for (; 0 < len
; p
+= 16, len
-= 16) {
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 */
421 debug_printf("%c%02.2x",
422 &p
[i
] != (unsigned char *) buf
423 && i
% 4 ?
'.' : ' ', p
[i
]
425 if (p
[i
] >= ' ' && p
[i
] <= '~')
426 foo
[i
] = (char) p
[i
];
430 debug_printf("%*s%s\n", (16 - i
) * 3 + 2, "", foo
);
434 #endif /* def DEBUG */