Rizin
unix-like reverse engineering framework and cli tools
rz_num.h
Go to the documentation of this file.
1 #ifndef RZ_NUM_H
2 #define RZ_NUM_H
3 
4 #include <rz_list.h>
5 
6 #define RZ_NUMCALC_STRSZ 1024
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 typedef struct {
13  double d;
16 
17 typedef enum {
23  RNCLT, // comparison operator <
24  RNCGT, // comparison operator >
25  RNCPLUS = '+',
26  RNCMINUS = '-',
27  RNCMUL = '*',
28  RNCEXP = 'E',
29  RNCDIV = '/',
30  RNCMOD = '%',
31  // RNCXOR='^', RNCOR='|', RNCAND='&',
32  RNCNEG = '~',
33  RNCAND = '&',
34  RNCORR = '|',
35  RNCXOR = '^',
36  RNCPRINT = ';',
37  RNCASSIGN = '=',
38  RNCLEFTP = '(',
39  RNCRIGHTP = ')',
40  RNCSHL = '<',
41  RNCSHR = '>',
42  RNCROL = '#',
43  RNCROR = '$'
45 
46 typedef struct rz_num_calc_t {
50  int errors;
51  char oc;
52  const char *calc_err;
53  int calc_i;
54  const char *calc_buf;
55  int calc_len;
56  bool under_calc;
58 
59 typedef struct rz_num_t {
60  ut64 (*callback)(struct rz_num_t *userptr, const char *str, int *ok);
61  const char *(*cb_from_value)(struct rz_num_t *userptr, ut64 value, int *ok);
62  // RzNumCallback callback;
64  double fvalue;
65  void *userptr;
66  int dbz;
69 
70 typedef ut64 (*RzNumCallback)(struct rz_num_t *self, const char *str, int *ok);
71 typedef const char *(*RzNumCallback2)(struct rz_num_t *self, ut64, int *ok);
72 
75 RZ_API char *rz_num_units(char *buf, size_t len, ut64 number);
76 RZ_API int rz_num_conditional(RzNum *num, const char *str);
77 RZ_API ut64 rz_num_calc(RzNum *num, const char *str, const char **err);
78 RZ_API const char *rz_num_calc_index(RzNum *num, const char *p);
79 RZ_API ut64 rz_num_chs(int cylinder, int head, int sector, int sectorsize);
80 RZ_API int rz_num_is_valid_input(RzNum *num, const char *input_value);
81 RZ_API ut64 rz_num_get_input_value(RzNum *num, const char *input_value);
82 RZ_API const char *rz_num_get_name(RzNum *num, ut64 n);
83 RZ_API char *rz_num_as_string(RzNum *___, ut64 n, bool printable_only);
84 RZ_API ut64 rz_num_tail(RzNum *num, ut64 addr, const char *hex);
87 RZ_API void rz_num_minmax_swap_i(int *a, int *b); // XXX this can be a cpp macro :??
88 RZ_API ut64 rz_num_math(RzNum *num, const char *str);
89 RZ_API ut64 rz_num_get(RzNum *num, const char *str);
90 RZ_API int rz_num_to_bits(char *out, ut64 num);
91 RZ_API int rz_num_to_trits(char *out, ut64 num); // Rename this please
92 RZ_API int rz_num_rand(int max);
93 RZ_API void rz_num_irand(void);
94 RZ_API ut64 rz_get_input_num_value(RzNum *num, const char *input_value);
95 RZ_API bool rz_is_valid_input_num_value(RzNum *num, const char *input_value);
96 RZ_API int rz_num_between(RzNum *num, const char *input_value);
97 RZ_API bool rz_num_is_op(const char c);
98 RZ_API int rz_num_str_len(const char *str);
99 RZ_API int rz_num_str_split(char *str);
100 RZ_API RzList /*<char *>*/ *rz_num_str_split_list(char *str);
101 RZ_API void *rz_num_dup(ut64 n);
102 RZ_API size_t rz_num_base_of_string(RzNum *num, RZ_NONNULL const char *str);
103 RZ_API double rz_num_cos(double a);
104 RZ_API double rz_num_sin(double a);
105 RZ_API double rz_num_get_float(RzNum *num, const char *str);
106 RZ_API bool rz_num_is_hex_prefix(const char *p);
107 
108 static inline st64 rz_num_abs(st64 num) {
109  return num < 0 ? -num : num;
110 }
111 
116 static inline ut64 rz_num_align_delta(ut64 v, ut64 alignment) {
117  if (!alignment) {
118  return 0;
119  }
120  ut64 excess = v % alignment;
121  if (!excess) {
122  return 0;
123  }
124  return alignment - excess;
125 }
126 
134 static inline ut64 rz_num_bitmask(ut8 width) {
135  if (width >= 64) {
136  return 0xffffffffffffffffull;
137  }
138  return (1ull << (ut64)width) - 1;
139 }
140 
141 #define CONVERT_TO_TWO_COMPLEMENT(x) \
142  static inline st##x convert_to_two_complement_##x(ut##x value) { \
143  if (value <= ST##x##_MAX) { \
144  return (st##x)value; \
145  } \
146 \
147  value = ~value + 1; \
148  return -(st##x)value; \
149  }
150 
155 
157 #define RZ_NUM_CMP(a, b) ((a) > (b) ? 1 : ((b) > (a) ? -1 : 0))
158 
167 static inline ut64 rz_num_2_pow_64_div(ut64 divisor) {
168  return (-(st64)divisor) / divisor + 1;
169 }
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif // RZ_NUM_H
size_t len
Definition: 6502dis.c:15
static bool err
Definition: armass.c:435
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
const char * v
Definition: dsignal.c:12
int max
Definition: enough.c:225
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
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 static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
@ ok
Definition: lz4.c:1706
int n
Definition: mipsasm.c:19
int off
Definition: pal.c:13
static const char hex[16]
Definition: print.c:21
RZ_API RzList * rz_num_str_split_list(char *str)
Definition: unum.c:905
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
Definition: unum.c:172
struct rz_num_calc_t RzNumCalc
RZ_API void rz_num_minmax_swap(ut64 *a, ut64 *b)
Definition: unum.c:59
static st64 rz_num_abs(st64 num)
Definition: rz_num.h:108
static ut64 rz_num_2_pow_64_div(ut64 divisor)
Definition: rz_num.h:167
RZ_API bool rz_num_is_op(const char c)
Definition: unum.c:848
RZ_API ut64 rz_num_tail(RzNum *num, ut64 addr, const char *hex)
Definition: unum.c:775
static ut64 rz_num_align_delta(ut64 v, ut64 alignment)
Padding to align v to the next alignment-boundary.
Definition: rz_num.h:116
ut64(* RzNumCallback)(struct rz_num_t *self, const char *str, int *ok)
Definition: rz_num.h:70
RZ_API bool rz_is_valid_input_num_value(RzNum *num, const char *input_value)
Definition: unum.c:735
RZ_API double rz_num_get_float(RzNum *num, const char *str)
Definition: unum.c:536
RZ_API int rz_num_str_len(const char *str)
Definition: unum.c:854
RZ_API RzNum * rz_num_new(RzNumCallback cb, RzNumCallback2 cb2, void *ptr)
Definition: unum.c:75
RZ_API void rz_num_minmax_swap_i(int *a, int *b)
Definition: unum.c:67
RZ_API ut64 rz_num_tail_base(RzNum *num, ut64 addr, ut64 off)
Definition: unum.c:753
#define RZ_NUMCALC_STRSZ
Definition: rz_num.h:6
#define CONVERT_TO_TWO_COMPLEMENT(x)
Definition: rz_num.h:141
RZ_API int rz_num_to_bits(char *out, ut64 num)
Definition: unum.c:542
struct rz_num_t RzNum
static ut64 rz_num_bitmask(ut8 width)
Get the 64-bit value that has exactly its width lowest bits set to 1. e.g. rz_num_bitmask(2) == 0b11 ...
Definition: rz_num.h:134
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
RZ_API int rz_num_is_valid_input(RzNum *num, const char *input_value)
Definition: unum.c:676
RZ_API int rz_num_conditional(RzNum *num, const char *str)
Definition: unum.c:603
RZ_API int rz_num_to_trits(char *out, ut64 num)
Definition: unum.c:578
RzNumCalcToken
Definition: rz_num.h:17
@ RNCNEG
Definition: rz_num.h:32
@ RNCORR
Definition: rz_num.h:34
@ RNCROR
Definition: rz_num.h:43
@ RNCXOR
Definition: rz_num.h:35
@ RNCLT
Definition: rz_num.h:23
@ RNCAND
Definition: rz_num.h:33
@ RNCMINUS
Definition: rz_num.h:26
@ RNCNUMBER
Definition: rz_num.h:19
@ RNCRIGHTP
Definition: rz_num.h:39
@ RNCMOD
Definition: rz_num.h:30
@ RNCDEC
Definition: rz_num.h:22
@ RNCEXP
Definition: rz_num.h:28
@ RNCINC
Definition: rz_num.h:21
@ RNCPLUS
Definition: rz_num.h:25
@ RNCGT
Definition: rz_num.h:24
@ RNCLEFTP
Definition: rz_num.h:38
@ RNCMUL
Definition: rz_num.h:27
@ RNCROL
Definition: rz_num.h:42
@ RNCSHR
Definition: rz_num.h:41
@ RNCASSIGN
Definition: rz_num.h:37
@ RNCPRINT
Definition: rz_num.h:36
@ RNCDIV
Definition: rz_num.h:29
@ RNCEND
Definition: rz_num.h:20
@ RNCSHL
Definition: rz_num.h:40
@ RNCNAME
Definition: rz_num.h:18
RZ_API int rz_num_str_split(char *str)
Definition: unum.c:893
RZ_API double rz_num_cos(double a)
Definition: unum.c:924
RZ_API ut64 rz_num_get_input_value(RzNum *num, const char *input_value)
Definition: unum.c:681
RZ_API void * rz_num_dup(ut64 n)
Definition: unum.c:915
RZ_API char * rz_num_units(char *buf, size_t len, ut64 number)
Definition: unum.c:108
RZ_API int rz_num_between(RzNum *num, const char *input_value)
Definition: unum.c:826
RZ_API size_t rz_num_base_of_string(RzNum *num, RZ_NONNULL const char *str)
Convert the base suffix to the numeric value.
Definition: unum.c:935
RZ_API char * rz_num_as_string(RzNum *___, ut64 n, bool printable_only)
Definition: unum.c:705
RZ_API const char * rz_num_calc_index(RzNum *num, const char *p)
Definition: calc.c:288
RZ_API ut64 rz_num_calc(RzNum *num, const char *str, const char **err)
Definition: calc.c:482
RZ_API int rz_num_rand(int max)
Definition: unum.c:47
RZ_API ut64 rz_get_input_num_value(RzNum *num, const char *input_value)
Definition: unum.c:743
RZ_API void rz_num_free(RzNum *num)
Definition: unum.c:87
RZ_API void rz_num_irand(void)
Definition: unum.c:43
RZ_API double rz_num_sin(double a)
Definition: unum.c:928
const char *(* RzNumCallback2)(struct rz_num_t *self, ut64, int *ok)
Definition: rz_num.h:71
RZ_API const char * rz_num_get_name(RzNum *num, ut64 n)
Definition: unum.c:147
RZ_API bool rz_num_is_hex_prefix(const char *p)
Checks if the first two chars of p equal "0x".
Definition: unum.c:17
RZ_API ut64 rz_num_chs(int cylinder, int head, int sector, int sectorsize)
Definition: unum.c:596
#define RZ_NONNULL
Definition: rz_types.h:64
#define st64
Definition: rz_types_base.h:10
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
double d
Definition: rz_num.h:13
bool under_calc
Definition: rz_num.h:56
RzNumCalcValue number_value
Definition: rz_num.h:48
const char * calc_buf
Definition: rz_num.h:54
RzNumCalcToken curr_tok
Definition: rz_num.h:47
int calc_i
Definition: rz_num.h:53
int calc_len
Definition: rz_num.h:55
char string_value[RZ_NUMCALC_STRSZ]
Definition: rz_num.h:49
const char * calc_err
Definition: rz_num.h:52
int errors
Definition: rz_num.h:50
char oc
Definition: rz_num.h:51
ut64(* callback)(struct rz_num_t *userptr, const char *str, int *ok)
Definition: rz_num.h:60
double fvalue
Definition: rz_num.h:64
RzNumCalc nc
division by zero happened
Definition: rz_num.h:67
ut64 value
Definition: rz_num.h:63
int dbz
Definition: rz_num.h:66
void * userptr
Definition: rz_num.h:65
int width
Definition: main.c:10
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const char * cb[]
Definition: z80_tab.h:176
static int addr
Definition: z80asm.c:58