Rizin
unix-like reverse engineering framework and cli tools
big.c File Reference
#include <rz_util.h>

Go to the source code of this file.

Functions

static void _lshift_one_bit (RzNumBig *a)
 
static void _rshift_one_bit (RzNumBig *a)
 
static void _lshift_word (RzNumBig *a, int nwords)
 
static void _rshift_word (RzNumBig *a, int nwords)
 
static void _r_big_zero_out (RzNumBig *n)
 
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 n)
 
static void rz_big_from_unsigned (RzNumBig *b, ut64 v)
 
RZ_API st64 rz_big_to_int (RzNumBig *b)
 
RZ_API void rz_big_from_hexstr (RzNumBig *n, const char *str)
 
RZ_API char * rz_big_to_hexstr (RzNumBig *b)
 
RZ_API void rz_big_assign (RzNumBig *dst, RzNumBig *src)
 
static void rz_big_add_inner (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
static void rz_big_sub_inner (RzNumBig *c, RzNumBig *a, RzNumBig *b)
 
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 *b, RzNumBig *a, size_t nbits)
 
RZ_API void rz_big_rshift (RzNumBig *b, 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 *b, RzNumBig *a)
 

Function Documentation

◆ _lshift_one_bit()

static void _lshift_one_bit ( RzNumBig a)
static

Definition at line 664 of file big.c.

664  {
666 
667  int i;
668  for (i = (RZ_BIG_ARRAY_SIZE - 1); i > 0; i--) {
669  a->array[i] = (a->array[i] << 1) | (a->array[i - 1] >> ((8 * RZ_BIG_WORD_SIZE) - 1));
670  }
671  a->array[0] <<= 1;
672 }
lzma_index ** i
Definition: index.h:629
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define RZ_BIG_ARRAY_SIZE
Definition: rz_big.h:19
#define RZ_BIG_WORD_SIZE
Definition: rz_big.h:17
#define a(i)
Definition: sha256.c:41

References a, i, RZ_BIG_ARRAY_SIZE, RZ_BIG_WORD_SIZE, and rz_return_if_fail.

Referenced by rz_big_div().

◆ _lshift_word()

static void _lshift_word ( RzNumBig a,
int  nwords 
)
static

Definition at line 649 of file big.c.

649  {
651  rz_return_if_fail(nwords >= 0);
652 
653  int i;
654  /* Shift whole words */
655  for (i = (RZ_BIG_ARRAY_SIZE - 1); i >= nwords; i--) {
656  a->array[i] = a->array[i - nwords];
657  }
658  /* Zero pad shifted words. */
659  for (; i >= 0; i--) {
660  a->array[i] = 0;
661  }
662 }

References a, i, RZ_BIG_ARRAY_SIZE, and rz_return_if_fail.

Referenced by rz_big_lshift(), and rz_big_mul().

◆ _r_big_zero_out()

static void _r_big_zero_out ( RzNumBig n)
static

Definition at line 684 of file big.c.

684  {
686 
687  size_t i;
688  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
689  a->array[i] = 0;
690  }
691  a->sign = 1; /* hack to avoid -0 */
692 }

References a, i, RZ_BIG_ARRAY_SIZE, and rz_return_if_fail.

Referenced by rz_big_div(), rz_big_fini(), rz_big_from_hexstr(), rz_big_from_int(), rz_big_from_unsigned(), rz_big_init(), rz_big_mul(), and rz_big_new().

◆ _rshift_one_bit()

static void _rshift_one_bit ( RzNumBig a)
static

Definition at line 674 of file big.c.

674  {
676 
677  int i;
678  for (i = 0; i < (RZ_BIG_ARRAY_SIZE - 1); i++) {
679  a->array[i] = (a->array[i] >> 1) | (a->array[i + 1] << ((8 * RZ_BIG_WORD_SIZE) - 1));
680  }
681  a->array[RZ_BIG_ARRAY_SIZE - 1] >>= 1;
682 }

References a, i, RZ_BIG_ARRAY_SIZE, RZ_BIG_WORD_SIZE, and rz_return_if_fail.

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

◆ _rshift_word()

static void _rshift_word ( RzNumBig a,
int  nwords 
)
static

Definition at line 628 of file big.c.

628  {
629  /* Naive method: */
631  rz_return_if_fail(nwords >= 0);
632 
633  size_t i;
634  if (nwords >= RZ_BIG_ARRAY_SIZE) {
635  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
636  a->array[i] = 0;
637  }
638  return;
639  }
640 
641  for (i = 0; i < RZ_BIG_ARRAY_SIZE - nwords; i++) {
642  a->array[i] = a->array[i + nwords];
643  }
644  for (; i < RZ_BIG_ARRAY_SIZE; i++) {
645  a->array[i] = 0;
646  }
647 }

References a, i, RZ_BIG_ARRAY_SIZE, and rz_return_if_fail.

Referenced by rz_big_rshift().

◆ rz_big_add()

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

Definition at line 242 of file big.c.

242  {
246 
247  if (a->sign >= 0 && b->sign >= 0) {
248  rz_big_add_inner(c, a, b);
249  c->sign = 1;
250  return;
251  }
252  if (a->sign >= 0 && b->sign < 0) {
253  rz_big_sub_inner(c, a, b);
254  return;
255  }
256  if (a->sign < 0 && b->sign >= 0) {
257  rz_big_sub_inner(c, b, a);
258  return;
259  }
260  if (a->sign < 0 && b->sign < 0) {
261  rz_big_add_inner(c, a, b);
262  c->sign = -1;
263  return;
264  }
265 }
static void rz_big_sub_inner(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:218
static void rz_big_add_inner(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:207
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43

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

Referenced by rz_big_inc(), rz_big_isqrt(), and rz_big_mul().

◆ rz_big_add_inner()

static void rz_big_add_inner ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)
static

Definition at line 207 of file big.c.

207  {
209  int carry = 0;
210  int i;
211  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
212  tmp = (RZ_BIG_DTYPE_TMP)a->array[i] + b->array[i] + carry;
213  carry = (tmp > RZ_BIG_MAX_VAL);
214  c->array[i] = (tmp & RZ_BIG_MAX_VAL);
215  }
216 }
#define RZ_BIG_MAX_VAL
Definition: rz_big.h:26
#define RZ_BIG_DTYPE_TMP
Definition: rz_big.h:22

References a, b, c, i, RZ_BIG_ARRAY_SIZE, RZ_BIG_DTYPE_TMP, RZ_BIG_MAX_VAL, and autogen_x86imm::tmp.

Referenced by rz_big_add(), and rz_big_sub().

◆ rz_big_and()

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

Definition at line 424 of file big.c.

424  {
428  rz_return_if_fail(a->sign > 0);
429  rz_return_if_fail(b->sign > 0);
430 
431  int i;
432  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
433  c->array[i] = (a->array[i] & b->array[i]);
434  }
435 }

References a, b, c, i, RZ_BIG_ARRAY_SIZE, and rz_return_if_fail.

◆ rz_big_assign()

RZ_API void rz_big_assign ( RzNumBig dst,
RzNumBig src 
)

Definition at line 200 of file big.c.

200  {
203 
204  memcpy(dst, src, sizeof(RzNumBig));
205 }
lzma_index * src
Definition: index.h:567
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
char * dst
Definition: lz4.h:724

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

Referenced by rz_big_div(), rz_big_isqrt(), rz_big_lshift(), rz_big_mul(), rz_big_powm(), and rz_big_rshift().

◆ rz_big_cmp()

RZ_API int rz_big_cmp ( RzNumBig a,
RzNumBig b 
)

Definition at line 511 of file big.c.

511  {
514 
515  if (a->sign != b->sign)
516  return a->sign > 0 ? 1 : -1;
517 
518  int i = RZ_BIG_ARRAY_SIZE;
519  do {
520  i -= 1; /* Decrement first, to start with last array element */
521  if (a->array[i] > b->array[i]) {
522  return 1 * a->sign;
523  }
524  if (a->array[i] < b->array[i]) {
525  return -1 * a->sign;
526  }
527  } while (i != 0);
528 
529  return 0;
530 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

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 555 of file big.c.

555  {
557  RzNumBig *tmp = rz_big_new();
558 
559  rz_big_from_int(tmp, 1);
560  rz_big_sub(a, a, tmp);
561 
562  rz_big_free(tmp);
563 }
RZ_API void rz_big_from_int(RzNumBig *b, st64 n)
Definition: big.c:37
RZ_API void rz_big_free(RzNumBig *b)
Definition: big.c:25
RZ_API RzNumBig * rz_big_new(void)
Definition: big.c:17
RZ_API void rz_big_sub(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:267

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_API void rz_big_div ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 328 of file big.c.

328  {
333 
334  RzNumBig *current = rz_big_new();
335  RzNumBig *denom = rz_big_new();
336  ;
337  RzNumBig *tmp = rz_big_new();
338  int sign = a->sign * b->sign;
339 
340  rz_big_from_int(current, 1); // int current = 1;
341  rz_big_assign(denom, b); // denom = b
342  denom->sign = 1;
343  rz_big_assign(tmp, denom); // tmp = denom = b
344  _lshift_one_bit(tmp); // tmp <= 1
345 
346  while (rz_big_cmp(tmp, a) != 1) { // while (tmp <= a)
347  if ((denom->array[RZ_BIG_ARRAY_SIZE - 1] >> (RZ_BIG_WORD_SIZE * 8 - 1)) == 1) {
348  break; // Reach the max value
349  }
350  _lshift_one_bit(tmp); // tmp <= 1
351  _lshift_one_bit(denom); // denom <= 1
352  _lshift_one_bit(current); // current <= 1
353  }
354 
355  rz_big_assign(tmp, a); // tmp = a
356  tmp->sign = 1;
357  _r_big_zero_out(c); // int answer = 0;
358 
359  while (!rz_big_is_zero(current)) // while (current != 0)
360  {
361  if (rz_big_cmp(tmp, denom) != -1) // if (dividend >= denom)
362  {
363  rz_big_sub(tmp, tmp, denom); // dividend -= denom;
364  rz_big_or(c, current, c); // answer |= current;
365  }
366  _rshift_one_bit(current); // current >>= 1;
367  _rshift_one_bit(denom); // denom >>= 1;
368  } // return answer;
369 
370  c->sign = sign;
371  if (rz_big_is_zero(c)) {
372  c->sign = 1; // For -1 * 0 case
373  }
374  rz_big_free(current);
375  rz_big_free(denom);
376  rz_big_free(tmp);
377 }
static void _rshift_one_bit(RzNumBig *a)
Definition: big.c:674
RZ_API int rz_big_cmp(RzNumBig *a, RzNumBig *b)
Definition: big.c:511
RZ_API void rz_big_assign(RzNumBig *dst, RzNumBig *src)
Definition: big.c:200
static void _r_big_zero_out(RzNumBig *n)
Definition: big.c:684
RZ_API void rz_big_or(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:437
static void _lshift_one_bit(RzNumBig *a)
Definition: big.c:664
RZ_API int rz_big_is_zero(RzNumBig *a)
Definition: big.c:532
int sign
Definition: rz_big.h:30
RZ_BIG_DTYPE array[RZ_BIG_ARRAY_SIZE]
Definition: rz_big.h:29

References _lshift_one_bit(), _r_big_zero_out(), _rshift_one_bit(), a, rz_num_big_t::array, b, c, RZ_BIG_ARRAY_SIZE, rz_big_assign(), rz_big_cmp(), rz_big_free(), rz_big_from_int(), rz_big_is_zero(), rz_big_new(), rz_big_or(), rz_big_sub(), RZ_BIG_WORD_SIZE, rz_return_if_fail, rz_num_big_t::sign, and autogen_x86imm::tmp.

Referenced by rz_big_divmod().

◆ rz_big_divmod()

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

Definition at line 395 of file big.c.

395  {
396  /*
397  Puts a%b in d
398  and a/b in c
399 
400  mod(a,b) = a - ((a / b) * b)
401 
402  example:
403  mod(8, 3) = 8 - ((8 / 3) * 3) = 2
404  */
409 
410  RzNumBig *tmp = rz_big_new();
411 
412  /* c = (a / b) */
413  rz_big_div(c, a, b);
414 
415  /* tmp = (c * b) */
416  rz_big_mul(tmp, c, b);
417 
418  /* d = a - tmp */
419  rz_big_sub(d, a, tmp);
420 
421  rz_big_free(tmp);
422 }
RZ_API void rz_big_div(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:328
RZ_API void rz_big_mul(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:292
#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 33 of file big.c.

33  {
35 }

References _r_big_zero_out(), and b.

◆ rz_big_free()

RZ_API void rz_big_free ( RzNumBig b)

Definition at line 25 of file big.c.

25  {
26  free(b);
27 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References b, and free().

Referenced by rz_big_dec(), rz_big_div(), rz_big_divmod(), rz_big_inc(), rz_big_isqrt(), rz_big_mod(), rz_big_mul(), and rz_big_powm().

◆ rz_big_from_hexstr()

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

Definition at line 113 of file big.c.

113  {
116  int nbytes = strlen(str);
117 
119 
120  if (str[0] == '-') {
121  n->sign = -1;
122  str += 1;
123  nbytes -= 1;
124  }
125 
126  if (str[0] == '0' && str[1] == 'x') {
127  str += 2;
128  nbytes -= 2;
129  }
131 
133  int i = nbytes - (2 * RZ_BIG_WORD_SIZE); /* index into string */
134  int j = 0; /* index into array */
135 
136  while (i >= 0) {
137  tmp = 0;
138  sscanf(&str[i], RZ_BIG_SSCANF_FORMAT_STR, &tmp);
139  n->array[j] = tmp;
140  i -= (2 * RZ_BIG_WORD_SIZE); /* step RZ_BIG_WORD_SIZE hex-byte(s) back in the string. */
141  j += 1; /* step one element forward in the array. */
142  }
143 
144  if (-2 * RZ_BIG_WORD_SIZE < i) {
145  char buffer[2 * RZ_BIG_WORD_SIZE];
146  memset(buffer, 0, sizeof(buffer));
147  i += 2 * RZ_BIG_WORD_SIZE - 1;
148  for (; i >= 0; i--) {
149  buffer[i] = str[i];
150  }
151  tmp = 0;
153  n->array[j] = tmp;
154  }
155 }
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 nbytes
Definition: sflib.h:113
return memset(p, 0, total)
int n
Definition: mipsasm.c:19
#define RZ_BIG_DTYPE
Definition: rz_big.h:21
#define RZ_BIG_SSCANF_FORMAT_STR
Definition: rz_big.h:25
Definition: buffer.h:15

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  n 
)

Definition at line 37 of file big.c.

37  {
39 
41  b->sign = (n < 0) ? -1 : 1;
42  RZ_BIG_DTYPE_TMP v = n * b->sign;
43 
44  /* Endianness issue if machine is not little-endian? */
45 #ifdef RZ_BIG_WORD_SIZE
46 #if (RZ_BIG_WORD_SIZE == 1)
47  b->array[0] = (v & 0x000000ff);
48  b->array[1] = (v & 0x0000ff00) >> 8;
49  b->array[2] = (v & 0x00ff0000) >> 16;
50  b->array[3] = (v & 0xff000000) >> 24;
51 #elif (RZ_BIG_WORD_SIZE == 2)
52  b->array[0] = (v & 0x0000ffff);
53  b->array[1] = (v & 0xffff0000) >> 16;
54 #elif (RZ_BIG_WORD_SIZE == 4)
55  b->array[0] = v;
56  RZ_BIG_DTYPE_TMP num_32 = 32;
57  RZ_BIG_DTYPE_TMP tmp = v >> num_32;
58  b->array[1] = tmp;
59 #endif
60 #endif
61 }
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_dec(), rz_big_div(), rz_big_inc(), and rz_big_powm().

◆ rz_big_from_unsigned()

static void rz_big_from_unsigned ( RzNumBig b,
ut64  v 
)
static

Definition at line 63 of file big.c.

63  {
65 
67 
68  /* Endianness issue if machine is not little-endian? */
69 #ifdef RZ_BIG_WORD_SIZE
70 #if (RZ_BIG_WORD_SIZE == 1)
71  b->array[0] = (v & 0x000000ff);
72  b->array[1] = (v & 0x0000ff00) >> 8;
73  b->array[2] = (v & 0x00ff0000) >> 16;
74  b->array[3] = (v & 0xff000000) >> 24;
75 #elif (RZ_BIG_WORD_SIZE == 2)
76  b->array[0] = (v & 0x0000ffff);
77  b->array[1] = (v & 0xffff0000) >> 16;
78 #elif (RZ_BIG_WORD_SIZE == 4)
79  b->array[0] = v;
80  RZ_BIG_DTYPE_TMP num_32 = 32;
81  RZ_BIG_DTYPE_TMP tmp = v >> num_32;
82  b->array[1] = tmp;
83 #endif
84 #endif
85 }

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

Referenced by rz_big_mul().

◆ rz_big_inc()

RZ_API void rz_big_inc ( RzNumBig a)

Definition at line 545 of file big.c.

545  {
547  RzNumBig *tmp = rz_big_new();
548 
549  rz_big_from_int(tmp, 1);
550  rz_big_add(a, a, tmp);
551 
552  rz_big_free(tmp);
553 }
RZ_API void rz_big_add(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:242

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 29 of file big.c.

29  {
31 }

References _r_big_zero_out(), and b.

◆ rz_big_is_zero()

RZ_API int rz_big_is_zero ( RzNumBig a)

Definition at line 532 of file big.c.

532  {
534 
535  int i;
536  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
537  if (a->array[i]) {
538  return 0;
539  }
540  }
541 
542  return 1;
543 }

References a, i, RZ_BIG_ARRAY_SIZE, and rz_return_val_if_fail.

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

◆ rz_big_isqrt()

RZ_API void rz_big_isqrt ( RzNumBig b,
RzNumBig a 
)

Definition at line 593 of file big.c.

593  {
596 
597  RzNumBig *tmp = rz_big_new();
598  RzNumBig *low = rz_big_new();
599  RzNumBig *high = rz_big_new();
600  RzNumBig *mid = rz_big_new();
601 
602  rz_big_assign(high, a);
603  rz_big_rshift(mid, high, 1);
604  rz_big_inc(mid);
605 
606  while (rz_big_cmp(high, low) > 0) {
607  rz_big_mul(tmp, mid, mid);
608  if (rz_big_cmp(tmp, a) > 0) {
609  rz_big_assign(high, mid);
610  rz_big_dec(high);
611  } else {
612  rz_big_assign(low, mid);
613  }
614  rz_big_sub(mid, high, low);
615  _rshift_one_bit(mid);
616  rz_big_add(mid, mid, low);
617  rz_big_inc(mid);
618  }
619  rz_big_assign(b, low);
620 
621  rz_big_free(tmp);
622  rz_big_free(low);
623  rz_big_free(high);
624  rz_big_free(mid);
625 }
RZ_API void rz_big_dec(RzNumBig *a)
Definition: big.c:555
RZ_API void rz_big_rshift(RzNumBig *b, RzNumBig *a, size_t nbits)
Definition: big.c:487
RZ_API void rz_big_inc(RzNumBig *a)
Definition: big.c:545

References _rshift_one_bit(), a, b, c, rz_big_add(), rz_big_assign(), rz_big_cmp(), rz_big_dec(), rz_big_free(), rz_big_inc(), rz_big_mul(), rz_big_new(), rz_big_rshift(), rz_big_sub(), rz_return_if_fail, and autogen_x86imm::tmp.

◆ rz_big_lshift()

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

Definition at line 463 of file big.c.

463  {
466  rz_return_if_fail(a->sign > 0);
467  rz_return_if_fail(b->sign > 0);
468 
469  rz_big_assign(b, a);
470  /* Handle shift in multiples of word-size */
471  const int nbits_pr_word = (RZ_BIG_WORD_SIZE * 8);
472  int nwords = nbits / nbits_pr_word;
473  if (nwords != 0) {
474  _lshift_word(b, nwords);
475  nbits -= (nwords * nbits_pr_word);
476  }
477 
478  if (nbits != 0) {
479  int i;
480  for (i = (RZ_BIG_ARRAY_SIZE - 1); i > 0; i--) {
481  b->array[i] = (b->array[i] << nbits) | (b->array[i - 1] >> ((8 * RZ_BIG_WORD_SIZE) - nbits));
482  }
483  b->array[i] <<= nbits;
484  }
485 }
static void _lshift_word(RzNumBig *a, int nwords)
Definition: big.c:649

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

◆ rz_big_mod()

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

Definition at line 379 of file big.c.

379  {
380  /*
381  Take divmod and throw away div part
382  */
387 
388  RzNumBig *tmp = rz_big_new();
389 
390  rz_big_divmod(tmp, c, a, b);
391 
392  rz_big_free(tmp);
393 }
RZ_API void rz_big_divmod(RzNumBig *c, RzNumBig *d, RzNumBig *a, RzNumBig *b)
Definition: big.c:395

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_API void rz_big_mul ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)

Definition at line 292 of file big.c.

292  {
296 
297  RzNumBig *row = rz_big_new();
298  RzNumBig *tmp = rz_big_new();
299  RzNumBig *res = rz_big_new();
300  int i, j;
301 
302  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
303  _r_big_zero_out(row);
304 
305  for (j = 0; j < RZ_BIG_ARRAY_SIZE; j++) {
306  if (i + j < RZ_BIG_ARRAY_SIZE) {
308  RZ_BIG_DTYPE_TMP intermediate = ((RZ_BIG_DTYPE_TMP)a->array[i] * (RZ_BIG_DTYPE_TMP)b->array[j]);
309  rz_big_from_unsigned(tmp, intermediate);
310  _lshift_word(tmp, i + j);
311  rz_big_add(row, row, tmp);
312  }
313  }
314  rz_big_add(res, row, res);
315  }
316 
317  res->sign = a->sign * b->sign;
318  if (rz_big_is_zero(res)) {
319  res->sign = 1; // For -1 * 0 case
320  }
321  rz_big_assign(c, res);
322 
323  rz_big_free(row);
324  rz_big_free(tmp);
325  rz_big_free(res);
326 }
static void rz_big_from_unsigned(RzNumBig *b, ut64 v)
Definition: big.c:63

References _lshift_word(), _r_big_zero_out(), a, b, c, i, rz_big_add(), RZ_BIG_ARRAY_SIZE, rz_big_assign(), RZ_BIG_DTYPE_TMP, rz_big_free(), rz_big_from_unsigned(), rz_big_is_zero(), rz_big_new(), rz_return_if_fail, rz_num_big_t::sign, and autogen_x86imm::tmp.

Referenced by rz_big_divmod(), rz_big_isqrt(), and rz_big_powm().

◆ rz_big_new()

RZ_API RzNumBig* rz_big_new ( void  )

Definition at line 17 of file big.c.

17  {
19  if (n) {
21  }
22  return n;
23 }
#define RZ_NEW(x)
Definition: rz_types.h:285

References _r_big_zero_out(), n, and RZ_NEW.

Referenced by rz_big_dec(), rz_big_div(), rz_big_divmod(), rz_big_inc(), rz_big_isqrt(), rz_big_mod(), rz_big_mul(), and rz_big_powm().

◆ rz_big_or()

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

Definition at line 437 of file big.c.

437  {
441  rz_return_if_fail(a->sign > 0);
442  rz_return_if_fail(b->sign > 0);
443 
444  int i;
445  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
446  c->array[i] = (a->array[i] | b->array[i]);
447  }
448 }

References a, b, c, i, RZ_BIG_ARRAY_SIZE, and rz_return_if_fail.

Referenced by rz_big_div().

◆ rz_big_powm()

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

Definition at line 565 of file big.c.

565  {
570 
571  RzNumBig *bcopy = rz_big_new();
572  RzNumBig *acopy = rz_big_new();
573 
574  rz_big_assign(bcopy, b);
575  rz_big_assign(acopy, a);
576  rz_big_mod(acopy, acopy, m);
577  rz_big_from_int(c, 1);
578 
579  while (!rz_big_is_zero(bcopy)) {
580  if (rz_big_to_int(bcopy) % 2 == 1) {
581  rz_big_mul(c, c, acopy);
582  rz_big_mod(c, c, m);
583  }
584  _rshift_one_bit(bcopy);
585  rz_big_mul(acopy, acopy, acopy);
586  rz_big_mod(acopy, acopy, m);
587  }
588 
589  rz_big_free(bcopy);
590  rz_big_free(acopy);
591 }
RZ_API st64 rz_big_to_int(RzNumBig *b)
Definition: big.c:87
RZ_API void rz_big_mod(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big.c:379

References _rshift_one_bit(), a, b, c, regress::m, rz_big_assign(), rz_big_free(), rz_big_from_int(), rz_big_is_zero(), rz_big_mod(), rz_big_mul(), rz_big_new(), rz_big_to_int(), and rz_return_if_fail.

◆ rz_big_rshift()

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

Definition at line 487 of file big.c.

487  {
490  rz_return_if_fail(a->sign > 0);
491  rz_return_if_fail(b->sign > 0);
492 
493  rz_big_assign(b, a);
494  /* Handle shift in multiples of word-size */
495  const int nbits_pr_word = (RZ_BIG_WORD_SIZE * 8);
496  int nwords = nbits / nbits_pr_word;
497  if (nwords != 0) {
498  _rshift_word(b, nwords);
499  nbits -= (nwords * nbits_pr_word);
500  }
501 
502  if (nbits != 0) {
503  int i;
504  for (i = 0; i < (RZ_BIG_ARRAY_SIZE - 1); i++) {
505  b->array[i] = (b->array[i] >> nbits) | (b->array[i + 1] << ((8 * RZ_BIG_WORD_SIZE) - nbits));
506  }
507  b->array[i] >>= nbits;
508  }
509 }
static void _rshift_word(RzNumBig *a, int nwords)
Definition: big.c:628

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_isqrt().

◆ rz_big_sub()

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

Definition at line 267 of file big.c.

267  {
271 
272  if (a->sign >= 0 && b->sign >= 0) {
273  rz_big_sub_inner(c, a, b);
274  return;
275  }
276  if (a->sign >= 0 && b->sign < 0) {
277  rz_big_add_inner(c, a, b);
278  c->sign = 1;
279  return;
280  }
281  if (a->sign < 0 && b->sign >= 0) {
282  rz_big_add_inner(c, a, b);
283  c->sign = -1;
284  return;
285  }
286  if (a->sign < 0 && b->sign < 0) {
287  rz_big_sub_inner(c, b, a);
288  return;
289  }
290 }

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_sub_inner()

static void rz_big_sub_inner ( RzNumBig c,
RzNumBig a,
RzNumBig b 
)
static

Definition at line 218 of file big.c.

218  {
219  RZ_BIG_DTYPE_TMP res;
220  RzNumBig *tmp;
221  RZ_BIG_DTYPE_TMP tmp1;
222  RZ_BIG_DTYPE_TMP tmp2;
223  int borrow = 0;
224  int sign = rz_big_cmp(a, b);
225  c->sign = (sign >= 0 ? 1 : -1);
226  if (sign < 0) {
227  tmp = a;
228  a = b;
229  b = tmp;
230  }
231  int i;
232  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
233  tmp1 = (RZ_BIG_DTYPE_TMP)a->array[i] + (RZ_BIG_MAX_VAL + 1); /* + number_base */
234  tmp2 = (RZ_BIG_DTYPE_TMP)b->array[i] + borrow;
235 
236  res = (tmp1 - tmp2);
237  c->array[i] = (RZ_BIG_DTYPE)(res & RZ_BIG_MAX_VAL); /* "modulo number_base" == "% (number_base - 1)" if nu mber_base is 2^N */
238  borrow = (res <= RZ_BIG_MAX_VAL);
239  }
240 }

References a, b, c, i, RZ_BIG_ARRAY_SIZE, rz_big_cmp(), RZ_BIG_DTYPE, RZ_BIG_DTYPE_TMP, RZ_BIG_MAX_VAL, and autogen_x86imm::tmp.

Referenced by rz_big_add(), and rz_big_sub().

◆ rz_big_to_hexstr()

RZ_API char* rz_big_to_hexstr ( RzNumBig b)

Definition at line 157 of file big.c.

157  {
159 
160  int j = RZ_BIG_ARRAY_SIZE - 1; /* index into array - reading "MSB" first -> big-endian */
161  size_t i = 0; /* index into string representation. */
162  size_t k = 0; /* Leading zero's amount */
163  size_t z, last_z = 2 * RZ_BIG_WORD_SIZE;
164 
165  for (; b->array[j] == 0 && j >= 0; j--) {
166  }
167  if (j == -1) {
168  return "0x0";
169  }
170 
171  size_t size = 3 + 2 * RZ_BIG_WORD_SIZE * (j + 1) + ((b->sign > 0) ? 0 : 1);
172  char *ret_str = calloc(size, sizeof(char));
173  if (!ret_str) {
174  return NULL;
175  }
176 
177  if (b->sign < 0) {
178  ret_str[i++] = '-';
179  }
180  ret_str[i++] = '0';
181  ret_str[i++] = 'x';
182 
184  for (; ret_str[i + k] == '0' && k < 2 * RZ_BIG_WORD_SIZE; k++) {
185  }
186  for (z = k; ret_str[i + z] && z < last_z; z++) {
187  ret_str[i + z - k] = ret_str[i + z];
188  }
189  i += z - k;
190  ret_str[i] = '\x00'; // Truncate string for case(j < 0)
191 
192  for (; j >= 0; j--) {
194  i += 2 * RZ_BIG_WORD_SIZE;
195  }
196 
197  return ret_str;
198 }
#define NULL
Definition: cris-opc.c:27
const char * k
Definition: dsignal.c:11
voidpf void uLong size
Definition: ioapi.h:138
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define RZ_BIG_FORMAT_STR_LEN
Definition: rz_big.h:24
#define RZ_BIG_SPRINTF_FORMAT_STR
Definition: rz_big.h:23
RZ_API int rz_snprintf(char *string, int len, const char *fmt,...) RZ_PRINTF_CHECK(3

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(), and autogen_x86imm::tmp.

◆ rz_big_to_int()

RZ_API st64 rz_big_to_int ( RzNumBig b)

Definition at line 87 of file big.c.

87  {
89 
90  RZ_BIG_DTYPE_TMP ret = 0;
91 
92  /* Endianness issue if machine is not little-endian? */
93 #if (RZ_BIG_WORD_SIZE == 1)
94  ret += b->array[0];
95  ret += b->array[1] << 8;
96  ret += b->array[2] << 16;
97  ret += b->array[3] << 24;
98 #elif (RZ_BIG_WORD_SIZE == 2)
99  ret += b->array[0];
100  ret += b->array[1] << 16;
101 #elif (RZ_BIG_WORD_SIZE == 4)
102  ret += b->array[1];
103  ret <<= 32;
104  ret += b->array[0];
105 #endif
106 
107  if (b->sign < 0) {
108  return -ret;
109  }
110  return ret;
111 }

References b, RZ_BIG_DTYPE_TMP, and rz_return_val_if_fail.

Referenced by rz_big_powm().

◆ rz_big_xor()

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

Definition at line 450 of file big.c.

450  {
454  rz_return_if_fail(a->sign > 0);
455  rz_return_if_fail(b->sign > 0);
456 
457  int i;
458  for (i = 0; i < RZ_BIG_ARRAY_SIZE; i++) {
459  c->array[i] = (a->array[i] ^ b->array[i]);
460  }
461 }

References a, b, c, i, RZ_BIG_ARRAY_SIZE, and rz_return_if_fail.