Rizin
unix-like reverse engineering framework and cli tools
annotated_code.c File Reference
#include <rz_util/rz_annotated_code.h>
#include <rz_core.h>
#include <rz_util.h>

Go to the source code of this file.

Functions

RZ_API RzAnnotatedCoderz_annotated_code_new (char *code)
 Create and initialize a RzAnnotatedCode structure and returns its pointer. More...
 
RZ_API void rz_annotation_free (void *e, void *user)
 Deallocates dynamically allocated memory for the specified annotation. More...
 
RZ_API bool rz_annotation_is_reference (RzCodeAnnotation *annotation)
 Checks if the specified annotation is a reference. More...
 
RZ_API bool rz_annotation_is_variable (RzCodeAnnotation *annotation)
 Checks if the specified annotation is a function variable. More...
 
RZ_API void rz_annotated_code_free (RzAnnotatedCode *code)
 Deallocates the dynamically allocated memory for the specified RzAnnotatedCode. More...
 
RZ_API void rz_annotated_code_add_annotation (RzAnnotatedCode *code, RzCodeAnnotation *annotation)
 Inserts the specified annotation into the list of annotations in the specified RzAnnotatedCode. More...
 
RZ_API RzPVectorrz_annotated_code_annotations_in (RzAnnotatedCode *code, size_t offset)
 Returns all annotations with range that contains the given offset. More...
 
RZ_API RzPVectorrz_annotated_code_annotations_range (RzAnnotatedCode *code, size_t start, size_t end)
 Returns all annotations with range that overlap with the specified range. More...
 
RZ_API RzVectorrz_annotated_code_line_offsets (RzAnnotatedCode *code)
 Returns the offset for every line of decompiled code in the specified RzAnnotatedCode. More...
 

Function Documentation

◆ rz_annotated_code_add_annotation()

RZ_API void rz_annotated_code_add_annotation ( RzAnnotatedCode code,
RzCodeAnnotation annotation 
)

Inserts the specified annotation into the list of annotations in the specified RzAnnotatedCode.

Parameters
codePointer to a RzAnnotatedCode.
annotationPointer to an annotation.

Definition at line 45 of file annotated_code.c.

45  {
46  rz_vector_push(&code->annotations, annotation);
47 }
RZ_API void * rz_vector_push(RzVector *vec, void *x)
Definition: vector.c:197
Definition: inftree9.h:24

References rz_vector_push().

◆ rz_annotated_code_annotations_in()

RZ_API RzPVector* rz_annotated_code_annotations_in ( RzAnnotatedCode code,
size_t  offset 
)

Returns all annotations with range that contains the given offset.

Creates a RzPVector <RzCodeAnnotation> and inserts the pointers to all annotations in which annotation->start <= offset < annotation->end.

Parameters
codePointer to a RzAnnotatedCode.
offsetOffset.
Returns
Pointer to the RzPVector created.

Definition at line 49 of file annotated_code.c.

49  {
51  if (!r) {
52  return NULL;
53  }
54  RzCodeAnnotation *annotation;
55  rz_vector_foreach(&code->annotations, annotation) {
56  if (offset >= annotation->start && offset < annotation->end) {
57  rz_pvector_push(r, annotation);
58  }
59  }
60  return r;
61 }
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
voidpf uLong offset
Definition: ioapi.h:144
RZ_API RzPVector * rz_pvector_new(RzPVectorFree free)
Definition: vector.c:302
#define rz_vector_foreach(vec, it)
Definition: rz_vector.h:169
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300
Annotations for the decompiled code are represented using this structure.

References test_evm::end, NULL, r, rz_pvector_new(), rz_pvector_push(), rz_vector_foreach, and rz_code_annotation_t::start.

◆ rz_annotated_code_annotations_range()

RZ_API RzPVector* rz_annotated_code_annotations_range ( RzAnnotatedCode code,
size_t  start,
size_t  end 
)

Returns all annotations with range that overlap with the specified range.

Creates an RzPVector <RzCodeAnnotation> and inserts the pointers to all annotations whose range overlap with range specified.

Parameters
codePointer to a RzAnnotatedCode.
startStart of the range(inclusive).
endEnd of the range(exclusive).
Returns
Pointer to the RzPVector created.

Definition at line 63 of file annotated_code.c.

63  {
65  if (!r) {
66  return NULL;
67  }
68  RzCodeAnnotation *annotation;
69  rz_vector_foreach(&code->annotations, annotation) {
70  if (start >= annotation->end || end < annotation->start) {
71  continue;
72  }
73  rz_pvector_push(r, annotation);
74  }
75  return r;
76 }
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 start
Definition: sflib.h:133

References rz_code_annotation_t::end, NULL, r, rz_pvector_new(), rz_pvector_push(), rz_vector_foreach, and start.

Referenced by rz_annotated_code_line_offsets().

◆ rz_annotated_code_free()

RZ_API void rz_annotated_code_free ( RzAnnotatedCode code)

Deallocates the dynamically allocated memory for the specified RzAnnotatedCode.

Parameters
codePointer to a RzAnnotatedCode.

Definition at line 36 of file annotated_code.c.

36  {
37  if (!code) {
38  return;
39  }
40  rz_vector_clear(&code->annotations);
41  rz_free(code->code);
42  rz_free(code);
43 }
#define rz_free(x)
Definition: rz_alloc.h:44
RZ_API void rz_vector_clear(RzVector *vec)
Definition: vector.c:68

References rz_free, and rz_vector_clear().

◆ rz_annotated_code_line_offsets()

RZ_API RzVector* rz_annotated_code_line_offsets ( RzAnnotatedCode code)

Returns the offset for every line of decompiled code in the specified RzAnnotatedCode.

Creates an RzVector <ut64> and inserts the offsets for every seperate line of decompiled code in the specified RzAnnotatedCode. If a line of decompiled code doesn't have a unique offset, UT64_MAX is inserted as its offset.

Parameters
codePointer to a RzAnnotatedCode.
Returns
Pointer to the RzVector created.

Definition at line 78 of file annotated_code.c.

78  {
79  RzVector *r = rz_vector_new(sizeof(ut64), NULL, NULL);
80  if (!r) {
81  return NULL;
82  }
83  size_t cur = 0;
84  size_t len = strlen(code->code);
85  do {
86  char *next = strchr(code->code + cur, '\n');
87  size_t next_i = next ? (next - code->code) + 1 : len;
88  RzPVector *annotations = rz_annotated_code_annotations_range(code, cur, next_i);
90  void **it;
91  rz_pvector_foreach (annotations, it) {
92  RzCodeAnnotation *annotation = *it;
93  if (annotation->type != RZ_CODE_ANNOTATION_TYPE_OFFSET) {
94  continue;
95  }
96  offset = annotation->offset.offset;
97  break;
98  }
100  cur = next_i;
101  rz_pvector_free(annotations);
102  } while (cur < len);
103  return r;
104 }
size_t len
Definition: 6502dis.c:15
RZ_API RzPVector * rz_annotated_code_annotations_range(RzAnnotatedCode *code, size_t start, size_t end)
Returns all annotations with range that overlap with the specified range.
@ RZ_CODE_ANNOTATION_TYPE_OFFSET
#define UT64_MAX
Definition: rz_types_base.h:86
RZ_API void rz_pvector_free(RzPVector *vec)
Definition: vector.c:336
RZ_API RzVector * rz_vector_new(size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:42
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
RzCodeAnnotationType type
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References len, NULL, rz_code_annotation_t::offset, r, rz_annotated_code_annotations_range(), RZ_CODE_ANNOTATION_TYPE_OFFSET, rz_pvector_foreach, rz_pvector_free(), rz_vector_new(), rz_vector_push(), rz_code_annotation_t::type, ut64(), and UT64_MAX.

◆ rz_annotated_code_new()

RZ_API RzAnnotatedCode* rz_annotated_code_new ( char *  code)

Create and initialize a RzAnnotatedCode structure and returns its pointer.

This function creates and initializes a new RzAnnotatedCode structure with the specified decompiled code that's passed as an argument. Here, the argument code must be a string that can be deallocated. This will initialize RzVector <RzCodeAnnotation> annotations as well.

Parameters
codeA deallocatable character array.
Returns
Pointer to the new RzAnnotatedCode structure created.

Definition at line 8 of file annotated_code.c.

8  {
10  if (!r) {
11  return NULL;
12  }
13  r->code = code;
14  rz_vector_init(&r->annotations, sizeof(RzCodeAnnotation), rz_annotation_free, NULL);
15  return r;
16 }
RZ_API void rz_annotation_free(void *e, void *user)
Deallocates dynamically allocated memory for the specified annotation.
const char * code
Definition: pal.c:98
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API void rz_vector_init(RzVector *vec, size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:33
This structure contains the decompiled code and all the annotations for the decompiled code.

References code, NULL, r, rz_annotation_free(), RZ_NEW0, and rz_vector_init().

◆ rz_annotation_free()

RZ_API void rz_annotation_free ( void *  e,
void *  user 
)

Deallocates dynamically allocated memory for the specified annotation.

This function recognizes the type of the specified annotation and frees memory that is dynamically allocated for it.

Parameters
ePointer to the annotation.
userAlways NULL for this function. Present here for this function to be of the type RzVectorFree.

Definition at line 18 of file annotated_code.c.

18  {
19  (void)user;
20  RzCodeAnnotation *annotation = e;
21  if (annotation->type == RZ_CODE_ANNOTATION_TYPE_FUNCTION_NAME) {
22  free(annotation->reference.name);
24  free(annotation->variable.name);
25  }
26 }
#define e(frag)
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
@ RZ_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER
@ RZ_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE
@ RZ_CODE_ANNOTATION_TYPE_FUNCTION_NAME
struct rz_code_annotation_t::@297::@301 reference
struct rz_code_annotation_t::@297::@302 variable

References e, free(), rz_code_annotation_t::reference, RZ_CODE_ANNOTATION_TYPE_FUNCTION_NAME, RZ_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER, RZ_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE, rz_code_annotation_t::type, and rz_code_annotation_t::variable.

Referenced by rz_annotated_code_new().

◆ rz_annotation_is_reference()

RZ_API bool rz_annotation_is_reference ( RzCodeAnnotation annotation)

Checks if the specified annotation is a reference.

This function recognizes the type of the specified annotation and returns true if its type is any of the following three: RZ_CODE_ANNOTATION_TYPE_GLOBAL_VARIABLE, RZ_CODE_ANNOTATION_TYPE_CONSTANT_VARIABLE, RZ_CODE_ANNOTATION_TYPE_FUNCTION_NAME

Parameters
annotationPointer to an annotation.
Returns
Returns true if the specified annotation is a reference.

Definition at line 28 of file annotated_code.c.

28  {
30 }
@ RZ_CODE_ANNOTATION_TYPE_GLOBAL_VARIABLE
@ RZ_CODE_ANNOTATION_TYPE_CONSTANT_VARIABLE

References RZ_CODE_ANNOTATION_TYPE_CONSTANT_VARIABLE, RZ_CODE_ANNOTATION_TYPE_FUNCTION_NAME, RZ_CODE_ANNOTATION_TYPE_GLOBAL_VARIABLE, and rz_code_annotation_t::type.

◆ rz_annotation_is_variable()

RZ_API bool rz_annotation_is_variable ( RzCodeAnnotation annotation)

Checks if the specified annotation is a function variable.

This function recognizes the type of the specified annotation and returns true if its type is any of the following two: RZ_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE, RZ_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER

Parameters
annotationPointer to an annotation.
Returns
Returns true if the specified annotation is a function variable.

Definition at line 32 of file annotated_code.c.

32  {
34 }

References RZ_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER, RZ_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE, and rz_code_annotation_t::type.