Rizin
unix-like reverse engineering framework and cli tools
match.c File Reference
#include "sdb.h"
#include <ctype.h>

Go to the source code of this file.

Enumerations

enum  MatchFlag {
  SDB_LIKE_NONE = 0 , SDB_LIKE_ICASE = 1 , SDB_LIKE_START = 2 , SDB_LIKE_END = 4 ,
  SDB_LIKE_BASE64 = 8
}
 

Functions

static int haveSuffix (const char *glob, int glob_len, const char *sfx)
 
static int havePrefix (const char *glob, int glob_len, const char *pfx)
 
static int mycmp (const char *a, const char *b, int n, int any)
 
static int strstr2 (const char *a, const char *b, int n)
 
static bool compareString (const char *a, const char *b, int blen, int flags)
 
RZ_API bool sdb_match (const char *str, const char *glob)
 

Enumeration Type Documentation

◆ MatchFlag

enum MatchFlag
Enumerator
SDB_LIKE_NONE 
SDB_LIKE_ICASE 
SDB_LIKE_START 
SDB_LIKE_END 
SDB_LIKE_BASE64 

Definition at line 17 of file match.c.

17  {
18  SDB_LIKE_NONE = 0,
19  SDB_LIKE_ICASE = 1, // ?i
20  SDB_LIKE_START = 2, // ^
21  SDB_LIKE_END = 4, // $
22  SDB_LIKE_BASE64 = 8 // %
23 };
@ SDB_LIKE_ICASE
Definition: match.c:19
@ SDB_LIKE_END
Definition: match.c:21
@ SDB_LIKE_NONE
Definition: match.c:18
@ SDB_LIKE_BASE64
Definition: match.c:22
@ SDB_LIKE_START
Definition: match.c:20

Function Documentation

◆ compareString()

static bool compareString ( const char *  a,
const char *  b,
int  blen,
int  flags 
)
inlinestatic

Definition at line 52 of file match.c.

52  {
53  const int start = flags & SDB_LIKE_START;
54  const int end = flags & SDB_LIKE_END;
55  char *aa = NULL;
56  int alen;
57  bool ret = false;
58  if (!a || !b || blen < 0) {
59  return 0;
60  }
61  if (flags & SDB_LIKE_BASE64) {
62  aa = (char *)sdb_decode(a, &alen);
63  if (!aa) {
64  return 0;
65  }
66  a = (const char *)aa;
67  } else {
68  alen = strlen(a);
69  }
70  if (blen <= alen) {
71  if (flags & SDB_LIKE_ICASE) {
72  if (start && end)
73  ret = (alen == blen && !mycmp(a, b, blen, 0));
74  else if (start)
75  ret = !mycmp(a, b, blen, 0);
76  else if (end)
77  ret = !mycmp(a + (alen - blen), b, blen, 0);
78  else
79  ret = !mycmp(a, b, blen, 1);
80  } else {
81  if (start && end)
82  ret = (alen == blen && !strncmp(a, b, blen));
83  else if (start)
84  ret = !strncmp(a, b, blen);
85  else if (end)
86  ret = !strncmp(a + (alen - blen), b, blen);
87  else
88  ret = strstr2(a, b, blen);
89  }
90  }
91  free(aa);
92  return ret;
93 }
RZ_API ut8 * sdb_decode(const char *in, int *len)
Definition: base64.c:37
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static int strstr2(const char *a, const char *b, int n)
Definition: match.c:40
static int mycmp(const char *a, const char *b, int n, int any)
Definition: match.c:25
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, b, test_evm::end, flags, free(), mycmp(), NULL, sdb_decode(), SDB_LIKE_BASE64, SDB_LIKE_END, SDB_LIKE_ICASE, SDB_LIKE_START, start, and strstr2().

Referenced by sdb_match().

◆ havePrefix()

static int havePrefix ( const char *  glob,
int  glob_len,
const char *  pfx 
)
inlinestatic

Definition at line 12 of file match.c.

12  {
13  const int pfx_len = strlen(pfx);
14  return (pfx_len < glob_len && !strncmp(glob, pfx, pfx_len));
15 }

Referenced by sdb_match().

◆ haveSuffix()

static int haveSuffix ( const char *  glob,
int  glob_len,
const char *  sfx 
)
inlinestatic

Definition at line 7 of file match.c.

7  {
8  const int sfx_len = strlen(sfx);
9  return (glob_len > sfx_len && !strcmp(glob + glob_len - sfx_len, sfx));
10 }

Referenced by sdb_match().

◆ mycmp()

static int mycmp ( const char *  a,
const char *  b,
int  n,
int  any 
)
inlinestatic

Definition at line 25 of file match.c.

25  {
26  int i, j;
27  for (i = j = 0; a[i] && b[j] && j < n; i++) {
28  if (tolower((const ut8)a[i]) == tolower((const ut8)b[j])) {
29  j++;
30  } else {
31  if (!any) {
32  return 0;
33  }
34  j = 0;
35  }
36  }
37  return any ? j != n : 1;
38 }
lzma_index ** i
Definition: index.h:629
uint8_t ut8
Definition: lh5801.h:11
int n
Definition: mipsasm.c:19
#define tolower(c)
Definition: safe-ctype.h:149

References a, b, i, n, and tolower.

Referenced by compareString().

◆ sdb_match()

RZ_API bool sdb_match ( const char *  str,
const char *  glob 
)

Definition at line 95 of file match.c.

95  {
96  int glob_len, flags = SDB_LIKE_NONE;
97  if (!str || !glob) {
98  return false;
99  }
100  glob_len = strlen(glob);
101  if (haveSuffix(glob, glob_len, "?i")) {
102  glob_len -= 2;
104  }
105  if (havePrefix(glob, glob_len, "%")) {
106  glob++;
107  glob_len--;
109  }
110  if (havePrefix(glob, glob_len, "^")) {
111  glob++;
112  glob_len--;
114  }
115  if (haveSuffix(glob, glob_len, "$")) {
116  glob_len--;
117  flags |= SDB_LIKE_END;
118  }
119  return compareString(str, glob, glob_len, flags);
120 }
static bool compareString(const char *a, const char *b, int blen, int flags)
Definition: match.c:52
static int havePrefix(const char *glob, int glob_len, const char *pfx)
Definition: match.c:12
static int haveSuffix(const char *glob, int glob_len, const char *sfx)
Definition: match.c:7

References compareString(), flags, havePrefix(), haveSuffix(), SDB_LIKE_BASE64, SDB_LIKE_END, SDB_LIKE_ICASE, SDB_LIKE_NONE, SDB_LIKE_START, and cmd_descs_generate::str.

Referenced by like_cb(), and unset_cb().

◆ strstr2()

static int strstr2 ( const char *  a,
const char *  b,
int  n 
)
inlinestatic

Definition at line 40 of file match.c.

40  {
41  int i, j;
42  for (i = j = 0; a[i] && b[j] && j < n; i++) {
43  if (a[i] == b[j]) {
44  j++;
45  } else {
46  j = 0;
47  }
48  }
49  return j == n;
50 }

References a, b, i, and n.

Referenced by compareString().