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"
40 create a new ldb_message in a given memory context (NULL for top level)
42 struct ldb_message *ldb_msg_new(void *mem_ctx)
44 return talloc_zero(mem_ctx, struct ldb_message);
48 find an element in a message by attribute name
50 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
51 const char *attr_name)
54 for (i=0;i<msg->num_elements;i++) {
55 if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
56 return &msg->elements[i];
63 see if two ldb_val structures contain exactly the same data
64 return 1 for a match, 0 for a mis-match
66 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
68 if (v1->length != v2->length) return 0;
70 if (v1->length == 0) return 1;
72 if (memcmp(v1->data, v2->data, v1->length) == 0) {
80 find a value in an element
81 assumes case sensitive comparison
83 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
87 for (i=0;i<el->num_values;i++) {
88 if (ldb_val_equal_exact(val, &el->values[i])) {
89 return &el->values[i];
96 duplicate a ldb_val structure
98 struct ldb_val ldb_val_dup(TALLOC_CTX *mem_ctx,
99 const struct ldb_val *v)
102 v2.length = v->length;
103 if (v->length == 0) {
108 /* the +1 is to cope with buggy C library routines like strndup
109 that look one byte beyond */
110 v2.data = talloc_array(mem_ctx, char, v->length+1);
116 memcpy(v2.data, v->data, v->length);
117 ((char *)v2.data)[v->length] = 0;
122 add an empty element to a message
124 int ldb_msg_add_empty(struct ldb_context *ldb,
125 struct ldb_message *msg, const char *attr_name, int flags)
127 struct ldb_message_element *els;
129 els = talloc_realloc(msg, msg->elements,
130 struct ldb_message_element, msg->num_elements+1);
136 els[msg->num_elements].values = NULL;
137 els[msg->num_elements].num_values = 0;
138 els[msg->num_elements].flags = flags;
139 els[msg->num_elements].name = talloc_strdup(els, attr_name);
140 if (!els[msg->num_elements].name) {
151 add an empty element to a message
153 int ldb_msg_add(struct ldb_context *ldb,
154 struct ldb_message *msg,
155 const struct ldb_message_element *el,
158 if (ldb_msg_add_empty(ldb, msg, el->name, flags) != 0) {
162 msg->elements[msg->num_elements-1] = *el;
163 msg->elements[msg->num_elements-1].flags = flags;
169 add a value to a message
171 int ldb_msg_add_value(struct ldb_context *ldb,
172 struct ldb_message *msg,
173 const char *attr_name,
174 const struct ldb_val *val)
176 struct ldb_message_element *el;
177 struct ldb_val *vals;
179 el = ldb_msg_find_element(msg, attr_name);
181 ldb_msg_add_empty(ldb, msg, attr_name, 0);
182 el = ldb_msg_find_element(msg, attr_name);
188 vals = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1);
194 el->values[el->num_values] = *val;
202 add a string element to a message
204 int ldb_msg_add_string(struct ldb_context *ldb, struct ldb_message *msg,
205 const char *attr_name, const char *str)
209 val.data = discard_const_p(char, str);
210 val.length = strlen(str);
212 return ldb_msg_add_value(ldb, msg, attr_name, &val);
216 compare two ldb_message_element structures
217 assumes case senistive comparison
219 int ldb_msg_element_compare(struct ldb_message_element *el1,
220 struct ldb_message_element *el2)
224 if (el1->num_values != el2->num_values) {
225 return el1->num_values - el2->num_values;
228 for (i=0;i<el1->num_values;i++) {
229 if (!ldb_msg_find_val(el2, &el1->values[i])) {
238 compare two ldb_message_element structures
239 comparing by element name
241 int ldb_msg_element_compare_name(struct ldb_message_element *el1,
242 struct ldb_message_element *el2)
244 return ldb_attr_cmp(el1->name, el2->name);
248 convenience functions to return common types from a message
249 these return the first value if the attribute is multi-valued
251 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
253 struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
254 if (!el || el->num_values == 0) {
257 return &el->values[0];
260 int ldb_msg_find_int(const struct ldb_message *msg,
261 const char *attr_name,
264 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
265 if (!v || !v->data) {
266 return default_value;
268 return strtol(v->data, NULL, 0);
271 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
272 const char *attr_name,
273 unsigned int default_value)
275 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
276 if (!v || !v->data) {
277 return default_value;
279 return strtoul(v->data, NULL, 0);
282 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
283 const char *attr_name,
284 int64_t default_value)
286 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
287 if (!v || !v->data) {
288 return default_value;
290 return strtoll(v->data, NULL, 0);
293 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
294 const char *attr_name,
295 uint64_t default_value)
297 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
298 if (!v || !v->data) {
299 return default_value;
301 return strtoull(v->data, NULL, 0);
304 double ldb_msg_find_double(const struct ldb_message *msg,
305 const char *attr_name,
306 double default_value)
308 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
309 if (!v || !v->data) {
310 return default_value;
312 return strtod(v->data, NULL);
315 const char *ldb_msg_find_string(const struct ldb_message *msg,
316 const char *attr_name,
317 const char *default_value)
319 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
320 if (!v || !v->data) {
321 return default_value;
328 sort the elements of a message by name
330 void ldb_msg_sort_elements(struct ldb_message *msg)
332 qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element),
333 (comparison_fn_t)ldb_msg_element_compare_name);
338 free a message created using ldb_msg_copy
340 void ldb_msg_free(struct ldb_context *ldb, struct ldb_message *msg)
346 copy a message, allocating new memory for all parts
348 struct ldb_message *ldb_msg_copy(struct ldb_context *ldb,
349 const struct ldb_message *msg)
351 struct ldb_message *msg2;
354 msg2 = talloc(ldb, struct ldb_message);
355 if (msg2 == NULL) return NULL;
357 msg2->elements = NULL;
358 msg2->num_elements = 0;
359 msg2->private_data = NULL;
361 msg2->dn = talloc_strdup(msg2, msg->dn);
362 if (msg2->dn == NULL) goto failed;
364 msg2->elements = talloc_array(msg2, struct ldb_message_element, msg->num_elements);
365 if (msg2->elements == NULL) goto failed;
367 for (i=0;i<msg->num_elements;i++) {
368 struct ldb_message_element *el1 = &msg->elements[i];
369 struct ldb_message_element *el2 = &msg2->elements[i];
371 el2->flags = el1->flags;
374 el2->name = talloc_strdup(msg2->elements, el1->name);
375 if (el2->name == NULL) goto failed;
376 el2->values = talloc_array(msg2->elements, struct ldb_val, el1->num_values);
377 for (j=0;j<el1->num_values;j++) {
378 el2->values[j] = ldb_val_dup(ldb, &el1->values[j]);
379 if (el2->values[j].data == NULL &&
380 el1->values[j].length != 0) {
383 el2->values[j].data = talloc_steal(el2->values, el2->values[j].data);
387 msg2->num_elements++;
399 canonicalise a message, merging elements of the same name
401 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
402 const struct ldb_message *msg)
405 struct ldb_message *msg2;
407 msg2 = ldb_msg_copy(ldb, msg);
408 if (msg2 == NULL) return NULL;
410 ldb_msg_sort_elements(msg2);
412 for (i=1;i<msg2->num_elements;i++) {
413 struct ldb_message_element *el1 = &msg2->elements[i-1];
414 struct ldb_message_element *el2 = &msg2->elements[i];
415 if (ldb_msg_element_compare_name(el1, el2) == 0) {
416 el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val,
417 el1->num_values + el2->num_values);
418 if (el1->values == NULL) {
421 memcpy(el1->values + el1->num_values,
423 sizeof(struct ldb_val) * el2->num_values);
424 el1->num_values += el2->num_values;
425 talloc_free(el2->name);
426 talloc_free(el2->values);
427 if (i+1<msg2->num_elements) {
428 memmove(el2, el2+1, sizeof(struct ldb_message_element) *
429 (msg2->num_elements - (i+1)));
431 msg2->num_elements--;