/src/gnutls/lib/name_val_array.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011-2019 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2019 Red Hat, Inc. |
4 | | * |
5 | | * Author: Nikos Mavrogiannopoulos |
6 | | * |
7 | | * This file is part of GnuTLS. |
8 | | * |
9 | | * The GnuTLS is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public License |
11 | | * as published by the Free Software Foundation; either version 2.1 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, but |
15 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
21 | | * |
22 | | */ |
23 | | |
24 | | #ifndef GNUTLS_NAME_VAL_ARRAY_H |
25 | | # define GNUTLS_NAME_VAL_ARRAY_H |
26 | | |
27 | | # include "gnutls_int.h" |
28 | | # include "errors.h" |
29 | | |
30 | | /* Functionality to allow an array of strings. Strings |
31 | | * are allowed to be added to the list and matched against it. |
32 | | */ |
33 | | |
34 | | typedef struct name_val_array_st { |
35 | | char *name; |
36 | | unsigned name_size; |
37 | | char *val; |
38 | | struct name_val_array_st *next; |
39 | | } *name_val_array_t; |
40 | | |
41 | | inline static void _name_val_array_clear(name_val_array_t * head) |
42 | 0 | { |
43 | 0 | name_val_array_t prev, array = *head; |
44 | |
|
45 | 0 | while (array != NULL) { |
46 | 0 | prev = array; |
47 | 0 | array = prev->next; |
48 | 0 | gnutls_free(prev); |
49 | 0 | } |
50 | 0 | *head = NULL; |
51 | 0 | } |
52 | | |
53 | | inline static const char *_name_val_array_value(name_val_array_t head, |
54 | | const char *name, |
55 | | unsigned name_size) |
56 | 0 | { |
57 | 0 | name_val_array_t array = head; |
58 | |
|
59 | 0 | while (array != NULL) { |
60 | 0 | if (array->name_size == name_size && |
61 | 0 | memcmp(array->name, name, name_size) == 0) { |
62 | 0 | return array->val; |
63 | 0 | } |
64 | 0 | array = array->next; |
65 | 0 | } |
66 | | |
67 | 0 | return NULL; |
68 | 0 | } |
69 | | |
70 | | inline static void append(name_val_array_t array, const char *name, |
71 | | unsigned name_len, const char *val, unsigned val_len) |
72 | 0 | { |
73 | 0 | array->name = ((char *)array) + sizeof(struct name_val_array_st); |
74 | 0 | memcpy(array->name, name, name_len); |
75 | 0 | array->name[name_len] = 0; |
76 | 0 | array->name_size = name_len; |
77 | |
|
78 | 0 | array->val = |
79 | 0 | ((char *)array) + name_len + 1 + sizeof(struct name_val_array_st); |
80 | 0 | if (val) |
81 | 0 | memcpy(array->val, val, val_len); |
82 | 0 | array->val[val_len] = 0; |
83 | |
|
84 | 0 | array->next = NULL; |
85 | 0 | } |
86 | | |
87 | | inline static int _name_val_array_append(name_val_array_t * head, |
88 | | const char *name, const char *val) |
89 | 0 | { |
90 | 0 | name_val_array_t prev, array; |
91 | 0 | unsigned name_len = strlen(name); |
92 | 0 | unsigned val_len = (val == NULL) ? 0 : strlen(val); |
93 | |
|
94 | 0 | if (*head == NULL) { |
95 | 0 | *head = |
96 | 0 | gnutls_malloc(val_len + name_len + 2 + |
97 | 0 | sizeof(struct name_val_array_st)); |
98 | 0 | if (*head == NULL) |
99 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
100 | | |
101 | 0 | array = *head; |
102 | 0 | append(array, name, name_len, val, val_len); |
103 | 0 | } else { |
104 | 0 | array = *head; |
105 | 0 | prev = array; |
106 | |
|
107 | 0 | while (array != NULL) { |
108 | 0 | prev = array; |
109 | 0 | array = prev->next; |
110 | 0 | } |
111 | 0 | prev->next = |
112 | 0 | gnutls_malloc(name_len + val_len + 2 + |
113 | 0 | sizeof(struct name_val_array_st)); |
114 | 0 | array = prev->next; |
115 | |
|
116 | 0 | if (array == NULL) |
117 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
118 | | |
119 | 0 | append(array, name, name_len, val, val_len); |
120 | 0 | } |
121 | | |
122 | 0 | return 0; |
123 | 0 | } |
124 | | |
125 | | #endif |