Tizen 2.0 Release
[external/libgnutls26.git] / lib / gnutls_str.h
1 /*
2  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
3  * 2010 Free Software Foundation, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS.
8  *
9  * The GnuTLS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA
23  *
24  */
25
26 #ifndef GNUTLS_STR_H
27 #define GNUTLS_STR_H
28
29 #include <gnutls_int.h>
30 #include <gnutls_datum.h>
31
32 void _gnutls_str_cpy (char *dest, size_t dest_tot_size, const char *src);
33 void _gnutls_mem_cpy (char *dest, size_t dest_tot_size, const char *src,
34                       size_t src_size);
35 void _gnutls_str_cat (char *dest, size_t dest_tot_size, const char *src);
36
37 typedef struct
38 {
39   opaque *allocd;               /* pointer to allocated data */
40   opaque *data;                 /* API: pointer to data to copy from */
41   size_t max_length;
42   size_t length;                /* API: current length */
43 } gnutls_buffer_st;
44
45 void _gnutls_buffer_init (gnutls_buffer_st *);
46 void _gnutls_buffer_clear (gnutls_buffer_st *);
47 int _gnutls_buffer_resize (gnutls_buffer_st *, size_t new_size);
48
49 int _gnutls_buffer_append_str (gnutls_buffer_st *, const char *str);
50 int _gnutls_buffer_append_data (gnutls_buffer_st *, const void *data,
51                                 size_t data_size);
52
53 #include <gnutls_num.h>
54
55 int _gnutls_buffer_append_prefix (gnutls_buffer_st * buf, size_t data_size);
56
57 int _gnutls_buffer_append_data_prefix (gnutls_buffer_st * buf,
58                                        const void *data, size_t data_size);
59 void _gnutls_buffer_pop_data (gnutls_buffer_st *, void *, size_t * size);
60 void _gnutls_buffer_pop_datum (gnutls_buffer_st *, gnutls_datum_t *,
61                                size_t max_size);
62
63 int _gnutls_buffer_pop_prefix (gnutls_buffer_st * buf, size_t * data_size,
64                                int check);
65
66 int _gnutls_buffer_pop_data_prefix (gnutls_buffer_st * buf, void *data,
67                                     size_t * data_size);
68
69 int _gnutls_buffer_pop_datum_prefix (gnutls_buffer_st * buf,
70                                      gnutls_datum_t * data);
71 int _gnutls_buffer_to_datum (gnutls_buffer_st * str, gnutls_datum_t * data);
72
73 int _gnutls_buffer_escape (gnutls_buffer_st * dest, int all, 
74                            const char *const invalid_chars);
75 int _gnutls_buffer_unescape (gnutls_buffer_st * dest);
76
77 #ifndef __attribute__
78 /* This feature is available in gcc versions 2.5 and later.  */
79 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
80 #define __attribute__(Spec)     /* empty */
81 #endif
82 #endif
83
84 int _gnutls_buffer_append_printf (gnutls_buffer_st * dest, const char *fmt,
85                                   ...)
86   __attribute__ ((format (printf, 2, 3)));
87
88 char *_gnutls_bin2hex (const void *old, size_t oldlen, char *buffer,
89                        size_t buffer_size, const char *separator);
90 int _gnutls_hex2bin (const opaque * hex_data, int hex_size, opaque * bin_data,
91                      size_t * bin_size);
92
93 int _gnutls_hostname_compare (const char *certname, size_t certnamesize,
94                               const char *hostname, int level);
95 #define MAX_CN 256
96 #define MAX_DN 1024
97
98 #define BUFFER_APPEND(b, x, s) { \
99         ret = _gnutls_buffer_append_data(b, x, s); \
100         if (ret < 0) { \
101             gnutls_assert(); \
102             return ret; \
103         } \
104     }
105
106 #define BUFFER_APPEND_PFX(b, x, s) { \
107         ret = _gnutls_buffer_append_data_prefix(b, x, s); \
108         if (ret < 0) { \
109             gnutls_assert(); \
110             return ret; \
111         } \
112     }
113
114 #define BUFFER_APPEND_NUM(b, s) { \
115         ret = _gnutls_buffer_append_prefix(b, s); \
116         if (ret < 0) { \
117             gnutls_assert(); \
118             return ret; \
119         } \
120     }
121
122
123 #define BUFFER_POP(b, x, s) { \
124         size_t is = s; \
125         _gnutls_buffer_pop_data(b, x, &is); \
126         if (is != s) { \
127             ret = GNUTLS_E_PARSING_ERROR; \
128             gnutls_assert(); \
129             goto error; \
130         } \
131     }
132
133 #define BUFFER_POP_DATUM(b, o) { \
134         gnutls_datum_t d; \
135         ret = _gnutls_buffer_pop_datum_prefix(b, &d); \
136         if (ret >= 0) \
137             ret = _gnutls_set_datum (o, d.data, d.size); \
138         if (ret < 0) { \
139             gnutls_assert(); \
140             goto error; \
141         } \
142     }
143
144 #define BUFFER_POP_NUM(b, o) { \
145         size_t s; \
146         ret = _gnutls_buffer_pop_prefix(b, &s, 0); \
147         if (ret < 0) { \
148             gnutls_assert(); \
149             goto error; \
150         } \
151         o = s; \
152     }
153
154 #endif