8 /* ----------------------------------------------------------------------
9 * String handling routines.
12 char *dupstr(const char *s
)
15 char *p
= snewn(len
+ 1, char);
20 /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
21 char *dupcat(const char *s1
, ...)
30 sn
= va_arg(ap
, char *);
37 p
= snewn(len
+ 1, char);
43 sn
= va_arg(ap
, char *);
55 * Do an sprintf(), but into a custom-allocated buffer.
57 * Irritatingly, we don't seem to be able to do this portably using
58 * vsnprintf(), because there appear to be issues with re-using the
59 * same va_list for two calls, and the excellent C99 va_copy is not
60 * yet widespread. Bah. Instead I'm going to do a horrid, horrid
61 * hack, in which I trawl the format string myself, work out the
62 * maximum length of each format component, and resize the buffer
65 char *dupprintf(const char *fmt
, ...)
70 ret
= dupvprintf(fmt
, ap
);
74 char *dupvprintf(const char *fmt
, va_list ap
)
79 buf
= snewn(512, char);
84 #define vsnprintf _vsnprintf
86 len
= vsnprintf(buf
, size
, fmt
, ap
);
87 if (len
>= 0 && len
< size
) {
88 /* This is the C99-specified criterion for snprintf to have
89 * been completely successful. */
92 /* This is the C99 error condition: the returned length is
93 * the required buffer size not counting the NUL. */
96 /* This is the pre-C99 glibc error condition: <0 means the
97 * buffer wasn't big enough, so we enlarge it a bit and hope. */
100 buf
= sresize(buf
, size
, char);
104 /* ----------------------------------------------------------------------
105 * Base64 encoding routine. This is required in public-key writing
106 * but also in HTTP proxy handling, so it's centralised here.
109 void base64_encode_atom(unsigned char *data
, int n
, char *out
)
111 static const char base64_chars
[] =
112 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
116 word
= data
[0] << 16;
118 word
|= data
[1] << 8;
121 out
[0] = base64_chars
[(word
>> 18) & 0x3F];
122 out
[1] = base64_chars
[(word
>> 12) & 0x3F];
124 out
[2] = base64_chars
[(word
>> 6) & 0x3F];
128 out
[3] = base64_chars
[word
& 0x3F];
133 /* ----------------------------------------------------------------------
134 * Generic routines to deal with send buffers: a linked list of
135 * smallish blocks, with the operations
137 * - add an arbitrary amount of data to the end of the list
138 * - remove the first N bytes from the list
139 * - return a (pointer,length) pair giving some initial data in
140 * the list, suitable for passing to a send or write system
142 * - retrieve a larger amount of initial data from the list
143 * - return the current size of the buffer chain in bytes
146 #define BUFFER_GRANULE 512
148 struct bufchain_granule
{
149 struct bufchain_granule
*next
;
151 char buf
[BUFFER_GRANULE
];
154 void bufchain_init(bufchain
*ch
)
156 ch
->head
= ch
->tail
= NULL
;
160 void bufchain_clear(bufchain
*ch
)
162 struct bufchain_granule
*b
;
165 ch
->head
= ch
->head
->next
;
172 int bufchain_size(bufchain
*ch
)
174 return ch
->buffersize
;
177 void bufchain_add(bufchain
*ch
, const void *data
, int len
)
179 const char *buf
= (const char *)data
;
181 ch
->buffersize
+= len
;
183 if (ch
->tail
&& ch
->tail
->buflen
< BUFFER_GRANULE
) {
184 int copylen
= min(len
, BUFFER_GRANULE
- ch
->tail
->buflen
);
185 memcpy(ch
->tail
->buf
+ ch
->tail
->buflen
, buf
, copylen
);
188 ch
->tail
->buflen
+= copylen
;
191 int grainlen
= min(len
, BUFFER_GRANULE
);
192 struct bufchain_granule
*newbuf
;
193 newbuf
= snew(struct bufchain_granule
);
195 newbuf
->buflen
= grainlen
;
196 memcpy(newbuf
->buf
, buf
, grainlen
);
200 ch
->tail
->next
= newbuf
;
202 ch
->head
= ch
->tail
= newbuf
;
208 void bufchain_consume(bufchain
*ch
, int len
)
210 struct bufchain_granule
*tmp
;
212 assert(ch
->buffersize
>= len
);
215 assert(ch
->head
!= NULL
);
216 if (remlen
>= ch
->head
->buflen
- ch
->head
->bufpos
) {
217 remlen
= ch
->head
->buflen
- ch
->head
->bufpos
;
219 ch
->head
= tmp
->next
;
224 ch
->head
->bufpos
+= remlen
;
225 ch
->buffersize
-= remlen
;
230 void bufchain_prefix(bufchain
*ch
, void **data
, int *len
)
232 *len
= ch
->head
->buflen
- ch
->head
->bufpos
;
233 *data
= ch
->head
->buf
+ ch
->head
->bufpos
;
236 void bufchain_fetch(bufchain
*ch
, void *data
, int len
)
238 struct bufchain_granule
*tmp
;
239 char *data_c
= (char *)data
;
243 assert(ch
->buffersize
>= len
);
248 if (remlen
>= tmp
->buflen
- tmp
->bufpos
)
249 remlen
= tmp
->buflen
- tmp
->bufpos
;
250 memcpy(data_c
, tmp
->buf
+ tmp
->bufpos
, remlen
);
258 /* ----------------------------------------------------------------------
259 * My own versions of malloc, realloc and free. Because I want
260 * malloc and realloc to bomb out and exit the program if they run
261 * out of memory, realloc to reliably call malloc if passed a NULL
262 * pointer, and free to reliably do nothing if passed a NULL
263 * pointer. We can also put trace printouts in, if we need to; and
264 * we can also replace the allocator with an ElectricFence-like
269 void *minefield_c_malloc(size_t size
);
270 void minefield_c_free(void *p
);
271 void *minefield_c_realloc(void *p
, size_t size
);
275 static FILE *fp
= NULL
;
277 static char *mlog_file
= NULL
;
278 static int mlog_line
= 0;
280 void mlog(char *file
, int line
)
285 fp
= fopen("putty_mem.log", "w");
286 setvbuf(fp
, NULL
, _IONBF
, BUFSIZ
);
289 fprintf(fp
, "%s:%d: ", file
, line
);
293 void *safemalloc(size_t size
)
297 p
= minefield_c_malloc(size
);
304 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
305 mlog_file
, mlog_line
, size
);
306 fprintf(fp
, "*** %s\n", str
);
309 strcpy(str
, "Out of memory!");
315 fprintf(fp
, "malloc(%d) returns %p\n", size
, p
);
320 void *saferealloc(void *ptr
, size_t size
)
325 p
= minefield_c_malloc(size
);
331 p
= minefield_c_realloc(ptr
, size
);
333 p
= realloc(ptr
, size
);
339 sprintf(str
, "Out of memory! (%s:%d, size=%d)",
340 mlog_file
, mlog_line
, size
);
341 fprintf(fp
, "*** %s\n", str
);
344 strcpy(str
, "Out of memory!");
350 fprintf(fp
, "realloc(%p,%d) returns %p\n", ptr
, size
, p
);
355 void safefree(void *ptr
)
360 fprintf(fp
, "free(%p)\n", ptr
);
363 minefield_c_free(ptr
);
370 fprintf(fp
, "freeing null pointer - no action taken\n");
374 /* ----------------------------------------------------------------------
375 * Debugging routines.
379 extern void dputs(char *); /* defined in per-platform *misc.c */
381 void debug_printf(char *fmt
, ...)
387 buf
= dupvprintf(fmt
, ap
);
394 void debug_memdump(void *buf
, int len
, int L
)
397 unsigned char *p
= buf
;
401 debug_printf("\t%d (0x%x) bytes:\n", len
, len
);
402 delta
= 15 & (int) p
;
406 for (; 0 < len
; p
+= 16, len
-= 16) {
409 debug_printf("%p: ", p
);
410 strcpy(foo
, "................"); /* sixteen dots */
411 for (i
= 0; i
< 16 && i
< len
; ++i
) {
412 if (&p
[i
] < (unsigned char *) buf
) {
413 dputs(" "); /* 3 spaces */
416 debug_printf("%c%02.2x",
417 &p
[i
] != (unsigned char *) buf
418 && i
% 4 ?
'.' : ' ', p
[i
]
420 if (p
[i
] >= ' ' && p
[i
] <= '~')
421 foo
[i
] = (char) p
[i
];
425 debug_printf("%*s%s\n", (16 - i
) * 3 + 2, "", foo
);
429 #endif /* def DEBUG */