Coverage Report

Created: 2023-03-26 08:33

/src/gnutls/lib/datum.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * This file is part of GnuTLS.
7
 *
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 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
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.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
20
 *
21
 */
22
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))
26
 */
27
28
#include "gnutls_int.h"
29
#include <num.h>
30
#include <datum.h>
31
#include "errors.h"
32
33
/* On error, @dat is not changed. */
34
int _gnutls_set_datum(gnutls_datum_t * dat, const void *data, size_t data_size)
35
0
{
36
0
  if (data_size == 0 || data == NULL) {
37
0
    dat->data = NULL;
38
0
    dat->size = 0;
39
0
    return 0;
40
0
  }
41
42
0
  unsigned char *m = gnutls_malloc(data_size);
43
0
  if (!m)
44
0
    return GNUTLS_E_MEMORY_ERROR;
45
46
0
  dat->data = m;
47
0
  dat->size = data_size;
48
0
  memcpy(dat->data, data, data_size);
49
50
0
  return 0;
51
0
}
52
53
/* ensures that the data set are null-terminated
54
 * The function always returns an allocated string in @dat on success.
55
 * On error, @dat is not changed.
56
 */
57
int
58
_gnutls_set_strdatum(gnutls_datum_t * dat, const void *data, size_t data_size)
59
0
{
60
0
  if (data == NULL)
61
0
    return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
62
63
0
  unsigned char *m = gnutls_malloc(data_size + 1);
64
0
  if (!m)
65
0
    return GNUTLS_E_MEMORY_ERROR;
66
67
0
  dat->data = m;
68
0
  dat->size = data_size;
69
0
  if (data_size)
70
0
    memcpy(dat->data, data, data_size);
71
0
  dat->data[data_size] = 0;
72
73
0
  return 0;
74
0
}