Rizin
unix-like reverse engineering framework and cli tools
name.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 
6 RZ_API bool rz_name_validate_char(const char ch, bool strict) {
7  if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (IS_DIGIT(ch))) {
8  return true;
9  }
10  switch (ch) {
11  case ':':
12  case '.':
13  case '_':
14  return true;
15  case '-':
16  case '[':
17  case ']':
18  return !strict;
19  default:
20  return false;
21  }
22 }
23 
24 RZ_API bool rz_name_check(const char *name, bool strict) {
25  /* Cannot start by number */
26  if (!name || !*name || IS_DIGIT(*name)) {
27  return false;
28  }
29  /* Cannot contain non-alphanumeric chars + [:._] */
30  for (; *name != '\0'; name++) {
31  if (!rz_name_validate_char(*name, strict)) {
32  return false;
33  }
34  }
35  return true;
36 }
37 
38 static inline bool is_special_char(char *name) {
39  const char n = *name;
40  return (n == 'b' || n == 'f' || n == 'n' || n == 'r' || n == 't' || n == 'v' || n == 'a');
41 }
42 
43 RZ_API bool rz_name_filter(char *name, int maxlen, bool strict) {
44  size_t i, len;
45  if (!name) {
46  return false;
47  }
48  if (maxlen < 0) {
49  maxlen = strlen(name);
50  }
52  char *oname = name;
53  for (i = 0; *name; name++, i++) {
54  if (maxlen && i > maxlen) {
55  *name = '\0';
56  break;
57  }
58  if (!rz_name_validate_char(*name, strict) && *name != '\\') {
59  *name = '_';
60  }
61  }
62  while (i > 0) {
63  if (*(name - 1) == '\\' && is_special_char(name)) {
64  *name = '_';
65  *(name - 1) = '_';
66  }
67  if (*name == '\\') {
68  *name = '_';
69  }
70  name--;
71  i--;
72  }
73  if (*name == '\\') {
74  *name = '_';
75  }
76  // trimming trailing and leading underscores
77  len = strlen(name);
78  for (; len > 0 && *(name + len - 1) == '_'; len--) {
79  ;
80  }
81  if (!len) { // name consists only of underscores
82  return rz_name_check(oname, strict);
83  }
84  for (i = 0; *(name + i) == '_'; i++, len--) {
85  ;
86  }
87  memmove(name, name + i, len);
88  *(name + len) = '\0';
89  return rz_name_check(oname, strict);
90 }
91 
92 RZ_API char *rz_name_filter2(const char *name, bool strict) {
93  size_t i;
94  while (!rz_name_validate_char(*name, strict)) {
95  name++;
96  }
97  char *res = strdup(name);
98  for (i = 0; res[i]; i++) {
99  if (!rz_name_validate_char(res[i], strict)) {
100  res[i] = '_';
101  }
102  }
103  for (i--; i != 0 && res[i] == '_'; i--) {
104  res[i] = '\0';
105  }
106  return res;
107 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define RZ_API
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int n
Definition: mipsasm.c:19
RZ_API bool rz_name_validate_char(const char ch, bool strict)
Definition: name.c:6
static bool is_special_char(char *name)
Definition: name.c:38
RZ_API char * rz_name_filter2(const char *name, bool strict)
Definition: name.c:92
RZ_API bool rz_name_filter(char *name, int maxlen, bool strict)
Definition: name.c:43
RZ_API bool rz_name_check(const char *name, bool strict)
Definition: name.c:24
const char * name
Definition: op.c:541
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str)
Removes whitespace characters (space, tab, newline etc.) from the beginning and end of a string.
Definition: str_trim.c:190
#define IS_DIGIT(x)
Definition: rz_str_util.h:11
Definition: z80asm.h:102
ut64 maxlen
Definition: core.c:76