Rizin
unix-like reverse engineering framework and cli tools
str.c File Reference
#include <rz_regex.h>
#include "rz_list.h"
#include "rz_types.h"
#include "rz_util.h"
#include "rz_cons.h"
#include "rz_bin.h"
#include "rz_util/rz_assert.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wchar.h>
#include <stdarg.h>
#include <rz_util/rz_base64.h>
#include <rz_util/rz_utf8.h>
#include <rz_util/rz_utf16.h>
#include <rz_util/rz_utf32.h>
#include <rz_util/rz_ebcdic.h>
#include <rz_userconf.h>

Go to the source code of this file.

Macros

#define HASCH(x)   strchr(input_value, x)
 
#define CAST   (void *)(size_t)
 
#define RZ_STR_PKG_VERSION_STRING   ""
 

Functions

RZ_API const char * rz_str_enc_as_string (RzStrEnc enc)
 
RZ_API RzStrEnc rz_str_enc_string_as_type (RZ_NULLABLE const char *encoding)
 converts an encoding name to RzStrEnc More...
 
RZ_API int rz_str_casecmp (const char *s1, const char *s2)
 
RZ_API int rz_str_ncasecmp (const char *s1, const char *s2, size_t n)
 
RZ_API int rz_str_replace_ch (char *s, char a, char b, bool global)
 
RZ_API int rz_str_replace_char_once (char *s, int a, int b)
 
RZ_API int rz_str_replace_char (char *s, int a, int b)
 
RZ_API void rz_str_remove_char (char *str, char c)
 
RZ_API void rz_str_reverse (char *str)
 
RZ_API int rz_str_bits (char *strout, const ut8 *buf, int len, const char *bitz)
 
RZ_API const char * rz_str_sysbits (const int v)
 
static void trimbits (char *b)
 
RZ_API int rz_str_bits64 (char *strout, ut64 in)
 
RZ_API ut64 rz_str_bits_from_string (const char *buf, const char *bitz)
 
RZ_API int rz_str_binstr2bin (const char *str, ut8 *out, int outlen)
 
RZ_API int rz_str_rwx (const char *str)
 
RZ_API const char * rz_str_rwx_i (int rwx)
 
RZ_API void rz_str_case (char *str, bool up)
 
RZ_API char * rz_str_home (const char *str)
 
RZ_API ut64 rz_str_djb2_hash (const char *s)
 
RZ_API int rz_str_delta (char *p, char a, char b)
 
RZ_API size_t rz_str_split (char *str, char ch)
 Split string str in place by using ch as a delimiter. More...
 
RZ_API int rz_str_word_set0 (char *str)
 
RZ_API int rz_str_word_set0_stack (char *str)
 
RZ_API char * rz_str_word_get0set (char *stra, int stralen, int idx, const char *newstr, int *newlen)
 
RZ_API const char * rz_str_word_get0 (const char *str, int idx)
 
RZ_API int rz_str_char_count (const char *string, char ch)
 
static const char * separator_get_first (const char *text)
 
static const char * word_get_first (const char *text)
 
RZ_API char * rz_str_word_get_first (const char *text)
 
RZ_API int rz_str_word_count (const char *string)
 
RZ_API char * rz_str_ichr (char *str, char chr)
 
RZ_API const char * rz_str_lchr (const char *str, char chr)
 
RZ_API const char * rz_sub_str_lchr (const char *str, int start, int end, char chr)
 
RZ_API const char * rz_sub_str_rchr (const char *str, int start, int end, char chr)
 
RZ_API bool rz_str_is2utf8 (RZ_NONNULL const char *c)
 Checks if the given character string is a two byte UTF-8 character. More...
 
RZ_API bool rz_str_is3utf8 (RZ_NONNULL const char *c)
 Checks if the given character string is a three byte UTF-8 character. More...
 
RZ_API bool rz_str_is4utf8 (RZ_NONNULL const char *c)
 Checks if the given character string is a four byte UTF-8 character. More...
 
RZ_API bool rz_str_isXutf8 (RZ_NONNULL const char *c, ut8 x)
 Checks if the byte string matches the criteria of a UTF-8 character of length x. More...
 
RZ_API const char * rz_str_strchr (RZ_NONNULL const char *str, RZ_NONNULL const char *c)
 Returns a pointer to the first occurrence of UTF-8 character c in the string s. More...
 
RZ_API const char * rz_str_sep (const char *base, const char *sep)
 
RZ_API const char * rz_str_rsep (const char *base, const char *p, const char *sep)
 
RZ_API const char * rz_str_rstr (const char *base, const char *p)
 
RZ_API const char * rz_str_rchr (const char *base, const char *p, int ch)
 
RZ_API const char * rz_str_nstr (const char *s, const char *find, int slen)
 
RZ_API char * rz_str_new (const char *str)
 
RZ_API char * rz_str_newlen (const char *str, int len)
 
RZ_API char * rz_str_trunc_ellipsis (const char *str, int len)
 
RZ_API char * rz_str_newf (const char *fmt,...)
 
RZ_API size_t rz_str_ncpy (char *dst, const char *src, size_t dst_size)
 Secure string copy with null terminator. More...
 
RZ_API bool rz_str_ccmp (const char *dst, const char *src, int ch)
 
RZ_API bool rz_str_cmp_list (const char *list, const char *item, char sep)
 
RZ_API int rz_str_cmp (const char *a, const char *b, int len)
 
RZ_API int rz_str_ccpy (char *dst, char *src, int ch)
 
RZ_API char * rz_str_ndup (RZ_NULLABLE const char *ptr, int len)
 Create new copy of string ptr limited to size len. More...
 
RZ_API char * rz_str_dup (char *ptr, const char *string)
 
RZ_API char * rz_str_prepend (char *ptr, const char *string)
 
RZ_API char * rz_str_appendlen (char *ptr, const char *string, int slen)
 
RZ_API char * rz_str_append_owned (char *ptr, char *string)
 
RZ_API char * rz_str_append (char *ptr, const char *string)
 
RZ_API char * rz_str_appendf (char *ptr, const char *fmt,...)
 
RZ_API char * rz_str_appendch (char *x, char y)
 
RZ_API char * rz_str_replace (char *str, const char *key, const char *val, int g)
 
RZ_API char * rz_str_replace_icase (char *str, const char *key, const char *val, int g, int keep_case)
 
RZ_API char * rz_str_replace_thunked (char *str, char *clean, int *thunk, int clen, const char *key, const char *val, int g)
 
RZ_API char * rz_str_replace_in (char *str, ut32 sz, const char *key, const char *val, int g)
 
RZ_API int rz_str_unescape (char *buf)
 
RZ_API void rz_str_sanitize (char *c)
 
RZ_API char * rz_str_sanitize_sdb_key (const char *s)
 
RZ_API void rz_str_byte_escape (const char *p, char **dst, RzStrEscOptions *opt)
 Converts unprintable characters to C-like backslash representation. More...
 
static RZ_OWN char * rz_str_escape_ (const char *buf, bool parse_esc_seq, bool ign_esc_seq, RzStrEscOptions *opt)
 
RZ_API RZ_OWN char * rz_str_escape (RZ_NONNULL const char *buf)
 
RZ_API char * rz_str_escape_sh (const char *buf)
 
RZ_API char * rz_str_escape_dot (const char *buf)
 
RZ_API char * rz_str_escape_8bit (const char *buf, bool colors, RzStrEscOptions *opt)
 
static char * rz_str_escape_utf (const char *buf, int buf_size, RzStrEnc enc, bool show_asciidot, bool esc_bslash, bool esc_double_quotes, bool keep_printable)
 
RZ_API char * rz_str_escape_utf8 (const char *buf, RzStrEscOptions *opt)
 
RZ_API char * rz_str_escape_utf8_keep_printable (const char *buf, RzStrEscOptions *opt)
 
RZ_API char * rz_str_escape_utf16le (const char *buf, int buf_size, RzStrEscOptions *opt)
 
RZ_API char * rz_str_escape_utf32le (const char *buf, int buf_size, RzStrEscOptions *opt)
 
RZ_API char * rz_str_escape_utf16be (const char *buf, int buf_size, RzStrEscOptions *opt)
 
RZ_API char * rz_str_escape_utf32be (const char *buf, int buf_size, RzStrEscOptions *opt)
 
static char * escape_utf8_for_json (const char *buf, int buf_size, bool mutf8)
 
RZ_API char * rz_str_escape_utf8_for_json (const char *buf, int buf_size)
 
RZ_API char * rz_str_escape_mutf8_for_json (const char *buf, int buf_size)
 
RZ_API char * rz_str_format_msvc_argv (size_t argc, const char **argv)
 
static size_t __str_ansi_length (char const *str)
 
RZ_API size_t rz_str_ansi_nlen (const char *str, size_t slen)
 
RZ_API size_t rz_str_ansi_len (const char *str)
 
RZ_API size_t rz_str_nlen (const char *str, size_t n)
 
RZ_API size_t rz_str_nlen_w (const char *str, int n)
 
RZ_API bool rz_str_is_ascii (const char *str)
 
RZ_API bool rz_str_is_whitespace (RZ_NONNULL const char *str)
 Checks if the whole string is composed of whitespace. More...
 
RZ_API bool rz_str_is_utf8 (RZ_NONNULL const char *str)
 Returns true if the input string is correctly UTF-8-encoded. More...
 
RZ_API bool rz_str_is_printable (const char *str)
 
RZ_API bool rz_str_is_printable_limited (const char *str, int size)
 
RZ_API bool rz_str_is_printable_incl_newlines (const char *str)
 
RZ_API size_t rz_wstr_clen (const char *s)
 
RZ_API const char * rz_str_ansi_chrn (const char *str, size_t n)
 
RZ_API int rz_str_ansi_filter (char *str, char **out, int **cposs, int len)
 
RZ_API char * rz_str_ansi_crop (const char *str, ut32 x, ut32 y, ut32 x2, ut32 y2)
 
RZ_API size_t rz_str_utf8_codepoint (const char *s, size_t left)
 
RZ_API bool rz_str_char_fullwidth (const char *s, size_t left)
 
RZ_API size_t rz_str_utf8_charsize (const char *str)
 
RZ_API size_t rz_str_utf8_charsize_prev (const char *str, int prev_len)
 
RZ_API size_t rz_str_utf8_charsize_last (const char *str)
 
RZ_API void rz_str_filter_zeroline (char *str, int len)
 
RZ_API void rz_str_filter (char *str)
 Convert all non-printable characters in str with '.'. More...
 
RZ_API bool rz_str_glob (const char *str, const char *glob)
 
RZ_API char * rz_str_arg_escape (const char *arg)
 
RZ_API int rz_str_arg_unescape (char *arg)
 
RZ_API char * rz_str_path_escape (const char *path)
 
RZ_API int rz_str_path_unescape (char *path)
 
RZ_API char ** rz_str_argv (const char *cmdline, int *_argc)
 
RZ_API void rz_str_argv_free (char **argv)
 
RZ_API const char * rz_str_firstbut (const char *s, char ch, const char *but)
 
RZ_API const char * rz_str_lastbut (const char *s, char ch, const char *but)
 
RZ_API size_t rz_str_len_utf8char (const char *s, int left)
 
RZ_API size_t rz_str_len_utf8 (const char *s)
 
RZ_API size_t rz_str_len_utf8_ansi (const char *str)
 
RZ_API const char * rz_strstr_ansi (const char *a, const char *b)
 
RZ_API const char * rz_str_casestr (const char *a, const char *b)
 
RZ_API int rz_str_write (int fd, const char *b)
 
RZ_API void rz_str_range_foreach (const char *r, RzStrRangeCallback cb, void *u)
 
RZ_API bool rz_str_range_in (const char *r, ut64 addr)
 
RZ_API void rz_str_uri_decode (char *s)
 
RZ_API char * rz_str_uri_encode (const char *s)
 
RZ_API int rz_str_utf16_to_utf8 (ut8 *dst, int len_dst, const ut8 *src, int len_src, bool little_endian)
 
RZ_API char * rz_str_utf16_decode (const ut8 *s, int len)
 
RZ_API char * rz_str_utf16_encode (const char *s, int len)
 
RZ_API char * rz_str_prefix_all (const char *s, const char *pfx)
 
RZ_API ut8 rz_str_contains_macro (const char *input_value)
 
RZ_API void rz_str_truncate_cmd (char *string)
 
RZ_API const char * rz_str_closer_chr (const char *b, const char *s)
 
RZ_API int rz_str_bounds (const char *_str, int *h)
 
RZ_API char * rz_str_crop (const char *str, unsigned int x, unsigned int y, unsigned int x2, unsigned int y2)
 
RZ_API const char * rz_str_tok (const char *str1, const char b, size_t len)
 
RZ_API int rz_str_do_until_token (str_operation op, char *str, const char tok)
 
RZ_API const char * rz_str_pad (const char ch, int sz)
 
RZ_API char * rz_str_repeat (const char *ch, int sz)
 
RZ_API char * rz_str_between (const char *cmt, const char *prefix, const char *suffix)
 
RZ_API bool rz_str_startswith (RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
 Checks if a string starts with a specifc sequence of characters (case sensitive) More...
 
RZ_API bool rz_str_startswith_icase (RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
 Checks if a string starts with a specifc sequence of characters (case insensitive) More...
 
static bool str_endswith (RZ_NONNULL const char *str, RZ_NONNULL const char *needle, bool case_sensitive)
 
RZ_API bool rz_str_endswith (RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
 Checks if a string ends with a specifc sequence of characters (case sensitive) More...
 
RZ_API bool rz_str_endswith_icase (RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
 Checks if a string ends with a specifc sequence of characters (case insensitive) More...
 
static RzListstr_split_list_common (char *str, const char *c, int n, bool trim, bool dup)
 
static RzListstr_split_list_common_regex (RZ_BORROW char *str, RZ_BORROW RzRegex *r, int n, bool trim, bool dup)
 
RZ_API RzListrz_str_split_list (char *str, const char *c, int n)
 Split the string str according to the substring c and returns a RzList with the result. More...
 
RZ_API RZ_OWN RzListrz_str_split_list_regex (RZ_NONNULL char *str, RZ_NONNULL const char *r, int n)
 Split the string str according to the regex r and returns a RzList with the result. More...
 
RZ_API RzListrz_str_split_duplist (const char *_str, const char *c, bool trim)
 Split the string str according to the substring c and returns a RzList with the result. More...
 
RZ_API RzListrz_str_split_duplist_n (const char *_str, const char *c, int n, bool trim)
 Split the string str according to the substring c and returns a RzList with the result. More...
 
RZ_API RZ_OWN RzListrz_str_split_duplist_n_regex (RZ_NONNULL const char *_str, RZ_NONNULL const char *r, int n, bool trim)
 Split the string str according to the regex r and returns a RzList with the result. More...
 
RZ_API size_trz_str_split_lines (char *str, size_t *count)
 Split the string str in lines and returns the result in an array. More...
 
RZ_API bool rz_str_isnumber (const char *str)
 
RZ_API const char * rz_str_last (const char *str, const char *ch)
 
static int strncpy_with_color_codes (char *s1, char *s2, int n)
 
static int strncmp_skip_color_codes (const char *s1, const char *s2, int n)
 
static char * strchr_skip_color_codes (const char *s, int c)
 
RZ_API char * rz_str_highlight (char *str, const char *word, const char *color, const char *color_reset)
 
RZ_API wchar_t * rz_str_mb_to_wc_l (const char *buf, int len)
 
RZ_API char * rz_str_wc_to_mb_l (const wchar_t *buf, int len)
 
RZ_API char * rz_str_wc_to_mb (const wchar_t *buf)
 
RZ_API wchar_t * rz_str_mb_to_wc (const char *buf)
 
RZ_API char * rz_str_from_ut64 (ut64 val)
 
RZ_API int rz_snprintf (char *string, int len, const char *fmt,...)
 
RZ_API void rz_str_stripLine (char *str, const char *key)
 
RZ_API char * rz_str_list_join (RzList *str, const char *sep)
 
RZ_API char * rz_str_array_join (const char **a, size_t n, const char *sep)
 
RZ_API int rz_str_fmtargs (const char *fmt)
 
RZ_API const char * rz_str_bool (int b)
 
RZ_API bool rz_str_is_true (const char *s)
 
RZ_API bool rz_str_is_false (const char *s)
 
RZ_API bool rz_str_is_bool (const char *val)
 
RZ_API char * rz_str_nextword (char *s, char ch)
 
RZ_API char * rz_str_scale (const char *s, int w, int h)
 
RZ_API const char * rz_str_str_xy (const char *s, const char *word, const char *prev, int *x, int *y)
 
RZ_API RzListrz_str_wrap (char *str, size_t width)
 
RZ_API char * rz_str_version (const char *program)
 
RZ_API RzStrEnc rz_str_guess_encoding_from_buffer (RZ_NONNULL const ut8 *buffer, ut32 length)
 Tries to guess the string encoding method from the buffer. More...
 
RZ_API RZ_OWN char * rz_str_stringify_raw_buffer (RzStrStringifyOpt *option, RZ_NULLABLE RZ_OUT ut32 *length)
 Converts a raw buffer to a printable string based on the selected options. More...
 

Variables

static const char * rwxstr []
 

Macro Definition Documentation

◆ CAST

#define CAST   (void *)(size_t)

Definition at line 3075 of file str.c.

◆ HASCH

#define HASCH (   x)    strchr(input_value, x)

Definition at line 3074 of file str.c.

◆ RZ_STR_PKG_VERSION_STRING

#define RZ_STR_PKG_VERSION_STRING   ""

Definition at line 4048 of file str.c.

Function Documentation

◆ __str_ansi_length()

static size_t __str_ansi_length ( char const str)
static

Definition at line 1902 of file str.c.

1902  {
1903  size_t i = 1;
1904  if (str[0] == 0x1b) {
1905  if (str[1] == '[') {
1906  i++;
1907  while (str[i] && str[i] != 'J' && str[i] != 'm' && str[i] != 'H' && str[i] != 'K') {
1908  i++;
1909  }
1910  } else if (str[1] == '#') {
1911  while (str[i] && str[i] != 'q') {
1912  i++;
1913  }
1914  }
1915  if (str[i]) {
1916  i++;
1917  }
1918  }
1919  return i;
1920 }
lzma_index ** i
Definition: index.h:629

References i, and cmd_descs_generate::str.

Referenced by rz_str_ansi_chrn(), rz_str_ansi_filter(), rz_str_ansi_nlen(), rz_str_len_utf8_ansi(), and rz_strstr_ansi().

◆ escape_utf8_for_json()

static char* escape_utf8_for_json ( const char *  buf,
int  buf_size,
bool  mutf8 
)
static

Definition at line 1723 of file str.c.

1723  {
1724  char *new_buf, *q;
1725  const ut8 *p, *end;
1726  RzRune ch;
1727  int i, len, ch_bytes;
1728 
1729  if (!buf) {
1730  return NULL;
1731  }
1732  len = buf_size < 0 ? strlen(buf) : buf_size;
1733  end = (const ut8 *)buf + len;
1734  /* Worst case scenario, we convert every byte to \u00hh */
1735  new_buf = malloc(1 + (len * 6));
1736  if (!new_buf) {
1737  return NULL;
1738  }
1739  p = (const ut8 *)buf;
1740  q = new_buf;
1741  while (p < end) {
1742  ptrdiff_t bytes_left = end - p;
1743  ch_bytes = mutf8 ? rz_mutf8_decode(p, bytes_left, &ch) : rz_utf8_decode(p, bytes_left, &ch);
1744  if (ch_bytes == 1) {
1745  switch (*p) {
1746  case '\n':
1747  *q++ = '\\';
1748  *q++ = 'n';
1749  break;
1750  case '\r':
1751  *q++ = '\\';
1752  *q++ = 'r';
1753  break;
1754  case '\\':
1755  *q++ = '\\';
1756  *q++ = '\\';
1757  break;
1758  case '\t':
1759  *q++ = '\\';
1760  *q++ = 't';
1761  break;
1762  case '"':
1763  *q++ = '\\';
1764  *q++ = '"';
1765  break;
1766  case '\f':
1767  *q++ = '\\';
1768  *q++ = 'f';
1769  break;
1770  case '\b':
1771  *q++ = '\\';
1772  *q++ = 'b';
1773  break;
1774  default:
1775  if (!IS_PRINTABLE(*p)) {
1776  *q++ = '\\';
1777  *q++ = 'u';
1778  *q++ = '0';
1779  *q++ = '0';
1780  *q++ = "0123456789abcdef"[*p >> 4 & 0xf];
1781  *q++ = "0123456789abcdef"[*p & 0xf];
1782  } else {
1783  *q++ = *p;
1784  }
1785  }
1786  } else if (ch_bytes == 4) {
1787  if (rz_rune_is_printable(ch)) {
1788  // Assumes buf is UTF8-encoded
1789  for (i = 0; i < ch_bytes; i++) {
1790  *q++ = *(p + i);
1791  }
1792  } else {
1793  RzRune high, low;
1794  ch -= 0x10000;
1795  high = 0xd800 + (ch >> 10 & 0x3ff);
1796  low = 0xdc00 + (ch & 0x3ff);
1797  *q++ = '\\';
1798  *q++ = 'u';
1799  for (i = 2; i >= 0; i -= 2) {
1800  *q++ = "0123456789abcdef"[high >> 4 * (i + 1) & 0xf];
1801  *q++ = "0123456789abcdef"[high >> 4 * i & 0xf];
1802  }
1803  *q++ = '\\';
1804  *q++ = 'u';
1805  for (i = 2; i >= 0; i -= 2) {
1806  *q++ = "0123456789abcdef"[low >> 4 * (i + 1) & 0xf];
1807  *q++ = "0123456789abcdef"[low >> 4 * i & 0xf];
1808  }
1809  }
1810  } else if (ch_bytes > 1) {
1811  if (rz_rune_is_printable(ch)) {
1812  // Assumes buf is UTF8-encoded
1813  for (i = 0; i < ch_bytes; i++) {
1814  *q++ = *(p + i);
1815  }
1816  } else {
1817  *q++ = '\\';
1818  *q++ = 'u';
1819  for (i = 2; i >= 0; i -= 2) {
1820  *q++ = "0123456789abcdef"[ch >> 4 * (i + 1) & 0xf];
1821  *q++ = "0123456789abcdef"[ch >> 4 * i & 0xf];
1822  }
1823  }
1824  } else { // ch_bytes == 0
1825  // invalid utf-8
1826  ch_bytes = 1;
1827  }
1828  p += ch_bytes;
1829  }
1830  *q = '\0';
1831  return new_buf;
1832 }
size_t len
Definition: 6502dis.c:15
#define NULL
Definition: cris-opc.c:27
static int buf_size
Definition: debug_qnx.c:35
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
void * malloc(size_t size)
Definition: malloc.c:123
#define IS_PRINTABLE(x)
Definition: rz_str_util.h:10
RZ_API int rz_utf8_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf8.c:492
RZ_API bool rz_rune_is_printable(const RzRune c)
Returns true when the RzRune is a printable symbol.
Definition: utf8.c:606
RZ_API int rz_mutf8_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf8.c:524
ut32 RzRune
Definition: rz_utf8.h:13
int ptrdiff_t
Definition: sftypes.h:68

References buf_size, test_evm::end, i, IS_PRINTABLE, len, malloc(), NULL, p, rz_mutf8_decode(), rz_rune_is_printable(), and rz_utf8_decode().

Referenced by rz_str_escape_mutf8_for_json(), and rz_str_escape_utf8_for_json().

◆ rz_snprintf()

RZ_API int rz_snprintf ( char *  string,
int  len,
const char *  fmt,
  ... 
)

Definition at line 3804 of file str.c.

3804  {
3805  va_list ap;
3806  va_start(ap, fmt);
3807  int ret = vsnprintf(string, len, fmt, ap);
3808  string[len - 1] = 0;
3809  va_end(ap);
3810  return ret;
3811 }
vsnprintf
Definition: kernel.h:366

References len, and vsnprintf.

◆ rz_str_ansi_chrn()

RZ_API const char* rz_str_ansi_chrn ( const char *  str,
size_t  n 
)

Definition at line 2098 of file str.c.

2098  {
2099  int len, i, li;
2100  for (li = i = len = 0; str[i] && (n != len); i++) {
2101  size_t chlen = __str_ansi_length(str + i);
2102  if (chlen > 1) {
2103  i += chlen - 1;
2104  } else {
2105  if ((str[i] & 0xc0) != 0x80) {
2106  len++;
2107  }
2108  li = i;
2109  }
2110  }
2111  return str + li;
2112 }
int n
Definition: mipsasm.c:19
static size_t __str_ansi_length(char const *str)
Definition: str.c:1902

References __str_ansi_length(), i, len, n, and cmd_descs_generate::str.

Referenced by ds_print_show_bytes(), rz_cons_strcat_at(), and rz_cons_visual_write().

◆ rz_str_ansi_crop()

RZ_API char* rz_str_ansi_crop ( const char *  str,
ut32  x,
ut32  y,
ut32  x2,
ut32  y2 
)

Definition at line 2174 of file str.c.

2174  {
2175  char *r, *rz_end, *ret;
2176  const char *s, *s_start;
2177  size_t rz_len, str_len = 0, nr_of_lines = 0;
2178  ut32 ch = 0, cw = 0;
2179  if (x2 <= x || y2 <= y || !str) {
2180  return strdup("");
2181  }
2182  s = s_start = str;
2183  while (*s) {
2184  str_len++;
2185  if (*s == '\n') {
2186  nr_of_lines++;
2187  }
2188  s++;
2189  }
2190  rz_len = str_len + nr_of_lines * strlen(Color_RESET) + 1;
2191  r = ret = malloc(rz_len);
2192  if (!r) {
2193  return NULL;
2194  }
2195  rz_end = r + rz_len;
2196  while (*str) {
2197  /* crop height */
2198  if (ch >= y2) {
2199  r--;
2200  break;
2201  }
2202  if (*str == '\n') {
2203  if (ch >= y && ch < y2) {
2204  const char *reset = Color_RESET "\n";
2205  if (strlen(reset) < (rz_end - r)) {
2206  const int reset_length = strlen(reset);
2207  memcpy(r, reset, reset_length + 1);
2208  r += reset_length;
2209  }
2210  }
2211  str++;
2212  ch++;
2213  cw = 0;
2214  } else {
2215  if (ch >= y && ch < y2) {
2216  if ((*str & 0xc0) == 0x80) {
2217  if (cw > x) {
2218  *r++ = *str++;
2219  } else {
2220  str++;
2221  }
2222  continue;
2223  }
2224  if (rz_str_char_fullwidth(str, str_len - (str - s_start))) {
2225  cw++;
2226  if (cw == x) {
2227  *r++ = ' ';
2228  str++;
2229  continue;
2230  }
2231  }
2232  if (*str == 0x1b && *(str + 1) == '[') {
2233  const char *ptr = str;
2234  if ((rz_end - r) > 2) {
2235  /* copy 0x1b and [ */
2236  *r++ = *str++;
2237  *r++ = *str++;
2238  for (ptr = str; *ptr && *ptr != 'J' && *ptr != 'm' && *ptr != 'H'; ptr++) {
2239  *r++ = *ptr;
2240  }
2241  *r++ = *ptr++;
2242  }
2243  str = ptr;
2244  continue;
2245  } else if (cw >= x && cw < x2) {
2246  *r++ = *str;
2247  }
2248  }
2249  /* skip until newline */
2250  if (cw >= x2) {
2251  while (*str && *str != '\n') {
2252  str++;
2253  }
2254  } else {
2255  str++;
2256  }
2257  cw++;
2258  }
2259  }
2260  *r = 0;
2261  return ret;
2262 }
#define r
Definition: crypto_rc6.c:12
uint32_t ut32
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
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 x
Definition: mipsasm.c:20
static RzSocket * s
Definition: rtr.c:28
#define Color_RESET
Definition: rz_cons.h:617
RZ_API bool rz_str_char_fullwidth(const char *s, size_t left)
Definition: str.c:2277

References Color_RESET, malloc(), memcpy(), NULL, r, rz_str_char_fullwidth(), s, cmd_descs_generate::str, strdup(), x, and x2.

Referenced by __menu_panel_print(), __update_help_contents(), __update_panel_contents(), get_word_from_canvas(), get_word_from_canvas_for_menu(), mini_RzANode_print(), normal_RzANode_print(), readline_callback(), rz_cons_strcat_at(), rz_core_visual_analysis_refresh(), rz_core_visual_analysis_refresh_column(), rz_core_visual_colors(), rz_core_visual_xrefs(), var_functions_show(), and visual_refresh().

◆ rz_str_ansi_filter()

RZ_API int rz_str_ansi_filter ( char *  str,
char **  out,
int **  cposs,
int  len 
)

Definition at line 2124 of file str.c.

2124  {
2125  int i, j, *cps;
2126 
2127  if (len == 0) {
2128  return 0;
2129  }
2130  if (len < 0) {
2131  len = strlen(str);
2132  }
2133  char *tmp = malloc(len + 1);
2134  if (!tmp) {
2135  return -1;
2136  }
2137  memcpy(tmp, str, len + 1);
2138  cps = calloc(len + 1, sizeof(int));
2139  if (!cps) {
2140  free(tmp);
2141  return -1;
2142  }
2143 
2144  for (i = j = 0; i < len; i++) {
2145  if (tmp[i] == 0x1b) {
2146  size_t chlen = __str_ansi_length(str + i);
2147  if (chlen > 1) {
2148  i += chlen;
2149  i--;
2150  }
2151  } else {
2152  str[j] = tmp[i];
2153  cps[j] = i;
2154  j++;
2155  }
2156  }
2157  str[j] = tmp[i];
2158 
2159  if (out) {
2160  *out = tmp;
2161  } else {
2162  free(tmp);
2163  }
2164 
2165  if (cposs) {
2166  *cposs = cps;
2167  } else {
2168  free(cps);
2169  }
2170 
2171  return j;
2172 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * calloc(size_t number, size_t size)
Definition: malloc.c:102

References __str_ansi_length(), calloc(), free(), i, len, malloc(), memcpy(), out, cmd_descs_generate::str, and autogen_x86imm::tmp.

Referenced by __matchString(), __settings_colors_cb(), ds_show_flags(), get_word_from_canvas(), get_word_from_canvas_for_menu(), pager_all_matches(), pager_color_line(), rz_cons_grepbuf(), rz_cons_highlight(), rz_str_replace_thunked(), and rz_str_trim_lines().

◆ rz_str_ansi_len()

RZ_API size_t rz_str_ansi_len ( const char *  str)

◆ rz_str_ansi_nlen()

RZ_API size_t rz_str_ansi_nlen ( const char *  str,
size_t  slen 
)

Definition at line 1923 of file str.c.

1923  {
1924  size_t i = 0, len = 0;
1925  if (slen > 0) {
1926  while (str[i] && i < slen) {
1927  size_t chlen = __str_ansi_length(str + i);
1928  if (chlen == 1) {
1929  len++;
1930  }
1931  i += chlen;
1932  }
1933  return len > 0 ? len : 1;
1934  }
1935  while (str[i]) {
1936  size_t chlen = __str_ansi_length(str + i);
1937  if (chlen == 1) {
1938  len++;
1939  }
1940  i += chlen;
1941  }
1942  return len > 0 ? len : 1;
1943 }

References __str_ansi_length(), i, len, and cmd_descs_generate::str.

Referenced by is_indirect_reg(), is_reg(), rz_str_ansi_len(), and rz_str_bounds().

◆ rz_str_append()

RZ_API char* rz_str_append ( char *  ptr,
const char *  string 
)

Definition at line 1063 of file str.c.

1063  {
1064  if (string && !ptr) {
1065  return strdup(string);
1066  }
1067  if (RZ_STR_ISEMPTY(string)) {
1068  return ptr;
1069  }
1070  int plen = strlen(ptr);
1071  int slen = strlen(string);
1072  char *newptr = realloc(ptr, slen + plen + 1);
1073  if (!newptr) {
1074  free(ptr);
1075  return NULL;
1076  }
1077  ptr = newptr;
1078  memcpy(ptr + plen, string, slen + 1);
1079  return ptr;
1080 }
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67

References free(), memcpy(), NULL, realloc(), RZ_STR_ISEMPTY, and strdup().

Referenced by __print_hexdump_cb(), __print_stack_cb(), __system(), dalvik_disassemble(), ds_print_calls_hints(), ds_print_lines_left(), filterFlags(), gdbr_exec_file_read(), get_ver_flags(), preprocess_filter_expr(), rop_classify_arithmetic(), rop_classify_arithmetic_const(), rtr_dir_files(), runcmd(), rz_analysis_reflines_str(), rz_analysis_value_to_string(), rz_cf_value_dict_parse(), rz_cons_hud_path(), rz_core_agraph_add_node(), rz_core_cmd_subst_i(), rz_core_rtr_cmds_query(), rz_core_rtr_http_run(), rz_core_visual_title(), rz_core_visual_xrefs(), rz_file_root(), rz_lib_path(), rz_main_rizin(), rz_main_rz_gg(), rz_reg_set_profile(), rz_str_append_owned(), rz_str_appendch(), rz_str_appendf(), rz_str_appendlen(), rz_syscmd_join(), rz_syscmd_ls(), rz_type_db_enum_get_bitfield(), showfile(), and walk_exports().

◆ rz_str_append_owned()

RZ_API char* rz_str_append_owned ( char *  ptr,
char *  string 
)

Definition at line 1050 of file str.c.

1050  {
1051  if (!ptr) {
1052  return string;
1053  }
1054  char *r = rz_str_append(ptr, string);
1055  free(string);
1056  return r;
1057 }
RZ_API char * rz_str_append(char *ptr, const char *string)
Definition: str.c:1063

References free(), r, and rz_str_append().

Referenced by add_cpu_info(), and myregwrite().

◆ rz_str_appendch()

RZ_API char* rz_str_appendch ( char *  x,
char  y 
)

Definition at line 1105 of file str.c.

1105  {
1106  char b[2] = { y, 0 };
1107  return rz_str_append(x, b);
1108 }
#define b(i)
Definition: sha256.c:42

References b, rz_str_append(), and x.

Referenced by capture_filter_keywords(), find_e_opts(), and rz_core_visual_analysis().

◆ rz_str_appendf()

RZ_API char* rz_str_appendf ( char *  ptr,
const char *  fmt,
  ... 
)

Definition at line 1082 of file str.c.

1082  {
1084  va_list ap, ap2;
1085 
1086  va_start(ap, fmt);
1087  if (!strchr(fmt, '%')) {
1088  va_end(ap);
1089  return rz_str_append(ptr, fmt);
1090  }
1091  va_copy(ap2, ap);
1092  int ret = vsnprintf(NULL, 0, fmt, ap2);
1093  ret++;
1094  char *p = calloc(1, ret);
1095  if (p) {
1096  (void)vsnprintf(p, ret, fmt, ap);
1097  ptr = rz_str_append(ptr, p);
1098  free(p);
1099  }
1100  va_end(ap2);
1101  va_end(ap);
1102  return ptr;
1103 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References calloc(), free(), NULL, p, rz_return_val_if_fail, rz_str_append(), and vsnprintf.

◆ rz_str_appendlen()

RZ_API char* rz_str_appendlen ( char *  ptr,
const char *  string,
int  slen 
)

Definition at line 1043 of file str.c.

1043  {
1044  char *msg = rz_str_newlen(string, slen);
1045  char *ret = rz_str_append(ptr, msg);
1046  free(msg);
1047  return ret;
1048 }
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
RZ_API char * rz_str_newlen(const char *str, int len)
Definition: str.c:871

References free(), msg, rz_str_append(), and rz_str_newlen().

Referenced by filterFlags(), and preprocess_filter_expr().

◆ rz_str_arg_escape()

RZ_API char* rz_str_arg_escape ( const char *  arg)

Definition at line 2413 of file str.c.

2413  {
2414  char *str;
2415  int dest_i = 0, src_i = 0;
2416  if (!arg) {
2417  return NULL;
2418  }
2419  str = malloc((2 * strlen(arg) + 1) * sizeof(char)); // Worse case when every character need to be escaped
2420  if (!str) {
2421  return NULL;
2422  }
2423  for (src_i = 0; arg[src_i] != '\0'; src_i++) {
2424  char c = arg[src_i];
2425  switch (c) {
2426  case '\'':
2427  case '"':
2428  case '\\':
2429  case ' ':
2430  str[dest_i++] = '\\';
2431  str[dest_i++] = c;
2432  break;
2433  default:
2434  str[dest_i++] = c;
2435  break;
2436  }
2437  }
2438  str[dest_i] = '\0';
2439  return realloc(str, (strlen(str) + 1) * sizeof(char));
2440 }
#define c(i)
Definition: sha256.c:43

References c, malloc(), NULL, realloc(), and cmd_descs_generate::str.

Referenced by rz_core_file_reopen_debug(), and rz_main_rizin().

◆ rz_str_arg_unescape()

RZ_API int rz_str_arg_unescape ( char *  arg)

Definition at line 2443 of file str.c.

2443  {
2444  int dest_i = 0, src_i = 0;
2445  if (!arg) {
2446  return 0;
2447  }
2448  for (src_i = 0; arg[src_i] != '\0'; src_i++) {
2449  char c = arg[src_i];
2450  if (c == '\\') {
2451  if (arg[++src_i] == '\0') {
2452  break;
2453  }
2454  arg[dest_i++] = arg[src_i];
2455  } else {
2456  arg[dest_i++] = c;
2457  }
2458  }
2459  arg[dest_i] = '\0';
2460  return dest_i;
2461 }

References c.

◆ rz_str_argv()

RZ_API char** rz_str_argv ( const char *  cmdline,
int _argc 
)

Definition at line 2509 of file str.c.

2509  {
2510  int argc = 0;
2511  int argv_len = 128; // Begin with that, argv will reallocated if necessary
2512  char *args; // Working buffer for writing unescaped args
2513  int cmdline_current = 0; // Current character index in _cmdline
2514  int args_current = 0; // Current character index in args
2515  int arg_begin = 0; // Index of the first character of the current argument in args
2516 
2517  if (!cmdline) {
2518  return NULL;
2519  }
2520 
2521  char **argv = malloc(argv_len * sizeof(char *));
2522  if (!argv) {
2523  return NULL;
2524  }
2525  args = malloc(128 + strlen(cmdline) * sizeof(char)); // Unescaped args will be shorter, so strlen (cmdline) will be enough
2526  if (!args) {
2527  free(argv);
2528  return NULL;
2529  }
2530  do {
2531  // States for parsing args
2532  int escaped = 0;
2533  int singlequoted = 0;
2534  int doublequoted = 0;
2535 
2536  // Seek the beginning of next argument (skip whitespaces)
2537  while (cmdline[cmdline_current] != '\0' && IS_WHITECHAR(cmdline[cmdline_current])) {
2538  cmdline_current++;
2539  }
2540 
2541  if (cmdline[cmdline_current] == '\0') {
2542  break; // No more arguments
2543  }
2544  // Read the argument
2545  while (1) {
2546  char c = cmdline[cmdline_current];
2547  int end_of_current_arg = 0;
2548  if (escaped) {
2549  switch (c) {
2550  case '\'':
2551  case '"':
2552  case ' ':
2553  case '\\':
2554  args[args_current++] = '\\';
2555  args[args_current++] = c;
2556  break;
2557  case '\0':
2558  args[args_current++] = '\\';
2559  end_of_current_arg = 1;
2560  break;
2561  default:
2562  args[args_current++] = '\\';
2563  args[args_current++] = c;
2564  }
2565  escaped = 0;
2566  } else {
2567  switch (c) {
2568  case '\'':
2569  if (doublequoted) {
2570  args[args_current++] = c;
2571  } else {
2572  singlequoted = !singlequoted;
2573  }
2574  break;
2575  case '"':
2576  if (singlequoted) {
2577  args[args_current++] = c;
2578  } else {
2579  doublequoted = !doublequoted;
2580  }
2581  break;
2582  case '\\':
2583  escaped = 1;
2584  break;
2585  case ' ':
2586  if (singlequoted || doublequoted) {
2587  args[args_current++] = c;
2588  } else {
2589  end_of_current_arg = 1;
2590  }
2591  break;
2592  case '\0':
2593  end_of_current_arg = 1;
2594  break;
2595  default:
2596  args[args_current++] = c;
2597  }
2598  }
2599  if (end_of_current_arg) {
2600  break;
2601  }
2602  cmdline_current++;
2603  }
2604  args[args_current++] = '\0';
2605  argv[argc++] = strdup(&args[arg_begin]);
2606  if (argc >= argv_len) {
2607  argv_len *= 2;
2608  char **tmp = realloc(argv, argv_len * sizeof(char *));
2609  if (!tmp) {
2610  free(args);
2611  free(argv);
2612  return NULL;
2613  }
2614  argv = tmp;
2615  }
2616  arg_begin = args_current;
2617  } while (cmdline[cmdline_current++] != '\0');
2618  argv[argc] = NULL;
2619  char **tmp = realloc(argv, (argc + 1) * sizeof(char *));
2620  if (tmp) {
2621  argv = tmp;
2622  } else {
2623  free(argv);
2624  argv = NULL;
2625  }
2626  if (_argc) {
2627  *_argc = argc;
2628  }
2629  free(args);
2630  return argv;
2631 }
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
int args
Definition: mipsasm.c:18
#define IS_WHITECHAR(x)
Definition: rz_str_util.h:5

References args, argv, c, free(), IS_WHITECHAR, malloc(), NULL, realloc(), strdup(), and autogen_x86imm::tmp.

Referenced by cmd_agraph_edge(), cmd_agraph_node(), ds_build_op_str(), rz_cmd_search(), rz_sys_cmd_str_full(), and windbg_open().

◆ rz_str_argv_free()

RZ_API void rz_str_argv_free ( char **  argv)

Definition at line 2633 of file str.c.

2633  {
2634  int argc = 0;
2635  if (!argv) {
2636  return;
2637  }
2638  while (argv[argc]) {
2639  free(argv[argc++]);
2640  }
2641  free(argv);
2642 }

References argv, and free().

Referenced by cmd_agraph_edge(), cmd_agraph_node(), rz_cmd_search(), rz_sys_cmd_str_full(), and windbg_open().

◆ rz_str_array_join()

RZ_API char* rz_str_array_join ( const char **  a,
size_t  n,
const char *  sep 
)

Definition at line 3861 of file str.c.

3861  {
3862  RzStrBuf *sb = rz_strbuf_new("");
3863  size_t i;
3864 
3865  if (n > 0) {
3866  rz_strbuf_append(sb, a[0]);
3867  }
3868 
3869  for (i = 1; i < n; i++) {
3870  rz_strbuf_append(sb, sep);
3871  rz_strbuf_append(sb, a[i]);
3872  }
3873  return rz_strbuf_drain(sb);
3874 }
static SblHeader sb
Definition: bin_mbn.c:26
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
#define a(i)
Definition: sha256.c:41

References a, i, n, rz_strbuf_append(), rz_strbuf_drain(), rz_strbuf_new(), and sb.

Referenced by args_preprocessing(), rz_cmd_shell_echo_handler(), rz_cmd_shell_ls_handler(), rz_hash_bang_handler(), rz_push_escaped_handler(), rz_remote_add_handler(), rz_remote_del_handler(), rz_remote_handler(), rz_remote_open_handler(), rz_remote_rap_bg_handler(), rz_remote_rap_handler(), rz_remote_send_handler(), rz_reopen_debug_handler(), rz_write_assembly_handler(), and rz_write_assembly_inside_handler().

◆ rz_str_between()

RZ_API char* rz_str_between ( const char *  cmt,
const char *  prefix,
const char *  suffix 
)

Definition at line 3264 of file str.c.

3264  {
3265  char *c0, *c1;
3266  if (!cmt || !prefix || !suffix || !*cmt) {
3267  return NULL;
3268  }
3269  c0 = strstr(cmt, prefix);
3270  if (c0) {
3271  c1 = strstr(c0 + strlen(prefix), suffix);
3272  if (c1) {
3273  return rz_str_ndup(c0 + strlen(prefix), (c1 - c0 - strlen(prefix)));
3274  }
3275  }
3276  return NULL;
3277 }
lsl lsr asr ror lsl lsr asr ror lsl lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror c1
lsl lsr asr ror lsl lsr asr ror lsl lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror lsl lsr asr ror c0
unsigned char suffix[65536]
Definition: gun.c:164
unsigned short prefix[65536]
Definition: gun.c:163
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006

References c0, c1, NULL, prefix, rz_str_ndup(), and suffix.

Referenced by __rotate_panel_cmds(), rz_comment_filelink_handler(), and visual_comma().

◆ rz_str_binstr2bin()

RZ_API int rz_str_binstr2bin ( const char *  str,
ut8 out,
int  outlen 
)

Definition at line 284 of file str.c.

284  {
285  int n, i, j, k, ret, len;
286  len = strlen(str);
287  for (n = i = 0; i < len; i += 8) {
288  ret = 0;
289  while (str[i] == ' ') {
290  str++;
291  }
292  if (i + 7 < len) {
293  for (k = 0, j = i + 7; j >= i; j--, k++) {
294  // INVERSE for (k=0,j=i; j<i+8; j++,k++) {
295  if (str[j] == ' ') {
296  // k--;
297  continue;
298  }
299  // printf ("---> j=%d (%c) (%02x)\n", j, str[j], str[j]);
300  if (str[j] == '1') {
301  ret |= 1 << k;
302  } else if (str[j] != '0') {
303  return n;
304  }
305  }
306  }
307  // printf ("-======> %02x\n", ret);
308  out[n++] = ret;
309  if (n == outlen) {
310  return n;
311  }
312  }
313  return n;
314 }
const char * k
Definition: dsignal.c:11

References i, k, len, n, out, and cmd_descs_generate::str.

Referenced by rax().

◆ rz_str_bits()

RZ_API int rz_str_bits ( char *  strout,
const ut8 buf,
int  len,
const char *  bitz 
)

Definition at line 195 of file str.c.

195  {
196  int i, j, idx;
197  if (bitz) {
198  for (i = j = 0; i < len && (!bitz || bitz[i]); i++) {
199  if (i > 0 && (i % 8) == 0) {
200  buf++;
201  }
202  if (*buf & (1 << (i % 8))) {
203  strout[j++] = toupper((const ut8)bitz[i]);
204  }
205  }
206  } else {
207  for (i = j = 0; i < len; i++) {
208  idx = (i / 8);
209  int bit = 7 - (i % 8);
210  strout[j++] = (buf[idx] & (1 << bit)) ? '1' : '0';
211  }
212  }
213  strout[j] = 0;
214  return j;
215 }
RzCryptoSelector bit
Definition: crypto.c:16
int idx
Definition: setup.py:197
#define toupper(c)
Definition: safe-ctype.h:147

References bit, i, setup::idx, len, and toupper.

Referenced by ds_print_data_type(), rax(), rz_cmd_help(), rz_print_bitstream_handler(), rz_print_byte_bitstream_handler(), rz_print_hexdump_bits_handler(), and rz_reg_get_bvalue().

◆ rz_str_bits64()

RZ_API int rz_str_bits64 ( char *  strout,
ut64  in 
)

Definition at line 244 of file str.c.

244  {
245  int i, bit, count = 0;
246  count = 0;
247  for (i = (sizeof(in) * 8) - 1; i >= 0; i--) {
248  bit = in >> i;
249  if (bit & 1) {
250  strout[count] = '1';
251  } else {
252  strout[count] = '0';
253  }
254  count++;
255  }
256  strout[count] = '\0';
257  /* trim by 8 bits */
258  trimbits(strout);
259  return count;
260 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
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 count
Definition: sflib.h:98
static void trimbits(char *b)
Definition: str.c:233

References bit, count, i, in, and trimbits().

Referenced by rax(), and rz_cmd_help().

◆ rz_str_bits_from_string()

RZ_API ut64 rz_str_bits_from_string ( const char *  buf,
const char *  bitz 
)

function: rz_str_bits_from_num

Definition at line 266 of file str.c.

266  {
267  ut64 out = 0LL;
268  /* return the numeric value associated to a string (rflags) */
269  for (; *buf; buf++) {
270  char *ch = strchr(bitz, toupper((const unsigned char)*buf));
271  if (!ch) {
272  ch = strchr(bitz, tolower((const unsigned char)*buf));
273  }
274  if (ch) {
275  int bit = (int)(size_t)(ch - bitz);
276  out |= (ut64)(1LL << bit);
277  } else {
278  return UT64_MAX;
279  }
280  }
281  return out;
282 }
#define UT64_MAX
Definition: rz_types_base.h:86
#define tolower(c)
Definition: safe-ctype.h:149
static int
Definition: sfsocketcall.h:114
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References bit, int, out, tolower, toupper, ut64(), and UT64_MAX.

Referenced by rz_reg_set_bvalue().

◆ rz_str_bool()

◆ rz_str_bounds()

RZ_API int rz_str_bounds ( const char *  _str,
int h 
)

Definition at line 3124 of file str.c.

3124  {
3125  const char *str, *ptr;
3126  int W = 0, H = 0;
3127  int cw = 0;
3128 
3129  if (_str) {
3130  ptr = str = _str;
3131  while (*str) {
3132  if (*str == '\n') {
3133  H++;
3134  cw = rz_str_ansi_nlen(ptr, (size_t)(str - ptr));
3135  if (cw > W) {
3136  W = cw;
3137  }
3138  cw = 0;
3139  ptr = str + 1;
3140  }
3141  str++;
3142  cw++;
3143  }
3144  if (*str == '\n') { // skip last newline
3145  H--;
3146  }
3147  if (h) {
3148  *h = H;
3149  }
3150  }
3151  return W;
3152 }
#define W(x, y, z)
#define H(x)
#define h(i)
Definition: sha256.c:48

References h, H, rz_str_ansi_nlen(), cmd_descs_generate::str, and W.

Referenced by __open_menu_cb(), __update_menu_contents(), and update_node_dimension().

◆ rz_str_byte_escape()

RZ_API void rz_str_byte_escape ( const char *  p,
char **  dst,
RzStrEscOptions opt 
)

Converts unprintable characters to C-like backslash representation.

Parameters
ppointer to the original string
dstpointer where pointer to the resulting characters sequence is put
optpointer to encoding options structure

Definition at line 1436 of file str.c.

1436  {
1437  char *q = *dst;
1438  switch (*p) {
1439  case '\n':
1440  *q++ = '\\';
1441  *q++ = opt->dot_nl ? 'l' : 'n';
1442  break;
1443  case '\r':
1444  *q++ = '\\';
1445  *q++ = 'r';
1446  break;
1447  case '\\':
1448  if (opt->esc_bslash) {
1449  *q++ = '\\';
1450  }
1451  *q++ = '\\';
1452  break;
1453  case '\t':
1454  *q++ = '\\';
1455  *q++ = 't';
1456  break;
1457  case '"':
1458  if (opt->esc_double_quotes) {
1459  *q++ = '\\';
1460  }
1461  *q++ = '"';
1462  break;
1463  case '\f':
1464  *q++ = '\\';
1465  *q++ = 'f';
1466  break;
1467  case '\b':
1468  *q++ = '\\';
1469  *q++ = 'b';
1470  break;
1471  case '\v':
1472  *q++ = '\\';
1473  *q++ = 'v';
1474  break;
1475  case '\a':
1476  *q++ = '\\';
1477  *q++ = 'a';
1478  break;
1479  case '\x1b':
1480  *q++ = '\\';
1481  *q++ = 'e';
1482  break;
1483  default:
1484  /* Outside the ASCII printable range */
1485  if (!IS_PRINTABLE(*p)) {
1486  if (opt->show_asciidot) {
1487  *q++ = '.';
1488  } else {
1489  *q++ = '\\';
1490  *q++ = 'x';
1491  *q++ = "0123456789abcdef"[*p >> 4 & 0xf];
1492  *q++ = "0123456789abcdef"[*p & 0xf];
1493  }
1494  } else {
1495  *q++ = *p;
1496  }
1497  }
1498  *dst = q;
1499 }
char * dst
Definition: lz4.h:724
bool esc_bslash
When true, backslashes \ are quoted with \\
Definition: rz_str.h:41
bool show_asciidot
When true, dots . are placed instead of unprintable characters.
Definition: rz_str.h:40
bool esc_double_quotes
When true, double quotes "</tt> are quoted with <tt>\\"
Definition: rz_str.h:42
bool dot_nl
When true, is converted into the graphiz-compatible newline \l.
Definition: rz_str.h:43

References RzStrEscOptions::dot_nl, dst, RzStrEscOptions::esc_bslash, RzStrEscOptions::esc_double_quotes, IS_PRINTABLE, p, and RzStrEscOptions::show_asciidot.

Referenced by rz_str_escape_(), rz_str_escape_utf(), and rz_type_byte_escape().

◆ rz_str_case()

RZ_API void rz_str_case ( char *  str,
bool  up 
)

Definition at line 341 of file str.c.

341  {
342  if (up) {
343  char oc = 0;
344  for (; *str; oc = *str++) {
345  *str = (*str == 'x' && oc == '0') ? 'x' : toupper((int)(ut8)*str);
346  }
347  } else {
348  for (; *str; str++) {
349  *str = tolower((int)(ut8)*str);
350  }
351  }
352 }
#define ut8
Definition: dcpu16.h:8

References cmd_descs_generate::str, tolower, toupper, and ut8.

Referenced by bin_pe_parse_imports(), cb_binstrenc(), ds_disassemble(), ds_print_comments_right(), ds_show_comments_describe(), iflag(), itmask(), load_buffer(), mount_oldstr(), opmask(), rz_asm_assemble(), rz_bin_pe_get_libs(), rz_cons_grep_line(), rz_core_disasm_pdi_with_buf(), set_bin_relocs(), subs_var_string(), subvar(), thumb_assemble(), and thumb_getshift().

◆ rz_str_casecmp()

◆ rz_str_casestr()

RZ_API const char* rz_str_casestr ( const char *  a,
const char *  b 
)

Definition at line 2757 of file str.c.

2757  {
2758  // That's a GNUism that works in many places.. but we don't want it
2759  // return strcasestr (a, b);
2760  size_t hay_len = strlen(a);
2761  size_t needle_len = strlen(b);
2762  if (!hay_len || !needle_len) {
2763  return NULL;
2764  }
2765  while (hay_len >= needle_len) {
2766  if (!rz_str_ncasecmp(a, b, needle_len)) {
2767  return (const char *)a;
2768  }
2769  a++;
2770  hay_len--;
2771  }
2772  return NULL;
2773 }
RZ_API int rz_str_ncasecmp(const char *s1, const char *s2, size_t n)
Definition: str.c:129

References a, b, NULL, and rz_str_ncasecmp().

Referenced by __matchString(), filter(), rz_str_replace_icase(), show_class(), showfile(), socket_http_answer(), and subvar().

◆ rz_str_ccmp()

RZ_API bool rz_str_ccmp ( const char *  dst,
const char *  src,
int  ch 
)

Definition at line 941 of file str.c.

941  {
942  rz_return_val_if_fail(dst && src, false);
943  int i;
944  for (i = 0; src[i] && src[i] != ch; i++) {
945  if (dst[i] != src[i]) {
946  return true;
947  }
948  }
949  return false;
950 }
lzma_index * src
Definition: index.h:567

References dst, i, rz_return_val_if_fail, and src.

Referenced by rz_core_visual_config().

◆ rz_str_ccpy()

RZ_API int rz_str_ccpy ( char *  dst,
char *  src,
int  ch 
)

Definition at line 991 of file str.c.

991  {
992  int i;
993  for (i = 0; src[i] && src[i] != ch; i++) {
994  dst[i] = src[i];
995  }
996  dst[i] = '\0';
997  return i;
998 }

References dst, i, and src.

Referenced by rz_core_visual_config().

◆ rz_str_char_count()

RZ_API int rz_str_char_count ( const char *  string,
char  ch 
)

Definition at line 611 of file str.c.

611  {
612  int i, count = 0;
613  for (i = 0; string[i]; i++) {
614  if (string[i] == ch) {
615  count++;
616  }
617  }
618  return count;
619 }

References count, and i.

Referenced by replace_cmd_kv(), and rz_cons_flush().

◆ rz_str_char_fullwidth()

RZ_API bool rz_str_char_fullwidth ( const char *  s,
size_t  left 
)

Definition at line 2277 of file str.c.

2277  {
2278  size_t codepoint = rz_str_utf8_codepoint(s, left);
2279  return (codepoint >= 0x1100 &&
2280  (codepoint <= 0x115f || /* Hangul Jamo init. consonants */
2281  codepoint == 0x2329 || codepoint == 0x232a ||
2282  (RZ_BETWEEN(0x2e80, codepoint, 0xa4cf) && codepoint != 0x303f) || /* CJK ... Yi */
2283  RZ_BETWEEN(0xac00, codepoint, 0xd7a3) || /* Hangul Syllables */
2284  RZ_BETWEEN(0xf900, codepoint, 0xfaff) || /* CJK Compatibility Ideographs */
2285  RZ_BETWEEN(0xfe10, codepoint, 0xfe19) || /* Vertical forms */
2286  RZ_BETWEEN(0xfe30, codepoint, 0xfe6f) || /* CJK Compatibility Forms */
2287  RZ_BETWEEN(0xff00, codepoint, 0xff60) || /* Fullwidth Forms */
2288  RZ_BETWEEN(0xffe0, codepoint, 0xffe6) ||
2289  RZ_BETWEEN(0x20000, codepoint, 0x2fffd) ||
2290  RZ_BETWEEN(0x30000, codepoint, 0x3fffd)));
2291 }
#define RZ_BETWEEN(x, y, z)
RZ_API size_t rz_str_utf8_codepoint(const char *s, size_t left)
Definition: str.c:2264

References RZ_BETWEEN, rz_str_utf8_codepoint(), and s.

Referenced by __getUtf8Length(), __getUtf8Length2(), rz_cons_canvas_to_string(), rz_str_ansi_crop(), rz_str_len_utf8(), and rz_str_len_utf8_ansi().

◆ rz_str_closer_chr()

RZ_API const char* rz_str_closer_chr ( const char *  b,
const char *  s 
)

Definition at line 3111 of file str.c.

3111  {
3112  const char *a;
3113  while (*b) {
3114  for (a = s; *a; a++) {
3115  if (*b == *a) {
3116  return b;
3117  }
3118  }
3119  b++;
3120  }
3121  return NULL;
3122 }

References a, b, NULL, and s.

Referenced by rz_asm_massemble().

◆ rz_str_cmp()

RZ_API int rz_str_cmp ( const char *  a,
const char *  b,
int  len 
)

Definition at line 974 of file str.c.

974  {
975  if ((a == b) || (!a && !b)) {
976  return 0;
977  }
978  if (!a && b) {
979  return -1;
980  }
981  if (a && !b) {
982  return 1;
983  }
984  if (len < 0) {
985  return strcmp(a, b);
986  }
987  return strncmp(a, b, len);
988 }

References a, b, and len.

Referenced by __computeTotal(), detect_constructor_destructor(), get_backtrace_info(), run_basic_block_analysis(), rz_analysis_cc_get(), rz_bin_list_plugin(), and rz_core_theme_load().

◆ rz_str_cmp_list()

RZ_API bool rz_str_cmp_list ( const char *  list,
const char *  item,
char  sep 
)

Definition at line 953 of file str.c.

953  {
954  if (!list || !item) {
955  return false;
956  }
957  int i = 0, j = 0;
958  for (; list[i] && list[i] != sep; i++, j++) {
959  if (item[j] != list[i]) {
960  while (list[i] && list[i] != sep) {
961  i++;
962  }
963  if (!list[i]) {
964  return false;
965  }
966  j = -1;
967  continue;
968  }
969  }
970  return true;
971 }
static void list(RzEgg *egg)
Definition: rz-gg.c:52

References i, and list().

Referenced by cb_binstrenc().

◆ rz_str_contains_macro()

RZ_API ut8 rz_str_contains_macro ( const char *  input_value)

Definition at line 3076 of file str.c.

3076  {
3077  char *has_tilde = input_value ? HASCH('~') : NULL,
3078  *has_bang = input_value ? HASCH('!') : NULL,
3079  *has_brace = input_value ? CAST(HASCH('[') || HASCH(']')) : NULL,
3080  *has_paren = input_value ? CAST(HASCH('(') || HASCH(')')) : NULL,
3081  *has_cbrace = input_value ? CAST(HASCH('{') || HASCH('}')) : NULL,
3082  *has_qmark = input_value ? HASCH('?') : NULL,
3083  *has_colon = input_value ? HASCH(':') : NULL,
3084  *has_at = input_value ? strchr(input_value, '@') : NULL;
3085 
3086  return has_tilde || has_bang || has_brace || has_cbrace || has_qmark || has_paren || has_colon || has_at;
3087 }
#define CAST
Definition: str.c:3075
#define HASCH(x)
Definition: str.c:3074

References CAST, HASCH, and NULL.

◆ rz_str_crop()

RZ_API char* rz_str_crop ( const char *  str,
unsigned int  x,
unsigned int  y,
unsigned int  x2,
unsigned int  y2 
)

Definition at line 3157 of file str.c.

3158  {
3159  char *r, *ret;
3160  unsigned int ch = 0, cw = 0;
3161  if (x2 < 1 || y2 < 1 || !str) {
3162  return strdup("");
3163  }
3164  r = ret = strdup(str);
3165  while (*str) {
3166  /* crop height */
3167  if (ch >= y2) {
3168  r--;
3169  break;
3170  }
3171 
3172  if (*str == '\n') {
3173  if (ch >= y && ch < y2) {
3174  *r++ = *str;
3175  }
3176  str++;
3177  ch++;
3178  cw = 0;
3179  } else {
3180  if (ch >= y && ch < y2 && cw >= x && cw < x2) {
3181  *r++ = *str;
3182  }
3183  /* crop width */
3184  /* skip until newline */
3185  if (cw >= x2) {
3186  while (*str && *str != '\n') {
3187  str++;
3188  }
3189  } else {
3190  str++;
3191  }
3192  cw++;
3193  }
3194  }
3195  *r = 0;
3196  return ret;
3197 }

References r, cmd_descs_generate::str, strdup(), x, and x2.

◆ rz_str_delta()

RZ_API int rz_str_delta ( char *  p,
char  a,
char  b 
)

Definition at line 394 of file str.c.

394  {
395  char *_a = strchr(p, a);
396  char *_b = strchr(p, b);
397  return (!_a || !_b) ? 0 : (_a - _b);
398 }

References a, b, and p.

Referenced by rz_num_math().

◆ rz_str_djb2_hash()

RZ_API ut64 rz_str_djb2_hash ( const char *  s)

Definition at line 383 of file str.c.

383  {
384  ut64 len, h = 5381;
385  if (!s) {
386  return 0;
387  }
388  for (len = strlen(s); len > 0; len--) {
389  h = (h ^ (h << 5)) ^ *s++;
390  }
391  return h;
392 }

References h, len, s, and ut64().

Referenced by rax(), rz_cmd_help(), and rz_egg_setup().

◆ rz_str_do_until_token()

RZ_API int rz_str_do_until_token ( str_operation  op,
char *  str,
const char  tok 
)

Definition at line 3219 of file str.c.

3219  {
3220  int ret;
3221  if (!str) {
3222  return -1;
3223  }
3224  if (!op) {
3225  for (ret = 0; (str[ret] != tok) && str[ret]; ret++) {
3226  // empty body
3227  }
3228  } else {
3229  for (ret = 0; (str[ret] != tok) && str[ret]; ret++) {
3230  op(str + ret);
3231  }
3232  }
3233  return ret;
3234 }
ut8 op
Definition: 6502dis.c:13
Definition: dis.c:32

References op, and cmd_descs_generate::str.

Referenced by gb_parse_arith1(), gb_parse_cb1(), gb_parse_ld1(), and gbAsm().

◆ rz_str_dup()

RZ_API char* rz_str_dup ( char *  ptr,
const char *  string 
)

Definition at line 1021 of file str.c.

1021  {
1022  char *str = rz_str_new(string);
1023  free(ptr); // in case ptr == string
1024  return str;
1025 }
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865

References free(), rz_str_new(), and cmd_descs_generate::str.

Referenced by __check_func_diff(), __init_panel_param(), __replace_cmd(), __rotate_panel_cmds(), __update_help(), config_visual_hit(), disassemble(), entitlements(), info(), rz_cmd_print_gadget_add_handler(), rz_core_theme_load(), rz_core_visual_view_rop(), rz_load_panels_layout(), rz_reg_set_name(), rz_syscmd_mkdir(), and signature().

◆ rz_str_enc_as_string()

RZ_API const char* rz_str_enc_as_string ( RzStrEnc  enc)

Definition at line 44 of file str.c.

44  {
45  switch (enc) {
46  case RZ_STRING_ENC_8BIT:
47  return "ascii";
48  case RZ_STRING_ENC_UTF8:
49  return "utf8";
51  return "mutf8";
53  return "utf16le";
55  return "utf32le";
57  return "utf16be";
59  return "utf32be";
61  return "base64";
63  return "ibm037";
65  return "ibm290";
67  return "ebcdices";
69  return "ebcdicuk";
71  return "ebcdicus";
73  return "guessed";
74  default:
76  return "unknown";
77  }
78 }
#define rz_warn_if_reached()
Definition: rz_assert.h:29
@ RZ_STRING_ENC_UTF32LE
Definition: rz_str.h:24
@ RZ_STRING_ENC_8BIT
Definition: rz_str.h:20
@ RZ_STRING_ENC_BASE64
Definition: rz_str.h:27
@ RZ_STRING_ENC_EBCDIC_US
Definition: rz_str.h:31
@ RZ_STRING_ENC_MUTF8
Definition: rz_str.h:22
@ RZ_STRING_ENC_UTF32BE
Definition: rz_str.h:26
@ RZ_STRING_ENC_UTF8
Definition: rz_str.h:21
@ RZ_STRING_ENC_GUESS
Definition: rz_str.h:33
@ RZ_STRING_ENC_UTF16LE
Definition: rz_str.h:23
@ RZ_STRING_ENC_UTF16BE
Definition: rz_str.h:25
@ RZ_STRING_ENC_EBCDIC_UK
Definition: rz_str.h:30
@ RZ_STRING_ENC_IBM037
Definition: rz_str.h:28
@ RZ_STRING_ENC_IBM290
Definition: rz_str.h:29
@ RZ_STRING_ENC_EBCDIC_ES
Definition: rz_str.h:32

References RZ_STRING_ENC_8BIT, RZ_STRING_ENC_BASE64, RZ_STRING_ENC_EBCDIC_ES, RZ_STRING_ENC_EBCDIC_UK, RZ_STRING_ENC_EBCDIC_US, RZ_STRING_ENC_GUESS, RZ_STRING_ENC_IBM037, RZ_STRING_ENC_IBM290, RZ_STRING_ENC_MUTF8, RZ_STRING_ENC_UTF16BE, RZ_STRING_ENC_UTF16LE, RZ_STRING_ENC_UTF32BE, RZ_STRING_ENC_UTF32LE, RZ_STRING_ENC_UTF8, and rz_warn_if_reached.

Referenced by print_bin_string(), print_json_string(), print_string(), rz_core_meta_print(), rz_scan_strings(), and strings_print().

◆ rz_str_enc_string_as_type()

RZ_API RzStrEnc rz_str_enc_string_as_type ( RZ_NULLABLE const char *  encoding)

converts an encoding name to RzStrEnc

Parameters
[in]encodingEncoding name
Returns
Returns a RzStrEnc type.

Definition at line 86 of file str.c.

86  {
87  if (!encoding || !strncmp(encoding, "guess", 5)) {
88  return RZ_STRING_ENC_GUESS;
89  } else if (!strcmp(encoding, "ascii") || !strcmp(encoding, "8bit")) {
90  return RZ_STRING_ENC_8BIT;
91  } else if (!strcmp(encoding, "mutf8")) {
92  return RZ_STRING_ENC_MUTF8;
93  } else if (!strcmp(encoding, "utf8")) {
94  return RZ_STRING_ENC_UTF8;
95  } else if (!strcmp(encoding, "utf16le")) {
96  return RZ_STRING_ENC_UTF16LE;
97  } else if (!strcmp(encoding, "utf32le")) {
98  return RZ_STRING_ENC_UTF32LE;
99  } else if (!strcmp(encoding, "utf16be")) {
100  return RZ_STRING_ENC_UTF16BE;
101  } else if (!strcmp(encoding, "utf32be")) {
102  return RZ_STRING_ENC_UTF32BE;
103  } else if (!strcmp(encoding, "ibm037")) {
104  return RZ_STRING_ENC_IBM037;
105  } else if (!strcmp(encoding, "ibm290")) {
106  return RZ_STRING_ENC_IBM290;
107  } else if (!strcmp(encoding, "ebcdices")) {
109  } else if (!strcmp(encoding, "ebcdicuk")) {
111  } else if (!strcmp(encoding, "ebcdicus")) {
113  } else if (!strcmp(encoding, "base64")) {
114  return RZ_STRING_ENC_BASE64;
115  }
116 
117  RZ_LOG_ERROR("rz_str: encoding %s not supported\n", encoding);
118  return RZ_STRING_ENC_GUESS;
119 }
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58

References cmd_descs_generate::encoding, RZ_LOG_ERROR, RZ_STRING_ENC_8BIT, RZ_STRING_ENC_BASE64, RZ_STRING_ENC_EBCDIC_ES, RZ_STRING_ENC_EBCDIC_UK, RZ_STRING_ENC_EBCDIC_US, RZ_STRING_ENC_GUESS, RZ_STRING_ENC_IBM037, RZ_STRING_ENC_IBM290, RZ_STRING_ENC_MUTF8, RZ_STRING_ENC_UTF16BE, RZ_STRING_ENC_UTF16LE, RZ_STRING_ENC_UTF32BE, RZ_STRING_ENC_UTF32LE, and RZ_STRING_ENC_UTF8.

Referenced by create_string_search_thread(), and ds_init().

◆ rz_str_endswith()

RZ_API bool rz_str_endswith ( RZ_NONNULL const char *  str,
RZ_NONNULL const char *  needle 
)

Checks if a string ends with a specifc sequence of characters (case sensitive)

Parameters
strC-string to be scanned
needleC-string containing the sequence of characters to match
Returns
True if needle is found at the end of str and false otherwise
See also
rz_str_endswith_icase()

Definition at line 3329 of file str.c.

3329  {
3330  return str_endswith(str, needle, true);
3331 }
static bool str_endswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle, bool case_sensitive)
Definition: str.c:3309

References cmd_descs_generate::str, and str_endswith().

Referenced by __lib_dl_check_filename(), __toggle_help(), __update_help(), can_section_contain_rtti_vpointer(), cmd_print_format(), database_load(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), ds_show_functions(), ds_show_functions_argvar(), find_apk_binary(), getnumbang(), getnummemend(), getnummemendbang(), getreglist(), getregmembang(), getregmemend(), getregmemstartend(), getshiftmemend(), is_arg_identifier_in_tmp_stmt(), lang_lib_file_run(), map_multi_dex(), preprocess_filter_expr(), rax(), run_basic_block_analysis(), rz_core_rtr_add(), rz_core_run_script(), rz_debug_dmp_init(), rz_egg_load_file(), rz_eval_getset_handler(), rz_file_path(), rz_load_panels_layout(), rz_sign_sigdb_load_database(), rz_sys_perror_str(), rz_test_run_fuzz_test(), section_can_contain_rtti(), set_color_default(), sh_op_get_addr_mode(), sigdb_signature_resolve_details(), skip_archos(), v1_v2_types_foreach_cb(), vtable_section_can_contain_vtables(), and winkd_wait_packet().

◆ rz_str_endswith_icase()

RZ_API bool rz_str_endswith_icase ( RZ_NONNULL const char *  str,
RZ_NONNULL const char *  needle 
)

Checks if a string ends with a specifc sequence of characters (case insensitive)

Parameters
strC-string to be scanned
needleC-string containing the sequence of characters to match
Returns
True if needle is found at the end of str and false otherwise
See also
rz_str_endswith()

Definition at line 3340 of file str.c.

3340  {
3341  return str_endswith(str, needle, false);
3342 }

References cmd_descs_generate::str, and str_endswith().

Referenced by check_filename().

◆ rz_str_escape()

RZ_API RZ_OWN char* rz_str_escape ( RZ_NONNULL const char *  buf)

Definition at line 1550 of file str.c.

1550  {
1552  RzStrEscOptions opt = { 0 };
1553  opt.dot_nl = false;
1554  opt.show_asciidot = false;
1555  opt.esc_bslash = true;
1556  return rz_str_escape_(buf, true, true, &opt);
1557 }
static RZ_OWN char * rz_str_escape_(const char *buf, bool parse_esc_seq, bool ign_esc_seq, RzStrEscOptions *opt)
Definition: str.c:1503
Group together some common options used by string escaping functions.
Definition: rz_str.h:39

References RzStrEscOptions::dot_nl, RzStrEscOptions::esc_bslash, NULL, rz_return_val_if_fail, rz_str_escape_(), and RzStrEscOptions::show_asciidot.

Referenced by __update_prompt_color(), cmd_debug_current_modules(), cmd_debug_modules(), cmd_pxr(), config_print_node(), emit_set_string(), print_space_stack(), rz_bin_elf_get_compiler(), rz_cmd_debug_dmS_handler(), rz_core_meta_print(), and rz_main_rz_agent().

◆ rz_str_escape_()

static RZ_OWN char* rz_str_escape_ ( const char *  buf,
bool  parse_esc_seq,
bool  ign_esc_seq,
RzStrEscOptions opt 
)
static

Definition at line 1503 of file str.c.

1503  {
1505 
1506  /* Worst case scenario, we convert every byte to a single-char escape
1507  * (e.g. \n) if show_asciidot, or \xhh if !show_asciidot */
1508  char *new_buf = malloc(1 + strlen(buf) * (opt->show_asciidot ? 2 : 4));
1509  if (!new_buf) {
1510  return NULL;
1511  }
1512  const char *p = buf;
1513  char *q = new_buf;
1514  while (*p) {
1515  switch (*p) {
1516  case 0x1b: // ESC
1517  if (parse_esc_seq) {
1518  const char *start_seq = p;
1519  p++;
1520  /* Parse the ANSI code (only the graphic mode
1521  * set ones are supported) */
1522  if (*p == '\0') {
1523  goto out;
1524  }
1525  if (*p == '[') {
1526  for (p++; *p != 'm'; p++) {
1527  if (*p == '\0') {
1528  goto out;
1529  }
1530  }
1531  if (!ign_esc_seq) {
1532  memcpy(q, start_seq, p - start_seq + 1);
1533  q += (p - start_seq + 1);
1534  }
1535  }
1536  break;
1537  }
1538  /* fallthrough */
1539  default:
1540  rz_str_byte_escape(p, &q, opt);
1541  break;
1542  }
1543  p++;
1544  }
1545 out:
1546  *q = '\0';
1547  return new_buf;
1548 }
RZ_API void rz_str_byte_escape(const char *p, char **dst, RzStrEscOptions *opt)
Converts unprintable characters to C-like backslash representation.
Definition: str.c:1436

References malloc(), memcpy(), NULL, out, p, rz_return_val_if_fail, rz_str_byte_escape(), and RzStrEscOptions::show_asciidot.

Referenced by rz_str_escape(), rz_str_escape_8bit(), and rz_str_escape_dot().

◆ rz_str_escape_8bit()

RZ_API char* rz_str_escape_8bit ( const char *  buf,
bool  colors,
RzStrEscOptions opt 
)

Definition at line 1595 of file str.c.

1595  {
1596  return rz_str_escape_(buf, colors, !colors, opt);
1597 }
struct @219 colors[]

References colors, and rz_str_escape_().

Referenced by ds_esc_str(), ds_print_meta_infos(), meta_string_escape(), and rz_run_get_environ_profile().

◆ rz_str_escape_dot()

RZ_API char* rz_str_escape_dot ( const char *  buf)

Definition at line 1587 of file str.c.

1587  {
1588  RzStrEscOptions opt = { 0 };
1589  opt.dot_nl = true;
1590  opt.show_asciidot = false;
1591  opt.esc_bslash = true;
1592  return rz_str_escape_(buf, true, true, &opt);
1593 }

References RzStrEscOptions::dot_nl, RzStrEscOptions::esc_bslash, rz_str_escape_(), and RzStrEscOptions::show_asciidot.

Referenced by core_analysis_graph_label().

◆ rz_str_escape_mutf8_for_json()

RZ_API char* rz_str_escape_mutf8_for_json ( const char *  buf,
int  buf_size 
)

Definition at line 1838 of file str.c.

1838  {
1839  return escape_utf8_for_json(buf, buf_size, true);
1840 }
static char * escape_utf8_for_json(const char *buf, int buf_size, bool mutf8)
Definition: str.c:1723

References buf_size, and escape_utf8_for_json().

Referenced by java_constant_pool_stringify().

◆ rz_str_escape_sh()

RZ_API char* rz_str_escape_sh ( const char *  buf)

Definition at line 1560 of file str.c.

1560  {
1562  char *new_buf = malloc(1 + strlen(buf) * 2);
1563  if (!new_buf) {
1564  return NULL;
1565  }
1566  const char *p = buf;
1567  char *q = new_buf;
1568  while (*p) {
1569  switch (*p) {
1570 #if __UNIX__
1571  case '$':
1572  case '`':
1573 #endif
1574  case '\\':
1575  case '"':
1576  *q++ = '\\';
1577  /* FALLTHRU */
1578  default:
1579  *q++ = *p++;
1580  break;
1581  }
1582  }
1583  *q = '\0';
1584  return new_buf;
1585 }

References malloc(), NULL, p, and rz_return_val_if_fail.

Referenced by rz_core_editor(), rzfind_open_file(), and socket_http_get_recursive().

◆ rz_str_escape_utf()

static char* rz_str_escape_utf ( const char *  buf,
int  buf_size,
RzStrEnc  enc,
bool  show_asciidot,
bool  esc_bslash,
bool  esc_double_quotes,
bool  keep_printable 
)
static

Definition at line 1599 of file str.c.

1599  {
1600  char *new_buf, *q;
1601  const char *p, *end;
1602  RzRune ch;
1603  int i, len, ch_bytes;
1604 
1605  if (!buf) {
1606  return NULL;
1607  }
1608  switch (enc) {
1609  case RZ_STRING_ENC_UTF16LE:
1610  case RZ_STRING_ENC_UTF16BE:
1611  case RZ_STRING_ENC_UTF32LE:
1612  case RZ_STRING_ENC_UTF32BE:
1613  if (buf_size < 0) {
1614  return NULL;
1615  }
1616  if (enc == RZ_STRING_ENC_UTF16LE || enc == RZ_STRING_ENC_UTF16BE) {
1617  end = (char *)rz_mem_mem_aligned((ut8 *)buf, buf_size, (ut8 *)"\0\0", 2, 2);
1618  } else {
1619  end = (char *)rz_mem_mem_aligned((ut8 *)buf, buf_size, (ut8 *)"\0\0\0\0", 4, 4);
1620  }
1621  if (!end) {
1622  end = buf + buf_size - 1; /* TODO: handle overlong strings properly */
1623  }
1624  len = end - buf;
1625  break;
1626  default:
1627  len = strlen(buf);
1628  end = buf + len;
1629  }
1630  /* Worst case scenario, we convert every byte to \xhh */
1631  new_buf = malloc(1 + (len * 4));
1632  if (!new_buf) {
1633  return NULL;
1634  }
1635  p = buf;
1636  q = new_buf;
1637  while (p < end) {
1638  switch (enc) {
1639  case RZ_STRING_ENC_UTF16LE:
1640  case RZ_STRING_ENC_UTF16BE:
1641  case RZ_STRING_ENC_UTF32LE:
1642  case RZ_STRING_ENC_UTF32BE:
1643  if (enc == RZ_STRING_ENC_UTF16LE || enc == RZ_STRING_ENC_UTF16BE) {
1644  ch_bytes = rz_utf16_decode((ut8 *)p, end - p, &ch, enc == RZ_STRING_ENC_UTF16BE);
1645  } else {
1646  ch_bytes = rz_utf32_decode((ut8 *)p, end - p, &ch, enc == RZ_STRING_ENC_UTF32BE);
1647  }
1648  if (ch_bytes == 0) {
1649  p++;
1650  continue;
1651  }
1652  break;
1653  default:
1654  ch_bytes = rz_utf8_decode((ut8 *)p, end - p, &ch);
1655  if (ch_bytes == 0) {
1656  ch_bytes = 1;
1657  }
1658  }
1659  if (show_asciidot && !IS_PRINTABLE(ch)) {
1660  *q++ = '.';
1661  } else if (ch_bytes > 1) {
1662  if (keep_printable) {
1663  q += rz_utf8_encode((ut8 *)q, ch);
1664  } else {
1665  *q++ = '\\';
1666  *q++ = ch_bytes == 4 ? 'U' : 'u';
1667  for (i = ch_bytes == 4 ? 6 : 2; i >= 0; i -= 2) {
1668  *q++ = "0123456789abcdef"[ch >> 4 * (i + 1) & 0xf];
1669  *q++ = "0123456789abcdef"[ch >> 4 * i & 0xf];
1670  }
1671  }
1672  } else {
1673  int offset = enc == RZ_STRING_ENC_UTF16BE ? 1 : enc == RZ_STRING_ENC_UTF32BE ? 3
1674  : 0;
1675  RzStrEscOptions opt = { 0 };
1676  opt.dot_nl = false;
1677  opt.show_asciidot = false;
1678  opt.esc_bslash = esc_bslash;
1679  opt.esc_double_quotes = esc_double_quotes;
1680  rz_str_byte_escape(p + offset, &q, &opt);
1681  }
1682  switch (enc) {
1683  case RZ_STRING_ENC_UTF16LE:
1684  case RZ_STRING_ENC_UTF16BE:
1685  p += ch_bytes < 2 ? 2 : ch_bytes;
1686  break;
1687  case RZ_STRING_ENC_UTF32LE:
1688  case RZ_STRING_ENC_UTF32BE:
1689  p += 4;
1690  break;
1691  default:
1692  p += ch_bytes;
1693  }
1694  }
1695  *q = '\0';
1696  return new_buf;
1697 }
voidpf uLong offset
Definition: ioapi.h:144
RZ_API const ut8 * rz_mem_mem_aligned(const ut8 *haystack, int hlen, const ut8 *needle, int nlen, int align)
Definition: mem.c:260
RZ_API int rz_utf16_decode(const ut8 *ptr, int ptrlen, RzRune *ch, bool bigendian)
Definition: utf16.c:8
RZ_API int rz_utf32_decode(const ut8 *ptr, int ptrlen, RzRune *ch, bool bigendian)
Definition: utf32.c:8
RZ_API int rz_utf8_encode(ut8 *ptr, const RzRune ch)
Definition: utf8.c:535

References buf_size, RzStrEscOptions::dot_nl, test_evm::end, RzStrEscOptions::esc_bslash, RzStrEscOptions::esc_double_quotes, i, IS_PRINTABLE, len, malloc(), NULL, p, rz_mem_mem_aligned(), rz_str_byte_escape(), RZ_STRING_ENC_UTF16BE, RZ_STRING_ENC_UTF16LE, RZ_STRING_ENC_UTF32BE, RZ_STRING_ENC_UTF32LE, rz_utf16_decode(), rz_utf32_decode(), rz_utf8_decode(), rz_utf8_encode(), and RzStrEscOptions::show_asciidot.

Referenced by rz_str_escape_utf16be(), rz_str_escape_utf16le(), rz_str_escape_utf32be(), rz_str_escape_utf32le(), rz_str_escape_utf8(), and rz_str_escape_utf8_keep_printable().

◆ rz_str_escape_utf16be()

RZ_API char* rz_str_escape_utf16be ( const char *  buf,
int  buf_size,
RzStrEscOptions opt 
)

Definition at line 1715 of file str.c.

1715  {
1717 }
static char * rz_str_escape_utf(const char *buf, int buf_size, RzStrEnc enc, bool show_asciidot, bool esc_bslash, bool esc_double_quotes, bool keep_printable)
Definition: str.c:1599

References buf_size, RzStrEscOptions::esc_bslash, RzStrEscOptions::esc_double_quotes, rz_str_escape_utf(), RZ_STRING_ENC_UTF16BE, and RzStrEscOptions::show_asciidot.

Referenced by ds_esc_str().

◆ rz_str_escape_utf16le()

RZ_API char* rz_str_escape_utf16le ( const char *  buf,
int  buf_size,
RzStrEscOptions opt 
)

◆ rz_str_escape_utf32be()

RZ_API char* rz_str_escape_utf32be ( const char *  buf,
int  buf_size,
RzStrEscOptions opt 
)

◆ rz_str_escape_utf32le()

RZ_API char* rz_str_escape_utf32le ( const char *  buf,
int  buf_size,
RzStrEscOptions opt 
)

◆ rz_str_escape_utf8()

◆ rz_str_escape_utf8_for_json()

RZ_API char* rz_str_escape_utf8_for_json ( const char *  buf,
int  buf_size 
)

Definition at line 1834 of file str.c.

1834  {
1835  return escape_utf8_for_json(buf, buf_size, false);
1836 }

References buf_size, and escape_utf8_for_json().

Referenced by core_analysis_graph_nodes(), draw_graph_nodes(), pj_s(), pj_S(), and rz_type_format_nulltermstring().

◆ rz_str_escape_utf8_keep_printable()

RZ_API char* rz_str_escape_utf8_keep_printable ( const char *  buf,
RzStrEscOptions opt 
)

◆ rz_str_filter()

RZ_API void rz_str_filter ( char *  str)

Convert all non-printable characters in str with '.'.

Parameters
strString to make printable.

Definition at line 2359 of file str.c.

2359  {
2360  size_t i;
2361  for (i = 0; str[i]; i++) {
2362  if (!IS_PRINTABLE(str[i])) {
2363  str[i] = '.';
2364  }
2365  }
2366 }

References i, IS_PRINTABLE, and cmd_descs_generate::str.

Referenced by cb_binprefix(), ds_print_ptr(), get_maps_unpatched(), get_name(), get_sections(), get_segments(), maps(), rz_asn1_stringify_string(), rz_core_syscall_as_string(), and sections().

◆ rz_str_filter_zeroline()

RZ_API void rz_str_filter_zeroline ( char *  str,
int  len 
)

Definition at line 2341 of file str.c.

2341  {
2342  int i;
2343  for (i = 0; i < len && str[i]; i++) {
2344  if (str[i] == '\n' || str[i] == '\r') {
2345  break;
2346  }
2347  if (!IS_PRINTABLE(str[i])) {
2348  break;
2349  }
2350  }
2351  str[i] = 0;
2352 }

References i, IS_PRINTABLE, len, and cmd_descs_generate::str.

◆ rz_str_firstbut()

RZ_API const char* rz_str_firstbut ( const char *  s,
char  ch,
const char *  but 
)

Definition at line 2644 of file str.c.

2644  {
2645  int idx, _b = 0;
2646  ut8 *b = (ut8 *)&_b;
2647  const char *isbut, *p;
2648  const int bsz = sizeof(_b) * 8;
2649  if (!but) {
2650  return strchr(s, ch);
2651  }
2652  if (strlen(but) >= bsz) {
2653  eprintf("rz_str_firstbut: but string too long\n");
2654  return NULL;
2655  }
2656  for (p = s; *p; p++) {
2657  isbut = strchr(but, *p);
2658  if (isbut) {
2659  idx = (int)(size_t)(isbut - but);
2660  _b = RZ_BIT_TOGGLE(b, idx);
2661  continue;
2662  }
2663  if (*p == ch && !_b) {
2664  return p;
2665  }
2666  }
2667  return NULL;
2668 }
#define eprintf(x, y...)
Definition: rlcc.c:7
#define RZ_BIT_TOGGLE(x, y)
Definition: rz_types.h:313

References b, eprintf, setup::idx, int, NULL, p, RZ_BIT_TOGGLE, and s.

Referenced by find_next_intgrep(), and rz_core_cmd_subst_i().

◆ rz_str_fmtargs()

RZ_API int rz_str_fmtargs ( const char *  fmt)

Definition at line 3877 of file str.c.

3877  {
3878  int n = 0;
3879  while (*fmt) {
3880  if (*fmt == '%') {
3881  if (fmt[1] == '*') {
3882  n++;
3883  }
3884  n++;
3885  }
3886  fmt++;
3887  }
3888  return n;
3889 }

References n.

◆ rz_str_format_msvc_argv()

RZ_API char* rz_str_format_msvc_argv ( size_t  argc,
const char **  argv 
)

Definition at line 1844 of file str.c.

1844  {
1845  RzStrBuf sb;
1846  rz_strbuf_init(&sb);
1847 
1848  size_t i;
1849  for (i = 0; i < argc; i++) {
1850  if (i > 0) {
1851  rz_strbuf_append(&sb, " ");
1852  }
1853  const char *arg = argv[i];
1854  bool must_escape = strchr(arg, '\"') != NULL;
1855  bool must_quote = strpbrk(arg, " \t") != NULL || !*arg;
1856  if (!must_escape && must_quote && *arg && arg[strlen(arg) - 1] == '\\') {
1857  // if the last char is a bs and we would quote it, we must also escape
1858  must_escape = true;
1859  }
1860  if (must_quote) {
1861  rz_strbuf_append(&sb, "\"");
1862  }
1863  if (must_escape) {
1864  size_t bs_count = 0; // backslash counter
1865  for (; *arg; arg++) {
1866  switch (*arg) {
1867  case '\"':
1868  for (; bs_count; bs_count--) {
1869  // backslashes must be escaped iff they precede a "
1870  // so just duplicate the number of backslashes already printed
1871  rz_strbuf_append(&sb, "\\");
1872  }
1873  rz_strbuf_append(&sb, "\\\"");
1874  break;
1875  case '\\':
1876  bs_count++;
1877  rz_strbuf_append(&sb, "\\");
1878  break;
1879  default:
1880  bs_count = 0;
1881  rz_strbuf_append_n(&sb, arg, 1);
1882  break;
1883  }
1884  }
1885  if (must_quote) {
1886  // there will be a quote after this so we have to escape bs here as well
1887  for (; bs_count; bs_count--) {
1888  rz_strbuf_append(&sb, "\\");
1889  }
1890  }
1891  } else {
1892  rz_strbuf_append(&sb, arg);
1893  }
1894  if (must_quote) {
1895  rz_strbuf_append(&sb, "\"");
1896  }
1897  }
1898 
1899  return rz_strbuf_drain_nofree(&sb);
1900 }
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
RZ_API RZ_OWN char * rz_strbuf_drain_nofree(RzStrBuf *sb)
Definition: strbuf.c:349
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
RZ_API bool rz_strbuf_append_n(RzStrBuf *sb, const char *s, size_t l)
Definition: strbuf.c:229

References arg(), argv, i, NULL, rz_strbuf_append(), rz_strbuf_append_n(), rz_strbuf_drain_nofree(), rz_strbuf_init(), and sb.

Referenced by windbg_open().

◆ rz_str_from_ut64()

RZ_API char* rz_str_from_ut64 ( ut64  val)

Definition at line 3791 of file str.c.

3791  {
3792  int i = 0;
3793  char *v = (char *)&val;
3794  char *str = (char *)calloc(1, 9);
3795  if (!str) {
3796  return NULL;
3797  }
3798  while (i < 8 && *v) {
3799  str[i++] = *v++;
3800  }
3801  return str;
3802 }
ut16 val
Definition: armass64_const.h:6
const char * v
Definition: dsignal.c:12

References calloc(), i, NULL, cmd_descs_generate::str, v, and val.

Referenced by ds_print_ptr().

◆ rz_str_glob()

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

Definition at line 2368 of file str.c.

2368  {
2369  if (!glob) {
2370  return true;
2371  }
2372  char *begin = strchr(glob, '^');
2373  if (begin) {
2374  glob = ++begin;
2375  }
2376  while (*str) {
2377  if (!*glob) {
2378  return true;
2379  }
2380  switch (*glob) {
2381  case '*':
2382  if (!*++glob) {
2383  return true;
2384  }
2385  while (*str) {
2386  if (*glob == *str) {
2387  break;
2388  }
2389  str++;
2390  }
2391  break;
2392  case '$':
2393  return (*++glob == '\x00');
2394  case '?':
2395  str++;
2396  glob++;
2397  break;
2398  default:
2399  if (*glob != *str) {
2400  return false;
2401  }
2402  str++;
2403  glob++;
2404  }
2405  }
2406  while (*glob == '*') {
2407  ++glob;
2408  }
2409  return ((*glob == '$' && !*glob++) || !*glob);
2410 }

References cmd_descs_generate::str.

Referenced by DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), duplicate_flag(), iter_glob_flag(), recursive_search_glob(), rz_core_autocomplete_remove(), rz_core_cmd_foreach3(), rz_flag_foreach_glob(), rz_flag_foreach_space_glob(), and rz_syscmd_ls().

◆ rz_str_guess_encoding_from_buffer()

RZ_API RzStrEnc rz_str_guess_encoding_from_buffer ( RZ_NONNULL const ut8 buffer,
ut32  length 
)

Tries to guess the string encoding method from the buffer.

Parameters
bufferThe string buffer to use for guessing the encoding
lengthThe string buffer length
Returns
string encoding as RzStrEnc type

Definition at line 4089 of file str.c.

4089  {
4092  if (enc != RZ_STRING_ENC_GUESS) {
4093  return enc;
4094  }
4095  for (ut32 i = 0, utf32le = 0, utf32be = 0, utf16le = 0, utf16be = 0, ascii = 0; i < length; ++i) {
4096  ut32 leftovers = length - i;
4097  if (leftovers > 4 && IS_PRINTABLE(buffer[i]) && buffer[i + 1] == 0 && buffer[i + 2] == 0 && buffer[i + 3] == 0) {
4098  utf32le++;
4099  if (utf32le > 2) {
4100  enc = RZ_STRING_ENC_UTF32LE;
4101  break;
4102  }
4103  } else if (leftovers > 4 && buffer[i] == 0 && buffer[i + 1] == 0 && buffer[i + 2] == 0 && IS_PRINTABLE(buffer[i + 3])) {
4104  utf32be++;
4105  if (utf32be > 2) {
4106  enc = RZ_STRING_ENC_UTF32BE;
4107  break;
4108  }
4109  }
4110  if (leftovers > 2 && IS_PRINTABLE(buffer[i]) && buffer[i + 1] == 0) {
4111  utf16le++;
4112  if (utf16le > 2) {
4113  enc = RZ_STRING_ENC_UTF16LE;
4114  break;
4115  }
4116  } else if (leftovers > 2 && buffer[i] == 0 && IS_PRINTABLE(buffer[i + 1])) {
4117  utf16be++;
4118  if (utf16be > 2) {
4119  enc = RZ_STRING_ENC_UTF16BE;
4120  break;
4121  }
4122  }
4123  if (IS_PRINTABLE(buffer[i]) || buffer[i] == ' ') {
4124  ascii++;
4125  if (ascii > length - 1) {
4126  enc = RZ_STRING_ENC_8BIT;
4127  break;
4128  }
4129  }
4130  }
4131  return enc == RZ_STRING_ENC_GUESS ? RZ_STRING_ENC_UTF8 : enc;
4132 }
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 length
Definition: sflib.h:133
RzStrEnc
Definition: rz_str.h:19
RZ_API RzStrEnc rz_utf_bom_encoding(const ut8 *ptr, int ptrlen)
Definition: utf8.c:809
Definition: buffer.h:15

References i, IS_PRINTABLE, length, rz_return_val_if_fail, RZ_STRING_ENC_8BIT, RZ_STRING_ENC_GUESS, RZ_STRING_ENC_UTF16BE, RZ_STRING_ENC_UTF16LE, RZ_STRING_ENC_UTF32BE, RZ_STRING_ENC_UTF32LE, RZ_STRING_ENC_UTF8, and rz_utf_bom_encoding().

Referenced by rz_cmd_print().

◆ rz_str_highlight()

RZ_API char* rz_str_highlight ( char *  str,
const char *  word,
const char *  color,
const char *  color_reset 
)

Definition at line 3657 of file str.c.

3657  {
3658  if (!str || !*str) {
3659  return NULL;
3660  }
3661  ut32 i = 0, j = 0, to_copy;
3662  char *start = str;
3663  ut32 l_str = strlen(str);
3664  ut32 l_reset = strlen(color_reset);
3665  ut32 l_color = color ? strlen(color) : 0;
3666  if (!color) {
3667  return strdup(str);
3668  }
3669  if (!word || !*word) {
3670  return rz_str_newf("%s%s%s", color, str, color_reset);
3671  }
3672  ut32 l_word = strlen(word);
3673  // XXX don't use static buffers
3674  char o[1024] = { 0 };
3675  while (start && (start < str + l_str)) {
3676  int copied = 0;
3677  // find first letter
3678  start = strchr_skip_color_codes(str + i, *word);
3679  if (start) {
3680  to_copy = start - (str + i);
3681  if (to_copy + j + 1 > sizeof(o)) {
3682  // XXX. no limits
3683  break;
3684  }
3685  strncpy(o + j, str + i, to_copy);
3686  i += to_copy;
3687  j += to_copy;
3688  if (!strncmp_skip_color_codes(start, word, l_word)) {
3689  if (j + strlen(color) >= sizeof(o)) {
3690  // XXX. no limits
3691  break;
3692  }
3693  strcpy(o + j, color);
3694  j += l_color;
3695  if (j + l_word >= sizeof(o)) {
3696  // XXX. no limits
3697  break;
3698  }
3699  copied = strncpy_with_color_codes(o + j, str + i, l_word);
3700  i += copied;
3701  j += copied;
3702  if (j + strlen(color_reset) >= sizeof(o)) {
3703  // XXX. no limits
3704  break;
3705  }
3706  strcpy(o + j, color_reset);
3707  j += l_reset;
3708  } else {
3709  o[j++] = str[i++];
3710  }
3711  } else {
3712  if (j + strlen(str + i) >= sizeof(o)) {
3713  break;
3714  }
3715  strcpy(o + j, str + i);
3716  break;
3717  }
3718  }
3719  return strdup(o);
3720 }
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 char * rz_str_newf(const char *fmt,...)
Definition: str.c:897
static int strncpy_with_color_codes(char *s1, char *s2, int n)
Definition: str.c:3582
static int strncmp_skip_color_codes(const char *s1, const char *s2, int n)
Definition: str.c:3605
static char * strchr_skip_color_codes(const char *s, int c)
Definition: str.c:3637
static int color
Definition: visual.c:20

References color, i, NULL, rz_str_newf(), start, cmd_descs_generate::str, strchr_skip_color_codes(), strdup(), strncmp_skip_color_codes(), and strncpy_with_color_codes().

Referenced by ds_highlight_word().

◆ rz_str_home()

RZ_API char* rz_str_home ( const char *  str)

Definition at line 354 of file str.c.

354  {
355  char *dst, *home = rz_sys_getenv(RZ_SYS_HOME);
356  size_t length;
357  if (!home) {
358  home = rz_file_tmpdir();
359  if (!home) {
360  return NULL;
361  }
362  }
363  length = strlen(home) + 1;
364  if (str) {
365  length += strlen(RZ_SYS_DIR) + strlen(str);
366  }
367  dst = (char *)malloc(length);
368  if (!dst) {
369  goto fail;
370  }
371  int home_len = strlen(home);
372  memcpy(dst, home, home_len + 1);
373  if (str) {
374  dst[home_len] = RZ_SYS_DIR[0];
375  strcpy(dst + home_len + 1, str);
376  }
377 fail:
378  free(home);
379  return dst;
380 }
RZ_API char * rz_file_tmpdir(void)
Definition: file.c:1132
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
#define RZ_SYS_DIR
Definition: rz_types.h:218
#define RZ_SYS_HOME
Definition: rz_types.h:220
#define fail(test)
Definition: tests.h:29

References dst, fail, free(), length, malloc(), memcpy(), NULL, rz_file_tmpdir(), RZ_SYS_DIR, rz_sys_getenv(), RZ_SYS_HOME, and cmd_descs_generate::str.

Referenced by autocomplete_process_path(), rz_file_globsearch(), and rz_syscmd_ls().

◆ rz_str_ichr()

RZ_API char* rz_str_ichr ( char *  str,
char  chr 
)

Definition at line 660 of file str.c.

660  {
661  while (*str == chr) {
662  str++;
663  }
664  return str;
665 }

References cmd_descs_generate::str.

Referenced by find_e_opts(), getargpos(), and run_ioctl_command().

◆ rz_str_is2utf8()

RZ_API bool rz_str_is2utf8 ( RZ_NONNULL const char *  c)

Checks if the given character string is a two byte UTF-8 character.

Parameters
cThe character string to test.
Returns
bool True if the character string is a two byte UTF-8 character. False otherwise.

Definition at line 703 of file str.c.

703  {
704  rz_return_val_if_fail(c, false);
705  if (!c[0] || !c[1]) {
706  return false;
707  }
708  return ((c[0] & 0xe0) == 0xc0) && ((c[1] & 0xc0) == 0x80);
709 }

References c, and rz_return_val_if_fail.

Referenced by rz_str_isXutf8(), and rz_str_strchr().

◆ rz_str_is3utf8()

RZ_API bool rz_str_is3utf8 ( RZ_NONNULL const char *  c)

Checks if the given character string is a three byte UTF-8 character.

Parameters
cThe character string to test.
Returns
bool True if the character string is a three byte UTF-8 character. False otherwise.

Definition at line 717 of file str.c.

717  {
718  rz_return_val_if_fail(c, false);
719  if (!c[0] || !c[1] || !c[2]) {
720  return false;
721  }
722  return ((c[0] & 0xf0) == 0xe0) && ((c[1] & 0xc0) == 0x80) && ((c[2] & 0xc0) == 0x80);
723 }

References c, and rz_return_val_if_fail.

Referenced by rz_str_isXutf8(), and rz_str_strchr().

◆ rz_str_is4utf8()

RZ_API bool rz_str_is4utf8 ( RZ_NONNULL const char *  c)

Checks if the given character string is a four byte UTF-8 character.

Parameters
cThe character string to test.
Returns
bool True if the character string is a four byte UTF-8 character. False otherwise.

Definition at line 731 of file str.c.

731  {
732  rz_return_val_if_fail(c, false);
733  if (!c[0] || !c[1] || !c[2] || !c[3]) {
734  return false;
735  }
736  return ((c[0] & 0xf8) == 0xf0) && ((c[1] & 0xc0) == 0x80) && ((c[2] & 0xc0) == 0x80) && ((c[3] & 0xc0) == 0x80);
737 }

References c, and rz_return_val_if_fail.

Referenced by rz_str_isXutf8(), and rz_str_strchr().

◆ rz_str_is_ascii()

RZ_API bool rz_str_is_ascii ( const char *  str)

Definition at line 1988 of file str.c.

1988  {
1989  const ut8 *ptr;
1990  for (ptr = (const ut8 *)str; *ptr; ptr++) {
1991  if (*ptr > 0x7f) {
1992  return false;
1993  }
1994  }
1995  return true;
1996 }

References cmd_descs_generate::str.

Referenced by rz_core_meta_print().

◆ rz_str_is_bool()

RZ_API bool rz_str_is_bool ( const char *  val)

Definition at line 3908 of file str.c.

3908  {
3909  return rz_str_is_true(val) || rz_str_is_false(val);
3910 }
RZ_API bool rz_str_is_false(const char *s)
Definition: str.c:3904
RZ_API bool rz_str_is_true(const char *s)
Definition: str.c:3900

References rz_str_is_false(), rz_str_is_true(), and val.

Referenced by config_print_node().

◆ rz_str_is_false()

RZ_API bool rz_str_is_false ( const char *  s)

Definition at line 3904 of file str.c.

3904  {
3905  return !rz_str_casecmp("no", s) || !rz_str_casecmp("off", s) || !rz_str_casecmp("false", s) || !rz_str_casecmp("0", s) || !*s;
3906 }
RZ_API int rz_str_casecmp(const char *s1, const char *s2)
Definition: str.c:121

References rz_str_casecmp(), and s.

Referenced by boolify_var_cb(), and rz_str_is_bool().

◆ rz_str_is_printable()

RZ_API bool rz_str_is_printable ( const char *  str)

Definition at line 2038 of file str.c.

2038  {
2039  while (*str) {
2040  int ulen = rz_utf8_decode((const ut8 *)str, strlen(str), NULL);
2041  if (ulen > 1) {
2042  str += ulen;
2043  continue;
2044  }
2045  if (!IS_PRINTABLE(*str)) {
2046  return false;
2047  }
2048  str++;
2049  }
2050  return true;
2051 }

References IS_PRINTABLE, NULL, rz_utf8_decode(), and cmd_descs_generate::str.

Referenced by rz_bin_string_decode_base64().

◆ rz_str_is_printable_incl_newlines()

RZ_API bool rz_str_is_printable_incl_newlines ( const char *  str)

Definition at line 2069 of file str.c.

2069  {
2070  while (*str) {
2071  int ulen = rz_utf8_decode((const ut8 *)str, strlen(str), NULL);
2072  if (ulen > 1) {
2073  str += ulen;
2074  continue;
2075  }
2076  if (!IS_PRINTABLE(*str)) {
2077  if (*str != '\r' && *str != '\n' && *str != '\t') {
2078  return false;
2079  }
2080  }
2081  str++;
2082  }
2083  return true;
2084 }

References IS_PRINTABLE, NULL, rz_utf8_decode(), and cmd_descs_generate::str.

Referenced by ds_print_ptr(), and myregwrite().

◆ rz_str_is_printable_limited()

RZ_API bool rz_str_is_printable_limited ( const char *  str,
int  size 
)

Definition at line 2053 of file str.c.

2053  {
2054  while (size > 0 && *str) {
2055  int ulen = rz_utf8_decode((const ut8 *)str, strlen(str), NULL);
2056  if (ulen > 1) {
2057  str += ulen;
2058  continue;
2059  }
2060  if (!IS_PRINTABLE(*str)) {
2061  return false;
2062  }
2063  str++;
2064  size--;
2065  }
2066  return true;
2067 }
voidpf void uLong size
Definition: ioapi.h:138

References IS_PRINTABLE, NULL, rz_utf8_decode(), and cmd_descs_generate::str.

Referenced by rz_asn1_to_string().

◆ rz_str_is_true()

RZ_API bool rz_str_is_true ( const char *  s)

Definition at line 3900 of file str.c.

3900  {
3901  return !rz_str_casecmp("yes", s) || !rz_str_casecmp("on", s) || !rz_str_casecmp("true", s) || !rz_str_casecmp("1", s);
3902 }

References rz_str_casecmp(), and s.

Referenced by rz_config_get(), rz_config_get_b(), rz_config_set(), and rz_str_is_bool().

◆ rz_str_is_utf8()

RZ_API bool rz_str_is_utf8 ( RZ_NONNULL const char *  str)

Returns true if the input string is correctly UTF-8-encoded.

Goes through a null-terminated string and returns false if there is a byte sequence that does not encode a valid UTF-8 code point (as determined by rz_utf8_decode()). If there are no such sequences, it returns true.

Parameters
strInput string to check for UTF-8 validity.

Definition at line 2023 of file str.c.

2023  {
2024  rz_return_val_if_fail(str, false);
2025  const ut8 *ptr = (const ut8 *)str;
2026  size_t len = strlen(str);
2027  while (len) {
2028  int bytes = rz_utf8_decode(ptr, len, NULL);
2029  if (!bytes) {
2030  return false;
2031  }
2032  len -= bytes;
2033  ptr += bytes;
2034  }
2035  return true;
2036 }
static ut8 bytes[32]
Definition: asm_arc.c:23

References bytes, len, NULL, rz_return_val_if_fail, rz_utf8_decode(), and cmd_descs_generate::str.

Referenced by rz_core_file_info_print().

◆ rz_str_is_whitespace()

RZ_API bool rz_str_is_whitespace ( RZ_NONNULL const char *  str)

Checks if the whole string is composed of whitespace.

Parameters
strinput string
Returns
bool true if whitespace (or empty), false otherwise

Definition at line 2004 of file str.c.

2004  {
2005  rz_return_val_if_fail(str, false);
2006  for (const char *ptr = str; *ptr != '\0'; ptr++) {
2007  if (!isspace(*ptr)) {
2008  return false;
2009  }
2010  }
2011  return true;
2012 }
#define isspace(c)
Definition: safe-ctype.h:141

References isspace, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by sh_assembler().

◆ rz_str_isnumber()

RZ_API bool rz_str_isnumber ( const char *  str)

Definition at line 3550 of file str.c.

3550  {
3551  if (!str || (!IS_DIGIT(*str) && *str != '-')) {
3552  return false;
3553  }
3554 
3555  while (*++str) {
3556  if (!IS_DIGIT(*str)) {
3557  return false;
3558  }
3559  }
3560 
3561  return true;
3562 }
#define IS_DIGIT(x)
Definition: rz_str_util.h:11

References IS_DIGIT, and cmd_descs_generate::str.

Referenced by __computeTotal(), ar_parse_header(), clean_function_name(), cmd_print_format(), config_print_node(), extract_arg(), rz_convert_mne_handler(), and windbg_open().

◆ rz_str_isXutf8()

RZ_API bool rz_str_isXutf8 ( RZ_NONNULL const char *  c,
ut8  x 
)

Checks if the byte string matches the criteria of a UTF-8 character of length x.

Parameters
cThe byte string to test.
Returns
bool True if the bytes match an UTF-8 character of length x. False otherwise.

Definition at line 745 of file str.c.

745  {
746  rz_return_val_if_fail(c, false);
747  switch (x) {
748  default:
749  return false;
750  case 1:
751  return isascii(c[0]);
752  case 2:
753  return rz_str_is2utf8(c);
754  case 3:
755  return rz_str_is3utf8(c);
756  case 4:
757  return rz_str_is4utf8(c);
758  }
759 }
RZ_API bool rz_str_is4utf8(RZ_NONNULL const char *c)
Checks if the given character string is a four byte UTF-8 character.
Definition: str.c:731
RZ_API bool rz_str_is2utf8(RZ_NONNULL const char *c)
Checks if the given character string is a two byte UTF-8 character.
Definition: str.c:703
RZ_API bool rz_str_is3utf8(RZ_NONNULL const char *c)
Checks if the given character string is a three byte UTF-8 character.
Definition: str.c:717

References c, rz_return_val_if_fail, rz_str_is2utf8(), rz_str_is3utf8(), rz_str_is4utf8(), and x.

◆ rz_str_last()

RZ_API const char* rz_str_last ( const char *  str,
const char *  ch 
)

Definition at line 3565 of file str.c.

3565  {
3566  char *ptr, *end = NULL;
3567  if (!str || !ch) {
3568  return NULL;
3569  }
3570  do {
3571  ptr = strstr(str, ch);
3572  if (!ptr) {
3573  break;
3574  }
3575  end = ptr;
3576  str = ptr + 1;
3577  } while (true);
3578  return end;
3579 }

References test_evm::end, NULL, and cmd_descs_generate::str.

Referenced by autocomplete_process_path(), and rz_file_globsearch().

◆ rz_str_lastbut()

RZ_API const char* rz_str_lastbut ( const char *  s,
char  ch,
const char *  but 
)

Definition at line 2670 of file str.c.

2670  {
2671  int idx, _b = 0;
2672  ut8 *b = (ut8 *)&_b;
2673  const char *isbut, *p, *lp = NULL;
2674  const int bsz = sizeof(_b) * 8;
2675  if (!but) {
2676  return rz_str_lchr(s, ch);
2677  }
2678  if (strlen(but) >= bsz) {
2679  eprintf("rz_str_lastbut: but string too long\n");
2680  return NULL;
2681  }
2682  for (p = s; *p; p++) {
2683  isbut = strchr(but, *p);
2684  if (isbut) {
2685  idx = (int)(size_t)(isbut - but);
2686  _b = RZ_BIT_TOGGLE(b, idx);
2687  continue;
2688  }
2689  if (*p == ch && !_b) {
2690  lp = p;
2691  }
2692  }
2693  return lp;
2694 }
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669

References b, eprintf, setup::idx, int, NULL, p, RZ_BIT_TOGGLE, rz_str_lchr(), and s.

Referenced by rz_core_cmd_subst_i(), and rz_line_hist_save().

◆ rz_str_lchr()

◆ rz_str_len_utf8()

RZ_API size_t rz_str_len_utf8 ( const char *  s)

Definition at line 2709 of file str.c.

2709  {
2710  size_t i = 0, j = 0, fullwidths = 0;
2711  while (s[i]) {
2712  if ((s[i] & 0xc0) != 0x80) {
2713  j++;
2714  if (rz_str_char_fullwidth(s + i, 4)) {
2715  fullwidths++;
2716  }
2717  }
2718  i++;
2719  }
2720  return j + fullwidths;
2721 }

References i, rz_str_char_fullwidth(), and s.

Referenced by __strbuf_append_col_aligned(), __strbuf_append_col_aligned_fancy(), real_strlen(), rz_core_clippy(), and rz_core_print_disasm().

◆ rz_str_len_utf8_ansi()

RZ_API size_t rz_str_len_utf8_ansi ( const char *  str)

Definition at line 2723 of file str.c.

2723  {
2724  int i = 0, len = 0, fullwidths = 0;
2725  while (str[i]) {
2726  char ch = str[i];
2727  size_t chlen = __str_ansi_length(str + i);
2728  if (chlen > 1) {
2729  i += chlen - 1;
2730  } else if ((ch & 0xc0) != 0x80) { // utf8
2731  len++;
2732  if (rz_str_char_fullwidth(str + i, chlen)) {
2733  fullwidths++;
2734  }
2735  }
2736  i++;
2737  }
2738  return len + fullwidths;
2739 }

References __str_ansi_length(), i, len, rz_str_char_fullwidth(), and cmd_descs_generate::str.

Referenced by __addRow(), __strbuf_append_col_aligned(), __table_adjust(), core_recover_golang_functions_go_1_16(), core_recover_golang_functions_go_1_18(), core_recover_golang_functions_go_1_2(), ds_align_comment(), recover_string_at(), rz_table_add_column(), rz_table_tofancystring(), and rz_table_tosimplestring().

◆ rz_str_len_utf8char()

RZ_API size_t rz_str_len_utf8char ( const char *  s,
int  left 
)

Definition at line 2697 of file str.c.

2697  {
2698  size_t i = 1;
2699  while (s[i] && (!left || i < left)) {
2700  if ((s[i] & 0xc0) != 0x80) {
2701  i++;
2702  } else {
2703  break;
2704  }
2705  }
2706  return i;
2707 }

References i, and s.

◆ rz_str_list_join()

RZ_API char* rz_str_list_join ( RzList str,
const char *  sep 
)

Definition at line 3849 of file str.c.

3849  {
3850  RzStrBuf *sb = rz_strbuf_new("");
3851  const char *p;
3852  while ((p = rz_list_pop_head(str))) {
3853  if (rz_strbuf_length(sb) != 0) {
3854  rz_strbuf_append(sb, sep);
3855  }
3856  rz_strbuf_append(sb, p);
3857  }
3858  return rz_strbuf_drain(sb);
3859 }
RZ_API RZ_OWN void * rz_list_pop_head(RZ_NONNULL RzList *list)
Removes and returns the first element of the list.
Definition: list.c:401
RZ_API int rz_strbuf_length(RzStrBuf *sb)
Definition: strbuf.c:28

References p, rz_list_pop_head(), rz_strbuf_append(), rz_strbuf_drain(), rz_strbuf_length(), rz_strbuf_new(), sb, and cmd_descs_generate::str.

Referenced by cmd_print_gadget(), rz_str_scale(), and sections_print_table().

◆ rz_str_mb_to_wc()

RZ_API wchar_t* rz_str_mb_to_wc ( const char *  buf)

Definition at line 3784 of file str.c.

3784  {
3785  if (!buf) {
3786  return NULL;
3787  }
3788  return rz_str_mb_to_wc_l(buf, strlen(buf));
3789 }
RZ_API wchar_t * rz_str_mb_to_wc_l(const char *buf, int len)
Definition: str.c:3722

References NULL, and rz_str_mb_to_wc_l().

◆ rz_str_mb_to_wc_l()

RZ_API wchar_t* rz_str_mb_to_wc_l ( const char *  buf,
int  len 
)

Definition at line 3722 of file str.c.

3722  {
3723  wchar_t *res_buf = NULL;
3724  size_t sz;
3725  bool fail = true;
3726 
3727  if (!buf || len <= 0) {
3728  return NULL;
3729  }
3730  sz = mbstowcs(NULL, buf, len);
3731  if (sz == (size_t)-1) {
3732  goto err_r_str_mb_to_wc;
3733  }
3734  res_buf = (wchar_t *)calloc(1, (sz + 1) * sizeof(wchar_t));
3735  if (!res_buf) {
3736  goto err_r_str_mb_to_wc;
3737  }
3738  sz = mbstowcs(res_buf, buf, sz + 1);
3739  if (sz == (size_t)-1) {
3740  goto err_r_str_mb_to_wc;
3741  }
3742  fail = false;
3743 err_r_str_mb_to_wc:
3744  if (fail) {
3745  RZ_FREE(res_buf);
3746  }
3747  return res_buf;
3748 }
#define RZ_FREE(x)
Definition: rz_types.h:369

References calloc(), fail, len, NULL, and RZ_FREE.

Referenced by rz_str_mb_to_wc().

◆ rz_str_ncasecmp()

RZ_API int rz_str_ncasecmp ( const char *  s1,
const char *  s2,
size_t  n 
)

◆ rz_str_ncpy()

RZ_API size_t rz_str_ncpy ( char *  dst,
const char *  src,
size_t  dst_size 
)

Secure string copy with null terminator.

This API behaves like strlcpy or strscpy.

Definition at line 923 of file str.c.

923  {
925 
926  // do not do anything if dst_size is 0
927  if (dst_size == 0) {
928  return 0;
929  }
930 #if HAVE_STRLCPY
931  return strlcpy(dst, src, dst_size);
932 #else
933  strncpy(dst, src, dst_size - 1);
934  dst[dst_size - 1] = '\0';
935  return strlen(src);
936 #endif
937 }

References dst, rz_return_val_if_fail, and src.

Referenced by address_bit(), bgets(), build(), cr16_decode_slpr(), ds_atabs_option(), ds_show_flags(), get_imports(), getlistmask(), maps(), meta_string_guess_add(), parse(), parse_format(), parse_grep_expression(), parse_relocation_info(), printCol(), rz_analysis_class_add_attr_unique_raw(), rz_asm_massemble(), rz_bin_addr2line(), rz_config_node_value_format_i(), rz_cons_html_filter(), rz_cons_hud(), rz_core_fgets(), rz_core_get_section_name(), rz_core_visual_title(), rz_file_mkstemp(), rz_rebase_info_new_from_mach0(), rz_regex_get_match_list(), rz_sys_pid_to_path(), rz_th_get_name(), sections(), str_dup_safe_fixed(), and visual_search().

◆ rz_str_ndup()

RZ_API char* rz_str_ndup ( RZ_NULLABLE const char *  ptr,
int  len 
)

Create new copy of string ptr limited to size len.

Parameters
[in]ptrString to create new copy from
[in]lenUpper limit for new string size
Returns
New copy of string ptr with size limited by len or NULL if ptr is NULL

Definition at line 1006 of file str.c.

1006  {
1007  if (!ptr || len < 0) {
1008  return NULL;
1009  }
1010  const size_t str_len = rz_str_nlen(ptr, len);
1011  char *out = malloc(str_len + 1);
1012  if (!out) {
1013  return NULL;
1014  }
1015  memcpy(out, ptr, str_len);
1016  out[str_len] = 0;
1017  return out;
1018 }
RZ_API size_t rz_str_nlen(const char *str, size_t n)
Definition: str.c:1949

References len, malloc(), memcpy(), NULL, out, and rz_str_nlen().

Referenced by _6502_tokenize(), __childrenFlagsOf(), __input_cb(), __open(), arm_tokenize(), autocmplt_cmd_arg_env(), autocmplt_cmd_arg_eval_full(), autocmplt_cmd_arg_file(), autocomplete_sdb(), avr_tokenize(), backward_kill_word(), bin_section_from_section(), bin_section_from_segment(), bootimg_header_load(), dalvik_tokenize(), demangle_classname(), dex_resolve_string_id(), disasm_strings(), do_ref_search(), ds_sub_jumps(), esil_split_flg(), get_cd_from_cmdid(), get_maps_unpatched(), get_max_common_pfx(), get_name(), get_segments(), getnumbang(), getnummemend(), getnummemendbang(), getreglist(), getregmembang(), getregmemend(), getregmemstartend(), getshiftmemend(), info(), is_cpu_valid(), kill_word(), m68k_tokenize(), meta_set(), mips_tokenize(), pager_color_line(), parse_attr_value(), parse_line_header_source(), parse_signature(), parseOpcode(), process_one_string(), readString(), rz_analysis_xrefs_to_list_handler(), rz_asn1_stringify_string(), rz_bin_dex_strings(), rz_cmd_kuery(), rz_coff_symbol_name(), rz_cons_filter(), rz_cons_flush(), rz_cons_grepbuf(), rz_cons_highlight(), rz_core_file_open(), rz_core_rtr_add(), rz_core_visual_esil(), rz_hex_from_c_array(), rz_hex_from_js(), rz_hex_from_py_array(), rz_print_json_path(), rz_remote_tcp_handler(), rz_str_between(), rz_str_replace_icase(), sh_tokenize(), showstr(), socket_http_answer(), str_dup_safe(), str_split_list_common_regex(), tms320_tokenize(), tokenize_asm_generic(), tokenize_lines(), unix_word_rubout(), v850_tokenize(), and z80_tokenize().

◆ rz_str_new()

RZ_API char* rz_str_new ( const char *  str)

◆ rz_str_newf()

RZ_API char* rz_str_newf ( const char *  fmt,
  ... 
)

Definition at line 897 of file str.c.

897  {
899  va_list ap, ap2;
900 
901  va_start(ap, fmt);
902  if (!strchr(fmt, '%')) {
903  va_end(ap);
904  return strdup(fmt);
905  }
906  va_copy(ap2, ap);
907  int ret = vsnprintf(NULL, 0, fmt, ap2);
908  ret++;
909  char *p = calloc(1, ret);
910  if (p) {
911  (void)vsnprintf(p, ret, fmt, ap);
912  }
913  va_end(ap2);
914  va_end(ap);
915  return p;
916 }

References calloc(), NULL, p, rz_return_val_if_fail, strdup(), and vsnprintf.

Referenced by rz_str_highlight(), and rz_str_version().

◆ rz_str_newlen()

RZ_API char* rz_str_newlen ( const char *  str,
int  len 
)

Definition at line 871 of file str.c.

871  {
872  if (len < 0) {
873  return NULL;
874  }
875  char *buf = malloc(len + 1);
876  if (buf) {
877  memcpy(buf, str, len);
878  buf[len] = 0;
879  }
880  return buf;
881 }

References len, malloc(), memcpy(), NULL, and cmd_descs_generate::str.

Referenced by core_cmd_raw(), core_cmd_raw_node(), ds_build_op_str(), filterFlags(), get_word_from_canvas_for_menu(), name_from_table(), print_main_arena_bins(), rz_file_globsearch(), rz_str_appendlen(), rz_str_trunc_ellipsis(), rz_subprocess_err(), and rz_subprocess_out().

◆ rz_str_nextword()

RZ_API char* rz_str_nextword ( char *  s,
char  ch 
)

Definition at line 3912 of file str.c.

3912  {
3913  char *p = strchr(s, ch);
3914  if (!p) {
3915  return NULL;
3916  }
3917  *p++ = 0;
3918  return p;
3919 }

References NULL, p, and s.

Referenced by __printPattern().

◆ rz_str_nlen()

RZ_API size_t rz_str_nlen ( const char *  str,
size_t  n 
)

Definition at line 1949 of file str.c.

1949  {
1951 #if HAVE_STRNLEN
1952  return strnlen(str, n);
1953 #else
1954  size_t len = 0;
1955  while (*str && n) {
1956  len++;
1957  str++;
1958  n--;
1959  }
1960  return len;
1961 #endif
1962 }
size_t strnlen(const char *str, size_t maxlen)

References len, n, rz_return_val_if_fail, cmd_descs_generate::str, and strnlen().

Referenced by parse_ext_opcode(), parse_line_header_source(), rax(), rz_arm_it_update_block(), rz_buf_get_nstring(), rz_cons_canvas_write(), rz_core_yank_print_string(), rz_core_yank_string(), rz_file_is_deflated(), rz_log_set_file(), rz_print_hexdump_hexpair_bytes_handler(), rz_str_ndup(), rz_type_format_nulltermstring(), str_dup_safe(), and type_as_pretty_string().

◆ rz_str_nlen_w()

RZ_API size_t rz_str_nlen_w ( const char *  str,
int  n 
)

Definition at line 1966 of file str.c.

1966  {
1967  size_t len = 0;
1968  if (str) {
1969  while (*str && n > 0) {
1970  len++;
1971  str++;
1972  if (!*str) {
1973  // handle wide strings
1974  // xx00yy00bb00
1975  if (n - 2 > 0) {
1976  if (str[2]) {
1977  break;
1978  }
1979  }
1980  str++;
1981  }
1982  n--;
1983  }
1984  }
1985  return len;
1986 }

References len, n, and cmd_descs_generate::str.

Referenced by rz_core_visual_define().

◆ rz_str_nstr()

RZ_API const char* rz_str_nstr ( const char *  s,
const char *  find,
int  slen 
)

Definition at line 842 of file str.c.

842  {
843  char c, sc;
844  size_t len;
845 
846  if ((c = *find++) != '\0') {
847  len = strlen(find);
848  do {
849  do {
850  if (slen-- < 1 || !(sc = *s++)) {
851  return NULL;
852  }
853  } while (sc != c);
854  if (len > slen) {
855  return NULL;
856  }
857  } while (strncmp(s, find, len) != 0);
858  s--;
859  }
860  return (char *)s;
861 }
static char sc[]
Definition: egg_cb.c:6
RZ_API VALUE_TYPE Ht_() find(HtName_(Ht) *ht, const KEY_TYPE key, bool *found)
Definition: ht_inc.c:330

References c, find(), len, NULL, s, and sc.

Referenced by rz_strstr_ansi().

◆ rz_str_pad()

RZ_API const char* rz_str_pad ( const char  ch,
int  sz 
)

Definition at line 3236 of file str.c.

3236  {
3237  static char pad[1024];
3238  if (sz < 0) {
3239  sz = 0;
3240  }
3241  memset(pad, ch, RZ_MIN(sz, sizeof(pad)));
3242  if (sz < sizeof(pad)) {
3243  pad[sz] = 0;
3244  }
3245  pad[sizeof(pad) - 1] = 0;
3246  return pad;
3247 }
return memset(p, 0, total)
static void pad(RzStrBuf *sb, ut32 count)
Definition: protobuf.c:36
#define RZ_MIN(x, y)

References memset(), pad(), and RZ_MIN.

Referenced by __printRecursive(), __update_help_contents(), __update_panel_contents(), add_spaces(), core_cmp_bits(), ds_print_optype(), fill_level(), flirt_pat_write_line(), flirt_print_indentation(), print_addr(), rz_analysis_function_vars_stackframe_handler(), rz_asn1_print_hex(), rz_asn1_print_hexdump_padded(), rz_asn1_print_padded(), rz_core_clippy(), rz_core_help_vars_print(), rz_core_visual_esil(), rz_list_mne_handler(), rz_print_offset_sg(), rz_str_scale(), selection_widget_draw(), ut64_to_hex(), and visual_flagzone().

◆ rz_str_path_escape()

RZ_API char* rz_str_path_escape ( const char *  path)

Definition at line 2463 of file str.c.

2463  {
2464  char *str;
2465  int dest_i = 0, src_i = 0;
2466 
2467  if (!path) {
2468  return NULL;
2469  }
2470  // Worst case when every character need to be escaped
2471  str = malloc((2 * strlen(path) + 1) * sizeof(char));
2472  if (!str) {
2473  return NULL;
2474  }
2475 
2476  for (src_i = 0; path[src_i] != '\0'; src_i++) {
2477  char c = path[src_i];
2478  switch (c) {
2479  case ' ':
2480  str[dest_i++] = '\\';
2481  str[dest_i++] = c;
2482  break;
2483  default:
2484  str[dest_i++] = c;
2485  break;
2486  }
2487  }
2488 
2489  str[dest_i] = '\0';
2490  return realloc(str, (strlen(str) + 1) * sizeof(char));
2491 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35

References c, malloc(), NULL, path, realloc(), and cmd_descs_generate::str.

Referenced by rz_core_io_file_open().

◆ rz_str_path_unescape()

RZ_API int rz_str_path_unescape ( char *  path)

Definition at line 2493 of file str.c.

2493  {
2494  int i;
2495 
2496  for (i = 0; path[i]; i++) {
2497  if (path[i] != '\\') {
2498  continue;
2499  }
2500  if (path[i + 1] == ' ') {
2501  path[i] = ' ';
2502  memmove(path + i + 1, path + i + 2, strlen(path + i + 2) + 1);
2503  }
2504  }
2505 
2506  return i;
2507 }

References i, and path.

◆ rz_str_prefix_all()

RZ_API char* rz_str_prefix_all ( const char *  s,
const char *  pfx 
)

Definition at line 3038 of file str.c.

3038  {
3039  const char *os = s;
3040  char *p;
3041  int newlines = 1;
3042  int len = 0;
3043  int pfx_len = 0;
3044 
3045  if (!s) {
3046  return strdup(pfx);
3047  }
3048  if (!pfx) {
3049  return strdup(s);
3050  }
3051  len = strlen(s);
3052  pfx_len = strlen(pfx);
3053  for (os = s; *os; os++) {
3054  if (*os == '\n') {
3055  newlines++;
3056  }
3057  }
3058  char *o = malloc(len + (pfx_len * newlines) + 1);
3059  if (!o) {
3060  return NULL;
3061  }
3062  memcpy(o, pfx, pfx_len);
3063  for (p = o + pfx_len; *s; s++) {
3064  *p++ = *s;
3065  if (*s == '\n' && s[1]) {
3066  memcpy(p, pfx, pfx_len);
3067  p += pfx_len;
3068  }
3069  }
3070  *p = 0;
3071  return o;
3072 }

References len, malloc(), memcpy(), NULL, p, s, and strdup().

Referenced by __update_help_contents(), and __update_panel_contents().

◆ rz_str_prepend()

RZ_API char* rz_str_prepend ( char *  ptr,
const char *  string 
)

Definition at line 1027 of file str.c.

1027  {
1028  int slen, plen;
1029  if (!ptr) {
1030  return strdup(string);
1031  }
1032  plen = strlen(ptr);
1033  slen = strlen(string);
1034  ptr = realloc(ptr, slen + plen + 1);
1035  if (!ptr) {
1036  return NULL;
1037  }
1038  memmove(ptr + slen, ptr, plen + 1);
1039  memmove(ptr, string, slen);
1040  return ptr;
1041 }

References NULL, realloc(), and strdup().

Referenced by annotated_hexdump(), core_analysis_graph_construct_nodes(), gdbr_parse_target_xml(), lang_lib_file_run(), parse_reg_profile_str(), parse_tmp_evals(), rz_analysis_reflines_str(), rz_core_cmd_subst_i(), rz_core_print_bb_custom(), rz_main_rizin(), rz_remote_rap_bg_handler(), and rz_remote_rap_handler().

◆ rz_str_range_foreach()

RZ_API void rz_str_range_foreach ( const char *  r,
RzStrRangeCallback  cb,
void *  u 
)

Definition at line 2779 of file str.c.

2779  {
2780  const char *p = r;
2781  for (; *r; r++) {
2782  if (*r == ',') {
2783  cb(u, atoi(p));
2784  p = r + 1;
2785  }
2786  if (*r == '-') {
2787  if (p != r) {
2788  int from = atoi(p);
2789  int to = atoi(r + 1);
2790  for (; from <= to; from++) {
2791  cb(u, from);
2792  }
2793  } else {
2794  fprintf(stderr, "Invalid range\n");
2795  }
2796  for (r++; *r && *r != ',' && *r != '-'; r++) {
2797  ;
2798  }
2799  p = r;
2800  }
2801  }
2802  if (*p) {
2803  cb(u, atoi(p));
2804  }
2805 }
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
static const char * cb[]
Definition: z80_tab.h:176

References cb, from, p, r, and to.

◆ rz_str_range_in()

RZ_API bool rz_str_range_in ( const char *  r,
ut64  addr 
)

Definition at line 2807 of file str.c.

2807  {
2808  const char *p = r;
2809  if (!r) {
2810  return false;
2811  }
2812  for (; *r; r++) {
2813  if (*r == ',') {
2814  if (addr == rz_num_get(NULL, p)) {
2815  return true;
2816  }
2817  p = r + 1;
2818  }
2819  if (*r == '-') {
2820  if (p != r) {
2821  ut64 from = rz_num_get(NULL, p);
2822  ut64 to = rz_num_get(NULL, r + 1);
2823  if (addr >= from && addr <= to) {
2824  return true;
2825  }
2826  } else {
2827  fprintf(stderr, "Invalid range\n");
2828  }
2829  for (r++; *r && *r != ',' && *r != '-'; r++) {
2830  ;
2831  }
2832  p = r;
2833  }
2834  }
2835  if (*p) {
2836  if (addr == rz_num_get(NULL, p)) {
2837  return true;
2838  }
2839  }
2840  return false;
2841 }
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
Definition: unum.c:172
static int addr
Definition: z80asm.c:58

References addr, from, NULL, p, r, rz_num_get(), to, and ut64().

Referenced by internal_esil_mem_read(), and internal_esil_mem_write().

◆ rz_str_rchr()

RZ_API const char* rz_str_rchr ( const char *  base,
const char *  p,
int  ch 
)

Definition at line 829 of file str.c.

829  {
831  if (!p) {
832  return strrchr(base, ch);
833  }
834  for (; p >= base; p--) {
835  if (ch == *p) {
836  break;
837  }
838  }
839  return (p >= base) ? p : NULL;
840 }

References NULL, p, and rz_return_val_if_fail.

Referenced by classdump_java(), disasm_strings(), find_ipa_binary(), riscv_op(), rz_bin_dex_imports(), rz_bin_java_class_const_pool_as_imports(), rz_file_basename(), rz_file_dirname(), rz_file_dos_basename(), rz_type_format_data_internal(), rz_type_format_struct_size(), and winkd_download_module_and_pdb().

◆ rz_str_remove_char()

RZ_API void rz_str_remove_char ( char *  str,
char  c 
)

Definition at line 173 of file str.c.

173  {
174  while (*str) {
175  if (*str == c) {
176  memmove(str, str + 1, strlen(str + 1) + 1);
177  continue;
178  }
179  str++;
180  }
181 }

References c, and cmd_descs_generate::str.

Referenced by rz_cmd_cmp_hexpair_string_handler().

◆ rz_str_repeat()

RZ_API char* rz_str_repeat ( const char *  ch,
int  sz 
)

Definition at line 3249 of file str.c.

3249  {
3250  int i;
3251  if (sz < 0) {
3252  sz = 0;
3253  }
3254  if (sz == 0) {
3255  return strdup("");
3256  }
3257  RzStrBuf *buf = rz_strbuf_new(ch);
3258  for (i = 1; i < sz; i++) {
3259  rz_strbuf_append(buf, ch);
3260  }
3261  return rz_strbuf_drain(buf);
3262 }

References i, rz_strbuf_append(), rz_strbuf_drain(), rz_strbuf_new(), and strdup().

Referenced by __strbuf_append_col_aligned(), rz_core_clippy(), rz_table_tofancystring(), and rz_table_tosimplestring().

◆ rz_str_replace()

RZ_API char* rz_str_replace ( char *  str,
const char *  key,
const char *  val,
int  g 
)

Definition at line 1110 of file str.c.

1110  {
1111  if (g == 'i') {
1112  return rz_str_replace_icase(str, key, val, g, true);
1113  }
1115 
1116  int off, i, slen;
1117  char *newstr, *p = str;
1118  int klen = strlen(key);
1119  int vlen = strlen(val);
1120  if (klen == 1 && vlen < 2) {
1122  return str;
1123  }
1124  if (klen == vlen && !strcmp(key, val)) {
1125  return str;
1126  }
1127  slen = strlen(str);
1128  char *q = str;
1129  for (;;) {
1130  p = strstr(q, key);
1131  if (!p) {
1132  break;
1133  }
1134  off = (int)(size_t)(p - str);
1135  if (vlen != klen) {
1136  int tlen = slen - (off + klen);
1137  slen += vlen - klen;
1138  if (vlen > klen) {
1139  newstr = realloc(str, slen + 1);
1140  if (!newstr) {
1141  eprintf("realloc fail\n");
1142  RZ_FREE(str);
1143  break;
1144  }
1145  str = newstr;
1146  }
1147  p = str + off;
1148  memmove(p + vlen, p + klen, tlen + 1);
1149  }
1150  memcpy(p, val, vlen);
1151  i = off + vlen;
1152  q = str + i;
1153  if (!g) {
1154  break;
1155  }
1156  }
1157  return str;
1158 }
static RASN1String * newstr(const char *string)
Definition: astr.c:23
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 key
Definition: sflib.h:118
struct @667 g
int off
Definition: pal.c:13
RZ_API char * rz_str_replace_icase(char *str, const char *key, const char *val, int g, int keep_case)
Definition: str.c:1160
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169

References eprintf, g, i, int, key, memcpy(), newstr(), NULL, off, p, realloc(), RZ_FREE, rz_return_val_if_fail, rz_str_replace_char(), rz_str_replace_icase(), cmd_descs_generate::str, and val.

Referenced by __replaceImports(), __rotate_panel_cmds(), _replace_register(), apply_edits(), arm_tokenize(), classdump_java(), core_analysis_graph_construct_nodes(), dalvik_tokenize(), ds_build_op_str(), ds_sub_jumps(), get_body(), graph_construct_nodes(), parse(), parse_go_build_info(), pdb_set_symbols(), run_basic_block_analysis(), rz_asm_code_equ_replace(), rz_asm_massemble(), rz_cf_value_string_print(), rz_cons_grepbuf(), rz_core_agraph_add_node(), rz_core_cmd_subst_i(), rz_core_search_rop(), rz_debug_dmp_init(), rz_egg_Cfile_parseCompiled(), rz_file_copy(), rz_file_path_local_to_unix(), rz_file_path_unix_to_local(), rz_file_root(), rz_parse_immtrim(), rz_pseudo_convert(), rz_socket_spawn(), rz_str_replace_in(), sh_tokenize(), subs_var_string(), subvar(), system_apply_env_var(), vernum(), and z80_tokenize().

◆ rz_str_replace_ch()

RZ_API int rz_str_replace_ch ( char *  s,
char  a,
char  b,
bool  global 
)

Definition at line 139 of file str.c.

139  {
140  int ret = 0;
141  char *o = s;
142  if (!s || a == b) {
143  return 0;
144  }
145  for (; *o; s++, o++) {
146  if (*o == a) {
147  ret++;
148  if (b) {
149  *s = b;
150  } else {
151  /* remove char */
152  s--;
153  }
154  if (!global) {
155  return 1;
156  }
157  } else {
158  *s = *o;
159  }
160  }
161  *s = 0;
162  return ret;
163 }

References a, b, and s.

Referenced by autocmplt_cmd_arg_file(), autocomplete_process_path(), demangle_class(), dex_resolve_library(), disassemble(), rz_bin_dex_imports(), rz_bin_java_class_const_pool_as_imports(), rz_bin_java_class_const_pool_as_symbols(), rz_cons_hud_string(), rz_core_bin_class_build_flag_name(), rz_core_bin_field_build_flag_name(), rz_core_bin_method_build_flag_name(), rz_core_bin_super_build_flag_name(), rz_str_replace_char(), rz_str_replace_char_once(), types_xrefs_graph(), windbg_open(), and xrefs_list_print().

◆ rz_str_replace_char()

◆ rz_str_replace_char_once()

RZ_API int rz_str_replace_char_once ( char *  s,
int  a,
int  b 
)

Definition at line 165 of file str.c.

165  {
166  return rz_str_replace_ch(s, a, b, false);
167 }

References a, b, rz_str_replace_ch(), and s.

◆ rz_str_replace_icase()

RZ_API char* rz_str_replace_icase ( char *  str,
const char *  key,
const char *  val,
int  g,
int  keep_case 
)

Definition at line 1160 of file str.c.

1160  {
1162 
1163  int off, i, klen, vlen, slen;
1164  char *newstr, *p = str;
1165  klen = strlen(key);
1166  vlen = strlen(val);
1167 
1168  slen = strlen(str);
1169  for (i = 0; i < slen;) {
1170  p = (char *)rz_str_casestr(str + i, key);
1171  if (!p) {
1172  break;
1173  }
1174  off = (int)(size_t)(p - str);
1175  if (vlen != klen) {
1176  int tlen = slen - (off + klen);
1177  slen += vlen - klen;
1178  if (vlen > klen) {
1179  newstr = realloc(str, slen + 1);
1180  if (!newstr) {
1181  goto alloc_fail;
1182  }
1183  str = newstr;
1184  }
1185  p = str + off;
1186  memmove(p + vlen, p + klen, tlen + 1);
1187  }
1188 
1189  if (keep_case) {
1190  char *tmp_val = strdup(val);
1191  char *str_case = rz_str_ndup(p, klen);
1192  if (!tmp_val || !str_case) {
1193  free(tmp_val);
1194  free(str_case);
1195  goto alloc_fail;
1196  }
1197  tmp_val = rz_str_replace_icase(tmp_val, key, str_case, 0, 0);
1198  free(str_case);
1199  if (!tmp_val) {
1200  goto alloc_fail;
1201  }
1202  memcpy(p, tmp_val, vlen);
1203  free(tmp_val);
1204  } else {
1205  memcpy(p, val, vlen);
1206  }
1207 
1208  i = off + vlen;
1209  if (!g) {
1210  break;
1211  }
1212  }
1213  return str;
1214 
1215 alloc_fail:
1216  eprintf("alloc fail\n");
1217  free(str);
1218  return NULL;
1219 }
RZ_API const char * rz_str_casestr(const char *a, const char *b)
Definition: str.c:2757

References eprintf, free(), g, i, int, key, memcpy(), newstr(), NULL, off, p, realloc(), rz_return_val_if_fail, rz_str_casestr(), rz_str_ndup(), rz_str_replace_icase(), cmd_descs_generate::str, strdup(), and val.

Referenced by rz_cons_grepbuf(), rz_str_replace(), rz_str_replace_icase(), and subvar().

◆ rz_str_replace_in()

RZ_API char* rz_str_replace_in ( char *  str,
ut32  sz,
const char *  key,
const char *  val,
int  g 
)

Definition at line 1288 of file str.c.

1288  {
1289  if (!str || !key || !val) {
1290  return NULL;
1291  }
1292  char *heaped = rz_str_replace(strdup(str), key, val, g);
1293  if (heaped) {
1294  strncpy(str, heaped, sz);
1295  free(heaped);
1296  }
1297  return str;
1298 }
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110

References free(), g, key, NULL, rz_str_replace(), cmd_descs_generate::str, strdup(), and val.

Referenced by gb_parse_arith1(), gb_parse_cb1(), gb_parse_cb2(), gb_parse_ld1(), and gbAsm().

◆ rz_str_replace_thunked()

RZ_API char* rz_str_replace_thunked ( char *  str,
char *  clean,
int thunk,
int  clen,
const char *  key,
const char *  val,
int  g 
)

Definition at line 1233 of file str.c.

1234  {
1235  int i, klen, vlen, slen, delta = 0, bias;
1236  char *newstr, *scnd, *p = clean, *str_p;
1237 
1238  if (!str || !key || !val || !clean || !thunk) {
1239  return NULL;
1240  }
1241  klen = strlen(key);
1242  vlen = strlen(val);
1243  if (klen == vlen && !strcmp(key, val)) {
1244  return str;
1245  }
1246  slen = strlen(str) + 1;
1247 
1248  for (i = 0; i < clen;) {
1249  p = (char *)rz_mem_mem(
1250  (const ut8 *)clean + i, clen - i,
1251  (const ut8 *)key, klen);
1252  if (!p) {
1253  break;
1254  }
1255  i = (int)(size_t)(p - clean);
1256  /* as the original string changes size during replacement
1257  * we need delta to keep track of it*/
1258  str_p = str + thunk[i] + delta;
1259 
1260  int newo = thunk[i + klen] - thunk[i];
1261  rz_str_ansi_filter(str_p, NULL, NULL, newo);
1262  scnd = strdup(str_p + newo);
1263  bias = vlen - newo;
1264 
1265  slen += bias;
1266  // HACK: this 32 avoids overwrites
1267  newstr = realloc(str, slen + klen);
1268  if (!newstr) {
1269  eprintf("realloc fail\n");
1270  RZ_FREE(str);
1271  free(scnd);
1272  break;
1273  }
1274  str = newstr;
1275  str_p = str + thunk[i] + delta;
1276  memcpy(str_p, val, vlen);
1277  memcpy(str_p + vlen, scnd, strlen(scnd) + 1);
1278  i += klen;
1279  delta += bias;
1280  free(scnd);
1281  if (!g) {
1282  break;
1283  }
1284  }
1285  return str;
1286 }
RZ_API const ut8 * rz_mem_mem(const ut8 *haystack, int hlen, const ut8 *needle, int nlen)
Definition: mem.c:246
RZ_API int rz_str_ansi_filter(char *str, char **out, int **cposs, int len)
Definition: str.c:2124
static st64 delta
Definition: vmenus.c:2425

References delta, eprintf, free(), g, i, int, key, memcpy(), newstr(), NULL, p, realloc(), RZ_FREE, rz_mem_mem(), rz_str_ansi_filter(), cmd_descs_generate::str, strdup(), and val.

Referenced by rz_cons_highlight().

◆ rz_str_reverse()

RZ_API void rz_str_reverse ( char *  str)

Definition at line 183 of file str.c.

183  {
184  int i, len = strlen(str);
185  int half = len / 2;
186  for (i = 0; i < half; i++) {
187  char ch = str[i];
188  str[i] = str[len - i - 1];
189  str[len - i - 1] = ch;
190  }
191 }

References i, len, and cmd_descs_generate::str.

Referenced by rax(), rz_num_to_trits(), and rz_str_rstr().

◆ rz_str_rsep()

RZ_API const char* rz_str_rsep ( const char *  base,
const char *  p,
const char *  sep 
)

Definition at line 801 of file str.c.

801  {
802  int i;
803  while (p >= base) {
804  for (i = 0; sep[i]; i++) {
805  if (*p == sep[i]) {
806  return p;
807  }
808  }
809  p--;
810  }
811  return NULL;
812 }

References i, NULL, and p.

Referenced by get_word_from_canvas().

◆ rz_str_rstr()

RZ_API const char* rz_str_rstr ( const char *  base,
const char *  p 
)

Definition at line 814 of file str.c.

814  {
815  char *s = strdup(base);
816  char *k = strdup(p);
817  rz_str_reverse(s);
818  rz_str_reverse(k);
819  char *q = strstr(s, k);
820  const char *r = NULL;
821  if (q) {
822  r = base + strlen(base) - (q - s) - strlen(p);
823  }
824  free(s);
825  free(k);
826  return r;
827 }
RZ_API void rz_str_reverse(char *str)
Definition: str.c:183

References free(), k, NULL, p, r, rz_str_reverse(), s, and strdup().

Referenced by __already_loaded(), and filter().

◆ rz_str_rwx()

RZ_API int rz_str_rwx ( const char *  str)

Definition at line 318 of file str.c.

318  {
319  int ret = atoi(str);
320  if (!ret) {
321  ret |= strchr(str, 'm') ? 16 : 0;
322  ret |= strchr(str, 'r') ? 4 : 0;
323  ret |= strchr(str, 'w') ? 2 : 0;
324  ret |= strchr(str, 'x') ? 1 : 0;
325  } else if (ret < 0 || ret >= RZ_ARRAY_SIZE(rwxstr)) {
326  ret = 0;
327  }
328  return ret;
329 }
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
static const char * rwxstr[]
Definition: str.c:24

References rwxstr, RZ_ARRAY_SIZE, and cmd_descs_generate::str.

Referenced by __io_maps(), mem(), rz_bin_mz_get_segments(), rz_core_debug_bp_add(), rz_core_get_boundaries_prot(), rz_debug_drx_handler(), rz_debug_memory_permission_handler(), rz_open_handler(), rz_open_maps_flags_global_handler(), rz_open_maps_flags_handler(), rz_open_maps_map_handler(), rz_open_nobin_handler(), rz_open_nobin_write_handler(), rz_open_write_handler(), and sections().

◆ rz_str_rwx_i()

◆ rz_str_sanitize()

RZ_API void rz_str_sanitize ( char *  c)

Definition at line 1381 of file str.c.

1381  {
1382  char *d = c;
1383  if (d) {
1384  for (; *d; c++, d++) {
1385  switch (*d) {
1386  case '`':
1387  case '$':
1388  case '{':
1389  case '}':
1390  case '~':
1391  case '|':
1392  case ';':
1393  case '#':
1394  case '@':
1395  case '&':
1396  case '<':
1397  case '>':
1398  *c = '_';
1399  continue;
1400  }
1401  }
1402  }
1403 }
#define d(i)
Definition: sha256.c:44

References c, and d.

Referenced by rz_core_meta_print(), rz_egg_Cfile_parser(), and rz_egg_load_file().

◆ rz_str_sanitize_sdb_key()

RZ_API char* rz_str_sanitize_sdb_key ( const char *  s)

Definition at line 1405 of file str.c.

1405  {
1406  if (!s || !*s) {
1407  return NULL;
1408  }
1409  size_t len = strlen(s);
1410  char *ret = malloc(len + 1);
1411  if (!ret) {
1412  return NULL;
1413  }
1414  char *cur = ret;
1415  while (len > 0) {
1416  char c = *s;
1417  if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && c != '_' && c != ':') {
1418  c = '_';
1419  }
1420  *cur = c;
1421  s++;
1422  cur++;
1423  len--;
1424  }
1425  *cur = '\0';
1426  return ret;
1427 }

References c, len, malloc(), NULL, and s.

Referenced by flagname_attr(), rz_analysis_class_add_attr_unique(), rz_analysis_class_base_get(), rz_analysis_class_base_get_all(), rz_analysis_class_base_list_handler(), rz_analysis_class_base_set(), rz_analysis_class_create(), rz_analysis_class_delete(), rz_analysis_class_delete_attr(), rz_analysis_class_exists(), rz_analysis_class_get_attr(), rz_analysis_class_method_delete(), rz_analysis_class_method_get(), rz_analysis_class_method_get_all(), rz_analysis_class_rename(), rz_analysis_class_rename_attr(), rz_analysis_class_set_attr(), rz_analysis_class_vtable_delete(), rz_analysis_class_vtable_get(), rz_analysis_class_vtable_get_all(), rz_analysis_class_vtable_list_handler(), save_callable(), save_enum(), save_struct(), save_union(), and sdb_save_dwarf_function().

◆ rz_str_scale()

RZ_API char* rz_str_scale ( const char *  s,
int  w,
int  h 
)

Definition at line 3921 of file str.c.

3921  {
3922  // count lines and rows in (s) string
3923  // compute how many lines we should remove or combine
3924  // return a string containing
3925  // for now this function is ascii only (no utf8 or ansi escapes)
3926  RzListIter *iter;
3927  char *line;
3928  char *str = strdup(s);
3929  RzList *lines = rz_str_split_list(str, "\n", 0);
3930  int i, j;
3931  int rows = 0;
3932  int maxcol = 0;
3933 
3934  rows = rz_list_length(lines);
3935  rz_list_foreach (lines, iter, line) {
3936  maxcol = RZ_MAX(strlen(line), maxcol);
3937  }
3938 
3940 
3941  int curline = -1;
3942  char *linetext = (char *)rz_str_pad(' ', w);
3943  for (i = 0; i < h; i++) {
3944  int zoomedline = i * (int)((float)rows / h);
3945  const char *srcline = rz_list_get_n(lines, zoomedline);
3946  int cols = strlen(srcline);
3947  for (j = 0; j < w; j++) {
3948  int zoomedcol = j * ((float)cols / w);
3949  linetext[j] = srcline[zoomedcol];
3950  }
3951  if (curline != zoomedline) {
3952  rz_list_append(out, strdup(linetext));
3953  curline = zoomedline;
3954  }
3955  memset(linetext, ' ', w);
3956  }
3957  free(str);
3958 
3959  char *join = rz_str_list_join(out, "\n");
3960  rz_list_free(out);
3961  return join;
3962 }
static RzBinSourceLineInfo * lines(RzBinFile *bf)
Definition: bin_symbols.c:427
#define w
Definition: crypto_rc6.c:13
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW void * rz_list_get_n(RZ_NONNULL const RzList *list, ut32 n)
Returns the N-th element of the list.
Definition: list.c:574
RZ_API ut32 rz_list_length(RZ_NONNULL const RzList *list)
Returns the length of the list.
Definition: list.c:109
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
line
Definition: setup.py:34
#define RZ_MAX(x, y)
RZ_API const char * rz_str_pad(const char ch, int sz)
Definition: str.c:3236
RZ_API RzList * rz_str_split_list(char *str, const char *c, int n)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3429
RZ_API char * rz_str_list_join(RzList *str, const char *sep)
Definition: str.c:3849

References test-lz4-speed::float, free(), h, i, int, setup::line, lines(), memset(), out, rz_list_append(), rz_list_free(), rz_list_get_n(), rz_list_length(), rz_list_newf(), RZ_MAX, rz_str_list_join(), rz_str_pad(), rz_str_split_list(), s, cmd_descs_generate::str, strdup(), and w.

Referenced by rz_cons_grepbuf().

◆ rz_str_sep()

RZ_API const char* rz_str_sep ( const char *  base,
const char *  sep 
)

Definition at line 788 of file str.c.

788  {
789  int i;
790  while (*base) {
791  for (i = 0; sep[i]; i++) {
792  if (*base == sep[i]) {
793  return base;
794  }
795  }
796  base++;
797  }
798  return NULL;
799 }

References i, and NULL.

Referenced by get_word_from_canvas().

◆ rz_str_split()

RZ_API size_t rz_str_split ( char *  str,
char  ch 
)

Split string str in place by using ch as a delimiter.

Replaces all instances of ch in str with a NULL byte and it returns the number of split strings.

Definition at line 406 of file str.c.

406  {
408  size_t i;
409  char *p;
410  for (i = 1, p = str; *p; p++) {
411  if (*p == ch) {
412  i++;
413  *p = '\0';
414  }
415  }
416  return i;
417 }

References i, p, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by __check_panel_type(), __config_toggle_cb(), __config_value_cb(), __init_menu_color_settings_layout(), __print_hexdump_cb(), __print_stack_cb(), core_analysis_bytes_desc(), disasm_strings(), parse_tmp_evals(), rz_analysis_class_method_recover(), rz_bin_strpurge(), rz_core_asm_plugins_print(), rz_core_cmd_foreach(), rz_core_rtr_http_run(), rz_syscall_item_new_from_string(), rz_write_from_socket_handler(), update_asmcpu_options(), update_asmfeatures_options(), and update_asmplatforms_options().

◆ rz_str_split_duplist()

RZ_API RzList* rz_str_split_duplist ( const char *  _str,
const char *  c,
bool  trim 
)

Split the string str according to the substring c and returns a RzList with the result.

Split a string str according to the delimiter specified in c. It can optionally trim (aka remove spaces) the tokens. The result is a RzList with newly allocated strings for each token.

Parameters
strInput string to split
cDelimiter string used to split str
trimIf true each token is considered without trailing/leading whitespaces.

Definition at line 3464 of file str.c.

3464  {
3465  rz_return_val_if_fail(_str && c, NULL);
3466  char *str = strdup(_str);
3467  RzList *res = str_split_list_common(str, c, 0, trim, true);
3468  free(str);
3469  return res;
3470 }
void trim(string &s, string characters)
Definition: gen_manual.cpp:43
static RzList * str_split_list_common(char *str, const char *c, int n, bool trim, bool dup)
Definition: str.c:3344

References c, free(), NULL, rz_return_val_if_fail, cmd_descs_generate::str, str_split_list_common(), strdup(), and trim().

Referenced by _6502_tokenize(), ar_parse_header(), arm_tokenize(), avr_tokenize(), cb_analysis_roregs(), dalvik_tokenize(), m68k_tokenize(), mips_tokenize(), parse_asm_path(), parse_list(), rz_core_flirt_app_from_option_list(), rz_core_flirt_file_from_option_list(), rz_core_flirt_os_from_option_list(), rz_test_run_cmd_test(), sh_assembler(), sh_tokenize(), tms320_tokenize(), v850_tokenize(), and z80_tokenize().

◆ rz_str_split_duplist_n()

RZ_API RzList* rz_str_split_duplist_n ( const char *  _str,
const char *  c,
int  n,
bool  trim 
)

Split the string str according to the substring c and returns a RzList with the result.

Split a string str according to the delimiter specified in c. It can optionally trim (aka remove spaces) the tokens and/or consider at most n delimiters. The result is a RzList with newly allocated strings for each token.

Parameters
strInput string to split
cDelimiter string used to split str
nIf > 0 at most this number of delimiters are considered.
trimIf true each token is considered without trailing/leading whitespaces.

Definition at line 3485 of file str.c.

3485  {
3486  rz_return_val_if_fail(_str && c, NULL);
3487  char *str = strdup(_str);
3488  RzList *res = str_split_list_common(str, c, n, trim, true);
3489  free(str);
3490  return res;
3491 }

References c, free(), n, NULL, rz_return_val_if_fail, cmd_descs_generate::str, str_split_list_common(), strdup(), and trim().

Referenced by parse_reg_profile_str(), rz_cmd_debug_process_profile_handler(), rz_egg_config_handler(), rz_eval_getset_handler(), and rz_main_rz_bin().

◆ rz_str_split_duplist_n_regex()

RZ_API RZ_OWN RzList* rz_str_split_duplist_n_regex ( RZ_NONNULL const char *  _str,
RZ_NONNULL const char *  r,
int  n,
bool  trim 
)

Split the string str according to the regex r and returns a RzList with the result.

Split a string str according to the regex specified in r. It can optionally trim (aka remove spaces) the tokens and/or consider at most n delimiters. The result is a RzList with newly allocated strings for each token.

Parameters
strInput string to split
rDelimiter regex string used to split str
nIf > 0 at most this number of delimiters are considered.
trimIf true each token is considered without trailing/leading whitespaces.

Definition at line 3506 of file str.c.

3506  {
3507  rz_return_val_if_fail(_str && r, NULL);
3508  char *str = strdup(_str);
3509  RzRegex *regex = rz_regex_new(r, "e");
3510  RzList *res = str_split_list_common_regex(str, regex, n, trim, true);
3511  free(str);
3512  rz_regex_free(regex);
3513  return res;
3514 }
RZ_API RzRegex * rz_regex_new(const char *pattern, const char *cflags)
Definition: regcomp.c:183
RZ_API void rz_regex_free(RzRegex *)
Definition: regcomp.c:249
static RzList * str_split_list_common_regex(RZ_BORROW char *str, RZ_BORROW RzRegex *r, int n, bool trim, bool dup)
Definition: str.c:3370

References free(), n, NULL, r, rz_regex_free(), rz_regex_new(), rz_return_val_if_fail, cmd_descs_generate::str, str_split_list_common_regex(), strdup(), and trim().

Referenced by parse_reg_profile_str().

◆ rz_str_split_lines()

RZ_API size_t* rz_str_split_lines ( char *  str,
size_t count 
)

Split the string str in lines and returns the result in an array.

Split a string str in lines. The number of lines is optionally stored in count, if not NULL. The result is an array of count entries, with the i-th entry containing the index of the first character of the i-th line.

Parameters
strInput string to split
countPointer to a size_t variable that can hold the number of lines.

Definition at line 3526 of file str.c.

3526  {
3528  RzList *l = str_split_list_common(str, "\n", 0, false, false);
3529  if (!l) {
3530  return NULL;
3531  }
3532  size_t cnt = rz_list_length(l);
3533  size_t *res = RZ_NEWS(size_t, cnt);
3534  if (!res) {
3535  return NULL;
3536  }
3537  RzListIter *it;
3538  char *s;
3539  size_t i = 0;
3540  rz_list_foreach (l, it, s) {
3541  res[i++] = s - str;
3542  }
3543  if (count) {
3544  *count = cnt;
3545  }
3546  rz_list_free(l);
3547  return res;
3548 }
#define RZ_NEWS(x, y)
Definition: rz_types.h:283

References count, i, NULL, rz_list_free(), rz_list_length(), RZ_NEWS, rz_return_val_if_fail, s, cmd_descs_generate::str, and str_split_list_common().

Referenced by ds_print_comments_right().

◆ rz_str_split_list()

RZ_API RzList* rz_str_split_list ( char *  str,
const char *  c,
int  n 
)

Split the string str according to the substring c and returns a RzList with the result.

Split a string str according to the delimiter specified in c and it considers at most n delimiters. The result is a RzList with pointers to the input string str. Each token is trimmed as well.

Parameters
strInput string to split. It will be modified by this function.
cDelimiter string used to split str
nIf > 0 at most this number of delimiters are considered.

Definition at line 3429 of file str.c.

3429  {
3431  return str_split_list_common(str, c, n, true, false);
3432 }

References c, n, NULL, rz_return_val_if_fail, cmd_descs_generate::str, and str_split_list_common().

Referenced by cmd_print_gadget(), create_cache_bins(), flirt_pat_parse_line(), parse_hash_algorithms(), rax(), rz_analysis_cc_set(), rz_core_io_plugin_print(), rz_core_rtr_http_run(), rz_core_visual_view_rop(), rz_flag_tags_get(), rz_main_rz_agent(), rz_str_scale(), rz_str_trim_lines(), rz_syscmd_join(), rz_syscmd_sort(), rz_syscmd_uniq(), rz_table_query(), and test_type_for_path().

◆ rz_str_split_list_regex()

RZ_API RZ_OWN RzList* rz_str_split_list_regex ( RZ_NONNULL char *  str,
RZ_NONNULL const char *  r,
int  n 
)

Split the string str according to the regex r and returns a RzList with the result.

Split a string str according to the regex specified in r and it considers at most n delimiters. The result is a RzList with pointers to the input string str.

Parameters
strInput string to split. It will be modified by this function.
rDelimiter regex used to split str
nIf > 0 at most this number of delimiters are considered.

Definition at line 3445 of file str.c.

3445  {
3447  RzRegex *regex = rz_regex_new(r, "e");
3448  RzList *res = str_split_list_common_regex(str, regex, n, false, false);
3449  rz_regex_free(regex);
3450  return res;
3451 }

References n, NULL, r, rz_regex_free(), rz_regex_new(), rz_return_val_if_fail, cmd_descs_generate::str, and str_split_list_common_regex().

◆ rz_str_startswith()

RZ_API bool rz_str_startswith ( RZ_NONNULL const char *  str,
RZ_NONNULL const char *  needle 
)

Checks if a string starts with a specifc sequence of characters (case sensitive)

Parameters
strC-string to be scanned
needleC-string containing the sequence of characters to match
Returns
True if needle is found at the beginning of str and false otherwise
See also
rz_str_startswith_icase()

Definition at line 3286 of file str.c.

3286  {
3287  rz_return_val_if_fail(str && needle, false);
3288  if (str == needle) {
3289  return true;
3290  }
3291  return !strncmp(str, needle, strlen(needle));
3292 }

References rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by __open(), __plugin_open(), __rap_plugin_open(), __system(), _extract_regs(), _resolve_arch(), autocmplt_cmd_arg_file(), autoname_imp_trampoline(), cb_searchin(), cqcheck(), detect_constructor_destructor(), disasm_strings(), do_analysis_search(), download_and_write(), ds_print_data_type(), ds_print_esil_analysis(), ds_print_fcn_name(), ds_print_str(), ds_show_xrefs(), fill_children_chars(), flagname_method(), gdb_to_rz_profile(), gdbr_get_baddr(), gdbr_parse_processes_xml(), gdbr_server_serve(), get_category_t(), get_class_t(), get_dispatchmessage_offset(), getcoprocreg(), handle_qSupported(), handle_stop_reason(), interpret_msrbank(), is_arg_identifier_in_tmp_stmt(), is_auto_named(), is_entry_flag(), isArm(), isSkippable(), itmask(), lang_lib_file_run(), linux_handle_signals(), match_hist_line(), myregwrite(), objc_type_toc(), opmask(), rax(), read_thread_id(), run_basic_block_analysis(), run_new_command(), rz_analysis_noreturn_at_name(), rz_analysis_vtable_begin(), rz_asm_massemble(), rz_big_from_hexstr(), rz_cmd_search(), rz_core_agraph_add_node(), rz_core_analysis_everything(), rz_core_bin_load(), rz_core_cmd_subst(), rz_core_config_print_all(), rz_core_get_boundaries_prot(), rz_io_def_mmap_check_default(), rz_io_def_mmap_create_new_file(), rz_num_base_of_string(), rz_pointer_handler(), rz_socket_http_post(), rz_syscmd_join(), rz_syscmd_ls(), rz_test_main(), set_bin_relocs(), set_jump_realname(), sh_op_get_addr_mode(), show_class(), skip_archos(), socket_http_get_recursive(), start_a_sequence_of_data(), start_a_sequence_of_instruction(), start_a_sequence_of_thumb_instruction(), step_until_inst(), strip_common_prefixes_stdlib(), type_match(), v1_v2_types_foreach_cb(), v2_v3_types_foreach_cb(), var_rename(), and windbg_open().

◆ rz_str_startswith_icase()

RZ_API bool rz_str_startswith_icase ( RZ_NONNULL const char *  str,
RZ_NONNULL const char *  needle 
)

Checks if a string starts with a specifc sequence of characters (case insensitive)

Parameters
strC-string to be scanned
needleC-string containing the sequence of characters to match
Returns
True if needle is found at the beginning of str and false otherwise
See also
rz_str_startswith()

Definition at line 3301 of file str.c.

3301  {
3302  rz_return_val_if_fail(str && needle, false);
3303  if (str == needle) {
3304  return true;
3305  }
3306  return !rz_str_ncasecmp(str, needle, strlen(needle));
3307 }

References rz_return_val_if_fail, rz_str_ncasecmp(), and cmd_descs_generate::str.

Referenced by is_lea().

◆ rz_str_str_xy()

RZ_API const char* rz_str_str_xy ( const char *  s,
const char *  word,
const char *  prev,
int x,
int y 
)

Definition at line 3964 of file str.c.

3964  {
3965  rz_return_val_if_fail(s && word && x && y, NULL);
3966  rz_return_val_if_fail(word[0] != '\0' && word[0] != '\n', NULL);
3967  const char *src = prev ? prev + 1 : s;
3968  const char *d = strstr(src, word);
3969  if (!d) {
3970  return NULL;
3971  }
3972  const char *q;
3973  for (q = prev ? prev : s; q < d; q++) {
3974  if (*q == '\n') {
3975  (*y)++;
3976  *x = 0;
3977 
3978  } else {
3979  (*x)++;
3980  }
3981  }
3982  return d;
3983 }

References d, NULL, rz_return_val_if_fail, s, src, and x.

Referenced by nextword().

◆ rz_str_strchr()

RZ_API const char* rz_str_strchr ( RZ_NONNULL const char *  str,
RZ_NONNULL const char *  c 
)

Returns a pointer to the first occurrence of UTF-8 character c in the string s.

Parameters
strThe string to search.
cThe UTF-8 character to search for.
Returns
char* A pointer to the first occurrence of c in the string (first from the left) or NULL if c was not found.

Definition at line 768 of file str.c.

768  {
770  ut32 i = 0;
771  ut64 str_len = strlen(str);
772  ut8 c_len = isascii(*c) ? 1 : (rz_str_is2utf8(c) ? 2 : (rz_str_is3utf8(c) ? 3 : (rz_str_is4utf8(c) ? 4 : 1)));
773  while (i <= str_len && i + c_len <= str_len) {
774  if (c_len == 1) {
775  if (str[i] == c[0]) {
776  return str + i;
777  }
778  } else {
779  if (rz_mem_eq((ut8 *)str + i, (ut8 *)c, c_len)) {
780  return str + i;
781  }
782  }
783  ++i;
784  }
785  return NULL;
786 }
RZ_API int rz_mem_eq(const ut8 *a, const ut8 *b, int len)
Compares memory a with b over len bytes.
Definition: mem.c:31

References c, i, NULL, rz_mem_eq(), rz_return_val_if_fail, rz_str_is2utf8(), rz_str_is3utf8(), rz_str_is4utf8(), cmd_descs_generate::str, and ut64().

Referenced by parse_reg_profile_str().

◆ rz_str_stringify_raw_buffer()

RZ_API RZ_OWN char* rz_str_stringify_raw_buffer ( RzStrStringifyOpt option,
RZ_NULLABLE RZ_OUT ut32 length 
)

Converts a raw buffer to a printable string based on the selected options.

Parameters
optionPointer to RzStrStringifyOpt.
lengthThe real string length.
Returns
The stringified raw buffer

Definition at line 4141 of file str.c.

4141  {
4142  rz_return_val_if_fail(option && option->buffer && option->encoding != RZ_STRING_ENC_GUESS, NULL);
4143  if (option->length < 1) {
4144  return NULL;
4145  }
4146 
4147  RzStrBuf sb;
4148  const ut8 *buf = option->buffer;
4149  ut32 buflen = option->length;
4150  RzStrEnc enc = option->encoding;
4151  ut32 wrap_at = option->wrap_at;
4152  RzRune rune;
4153  ut32 n_runes = 0;
4154  int rsize = 1; // rune size
4155 
4156  rz_strbuf_init(&sb);
4157  for (ut32 i = 0, line_runes = 0; i < buflen; i += rsize) {
4158  if (enc == RZ_STRING_ENC_UTF32LE) {
4159  rsize = rz_utf32le_decode(&buf[i], buflen - i, &rune);
4160  if (rsize) {
4161  rsize = 4;
4162  }
4163  } else if (enc == RZ_STRING_ENC_UTF16LE) {
4164  rsize = rz_utf16le_decode(&buf[i], buflen - i, &rune);
4165  if (rsize == 1) {
4166  rsize = 2;
4167  }
4168  } else if (enc == RZ_STRING_ENC_UTF32BE) {
4169  rsize = rz_utf32be_decode(&buf[i], buflen - i, &rune);
4170  if (rsize) {
4171  rsize = 4;
4172  }
4173  } else if (enc == RZ_STRING_ENC_UTF16BE) {
4174  rsize = rz_utf16be_decode(&buf[i], buflen - i, &rune);
4175  if (rsize == 1) {
4176  rsize = 2;
4177  }
4178  } else if (enc == RZ_STRING_ENC_IBM037) {
4179  rsize = rz_str_ibm037_to_unicode(buf[i], &rune);
4180  } else if (enc == RZ_STRING_ENC_IBM290) {
4181  rsize = rz_str_ibm290_to_unicode(buf[i], &rune);
4182  } else if (enc == RZ_STRING_ENC_EBCDIC_ES) {
4183  rsize = rz_str_ebcdic_es_to_unicode(buf[i], &rune);
4184  } else if (enc == RZ_STRING_ENC_EBCDIC_UK) {
4185  rsize = rz_str_ebcdic_uk_to_unicode(buf[i], &rune);
4186  } else if (enc == RZ_STRING_ENC_EBCDIC_US) {
4187  rsize = rz_str_ebcdic_us_to_unicode(buf[i], &rune);
4188  } else if (enc == RZ_STRING_ENC_8BIT) {
4189  rune = buf[i];
4190  rsize = rune < 0x7F ? 1 : 0;
4191  } else {
4192  rsize = rz_utf8_decode(&buf[i], buflen - i, &rune);
4193  }
4194 
4195  if (rsize == 0) {
4196  switch (enc) {
4197  case RZ_STRING_ENC_UTF32LE:
4198  rsize = RZ_MIN(4, buflen - i);
4199  break;
4200  case RZ_STRING_ENC_UTF16LE:
4201  rsize = RZ_MIN(2, buflen - i);
4202  break;
4203  case RZ_STRING_ENC_UTF32BE:
4204  rsize = RZ_MIN(4, buflen - i);
4205  break;
4206  case RZ_STRING_ENC_UTF16BE:
4207  rsize = RZ_MIN(2, buflen - i);
4208  break;
4209  default:
4210  rsize = 1;
4211  break;
4212  }
4213  for (int j = 0; j < rsize; ++j) {
4214  rune = buf[i + j];
4215  n_runes++;
4216  if (option->urlencode) {
4217  rz_strbuf_appendf(&sb, "%%%02x", rune);
4218  } else if (option->json) {
4219  rz_strbuf_appendf(&sb, "\\u%04x", rune);
4220  } else {
4221  rz_strbuf_appendf(&sb, "\\x%02x", rune);
4222  }
4223  }
4224  if (wrap_at && line_runes + 1 >= wrap_at) {
4225  rz_strbuf_appendf(&sb, "\n");
4226  line_runes = 0;
4227  }
4228  continue;
4229  } else if (rune == '\0' && option->stop_at_nil) {
4230  break;
4231  } else if (rune == '\n') {
4232  line_runes = 0;
4233  }
4234  line_runes++;
4235  n_runes++;
4236  if (option->urlencode) {
4237  if (IS_DIGIT(rune) || IS_UPPER(rune) || IS_LOWER(rune) || rune == '-' || rune == '_' || rune == '.' || rune == '~') {
4238  // RFC 3986 section 2.3 Unreserved Characters
4239  // A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
4240  // a b c d e f g h i j k l m n o p q r s t u v w x y z
4241  // 0 1 2 3 4 5 6 7 8 9 - _ . ~
4242  char ch = rune;
4243  rz_strbuf_appendf(&sb, "%c", ch);
4244  } else {
4245  ut8 tmp[4];
4246  int n_enc = rz_utf8_encode((ut8 *)tmp, rune);
4247  for (int j = 0; j < n_enc; ++j) {
4248  rz_strbuf_appendf(&sb, "%%%02x", tmp[j]);
4249  }
4250  }
4251  } else if (option->json) {
4252  if (IS_PRINTABLE(rune) && rune != '\"' && rune != '\\') {
4253  char ch = rune;
4254  rz_strbuf_appendf(&sb, "%c", ch);
4255  } else if (rune == '\n') {
4256  rz_strbuf_append(&sb, "\\n");
4257  } else if (rune == '\r') {
4258  rz_strbuf_append(&sb, "\\r");
4259  } else if (rune == '\\') {
4260  rz_strbuf_append(&sb, "\\\\");
4261  } else if (rune == '\t') {
4262  rz_strbuf_append(&sb, "\\t");
4263  } else if (rune == '\f') {
4264  rz_strbuf_append(&sb, "\\f");
4265  } else if (rune == '\b') {
4266  rz_strbuf_append(&sb, "\\b");
4267  } else if (rune == '"') {
4268  rz_strbuf_append(&sb, "\\\"");
4269  } else {
4270  for (int j = 0; j < rsize; ++j) {
4271  rune = buf[i + j];
4272  rz_strbuf_appendf(&sb, "\\u%04x", rune);
4273  }
4274  n_runes += rsize - 1;
4275  }
4276  } else {
4277  if (rune == '\\') {
4278  rz_strbuf_appendf(&sb, "\\\\");
4279  } else if ((rune == '\n' && !option->escape_nl) || (rz_rune_is_printable(rune) && rune >= ' ')) {
4280  char tmp[5] = { 0 };
4281  rz_utf8_encode((ut8 *)tmp, rune);
4282  rz_strbuf_appendf(&sb, "%s", tmp);
4283  } else {
4284  ut8 tmp[4];
4285  int n_enc = rz_utf8_encode((ut8 *)tmp, rune);
4286  for (int j = 0; j < n_enc; ++j) {
4287  rz_strbuf_appendf(&sb, "\\x%02x", tmp[j]);
4288  }
4289  }
4290  }
4291  if (wrap_at && line_runes + 1 >= wrap_at) {
4292  rz_strbuf_appendf(&sb, "\n");
4293  line_runes = 0;
4294  }
4295  }
4296  if (!option->json) {
4297  rz_strbuf_appendf(&sb, "\n");
4298  }
4299  if (length) {
4300  *length = n_runes;
4301  }
4302  return rz_strbuf_drain_nofree(&sb);
4303 }
RZ_API int rz_str_ibm037_to_unicode(const ut8 src, RZ_NONNULL RZ_OUT RzRune *dst)
Convert an ibm037 char into an unicode RzRune.
Definition: ebcdic.c:457
RZ_API int rz_str_ibm290_to_unicode(const ut8 src, RZ_NONNULL RZ_OUT RzRune *dst)
Convert an ibm290 char into an unicode RzRune.
Definition: ebcdic.c:516
RZ_API int rz_str_ebcdic_es_to_unicode(const ut8 src, RZ_NONNULL RZ_OUT RzRune *dst)
Convert an ebcdic_es char into an unicode RzRune.
Definition: ebcdic.c:650
RZ_API int rz_str_ebcdic_us_to_unicode(const ut8 src, RZ_NONNULL RZ_OUT RzRune *dst)
Convert an ebcdic_us char into an unicode RzRune.
Definition: ebcdic.c:607
RZ_API int rz_str_ebcdic_uk_to_unicode(const ut8 src, RZ_NONNULL RZ_OUT RzRune *dst)
Convert an ebcdic_uk char into an unicode RzRune.
Definition: ebcdic.c:563
#define IS_UPPER(c)
Definition: rz_str_util.h:14
#define IS_LOWER(c)
Definition: rz_str_util.h:15
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API int rz_utf16le_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf16.c:36
RZ_API int rz_utf16be_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf16.c:41
RZ_API int rz_utf32le_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf32.c:39
RZ_API int rz_utf32be_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf32.c:44
Definition: getopt.h:84
ut64 buflen
Definition: core.c:76

References buflen, i, IS_DIGIT, IS_LOWER, IS_PRINTABLE, IS_UPPER, length, NULL, RZ_MIN, rz_return_val_if_fail, rz_rune_is_printable(), rz_str_ebcdic_es_to_unicode(), rz_str_ebcdic_uk_to_unicode(), rz_str_ebcdic_us_to_unicode(), rz_str_ibm037_to_unicode(), rz_str_ibm290_to_unicode(), rz_strbuf_append(), rz_strbuf_appendf(), rz_strbuf_drain_nofree(), rz_strbuf_init(), RZ_STRING_ENC_8BIT, RZ_STRING_ENC_EBCDIC_ES, RZ_STRING_ENC_EBCDIC_UK, RZ_STRING_ENC_EBCDIC_US, RZ_STRING_ENC_GUESS, RZ_STRING_ENC_IBM037, RZ_STRING_ENC_IBM290, RZ_STRING_ENC_UTF16BE, RZ_STRING_ENC_UTF16LE, RZ_STRING_ENC_UTF32BE, RZ_STRING_ENC_UTF32LE, rz_utf16be_decode(), rz_utf16le_decode(), rz_utf32be_decode(), rz_utf32le_decode(), rz_utf8_decode(), rz_utf8_encode(), sb, and autogen_x86imm::tmp.

Referenced by core_print_raw_buffer(), and print_json_string().

◆ rz_str_stripLine()

RZ_API void rz_str_stripLine ( char *  str,
const char *  key 
)

Definition at line 3814 of file str.c.

3814  {
3815  size_t i, j, klen, slen, off;
3816  const char *ptr;
3817 
3818  if (!str || !key) {
3819  return;
3820  }
3821  klen = strlen(key);
3822  slen = strlen(str);
3823 
3824  for (i = 0; i < slen;) {
3825  ptr = (char *)rz_mem_mem((ut8 *)str + i, slen - i, (ut8 *)"\n", 1);
3826  if (!ptr) {
3827  ptr = (char *)rz_mem_mem((ut8 *)str + i, slen - i, (ut8 *)key, klen);
3828  if (ptr) {
3829  str[i] = '\0';
3830  break;
3831  }
3832  break;
3833  }
3834 
3835  off = (size_t)(ptr - (str + i)) + 1;
3836 
3837  ptr = (char *)rz_mem_mem((ut8 *)str + i, off, (ut8 *)key, klen);
3838  if (ptr) {
3839  for (j = i; j < slen - off + 1; j++) {
3840  str[j] = str[j + off];
3841  }
3842  slen -= off;
3843  } else {
3844  i += off;
3845  }
3846  }
3847 }
int size_t
Definition: sftypes.h:40

References i, key, off, rz_mem_mem(), and cmd_descs_generate::str.

Referenced by rz_egg_Cfile_parseCompiled().

◆ rz_str_sysbits()

RZ_API const char* rz_str_sysbits ( const int  v)

Definition at line 217 of file str.c.

217  {
218  switch (v) {
219  case RZ_SYS_BITS_8: return "8";
220  case RZ_SYS_BITS_16: return "16";
221  case RZ_SYS_BITS_32: return "32";
222  case RZ_SYS_BITS_64: return "64";
223  case RZ_SYS_BITS_16 | RZ_SYS_BITS_32: return "16,32";
224  case RZ_SYS_BITS_16 | RZ_SYS_BITS_32 | RZ_SYS_BITS_64: return "16,32,64";
225  case RZ_SYS_BITS_32 | RZ_SYS_BITS_64: return "32,64";
226  }
227  return "?";
228 }
@ RZ_SYS_BITS_32
Definition: rz_sys.h:20
@ RZ_SYS_BITS_64
Definition: rz_sys.h:21
@ RZ_SYS_BITS_16
Definition: rz_sys.h:19
@ RZ_SYS_BITS_8
Definition: rz_sys.h:18

References RZ_SYS_BITS_16, RZ_SYS_BITS_32, RZ_SYS_BITS_64, RZ_SYS_BITS_8, and v.

◆ rz_str_tok()

RZ_API const char* rz_str_tok ( const char *  str1,
const char  b,
size_t  len 
)

Definition at line 3199 of file str.c.

3199  {
3200  const char *p = str1;
3201  size_t i = 0;
3202  if (!p || !*p) {
3203  return p;
3204  }
3205  if (len == -1) {
3206  len = strlen(str1);
3207  }
3208  for (; i < len; i++, p++) {
3209  if (*p == b) {
3210  break;
3211  }
3212  }
3213  if (i == len) {
3214  p = NULL;
3215  }
3216  return p;
3217 }

References b, i, len, NULL, and p.

◆ rz_str_trunc_ellipsis()

RZ_API char* rz_str_trunc_ellipsis ( const char *  str,
int  len 
)

Definition at line 883 of file str.c.

883  {
884  if (!str) {
885  return NULL;
886  }
887  if (strlen(str) < len) {
888  return strdup(str);
889  }
890  char *buf = rz_str_newlen(str, len);
891  if (buf && len > 4) {
892  strcpy(buf + len - 4, "...");
893  }
894  return buf;
895 }

References len, NULL, rz_str_newlen(), cmd_descs_generate::str, and strdup().

Referenced by rz_agraph_add_node_with_color(), rz_agraph_del_node(), and rz_agraph_get_node().

◆ rz_str_truncate_cmd()

RZ_API void rz_str_truncate_cmd ( char *  string)

Definition at line 3089 of file str.c.

3089  {
3090  ut32 pos = 0;
3091  if (string && *string) {
3092  ut32 sz = strlen(string);
3093  for (pos = 0; pos < sz; pos++) {
3094  switch (string[pos]) {
3095  case '!':
3096  case ':':
3097  case ';':
3098  case '@':
3099  case '~':
3100  case '(':
3101  case '[':
3102  case '{':
3103  case '?':
3104  string[pos] = '\0';
3105  return;
3106  }
3107  }
3108  }
3109 }
int pos
Definition: main.c:11

References pos.

◆ rz_str_unescape()

RZ_API int rz_str_unescape ( char *  buf)

Definition at line 1300 of file str.c.

1300  {
1301  unsigned char ch = 0, ch2 = 0;
1302  int err = 0;
1303  int i;
1304 
1305  for (i = 0; buf[i]; i++) {
1306  if (buf[i] != '\\') {
1307  continue;
1308  }
1309  int esc_seq_len = 2;
1310  switch (buf[i + 1]) {
1311  case '\\':
1312  case '?':
1313  case '$':
1314  buf[i] = buf[i + 1];
1315  break;
1316  case 'e':
1317  buf[i] = 0x1b;
1318  break;
1319  case 'r':
1320  buf[i] = 0x0d;
1321  break;
1322  case 'n':
1323  buf[i] = 0x0a;
1324  break;
1325  case 'a':
1326  buf[i] = 0x07;
1327  break;
1328  case 'b':
1329  buf[i] = 0x08;
1330  break;
1331  case 't':
1332  buf[i] = 0x09;
1333  break;
1334  case 'v':
1335  buf[i] = 0x0b;
1336  break;
1337  case 'f':
1338  buf[i] = 0x0c;
1339  break;
1340  case 'x':
1341  err = ch2 = ch = 0;
1342  if (!buf[i + 2] || !buf[i + 3]) {
1343  eprintf("Unexpected end of string.\n");
1344  return 0;
1345  }
1346  err |= rz_hex_to_byte(&ch, buf[i + 2]);
1347  err |= rz_hex_to_byte(&ch2, buf[i + 3]);
1348  if (err) {
1349  eprintf("Error: Non-hexadecimal chars in input.\n");
1350  return 0; // -1?
1351  }
1352  buf[i] = (ch << 4) + ch2;
1353  esc_seq_len = 4;
1354  break;
1355  case '\0':
1356  buf[i] = '\0';
1357  return i;
1358  default:
1359  if (IS_OCTAL(buf[i + 1])) {
1360  int num_digits = 1;
1361  buf[i] = buf[i + 1] - '0';
1362  if (IS_OCTAL(buf[i + 2])) {
1363  num_digits++;
1364  buf[i] = (ut8)buf[i] * 8 + (buf[i + 2] - '0');
1365  if (IS_OCTAL(buf[i + 3])) {
1366  num_digits++;
1367  buf[i] = (ut8)buf[i] * 8 + (buf[i + 3] - '0');
1368  }
1369  }
1370  esc_seq_len = 1 + num_digits;
1371  } else {
1372  buf[i] = buf[i + 1];
1373  }
1374  break;
1375  }
1376  memmove(buf + i + 1, buf + i + esc_seq_len, strlen(buf + i + esc_seq_len) + 1);
1377  }
1378  return i;
1379 }
static bool err
Definition: armass.c:435
RZ_API bool rz_hex_to_byte(ut8 *val, ut8 c)
Definition: hex.c:10
#define IS_OCTAL(x)
Definition: rz_str_util.h:12

References eprintf, err, i, IS_OCTAL, rz_hex_to_byte(), and ut8.

Referenced by DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), getstr(), hash_parse_string(), run_old_command(), rz_asm_pseudo_string(), rz_cmd_cmp_string_handler(), rz_cmd_help(), rz_cmd_search(), rz_core_meta_append(), rz_core_write_length_string_at(), rz_core_write_string_at(), rz_core_write_string_wide_at(), rz_core_write_string_zero_at(), rz_main_rz_ax(), and rz_push_escaped_handler().

◆ rz_str_uri_decode()

RZ_API void rz_str_uri_decode ( char *  s)

Definition at line 2845 of file str.c.

2845  {
2846  int n;
2847  char *d;
2848  for (d = s; *s; s++, d++) {
2849  if (*s == '%') {
2850  sscanf(s + 1, "%02x", &n);
2851  *d = n;
2852  s += 2;
2853  } else {
2854  *d = *s;
2855  }
2856  }
2857  *d = 0;
2858 }

References d, n, and s.

Referenced by rz_core_rtr_http_run(), and rz_core_serve().

◆ rz_str_uri_encode()

RZ_API char* rz_str_uri_encode ( const char *  s)

Definition at line 2860 of file str.c.

2860  {
2861  char ch[4], *d, *od;
2862  if (!s) {
2863  return NULL;
2864  }
2865  od = d = malloc(1 + (strlen(s) * 4));
2866  if (!d) {
2867  return NULL;
2868  }
2869  for (; *s; s++) {
2870  if ((*s >= '0' && *s <= '9') || (*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z')) {
2871  *d++ = *s;
2872  } else {
2873  *d++ = '%';
2874  snprintf(ch, sizeof(ch), "%02x", 0xff & ((ut8)*s));
2875  *d++ = ch[0];
2876  *d++ = ch[1];
2877  }
2878  }
2879  *d = 0;
2880  char *trimDown = realloc(od, strlen(od) + 1); // FIT
2881  return trimDown ? trimDown : od;
2882 }
snprintf
Definition: kernel.h:364

References d, malloc(), NULL, realloc(), s, and snprintf.

Referenced by __rtr_shell(), __system(), rtrcmd(), and rz_core_rtr_http_run().

◆ rz_str_utf16_decode()

RZ_API char* rz_str_utf16_decode ( const ut8 s,
int  len 
)

Definition at line 2961 of file str.c.

2961  {
2962  int i = 0;
2963  int j = 0;
2964  char *result = NULL;
2965  int count_unicode = 0;
2966  int count_ascii = 0;
2967  int lenresult = 0;
2968  if (!s) {
2969  return NULL;
2970  }
2971  for (i = 0; i < len && (s[i] || s[i + 1]); i += 2) {
2972  if (!s[i + 1] && 0x20 <= s[i] && s[i] <= 0x7E) {
2973  ++count_ascii;
2974  } else {
2975  ++count_unicode;
2976  }
2977  }
2978  lenresult = 1 + count_ascii + count_unicode * 6; // len("\\uXXXX") = 6
2979  if (!(result = calloc(1 + count_ascii + count_unicode * 6, 1))) {
2980  return NULL;
2981  }
2982  for (i = 0; i < len && j < lenresult && (s[i] || s[i + 1]); i += 2) {
2983  if (!s[i + 1] && IS_PRINTABLE(s[i])) {
2984  result[j++] = s[i];
2985  } else {
2986  j += snprintf(&result[j], lenresult - j, "\\u%.2" HHXFMT "%.2" HHXFMT "", s[i], s[i + 1]);
2987  }
2988  }
2989  return result;
2990 }
#define HHXFMT
Definition: rz_types.h:402

References calloc(), HHXFMT, i, IS_PRINTABLE, len, NULL, s, and snprintf.

◆ rz_str_utf16_encode()

RZ_API char* rz_str_utf16_encode ( const char *  s,
int  len 
)

Definition at line 2993 of file str.c.

2993  {
2994  int i;
2995  char ch[4], *d, *od, *tmp;
2996  if (!s) {
2997  return NULL;
2998  }
2999  if (len < 0) {
3000  len = strlen(s);
3001  }
3002  if ((len * 7) + 1 < len) {
3003  return NULL;
3004  }
3005  od = d = malloc(1 + (len * 7));
3006  if (!d) {
3007  return NULL;
3008  }
3009  for (i = 0; i < len; s++, i++) {
3010  if (*s == '\\') {
3011  *d++ = '\\';
3012  *d++ = '\\';
3013  } else if (*s == '"') {
3014  *d++ = '\\';
3015  *d++ = '"';
3016  } else if ((*s >= 0x20) && (*s <= 126)) {
3017  *d++ = *s;
3018  } else {
3019  *d++ = '\\';
3020  // *d++ = '\\';
3021  *d++ = 'u';
3022  *d++ = '0';
3023  *d++ = '0';
3024  snprintf(ch, sizeof(ch), "%02x", 0xff & ((ut8)*s));
3025  *d++ = ch[0];
3026  *d++ = ch[1];
3027  }
3028  }
3029  *d = 0;
3030  tmp = realloc(od, strlen(od) + 1); // FIT
3031  if (!tmp) {
3032  free(od);
3033  return NULL;
3034  }
3035  return tmp;
3036 }

References d, free(), i, len, malloc(), NULL, realloc(), s, snprintf, and autogen_x86imm::tmp.

Referenced by _cb_hit(), rz_cmd_print(), and rz_type_format_string().

◆ rz_str_utf16_to_utf8()

RZ_API int rz_str_utf16_to_utf8 ( ut8 dst,
int  len_dst,
const ut8 src,
int  len_src,
bool  little_endian 
)

Definition at line 2884 of file str.c.

2884  {
2885  ut8 *outstart = dst;
2886  ut8 *outend = dst + len_dst;
2887  ut16 *in = (ut16 *)src;
2888  ut16 *inend;
2889  ut32 c, d, inlen;
2890  ut8 *tmp;
2891  int bits;
2892 
2893  if ((len_src % 2) == 1) {
2894  len_src--;
2895  }
2896  inlen = len_src / 2;
2897  inend = in + inlen;
2898  while ((in < inend) && (dst - outstart + 5 < len_dst)) {
2899  if (little_endian) {
2900  c = *in++;
2901  } else {
2902  tmp = (ut8 *)in;
2903  c = *tmp++;
2904  if (!c && !*tmp) {
2905  break;
2906  }
2907  c = c | (((ut32)*tmp) << 8);
2908  in++;
2909  }
2910  if ((c & 0xFC00) == 0xD800) { /* surrogates */
2911  if (in >= inend) { /* (in > inend) shouldn't happens */
2912  break;
2913  }
2914  if (little_endian) {
2915  d = *in++;
2916  } else {
2917  tmp = (ut8 *)in;
2918  d = *tmp++;
2919  d = d | (((ut32)*tmp) << 8);
2920  in++;
2921  }
2922  if ((d & 0xFC00) == 0xDC00) {
2923  c &= 0x03FF;
2924  c <<= 10;
2925  c |= d & 0x03FF;
2926  c += 0x10000;
2927  } else {
2928  return -2;
2929  }
2930  }
2931 
2932  /* assertion: c is a single UTF-4 value */
2933  if (dst >= outend) {
2934  break;
2935  }
2936  if (c < 0x80) {
2937  *dst++ = c;
2938  bits = -6;
2939  } else if (c < 0x800) {
2940  *dst++ = ((c >> 6) & 0x1F) | 0xC0;
2941  bits = 0;
2942  } else if (c < 0x10000) {
2943  *dst++ = ((c >> 12) & 0x0F) | 0xE0;
2944  bits = 6;
2945  } else {
2946  *dst++ = ((c >> 18) & 0x07) | 0xF0;
2947  bits = 12;
2948  }
2949 
2950  for (; bits >= 0; bits -= 6) {
2951  if (dst >= outend) {
2952  break;
2953  }
2954  *dst++ = ((c >> bits) & 0x3F) | 0x80;
2955  }
2956  }
2957  len_dst = dst - outstart;
2958  return len_dst;
2959 }
int bits(struct state *s, int need)
Definition: blast.c:72
uint16_t ut16

References bits(), c, d, dst, in, src, and autogen_x86imm::tmp.

Referenced by bin_pe_versioninfo(), rz_bin_dmp64_init_triage_drivers(), sections(), and winkd_list_modules().

◆ rz_str_utf8_charsize()

RZ_API size_t rz_str_utf8_charsize ( const char *  str)

Returns size in bytes of the utf8 char Returns 1 in case of ASCII str - Pointer to buffer

Definition at line 2298 of file str.c.

2298  {
2300  size_t size = 0;
2301  size_t length = strlen(str);
2302  while (size < length && size < 5) {
2303  size++;
2304  if ((str[size] & 0xc0) != 0x80) {
2305  break;
2306  }
2307  }
2308  return size < 5 ? size : 0;
2309 }

References length, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by __delete_next_char(), and __move_cursor_right().

◆ rz_str_utf8_charsize_last()

RZ_API size_t rz_str_utf8_charsize_last ( const char *  str)

Returns size in bytes of the last utf8 char of the string Returns 1 in case of ASCII str - Pointer to buffer

Definition at line 2335 of file str.c.

2335  {
2337  size_t len = strlen(str);
2338  return rz_str_utf8_charsize_prev(str + len, len);
2339 }
RZ_API size_t rz_str_utf8_charsize_prev(const char *str, int prev_len)
Definition: str.c:2317

References len, rz_return_val_if_fail, rz_str_utf8_charsize_prev(), and cmd_descs_generate::str.

Referenced by __delete_prev_char().

◆ rz_str_utf8_charsize_prev()

RZ_API size_t rz_str_utf8_charsize_prev ( const char *  str,
int  prev_len 
)

Returns size in bytes of the utf8 char previous to str Returns 1 in case of ASCII str - Pointer to leading utf8 char prev_len - Length in bytes of the buffer until str

Definition at line 2317 of file str.c.

2317  {
2319  int pos = 0;
2320  size_t size = 0, minsize = RZ_MIN(5, prev_len);
2321  while (size < minsize) {
2322  size++;
2323  if ((str[--pos] & 0xc0) != 0x80) {
2324  break;
2325  }
2326  }
2327  return size < 5 ? size : 0;
2328 }

References pos, RZ_MIN, rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by __delete_prev_char(), __move_cursor_left(), and rz_str_utf8_charsize_last().

◆ rz_str_utf8_codepoint()

RZ_API size_t rz_str_utf8_codepoint ( const char *  s,
size_t  left 
)

Definition at line 2264 of file str.c.

2264  {
2265  if ((*s & 0x80) != 0x80) {
2266  return 0;
2267  } else if ((*s & 0xe0) == 0xc0 && left >= 1) {
2268  return ((*s & 0x1f) << 6) + (*(s + 1) & 0x3f);
2269  } else if ((*s & 0xf0) == 0xe0 && left >= 2) {
2270  return ((*s & 0xf) << 12) + ((*(s + 1) & 0x3f) << 6) + (*(s + 2) & 0x3f);
2271  } else if ((*s & 0xf8) == 0xf0 && left >= 3) {
2272  return ((*s & 0x7) << 18) + ((*(s + 1) & 0x3f) << 12) + ((*(s + 2) & 0x3f) << 6) + (*(s + 3) & 0x3f);
2273  }
2274  return 0;
2275 }

References s.

Referenced by rz_str_char_fullwidth().

◆ rz_str_version()

RZ_API char* rz_str_version ( const char *  program)

Definition at line 4051 of file str.c.

4051  {
4053  if (program) {
4054  rz_strbuf_appendf(sb, "%s ", program);
4055  }
4057  (RZ_SYS_BITS & 8) ? 64 : 32);
4060  }
4061  char *gittip_pathname = rz_str_newf("%s" RZ_SYS_DIR "gittip", rz_path_bindir());
4062  char *gittip = NULL;
4063  if (!gittip_pathname) {
4064  goto done;
4065  }
4066  gittip = rz_file_slurp(gittip_pathname, NULL);
4067  if (!gittip || !*rz_str_trim_head_ro(gittip)) {
4068  goto done;
4069  }
4070  rz_str_trim(gittip);
4071  rz_strbuf_append(sb, "\n");
4072  rz_strbuf_appendf(sb, "commit: %s", gittip);
4073 done:
4074  free(gittip_pathname);
4075  free(gittip);
4076  return rz_strbuf_drain(sb);
4077 }
struct tab * done
Definition: enough.c:233
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
RZ_API RZ_OWN char * rz_path_bindir(void)
Return the directory where the Rizin binaries are placed.
Definition: path.c:148
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
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 RZ_SYS_BITS
Definition: rz_types.h:520
#define RZ_SYS_ARCH
Definition: rz_types.h:519
#define RZ_SYS_OS
Definition: rz_types.h:587
#define RZ_VERSION
Definition: rz_version.h:8
#define RZ_STR_PKG_VERSION_STRING
Definition: str.c:4048

References done, free(), NULL, rz_file_slurp(), rz_path_bindir(), RZ_STR_ISNOTEMPTY, rz_str_newf(), RZ_STR_PKG_VERSION_STRING, rz_str_trim(), rz_str_trim_head_ro(), rz_strbuf_append(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), RZ_SYS_ARCH, RZ_SYS_BITS, RZ_SYS_DIR, RZ_SYS_OS, RZ_VERSION, and sb.

Referenced by __version_cb(), rz_cmd_help(), rz_main_version_print(), and rz_test_main().

◆ rz_str_wc_to_mb()

RZ_API char* rz_str_wc_to_mb ( const wchar_t *  buf)

Definition at line 3777 of file str.c.

3777  {
3778  if (!buf) {
3779  return NULL;
3780  }
3781  return rz_str_wc_to_mb_l(buf, wcslen(buf));
3782 }
RZ_API char * rz_str_wc_to_mb_l(const wchar_t *buf, int len)
Definition: str.c:3750

References NULL, and rz_str_wc_to_mb_l().

◆ rz_str_wc_to_mb_l()

RZ_API char* rz_str_wc_to_mb_l ( const wchar_t *  buf,
int  len 
)

Definition at line 3750 of file str.c.

3750  {
3751  char *res_buf = NULL;
3752  mbstate_t mbstate = { 0 };
3753  size_t sz;
3754 
3755  if (!buf || len <= 0) {
3756  return NULL;
3757  }
3758  sz = wcsrtombs(NULL, &buf, 0, &mbstate);
3759  if (sz == (size_t)-1) {
3760  goto err_r_str_wc_to_mb;
3761  }
3762  res_buf = RZ_NEWS0(char, sz + 1);
3763  if (!res_buf) {
3764  goto err_r_str_wc_to_mb;
3765  }
3766  sz = wcsrtombs(res_buf, &buf, sz + 1, &mbstate);
3767  if (sz == (size_t)-1) {
3768  goto err_r_str_wc_to_mb;
3769  }
3770  return res_buf;
3771 
3772 err_r_str_wc_to_mb:
3773  free(res_buf);
3774  return NULL;
3775 }
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282

References free(), len, NULL, and RZ_NEWS0.

Referenced by rz_str_wc_to_mb().

◆ rz_str_word_count()

RZ_API int rz_str_word_count ( const char *  string)

Definition at line 643 of file str.c.

643  {
644  int word;
645  const char *text = word_get_first(string);
646 
647  for (word = 0; *text; word++) {
650  }
651 
652  return word;
653 }
static const char * separator_get_first(const char *text)
Definition: str.c:621
static const char * word_get_first(const char *text)
Definition: str.c:629

References separator_get_first(), create_tags_rz::text, and word_get_first().

Referenced by rz_asm_pseudo_byte().

◆ rz_str_word_get0()

RZ_API const char* rz_str_word_get0 ( const char *  str,
int  idx 
)

◆ rz_str_word_get0set()

RZ_API char* rz_str_word_get0set ( char *  stra,
int  stralen,
int  idx,
const char *  newstr,
int newlen 
)

Definition at line 548 of file str.c.

548  {
549  char *p = NULL;
550  char *out;
551  int alen, blen, nlen;
552  if (!stra && !newstr) {
553  return NULL;
554  }
555  if (stra) {
556  p = (char *)rz_str_word_get0(stra, idx);
557  }
558  if (!p) {
559  int nslen = strlen(newstr);
560  out = malloc(nslen + 1);
561  if (!out) {
562  return NULL;
563  }
564  strcpy(out, newstr);
565  out[nslen] = 0;
566  if (newlen) {
567  *newlen = nslen;
568  }
569  return out;
570  }
571  alen = (size_t)(p - stra);
572  blen = stralen - ((alen + strlen(p)) + 1);
573  if (blen < 0) {
574  blen = 0;
575  }
576  nlen = alen + blen + strlen(newstr);
577  out = malloc(nlen + 2);
578  if (!out) {
579  return NULL;
580  }
581  if (alen > 0) {
582  memcpy(out, stra, alen);
583  }
584  memcpy(out + alen, newstr, strlen(newstr) + 1);
585  if (blen > 0) {
586  memcpy(out + alen + strlen(newstr) + 1, p + strlen(p) + 1, blen + 1);
587  }
588  out[nlen + 1] = 0;
589  if (newlen) {
590  *newlen = nlen + ((blen == 0) ? 1 : 0);
591  }
592  return out;
593 }
RZ_API const char * rz_str_word_get0(const char *str, int idx)
Definition: str.c:598

References setup::idx, malloc(), memcpy(), newstr(), NULL, out, p, and rz_str_word_get0().

◆ rz_str_word_get_first()

RZ_API char* rz_str_word_get_first ( const char *  text)

Definition at line 637 of file str.c.

637  {
638  return strdup(word_get_first(text));
639 }

References strdup(), create_tags_rz::text, and word_get_first().

◆ rz_str_word_set0()

RZ_API int rz_str_word_set0 ( char *  str)

Definition at line 423 of file str.c.

423  {
424  int i, quote = 0;
425  char *p;
426  if (!str || !*str) {
427  return 0;
428  }
429  for (i = 0; str[i] && str[i + 1]; i++) {
430  if (i > 0 && str[i - 1] == ' ' && str[i] == ' ') {
431  int len = strlen(str + i);
432  memmove(str + i, str + i + 1, len);
433  i--;
434  }
435  }
436  if (str[i] == ' ') {
437  str[i] = 0;
438  }
439  for (i = 1, p = str; *p; p++) {
440  if (*p == '\"') {
441  if (quote) {
442  quote = 0;
443  *p = '\0';
444  // FIX: i++;
445  continue;
446  } else {
447  quote = 1;
448  memmove(p, p + 1, strlen(p + 1) + 1);
449  }
450  }
451  if (quote) {
452  continue;
453  }
454  if (*p == ' ') {
455  char *q = p - 1;
456  if (p > str && (*q == '\\' || !*q)) {
457  memmove(p, p + 1, strlen(p + 1) + 1);
458  if (*q == '\\') {
459  *q = ' ';
460  continue;
461  }
462  p--;
463  }
464  i++;
465  *p = '\0';
466  } // s/ /\0/g
467  }
468  return i;
469 }

References i, len, p, and cmd_descs_generate::str.

Referenced by autocompleteFilename(), cmd_debug_cont_syscall(), cmp(), macro_call(), rz_asm_pseudo_byte(), rz_asm_pseudo_incbin(), rz_cmd_debug_dmi(), and rz_cmd_macro_add().

◆ rz_str_word_set0_stack()

RZ_API int rz_str_word_set0_stack ( char *  str)

Definition at line 471 of file str.c.

471  {
472  int i;
473  char *p, *q;
474  RzStack *s;
475  void *pop;
476  if (!str || !*str) {
477  return 0;
478  }
479  for (i = 0; str[i] && str[i + 1]; i++) {
480  if (i > 0 && str[i - 1] == ' ' && str[i] == ' ') {
481  memmove(str + i, str + i + 1, strlen(str + i));
482  i--;
483  }
484  if (i == 0 && str[i] == ' ') {
485  memmove(str + i, str + i + 1, strlen(str + i));
486  }
487  }
488  if (str[i] == ' ') {
489  str[i] = 0;
490  }
491  s = rz_stack_new(5); // Some random number
492  for (i = 1, p = str; *p; p++) {
493  q = p - 1;
494  if (p > str && (*q == '\\')) {
495  memmove(q, p, strlen(p) + 1);
496  p--;
497  continue;
498  }
499  switch (*p) {
500  case '(':
501  case '{':
502  case '[':
503  rz_stack_push(s, (void *)p);
504  continue;
505  case '\'':
506  case '"':
507  pop = rz_stack_pop(s);
508  if (pop && *(char *)pop != *p) {
509  rz_stack_push(s, pop);
510  rz_stack_push(s, (void *)p);
511  } else if (!pop) {
512  rz_stack_push(s, (void *)p);
513  }
514  continue;
515  case ')':
516  case '}':
517  case ']':
518  pop = rz_stack_pop(s);
519  if (pop) {
520  if ((*(char *)pop == '(' && *p == ')') ||
521  (*(char *)pop == '{' && *p == '}') ||
522  (*(char *)pop == '[' && *p == ']')) {
523  continue;
524  }
525  }
526  break;
527  case ' ':
528  if (p > str && !*q) {
529  memmove(p, p + 1, strlen(p + 1) + 1);
530  if (*q == '\\') {
531  *q = ' ';
532  continue;
533  }
534  p--;
535  }
536  if (rz_stack_is_empty(s)) {
537  i++;
538  *p = '\0';
539  }
540  default:
541  break;
542  }
543  }
544  rz_stack_free(s);
545  return i;
546 }
RZ_API bool rz_stack_is_empty(RzStack *s)
Definition: stack.c:68
RZ_API void * rz_stack_pop(RzStack *s)
Definition: stack.c:59
RZ_API bool rz_stack_push(RzStack *s, void *el)
Definition: stack.c:42
RZ_API RzStack * rz_stack_new(ut32 n)
Definition: stack.c:6
RZ_API void rz_stack_free(RzStack *s)
Definition: stack.c:29

References i, p, rz_stack_free(), rz_stack_is_empty(), rz_stack_new(), rz_stack_pop(), rz_stack_push(), s, and cmd_descs_generate::str.

Referenced by rz_type_format_data_internal(), and rz_type_format_struct_size().

◆ rz_str_wrap()

RZ_API RzList* rz_str_wrap ( char *  str,
size_t  width 
)

Wrap the input string according to the provided width, so that (if possible), each line fits in width characters. Words will not be split across multiple lines. Words are consecutive characters separated by one or more space. Spaces at the beginning of string will be maintained, trailing whitespaces at the end of each split line is removed.

Parameters
stringa writable string, it will be modified by the function
widththe maximum size of each line. It will be respected only if possible, as the function won't split words.

Definition at line 3996 of file str.c.

3996  {
3998 
3999  RzList *res = rz_list_new();
4000  if (!res) {
4001  return NULL;
4002  }
4003  char *p, *start_line = str;
4004  char *first_space = NULL, *last_space = NULL;
4005 
4006  p = (char *)rz_str_trim_head_ro(str);
4007  if (!*p) {
4008  return res;
4009  }
4010 
4011  do {
4012  p++;
4013  if (!*p || isspace((int)*p)) {
4014  if (!last_space || p != last_space + 1) {
4015  if (p - start_line > width && first_space) {
4016  rz_list_append(res, start_line);
4017  *first_space = '\0';
4018  start_line = last_space + 1;
4019  }
4020  first_space = p;
4021  }
4022  last_space = p;
4023  }
4024  } while (*p);
4025  p--;
4026  while (p >= str && isspace((int)*p)) {
4027  *p = '\0';
4028  p--;
4029  }
4030  if (p > start_line) {
4031  rz_list_append(res, start_line);
4032  }
4033 
4034  return res;
4035 }
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
int width
Definition: main.c:10

References isspace, NULL, p, rz_list_append(), rz_list_new(), rz_return_val_if_fail, rz_str_trim_head_ro(), cmd_descs_generate::str, and width.

Referenced by fill_wrapped_comment().

◆ rz_str_write()

RZ_API int rz_str_write ( int  fd,
const char *  b 
)

Definition at line 2775 of file str.c.

2775  {
2776  return write(fd, b, strlen(b));
2777 }
static static fork write
Definition: sflib.h:33
static const z80_opcode fd[]
Definition: z80_tab.h:997

References b, fd, and write.

◆ rz_strstr_ansi()

RZ_API const char* rz_strstr_ansi ( const char *  a,
const char *  b 
)

Definition at line 2742 of file str.c.

2742  {
2743  const char *ch, *p = a;
2744  do {
2745  ch = strchr(p, '\x1b');
2746  if (ch) {
2747  const char *v = rz_str_nstr(p, b, ch - p);
2748  if (v) {
2749  return v;
2750  }
2751  p = ch + __str_ansi_length(ch);
2752  }
2753  } while (ch);
2754  return strstr(p, b);
2755 }
RZ_API const char * rz_str_nstr(const char *s, const char *find, int slen)
Definition: str.c:842

References __str_ansi_length(), a, b, p, rz_str_nstr(), and v.

Referenced by rz_cons_grep_line().

◆ rz_sub_str_lchr()

RZ_API const char* rz_sub_str_lchr ( const char *  str,
int  start,
int  end,
char  chr 
)

Definition at line 682 of file str.c.

682  {
683  do {
684  end--;
685  } while (str[end] != chr && end >= start);
686  return str[end] == chr ? &str[end] : NULL;
687 }

References test_evm::end, NULL, start, and cmd_descs_generate::str.

Referenced by rz_core_autocomplete(), and rz_line_autocomplete().

◆ rz_sub_str_rchr()

RZ_API const char* rz_sub_str_rchr ( const char *  str,
int  start,
int  end,
char  chr 
)

Definition at line 690 of file str.c.

690  {
691  while (str[start] && str[start] != chr && start < end) {
692  start++;
693  }
694  return str[start] == chr ? str + start : NULL;
695 }

References test_evm::end, NULL, start, and cmd_descs_generate::str.

Referenced by autocmplt_arch(), autocmplt_cmd_arg_eval_full(), rz_core_autocomplete(), and rz_line_autocomplete().

◆ rz_wstr_clen()

RZ_API size_t rz_wstr_clen ( const char *  s)

Definition at line 2087 of file str.c.

2087  {
2088  size_t len = 0;
2089  if (!*s++) {
2090  return 0;
2091  }
2092  while (*s++ || *s++) {
2093  len++;
2094  }
2095  return len + 1;
2096 }

References len, and s.

Referenced by rz_type_format_data_internal(), and rz_type_format_nulltermwidestring().

◆ separator_get_first()

static const char* separator_get_first ( const char *  text)
static

Definition at line 621 of file str.c.

621  {
622  for (; *text && !IS_SEPARATOR(*text); text++)
623  ;
624  ;
625 
626  return text;
627 }
#define IS_SEPARATOR(x)
Definition: rz_str_util.h:6

References IS_SEPARATOR, and create_tags_rz::text.

Referenced by rz_str_word_count().

◆ str_endswith()

static bool str_endswith ( RZ_NONNULL const char *  str,
RZ_NONNULL const char *  needle,
bool  case_sensitive 
)
static

Definition at line 3309 of file str.c.

3309  {
3310  rz_return_val_if_fail(str && needle, false);
3311  if (!*needle) {
3312  return true;
3313  }
3314  int slen = strlen(str);
3315  int nlen = strlen(needle);
3316  if (!slen || !nlen || slen < nlen) {
3317  return false;
3318  }
3319  return case_sensitive ? !strcmp(str + (slen - nlen), needle) : !rz_str_ncasecmp(str + (slen - nlen), needle, nlen);
3320 }

References rz_return_val_if_fail, rz_str_ncasecmp(), and cmd_descs_generate::str.

Referenced by rz_str_endswith(), and rz_str_endswith_icase().

◆ str_split_list_common()

static RzList* str_split_list_common ( char *  str,
const char *  c,
int  n,
bool  trim,
bool  dup 
)
static

Definition at line 3344 of file str.c.

3344  {
3346  RzList *lst = rz_list_newf(dup ? free : NULL);
3347  char *aux = str;
3348  int i = 0;
3349  char *e = aux;
3350  size_t clen = strlen(c);
3351  for (; e;) {
3352  e = strstr(aux, c);
3353  if (n > 0 && ++i > n) {
3354  rz_list_append(lst, dup ? strdup(aux) : aux);
3355  break;
3356  }
3357  if (e) {
3358  *e = 0;
3359  e += clen;
3360  }
3361  if (trim) {
3362  rz_str_trim(aux);
3363  }
3364  rz_list_append(lst, dup ? strdup(aux) : aux);
3365  aux = e;
3366  }
3367  return lst;
3368 }
#define e(frag)
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode dup
Definition: sflib.h:68

References c, dup, e, free(), i, n, NULL, rz_list_append(), rz_list_newf(), rz_return_val_if_fail, rz_str_trim(), cmd_descs_generate::str, strdup(), and trim().

Referenced by rz_str_split_duplist(), rz_str_split_duplist_n(), rz_str_split_lines(), and rz_str_split_list().

◆ str_split_list_common_regex()

static RzList* str_split_list_common_regex ( RZ_BORROW char *  str,
RZ_BORROW RzRegex r,
int  n,
bool  trim,
bool  dup 
)
static

Definition at line 3370 of file str.c.

3370  {
3372  RzList *lst = rz_list_newf(dup ? free : NULL);
3373  RzRegexMatch m[1];
3374  char *aux;
3375  int i = 0;
3376  int s = 0, e = 0;
3377  int j = 0;
3378  while (rz_regex_exec(r, str + j, 1, m, 0) == 0) {
3379  if (n == i && n > 0) {
3380  break;
3381  }
3382  s = m[0].rm_so; // Match start (inclusive) in string str + j
3383  e = m[0].rm_eo; // Match end (exclusive) in string str + j
3384  if (dup) {
3385  aux = rz_str_ndup(str + j, s);
3386  } else {
3387  // Overwrite split chararcters.
3388  memset(str + j + s, 0, e - s);
3389  aux = str + j;
3390  }
3391  if (trim) {
3392  rz_str_trim(aux);
3393  }
3394  rz_list_append(lst, aux);
3395  j += e;
3396  ++i;
3397  }
3398  if (*(str + j) == 0 || (n == i && n > 0) || rz_list_length(lst) == 0) {
3399  // No token left.
3400  return lst;
3401  }
3402 
3403  if (dup) {
3404  aux = rz_str_ndup(str + j, strlen(str + j));
3405  } else {
3406  // Overwrite split chararcters.
3407  memset(str + j + s, 0, e - s);
3408  aux = str + j;
3409  }
3410  if (trim) {
3411  rz_str_trim(aux);
3412  }
3413  rz_list_append(lst, aux);
3414 
3415  return lst;
3416 }
RZ_API int rz_regex_exec(const RzRegex *preg, const char *string, size_t nmatch, RzRegexMatch __pmatch[], int eflags)
Definition: regexec.c:149

References dup, e, free(), i, regress::m, memset(), n, NULL, r, rz_list_append(), rz_list_length(), rz_list_newf(), rz_regex_exec(), rz_return_val_if_fail, rz_str_ndup(), rz_str_trim(), s, cmd_descs_generate::str, and trim().

Referenced by rz_str_split_duplist_n_regex(), and rz_str_split_list_regex().

◆ strchr_skip_color_codes()

static char* strchr_skip_color_codes ( const char *  s,
int  c 
)
static

Definition at line 3637 of file str.c.

3637  {
3638  int i = 0;
3639  for (i = 0; s[i]; i++) {
3640  while (s[i] && s[i] == 0x1b) {
3641  while (s[i] && s[i] != 'm') {
3642  i++;
3643  }
3644  if (s[i]) {
3645  i++;
3646  }
3647  }
3648  if (!s[i] || s[i] == (char)c) {
3649  return (char *)s + i;
3650  }
3651  }
3652  return NULL;
3653 }

References c, i, NULL, and s.

Referenced by rz_str_highlight().

◆ strncmp_skip_color_codes()

static int strncmp_skip_color_codes ( const char *  s1,
const char *  s2,
int  n 
)
static

Definition at line 3605 of file str.c.

3605  {
3606  int i = 0, j = 0;
3607  int count = 0;
3608  for (i = 0, j = 0; s1[i] && s2[j] && count < n; i++, j++, count++) {
3609  while (s1[i] == 0x1b) {
3610  while (s1[i] && s1[i] != 'm') {
3611  i++;
3612  }
3613  if (s1[i]) {
3614  i++;
3615  }
3616  }
3617  while (s2[j] == 0x1b) {
3618  while (s2[j] && s2[j] != 'm') {
3619  j++;
3620  }
3621  if (s2[j]) {
3622  j++;
3623  }
3624  }
3625  if (s1[i] != s2[j]) {
3626  return -1;
3627  }
3628  }
3629 
3630  if (count < n && s1[i] != s2[j]) {
3631  return -1;
3632  }
3633 
3634  return 0;
3635 }

References count, i, n, s1, and s2.

Referenced by rz_str_highlight().

◆ strncpy_with_color_codes()

static int strncpy_with_color_codes ( char *  s1,
char *  s2,
int  n 
)
static

Definition at line 3582 of file str.c.

3582  {
3583  int i = 0, j = 0;
3584  int count = 0;
3585  while (s2[j] && count < n) {
3586  // detect (consecutive) color codes
3587  while (s2[j] == 0x1b) {
3588  // copy till 'm'
3589  while (s2[j] && s2[j] != 'm') {
3590  s1[i++] = s2[j++];
3591  }
3592  // copy 'm'
3593  if (s2[j]) {
3594  s1[i++] = s2[j++];
3595  }
3596  }
3597  if (s2[j]) {
3598  s1[i++] = s2[j++];
3599  count++;
3600  }
3601  }
3602  return i;
3603 }

References count, i, n, s1, and s2.

Referenced by rz_str_highlight().

◆ trimbits()

static void trimbits ( char *  b)
static

Definition at line 233 of file str.c.

233  {
234  const int len = strlen(b);
235  char *one = strchr(b, '1');
236  int pos = one ? (int)(size_t)(one - b) : len - 1;
237  pos = (pos / 8) * 8;
238  memmove(b, b + pos, len - pos + 1);
239 }

References b, int, len, and pos.

Referenced by rz_str_bits64().

◆ word_get_first()

static const char* word_get_first ( const char *  text)
static

Definition at line 629 of file str.c.

629  {
630  for (; *text && IS_SEPARATOR(*text); text++)
631  ;
632  ;
633 
634  return text;
635 }

References IS_SEPARATOR, and create_tags_rz::text.

Referenced by rz_str_word_count(), and rz_str_word_get_first().

Variable Documentation

◆ rwxstr

const char* rwxstr[]
static
Initial value:
= {
[0] = "---",
[1] = "--x",
[2] = "-w-",
[3] = "-wx",
[4] = "r--",
[5] = "r-x",
[6] = "rw-",
[7] = "rwx",
[8] = "---",
[9] = "--x",
[10] = "-w-",
[11] = "-wx",
[12] = "r--",
[13] = "r-x",
[14] = "rw-",
[15] = "rwx",
}

Definition at line 24 of file str.c.

Referenced by rz_str_rwx(), and rz_str_rwx_i().