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 * Currently I'm doing this via vsnprintf. This has worked so far,
65 * but it's not good, because:
67 * - vsnprintf is not available on all platforms. There's an ifdef
68 * to use `_vsnprintf', which seems to be the local name for it
69 * on Windows. Other platforms may lack it completely, in which
70 * case it'll be time to rewrite this function in a totally
73 * - technically you can't reuse a va_list like this: it is left
74 * unspecified whether advancing a va_list pointer modifies its
75 * value or something it points to, so on some platforms calling
76 * vsnprintf twice on the same va_list might fail hideously. It
77 * would be better to use the `va_copy' macro mandated by C99,
78 * but that too is not yet ubiquitous.
80 * The only `properly' portable solution I can think of is to
81 * implement my own format string scanner, which figures out an
82 * upper bound for the length of each formatting directive,
83 * allocates the buffer as it goes along, and calls sprintf() to
84 * actually process each directive. If I ever need to actually do
87 * - It's very hard to find a reliable upper bound for
88 * floating-point values. %f, in particular, when supplied with
89 * a number near to the upper or lower limit of representable
90 * numbers, could easily take several hundred characters. It's
91 * probably feasible to predict this statically using the
92 * constants in <float.h>, or even to predict it dynamically by
93 * looking at the exponent of the specific float provided, but
96 * - Don't forget to _check_, after calling sprintf, that it's
97 * used at most the amount of space we had available.
99 * - Fault any formatting directive we don't fully understand. The
100 * aim here is to _guarantee_ that we never overflow the buffer,
101 * because this is a security-critical function. If we see a
102 * directive we don't know about, we should panic and die rather
105 char *dupprintf(const char *fmt
, ...)
110 ret
= dupvprintf(fmt
, ap
);
114 char *dupvprintf(const char *fmt
, va_list ap
)
119 buf
= snewn(512, char);
124 #define vsnprintf _vsnprintf
126 len
= vsnprintf(buf
, size
, fmt
, ap
);
127 if (len
>= 0 && len
< size
) {
128 /* This is the C99-specified criterion for snprintf to have
129 * been completely successful. */
131 } else if (len
> 0) {
132 /* This is the C99 error condition: the returned length is
133 * the required buffer size not counting the NUL. */
136 /* This is the pre-C99 glibc error condition: <0 means the
137 * buffer wasn't big enough, so we enlarge it a bit and hope. */
140 buf
= sresize(buf
, size
, char);
144 /* ----------------------------------------------------------------------
145 * Base64 encoding routine. This is required in public-key writing
146 * but also in HTTP proxy handling, so it's centralised here.
149 void base64_encode_atom(unsigned char *data
, int n
, char *out
)
151 static const char base64_chars
[] =
152 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
156 word
= data
[0] << 16;
158 word
|= data
[1] << 8;
161 out
[0] = base64_chars
[(word
>> 18) & 0x3F];
162 out
[1] = base64_chars
[(word
>> 12) & 0x3F];
164 out
[2] = base64_chars
[(word
>> 6) & 0x3F];
168 out
[3] = base64_chars
[word
& 0x3F];
173 /* ----------------------------------------------------------------------
174 * Generic routines to deal with send buffers: a linked list of
175 * smallish blocks, with the operations
177 * - add an arbitrary amount of data to the end of the list
178 * - remove the first N bytes from the list
179 * - return a (pointer,length) pair giving some initial data in
180 * the list, suitable for passing to a send or write system
182 * - retrieve a larger amount of initial data from the list
183 * - return the current size of the buffer chain in bytes
186 #define BUFFER_GRANULE 512
188 struct bufchain_granule
{
189 struct bufchain_granule
*next
;
191 char buf
[BUFFER_GRANULE
];
194 void bufchain_init(bufchain
*ch
)
196 ch
->head
= ch
->tail
= NULL
;
200 void bufchain_clear(bufchain
*ch
)
202 struct bufchain_granule
*b
;
205 ch
->head
= ch
->head
->next
;
212 int bufchain_size(bufchain
*ch
)
214 return ch
->buffersize
;
217 void bufchain_add(bufchain
*ch
, const void *data
, int len
)
219 const char *buf
= (const char *)data
;
221 if (len
== 0) return;
223 ch
->buffersize
+= len
;
225 if (ch
->tail
&& ch
->tail
->buflen
< BUFFER_GRANULE
) {
226 int copylen
= min(len
, BUFFER_GRANULE
- ch
->tail
->buflen
);
227 memcpy(ch
->tail
->buf
+ ch
->tail
->buflen
, buf
, copylen
);
230 ch
->tail
->buflen
+= copylen
;
233 int grainlen
= min(len
, BUFFER_GRANULE
);
234 struct bufchain_granule
*newbuf
;
235 newbuf
= snew(struct bufchain_granule
);
237 newbuf
->buflen
= grainlen
;
238 memcpy(newbuf
->buf
, buf
, grainlen
);
242 ch
->tail
->next
= newbuf
;
244 ch
->head
= ch
->tail
= newbuf
;
250 void bufchain_consume(bufchain
*ch
, int len
)
252 struct bufchain_granule
*tmp
;
254 assert(ch
->buffersize
>= len
);
257 assert(ch
->head
!= NULL
);
258 if (remlen
>= ch
->head
->buflen
- ch
->head
->bufpos
) {
259 remlen
= ch
->head
->buflen
- ch
->head
->bufpos
;
261 ch
->head
= tmp
->next
;
266 ch
->head
->bufpos
+= remlen
;
267 ch
->buffersize
-= remlen
;
272 void bufchain_prefix(bufchain
*ch
, void **data
, int *len
)
274 *len
= ch
->head
->buflen
- ch
->head
->bufpos
;
275 *data
= ch
->head
->buf
+ ch
->head
->bufpos
;
278 void bufchain_fetch(bufchain
*ch
, void *data
, int len
)
280 struct bufchain_granule
*tmp
;
281 char *data_c
= (char *)data
;
285 assert(ch
->buffersize
>= len
);
290 if (remlen
>= tmp
->buflen
- tmp
->bufpos
)
291 remlen
= tmp
->buflen
- tmp
->bufpos
;
292 memcpy(data_c
, tmp
->buf
+ tmp
->bufpos
, remlen
);
300 /* ----------------------------------------------------------------------
301 * My own versions of malloc, realloc and free. Because I want
302 * malloc and realloc to bomb out and exit the program if they run
303 * out of memory, realloc to reliably call malloc if passed a NULL
304 * pointer, and free to reliably do nothing if passed a NULL
305 * pointer. We can also put trace printouts in, if we need to; and
306 * we can also replace the allocator with an ElectricFence-like
311 void *minefield_c_malloc(size_t size
);
312 void minefield_c_free(void *p
);
313 void *minefield_c_realloc(void *p
, size_t size
);
317 static FILE *fp
= NULL
;
319 static char *mlog_file
= NULL
;
320 static int mlog_line
= 0;
322 void mlog(char *file
, int line
)
327 fp
= fopen("putty_mem.log", "w");
328 setvbuf(fp
, NULL
, _IONBF
, BUFSIZ
);
331 fprintf(fp
, "%s:%d: ", file
, line
);
335 void *safemalloc(size_t size
)
339 p
= minefield_c_malloc(size
);
346 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
347 mlog_file
, mlog_line
, size
);
348 fprintf(fp
, "*** %s\n", str
);
351 strcpy(str
, "Out of memory!");
357 fprintf(fp
, "malloc(%d) returns %p\n", size
, p
);
362 void *saferealloc(void *ptr
, size_t size
)
367 p
= minefield_c_malloc(size
);
373 p
= minefield_c_realloc(ptr
, size
);
375 p
= realloc(ptr
, size
);
381 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
382 mlog_file
, mlog_line
, size
);
383 fprintf(fp
, "*** %s\n", str
);
386 strcpy(str
, "Out of memory!");
392 fprintf(fp
, "realloc(%p,%d) returns %p\n", ptr
, size
, p
);
397 void safefree(void *ptr
)
402 fprintf(fp
, "free(%p)\n", ptr
);
405 minefield_c_free(ptr
);
412 fprintf(fp
, "freeing null pointer - no action taken\n");
416 /* ----------------------------------------------------------------------
417 * Debugging routines.
421 extern void dputs(char *); /* defined in per-platform *misc.c */
423 void debug_printf(char *fmt
, ...)
429 buf
= dupvprintf(fmt
, ap
);
436 void debug_memdump(void *buf
, int len
, int L
)
439 unsigned char *p
= buf
;
443 debug_printf("\t%d (0x%x) bytes:\n", len
, len
);
444 delta
= 15 & (int) p
;
448 for (; 0 < len
; p
+= 16, len
-= 16) {
451 debug_printf("%p: ", p
);
452 strcpy(foo
, "................"); /* sixteen dots */
453 for (i
= 0; i
< 16 && i
< len
; ++i
) {
454 if (&p
[i
] < (unsigned char *) buf
) {
455 dputs(" "); /* 3 spaces */
458 debug_printf("%c%02.2x",
459 &p
[i
] != (unsigned char *) buf
460 && i
% 4 ?
'.' : ' ', p
[i
]
462 if (p
[i
] >= ' ' && p
[i
] <= '~')
463 foo
[i
] = (char) p
[i
];
467 debug_printf("%*s%s\n", (16 - i
) * 3 + 2, "", foo
);
471 #endif /* def DEBUG */