# SPDX-FileCopyrightText: 2023 RizinOrg <info@rizin.re>
# SPDX-License-Identifier: LGPL-3.0-only
---
name: cmd_math
commands:
  - name: "%"
    summary: Evaluate numerical expression <expr>
    cname: calculate_expr
    type: RZ_CMD_DESC_TYPE_ARGV_STATE
    modes:
      - RZ_OUTPUT_MODE_STANDARD
      - RZ_OUTPUT_MODE_JSON
    args:
      - name: expr
        type: RZ_CMD_ARG_TYPE_STRING
  - name: "%$"
    summary: Print Rizin variables and their values
    cname: list_rizin_vars
    args:
      - name: var
        type: RZ_CMD_ARG_TYPE_STRING
        optional: true
    details_cb: rz_cmd_math_help_vars_details_cb
  - name: "%0"
    summary: Set first tab as the current active tab
    cname: set_active_tab_zero
    args: []
  - name: "%1"
    summary: Set next tab as the current active tab
    cname: set_active_tab_next
    args: []
  - name: "%r"
    summary: Generate a random number between <lowlimit> and <uplimit>
    cname: generate_random_number
    args:
      - name: lowlimit
        type: RZ_CMD_ARG_TYPE_RZNUM
      - name: uplimit
        type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%b"
    summary: Print <expr> in binary format
    cname: print_binary
    args:
      - name: expr
        type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%b64"
    summary: Encode <str> in Base64
    cname: base64_encode
    args:
      - name: str
        type: RZ_CMD_ARG_TYPE_STRING
    details:
      - name: Examples
        entries:
          - text: "%b64"
            arg_str: " ILoveRizin"
            comment: (SUxvdmVSaXppbgo=) Encodes given string into base64
  - name: "%b64-"
    summary: Decode <str> from Base64
    cname: base64_decode
    args:
      - name: str
        type: RZ_CMD_ARG_TYPE_STRING
    details:
      - name: Examples
        entries:
          - text: "%b64- "
            arg_str: SUxvdmVSaXppbgo=
            comment: (ILoveRizin) Decodes given base64 string
  - name: "%btw"
    summary: Check if <middle> number is between <first> and <last>
    cname: check_between
    args:
      - name: first
        type: RZ_CMD_ARG_TYPE_RZNUM
      - name: middle
        type: RZ_CMD_ARG_TYPE_RZNUM
      - name: last
        type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%B"
    summary: Prints the search boundaries based on the search.in mode.
    description: >-
      There are multiple search modes in Rizin that can be listed using the
      command `e search.in=?`. This command can be used to get boundaries of
      those search modes.
    cname: print_boundaries_prot
    type: RZ_CMD_DESC_TYPE_ARGV_STATE
    modes:
      - RZ_OUTPUT_MODE_STANDARD
      - RZ_OUTPUT_MODE_JSON
      - RZ_OUTPUT_MODE_TABLE
    args: []
    details:
      - name: Examples
        entries:
          - text: "%B "
            arg_str: "@e:search.in=file"
            comment: Prints boundary of this file
          - text: "%Bt "
            arg_str: "@e:search.in=io.maps"
            comment: Prints boundaries of all io maps
  - name: "%h"
    summary: Print hash value of string <str>
    cname: print_djb2_hash
    args:
      - name: <str>
        type: RZ_CMD_ARG_TYPE_STRING
    details:
      - name: Examples
        entries:
          - text: "%h "
            arg_str: ILoveRizin
            comment: 0x56b7215a -> hashed value
  - name: "%f"
    summary: bitstring manipulation.
    description: >-
      Treat given string as bitstring and get selected characters from
      bitstring using given value. Bits that are flagged in value are used to
      get characters from given string. Bitstring is treated in big-endian
      format
    cname: print_bitstring
    args:
      - name: value
        type: RZ_CMD_ARG_TYPE_NUM
      - name: bitstring
        type: RZ_CMD_ARG_TYPE_STRING
    details:
      - name: Examples
        entries:
          - text: "%f"
            arg_str: " 28 Hello"
            comment: "LLO (00111b selected : big-endian bitstring)"
          - text: "%f"
            arg_str: " 12 Hello"
            comment: "LL (00110b selected : big-endian bitstring)"
  - name: "%o"
    summary: Print <expr> in octal format
    cname: eval_expr_print_octal
    args:
      - name: expr
        type: RZ_CMD_ARG_TYPE_RZNUM
    details:
      - name: Examples
        entries:
          - text: "%o"
            arg_str: " 123"
            comment: 0173 in octal
          - text: "%o"
            arg_str: " 321"
            comment: 0501 in octal
  - name: "%u"
    summary: "Convert <expr> to K, M, G, T etc... units"
    cname: num_to_units
    args:
      - name: expr
        type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%v"
    summary: Show value commands
    type: RZ_CMD_DESC_TYPE_GROUP
    subcommands:
      - name: "%v"
        summary: Show last expression ($?) or currently evaluated <expr>
        cname: show_value
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
            optional: true
      - name: "%vx"
        summary: Show last expression ($?) or currently evaluated <expr> in hex
        cname: show_value_hex
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
            optional: true
      - name: "%vi1"
        summary: Show last expression ($?) or currently evaluated <expr> as 1 byte integer
        cname: show_value_i1
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
            optional: true
      - name: "%vi2"
        summary: Show last expression ($?) or currently evaluated <expr> as 2 bytes integer
        cname: show_value_i2
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
            optional: true
      - name: "%vi4"
        summary: Show last expression ($?) or currently evaluated <expr> as 4 bytes integer
        cname: show_value_i4
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
            optional: true
      - name: "%vi8"
        summary: Show last expression ($?) or currently evaluated <expr> as 8 bytes integer
        cname: show_value_i8
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
            optional: true
      - name: "%vi"
        summary: Show last expression ($?) or currently evaluated <expr> as integer
        cname: show_value_int
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
            optional: true
  - name: "%="
    summary: Update $? (last evaluated expression) with <expr>, without printing anything
    cname: set_last_eval_expr
    args:
      - name: expr
        type: RZ_CMD_ARG_TYPE_RZNUM
    details:
      - name: Examples
        entries:
          - text: "%q"
            arg_str: " 123"
            comment: >-
              This will set $?. Then commands like %+, %-, etc. can be used to do some task
              by checking whether $? holds positive value or not.
  - name: "%=="
    summary: Compare strings <str1> and <str2> and set $? register to cmp result
    cname: compare_and_set_core_num_value
    args:
      - name: str1
        type: RZ_CMD_ARG_TYPE_STRING
      - name: str2
        type: RZ_CMD_ARG_TYPE_STRING
    details:
      - name: Examples
        entries:
          - text: "%=="
            arg_str: " str1 str2"
            comment: $? will be set to 0 if these two strings are equal, otherwise some other positive value.
  - name: "%+"
    summary: Execute command <cmd> if $? register is greater than 0
    cname: exec_cmd_if_core_num_value_positive
    args:
      - name: cmd
        type: RZ_CMD_ARG_TYPE_CMD
    details:
      - name: Examples
        entries:
          - text: "%=1; %+"
            arg_str: " %%x"
            comment: Will display hexdump at current seek address
          - text: "%=-2; %+"
            arg_str: " %%x"
            comment: Won"t do anything
          - text: "%=0; %+"
            arg_str: " %%x"
            comment: Won"t do anything
  - name: "%-"
    summary: Execute command <cmd> if $? register is less than 0
    cname: exec_cmd_if_core_num_value_negative
    args:
      - name: cmd
        type: RZ_CMD_ARG_TYPE_CMD
    details:
      - name: Examples
        entries:
          - text: "%=1; %-"
            arg_str: " %%x"
            comment: Won"t do anything
          - text: "%=-2; %-"
            arg_str: " %%x"
            comment: Will display hexdump at current seek address
          - text: "%=0; %+"
            arg_str: " %%x"
            comment: Won"t do anything
  - name: "%!"
    summary: Execute command <cmd> if $? is 0
    cname: exec_cmd_if_core_num_value_zero
    args:
      - name: cmd
        type: RZ_CMD_ARG_TYPE_CMD
  - name: "%%"
    summary: Execute command <cmd> if $? is not 0
    cname: exec_cmd_if_core_num_value_nonzero
    args:
      - name: cmd
        type: RZ_CMD_ARG_TYPE_CMD
  - name: "%l"
    summary: Calculate length of string <str>. Quiet mode stores value in `$?` register.
    cname: calculate_string_length
    args:
      - name: str
        type: RZ_CMD_ARG_TYPE_STRING
    type: RZ_CMD_DESC_TYPE_ARGV_STATE
    modes:
      - RZ_OUTPUT_MODE_STANDARD
      - RZ_OUTPUT_MODE_QUIET
  - name: "%X"
    summary: Show evaluated expression <expr> in hex
    cname: calc_expr_show_hex
    args:
      - name: expr
        type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%x"
    summary: String/Numeric to hex manipulation commands
    type: RZ_CMD_DESC_TYPE_GROUP
    subcommands:
      - name: "%x"
        summary: ASCII string to hex string
        cname: ascii_to_hex
        args:
          - name: astr
            type: RZ_CMD_ARG_TYPE_STRING
      - name: "%x+"
        summary: Numerical expression to hex
        cname: numeric_expr_to_hex
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
      - name: "%x-"
        summary: Hex string to ASCII string
        cname: hex_to_ascii
        args:
          - name: hexnum
            type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%s"
    summary: Generate sequence of numbers from <start> to <stop> with <step> increments
    cname: generate_sequence
    args:
      - name: start
        type: RZ_CMD_ARG_TYPE_RZNUM
      - name: stop
        type: RZ_CMD_ARG_TYPE_RZNUM
      - name: step
        type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%P"
    summary: Convert physical to virtual address
    cname: phys2virt
    args:
      - name: paddr
        type: RZ_CMD_ARG_TYPE_RZNUM
        optional: true
  - name: "%p"
    summary: Virtual to physical address conversion
    cname: virt2phys
    args:
      - name: vaddr
        type: RZ_CMD_ARG_TYPE_RZNUM
        optional: true
  - name: "%_"
    summary: HUD input
    cname: yank_hud_file
    args:
      - name: input
        type: RZ_CMD_ARG_TYPE_STRING
  - name: "%i"
    summary: Input commands
    type: RZ_CMD_DESC_TYPE_GROUP
    subcommands:
      - name: "%i"
        summary: Input the <prompt> and save response in yank clipboard (`y` commands) 
        cname: input_prompt
        args:
          - name: prompt
            type: RZ_CMD_ARG_TYPE_STRING
      - name: "%ie"
        summary: Input the <prompt>, save response in yank clipboard (`y` commands), and print it
        cname: input_prompt_echo
        args:
          - name: prompt
            type: RZ_CMD_ARG_TYPE_STRING
      - name: "%in"
        summary: Input Yes/No <question> and store result in $? register (default No)
        cname: input_yesno_no
        args:
          - name: question
            type: RZ_CMD_ARG_TYPE_STRING
            optional: true
      - name: "%iy"
        summary: Input Yes/No <question> and store result in $? register (default Yes)
        cname: input_yesno_yes
        args:
          - name: question
            type: RZ_CMD_ARG_TYPE_STRING
            optional: true
      - name: "%ik"
        summary: Input any key. Does nothing else.
        cname: input_any_key
        args: []
      - name: "%ip"
        summary: Interactive HUD mode to find files in <path>
        cname: input_yank_hud
        args:
          - name: path
            type: RZ_CMD_ARG_TYPE_STRING
            optional: true
      - name: "%im"
        summary: Display <msg> in console and wait for key
        cname: input_msg
        args:
          - name: msg
            type: RZ_CMD_ARG_TYPE_STRING
      - name: "%if"
        summary: Evaluate <expr>, store result in $? register and print true if <expr> != 0, false otherwise
        cname: input_conditional
        args:
          - name: expr
            type: RZ_CMD_ARG_TYPE_RZNUM
  - name: "%w"
    summary: Get references of given address
    cname: get_addr_references
    args:
      - name: addr
        type: RZ_CMD_ARG_TYPE_RZNUM
