1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file der_sequence_free.c
14   ASN.1 DER, free's a structure allocated by der_decode_sequence_flexi(), Tom St Denis
15 */
16 
17 #ifdef LTC_DER
18 
19 /**
20   Free memory allocated by der_decode_sequence_flexi()
21   @param in     The list to free
22 */
der_sequence_free(ltc_asn1_list * in)23 void der_sequence_free(ltc_asn1_list *in)
24 {
25    ltc_asn1_list *l;
26 
27    if (!in) return;
28 
29    /* walk to the start of the chain */
30    while (in->prev != NULL || in->parent != NULL) {
31       if (in->parent != NULL) {
32           in = in->parent;
33       } else {
34           in = in->prev;
35       }
36    }
37 
38    /* now walk the list and free stuff */
39    while (in != NULL) {
40       /* is there a child? */
41       if (in->child) {
42          /* disconnect */
43          in->child->parent = NULL;
44          der_sequence_free(in->child);
45       }
46 
47       switch (in->type) {
48          case LTC_ASN1_SETOF: break;
49          case LTC_ASN1_INTEGER : if (in->data != NULL) { mp_clear(in->data); } break;
50          default               : if (in->data != NULL) { XFREE(in->data);    }
51       }
52 
53       /* move to next and free current */
54       l = in->next;
55       XFREE(in);
56       in = l;
57    }
58 }
59 
60 #endif
61 
62 /* ref:         $Format:%D$ */
63 /* git commit:  $Format:%H$ */
64 /* commit time: $Format:%ai$ */
65