2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2010 Free Software
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 /* contains functions that make it easier to
27 * write vectors of <size|data>. The destination size
28 * should be preallocated (datum.size+(bits/8))
31 #include <gnutls_int.h>
32 #include <gnutls_num.h>
33 #include <gnutls_datum.h>
34 #include <gnutls_errors.h>
38 _gnutls_write_datum16 (opaque
* dest
, gnutls_datum_t dat
)
40 _gnutls_write_uint16 (dat
.size
, dest
);
42 memcpy (&dest
[2], dat
.data
, dat
.size
);
46 _gnutls_write_datum24 (opaque
* dest
, gnutls_datum_t dat
)
48 _gnutls_write_uint24 (dat
.size
, dest
);
50 memcpy (&dest
[3], dat
.data
, dat
.size
);
54 _gnutls_write_datum32 (opaque
* dest
, gnutls_datum_t dat
)
56 _gnutls_write_uint32 (dat
.size
, dest
);
58 memcpy (&dest
[4], dat
.data
, dat
.size
);
62 _gnutls_write_datum8 (opaque
* dest
, gnutls_datum_t dat
)
64 dest
[0] = (uint8_t) dat
.size
;
66 memcpy (&dest
[1], dat
.data
, dat
.size
);
71 _gnutls_set_datum_m (gnutls_datum_t
* dat
, const void *data
,
72 size_t data_size
, gnutls_alloc_function galloc_func
)
74 if (data_size
== 0 || data
== NULL
)
81 dat
->data
= galloc_func (data_size
);
82 if (dat
->data
== NULL
)
83 return GNUTLS_E_MEMORY_ERROR
;
85 dat
->size
= data_size
;
86 memcpy (dat
->data
, data
, data_size
);
92 _gnutls_datum_append_m (gnutls_datum_t
* dst
, const void *data
,
94 gnutls_realloc_function grealloc_func
)
97 dst
->data
= grealloc_func (dst
->data
, data_size
+ dst
->size
);
98 if (dst
->data
== NULL
)
99 return GNUTLS_E_MEMORY_ERROR
;
101 memcpy (&dst
->data
[dst
->size
], data
, data_size
);
102 dst
->size
+= data_size
;
108 _gnutls_free_datum_m (gnutls_datum_t
* dat
, gnutls_free_function gfree_func
)
110 if (dat
->data
!= NULL
)
111 gfree_func (dat
->data
);