2 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS 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 3 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 License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* contains functions that make it easier to
24 * write vectors of <size|data>. The destination size
25 * should be preallocated (datum.size+(bits/8))
28 #include <gnutls_int.h>
29 #include <gnutls_num.h>
30 #include <gnutls_datum.h>
31 #include <gnutls_errors.h>
35 _gnutls_write_datum16 (uint8_t * dest
, gnutls_datum_t dat
)
37 _gnutls_write_uint16 (dat
.size
, dest
);
39 memcpy (&dest
[2], dat
.data
, dat
.size
);
43 _gnutls_write_datum24 (uint8_t * dest
, gnutls_datum_t dat
)
45 _gnutls_write_uint24 (dat
.size
, dest
);
47 memcpy (&dest
[3], dat
.data
, dat
.size
);
51 _gnutls_write_datum32 (uint8_t * dest
, gnutls_datum_t dat
)
53 _gnutls_write_uint32 (dat
.size
, dest
);
55 memcpy (&dest
[4], dat
.data
, dat
.size
);
59 _gnutls_write_datum8 (uint8_t * dest
, gnutls_datum_t dat
)
61 dest
[0] = (uint8_t) dat
.size
;
63 memcpy (&dest
[1], dat
.data
, dat
.size
);
68 _gnutls_set_datum (gnutls_datum_t
* dat
, const void *data
,
71 if (data_size
== 0 || data
== NULL
)
78 dat
->data
= gnutls_malloc (data_size
);
79 if (dat
->data
== NULL
)
80 return GNUTLS_E_MEMORY_ERROR
;
82 dat
->size
= data_size
;
83 memcpy (dat
->data
, data
, data_size
);
89 _gnutls_datum_append (gnutls_datum_t
* dst
, const void *data
,
93 dst
->data
= gnutls_realloc (dst
->data
, data_size
+ dst
->size
);
94 if (dst
->data
== NULL
)
95 return GNUTLS_E_MEMORY_ERROR
;
97 memcpy (&dst
->data
[dst
->size
], data
, data_size
);
98 dst
->size
+= data_size
;
104 _gnutls_free_datum (gnutls_datum_t
* dat
)
106 if (dat
->data
!= NULL
)
107 gnutls_free (dat
->data
);