1 /* radare - LGPL - Copyright 2018-2020 - pancake */
2
3 #include <r_asm.h>
4
r_asm_op_new(void)5 R_API RAsmOp *r_asm_op_new(void) {
6 return R_NEW0 (RAsmOp);
7 }
8
r_asm_op_free(RAsmOp * op)9 R_API void r_asm_op_free(RAsmOp *op) {
10 r_asm_op_fini (op);
11 free (op);
12 }
13
r_asm_op_init(RAsmOp * op)14 R_API void r_asm_op_init(RAsmOp *op) {
15 if (op) {
16 memset (op, 0, sizeof (*op));
17 }
18 }
19
r_asm_op_fini(RAsmOp * op)20 R_API void r_asm_op_fini(RAsmOp *op) {
21 r_strbuf_fini (&op->buf);
22 r_strbuf_fini (&op->buf_asm);
23 r_buf_fini (op->buf_inc);
24 }
25
26 // accessors
r_asm_op_get_hex(RAsmOp * op)27 R_API char *r_asm_op_get_hex(RAsmOp *op) {
28 r_return_val_if_fail (op, NULL);
29 int size = r_strbuf_length (&op->buf);
30 char* str = calloc (size + 1, 2);
31 r_return_val_if_fail (str, NULL);
32 r_hex_bin2str ((const ut8*) r_strbuf_get (&op->buf), size, str);
33 return str;
34 }
35
r_asm_op_get_asm(RAsmOp * op)36 R_API char *r_asm_op_get_asm(RAsmOp *op) {
37 r_return_val_if_fail (op, NULL);
38 return r_strbuf_get (&op->buf_asm);
39 }
40
r_asm_op_get_buf(RAsmOp * op)41 R_API ut8 *r_asm_op_get_buf(RAsmOp *op) {
42 r_return_val_if_fail (op, NULL);
43 return (ut8*)r_strbuf_get (&op->buf);
44 }
45
r_asm_op_get_size(RAsmOp * op)46 R_API int r_asm_op_get_size(RAsmOp *op) {
47 r_return_val_if_fail (op, 1);
48 const int len = op->size - op->payload;
49 return R_MAX (1, len);
50 }
51
r_asm_op_set_asm(RAsmOp * op,const char * str)52 R_API void r_asm_op_set_asm(RAsmOp *op, const char *str) {
53 r_return_if_fail (op && str);
54 r_strbuf_set (&op->buf_asm, str);
55 }
56
r_asm_op_set_hex(RAsmOp * op,const char * str)57 R_API int r_asm_op_set_hex(RAsmOp *op, const char *str) {
58 ut8 *bin = (ut8*)strdup (str);
59 if (bin) {
60 int len = r_hex_str2bin (str, bin);
61 if (len > 0) {
62 r_strbuf_setbin (&op->buf, bin, len);
63 }
64 free (bin);
65 return len;
66 }
67 return 0;
68 }
69
r_asm_op_set_hexbuf(RAsmOp * op,const ut8 * buf,int len)70 R_API int r_asm_op_set_hexbuf(RAsmOp *op, const ut8 *buf, int len) {
71 r_return_val_if_fail (op && buf && len >= 0, 0);
72 char *hex = malloc (len * 4 + 1);
73 if (hex) {
74 (void)r_hex_bin2str (buf, len, hex);
75 int olen = r_asm_op_set_hex (op, hex);
76 free (hex);
77 return olen;
78 }
79 return 0;
80 }
81
r_asm_op_set_buf(RAsmOp * op,const ut8 * buf,int len)82 R_API void r_asm_op_set_buf(RAsmOp *op, const ut8 *buf, int len) {
83 r_return_if_fail (op && buf && len >= 0);
84 r_strbuf_setbin (&op->buf, buf, len);
85 r_asm_op_set_hexbuf (op, buf, len);
86 }
87