Rizin
unix-like reverse engineering framework and cli tools
match.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include "sdb.h"
5 #include <ctype.h>
6 
7 static inline int haveSuffix(const char *glob, int glob_len, const char *sfx) {
8  const int sfx_len = strlen(sfx);
9  return (glob_len > sfx_len && !strcmp(glob + glob_len - sfx_len, sfx));
10 }
11 
12 static inline int havePrefix(const char *glob, int glob_len, const char *pfx) {
13  const int pfx_len = strlen(pfx);
14  return (pfx_len < glob_len && !strncmp(glob, pfx, pfx_len));
15 }
16 
17 enum MatchFlag {
19  SDB_LIKE_ICASE = 1, // ?i
20  SDB_LIKE_START = 2, // ^
21  SDB_LIKE_END = 4, // $
22  SDB_LIKE_BASE64 = 8 // %
23 };
24 
25 static inline int mycmp(const char *a, const char *b, int n, int any) {
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 }
39 
40 static inline int strstr2(const char *a, const char *b, int n) {
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 }
51 
52 static inline bool compareString(const char *a, const char *b, int blen, int flags) {
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 }
94 
95 RZ_API bool sdb_match(const char *str, const char *glob) {
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 }
lzma_index ** i
Definition: index.h:629
RZ_API ut8 * sdb_decode(const char *in, int *len)
Definition: base64.c:37
#define RZ_API
#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
uint8_t ut8
Definition: lh5801.h:11
static bool compareString(const char *a, const char *b, int blen, int flags)
Definition: match.c:52
static int strstr2(const char *a, const char *b, int n)
Definition: match.c:40
static int havePrefix(const char *glob, int glob_len, const char *pfx)
Definition: match.c:12
MatchFlag
Definition: match.c:17
@ 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
RZ_API bool sdb_match(const char *str, const char *glob)
Definition: match.c:95
static int mycmp(const char *a, const char *b, int n, int any)
Definition: match.c:25
static int haveSuffix(const char *glob, int glob_len, const char *sfx)
Definition: match.c:7
int n
Definition: mipsasm.c:19
#define tolower(c)
Definition: safe-ctype.h:149
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