From 4a1a624318704778f784b9d888580f26036f4a46 Mon Sep 17 00:00:00 2001 From: Lazula <26179473+Lazula@users.noreply.github.com> Date: Thu, 17 Dec 2020 15:24:27 -0600 Subject: [PATCH] Clean up r_num ##util ##refactor (#18049) * Fix function spacing for r_num functions * Remove r_num_ntohs * Rename r_num_to_trits to r_num_to_ternary for consistency with surrounding code * Rename trits to ternary in other files * Fix spacing in test_contrbtree.c --- libr/anal/esil2reil.c | 4 ++-- libr/asm/arch/z80/z80asm.c | 2 +- libr/core/cmd.c | 6 +++--- libr/core/cmd_help.c | 10 ++++------ libr/include/r_util/r_num.h | 6 ++---- libr/main/rax2.c | 6 +++--- libr/util/calc.c | 8 ++++---- libr/util/unum.c | 7 +++---- test/db/cmd/cmd_help | 8 ++++---- test/db/cmd/feat_variables | 6 +++--- test/db/formats/firmware | 2 +- test/unit/test_contrbtree.c | 38 ++++++++++++++++++------------------- test/unit/test_unum.c | 16 ++++++++-------- 13 files changed, 57 insertions(+), 62 deletions(-) diff --git a/libr/anal/esil2reil.c b/libr/anal/esil2reil.c index 469261433f..2d00d924c3 100644 --- a/libr/anal/esil2reil.c +++ b/libr/anal/esil2reil.c @@ -1107,11 +1107,11 @@ void reil_flag_spew_inst(RAnalEsil *esil, const char *flag) { r_anal_esil_push(esil, esil->Reil->cur); break; case 'b': - bit = (ut8)r_num_get(NULL, &flag[1]); + bit = (ut8)r_num_get (NULL, &flag[1]); reil_generate_borrow_flag(esil, bit); break; case 'c': - bit = (ut8)r_num_get(NULL, &flag[1]); + bit = (ut8)r_num_get (NULL, &flag[1]); reil_generate_carry_flag(esil, bit); break; case 'o': diff --git a/libr/asm/arch/z80/z80asm.c b/libr/asm/arch/z80/z80asm.c index eeeed4f11d..0291668e2c 100644 --- a/libr/asm/arch/z80/z80asm.c +++ b/libr/asm/arch/z80/z80asm.c @@ -1144,7 +1144,7 @@ static int assemble(const char *str, unsigned char *_obuf) { break; } if (s == 7) { - char n = r_num_math(NULL, readbyte); + char n = r_num_math (NULL, readbyte); wrtb (0x08 * (r - 7) + 0x6); wrtb (n); } else { diff --git a/libr/core/cmd.c b/libr/core/cmd.c index 4b0dde9860..7340c5c783 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -3233,7 +3233,7 @@ static int r_core_cmd_subst_i(RCore *core, char *cmd, char *colon, bool *tmpseek } haveQuote = q != NULL; oseek = core->offset; - r_core_seek (core, r_num_math(core->num, p + 2), true); + r_core_seek (core, r_num_math (core->num, p + 2), true); if (q) { *p = '"'; p = q; @@ -3893,7 +3893,7 @@ repeat_arroba: { char *out = sdb_querys (core->sdb, NULL, 0, ptr + ((ptr[1])? 2: 1)); if (out) { - r_core_seek (core, r_num_math(core->num, out), true); + r_core_seek (core, r_num_math (core->num, out), true); free (out); usemyblock = true; } @@ -5795,7 +5795,7 @@ DEFINE_HANDLE_TS_FCN_AND_SYMBOL(tmp_kuery_command) { ut64 orig_offset = state->core->offset; char *out = sdb_querys (core->sdb, NULL, 0, arg_str); if (out) { - r_core_seek (core, r_num_math(core->num, out), true); + r_core_seek (core, r_num_math (core->num, out), true); free (out); } RCmdStatus res = handle_ts_command_tmpseek (state, command); diff --git a/libr/core/cmd_help.c b/libr/core/cmd_help.c index 8102807f86..858aecec57 100644 --- a/libr/core/cmd_help.c +++ b/libr/core/cmd_help.c @@ -664,17 +664,15 @@ static int cmd_help(void *data, const char *input) { pj_ks (pj, "float", sdb_fmt ("%ff", f)); pj_ks (pj, "double", sdb_fmt ("%lf", d)); pj_ks (pj, "binary", sdb_fmt ("0b%s", out)); - r_num_to_trits (out, n); - pj_ks (pj, "trits", sdb_fmt ("0t%s", out)); + r_num_to_ternary (out, n); + pj_ks (pj, "ternary", sdb_fmt ("0t%s", out)); } else { r_cons_printf ("fvalue: %.1lf\n", core->num->fvalue); r_cons_printf ("float: %ff\n", f); r_cons_printf ("double: %lf\n", d); r_cons_printf ("binary 0b%s\n", out); - - /* ternary */ - r_num_to_trits (out, n); - r_cons_printf ("trits 0t%s\n", out); + r_num_to_ternary (out, n); + r_cons_printf ("ternary 0t%s\n", out); } } if (*input == 'j') { diff --git a/libr/include/r_util/r_num.h b/libr/include/r_util/r_num.h index eab6c012c2..c23c2e1787 100644 --- a/libr/include/r_util/r_num.h +++ b/libr/include/r_util/r_num.h @@ -15,8 +15,7 @@ typedef struct { typedef enum { RNCNAME, RNCNUMBER, RNCEND, RNCINC, RNCDEC, RNCPLUS='+', RNCMINUS='-', RNCMUL='*', RNCDIV='/', RNCMOD='%', - //RNCXOR='^', RNCOR='|', RNCAND='&', - RNCNEG='~', RNCAND='&', RNCORR='|', RNCXOR='^', + RNCNEG='~', RNCAND='&', RNCOR='|', RNCXOR='^', RNCPRINT=';', RNCASSIGN='=', RNCLEFTP='(', RNCRIGHTP=')', RNCSHL='<', RNCSHR = '>', RNCROL = '#', RNCROR = '$' } RNumCalcToken; @@ -66,10 +65,9 @@ R_API void r_num_minmax_swap_i(int *a, int *b); // XXX this can be a cpp macro : R_API ut64 r_num_math(RNum *num, const char *str); R_API ut64 r_num_get(RNum *num, const char *str); R_API int r_num_to_bits(char *out, ut64 num); -R_API int r_num_to_trits(char *out, ut64 num); //Rename this please +R_API int r_num_to_ternary(char *out, ut64 num); R_API int r_num_rand(int max); R_API void r_num_irand(void); -R_API ut16 r_num_ntohs(ut16 foo); R_API ut64 r_get_input_num_value(RNum *num, const char *input_value); R_API bool r_is_valid_input_num_value(RNum *num, const char *input_value); R_API int r_num_between(RNum *num, const char *input_value); diff --git a/libr/main/rax2.c b/libr/main/rax2.c index 0ede32ea44..a550ea2c37 100644 --- a/libr/main/rax2.c +++ b/libr/main/rax2.c @@ -90,7 +90,7 @@ static int format_output(RNum *num, char mode, const char *s, int force_mode, ut break; case 'T': if (n) { - r_num_to_trits (strbits, n); + r_num_to_ternary (strbits, n); printf ("%st\n", strbits); } else { printf ("0t\n"); @@ -520,8 +520,8 @@ dotherax: printf ("double: %lf\n", d); /* ternary */ - r_num_to_trits (out, n); - printf ("trits 0t%s\n", out); + r_num_to_ternary (out, n); + printf ("ternary 0t%s\n", out); return true; } else if (flags & (1 << 19)) { // -L diff --git a/libr/util/calc.c b/libr/util/calc.c index 24b5a0437b..09d9375816 100644 --- a/libr/util/calc.c +++ b/libr/util/calc.c @@ -20,7 +20,7 @@ static inline RNumCalcValue Nsetf(double v) { RNumCalcValue n; n.d = v; n.n = (u static inline RNumCalcValue Naddi(RNumCalcValue n, ut64 v) { n.d += (double)v; n.n += v; return n; } static inline RNumCalcValue Nsubi(RNumCalcValue n, ut64 v) { n.d -= (double)v; n.n -= v; return n; } static inline RNumCalcValue Nneg(RNumCalcValue n) { n.n = ~n.n; return n; } -static inline RNumCalcValue Norr(RNumCalcValue n, RNumCalcValue v) { n.d = v.d; n.n |= v.n; return n; } +static inline RNumCalcValue Nor(RNumCalcValue n, RNumCalcValue v) { n.d = v.d; n.n |= v.n; return n; } static inline RNumCalcValue Nxor(RNumCalcValue n, RNumCalcValue v) { n.d = v.d; n.n ^= v.n; return n; } static inline RNumCalcValue Nand(RNumCalcValue n, RNumCalcValue v) { n.d = v.d; n.n &= v.n; return n; } static inline RNumCalcValue Nadd(RNumCalcValue n, RNumCalcValue v) { n.d += v.d; n.n += v.n; return n; } @@ -94,7 +94,7 @@ static RNumCalcValue expr(RNum *num, RNumCalc *nc, int get) { case RNCPLUS: left = Nadd (left, term (num, nc, 1)); break; case RNCMINUS: left = Nsub (left, term (num, nc, 1)); break; case RNCXOR: left = Nxor (left, term (num, nc, 1)); break; - case RNCORR: left = Norr (left, term (num, nc, 1)); break; + case RNCOR: left = Nor (left, term (num, nc, 1)); break; case RNCAND: left = Nand (left, term (num, nc, 1)); break; default: return left; @@ -161,8 +161,8 @@ static RNumCalcValue prim(RNum *num, RNumCalc *nc, int get) { return Naddi (prim (num, nc, 1), 1); case RNCDEC: return Naddi (prim (num, nc, 1), -1); - case RNCORR: - return Norr (v, prim (num, nc, 1)); + case RNCOR: + return Nor (v, prim (num, nc, 1)); case RNCMINUS: return Nsub (v, prim (num, nc, 1)); case RNCLEFTP: diff --git a/libr/util/unum.c b/libr/util/unum.c index 3ba8549f6c..7c6840172a 100644 --- a/libr/util/unum.c +++ b/libr/util/unum.c @@ -11,8 +11,7 @@ static ut64 r_num_tailff(RNum *num, const char *hex); -// TODO: rename to r_num_srand() -static void r_srand(int seed) { +static void r_num_srand(int seed) { #if HAVE_ARC4RANDOM_UNIFORM // no-op (void)seed; @@ -30,7 +29,7 @@ static int r_rand(int mod) { } R_API void r_num_irand(void) { - r_srand (r_time_now ()); + r_num_srand (r_time_now ()); } R_API int r_num_rand(int max) { @@ -540,7 +539,7 @@ R_API int r_num_to_bits(char *out, ut64 num) { return size; } -R_API int r_num_to_trits(char *out, ut64 num) { +R_API int r_num_to_ternary(char *out, ut64 num) { if (out == NULL) { return false; } diff --git a/test/db/cmd/cmd_help b/test/db/cmd/cmd_help index a91a324b4b..0a882da614 100644 --- a/test/db/cmd/cmd_help +++ b/test/db/cmd/cmd_help @@ -151,7 +151,7 @@ fvalue: 1.0 float: 0.000000f double: 0.000000 binary 0b00000001 -trits 0t1 +ternary 0t1 1 EOF RUN @@ -171,7 +171,7 @@ fvalue: 0.0 float: 0.000000f double: 0.000000 binary 0b00000000 -trits 0t0 +ternary 0t0 0 EOF RUN @@ -191,7 +191,7 @@ fvalue: -1.0 float: nanf double: nan binary 0b1111111111111111111111111111111111111111111111111111111111111111 -trits 0t11112220022122120101211020120210210211220 +ternary 0t11112220022122120101211020120210210211220 EOF RUN @@ -210,7 +210,7 @@ fvalue: 3.0 float: 0.000000f double: 0.000000 binary 0b00000010 -trits 0t2 +ternary 0t2 EOF RUN diff --git a/test/db/cmd/feat_variables b/test/db/cmd/feat_variables index 663d0a7bef..6894e43c85 100644 --- a/test/db/cmd/feat_variables +++ b/test/db/cmd/feat_variables @@ -278,7 +278,7 @@ fvalue: 8.0 float: 0.000000f double: 0.000000 binary 0b00001000 -trits 0t22 +ternary 0t22 0x8 int32 0 uint32 0 @@ -291,7 +291,7 @@ fvalue: 0.0 float: 0.000000f double: 0.000000 binary 0b00000000 -trits 0t0 +ternary 0t0 0x0 EOF RUN @@ -351,7 +351,7 @@ fvalue: 123.0 float: 0.000000f double: 0.000000 binary 0b01111011 -trits 0t11120 +ternary 0t11120 EOF RUN diff --git a/test/db/formats/firmware b/test/db/formats/firmware index 96424bda30..43af9eca24 100644 --- a/test/db/formats/firmware +++ b/test/db/formats/firmware @@ -39,7 +39,7 @@ fvalue: 1048490.0 float: 0.000000f double: 0.000000 binary 0b000011111111111110101010 -trits 0t1222021020222 +ternary 0t1222021020222 EOF RUN diff --git a/test/unit/test_contrbtree.c b/test/unit/test_contrbtree.c index 523546e8df..8289ddfd46 100644 --- a/test/unit/test_contrbtree.c +++ b/test/unit/test_contrbtree.c @@ -8,57 +8,57 @@ static int simple_cmp(void *incoming, void *in, void *user) { } bool test_r_rbtree_cont_insert() { - RContRBTree *tree = r_rbtree_cont_new(); + RContRBTree *tree = r_rbtree_cont_new (); ut32 i; for (i = 0; i < 2000; i++) { - ut32 v = (ut32)r_num_rand(UT32_MAX >> 1); - r_rbtree_cont_insert(tree, (void *)(size_t)v, simple_cmp, NULL); + ut32 v = (ut32)r_num_rand (UT32_MAX >> 1); + r_rbtree_cont_insert (tree, (void *)(size_t)v, simple_cmp, NULL); } i = 0; bool ret = true; void *v; RBIter ator; - r_rbtree_cont_foreach(tree, ator, v) { + r_rbtree_cont_foreach (tree, ator, v) { const ut32 next = (ut32)(size_t)v; ret &= (i <= next); i = next; } - r_rbtree_cont_free(tree); - mu_assert("rbtree_cont_insert", ret); + r_rbtree_cont_free (tree); + mu_assert ("rbtree_cont_insert", ret); mu_end; } static int strbuf_num_cmp0(void *incoming, void *in, void *user) { ut64 v[2] = { - r_num_get(NULL, r_strbuf_get((RStrBuf *)incoming)), - r_num_get(NULL, r_strbuf_get((RStrBuf *)in)) + r_num_get (NULL, r_strbuf_get ((RStrBuf *)incoming)), + r_num_get (NULL, r_strbuf_get ((RStrBuf *)in)) }; return (int)(v[0] - v[1]); } static int strbuf_num_cmp1(void *incoming, void *in, void *user) { - ut64 v[2] = { ((ut64 *)incoming)[0], r_num_get(NULL, r_strbuf_get((RStrBuf *)in)) }; + ut64 v[2] = { ((ut64 *)incoming)[0], r_num_get (NULL, r_strbuf_get ((RStrBuf *)in)) }; return (int)(v[0] - v[1]); } bool test_r_rbtree_cont_delete() { RContRBTree *tree = r_rbtree_cont_newf ((RContRBFree)r_strbuf_free); - r_rbtree_cont_insert(tree, r_strbuf_new("13"), strbuf_num_cmp0, NULL); - r_rbtree_cont_insert(tree, r_strbuf_new("0x9090"), strbuf_num_cmp0, NULL); - r_rbtree_cont_insert(tree, r_strbuf_new("42"), strbuf_num_cmp0, NULL); - r_rbtree_cont_insert(tree, r_strbuf_new("23"), strbuf_num_cmp0, NULL); - r_rbtree_cont_insert(tree, r_strbuf_new("0x13373"), strbuf_num_cmp0, NULL); + r_rbtree_cont_insert (tree, r_strbuf_new ("13"), strbuf_num_cmp0, NULL); + r_rbtree_cont_insert (tree, r_strbuf_new ("0x9090"), strbuf_num_cmp0, NULL); + r_rbtree_cont_insert (tree, r_strbuf_new ("42"), strbuf_num_cmp0, NULL); + r_rbtree_cont_insert (tree, r_strbuf_new ("23"), strbuf_num_cmp0, NULL); + r_rbtree_cont_insert (tree, r_strbuf_new ("0x13373"), strbuf_num_cmp0, NULL); ut64 del_me = 0x9090; - r_rbtree_cont_delete(tree, &del_me, strbuf_num_cmp1, NULL); + r_rbtree_cont_delete (tree, &del_me, strbuf_num_cmp1, NULL); RStrBuf *s; RBIter ator; bool ret = true; - r_rbtree_cont_foreach_prev(tree, ator, s) { - const ut64 v = r_num_get(NULL, r_strbuf_get(s)); + r_rbtree_cont_foreach_prev (tree, ator, s) { + const ut64 v = r_num_get (NULL, r_strbuf_get(s)); ret &= (v != 0x9090); } - r_rbtree_cont_free(tree); - mu_assert("rbtree_cont_delete", ret); + r_rbtree_cont_free (tree); + mu_assert ("rbtree_cont_delete", ret); mu_end; } diff --git a/test/unit/test_unum.c b/test/unit/test_unum.c index 64d54db18f..a69d453ba3 100644 --- a/test/unit/test_unum.c +++ b/test/unit/test_unum.c @@ -3,7 +3,7 @@ static RNum *num; -bool test_r_num_units() { +bool test_r_num_units(void) { char humansz[8]; const struct { const char *expected_res; @@ -44,7 +44,7 @@ bool test_r_num_units() { mu_end; } -bool test_r_num_minmax_swap_i() { +bool test_r_num_minmax_swap_i(void) { int a = -1, b = 2; r_num_minmax_swap_i (&a, &b); mu_assert_eq (a == -1 && b == 2, 1, "a < b -> a < b"); @@ -54,7 +54,7 @@ bool test_r_num_minmax_swap_i() { mu_end; } -bool test_r_num_minmax_swap() { +bool test_r_num_minmax_swap(void) { ut64 a = 1, b = 2; r_num_minmax_swap (&a, &b); mu_assert_eq (a == 1 && b == 2, 1, "a < b -> a < b"); @@ -64,7 +64,7 @@ bool test_r_num_minmax_swap() { mu_end; } -bool test_r_num_between() { +bool test_r_num_between(void) { mu_assert_eq (r_num_between (num, "1 2 3"), 1, "1 <= 2 <= 3"); mu_assert_eq (r_num_between (num, "3 2 1"), 0, "3 <= 2 <= 1"); mu_assert_eq (r_num_between (num, "1 1 1"), 1, "1 <= 1 <= 1"); @@ -76,7 +76,7 @@ bool test_r_num_between() { mu_end; } -bool test_r_num_str_len() { +bool test_r_num_str_len(void) { mu_assert_eq (r_num_str_len ("1"), 1, "\"1\""); mu_assert_eq (r_num_str_len ("1+1"), 3, "\"1+1\""); mu_assert_eq (r_num_str_len ("1 + 1"), 5, "\"1 + 1\""); @@ -92,7 +92,7 @@ bool test_r_num_str_len() { mu_end; } -bool test_r_num_str_split() { +bool test_r_num_str_split(void) { char *str = malloc (0x20); strcpy (str, "1 1 + 2 1 + (2 + 3) 4 "); //expected "1\01 + 2\01 + (2 + 3)\04\0" @@ -106,7 +106,7 @@ bool test_r_num_str_split() { mu_end; } -bool test_r_num_str_split_list() { +bool test_r_num_str_split_list(void) { char *s; char *str = malloc (0x20); strcpy (str, "1 1 + 2 1 + (2 + 3) 4 "); @@ -126,7 +126,7 @@ bool test_r_num_str_split_list() { mu_end; } -bool all_tests() { +bool all_tests(void) { mu_run_test (test_r_num_units); mu_run_test (test_r_num_minmax_swap_i); mu_run_test (test_r_num_minmax_swap);