updated licenses
[gnutls.git] / lib / gnutls_datum.c
blob9a6d55d09c7524ea6438704d164f403ee8aed85b
1 /*
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>
34 void
35 _gnutls_write_datum16 (uint8_t * dest, gnutls_datum_t dat)
37 _gnutls_write_uint16 (dat.size, dest);
38 if (dat.data != NULL)
39 memcpy (&dest[2], dat.data, dat.size);
42 void
43 _gnutls_write_datum24 (uint8_t * dest, gnutls_datum_t dat)
45 _gnutls_write_uint24 (dat.size, dest);
46 if (dat.data != NULL)
47 memcpy (&dest[3], dat.data, dat.size);
50 void
51 _gnutls_write_datum32 (uint8_t * dest, gnutls_datum_t dat)
53 _gnutls_write_uint32 (dat.size, dest);
54 if (dat.data != NULL)
55 memcpy (&dest[4], dat.data, dat.size);
58 void
59 _gnutls_write_datum8 (uint8_t * dest, gnutls_datum_t dat)
61 dest[0] = (uint8_t) dat.size;
62 if (dat.data != NULL)
63 memcpy (&dest[1], dat.data, dat.size);
67 int
68 _gnutls_set_datum (gnutls_datum_t * dat, const void *data,
69 size_t data_size)
71 if (data_size == 0 || data == NULL)
73 dat->data = NULL;
74 dat->size = 0;
75 return 0;
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);
85 return 0;
88 int
89 _gnutls_datum_append (gnutls_datum_t * dst, const void *data,
90 size_t data_size)
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;
100 return 0;
103 void
104 _gnutls_free_datum (gnutls_datum_t * dat)
106 if (dat->data != NULL)
107 gnutls_free (dat->data);
109 dat->data = NULL;
110 dat->size = 0;