From a6f3e61d7070a195a87128974ed916b3a4bc2602 Mon Sep 17 00:00:00 2001 From: John Thacker Date: Wed, 11 Oct 2023 09:07:41 -0400 Subject: [PATCH] wsutil: Add a reverse mempbrk function --- packaging/debian/libwsutil0.symbols | 1 + wsutil/ws_mempbrk.c | 15 +++++++++++++++ wsutil/ws_mempbrk.h | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/packaging/debian/libwsutil0.symbols b/packaging/debian/libwsutil0.symbols index 07aa526dd9..f3aab23b3e 100644 --- a/packaging/debian/libwsutil0.symbols +++ b/packaging/debian/libwsutil0.symbols @@ -491,6 +491,7 @@ libwsutil.so.0 libwsutil0 #MINVER# ws_mempbrk_compile@Base 1.99.4 ws_mempbrk_exec@Base 1.99.4 ws_memrchr@Base 4.3.0rc0 + ws_memrpbrk_exec@Base 4.3.0rc0 ws_optarg@Base 3.5.1 ws_opterr@Base 3.5.1 ws_optind@Base 3.5.1 diff --git a/wsutil/ws_mempbrk.c b/wsutil/ws_mempbrk.c index f3f99b7167..55334242f8 100644 --- a/wsutil/ws_mempbrk.c +++ b/wsutil/ws_mempbrk.c @@ -72,6 +72,21 @@ ws_mempbrk_exec(const uint8_t* haystack, size_t haystacklen, const ws_mempbrk_pa return ws_mempbrk_portable_exec(haystack, haystacklen, pattern, found_needle); } +WS_DLL_PUBLIC const uint8_t * +ws_memrpbrk_exec(const uint8_t* haystack, size_t haystacklen, const ws_mempbrk_pattern* pattern, unsigned char *found_needle) +{ + const uint8_t *haystack_end = haystack + haystacklen; + + while (haystack_end > haystack) { + if (pattern->patt[*(--haystack_end)]) { + if (found_needle) + *found_needle = *haystack_end; + return haystack_end; + } + } + + return NULL; +} /* * Editor modelines - https://www.wireshark.org/tools/modelines.html diff --git a/wsutil/ws_mempbrk.h b/wsutil/ws_mempbrk.h index 7aff505f81..210fdfcedf 100644 --- a/wsutil/ws_mempbrk.h +++ b/wsutil/ws_mempbrk.h @@ -34,4 +34,9 @@ WS_DLL_PUBLIC void ws_mempbrk_compile(ws_mempbrk_pattern* pattern, const char *n */ WS_DLL_PUBLIC const uint8_t *ws_mempbrk_exec(const uint8_t* haystack, size_t haystacklen, const ws_mempbrk_pattern* pattern, unsigned char *found_needle); +/** Scan for the needles specified by the compiled pattern, starting at the + * end of the haystack and working backwards. + */ +WS_DLL_PUBLIC const uint8_t *ws_memrpbrk_exec(const uint8_t* haystack, size_t haystacklen, const ws_mempbrk_pattern* pattern, unsigned char *found_needle); + #endif /* __WS_MEMPBRK_H__ */