*** empty log message ***
[gnutls.git] / lib / gnutls_datum.c
blob4ee896752b7facdc51a2059431f1ec73f28e3ee6
1 /*
2 * Copyright (C) 2001,2002 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <gnutls_int.h>
23 #include <gnutls_num.h>
24 #include <gnutls_datum.h>
25 #include <gnutls_errors.h>
27 /* contains functions that make it easier to
28 * write vectors of <size|data>. The destination size
29 * should be preallocated (datum.size+(bits/8))
32 void _gnutls_write_datum16( opaque* dest, gnutls_datum dat) {
33 _gnutls_write_uint16( dat.size, dest);
34 memcpy( &dest[2], dat.data, dat.size);
36 void _gnutls_write_datum24( opaque* dest, gnutls_datum dat) {
37 _gnutls_write_uint24( dat.size, dest);
38 memcpy( &dest[3], dat.data, dat.size);
40 void _gnutls_write_datum32( opaque* dest, gnutls_datum dat) {
41 _gnutls_write_uint32( dat.size, dest);
42 memcpy( &dest[4], dat.data, dat.size);
44 void _gnutls_write_datum8( opaque* dest, gnutls_datum dat) {
45 dest[0] = (uint8) dat.size;
46 memcpy( &dest[1], dat.data, dat.size);
49 int gnutls_set_datum( gnutls_datum* dat, const void* data, int data_size) {
50 dat->data = gnutls_malloc(data_size);
51 if (dat->data==NULL) return GNUTLS_E_MEMORY_ERROR;
53 dat->size = data_size;
54 memcpy( dat->data, data, data_size);
56 return 0;
59 int gnutls_datum_append( gnutls_datum* dst, const void* data, int data_size) {
61 dst->data = gnutls_realloc(dst->data, data_size+dst->size);
62 if (dst->data==NULL) return GNUTLS_E_MEMORY_ERROR;
64 memcpy( &dst->data[dst->size], data, data_size);
65 dst->size += data_size;
67 return 0;
70 int gnutls_sset_datum( gnutls_datum* dat, const void* data, int data_size) {
71 dat->data = gnutls_secure_malloc(data_size);
72 if (dat->data==NULL) return GNUTLS_E_MEMORY_ERROR;
74 dat->size = data_size;
75 memcpy( dat->data, data, data_size);
77 return 0;
80 void gnutls_free_datum( gnutls_datum* dat) {
81 if (dat->data!=NULL && dat->size!=0)
82 gnutls_free( dat->data);
84 dat->data = NULL;
85 dat->size = 0;
88 void gnutls_sfree_datum( gnutls_datum* dat) {
89 if (dat->data!=NULL && dat->size!=0)
90 gnutls_free( dat->data);
92 dat->data = NULL;
93 dat->size = 0;