2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation
4 * Author: Nikos Mavroyanopoulos
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,
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>
37 _gnutls_write_datum16 (opaque
* dest
, gnutls_datum_t dat
)
39 _gnutls_write_uint16 (dat
.size
, dest
);
41 memcpy (&dest
[2], dat
.data
, dat
.size
);
45 _gnutls_write_datum24 (opaque
* dest
, gnutls_datum_t dat
)
47 _gnutls_write_uint24 (dat
.size
, dest
);
49 memcpy (&dest
[3], dat
.data
, dat
.size
);
53 _gnutls_write_datum32 (opaque
* dest
, gnutls_datum_t dat
)
55 _gnutls_write_uint32 (dat
.size
, dest
);
57 memcpy (&dest
[4], dat
.data
, dat
.size
);
61 _gnutls_write_datum8 (opaque
* dest
, gnutls_datum_t dat
)
63 dest
[0] = (uint8_t) dat
.size
;
65 memcpy (&dest
[1], dat
.data
, dat
.size
);
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
)
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
);
91 _gnutls_datum_append_m (gnutls_datum_t
* dst
, const void *data
,
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
;
107 _gnutls_free_datum_m (gnutls_datum_t
* dat
, gnutls_free_function gfree_func
)
109 if (dat
->data
!= NULL
)
110 gfree_func (dat
->data
);