Rizin
unix-like reverse engineering framework and cli tools
rz_big.h File Reference
#include "../rz_types.h"

Go to the source code of this file.

Classes

struct  rz_num_big_t
 

Macros

#define RZ_BIG_WORD_SIZE   4
 
#define RZ_BIG_ARRAY_SIZE   (512 / RZ_BIG_WORD_SIZE)
 
#define RZ_BIG_DTYPE   ut32
 
#define RZ_BIG_DTYPE_TMP   ut64
 
#define RZ_BIG_SPRINTF_FORMAT_STR   "%.08x"
 
#define RZ_BIG_FORMAT_STR_LEN   9
 
#define RZ_BIG_SSCANF_FORMAT_STR   "%8x"
 
#define RZ_BIG_MAX_VAL   (RZ_BIG_DTYPE_TMP) UT32_MAX
 

Typedefs

typedef struct rz_num_big_t RzNumBig
 

Functions

RZ_API RzNumBigrz_big_new (void)
 
RZ_API void rz_big_free (RzNumBig *b)
 
RZ_API void rz_big_init (RzNumBig *b)
 
RZ_API void rz_big_fini (RzNumBig *b)
 
RZ_API void rz_big_from_int (RzNumBig *b, st64 v)
 
RZ_API st64 rz_big_to_int (RzNumBig *b)
 
RZ_API void rz_big_from_hexstr (RzNumBig *b, const char *str)
 
RZ_API char * rz_big_to_hexstr (RzNumBig *b)
 
RZ_API void rz_big_assign (RzNumBig *dst, RzNumBig *src)
 
RZ_API void rz_big_add (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_sub (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_mul (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_div (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_mod (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_divmod (RzNumBig *c, RzNumBig *d, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_and (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_or (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_xor (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
RZ_API void rz_big_lshift (RzNumBig *c, RzNumBig *a, size_t nbits)
 
RZ_API void rz_big_rshift (RzNumBig *c, RzNumBig *a, size_t nbits)
 
RZ_API int rz_big_cmp (RzNumBig *a, RzNumBig *b)
 
RZ_API int rz_big_is_zero (RzNumBig *a)
 
RZ_API void rz_big_inc (RzNumBig *a)
 
RZ_API void rz_big_dec (RzNumBig *a)
 
RZ_API void rz_big_powm (RzNumBig *c, RzNumBig *a, RzNumBig *b, RzNumBig *m)
 
RZ_API void rz_big_isqrt (RzNumBig *c, RzNumBig *a)
 

Macro Definition Documentation

◆ RZ_BIG_ARRAY_SIZE

#define RZ_BIG_ARRAY_SIZE   (512 / RZ_BIG_WORD_SIZE)

Definition at line 19 of file rz_big.h.

◆ RZ_BIG_DTYPE

#define RZ_BIG_DTYPE   ut32

Definition at line 21 of file rz_big.h.

◆ RZ_BIG_DTYPE_TMP

#define RZ_BIG_DTYPE_TMP   ut64

Definition at line 22 of file rz_big.h.

◆ RZ_BIG_FORMAT_STR_LEN

#define RZ_BIG_FORMAT_STR_LEN   9

Definition at line 24 of file rz_big.h.

◆ RZ_BIG_MAX_VAL

#define RZ_BIG_MAX_VAL   (RZ_BIG_DTYPE_TMP) UT32_MAX

Definition at line 26 of file rz_big.h.

◆ RZ_BIG_SPRINTF_FORMAT_STR

#define RZ_BIG_SPRINTF_FORMAT_STR   "%.08x"

Definition at line 23 of file rz_big.h.

◆ RZ_BIG_SSCANF_FORMAT_STR

#define RZ_BIG_SSCANF_FORMAT_STR   "%8x"

Definition at line 25 of file rz_big.h.

◆ RZ_BIG_WORD_SIZE

#define RZ_BIG_WORD_SIZE   4

Definition at line 17 of file rz_big.h.

Typedef Documentation

◆ RzNumBig

typedef struct rz_num_big_t RzNumBig

Function Documentation

◆ rz_big_add()

RZ_API void rz_big_add ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 63 of file big-gmp.c.

63  {
64  mpz_add(*c, *a, *b);
65 }
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41

References a, b, c, rz_big_add_inner(), rz_big_sub_inner(), and rz_return_if_fail.

Referenced by rz_big_and(), rz_big_inc(), rz_big_isqrt(), rz_big_mul(), rz_big_or(), and rz_big_xor().

◆ rz_big_and()

RZ_API void rz_big_and ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

◆ rz_big_assign()

RZ_API void rz_big_assign ( RzNumBig dst,
RzNumBig src 
)

Definition at line 59 of file big-gmp.c.

59  {
60  mpz_set(*dst, *src);
61 }
lzma_index * src
Definition: index.h:567
char * dst
Definition: lz4.h:724

References dst, memcpy(), rz_return_if_fail, and src.

Referenced by rz_big_and(), rz_big_div(), rz_big_isqrt(), rz_big_lshift(), rz_big_mul(), rz_big_or(), rz_big_powm(), rz_big_rshift(), rz_big_to_int(), and rz_big_xor().

◆ rz_big_cmp()

RZ_API int rz_big_cmp ( RzNumBig a,
RzNumBig b 
)

Definition at line 107 of file big-gmp.c.

107  {
108  int res = mpz_cmp(*a, *b);
109  if (res > 0) {
110  return 1;
111  } else if (res < 0) {
112  return -1;
113  } else {
114  return 0;
115  }
116 }

References a, b, i, RZ_BIG_ARRAY_SIZE, and rz_return_val_if_fail.

Referenced by rz_big_div(), rz_big_isqrt(), and rz_big_sub_inner().

◆ rz_big_dec()

RZ_API void rz_big_dec ( RzNumBig a)

Definition at line 128 of file big-gmp.c.

128  {
129  RzNumBig tmp;
130  mpz_init_set_si(tmp, 1);
131  mpz_sub(*a, *a, tmp);
132 }

References a, rz_big_free(), rz_big_from_int(), rz_big_new(), rz_big_sub(), rz_return_if_fail, and autogen_x86imm::tmp.

Referenced by rz_big_isqrt().

◆ rz_big_div()

◆ rz_big_divmod()

RZ_API void rz_big_divmod ( RzNumBig c,
RzNumBig d,
RzNumBig a,
RzNumBig b 
)

Definition at line 83 of file big-gmp.c.

83  {
84  mpz_tdiv_qr(*c, *d, *a, *b);
85 }
#define d(i)
Definition: sha256.c:44

References a, b, c, d, rz_big_div(), rz_big_free(), rz_big_is_zero(), rz_big_mul(), rz_big_new(), rz_big_sub(), rz_return_if_fail, and autogen_x86imm::tmp.

Referenced by rz_big_mod().

◆ rz_big_fini()

RZ_API void rz_big_fini ( RzNumBig b)

Definition at line 24 of file big-gmp.c.

24  {
25  mpz_clear(*b);
26 }

References _r_big_zero_out(), and b.

◆ rz_big_free()

RZ_API void rz_big_free ( RzNumBig b)

Definition at line 15 of file big-gmp.c.

15  {
16  mpz_clear(*b);
17  free(b);
18 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References b, and free().

Referenced by rz_big_and(), rz_big_dec(), rz_big_div(), rz_big_divmod(), rz_big_inc(), rz_big_isqrt(), rz_big_mod(), rz_big_mul(), rz_big_or(), rz_big_powm(), rz_big_to_int(), and rz_big_xor().

◆ rz_big_from_hexstr()

RZ_API void rz_big_from_hexstr ( RzNumBig b,
const char *  str 
)

Definition at line 36 of file big-gmp.c.

36  {
37  if (rz_str_startswith(str, "0x")) {
38  str += 2;
39  mpz_set_str(*b, str, 16);
40  } else if (rz_str_startswith(str, "-0x")) {
41  str += 3;
42  mpz_set_str(*b, str, 16);
43  mpz_mul_si(*b, *b, -1);
44  }
45 }
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

References _r_big_zero_out(), b, i, memset(), n, nbytes, RZ_BIG_DTYPE, RZ_BIG_SSCANF_FORMAT_STR, RZ_BIG_WORD_SIZE, rz_return_if_fail, rz_str_startswith(), cmd_descs_generate::str, and autogen_x86imm::tmp.

◆ rz_big_from_int()

RZ_API void rz_big_from_int ( RzNumBig b,
st64  v 
)

Definition at line 28 of file big-gmp.c.

28  {
29  mpz_set_si(*b, v);
30 }
const char * v
Definition: dsignal.c:12

References _r_big_zero_out(), b, n, RZ_BIG_DTYPE_TMP, rz_return_if_fail, autogen_x86imm::tmp, and v.

Referenced by rz_big_and(), rz_big_dec(), rz_big_div(), rz_big_inc(), rz_big_or(), rz_big_powm(), and rz_big_xor().

◆ rz_big_inc()

RZ_API void rz_big_inc ( RzNumBig a)

Definition at line 122 of file big-gmp.c.

122  {
123  RzNumBig tmp;
124  mpz_init_set_si(tmp, 1);
125  mpz_add(*a, *a, tmp);
126 }

References a, rz_big_add(), rz_big_free(), rz_big_from_int(), rz_big_new(), rz_return_if_fail, and autogen_x86imm::tmp.

Referenced by rz_big_isqrt().

◆ rz_big_init()

RZ_API void rz_big_init ( RzNumBig b)

Definition at line 20 of file big-gmp.c.

20  {
21  mpz_init(*b);
22 }

References _r_big_zero_out(), and b.

◆ rz_big_is_zero()

RZ_API int rz_big_is_zero ( RzNumBig a)

Definition at line 118 of file big-gmp.c.

118  {
119  return mpz_cmp_ui(*a, 0) == 0;
120 }

References a, i, RZ_BIG_ARRAY_SIZE, and rz_return_val_if_fail.

Referenced by rz_big_and(), rz_big_div(), rz_big_divmod(), rz_big_mod(), rz_big_mul(), rz_big_or(), rz_big_powm(), and rz_big_xor().

◆ rz_big_isqrt()

RZ_API void rz_big_isqrt ( RzNumBig c,
RzNumBig a 
)

◆ rz_big_lshift()

RZ_API void rz_big_lshift ( RzNumBig c,
RzNumBig a,
size_t  nbits 
)

Definition at line 99 of file big-gmp.c.

99  {
100  mpz_mul_2exp(*c, *a, nbits);
101 }

References _lshift_word(), a, b, c, i, RZ_BIG_ARRAY_SIZE, rz_big_assign(), RZ_BIG_WORD_SIZE, and rz_return_if_fail.

Referenced by rz_big_and(), rz_big_or(), and rz_big_xor().

◆ rz_big_mod()

RZ_API void rz_big_mod ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 79 of file big-gmp.c.

79  {
80  mpz_mod(*c, *a, *b);
81 }

References a, b, c, rz_big_divmod(), rz_big_free(), rz_big_is_zero(), rz_big_new(), rz_return_if_fail, and autogen_x86imm::tmp.

Referenced by rz_big_powm().

◆ rz_big_mul()

◆ rz_big_new()

RZ_API RzNumBig* rz_big_new ( void  )

Definition at line 7 of file big-gmp.c.

7  {
9  if (n) {
10  mpz_init(*n);
11  }
12  return n;
13 }
int n
Definition: mipsasm.c:19
#define RZ_NEW(x)
Definition: rz_types.h:285

References _r_big_zero_out(), n, and RZ_NEW.

Referenced by rz_big_and(), rz_big_dec(), rz_big_div(), rz_big_divmod(), rz_big_inc(), rz_big_isqrt(), rz_big_mod(), rz_big_mul(), rz_big_or(), rz_big_powm(), rz_big_to_int(), and rz_big_xor().

◆ rz_big_or()

RZ_API void rz_big_or ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

◆ rz_big_powm()

RZ_API void rz_big_powm ( RzNumBig c,
RzNumBig a,
RzNumBig b,
RzNumBig m 
)

◆ rz_big_rshift()

RZ_API void rz_big_rshift ( RzNumBig c,
RzNumBig a,
size_t  nbits 
)

Definition at line 103 of file big-gmp.c.

103  {
104  mpz_tdiv_q_2exp(*c, *a, nbits);
105 }

References _rshift_word(), a, b, c, i, RZ_BIG_ARRAY_SIZE, rz_big_assign(), RZ_BIG_WORD_SIZE, and rz_return_if_fail.

Referenced by rz_big_and(), rz_big_isqrt(), rz_big_or(), and rz_big_xor().

◆ rz_big_sub()

RZ_API void rz_big_sub ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 67 of file big-gmp.c.

67  {
68  mpz_sub(*c, *a, *b);
69 }

References a, b, c, rz_big_add_inner(), rz_big_sub_inner(), and rz_return_if_fail.

Referenced by rz_big_dec(), rz_big_div(), rz_big_divmod(), and rz_big_isqrt().

◆ rz_big_to_hexstr()

RZ_API char* rz_big_to_hexstr ( RzNumBig b)

Definition at line 47 of file big-gmp.c.

47  {
48  char *tmp = mpz_get_str(NULL, 16, *b);
49  char *res;
50  if (tmp[0] == '-') {
51  res = rz_str_newf("-0x%s", &tmp[1]);
52  } else {
53  res = rz_str_newf("0x%s", tmp);
54  }
55  free(tmp);
56  return res;
57 }
#define NULL
Definition: cris-opc.c:27
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1

References b, calloc(), free(), i, k, NULL, RZ_BIG_ARRAY_SIZE, RZ_BIG_FORMAT_STR_LEN, RZ_BIG_SPRINTF_FORMAT_STR, RZ_BIG_WORD_SIZE, rz_return_val_if_fail, rz_snprintf(), rz_str_newf(), autogen_x86imm::tmp, and tolower.

◆ rz_big_to_int()

RZ_API st64 rz_big_to_int ( RzNumBig b)

Definition at line 32 of file big-gmp.c.

32  {
33  return mpz_get_si(*b);
34 }

References b, B, rz_big_assign(), RZ_BIG_DTYPE_TMP, rz_big_free(), rz_big_new(), and rz_return_val_if_fail.

Referenced by rz_big_and(), rz_big_or(), rz_big_powm(), and rz_big_xor().

◆ rz_big_xor()

RZ_API void rz_big_xor ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)