]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ldb: Add ldb_val -> bool,uint64,int64 parsing functions
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Wed, 15 Mar 2023 22:42:04 +0000 (11:42 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 31 Mar 2023 08:29:32 +0000 (08:29 +0000)
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>
lib/ldb/ABI/ldb-2.8.0.sigs
lib/ldb/common/ldb_msg.c
lib/ldb/include/ldb_module.h

index 35a07c3e2dc2f03bc7593aa2ab0cefdc26ff83cc..b53c9925cde21ea820e4da36d38203366cd11adf 100644 (file)
@@ -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 *)
index cd22e5a58d961a2570587e65c3369860be80e041..f584745968f473b9659155ac1cb9babb957a4a76 100644 (file)
@@ -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,
index a37f308b202f55fdaf3ef7ceacd1261ca1c918cd..323874b0bfc4a1c392243ccec4ca3b85e4ca1cbb 100644 (file)
@@ -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