Skip to content

Commit a566d81

Browse files
authored
Merge pull request onlyliuxin#49 from onlyliuxin/master
malware clean up
2 parents a910d77 + b4213a7 commit a566d81

File tree

1,496 files changed

+68519
-7146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,496 files changed

+68519
-7146
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package week06.expr;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
public class InfixExpr {
8+
String expr = null;
9+
10+
public InfixExpr(String expr) {
11+
this.expr = expr;
12+
}
13+
14+
public float evaluate() {
15+
float result = 0;
16+
17+
Stack operandStack = new Stack(); // 运算数栈
18+
Stack operaterStack = new Stack(); // 运算符栈
19+
20+
char[] exprCharArr = expr.toCharArray();
21+
List<String> exprStringList = new ArrayList<String>();
22+
addOperandAndOperater(exprCharArr, exprStringList);
23+
24+
for (int i = 0; i < exprStringList.size(); i++) {
25+
if (isOperand(exprStringList.get(i))) {
26+
operandStack.push(exprStringList.get(i));
27+
} else if (isOperater(exprStringList.get(i))) {
28+
operaterStack.push(exprStringList.get(i));
29+
} else {
30+
throw new RuntimeException("this operater has not yet implemented.");
31+
}
32+
33+
if (operaterStack.size() == 2) {
34+
35+
String operater_1 = (String) operaterStack.pop();
36+
String operater_2 = (String) operaterStack.pop();
37+
if (hasTheSameOrHighPriority(operater_2, operater_1)) {
38+
39+
String operand_1 = (String) operandStack.pop();
40+
String operand_2 = (String) operandStack.pop();
41+
operation(operandStack, operater_2, operand_1, operand_2);
42+
43+
operaterStack.push(operater_1);
44+
} else if (hasTheLowPriority(operater_2, operater_1)) {
45+
46+
operandStack.push(exprStringList.get(++i));
47+
String operand_1 = (String) operandStack.pop();
48+
String operand_2 = (String) operandStack.pop();
49+
operation(operandStack, operater_1, operand_1, operand_2);
50+
51+
operaterStack.push(operater_2);
52+
}
53+
}
54+
55+
if (i == exprStringList.size() - 1) {
56+
57+
String operater = (String) operaterStack.pop();
58+
String operand_1 = (String) operandStack.pop();
59+
String operand_2 = (String) operandStack.pop();
60+
operation(operandStack, operater, operand_1, operand_2);
61+
62+
result = (float) Integer.parseInt((String) operandStack.pop());
63+
}
64+
}
65+
66+
return result;
67+
}
68+
69+
private void addOperandAndOperater(char[] exprCharArr, List<String> exprStringList) {
70+
for (int i = 0; i < exprCharArr.length; i++) {
71+
if (isOperand(exprCharArr[i])) {
72+
StringBuilder sb = new StringBuilder();
73+
sb.append(exprCharArr[i]);
74+
if (i < exprCharArr.length - 1) {
75+
while (i < exprCharArr.length - 1 && isOperand(exprCharArr[i + 1])) {
76+
sb.append(exprCharArr[i + 1]);
77+
i++;
78+
}
79+
}
80+
exprStringList.add(sb.toString());
81+
} else if (isOperater(exprCharArr[i])) {
82+
exprStringList.add(exprCharArr[i] + "");
83+
}
84+
}
85+
}
86+
87+
private boolean isOperand(char c) {
88+
return !isOperater(c);
89+
}
90+
91+
private boolean isOperater(char c) {
92+
if (c == '+' || c == '-' || c == '*' || c == '/') {
93+
return true;
94+
}
95+
return false;
96+
}
97+
98+
private boolean isOperand(String c) {
99+
return !isOperater(c);
100+
}
101+
102+
// 字符串相等用equals()比较.
103+
private boolean isOperater(String c) {
104+
if (c.equals("+") || c.equals("-") || c.equals("*") || c.equals("/")) {
105+
return true;
106+
}
107+
return false;
108+
}
109+
110+
// operater_1 has the same or high priority compare with the operater_2.
111+
private boolean hasTheSameOrHighPriority(Object operater_1, Object operater_2) {
112+
if ((operater_1.equals("+") && operater_2.equals("+")) || (operater_1.equals("+") && operater_2.equals("-"))
113+
|| (operater_1.equals("-") && operater_2.equals("+"))
114+
|| (operater_1.equals("-") && operater_2.equals("-"))
115+
|| (operater_1.equals("*") && operater_2.equals("*"))
116+
|| (operater_1.equals("*") && operater_2.equals("/"))
117+
|| (operater_1.equals("/") && operater_2.equals("*"))
118+
|| (operater_1.equals("/") && operater_2.equals("/"))
119+
|| (operater_1.equals("*") && operater_2.equals("+"))
120+
|| (operater_1.equals("*") && operater_2.equals("-"))
121+
|| (operater_1.equals("/") && operater_2.equals("+"))
122+
|| (operater_1.equals("/") && operater_2.equals("-"))) {
123+
return true;
124+
}
125+
return false;
126+
}
127+
128+
//// operater_1 has the low priority compare with the operater_2.
129+
private boolean hasTheLowPriority(Object operater_1, Object operater_2) {
130+
if ((operater_1.equals("+") && operater_2.equals("*")) || (operater_1.equals("+") && operater_2.equals("/"))
131+
|| (operater_1.equals("-") && operater_2.equals("*"))
132+
|| (operater_1.equals("-") && operater_2.equals("/"))) {
133+
return true;
134+
}
135+
return false;
136+
}
137+
138+
private void operation(Stack operandStack, String operater, String operand_1, String operand_2) {
139+
if (operater.equals("+")) {
140+
operandStack.push((Integer.parseInt(operand_2) + Integer.parseInt(operand_1)) + "");
141+
} else if (operater.equals("-")) {
142+
operandStack.push((Integer.parseInt(operand_2) - Integer.parseInt(operand_1)) + "");
143+
} else if (operater.equals("*")) {
144+
operandStack.push((Integer.parseInt(operand_2) * Integer.parseInt(operand_1)) + "");
145+
} else if (operater.equals("/")) {
146+
operandStack.push((Integer.parseInt(operand_2) / Integer.parseInt(operand_1)) + "");
147+
}
148+
}
149+
150+
}

group01/895457260/code/src/test/java/algorithm/InfixExprTest.java renamed to group01/1814014897/zhouhui/src/week06/expr/InfixExprTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package algorithm;
1+
package week06.expr;
22

33
import org.junit.After;
44
import org.junit.Assert;
@@ -17,7 +17,7 @@ public void tearDown() throws Exception {
1717
}
1818

1919
@Test
20-
public void testEvaluate() throws InfixExpr.CalculateException {
20+
public void testEvaluate() {
2121
//InfixExpr expr = new InfixExpr("300*20+12*5-20/4");
2222
{
2323
InfixExpr expr = new InfixExpr("2+3*4+5");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package week06.jvm.attr;
2+
3+
public abstract class AttributeInfo {
4+
public static final String CODE = "Code";
5+
public static final String CONST_VALUE = "ConstantValue";
6+
public static final String EXCEPTIONS = "Exceptions";
7+
public static final String LINE_NUM_TABLE = "LineNumberTable";
8+
public static final String LOCAL_VAR_TABLE = "LocalVariableTable";
9+
public static final String STACK_MAP_TABLE = "StackMapTable";
10+
int attrNameIndex;
11+
int attrLen ;
12+
public AttributeInfo(int attrNameIndex, int attrLen) {
13+
14+
this.attrNameIndex = attrNameIndex;
15+
this.attrLen = attrLen;
16+
}
17+
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package week06.jvm.attr;
2+
3+
import week06.jvm.clz.ClassFile;
4+
import week06.jvm.constant.ConstantPool;
5+
import week06.jvm.loader.ByteCodeIterator;
6+
7+
public class CodeAttr extends AttributeInfo {
8+
private int maxStack;
9+
private int maxLocals;
10+
private int codeLen;
11+
private String code;
12+
13+
public String getCode() {
14+
return code;
15+
}
16+
17+
// private ByteCodeCommand[] cmds ;
18+
// public ByteCodeCommand[] getCmds() {
19+
// return cmds;
20+
// }
21+
private LineNumberTable lineNumTable;
22+
private LocalVariableTable localVarTable;
23+
private StackMapTable stackMapTable;
24+
25+
public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,
26+
String code /* ByteCodeCommand[] cmds */) {
27+
super(attrNameIndex, attrLen);
28+
this.maxStack = maxStack;
29+
this.maxLocals = maxLocals;
30+
this.codeLen = codeLen;
31+
this.code = code;
32+
// this.cmds = cmds;
33+
}
34+
35+
public void setLineNumberTable(LineNumberTable t) {
36+
this.lineNumTable = t;
37+
}
38+
39+
public void setLocalVariableTable(LocalVariableTable t) {
40+
this.localVarTable = t;
41+
}
42+
43+
public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter) {
44+
int attrNameIndex = iter.nextU2ToInt();
45+
int attrLen = iter.nextU4ToInt();
46+
int maxStack = iter.nextU2ToInt();
47+
int maxLocals = iter.nextU2ToInt();
48+
int codeLen = iter.nextU4ToInt();
49+
50+
String code = iter.nextUxToHexString(codeLen);
51+
52+
System.out.println(code);
53+
54+
CodeAttr codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code);
55+
56+
int exceptionTableLen = iter.nextU2ToInt();
57+
58+
if (exceptionTableLen > 0) {
59+
String exTable = iter.nextUxToHexString(exceptionTableLen);
60+
System.out.println("Encountered exception table,just ignore it");
61+
}
62+
63+
int subAttrCount = iter.nextU2ToInt();
64+
65+
for (int i = 1; i <= subAttrCount; i++) {
66+
int subAttrIndex = iter.nextU2ToInt();
67+
String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex);
68+
69+
iter.back(2);
70+
71+
if (AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)) {
72+
LineNumberTable t = LineNumberTable.parse(iter);
73+
codeAttr.setLineNumberTable(t);
74+
} else if (AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)) {
75+
LocalVariableTable t = LocalVariableTable.parse(iter);
76+
codeAttr.setLocalVariableTable(t);
77+
} else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)) {
78+
StackMapTable t = StackMapTable.parse(iter);
79+
codeAttr.setStackMapTable(t);
80+
} else {
81+
throw new RuntimeException("Need code to process " + subAttrName);
82+
}
83+
}
84+
85+
return codeAttr;
86+
}
87+
88+
private void setStackMapTable(StackMapTable t) {
89+
this.stackMapTable = t;
90+
91+
}
92+
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package week06.jvm.attr;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import week06.jvm.loader.ByteCodeIterator;
7+
8+
public class LineNumberTable extends AttributeInfo {
9+
List<LineNumberItem> items = new ArrayList<LineNumberItem>();
10+
11+
private static class LineNumberItem{
12+
int startPC;
13+
int lineNum;
14+
public int getStartPC() {
15+
return startPC;
16+
}
17+
public void setStartPC(int startPC) {
18+
this.startPC = startPC;
19+
}
20+
public int getLineNum() {
21+
return lineNum;
22+
}
23+
public void setLineNum(int lineNum) {
24+
this.lineNum = lineNum;
25+
}
26+
}
27+
public void addLineNumberItem(LineNumberItem item){
28+
this.items.add(item);
29+
}
30+
public LineNumberTable(int attrNameIndex, int attrLen) {
31+
super(attrNameIndex, attrLen);
32+
33+
}
34+
35+
public static LineNumberTable parse(ByteCodeIterator iter){
36+
int index = iter.nextU2ToInt();
37+
int len = iter.nextU4ToInt();
38+
39+
LineNumberTable table = new LineNumberTable(index, len);
40+
41+
int itemLen = iter.nextU2ToInt();
42+
43+
for(int i=1;i<=itemLen;i++){
44+
LineNumberItem item = new LineNumberItem();
45+
item.setStartPC(iter.nextU2ToInt());
46+
item.setLineNum(iter.nextU2ToInt());
47+
table.addLineNumberItem(item);
48+
}
49+
return table;
50+
}
51+
52+
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package week06.jvm.attr;
2+
3+
public class LocalVariableItem {
4+
private int startPC;
5+
private int length;
6+
private int nameIndex;
7+
private int descIndex;
8+
private int index;
9+
public int getStartPC() {
10+
return startPC;
11+
}
12+
public void setStartPC(int startPC) {
13+
this.startPC = startPC;
14+
}
15+
public int getLength() {
16+
return length;
17+
}
18+
public void setLength(int length) {
19+
this.length = length;
20+
}
21+
public int getNameIndex() {
22+
return nameIndex;
23+
}
24+
public void setNameIndex(int nameIndex) {
25+
this.nameIndex = nameIndex;
26+
}
27+
public int getDescIndex() {
28+
return descIndex;
29+
}
30+
public void setDescIndex(int descIndex) {
31+
this.descIndex = descIndex;
32+
}
33+
public int getIndex() {
34+
return index;
35+
}
36+
public void setIndex(int index) {
37+
this.index = index;
38+
}
39+
}

0 commit comments

Comments
 (0)