summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/common/ldb_msg.c
blob: 633cc91f2c6a96e953983e5cfee3561de97f3b52 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* 
   ldb database library

   Copyright (C) Andrew Tridgell  2004

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 *  Name: ldb
 *
 *  Component: ldb message component utility functions
 *
 *  Description: functions for manipulating ldb_message structures
 *
 *  Author: Andrew Tridgell
 */

#include "includes.h"


/*
  find an element in a message by attribute name
*/
struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, 
						 const char *attr_name)
{
	int i;
	for (i=0;i<msg->num_elements;i++) {
		if (strcmp(msg->elements[i].name, attr_name) == 0) {
			return &msg->elements[i];
		}
	}
	return NULL;
}


/*
  find a value in an element
*/
struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, 
				 struct ldb_val *val)
{
	int i;
	for (i=0;i<el->num_values;i++) {
		if (ldb_val_equal(val, &el->values[i])) {
			return &el->values[i];
		}
	}
	return NULL;
}


/*
  add an empty element to a message
*/
int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
{
	struct ldb_message_element *els;

	els = realloc_p(msg->elements, struct ldb_message_element, msg->num_elements+1);
	if (!els) {
		errno = ENOMEM;
		return -1;
	}

	els[msg->num_elements].values = NULL;
	els[msg->num_elements].num_values = 0;
	els[msg->num_elements].flags = flags;
	els[msg->num_elements].name = strdup(attr_name);
	if (!els[msg->num_elements].name) {
		return -1;
	}

	msg->elements = els;
	msg->num_elements++;

	return 0;
}

/*
  add an empty element to a message
*/
int ldb_msg_add(struct ldb_message *msg, 
		const struct ldb_message_element *el, 
		int flags)
{
	if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
		return -1;
	}

	msg->elements[msg->num_elements-1] = *el;
	msg->elements[msg->num_elements-1].flags = flags;

	return 0;
}

/*
  compare two ldb_message_element structures
*/
int ldb_msg_element_compare(struct ldb_message_element *el1, 
			    struct ldb_message_element *el2)
{
	int i;

	if (el1->num_values != el2->num_values) {
		return el1->num_values - el2->num_values;
	}

	for (i=0;i<el1->num_values;i++) {
		if (!ldb_msg_find_val(el2, &el1->values[i])) {
			return -1;
		}
	}

	return 0;
}