|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2009-2012 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Jonathan Bastien-Filiatrault
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GNUTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The GNUTLS library is free software; you can redistribute it and/or
|
|
Packit Service |
4684c1 |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
4684c1 |
* the License, or (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This library is distributed in the hope that it will be useful, but
|
|
Packit Service |
4684c1 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
4684c1 |
* Lesser General Public License for more details.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifndef GNUTLS_LIB_MBUFFERS_H
|
|
Packit Service |
4684c1 |
#define GNUTLS_LIB_MBUFFERS_H
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "gnutls_int.h"
|
|
Packit Service |
4684c1 |
#include "errors.h"
|
|
Packit Service |
4684c1 |
#include <assert.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void _mbuffer_head_init(mbuffer_head_st * buf);
|
|
Packit Service |
4684c1 |
void _mbuffer_head_clear(mbuffer_head_st * buf);
|
|
Packit Service |
4684c1 |
void _mbuffer_enqueue(mbuffer_head_st * buf, mbuffer_st * bufel);
|
|
Packit Service |
4684c1 |
mbuffer_st *_mbuffer_dequeue(mbuffer_head_st * buf, mbuffer_st * bufel);
|
|
Packit Service |
4684c1 |
int _mbuffer_head_remove_bytes(mbuffer_head_st * buf, size_t bytes);
|
|
Packit Service |
4684c1 |
mbuffer_st *_mbuffer_alloc(size_t maximum_size);
|
|
Packit Service |
4684c1 |
int _mbuffer_linearize(mbuffer_head_st * buf);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
mbuffer_st *_mbuffer_head_get_first(mbuffer_head_st * buf,
|
|
Packit Service |
4684c1 |
gnutls_datum_t * msg);
|
|
Packit Service |
4684c1 |
mbuffer_st *_mbuffer_head_get_next(mbuffer_st * cur, gnutls_datum_t * msg);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void _mbuffer_head_push_first(mbuffer_head_st * buf, mbuffer_st * bufel);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
mbuffer_st *_mbuffer_head_pop_first(mbuffer_head_st * buf);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This is dangerous since it will replace bufel with a new
|
|
Packit Service |
4684c1 |
* one.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
int _mbuffer_append_data(mbuffer_st * bufel, void *newdata,
|
|
Packit Service |
4684c1 |
size_t newdata_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* For "user" use. One can have buffer data and header.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static void *_mbuffer_get_uhead_ptr(mbuffer_st * bufel)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return bufel->msg.data + bufel->mark;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static void *_mbuffer_get_udata_ptr(mbuffer_st * bufel)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return bufel->msg.data + bufel->uhead_mark + bufel->mark;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static void _mbuffer_set_udata_size(mbuffer_st * bufel, size_t size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
bufel->msg.size = size + bufel->uhead_mark + bufel->mark;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static void
|
|
Packit Service |
4684c1 |
_mbuffer_set_udata(mbuffer_st * bufel, void *data, size_t data_size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
memcpy(_mbuffer_get_udata_ptr(bufel), data,
|
|
Packit Service |
4684c1 |
data_size);
|
|
Packit Service |
4684c1 |
_mbuffer_set_udata_size(bufel, data_size);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static size_t _mbuffer_get_udata_size(mbuffer_st * bufel)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return bufel->msg.size - bufel->uhead_mark - bufel->mark;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* discards size bytes from the begging of the buffer */
|
|
Packit Service |
4684c1 |
inline static void
|
|
Packit Service |
4684c1 |
_mbuffer_consume(mbuffer_head_st * buf, mbuffer_st * bufel, size_t size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
bufel->uhead_mark = 0;
|
|
Packit Service |
4684c1 |
if (bufel->mark + size < bufel->msg.size)
|
|
Packit Service |
4684c1 |
bufel->mark += size;
|
|
Packit Service |
4684c1 |
else
|
|
Packit Service |
4684c1 |
bufel->mark = bufel->msg.size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
buf->byte_length -= size;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static size_t _mbuffer_get_uhead_size(mbuffer_st * bufel)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return bufel->uhead_mark;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static void _mbuffer_set_uhead_size(mbuffer_st * bufel, size_t size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
bufel->uhead_mark = size;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static void _mbuffer_init(mbuffer_st *bufel, size_t max)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
memset(bufel, 0, sizeof(*bufel));
|
|
Packit Service |
4684c1 |
bufel->maximum_size = max;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* payload points after the mbuffer_st structure */
|
|
Packit Service |
4684c1 |
bufel->msg.data = (uint8_t *) bufel + sizeof(mbuffer_st);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Helper functions to utilize a gnutls_buffer_st in order
|
|
Packit Service |
4684c1 |
* to generate a gnutls_mbuffer_st, without multiple allocations.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
inline static int _gnutls_buffer_init_mbuffer(gnutls_buffer_st * buf, size_t header_size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
mbuffer_st *bufel;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_gnutls_buffer_init(buf);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_buffer_resize(buf, sizeof(mbuffer_st)+header_size);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* we store the uhead size on the uninitialized bufel, only to read
|
|
Packit Service |
4684c1 |
* it back on _gnutls_buffer_to_mbuffer(). */
|
|
Packit Service |
4684c1 |
bufel = (void*)buf->data;
|
|
Packit Service |
4684c1 |
_mbuffer_set_uhead_size(bufel, header_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
buf->length = sizeof(mbuffer_st)+header_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define _gnutls_buffer_init_handshake_mbuffer(b) _gnutls_buffer_init_mbuffer(b, HANDSHAKE_HEADER_SIZE(session))
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Cannot fail */
|
|
Packit Service |
4684c1 |
inline static mbuffer_st *_gnutls_buffer_to_mbuffer(gnutls_buffer_st * buf)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
mbuffer_st *bufel;
|
|
Packit Service |
4684c1 |
unsigned header_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
bufel = (void*)buf->data;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
header_size = _mbuffer_get_uhead_size(bufel);
|
|
Packit Service |
4684c1 |
assert(buf->length >= sizeof(mbuffer_st)+header_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_mbuffer_init(bufel, buf->length - sizeof(mbuffer_st));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_mbuffer_set_udata_size(bufel, buf->length - sizeof(mbuffer_st));
|
|
Packit Service |
4684c1 |
_mbuffer_set_uhead_size(bufel, header_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_gnutls_buffer_init(buf); /* avoid double frees */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return bufel;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
inline static mbuffer_st *_gnutls_handshake_alloc(gnutls_session_t session,
|
|
Packit Service |
4684c1 |
size_t maximum)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
mbuffer_st *bufel =
|
|
Packit Service |
4684c1 |
_mbuffer_alloc(HANDSHAKE_HEADER_SIZE(session) + maximum);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (!bufel)
|
|
Packit Service |
4684c1 |
return NULL;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_mbuffer_set_uhead_size(bufel, HANDSHAKE_HEADER_SIZE(session));
|
|
Packit Service |
4684c1 |
_mbuffer_set_udata_size(bufel, maximum);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return bufel;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Free a segment, if the pointer is not NULL
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* We take a ** to detect and fix double free bugs (the dangling
|
|
Packit Service |
4684c1 |
* pointer case). It also makes sure the pointer has a known value
|
|
Packit Service |
4684c1 |
* after freeing.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
inline static void _mbuffer_xfree(mbuffer_st ** bufel)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
if (*bufel)
|
|
Packit Service |
4684c1 |
gnutls_free(*bufel);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
*bufel = NULL;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef ENABLE_ALIGN16
|
|
Packit Service |
4684c1 |
mbuffer_st *_mbuffer_alloc_align16(size_t maximum_size, unsigned align_pos);
|
|
Packit Service |
4684c1 |
int _mbuffer_linearize_align16(mbuffer_head_st * buf, unsigned align_pos);
|
|
Packit Service |
4684c1 |
#else
|
|
Packit Service |
4684c1 |
# define _mbuffer_alloc_align16(x,y) _mbuffer_alloc(x)
|
|
Packit Service |
4684c1 |
# define _mbuffer_linearize_align16(x,y) _mbuffer_linearize(x)
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#endif /* GNUTLS_LIB_MBUFFERS_H */
|