From c4dcb205225097dc7c672b4c8435506465ac8b44 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 18 Aug 2016 09:59:22 +0200 Subject: [PATCH] detect-parse: add new func to get last sigmatch Add SigMatchGetLastSM which simply returns the very last SM added to the signature. Minor cleanups. --- src/detect-parse.c | 35 +++++++++++++++++++++++++++++------ src/detect-parse.h | 3 ++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/detect-parse.c b/src/detect-parse.c index ca1a238afb..1215d56211 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -411,7 +411,7 @@ void SigMatchRemoveSMFromList(Signature *s, SigMatch *sm, int sm_list) * * \retval match Pointer to the last SigMatch instance of type 'type'. */ -static inline SigMatch *SigMatchGetLastSM(SigMatch *sm, uint8_t type) +static SigMatch *SigMatchGetLastSMByType(SigMatch *sm, uint8_t type) { while (sm != NULL) { if (sm->type == type) { @@ -424,11 +424,12 @@ static inline SigMatch *SigMatchGetLastSM(SigMatch *sm, uint8_t type) } /** - * \brief Returns the sm with the largest index (added latest) from all the lists. + * \brief Returns the sm with the largest index (added latest) from the lists + * passed to us. * * \retval Pointer to Last sm. */ -SigMatch *SigMatchGetLastSMFromLists(Signature *s, int args, ...) +SigMatch *SigMatchGetLastSMFromLists(const Signature *s, int args, ...) { if (args == 0 || args % 2 != 0) { SCLogError(SC_ERR_INVALID_ARGUMENTS, "You need to send an even no of args " @@ -449,11 +450,11 @@ SigMatch *SigMatchGetLastSMFromLists(Signature *s, int args, ...) for (i = 0; i < args; i += 2) { int sm_type = va_arg(ap, int); SigMatch *sm_list = va_arg(ap, SigMatch *); - sm_new = SigMatchGetLastSM(sm_list, sm_type); + sm_new = SigMatchGetLastSMByType(sm_list, sm_type); if (sm_new == NULL) - continue; + continue; if (sm_last == NULL || sm_new->idx > sm_last->idx) - sm_last = sm_new; + sm_last = sm_new; } va_end(ap); @@ -461,6 +462,28 @@ SigMatch *SigMatchGetLastSMFromLists(Signature *s, int args, ...) return sm_last; } +/** + * \brief Returns the sm with the largest index (added latest) from this sig + * + * \retval Pointer to Last sm. + */ +SigMatch *SigMatchGetLastSM(const Signature *s) +{ + SigMatch *sm_last = NULL; + SigMatch *sm_new; + int i; + + for (i = 0; i < DETECT_SM_LIST_MAX; i ++) { + sm_new = s->sm_lists_tail[i]; + if (sm_new == NULL) + continue; + if (sm_last == NULL || sm_new->idx > sm_last->idx) + sm_last = sm_new; + } + + return sm_last; +} + void SigMatchTransferSigMatchAcrossLists(SigMatch *sm, SigMatch **src_sm_list, SigMatch **src_sm_list_tail, SigMatch **dst_sm_list, SigMatch **dst_sm_list_tail) diff --git a/src/detect-parse.h b/src/detect-parse.h index e780b86ad6..6d1d454adf 100644 --- a/src/detect-parse.h +++ b/src/detect-parse.h @@ -45,7 +45,8 @@ Signature *SigAlloc(void); void SigFree(Signature *s); Signature *SigInit(DetectEngineCtx *,char *sigstr); Signature *SigInitReal(DetectEngineCtx *, char *); -SigMatch *SigMatchGetLastSMFromLists(Signature *, int, ...); +SigMatch *SigMatchGetLastSMFromLists(const Signature *, int, ...); +SigMatch *SigMatchGetLastSM(const Signature *); void SigMatchTransferSigMatchAcrossLists(SigMatch *sm, SigMatch **, SigMatch **s, SigMatch **, SigMatch **);