📄 ldb_msg.c
字号:
/* 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 3 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, see <http://www.gnu.org/licenses/>.*//* * Name: ldb * * Component: ldb message component utility functions * * Description: functions for manipulating ldb_message structures * * Author: Andrew Tridgell */#include "ldb_includes.h"/* create a new ldb_message in a given memory context (NULL for top level)*/struct ldb_message *ldb_msg_new(void *mem_ctx){ return talloc_zero(mem_ctx, struct ldb_message);}/* find an element in a message by attribute name*/struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, const char *attr_name){ unsigned int i; for (i=0;i<msg->num_elements;i++) { if (ldb_attr_cmp(msg->elements[i].name, attr_name) == 0) { return &msg->elements[i]; } } return NULL;}/* see if two ldb_val structures contain exactly the same data return 1 for a match, 0 for a mis-match*/int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2){ if (v1->length != v2->length) return 0; if (v1->length == 0) return 1; if (memcmp(v1->data, v2->data, v1->length) == 0) { return 1; } return 0;}/* find a value in an element assumes case sensitive comparison*/struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, struct ldb_val *val){ unsigned int i; for (i=0;i<el->num_values;i++) { if (ldb_val_equal_exact(val, &el->values[i])) { return &el->values[i]; } } return NULL;}/* duplicate a ldb_val structure*/struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v){ struct ldb_val v2; v2.length = v->length; if (v->data == NULL) { v2.data = NULL; return v2; } /* the +1 is to cope with buggy C library routines like strndup that look one byte beyond */ v2.data = talloc_array(mem_ctx, uint8_t, v->length+1); if (!v2.data) { v2.length = 0; return v2; } memcpy(v2.data, v->data, v->length); ((char *)v2.data)[v->length] = 0; return v2;}/* add an empty element to a message*/int ldb_msg_add_empty( struct ldb_message *msg, const char *attr_name, int flags, struct ldb_message_element **return_el){ struct ldb_message_element *els; els = talloc_realloc(msg, msg->elements, struct ldb_message_element, msg->num_elements+1); if (!els) { errno = ENOMEM; return LDB_ERR_OPERATIONS_ERROR; } els[msg->num_elements].values = NULL; els[msg->num_elements].num_values = 0; els[msg->num_elements].flags = flags; els[msg->num_elements].name = talloc_strdup(els, attr_name); if (!els[msg->num_elements].name) { errno = ENOMEM; return LDB_ERR_OPERATIONS_ERROR; } msg->elements = els; msg->num_elements++; if (return_el) { *return_el = &els[msg->num_elements-1]; } return LDB_SUCCESS;}/* add an empty element to a message*/int ldb_msg_add(struct ldb_message *msg, const struct ldb_message_element *el, int flags){ /* We have to copy this, just in case *el is a pointer into * what ldb_msg_add_empty() is about to realloc() */ struct ldb_message_element el_copy = *el; if (ldb_msg_add_empty(msg, el->name, flags, NULL) != 0) { return LDB_ERR_OPERATIONS_ERROR; } msg->elements[msg->num_elements-1] = el_copy; msg->elements[msg->num_elements-1].flags = flags; return LDB_SUCCESS;}/* add a value to a message*/int ldb_msg_add_value(struct ldb_message *msg, const char *attr_name, const struct ldb_val *val, struct ldb_message_element **return_el){ struct ldb_message_element *el; struct ldb_val *vals; int ret; el = ldb_msg_find_element(msg, attr_name); if (!el) { ret = ldb_msg_add_empty(msg, attr_name, 0, &el); if (ret != LDB_SUCCESS) { return ret; } } vals = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1); if (!vals) { errno = ENOMEM; return LDB_ERR_OPERATIONS_ERROR; } el->values = vals; el->values[el->num_values] = *val; el->num_values++; if (return_el) { *return_el = el; } return LDB_SUCCESS;}/* add a value to a message, stealing it into the 'right' place*/int ldb_msg_add_steal_value(struct ldb_message *msg, const char *attr_name, struct ldb_val *val){ int ret; struct ldb_message_element *el; ret = ldb_msg_add_value(msg, attr_name, val, &el); if (ret == LDB_SUCCESS) { talloc_steal(el->values, val->data); } return ret;}/* add a string element to a message*/int ldb_msg_add_string(struct ldb_message *msg, const char *attr_name, const char *str){ struct ldb_val val; val.data = discard_const_p(uint8_t, str); val.length = strlen(str); if (val.length == 0) { /* allow empty strings as non-existant attributes */ return LDB_SUCCESS; } return ldb_msg_add_value(msg, attr_name, &val, NULL);}/* add a string element to a message, stealing it into the 'right' place*/int ldb_msg_add_steal_string(struct ldb_message *msg, const char *attr_name, char *str){ struct ldb_val val; val.data = (uint8_t *)str; val.length = strlen(str); return ldb_msg_add_steal_value(msg, attr_name, &val);}/* add a printf formatted element to a message*/int ldb_msg_add_fmt(struct ldb_message *msg, const char *attr_name, const char *fmt, ...){ struct ldb_val val; va_list ap; char *str; va_start(ap, fmt); str = talloc_vasprintf(msg, fmt, ap); va_end(ap); if (str == NULL) return LDB_ERR_OPERATIONS_ERROR; val.data = (uint8_t *)str; val.length = strlen(str); return ldb_msg_add_steal_value(msg, attr_name, &val);}/* compare two ldb_message_element structures assumes case senistive comparison*/int ldb_msg_element_compare(struct ldb_message_element *el1, struct ldb_message_element *el2){ unsigned int i; if (el1->num_values != el2->num_values) { return el1->num_values - el2->num_values; } for (i=0;i<el1->num_values;i++) { if (!ldb_msg_find_val(el2, &el1->values[i])) { return -1; } } return 0;}/* compare two ldb_message_element structures comparing by element name*/int ldb_msg_element_compare_name(struct ldb_message_element *el1, struct ldb_message_element *el2){ return ldb_attr_cmp(el1->name, el2->name);}/* convenience functions to return common types from a message these return the first value if the attribute is multi-valued*/const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name){ struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name); if (!el || el->num_values == 0) { return NULL; } return &el->values[0];}int ldb_msg_find_attr_as_int(const struct ldb_message *msg, const char *attr_name, int default_value){ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return default_value; } return strtol((const char *)v->data, NULL, 0);}unsigned int ldb_msg_find_attr_as_uint(const struct ldb_message *msg, const char *attr_name, unsigned int default_value){ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return default_value; } return strtoul((const char *)v->data, NULL, 0);}int64_t ldb_msg_find_attr_as_int64(const struct ldb_message *msg, const char *attr_name, int64_t default_value){ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return default_value; } return strtoll((const char *)v->data, NULL, 0);}uint64_t ldb_msg_find_attr_as_uint64(const struct ldb_message *msg, const char *attr_name, uint64_t default_value){ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return default_value; } return strtoull((const char *)v->data, NULL, 0);}double ldb_msg_find_attr_as_double(const struct ldb_message *msg, const char *attr_name, double default_value){ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return default_value; } return strtod((const char *)v->data, NULL);}int ldb_msg_find_attr_as_bool(const struct ldb_message *msg, const char *attr_name, int default_value){ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return default_value; } if (strcasecmp((const char *)v->data, "FALSE") == 0) { return 0; } if (strcasecmp((const char *)v->data, "TRUE") == 0) { return 1; } return default_value;}const char *ldb_msg_find_attr_as_string(const struct ldb_message *msg, const char *attr_name, const char *default_value){ const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return default_value; } return (const char *)v->data;}struct ldb_dn *ldb_msg_find_attr_as_dn(struct ldb_context *ldb, void *mem_ctx, const struct ldb_message *msg, const char *attr_name){ struct ldb_dn *res_dn; const struct ldb_val *v; v = ldb_msg_find_ldb_val(msg, attr_name); if (!v || !v->data) { return NULL; } res_dn = ldb_dn_new(mem_ctx, ldb, (const char *)v->data); if ( ! ldb_dn_validate(res_dn)) { talloc_free(res_dn); return NULL; } return res_dn;}/* sort the elements of a message by name*/void ldb_msg_sort_elements(struct ldb_message *msg){ qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element), (comparison_fn_t)ldb_msg_element_compare_name);}/* shallow copy a message - copying only the elements array so that the caller can safely add new elements without changing the message*/struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx, const struct ldb_message *msg){ struct ldb_message *msg2; int i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -