Sync with TP.
[gnutls.git] / lib / gnutls_datum.c
blob04c4411fd3adf902a1bf63ab07a6b5dd7aca25d2
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2010 Free Software
3 * Foundation, Inc.
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,
22 * USA
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>
37 void
38 _gnutls_write_datum16 (opaque * dest, gnutls_datum_t dat)
40 _gnutls_write_uint16 (dat.size, dest);
41 if (dat.data != NULL)
42 memcpy (&dest[2], dat.data, dat.size);
45 void
46 _gnutls_write_datum24 (opaque * dest, gnutls_datum_t dat)
48 _gnutls_write_uint24 (dat.size, dest);
49 if (dat.data != NULL)
50 memcpy (&dest[3], dat.data, dat.size);
53 void
54 _gnutls_write_datum32 (opaque * dest, gnutls_datum_t dat)
56 _gnutls_write_uint32 (dat.size, dest);
57 if (dat.data != NULL)
58 memcpy (&dest[4], dat.data, dat.size);
61 void
62 _gnutls_write_datum8 (opaque * dest, gnutls_datum_t dat)
64 dest[0] = (uint8_t) dat.size;
65 if (dat.data != NULL)
66 memcpy (&dest[1], dat.data, dat.size);
70 int
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)
76 dat->data = NULL;
77 dat->size = 0;
78 return 0;
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);
88 return 0;
91 int
92 _gnutls_datum_append_m (gnutls_datum_t * dst, const void *data,
93 size_t data_size,
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;
104 return 0;
107 void
108 _gnutls_free_datum_m (gnutls_datum_t * dat, gnutls_free_function gfree_func)
110 if (dat->data != NULL)
111 gfree_func (dat->data);
113 dat->data = NULL;
114 dat->size = 0;