Rizin
unix-like reverse engineering framework and cli tools
annotated_code.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 NIRMAL MANOJ C <nimmumanoj@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
5 #include <rz_core.h>
6 #include <rz_util.h>
7 
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 }
17 
18 RZ_API void rz_annotation_free(void *e, void *user) {
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 }
27 
30 }
31 
34 }
35 
37  if (!code) {
38  return;
39  }
40  rz_vector_clear(&code->annotations);
41  rz_free(code->code);
42  rz_free(code);
43 }
44 
46  rz_vector_push(&code->annotations, annotation);
47 }
48 
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 }
62 
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 }
77 
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
#define e(frag)
RZ_API bool rz_annotation_is_reference(RzCodeAnnotation *annotation)
Checks if the specified annotation is a reference.
RZ_API void rz_annotation_free(void *e, void *user)
Deallocates dynamically allocated memory for the specified annotation.
RZ_API RzAnnotatedCode * rz_annotated_code_new(char *code)
Create and initialize a RzAnnotatedCode structure and returns its pointer.
Definition: annotated_code.c:8
RZ_API RzVector * rz_annotated_code_line_offsets(RzAnnotatedCode *code)
Returns the offset for every line of decompiled code in the specified RzAnnotatedCode.
RZ_API bool rz_annotation_is_variable(RzCodeAnnotation *annotation)
Checks if the specified annotation is a function variable.
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_API void rz_annotated_code_add_annotation(RzAnnotatedCode *code, RzCodeAnnotation *annotation)
Inserts the specified annotation into the list of annotations in the specified RzAnnotatedCode.
RZ_API void rz_annotated_code_free(RzAnnotatedCode *code)
Deallocates the dynamically allocated memory for the specified RzAnnotatedCode.
RZ_API RzPVector * rz_annotated_code_annotations_in(RzAnnotatedCode *code, size_t offset)
Returns all annotations with range that contains the given offset.
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf uLong offset
Definition: ioapi.h:144
const char * code
Definition: pal.c:98
#define rz_free(x)
Definition: rz_alloc.h:44
@ RZ_CODE_ANNOTATION_TYPE_GLOBAL_VARIABLE
@ RZ_CODE_ANNOTATION_TYPE_FUNCTION_PARAMETER
@ RZ_CODE_ANNOTATION_TYPE_CONSTANT_VARIABLE
@ RZ_CODE_ANNOTATION_TYPE_LOCAL_VARIABLE
@ RZ_CODE_ANNOTATION_TYPE_OFFSET
@ RZ_CODE_ANNOTATION_TYPE_FUNCTION_NAME
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define UT64_MAX
Definition: rz_types_base.h:86
RZ_API void * rz_vector_push(RzVector *vec, void *x)
Definition: vector.c:197
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
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
RZ_API void rz_vector_clear(RzVector *vec)
Definition: vector.c:68
RZ_API void rz_vector_init(RzVector *vec, size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:33
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
Definition: inftree9.h:24
This structure contains the decompiled code and all the annotations for the decompiled code.
Annotations for the decompiled code are represented using this structure.
RzCodeAnnotationType type
struct rz_code_annotation_t::@297::@301 reference
struct rz_code_annotation_t::@297::@302 variable
ut64(WINAPI *w32_GetEnabledXStateFeatures)()