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
41 typedef struct strbuf strbuf;
\r
42 strbuf *strbuf_new(void);
\r
43 void strbuf_free(strbuf *buf);
\r
44 char *strbuf_str(strbuf *buf); /* does not free buf */
\r
45 char *strbuf_to_str(strbuf *buf); /* does free buf, but you must free result */
\r
46 void strbuf_catf(strbuf *buf, const char *fmt, ...);
\r
47 void strbuf_catfv(strbuf *buf, const char *fmt, va_list ap);
\r
49 /* String-to-Unicode converters that auto-allocate the destination and
\r
50 * work around the rather deficient interface of mb_to_wc.
\r
52 * These actually live in miscucs.c, not misc.c (the distinction being
\r
53 * that the former is only linked into tools that also have the main
\r
54 * Unicode support). */
\r
55 wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
\r
56 wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
\r
58 int toint(unsigned);
\r
60 char *fgetline(FILE *fp);
\r
61 char *chomp(char *str);
\r
62 int strstartswith(const char *s, const char *t);
\r
63 int strendswith(const char *s, const char *t);
\r
65 void base64_encode_atom(const unsigned char *data, int n, char *out);
\r
66 int base64_decode_atom(const char *atom, unsigned char *out);
\r
68 struct bufchain_granule;
\r
69 struct bufchain_tag {
\r
70 struct bufchain_granule *head, *tail;
\r
71 int buffersize; /* current amount of buffered data */
\r
73 #ifndef BUFCHAIN_TYPEDEF
\r
74 typedef struct bufchain_tag bufchain; /* rest of declaration in misc.c */
\r
75 #define BUFCHAIN_TYPEDEF
\r
78 void bufchain_init(bufchain *ch);
\r
79 void bufchain_clear(bufchain *ch);
\r
80 int bufchain_size(bufchain *ch);
\r
81 void bufchain_add(bufchain *ch, const void *data, int len);
\r
82 void bufchain_prefix(bufchain *ch, void **data, int *len);
\r
83 void bufchain_consume(bufchain *ch, int len);
\r
84 void bufchain_fetch(bufchain *ch, void *data, int len);
\r
86 int validate_manual_hostkey(char *key);
\r
88 struct tm ltime(void);
\r
90 /* Wipe sensitive data out of memory that's about to be freed. Simpler
\r
91 * than memset because we don't need the fill char parameter; also
\r
92 * attempts (by fiddly use of volatile) to inhibit the compiler from
\r
93 * over-cleverly trying to optimise the memset away because it knows
\r
94 * the variable is going out of scope. */
\r
95 void smemclr(void *b, size_t len);
\r
97 /* Compare two fixed-length chunks of memory for equality, without
\r
98 * data-dependent control flow (so an attacker with a very accurate
\r
99 * stopwatch can't try to guess where the first mismatching byte was).
\r
100 * Returns 0 for mismatch or 1 for equality (unlike memcmp), hinted at
\r
101 * by the 'eq' in the name. */
\r
102 int smemeq(const void *av, const void *bv, size_t len);
\r
104 /* Extracts an SSH-marshalled string from the start of *data. If
\r
105 * successful (*datalen is not too small), advances data/datalen past
\r
106 * the string and returns a pointer to the string itself and its
\r
107 * length in *stringlen. Otherwise does nothing and returns NULL.
\r
109 * Like strchr, this function can discard const from its parameter.
\r
110 * Treat it as if it was a family of two functions, one returning a
\r
111 * non-const string given a non-const pointer, and one taking and
\r
112 * returning const. */
\r
113 void *get_ssh_string(int *datalen, const void **data, int *stringlen);
\r
114 /* Extracts an SSH uint32, similarly. Returns TRUE on success, and
\r
115 * leaves the extracted value in *ret. */
\r
116 int get_ssh_uint32(int *datalen, const void **data, unsigned *ret);
\r
117 /* Given a not-necessarily-zero-terminated string in (length,data)
\r
118 * form, check if it equals an ordinary C zero-terminated string. */
\r
119 int match_ssh_id(int stringlen, const void *string, const char *id);
\r
121 char *buildinfo(const char *newline);
\r
124 * Debugging functions.
\r
126 * Output goes to debug.log
\r
128 * debug(()) (note the double brackets) is like printf().
\r
130 * dmemdump() and dmemdumpl() both do memory dumps. The difference
\r
131 * is that dmemdumpl() is more suited for when the memory address is
\r
132 * important (say because you'll be recording pointer values later
\r
133 * on). dmemdump() is more concise.
\r
137 void debug_printf(const char *fmt, ...);
\r
138 void debug_memdump(const void *buf, int len, int L);
\r
139 #define debug(x) (debug_printf x)
\r
140 #define dmemdump(buf,len) debug_memdump (buf, len, 0);
\r
141 #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
\r
144 #define dmemdump(buf,len)
\r
145 #define dmemdumpl(buf,len)
\r
149 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
\r
153 #define min(x,y) ( (x) < (y) ? (x) : (y) )
\r
156 #define max(x,y) ( (x) > (y) ? (x) : (y) )
\r
159 #define GET_32BIT_LSB_FIRST(cp) \
\r
160 (((unsigned long)(unsigned char)(cp)[0]) | \
\r
161 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
\r
162 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
\r
163 ((unsigned long)(unsigned char)(cp)[3] << 24))
\r
165 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
\r
166 (cp)[0] = (unsigned char)(value), \
\r
167 (cp)[1] = (unsigned char)((value) >> 8), \
\r
168 (cp)[2] = (unsigned char)((value) >> 16), \
\r
169 (cp)[3] = (unsigned char)((value) >> 24) )
\r
171 #define GET_16BIT_LSB_FIRST(cp) \
\r
172 (((unsigned long)(unsigned char)(cp)[0]) | \
\r
173 ((unsigned long)(unsigned char)(cp)[1] << 8))
\r
175 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
\r
176 (cp)[0] = (unsigned char)(value), \
\r
177 (cp)[1] = (unsigned char)((value) >> 8) )
\r
179 #define GET_32BIT_MSB_FIRST(cp) \
\r
180 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
\r
181 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
\r
182 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
\r
183 ((unsigned long)(unsigned char)(cp)[3]))
\r
185 #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
\r
187 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
\r
188 (cp)[0] = (unsigned char)((value) >> 24), \
\r
189 (cp)[1] = (unsigned char)((value) >> 16), \
\r
190 (cp)[2] = (unsigned char)((value) >> 8), \
\r
191 (cp)[3] = (unsigned char)(value) )
\r
193 #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
\r
195 #define GET_16BIT_MSB_FIRST(cp) \
\r
196 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
\r
197 ((unsigned long)(unsigned char)(cp)[1]))
\r
199 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
\r
200 (cp)[0] = (unsigned char)((value) >> 8), \
\r
201 (cp)[1] = (unsigned char)(value) )
\r
203 /* Replace NULL with the empty string, permitting an idiom in which we
\r
204 * get a string (pointer,length) pair that might be NULL,0 and can
\r
205 * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
\r
206 #define NULLTOEMPTY(s) ((s)?(s):"")
\r