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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
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"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.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)
{
unsigned int i;
for (i=0;i<msg->num_elements;i++) {
if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) {
return &msg->elements[i];
}
}
return NULL;
}
/*
see if two ldb_val structures contain exactly the same data
return 1 for a match, 0 for a mis-match
*/
int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
{
if (v1->length != v2->length) return 0;
if (v1->length == 0) return 1;
if (memcmp(v1->data, v2->data, v1->length) == 0) {
return 1;
}
return 0;
}
/*
find a value in an element
assumes case sensitive comparison
*/
struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
struct ldb_val *val)
{
unsigned int i;
for (i=0;i<el->num_values;i++) {
if (ldb_val_equal_exact(val, &el->values[i])) {
return &el->values[i];
}
}
return NULL;
}
/*
duplicate a ldb_val structure
*/
struct ldb_val ldb_val_dup(struct ldb_context *ldb,
const struct ldb_val *v)
{
struct ldb_val v2;
v2.length = v->length;
if (v->length == 0) {
v2.data = NULL;
return v2;
}
/* the +1 is to cope with buggy C library routines like strndup
that look one byte beyond */
v2.data = ldb_malloc(ldb, v->length+1);
if (!v2.data) {
v2.length = 0;
return v2;
}
memcpy(v2.data, v->data, v->length);
((char *)v2.data)[v->length] = 0;
return v2;
}
/*
add an empty element to a message
*/
int ldb_msg_add_empty(struct ldb_context *ldb,
struct ldb_message *msg, const char *attr_name, int flags)
{
struct ldb_message_element *els;
els = ldb_realloc_p(ldb, 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 = ldb_strdup(ldb, 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_context *ldb,
struct ldb_message *msg,
const struct ldb_message_element *el,
int flags)
{
if (ldb_msg_add_empty(ldb, msg, el->name, flags) != 0) {
return -1;
}
msg->elements[msg->num_elements-1] = *el;
msg->elements[msg->num_elements-1].flags = flags;
return 0;
}
/*
add a value to a message
*/
int ldb_msg_add_value(struct ldb_context *ldb,
struct ldb_message *msg,
const char *attr_name,
struct ldb_val *val)
{
struct ldb_message_element *el;
struct ldb_val *vals;
el = ldb_msg_find_element(msg, attr_name);
if (!el) {
ldb_msg_add_empty(ldb, msg, attr_name, 0);
el = ldb_msg_find_element(msg, attr_name);
}
if (!el) {
return -1;
}
vals = ldb_realloc_p(ldb, el->values, struct ldb_val, el->num_values+1);
if (!vals) {
errno = ENOMEM;
return -1;
}
el->values = vals;
el->values[el->num_values] = *val;
el->num_values++;
return 0;
}
/*
add a string element to a message
*/
int ldb_msg_add_string(struct ldb_context *ldb, struct ldb_message *msg,
const char *attr_name, char *str)
{
struct ldb_val val;
val.data = str;
val.length = strlen(str);
return ldb_msg_add_value(ldb, msg, attr_name, &val);
}
/*
compare two ldb_message_element structures
assumes case senistive comparison
*/
int ldb_msg_element_compare(struct ldb_message_element *el1,
struct ldb_message_element *el2)
{
unsigned 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;
}
/*
convenience functions to return common types from a message
these return the first value if the attribute is multi-valued
*/
const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
{
struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
if (!el || el->num_values == 0) {
return NULL;
}
return &el->values[0];
}
int ldb_msg_find_int(const struct ldb_message *msg,
const char *attr_name,
int default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return default_value;
}
return strtol(v->data, NULL, 0);
}
unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
const char *attr_name,
unsigned int default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return default_value;
}
return strtoul(v->data, NULL, 0);
}
int64_t ldb_msg_find_int64(const struct ldb_message *msg,
const char *attr_name,
int64_t default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return default_value;
}
return strtoll(v->data, NULL, 0);
}
uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
const char *attr_name,
uint64_t default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return default_value;
}
return strtoull(v->data, NULL, 0);
}
double ldb_msg_find_double(const struct ldb_message *msg,
const char *attr_name,
double default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return default_value;
}
return strtod(v->data, NULL);
}
const char *ldb_msg_find_string(const struct ldb_message *msg,
const char *attr_name,
const char *default_value)
{
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
if (!v || !v->data) {
return default_value;
}
return v->data;
}
|