net: buf: add function to match buffer's content with a given data
This commit adds a new function the net_buf's API that allow an user to match the net_buf's content with a data without copying it to a temporary buffer. Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
This commit is contained in:
parent
f4cbf403e8
commit
a5b868d94a
|
@ -2414,6 +2414,22 @@ size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
|
|||
const void *value, k_timeout_t timeout,
|
||||
net_buf_allocator_cb allocate_cb, void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Match data with a net_buf's content
|
||||
*
|
||||
* @details Compare data with a content of a net_buf. Provide information about
|
||||
* the number of bytes matching between both. If needed, traverse
|
||||
* through multiple buffer fragments.
|
||||
*
|
||||
* @param buf Network buffer
|
||||
* @param offset Starting offset to compare from
|
||||
* @param data Data buffer for comparison
|
||||
* @param len Number of bytes to compare
|
||||
*
|
||||
* @return The number of bytes compared before the first difference.
|
||||
*/
|
||||
size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Skip N number of bytes in a net_buf
|
||||
*
|
||||
|
|
|
@ -694,3 +694,39 @@ size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
|
|||
/* Unreachable */
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len)
|
||||
{
|
||||
const uint8_t *dptr = data;
|
||||
const uint8_t *bptr;
|
||||
size_t compared = 0;
|
||||
size_t to_compare;
|
||||
|
||||
if (!buf || !data) {
|
||||
return compared;
|
||||
}
|
||||
|
||||
/* find the right fragment to start comparison */
|
||||
while (buf && offset >= buf->len) {
|
||||
offset -= buf->len;
|
||||
buf = buf->frags;
|
||||
}
|
||||
|
||||
while (buf && len > 0) {
|
||||
bptr = buf->data + offset;
|
||||
to_compare = MIN(len, buf->len - offset);
|
||||
|
||||
for (size_t i = 0; i < to_compare; ++i) {
|
||||
if (dptr[compared] != bptr[i]) {
|
||||
return compared;
|
||||
}
|
||||
compared++;
|
||||
}
|
||||
|
||||
len -= to_compare;
|
||||
buf = buf->frags;
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
return compared;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue