2 * Platform-independent routines shared between all PuTTY programs.
12 /* ----------------------------------------------------------------------
13 * String handling routines.
16 char *dupstr(const char *s
)
21 p
= snewn(len
+ 1, char);
27 /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
28 char *dupcat(const char *s1
, ...)
37 sn
= va_arg(ap
, char *);
44 p
= snewn(len
+ 1, char);
50 sn
= va_arg(ap
, char *);
62 * Do an sprintf(), but into a custom-allocated buffer.
64 * Irritatingly, we don't seem to be able to do this portably using
65 * vsnprintf(), because there appear to be issues with re-using the
66 * same va_list for two calls, and the excellent C99 va_copy is not
67 * yet widespread. Bah. Instead I'm going to do a horrid, horrid
68 * hack, in which I trawl the format string myself, work out the
69 * maximum length of each format component, and resize the buffer
72 char *dupprintf(const char *fmt
, ...)
77 ret
= dupvprintf(fmt
, ap
);
81 char *dupvprintf(const char *fmt
, va_list ap
)
86 buf
= snewn(512, char);
91 #define vsnprintf _vsnprintf
93 len
= vsnprintf(buf
, size
, fmt
, ap
);
94 if (len
>= 0 && len
< size
) {
95 /* This is the C99-specified criterion for snprintf to have
96 * been completely successful. */
99 /* This is the C99 error condition: the returned length is
100 * the required buffer size not counting the NUL. */
103 /* This is the pre-C99 glibc error condition: <0 means the
104 * buffer wasn't big enough, so we enlarge it a bit and hope. */
107 buf
= sresize(buf
, size
, char);
111 /* ----------------------------------------------------------------------
112 * Base64 encoding routine. This is required in public-key writing
113 * but also in HTTP proxy handling, so it's centralised here.
116 void base64_encode_atom(unsigned char *data
, int n
, char *out
)
118 static const char base64_chars
[] =
119 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
123 word
= data
[0] << 16;
125 word
|= data
[1] << 8;
128 out
[0] = base64_chars
[(word
>> 18) & 0x3F];
129 out
[1] = base64_chars
[(word
>> 12) & 0x3F];
131 out
[2] = base64_chars
[(word
>> 6) & 0x3F];
135 out
[3] = base64_chars
[word
& 0x3F];
140 /* ----------------------------------------------------------------------
141 * Generic routines to deal with send buffers: a linked list of
142 * smallish blocks, with the operations
144 * - add an arbitrary amount of data to the end of the list
145 * - remove the first N bytes from the list
146 * - return a (pointer,length) pair giving some initial data in
147 * the list, suitable for passing to a send or write system
149 * - retrieve a larger amount of initial data from the list
150 * - return the current size of the buffer chain in bytes
153 #define BUFFER_GRANULE 512
155 struct bufchain_granule
{
156 struct bufchain_granule
*next
;
158 char buf
[BUFFER_GRANULE
];
161 void bufchain_init(bufchain
*ch
)
163 ch
->head
= ch
->tail
= NULL
;
167 void bufchain_clear(bufchain
*ch
)
169 struct bufchain_granule
*b
;
172 ch
->head
= ch
->head
->next
;
179 int bufchain_size(bufchain
*ch
)
181 return ch
->buffersize
;
184 void bufchain_add(bufchain
*ch
, const void *data
, int len
)
186 const char *buf
= (const char *)data
;
188 if (len
== 0) return;
190 ch
->buffersize
+= len
;
192 if (ch
->tail
&& ch
->tail
->buflen
< BUFFER_GRANULE
) {
193 int copylen
= min(len
, BUFFER_GRANULE
- ch
->tail
->buflen
);
194 memcpy(ch
->tail
->buf
+ ch
->tail
->buflen
, buf
, copylen
);
197 ch
->tail
->buflen
+= copylen
;
200 int grainlen
= min(len
, BUFFER_GRANULE
);
201 struct bufchain_granule
*newbuf
;
202 newbuf
= snew(struct bufchain_granule
);
204 newbuf
->buflen
= grainlen
;
205 memcpy(newbuf
->buf
, buf
, grainlen
);
209 ch
->tail
->next
= newbuf
;
211 ch
->head
= ch
->tail
= newbuf
;
217 void bufchain_consume(bufchain
*ch
, int len
)
219 struct bufchain_granule
*tmp
;
221 assert(ch
->buffersize
>= len
);
224 assert(ch
->head
!= NULL
);
225 if (remlen
>= ch
->head
->buflen
- ch
->head
->bufpos
) {
226 remlen
= ch
->head
->buflen
- ch
->head
->bufpos
;
228 ch
->head
= tmp
->next
;
233 ch
->head
->bufpos
+= remlen
;
234 ch
->buffersize
-= remlen
;
239 void bufchain_prefix(bufchain
*ch
, void **data
, int *len
)
241 *len
= ch
->head
->buflen
- ch
->head
->bufpos
;
242 *data
= ch
->head
->buf
+ ch
->head
->bufpos
;
245 void bufchain_fetch(bufchain
*ch
, void *data
, int len
)
247 struct bufchain_granule
*tmp
;
248 char *data_c
= (char *)data
;
252 assert(ch
->buffersize
>= len
);
257 if (remlen
>= tmp
->buflen
- tmp
->bufpos
)
258 remlen
= tmp
->buflen
- tmp
->bufpos
;
259 memcpy(data_c
, tmp
->buf
+ tmp
->bufpos
, remlen
);
267 /* ----------------------------------------------------------------------
268 * My own versions of malloc, realloc and free. Because I want
269 * malloc and realloc to bomb out and exit the program if they run
270 * out of memory, realloc to reliably call malloc if passed a NULL
271 * pointer, and free to reliably do nothing if passed a NULL
272 * pointer. We can also put trace printouts in, if we need to; and
273 * we can also replace the allocator with an ElectricFence-like
278 void *minefield_c_malloc(size_t size
);
279 void minefield_c_free(void *p
);
280 void *minefield_c_realloc(void *p
, size_t size
);
284 static FILE *fp
= NULL
;
286 static char *mlog_file
= NULL
;
287 static int mlog_line
= 0;
289 void mlog(char *file
, int line
)
294 fp
= fopen("putty_mem.log", "w");
295 setvbuf(fp
, NULL
, _IONBF
, BUFSIZ
);
298 fprintf(fp
, "%s:%d: ", file
, line
);
302 void *safemalloc(size_t size
)
306 p
= minefield_c_malloc(size
);
313 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
314 mlog_file
, mlog_line
, size
);
315 fprintf(fp
, "*** %s\n", str
);
318 strcpy(str
, "Out of memory!");
324 fprintf(fp
, "malloc(%d) returns %p\n", size
, p
);
329 void *saferealloc(void *ptr
, size_t size
)
334 p
= minefield_c_malloc(size
);
340 p
= minefield_c_realloc(ptr
, size
);
342 p
= realloc(ptr
, size
);
348 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
349 mlog_file
, mlog_line
, size
);
350 fprintf(fp
, "*** %s\n", str
);
353 strcpy(str
, "Out of memory!");
359 fprintf(fp
, "realloc(%p,%d) returns %p\n", ptr
, size
, p
);
364 void safefree(void *ptr
)
369 fprintf(fp
, "free(%p)\n", ptr
);
372 minefield_c_free(ptr
);
379 fprintf(fp
, "freeing null pointer - no action taken\n");
383 /* ----------------------------------------------------------------------
384 * Debugging routines.
388 extern void dputs(char *); /* defined in per-platform *misc.c */
390 void debug_printf(char *fmt
, ...)
396 buf
= dupvprintf(fmt
, ap
);
403 void debug_memdump(void *buf
, int len
, int L
)
406 unsigned char *p
= buf
;
410 debug_printf("\t%d (0x%x) bytes:\n", len
, len
);
411 delta
= 15 & (int) p
;
415 for (; 0 < len
; p
+= 16, len
-= 16) {
418 debug_printf("%p: ", p
);
419 strcpy(foo
, "................"); /* sixteen dots */
420 for (i
= 0; i
< 16 && i
< len
; ++i
) {
421 if (&p
[i
] < (unsigned char *) buf
) {
422 dputs(" "); /* 3 spaces */
425 debug_printf("%c%02.2x",
426 &p
[i
] != (unsigned char *) buf
427 && i
% 4 ?
'.' : ' ', p
[i
]
429 if (p
[i
] >= ' ' && p
[i
] <= '~')
430 foo
[i
] = (char) p
[i
];
434 debug_printf("%*s%s\n", (16 - i
) * 3 + 2, "", foo
);
438 #endif /* def DEBUG */