Qt: fix build
[vlc.git] / modules / keystore / list_util.c
blob66de585a85aded28afe4d64d7191e9b4510fea8e
1 /*****************************************************************************
2 * list_util.c: list helper used by memory and file_crypt keystores
3 *****************************************************************************
4 * Copyright © 2015-2016 VLC authors, VideoLAN and VideoLabs
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <vlc_common.h>
26 #include <vlc_keystore.h>
28 #include "list_util.h"
30 void
31 ks_list_free(struct ks_list *p_list)
33 vlc_keystore_release_entries(p_list->p_entries, p_list->i_count);
34 p_list->p_entries = NULL;
35 p_list->i_count = 0;
36 p_list->i_max = 0;
39 int
40 ks_values_copy(const char * ppsz_dst[KEY_MAX],
41 const char *const ppsz_src[KEY_MAX])
43 for (unsigned int i = 0; i < KEY_MAX; ++i)
45 if (ppsz_src[i])
47 ppsz_dst[i] = strdup(ppsz_src[i]);
48 if (!ppsz_dst[i])
49 return VLC_EGENERIC;
52 return VLC_SUCCESS;
55 vlc_keystore_entry *
56 ks_list_new_entry(struct ks_list *p_list)
58 if (p_list->i_count + 1 > p_list->i_max)
60 p_list->i_max += 10;
61 vlc_keystore_entry *p_entries = realloc(p_list->p_entries, p_list->i_max
62 * sizeof(*p_list->p_entries));
63 if (!p_entries)
65 ks_list_free(p_list);
66 return NULL;
68 p_list->p_entries = p_entries;
70 vlc_keystore_entry *p_entry = &p_list->p_entries[p_list->i_count];
71 p_entry->p_secret = NULL;
72 VLC_KEYSTORE_VALUES_INIT(p_entry->ppsz_values);
73 p_list->i_count++;
75 return p_entry;
78 vlc_keystore_entry *
79 ks_list_find_entry(struct ks_list *p_list, const char *const ppsz_values[KEY_MAX],
80 unsigned *p_start_index)
82 for (unsigned int i = p_start_index ? *p_start_index : 0;
83 i < p_list->i_count; ++i)
85 vlc_keystore_entry *p_entry = &p_list->p_entries[i];
86 if (!p_entry->p_secret)
87 continue;
89 bool b_match = true;
90 for (unsigned int j = 0; j < KEY_MAX; ++j)
92 const char *psz_value1 = ppsz_values[j];
93 const char *psz_value2 = p_entry->ppsz_values[j];
95 if (!psz_value1)
96 continue;
97 if (!psz_value2 || strcmp(psz_value1, psz_value2))
98 b_match = false;
100 if (b_match)
102 if (p_start_index)
103 *p_start_index = i + 1;
104 return p_entry;
107 return NULL;