xref: /freebsd-src/contrib/libfido2/src/types.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
1f540a430SEd Maste /*
2*2ccfa855SEd Maste  * Copyright (c) 2018-2022 Yubico AB. All rights reserved.
3f540a430SEd Maste  * Use of this source code is governed by a BSD-style
4f540a430SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
6f540a430SEd Maste  */
7f540a430SEd Maste 
8f540a430SEd Maste #include "fido.h"
9f540a430SEd Maste 
10f540a430SEd Maste void
fido_str_array_free(fido_str_array_t * sa)11f540a430SEd Maste fido_str_array_free(fido_str_array_t *sa)
12f540a430SEd Maste {
13f540a430SEd Maste 	for (size_t i = 0; i < sa->len; i++)
14f540a430SEd Maste 		free(sa->ptr[i]);
15f540a430SEd Maste 
16f540a430SEd Maste 	free(sa->ptr);
17f540a430SEd Maste 	sa->ptr = NULL;
18f540a430SEd Maste 	sa->len = 0;
19f540a430SEd Maste }
20f540a430SEd Maste 
21f540a430SEd Maste void
fido_opt_array_free(fido_opt_array_t * oa)22f540a430SEd Maste fido_opt_array_free(fido_opt_array_t *oa)
23f540a430SEd Maste {
24f540a430SEd Maste 	for (size_t i = 0; i < oa->len; i++)
25f540a430SEd Maste 		free(oa->name[i]);
26f540a430SEd Maste 
27f540a430SEd Maste 	free(oa->name);
28f540a430SEd Maste 	free(oa->value);
29f540a430SEd Maste 	oa->name = NULL;
30f540a430SEd Maste 	oa->value = NULL;
31*2ccfa855SEd Maste 	oa->len = 0;
32f540a430SEd Maste }
33f540a430SEd Maste 
34f540a430SEd Maste void
fido_byte_array_free(fido_byte_array_t * ba)35f540a430SEd Maste fido_byte_array_free(fido_byte_array_t *ba)
36f540a430SEd Maste {
37f540a430SEd Maste 	free(ba->ptr);
38f540a430SEd Maste 
39f540a430SEd Maste 	ba->ptr = NULL;
40f540a430SEd Maste 	ba->len = 0;
41f540a430SEd Maste }
42f540a430SEd Maste 
43f540a430SEd Maste void
fido_algo_free(fido_algo_t * a)44f540a430SEd Maste fido_algo_free(fido_algo_t *a)
45f540a430SEd Maste {
46f540a430SEd Maste 	free(a->type);
47f540a430SEd Maste 	a->type = NULL;
48f540a430SEd Maste 	a->cose = 0;
49f540a430SEd Maste }
50f540a430SEd Maste 
51f540a430SEd Maste void
fido_algo_array_free(fido_algo_array_t * aa)52f540a430SEd Maste fido_algo_array_free(fido_algo_array_t *aa)
53f540a430SEd Maste {
54f540a430SEd Maste 	for (size_t i = 0; i < aa->len; i++)
55f540a430SEd Maste 		fido_algo_free(&aa->ptr[i]);
56f540a430SEd Maste 
57f540a430SEd Maste 	free(aa->ptr);
58f540a430SEd Maste 	aa->ptr = NULL;
59f540a430SEd Maste 	aa->len = 0;
60f540a430SEd Maste }
61f540a430SEd Maste 
62*2ccfa855SEd Maste void
fido_cert_array_free(fido_cert_array_t * ca)63*2ccfa855SEd Maste fido_cert_array_free(fido_cert_array_t *ca)
64*2ccfa855SEd Maste {
65*2ccfa855SEd Maste 	for (size_t i = 0; i < ca->len; i++)
66*2ccfa855SEd Maste 		free(ca->name[i]);
67*2ccfa855SEd Maste 
68*2ccfa855SEd Maste 	free(ca->name);
69*2ccfa855SEd Maste 	free(ca->value);
70*2ccfa855SEd Maste 	ca->name = NULL;
71*2ccfa855SEd Maste 	ca->value = NULL;
72*2ccfa855SEd Maste 	ca->len = 0;
73*2ccfa855SEd Maste }
74*2ccfa855SEd Maste 
75f540a430SEd Maste int
fido_str_array_pack(fido_str_array_t * sa,const char * const * v,size_t n)76f540a430SEd Maste fido_str_array_pack(fido_str_array_t *sa, const char * const *v, size_t n)
77f540a430SEd Maste {
78f540a430SEd Maste 	if ((sa->ptr = calloc(n, sizeof(char *))) == NULL) {
79f540a430SEd Maste 		fido_log_debug("%s: calloc", __func__);
80f540a430SEd Maste 		return -1;
81f540a430SEd Maste 	}
82f540a430SEd Maste 	for (size_t i = 0; i < n; i++) {
83f540a430SEd Maste 		if ((sa->ptr[i] = strdup(v[i])) == NULL) {
84f540a430SEd Maste 			fido_log_debug("%s: strdup", __func__);
85f540a430SEd Maste 			return -1;
86f540a430SEd Maste 		}
87f540a430SEd Maste 		sa->len++;
88f540a430SEd Maste 	}
89f540a430SEd Maste 
90f540a430SEd Maste 	return 0;
91f540a430SEd Maste }
92