summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/common
diff options
context:
space:
mode:
Diffstat (limited to 'source4/lib/ldb/common')
-rw-r--r--source4/lib/ldb/common/ldb_alloc.c174
-rw-r--r--source4/lib/ldb/common/ldb_ldif.c80
-rw-r--r--source4/lib/ldb/common/ldb_msg.c13
-rw-r--r--source4/lib/ldb/common/ldb_parse.c205
-rw-r--r--source4/lib/ldb/common/ldb_utf8.c4
-rw-r--r--source4/lib/ldb/common/util.c18
6 files changed, 284 insertions, 210 deletions
diff --git a/source4/lib/ldb/common/ldb_alloc.c b/source4/lib/ldb/common/ldb_alloc.c
new file mode 100644
index 0000000000..667759e832
--- /dev/null
+++ b/source4/lib/ldb/common/ldb_alloc.c
@@ -0,0 +1,174 @@
+/*
+ ldb database library
+
+ Copyright (C) Andrew Tridgell 2004
+
+ ** NOTE! The following LGPL license applies to the ldb
+ ** library. This does NOT imply that all of Samba is released
+ ** under the LGPL
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/*
+ * Name: ldb
+ *
+ * Component: ldb alloc
+ *
+ * Description: functions for memory allocation
+ *
+ * Author: Andrew Tridgell
+ */
+
+#include "includes.h"
+
+
+/*
+ this allows the user to choose their own allocation function
+*/
+int ldb_set_alloc(struct ldb_context *ldb,
+ void *(*alloc)(void *context, void *ptr, size_t size),
+ void *context)
+{
+ ldb->alloc_ops.alloc = alloc;
+ ldb->alloc_ops.context = context;
+ return 0;
+}
+
+/*
+ this is the default memory allocation function
+*/
+static void *ldb_default_alloc(void *context, void *ptr, size_t size)
+{
+ /* by setting LDB_ALLOC_OFS to non-zero the test suite can
+ catch any places where we incorrectly use the libc alloc
+ funcitons directly */
+#define LDB_ALLOC_OFS 4
+ /* we don't assume a modern realloc function */
+ if (ptr == NULL) {
+ ptr = malloc(size+LDB_ALLOC_OFS);
+ if (ptr) return ((char *)ptr)+LDB_ALLOC_OFS;
+ return NULL;
+ }
+ if (size == 0) {
+ free(((char *)ptr)-LDB_ALLOC_OFS);
+ return NULL;
+ }
+ ptr = realloc(((char *)ptr)-LDB_ALLOC_OFS, size+LDB_ALLOC_OFS);
+ if (ptr) {
+ return ((char *)ptr)+LDB_ALLOC_OFS;
+ }
+ return NULL;
+}
+
+/*
+ all memory allocation goes via this function
+*/
+void *ldb_realloc(struct ldb_context *ldb, void *ptr, size_t size)
+{
+ if (!ldb->alloc_ops.alloc) {
+ ldb_set_alloc(ldb, ldb_default_alloc, NULL);
+ }
+ return ldb->alloc_ops.alloc(ldb->alloc_ops.context, ptr, size);
+}
+
+void *ldb_malloc(struct ldb_context *ldb, size_t size)
+{
+ return ldb_realloc(ldb, NULL, size);
+}
+
+void ldb_free(struct ldb_context *ldb, void *ptr)
+{
+ if (ptr != NULL) {
+ ldb_realloc(ldb, ptr, 0);
+ }
+}
+
+void *ldb_strndup(struct ldb_context *ldb, const char *str, size_t maxlen)
+{
+ size_t len = strnlen(str, maxlen);
+ void *ret;
+ ret = ldb_realloc(ldb, NULL, len+1);
+ if (ret) {
+ memcpy(ret, str, len);
+ ((char *)ret)[len] = 0;
+ }
+ return ret;
+}
+
+void *ldb_strdup(struct ldb_context *ldb, const char *str)
+{
+ size_t len = strlen(str);
+ void *ret;
+ ret = ldb_realloc(ldb, NULL, len+1);
+ if (ret) {
+ memcpy(ret, str, len+1);
+ }
+ return ret;
+}
+
+/*
+ a ldb wrapper for asprintf(), using ldb_malloc()
+*/
+int ldb_asprintf(struct ldb_context *ldb, char **strp, const char *fmt, ...)
+{
+ int len, len2;
+ va_list ap;
+
+ *strp = NULL;
+
+ va_start(ap, fmt);
+ len = vsnprintf(NULL, 0, fmt, ap);
+ va_end(ap);
+ if (len < 0) {
+ return len;
+ }
+
+ *strp = ldb_malloc(ldb, len+1);
+ if (! *strp) {
+ return -1;
+ }
+
+ va_start(ap, fmt);
+ len2 = vsnprintf(*strp, len+1, fmt, ap);
+ va_end(ap);
+
+ if (len2 != len) {
+ /* buggy (or non-C99) vsnprintf function */
+ ldb_free(ldb, *strp);
+ return -1;
+ }
+
+ return len;
+}
+
+/*
+ realloc an array, checking for integer overflow in the array size
+*/
+void *ldb_realloc_array(struct ldb_context *ldb,
+ void *ptr, size_t el_size, unsigned count)
+{
+#define MAX_MALLOC_SIZE 0x7fffffff
+
+ if (count == 0 ||
+ count >= MAX_MALLOC_SIZE/el_size) {
+ return NULL;
+ }
+ if (!ptr) {
+ return ldb_malloc(ldb, el_size * count);
+ }
+ return ldb_realloc(ldb, ptr, el_size * count);
+}
+
diff --git a/source4/lib/ldb/common/ldb_ldif.c b/source4/lib/ldb/common/ldb_ldif.c
index ef782e90e3..451276c48d 100644
--- a/source4/lib/ldb/common/ldb_ldif.c
+++ b/source4/lib/ldb/common/ldb_ldif.c
@@ -86,7 +86,7 @@ static int base64_decode(char *s)
encode as base64
caller frees
*/
-char *ldb_base64_encode(const char *buf, int len)
+char *ldb_base64_encode(struct ldb_context *ldb, const char *buf, int len)
{
const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int bit_offset, byte_offset, idx, i;
@@ -94,7 +94,7 @@ char *ldb_base64_encode(const char *buf, int len)
int bytes = (len*8 + 5)/6;
char *out;
- out = malloc(bytes+2);
+ out = ldb_malloc(ldb, bytes+2);
if (!out) return NULL;
for (i=0;i<bytes;i++) {
@@ -164,10 +164,12 @@ static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *priva
/*
encode as base64 to a file
*/
-static int base64_encode_f(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
+static int base64_encode_f(struct ldb_context *ldb,
+ int (*fprintf_fn)(void *, const char *, ...),
+ void *private_data,
const char *buf, int len, int start_pos)
{
- char *b = ldb_base64_encode(buf, len);
+ char *b = ldb_base64_encode(ldb, buf, len);
int ret;
if (!b) {
@@ -176,7 +178,7 @@ static int base64_encode_f(int (*fprintf_fn)(void *, const char *, ...), void *p
ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
- free(b);
+ ldb_free(ldb, b);
return ret;
}
@@ -194,7 +196,8 @@ static const struct {
/*
write to ldif, using a caller supplied write method
*/
-int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
+int ldif_write(struct ldb_context *ldb,
+ int (*fprintf_fn)(void *, const char *, ...),
void *private_data,
const struct ldb_ldif *ldif)
{
@@ -244,7 +247,7 @@ int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
ret = fprintf_fn(private_data, "%s:: ",
msg->elements[i].name);
CHECK_RET;
- ret = base64_encode_f(fprintf_fn, private_data,
+ ret = base64_encode_f(ldb, fprintf_fn, private_data,
msg->elements[i].values[j].data,
msg->elements[i].values[j].length,
strlen(msg->elements[i].name)+3);
@@ -282,7 +285,8 @@ int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
caller frees
*/
-static char *next_chunk(int (*fgetc_fn)(void *), void *private_data)
+static char *next_chunk(struct ldb_context *ldb,
+ int (*fgetc_fn)(void *), void *private_data)
{
size_t alloc_size=0, chunk_size = 0;
char *chunk = NULL;
@@ -293,9 +297,9 @@ static char *next_chunk(int (*fgetc_fn)(void *), void *private_data)
if (chunk_size+1 >= alloc_size) {
char *c2;
alloc_size += 1024;
- c2 = realloc_p(chunk, char, alloc_size);
+ c2 = ldb_realloc_p(ldb, chunk, char, alloc_size);
if (!c2) {
- free(chunk);
+ ldb_free(ldb, chunk);
errno = ENOMEM;
return NULL;
}
@@ -402,27 +406,29 @@ static int next_attr(char **s, const char **attr, struct ldb_val *value)
/*
free a message from a ldif_read
*/
-void ldif_read_free(struct ldb_ldif *ldif)
+void ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
{
struct ldb_message *msg = &ldif->msg;
int i;
for (i=0;i<msg->num_elements;i++) {
- if (msg->elements[i].name) free(msg->elements[i].name);
- if (msg->elements[i].values) free(msg->elements[i].values);
+ if (msg->elements[i].name) ldb_free(ldb, msg->elements[i].name);
+ if (msg->elements[i].values) ldb_free(ldb, msg->elements[i].values);
}
- if (msg->elements) free(msg->elements);
- if (msg->private_data) free(msg->private_data);
- free(ldif);
+ if (msg->elements) ldb_free(ldb, msg->elements);
+ if (msg->private_data) ldb_free(ldb, msg->private_data);
+ ldb_free(ldb, ldif);
}
/*
add an empty element
*/
-static int msg_add_empty(struct ldb_message *msg, const char *name, unsigned flags)
+static int msg_add_empty(struct ldb_context *ldb,
+ struct ldb_message *msg, const char *name, unsigned flags)
{
struct ldb_message_element *el2, *el;
- el2 = realloc_p(msg->elements, struct ldb_message_element, msg->num_elements+1);
+ el2 = ldb_realloc_p(ldb, msg->elements,
+ struct ldb_message_element, msg->num_elements+1);
if (!el2) {
errno = ENOMEM;
return -1;
@@ -432,7 +438,7 @@ static int msg_add_empty(struct ldb_message *msg, const char *name, unsigned fla
el = &msg->elements[msg->num_elements];
- el->name = strdup(name);
+ el->name = ldb_strdup(ldb, name);
el->num_values = 0;
el->values = NULL;
el->flags = flags;
@@ -450,7 +456,8 @@ static int msg_add_empty(struct ldb_message *msg, const char *name, unsigned fla
/*
read from a LDIF source, creating a ldb_message
*/
-struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
+struct ldb_ldif *ldif_read(struct ldb_context *ldb,
+ int (*fgetc_fn)(void *), void *private_data)
{
struct ldb_ldif *ldif;
struct ldb_message *msg;
@@ -461,7 +468,7 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
value.data = NULL;
- ldif = malloc_p(struct ldb_ldif);
+ ldif = ldb_malloc_p(ldb, struct ldb_ldif);
if (!ldif) return NULL;
ldif->changetype = LDB_CHANGETYPE_NONE;
@@ -472,7 +479,7 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
msg->num_elements = 0;
msg->private_data = NULL;
- chunk = next_chunk(fgetc_fn, private_data);
+ chunk = next_chunk(ldb, fgetc_fn, private_data);
if (!chunk) {
goto failed;
}
@@ -530,7 +537,7 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
}
if (empty) {
- if (msg_add_empty(msg, (char *)value.data, flags) != 0) {
+ if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
goto failed;
}
continue;
@@ -542,7 +549,8 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
flags == el->flags) {
/* its a continuation */
el->values =
- realloc_p(el->values, struct ldb_val, el->num_values+1);
+ ldb_realloc_p(ldb, el->values,
+ struct ldb_val, el->num_values+1);
if (!el->values) {
goto failed;
}
@@ -550,16 +558,16 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
el->num_values++;
} else {
/* its a new attribute */
- msg->elements = realloc_p(msg->elements,
- struct ldb_message_element,
- msg->num_elements+1);
+ msg->elements = ldb_realloc_p(ldb, msg->elements,
+ struct ldb_message_element,
+ msg->num_elements+1);
if (!msg->elements) {
goto failed;
}
el = &msg->elements[msg->num_elements];
el->flags = flags;
- el->name = strdup(attr);
- el->values = malloc_p(struct ldb_val);
+ el->name = ldb_strdup(ldb, attr);
+ el->values = ldb_malloc_p(ldb, struct ldb_val);
if (!el->values || !el->name) {
goto failed;
}
@@ -572,7 +580,7 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
return ldif;
failed:
- if (ldif) ldif_read_free(ldif);
+ if (ldif) ldif_read_free(ldb, ldif);
return NULL;
}
@@ -591,11 +599,11 @@ static int fgetc_file(void *private_data)
return fgetc(state->f);
}
-struct ldb_ldif *ldif_read_file(FILE *f)
+struct ldb_ldif *ldif_read_file(struct ldb_context *ldb, FILE *f)
{
struct ldif_read_file_state state;
state.f = f;
- return ldif_read(fgetc_file, &state);
+ return ldif_read(ldb, fgetc_file, &state);
}
@@ -615,11 +623,11 @@ static int fgetc_string(void *private_data)
return EOF;
}
-struct ldb_ldif *ldif_read_string(const char *s)
+struct ldb_ldif *ldif_read_string(struct ldb_context *ldb, const char *s)
{
struct ldif_read_string_state state;
state.s = s;
- return ldif_read(fgetc_string, &state);
+ return ldif_read(ldb, fgetc_string, &state);
}
@@ -642,9 +650,9 @@ static int fprintf_file(void *private_data, const char *fmt, ...)
return ret;
}
-int ldif_write_file(FILE *f, const struct ldb_ldif *ldif)
+int ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
{
struct ldif_write_file_state state;
state.f = f;
- return ldif_write(fprintf_file, &state, ldif);
+ return ldif_write(ldb, fprintf_file, &state, ldif);
}
diff --git a/source4/lib/ldb/common/ldb_msg.c b/source4/lib/ldb/common/ldb_msg.c
index 8eb8a8c5ef..5976db81b6 100644
--- a/source4/lib/ldb/common/ldb_msg.c
+++ b/source4/lib/ldb/common/ldb_msg.c
@@ -87,11 +87,13 @@ struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
/*
add an empty element to a message
*/
-int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
+int ldb_msg_add_empty(struct ldb_context *ldb,
+ struct ldb_message *msg, const char *attr_name, int flags)
{
struct ldb_message_element *els;
- els = realloc_p(msg->elements, struct ldb_message_element, msg->num_elements+1);
+ els = ldb_realloc_p(ldb, msg->elements,
+ struct ldb_message_element, msg->num_elements+1);
if (!els) {
errno = ENOMEM;
return -1;
@@ -100,7 +102,7 @@ int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
els[msg->num_elements].values = NULL;
els[msg->num_elements].num_values = 0;
els[msg->num_elements].flags = flags;
- els[msg->num_elements].name = strdup(attr_name);
+ els[msg->num_elements].name = ldb_strdup(ldb, attr_name);
if (!els[msg->num_elements].name) {
return -1;
}
@@ -114,11 +116,12 @@ int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
/*
add an empty element to a message
*/
-int ldb_msg_add(struct ldb_message *msg,
+int ldb_msg_add(struct ldb_context *ldb,
+ struct ldb_message *msg,
const struct ldb_message_element *el,
int flags)
{
- if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
+ if (ldb_msg_add_empty(ldb, msg, el->name, flags) != 0) {
return -1;
}
diff --git a/source4/lib/ldb/common/ldb_parse.c b/source4/lib/ldb/common/ldb_parse.c
index 4f8d469e6c..75eb44fcc0 100644
--- a/source4/lib/ldb/common/ldb_parse.c
+++ b/source4/lib/ldb/common/ldb_parse.c
@@ -59,7 +59,7 @@ a filter is defined by:
/*
return next token element. Caller frees
*/
-static char *ldb_parse_lex(const char **s)
+static char *ldb_parse_lex(struct ldb_context *ldb, const char **s)
{
const char *p = *s;
char *ret;
@@ -75,7 +75,7 @@ static char *ldb_parse_lex(const char **s)
if (strchr("()&|=!", *p)) {
(*s) = p+1;
- ret = strndup(p, 1);
+ ret = ldb_strndup(ldb, p, 1);
if (!ret) {
errno = ENOMEM;
}
@@ -90,7 +90,7 @@ static char *ldb_parse_lex(const char **s)
return NULL;
}
- ret = strndup(*s, p - *s);
+ ret = ldb_strndup(ldb, *s, p - *s);
if (!ret) {
errno = ENOMEM;
}
@@ -122,46 +122,42 @@ static const char *match_brace(const char *s)
}
-static struct ldb_parse_tree *ldb_parse_filter(const char **s);
+static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s);
/*
<simple> ::= <attributetype> <filtertype> <attributevalue>
*/
-static struct ldb_parse_tree *ldb_parse_simple(const char *s)
+static struct ldb_parse_tree *ldb_parse_simple(struct ldb_context *ldb, const char *s)
{
char *eq, *val, *l;
struct ldb_parse_tree *ret;
- l = ldb_parse_lex(&s);
+ l = ldb_parse_lex(ldb, &s);
if (!l) {
- fprintf(stderr, "Unexpected end of expression\n");
return NULL;
}
if (strchr("()&|=", *l)) {
- fprintf(stderr, "Unexpected token '%s'\n", l);
- free(l);
+ ldb_free(ldb, l);
return NULL;
}
- eq = ldb_parse_lex(&s);
+ eq = ldb_parse_lex(ldb, &s);
if (!eq || strcmp(eq, "=") != 0) {
- fprintf(stderr, "Expected '='\n");
- free(l);
- if (eq) free(eq);
+ ldb_free(ldb, l);
+ if (eq) ldb_free(ldb, eq);
return NULL;
}
- free(eq);
+ ldb_free(ldb, eq);
- val = ldb_parse_lex(&s);
+ val = ldb_parse_lex(ldb, &s);
if (val && strchr("()&|=", *val)) {
- fprintf(stderr, "Unexpected token '%s'\n", val);
- free(l);
- if (val) free(val);
+ ldb_free(ldb, l);
+ if (val) ldb_free(ldb, val);
return NULL;
}
- ret = malloc_p(struct ldb_parse_tree);
+ ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
@@ -182,11 +178,12 @@ static struct ldb_parse_tree *ldb_parse_simple(const char *s)
<or> ::= '|' <filterlist>
<filterlist> ::= <filter> | <filter> <filterlist>
*/
-static struct ldb_parse_tree *ldb_parse_filterlist(enum ldb_parse_op op, const char *s)
+static struct ldb_parse_tree *ldb_parse_filterlist(struct ldb_context *ldb,
+ enum ldb_parse_op op, const char *s)
{
struct ldb_parse_tree *ret, *next;
- ret = malloc_p(struct ldb_parse_tree);
+ ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
@@ -194,31 +191,31 @@ static struct ldb_parse_tree *ldb_parse_filterlist(enum ldb_parse_op op, const c
ret->operation = op;
ret->u.list.num_elements = 1;
- ret->u.list.elements = malloc_p(struct ldb_parse_tree *);
+ ret->u.list.elements = ldb_malloc_p(ldb, struct ldb_parse_tree *);
if (!ret->u.list.elements) {
errno = ENOMEM;
- free(ret);
+ ldb_free(ldb, ret);
return NULL;
}
- ret->u.list.elements[0] = ldb_parse_filter(&s);
+ ret->u.list.elements[0] = ldb_parse_filter(ldb, &s);
if (!ret->u.list.elements[0]) {
- free(ret->u.list.elements);
- free(ret);
+ ldb_free(ldb, ret->u.list.elements);
+ ldb_free(ldb, ret);
return NULL;
}
while (isspace(*s)) s++;
- while (*s && (next = ldb_parse_filter(&s))) {
+ while (*s && (next = ldb_parse_filter(ldb, &s))) {
struct ldb_parse_tree **e;
- e = realloc_p(ret->u.list.elements,
- struct ldb_parse_tree *,
- ret->u.list.num_elements+1);
+ e = ldb_realloc_p(ldb, ret->u.list.elements,
+ struct ldb_parse_tree *,
+ ret->u.list.num_elements+1);
if (!e) {
errno = ENOMEM;
- ldb_parse_tree_free(next);
- ldb_parse_tree_free(ret);
+ ldb_parse_tree_free(ldb, next);
+ ldb_parse_tree_free(ldb, ret);
return NULL;
}
ret->u.list.elements = e;
@@ -234,20 +231,20 @@ static struct ldb_parse_tree *ldb_parse_filterlist(enum ldb_parse_op op, const c
/*
<not> ::= '!' <filter>
*/
-static struct ldb_parse_tree *ldb_parse_not(const char *s)
+static struct ldb_parse_tree *ldb_parse_not(struct ldb_context *ldb, const char *s)
{
struct ldb_parse_tree *ret;
- ret = malloc_p(struct ldb_parse_tree);
+ ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
}
ret->operation = LDB_OP_NOT;
- ret->u.not.child = ldb_parse_filter(&s);
+ ret->u.not.child = ldb_parse_filter(ldb, &s);
if (!ret->u.not.child) {
- free(ret);
+ ldb_free(ldb, ret);
return NULL;
}
@@ -258,67 +255,64 @@ static struct ldb_parse_tree *ldb_parse_not(const char *s)
parse a filtercomp
<filtercomp> ::= <and> | <or> | <not> | <simple>
*/
-static struct ldb_parse_tree *ldb_parse_filtercomp(const char *s)
+static struct ldb_parse_tree *ldb_parse_filtercomp(struct ldb_context *ldb,
+ const char *s)
{
while (isspace(*s)) s++;
switch (*s) {
case '&':
- return ldb_parse_filterlist(LDB_OP_AND, s+1);
+ return ldb_parse_filterlist(ldb, LDB_OP_AND, s+1);
case '|':
- return ldb_parse_filterlist(LDB_OP_OR, s+1);
+ return ldb_parse_filterlist(ldb, LDB_OP_OR, s+1);
case '!':
- return ldb_parse_not(s+1);
+ return ldb_parse_not(ldb, s+1);
case '(':
case ')':
- fprintf(stderr, "Unexpected token '%c'\n", *s);
return NULL;
}
- return ldb_parse_simple(s);
+ return ldb_parse_simple(ldb, s);
}
/*
<filter> ::= '(' <filtercomp> ')'
*/
-static struct ldb_parse_tree *ldb_parse_filter(const char **s)
+static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s)
{
char *l, *s2;
const char *p, *p2;
struct ldb_parse_tree *ret;
- l = ldb_parse_lex(s);
+ l = ldb_parse_lex(ldb, s);
if (!l) {
- fprintf(stderr, "Unexpected end of expression\n");
return NULL;
}
if (strcmp(l, "(") != 0) {
- free(l);
- fprintf(stderr, "Expected '('\n");
+ ldb_free(ldb, l);
return NULL;
}
- free(l);
+ ldb_free(ldb, l);
p = match_brace(*s);
if (!p) {
- fprintf(stderr, "Parse error - mismatched braces\n");
return NULL;
}
p2 = p + 1;
- s2 = strndup(*s, p - *s);
+ s2 = ldb_strndup(ldb, *s, p - *s);
if (!s2) {
errno = ENOMEM;
return NULL;
}
- ret = ldb_parse_filtercomp(s2);
- free(s2);
+ ret = ldb_parse_filtercomp(ldb, s2);
+ ldb_free(ldb, s2);
*s = p2;
@@ -331,130 +325,43 @@ static struct ldb_parse_tree *ldb_parse_filter(const char **s)
expression ::= <simple> | <filter>
*/
-struct ldb_parse_tree *ldb_parse_tree(const char *s)
+struct ldb_parse_tree *ldb_parse_tree(struct ldb_context *ldb, const char *s)
{
while (isspace(*s)) s++;
if (*s == '(') {
- return ldb_parse_filter(&s);
+ return ldb_parse_filter(ldb, &s);
}
- return ldb_parse_simple(s);
+ return ldb_parse_simple(ldb, s);
}
/*
free a parse tree returned from ldb_parse_tree()
*/
-void ldb_parse_tree_free(struct ldb_parse_tree *tree)
+void ldb_parse_tree_free(struct ldb_context *ldb, struct ldb_parse_tree *tree)
{
int i;
switch (tree->operation) {
case LDB_OP_SIMPLE:
- free(tree->u.simple.attr);
- if (tree->u.simple.value.data) free(tree->u.simple.value.data);
+ ldb_free(ldb, tree->u.simple.attr);
+ if (tree->u.simple.value.data) ldb_free(ldb, tree->u.simple.value.data);
break;
case LDB_OP_AND:
case LDB_OP_OR:
for (i=0;i<tree->u.list.num_elements;i++) {
- ldb_parse_tree_free(tree->u.list.elements[i]);
+ ldb_parse_tree_free(ldb, tree->u.list.elements[i]);
}
- if (tree->u.list.elements) free(tree->u.list.elements);
+ if (tree->u.list.elements) ldb_free(ldb, tree->u.list.elements);
break;
case LDB_OP_NOT:
- ldb_parse_tree_free(tree->u.not.child);
+ ldb_parse_tree_free(ldb, tree->u.not.child);
break;
}
- free(tree);
+ ldb_free(ldb, tree);
}
-#if TEST_PROGRAM
-/*
- return a string representation of a parse tree
- used for debugging
-*/
-static char *tree_string(struct ldb_parse_tree *tree)
-{
- char *s = NULL;
- char *s1, *s2;
- int i;
-
- switch (tree->operation) {
- case LDB_OP_SIMPLE:
- asprintf(&s, "( %s = \"%s\" )", tree->u.simple.attr,
- (char *)tree->u.simple.value.data);
- break;
-
- case LDB_OP_AND:
- case LDB_OP_OR:
- asprintf(&s, "( %c", tree->operation==LDB_OP_AND?'&':'|');
- if (!s) return NULL;
-
- for (i=0;i<tree->u.list.num_elements;i++) {
- s1 = tree_string(tree->u.list.elements[i]);
- if (!s1) {
- free(s);
- return NULL;
- }
- asprintf(&s2, "%s %s", s, s1);
- free(s);
- free(s1);
- s = s2;
- }
- if (!s) {
- return NULL;
- }
- asprintf(&s2, "%s )", s);
- free(s);
- s = s2;
- break;
-
- case LDB_OP_NOT:
- s1 = tree_string(tree->u.not.child);
- asprintf(&s, "( ! %s )", s1);
- free(s1);
- break;
- }
- return s;
-}
-
-
-/*
- print a tree
- */
-static void print_tree(struct ldb_parse_tree *tree)
-{
- char *s = tree_string(tree);
- printf("%s\n", s);
- free(s);
-}
-
-
- int main(void)
-{
- char line[1000];
- int ret = 0;
-
- while (fgets(line, sizeof(line)-1, stdin)) {
- struct ldb_parse_tree *tree;
-
- if (line[strlen(line)-1] == '\n') {
- line[strlen(line)-1] = 0;
- }
- tree = ldb_parse_tree(line);
- if (!tree) {
- fprintf(stderr, "Failed to parse\n");
- ret = 1;
- continue;
- }
- print_tree(tree);
- ldb_parse_tree_free(tree);
- }
-
- return ret;
-}
-#endif /* TEST_PROGRAM */
-
diff --git a/source4/lib/ldb/common/ldb_utf8.c b/source4/lib/ldb/common/ldb_utf8.c
index 7767b0955e..1e467d23af 100644
--- a/source4/lib/ldb/common/ldb_utf8.c
+++ b/source4/lib/ldb/common/ldb_utf8.c
@@ -38,10 +38,10 @@
TODO:
a simple case folding function - will be replaced by a UTF8 aware function later
*/
-char *ldb_casefold(const char *s)
+char *ldb_casefold(struct ldb_context *ldb, const char *s)
{
int i;
- char *ret = strdup(s);
+ char *ret = ldb_strdup(ldb, s);
if (!s) {
errno = ENOMEM;
return NULL;
diff --git a/source4/lib/ldb/common/util.c b/source4/lib/ldb/common/util.c
index 22bbc8334e..68537a7864 100644
--- a/source4/lib/ldb/common/util.c
+++ b/source4/lib/ldb/common/util.c
@@ -35,24 +35,6 @@
#include "includes.h"
-#define MAX_MALLOC_SIZE 0x7fffffff
-
-/*
- realloc an array, checking for integer overflow in the array size
-*/
-void *realloc_array(void *ptr, size_t el_size, unsigned count)
-{
- if (count == 0 ||
- count >= MAX_MALLOC_SIZE/el_size) {
- return NULL;
- }
- if (!ptr) {
- return malloc(el_size * count);
- }
- return realloc(ptr, el_size * count);
-}
-
-
/*
find an element in a list, using the given comparison function and
assuming that the list is already sorted using comp_fn