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;
150 unpack a ldb message from a linear buffer in ldb_val
152 Free with ldb_unpack_data_free()
154 int ldb_unpack_data(struct ldb_context
*ldb
,
155 const struct ldb_val
*data
,
156 struct ldb_message
*message
)
159 unsigned int remaining
;
164 message
->elements
= NULL
;
167 if (data
->length
< 8) {
172 format
= pull_uint32(p
, 0);
173 message
->num_elements
= pull_uint32(p
, 4);
176 remaining
= data
->length
- 8;
179 case LDB_PACKING_FORMAT_NODN
:
183 case LDB_PACKING_FORMAT
:
184 len
= strnlen((char *)p
, remaining
);
185 if (len
== remaining
) {
189 message
->dn
= ldb_dn_new(message
, ldb
, (char *)p
);
190 if (message
->dn
== NULL
) {
194 remaining
-= len
+ 1;
203 if (message
->num_elements
== 0) {
207 if (message
->num_elements
> remaining
/ 6) {
212 message
->elements
= talloc_array(message
, struct ldb_message_element
, message
->num_elements
);
213 if (!message
->elements
) {
218 memset(message
->elements
, 0,
219 message
->num_elements
* sizeof(struct ldb_message_element
));
221 for (i
=0;i
<message
->num_elements
;i
++) {
222 if (remaining
< 10) {
226 len
= strnlen((char *)p
, remaining
-6);
227 if (len
== remaining
-6) {
235 message
->elements
[i
].flags
= 0;
236 message
->elements
[i
].name
= talloc_strndup(message
->elements
, (char *)p
, len
);
237 if (message
->elements
[i
].name
== NULL
) {
241 remaining
-= len
+ 1;
243 message
->elements
[i
].num_values
= pull_uint32(p
, 0);
244 message
->elements
[i
].values
= NULL
;
245 if (message
->elements
[i
].num_values
!= 0) {
246 message
->elements
[i
].values
= talloc_array(message
->elements
,
248 message
->elements
[i
].num_values
);
249 if (!message
->elements
[i
].values
) {
256 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
257 len
= pull_uint32(p
, 0);
258 if (len
> remaining
-5) {
263 message
->elements
[i
].values
[j
].length
= len
;
264 message
->elements
[i
].values
[j
].data
= talloc_size(message
->elements
[i
].values
, len
+1);
265 if (message
->elements
[i
].values
[j
].data
== NULL
) {
269 memcpy(message
->elements
[i
].values
[j
].data
, p
+4, len
);
270 message
->elements
[i
].values
[j
].data
[len
] = 0;
272 remaining
-= len
+4+1;
277 if (remaining
!= 0) {
278 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
279 "Error: %d bytes unread in ldb_unpack_data", remaining
);
285 talloc_free(message
->elements
);