Add OpenSSL linking exemption to GPL license.
[libradsec.git] / tlv11.c
1 /* Copyright (c) 2006-2009, Stig Venaas, UNINETT AS.
2  * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3  * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
5
6 #ifdef SYS_SOLARIS9
7 #include <sys/inttypes.h>
8 #else
9 #include <stdint.h>
10 #endif
11 #include "list.h"
12 #include "tlv11.h"
13 #include <stdlib.h>
14 #include <string.h>
15 #include <arpa/inet.h>
16
17 struct tlv *maketlv(uint8_t t, uint8_t l, void *v) {
18     struct tlv *tlv;
19
20     tlv = malloc(sizeof(struct tlv));
21     if (!tlv)
22         return NULL;
23     tlv->t = t;
24     tlv->l = l;
25     if (l && v) {
26         tlv->v = malloc(l);
27         if (!tlv->v) {
28             free(tlv);
29             return NULL;
30         }
31         memcpy(tlv->v, v, l);
32     } else
33         tlv->v = NULL;
34     return tlv;
35 }
36
37 struct tlv *copytlv(struct tlv *in) {
38     return in ? maketlv(in->t, in->l, in->v) : NULL;
39 }
40
41 void freetlv(struct tlv *tlv) {
42     if (tlv) {
43         free(tlv->v);
44         free(tlv);
45     }
46 }
47
48 int eqtlv(struct tlv *t1, struct tlv *t2) {
49     if (!t1 || !t2)
50         return t1 == t2;
51     if (t1->t != t2->t || t1->l != t2->l)
52         return 0;
53     return memcmp(t1->v, t2->v, t1->l) == 0;
54 }
55
56 struct list *copytlvlist(struct list *tlvs) {
57     struct list *out;
58     struct list_node *node;
59
60     if (!tlvs)
61         return NULL;
62     out = list_create();
63     if (!out)
64         return NULL;
65     for (node = list_first(tlvs); node; node = list_next(node)) {
66         if (!list_push(out, copytlv((struct tlv *)node->data))) {
67             freetlvlist(out);
68             return NULL;
69         }
70     }
71     return out;
72 }
73
74 void freetlvlist(struct list *tlvs) {
75     struct tlv *tlv;
76     while ((tlv = (struct tlv *)list_shift(tlvs)))
77         freetlv(tlv);
78     list_destroy(tlvs);
79 }
80
81 void rmtlv(struct list *tlvs, uint8_t t) {
82     struct list_node *n, *p;
83     struct tlv *tlv;
84
85     p = NULL;
86     n = list_first(tlvs);
87     while (n) {
88         tlv = (struct tlv *)n->data;
89         if (tlv->t == t) {
90             list_removedata(tlvs, tlv);
91             freetlv(tlv);
92             n = p ? list_next(p) : list_first(tlvs);
93         } else {
94             p = n;
95             n = list_next(n);
96         }
97     }
98 }
99
100 uint8_t *tlv2str(struct tlv *tlv) {
101     uint8_t *s = malloc(tlv->l + 1);
102     if (s) {
103         memcpy(s, tlv->v, tlv->l);
104         s[tlv->l] = '\0';
105     }
106     return s;
107 }
108
109 uint8_t *tlv2buf(uint8_t *p, const struct tlv *tlv) {
110     *p++ = tlv->t;
111     *p++ = tlv->l;
112     if (tlv->l) {
113         if (tlv->v)
114             memcpy(p, tlv->v, tlv->l);
115         else
116             memset(p, 0, tlv->l);
117     }
118     return p;
119 }
120
121 /* Local Variables: */
122 /* c-file-style: "stroustrup" */
123 /* End: */