corrected copyright notices
[gnutls.git] / lib / gnutls_mbuffers.h
blob855814c57d533019378ec21d7bcca772fa9f4a1d
1 /*
2 * Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 * Author: Jonathan Bastien-Filiatrault
6 * This file is part of GNUTLS.
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 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_MBUFFERS_H
24 #define GNUTLS_MBUFFERS_H
26 #include <gnutls_int.h>
27 #include <gnutls_errors.h>
29 void _mbuffer_head_init (mbuffer_head_st * buf);
30 void _mbuffer_head_clear (mbuffer_head_st * buf);
31 void _mbuffer_enqueue (mbuffer_head_st * buf, mbuffer_st * bufel);
32 mbuffer_st* _mbuffer_dequeue (mbuffer_head_st * buf, mbuffer_st * bufel);
33 int _mbuffer_head_remove_bytes (mbuffer_head_st * buf, size_t bytes);
34 mbuffer_st *_mbuffer_alloc (size_t payload_size, size_t maximum_size);
36 mbuffer_st *_mbuffer_head_get_first (mbuffer_head_st * buf, gnutls_datum_t * msg);
37 mbuffer_st *_mbuffer_head_get_next (mbuffer_st * cur, gnutls_datum_t * msg);
39 mbuffer_st *
40 _mbuffer_head_pop_first (mbuffer_head_st * buf);
42 /* This is dangerous since it will replace bufel with a new
43 * one.
45 int _mbuffer_append_data (mbuffer_st * bufel, void *newdata,
46 size_t newdata_size);
47 int _mbuffer_linearize (mbuffer_head_st * buf);
50 /* For "user" use. One can have buffer data and header.
53 inline static void
54 _mbuffer_set_udata (mbuffer_st * bufel, void *data, size_t data_size)
56 memcpy (bufel->msg.data + bufel->mark + bufel->user_mark, data, data_size);
57 bufel->msg.size = data_size + bufel->user_mark + bufel->mark;
60 inline static void *
61 _mbuffer_get_uhead_ptr (mbuffer_st * bufel)
63 return bufel->msg.data + bufel->mark;
66 inline static void *
67 _mbuffer_get_udata_ptr (mbuffer_st * bufel)
69 return bufel->msg.data + bufel->user_mark + bufel->mark;
72 inline static void
73 _mbuffer_set_udata_size (mbuffer_st * bufel, size_t size)
75 bufel->msg.size = size + bufel->user_mark + bufel->mark;
78 inline static size_t
79 _mbuffer_get_udata_size (mbuffer_st * bufel)
81 return bufel->msg.size - bufel->user_mark - bufel->mark;
84 /* discards size bytes from the begging of the buffer */
85 inline static void
86 _mbuffer_consume (mbuffer_head_st* buf, mbuffer_st * bufel, size_t size)
88 bufel->user_mark = 0;
89 if (bufel->mark+size < bufel->msg.size)
90 bufel->mark += size;
91 else
92 bufel->mark = bufel->msg.size;
94 buf->byte_length -= size;
97 inline static size_t
98 _mbuffer_get_uhead_size (mbuffer_st * bufel)
100 return bufel->user_mark;
103 inline static void
104 _mbuffer_set_uhead_size (mbuffer_st * bufel, size_t size)
106 bufel->user_mark = size;
111 inline static mbuffer_st *
112 _gnutls_handshake_alloc (gnutls_session_t session, size_t size, size_t maximum)
114 mbuffer_st *ret = _mbuffer_alloc (HANDSHAKE_HEADER_SIZE(session) + size,
115 HANDSHAKE_HEADER_SIZE(session) + maximum);
117 if (!ret)
118 return NULL;
120 _mbuffer_set_uhead_size (ret, HANDSHAKE_HEADER_SIZE(session));
122 return ret;
125 /* Free a segment, if the pointer is not NULL
127 * We take a ** to detect and fix double free bugs (the dangling
128 * pointer case). It also makes sure the pointer has a known value
129 * after freeing.
131 inline static void
132 _mbuffer_xfree (mbuffer_st ** bufel)
134 if (*bufel)
135 gnutls_free (*bufel);
137 *bufel = NULL;
140 #endif