/src/gnutls/lib/str_array.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011-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 | | #ifndef GNUTLS_LIB_STR_ARRAY_H |
24 | | # define GNUTLS_LIB_STR_ARRAY_H |
25 | | |
26 | | # include "gnutls_int.h" |
27 | | # include "errors.h" |
28 | | |
29 | | /* Functionality to allow an array of strings. Strings |
30 | | * are allowed to be added to the list and matched against it. |
31 | | */ |
32 | | |
33 | | typedef struct gnutls_str_array_st { |
34 | | char *str; |
35 | | unsigned int len; |
36 | | struct gnutls_str_array_st *next; |
37 | | } *gnutls_str_array_t; |
38 | | |
39 | | inline static void _gnutls_str_array_init(gnutls_str_array_t * head) |
40 | 0 | { |
41 | 0 | *head = NULL; |
42 | 0 | } |
43 | | |
44 | | inline static void _gnutls_str_array_clear(gnutls_str_array_t * head) |
45 | 0 | { |
46 | 0 | gnutls_str_array_t prev, array = *head; |
47 | 0 |
|
48 | 0 | while (array != NULL) { |
49 | 0 | prev = array; |
50 | 0 | array = prev->next; |
51 | 0 | gnutls_free(prev); |
52 | 0 | } |
53 | 0 | *head = NULL; |
54 | 0 | } |
55 | | |
56 | | inline static int _gnutls_str_array_match(gnutls_str_array_t head, |
57 | | const char *str) |
58 | 0 | { |
59 | 0 | gnutls_str_array_t array = head; |
60 | 0 |
|
61 | 0 | while (array != NULL) { |
62 | 0 | if (strcmp(array->str, str) == 0) |
63 | 0 | return 1; |
64 | 0 | array = array->next; |
65 | 0 | } |
66 | 0 |
|
67 | 0 | return 0; |
68 | 0 | } |
69 | | |
70 | | inline static void append(gnutls_str_array_t array, const char *str, int len) |
71 | 0 | { |
72 | 0 | array->str = ((char *)array) + sizeof(struct gnutls_str_array_st); |
73 | 0 | memcpy(array->str, str, len); |
74 | 0 | array->str[len] = 0; |
75 | 0 | array->len = len; |
76 | 0 | array->next = NULL; |
77 | 0 | } |
78 | | |
79 | | inline static int _gnutls_str_array_append(gnutls_str_array_t * head, |
80 | | const char *str, int len) |
81 | 0 | { |
82 | 0 | gnutls_str_array_t prev, array; |
83 | 0 | if (*head == NULL) { |
84 | 0 | *head = |
85 | 0 | gnutls_malloc(len + 1 + sizeof(struct gnutls_str_array_st)); |
86 | 0 | if (*head == NULL) |
87 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
88 | 0 |
|
89 | 0 | array = *head; |
90 | 0 | append(array, str, len); |
91 | 0 | } else { |
92 | 0 | array = *head; |
93 | 0 | prev = array; |
94 | 0 | while (array != NULL) { |
95 | 0 | prev = array; |
96 | 0 | array = prev->next; |
97 | 0 | } |
98 | 0 | prev->next = |
99 | 0 | gnutls_malloc(len + 1 + sizeof(struct gnutls_str_array_st)); |
100 | 0 |
|
101 | 0 | array = prev->next; |
102 | 0 |
|
103 | 0 | if (array == NULL) |
104 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
105 | 0 |
|
106 | 0 | append(array, str, len); |
107 | 0 | } |
108 | 0 |
|
109 | 0 | return 0; |
110 | 0 | } |
111 | | |
112 | | inline static int _gnutls_str_array_append_idna(gnutls_str_array_t * head, |
113 | | const char *name, size_t size) |
114 | 0 | { |
115 | 0 | int ret; |
116 | 0 | gnutls_datum_t ahost; |
117 | 0 |
|
118 | 0 | /* convert the provided hostname to ACE-Labels domain. */ |
119 | 0 | ret = gnutls_idna_map(name, size, &ahost, 0); |
120 | 0 | if (ret < 0) { |
121 | 0 | _gnutls_debug_log |
122 | 0 | ("unable to convert hostname %s to IDNA format\n", name); |
123 | 0 | /* insert the raw name */ |
124 | 0 | return _gnutls_str_array_append(head, name, size); |
125 | 0 | } |
126 | 0 |
|
127 | 0 | ret = _gnutls_str_array_append(head, (char *)ahost.data, ahost.size); |
128 | 0 | gnutls_free(ahost.data); |
129 | 0 |
|
130 | 0 | return ret; |
131 | 0 | } |
132 | | |
133 | | #endif /* GNUTLS_LIB_STR_ARRAY_H */ |