From 07e622f224e0f0811e916645c21f4a7a68f442e9 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Tue, 3 Jan 2023 06:58:58 +0300 Subject: [PATCH] public: add generic implementation for Q_memmem --- public/crtlib.c | 17 +++++++++++++++++ public/crtlib.h | 1 + 2 files changed, 18 insertions(+) diff --git a/public/crtlib.c b/public/crtlib.c index 5b830cd4..b1d7ee5c 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -372,6 +372,23 @@ qboolean Q_stricmpext( const char *pattern, const char *text ) return Q_strnicmpext( pattern, text, ~((size_t)0) ); } +const byte *Q_memmem( const byte *haystack, size_t haystacklen, const byte *needle, size_t needlelen ) +{ + const byte *i; + + // quickly find first matching symbol + while( haystacklen && ( i = memchr( haystack, needle[0], haystacklen ))) + { + if( !memcmp( i, needle, needlelen )) + return i; + + haystacklen -= i - haystack; + haystack = i + 1; + } + + return NULL; +} + const char* Q_timestamp( int format ) { static string timestamp; diff --git a/public/crtlib.h b/public/crtlib.h index 6296930d..b3282d06 100644 --- a/public/crtlib.h +++ b/public/crtlib.h @@ -78,6 +78,7 @@ void Q_atov( float *vec, const char *str, size_t siz ); #define Q_strrchr strrchr qboolean Q_stricmpext( const char *pattern, const char *text ); qboolean Q_strnicmpext( const char *pattern, const char *text, size_t minimumlen ); +const byte *Q_memmem( const byte *haystack, size_t haystacklen, const byte *needle, size_t needlelen ); const char *Q_timestamp( int format ); #define Q_vsprintf( buffer, format, args ) Q_vsnprintf( buffer, 99999, format, args ) int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args );