-
Notifications
You must be signed in to change notification settings - Fork 6
/
Vars.java
199 lines (159 loc) · 4.49 KB
/
Vars.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
package agsecres.tool;
import java.lang.*;
import java.util.*;
public class Vars {
public static final int VAR_INT = 0x01;
public static final int VAR_STR = 0x02;
public static class Proto {
String name;
int type;
public Proto(String name, int type) {
this.name = name;
this.type = type;
}
public String name() {
return name;
}
public int type() {
return type;
}
}
public static class Var {
Proto proto;
Object val;
public Var(Proto proto, Object val) {
this.proto = proto;
this.val = val;
}
public Var(Proto proto) {
this(proto, null);
}
public Proto proto() {
return proto;
}
public Object val() {
return val;
}
public void set(Object val) {
this.val = val;
}
}
public static final int MAXVARNAME = 20;
public static final int UNKNOWN_VAL = -1;
static Vector<Var> vars;
static {
vars = new Vector<Var>();
}
public static Var get_var(String name) {
for (int i = 0; i < vars.size(); i++) {
Var var = vars.elementAt(i);
Proto proto = var.proto();
if (proto.name().equals(name)) return var;
}
return null;
}
static boolean known_var(String name) {
if (get_var(name) != null) return true;
else return false;
}
static int str2type(String s) {
if (s.equals("int")) return VAR_INT;
else
if (s.equals("str")) return VAR_STR;
else return -1;
}
public static void declare(String name, String stype) {
int type = -1;
if (known_var(name)) {
String s = "error: " + name + " is already defined";
Shell.println(s);
} else {
type = str2type(stype);
if (type == -1) {
String s = "error: unknown type " + stype;
Shell.println(s);
} else {
switch (type) {
case VAR_INT:
declare(new Proto(name, VAR_INT));
break;
case VAR_STR:
declare(new Proto(name, VAR_STR));
break;
}
}
}
}
public static void declare(Proto p) {
if (!known_var(p.name())) {
vars.addElement(new Var(p));
}
}
public static void set(String name, int val) {
if (!known_var(name)) {
declare(new Proto(name, VAR_INT));
}
Var v = get_var(name);
v.set(Integer.valueOf(val));
}
public static void set(String name, String val) {
if (!known_var(name)) {
declare(new Proto(name, VAR_STR));
}
Var v = get_var(name);
v.set(val);
}
public static void clear(String name) {
Var v = get_var(name);
if (v != null) {
v.set(null);
}
}
public static int get_int(Var
var) {
Integer ival = (Integer) var.val();
return ival.intValue();
}
public static String get_str(Var var) {
return (String) var.val();
}
public static int get_int(String name) {
Var var = get_var(name);
return get_int(var);
}
public static String get_str(String name) {
Var var = get_var(name);
return get_str(var);
}
public static void print() {
for (int i = 0; i < vars.size(); i++) {
Var
var = vars.elementAt(i);
Proto proto =
var.proto();
String p = proto.name();
String val = "";
if (var.val() != null) {
switch (proto.type()) {
case VAR_INT:
val += Utils.hex_value(get_int(var), 8);
break;
case VAR_STR:
val += "\"" + get_str(var) + "\"";
break;
}
} else {
val += "<not set>";
}
String s = Utils.padded_string("- " + p, MAXVARNAME) + " = " + val;
Shell.println(s);
}
}
}