Rizin
unix-like reverse engineering framework and cli tools
bitvector.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 heersin <teablearcher@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_util.h"
5 #include <stdlib.h>
6 #include <stdio.h>
7 
8 #define NELEM(N, ELEMPER) ((N + (ELEMPER)-1) / (ELEMPER))
9 #define BV_ELEM_SIZE 8U
10 
11 // optimization for reversing 8 bits which uses 32 bits
12 // https://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits
13 #define reverse_byte(x) ((((x)*0x0802LU & 0x22110LU) | ((x)*0x8020LU & 0x88440LU)) * 0x10101LU >> 16)
14 
15 // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
16 // With changes.
18  ut8 m = ~(UT8_MAX << w); // values bitmask
19  ut8 v = (x & m); // input bits to be reversed
20  ut8 r = v; // r will be reversed bits of v; first get LSB of v
21  int s = w - 1; // extra shift needed at end
22 
23  for (v >>= 1; v; v >>= 1) {
24  r <<= 1;
25  r |= v & 1;
26  s--;
27  }
28  r <<= s; // shift when v's highest bits are zero
29  return r;
30 }
31 
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 }
54 
60  if (bv->len > 64) {
61  free(bv->bits.large_a);
62  }
63  memset(bv, 0, sizeof(RzBitVector));
64 }
65 
74  if (!bv || !rz_bv_init(bv, length)) {
75  free(bv);
76  return NULL;
77  }
78  return bv;
79 }
80 
86  if (!bv) {
87  return;
88  }
89  rz_bv_fini(bv);
90  free(bv);
91 }
92 
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 }
114 
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 }
161 
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 }
178 
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 }
200 
210 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) {
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 }
229 
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 }
251 
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 }
274 
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 }
296 
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 }
319 
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 }
333 
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 }
360 
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 }
388 
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 }
403 
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 }
422 
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 }
438 
447  return rz_bv_lshift_fill(bv, size, false);
448 }
449 
458  return rz_bv_rshift_fill(bv, size, false);
459 }
460 
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 }
499 
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 }
538 
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 }
565 
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 }
592 
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 }
619 
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 }
646 
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 }
674 
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 }
706 
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 }
726 
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 }
768 
769 /* Treat x, y as unsigned
770  * Both operands must have the same length.
771  * if x < y return negtive (-1)
772  * if x == y return 0
773  * if x > y return positive (+1)
774  */
776  rz_return_val_if_fail(x && y, 0);
777 
778  if (x->len != y->len) {
780  return 0;
781  }
782 
783  ut32 len = x->len;
784  int pos;
785  bool x_bit, y_bit;
786  for (ut32 i = 0; i < len; ++i) {
787  pos = len - 1 - i;
788  x_bit = rz_bv_get(x, pos);
789  y_bit = rz_bv_get(y, pos);
790  if (x_bit ^ y_bit) {
791  return x_bit ? 1 : -1;
792  }
793  }
794 
795  // equal
796  return 0;
797 }
798 
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 }
855 
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 }
877 
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 }
936 
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 }
997 
1004  return rz_bv_get(bv, bv->len - 1);
1005 }
1006 
1013  return rz_bv_get(bv, 0);
1014 }
1015 
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 }
1037 
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 }
1045 
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 }
1057 
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 }
1078 
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 }
1102 
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 }
1118 
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 }
1134 
1141  rz_return_val_if_fail(bv, 0);
1142  return bv->len;
1143 }
1144 
1151  rz_return_val_if_fail(bv, 0);
1152  return (bv->len + 7) >> 3;
1153 }
1154 
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 }
1172 
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 }
1190 
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 }
1203 
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 }
1216 
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 }
1237 
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 }
1257 
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 }
1287 
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 }
1312 
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 }
1343 
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 }
1366 
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 }
1391 
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 }
1410 
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 }
1429 
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 }
1448 
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
static RZ_NULLABLE RzILOpBitVector * shift(RzILOpBitVector *val, RZ_NULLABLE RzILOpBool **carry_out, arm_shifter type, RZ_OWN RzILOpBitVector *dist)
Definition: arm_il32.c:190
ut16 val
Definition: armass64_const.h:6
static ut8 bytes[32]
Definition: asm_arc.c:23
RZ_API RZ_OWN RzBitVector * rz_bv_add(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *carry)
Definition: bitvector.c:683
RZ_API RZ_OWN RzBitVector * rz_bv_dup(const RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:167
RZ_API bool rz_bv_lsb(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1012
RZ_API RZ_OWN RzBitVector * rz_bv_append(RZ_NONNULL RzBitVector *high, RZ_NONNULL RzBitVector *low)
Definition: bitvector.c:326
RZ_API ut32 rz_bv_ctz(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1123
#define BV_ELEM_SIZE
Definition: bitvector.c:9
RZ_API ut32 rz_bv_copy(RZ_NONNULL const RzBitVector *src, RZ_NONNULL RzBitVector *dst)
Definition: bitvector.c:186
#define reverse_byte(x)
Definition: bitvector.c:13
#define NELEM(N, ELEMPER)
Definition: bitvector.c:8
RZ_API RZ_OWN RzBitVector * rz_bv_prepend_zero(RZ_NONNULL RzBitVector *bv, ut32 delta_len)
Definition: bitvector.c:236
RZ_API RZ_OWN RzBitVector * rz_bv_or(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:573
RZ_API bool rz_bv_set_from_ut64(RZ_NONNULL RzBitVector *bv, ut64 value)
Definition: bitvector.c:1222
RZ_API void rz_bv_free(RZ_NULLABLE RzBitVector *bv)
Definition: bitvector.c:85
RZ_API bool rz_bv_ule(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1052
RZ_API bool rz_bv_lshift_fill(RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
Definition: bitvector.c:469
RZ_API bool rz_bv_cmp(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1086
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
RZ_API void rz_bv_fini(RZ_NONNULL RzBitVector *bv)
Clear a RzBitVector structure.
Definition: bitvector.c:58
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.
Definition: bitvector.c:1318
RZ_API bool rz_bv_sle(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1064
RZ_API ut64 rz_bv_to_ut64(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1454
RZ_API ut32 rz_bv_len(RZ_NONNULL const RzBitVector *bv)
Definition: bitvector.c:1140
RZ_API bool rz_bv_toggle(RZ_NONNULL RzBitVector *bv, ut32 pos)
Definition: bitvector.c:396
RZ_API bool rz_bv_is_zero_vector(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1021
RZ_API RZ_OWN RzBitVector * rz_bv_new_from_st64(ut32 length, st64 value)
Definition: bitvector.c:1179
RZ_API RZ_OWN RzBitVector * rz_bv_mod(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:865
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 ut32 rz_bv_to_ut32(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1435
RZ_API RZ_OWN RzBitVector * rz_bv_new_from_ut64(ut32 length, ut64 value)
Definition: bitvector.c:1161
RZ_API ut8 rz_bv_to_ut8(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1397
RZ_API RZ_OWN char * rz_bv_as_string(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:98
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
RZ_API bool rz_bv_toggle_all(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:410
RZ_API RZ_OWN RzBitVector * rz_bv_cut_tail(RZ_NONNULL RzBitVector *bv, ut32 delta_len)
Definition: bitvector.c:303
RZ_API ut32 rz_bv_hash(RZ_NULLABLE RzBitVector *x)
Definition: bitvector.c:1372
RZ_API ut32 rz_bv_len_bytes(RZ_NONNULL const RzBitVector *bv)
Definition: bitvector.c:1150
RZ_API RZ_OWN RzBitVector * rz_bv_complement_2(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:652
RZ_API bool rz_bv_rshift(RZ_NONNULL RzBitVector *bv, ut32 size)
Definition: bitvector.c:457
ut8 reverse_lt_8bits(ut8 x, ut8 w)
Definition: bitvector.c:17
RZ_API RZ_OWN RzBitVector * rz_bv_xor(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:600
RZ_API RZ_OWN RzBitVector * rz_bv_mul(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:734
RZ_API bool rz_bv_msb(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1003
RZ_API RZ_OWN RzBitVector * rz_bv_and(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:546
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_from_bytes_be(RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
Definition: bitvector.c:1207
RZ_API RZ_OWN RzBitVector * rz_bv_new(ut32 length)
Definition: bitvector.c:71
RZ_API ut16 rz_bv_to_ut16(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1416
RZ_API ut32 rz_bv_clz(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1107
RZ_API bool rz_bv_set(RZ_NONNULL RzBitVector *bv, ut32 pos, bool b)
Definition: bitvector.c:341
RZ_API RZ_OWN RzBitVector * rz_bv_new_from_bytes_le(RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
Definition: bitvector.c:1194
RZ_API RZ_OWN RzBitVector * rz_bv_sub(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *borrow)
Definition: bitvector.c:715
RZ_API RZ_OWN RzBitVector * rz_bv_complement_1(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:625
RZ_API bool rz_bv_rshift_fill(RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
Definition: bitvector.c:508
RZ_API RZ_OWN RzBitVector * rz_bv_sdiv(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:893
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.
Definition: bitvector.c:1349
int bv_unsigned_cmp(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:775
RZ_API RZ_OWN RzBitVector * rz_bv_smod(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:952
RZ_API RZ_OWN RzBitVector * rz_bv_append_zero(RZ_NONNULL RzBitVector *bv, ut32 delta_len)
Definition: bitvector.c:258
RZ_API bool rz_bv_init(RZ_NONNULL RzBitVector *bv, ut32 length)
Initialize a RzBitVector structure.
Definition: bitvector.c:38
RZ_API bool rz_bv_set_from_st64(RZ_NONNULL RzBitVector *bv, st64 value)
Definition: bitvector.c:1243
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
RZ_API RZ_OWN RzBitVector * rz_bv_cut_head(RZ_NONNULL RzBitVector *bv, ut32 delta_len)
Definition: bitvector.c:281
RZ_API bool rz_bv_eq(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1041
RZ_API RZ_OWN RzBitVector * rz_bv_div(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:808
RZ_API RZ_OWN char * rz_bv_as_hex_string(RZ_NONNULL RzBitVector *bv, bool pad)
Definition: bitvector.c:121
int bits(struct state *s, int need)
Definition: blast.c:72
static int value
Definition: cmd_api.c:93
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RzCryptoSelector bit
Definition: crypto.c:16
#define r
Definition: crypto_rc6.c:12
#define w
Definition: crypto_rc6.c:13
int mod(int a, int b)
Definition: crypto_rot.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 ut8
Definition: dcpu16.h:8
uint16_t ut16
uint32_t ut32
const char * v
Definition: dsignal.c:12
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
char * dst
Definition: lz4.h:724
int x
Definition: mipsasm.c:20
int idx
Definition: setup.py:197
static const char hex[16]
Definition: print.c:21
static void pad(RzStrBuf *sb, ut32 count)
Definition: protobuf.c:36
static RzSocket * s
Definition: rtr.c:28
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define rz_return_val_if_reached(val)
Definition: rz_assert.h:122
#define rz_bv_new_one(l)
Definition: rz_bitvector.h:106
#define rz_bv_new_zero(l)
Definition: rz_bitvector.h:105
#define rz_bv_neg
Definition: rz_bitvector.h:58
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
static ut64 rz_num_bitmask(ut8 width)
Get the 64-bit value that has exactly its width lowest bits set to 1. e.g. rz_num_bitmask(2) == 0b11 ...
Definition: rz_num.h:134
RZ_API 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 RZ_IN
Definition: rz_types.h:50
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define PFMT64x
Definition: rz_types.h:393
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
#define UT32_MAX
Definition: rz_types_base.h:99
#define UT64_MAX
Definition: rz_types_base.h:86
#define UT8_MAX
#define UT16_MAX
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
#define h(i)
Definition: sha256.c:48
structure for bitvector
Definition: rz_bitvector.h:19
union bitvector_t::@303 bits
ut32 len
number of bits – virtual / logical
Definition: rz_bitvector.h:25
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
int pos
Definition: main.c:11
ut64(WINAPI *w32_GetEnabledXStateFeatures)()