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