source: filezilla/trunk/fuentes/src/putty/misc.h @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

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