Rizin
unix-like reverse engineering framework and cli tools
assembler.c File Reference
#include "assembler.h"
#include "const.h"

Go to the source code of this file.

Classes

struct  _jasm
 

Macros

#define return_error_if_size_lt(a, b)
 
#define return_error_if_empty_input(a, b)
 
#define NS(x)   x, (sizeof(x) - 1)
 

Typedefs

typedef bool(* AsmEncoder) (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
typedef struct _jasm JavaAsm
 

Functions

static bool encode_not_implemented (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_only_bcode (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_st8 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_ut8 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_addr32 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_addr16 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_st16 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_const_pool8 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_const_pool16 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_const_pool16_ut8 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_ut8x2 (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
static bool encode_atype (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 
bool java_assembler (const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
 

Variables

static const JavaAsm instructions [205]
 

Macro Definition Documentation

◆ NS

#define NS (   x)    x, (sizeof(x) - 1)

Definition at line 262 of file assembler.c.

◆ return_error_if_empty_input

#define return_error_if_empty_input (   a,
  b 
)
Value:
do { \
if (RZ_STR_ISEMPTY(a) || b < 1) { \
RZ_LOG_ERROR("[!] java_assembler: the input is empty.\n"); \
return false; \
} \
} while (0)
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

Definition at line 15 of file assembler.c.

◆ return_error_if_size_lt

#define return_error_if_size_lt (   a,
  b 
)
Value:
do { \
if (a < b) { \
RZ_LOG_ERROR("[!] java_assembler: no enough output buffer (requires %d bytes).\n", b); \
return false; \
} \
} while (0)

Definition at line 7 of file assembler.c.

Typedef Documentation

◆ AsmEncoder

typedef bool(* AsmEncoder) (ut8 bytecode, const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)

Definition at line 23 of file assembler.c.

◆ JavaAsm

typedef struct _jasm JavaAsm

Function Documentation

◆ encode_addr16()

static bool encode_addr16 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 92 of file assembler.c.

92  {
93  return_error_if_size_lt(output_size, 3);
95 
97  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between %d and %d (inclusive).\n", input, INT16_MIN, INT16_MAX);
98  return false;
99  }
100 
101  *written = 3;
102  output[0] = bytecode;
103  st64 n = strtoll(input, NULL, 0);
104  st16 addr = n - pc;
105  rz_write_be16(output + 1, addr);
106  return true;
107 }
#define NULL
Definition: cris-opc.c:27
#define return_error_if_empty_input(a, b)
Definition: assembler.c:15
#define return_error_if_size_lt(a, b)
Definition: assembler.c:7
int n
Definition: mipsasm.c:19
static void rz_write_be16(void *dest, ut16 val)
Definition: rz_endian.h:60
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API bool rz_is_valid_input_num_value(RzNum *num, const char *input_value)
Definition: unum.c:735
#define st64
Definition: rz_types_base.h:10
#define st16
Definition: rz_types_base.h:14
#define INT16_MAX
#define INT16_MIN
static int addr
Definition: z80asm.c:58
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237

References addr, input(), INT16_MAX, INT16_MIN, n, NULL, output, pc, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, rz_write_be16(), st16, and st64.

◆ encode_addr32()

static bool encode_addr32 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 75 of file assembler.c.

75  {
76  return_error_if_size_lt(output_size, 5);
78 
80  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between %d and %d (inclusive).\n", input, INT16_MIN, INT16_MAX);
81  return false;
82  }
83 
84  *written = 5;
85  output[0] = bytecode;
86  st64 n = strtoll(input, NULL, 0);
87  st32 addr = n - pc;
89  return true;
90 }
static void rz_write_be32(void *dest, ut32 val)
Definition: rz_endian.h:98
#define st32
Definition: rz_types_base.h:12

References addr, input(), INT16_MAX, INT16_MIN, n, NULL, output, pc, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, rz_write_be32(), st32, and st64.

◆ encode_atype()

static bool encode_atype ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 229 of file assembler.c.

229  {
230  return_error_if_size_lt(output_size, 2);
231  return_error_if_empty_input(input, input_size);
232 
233  ut8 byte = 0;
234  /* bool 4, char 5, float 6, double 7, byte 8, short 9, int 10, long 11 */
235  if (!strncmp(input, "bool", strlen("bool"))) {
236  byte = 4;
237  } else if (!strncmp(input, "char", strlen("char"))) {
238  byte = 5;
239  } else if (!strncmp(input, "float", strlen("float"))) {
240  byte = 6;
241  } else if (!strncmp(input, "double", strlen("double"))) {
242  byte = 7;
243  } else if (!strncmp(input, "byte", strlen("byte"))) {
244  byte = 8;
245  } else if (!strncmp(input, "short", strlen("short"))) {
246  byte = 9;
247  } else if (!strncmp(input, "int", strlen("int"))) {
248  byte = 10;
249  } else if (!strncmp(input, "long", strlen("long"))) {
250  byte = 11;
251  } else {
252  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid native type (accepted: bool, char, float, double, byte, short, int, long).\n", input);
253  return false;
254  }
255 
256  *written = 2;
257  output[0] = bytecode;
258  output[1] = byte;
259  return true;
260 }
uint8_t ut8
Definition: lh5801.h:11

References input(), output, return_error_if_empty_input, return_error_if_size_lt, and RZ_LOG_ERROR.

◆ encode_const_pool16()

static bool encode_const_pool16 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 145 of file assembler.c.

145  {
146  return_error_if_size_lt(output_size, 3);
147  return_error_if_empty_input(input, input_size);
148 
149  ut32 cpool_len = strlen(JAVA_ASM_CONSTANT_POOL_STR);
150  if (!strncmp(input, JAVA_ASM_CONSTANT_POOL_STR, cpool_len)) {
151  input += cpool_len;
152  }
153 
155  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", input, UINT16_MAX);
156  return false;
157  }
158 
159  *written = 3;
160  output[0] = bytecode;
161  ut16 n = (ut16)strtoll(input, NULL, 0);
162  rz_write_be16(output + 1, n);
163  return true;
164 }
#define JAVA_ASM_CONSTANT_POOL_STR
Definition: const.h:7
uint16_t ut16
uint32_t ut32
#define UINT16_MAX

References input(), JAVA_ASM_CONSTANT_POOL_STR, n, NULL, output, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, rz_write_be16(), and UINT16_MAX.

◆ encode_const_pool16_ut8()

static bool encode_const_pool16_ut8 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 166 of file assembler.c.

166  {
167  return_error_if_size_lt(output_size, 4);
168  return_error_if_empty_input(input, input_size);
169 
170  ut32 cpool_len = strlen(JAVA_ASM_CONSTANT_POOL_STR);
171  if (!strncmp(input, JAVA_ASM_CONSTANT_POOL_STR, cpool_len)) {
172  input += cpool_len;
173  }
174 
176  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", input, UINT16_MAX);
177  return false;
178  }
179 
180  const char *next = NULL;
181  char *tmp = NULL;
182  ut16 cpool = (ut16)strtoll(input, &tmp, 0);
183  if (!tmp || tmp == (input + input_size) || !(next = rz_str_trim_head_ro(tmp))) {
184  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", tmp, UINT8_MAX);
185  }
186 
187  if (!rz_is_valid_input_num_value(NULL, next)) {
188  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", input, UINT8_MAX);
189  return false;
190  }
191  ut8 num = (ut8)strtoll(next, NULL, 0);
192 
193  *written = 4;
194  output[0] = bytecode;
195  rz_write_be16(output + 1, cpool);
196  output[3] = num;
197  return true;
198 }
#define ut8
Definition: dcpu16.h:8
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
#define UINT8_MAX

References input(), JAVA_ASM_CONSTANT_POOL_STR, NULL, num, output, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, rz_str_trim_head_ro(), rz_write_be16(), autogen_x86imm::tmp, UINT16_MAX, UINT8_MAX, and ut8.

◆ encode_const_pool8()

static bool encode_const_pool8 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 125 of file assembler.c.

125  {
126  return_error_if_size_lt(output_size, 2);
127  return_error_if_empty_input(input, input_size);
128 
129  ut32 cpool_len = strlen(JAVA_ASM_CONSTANT_POOL_STR);
130  if (!strncmp(input, JAVA_ASM_CONSTANT_POOL_STR, cpool_len)) {
131  input += cpool_len;
132  }
133 
135  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", input, UINT8_MAX);
136  return false;
137  }
138 
139  *written = 2;
140  output[0] = bytecode;
141  output[1] = (ut8)strtoll(input, NULL, 0);
142  return true;
143 }

References input(), JAVA_ASM_CONSTANT_POOL_STR, NULL, output, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, UINT8_MAX, and ut8.

◆ encode_not_implemented()

static bool encode_not_implemented ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 32 of file assembler.c.

32  {
33  RZ_LOG_ERROR("[!] java_assembler: not implemented.\n");
34  return false;
35 }

References RZ_LOG_ERROR.

◆ encode_only_bcode()

static bool encode_only_bcode ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 37 of file assembler.c.

37  {
38  return_error_if_size_lt(output_size, 1);
39 
40  *written = 1;
41  output[0] = bytecode;
42  return true;
43 }

References output, and return_error_if_size_lt.

◆ encode_st16()

static bool encode_st16 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 109 of file assembler.c.

109  {
110  return_error_if_size_lt(output_size, 3);
111  return_error_if_empty_input(input, input_size);
112 
114  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between %d and %d (inclusive).\n", input, INT16_MIN, INT16_MAX);
115  return false;
116  }
117 
118  *written = 3;
119  output[0] = bytecode;
120  st16 n = strtoll(input, NULL, 0);
121  rz_write_be16(output + 1, n);
122  return true;
123 }

References input(), INT16_MAX, INT16_MIN, n, NULL, output, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, rz_write_be16(), and st16.

◆ encode_st8()

static bool encode_st8 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 45 of file assembler.c.

45  {
46  return_error_if_size_lt(output_size, 2);
48 
50  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between %d and %d (inclusive).\n", input, INT8_MIN, INT8_MAX);
51  return false;
52  }
53 
54  *written = 2;
55  output[0] = bytecode;
56  ((st8 *)output)[1] = (st8)strtoll(input, NULL, 0);
57  return true;
58 }
#define st8
Definition: rz_types_base.h:16
#define INT8_MIN
#define INT8_MAX

References input(), INT8_MAX, INT8_MIN, NULL, output, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, and st8.

◆ encode_ut8()

static bool encode_ut8 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 60 of file assembler.c.

60  {
61  return_error_if_size_lt(output_size, 2);
63 
65  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between %d and %d (inclusive).\n", input, INT8_MIN, INT8_MAX);
66  return false;
67  }
68 
69  *written = 2;
70  output[0] = bytecode;
71  output[1] = (ut8)strtoll(input, NULL, 0);
72  return true;
73 }

References input(), INT8_MAX, INT8_MIN, NULL, output, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, and ut8.

◆ encode_ut8x2()

static bool encode_ut8x2 ( ut8  bytecode,
const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)
static

Definition at line 200 of file assembler.c.

200  {
201  return_error_if_size_lt(output_size, 3);
202  return_error_if_empty_input(input, input_size);
203 
205  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", input, UINT8_MAX);
206  return false;
207  }
208 
209  const char *next = NULL;
210  char *tmp = NULL;
211  ut16 arg0 = (ut16)strtoll(input, &tmp, 0);
212  if (!tmp || tmp == (input + input_size) || !(next = rz_str_trim_head_ro(tmp))) {
213  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", tmp, UINT8_MAX);
214  }
215 
216  if (!rz_is_valid_input_num_value(NULL, next)) {
217  RZ_LOG_ERROR("[!] java_assembler: '%s' is not a valid number between 0 and %u (inclusive).\n", input, UINT8_MAX);
218  return false;
219  }
220  ut8 arg1 = (ut8)strtoll(next, NULL, 0);
221 
222  *written = 3;
223  output[0] = bytecode;
224  output[1] = arg0;
225  output[2] = arg1;
226  return true;
227 }

References input(), NULL, output, return_error_if_empty_input, return_error_if_size_lt, rz_is_valid_input_num_value(), RZ_LOG_ERROR, rz_str_trim_head_ro(), autogen_x86imm::tmp, UINT8_MAX, and ut8.

◆ java_assembler()

bool java_assembler ( const char *  input,
st32  input_size,
ut8 output,
st32  output_size,
ut64  pc,
st32 written 
)

Definition at line 472 of file assembler.c.

472  {
473  rz_return_val_if_fail(input && output && input_size > 0 && output_size > 0, false);
474 
475  for (ut32 i = 0; i < RZ_ARRAY_SIZE(instructions); ++i) {
476  if (input_size < instructions[i].length) {
477  continue;
478  }
480  const char *p = rz_str_trim_head_ro(input + instructions[i].length);
481  st32 used = p ? (p - input) : input_size;
482  return instructions[i].encode(instructions[i].bytecode, p, input_size - used, output, output_size, pc, written);
483  }
484  }
485 
486  RZ_LOG_ERROR("[!] java_assembler: invalid assembly.\n");
487  return false;
488 }
lzma_index ** i
Definition: index.h:629
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 length
Definition: sflib.h:133
static const JavaAsm instructions[205]
Definition: assembler.c:263
void * p
Definition: libc.cpp:67
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API int rz_str_ncasecmp(const char *dst, const char *orig, size_t n)
Definition: str.c:129
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
AsmEncoder encode
Definition: assembler.c:29

References _jasm::encode, i, input(), instructions, length, output, p, pc, RZ_ARRAY_SIZE, RZ_LOG_ERROR, rz_return_val_if_fail, rz_str_ncasecmp(), rz_str_trim_head_ro(), and st32.

Referenced by java_assemble().

Variable Documentation

◆ instructions

const JavaAsm instructions[205]
static

Definition at line 263 of file assembler.c.

Referenced by java_assembler().