Rizin
unix-like reverse engineering framework and cli tools
utils.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014 defragger <rlaemmert@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_types.h"
5 #include "rz_util.h"
6 #include "utils.h"
7 
8 // XXX: most of those functions are already implemented in rz_util. reuse!
9 
17  uint8_t sum = 0;
18  while (*command != '\0') {
19  sum += *command++;
20  }
21  return sum;
22 }
23 
27 uint64_t unpack_uint64(char *buff, int len) {
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 }
40 
45 uint64_t unpack_uint64_co(char *buff, int len) {
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 }
56 
61 int hex2int(int ch) {
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 }
73 
78 int int2hex(int i) {
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 }
87 
88 char hex2char(char *hex) {
89  uint8_t result = hex2int((int)hex[0]);
90  result <<= 4;
91  result |= hex2int(hex[1]);
92  return (char)result;
93 }
94 
95 int unpack_hex(const char *src, ut64 len, char *dst) {
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 }
110 
111 int pack_hex(const char *src, ut64 len, char *dst) {
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 }
122 
123 void hexdump(void *ptr, ut64 len, ut64 offset) {
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 }
143 
144 int write_thread_id(char *dest, int len, int pid, int tid, bool multiprocess) {
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 }
160 
161 int read_thread_id(const char *src, int *pid, int *tid, bool multiprocess) {
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
ut16 val
Definition: armass64_const.h:6
#define NULL
Definition: cris-opc.c:27
voidpf uLong offset
Definition: ioapi.h:144
snprintf
Definition: kernel.h:364
sprintf
Definition: kernel.h:365
void * p
Definition: libc.cpp:67
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
char * dst
Definition: lz4.h:724
char * dest
Definition: lz4.h:697
int x
Definition: mipsasm.c:20
static const char hex[16]
Definition: print.c:21
#define eprintf(x, y...)
Definition: rlcc.c:7
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 PFMT64x
Definition: rz_types.h:393
#define isxdigit(c)
Definition: safe-ctype.h:145
static int
Definition: sfsocketcall.h:114
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
#define c(i)
Definition: sha256.c:43
const char * command
Definition: main.c:7
uint64_t unpack_uint64(char *buff, int len)
Definition: utils.c:27
void hexdump(void *ptr, ut64 len, ut64 offset)
Definition: utils.c:123
int int2hex(int i)
Definition: utils.c:78
uint8_t cmd_checksum(const char *command)
Definition: utils.c:16
uint64_t unpack_uint64_co(char *buff, int len)
Definition: utils.c:45
int unpack_hex(const char *src, ut64 len, char *dst)
Definition: utils.c:95
char hex2char(char *hex)
Definition: utils.c:88
int write_thread_id(char *dest, int len, int pid, int tid, bool multiprocess)
Definition: utils.c:144
int read_thread_id(const char *src, int *pid, int *tid, bool multiprocess)
Definition: utils.c:161
int pack_hex(const char *src, ut64 len, char *dst)
Definition: utils.c:111
int hex2int(int ch)
Definition: utils.c:61
ut64(WINAPI *w32_GetEnabledXStateFeatures)()