summaryrefslogtreecommitdiff
path: root/lib/name_val_array.h
blob: e4b3754df107336cab797836862601e8fc801c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) 2011-2019 Free Software Foundation, Inc.
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

#ifndef GNUTLS_NAME_VAL_ARRAY_H
#define GNUTLS_NAME_VAL_ARRAY_H

#include "gnutls_int.h"
#include "errors.h"

/* Functionality to allow an array of strings. Strings
 * are allowed to be added to the list and matched against it.
 */

typedef struct name_val_array_st {
	char *name;
	unsigned name_size;
	char *val;
	struct name_val_array_st *next;
} *name_val_array_t;

inline static void _name_val_array_init(name_val_array_t * head)
{
	*head = NULL;
}

inline static void _name_val_array_clear(name_val_array_t * head)
{
	name_val_array_t prev, array = *head;

	while (array != NULL) {
		prev = array;
		array = prev->next;
		gnutls_free(prev);
	}
	*head = NULL;
}

inline static const char *_name_val_array_value(name_val_array_t head,
					        const char *name, unsigned name_size)
{
	name_val_array_t array = head;

	while (array != NULL) {
		if (array->name_size == name_size &&
		    memcmp(array->name, name, name_size) == 0) {
			return array->val;
		}
		array = array->next;
	}

	return NULL;
}

inline static void append(name_val_array_t array, const char *name,
			  unsigned name_len, const char *val,
			  unsigned val_len)
{
	array->name = ((char *) array) + sizeof(struct name_val_array_st);
	memcpy(array->name, name, name_len);
	array->name[name_len] = 0;
	array->name_size = name_len;

	array->val = ((char *) array) + name_len + 1 + sizeof(struct name_val_array_st);
	if (val)
		memcpy(array->val, val, val_len);
	array->val[val_len] = 0;

	array->next = NULL;
}

inline static int _name_val_array_append(name_val_array_t * head,
					 const char *name,
					 const char *val)
{
	name_val_array_t prev, array;
	unsigned name_len = strlen(name);
	unsigned val_len = (val==NULL)?0:strlen(val);

	if (*head == NULL) {
		*head =
		    gnutls_malloc(val_len + name_len + 2 +
				  sizeof(struct name_val_array_st));
		if (*head == NULL)
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

		array = *head;
		append(array, name, name_len, val, val_len);
	} else {
		array = *head;
		prev = array;

		while (array != NULL) {
			prev = array;
			array = prev->next;
		}
		prev->next =
		    gnutls_malloc(name_len + val_len + 2 +
				  sizeof(struct name_val_array_st));
		array = prev->next;

		if (array == NULL)
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

		append(array, name, name_len, val, val_len);
	}

	return 0;
}

#endif