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 *****************************************************************************/
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>
35 #include "list_util.h"
37 static int Open(vlc_object_t
*);
38 static void Close(vlc_object_t
*);
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")
50 struct vlc_keystore_sys
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
)
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
);
69 vlc_keystore_release_entry(p_entry
);
72 p_entry
= ks_list_new_entry(p_list
);
76 if (ks_values_copy((const char **)p_entry
->ppsz_values
, ppsz_values
))
79 if (vlc_keystore_entry_set_secret(p_entry
, p_secret
, i_secret_len
))
84 vlc_mutex_unlock(&p_sys
->lock
);
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
;
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
);
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
);
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
);
118 vlc_mutex_unlock(&p_sys
->lock
);
120 *pp_entries
= out_list
.p_entries
;
122 return out_list
.i_count
;
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
);
139 vlc_mutex_unlock(&p_sys
->lock
);
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
);
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
)
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
;