From: Volker Lendecke Date: Wed, 28 Aug 2024 10:32:45 +0000 (+0200) Subject: libndr: Streamline ndr_token_retrieve_cmp_fn X-Git-Tag: tdb-1.4.13~1259 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=df103890f9febd5551d5bbff5498179866ab890e;p=thirdparty%2Fsamba.git libndr: Streamline ndr_token_retrieve_cmp_fn Rename the public function to ndr_token_peek_cmp_fn, the only user does not remove the token. Factor out ndr_token_find to move the token-removing logic to ndr_token_retrieve, the only caller that does remove the token. Keep libndr at 6.0.0, this has not been released yet. Signed-off-by: Volker Lendecke Reviewed-by: Jennifer Sutton Autobuild-User(master): Volker Lendecke Autobuild-Date(master): Thu Aug 29 08:40:52 UTC 2024 on atb-devel-224 --- diff --git a/librpc/ABI/ndr-6.0.0.sigs b/librpc/ABI/ndr-6.0.0.sigs index cfc27b32a9a..3244948abcc 100644 --- a/librpc/ABI/ndr-6.0.0.sigs +++ b/librpc/ABI/ndr-6.0.0.sigs @@ -266,8 +266,8 @@ ndr_syntax_id_null: uuid = {time_low = 0, time_mid = 0, time_hi_and_version = 0, ndr_syntax_id_to_string: char *(TALLOC_CTX *, const struct ndr_syntax_id *) ndr_token_max_list_size: size_t (void) ndr_token_peek: enum ndr_err_code (struct ndr_token_list *, const void *, uint32_t *) +ndr_token_peek_cmp_fn: enum ndr_err_code (struct ndr_token_list *, const void *, uint32_t *, comparison_fn_t) ndr_token_retrieve: enum ndr_err_code (struct ndr_token_list *, const void *, uint32_t *) -ndr_token_retrieve_cmp_fn: enum ndr_err_code (struct ndr_token_list *, const void *, uint32_t *, comparison_fn_t, bool) ndr_token_store: enum ndr_err_code (TALLOC_CTX *, struct ndr_token_list *, const void *, uint32_t) ndr_transfer_syntax_ndr: uuid = {time_low = 2324192516, time_mid = 7403, time_hi_and_version = 4553, clock_seq = "\237\350", node = "\b\000+\020H`"}, if_version = 2 ndr_transfer_syntax_ndr64: uuid = {time_low = 1903232307, time_mid = 48826, time_hi_and_version = 18743, clock_seq = "\203\031", node = "\265\333\357\234\3146"}, if_version = 1 diff --git a/librpc/ndr/libndr.h b/librpc/ndr/libndr.h index aafdc1536eb..223501ab781 100644 --- a/librpc/ndr/libndr.h +++ b/librpc/ndr/libndr.h @@ -721,8 +721,11 @@ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx, struct ndr_token_list *list, const void *key, uint32_t value); -enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list *list, const void *key, uint32_t *v, - int(*_cmp_fn)(const void*,const void*), bool erase); +enum ndr_err_code ndr_token_peek_cmp_fn(struct ndr_token_list *list, + const void *key, + uint32_t *v, + int (*_cmp_fn)(const void *, + const void *)); enum ndr_err_code ndr_token_retrieve(struct ndr_token_list *list, const void *key, uint32_t *v); enum ndr_err_code ndr_token_peek(struct ndr_token_list *list, const void *key, uint32_t *v); enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p); diff --git a/librpc/ndr/ndr.c b/librpc/ndr/ndr.c index 75b6f67f3f3..16dc54463b8 100644 --- a/librpc/ndr/ndr.c +++ b/librpc/ndr/ndr.c @@ -1044,28 +1044,31 @@ _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx, /* retrieve a token from a ndr context, using cmp_fn to match the tokens */ -_PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list *list, - const void *key, uint32_t *v, - comparison_fn_t _cmp_fn, - bool erase) +static enum ndr_err_code ndr_token_find(struct ndr_token_list *list, + const void *key, + uint32_t *v, + comparison_fn_t _cmp_fn, + unsigned *_i) { struct ndr_token *tokens = list->tokens; unsigned i; for (i = list->count - 1; i < list->count; i--) { if (_cmp_fn(tokens[i].key, key) == 0) { - goto found; + *_i = i; + *v = tokens[i].value; + return NDR_ERR_SUCCESS; } } return NDR_ERR_TOKEN; -found: - *v = tokens[i].value; - if (erase) { - if (i != list->count - 1) { - tokens[i] = tokens[list->count - 1]; - } - list->count--; - } - return NDR_ERR_SUCCESS; +} + +_PUBLIC_ enum ndr_err_code ndr_token_peek_cmp_fn(struct ndr_token_list *list, + const void *key, + uint32_t *v, + comparison_fn_t _cmp_fn) +{ + unsigned i; + return ndr_token_find(list, key, v, _cmp_fn, &i); } static int token_cmp_ptr(const void *a, const void *b) @@ -1079,7 +1082,22 @@ static int token_cmp_ptr(const void *a, const void *b) _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list *list, const void *key, uint32_t *v) { - return ndr_token_retrieve_cmp_fn(list, key, v, token_cmp_ptr, true); + enum ndr_err_code err; + uint32_t last; + unsigned i; + + err = ndr_token_find(list, key, v, token_cmp_ptr, &i); + if (!NDR_ERR_CODE_IS_SUCCESS(err)) { + return err; + } + + last = list->count - 1; + if (i != last) { + list->tokens[i] = list->tokens[last]; + } + list->count--; + + return NDR_ERR_SUCCESS; } /* @@ -1088,7 +1106,8 @@ _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list *list, _PUBLIC_ enum ndr_err_code ndr_token_peek(struct ndr_token_list *list, const void *key, uint32_t *v) { - return ndr_token_retrieve_cmp_fn(list, key, v, token_cmp_ptr, false); + unsigned i; + return ndr_token_find(list, key, v, token_cmp_ptr, &i); } /* diff --git a/librpc/ndr/ndr_dns_utils.c b/librpc/ndr/ndr_dns_utils.c index a2e04640d52..fe9e060306e 100644 --- a/librpc/ndr/ndr_dns_utils.c +++ b/librpc/ndr/ndr_dns_utils.c @@ -50,10 +50,11 @@ enum ndr_err_code ndr_push_dns_string_list(struct ndr_push *ndr, /* see if we have pushed the remaining string already, * if so we use a label pointer to this string */ - ndr_err = ndr_token_retrieve_cmp_fn(string_list, s, - &offset, - (comparison_fn_t)strcmp, - false); + ndr_err = ndr_token_peek_cmp_fn(string_list, + s, + &offset, + (comparison_fn_t) + strcmp); if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { uint8_t b[2];