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 3 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, see <http://www.gnu.org/licenses/>.
27 * Component: ldb pack/unpack
29 * Description: pack/unpack routines for ldb messages as key/value blobs
31 * Author: Andrew Tridgell
34 #include "ldb_private.h"
36 /* change this if the data format ever changes */
37 #define LDB_PACKING_FORMAT 0x26011967
39 /* old packing formats */
40 #define LDB_PACKING_FORMAT_NODN 0x26011966
42 /* use a portable integer format */
43 static void put_uint32(uint8_t *p, int ofs, unsigned int val)
47 p[1] = (val>>8) & 0xFF;
48 p[2] = (val>>16) & 0xFF;
49 p[3] = (val>>24) & 0xFF;
52 static unsigned int pull_uint32(uint8_t *p, int ofs)
55 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
58 static int attribute_storable_values(const struct ldb_message_element *el)
60 if (el->num_values == 0) return 0;
62 if (ldb_attr_cmp(el->name, "distinguishedName") == 0) return 0;
64 return el->num_values;
68 pack a ldb message into a linear buffer in a ldb_val
70 note that this routine avoids saving elements with zero values,
71 as these are equivalent to having no element
73 caller frees the data buffer after use
75 int ldb_pack_data(struct ldb_context *ldb,
76 const struct ldb_message *message,
79 unsigned int i, j, real_elements=0;
85 dn = ldb_dn_get_linearized(message->dn);
91 /* work out how big it needs to be */
94 size += 1 + strlen(dn);
96 for (i=0;i<message->num_elements;i++) {
97 if (attribute_storable_values(&message->elements[i]) == 0) {
103 size += 1 + strlen(message->elements[i].name) + 4;
104 for (j=0;j<message->elements[i].num_values;j++) {
105 size += 4 + message->elements[i].values[j].length + 1;
110 data->data = talloc_array(ldb, uint8_t, size);
118 put_uint32(p, 0, LDB_PACKING_FORMAT);
119 put_uint32(p, 4, real_elements);
122 /* the dn needs to be packed so we can be case preserving
123 while hashing on a case folded dn */
125 memcpy(p, dn, len+1);
128 for (i=0;i<message->num_elements;i++) {
129 if (attribute_storable_values(&message->elements[i]) == 0) {
132 len = strlen(message->elements[i].name);
133 memcpy(p, message->elements[i].name, len+1);
135 put_uint32(p, 0, message->elements[i].num_values);
137 for (j=0;j<message->elements[i].num_values;j++) {
138 put_uint32(p, 0, message->elements[i].values[j].length);
139 memcpy(p+4, message->elements[i].values[j].data,
140 message->elements[i].values[j].length);
141 p[4+message->elements[i].values[j].length] = 0;
142 p += 4 + message->elements[i].values[j].length + 1;
149 static bool ldb_consume_element_data(uint8_t **pp, unsigned int *premaining)
151 unsigned int remaining = *premaining;
153 uint32_t num_values = pull_uint32(p, 0);
159 for (j = 0; j < num_values; j++) {
160 len = pull_uint32(p, 0);
161 if (len > remaining - 5) {
164 remaining -= len + 4 + 1;
168 *premaining = remaining;
173 unpack a ldb message from a linear buffer in ldb_val
175 Free with ldb_unpack_data_free()
177 int ldb_unpack_data_withlist(struct ldb_context *ldb,
178 const struct ldb_val *data,
179 struct ldb_message *message,
180 const char * const *list,
181 unsigned int list_size,
182 unsigned int *nb_elements_in_db)
185 unsigned int remaining;
188 unsigned int nelem = 0;
190 unsigned int found = 0;
196 message->elements = NULL;
199 if (data->length < 8) {
204 format = pull_uint32(p, 0);
205 message->num_elements = pull_uint32(p, 4);
207 if (nb_elements_in_db) {
208 *nb_elements_in_db = message->num_elements;
211 remaining = data->length - 8;
214 case LDB_PACKING_FORMAT_NODN:
218 case LDB_PACKING_FORMAT:
219 len = strnlen((char *)p, remaining);
220 if (len == remaining) {
224 message->dn = ldb_dn_new(message, ldb, (char *)p);
225 if (message->dn == NULL) {
229 remaining -= len + 1;
238 if (message->num_elements == 0) {
242 if (message->num_elements > remaining / 6) {
247 message->elements = talloc_array(message, struct ldb_message_element,
248 message->num_elements);
249 if (!message->elements) {
254 memset(message->elements, 0,
255 message->num_elements * sizeof(struct ldb_message_element));
257 for (i=0;i<message->num_elements;i++) {
258 const char *attr = NULL;
259 struct ldb_message_element *element = NULL;
261 if (remaining < 10) {
265 len = strnlen((char *)p, remaining-6);
266 if (len == remaining-6) {
276 * This is a bit expensive but normally the list is pretty small
277 * also the cost of freeing unused attributes is quite important
278 * and can dwarf the cost of looping
280 if (list_size != 0) {
285 * We know that p has a \0 terminator before the
286 * end of the buffer due to the check above.
288 for (h = 0; h < list_size && found < list_size; h++) {
289 if (ldb_attr_cmp(attr, list[h]) == 0) {
297 remaining -= len + 1;
299 if (!ldb_consume_element_data(&p, &remaining)) {
306 element = &message->elements[nelem];
307 element->name = talloc_strndup(message->elements, (char *)p, len);
308 if (element->name == NULL) {
313 remaining -= len + 1;
315 element->num_values = pull_uint32(p, 0);
316 element->values = NULL;
317 if (element->num_values != 0) {
318 element->values = talloc_array(message->elements,
320 element->num_values);
321 if (!element->values) {
328 for (j = 0; j < element->num_values; j++) {
329 len = pull_uint32(p, 0);
330 if (len > remaining-5) {
335 element->values[j].length = len;
336 element->values[j].data = talloc_size(element->values, len+1);
337 if (element->values[j].data == NULL) {
341 memcpy(element->values[j].data, p + 4,
343 element->values[j].data[len] = 0;
345 remaining -= len+4+1;
351 * Adapt the number of elements to the real number of unpacked elements,
352 * it means that we overallocated elements array, I guess it's not that
355 message->num_elements = nelem;
357 if (remaining != 0) {
358 ldb_debug(ldb, LDB_DEBUG_ERROR,
359 "Error: %d bytes unread in ldb_unpack_data_withlist",
366 talloc_free(message->elements);
370 int ldb_unpack_data(struct ldb_context *ldb,
371 const struct ldb_val *data,
372 struct ldb_message *message)
374 return ldb_unpack_data_withlist(ldb, data, message, NULL, 0, NULL);