2 * lib/data.c Abstract Data
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup data Abstract Data
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/utils.h>
21 #include <linux/socket.h>
29 * Allocate a new abstract data object.
30 * @arg buf Data buffer containing the actual data.
31 * @arg size Size of data buffer.
33 * Allocates a new abstract data and copies the specified data
34 * buffer into the new handle.
36 * @return Newly allocated data handle or NULL
38 struct nl_data *nl_data_alloc(void *buf, size_t size)
42 data = calloc(1, sizeof(*data));
46 data->d_data = calloc(1, size);
55 memcpy(data->d_data, buf, size);
63 * Allocate abstract data object based on netlink attribute.
64 * @arg nla Netlink attribute of unspecific type.
66 * Allocates a new abstract data and copies the payload of the
67 * attribute to the abstract data object.
70 * @return Newly allocated data handle or NULL
72 struct nl_data *nl_data_alloc_attr(struct nlattr *nla)
74 return nl_data_alloc(nla_data(nla), nla_len(nla));
78 * Clone an abstract data object.
79 * @arg src Abstract data object
81 * @return Cloned object or NULL
83 struct nl_data *nl_data_clone(struct nl_data *src)
85 return nl_data_alloc(src->d_data, src->d_size);
89 * Append data to an abstract data object.
90 * @arg data Abstract data object.
91 * @arg buf Data buffer containing the data to be appended.
92 * @arg size Size of data to be apppended.
94 * Reallocates an abstract data and copies the specified data
95 * buffer into the new handle.
97 * @return 0 on success or a negative error code
99 int nl_data_append(struct nl_data *data, void *buf, size_t size)
105 data->d_data = realloc(data->d_data, data->d_size + size);
110 memcpy(data->d_data + data->d_size, buf, size);
112 memset(data->d_data + data->d_size, 0, size);
114 data->d_size += size;
121 * Free an abstract data object.
122 * @arg data Abstract data object.
124 void nl_data_free(struct nl_data *data)
135 * @name Attribute Access
140 * Get data buffer of abstract data object.
141 * @arg data Abstract data object.
142 * @return Data buffer or NULL if empty.
144 void *nl_data_get(struct nl_data *data)
146 return data->d_size > 0 ? data->d_data : NULL;
150 * Get size of data buffer of abstract data object.
151 * @arg data Abstract data object.
152 * @return Size of data buffer.
154 size_t nl_data_get_size(struct nl_data *data)
167 * Compare two abstract data objects.
168 * @arg a Abstract data object.
169 * @arg b Another abstract data object.
170 * @return An integer less than, equal to, or greater than zero if
171 * a is found, respectively, to be less than, to match, or
174 int nl_data_cmp(struct nl_data *a, struct nl_data *b)
176 void *a_ = nl_data_get(a);
177 void *b_ = nl_data_get(b);
180 return memcmp(a_, b_, nl_data_get_size(a));