Rizin
unix-like reverse engineering framework and cli tools
cil_dis.h File Reference
#include <rz_util/rz_strbuf.h>
#include "opcodes_single.def"
#include "opcodes_double.def"
#include "opcodes_prefix.def"

Go to the source code of this file.

Classes

struct  CILOp
 

Macros

#define OPCODE_SINGLE(name, str, param, byte, control)   name = byte,
 
#define OPCODE_DOUBLE(name, str, param, byte, control)   name = byte,
 
#define OPCODE_PREFIX(name, str, param, byte, control)   name = byte,
 

Enumerations

enum  
 

Functions

int cil_dis (CILOp *op, const ut8 *buf, int len)
 Disassemble a CIL buffer. More...
 

Macro Definition Documentation

◆ OPCODE_DOUBLE

#define OPCODE_DOUBLE (   name,
  str,
  param,
  byte,
  control 
)    name = byte,

Definition at line 16 of file cil_dis.h.

◆ OPCODE_PREFIX

#define OPCODE_PREFIX (   name,
  str,
  param,
  byte,
  control 
)    name = byte,

Definition at line 17 of file cil_dis.h.

◆ OPCODE_SINGLE

#define OPCODE_SINGLE (   name,
  str,
  param,
  byte,
  control 
)    name = byte,

Definition at line 15 of file cil_dis.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Definition at line 18 of file cil_dis.h.

18  {
19 #include "opcodes_single.def"
20 #include "opcodes_double.def"
21 #include "opcodes_prefix.def"
22 };

Function Documentation

◆ cil_dis()

int cil_dis ( CILOp op,
const ut8 buf,
int  len 
)

Disassemble a CIL buffer.

Returns
0 on success, -1 on fail

Definition at line 150 of file cil_dis.c.

150  {
151  int pos = 0;
152  if (pos >= len) { // pos + 1 > len
153  return -1;
154  }
155 
156  ut8 byte;
157  rz_strbuf_init(&op->strbuf);
158 
159 start: // Taken after a prefix opcode has been consumed
160  byte = buf[pos++];
161 
162  CILOpcodeReader opcode_reader;
163  if (byte != 0xFE) { // Single-byte
164  op->byte1 = byte;
165  opcode_reader = opcode_readers_single[byte];
166  } else { // Double-byte
167  if (pos >= len) { // pos + 1 > len
168  return -1; // OOB
169  }
170 
171  op->byte1 = byte;
172  op->byte2 = byte = buf[pos++];
173  opcode_reader = opcode_readers_double[byte];
174  }
175 
176  if (!opcode_reader.str) {
177  return -1; // Invalid
178  }
179 
180  // Mnemonic
181  if (!rz_strbuf_append(&op->strbuf, opcode_reader.str)) {
182  return -1;
183  }
184 
185  // Dispatch based on opcode `param`
186  if (opcode_reader.read_param(&pos, op, buf, len)) {
187  return -1;
188  }
189 
190  if (opcode_reader.prefix) {
191  if (!rz_strbuf_append(&op->strbuf, " ")) { // extra space
192  return -1;
193  }
194  goto start; // continue
195  }
196 
197  op->size = pos;
198  return 0;
199 }
size_t len
Definition: 6502dis.c:15
static const CILOpcodeReader opcode_readers_single[]
Definition: cil_dis.c:129
static const CILOpcodeReader opcode_readers_double[]
Definition: cil_dis.c:140
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
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
char * str
Definition: cil_dis.c:111
int(* read_param)(int *pos, CILOp *op, const ut8 *buf, int len)
Definition: cil_dis.c:112
int pos
Definition: main.c:11
Definition: dis.c:32

References len, opcode_readers_double, opcode_readers_single, pos, CILOpcodeReader::prefix, CILOpcodeReader::read_param, rz_strbuf_append(), rz_strbuf_init(), start, and CILOpcodeReader::str.

Referenced by cil_analyze_op(), and disassemble().