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"
37 * These macros are from byte_array.h via libssh
38 * TODO: This will be replaced with use of the byte_array.h header when it
41 * Macros for handling integer types in byte arrays
43 * This file is originally from the libssh.org project
45 * Copyright (c) 2018 Andreas Schneider <asn@cryptomilk.org>
47 * This library is free software; you can redistribute it and/or
48 * modify it under the terms of the GNU Lesser General Public
49 * License as published by the Free Software Foundation; either
50 * version 2.1 of the License, or (at your option) any later version.
52 * This library is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55 * Lesser General Public License for more details.
57 * You should have received a copy of the GNU Lesser General Public
58 * License along with this library; if not, write to the Free Software
59 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
61 #define _DATA_BYTE_CONST(data, pos) \
62 ((uint8_t)(((const uint8_t *)(data))[(pos)]))
63 #define PULL_LE_U8(data, pos) \
64 (_DATA_BYTE_CONST(data, pos))
65 #define PULL_LE_U16(data, pos) \
66 ((uint16_t)PULL_LE_U8(data, pos) |\
67 ((uint16_t)(PULL_LE_U8(data, (pos) + 1))) << 8)
68 #define PULL_LE_U32(data, pos) \
69 ((uint32_t)(PULL_LE_U16(data, pos) |\
70 ((uint32_t)PULL_LE_U16(data, (pos) + 2)) << 16))
72 #define _DATA_BYTE(data, pos) \
73 (((uint8_t *)(data))[(pos)])
74 #define PUSH_LE_U8(data, pos, val) \
75 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
76 #define PUSH_LE_U16(data, pos, val) \
77 (PUSH_LE_U8((data), (pos), (uint8_t)((uint16_t)(val) & 0xff)),\
78 PUSH_LE_U8((data), (pos) + 1,\
79 (uint8_t)((uint16_t)(val) >> 8)))
80 #define PUSH_LE_U32(data, pos, val) \
81 (PUSH_LE_U16((data), (pos), (uint16_t)((uint32_t)(val) & 0xffff)),\
82 PUSH_LE_U16((data), (pos) + 2, (uint16_t)((uint32_t)(val) >> 16)))
87 #define NULL_PAD_BYTE_LEN 1
89 static int attribute_storable_values(const struct ldb_message_element
*el
)
91 if (el
->num_values
== 0) return 0;
93 if (ldb_attr_cmp(el
->name
, "distinguishedName") == 0) return 0;
95 return el
->num_values
;
98 static int ldb_pack_data_v1(struct ldb_context
*ldb
,
99 const struct ldb_message
*message
,
100 struct ldb_val
*data
)
102 unsigned int i
, j
, real_elements
=0;
103 size_t size
, dn_len
, attr_len
, value_len
;
108 dn
= ldb_dn_get_linearized(message
->dn
);
114 /* work out how big it needs to be */
115 size
= U32_LEN
* 2 + NULL_PAD_BYTE_LEN
;
118 if (size
+ dn_len
< size
) {
125 * First calculate the buffer size we need, and check for
128 for (i
=0;i
<message
->num_elements
;i
++) {
129 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
135 if (size
+ U32_LEN
+ NULL_PAD_BYTE_LEN
< size
) {
139 size
+= U32_LEN
+ NULL_PAD_BYTE_LEN
;
141 attr_len
= strlen(message
->elements
[i
].name
);
142 if (size
+ attr_len
< size
) {
148 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
149 if (size
+ U32_LEN
+ NULL_PAD_BYTE_LEN
< size
) {
153 size
+= U32_LEN
+ NULL_PAD_BYTE_LEN
;
155 value_len
= message
->elements
[i
].values
[j
].length
;
156 if (size
+ value_len
< size
) {
165 data
->data
= talloc_array(ldb
, uint8_t, size
);
173 PUSH_LE_U32(p
, 0, LDB_PACKING_FORMAT
);
175 PUSH_LE_U32(p
, 0, real_elements
);
178 /* the dn needs to be packed so we can be case preserving
179 while hashing on a case folded dn */
181 memcpy(p
, dn
, len
+NULL_PAD_BYTE_LEN
);
182 p
+= len
+ NULL_PAD_BYTE_LEN
;
184 for (i
=0;i
<message
->num_elements
;i
++) {
185 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
188 len
= strlen(message
->elements
[i
].name
);
189 memcpy(p
, message
->elements
[i
].name
, len
+NULL_PAD_BYTE_LEN
);
190 p
+= len
+ NULL_PAD_BYTE_LEN
;
191 PUSH_LE_U32(p
, 0, message
->elements
[i
].num_values
);
193 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
195 message
->elements
[i
].values
[j
].length
);
197 memcpy(p
, message
->elements
[i
].values
[j
].data
,
198 message
->elements
[i
].values
[j
].length
);
199 p
[message
->elements
[i
].values
[j
].length
] = 0;
200 p
+= message
->elements
[i
].values
[j
].length
+
209 * New pack version designed based on performance profiling of version 1.
210 * The approach is to separate value data from the rest of the record's data.
211 * This improves performance because value data is not needed during unpacking
212 * or filtering of the message's attribute list. During filtering we only copy
213 * attributes which are present in the attribute list, however at the parse
214 * stage we need to point to all attributes as they may be referenced in the
216 * With this new format, we don't lose time loading data (eg via
217 * talloc_memdup()) that is never needed (for the vast majority of attributes
218 * are are never found in either the search expression or attribute list).
219 * Additional changes include adding a canonicalized DN (for later
220 * optimizations) and variable width length fields for faster unpacking.
221 * The pack and unpack performance improvement is tested in the torture
222 * test torture_ldb_pack_format_perf.
227 * Number of Elements (4 bytes)
228 * DN length (4 bytes)
229 * DN with null terminator (DN length + 1 bytes)
230 * Canonicalized DN length (4 bytes)
231 * Canonicalized DN with null terminator (Canonicalized DN length + 1 bytes)
232 * Number of bytes from here to value data section (4 bytes)
233 * # For each element:
234 * Element name length (4 bytes)
235 * Element name with null terminator (Element name length + 1 bytes)
236 * Number of values (4 bytes)
237 * Width of value lengths
239 * Value data length (#bytes given by width field above)
240 * # For each element:
242 * Value data (#bytes given by corresponding length above)
244 static int ldb_pack_data_v2(struct ldb_context
*ldb
,
245 const struct ldb_message
*message
,
246 struct ldb_val
*data
)
248 unsigned int i
, j
, real_elements
=0;
249 size_t size
, dn_len
, dn_canon_len
, attr_len
, value_len
;
250 const char *dn
, *dn_canon
;
254 uint8_t val_len_width
;
257 * First half of this function will calculate required size for
258 * packed data. Initial size is 20 = 5 * 4. 5 fixed fields are:
259 * version, num elements, dn len, canon dn len, attr section len
264 * Get linearized and canonicalized form of the DN and add the lengths
265 * of each to size, plus 1 for null terminator.
267 dn
= ldb_dn_get_linearized(message
->dn
);
273 dn_len
= strlen(dn
) + NULL_PAD_BYTE_LEN
;
274 if (size
+ dn_len
< size
) {
280 if (ldb_dn_is_special(message
->dn
)) {
281 dn_canon_len
= NULL_PAD_BYTE_LEN
;
282 dn_canon
= discard_const_p(char, "\0");
284 dn_canon
= ldb_dn_canonical_string(message
->dn
, message
->dn
);
285 if (dn_canon
== NULL
) {
290 dn_canon_len
= strlen(dn_canon
) + NULL_PAD_BYTE_LEN
;
291 if (size
+ dn_canon_len
< size
) {
296 size
+= dn_canon_len
;
298 /* Add the size required by each element */
299 for (i
=0;i
<message
->num_elements
;i
++) {
300 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
307 * Add length of element name + 9 for:
308 * 1 for null terminator
309 * 4 for element name length field
310 * 4 for number of values field
312 attr_len
= strlen(message
->elements
[i
].name
);
313 if (size
+ attr_len
+ U32_LEN
* 2 + NULL_PAD_BYTE_LEN
< size
) {
317 size
+= attr_len
+ U32_LEN
* 2 + NULL_PAD_BYTE_LEN
;
320 * Find the max value length, so we can calculate the width
321 * required for the value length fields.
324 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
325 value_len
= message
->elements
[i
].values
[j
].length
;
326 if (value_len
> max_val_len
) {
327 max_val_len
= value_len
;
330 if (size
+ value_len
+ NULL_PAD_BYTE_LEN
< size
) {
334 size
+= value_len
+ NULL_PAD_BYTE_LEN
;
337 if (max_val_len
<= UCHAR_MAX
) {
338 val_len_width
= U8_LEN
;
339 } else if (max_val_len
<= USHRT_MAX
) {
340 val_len_width
= U16_LEN
;
341 } else if (max_val_len
<= UINT_MAX
) {
342 val_len_width
= U32_LEN
;
348 /* Total size required for val lengths (re-using variable) */
349 max_val_len
= (val_len_width
*message
->elements
[i
].num_values
);
351 /* Add one for storing the width */
352 max_val_len
+= U8_LEN
;
353 if (size
+ max_val_len
< size
) {
361 data
->data
= talloc_array(ldb
, uint8_t, size
);
368 /* Packing format version and number of element */
370 PUSH_LE_U32(p
, 0, LDB_PACKING_FORMAT_V2
);
372 PUSH_LE_U32(p
, 0, real_elements
);
375 /* Pack DN and Canonicalized DN */
376 PUSH_LE_U32(p
, 0, dn_len
-NULL_PAD_BYTE_LEN
);
378 memcpy(p
, dn
, dn_len
);
381 PUSH_LE_U32(p
, 0, dn_canon_len
-NULL_PAD_BYTE_LEN
);
383 memcpy(p
, dn_canon
, dn_canon_len
);
387 * Save pointer at this point and leave a U32_LEN gap for
388 * storing the size of the attribute names and value lengths
394 for (i
=0;i
<message
->num_elements
;i
++) {
395 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
399 /* Length of el name */
400 len
= strlen(message
->elements
[i
].name
);
401 PUSH_LE_U32(p
, 0, len
);
405 * Even though we have the element name's length, put a null
406 * terminator at the end so if any code uses the name
407 * directly, it'll be safe to do things requiring null
408 * termination like strlen
410 memcpy(p
, message
->elements
[i
].name
, len
+NULL_PAD_BYTE_LEN
);
411 p
+= len
+ NULL_PAD_BYTE_LEN
;
413 PUSH_LE_U32(p
, 0, message
->elements
[i
].num_values
);
417 * Calculate value length width again. It's faster to
418 * calculate it again than do the array management to
419 * store the result during size calculation.
422 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
423 value_len
= message
->elements
[i
].values
[j
].length
;
424 if (value_len
> max_val_len
) {
425 max_val_len
= value_len
;
429 if (max_val_len
<= UCHAR_MAX
) {
430 val_len_width
= U8_LEN
;
431 } else if (max_val_len
<= USHRT_MAX
) {
432 val_len_width
= U16_LEN
;
433 } else if (max_val_len
<= UINT_MAX
) {
434 val_len_width
= U32_LEN
;
441 *p
= val_len_width
& 0xFF;
445 * Pack each value's length using the minimum number of bytes
446 * required, which we just calculated. We repeat the loop
447 * for each case here so the compiler can inline code.
449 if (val_len_width
== U8_LEN
) {
450 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
452 message
->elements
[i
].values
[j
].length
);
455 } else if (val_len_width
== U16_LEN
) {
456 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
458 message
->elements
[i
].values
[j
].length
);
461 } else if (val_len_width
== U32_LEN
) {
462 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
464 message
->elements
[i
].values
[j
].length
);
471 * We've finished packing the attr names and value lengths
472 * section, so store the size in the U32_LEN gap we left
475 PUSH_LE_U32(q
, 0, p
-q
);
477 /* Now pack the values */
478 for (i
=0;i
<message
->num_elements
;i
++) {
479 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
482 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
483 memcpy(p
, message
->elements
[i
].values
[j
].data
,
484 message
->elements
[i
].values
[j
].length
);
487 * Even though we have the data length, put a null
488 * terminator at the end of each value's data so if
489 * any code uses the data directly, it'll be safe to
490 * do things requiring null termination like strlen.
492 p
[message
->elements
[i
].values
[j
].length
] = 0;
493 p
+= message
->elements
[i
].values
[j
].length
+
499 * If we didn't end up at the end of the data here, something has
502 if (p
!= data
->data
+ size
) {
511 pack a ldb message into a linear buffer in a ldb_val
513 note that this routine avoids saving elements with zero values,
514 as these are equivalent to having no element
516 caller frees the data buffer after use
518 int ldb_pack_data(struct ldb_context
*ldb
,
519 const struct ldb_message
*message
,
520 struct ldb_val
*data
,
521 uint32_t pack_format_version
) {
523 if (pack_format_version
== LDB_PACKING_FORMAT
) {
524 return ldb_pack_data_v1(ldb
, message
, data
);
525 } else if (pack_format_version
== LDB_PACKING_FORMAT_V2
) {
526 return ldb_pack_data_v2(ldb
, message
, data
);
534 * Unpack a ldb message from a linear buffer in ldb_val
536 static int ldb_unpack_data_flags_v1(struct ldb_context
*ldb
,
537 const struct ldb_val
*data
,
538 struct ldb_message
*message
,
546 unsigned int nelem
= 0;
548 struct ldb_val
*ldb_val_single_array
= NULL
;
550 message
->elements
= NULL
;
554 /* Format (U32, already read) + U32 for num_elements */
555 if (data
->length
< U32_LEN
* 2) {
560 /* Skip first 4 bytes, format already read */
562 message
->num_elements
= PULL_LE_U32(p
, 0);
565 remaining
= data
->length
- U32_LEN
* 2;
568 case LDB_PACKING_FORMAT_NODN
:
572 case LDB_PACKING_FORMAT
:
574 * With this check, we know that the DN at p is \0
577 dn_len
= strnlen((char *)p
, remaining
);
578 if (dn_len
== remaining
) {
582 if (flags
& LDB_UNPACK_DATA_FLAG_NO_DN
) {
586 blob
.data
= discard_const_p(uint8_t, p
);
587 blob
.length
= dn_len
;
588 message
->dn
= ldb_dn_from_ldb_val(message
, ldb
, &blob
);
589 if (message
->dn
== NULL
) {
595 * Redundant: by definition, remaining must be more
596 * than one less than dn_len, as otherwise it would be
599 if (remaining
< dn_len
+ NULL_PAD_BYTE_LEN
) {
603 remaining
-= dn_len
+ NULL_PAD_BYTE_LEN
;
604 p
+= dn_len
+ NULL_PAD_BYTE_LEN
;
612 if (flags
& LDB_UNPACK_DATA_FLAG_NO_ATTRS
) {
613 message
->num_elements
= 0;
617 if (message
->num_elements
== 0) {
621 if (message
->num_elements
> remaining
/ 6) {
626 message
->elements
= talloc_zero_array(message
, struct ldb_message_element
,
627 message
->num_elements
);
628 if (!message
->elements
) {
634 * In typical use, most values are single-valued. This makes
635 * it quite expensive to allocate an array of ldb_val for each
636 * of these, just to then hold the pointer to the data buffer
637 * So with LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC we allocate this
638 * ahead of time and use it for the single values where possible.
639 * (This is used the the normal search case, but not in the
640 * index case because of caller requirements).
642 if (flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) {
643 ldb_val_single_array
= talloc_array(message
->elements
, struct ldb_val
,
644 message
->num_elements
);
645 if (ldb_val_single_array
== NULL
) {
651 for (i
=0;i
<message
->num_elements
;i
++) {
652 const char *attr
= NULL
;
654 struct ldb_message_element
*element
= NULL
;
657 * Sanity check: Element must be at least the size of empty
658 * attr name and value and NULL terms for each.
660 if (remaining
< U32_LEN
* 2 + NULL_PAD_BYTE_LEN
* 2) {
666 * With this check, we know that the attribute name at
667 * p is \0 terminated.
669 attr_len
= strnlen((char *)p
, remaining
-6);
670 if (attr_len
== remaining
-6) {
680 element
= &message
->elements
[nelem
];
681 element
->name
= attr
;
684 if (remaining
< (attr_len
+ NULL_PAD_BYTE_LEN
)) {
688 remaining
-= attr_len
+ NULL_PAD_BYTE_LEN
;
689 p
+= attr_len
+ NULL_PAD_BYTE_LEN
;
690 element
->num_values
= PULL_LE_U32(p
, 0);
691 element
->values
= NULL
;
692 if ((flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) && element
->num_values
== 1) {
693 element
->values
= &ldb_val_single_array
[nelem
];
694 element
->flags
|= LDB_FLAG_INTERNAL_SHARED_VALUES
;
695 } else if (element
->num_values
!= 0) {
696 element
->values
= talloc_array(message
->elements
,
698 element
->num_values
);
699 if (!element
->values
) {
705 if (remaining
< U32_LEN
) {
709 remaining
-= U32_LEN
;
710 for (j
= 0; j
< element
->num_values
; j
++) {
712 * Sanity check: Value must be at least the size of
713 * empty val and NULL terminator.
715 if (remaining
< U32_LEN
+ NULL_PAD_BYTE_LEN
) {
719 remaining
-= U32_LEN
+ NULL_PAD_BYTE_LEN
;
721 len
= PULL_LE_U32(p
, 0);
722 if (remaining
< len
) {
726 if (len
+ NULL_PAD_BYTE_LEN
< len
) {
731 element
->values
[j
].length
= len
;
732 element
->values
[j
].data
= p
+ U32_LEN
;
734 p
+= len
+ U32_LEN
+ NULL_PAD_BYTE_LEN
;
739 * Adapt the number of elements to the real number of unpacked elements,
740 * it means that we overallocated elements array.
742 message
->num_elements
= nelem
;
745 * Shrink the allocated size. On current talloc behaviour
746 * this will help if we skipped 32 or more attributes.
748 message
->elements
= talloc_realloc(message
, message
->elements
,
749 struct ldb_message_element
,
750 message
->num_elements
);
752 if (remaining
!= 0) {
753 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
754 "Error: %zu bytes unread in ldb_unpack_data_flags",
761 talloc_free(message
->elements
);
766 * Unpack a ldb message from a linear buffer in ldb_val
768 static int ldb_unpack_data_flags_v2(struct ldb_context
*ldb
,
769 const struct ldb_val
*data
,
770 struct ldb_message
*message
,
773 uint8_t *p
, *q
, *end_p
, *value_section_p
;
775 unsigned int nelem
= 0;
777 struct ldb_val
*ldb_val_single_array
= NULL
;
778 uint8_t val_len_width
;
780 message
->elements
= NULL
;
783 end_p
= p
+ data
->length
;
785 /* Skip first 4 bytes, format already read */
788 /* First fields are fixed: num_elements, DN length */
789 if (U32_LEN
* 2 > end_p
- p
) {
794 message
->num_elements
= PULL_LE_U32(p
, 0);
797 len
= PULL_LE_U32(p
, 0);
800 if (len
+ NULL_PAD_BYTE_LEN
> end_p
- p
) {
805 if (flags
& LDB_UNPACK_DATA_FLAG_NO_DN
) {
809 blob
.data
= discard_const_p(uint8_t, p
);
811 message
->dn
= ldb_dn_from_ldb_val(message
, ldb
, &blob
);
812 if (message
->dn
== NULL
) {
818 p
+= len
+ NULL_PAD_BYTE_LEN
;
820 if (*(p
-NULL_PAD_BYTE_LEN
) != '\0') {
825 /* Now skip the canonicalized DN and its length */
826 len
= PULL_LE_U32(p
, 0) + NULL_PAD_BYTE_LEN
;
829 if (len
> end_p
- p
) {
836 if (*(p
-NULL_PAD_BYTE_LEN
) != '\0') {
841 if (flags
& LDB_UNPACK_DATA_FLAG_NO_ATTRS
) {
842 message
->num_elements
= 0;
846 if (message
->num_elements
== 0) {
851 * Sanity check (17 bytes is the minimum element size)
853 if (message
->num_elements
> (end_p
- p
) / 17) {
858 message
->elements
= talloc_zero_array(message
,
859 struct ldb_message_element
,
860 message
->num_elements
);
861 if (!message
->elements
) {
867 * In typical use, most values are single-valued. This makes
868 * it quite expensive to allocate an array of ldb_val for each
869 * of these, just to then hold the pointer to the data buffer.
870 * So with LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC we allocate this
871 * ahead of time and use it for the single values where possible.
872 * (This is used the the normal search case, but not in the
873 * index case because of caller requirements).
875 if (flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) {
876 ldb_val_single_array
= talloc_array(message
->elements
,
878 message
->num_elements
);
879 if (ldb_val_single_array
== NULL
) {
885 q
= p
+ PULL_LE_U32(p
, 0);
889 for (i
=0;i
<message
->num_elements
;i
++) {
890 const char *attr
= NULL
;
892 struct ldb_message_element
*element
= NULL
;
894 /* Sanity check: minimum element size */
895 if ((U32_LEN
* 2) + /* attr name len, num values */
896 (U8_LEN
* 2) + /* value length width, one val length */
897 (NULL_PAD_BYTE_LEN
* 2) /* null for attr name + val */
898 > value_section_p
- p
) {
903 attr_len
= PULL_LE_U32(p
, 0);
912 p
+= attr_len
+ NULL_PAD_BYTE_LEN
;
914 * num_values, val_len_width
916 * val_len_width is the width specifier
917 * for the variable length encoding
919 if (U32_LEN
+ U8_LEN
> value_section_p
- p
) {
924 if (*(p
-NULL_PAD_BYTE_LEN
) != '\0') {
929 element
= &message
->elements
[nelem
];
930 element
->name
= attr
;
933 element
->num_values
= PULL_LE_U32(p
, 0);
934 element
->values
= NULL
;
935 if ((flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) &&
936 element
->num_values
== 1) {
937 element
->values
= &ldb_val_single_array
[nelem
];
938 element
->flags
|= LDB_FLAG_INTERNAL_SHARED_VALUES
;
939 } else if (element
->num_values
!= 0) {
940 element
->values
= talloc_array(message
->elements
,
942 element
->num_values
);
943 if (!element
->values
) {
952 * Here we read how wide the remaining lengths are
953 * which avoids storing and parsing a lot of leading
959 if (val_len_width
* element
->num_values
>
960 value_section_p
- p
) {
966 * This is structured weird for compiler optimization
967 * purposes, but we need to pull the array of widths
968 * with different macros depending on how wide the
969 * biggest one is (specified by val_len_width)
971 if (val_len_width
== U8_LEN
) {
972 for (j
= 0; j
< element
->num_values
; j
++) {
973 element
->values
[j
].length
= PULL_LE_U8(p
, 0);
976 } else if (val_len_width
== U16_LEN
) {
977 for (j
= 0; j
< element
->num_values
; j
++) {
978 element
->values
[j
].length
= PULL_LE_U16(p
, 0);
981 } else if (val_len_width
== U32_LEN
) {
982 for (j
= 0; j
< element
->num_values
; j
++) {
983 element
->values
[j
].length
= PULL_LE_U32(p
, 0);
991 for (j
= 0; j
< element
->num_values
; j
++) {
992 len
= element
->values
[j
].length
;
993 if (len
+ NULL_PAD_BYTE_LEN
< len
) {
997 if (len
+ NULL_PAD_BYTE_LEN
> end_p
- q
) {
1002 element
->values
[j
].data
= q
;
1003 q
+= len
+ NULL_PAD_BYTE_LEN
;
1009 * If p isn't now pointing at the beginning of the value section,
1010 * something went very wrong.
1012 if (p
!= value_section_p
) {
1013 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
1014 "Error: Data corruption in ldb_unpack_data_flags");
1020 * Adapt the number of elements to the real number of unpacked
1021 * elements it means that we overallocated elements array.
1023 message
->num_elements
= nelem
;
1026 * Shrink the allocated size. On current talloc behaviour
1027 * this will help if we skipped 32 or more attributes.
1029 message
->elements
= talloc_realloc(message
, message
->elements
,
1030 struct ldb_message_element
,
1031 message
->num_elements
);
1034 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
1035 "Error: %zu bytes unread in ldb_unpack_data_flags",
1044 talloc_free(message
->elements
);
1048 int ldb_unpack_get_format(const struct ldb_val
*data
,
1049 uint32_t *pack_format_version
)
1051 if (data
->length
< U32_LEN
) {
1052 return LDB_ERR_OPERATIONS_ERROR
;
1054 *pack_format_version
= PULL_LE_U32(data
->data
, 0);
1059 * Unpack a ldb message from a linear buffer in ldb_val
1061 int ldb_unpack_data_flags(struct ldb_context
*ldb
,
1062 const struct ldb_val
*data
,
1063 struct ldb_message
*message
,
1068 if (data
->length
< U32_LEN
) {
1073 format
= PULL_LE_U32(data
->data
, 0);
1074 if (format
== LDB_PACKING_FORMAT_V2
) {
1075 return ldb_unpack_data_flags_v2(ldb
, data
, message
, flags
);
1079 * The v1 function we're about to call takes either LDB_PACKING_FORMAT
1080 * or LDB_PACKING_FORMAT_NODN packing format versions, and will error
1081 * if given some other version, so we don't need to do any further
1082 * checks on 'format'.
1084 return ldb_unpack_data_flags_v1(ldb
, data
, message
, flags
, format
);
1089 * Unpack a ldb message from a linear buffer in ldb_val
1091 * Free with ldb_unpack_data_free()
1093 int ldb_unpack_data(struct ldb_context
*ldb
,
1094 const struct ldb_val
*data
,
1095 struct ldb_message
*message
)
1097 return ldb_unpack_data_flags(ldb
, data
, message
, 0);
1101 add the special distinguishedName element
1103 int ldb_msg_add_distinguished_name(struct ldb_message
*msg
)
1105 const char *dn_attr
= "distinguishedName";
1108 if (ldb_msg_find_element(msg
, dn_attr
)) {
1110 * This should not happen, but this is
1111 * existing behaviour...
1116 dn
= ldb_dn_alloc_linearized(msg
, msg
->dn
);
1118 return LDB_ERR_OPERATIONS_ERROR
;
1121 return ldb_msg_add_steal_string(msg
, dn_attr
, dn
);
1125 * filter the specified list of attributes from msg,
1126 * adding requested attributes, and perhaps all for *,
1127 * but not the DN to filtered_msg.
1129 int ldb_filter_attrs(struct ldb_context
*ldb
,
1130 const struct ldb_message
*msg
,
1131 const char *const *attrs
,
1132 struct ldb_message
*filtered_msg
)
1135 bool keep_all
= false;
1136 bool add_dn
= false;
1137 uint32_t num_elements
;
1138 uint32_t elements_size
;
1141 /* check for special attrs */
1142 for (i
= 0; attrs
[i
]; i
++) {
1143 int cmp
= strcmp(attrs
[i
], "*");
1148 cmp
= ldb_attr_cmp(attrs
[i
], "distinguishedName");
1159 elements_size
= msg
->num_elements
+ 1;
1161 /* Shortcuts for the simple cases */
1162 } else if (add_dn
&& i
== 1) {
1163 if (ldb_msg_add_distinguished_name(filtered_msg
) != 0) {
1167 } else if (i
== 0) {
1171 * Otherwise we are copying at most as many elements as we
1178 filtered_msg
->elements
= talloc_array(filtered_msg
,
1179 struct ldb_message_element
,
1181 if (filtered_msg
->elements
== NULL
) goto failed
;
1185 for (i
= 0; i
< msg
->num_elements
; i
++) {
1186 struct ldb_message_element
*el
= &msg
->elements
[i
];
1189 * el2 is assigned after the Pigeonhole principle
1190 * check below for clarity
1192 struct ldb_message_element
*el2
= NULL
;
1195 if (keep_all
== false) {
1197 for (j
= 0; attrs
[j
]; j
++) {
1198 int cmp
= ldb_attr_cmp(el
->name
, attrs
[j
]);
1204 if (found
== false) {
1210 * Pigeonhole principle: we can't have more elements
1211 * than the number of attributes if they are unique in
1214 if (num_elements
>= elements_size
) {
1218 el2
= &filtered_msg
->elements
[num_elements
];
1221 el2
->name
= talloc_strdup(filtered_msg
->elements
,
1223 if (el2
->name
== NULL
) {
1226 el2
->values
= talloc_array(filtered_msg
->elements
,
1227 struct ldb_val
, el
->num_values
);
1228 if (el2
->values
== NULL
) {
1231 for (j
=0;j
<el
->num_values
;j
++) {
1232 el2
->values
[j
] = ldb_val_dup(el2
->values
, &el
->values
[j
]);
1233 if (el2
->values
[j
].data
== NULL
&& el
->values
[j
].length
!= 0) {
1240 filtered_msg
->num_elements
= num_elements
;
1243 if (ldb_msg_add_distinguished_name(filtered_msg
) != 0) {
1248 if (filtered_msg
->num_elements
> 0) {
1249 filtered_msg
->elements
1250 = talloc_realloc(filtered_msg
,
1251 filtered_msg
->elements
,
1252 struct ldb_message_element
,
1253 filtered_msg
->num_elements
);
1254 if (filtered_msg
->elements
== NULL
) {
1258 TALLOC_FREE(filtered_msg
->elements
);
1263 TALLOC_FREE(filtered_msg
->elements
);
1268 * filter the specified list of attributes from msg,
1269 * adding requested attributes, and perhaps all for *.
1270 * Unlike ldb_filter_attrs(), the DN will not be added
1273 int ldb_filter_attrs_in_place(struct ldb_message
*msg
,
1274 const char *const *attrs
)
1277 bool keep_all
= false;
1278 unsigned int num_del
= 0;
1281 /* check for special attrs */
1282 for (i
= 0; attrs
[i
]; i
++) {
1283 int cmp
= strcmp(attrs
[i
], "*");
1289 if (!keep_all
&& i
== 0) {
1290 msg
->num_elements
= 0;
1297 for (i
= 0; i
< msg
->num_elements
; i
++) {
1304 for (j
= 0; attrs
[j
]; j
++) {
1305 int cmp
= ldb_attr_cmp(msg
->elements
[i
].name
, attrs
[j
]);
1315 } else if (num_del
!= 0) {
1316 msg
->elements
[i
- num_del
] = msg
->elements
[i
];
1320 msg
->num_elements
-= num_del
;
1325 /* Have an unpacked ldb message take talloc ownership of its elements. */
1326 int ldb_msg_elements_take_ownership(struct ldb_message
*msg
)
1330 for (i
= 0; i
< msg
->num_elements
; i
++) {
1331 struct ldb_message_element
*el
= &msg
->elements
[i
];
1335 name
= talloc_strdup(msg
->elements
,
1342 if (el
->flags
& LDB_FLAG_INTERNAL_SHARED_VALUES
) {
1343 struct ldb_val
*values
= talloc_memdup(msg
->elements
, el
->values
,
1344 sizeof(struct ldb_val
) * el
->num_values
);
1345 if (values
== NULL
) {
1348 el
->values
= values
;
1349 el
->flags
&= ~LDB_FLAG_INTERNAL_SHARED_VALUES
;
1352 for (j
= 0; j
< el
->num_values
; j
++) {
1353 struct ldb_val val
= ldb_val_dup(el
->values
, &el
->values
[j
]);
1354 if (val
.data
== NULL
&& el
->values
[j
].length
!= 0) {
1357 el
->values
[j
] = val
;