summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoseph Sutton <josephsutton@catalyst.net.nz>2023-03-16 11:42:04 +1300
committerAndrew Bartlett <abartlet@samba.org>2023-03-31 08:29:32 +0000
commitf41f988038920bc19e8d9f2502ff0d3f2aaa2196 (patch)
tree0d8382f50b5db0064b0705fdb284c419e2bc6dbb /lib
parent570a3ac866dfcd3584742c3af553af76aac91c6c (diff)
downloadsamba-f41f988038920bc19e8d9f2502ff0d3f2aaa2196.tar.gz
ldb: Add ldb_val -> bool,uint64,int64 parsing functions
These functions allow us to parse any value of a message element, not only the first. They also unambiguously indicate whether an error has occurred. Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/ldb/ABI/ldb-2.8.0.sigs3
-rw-r--r--lib/ldb/common/ldb_msg.c65
-rw-r--r--lib/ldb/include/ldb_module.h3
3 files changed, 52 insertions, 19 deletions
diff --git a/lib/ldb/ABI/ldb-2.8.0.sigs b/lib/ldb/ABI/ldb-2.8.0.sigs
index 35a07c3e2dc..b53c9925cde 100644
--- a/lib/ldb/ABI/ldb-2.8.0.sigs
+++ b/lib/ldb/ABI/ldb-2.8.0.sigs
@@ -280,7 +280,10 @@ ldb_transaction_start: int (struct ldb_context *)
ldb_unpack_data: int (struct ldb_context *, const struct ldb_val *, struct ldb_message *)
ldb_unpack_data_flags: int (struct ldb_context *, const struct ldb_val *, struct ldb_message *, unsigned int)
ldb_unpack_get_format: int (const struct ldb_val *, uint32_t *)
+ldb_val_as_bool: int (const struct ldb_val *, bool *)
ldb_val_as_dn: struct ldb_dn *(struct ldb_context *, TALLOC_CTX *, const struct ldb_val *)
+ldb_val_as_int64: int (const struct ldb_val *, int64_t *)
+ldb_val_as_uint64: int (const struct ldb_val *, uint64_t *)
ldb_val_dup: struct ldb_val (TALLOC_CTX *, const struct ldb_val *)
ldb_val_equal_exact: int (const struct ldb_val *, const struct ldb_val *)
ldb_val_map_local: struct ldb_val (struct ldb_module *, void *, const struct ldb_map_attribute *, const struct ldb_val *)
diff --git a/lib/ldb/common/ldb_msg.c b/lib/ldb/common/ldb_msg.c
index cd22e5a58d9..f584745968f 100644
--- a/lib/ldb/common/ldb_msg.c
+++ b/lib/ldb/common/ldb_msg.c
@@ -877,64 +877,82 @@ int64_t ldb_msg_find_attr_as_int64(const struct ldb_message *msg,
const char *attr_name,
int64_t default_value)
{
+ int64_t val = 0;
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
+ int ret = ldb_val_as_int64(v, &val);
+ return ret ? default_value : val;
+}
+
+int ldb_val_as_int64(const struct ldb_val *v, int64_t *val)
+{
char buf[sizeof("-9223372036854775808")];
char *end = NULL;
- int64_t ret;
+ int64_t result;
if (!v || !v->data) {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
ZERO_STRUCT(buf);
if (v->length >= sizeof(buf)) {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
memcpy(buf, v->data, v->length);
errno = 0;
- ret = (int64_t) strtoll(buf, &end, 10);
+ result = (int64_t) strtoll(buf, &end, 10);
if (errno != 0) {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
if (end && end[0] != '\0') {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
- return ret;
+
+ *val = result;
+ return LDB_SUCCESS;
}
uint64_t ldb_msg_find_attr_as_uint64(const struct ldb_message *msg,
const char *attr_name,
uint64_t default_value)
{
+ uint64_t val = 0;
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
+ int ret = ldb_val_as_uint64(v, &val);
+ return ret ? default_value : val;
+}
+
+int ldb_val_as_uint64(const struct ldb_val *v, uint64_t *val)
+{
char buf[sizeof("-9223372036854775808")];
char *end = NULL;
- uint64_t ret;
+ uint64_t result;
if (!v || !v->data) {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
ZERO_STRUCT(buf);
if (v->length >= sizeof(buf)) {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
memcpy(buf, v->data, v->length);
errno = 0;
- ret = (uint64_t) strtoll(buf, &end, 10);
+ result = (uint64_t) strtoll(buf, &end, 10);
if (errno != 0) {
errno = 0;
- ret = (uint64_t) strtoull(buf, &end, 10);
+ result = (uint64_t) strtoull(buf, &end, 10);
if (errno != 0) {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
}
if (end && end[0] != '\0') {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
- return ret;
+
+ *val = result;
+ return LDB_SUCCESS;
}
double ldb_msg_find_attr_as_double(const struct ldb_message *msg,
@@ -970,17 +988,26 @@ int ldb_msg_find_attr_as_bool(const struct ldb_message *msg,
const char *attr_name,
int default_value)
{
+ bool val = false;
const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
+ int ret = ldb_val_as_bool(v, &val);
+ return ret ? default_value : val;
+}
+
+int ldb_val_as_bool(const struct ldb_val *v, bool *val)
+{
if (!v || !v->data) {
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
if (v->length == 5 && strncasecmp((const char *)v->data, "FALSE", 5) == 0) {
- return 0;
+ *val = false;
+ return LDB_SUCCESS;
}
if (v->length == 4 && strncasecmp((const char *)v->data, "TRUE", 4) == 0) {
- return 1;
+ *val = true;
+ return LDB_SUCCESS;
}
- return default_value;
+ return LDB_ERR_OPERATIONS_ERROR;
}
const char *ldb_msg_find_attr_as_string(const struct ldb_message *msg,
diff --git a/lib/ldb/include/ldb_module.h b/lib/ldb/include/ldb_module.h
index a37f308b202..323874b0bfc 100644
--- a/lib/ldb/include/ldb_module.h
+++ b/lib/ldb/include/ldb_module.h
@@ -602,5 +602,8 @@ const char **ldb_options_get(struct ldb_context *ldb);
struct ldb_dn *ldb_val_as_dn(struct ldb_context *ldb,
TALLOC_CTX *mem_ctx,
const struct ldb_val *v);
+int ldb_val_as_int64(const struct ldb_val *v, int64_t *val);
+int ldb_val_as_uint64(const struct ldb_val *v, uint64_t *val);
+int ldb_val_as_bool(const struct ldb_val *v, bool *val);
#endif