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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb pack/unpack
30 * Description: pack/unpack routines for ldb messages as key/value blobs
32 * Author: Andrew Tridgell
36 #include "ldb/ldb_tdb/ldb_tdb.h"
38 /* change this if the data format ever changes */
39 #define LTDB_PACKING_FORMAT 0x26011967
41 /* old packing formats */
42 #define LTDB_PACKING_FORMAT_NODN 0x26011966
45 pack a ldb message into a linear buffer in a TDB_DATA
47 note that this routine avoids saving elements with zero values,
48 as these are equivalent to having no element
50 caller frees the data buffer after use
52 int ltdb_pack_data(struct ldb_context *ctx,
53 const struct ldb_message *message,
54 struct TDB_DATA *data)
61 /* work out how big it needs to be */
64 size += 1 + strlen(message->dn);
66 for (i=0;i<message->num_elements;i++) {
67 if (message->elements[i].num_values == 0) {
70 size += 1 + strlen(message->elements[i].name) + 4;
71 for (j=0;j<message->elements[i].num_values;j++) {
72 size += 4 + message->elements[i].values[j].length + 1;
77 data->dptr = malloc(size);
85 SIVAL(p, 0, LTDB_PACKING_FORMAT);
86 SIVAL(p, 4, message->num_elements);
89 /* the dn needs to be packed so we can be case preserving
90 while hashing on a case folded dn */
91 len = strlen(message->dn);
92 memcpy(p, message->dn, len+1);
95 for (i=0;i<message->num_elements;i++) {
96 if (message->elements[i].num_values == 0) {
99 len = strlen(message->elements[i].name);
100 memcpy(p, message->elements[i].name, len+1);
102 SIVAL(p, 0, message->elements[i].num_values);
104 for (j=0;j<message->elements[i].num_values;j++) {
105 SIVAL(p, 0, message->elements[i].values[j].length);
106 memcpy(p+4, message->elements[i].values[j].data,
107 message->elements[i].values[j].length);
108 p[4+message->elements[i].values[j].length] = 0;
109 p += 4 + message->elements[i].values[j].length + 1;
117 free the memory allocated from a ltdb_unpack_data()
119 void ltdb_unpack_data_free(struct ldb_message *message)
123 for (i=0;i<message->num_elements;i++) {
124 if (message->elements[i].values) free(message->elements[i].values);
126 if (message->elements) free(message->elements);
131 unpack a ldb message from a linear buffer in TDB_DATA
133 note that this does not fill in the class and key elements
135 caller frees. Memory for the elements[] and values[] arrays are
136 malloced, but the memory for the elements is re-used from the
137 TDB_DATA data. This means the caller only has to free the elements
138 and values arrays. This can be done with ltdb_unpack_data_free()
140 int ltdb_unpack_data(struct ldb_context *ctx,
141 const struct TDB_DATA *data,
142 struct ldb_message *message)
145 unsigned int remaining;
150 message->elements = NULL;
153 if (data->dsize < 8) {
159 message->num_elements = IVAL(p, 4);
162 remaining = data->dsize - 8;
165 case LTDB_PACKING_FORMAT_NODN:
169 case LTDB_PACKING_FORMAT:
170 len = strnlen(p, remaining);
171 if (len == remaining) {
176 remaining -= len + 1;
185 if (message->num_elements == 0) {
186 message->elements = NULL;
190 if (message->num_elements > remaining / 6) {
195 message->elements = malloc_array_p(struct ldb_message_element,
196 message->num_elements);
198 if (!message->elements) {
203 for (i=0;i<message->num_elements;i++) {
204 if (remaining < 10) {
208 len = strnlen(p, remaining-6);
209 if (len == remaining-6) {
213 message->elements[i].flags = 0;
214 message->elements[i].name = p;
215 remaining -= len + 1;
217 message->elements[i].num_values = IVAL(p, 0);
218 message->elements[i].values = NULL;
219 if (message->elements[i].num_values != 0) {
220 message->elements[i].values = malloc_array_p(struct ldb_val,
221 message->elements[i].num_values);
222 if (!message->elements[i].values) {
228 for (j=0;j<message->elements[i].num_values;j++) {
230 if (len > remaining-5) {
235 message->elements[i].values[j].length = len;
236 message->elements[i].values[j].data = p+4;
237 remaining -= len+4+1;
245 ltdb_unpack_data_free(message);