Improve.
[gnutls.git] / lib / gnutls_datum.c
blobd374ca1e23fc64509c8133d30f7fe6e25160b9ef
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
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 2.1 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
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
25 /* contains functions that make it easier to
26 * write vectors of <size|data>. The destination size
27 * should be preallocated (datum.size+(bits/8))
30 #include <gnutls_int.h>
31 #include <gnutls_num.h>
32 #include <gnutls_datum.h>
33 #include <gnutls_errors.h>
36 void
37 _gnutls_write_datum16 (opaque * dest, gnutls_datum_t dat)
39 _gnutls_write_uint16 (dat.size, dest);
40 if (dat.data != NULL)
41 memcpy (&dest[2], dat.data, dat.size);
44 void
45 _gnutls_write_datum24 (opaque * dest, gnutls_datum_t dat)
47 _gnutls_write_uint24 (dat.size, dest);
48 if (dat.data != NULL)
49 memcpy (&dest[3], dat.data, dat.size);
52 void
53 _gnutls_write_datum32 (opaque * dest, gnutls_datum_t dat)
55 _gnutls_write_uint32 (dat.size, dest);
56 if (dat.data != NULL)
57 memcpy (&dest[4], dat.data, dat.size);
60 void
61 _gnutls_write_datum8 (opaque * dest, gnutls_datum_t dat)
63 dest[0] = (uint8_t) dat.size;
64 if (dat.data != NULL)
65 memcpy (&dest[1], dat.data, dat.size);
69 int
70 _gnutls_set_datum_m (gnutls_datum_t * dat, const void *data,
71 size_t data_size, gnutls_alloc_function galloc_func)
73 if (data_size == 0 || data == NULL)
75 dat->data = NULL;
76 dat->size = 0;
77 return 0;
80 dat->data = galloc_func (data_size);
81 if (dat->data == NULL)
82 return GNUTLS_E_MEMORY_ERROR;
84 dat->size = data_size;
85 memcpy (dat->data, data, data_size);
87 return 0;
90 int
91 _gnutls_datum_append_m (gnutls_datum_t * dst, const void *data,
92 size_t data_size,
93 gnutls_realloc_function grealloc_func)
96 dst->data = grealloc_func (dst->data, data_size + dst->size);
97 if (dst->data == NULL)
98 return GNUTLS_E_MEMORY_ERROR;
100 memcpy (&dst->data[dst->size], data, data_size);
101 dst->size += data_size;
103 return 0;
106 void
107 _gnutls_free_datum_m (gnutls_datum_t * dat, gnutls_free_function gfree_func)
109 if (dat->data != NULL)
110 gfree_func (dat->data);
112 dat->data = NULL;
113 dat->size = 0;