Tizen 2.0 Release
[external/libgnutls26.git] / lib / gnutls_mbuffers.h
1 /*
2  * Copyright (C) 2009 Free Software Foundation
3  *
4  * Author: Jonathan Bastien-Filiatrault
5  *
6  * This file is part of GNUTLS.
7  *
8  * The GNUTLS library 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 2.1 of
11  * the License, or (at your option) any later version.
12  *
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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21  * USA
22  *
23  */
24
25 #ifndef GNUTLS_MBUFFERS_H
26 #define GNUTLS_MBUFFERS_H
27
28 #include <gnutls_int.h>
29 #include <gnutls_errors.h>
30
31 void _mbuffer_init (mbuffer_head_st * buf);
32 void _mbuffer_clear (mbuffer_head_st * buf);
33 void _mbuffer_enqueue (mbuffer_head_st * buf, mbuffer_st * bufel);
34 int _mbuffer_remove_bytes (mbuffer_head_st * buf, size_t bytes);
35 mbuffer_st *_mbuffer_alloc (size_t payload_size, size_t maximum_size);
36
37 mbuffer_st *_mbuffer_get_first (mbuffer_head_st * buf, gnutls_datum_t * msg);
38 mbuffer_st *_mbuffer_get_next (mbuffer_st * cur, gnutls_datum_t * msg);
39
40 /* This is dangerous since it will replace bufel with a new
41  * one.
42  */
43 int _mbuffer_append_data (mbuffer_st * bufel, void *newdata,
44                           size_t newdata_size);
45 int _mbuffer_linearize (mbuffer_head_st * buf);
46
47
48 /* For "user" use. One can have buffer data and header.
49  */
50
51 inline static void
52 _mbuffer_set_udata (mbuffer_st * bufel, void *data, size_t data_size)
53 {
54   memcpy (bufel->msg.data + bufel->user_mark, data, data_size);
55 }
56
57 inline static void *
58 _mbuffer_get_uhead_ptr (mbuffer_st * bufel)
59 {
60   return bufel->msg.data;
61 }
62
63 inline static void *
64 _mbuffer_get_udata_ptr (mbuffer_st * bufel)
65 {
66   return bufel->msg.data + bufel->user_mark;
67 }
68
69 inline static void
70 _mbuffer_set_udata_size (mbuffer_st * bufel, size_t size)
71 {
72   bufel->msg.size = size + bufel->user_mark;
73 }
74
75 inline static size_t
76 _mbuffer_get_udata_size (mbuffer_st * bufel)
77 {
78   return bufel->msg.size - bufel->user_mark;
79 }
80
81 inline static size_t
82 _mbuffer_get_uhead_size (mbuffer_st * bufel)
83 {
84   return bufel->user_mark;
85 }
86
87 inline static void
88 _mbuffer_set_uhead_size (mbuffer_st * bufel, size_t size)
89 {
90   bufel->user_mark = size;
91 }
92
93
94
95 inline static mbuffer_st *
96 _gnutls_handshake_alloc (size_t size, size_t maximum)
97 {
98   mbuffer_st *ret = _mbuffer_alloc (HANDSHAKE_HEADER_SIZE + size,
99                                     HANDSHAKE_HEADER_SIZE + maximum);
100
101   if (!ret)
102     return NULL;
103
104   _mbuffer_set_uhead_size (ret, HANDSHAKE_HEADER_SIZE);
105
106   return ret;
107 }
108
109 /* Free a segment, if the pointer is not NULL
110  *
111  * We take a ** to detect and fix double free bugs (the dangling
112  * pointer case). It also makes sure the pointer has a known value
113  * after freeing.
114  */
115 inline static void
116 _mbuffer_xfree (mbuffer_st ** bufel)
117 {
118   if (*bufel)
119     gnutls_free (*bufel);
120
121   *bufel = NULL;
122 }
123
124 #endif