]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - misc.h
Merge branch 'pre-0.65'
[PuTTY.git] / misc.h
1 /*
2  * Header for misc.c.
3  */
4
5 #ifndef PUTTY_MISC_H
6 #define PUTTY_MISC_H
7
8 #include "puttymem.h"
9
10 #include <stdio.h>                     /* for FILE * */
11 #include <stdarg.h>                    /* for va_list */
12 #include <time.h>                      /* for struct tm */
13
14 #ifndef FALSE
15 #define FALSE 0
16 #endif
17 #ifndef TRUE
18 #define TRUE 1
19 #endif
20
21 typedef struct Filename Filename;
22 typedef struct FontSpec FontSpec;
23
24 unsigned long parse_blocksize(const char *bs);
25 char ctrlparse(char *s, char **next);
26
27 size_t host_strcspn(const char *s, const char *set);
28 char *host_strchr(const char *s, int c);
29 char *host_strrchr(const char *s, int c);
30 char *host_strduptrim(const char *s);
31
32 char *dupstr(const char *s);
33 char *dupcat(const char *s1, ...);
34 char *dupprintf(const char *fmt, ...)
35 #ifdef __GNUC__
36     __attribute__ ((format (printf, 1, 2)))
37 #endif
38     ;
39 char *dupvprintf(const char *fmt, va_list ap);
40 void burnstr(char *string);
41
42 int toint(unsigned);
43
44 char *fgetline(FILE *fp);
45 char *chomp(char *str);
46
47 void base64_encode_atom(const unsigned char *data, int n, char *out);
48 int base64_decode_atom(const char *atom, unsigned char *out);
49
50 struct bufchain_granule;
51 typedef struct bufchain_tag {
52     struct bufchain_granule *head, *tail;
53     int buffersize;                    /* current amount of buffered data */
54 } bufchain;
55
56 void bufchain_init(bufchain *ch);
57 void bufchain_clear(bufchain *ch);
58 int bufchain_size(bufchain *ch);
59 void bufchain_add(bufchain *ch, const void *data, int len);
60 void bufchain_prefix(bufchain *ch, void **data, int *len);
61 void bufchain_consume(bufchain *ch, int len);
62 void bufchain_fetch(bufchain *ch, void *data, int len);
63
64 int validate_manual_hostkey(char *key);
65
66 struct tm ltime(void);
67
68 /* Wipe sensitive data out of memory that's about to be freed. Simpler
69  * than memset because we don't need the fill char parameter; also
70  * attempts (by fiddly use of volatile) to inhibit the compiler from
71  * over-cleverly trying to optimise the memset away because it knows
72  * the variable is going out of scope. */
73 void smemclr(void *b, size_t len);
74
75 /* Compare two fixed-length chunks of memory for equality, without
76  * data-dependent control flow (so an attacker with a very accurate
77  * stopwatch can't try to guess where the first mismatching byte was).
78  * Returns 0 for mismatch or 1 for equality (unlike memcmp), hinted at
79  * by the 'eq' in the name. */
80 int smemeq(const void *av, const void *bv, size_t len);
81
82 /* Extracts an SSH-marshalled string from the start of *data. If
83  * successful (*datalen is not too small), advances data/datalen past
84  * the string and returns a pointer to the string itself and its
85  * length in *stringlen. Otherwise does nothing and returns NULL.
86  *
87  * Like strchr, this function can discard const from its parameter.
88  * Treat it as if it was a family of two functions, one returning a
89  * non-const string given a non-const pointer, and one taking and
90  * returning const. */
91 void *get_ssh_string(int *datalen, const void **data, int *stringlen);
92 /* Extracts an SSH uint32, similarly. Returns TRUE on success, and
93  * leaves the extracted value in *ret. */
94 int get_ssh_uint32(int *datalen, const void **data, unsigned *ret);
95 /* Given a not-necessarily-zero-terminated string in (length,data)
96  * form, check if it equals an ordinary C zero-terminated string. */
97 int match_ssh_id(int stringlen, const void *string, const char *id);
98
99 /*
100  * Debugging functions.
101  *
102  * Output goes to debug.log
103  *
104  * debug(()) (note the double brackets) is like printf().
105  *
106  * dmemdump() and dmemdumpl() both do memory dumps.  The difference
107  * is that dmemdumpl() is more suited for when the memory address is
108  * important (say because you'll be recording pointer values later
109  * on).  dmemdump() is more concise.
110  */
111
112 #ifdef DEBUG
113 void debug_printf(const char *fmt, ...);
114 void debug_memdump(const void *buf, int len, int L);
115 #define debug(x) (debug_printf x)
116 #define dmemdump(buf,len) debug_memdump (buf, len, 0);
117 #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
118 #else
119 #define debug(x)
120 #define dmemdump(buf,len)
121 #define dmemdumpl(buf,len)
122 #endif
123
124 #ifndef lenof
125 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
126 #endif
127
128 #ifndef min
129 #define min(x,y) ( (x) < (y) ? (x) : (y) )
130 #endif
131 #ifndef max
132 #define max(x,y) ( (x) > (y) ? (x) : (y) )
133 #endif
134
135 #define GET_32BIT_LSB_FIRST(cp) \
136   (((unsigned long)(unsigned char)(cp)[0]) | \
137   ((unsigned long)(unsigned char)(cp)[1] << 8) | \
138   ((unsigned long)(unsigned char)(cp)[2] << 16) | \
139   ((unsigned long)(unsigned char)(cp)[3] << 24))
140
141 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
142   (cp)[0] = (unsigned char)(value), \
143   (cp)[1] = (unsigned char)((value) >> 8), \
144   (cp)[2] = (unsigned char)((value) >> 16), \
145   (cp)[3] = (unsigned char)((value) >> 24) )
146
147 #define GET_16BIT_LSB_FIRST(cp) \
148   (((unsigned long)(unsigned char)(cp)[0]) | \
149   ((unsigned long)(unsigned char)(cp)[1] << 8))
150
151 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
152   (cp)[0] = (unsigned char)(value), \
153   (cp)[1] = (unsigned char)((value) >> 8) )
154
155 #define GET_32BIT_MSB_FIRST(cp) \
156   (((unsigned long)(unsigned char)(cp)[0] << 24) | \
157   ((unsigned long)(unsigned char)(cp)[1] << 16) | \
158   ((unsigned long)(unsigned char)(cp)[2] << 8) | \
159   ((unsigned long)(unsigned char)(cp)[3]))
160
161 #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
162
163 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
164   (cp)[0] = (unsigned char)((value) >> 24), \
165   (cp)[1] = (unsigned char)((value) >> 16), \
166   (cp)[2] = (unsigned char)((value) >> 8), \
167   (cp)[3] = (unsigned char)(value) )
168
169 #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
170
171 #define GET_16BIT_MSB_FIRST(cp) \
172   (((unsigned long)(unsigned char)(cp)[0] << 8) | \
173   ((unsigned long)(unsigned char)(cp)[1]))
174
175 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
176   (cp)[0] = (unsigned char)((value) >> 8), \
177   (cp)[1] = (unsigned char)(value) )
178
179 #endif