Update translations from 2.2.x branch
[vlc.git] / modules / keystore / memory.c
blobcc2702084d1e50cb290317844d5f27ee2f87a5e5
1 /*****************************************************************************
2 * memory.c: Memory keystore
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 <stdio.h>
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_memory.h>
30 #include <vlc_keystore.h>
31 #include <vlc_strings.h>
33 #include <assert.h>
35 #include "list_util.h"
37 static int Open(vlc_object_t *);
38 static void Close(vlc_object_t *);
40 vlc_module_begin()
41 set_shortname(N_("memory keystore"))
42 set_description(N_("secrets are stored in memory"))
43 set_category(CAT_ADVANCED)
44 set_subcategory(SUBCAT_ADVANCED_MISC)
45 set_capability("keystore", 0)
46 set_callbacks(Open, Close)
47 add_shortcut("memory")
48 vlc_module_end ()
50 struct vlc_keystore_sys
52 struct ks_list list;
53 vlc_mutex_t lock;
56 static int
57 Store(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
58 const uint8_t *p_secret, size_t i_secret_len, const char *psz_label)
60 (void) psz_label;
61 vlc_keystore_sys *p_sys = p_keystore->p_sys;
62 struct ks_list *p_list = &p_sys->list;
63 vlc_keystore_entry *p_entry;
64 int i_ret = VLC_EGENERIC;
66 vlc_mutex_lock(&p_sys->lock);
67 p_entry = ks_list_find_entry(p_list, ppsz_values, NULL);
68 if (p_entry)
69 vlc_keystore_release_entry(p_entry);
70 else
72 p_entry = ks_list_new_entry(p_list);
73 if (!p_entry)
74 goto end;
76 if (ks_values_copy((const char **)p_entry->ppsz_values, ppsz_values))
77 goto end;
79 if (vlc_keystore_entry_set_secret(p_entry, p_secret, i_secret_len))
80 goto end;
82 i_ret = VLC_SUCCESS;
83 end:
84 vlc_mutex_unlock(&p_sys->lock);
85 return i_ret;
88 static unsigned int
89 Find(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
90 vlc_keystore_entry **pp_entries)
92 vlc_keystore_sys *p_sys = p_keystore->p_sys;
93 struct ks_list *p_list = &p_sys->list;
94 struct ks_list out_list = { 0 };
95 vlc_keystore_entry *p_entry;
96 unsigned i_index = 0;
98 vlc_mutex_lock(&p_sys->lock);
99 while ((p_entry = ks_list_find_entry(p_list, ppsz_values, &i_index)))
101 vlc_keystore_entry *p_out_entry = ks_list_new_entry(&out_list);
103 if (!p_out_entry
104 || ks_values_copy((const char **)p_out_entry->ppsz_values,
105 (const char *const*)p_entry->ppsz_values))
107 ks_list_free(&out_list);
108 break;
111 if (vlc_keystore_entry_set_secret(p_out_entry, p_entry->p_secret,
112 p_entry->i_secret_len))
114 ks_list_free(&out_list);
115 break;
118 vlc_mutex_unlock(&p_sys->lock);
120 *pp_entries = out_list.p_entries;
122 return out_list.i_count;
125 static unsigned int
126 Remove(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX])
128 vlc_keystore_sys *p_sys = p_keystore->p_sys;
129 struct ks_list *p_list = &p_sys->list;
130 vlc_keystore_entry *p_entry;
131 unsigned i_index = 0, i_count = 0;
133 vlc_mutex_lock(&p_sys->lock);
134 while ((p_entry = ks_list_find_entry(p_list, ppsz_values, &i_index)))
136 vlc_keystore_release_entry(p_entry);
137 i_count++;
139 vlc_mutex_unlock(&p_sys->lock);
141 return i_count;
144 static void
145 Close(vlc_object_t *p_this)
147 vlc_keystore *p_keystore = (vlc_keystore *)p_this;
148 vlc_keystore_sys *p_sys = p_keystore->p_sys;
150 ks_list_free(&p_sys->list);
151 vlc_mutex_destroy(&p_keystore->p_sys->lock);
152 free(p_sys);
155 static int
156 Open(vlc_object_t *p_this)
158 vlc_keystore *p_keystore = (vlc_keystore *)p_this;
159 p_keystore->p_sys = calloc(1, sizeof(vlc_keystore_sys));
160 if (!p_keystore->p_sys)
161 return VLC_EGENERIC;
163 vlc_mutex_init(&p_keystore->p_sys->lock);
164 p_keystore->pf_store = Store;
165 p_keystore->pf_find = Find;
166 p_keystore->pf_remove = Remove;
168 return VLC_SUCCESS;