Rizin
unix-like reverse engineering framework and cli tools
assembly_53.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 // SPDX-FileCopyrightText: 2017 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2021 Heersin <teablearcher@gmail.com>
4 
5 #include "arch_53.h"
6 
7 static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16 flag, ut8 arg_num) {
9  int args[3];
10  char buffer[64]; // buffer for digits
11  int cur_cnt = 0;
12  int delta_offset;
13  int temp;
14 
15  if (arg_num > sizeof(args)) {
16  return -1;
17  }
18 
19  for (int i = 0; i < arg_num; ++i) {
20  delta_offset = lua_load_next_arg_start(arg_start, buffer);
21  if (delta_offset == 0) {
22  return -1;
23  }
26  arg_start += delta_offset;
27  } else {
28  return -1;
29  }
30  }
31 
32  SET_OPCODE(instruction, opcode);
33  if (has_param_flag(flag, PARAM_A)) {
34  SETARG_A(instruction, args[cur_cnt++]);
35  }
36  if (has_param_flag(flag, PARAM_B)) {
37  temp = args[cur_cnt++];
38  temp = temp < 0 ? 0xFF - temp : temp;
39  SETARG_B(instruction, temp);
40  }
41  if (has_param_flag(flag, PARAM_C)) {
42  temp = args[cur_cnt++];
43  temp = temp < 0 ? 0xFF - temp : temp;
44  SETARG_C(instruction, temp);
45  }
46  if (has_param_flag(flag, PARAM_Ax)) {
47  SETARG_Ax(instruction, args[cur_cnt++]);
48  }
49  if (has_param_flag(flag, PARAM_sBx)) {
50  SETARG_sBx(instruction, args[cur_cnt++]);
51  }
52  if (has_param_flag(flag, PARAM_Bx)) {
53  SETARG_Bx(instruction, args[cur_cnt++]);
54  }
55  assert(cur_cnt == arg_num);
56 
57  return instruction;
58 }
59 
60 bool lua53_assembly(const char *input, st32 input_size, LuaInstruction *instruction_p) {
61  const char *opcode_start; // point to the header
62  const char *opcode_end; // point to the first white space
63  int opcode_len;
64 
65  const char *arg_start;
66 
67  ut8 opcode;
69 
70  /* Find the opcode */
71  opcode_start = input;
72  opcode_end = strchr(input, ' ');
73  if (opcode_end == NULL) {
74  opcode_end = input + input_size;
75  }
76 
77  opcode_len = opcode_end - opcode_start;
78  opcode = get_lua53_opcode_by_name(opcode_start, opcode_len);
79 
80  /* Find the arguments */
81  arg_start = rz_str_trim_head_ro(opcode_end);
82 
83  /* Encode opcode and args */
84  switch (opcode) {
85  case OP_LOADKX:
86  instruction = encode_instruction(opcode, arg_start, PARAM_A, 1);
87  break;
88  case OP_MOVE:
89  case OP_SETUPVAL:
90  case OP_UNM:
91  case OP_BNOT:
92  case OP_NOT:
93  case OP_LEN:
94  case OP_LOADNIL:
95  case OP_RETURN:
96  case OP_VARARG:
97  case OP_GETUPVAL:
98  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_B, 2);
99  break;
100  case OP_TEST:
101  case OP_TFORCALL:
102  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_C, 2);
103  break;
104  case OP_LOADK:
105  case OP_CLOSURE:
106  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_Bx, 2);
107  break;
108  case OP_CONCAT:
109  case OP_TESTSET:
110  case OP_CALL:
111  case OP_TAILCALL:
112  case OP_NEWTABLE:
113  case OP_SETLIST:
114  case OP_LOADBOOL:
115  case OP_SELF:
116  case OP_GETTABUP:
117  case OP_GETTABLE:
118  case OP_SETTABUP:
119  case OP_SETTABLE:
120  case OP_ADD:
121  case OP_SUB:
122  case OP_MUL:
123  case OP_MOD:
124  case OP_POW:
125  case OP_DIV:
126  case OP_IDIV:
127  case OP_BAND:
128  case OP_BOR:
129  case OP_BXOR:
130  case OP_SHL:
131  case OP_SHR:
132  case OP_EQ:
133  case OP_LT:
134  case OP_LE:
135  instruction = encode_instruction(opcode, arg_start,
136  PARAM_A | PARAM_B | PARAM_C,
137  3);
138  break;
139  case OP_JMP:
140  case OP_FORLOOP:
141  case OP_FORPREP:
142  case OP_TFORLOOP:
143  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_sBx, 2);
144  break;
145  case OP_EXTRAARG:
146  instruction = encode_instruction(opcode, arg_start, PARAM_Ax, 1);
147  break;
148  default:
149  return false;
150  }
151 
152  if (instruction == -1) {
153  return false;
154  }
155 
156  *instruction_p = instruction;
157  return true;
158 }
@ OP_DIV
Definition: 8051_ops.h:51
@ OP_ADD
Definition: 8051_ops.h:42
@ OP_MUL
Definition: 8051_ops.h:63
@ OP_JMP
Definition: 8051_ops.h:57
lzma_index ** i
Definition: index.h:629
#define SETARG_A(i, v)
Definition: arch_53.h:177
#define PARAM_Ax
Definition: arch_53.h:41
#define SETARG_C(i, v)
Definition: arch_53.h:183
#define SETARG_Bx(i, v)
Definition: arch_53.h:186
#define PARAM_Bx
Definition: arch_53.h:42
#define PARAM_C
Definition: arch_53.h:40
@ OP_SETLIST
Definition: arch_53.h:122
@ OP_EQ
Definition: arch_53.h:104
@ OP_VARARG
Definition: arch_53.h:126
@ OP_CONCAT
Definition: arch_53.h:101
@ OP_BOR
Definition: arch_53.h:92
@ OP_SETTABLE
Definition: arch_53.h:78
@ OP_POW
Definition: arch_53.h:88
@ OP_NOT
Definition: arch_53.h:98
@ OP_TESTSET
Definition: arch_53.h:109
@ OP_MOD
Definition: arch_53.h:87
@ OP_CLOSURE
Definition: arch_53.h:124
@ OP_SETUPVAL
Definition: arch_53.h:77
@ OP_FORPREP
Definition: arch_53.h:117
@ OP_LEN
Definition: arch_53.h:99
@ OP_LOADNIL
Definition: arch_53.h:70
@ OP_BAND
Definition: arch_53.h:91
@ OP_SELF
Definition: arch_53.h:82
@ OP_SUB
Definition: arch_53.h:85
@ OP_SHR
Definition: arch_53.h:95
@ OP_LT
Definition: arch_53.h:105
@ OP_TFORLOOP
Definition: arch_53.h:120
@ OP_SHL
Definition: arch_53.h:94
@ OP_TEST
Definition: arch_53.h:108
@ OP_TFORCALL
Definition: arch_53.h:119
@ OP_FORLOOP
Definition: arch_53.h:115
@ OP_GETTABLE
Definition: arch_53.h:74
@ OP_LOADK
Definition: arch_53.h:67
@ OP_GETUPVAL
Definition: arch_53.h:71
@ OP_SETTABUP
Definition: arch_53.h:76
@ OP_IDIV
Definition: arch_53.h:90
@ OP_GETTABUP
Definition: arch_53.h:73
@ OP_LE
Definition: arch_53.h:106
@ OP_RETURN
Definition: arch_53.h:113
@ OP_BNOT
Definition: arch_53.h:97
@ OP_MOVE
Definition: arch_53.h:66
@ OP_UNM
Definition: arch_53.h:96
@ OP_EXTRAARG
Definition: arch_53.h:128
@ OP_LOADKX
Definition: arch_53.h:68
@ OP_NEWTABLE
Definition: arch_53.h:80
@ OP_LOADBOOL
Definition: arch_53.h:69
@ OP_BXOR
Definition: arch_53.h:93
@ OP_TAILCALL
Definition: arch_53.h:112
#define SETARG_B(i, v)
Definition: arch_53.h:180
#define PARAM_sBx
Definition: arch_53.h:43
#define PARAM_B
Definition: arch_53.h:39
#define SET_OPCODE(i, o)
Definition: arch_53.h:169
#define PARAM_A
Definition: arch_53.h:38
#define SETARG_sBx(i, b)
Definition: arch_53.h:192
#define has_param_flag(flag, bit)
Definition: arch_53.h:45
#define SETARG_Ax(i, v)
Definition: arch_53.h:189
static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16 flag, ut8 arg_num)
Definition: assembly_53.c:7
bool lua53_assembly(const char *input, st32 input_size, LuaInstruction *instruction_p)
Definition: assembly_53.c:60
#define NULL
Definition: cris-opc.c:27
uint16_t ut16
uint8_t ut8
Definition: lh5801.h:11
bool lua_is_valid_num_value_string(const char *str)
Definition: lua_arch.c:129
int lua_load_next_arg_start(const char *raw_string, char *recv_buf)
Definition: lua_arch.c:98
int lua_convert_str_to_num(const char *str)
Definition: lua_arch.c:137
ut32 LuaInstruction
Definition: lua_arch.h:27
ut8 get_lua53_opcode_by_name(const char *name, int len)
Definition: opcode_53.c:69
assert(limit<=UINT32_MAX/2)
int args
Definition: mipsasm.c:18
#define OP_CALL
Definition: nios2.h:263
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
#define st32
Definition: rz_types_base.h:12
Definition: buffer.h:15
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)