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_keystore.h>
30 #include <vlc_strings.h>
34 #include "list_util.h"
36 static int Open(vlc_object_t
*);
37 static void Close(vlc_object_t
*);
40 set_shortname(N_("Memory keystore"))
41 set_description(N_("Secrets are stored in memory"))
42 set_category(CAT_ADVANCED
)
43 set_subcategory(SUBCAT_ADVANCED_MISC
)
44 set_capability("keystore", 0)
45 set_callbacks(Open
, Close
)
46 add_shortcut("memory")
49 struct vlc_keystore_sys
56 Store(vlc_keystore
*p_keystore
, const char *const ppsz_values
[KEY_MAX
],
57 const uint8_t *p_secret
, size_t i_secret_len
, const char *psz_label
)
60 vlc_keystore_sys
*p_sys
= p_keystore
->p_sys
;
61 struct ks_list
*p_list
= &p_sys
->list
;
62 vlc_keystore_entry
*p_entry
;
63 int i_ret
= VLC_EGENERIC
;
65 vlc_mutex_lock(&p_sys
->lock
);
66 p_entry
= ks_list_find_entry(p_list
, ppsz_values
, NULL
);
68 vlc_keystore_release_entry(p_entry
);
71 p_entry
= ks_list_new_entry(p_list
);
75 if (ks_values_copy((const char **)p_entry
->ppsz_values
, ppsz_values
))
78 if (vlc_keystore_entry_set_secret(p_entry
, p_secret
, i_secret_len
))
83 vlc_mutex_unlock(&p_sys
->lock
);
88 Find(vlc_keystore
*p_keystore
, const char *const ppsz_values
[KEY_MAX
],
89 vlc_keystore_entry
**pp_entries
)
91 vlc_keystore_sys
*p_sys
= p_keystore
->p_sys
;
92 struct ks_list
*p_list
= &p_sys
->list
;
93 struct ks_list out_list
= { 0 };
94 vlc_keystore_entry
*p_entry
;
97 vlc_mutex_lock(&p_sys
->lock
);
98 while ((p_entry
= ks_list_find_entry(p_list
, ppsz_values
, &i_index
)))
100 vlc_keystore_entry
*p_out_entry
= ks_list_new_entry(&out_list
);
103 || ks_values_copy((const char **)p_out_entry
->ppsz_values
,
104 (const char *const*)p_entry
->ppsz_values
))
106 ks_list_free(&out_list
);
110 if (vlc_keystore_entry_set_secret(p_out_entry
, p_entry
->p_secret
,
111 p_entry
->i_secret_len
))
113 ks_list_free(&out_list
);
117 vlc_mutex_unlock(&p_sys
->lock
);
119 *pp_entries
= out_list
.p_entries
;
121 return out_list
.i_count
;
125 Remove(vlc_keystore
*p_keystore
, const char *const ppsz_values
[KEY_MAX
])
127 vlc_keystore_sys
*p_sys
= p_keystore
->p_sys
;
128 struct ks_list
*p_list
= &p_sys
->list
;
129 vlc_keystore_entry
*p_entry
;
130 unsigned i_index
= 0, i_count
= 0;
132 vlc_mutex_lock(&p_sys
->lock
);
133 while ((p_entry
= ks_list_find_entry(p_list
, ppsz_values
, &i_index
)))
135 vlc_keystore_release_entry(p_entry
);
138 vlc_mutex_unlock(&p_sys
->lock
);
144 Close(vlc_object_t
*p_this
)
146 vlc_keystore
*p_keystore
= (vlc_keystore
*)p_this
;
147 vlc_keystore_sys
*p_sys
= p_keystore
->p_sys
;
149 ks_list_free(&p_sys
->list
);
154 Open(vlc_object_t
*p_this
)
156 vlc_keystore
*p_keystore
= (vlc_keystore
*)p_this
;
157 p_keystore
->p_sys
= calloc(1, sizeof(vlc_keystore_sys
));
158 if (!p_keystore
->p_sys
)
161 vlc_mutex_init(&p_keystore
->p_sys
->lock
);
162 p_keystore
->pf_store
= Store
;
163 p_keystore
->pf_find
= Find
;
164 p_keystore
->pf_remove
= Remove
;