ongrep

A cleaned up fork of ngrep for OpenBSD
git clone git://git.sgregoratto.me/ongrep
Log | Files | Refs | README | LICENSE

commit 918cf7ce2514d73f545f84bbad5a6ca1fc7316cf
parent a98d946d5e7464b29c91c818ec5e50d7a8197c47
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Wed,  1 Jul 2020 10:36:32 +1000

bin_match_func: Replace rolling memcmp with memmem

Diffstat:
Mngrep.c | 19+++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/ngrep.c b/ngrep.c @@ -910,24 +910,23 @@ bin_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, uint16_t *msize) { int32_t stop = len - match_len; - int32_t i = 0; + unsigned char *p; if (stop < 0) return 0; - while (i <= stop) { - if (!memcmp(data + (i++), bin_data, match_len)) { - matches++; + if ((p = memmem(data, len, bin_data, match_len)) != NULL) { + matches++; - if (match_after && keep_matching != match_after) - keep_matching = match_after; + if (match_after && keep_matching != match_after) + keep_matching = match_after; - *mindex = i - 1; - *msize = match_len; + *mindex = (uint16_t)(p - data); + *msize = match_len; - return 1; - } + return 1; } + return 0; }