corrected copyright notices
[gnutls.git] / lib / gnutls_str.h
blobafd99cbc7d469344d5be4e3cdb7b2ba2497f1f7f
1 /*
2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #ifndef GNUTLS_STR_H
24 #define GNUTLS_STR_H
26 #include <gnutls_int.h>
27 #include <gnutls_datum.h>
28 #include "gettext.h"
29 #define _(String) dgettext (PACKAGE, String)
31 void _gnutls_str_cpy (char *dest, size_t dest_tot_size, const char *src);
32 void _gnutls_mem_cpy (char *dest, size_t dest_tot_size, const char *src,
33 size_t src_size);
34 void _gnutls_str_cat (char *dest, size_t dest_tot_size, const char *src);
36 typedef struct
38 uint8_t *allocd; /* pointer to allocated data */
39 uint8_t *data; /* API: pointer to data to copy from */
40 size_t max_length;
41 size_t length; /* API: current length */
42 } gnutls_buffer_st;
44 /* Initialize a buffer */
45 void _gnutls_buffer_init (gnutls_buffer_st *);
47 /* Free the data in a buffer */
48 void _gnutls_buffer_clear (gnutls_buffer_st *);
50 /* Set the buffer data to be of zero length */
51 inline static void _gnutls_buffer_reset (gnutls_buffer_st * buf)
53 buf->data = buf->allocd;
54 buf->length = 0;
57 int _gnutls_buffer_resize (gnutls_buffer_st *, size_t new_size);
59 int _gnutls_buffer_append_str (gnutls_buffer_st *, const char *str);
60 int _gnutls_buffer_append_data (gnutls_buffer_st *, const void *data,
61 size_t data_size);
63 #include <gnutls_num.h>
65 void _gnutls_buffer_replace_data( gnutls_buffer_st * buf, gnutls_datum_t * data);
67 int _gnutls_buffer_append_prefix (gnutls_buffer_st * buf, int pfx_size, size_t data_size);
69 int _gnutls_buffer_append_mpi (gnutls_buffer_st * buf, int pfx_size, bigint_t, int lz);
71 int _gnutls_buffer_append_data_prefix (gnutls_buffer_st * buf, int pfx_size,
72 const void *data, size_t data_size);
73 void _gnutls_buffer_pop_data (gnutls_buffer_st *, void *, size_t * size);
74 void _gnutls_buffer_pop_datum (gnutls_buffer_st *, gnutls_datum_t *,
75 size_t max_size);
77 int _gnutls_buffer_pop_prefix (gnutls_buffer_st * buf, size_t * data_size,
78 int check);
80 int _gnutls_buffer_pop_data_prefix (gnutls_buffer_st * buf, void *data,
81 size_t * data_size);
83 int _gnutls_buffer_pop_datum_prefix (gnutls_buffer_st * buf,
84 gnutls_datum_t * data);
85 int _gnutls_buffer_to_datum (gnutls_buffer_st * str, gnutls_datum_t * data);
87 int _gnutls_buffer_escape (gnutls_buffer_st * dest, int all,
88 const char *const invalid_chars);
89 int _gnutls_buffer_unescape (gnutls_buffer_st * dest);
91 #ifndef __attribute__
92 /* This feature is available in gcc versions 2.5 and later. */
93 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
94 #define __attribute__(Spec) /* empty */
95 #endif
96 #endif
98 int _gnutls_buffer_append_printf (gnutls_buffer_st * dest, const char *fmt,
99 ...)
100 __attribute__ ((format (printf, 2, 3)));
102 void _gnutls_buffer_hexprint (gnutls_buffer_st * str,
103 const void *data, size_t len);
104 void _gnutls_buffer_hexdump (gnutls_buffer_st * str, const void *data,
105 size_t len, const char *spc);
106 void _gnutls_buffer_asciiprint (gnutls_buffer_st * str,
107 const char *data, size_t len);
109 char *_gnutls_bin2hex (const void *old, size_t oldlen, char *buffer,
110 size_t buffer_size, const char *separator);
111 int _gnutls_hex2bin (const char * hex_data, size_t hex_size, uint8_t * bin_data,
112 size_t * bin_size);
114 int _gnutls_hostname_compare (const char *certname, size_t certnamesize,
115 const char *hostname, int level);
117 #define MAX_CN 256
118 #define MAX_DN 1024
120 #define BUFFER_APPEND(b, x, s) { \
121 ret = _gnutls_buffer_append_data(b, x, s); \
122 if (ret < 0) { \
123 gnutls_assert(); \
124 return ret; \
128 /* append data prefixed with 4-bytes length field*/
129 #define BUFFER_APPEND_PFX4(b, x, s) { \
130 ret = _gnutls_buffer_append_data_prefix(b, 32, x, s); \
131 if (ret < 0) { \
132 gnutls_assert(); \
133 return ret; \
137 #define BUFFER_APPEND_PFX3(b, x, s) { \
138 ret = _gnutls_buffer_append_data_prefix(b, 24, x, s); \
139 if (ret < 0) { \
140 gnutls_assert(); \
141 return ret; \
145 #define BUFFER_APPEND_PFX2(b, x, s) { \
146 ret = _gnutls_buffer_append_data_prefix(b, 16, x, s); \
147 if (ret < 0) { \
148 gnutls_assert(); \
149 return ret; \
153 #define BUFFER_APPEND_PFX1(b, x, s) { \
154 ret = _gnutls_buffer_append_data_prefix(b, 8, x, s); \
155 if (ret < 0) { \
156 gnutls_assert(); \
157 return ret; \
161 #define BUFFER_APPEND_NUM(b, s) { \
162 ret = _gnutls_buffer_append_prefix(b, 32, s); \
163 if (ret < 0) { \
164 gnutls_assert(); \
165 return ret; \
169 #define BUFFER_POP(b, x, s) { \
170 size_t is = s; \
171 _gnutls_buffer_pop_data(b, x, &is); \
172 if (is != s) { \
173 ret = GNUTLS_E_PARSING_ERROR; \
174 gnutls_assert(); \
175 goto error; \
179 #define BUFFER_POP_DATUM(b, o) { \
180 gnutls_datum_t d; \
181 ret = _gnutls_buffer_pop_datum_prefix(b, &d); \
182 if (ret >= 0) \
183 ret = _gnutls_set_datum (o, d.data, d.size); \
184 if (ret < 0) { \
185 gnutls_assert(); \
186 goto error; \
190 #define BUFFER_POP_NUM(b, o) { \
191 size_t s; \
192 ret = _gnutls_buffer_pop_prefix(b, &s, 0); \
193 if (ret < 0) { \
194 gnutls_assert(); \
195 goto error; \
197 o = s; \
200 #endif