2 * Copyright (C) 2001 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * GNUTLS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNUTLS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include <gnutls_int.h>
22 #include <gnutls_num.h>
23 #include <gnutls_datum.h>
24 #include <gnutls_errors.h>
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 void WRITEdatum16( opaque
* dest
, gnutls_datum dat
) {
32 WRITEuint16( dat
.size
, dest
);
33 memcpy( &dest
[2], dat
.data
, dat
.size
);
35 void WRITEdatum24( opaque
* dest
, gnutls_datum dat
) {
36 WRITEuint24( dat
.size
, dest
);
37 memcpy( &dest
[3], dat
.data
, dat
.size
);
39 void WRITEdatum32( opaque
* dest
, gnutls_datum dat
) {
40 WRITEuint32( dat
.size
, dest
);
41 memcpy( &dest
[4], dat
.data
, dat
.size
);
43 void WRITEdatum8( opaque
* dest
, gnutls_datum dat
) {
44 dest
[0] = (uint8
) dat
.size
;
45 memcpy( &dest
[1], dat
.data
, dat
.size
);
48 int gnutls_set_datum( gnutls_datum
* dat
, const void* data
, int data_size
) {
49 dat
->data
= gnutls_malloc(data_size
);
50 if (dat
->data
==NULL
) return GNUTLS_E_MEMORY_ERROR
;
52 dat
->size
= data_size
;
53 memcpy( dat
->data
, data
, data_size
);
58 int gnutls_datum_append( gnutls_datum
* dst
, const void* data
, int data_size
) {
60 dst
->data
= gnutls_realloc(dst
->data
, data_size
+dst
->size
);
61 if (dst
->data
==NULL
) return GNUTLS_E_MEMORY_ERROR
;
63 memcpy( &dst
->data
[dst
->size
], data
, data_size
);
64 dst
->size
+= data_size
;
69 int gnutls_sset_datum( gnutls_datum
* dat
, const void* data
, int data_size
) {
70 dat
->data
= gnutls_secure_malloc(data_size
);
71 if (dat
->data
==NULL
) return GNUTLS_E_MEMORY_ERROR
;
73 dat
->size
= data_size
;
74 memcpy( dat
->data
, data
, data_size
);
79 void gnutls_free_datum( gnutls_datum
* dat
) {
80 if (dat
->data
!=NULL
&& dat
->size
!=0)
81 gnutls_free( dat
->data
);
87 void gnutls_sfree_datum( gnutls_datum
* dat
) {
88 if (dat
->data
!=NULL
&& dat
->size
!=0)
89 gnutls_secure_free( dat
->data
);