Rizin
unix-like reverse engineering framework and cli tools
strings.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2006-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_search.h"
5 
6 // TODO: this file needs some love
7 enum {
10 };
11 
12 static char *encodings[3] = { "ascii", "cp850", NULL };
13 // static int encoding = ENCODING_ASCII; // default
14 // encoding = resolve_encoding(config_get("cfg.encoding"));
15 
17  int i;
18  if (!name || !*name) {
19  return ENCODING_ASCII;
20  }
21  ut32 lename = strlen(name);
22  for (i = 0; encodings[i]; i++) {
23  ut32 sz = RZ_MIN(strlen(encodings[i]), lename);
24  if (!rz_str_ncasecmp(name, encodings[i], sz)) {
25  return i;
26  }
27  }
28  return ENCODING_ASCII;
29 }
30 
31 static bool is_encoded(int encoding, unsigned char c) {
32  switch (encoding) {
33  case ENCODING_ASCII:
34  break;
35  case ENCODING_CP850:
36  switch (c) {
37  // CP850
38  case 128: // cedilla
39  case 133: // a grave
40  case 135: // minicedilla
41  case 160: // a acute
42  case 161: // i acute
43  case 129: // u dieresi
44  case 130: // e acute
45  case 139: // i dieresi
46  case 162: // o acute
47  case 163: // u acute
48  case 164: // enye
49  case 165: // enyemay
50  case 181: // A acute
51  case 144: // E acute
52  case 214: // I acute
53  case 224: // O acute
54  case 233: // U acute
55  return true;
56  }
57  break;
58  }
59  return false;
60 }
61 
63  int i = 0;
64  int matches = 0;
65  char str[4096];
67  RzSearchKeyword *kw;
68 
69  rz_list_foreach (s->kws, iter, kw) {
70  for (i = 0; i < len; i++) {
71  char ch = buf[i];
72  // non-cp850 encoded
73  if (IS_PRINTABLE(ch) || IS_WHITESPACE(ch) || is_encoded(0, ch)) {
74  str[matches] = ch;
75  if (matches < sizeof(str)) {
76  matches++;
77  }
78  } else {
79  /* wide char check \x??\x00\x??\x00 */
80  if (matches && i + 2 < len && buf[i + 2] == '\0' && buf[i] == '\0' && buf[i + 1] != '\0') {
81  return 1; // widechar
82  }
83  /* check if the length fits on our request */
84  if (matches >= s->string_min && (s->string_max == 0 || matches <= s->string_max)) {
85  str[matches] = '\0';
86  int len = strlen(str);
87  if (len > 2) {
88  ut64 off = (ut64)from + i - matches;
89  rz_search_hit_new(s, kw, off);
90  }
91  fflush(stdout);
92  }
93  matches = 0;
94  }
95  }
96  }
97  return 0;
98 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define RZ_API
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
int off
Definition: pal.c:13
static RzSocket * s
Definition: rtr.c:28
RZ_API int rz_str_ncasecmp(const char *dst, const char *orig, size_t n)
Definition: str.c:129
#define IS_WHITESPACE(x)
Definition: rz_str_util.h:13
#define IS_PRINTABLE(x)
Definition: rz_str_util.h:10
#define RZ_MIN(x, y)
RZ_API int rz_search_hit_new(RzSearch *s, RzSearchKeyword *kw, ut64 addr)
Definition: search.c:107
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
#define c(i)
Definition: sha256.c:43
static char * encodings[3]
Definition: strings.c:12
RZ_API int rz_search_strings_update(RzSearch *s, ut64 from, const ut8 *buf, int len)
Definition: strings.c:62
@ ENCODING_CP850
Definition: strings.c:9
@ ENCODING_ASCII
Definition: strings.c:8
static bool is_encoded(int encoding, unsigned char c)
Definition: strings.c:31
RZ_API int rz_search_get_encoding(const char *name)
Definition: strings.c:16
Definition: z80asm.h:102
ut64(WINAPI *w32_GetEnabledXStateFeatures)()