Rizin
unix-like reverse engineering framework and cli tools
rz_diff.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
2 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #ifndef RZ_DIFF_H
6 #define RZ_DIFF_H
7 
8 #include <rz_types.h>
9 #include <rz_list.h>
10 #include <rz_util/rz_pj.h>
11 #include <rz_util/rz_strbuf.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
18 
19 typedef enum rz_diff_op_type_t {
26 
36 typedef const void *(*RzDiffMethodElemAt)(RZ_BORROW const void *array, ut32 index);
37 typedef ut32 (*RzDiffMethodElemHash)(RZ_BORROW const void *elem);
38 typedef int (*RzDiffMethodCompare)(RZ_BORROW const void *a_elem, RZ_BORROW const void *b_elem);
39 typedef bool (*RzDiffMethodIgnore)(RZ_BORROW const void *elem);
40 typedef void (*RzDiffMethodStringify)(RZ_BORROW const void *elem, RZ_BORROW RzStrBuf *sb);
41 typedef struct rz_diff_methods_t {
48 
49 typedef struct rz_diff_op_t {
56 
57 #define RZ_DIFF_OP_SIZE_A(op) (((op)->a_end) - ((op)->a_beg))
58 #define RZ_DIFF_OP_SIZE_B(op) (((op)->b_end) - ((op)->b_beg))
59 #define RZ_DIFF_DEFAULT_N_GROUPS 3
60 
61 typedef struct match_p_t {
66 
67 typedef bool (*RzDiffIgnoreByte)(const ut64 byte);
68 typedef bool (*RzDiffIgnoreLine)(RZ_BORROW const char *line);
69 
70 typedef struct rz_diff_t RzDiff;
71 
72 #ifdef RZ_API
73 
74 /* To calculate the hash of a complex structure made of
75  * various values, xor the results before returning the final value. */
77 
84 
85 RZ_API RZ_OWN RzList /*<RzDiffMatch *>*/ *rz_diff_matches_new(RZ_NONNULL RzDiff *diff);
86 RZ_API RZ_OWN RzList /*<RzDiffOp *>*/ *rz_diff_opcodes_new(RZ_NONNULL RzDiff *diff);
87 RZ_API RZ_OWN RzList /*<RzList<RzDiffOp *> *>*/ *rz_diff_opcodes_grouped_new(RZ_NONNULL RzDiff *diff, ut32 n_groups);
88 RZ_API bool rz_diff_ratio(RZ_NONNULL RzDiff *diff, RZ_NONNULL double *result);
89 RZ_API bool rz_diff_sizes_ratio(RZ_NONNULL RzDiff *diff, RZ_NONNULL double *result);
90 
91 RZ_API RZ_OWN char *rz_diff_unified_text(RZ_NONNULL RzDiff *diff, RZ_NULLABLE const char *from, RZ_NULLABLE const char *to, bool show_time, bool color);
92 RZ_API RZ_OWN PJ *rz_diff_unified_json(RZ_NONNULL RzDiff *diff, RZ_NULLABLE const char *from, RZ_NULLABLE const char *to, bool show_time);
93 
94 /* Distances algorithms */
95 RZ_API bool rz_diff_myers_distance(RZ_NONNULL const ut8 *a, ut32 size_a, RZ_NONNULL const ut8 *b, ut32 size_b, RZ_NULLABLE ut32 *distance, RZ_NULLABLE double *similarity);
96 RZ_API bool rz_diff_levenstein_distance(RZ_NONNULL const ut8 *a, ut32 size_a, RZ_NONNULL const ut8 *b, ut32 size_b, RZ_NULLABLE ut32 *distance, RZ_NULLABLE double *similarity);
97 
98 #endif
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif /* RZ_DIFF_H */
static SblHeader sb
Definition: bin_mbn.c:26
#define RZ_API
uint32_t ut32
RZ_API RZ_OWN RzDiff * rz_diff_generic_new(RZ_BORROW const void *a, ut32 a_size, RZ_BORROW const void *b, ut32 b_size, RZ_NONNULL RzDiffMethods *methods)
Returns the structure needed to diff arrays of user defined types.
Definition: diff.c:259
RZ_API RZ_BORROW const void * rz_diff_get_b(RZ_NONNULL RzDiff *diff)
returns the pointer of the B array that passed to rz_diff_XXX_new()
Definition: diff.c:322
RZ_API RZ_OWN RzList * rz_diff_opcodes_new(RZ_NONNULL RzDiff *diff)
Generates a list of steps needed to go from A to B.
Definition: diff.c:631
RZ_API ut32 rz_diff_hash_data(RZ_NULLABLE const ut8 *buffer, ut32 size)
Calculates the hash of any given data.
Definition: diff.c:103
RZ_API RZ_BORROW const void * rz_diff_get_a(RZ_NONNULL RzDiff *diff)
returns the pointer of the A array that passed to rz_diff_XXX_new()
Definition: diff.c:312
RZ_API RZ_OWN RzList * rz_diff_matches_new(RZ_NONNULL RzDiff *diff)
generates a list of matching blocks
Definition: diff.c:497
RZ_API bool rz_diff_sizes_ratio(RZ_NONNULL RzDiff *diff, RZ_NONNULL double *result)
Calculates the size ratio between A and B.
Definition: diff.c:865
RZ_API bool rz_diff_ratio(RZ_NONNULL RzDiff *diff, RZ_NONNULL double *result)
Calculates the similarity ratio between A and B.
Definition: diff.c:831
RZ_API RZ_OWN RzDiff * rz_diff_lines_new(RZ_BORROW const char *a, RZ_BORROW const char *b, RZ_NULLABLE RzDiffIgnoreLine ignore)
Returns the structure needed to diff lines.
Definition: diff.c:219
RZ_API RZ_OWN RzDiff * rz_diff_bytes_new(RZ_BORROW const ut8 *a, ut32 a_size, RZ_BORROW const ut8 *b, ut32 b_size, RZ_NULLABLE RzDiffIgnoreByte ignore)
Returns the structure needed to diff buffers of ut8.
Definition: diff.c:188
RZ_API RZ_OWN RzList * rz_diff_opcodes_grouped_new(RZ_NONNULL RzDiff *diff, ut32 n_groups)
Generates groups of opcodes needed to go from A to B.
Definition: diff.c:712
RZ_API void rz_diff_free(RZ_NULLABLE RzDiff *diff)
frees the diff structure
Definition: diff.c:295
RZ_API bool rz_diff_myers_distance(RZ_NONNULL const ut8 *a, ut32 la, RZ_NONNULL const ut8 *b, ut32 lb, RZ_NULLABLE ut32 *distance, RZ_NULLABLE double *similarity)
Calculates the distance between two buffers using the Myers algorithm.
Definition: distance.c:14
RZ_API bool rz_diff_levenstein_distance(RZ_NONNULL const ut8 *a, ut32 la, RZ_NONNULL const ut8 *b, ut32 lb, RZ_NULLABLE ut32 *distance, RZ_NULLABLE double *similarity)
Calculates the distance between two buffers using the Levenshtein algorithm.
Definition: distance.c:68
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
line
Definition: setup.py:34
const void *(* RzDiffMethodElemAt)(RZ_BORROW const void *array, ut32 index)
Definition: rz_diff.h:36
ut32(* RzDiffMethodElemHash)(RZ_BORROW const void *elem)
Definition: rz_diff.h:37
rz_diff_op_type_t
Definition: rz_diff.h:19
@ RZ_DIFF_OP_INVALID
Definition: rz_diff.h:20
@ RZ_DIFF_OP_REPLACE
Definition: rz_diff.h:24
@ RZ_DIFF_OP_DELETE
Definition: rz_diff.h:21
@ RZ_DIFF_OP_EQUAL
Definition: rz_diff.h:22
@ RZ_DIFF_OP_INSERT
Definition: rz_diff.h:23
bool(* RzDiffMethodIgnore)(RZ_BORROW const void *elem)
Definition: rz_diff.h:39
RZ_LIB_VERSION_HEADER(rz_diff)
bool(* RzDiffIgnoreLine)(RZ_BORROW const char *line)
Definition: rz_diff.h:68
void(* RzDiffMethodStringify)(RZ_BORROW const void *elem, RZ_BORROW RzStrBuf *sb)
Definition: rz_diff.h:40
int(* RzDiffMethodCompare)(RZ_BORROW const void *a_elem, RZ_BORROW const void *b_elem)
Definition: rz_diff.h:38
struct rz_diff_op_t RzDiffOp
bool(* RzDiffIgnoreByte)(const ut64 byte)
Definition: rz_diff.h:67
enum rz_diff_op_type_t RzDiffOpType
struct rz_diff_methods_t RzDiffMethods
struct match_p_t RzDiffMatch
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_BORROW
Definition: rz_types.h:63
#define st32
Definition: rz_types_base.h:12
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
static int
Definition: sfsocketcall.h:114
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
Definition: buffer.h:15
ut32 b
Definition: rz_diff.h:63
ut32 size
Definition: rz_diff.h:64
ut32 a
Definition: rz_diff.h:62
Definition: rz_pj.h:12
RzDiffMethodStringify stringify
elements from A and B
Definition: rz_diff.h:45
RzDiffMethodCompare compare
elements from A and B
Definition: rz_diff.h:44
RzDiffMethodElemAt elem_at
can be either be an element of A or B
Definition: rz_diff.h:42
RzDiffMethodElemHash elem_hash
can be either be an element of A or B
Definition: rz_diff.h:43
RzDiffMethodIgnore ignore
elements from A and B
Definition: rz_diff.h:46
st32 a_end
Definition: rz_diff.h:52
RzDiffOpType type
Definition: rz_diff.h:50
st32 b_end
Definition: rz_diff.h:54
st32 a_beg
Definition: rz_diff.h:51
st32 b_beg
Definition: rz_diff.h:53
Definition: diff.c:89
MethodsInternal methods
Definition: diff.c:95
ut32 b_size
Definition: diff.c:93
ut32 a_size
Definition: diff.c:92
#define bool
Definition: sysdefs.h:146
RZ_API RZ_OWN char * rz_diff_unified_text(RZ_NONNULL RzDiff *diff, RZ_NULLABLE const char *from, RZ_NULLABLE const char *to, bool show_time, bool color)
Produces a diff output with A and B inputs presented immediately adjacent to each other.
Definition: unified_diff.c:333
RZ_API RZ_OWN PJ * rz_diff_unified_json(RZ_NONNULL RzDiff *diff, RZ_NULLABLE const char *from, RZ_NULLABLE const char *to, bool show_time)
Produces a diff output to convert A in B in a JSON format.
Definition: unified_diff.c:409
static int color
Definition: visual.c:20
ut64(WINAPI *w32_GetEnabledXStateFeatures)()