Rizin
unix-like reverse engineering framework and cli tools
TestSparc.java
Go to the documentation of this file.
1 // Capstone Java binding
2 // By Nguyen Anh Quynh & Dang Hoang Vu, 2013-2014
3 
4 import capstone.Capstone;
5 import capstone.Sparc;
6 
7 import static capstone.Sparc_const.*;
8 
9 public class TestSparc {
10 
11  static byte[] hexString2Byte(String s) {
12  // from http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
13  int len = s.length();
14  byte[] data = new byte[len / 2];
15  for (int i = 0; i < len; i += 2) {
16  data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
17  + Character.digit(s.charAt(i+1), 16));
18  }
19  return data;
20  }
21 
22  static final String SPARC_CODE = "80a0400285c2600885e8200181e8000090102001d5f610162100000a860040020100000012bfffff10bfffffa00200090dbfffffd4206000d44e00162ac28003";
23  static final String SPARCV9_CODE = "81a80a2489a0102089a01a6089a000e0";
24 
25  public static Capstone cs;
26 
27  private static String hex(int i) {
28  return Integer.toString(i, 16);
29  }
30 
31  private static String hex(long i) {
32  return Long.toString(i, 16);
33  }
34 
35  public static void print_ins_detail(Capstone.CsInsn ins) {
36  System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
37 
38  Sparc.OpInfo operands = (Sparc.OpInfo) ins.operands;
39 
40  if (operands.op.length != 0) {
41  System.out.printf("\top_count: %d\n", operands.op.length);
42  for (int c=0; c<operands.op.length; c++) {
44  if (i.type == SPARC_OP_REG)
45  System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
46  if (i.type == SPARC_OP_IMM)
47  System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
48  if (i.type == SPARC_OP_MEM) {
49  System.out.printf("\t\toperands[%d].type: MEM\n", c);
50  if (i.value.mem.base != SPARC_REG_INVALID)
51  System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, ins.regName(i.value.mem.base));
52  if (i.value.mem.index != SPARC_REG_INVALID)
53  System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, ins.regName(i.value.mem.index));
54  if (i.value.mem.disp != 0)
55  System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, i.value.mem.disp);
56  }
57  }
58  }
59 
60  if (operands.cc != 0)
61  System.out.printf("\tCode condition: %d\n", operands.cc);
62 
63  if (operands.hint != 0)
64  System.out.printf("\tHint code: %d\n", operands.hint);
65 
66  }
67 
68  public static void main(String argv[]) {
69 
70  final TestBasic.platform[] all_tests = {
71  new TestBasic.platform(Capstone.CS_ARCH_SPARC, Capstone.CS_MODE_BIG_ENDIAN, hexString2Byte(SPARC_CODE), "Sparc"),
72  new TestBasic.platform(Capstone.CS_ARCH_SPARC, Capstone.CS_MODE_BIG_ENDIAN + Capstone.CS_MODE_V9, hexString2Byte(SPARCV9_CODE), "SparcV9"),
73  };
74 
75  for (int i=0; i<all_tests.length; i++) {
77  System.out.println(new String(new char[16]).replace("\0", "*"));
78  System.out.println("Platform: " + test.comment);
79  System.out.println("Code: " + TestBasic.stringToHex(test.code));
80  System.out.println("Disasm:");
81 
82  cs = new Capstone(test.arch, test.mode);
83  cs.setDetail(Capstone.CS_OPT_ON);
84  Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
85 
86  for (int j = 0; j < all_ins.length; j++) {
87  print_ins_detail(all_ins[j]);
88  System.out.println();
89  }
90  System.out.printf("0x%x:\n\n", (all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size));
91 
92  // Close when done
93  cs.close();
94  }
95  }
96 
97 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static String stringToHex(byte[] code)
Definition: TestBasic.java:30
static void print_ins_detail(Capstone.CsInsn ins)
Definition: TestSparc.java:35
static String hex(int i)
Definition: TestSparc.java:27
static final String SPARC_CODE
Definition: TestSparc.java:22
static byte[] hexString2Byte(String s)
Definition: TestSparc.java:11
static final String SPARCV9_CODE
Definition: TestSparc.java:23
static Capstone cs
Definition: TestSparc.java:25
static void main(String argv[])
Definition: TestSparc.java:68
static String hex(long i)
Definition: TestSparc.java:31
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
Definition: Arm.java:4
tuple all_tests
Definition: test_arm.py:18
-lz4-versions
static RzSocket * s
Definition: rtr.c:28
#define c(i)
Definition: sha256.c:43
@ SPARC_REG_INVALID
Definition: sparc.h:79
@ SPARC_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: sparc.h:74
@ SPARC_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: sparc.h:73
@ SPARC_OP_REG
= CS_OP_REG (Register operand).
Definition: sparc.h:72
int replace(char *string, const char *token, const char *fmt,...)
Definition: tms320_dasm.c:325
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4