summaryrefslogtreecommitdiff
path: root/lib/gnutls_str_array.h
blob: 57aa828ac61d096b11ddbc0ffece7bd3b3384932 (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
/*
 * Copyright (C) 2011-2012 Free Software Foundation, 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 <http://www.gnu.org/licenses/>
 *
 */

#ifndef GNUTLS_STR_ARRAY_H
#define GNUTLS_STR_ARRAY_H

#include <gnutls_int.h>
#include <gnutls_errors.h>

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

typedef struct gnutls_str_array_st {
	char *str;
	unsigned int len;
	struct gnutls_str_array_st *next;
} *gnutls_str_array_t;

inline static void _gnutls_str_array_init(gnutls_str_array_t * head)
{
	*head = NULL;
}

inline static void _gnutls_str_array_clear(gnutls_str_array_t * head)
{
	gnutls_str_array_t prev, array = *head;

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

inline static int _gnutls_str_array_match(gnutls_str_array_t head,
					  const char *str)
{
	gnutls_str_array_t array = head;

	while (array != NULL) {
		if (strcmp(array->str, str) == 0)
			return 1;
		array = array->next;
	}

	return 0;
}

inline static void append(gnutls_str_array_t array, const char *str,
			  int len)
{
	array->str = ((char *) array) + sizeof(struct gnutls_str_array_st);
	memcpy(array->str, str, len);
	array->str[len] = 0;
	array->len = len;
	array->next = NULL;
}

inline static int _gnutls_str_array_append(gnutls_str_array_t * head,
					   const char *str, int len)
{
	gnutls_str_array_t prev, array;
	if (*head == NULL) {
		*head =
		    gnutls_malloc(len + 1 +
				  sizeof(struct gnutls_str_array_st));
		if (*head == NULL)
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

		array = *head;
		append(array, str, len);
	} else {
		array = *head;
		prev = array;
		while (array != NULL) {
			prev = array;
			array = prev->next;
		}
		prev->next =
		    gnutls_malloc(len + 1 +
				  sizeof(struct gnutls_str_array_st));

		array = prev->next;

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

		append(array, str, len);
	}

	return 0;
}

#endif