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/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "ldb/ldb_tdb/ldb_tdb.h"
40 /* change this if the data format ever changes */
41 #define LTDB_PACKING_FORMAT 0x26011967
43 /* old packing formats */
44 #define LTDB_PACKING_FORMAT_NODN 0x26011966
46 /* use a portable integer format */
47 static void put_uint32(uint8_t *p, int ofs, unsigned int val)
51 p[1] = (val>>8) & 0xFF;
52 p[2] = (val>>16) & 0xFF;
53 p[3] = (val>>24) & 0xFF;
56 static unsigned int pull_uint32(uint8_t *p, int ofs)
59 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
63 pack a ldb message into a linear buffer in a TDB_DATA
65 note that this routine avoids saving elements with zero values,
66 as these are equivalent to having no element
68 caller frees the data buffer after use
70 int ltdb_pack_data(struct ldb_module *module,
71 const struct ldb_message *message,
72 struct TDB_DATA *data)
74 struct ldb_context *ldb = module->ldb;
75 unsigned int i, j, real_elements=0;
80 for (i=0;i<message->num_elements;i++) {
81 if (message->elements[i].num_values != 0) {
86 /* work out how big it needs to be */
89 size += 1 + strlen(message->dn);
91 for (i=0;i<message->num_elements;i++) {
92 if (message->elements[i].num_values == 0) {
95 size += 1 + strlen(message->elements[i].name) + 4;
96 for (j=0;j<message->elements[i].num_values;j++) {
97 size += 4 + message->elements[i].values[j].length + 1;
102 data->dptr = talloc_array(ldb, char, size);
110 put_uint32(p, 0, LTDB_PACKING_FORMAT);
111 put_uint32(p, 4, real_elements);
114 /* the dn needs to be packed so we can be case preserving
115 while hashing on a case folded dn */
116 len = strlen(message->dn);
117 memcpy(p, message->dn, len+1);
120 for (i=0;i<message->num_elements;i++) {
121 if (message->elements[i].num_values == 0) {
124 len = strlen(message->elements[i].name);
125 memcpy(p, message->elements[i].name, len+1);
127 put_uint32(p, 0, message->elements[i].num_values);
129 for (j=0;j<message->elements[i].num_values;j++) {
130 put_uint32(p, 0, message->elements[i].values[j].length);
131 memcpy(p+4, message->elements[i].values[j].data,
132 message->elements[i].values[j].length);
133 p[4+message->elements[i].values[j].length] = 0;
134 p += 4 + message->elements[i].values[j].length + 1;
142 unpack a ldb message from a linear buffer in TDB_DATA
144 Free with ltdb_unpack_data_free()
146 int ltdb_unpack_data(struct ldb_module *module,
147 const struct TDB_DATA *data,
148 struct ldb_message *message)
150 struct ldb_context *ldb = module->ldb;
152 unsigned int remaining;
157 message->elements = NULL;
160 if (data->dsize < 8) {
165 format = pull_uint32(p, 0);
166 message->num_elements = pull_uint32(p, 4);
169 remaining = data->dsize - 8;
172 case LTDB_PACKING_FORMAT_NODN:
176 case LTDB_PACKING_FORMAT:
177 len = strnlen(p, remaining);
178 if (len == remaining) {
183 remaining -= len + 1;
192 if (message->num_elements == 0) {
193 message->elements = NULL;
197 if (message->num_elements > remaining / 6) {
202 message->elements = talloc_array(message, struct ldb_message_element, message->num_elements);
203 if (!message->elements) {
208 memset(message->elements, 0,
209 message->num_elements * sizeof(struct ldb_message_element));
211 for (i=0;i<message->num_elements;i++) {
212 if (remaining < 10) {
216 len = strnlen(p, remaining-6);
217 if (len == remaining-6) {
221 message->elements[i].flags = 0;
222 message->elements[i].name = p;
223 remaining -= len + 1;
225 message->elements[i].num_values = pull_uint32(p, 0);
226 message->elements[i].values = NULL;
227 if (message->elements[i].num_values != 0) {
228 message->elements[i].values = talloc_array(message->elements,
230 message->elements[i].num_values);
231 if (!message->elements[i].values) {
238 for (j=0;j<message->elements[i].num_values;j++) {
239 len = pull_uint32(p, 0);
240 if (len > remaining-5) {
245 message->elements[i].values[j].length = len;
246 message->elements[i].values[j].data = p+4;
247 remaining -= len+4+1;
252 if (remaining != 0) {
253 ldb_debug(ldb, LDB_DEBUG_ERROR,
254 "Error: %d bytes unread in ltdb_unpack_data\n", remaining);
260 talloc_free(message->elements);