summaryrefslogtreecommitdiff
path: root/lib/gnutls_mbuffers.h
blob: 99b0addf4b9451172c17e05ca4bb4b0d2300fa39 (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
/*
 * Copyright (C) 2009 Free Software Foundation
 *
 * Author: Jonathan Bastien-Filiatrault
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

#ifndef GNUTLS_MBUFFERS_H
#define GNUTLS_MBUFFERS_H

#include <gnutls_int.h>
#include <gnutls_errors.h>

void _mbuffer_init (mbuffer_head_st * buf);
void _mbuffer_clear (mbuffer_head_st * buf);
void _mbuffer_enqueue (mbuffer_head_st * buf, mbuffer_st * bufel);
int _mbuffer_remove_bytes (mbuffer_head_st * buf, size_t bytes);
mbuffer_st *_mbuffer_alloc (size_t payload_size, size_t maximum_size);

mbuffer_st *_mbuffer_get_first (mbuffer_head_st * buf, gnutls_datum_t * msg);
mbuffer_st *_mbuffer_get_next (mbuffer_st * cur, gnutls_datum_t * msg);

/* This is dangerous since it will replace bufel with a new
 * one.
 */
int _mbuffer_append_data (mbuffer_st * bufel, void *newdata,
                          size_t newdata_size);
int _mbuffer_linearize (mbuffer_head_st * buf);


/* For "user" use. One can have buffer data and header.
 */

inline static void
_mbuffer_set_udata (mbuffer_st * bufel, void *data, size_t data_size)
{
  memcpy (bufel->msg.data + bufel->user_mark, data, data_size);
}

inline static void *
_mbuffer_get_uhead_ptr (mbuffer_st * bufel)
{
  return bufel->msg.data;
}

inline static void *
_mbuffer_get_udata_ptr (mbuffer_st * bufel)
{
  return bufel->msg.data + bufel->user_mark;
}

inline static void
_mbuffer_set_udata_size (mbuffer_st * bufel, size_t size)
{
  bufel->msg.size = size + bufel->user_mark;
}

inline static size_t
_mbuffer_get_udata_size (mbuffer_st * bufel)
{
  return bufel->msg.size - bufel->user_mark;
}

inline static size_t
_mbuffer_get_uhead_size (mbuffer_st * bufel)
{
  return bufel->user_mark;
}

inline static void
_mbuffer_set_uhead_size (mbuffer_st * bufel, size_t size)
{
  bufel->user_mark = size;
}



inline static mbuffer_st *
_gnutls_handshake_alloc (size_t size, size_t maximum)
{
  mbuffer_st *ret = _mbuffer_alloc (HANDSHAKE_HEADER_SIZE + size,
                                    HANDSHAKE_HEADER_SIZE + maximum);

  if (!ret)
    return NULL;

  _mbuffer_set_uhead_size (ret, HANDSHAKE_HEADER_SIZE);

  return ret;
}

/* Free a segment, if the pointer is not NULL
 *
 * We take a ** to detect and fix double free bugs (the dangling
 * pointer case). It also makes sure the pointer has a known value
 * after freeing.
 */
inline static void
_mbuffer_xfree (mbuffer_st ** bufel)
{
  if (*bufel)
    gnutls_free (*bufel);

  *bufel = NULL;
}

#endif