4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb message component utility functions
30 * Description: functions for manipulating ldb_message structures
32 * Author: Andrew Tridgell
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
41 find an element in a message by attribute name
43 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
44 const char *attr_name)
47 for (i=0;i<msg->num_elements;i++) {
48 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
49 return &msg->elements[i];
56 see if two ldb_val structures contain exactly the same data
57 return 1 for a match, 0 for a mis-match
59 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
61 if (v1->length != v2->length) return 0;
63 if (v1->length == 0) return 1;
65 if (memcmp(v1->data, v2->data, v1->length) == 0) {
73 find a value in an element
74 assumes case sensitive comparison
76 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
80 for (i=0;i<el->num_values;i++) {
81 if (ldb_val_equal_exact(val, &el->values[i])) {
82 return &el->values[i];
89 duplicate a ldb_val structure
91 struct ldb_val ldb_val_dup(struct ldb_context *ldb,
92 const struct ldb_val *v)
95 v2.length = v->length;
101 /* the +1 is to cope with buggy C library routines like strndup
102 that look one byte beyond */
103 v2.data = ldb_malloc(ldb, v->length+1);
109 memcpy(v2.data, v->data, v->length);
110 ((char *)v2.data)[v->length] = 0;
115 add an empty element to a message
117 int ldb_msg_add_empty(struct ldb_context *ldb,
118 struct ldb_message *msg, const char *attr_name, int flags)
120 struct ldb_message_element *els;
122 els = ldb_realloc_p(ldb, msg->elements,
123 struct ldb_message_element, msg->num_elements+1);
129 els[msg->num_elements].values = NULL;
130 els[msg->num_elements].num_values = 0;
131 els[msg->num_elements].flags = flags;
132 els[msg->num_elements].name = ldb_strdup(ldb, attr_name);
133 if (!els[msg->num_elements].name) {
144 add an empty element to a message
146 int ldb_msg_add(struct ldb_context *ldb,
147 struct ldb_message *msg,
148 const struct ldb_message_element *el,
151 if (ldb_msg_add_empty(ldb, msg, el->name, flags) != 0) {
155 msg->elements[msg->num_elements-1] = *el;
156 msg->elements[msg->num_elements-1].flags = flags;
162 add a value to a message
164 int ldb_msg_add_value(struct ldb_context *ldb,
165 struct ldb_message *msg,
166 const char *attr_name,
169 struct ldb_message_element *el;
170 struct ldb_val *vals;
172 el = ldb_msg_find_element(msg, attr_name);
174 ldb_msg_add_empty(ldb, msg, attr_name, 0);
175 el = ldb_msg_find_element(msg, attr_name);
181 vals = ldb_realloc_p(ldb, el->values, struct ldb_val, el->num_values+1);
187 el->values[el->num_values] = *val;
195 add a string element to a message
197 int ldb_msg_add_string(struct ldb_context *ldb, struct ldb_message *msg,
198 const char *attr_name, char *str)
203 val.length = strlen(str);
205 return ldb_msg_add_value(ldb, msg, attr_name, &val);
209 compare two ldb_message_element structures
210 assumes case senistive comparison
212 int ldb_msg_element_compare(struct ldb_message_element *el1,
213 struct ldb_message_element *el2)
217 if (el1->num_values != el2->num_values) {
218 return el1->num_values - el2->num_values;
221 for (i=0;i<el1->num_values;i++) {
222 if (!ldb_msg_find_val(el2, &el1->values[i])) {
231 compare two ldb_message_element structures
232 comparing by element name
234 int ldb_msg_element_compare_name(struct ldb_message_element *el1,
235 struct ldb_message_element *el2)
237 return ldb_attr_cmp(el1->name, el2->name);
241 convenience functions to return common types from a message
242 these return the first value if the attribute is multi-valued
244 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
246 struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
247 if (!el || el->num_values == 0) {
250 return &el->values[0];
253 int ldb_msg_find_int(const struct ldb_message *msg,
254 const char *attr_name,
257 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
258 if (!v || !v->data) {
259 return default_value;
261 return strtol(v->data, NULL, 0);
264 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
265 const char *attr_name,
266 unsigned int default_value)
268 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
269 if (!v || !v->data) {
270 return default_value;
272 return strtoul(v->data, NULL, 0);
275 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
276 const char *attr_name,
277 int64_t default_value)
279 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
280 if (!v || !v->data) {
281 return default_value;
283 return strtoll(v->data, NULL, 0);
286 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
287 const char *attr_name,
288 uint64_t default_value)
290 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
291 if (!v || !v->data) {
292 return default_value;
294 return strtoull(v->data, NULL, 0);
297 double ldb_msg_find_double(const struct ldb_message *msg,
298 const char *attr_name,
299 double default_value)
301 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
302 if (!v || !v->data) {
303 return default_value;
305 return strtod(v->data, NULL);
308 const char *ldb_msg_find_string(const struct ldb_message *msg,
309 const char *attr_name,
310 const char *default_value)
312 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
313 if (!v || !v->data) {
314 return default_value;
321 sort the elements of a message by name
323 void ldb_msg_sort_elements(struct ldb_message *msg)
325 qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element),
326 (comparison_fn_t)ldb_msg_element_compare_name);
331 free a message created using ldb_msg_copy
333 void ldb_msg_free(struct ldb_context *ldb, struct ldb_message *msg)
337 for (i=0;i<msg->num_elements;i++) {
338 struct ldb_message_element *el = &msg->elements[i];
339 for (j=0;j<el->num_values;j++) {
340 ldb_free(ldb, el->values[j].data);
342 if (el->values) ldb_free(ldb, el->values);
343 ldb_free(ldb, el->name);
345 if (msg->elements) ldb_free(ldb, msg->elements);
346 ldb_free(ldb, msg->dn);
351 copy a message, allocating new memory for all parts
353 struct ldb_message *ldb_msg_copy(struct ldb_context *ldb,
354 const struct ldb_message *msg)
356 struct ldb_message *msg2;
359 msg2 = ldb_malloc_p(ldb, struct ldb_message);
360 if (msg2 == NULL) return NULL;
362 msg2->elements = NULL;
363 msg2->num_elements = 0;
364 msg2->private_data = NULL;
366 msg2->dn = ldb_strdup(ldb, msg->dn);
367 if (msg2->dn == NULL) goto failed;
369 msg2->elements = ldb_malloc_array_p(ldb, struct ldb_message_element, msg->num_elements);
370 if (msg2->elements == NULL) goto failed;
372 for (i=0;i<msg->num_elements;i++) {
373 struct ldb_message_element *el1 = &msg->elements[i];
374 struct ldb_message_element *el2 = &msg2->elements[i];
376 el2->flags = el1->flags;
379 el2->name = ldb_strdup(ldb, el1->name);
380 if (el2->name == NULL) goto failed;
381 el2->values = ldb_malloc_array_p(ldb, struct ldb_val, el1->num_values);
382 for (j=0;j<el1->num_values;j++) {
383 el2->values[j] = ldb_val_dup(ldb, &el1->values[j]);
384 if (el2->values[j].data == NULL &&
385 el1->values[j].length != 0) {
391 msg2->num_elements++;
397 ldb_msg_free(ldb, msg2);
403 canonicalise a message, merging elements of the same name
405 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
406 const struct ldb_message *msg)
409 struct ldb_message *msg2;
411 msg2 = ldb_msg_copy(ldb, msg);
412 if (msg2 == NULL) return NULL;
414 ldb_msg_sort_elements(msg2);
416 for (i=1;i<msg2->num_elements;i++) {
417 struct ldb_message_element *el1 = &msg2->elements[i-1];
418 struct ldb_message_element *el2 = &msg2->elements[i];
419 if (ldb_msg_element_compare_name(el1, el2) == 0) {
420 el1->values = ldb_realloc_p(ldb, el1->values, struct ldb_val,
421 el1->num_values + el2->num_values);
422 if (el1->values == NULL) {
425 memcpy(el1->values + el1->num_values,
427 sizeof(struct ldb_val) * el2->num_values);
428 el1->num_values += el2->num_values;
429 ldb_free(ldb, el2->name);
430 ldb_free(ldb, el2->values);
431 if (i+1<msg2->num_elements) {
432 memmove(el2, el2+1, sizeof(struct ldb_message_element) *
433 (msg2->num_elements - (i+1)));
435 msg2->num_elements--;