From edad96ffe90db633c073f962c4e309f2894f9c92 Mon Sep 17 00:00:00 2001 From: Daniel Mack Date: Fri, 15 Aug 2014 19:20:51 +0200 Subject: [PATCH] metadata: add kdbus_meta_offset_of() kdbus_meta_offset_of() will iterate over the given metadata and return the offset of the item of type @type, if it exists. --- metadata.c | 24 ++++++++++++++++++++++++ metadata.h | 1 + 2 files changed, 25 insertions(+) diff --git a/metadata.c b/metadata.c index ff1d203..c8b86c3 100644 --- a/metadata.c +++ b/metadata.c @@ -57,6 +57,30 @@ int kdbus_meta_new(struct kdbus_meta **meta) return 0; } +/** + * kdbus_meta_offset_of() - return the offset of a meta data element + * @meta: Metadata object + * @type: The metadata type to look for (KDBUS_ITEM_*) + * @off: Return pointer for the offset, if any + * + * This function will iterate over the given metadata and return the + * offset of the item of type @type, if it exists. + * + * Return: 0 on success, -ENOENT if the item was not found. + */ +int kdbus_meta_offset_of(struct kdbus_meta *meta, u64 type, off_t *off) +{ + const struct kdbus_item *item; + + KDBUS_ITEMS_FOREACH(item, meta->data, meta->size) + if (item->type == type) { + *off = (u8 *) item - (u8 *) meta->data; + return 0; + } + + return -ENOENT; +} + /** * kdbus_meta_free() - release metadata * @meta: Metadata object diff --git a/metadata.h b/metadata.h index 77b7303..82344b3 100644 --- a/metadata.h +++ b/metadata.h @@ -41,5 +41,6 @@ int kdbus_meta_append(struct kdbus_meta *meta, struct kdbus_conn *conn, u64 seq, u64 which); +int kdbus_meta_offset_of(struct kdbus_meta *meta, u64 type, off_t *off); void kdbus_meta_free(struct kdbus_meta *meta); #endif -- 2.34.1