R_UTIL(3)

NAME
r_util - radare2 utility library

SYNOPSIS
#include <r_util.h>

DESCRIPTION
The r_util library provides a comprehensive collection of utility functions for radare2, including number parsing, string manipulation, buffer management, memory operations, file handling, and various data structures. It serves as the foundation for many other radare2 libraries.

NUMBERS

Parse and format numeric values, convert textual representations into integers, recognize human-friendly unit suffixes, and evaluate simple mathematical expressions. Useful for configuration parsing, user input handling and preparing numbers for display. RNum * r_num_new(RNumCallback cb, RNumCallback2 cb2, void *ptr)

Creates a new number parsing context with custom callbacks.

ut64 r_num_get(RNum *num, const char *str)

Parses a string into a 64-bit unsigned integer, supporting various formats.

ut64 r_num_math(RNum *num, const char *str)

Evaluates mathematical expressions in strings.

char * r_num_units(char *buf, size_t len, ut64 number)

Formats a number with appropriate units (KB, MB, etc.).

BUFFERS

Create and manipulate in-memory buffers that support reading, writing, seeking and size queries — handy for temporary storage, streaming data between components, or abstracting underlying storage. RBuffer * r_buf_new(void)

Creates a new buffer.

st64 r_buf_read(RBuffer *b, ut8 *buf, ut64 len)

Reads data from a buffer.

st64 r_buf_write(RBuffer *b, const ut8 *buf, ut64 len)

Writes data to a buffer.

st64 r_buf_seek(RBuffer *b, st64 addr, int whence)

Seeks to a position in the buffer.

ut64 r_buf_size(RBuffer *b)

Returns the size of the buffer.

STRINGS

Common string helpers for allocation, concatenation, searching, replacement and splitting. They make safe text manipulation and higher-level processing simpler and less error-prone. char * r_str_new(const char *str)

Creates a new string (duplicate).

char * r_str_append(char *str, const char *new)

Appends a string to another.

bool r_str_startswith(const char *str, const char *needle)

Checks if a string starts with a prefix.

char * r_str_replace(char *str, const char *find, const char *replace, int *count)

Replaces substrings in a string.

RList * r_str_split_list(char *str, const char *c, int n)

Splits a string into a list.

LISTS

Lightweight linked-list utilities for creating, appending, indexing and querying lists of pointers. Use these when a simple ordered collection is sufficient. RList * r_list_new(void)

Creates a new linked list.

void r_list_append(RList *list, void *data)

Appends data to a list.

void * r_list_get_n(RList *list, int n)

Gets the nth element from a list.

int r_list_length(RList *list)

Returns the length of a list.

MEMORY

Convenience wrappers for allocating, duplicating and comparing raw memory blocks. These helpers centralize routine memory tasks and can be adapted for custom allocator hooks or debugging instrumentation. void * r_mem_alloc(size_t sz)

Allocates memory.

void * r_mem_dup(const void *src, size_t len)

Duplicates memory.

int r_mem_cmp(const void *a, const void *b, size_t len)

Compares memory regions.

FILES

Simple filesystem helpers to check existence, read entire files into memory, and write buffers to disk. Intended for straightforward file I/O needs in tools and utilities. bool r_file_exists(const char *str)

Checks if a file exists.

char * r_file_slurp(const char *str, size_t *usz)

Reads an entire file into memory.

bool r_file_dump(const char *file, const ut8 *buf, int len, bool append)

Writes data to a file.

HEX

Routines to convert between hexadecimal text and binary data, commonly used when presenting or parsing hex-encoded blobs. int r_hex_str2bin(const char *str, ut8 *buf)

Converts a hex string to binary.

char * r_hex_bin2str(const ut8 *bin, int len)

Converts binary data to a hex string.

BASE64

Utilities to encode binary data to base64 and decode it back, useful for transporting or embedding binary content in text contexts. char * r_base64_encode(const ut8 *bin, int len)

Encodes data to base64.

ut8 * r_base64_decode(const char *str, int *olen)

Decodes base64 data.

TABLES

Helpers for building textual tables: create table objects, add columns and rows, and format output for display when presenting structured, columnar data. RTable * r_table_new(void)

Creates a new table.

void r_table_add_column(RTable *t, RTableColumnType *type, const char *name, int maxwidth)

Adds a column to a table.

void r_table_add_row(RTable *t, const char *name, ...)

Adds a row to a table.

GRAPHS

APIs to build and traverse basic graph structures made of nodes and edges. Suitable for representing relationships, control-flow, or dependency graphs. RGraph * r_graph_new(void)

Creates a new graph.

RGraphNode * r_graph_add_node(RGraph *graph, void *data)

Adds a node to a graph.

void r_graph_add_edge(RGraph *graph, RGraphNode *from, RGraphNode *to)

Adds an edge between nodes.

QUEUES

A compact FIFO queue API for enqueuing and dequeuing pointer elements; useful in producer/consumer patterns and simple task scheduling. RQueue * r_queue_new(int n)

Creates a new queue.

void r_queue_enqueue(RQueue *q, void *el)

Enqueues an element.

void * r_queue_dequeue(RQueue *q)

Dequeues an element.

STACKS

A lightweight LIFO stack API for pushing and popping pointer elements, handy for depth-first traversals, undo stacks and transient state storage. RStack * r_stack_new(ut32 n)

Creates a new stack.

void r_stack_push(RStack *s, void *el)

Pushes an element onto the stack.

void * r_stack_pop(RStack *s)

Pops an element from the stack.

TIME

Helpers to obtain timestamps and format them as human-readable strings, used for logging, profiling and displaying time-related information. ut64 r_time_now(void)

Gets the current time in microseconds.

char * r_time_stamp_to_str(ut64 ts)

Converts a timestamp to a string.

LOGGING

Minimal logging helpers to emit error and informational messages, acting as thin wrappers for diagnostics and user-facing feedback. void r_log_error(const char *fmt, ...)

Logs an error message.

void r_log_info(const char *fmt, ...)

Logs an info message.

EXAMPLES
Number parsing:

    RNum *num = r_num_new(NULL, NULL, NULL);
    ut64 val = r_num_get(num, "0x100");
    r_num_free(num);

String manipulation:

    char *str = r_str_new("hello");
    str = r_str_append(str, " world");

Buffer operations:

    RBuffer *buf = r_buf_new();
    r_buf_write(buf, data, len);
    ut8 *read_data = malloc(len);
    r_buf_read(buf, read_data, len);

List usage:

    RList *list = r_list_new();
    r_list_append(list, item);
    void *first = r_list_get_n(list, 0);

SEE ALSO
r_types(3)