2 * Platform-independent routines shared between all PuTTY programs.
\r
14 * Parse a string block size specification. This is approximately a
\r
15 * subset of the block size specs supported by GNU fileutils:
\r
16 * "nk" = n kilobytes
\r
17 * "nM" = n megabytes
\r
18 * "nG" = n gigabytes
\r
19 * All numbers are decimal, and suffixes refer to powers of two.
\r
22 unsigned long parse_blocksize(const char *bs)
\r
25 unsigned long r = strtoul(bs, &suf, 10);
\r
27 while (*suf && isspace((unsigned char)*suf)) suf++;
\r
33 r *= 1024ul * 1024ul;
\r
36 r *= 1024ul * 1024ul * 1024ul;
\r
47 * Parse a ^C style character specification.
\r
48 * Returns NULL in `next' if we didn't recognise it as a control character,
\r
49 * in which case `c' should be ignored.
\r
50 * The precise current parsing is an oddity inherited from the terminal
\r
51 * answerback-string parsing code. All sequences start with ^; all except
\r
52 * ^<123> are two characters. The ones that are worth keeping are probably:
\r
56 * <num> specified by number (decimal, 0octal, 0xHEX)
\r
59 char ctrlparse(char *s, char **next)
\r
68 } else if (*s == '<') {
\r
70 c = (char)strtol(s, next, 0);
\r
71 if ((*next == s) || (**next != '>')) {
\r
76 } else if (*s >= 'a' && *s <= 'z') {
\r
77 c = (*s - ('a' - 1));
\r
79 } else if ((*s >= '@' && *s <= '_') || *s == '?' || (*s & 0x80)) {
\r
82 } else if (*s == '~') {
\r
90 prompts_t *new_prompts(void *frontend)
\r
92 prompts_t *p = snew(prompts_t);
\r
95 p->frontend = frontend;
\r
97 p->to_server = TRUE; /* to be on the safe side */
\r
98 p->name = p->instruction = NULL;
\r
99 p->name_reqd = p->instr_reqd = FALSE;
\r
102 void add_prompt(prompts_t *p, char *promptstr, int echo, size_t len)
\r
104 prompt_t *pr = snew(prompt_t);
\r
105 char *result = snewn(len, char);
\r
106 pr->prompt = promptstr;
\r
108 pr->result = result;
\r
109 pr->result_len = len;
\r
111 p->prompts = sresize(p->prompts, p->n_prompts, prompt_t *);
\r
112 p->prompts[p->n_prompts-1] = pr;
\r
114 void free_prompts(prompts_t *p)
\r
117 for (i=0; i < p->n_prompts; i++) {
\r
118 prompt_t *pr = p->prompts[i];
\r
119 memset(pr->result, 0, pr->result_len); /* burn the evidence */
\r
126 sfree(p->instruction);
\r
130 /* ----------------------------------------------------------------------
\r
131 * String handling routines.
\r
134 char *dupstr(const char *s)
\r
138 int len = strlen(s);
\r
139 p = snewn(len + 1, char);
\r
145 /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
\r
146 char *dupcat(const char *s1, ...)
\r
155 sn = va_arg(ap, char *);
\r
162 p = snewn(len + 1, char);
\r
168 sn = va_arg(ap, char *);
\r
180 * Do an sprintf(), but into a custom-allocated buffer.
\r
182 * Currently I'm doing this via vsnprintf. This has worked so far,
\r
183 * but it's not good, because vsnprintf is not available on all
\r
184 * platforms. There's an ifdef to use `_vsnprintf', which seems
\r
185 * to be the local name for it on Windows. Other platforms may
\r
186 * lack it completely, in which case it'll be time to rewrite
\r
187 * this function in a totally different way.
\r
189 * The only `properly' portable solution I can think of is to
\r
190 * implement my own format string scanner, which figures out an
\r
191 * upper bound for the length of each formatting directive,
\r
192 * allocates the buffer as it goes along, and calls sprintf() to
\r
193 * actually process each directive. If I ever need to actually do
\r
194 * this, some caveats:
\r
196 * - It's very hard to find a reliable upper bound for
\r
197 * floating-point values. %f, in particular, when supplied with
\r
198 * a number near to the upper or lower limit of representable
\r
199 * numbers, could easily take several hundred characters. It's
\r
200 * probably feasible to predict this statically using the
\r
201 * constants in <float.h>, or even to predict it dynamically by
\r
202 * looking at the exponent of the specific float provided, but
\r
205 * - Don't forget to _check_, after calling sprintf, that it's
\r
206 * used at most the amount of space we had available.
\r
208 * - Fault any formatting directive we don't fully understand. The
\r
209 * aim here is to _guarantee_ that we never overflow the buffer,
\r
210 * because this is a security-critical function. If we see a
\r
211 * directive we don't know about, we should panic and die rather
\r
212 * than run any risk.
\r
214 char *dupprintf(const char *fmt, ...)
\r
219 ret = dupvprintf(fmt, ap);
\r
223 char *dupvprintf(const char *fmt, va_list ap)
\r
228 buf = snewn(512, char);
\r
233 #define vsnprintf _vsnprintf
\r
236 /* Use the `va_copy' macro mandated by C99, if present.
\r
237 * XXX some environments may have this as __va_copy() */
\r
240 len = vsnprintf(buf, size, fmt, aq);
\r
243 /* Ugh. No va_copy macro, so do something nasty.
\r
244 * Technically, you can't reuse a va_list like this: it is left
\r
245 * unspecified whether advancing a va_list pointer modifies its
\r
246 * value or something it points to, so on some platforms calling
\r
247 * vsnprintf twice on the same va_list might fail hideously
\r
248 * (indeed, it has been observed to).
\r
249 * XXX the autoconf manual suggests that using memcpy() will give
\r
250 * "maximum portability". */
\r
251 len = vsnprintf(buf, size, fmt, ap);
\r
253 if (len >= 0 && len < size) {
\r
254 /* This is the C99-specified criterion for snprintf to have
\r
255 * been completely successful. */
\r
257 } else if (len > 0) {
\r
258 /* This is the C99 error condition: the returned length is
\r
259 * the required buffer size not counting the NUL. */
\r
262 /* This is the pre-C99 glibc error condition: <0 means the
\r
263 * buffer wasn't big enough, so we enlarge it a bit and hope. */
\r
266 buf = sresize(buf, size, char);
\r
271 * Read an entire line of text from a file. Return a buffer
\r
272 * malloced to be as big as necessary (caller must free).
\r
274 char *fgetline(FILE *fp)
\r
276 char *ret = snewn(512, char);
\r
277 int size = 512, len = 0;
\r
278 while (fgets(ret + len, size - len, fp)) {
\r
279 len += strlen(ret + len);
\r
280 if (ret[len-1] == '\n')
\r
281 break; /* got a newline, we're done */
\r
283 ret = sresize(ret, size, char);
\r
285 if (len == 0) { /* first fgets returned NULL */
\r
293 /* ----------------------------------------------------------------------
\r
294 * Base64 encoding routine. This is required in public-key writing
\r
295 * but also in HTTP proxy handling, so it's centralised here.
\r
298 void base64_encode_atom(unsigned char *data, int n, char *out)
\r
300 static const char base64_chars[] =
\r
301 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
\r
305 word = data[0] << 16;
\r
307 word |= data[1] << 8;
\r
310 out[0] = base64_chars[(word >> 18) & 0x3F];
\r
311 out[1] = base64_chars[(word >> 12) & 0x3F];
\r
313 out[2] = base64_chars[(word >> 6) & 0x3F];
\r
317 out[3] = base64_chars[word & 0x3F];
\r
322 /* ----------------------------------------------------------------------
\r
323 * Generic routines to deal with send buffers: a linked list of
\r
324 * smallish blocks, with the operations
\r
326 * - add an arbitrary amount of data to the end of the list
\r
327 * - remove the first N bytes from the list
\r
328 * - return a (pointer,length) pair giving some initial data in
\r
329 * the list, suitable for passing to a send or write system
\r
331 * - retrieve a larger amount of initial data from the list
\r
332 * - return the current size of the buffer chain in bytes
\r
335 #define BUFFER_GRANULE 512
\r
337 struct bufchain_granule {
\r
338 struct bufchain_granule *next;
\r
339 int buflen, bufpos;
\r
340 char buf[BUFFER_GRANULE];
\r
343 void bufchain_init(bufchain *ch)
\r
345 ch->head = ch->tail = NULL;
\r
346 ch->buffersize = 0;
\r
349 void bufchain_clear(bufchain *ch)
\r
351 struct bufchain_granule *b;
\r
354 ch->head = ch->head->next;
\r
358 ch->buffersize = 0;
\r
361 int bufchain_size(bufchain *ch)
\r
363 return ch->buffersize;
\r
366 void bufchain_add(bufchain *ch, const void *data, int len)
\r
368 const char *buf = (const char *)data;
\r
370 if (len == 0) return;
\r
372 ch->buffersize += len;
\r
374 if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) {
\r
375 int copylen = min(len, BUFFER_GRANULE - ch->tail->buflen);
\r
376 memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen);
\r
379 ch->tail->buflen += copylen;
\r
382 int grainlen = min(len, BUFFER_GRANULE);
\r
383 struct bufchain_granule *newbuf;
\r
384 newbuf = snew(struct bufchain_granule);
\r
385 newbuf->bufpos = 0;
\r
386 newbuf->buflen = grainlen;
\r
387 memcpy(newbuf->buf, buf, grainlen);
\r
391 ch->tail->next = newbuf;
\r
393 ch->head = ch->tail = newbuf;
\r
394 newbuf->next = NULL;
\r
399 void bufchain_consume(bufchain *ch, int len)
\r
401 struct bufchain_granule *tmp;
\r
403 assert(ch->buffersize >= len);
\r
406 assert(ch->head != NULL);
\r
407 if (remlen >= ch->head->buflen - ch->head->bufpos) {
\r
408 remlen = ch->head->buflen - ch->head->bufpos;
\r
410 ch->head = tmp->next;
\r
415 ch->head->bufpos += remlen;
\r
416 ch->buffersize -= remlen;
\r
421 void bufchain_prefix(bufchain *ch, void **data, int *len)
\r
423 *len = ch->head->buflen - ch->head->bufpos;
\r
424 *data = ch->head->buf + ch->head->bufpos;
\r
427 void bufchain_fetch(bufchain *ch, void *data, int len)
\r
429 struct bufchain_granule *tmp;
\r
430 char *data_c = (char *)data;
\r
434 assert(ch->buffersize >= len);
\r
438 assert(tmp != NULL);
\r
439 if (remlen >= tmp->buflen - tmp->bufpos)
\r
440 remlen = tmp->buflen - tmp->bufpos;
\r
441 memcpy(data_c, tmp->buf + tmp->bufpos, remlen);
\r
449 /* ----------------------------------------------------------------------
\r
450 * My own versions of malloc, realloc and free. Because I want
\r
451 * malloc and realloc to bomb out and exit the program if they run
\r
452 * out of memory, realloc to reliably call malloc if passed a NULL
\r
453 * pointer, and free to reliably do nothing if passed a NULL
\r
454 * pointer. We can also put trace printouts in, if we need to; and
\r
455 * we can also replace the allocator with an ElectricFence-like
\r
460 void *minefield_c_malloc(size_t size);
\r
461 void minefield_c_free(void *p);
\r
462 void *minefield_c_realloc(void *p, size_t size);
\r
466 static FILE *fp = NULL;
\r
468 static char *mlog_file = NULL;
\r
469 static int mlog_line = 0;
\r
471 void mlog(char *file, int line)
\r
476 fp = fopen("putty_mem.log", "w");
\r
477 setvbuf(fp, NULL, _IONBF, BUFSIZ);
\r
480 fprintf(fp, "%s:%d: ", file, line);
\r
484 void *safemalloc(size_t n, size_t size)
\r
488 if (n > INT_MAX / size) {
\r
492 if (size == 0) size = 1;
\r
494 p = minefield_c_malloc(size);
\r
503 sprintf(str, "Out of memory! (%s:%d, size=%d)",
\r
504 mlog_file, mlog_line, size);
\r
505 fprintf(fp, "*** %s\n", str);
\r
508 strcpy(str, "Out of memory!");
\r
510 modalfatalbox(str);
\r
514 fprintf(fp, "malloc(%d) returns %p\n", size, p);
\r
519 void *saferealloc(void *ptr, size_t n, size_t size)
\r
523 if (n > INT_MAX / size) {
\r
529 p = minefield_c_malloc(size);
\r
535 p = minefield_c_realloc(ptr, size);
\r
537 p = realloc(ptr, size);
\r
545 sprintf(str, "Out of memory! (%s:%d, size=%d)",
\r
546 mlog_file, mlog_line, size);
\r
547 fprintf(fp, "*** %s\n", str);
\r
550 strcpy(str, "Out of memory!");
\r
552 modalfatalbox(str);
\r
556 fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
\r
561 void safefree(void *ptr)
\r
566 fprintf(fp, "free(%p)\n", ptr);
\r
569 minefield_c_free(ptr);
\r
576 fprintf(fp, "freeing null pointer - no action taken\n");
\r
580 /* ----------------------------------------------------------------------
\r
581 * Debugging routines.
\r
585 extern void dputs(char *); /* defined in per-platform *misc.c */
\r
587 void debug_printf(char *fmt, ...)
\r
593 buf = dupvprintf(fmt, ap);
\r
600 void debug_memdump(void *buf, int len, int L)
\r
603 unsigned char *p = buf;
\r
607 debug_printf("\t%d (0x%x) bytes:\n", len, len);
\r
608 delta = 15 & (unsigned long int) p;
\r
612 for (; 0 < len; p += 16, len -= 16) {
\r
615 debug_printf("%p: ", p);
\r
616 strcpy(foo, "................"); /* sixteen dots */
\r
617 for (i = 0; i < 16 && i < len; ++i) {
\r
618 if (&p[i] < (unsigned char *) buf) {
\r
619 dputs(" "); /* 3 spaces */
\r
622 debug_printf("%c%02.2x",
\r
623 &p[i] != (unsigned char *) buf
\r
624 && i % 4 ? '.' : ' ', p[i]
\r
626 if (p[i] >= ' ' && p[i] <= '~')
\r
627 foo[i] = (char) p[i];
\r
631 debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
\r
635 #endif /* def DEBUG */
\r
638 * Determine whether or not a Config structure represents a session
\r
639 * which can sensibly be launched right now.
\r
641 int cfg_launchable(const Config *cfg)
\r
643 if (cfg->protocol == PROT_SERIAL)
\r
644 return cfg->serline[0] != 0;
\r
646 return cfg->host[0] != 0;
\r
649 char const *cfg_dest(const Config *cfg)
\r
651 if (cfg->protocol == PROT_SERIAL)
\r
652 return cfg->serline;
\r