Rizin
unix-like reverse engineering framework and cli tools
utils.c File Reference
#include "rz_types.h"
#include "rz_util.h"
#include "utils.h"

Go to the source code of this file.

Functions

uint8_t cmd_checksum (const char *command)
 
uint64_t unpack_uint64 (char *buff, int len)
 
uint64_t unpack_uint64_co (char *buff, int len)
 
int hex2int (int ch)
 
int int2hex (int i)
 
char hex2char (char *hex)
 
int unpack_hex (const char *src, ut64 len, char *dst)
 
int pack_hex (const char *src, ut64 len, char *dst)
 
void hexdump (void *ptr, ut64 len, ut64 offset)
 
int write_thread_id (char *dest, int len, int pid, int tid, bool multiprocess)
 
int read_thread_id (const char *src, int *pid, int *tid, bool multiprocess)
 

Function Documentation

◆ cmd_checksum()

uint8_t cmd_checksum ( const char *  command)

Function creates the checksum for the given command

  • command : is used to calculate the checksum needs to be null terminated
    Returns
    : calculated checksum

Definition at line 16 of file utils.c.

16  {
17  uint8_t sum = 0;
18  while (*command != '\0') {
19  sum += *command++;
20  }
21  return sum;
22 }
unsigned char uint8_t
Definition: sftypes.h:31
const char * command
Definition: main.c:7

References command.

Referenced by pack().

◆ hex2char()

char hex2char ( char *  hex)

Definition at line 88 of file utils.c.

88  {
89  uint8_t result = hex2int((int)hex[0]);
90  result <<= 4;
91  result |= hex2int(hex[1]);
92  return (char)result;
93 }
static const char hex[16]
Definition: print.c:21
int hex2int(int ch)
Definition: utils.c:61

References hex, and hex2int().

Referenced by gdbr_write_registers().

◆ hex2int()

int hex2int ( int  ch)

Converts a given hex character into its int value

Returns
value of hex or -1 on error

Definition at line 61 of file utils.c.

61  {
62  if (ch >= 'a' && ch <= 'f') {
63  return ch - 'a' + 10;
64  }
65  if (ch >= 'A' && ch <= 'F') {
66  return ch - 'A' + 10;
67  }
68  if (ch >= '0' && ch <= '9') {
69  return ch - '0';
70  }
71  return -1;
72 }

Referenced by hex2char(), unpack(), unpack_hex(), and unpack_uint64().

◆ hexdump()

void hexdump ( void *  ptr,
ut64  len,
ut64  offset 
)

Definition at line 123 of file utils.c.

123  {
124  unsigned char *data = (unsigned char *)ptr;
125  int x = 0;
126  char hex[49], *p;
127  char txt[17], *c;
128  ut64 curr_offset;
129  while (x < len) {
130  p = hex;
131  c = txt;
132  curr_offset = x + offset;
133 
134  do {
135  p += sprintf(p, "%02x ", data[x]);
136  *c++ = (data[x] >= 32 && data[x] <= 127) ? data[x] : '.';
137  } while (++x % 16 && x < len);
138 
139  *c = '\0';
140  eprintf("0x%016" PFMT64x ": %-48s- %s\n", (curr_offset), hex, txt);
141  }
142 }
size_t len
Definition: 6502dis.c:15
voidpf uLong offset
Definition: ioapi.h:144
sprintf
Definition: kernel.h:365
void * p
Definition: libc.cpp:67
int x
Definition: mipsasm.c:20
#define eprintf(x, y...)
Definition: rlcc.c:7
#define PFMT64x
Definition: rz_types.h:393
#define c(i)
Definition: sha256.c:43
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References c, eprintf, hex, len, p, PFMT64x, sprintf, ut64(), and x.

◆ int2hex()

int int2hex ( int  i)

Converts a given nibble (4bit) into its hex representation

Returns
hex char or -1 on error

Definition at line 78 of file utils.c.

78  {
79  if (i >= 0 && i <= 9) {
80  return i + 48;
81  }
82  if (i >= 10 && i <= 15) {
83  return i + 87;
84  }
85  return -1;
86 }
lzma_index ** i
Definition: index.h:629

References i.

Referenced by pack_hex().

◆ pack_hex()

int pack_hex ( const char *  src,
ut64  len,
char *  dst 
)

Definition at line 111 of file utils.c.

111  {
112  int i = 0;
113  int x = 0;
114  while (i < (len * 2)) {
115  int val = (src[x] & 0xf0) >> 4;
116  dst[i++] = int2hex(val);
117  dst[i++] = int2hex(src[x++] & 0x0f);
118  }
119  dst[i] = '\0';
120  return (len / 2);
121 }
lzma_index * src
Definition: index.h:567
ut16 val
Definition: armass64_const.h:6
char * dst
Definition: lz4.h:724
int int2hex(int i)
Definition: utils.c:78

References dst, i, int2hex(), len, src, val, and x.

Referenced by _server_handle_m(), gdbr_open_file(), gdbr_send_qRcmd(), gdbr_write_bin_registers(), gdbr_write_memory(), gdbr_write_register(), and gdbr_write_registers().

◆ read_thread_id()

int read_thread_id ( const char *  src,
int pid,
int tid,
bool  multiprocess 
)

Definition at line 161 of file utils.c.

161  {
162  char *ptr1;
163  if (multiprocess && *src == 'p') {
164  src++;
165  if (!(ptr1 = strchr(src, '.'))) {
166  return -1;
167  }
168  ptr1++;
169  if (rz_str_startswith(src, "-1")) {
170  if (rz_str_startswith(ptr1, "-1")) {
171  *pid = *tid = -1;
172  return 0;
173  }
174  return -1;
175  }
176  if (!isxdigit(*src)) {
177  return -1;
178  }
179  if (rz_str_startswith(ptr1, "-1")) {
180  *pid = (int)strtol(src, NULL, 16);
181  *tid = -1;
182  return 0;
183  }
184  if (!isxdigit(*ptr1)) {
185  return -1;
186  }
187  *pid = (int)strtol(src, NULL, 16);
188  *tid = (int)strtol(ptr1, NULL, 16);
189  return 0;
190  }
191  if (rz_str_startswith(src, "-1")) {
192  *tid = -1;
193  return 0;
194  }
195  if (!isxdigit(*src)) {
196  return -1;
197  }
198  *pid = *tid = (int)strtol(src, NULL, 16);
199  return 0;
200 }
#define NULL
Definition: cris-opc.c:27
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 pid
Definition: sflib.h:64
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)
Definition: str.c:3286
#define isxdigit(c)
Definition: safe-ctype.h:145
static int
Definition: sfsocketcall.h:114

References int, isxdigit, NULL, pid, rz_str_startswith(), and src.

Referenced by gdbr_pids_list(), gdbr_threads_list(), handle_qC(), and handle_stop_reason().

◆ unpack_hex()

int unpack_hex ( const char *  src,
ut64  len,
char *  dst 
)

Definition at line 95 of file utils.c.

95  {
96  int i = 0;
97  while (i < (len / 2)) {
98  int val = hex2int(src[(i * 2)]);
99  if (val > 0) {
100  val <<= 4;
101  } else {
102  val = 0;
103  }
104  val |= hex2int(src[(i * 2) + 1]);
105  dst[i++] = val;
106  }
107  dst[i] = '\0';
108  return len;
109 }

References dst, hex2int(), i, len, src, and val.

Referenced by gdbr_send_qRcmd(), handle_g(), handle_lldb_read_reg(), handle_m(), and handle_stop_reason().

◆ unpack_uint64()

uint64_t unpack_uint64 ( char *  buff,
int  len 
)

Converts str to uint64_t

Definition at line 27 of file utils.c.

27  {
28  int nibble;
29  uint64_t retval = 0;
30  while (len) {
31  nibble = hex2int(*buff++);
32  retval |= nibble;
33  len--;
34  if (len) {
35  retval = retval << 4;
36  }
37  }
38  return retval;
39 }
unsigned long uint64_t
Definition: sftypes.h:28

References hex2int(), and len.

Referenced by unpack_uint64_co().

◆ unpack_uint64_co()

uint64_t unpack_uint64_co ( char *  buff,
int  len 
)

Changed byte order and converts the value into uint64_t

Definition at line 45 of file utils.c.

45  {
46  uint64_t result = 0;
47  int i;
48  for (i = len - 2; i >= 0; i -= 2) {
49  result |= unpack_uint64(&buff[i], 2);
50  if (i) {
51  result <<= 8;
52  }
53  }
54  return result;
55 }
uint64_t unpack_uint64(char *buff, int len)
Definition: utils.c:27

References i, len, and unpack_uint64().

◆ write_thread_id()

int write_thread_id ( char *  dest,
int  len,
int  pid,
int  tid,
bool  multiprocess 
)

Definition at line 144 of file utils.c.

144  {
145  if (!multiprocess) {
146  if (tid < 0) {
147  strncpy(dest, "-1", len);
148  return 0;
149  }
150  return snprintf(dest, len, "%x", tid);
151  }
152  if (pid <= 0) {
153  return -1;
154  }
155  if (tid < 0) {
156  return snprintf(dest, len, "p%x.-1", pid);
157  }
158  return snprintf(dest, len, "p%x.%x", pid, tid);
159 }
snprintf
Definition: kernel.h:364
char * dest
Definition: lz4.h:697

References dest, len, pid, and snprintf.

Referenced by gdbr_continue(), gdbr_is_thread_dead(), gdbr_select(), and gdbr_step().