Rizin
unix-like reverse engineering framework and cli tools
rz_bitvector.h File Reference
#include <rz_types.h>

Go to the source code of this file.

Classes

struct  bitvector_t
 structure for bitvector More...
 

Macros

#define rz_bv_neg   rz_bv_complement_2
 
#define rz_bv_not   rz_bv_complement_1
 
#define rz_bv_new_zero(l)   rz_bv_new(l)
 
#define rz_bv_new_one(l)   rz_bv_new_from_ut64(l, 1)
 
#define rz_bv_new_two(l)   rz_bv_new_from_ut64(l, 2)
 
#define rz_bv_new_minus_one(l)   rz_bv_new_from_st64(l, -1)
 

Typedefs

typedef struct bitvector_t RzBitVector
 

Functions

RZ_API bool rz_bv_init (RZ_NONNULL RzBitVector *bv, ut32 length)
 Initialize a RzBitVector structure. More...
 
RZ_API RZ_OWN RzBitVectorrz_bv_new (ut32 length)
 
RZ_API RZ_OWN RzBitVectorrz_bv_dup (const RZ_NONNULL RzBitVector *bv)
 
RZ_API RZ_OWN RzBitVectorrz_bv_append (RZ_NONNULL RzBitVector *bv1, RZ_NONNULL RzBitVector *bv2)
 
RZ_API ut32 rz_bv_copy (RZ_NONNULL const RzBitVector *src, RZ_NONNULL RzBitVector *dst)
 
RZ_API ut32 rz_bv_copy_nbits (RZ_NONNULL const RzBitVector *src, ut32 src_start_pos, RZ_NONNULL RzBitVector *dst, ut32 dst_start_pos, ut32 nbit)
 
RZ_API void rz_bv_fini (RZ_NONNULL RzBitVector *bv)
 Clear a RzBitVector structure. More...
 
RZ_API void rz_bv_free (RZ_NULLABLE RzBitVector *bv)
 
RZ_API bool rz_bv_set (RZ_NONNULL RzBitVector *bv, ut32 pos, bool b)
 
RZ_API bool rz_bv_set_all (RZ_NONNULL RzBitVector *bv, bool b)
 
RZ_API bool rz_bv_toggle (RZ_NONNULL RzBitVector *bv, ut32 pos)
 
RZ_API bool rz_bv_toggle_all (RZ_NONNULL RzBitVector *bv)
 
RZ_API RZ_OWN RzBitVectorrz_bv_append_zero (RZ_NONNULL RzBitVector *bv, ut32 delta_len)
 
RZ_API RZ_OWN RzBitVectorrz_bv_prepend_zero (RZ_NONNULL RzBitVector *bv, ut32 delta_len)
 
RZ_API RZ_OWN RzBitVectorrz_bv_cut_head (RZ_NONNULL RzBitVector *bv, ut32 delta_len)
 
RZ_API RZ_OWN RzBitVectorrz_bv_cut_tail (RZ_NONNULL RzBitVector *bv, ut32 delta_len)
 
RZ_API bool rz_bv_get (RZ_NONNULL const RzBitVector *bv, ut32 pos)
 
RZ_API bool rz_bv_lshift (RZ_NONNULL RzBitVector *bv, ut32 size)
 
RZ_API bool rz_bv_rshift (RZ_NONNULL RzBitVector *bv, ut32 size)
 
RZ_API bool rz_bv_lshift_fill (RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
 
RZ_API bool rz_bv_rshift_fill (RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
 
RZ_API RZ_OWN RzBitVectorrz_bv_and (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API RZ_OWN RzBitVectorrz_bv_or (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API RZ_OWN RzBitVectorrz_bv_xor (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API RZ_OWN RzBitVectorrz_bv_complement_1 (RZ_NONNULL RzBitVector *bv)
 
RZ_API RZ_OWN RzBitVectorrz_bv_complement_2 (RZ_NONNULL RzBitVector *bv)
 
RZ_API RZ_OWN RzBitVectorrz_bv_add (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *carry)
 
RZ_API RZ_OWN RzBitVectorrz_bv_sub (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *borrow)
 
RZ_API RZ_OWN RzBitVectorrz_bv_mul (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API RZ_OWN RzBitVectorrz_bv_div (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API RZ_OWN RzBitVectorrz_bv_mod (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API RZ_OWN RzBitVectorrz_bv_sdiv (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API RZ_OWN RzBitVectorrz_bv_smod (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API bool rz_bv_msb (RZ_NONNULL RzBitVector *bv)
 
RZ_API bool rz_bv_lsb (RZ_NONNULL RzBitVector *bv)
 
RZ_API bool rz_bv_eq (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API bool rz_bv_ule (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API bool rz_bv_sle (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API ut32 rz_bv_clz (RZ_NONNULL RzBitVector *bv)
 
RZ_API ut32 rz_bv_ctz (RZ_NONNULL RzBitVector *bv)
 
RZ_API ut8 rz_bv_to_ut8 (RZ_NONNULL const RzBitVector *x)
 
RZ_API ut16 rz_bv_to_ut16 (RZ_NONNULL const RzBitVector *x)
 
RZ_API ut32 rz_bv_to_ut32 (RZ_NONNULL const RzBitVector *x)
 
RZ_API ut64 rz_bv_to_ut64 (RZ_NONNULL const RzBitVector *x)
 
RZ_API bool rz_bv_is_zero_vector (RZ_NONNULL const RzBitVector *x)
 
RZ_API RZ_OWN RzBitVectorrz_bv_new_from_ut64 (ut32 length, ut64 value)
 
RZ_API RZ_OWN RzBitVectorrz_bv_new_from_st64 (ut32 length, st64 value)
 
RZ_API RZ_OWN RzBitVectorrz_bv_new_from_bytes_le (RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
 
RZ_API RZ_OWN RzBitVectorrz_bv_new_from_bytes_be (RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
 
RZ_API bool rz_bv_set_from_ut64 (RZ_NONNULL RzBitVector *bv, ut64 value)
 
RZ_API bool rz_bv_set_from_st64 (RZ_NONNULL RzBitVector *bv, st64 value)
 
RZ_API void rz_bv_set_from_bytes_le (RZ_NONNULL RzBitVector *bv, RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
 
RZ_API void rz_bv_set_from_bytes_be (RZ_NONNULL RzBitVector *bv, RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
 Set the bitvector's contents from the given bits. The bitvector's size is unchanged. If bv->len < size, additional bits are cut off, if bv->len > size, the rest is filled up with 0. More...
 
RZ_API void rz_bv_set_to_bytes_le (RZ_NONNULL const RzBitVector *bv, RZ_OUT RZ_NONNULL ut8 *buf)
 Set the buffer contents from the given bitvector's bits in little endian format. More...
 
RZ_API void rz_bv_set_to_bytes_be (RZ_NONNULL const RzBitVector *bv, RZ_OUT RZ_NONNULL ut8 *buf)
 Set the buffer contents from the given bitvector's bits in big endian format. More...
 
RZ_API RZ_OWN char * rz_bv_as_string (RZ_NONNULL RzBitVector *bv)
 
RZ_API RZ_OWN char * rz_bv_as_hex_string (RZ_NONNULL RzBitVector *bv, bool pad)
 
RZ_API ut32 rz_bv_len (RZ_NONNULL const RzBitVector *bv)
 
RZ_API ut32 rz_bv_len_bytes (RZ_NONNULL const RzBitVector *bv)
 
RZ_API bool rz_bv_cmp (RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
 
RZ_API ut32 rz_bv_hash (RZ_NULLABLE RzBitVector *x)
 

Macro Definition Documentation

◆ rz_bv_neg

#define rz_bv_neg   rz_bv_complement_2

Definition at line 58 of file rz_bitvector.h.

◆ rz_bv_new_minus_one

#define rz_bv_new_minus_one (   l)    rz_bv_new_from_st64(l, -1)

Definition at line 108 of file rz_bitvector.h.

◆ rz_bv_new_one

#define rz_bv_new_one (   l)    rz_bv_new_from_ut64(l, 1)

Definition at line 106 of file rz_bitvector.h.

◆ rz_bv_new_two

#define rz_bv_new_two (   l)    rz_bv_new_from_ut64(l, 2)

Definition at line 107 of file rz_bitvector.h.

◆ rz_bv_new_zero

#define rz_bv_new_zero (   l)    rz_bv_new(l)

Definition at line 105 of file rz_bitvector.h.

◆ rz_bv_not

#define rz_bv_not   rz_bv_complement_1

Definition at line 59 of file rz_bitvector.h.

Typedef Documentation

◆ RzBitVector

typedef struct bitvector_t RzBitVector

Function Documentation

◆ rz_bv_add()

Result of (x + y) mod 2^length Both operands must have the same length.

Parameters
xRzBitVector, Operand
yRzBitVector, Operand
carrybool*, bool pointer to where to save the carry value.
Returns
ret RzBitVector, point to the new bitvector

Definition at line 683 of file bitvector.c.

683  {
685 
686  if (x->len != y->len) {
688  return NULL;
689  }
690 
691  bool a = false, b = false, _carry = false;
692  RzBitVector *ret = rz_bv_new(x->len);
693 
694  for (ut32 pos = 0; pos < x->len; ++pos) {
695  a = rz_bv_get(x, pos);
696  b = rz_bv_get(y, pos);
697  rz_bv_set(ret, pos, a ^ b ^ _carry);
698  _carry = ((a & b) | (a & _carry)) | (b & _carry);
699  }
700  if (carry) {
701  *carry = _carry;
702  }
703 
704  return ret;
705 }
RZ_API bool rz_bv_get(RZ_NONNULL const RzBitVector *bv, ut32 pos)
Definition: bitvector.c:429
RZ_API RZ_OWN RzBitVector * rz_bv_new(ut32 length)
Definition: bitvector.c:71
RZ_API bool rz_bv_set(RZ_NONNULL RzBitVector *bv, ut32 pos, bool b)
Definition: bitvector.c:341
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
int x
Definition: mipsasm.c:20
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
structure for bitvector
Definition: rz_bitvector.h:19
int pos
Definition: main.c:11

References a, b, NULL, pos, rz_bv_get(), rz_bv_new(), rz_bv_set(), rz_return_val_if_fail, rz_warn_if_reached, and x.

Referenced by rz_bv_mul(), rz_bv_sub(), and rz_il_handler_add().

◆ rz_bv_and()

Result of x AND y (and operation to every bits) Both operands must have the same length.

Parameters
xRzBitVector, operand
yRzBitVector, operand
Returns
ret RzBitVector, a new bitvector, which is the result of AND

Definition at line 546 of file bitvector.c.

546  {
548  if (x->len != y->len) {
549  return NULL;
550  }
551 
552  RzBitVector *ret = rz_bv_new(x->len);
553  if (!ret) {
554  return NULL;
555  } else if (x->len <= 64) {
556  ret->bits.small_u = x->bits.small_u & y->bits.small_u;
557  return ret;
558  }
559 
560  for (ut32 i = 0; i < ret->_elem_len; ++i) {
561  ret->bits.large_a[i] = x->bits.large_a[i] & y->bits.large_a[i];
562  }
563  return ret;
564 }
lzma_index ** i
Definition: index.h:629
union bitvector_t::@303 bits
ut8 * large_a
little endian array of bytes for bitvectors > 64 bits whose size is defined in _elem_len
Definition: rz_bitvector.h:21
ut64 small_u
value of the bitvector when the size is <= 64 bits
Definition: rz_bitvector.h:22
ut32 _elem_len
length of ut8 array (bits.large_a) – real / physical
Definition: rz_bitvector.h:24

References bitvector_t::_elem_len, bitvector_t::bits, i, bitvector_t::large_a, NULL, rz_bv_new(), rz_return_val_if_fail, bitvector_t::small_u, and x.

Referenced by rz_il_handler_logical_and().

◆ rz_bv_append()

RZ_API RZ_OWN RzBitVector* rz_bv_append ( RZ_NONNULL RzBitVector high,
RZ_NONNULL RzBitVector low 
)

Append bv2 to bv1 to get new bitvector

Parameters
highbitvector to occupy the most significant part of the result
lowbitvector to occupy the least significant part of the result
Returns
ret RzBitVector, the new bitvector

Definition at line 326 of file bitvector.c.

326  {
327  rz_return_val_if_fail(high && low, NULL);
328  RzBitVector *ret = rz_bv_new(high->len + low->len);
329  rz_bv_copy_nbits(low, 0, ret, 0, low->len);
330  rz_bv_copy_nbits(high, 0, ret, low->len, high->len);
331  return ret;
332 }
RZ_API ut32 rz_bv_copy_nbits(RZ_NONNULL const RzBitVector *src, ut32 src_start_pos, RZ_NONNULL RzBitVector *dst, ut32 dst_start_pos, ut32 nbit)
Definition: bitvector.c:210

References bitvector_t::len, NULL, rz_bv_copy_nbits(), rz_bv_new(), and rz_return_val_if_fail.

Referenced by rz_il_handler_append().

◆ rz_bv_append_zero()

RZ_API RZ_OWN RzBitVector* rz_bv_append_zero ( RZ_NONNULL RzBitVector bv,
ut32  delta_len 
)

Return a new bitvector appended with n zero bits

Parameters
bvRzBitVector, pointer to bitvector
delta_len,thenumber of zero bits
Returns
ret RzBitVector, pointert to the new btivector

Definition at line 258 of file bitvector.c.

258  {
260 
261  ut32 new_len = bv->len + delta_len;
262  RzBitVector *ret = rz_bv_new(new_len);
263  if (ret == NULL) {
264  return NULL;
265  }
266 
267  ut32 pos = delta_len;
268  for (ut32 i = 0; i < bv->len; ++i, ++pos) {
269  rz_bv_set(ret, pos, rz_bv_get(bv, i));
270  }
271 
272  return ret;
273 }

References i, NULL, pos, rz_bv_get(), rz_bv_new(), rz_bv_set(), and rz_return_val_if_fail.

◆ rz_bv_as_hex_string()

RZ_API RZ_OWN char* rz_bv_as_hex_string ( RZ_NONNULL RzBitVector bv,
bool  pad 
)

Return bitvector string in hexadecimal format

Parameters
bvRzBitVector, pointer to bitvector
padwhether to prepend leading zeroes to indicate the bitvector size
Returns
str char*, bitvector string in hexadecimal format

Definition at line 121 of file bitvector.c.

121  {
123 
124  if (bv->len <= 64) {
125  if (pad) {
126  char format[32] = { 0 };
127  rz_strf(format, "0x%%0%d" PFMT64x, (bv->len + 3) / 4);
128  return rz_str_newf(format, bv->bits.small_u);
129  } else {
130  return rz_str_newf("0x%" PFMT64x, bv->bits.small_u);
131  }
132  }
133 
134  const char *hex = "0123456789abcdef";
135  size_t str_len = (bv->_elem_len << 1) + 3; // 0x + \0
136  char *str = (char *)malloc(str_len);
137  if (!str) {
138  return NULL;
139  }
140 
141  str[0] = '0';
142  str[1] = 'x';
143  ut32 j = 2;
144  for (ut32 i = 0; i < bv->_elem_len; i++) {
145  ut8 b8 = bv->bits.large_a[bv->_elem_len - i - 1];
146  ut8 high = b8 >> 4;
147  ut8 low = b8 & 15;
148  if (pad || high) {
149  str[j++] = hex[high];
150  pad = true; // pad means "print all" from now on
151  }
152  if (pad || low || i == bv->_elem_len - 1) {
153  str[j++] = hex[low];
154  pad = true; // pad means "print all" from now on
155  }
156  }
157  str[j] = '\0';
158 
159  return str;
160 }
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
static const char hex[16]
Definition: print.c:21
static void pad(RzStrBuf *sb, ut32 count)
Definition: protobuf.c:36
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define rz_strf(buf,...)
Convenience macro for local temporary strings.
Definition: rz_str.h:59
#define PFMT64x
Definition: rz_types.h:393

References hex, i, malloc(), NULL, pad(), PFMT64x, rz_return_val_if_fail, rz_str_newf(), rz_strf, and cmd_descs_generate::str.

Referenced by il_opdmp_bitv(), rz_il_event_json(), rz_il_event_stringify(), rz_il_value_stringify(), and rzil_print_register_bitv().

◆ rz_bv_as_string()

RZ_API RZ_OWN char* rz_bv_as_string ( RZ_NONNULL RzBitVector bv)

Return bitvector string

Parameters
bvRzBitVector, pointer to bitvector
Returns
str char*, bitvector string

Definition at line 98 of file bitvector.c.

98  {
100 
101  char *str = (char *)malloc(bv->len + 1);
102  if (!str) {
103  return NULL;
104  }
105 
106  for (ut32 i = bv->len - 1, j = 0; i > 0; --i, j++) {
107  str[j] = rz_bv_get(bv, i) ? '1' : '0';
108  }
109  str[bv->len - 1] = rz_bv_get(bv, 0) ? '1' : '0';
110  str[bv->len] = '\0';
111 
112  return str;
113 }

References i, malloc(), NULL, rz_bv_get(), rz_return_val_if_fail, and cmd_descs_generate::str.

Referenced by rz_il_handler_empty().

◆ rz_bv_clz()

RZ_API ut32 rz_bv_clz ( RZ_NONNULL RzBitVector bv)

Count leading (most significant) zeroes All bits are considered leading zeroes for a zero bitvector.

Definition at line 1107 of file bitvector.c.

1107  {
1108  rz_return_val_if_fail(bv, 0);
1109  ut32 r = 0;
1110  for (ut32 i = rz_bv_len(bv); i; i--) {
1111  if (rz_bv_get(bv, i - 1)) {
1112  break;
1113  }
1114  r++;
1115  }
1116  return r;
1117 }
RZ_API ut32 rz_bv_len(RZ_NONNULL const RzBitVector *bv)
Definition: bitvector.c:1140
#define r
Definition: crypto_rc6.c:12

References i, r, rz_bv_get(), rz_bv_len(), and rz_return_val_if_fail.

Referenced by rz_bv_div().

◆ rz_bv_cmp()

RZ_API bool rz_bv_cmp ( RZ_NONNULL RzBitVector x,
RZ_NONNULL RzBitVector y 
)

Check if x equals to y Both operands must have the same length.

Parameters
xRzBitVector, operand
yRzBitVector, operand
Returns
ret int, return 1 if x != y, return 0 if x == y

Definition at line 1086 of file bitvector.c.

1086  {
1087  rz_return_val_if_fail(x && y, 0);
1088 
1089  if (x->len != y->len) {
1091  return true;
1092  }
1093 
1094  for (ut32 i = 0; i < x->len; ++i) {
1095  if (rz_bv_get(x, i) != rz_bv_get(y, i)) {
1096  return true;
1097  }
1098  }
1099 
1100  return false;
1101 }

References i, rz_bv_get(), rz_return_val_if_fail, rz_warn_if_reached, and x.

◆ rz_bv_complement_1()

RZ_API RZ_OWN RzBitVector* rz_bv_complement_1 ( RZ_NONNULL RzBitVector bv)

Get the 1's complement of bv

Parameters
bvRzBitVector, operand
Returns
ret RzBitVector, a new bitvector, which is the 1's complement of bv

Definition at line 625 of file bitvector.c.

625  {
627 
628  RzBitVector *ret = rz_bv_new(bv->len);
629  if (!ret) {
630  return NULL;
631  } else if (ret->len <= 64) {
632  ret->bits.small_u = ~bv->bits.small_u;
633  ret->bits.small_u &= UT64_MAX >> (64 - ret->len);
634  return ret;
635  }
636 
637  if (!(ret->bits.large_a && bv->bits.large_a)) {
638  rz_bv_free(ret);
640  }
641  for (ut32 i = 0; i < bv->_elem_len; ++i) {
642  ret->bits.large_a[i] = ~bv->bits.large_a[i];
643  }
644  return ret;
645 }
RZ_API void rz_bv_free(RZ_NULLABLE RzBitVector *bv)
Definition: bitvector.c:85
#define rz_return_val_if_reached(val)
Definition: rz_assert.h:122
#define UT64_MAX
Definition: rz_types_base.h:86
ut32 len
number of bits – virtual / logical
Definition: rz_bitvector.h:25

References bitvector_t::bits, i, bitvector_t::large_a, bitvector_t::len, NULL, rz_bv_free(), rz_bv_new(), rz_return_val_if_fail, rz_return_val_if_reached, bitvector_t::small_u, and UT64_MAX.

◆ rz_bv_complement_2()

RZ_API RZ_OWN RzBitVector* rz_bv_complement_2 ( RZ_NONNULL RzBitVector bv)

Get the 2's complement of bv

Parameters
bvRzBitVector, operand
Returns
ret RzBitVector, a new bitvector, which is the 2's complement of bv

Definition at line 652 of file bitvector.c.

652  {
654 
655  // from right side to left, find the 1st 1 bit
656  // flip/toggle every bit before it
657  RzBitVector *ret = rz_bv_dup(bv);
658 
659  ut32 i;
660  for (i = 0; i < bv->len; ++i) {
661  if (rz_bv_get(bv, i) == true) {
662  break;
663  }
664  }
665 
666  // assert bv[i] == true now
667  i += 1;
668  for (; i < bv->len; ++i) {
669  rz_bv_toggle(ret, i);
670  }
671 
672  return ret;
673 }
RZ_API RZ_OWN RzBitVector * rz_bv_dup(const RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:167
RZ_API bool rz_bv_toggle(RZ_NONNULL RzBitVector *bv, ut32 pos)
Definition: bitvector.c:396

References i, NULL, rz_bv_dup(), rz_bv_get(), rz_bv_toggle(), and rz_return_val_if_fail.

◆ rz_bv_copy()

RZ_API ut32 rz_bv_copy ( RZ_NONNULL const RzBitVector src,
RZ_NONNULL RzBitVector dst 
)

Copy from source bitvector to destination bitvector the maximum copied size depends on MIN(src_len, dst_len)

Parameters
srcRzBitVector, the source bitvector
dstRzBitVector, the destination bitvector
Returns
Actual size of copy

Definition at line 186 of file bitvector.c.

186  {
188 
189  if (dst->len != src->len) {
190  return 0;
191  } else if (dst->len <= 64) {
192  dst->bits.small_u = src->bits.small_u;
193  return sizeof(dst->bits.small_u);
194  }
195 
196  rz_return_val_if_fail(src->bits.large_a && dst->bits.large_a, 0);
197  memcpy(dst->bits.large_a, src->bits.large_a, dst->_elem_len);
198  return dst->_elem_len;
199 }
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_val_if_fail, and src.

Referenced by rz_bv_dup(), rz_bv_lshift_fill(), rz_bv_mul(), and rz_bv_rshift_fill().

◆ rz_bv_copy_nbits()

RZ_API ut32 rz_bv_copy_nbits ( RZ_NONNULL const RzBitVector src,
ut32  src_start_pos,
RZ_NONNULL RzBitVector dst,
ut32  dst_start_pos,
ut32  nbit 
)

Copy n bits from start position of source to start position of dest, return num of copied bits

Parameters
srcRzBitVector, data source
src_start_posut32, start position in source bitvector of copy
dstRzBitVector, destination of copy
dst_start_posut32, start position in destination bitvector
nbitut32, control the size of copy (in bits)
Returns
copied_size ut32, Actual copied size

Definition at line 210 of file bitvector.c.

210  {
212 
213  ut32 max_nbit = RZ_MIN((src->len - src_start_pos),
214  (dst->len - dst_start_pos));
215 
216  // prevent overflow
217  if (max_nbit < nbit) {
218  return 0;
219  }
220 
221  // normal case here
222  for (ut32 i = 0; i < nbit; ++i) {
223  bool c = rz_bv_get(src, src_start_pos + i);
224  rz_bv_set(dst, dst_start_pos + i, c);
225  }
226 
227  return nbit;
228 }
#define RZ_MIN(x, y)
#define c(i)
Definition: sha256.c:43

References c, dst, i, rz_bv_get(), rz_bv_set(), RZ_MIN, rz_return_val_if_fail, and src.

Referenced by rz_bv_append(), rz_bv_lshift_fill(), rz_bv_rshift_fill(), rz_il_handler_cast(), rz_il_vm_sync_from_reg(), and rz_il_vm_sync_to_reg().

◆ rz_bv_ctz()

RZ_API ut32 rz_bv_ctz ( RZ_NONNULL RzBitVector bv)

Count trailing (least significant) zeroes All bits are considered trailing zeroes for a zero bitvector.

Definition at line 1123 of file bitvector.c.

1123  {
1124  rz_return_val_if_fail(bv, 0);
1125  ut32 r = 0;
1126  for (ut32 i = 0; i < rz_bv_len(bv); i++) {
1127  if (rz_bv_get(bv, i)) {
1128  break;
1129  }
1130  r++;
1131  }
1132  return r;
1133 }

References i, r, rz_bv_get(), rz_bv_len(), and rz_return_val_if_fail.

◆ rz_bv_cut_head()

RZ_API RZ_OWN RzBitVector* rz_bv_cut_head ( RZ_NONNULL RzBitVector bv,
ut32  delta_len 
)

Return a new bitvector, cut n zero bits from head

Parameters
bvRzBitVector, pointer to bitvector
delta_len,thenumber of zero bits
Returns
ret RzBitVector, pointert to the new btivector

Definition at line 281 of file bitvector.c.

281  {
283 
284  ut32 new_len = bv->len - delta_len;
285  RzBitVector *ret = rz_bv_new(new_len);
286  if (!ret) {
287  return NULL;
288  }
289 
290  for (ut32 pos = 0; pos < new_len; ++pos) {
291  rz_bv_set(ret, pos, rz_bv_get(bv, pos));
292  }
293 
294  return ret;
295 }

References NULL, pos, rz_bv_get(), rz_bv_new(), rz_bv_set(), and rz_return_val_if_fail.

◆ rz_bv_cut_tail()

RZ_API RZ_OWN RzBitVector* rz_bv_cut_tail ( RZ_NONNULL RzBitVector bv,
ut32  delta_len 
)

Return a new bitvector, cut n zero bits from tail

Parameters
bvRzBitVector, pointer to bitvector
delta_len,thenumber of zero bits
Returns
ret RzBitVector, pointert to the new btivector

Definition at line 303 of file bitvector.c.

303  {
305 
306  ut32 new_len = bv->len - delta_len;
307  RzBitVector *ret = rz_bv_new(new_len);
308  if (!ret) {
309  return NULL;
310  }
311 
312  ut32 pos, i;
313  for (pos = 0, i = delta_len; pos < new_len; ++i, ++pos) {
314  rz_bv_set(ret, pos, rz_bv_get(bv, i));
315  }
316 
317  return ret;
318 }

References i, NULL, pos, rz_bv_get(), rz_bv_new(), rz_bv_set(), and rz_return_val_if_fail.

◆ rz_bv_div()

Result of (x / y) mod 2^length Both operands must have the same length. If y is a zero vector, the result defined as a vector of all ones.

Parameters
xdividend
ydivisor
Returns
ret quotient, of the same length as the operands

Definition at line 808 of file bitvector.c.

808  {
809  rz_return_val_if_fail(x && y && x->len == y->len, NULL);
810 
811  if (rz_bv_is_zero_vector(y)) {
812  RzBitVector *ret = rz_bv_new(y->len);
813  rz_bv_set_all(ret, true);
814  return ret;
815  }
816 
817  if (x->len <= 64) {
818  return rz_bv_new_from_ut64(x->len, rz_bv_to_ut64(x) / rz_bv_to_ut64(y));
819  }
820 
821  int compare_result = bv_unsigned_cmp(x, y);
822  // dividend < divisor
823  // remainder = dividend, quotient = 0
824  if (compare_result < 0) {
825  return rz_bv_new(x->len);
826  }
827  // dividend == divisor
828  // remainder = 0, quotient = 1
829  if (compare_result == 0) {
830  return rz_bv_new_one(rz_bv_len(x));
831  }
832 
833  // dividend > divisor
834  // do typical division by shift and subtract
835  RzBitVector *dend = rz_bv_dup(x);
836  RzBitVector *sor = rz_bv_dup(y);
837  // shift the divisor left to align both highest bits
838  ut32 sorlz = rz_bv_clz(sor);
839  ut32 shift = sorlz - rz_bv_clz(dend);
840  rz_bv_lshift(sor, shift);
842  for (ut32 b = shift + 1; b; b--) {
843  if (rz_bv_ule(sor, dend)) {
844  rz_bv_set(quot, b - 1, true);
845  RzBitVector *tmp = rz_bv_sub(dend, sor, NULL);
846  rz_bv_free(dend);
847  dend = tmp;
848  }
849  rz_bv_rshift(sor, 1);
850  }
851  rz_bv_free(dend);
852  rz_bv_free(sor);
853  return quot;
854 }
static RZ_NULLABLE RzILOpBitVector * shift(RzILOpBitVector *val, RZ_NULLABLE RzILOpBool **carry_out, arm_shifter type, RZ_OWN RzILOpBitVector *dist)
Definition: arm_il32.c:190
RZ_API bool rz_bv_ule(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1052
RZ_API ut64 rz_bv_to_ut64(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1454
RZ_API bool rz_bv_is_zero_vector(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1021
RZ_API bool rz_bv_set_all(RZ_NONNULL RzBitVector *bv, bool b)
Definition: bitvector.c:367
RZ_API bool rz_bv_lshift(RZ_NONNULL RzBitVector *bv, ut32 size)
Definition: bitvector.c:446
RZ_API RZ_OWN RzBitVector * rz_bv_new_from_ut64(ut32 length, ut64 value)
Definition: bitvector.c:1161
RZ_API bool rz_bv_rshift(RZ_NONNULL RzBitVector *bv, ut32 size)
Definition: bitvector.c:457
RZ_API ut32 rz_bv_clz(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1107
RZ_API RZ_OWN RzBitVector * rz_bv_sub(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *borrow)
Definition: bitvector.c:715
int bv_unsigned_cmp(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:775
#define rz_bv_new_one(l)
Definition: rz_bitvector.h:106
#define rz_bv_new_zero(l)
Definition: rz_bitvector.h:105

References b, bv_unsigned_cmp(), NULL, rz_bv_clz(), rz_bv_dup(), rz_bv_free(), rz_bv_is_zero_vector(), rz_bv_len(), rz_bv_lshift(), rz_bv_new(), rz_bv_new_from_ut64(), rz_bv_new_one, rz_bv_new_zero, rz_bv_rshift(), rz_bv_set(), rz_bv_set_all(), rz_bv_sub(), rz_bv_to_ut64(), rz_bv_ule(), rz_return_val_if_fail, shift(), autogen_x86imm::tmp, and x.

Referenced by rz_bv_mod(), rz_bv_sdiv(), and rz_il_handler_div().

◆ rz_bv_dup()

Clone a bitvector

Parameters
bvRzBitVector, pointer to the source bitvector
Returns
dup RzBitVector, pointer to a new bitvector, which is a copy of source

Definition at line 167 of file bitvector.c.

167  {
169 
170  RzBitVector *new_bv = rz_bv_new(bv->len);
171  if (!new_bv || !rz_bv_copy(bv, new_bv)) {
172  rz_bv_free(new_bv);
173  return NULL;
174  }
175 
176  return new_bv;
177 }
RZ_API ut32 rz_bv_copy(RZ_NONNULL const RzBitVector *src, RZ_NONNULL RzBitVector *dst)
Definition: bitvector.c:186

References NULL, rz_bv_copy(), rz_bv_free(), rz_bv_new(), and rz_return_val_if_fail.

Referenced by rz_bv_complement_2(), rz_bv_div(), rz_bv_mod(), rz_il_effect_label_dup(), rz_il_event_mem_read_new(), rz_il_event_mem_write_new(), rz_il_event_pc_write_new(), rz_il_handler_bitv(), rz_il_handler_goto(), rz_il_handler_shiftl(), rz_il_handler_shiftr(), rz_il_handler_var(), rz_il_op_pure_dup(), rz_il_value_dup(), rz_il_value_to_bv(), rz_il_vm_create_label(), and rz_il_vm_update_label().

◆ rz_bv_eq()

RZ_API bool rz_bv_eq ( RZ_NONNULL RzBitVector x,
RZ_NONNULL RzBitVector y 
)

Check if x == y

Definition at line 1041 of file bitvector.c.

1041  {
1042  rz_return_val_if_fail(x && y, false);
1043  return rz_bv_len(x) == rz_bv_len(y) && bv_unsigned_cmp(x, y) == 0;
1044 }

References bv_unsigned_cmp(), rz_bv_len(), rz_return_val_if_fail, and x.

Referenced by rz_il_handler_eq(), and rz_il_value_eq().

◆ rz_bv_fini()

RZ_API void rz_bv_fini ( RZ_NONNULL RzBitVector bv)

Clear a RzBitVector structure.

Definition at line 58 of file bitvector.c.

58  {
60  if (bv->len > 64) {
61  free(bv->bits.large_a);
62  }
63  memset(bv, 0, sizeof(RzBitVector));
64 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
return memset(p, 0, total)
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References free(), memset(), and rz_return_if_fail.

Referenced by rz_bv_free(), rz_bv_lshift_fill(), rz_bv_mul(), and rz_bv_rshift_fill().

◆ rz_bv_free()

RZ_API void rz_bv_free ( RZ_NULLABLE RzBitVector bv)

Free a bitvector

Parameters
bvRzBitVector, pointer to the bitvector you want to free

Definition at line 85 of file bitvector.c.

85  {
86  if (!bv) {
87  return;
88  }
89  rz_bv_fini(bv);
90  free(bv);
91 }
RZ_API void rz_bv_fini(RZ_NONNULL RzBitVector *bv)
Clear a RzBitVector structure.
Definition: bitvector.c:58

References free(), and rz_bv_fini().

Referenced by bf_syscall_read(), bf_syscall_write(), perform_jump(), rz_bv_complement_1(), rz_bv_div(), rz_bv_dup(), rz_bv_mod(), rz_bv_mul(), rz_bv_sdiv(), rz_bv_smod(), rz_bv_sub(), rz_core_analysis_il_vm_set(), rz_il_effect_label_free(), rz_il_event_free(), rz_il_handler_add(), rz_il_handler_append(), rz_il_handler_cast(), rz_il_handler_div(), rz_il_handler_eq(), rz_il_handler_is_zero(), rz_il_handler_load(), rz_il_handler_loadw(), rz_il_handler_logical_and(), rz_il_handler_logical_not(), rz_il_handler_logical_or(), rz_il_handler_logical_xor(), rz_il_handler_lsb(), rz_il_handler_mod(), rz_il_handler_msb(), rz_il_handler_mul(), rz_il_handler_neg(), rz_il_handler_sdiv(), rz_il_handler_shiftl(), rz_il_handler_shiftr(), rz_il_handler_sle(), rz_il_handler_smod(), rz_il_handler_store(), rz_il_handler_storew(), rz_il_handler_sub(), rz_il_handler_ule(), rz_il_op_new_bitv_from_st64(), rz_il_op_new_bitv_from_ut64(), rz_il_op_pure_free(), rz_il_value_free(), rz_il_value_stringify(), rz_il_vm_fini(), rz_il_vm_mem_store(), rz_il_vm_mem_storew(), rz_il_vm_step(), rz_il_vm_sync_from_reg(), rz_il_vm_sync_to_reg(), and rz_il_vm_update_label().

◆ rz_bv_get()

RZ_API bool rz_bv_get ( RZ_NONNULL const RzBitVector bv,
ut32  pos 
)

Get bit at position from bitvector

Parameters
bvRzBitVector, pointer to bv
posint, position
Returns
ret bit, bool value of bit

Definition at line 429 of file bitvector.c.

429  {
430  rz_return_val_if_fail(bv && pos < bv->len, false);
431  if (bv->len <= 64) {
432  return (bv->bits.small_u >> pos) & 1;
433  }
434 
435  rz_return_val_if_fail(bv->bits.large_a, false);
436  return ((bv->bits.large_a)[pos / BV_ELEM_SIZE] & (1u << (pos % BV_ELEM_SIZE)));
437 }
size_t len
Definition: 6502dis.c:15
#define BV_ELEM_SIZE
Definition: bitvector.c:9

References BV_ELEM_SIZE, len, pos, and rz_return_val_if_fail.

Referenced by bv_unsigned_cmp(), rz_bv_add(), rz_bv_append_zero(), rz_bv_as_string(), rz_bv_clz(), rz_bv_cmp(), rz_bv_complement_2(), rz_bv_copy_nbits(), rz_bv_ctz(), rz_bv_cut_head(), rz_bv_cut_tail(), rz_bv_lsb(), rz_bv_msb(), rz_bv_mul(), rz_bv_prepend_zero(), rz_bv_to_ut16(), rz_bv_to_ut32(), rz_bv_to_ut64(), rz_bv_to_ut8(), and rz_bv_toggle().

◆ rz_bv_hash()

RZ_API ut32 rz_bv_hash ( RZ_NULLABLE RzBitVector x)

Calculates the hash from the bitvector data

Parameters
xBitVector
Returns
ut32 bitvector hash

Definition at line 1372 of file bitvector.c.

1372  {
1373  ut32 h = 5381;
1374  if (!x) {
1375  return h;
1376  }
1377 
1378  ut32 size = (x->len > 64) ? x->_elem_len : sizeof(x->bits.small_u);
1379  ut8 *bits = (x->len > 64) ? x->bits.large_a : (ut8 *)&x->bits.small_u;
1380  if (!size || !bits) {
1381  return h;
1382  }
1383 
1384  for (ut32 i = 0; i < size; ++i) {
1385  h = (h + (h << 5)) ^ bits[i];
1386  }
1387 
1388  h ^= x->len;
1389  return h;
1390 }
int bits(struct state *s, int need)
Definition: blast.c:72
voidpf void uLong size
Definition: ioapi.h:138
#define h(i)
Definition: sha256.c:48

References bits(), h, i, and x.

◆ rz_bv_init()

RZ_API bool rz_bv_init ( RZ_NONNULL RzBitVector bv,
ut32  length 
)

Initialize a RzBitVector structure.

Parameters
bvPointer to a uninitialized RzBitVector instance
lengthint, the length of bitvector
Returns
true if succeed

Definition at line 38 of file bitvector.c.

38  {
39  rz_return_val_if_fail(bv && length, false);
40  memset(bv, 0, sizeof(RzBitVector));
41  if (length > 64) {
42  // how much ut8 do we need to represent `length` bits ?
43  size_t real_elem_cnt = NELEM(length, BV_ELEM_SIZE);
44  ut8 *tmp = RZ_NEWS0(ut8, real_elem_cnt);
45  if (!tmp) {
46  return false;
47  }
48  bv->bits.large_a = tmp;
49  bv->_elem_len = real_elem_cnt;
50  }
51  bv->len = length;
52  return true;
53 }
#define NELEM(N, ELEMPER)
Definition: bitvector.c:8
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 long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282

References BV_ELEM_SIZE, length, memset(), NELEM, RZ_NEWS0, rz_return_val_if_fail, and autogen_x86imm::tmp.

Referenced by rz_bv_lshift_fill(), rz_bv_mul(), rz_bv_new(), and rz_bv_rshift_fill().

◆ rz_bv_is_zero_vector()

RZ_API bool rz_bv_is_zero_vector ( RZ_NONNULL const RzBitVector x)

Check if the bitvector is zero

Parameters
xRzBitVector, pointer to bv
Returns
ret bool, return true if bv is a zero bitvector, false if not

Definition at line 1021 of file bitvector.c.

1021  {
1022  rz_return_val_if_fail(x, false);
1023 
1024  if (x->len <= 64) {
1025  return x->bits.small_u == 0;
1026  }
1027 
1028  rz_return_val_if_fail(x->bits.large_a, false);
1029 
1030  for (ut32 i = 0; i < x->_elem_len; ++i) {
1031  if (x->bits.large_a[i] != 0) {
1032  return false;
1033  }
1034  }
1035  return true;
1036 }

References i, rz_return_val_if_fail, and x.

Referenced by rz_bv_div(), rz_bv_mod(), rz_il_handler_div(), rz_il_handler_is_zero(), and rz_il_vm_sync_to_reg().

◆ rz_bv_len()

RZ_API ut32 rz_bv_len ( RZ_NONNULL const RzBitVector bv)

Get the length of bitvector in bits

Parameters
bvRzBitVector
Returns
len ut32, length of bitvector in bits

Definition at line 1140 of file bitvector.c.

1140  {
1141  rz_return_val_if_fail(bv, 0);
1142  return bv->len;
1143 }

References rz_return_val_if_fail.

Referenced by adjust_unsigned(), rz_bv_clz(), rz_bv_ctz(), rz_bv_div(), rz_bv_eq(), rz_il_mem_store(), rz_il_value_get_sort(), rz_il_vm_get_pc_len(), rz_il_vm_mem_storew(), rz_il_vm_sync_from_reg(), rz_il_vm_sync_to_reg(), rz_reg_set_bv(), and VALIDATOR_PURE().

◆ rz_bv_len_bytes()

RZ_API ut32 rz_bv_len_bytes ( RZ_NONNULL const RzBitVector bv)

Get the length of bitvector in bytes

Parameters
bvRzBitVector
Returns
len ut32, length of bitvector in bytes

Definition at line 1150 of file bitvector.c.

1150  {
1151  rz_return_val_if_fail(bv, 0);
1152  return (bv->len + 7) >> 3;
1153 }

References rz_return_val_if_fail.

Referenced by read_n_bits(), rz_bv_set_to_bytes_be(), rz_bv_set_to_bytes_le(), and write_n_bits().

◆ rz_bv_lsb()

RZ_API bool rz_bv_lsb ( RZ_NONNULL RzBitVector bv)

Get the least significant bit of bitvector

Parameters
bvRzBitVector, operand
Returns
b bit, bool value of LSB

Definition at line 1012 of file bitvector.c.

1012  {
1013  return rz_bv_get(bv, 0);
1014 }

References rz_bv_get().

Referenced by rz_il_handler_lsb().

◆ rz_bv_lshift()

RZ_API bool rz_bv_lshift ( RZ_NONNULL RzBitVector bv,
ut32  size 
)

Left shift bitvector (WARN : This operation will change the bitvector in argument) Fill with zero bits when shift

Parameters
bvRzBitVector, pointert to bv
sizeint, shift bits
Returns
flag bool, success or not

Definition at line 446 of file bitvector.c.

446  {
447  return rz_bv_lshift_fill(bv, size, false);
448 }
RZ_API bool rz_bv_lshift_fill(RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
Definition: bitvector.c:469

References rz_bv_lshift_fill().

Referenced by rz_bv_div(), and rz_bv_mul().

◆ rz_bv_lshift_fill()

RZ_API bool rz_bv_lshift_fill ( RZ_NONNULL RzBitVector bv,
ut32  size,
bool  fill_bit 
)

Left shift bitvector (WARN : This operation will change the bitvector in argument) Fill the bitvector with fill_bit

Parameters
bvRzBitVector, pointert to bv
sizeint, shift bits
fill_bitbool, bit used in filling
Returns
flag bool, success or not

Definition at line 469 of file bitvector.c.

469  {
470  rz_return_val_if_fail(bv, false);
471 
472  // left shift
473  if (size == 0) {
474  return false;
475  }
476 
477  if (size >= bv->len) {
478  rz_bv_set_all(bv, fill_bit);
479  return true;
480  }
481 
483  if (!rz_bv_init(&tmp, bv->len)) {
484  return false;
485  }
486  rz_bv_set_all(&tmp, fill_bit);
487 
488  int copied_size = rz_bv_copy_nbits(bv, 0, &tmp, size, bv->len - size);
489  if (copied_size == 0) {
490  rz_bv_fini(&tmp);
491  return false;
492  }
493 
494  rz_bv_copy(&tmp, bv);
495  rz_bv_fini(&tmp);
496 
497  return true;
498 }
RZ_API bool rz_bv_init(RZ_NONNULL RzBitVector *bv, ut32 length)
Initialize a RzBitVector structure.
Definition: bitvector.c:38

References rz_bv_copy(), rz_bv_copy_nbits(), rz_bv_fini(), rz_bv_init(), rz_bv_set_all(), rz_return_val_if_fail, and autogen_x86imm::tmp.

Referenced by rz_bv_lshift(), and rz_il_handler_shiftl().

◆ rz_bv_mod()

Result of (x mod y) mod 2^length Both operands must have the same length. If y == 0, the result is x

Parameters
xdividend
ydivisor
Returns
x - ((x / y) * y)

Definition at line 865 of file bitvector.c.

865  {
866  rz_return_val_if_fail(x && y && x->len == y->len, NULL);
867  if (rz_bv_is_zero_vector(y)) {
868  return rz_bv_dup(x);
869  }
870  RzBitVector *quot = rz_bv_div(x, y);
871  RzBitVector *remul = rz_bv_mul(quot, y);
872  RzBitVector *r = rz_bv_sub(x, remul, NULL);
873  rz_bv_free(quot);
874  rz_bv_free(remul);
875  return r;
876 }
RZ_API RZ_OWN RzBitVector * rz_bv_mul(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:734
RZ_API RZ_OWN RzBitVector * rz_bv_div(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:808

References NULL, r, rz_bv_div(), rz_bv_dup(), rz_bv_free(), rz_bv_is_zero_vector(), rz_bv_mul(), rz_bv_sub(), rz_return_val_if_fail, and x.

Referenced by rz_bv_smod(), and rz_il_handler_mod().

◆ rz_bv_msb()

RZ_API bool rz_bv_msb ( RZ_NONNULL RzBitVector bv)

Get the most significant bit of bitvector

Parameters
bvRzBitVector, operand
Returns
b bit, bool value of MSB

Definition at line 1003 of file bitvector.c.

1003  {
1004  return rz_bv_get(bv, bv->len - 1);
1005 }

References rz_bv_get().

Referenced by rz_bv_sdiv(), rz_bv_sle(), rz_bv_smod(), and rz_il_handler_msb().

◆ rz_bv_mul()

Result of (x * y) mod 2^length Both operands must have the same length.

Parameters
xRzBitVector, Operand
yRzBitVector, Operand
Returns
ret RzBitVector, point to the new bitvector

Definition at line 734 of file bitvector.c.

734  {
736 
737  RzBitVector dump;
738  bool cur_bit = false;
739 
740  if (x->len != y->len) {
742  return NULL;
743  }
744 
745  if (!rz_bv_init(&dump, x->len)) {
746  return NULL;
747  }
748  RzBitVector *result = rz_bv_new(x->len);
749  if (!result) {
750  goto exit;
751  }
752  rz_bv_copy(x, &dump);
753 
754  for (ut32 i = 0; i < y->len; ++i) {
755  cur_bit = rz_bv_get(y, i);
756  if (cur_bit) {
757  RzBitVector *tmp = rz_bv_add(result, &dump, NULL);
758  rz_bv_free(result);
759  result = tmp;
760  }
761  rz_bv_lshift(&dump, 1);
762  }
763 
764 exit:
765  rz_bv_fini(&dump);
766  return result;
767 }
RZ_API RZ_OWN RzBitVector * rz_bv_add(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *carry)
Definition: bitvector.c:683

References test-lz4-list::exit, i, NULL, rz_bv_add(), rz_bv_copy(), rz_bv_fini(), rz_bv_free(), rz_bv_get(), rz_bv_init(), rz_bv_lshift(), rz_bv_new(), rz_return_val_if_fail, rz_warn_if_reached, autogen_x86imm::tmp, and x.

Referenced by rz_bv_mod(), and rz_il_handler_mul().

◆ rz_bv_new()

RZ_API RZ_OWN RzBitVector* rz_bv_new ( ut32  length)

New a length-bits bitvector

Parameters
lengthint, the length of bitvector
Returns
bv RzBitVector, pointer to the new bitvector instance

Definition at line 71 of file bitvector.c.

71  {
74  if (!bv || !rz_bv_init(bv, length)) {
75  free(bv);
76  return NULL;
77  }
78  return bv;
79 }
#define RZ_NEW(x)
Definition: rz_types.h:285

References free(), length, NULL, rz_bv_init(), RZ_NEW, and rz_return_val_if_fail.

Referenced by rz_bv_add(), rz_bv_and(), rz_bv_append(), rz_bv_append_zero(), rz_bv_complement_1(), rz_bv_cut_head(), rz_bv_cut_tail(), rz_bv_div(), rz_bv_dup(), rz_bv_mul(), rz_bv_new_from_bytes_be(), rz_bv_new_from_bytes_le(), rz_bv_new_from_st64(), rz_bv_new_from_ut64(), rz_bv_or(), rz_bv_prepend_zero(), rz_bv_xor(), rz_il_handler_cast(), and rz_il_handler_div().

◆ rz_bv_new_from_bytes_be()

RZ_API RZ_OWN RzBitVector* rz_bv_new_from_bytes_be ( RZ_IN RZ_NONNULL const ut8 buf,
ut32  bit_offset,
ut32  size 
)

Create a new bitvector of size bits and apply rz_bv_set_from_bytes_be() to it

Definition at line 1207 of file bitvector.c.

1207  {
1209  RzBitVector *bv = rz_bv_new(size);
1210  if (!bv) {
1211  return NULL;
1212  }
1213  rz_bv_set_from_bytes_be(bv, buf, bit_offset, size);
1214  return bv;
1215 }
RZ_API void rz_bv_set_from_bytes_be(RZ_NONNULL RzBitVector *bv, RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
Set the bitvector's contents from the given bits. The bitvector's size is unchanged....
Definition: bitvector.c:1296
voidpf void * buf
Definition: ioapi.h:138

References NULL, rz_bv_new(), rz_bv_set_from_bytes_be(), and rz_return_val_if_fail.

Referenced by rz_reg_get_bv().

◆ rz_bv_new_from_bytes_le()

RZ_API RZ_OWN RzBitVector* rz_bv_new_from_bytes_le ( RZ_IN RZ_NONNULL const ut8 buf,
ut32  bit_offset,
ut32  size 
)

Create a new bitvector of size bits and apply rz_bv_set_from_bytes_le() to it

Definition at line 1194 of file bitvector.c.

1194  {
1196  RzBitVector *bv = rz_bv_new(size);
1197  if (!bv) {
1198  return NULL;
1199  }
1200  rz_bv_set_from_bytes_le(bv, buf, bit_offset, size);
1201  return bv;
1202 }
RZ_API void rz_bv_set_from_bytes_le(RZ_NONNULL RzBitVector *bv, RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
Definition: bitvector.c:1265

References NULL, rz_bv_new(), rz_bv_set_from_bytes_le(), and rz_return_val_if_fail.

Referenced by rz_reg_get_bv().

◆ rz_bv_new_from_st64()

RZ_API RZ_OWN RzBitVector* rz_bv_new_from_st64 ( ut32  length,
st64  value 
)

Convert st64 to length-bits bitvector

Parameters
lengthut32, length of bitvector
valuest64, the value to convert
Returns
bv RzBitVector, pointer to new bitvector

Definition at line 1179 of file bitvector.c.

1179  {
1181 
1182  RzBitVector *bv = rz_bv_new(length);
1183  if (!bv) {
1184  RZ_LOG_ERROR("RzIL: failed to allocate RzBitVector\n");
1185  return NULL;
1186  }
1188  return bv;
1189 }
RZ_API bool rz_bv_set_from_st64(RZ_NONNULL RzBitVector *bv, st64 value)
Definition: bitvector.c:1243
static int value
Definition: cmd_api.c:93
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58

References length, NULL, rz_bv_new(), rz_bv_set_from_st64(), RZ_LOG_ERROR, rz_return_val_if_fail, and value.

Referenced by rz_il_op_new_bitv_from_st64().

◆ rz_bv_new_from_ut64()

RZ_API RZ_OWN RzBitVector* rz_bv_new_from_ut64 ( ut32  length,
ut64  value 
)

Convert ut64 to length-bits bitvector

Parameters
lengthut32, length of bitvector
valueut64, the value to convert
Returns
bv RzBitVector, pointer to new bitvector

Definition at line 1161 of file bitvector.c.

1161  {
1163 
1164  RzBitVector *bv = rz_bv_new(length);
1165  if (!bv) {
1166  RZ_LOG_ERROR("RzIL: failed to allocate RzBitVector\n");
1167  return NULL;
1168  }
1170  return bv;
1171 }
RZ_API bool rz_bv_set_from_ut64(RZ_NONNULL RzBitVector *bv, ut64 value)
Definition: bitvector.c:1222

References length, NULL, rz_bv_new(), rz_bv_set_from_ut64(), RZ_LOG_ERROR, rz_return_val_if_fail, and value.

Referenced by bf_syscall_read(), il_config(), rz_bv_div(), rz_core_analysis_il_vm_set(), rz_il_mem_load(), rz_il_op_new_bitv_from_ut64(), rz_il_value_to_bv(), rz_il_vm_init(), and rz_il_vm_step().

◆ rz_bv_or()

Result of x OR y (or operation to every bits) Both operands must have the same length.

Parameters
xRzBitVector, operand
yRzBitVector, operand
Returns
ret RzBitVector, a new bitvector, which is the result of OR

Definition at line 573 of file bitvector.c.

573  {
575  if (x->len != y->len) {
576  return NULL;
577  }
578 
579  RzBitVector *ret = rz_bv_new(x->len);
580  if (!ret) {
581  return NULL;
582  } else if (x->len <= 64) {
583  ret->bits.small_u = x->bits.small_u | y->bits.small_u;
584  return ret;
585  }
586 
587  for (ut32 i = 0; i < ret->_elem_len; ++i) {
588  ret->bits.large_a[i] = x->bits.large_a[i] | y->bits.large_a[i];
589  }
590  return ret;
591 }

References bitvector_t::_elem_len, bitvector_t::bits, i, bitvector_t::large_a, NULL, rz_bv_new(), rz_return_val_if_fail, bitvector_t::small_u, and x.

Referenced by rz_il_handler_logical_or().

◆ rz_bv_prepend_zero()

RZ_API RZ_OWN RzBitVector* rz_bv_prepend_zero ( RZ_NONNULL RzBitVector bv,
ut32  delta_len 
)

Return a new bitvector prepended with bv with n zero bits

Parameters
bvRzBitVector, pointer to bitvector instance
delta_lenut32, the number of zero bits
Returns
ret RzBitVector, pointer to the new bitvector instance

Definition at line 236 of file bitvector.c.

236  {
238 
239  ut32 new_len = bv->len + delta_len;
240  RzBitVector *ret = rz_bv_new(new_len);
241  if (ret == NULL) {
242  return NULL;
243  }
244 
245  for (ut32 i = 0; i < bv->len; ++i) {
246  rz_bv_set(ret, i, rz_bv_get(bv, i));
247  }
248 
249  return ret;
250 }

References i, NULL, rz_bv_get(), rz_bv_new(), rz_bv_set(), and rz_return_val_if_fail.

◆ rz_bv_rshift()

RZ_API bool rz_bv_rshift ( RZ_NONNULL RzBitVector bv,
ut32  size 
)

Right shift bitvector (WARN : This operation will change the bitvector in argument) Fill with zero bits when shift

Parameters
bvRzBitVector, pointert to bv
sizeint, shift bits
Returns
flag bool, success or not

Definition at line 457 of file bitvector.c.

457  {
458  return rz_bv_rshift_fill(bv, size, false);
459 }
RZ_API bool rz_bv_rshift_fill(RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
Definition: bitvector.c:508

References rz_bv_rshift_fill().

Referenced by rz_bv_div().

◆ rz_bv_rshift_fill()

RZ_API bool rz_bv_rshift_fill ( RZ_NONNULL RzBitVector bv,
ut32  size,
bool  fill_bit 
)

Right shift bitvector (WARN : This operation will change the bitvector in argument) Fill the bitvector with fill_bit

Parameters
bvRzBitVector, pointert to bv
sizeint, shift bits
fill_bitbool, bit used in filling
Returns
flag bool, success or not

Definition at line 508 of file bitvector.c.

508  {
509  rz_return_val_if_fail(bv, false);
510 
511  // left shift
512  if (size == 0) {
513  return false;
514  }
515 
516  if (size >= bv->len) {
517  rz_bv_set_all(bv, fill_bit);
518  return true;
519  }
520 
522  if (!rz_bv_init(&tmp, bv->len)) {
523  return false;
524  }
525  rz_bv_set_all(&tmp, fill_bit);
526 
527  int copied_size = rz_bv_copy_nbits(bv, size, &tmp, 0, bv->len - size);
528  if (copied_size == 0) {
529  rz_bv_fini(&tmp);
530  return false;
531  }
532 
533  rz_bv_copy(&tmp, bv);
534  rz_bv_fini(&tmp);
535 
536  return true;
537 }

References rz_bv_copy(), rz_bv_copy_nbits(), rz_bv_fini(), rz_bv_init(), rz_bv_set_all(), rz_return_val_if_fail, and autogen_x86imm::tmp.

Referenced by rz_bv_rshift(), and rz_il_handler_shiftr().

◆ rz_bv_sdiv()

Result of (x / y) mod 2^length (signed algorithm) / | div x y : if not mx /\ not my | neg (div (neg x) y) if mx /\ not my x sdiv y = < | neg (div x (neg y)) if not mx /\ my | div (neg x) (neg y) if mx /\ my \

where mx = msb x, and my = msb y.

Parameters
xRzBitVector, Operand
yRzBitVector, Operand
Returns
ret RzBitVector, point to the new bitvector

Definition at line 893 of file bitvector.c.

893  {
895  bool mx = rz_bv_msb(x);
896  bool my = rz_bv_msb(y);
897 
898  RzBitVector *neg_x, *neg_y, *tmp, *ret;
899 
900  if ((!mx) && (!my)) {
901  return rz_bv_div(x, y);
902  }
903 
904  if ((mx) && (!my)) {
905  neg_x = rz_bv_neg(x);
906  tmp = rz_bv_div(neg_x, y);
907  ret = rz_bv_neg(tmp);
908 
909  rz_bv_free(tmp);
910  rz_bv_free(neg_x);
911  return ret;
912  }
913 
914  if ((!mx) && (my)) {
915  neg_y = rz_bv_neg(y);
916  tmp = rz_bv_div(x, neg_y);
917  ret = rz_bv_neg(tmp);
918 
919  rz_bv_free(tmp);
920  rz_bv_free(neg_y);
921  return ret;
922  }
923 
924  if (mx && my) {
925  neg_x = rz_bv_neg(x);
926  neg_y = rz_bv_neg(y);
927 
928  ret = rz_bv_div(neg_x, neg_y);
929  rz_bv_free(neg_x);
930  rz_bv_free(neg_y);
931  return ret;
932  }
933 
934  return NULL; // something wrong
935 }
RZ_API bool rz_bv_msb(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1003
#define rz_bv_neg
Definition: rz_bitvector.h:58

References NULL, rz_bv_div(), rz_bv_free(), rz_bv_msb(), rz_bv_neg, rz_return_val_if_fail, autogen_x86imm::tmp, and x.

Referenced by rz_il_handler_sdiv().

◆ rz_bv_set()

RZ_API bool rz_bv_set ( RZ_NONNULL RzBitVector bv,
ut32  pos,
bool  b 
)

Set a bit at position to true or false

Parameters
bvRzBitVector, pointer to bv
posut32, position
bbit, true or false (set or unset)
Returns
ret bool, bool value at pos after this operation

Definition at line 341 of file bitvector.c.

341  {
342  rz_return_val_if_fail(bv && pos < bv->len, false);
343  if (bv->len <= 64) {
344  if (b) {
345  bv->bits.small_u |= (1ull << pos);
346  } else {
347  bv->bits.small_u &= ~(1ull << pos);
348  }
349  return b;
350  }
351  rz_return_val_if_fail(bv->bits.large_a, false);
352 
353  if (b) {
354  bv->bits.large_a[pos / BV_ELEM_SIZE] |= (1u << (pos % BV_ELEM_SIZE));
355  } else {
356  bv->bits.large_a[pos / BV_ELEM_SIZE] &= ~(1u << (pos % BV_ELEM_SIZE));
357  }
358  return b;
359 }

References b, BV_ELEM_SIZE, len, pos, and rz_return_val_if_fail.

Referenced by rz_bv_add(), rz_bv_append_zero(), rz_bv_copy_nbits(), rz_bv_cut_head(), rz_bv_cut_tail(), rz_bv_div(), rz_bv_prepend_zero(), rz_bv_set_from_bytes_be(), rz_bv_set_from_bytes_le(), rz_bv_set_from_st64(), rz_bv_set_from_ut64(), and rz_bv_toggle().

◆ rz_bv_set_all()

RZ_API bool rz_bv_set_all ( RZ_NONNULL RzBitVector bv,
bool  b 
)

Set all bits to true or false

Parameters
bvRzBitVector, pointer to bv
bbit, true or false (set or unset)
Returns
ret bool, bool value at every positions after this operation

Definition at line 367 of file bitvector.c.

367  {
368  rz_return_val_if_fail(bv, false);
369 
370  if (bv->len <= 64) {
371  bv->bits.small_u = b ? UT64_MAX >> (64 - bv->len) : 0;
372  return b;
373  }
374 
375  rz_return_val_if_fail(bv->bits.large_a, false);
376  if (b) {
377  memset(bv->bits.large_a, 0xff, bv->_elem_len);
378  ut32 mod = bv->len % BV_ELEM_SIZE;
379  if (mod) {
380  bv->bits.large_a[bv->len / BV_ELEM_SIZE] = rz_num_bitmask(mod);
381  }
382  } else {
383  memset(bv->bits.large_a, 0, bv->_elem_len);
384  }
385 
386  return b;
387 }
int mod(int a, int b)
Definition: crypto_rot.c:8
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

References b, BV_ELEM_SIZE, memset(), mod(), rz_num_bitmask(), rz_return_val_if_fail, and UT64_MAX.

Referenced by rz_bv_div(), rz_bv_lshift_fill(), rz_bv_rshift_fill(), rz_il_handler_cast(), rz_il_handler_div(), and rz_il_vm_sync_from_reg().

◆ rz_bv_set_from_bytes_be()

RZ_API void rz_bv_set_from_bytes_be ( RZ_NONNULL RzBitVector bv,
RZ_IN RZ_NONNULL const ut8 buf,
ut32  bit_offset,
ut32  size 
)

Set the bitvector's contents from the given bits. The bitvector's size is unchanged. If bv->len < size, additional bits are cut off, if bv->len > size, the rest is filled up with 0.

Parameters
bufbig endian buffer of at least (bit_offset + size + 7) / 8 bytes
bit_offsetoffset inside buf to start reading from, in bits
sizenumber of bits to read from buf

Definition at line 1296 of file bitvector.c.

1296  {
1298  size = RZ_MIN(size, bv->len);
1299  // upper bits goes always in the upper bit of the bitv
1300  for (ut32 i = 0; i < bv->len; i++) {
1301  bool bit = false;
1302  if (i < size) {
1303  ut32 idx = (bit_offset + i) >> 3;
1304  ut32 sh = ((bit_offset + i) & 7);
1305  ut8 b8 = buf[idx];
1306  b8 = (size < 8) ? reverse_lt_8bits(b8, size) : (ut8)reverse_byte(b8);
1307  bit = (b8 >> sh) & 1;
1308  }
1309  rz_bv_set(bv, bv->len - 1 - i, bit);
1310  }
1311 }
#define reverse_byte(x)
Definition: bitvector.c:13
ut8 reverse_lt_8bits(ut8 x, ut8 w)
Definition: bitvector.c:17
RzCryptoSelector bit
Definition: crypto.c:16
#define ut8
Definition: dcpu16.h:8
int idx
Definition: setup.py:197

References bit, i, setup::idx, reverse_byte, reverse_lt_8bits(), rz_bv_set(), RZ_MIN, rz_return_if_fail, and ut8.

Referenced by read_n_bits(), and rz_bv_new_from_bytes_be().

◆ rz_bv_set_from_bytes_le()

RZ_API void rz_bv_set_from_bytes_le ( RZ_NONNULL RzBitVector bv,
RZ_IN RZ_NONNULL const ut8 buf,
ut32  bit_offset,
ut32  size 
)

Set the bitvector's contents from the given bits. The bitvector's size is unchanged. If bv->len < size, additional bits are cut off, if bv->len > size, the rest is filled up with 0.

Parameters
buflittle endian buffer of at least (bit_offset + size + 7) / 8 bytes
bit_offsetoffset inside buf to start reading from, in bits
sizenumber of bits to read from buf

Definition at line 1265 of file bitvector.c.

1265  {
1267  size = RZ_MIN(size, bv->len);
1268  if (!bit_offset && size <= 64) {
1269  ut64 val = 0;
1270  for (ut32 i = 0; i < (size + 7) / 8; i++) {
1271  val |= (ut64)buf[i] << (i * 8);
1272  }
1273  val &= (UT64_MAX >> (64 - size));
1274  rz_bv_set_from_ut64(bv, val);
1275  return;
1276  }
1277  for (ut32 i = 0; i < bv->len; i++) {
1278  bool bit = false;
1279  if (i < size) {
1280  ut32 idx = (bit_offset + i) >> 3;
1281  ut32 sh = (bit_offset + i) & 7;
1282  bit = (buf[idx] >> sh) & 1;
1283  }
1284  rz_bv_set(bv, i, bit);
1285  }
1286 }
ut16 val
Definition: armass64_const.h:6
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References bit, i, setup::idx, rz_bv_set(), rz_bv_set_from_ut64(), RZ_MIN, rz_return_if_fail, ut64(), UT64_MAX, and val.

Referenced by read_n_bits(), and rz_bv_new_from_bytes_le().

◆ rz_bv_set_from_st64()

RZ_API bool rz_bv_set_from_st64 ( RZ_NONNULL RzBitVector bv,
st64  value 
)

Convert st64 to N-bits bitvector

Parameters
bvRzBitVector, pointer to bitvector
valuest64, the value to convert

Definition at line 1243 of file bitvector.c.

1243  {
1244  rz_return_val_if_fail(bv, false);
1245  if (bv->len <= 64) {
1246  bv->bits.small_u = *((ut64 *)&value);
1247  bv->bits.small_u &= (UT64_MAX >> (64 - bv->len));
1248  return true;
1249  }
1250 
1251  for (ut32 i = 0; i < bv->len; ++i) {
1252  rz_bv_set(bv, i, value & 1);
1253  value >>= 1;
1254  }
1255  return true;
1256 }

References i, rz_bv_set(), rz_return_val_if_fail, ut64(), UT64_MAX, and value.

Referenced by rz_bv_new_from_st64().

◆ rz_bv_set_from_ut64()

RZ_API bool rz_bv_set_from_ut64 ( RZ_NONNULL RzBitVector bv,
ut64  value 
)

Convert ut64 to N-bits bitvector

Parameters
bvRzBitVector, pointer to bitvector
valueut64, the value to convert

Definition at line 1222 of file bitvector.c.

1222  {
1223  rz_return_val_if_fail(bv, false);
1224 
1225  if (bv->len <= 64) {
1226  bv->bits.small_u = value;
1227  bv->bits.small_u &= (UT64_MAX >> (64 - bv->len));
1228  return true;
1229  }
1230 
1231  for (ut32 i = 0; i < bv->len; ++i) {
1232  rz_bv_set(bv, i, value & 1);
1233  value >>= 1;
1234  }
1235  return true;
1236 }

References i, rz_bv_set(), rz_return_val_if_fail, UT64_MAX, and value.

Referenced by rz_bv_new_from_ut64(), rz_bv_set_from_bytes_le(), and rz_il_vm_sync_to_reg().

◆ rz_bv_set_to_bytes_be()

RZ_API void rz_bv_set_to_bytes_be ( RZ_NONNULL const RzBitVector bv,
RZ_OUT RZ_NONNULL ut8 buf 
)

Set the buffer contents from the given bitvector's bits in big endian format.

Parameters
bvBitVector to use as source of the bits
bufbuffer to write big endian data.

Definition at line 1349 of file bitvector.c.

1349  {
1350  rz_return_if_fail(bv && buf);
1351  ut32 bytes = rz_bv_len_bytes(bv);
1352  if (bv->len > 64) {
1353  ut32 end = bytes - 1;
1354  for (ut32 i = 0; i < bytes; i++) {
1355  buf[end - i] = bv->bits.large_a[i];
1356  }
1357  return;
1358  }
1359  ut64 val = bv->bits.small_u;
1360  for (ut32 i = bytes - 1; i; i--) {
1361  buf[i] = val & 0xFF;
1362  val >>= 8;
1363  }
1364  buf[0] = val & 0xFF;
1365 }
static ut8 bytes[32]
Definition: asm_arc.c:23
RZ_API ut32 rz_bv_len_bytes(RZ_NONNULL const RzBitVector *bv)
Definition: bitvector.c:1150

References bytes, test_evm::end, i, rz_bv_len_bytes(), rz_return_if_fail, ut64(), and val.

Referenced by rz_reg_set_bv(), and write_n_bits().

◆ rz_bv_set_to_bytes_le()

RZ_API void rz_bv_set_to_bytes_le ( RZ_NONNULL const RzBitVector bv,
RZ_OUT RZ_NONNULL ut8 buf 
)

Set the buffer contents from the given bitvector's bits in little endian format.

Parameters
bvBitVector to use as source of the bits
bufbuffer to write little endian data.

Definition at line 1318 of file bitvector.c.

1318  {
1319  rz_return_if_fail(bv && buf);
1320  ut32 bytes = rz_bv_len_bytes(bv);
1321  if (bv->len > 64) {
1322  for (ut32 i = 0; i < bytes; i++) {
1323  if (i + 1 == bytes && bv->len % 8) {
1324  buf[i] &= (0xff << (bv->len % 8)) & 0xff;
1325  buf[i] |= bv->bits.large_a[i];
1326  } else {
1327  buf[i] = bv->bits.large_a[i];
1328  }
1329  }
1330  return;
1331  }
1332  ut64 val = bv->bits.small_u;
1333  for (ut32 i = 0; i < bytes; i++) {
1334  if (i + 1 == bytes && bv->len % 8) {
1335  buf[i] &= (0xff << (bv->len % 8)) & 0xff;
1336  buf[i] |= val & 0xff;
1337  } else {
1338  buf[i] = val & 0xff;
1339  }
1340  val >>= 8;
1341  }
1342 }

References bytes, i, rz_bv_len_bytes(), rz_return_if_fail, ut64(), and val.

Referenced by rz_reg_set_bv(), and write_n_bits().

◆ rz_bv_sle()

RZ_API bool rz_bv_sle ( RZ_NONNULL RzBitVector x,
RZ_NONNULL RzBitVector y 
)

Check if x <= y (as signed value)

Parameters
xRzBitVector, operand
yRzBitVector, operand
Returns
ret bool, return true if x <= y, else return false

Definition at line 1064 of file bitvector.c.

1064  {
1065  rz_return_val_if_fail(x && y, false);
1066  bool x_msb = rz_bv_msb(x);
1067  bool y_msb = rz_bv_msb(y);
1068 
1069  if (x_msb == y_msb) {
1070  return rz_bv_ule(x, y);
1071  }
1072 
1073  // if x_msb set, y_msb unset => x < y
1074  // if x_msb unset, y_msb set => x > y
1075  // x != y when reaches here
1076  return x_msb;
1077 }

References rz_bv_msb(), rz_bv_ule(), rz_return_val_if_fail, and x.

Referenced by rz_il_handler_sle().

◆ rz_bv_smod()

Result of (x mod y) mod 2^length (signed algorithm) / | x % y : if not mx /\ not my | neg (neg x % y) if mx /\ not my x smodulo y = < | neg (x % (neg y)) if not mx /\ my | neg (neg x % neg y) mod m if mx /\ my \

where mx = msb x and my = msb y.

Parameters
xRzBitVector, Operand
yRzBitVector, Operand
Returns
ret RzBitVector, point to the new bitvector

Definition at line 952 of file bitvector.c.

952  {
954  bool mx = rz_bv_msb(x);
955  bool my = rz_bv_msb(y);
956 
957  RzBitVector *neg_x, *neg_y, *tmp, *ret;
958 
959  if ((!mx) && (!my)) {
960  return rz_bv_mod(x, y);
961  }
962 
963  if ((mx) && (!my)) {
964  neg_x = rz_bv_neg(x);
965  tmp = rz_bv_mod(neg_x, y);
966  ret = rz_bv_neg(tmp);
967 
968  rz_bv_free(tmp);
969  rz_bv_free(neg_x);
970  return ret;
971  }
972 
973  if ((!mx) && (my)) {
974  neg_y = rz_bv_neg(y);
975  tmp = rz_bv_mod(x, neg_y);
976  ret = rz_bv_neg(tmp);
977 
978  rz_bv_free(tmp);
979  rz_bv_free(neg_y);
980  return ret;
981  }
982 
983  if (mx && my) {
984  neg_x = rz_bv_neg(x);
985  neg_y = rz_bv_neg(y);
986 
987  tmp = rz_bv_mod(neg_x, neg_y);
988  ret = rz_bv_neg(tmp);
989  rz_bv_free(neg_x);
990  rz_bv_free(neg_y);
991  rz_bv_free(tmp);
992  return ret;
993  }
994 
995  return NULL; // something wrong
996 }
RZ_API RZ_OWN RzBitVector * rz_bv_mod(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:865

References NULL, rz_bv_free(), rz_bv_mod(), rz_bv_msb(), rz_bv_neg, rz_return_val_if_fail, autogen_x86imm::tmp, and x.

Referenced by rz_il_handler_smod().

◆ rz_bv_sub()

Result of (x - y) mod 2^length Both operands must have the same length.

Parameters
xRzBitVector, Operand
yRzBitVector, Operand
borrowbool*, bool pointer to where to save the borrow value.
Returns
ret RzBitVector, point to the new bitvector

Definition at line 715 of file bitvector.c.

715  {
717 
718  RzBitVector *ret;
719  RzBitVector *neg_y;
720 
721  neg_y = rz_bv_neg(y);
722  ret = rz_bv_add(x, neg_y, borrow);
723  rz_bv_free(neg_y);
724  return ret;
725 }

References NULL, rz_bv_add(), rz_bv_free(), rz_bv_neg, rz_return_val_if_fail, and x.

Referenced by rz_bv_div(), rz_bv_mod(), and rz_il_handler_sub().

◆ rz_bv_to_ut16()

RZ_API ut16 rz_bv_to_ut16 ( RZ_NONNULL const RzBitVector x)

Convert bitv to ut16 value

Parameters
xBitVector
Returns
ut16 value

Definition at line 1416 of file bitvector.c.

1416  {
1418  if (x->len <= 64) {
1419  return (ut16)x->bits.small_u & UT16_MAX;
1420  }
1421  ut16 ret = 0;
1422  for (ut32 i = 0; i < x->len && i < 16; ++i) {
1423  if (rz_bv_get(x, i)) {
1424  ret |= 1 << i;
1425  }
1426  }
1427  return ret;
1428 }
uint16_t ut16
#define UT16_MAX

References i, rz_bv_get(), rz_return_val_if_fail, UT16_MAX, and x.

◆ rz_bv_to_ut32()

RZ_API ut32 rz_bv_to_ut32 ( RZ_NONNULL const RzBitVector x)

Convert bitv to ut32 value

Parameters
xBitVector
Returns
ut32 value

Definition at line 1435 of file bitvector.c.

1435  {
1437  if (x->len <= 64) {
1438  return (ut32)x->bits.small_u & UT32_MAX;
1439  }
1440  ut32 ret = 0;
1441  for (ut32 i = 0; i < x->len && i < 32; ++i) {
1442  if (rz_bv_get(x, i)) {
1443  ret |= 1 << i;
1444  }
1445  }
1446  return ret;
1447 }
#define UT32_MAX
Definition: rz_types_base.h:99

References i, rz_bv_get(), rz_return_val_if_fail, UT32_MAX, and x.

Referenced by bf_syscall_write(), rz_il_handler_shiftl(), and rz_il_handler_shiftr().

◆ rz_bv_to_ut64()

RZ_API ut64 rz_bv_to_ut64 ( RZ_NONNULL const RzBitVector x)

Convert RzBitVector to ut64

Parameters
xRzBitVector, pointer to the bitvector
Returns
ret ut64, num value of bitvector

Definition at line 1454 of file bitvector.c.

1454  {
1456  if (x->len <= 64) {
1457  return x->bits.small_u;
1458  }
1459  ut64 ret = 0;
1460  for (ut32 i = 0; i < x->len && i < 64; ++i) {
1461  if (rz_bv_get(x, i)) {
1462  ret |= 1ULL << i;
1463  }
1464  }
1465  return ret;
1466 }

References i, rz_bv_get(), rz_return_val_if_fail, ut64(), and x.

Referenced by read_n_bits(), rz_analysis_il_vm_step(), rz_bv_div(), rz_il_mem_load(), rz_il_mem_store(), rz_reg_set_bv(), and write_n_bits().

◆ rz_bv_to_ut8()

RZ_API ut8 rz_bv_to_ut8 ( RZ_NONNULL const RzBitVector x)

Convert bitv to a ut8 value

Parameters
xBitVector
Returns
ut8 value

Definition at line 1397 of file bitvector.c.

1397  {
1399  if (x->len <= 64) {
1400  return (ut8)x->bits.small_u & UT8_MAX;
1401  }
1402  ut8 ret = 0;
1403  for (ut32 i = 0; i < x->len && i < 8; ++i) {
1404  if (rz_bv_get(x, i)) {
1405  ret |= 1 << i;
1406  }
1407  }
1408  return ret;
1409 }
#define UT8_MAX

References i, rz_bv_get(), rz_return_val_if_fail, UT8_MAX, and x.

Referenced by rz_il_mem_store().

◆ rz_bv_toggle()

RZ_API bool rz_bv_toggle ( RZ_NONNULL RzBitVector bv,
ut32  pos 
)

Invert a bit at position

Parameters
bvRzBitVector, pointer to bv
posut32, position
bbit, true or false (set or unset)
Returns
ret bool, bool value at pos after this operation

Definition at line 396 of file bitvector.c.

396  {
397  rz_return_val_if_fail(bv, false);
398  bool cur_bit = rz_bv_get(bv, pos);
399  bool new_bit = !cur_bit;
400  rz_bv_set(bv, pos, new_bit);
401  return new_bit;
402 }

References pos, rz_bv_get(), rz_bv_set(), and rz_return_val_if_fail.

Referenced by rz_bv_complement_2().

◆ rz_bv_toggle_all()

RZ_API bool rz_bv_toggle_all ( RZ_NONNULL RzBitVector bv)

Invert all bits

Parameters
bvRzBitVector, pointer to bv
bbit, true or false (set or unset)
Returns
ret bool, bool value at every positions after this operation

Definition at line 410 of file bitvector.c.

410  {
411  rz_return_val_if_fail(bv, false);
412  if (bv->len <= 64) {
413  bv->bits.small_u = ~(bv->bits.small_u);
414  }
415 
416  rz_return_val_if_fail(bv->bits.large_a, false);
417  for (ut32 i = 0; i < bv->_elem_len; ++i) {
418  bv->bits.large_a[i] = ~(bv->bits.large_a[i]);
419  }
420  return true;
421 }

References i, and rz_return_val_if_fail.

◆ rz_bv_ule()

RZ_API bool rz_bv_ule ( RZ_NONNULL RzBitVector x,
RZ_NONNULL RzBitVector y 
)

Check if x <= y (as unsigned value)

Parameters
xRzBitVector, operand
yRzBitVector, operand
Returns
ret bool, return true if x <= y, else return false

Definition at line 1052 of file bitvector.c.

1052  {
1053  rz_return_val_if_fail(x && y, false);
1054  // x > y ? return false : return true
1055  return bv_unsigned_cmp(x, y) <= 0;
1056 }

References bv_unsigned_cmp(), rz_return_val_if_fail, and x.

Referenced by rz_bv_div(), rz_bv_sle(), and rz_il_handler_ule().

◆ rz_bv_xor()

Result of x XOR y (xor operation to every bits) Both operands must have the same length.

Parameters
xRzBitVector, operand
yRzBitVector, operand
Returns
ret RzBitVector, a new bitvector, which is the result of XOR

Definition at line 600 of file bitvector.c.

600  {
602  if (x->len != y->len) {
603  return NULL;
604  }
605 
606  RzBitVector *ret = rz_bv_new(x->len);
607  if (!ret) {
608  return NULL;
609  } else if (x->len <= 64) {
610  ret->bits.small_u = x->bits.small_u ^ y->bits.small_u;
611  return ret;
612  }
613 
614  for (ut32 i = 0; i < ret->_elem_len; ++i) {
615  ret->bits.large_a[i] = x->bits.large_a[i] ^ y->bits.large_a[i];
616  }
617  return ret;
618 }

References bitvector_t::_elem_len, bitvector_t::bits, i, bitvector_t::large_a, NULL, rz_bv_new(), rz_return_val_if_fail, bitvector_t::small_u, and x.

Referenced by rz_il_handler_logical_xor().