Tizen 2.0 Release
[external/libgnutls26.git] / lib / gnutls_datum.c
1 /*
2  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS.
8  *
9  * The GnuTLS 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.
13  *
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.
18  *
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
23  *
24  */
25
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))
29  */
30
31 #include <gnutls_int.h>
32 #include <gnutls_num.h>
33 #include <gnutls_datum.h>
34 #include <gnutls_errors.h>
35
36
37 void
38 _gnutls_write_datum16 (opaque * dest, gnutls_datum_t dat)
39 {
40   _gnutls_write_uint16 (dat.size, dest);
41   if (dat.data != NULL)
42     memcpy (&dest[2], dat.data, dat.size);
43 }
44
45 void
46 _gnutls_write_datum24 (opaque * dest, gnutls_datum_t dat)
47 {
48   _gnutls_write_uint24 (dat.size, dest);
49   if (dat.data != NULL)
50     memcpy (&dest[3], dat.data, dat.size);
51 }
52
53 void
54 _gnutls_write_datum32 (opaque * dest, gnutls_datum_t dat)
55 {
56   _gnutls_write_uint32 (dat.size, dest);
57   if (dat.data != NULL)
58     memcpy (&dest[4], dat.data, dat.size);
59 }
60
61 void
62 _gnutls_write_datum8 (opaque * dest, gnutls_datum_t dat)
63 {
64   dest[0] = (uint8_t) dat.size;
65   if (dat.data != NULL)
66     memcpy (&dest[1], dat.data, dat.size);
67 }
68
69
70 int
71 _gnutls_set_datum_m (gnutls_datum_t * dat, const void *data,
72                      size_t data_size, gnutls_alloc_function galloc_func)
73 {
74   if (data_size == 0 || data == NULL)
75     {
76       dat->data = NULL;
77       dat->size = 0;
78       return 0;
79     }
80
81   dat->data = galloc_func (data_size);
82   if (dat->data == NULL)
83     return GNUTLS_E_MEMORY_ERROR;
84
85   dat->size = data_size;
86   memcpy (dat->data, data, data_size);
87
88   return 0;
89 }
90
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)
95 {
96
97   dst->data = grealloc_func (dst->data, data_size + dst->size);
98   if (dst->data == NULL)
99     return GNUTLS_E_MEMORY_ERROR;
100
101   memcpy (&dst->data[dst->size], data, data_size);
102   dst->size += data_size;
103
104   return 0;
105 }
106
107 void
108 _gnutls_free_datum_m (gnutls_datum_t * dat, gnutls_free_function gfree_func)
109 {
110   if (dat->data != NULL)
111     gfree_func (dat->data);
112
113   dat->data = NULL;
114   dat->size = 0;
115 }