]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - misc.h
New centralised helper function dup_mb_to_wc().
[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
55 void base64_encode_atom(unsigned char *data, int n, char *out);
56 int base64_decode_atom(char *atom, unsigned char *out);
57
58 struct bufchain_granule;
59 typedef struct bufchain_tag {
60     struct bufchain_granule *head, *tail;
61     int buffersize;                    /* current amount of buffered data */
62 } bufchain;
63
64 void bufchain_init(bufchain *ch);
65 void bufchain_clear(bufchain *ch);
66 int bufchain_size(bufchain *ch);
67 void bufchain_add(bufchain *ch, const void *data, int len);
68 void bufchain_prefix(bufchain *ch, void **data, int *len);
69 void bufchain_consume(bufchain *ch, int len);
70 void bufchain_fetch(bufchain *ch, void *data, int len);
71
72 int validate_manual_hostkey(char *key);
73
74 struct tm ltime(void);
75
76 /* Wipe sensitive data out of memory that's about to be freed. Simpler
77  * than memset because we don't need the fill char parameter; also
78  * attempts (by fiddly use of volatile) to inhibit the compiler from
79  * over-cleverly trying to optimise the memset away because it knows
80  * the variable is going out of scope. */
81 void smemclr(void *b, size_t len);
82
83 /* Compare two fixed-length chunks of memory for equality, without
84  * data-dependent control flow (so an attacker with a very accurate
85  * stopwatch can't try to guess where the first mismatching byte was).
86  * Returns 0 for mismatch or 1 for equality (unlike memcmp), hinted at
87  * by the 'eq' in the name. */
88 int smemeq(const void *av, const void *bv, size_t len);
89
90 /*
91  * Debugging functions.
92  *
93  * Output goes to debug.log
94  *
95  * debug(()) (note the double brackets) is like printf().
96  *
97  * dmemdump() and dmemdumpl() both do memory dumps.  The difference
98  * is that dmemdumpl() is more suited for when the memory address is
99  * important (say because you'll be recording pointer values later
100  * on).  dmemdump() is more concise.
101  */
102
103 #ifdef DEBUG
104 void debug_printf(char *fmt, ...);
105 void debug_memdump(void *buf, int len, int L);
106 #define debug(x) (debug_printf x)
107 #define dmemdump(buf,len) debug_memdump (buf, len, 0);
108 #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
109 #else
110 #define debug(x)
111 #define dmemdump(buf,len)
112 #define dmemdumpl(buf,len)
113 #endif
114
115 #ifndef lenof
116 #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
117 #endif
118
119 #ifndef min
120 #define min(x,y) ( (x) < (y) ? (x) : (y) )
121 #endif
122 #ifndef max
123 #define max(x,y) ( (x) > (y) ? (x) : (y) )
124 #endif
125
126 #define GET_32BIT_LSB_FIRST(cp) \
127   (((unsigned long)(unsigned char)(cp)[0]) | \
128   ((unsigned long)(unsigned char)(cp)[1] << 8) | \
129   ((unsigned long)(unsigned char)(cp)[2] << 16) | \
130   ((unsigned long)(unsigned char)(cp)[3] << 24))
131
132 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
133   (cp)[0] = (unsigned char)(value), \
134   (cp)[1] = (unsigned char)((value) >> 8), \
135   (cp)[2] = (unsigned char)((value) >> 16), \
136   (cp)[3] = (unsigned char)((value) >> 24) )
137
138 #define GET_16BIT_LSB_FIRST(cp) \
139   (((unsigned long)(unsigned char)(cp)[0]) | \
140   ((unsigned long)(unsigned char)(cp)[1] << 8))
141
142 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
143   (cp)[0] = (unsigned char)(value), \
144   (cp)[1] = (unsigned char)((value) >> 8) )
145
146 #define GET_32BIT_MSB_FIRST(cp) \
147   (((unsigned long)(unsigned char)(cp)[0] << 24) | \
148   ((unsigned long)(unsigned char)(cp)[1] << 16) | \
149   ((unsigned long)(unsigned char)(cp)[2] << 8) | \
150   ((unsigned long)(unsigned char)(cp)[3]))
151
152 #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
153
154 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
155   (cp)[0] = (unsigned char)((value) >> 24), \
156   (cp)[1] = (unsigned char)((value) >> 16), \
157   (cp)[2] = (unsigned char)((value) >> 8), \
158   (cp)[3] = (unsigned char)(value) )
159
160 #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
161
162 #define GET_16BIT_MSB_FIRST(cp) \
163   (((unsigned long)(unsigned char)(cp)[0] << 8) | \
164   ((unsigned long)(unsigned char)(cp)[1]))
165
166 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
167   (cp)[0] = (unsigned char)((value) >> 8), \
168   (cp)[1] = (unsigned char)(value) )
169
170 #endif