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