summaryrefslogtreecommitdiff
path: root/lib/str.h
blob: a9cb48fd6ec293f5ecd386a01ec7dbf0248541c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
 * Copyright (C) 2016-2017 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

#ifndef GNUTLS_STR_H
#define GNUTLS_STR_H

#include <config.h>
#include "gnutls_int.h"
#include "errors.h"
#include <datum.h>
#include <c-ctype.h>
#include "errors.h"

#ifdef HAVE_DCGETTEXT
# include "gettext.h"
# define _(String) dgettext (PACKAGE, String)
# define N_(String) gettext_noop (String)
#else
# define _(String) String
# define N_(String) String
#endif

int gnutls_utf8_password_normalize(const uint8_t *password, unsigned password_len,
				   gnutls_datum_t *out, unsigned flags);

#define _gnutls_utf8_password_normalize(p, plen, out, ignore_errs) \
	gnutls_utf8_password_normalize((unsigned char*)p, plen, out, \
		ignore_errs?(GNUTLS_UTF8_IGNORE_ERRS):0)

int _gnutls_idna_email_map(const char *input, unsigned ilen, gnutls_datum_t *output);
int _gnutls_idna_email_reverse_map(const char *input, unsigned ilen, gnutls_datum_t *output);

inline static unsigned _gnutls_str_is_print(const char *str, unsigned size)
{
	unsigned i;
	for (i=0;i<size;i++) {
		if (!c_isprint(str[i]))
			return 0;
	}
	return 1;
}

inline static unsigned _gnutls_dnsname_is_valid(const char *str, unsigned size)
{
	unsigned i;
	for (i=0;i<size;i++) {
		if (!(c_isalnum(str[i]) || str[i] == '-' || str[i] == '.'))
			return 0;
	}
	return 1;
}

void _gnutls_str_cpy(char *dest, size_t dest_tot_size, const char *src);
void _gnutls_str_cat(char *dest, size_t dest_tot_size, const char *src);

typedef struct gnutls_buffer_st {
	uint8_t *allocd;	/* pointer to allocated data */
	uint8_t *data;		/* API: pointer to data to copy from */
	size_t max_length;
	size_t length;		/* API: current length */
} gnutls_buffer_st;

/* Initialize a buffer */
void _gnutls_buffer_init(gnutls_buffer_st *);

/* Free the data in a buffer */
void _gnutls_buffer_clear(gnutls_buffer_st *);

/* Set the buffer data to be of zero length */
inline static void _gnutls_buffer_reset(gnutls_buffer_st * buf)
{
	buf->data = buf->allocd;
	buf->length = 0;
}

int _gnutls_buffer_resize(gnutls_buffer_st *, size_t new_size);

int _gnutls_buffer_append_str(gnutls_buffer_st *, const char *str);

#define _gnutls_buffer_append_data gnutls_buffer_append_data

#include <num.h>

int _gnutls_buffer_append_prefix(gnutls_buffer_st * buf, int pfx_size,
				 size_t data_size);

int _gnutls_buffer_append_mpi(gnutls_buffer_st * buf, int pfx_size,
			      bigint_t, int lz);

int _gnutls_buffer_append_fixed_mpi(gnutls_buffer_st * buf,
				    bigint_t mpi, unsigned size);

int _gnutls_buffer_append_data_prefix(gnutls_buffer_st * buf, int pfx_size,
				      const void *data, size_t data_size);
int _gnutls_buffer_pop_data(gnutls_buffer_st *, void *, size_t size);
void _gnutls_buffer_pop_datum(gnutls_buffer_st *, gnutls_datum_t *,
			      size_t max_size);

int _gnutls_buffer_pop_prefix8(gnutls_buffer_st *, uint8_t *, int check);

/* 32-bit prefix */
int _gnutls_buffer_pop_prefix32(gnutls_buffer_st * buf, size_t * data_size,
			        int check);

int _gnutls_buffer_pop_prefix24(gnutls_buffer_st * buf, size_t * data_size,
			        int check);

/* 32-bit prefix */
int _gnutls_buffer_pop_datum_prefix32(gnutls_buffer_st * buf,
				      gnutls_datum_t * data);

/* 16-bit prefix */
int _gnutls_buffer_pop_datum_prefix16(gnutls_buffer_st * buf,
				      gnutls_datum_t * data);

/* 8-bit prefix */
int _gnutls_buffer_pop_datum_prefix8(gnutls_buffer_st * buf,
				     gnutls_datum_t * data);

int _gnutls_buffer_to_datum(gnutls_buffer_st * str, gnutls_datum_t * data, unsigned is_str);

inline static
void _gnutls_ro_buffer_from_datum(gnutls_buffer_st * str, gnutls_datum_t * data)
{
	_gnutls_buffer_init(str);
	str->length = data->size;
	str->max_length = data->size;
	str->data = data->data;
}

int
_gnutls_buffer_append_escape(gnutls_buffer_st * dest, const void *data,
			     size_t data_size, const char *invalid_chars);
int _gnutls_buffer_unescape(gnutls_buffer_st * dest);

#ifndef __attribute__
/* This feature is available in gcc versions 2.5 and later.  */
#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
#define __attribute__(Spec)	/* empty */
#endif
#endif

int _gnutls_buffer_append_printf(gnutls_buffer_st * dest, const char *fmt,
				 ...)
    __attribute__ ((format(printf, 2, 3)));

void _gnutls_buffer_hexprint(gnutls_buffer_st * str,
			     const void *data, size_t len);
int _gnutls_buffer_base64print(gnutls_buffer_st * str,
			        const void *data, size_t len);
void _gnutls_buffer_hexdump(gnutls_buffer_st * str, const void *data,
			    size_t len, const char *spc);
void _gnutls_buffer_asciiprint(gnutls_buffer_st * str,
			       const char *data, size_t len);

char *_gnutls_bin2hex(const void *old, size_t oldlen, char *buffer,
		      size_t buffer_size, const char *separator);
int _gnutls_hex2bin(const char *hex_data, size_t hex_size,
		    uint8_t * bin_data, size_t * bin_size);

int _gnutls_hostname_compare(const char *certname, size_t certnamesize,
			     const char *hostname, unsigned vflags);

#define MAX_CN 256
#define MAX_DN 1024

#define BUFFER_APPEND(b, x, s) { \
	ret = _gnutls_buffer_append_data(b, x, s); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
    }

/* append data prefixed with 4-bytes length field*/
#define BUFFER_APPEND_PFX4(b, x, s) { \
	ret = _gnutls_buffer_append_data_prefix(b, 32, x, s); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
    }

#define BUFFER_APPEND_PFX3(b, x, s) { \
	ret = _gnutls_buffer_append_data_prefix(b, 24, x, s); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
    }

#define BUFFER_APPEND_PFX2(b, x, s) { \
	ret = _gnutls_buffer_append_data_prefix(b, 16, x, s); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
    }

#define BUFFER_APPEND_PFX1(b, x, s) { \
	ret = _gnutls_buffer_append_data_prefix(b, 8, x, s); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
    }

#define BUFFER_APPEND_NUM(b, s) { \
	ret = _gnutls_buffer_append_prefix(b, 32, s); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
    }

#define BUFFER_APPEND_TS(b, s) { \
	ret = _gnutls_buffer_append_prefix(b, 32, (uint64_t) s.tv_sec >> 32); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
	ret = _gnutls_buffer_append_prefix(b, 32, s.tv_sec & 0xFFFFFFFF); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
	ret = _gnutls_buffer_append_prefix(b, 32, s.tv_nsec); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    return ret; \
	} \
    }

#define BUFFER_POP(b, x, s) { \
	ret = _gnutls_buffer_pop_data(b, x, s); \
	if (ret < 0) { \
	    ret = GNUTLS_E_PARSING_ERROR; \
	    gnutls_assert(); \
	    goto error; \
	} \
    }

#define BUFFER_POP_DATUM(b, o) { \
	gnutls_datum_t d; \
	ret = _gnutls_buffer_pop_datum_prefix32(b, &d); \
	if (ret >= 0) \
	    ret = _gnutls_set_datum (o, d.data, d.size); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    goto error; \
	} \
    }

#define BUFFER_POP_NUM(b, o) { \
	size_t s; \
	ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    goto error; \
	} \
	o = s; \
    }

#define BUFFER_POP_CAST_NUM(b, o) { \
	size_t s; \
	ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    goto error; \
	} \
	o = (void *) (intptr_t)(s); \
    }

#define BUFFER_POP_TS(b, o) { \
	size_t s; \
	uint64_t v; \
	ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    goto error; \
	} \
	v = s; \
	ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    goto error; \
	} \
	v = (v << 32) | s; \
	ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
	if (ret < 0) { \
	    gnutls_assert(); \
	    goto error; \
	} \
	o.tv_sec = v; \
	o.tv_nsec = s; \
    }

#endif