8 #include "puttymem.h"
\r
10 #include <stdio.h> /* for FILE * */
\r
11 #include <stdarg.h> /* for va_list */
\r
12 #include <time.h> /* for struct tm */
\r
21 typedef struct Filename Filename;
\r
22 typedef struct FontSpec FontSpec;
\r
24 unsigned long parse_blocksize(const char *bs);
\r
25 char ctrlparse(char *s, char **next);
\r
27 size_t host_strcspn(const char *s, const char *set);
\r
28 char *host_strchr(const char *s, int c);
\r
29 char *host_strrchr(const char *s, int c);
\r
30 char *host_strduptrim(const char *s);
\r
32 char *dupstr(const char *s);
\r
33 char *dupcat(const char *s1, ...);
\r
34 char *dupprintf(const char *fmt, ...)
\r
36 __attribute__ ((format (printf, 1, 2)))
\r
39 char *dupvprintf(const char *fmt, va_list ap);
\r
40 void burnstr(char *string);
\r
42 /* String-to-Unicode converters that auto-allocate the destination and
\r
43 * work around the rather deficient interface of mb_to_wc.
\r
45 * These actually live in miscucs.c, not misc.c (the distinction being
\r
46 * that the former is only linked into tools that also have the main
\r
47 * Unicode support). */
\r
48 wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
\r
49 wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
\r
51 int toint(unsigned);
\r
53 char *fgetline(FILE *fp);
\r
54 int strstartswith(const char *s, const char *t);
\r
55 int strendswith(const char *s, const char *t);
\r
57 void base64_encode_atom(unsigned char *data, int n, char *out);
\r
58 int base64_decode_atom(char *atom, unsigned char *out);
\r
60 struct bufchain_granule;
\r
61 typedef struct bufchain_tag {
\r
62 struct bufchain_granule *head, *tail;
\r
63 int buffersize; /* current amount of buffered data */
\r
66 void bufchain_init(bufchain *ch);
\r
67 void bufchain_clear(bufchain *ch);
\r
68 int bufchain_size(bufchain *ch);
\r
69 void bufchain_add(bufchain *ch, const void *data, int len);
\r
70 void bufchain_prefix(bufchain *ch, void **data, int *len);
\r
71 void bufchain_consume(bufchain *ch, int len);
\r
72 void bufchain_fetch(bufchain *ch, void *data, int len);
\r
74 int validate_manual_hostkey(char *key);
\r
76 struct tm ltime(void);
\r
78 /* Wipe sensitive data out of memory that's about to be freed. Simpler
\r
79 * than memset because we don't need the fill char parameter; also
\r
80 * attempts (by fiddly use of volatile) to inhibit the compiler from
\r
81 * over-cleverly trying to optimise the memset away because it knows
\r
82 * the variable is going out of scope. */
\r
83 void smemclr(void *b, size_t len);
\r
85 /* Compare two fixed-length chunks of memory for equality, without
\r
86 * data-dependent control flow (so an attacker with a very accurate
\r
87 * stopwatch can't try to guess where the first mismatching byte was).
\r
88 * Returns 0 for mismatch or 1 for equality (unlike memcmp), hinted at
\r
89 * by the 'eq' in the name. */
\r
90 int smemeq(const void *av, const void *bv, size_t len);
\r
93 * Debugging functions.
\r
95 * Output goes to debug.log
\r
97 * debug(()) (note the double brackets) is like printf().
\r
99 * dmemdump() and dmemdumpl() both do memory dumps. The difference
\r
100 * is that dmemdumpl() is more suited for when the memory address is
\r
101 * important (say because you'll be recording pointer values later
\r
102 * on). dmemdump() is more concise.
\r
106 void debug_printf(char *fmt, ...);
\r
107 void debug_memdump(void *buf, int len, int L);
\r
108 #define debug(x) (debug_printf x)
\r
109 #define dmemdump(buf,len) debug_memdump (buf, len, 0);
\r
110 #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
\r
113 #define dmemdump(buf,len)
\r
114 #define dmemdumpl(buf,len)
\r
118 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
\r
122 #define min(x,y) ( (x) < (y) ? (x) : (y) )
\r
125 #define max(x,y) ( (x) > (y) ? (x) : (y) )
\r
128 #define GET_32BIT_LSB_FIRST(cp) \
\r
129 (((unsigned long)(unsigned char)(cp)[0]) | \
\r
130 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
\r
131 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
\r
132 ((unsigned long)(unsigned char)(cp)[3] << 24))
\r
134 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
\r
135 (cp)[0] = (unsigned char)(value), \
\r
136 (cp)[1] = (unsigned char)((value) >> 8), \
\r
137 (cp)[2] = (unsigned char)((value) >> 16), \
\r
138 (cp)[3] = (unsigned char)((value) >> 24) )
\r
140 #define GET_16BIT_LSB_FIRST(cp) \
\r
141 (((unsigned long)(unsigned char)(cp)[0]) | \
\r
142 ((unsigned long)(unsigned char)(cp)[1] << 8))
\r
144 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
\r
145 (cp)[0] = (unsigned char)(value), \
\r
146 (cp)[1] = (unsigned char)((value) >> 8) )
\r
148 #define GET_32BIT_MSB_FIRST(cp) \
\r
149 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
\r
150 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
\r
151 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
\r
152 ((unsigned long)(unsigned char)(cp)[3]))
\r
154 #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
\r
156 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
\r
157 (cp)[0] = (unsigned char)((value) >> 24), \
\r
158 (cp)[1] = (unsigned char)((value) >> 16), \
\r
159 (cp)[2] = (unsigned char)((value) >> 8), \
\r
160 (cp)[3] = (unsigned char)(value) )
\r
162 #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
\r
164 #define GET_16BIT_MSB_FIRST(cp) \
\r
165 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
\r
166 ((unsigned long)(unsigned char)(cp)[1]))
\r
168 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
\r
169 (cp)[0] = (unsigned char)((value) >> 8), \
\r
170 (cp)[1] = (unsigned char)(value) )
\r
172 /* Replace NULL with the empty string, permitting an idiom in which we
\r
173 * get a string (pointer,length) pair that might be NULL,0 and can
\r
174 * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
\r
175 #define NULLTOEMPTY(s) ((s)?(s):"")
\r