Rizin
unix-like reverse engineering framework and cli tools
html.c File Reference
#include <rz_cons.h>

Go to the source code of this file.

Functions

static bool gethtmlrgb (const char *str, char *buf)
 
static const char * gethtmlcolor (const char ptrch)
 
RZ_API char * rz_cons_html_filter (const char *ptr, int *newlen)
 

Function Documentation

◆ gethtmlcolor()

static const char* gethtmlcolor ( const char  ptrch)
static

Definition at line 17 of file html.c.

17  {
18  switch (ptrch) {
19  case '0': return "#000"; // BLACK
20  case '1': return "#f00"; // RED
21  case '2': return "#0f0"; // GREEN
22  case '3': return "#ff0"; // YELLOW
23  case '4': return "#00f"; // BLUE
24  case '5': return "#f0f"; // MAGENTA
25  case '6': return "#aaf"; // TURQOISE
26  case '7': return "#fff"; // WHITE
27  case '8': return "#777"; // GREY
28  case '9': break; // default
29  }
30  return "";
31 }

Referenced by rz_cons_html_filter().

◆ gethtmlrgb()

static bool gethtmlrgb ( const char *  str,
char *  buf 
)
static

Definition at line 7 of file html.c.

7  {
8  ut8 r = 0, g = 0, b = 0;
9  if (rz_cons_rgb_parse(str, &r, &g, &b, 0)) {
10  sprintf(buf, "#%02x%02x%02x", r, g, b);
11  return true;
12  }
13  buf[0] = '\0';
14  return false;
15 }
#define r
Definition: crypto_rc6.c:12
struct @667 g
voidpf void * buf
Definition: ioapi.h:138
sprintf
Definition: kernel.h:365
uint8_t ut8
Definition: lh5801.h:11
RZ_API int rz_cons_rgb_parse(const char *p, ut8 *r, ut8 *g, ut8 *b, ut8 *a)
Definition: rgb.c:113
#define b(i)
Definition: sha256.c:42

References b, g, r, rz_cons_rgb_parse(), sprintf, and cmd_descs_generate::str.

Referenced by rz_cons_html_filter().

◆ rz_cons_html_filter()

RZ_API char* rz_cons_html_filter ( const char *  ptr,
int newlen 
)

Definition at line 34 of file html.c.

34  {
35  const char *str = ptr;
36  int esc = 0;
37  int len = 0;
38  bool inv = false;
39  char text_color[16] = { 0 };
40  char background_color[16] = { 0 };
41  bool has_set = false;
42  bool need_to_set = false;
43  bool need_to_clear = false;
44  bool first_style;
45  int tmp;
46  if (!ptr) {
47  return NULL;
48  }
49  RzStrBuf *res = rz_strbuf_new("");
50  if (!res) {
51  return NULL;
52  }
53  for (; ptr[0]; ptr = ptr + 1) {
54  if (esc == 0 && ptr[0] != 0x1b && need_to_set) {
55  if (has_set) {
56  rz_strbuf_append(res, "</font>");
57  has_set = false;
58  }
59  if (!need_to_clear) {
60  first_style = true;
61  rz_strbuf_append(res, "<font");
62  if (text_color[0]) {
63  rz_strbuf_appendf(res, " color='%s'", text_color);
64  }
65  if (background_color[0]) {
66  rz_strbuf_append(res, first_style ? " style='" : ";");
67  rz_strbuf_appendf(res, "background-color:%s", background_color);
68  first_style = false;
69  }
70  if (inv) {
71  rz_strbuf_append(res, first_style ? " style='" : ";");
72  rz_strbuf_append(res, "text-decoration:underline overline");
73  first_style = false;
74  }
75  rz_strbuf_append(res, first_style ? ">" : "'>");
76  has_set = true;
77  }
78  need_to_clear = false;
79  need_to_set = false;
80  }
81  if (ptr[0] == '\n') {
82  tmp = (int)(size_t)(ptr - str);
83  rz_strbuf_append_n(res, str, tmp);
84  if (!ptr[1]) {
85  // write new line if it's the end of the output
86  rz_strbuf_append(res, "\n");
87  } else {
88  rz_strbuf_append(res, "<br />");
89  }
90  str = ptr + 1;
91  continue;
92  } else if (ptr[0] == '<') {
93  tmp = (int)(size_t)(ptr - str);
94  rz_strbuf_append_n(res, str, tmp);
95  rz_strbuf_append(res, "&lt;");
96  str = ptr + 1;
97  continue;
98  } else if (ptr[0] == '>') {
99  tmp = (int)(size_t)(ptr - str);
100  rz_strbuf_append_n(res, str, tmp);
101  rz_strbuf_append(res, "&gt;");
102  str = ptr + 1;
103  continue;
104  } else if (ptr[0] == ' ') {
105  tmp = (int)(size_t)(ptr - str);
106  rz_strbuf_append_n(res, str, tmp);
107  rz_strbuf_append(res, "&nbsp;");
108  str = ptr + 1;
109  continue;
110  }
111  if (ptr[0] == 0x1b) {
112  esc = 1;
113  tmp = (int)(size_t)(ptr - str);
114  rz_strbuf_append_n(res, str, tmp);
115  str = ptr + 1;
116  continue;
117  }
118  if (esc == 1) {
119  // \x1b[2J
120  if (ptr[0] != '[') {
121  eprintf("Oops invalid escape char\n");
122  esc = 0;
123  str = ptr + 1;
124  continue;
125  }
126  esc = 2;
127  continue;
128  } else if (esc == 2) {
129  // TODO: use dword comparison here
130  if (ptr[0] == '0' && ptr[1] == 'J') { // RZ_CONS_CLEAR_FROM_CURSOR_TO_END
131  ptr += 2;
132  esc = 0;
133  str = ptr;
134  } else if (!memcmp(ptr, "2K", 2)) {
135  ptr += 2;
136  esc = 0;
137  str = ptr;
138  continue;
139  } else if (ptr[0] == '2' && ptr[1] == 'J') {
140  rz_strbuf_append(res, "<hr />");
141  ptr++;
142  esc = 0;
143  str = ptr;
144  continue;
145  } else if (!strncmp(ptr, "48;5;", 5) || !strncmp(ptr, "48;2;", 5)) {
146  char *end = strchr(ptr, 'm');
147  gethtmlrgb(ptr, background_color);
148  need_to_set = true;
149  ptr = end;
150  str = ptr + 1;
151  esc = 0;
152  } else if (!strncmp(ptr, "38;5;", 5) || !strncmp(ptr, "38;2;", 5)) {
153  char *end = strchr(ptr, 'm');
154  gethtmlrgb(ptr, text_color);
155  need_to_set = true;
156  ptr = end;
157  str = ptr + 1;
158  esc = 0;
159  } else if (ptr[0] == '0' && ptr[1] == ';' && ptr[2] == '0') {
160  rz_cons_gotoxy(0, 0);
161  ptr += 4;
162  esc = 0;
163  str = ptr;
164  continue;
165  } else if (ptr[0] == '0' && ptr[1] == 'm') {
166  str = (++ptr) + 1;
167  esc = 0;
168  inv = false;
169  text_color[0] = '\0';
170  background_color[0] = '\0';
171  need_to_set = need_to_clear = true;
172  continue;
173  // reset color
174  } else if (!strncmp(ptr, "27m", 3)) {
175  inv = false;
176  need_to_set = true;
177  ptr = ptr + 2;
178  str = ptr + 1;
179  esc = 0;
180  continue;
181  // reset invert color
182  } else if (ptr[0] == '7' && ptr[1] == 'm') {
183  str = (++ptr) + 1;
184  inv = true;
185  need_to_set = true;
186  esc = 0;
187  continue;
188  // invert color
189  } else if (ptr[0] == '3' && ptr[2] == 'm') {
190  const char *htmlColor = gethtmlcolor(ptr[1]);
191  if (htmlColor) {
192  rz_str_ncpy(text_color, htmlColor, sizeof(text_color));
193  }
194  need_to_set = true;
195  ptr = ptr + 2;
196  str = ptr + 1;
197  esc = 0;
198  continue;
199  } else if (ptr[0] == '4' && ptr[2] == 'm') {
200  const char *htmlColor = gethtmlcolor(ptr[1]);
201  if (htmlColor) {
202  rz_str_ncpy(background_color, htmlColor, sizeof(background_color));
203  }
204  need_to_set = true;
205  ptr = ptr + 2;
206  str = ptr + 1;
207  esc = 0;
208  continue;
209  }
210  }
211  len++;
212  }
213  rz_strbuf_append_n(res, str, ptr - str);
214  if (has_set) {
215  rz_strbuf_append(res, "</font>");
216  }
217  if (newlen) {
218  *newlen = res->len;
219  }
220  return rz_strbuf_drain(res);
221 }
size_t len
Definition: 6502dis.c:15
RZ_API void rz_cons_gotoxy(int x, int y)
Definition: cons.c:724
#define NULL
Definition: cris-opc.c:27
static const char * gethtmlcolor(const char ptrch)
Definition: html.c:17
static bool gethtmlrgb(const char *str, char *buf)
Definition: html.c:7
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API size_t rz_str_ncpy(char *dst, const char *src, size_t n)
Secure string copy with null terminator.
Definition: str.c:923
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API bool rz_strbuf_append_n(RzStrBuf *sb, const char *s, size_t l)
Definition: strbuf.c:229
static int
Definition: sfsocketcall.h:114
size_t len
Definition: rz_strbuf.h:12

References test_evm::end, eprintf, gethtmlcolor(), gethtmlrgb(), int, len, RzStrBuf::len, NULL, rz_cons_gotoxy(), rz_str_ncpy(), rz_strbuf_append(), rz_strbuf_append_n(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), cmd_descs_generate::str, and autogen_x86imm::tmp.

Referenced by ds_newline(), and rz_cons_filter().