-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnsdp_property.c
86 lines (68 loc) · 1.64 KB
/
nsdp_property.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include "nsdp_property.h"
nsdp_property_t*
nsdp_property_new(unsigned tag, unsigned length)
{
nsdp_property_t *prop;
prop = calloc(1, sizeof(*prop) + length);
if (!prop)
return NULL;
INIT_LIST_HEAD(&prop->list);
prop->tag = tag;
prop->length = length;
return prop;
}
nsdp_property_t*
nsdp_property_from_data(unsigned tag, unsigned length, const void* data)
{
nsdp_property_t *prop;
prop = nsdp_property_new(tag, length);
if (!prop)
return NULL;
if (length > 0)
memcpy(prop->data, data, length);
return prop;
}
nsdp_property_t*
nsdp_property_from_txt(const struct nsdp_property_desc* desc,
const char* txt)
{
nsdp_property_t* prop;
int len;
if (!desc || !txt)
return NULL;
len = desc->type->from_text(txt, NULL, 0);
if (len < 0)
return NULL;
prop = nsdp_property_new(desc->tag, len);
if (!prop)
return NULL;
len = desc->type->from_text(txt, prop->data, len);
if (len < 0) {
nsdp_property_free(prop);
return NULL;
}
return prop;
}
const struct nsdp_property_desc*
nsdp_property_get_desc(const nsdp_property_t *prop)
{
return prop ? nsdp_get_property_desc_from_tag(prop->tag) : NULL;
}
int nsdp_property_to_txt(const nsdp_property_t *prop, char* txt,
unsigned size)
{
const struct nsdp_property_desc* desc = nsdp_property_get_desc(prop);
return desc ?
desc->type->to_text(prop->data, prop->length, txt, size) :
-EINVAL;
}
void nsdp_property_free(nsdp_property_t *prop)
{
list_del(&prop->list);
free(prop);
}