-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIVariableRepresentation.java
More file actions
488 lines (420 loc) · 19 KB
/
UIVariableRepresentation.java
File metadata and controls
488 lines (420 loc) · 19 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import java.math.*;
import javafx.beans.binding.*;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.text.*;
import javafx.stage.*;
public class UIVariableRepresentation {
private static Canvas canvas, canvasBottom;
private static GraphicsContext gc, gcBottom;
public static double fontSize = 1.0;
private static VariableRepresentation representation;
public static Scene scene = null;
private static boolean updateInProgress;
private static Stage window;
private static ComboBox<String> cboTopEndianness;
private static ComboBox<String> cboTopType;
private static TextField txtBytesTop;
private static TextField txtValueTop;
private static ComboBox<String> cboBottomEndianness;
private static ComboBox<String> cboBottomType;
private static TextField txtBytesBottom;
private static TextField txtValueBottom;
private static final String BIG_ENDIAN = "Big Endian";
private static final String LITTLE_ENDIAN = "Little Endian";
public static void display(String typeIn, String value) {
updateInProgress = false;
window = new Stage();
window.setTitle("Integer Representations");
window.initModality(Modality.APPLICATION_MODAL);
VariableType type = VariableType.convertFromAnalysis(typeIn);
representation = new VariableRepresentation(true);
representation.setValueFromDecimal(value, type);
BorderPane container = new BorderPane();
GridPane grid = new GridPane();
grid.setHgap(8);
grid.setVgap(24);
grid.setPadding(new Insets(10, 10, 10, 10));
{
ColumnConstraints column = new ColumnConstraints();
column.setPercentWidth(25.0);
grid.getColumnConstraints().add(column);
}
{
ColumnConstraints column = new ColumnConstraints();
column.setPercentWidth(25.0);
grid.getColumnConstraints().add(column);
}
{
ColumnConstraints column = new ColumnConstraints();
column.setPercentWidth(50.0);
grid.getColumnConstraints().add(column);
}
// Doesn't work right. GridPane is just broken from what I can tell. So sick of this.
// for (int n = 0; n < 20; ++n) {
// RowConstraints row = new RowConstraints();
// row.setPercentHeight(5.0);
// grid.getRowConstraints().add(row);
// }
cboTopType = new ComboBox<>();
cboTopType.getItems().addAll("signed char", "unsigned char", "signed short", "unsigned short",
"signed int", "unsigned int", "signed long", "unsigned long");
cboTopType.getSelectionModel().select(getIndexFromType(type));
cboTopType.setOnAction(e -> {
if (!updateInProgress) {
updateInProgress = true;
VariableType newType = getTypeFromSelection(cboTopType.getValue());
representation.setValueFromHex(
representation.resizeHex(newType,
txtBytesTop.getText(),
txtValueTop.getText().charAt(0) != '-')
);
txtValueTop.setText(representation.convertHexToDecimal(representation.getValue(), newType));
updateInProgress = false;
updateUI();
}
});
grid.add(cboTopType, 0, 0, 1, 1);
cboTopEndianness = new ComboBox<>();
cboTopEndianness.getItems().addAll(BIG_ENDIAN, LITTLE_ENDIAN);
cboTopEndianness.getSelectionModel().select(0);
cboTopEndianness.setOnAction(e -> {
if (!updateInProgress) {
updateInProgress = true;
if (representation.isBigEndian != (cboTopEndianness.getValue().equals(BIG_ENDIAN)))
representation.reverseEndianness();
txtValueTop.setText(representation.convertHexToDecimal(representation.getValue(),
getTypeFromSelection(cboTopType.getValue())));
updateInProgress = false;
updateUI();
}
});
grid.add(cboTopEndianness, 1, 0, 1, 1);
txtValueTop = new TextField(value);
txtValueTop.textProperty().addListener((obs, oldValue, newValue) -> {
if (!updateInProgress) {
updateInProgress = true;
if (newValue.matches("-*\\d+")) {
newValue = representation.clampValue(getTypeFromSelection(cboTopType.getValue()), newValue);
String newHex = representation.convertDecimalToHex(newValue);
representation.setValueFromHex(
representation.resizeHex(getTypeFromSelection(cboTopType.getValue()),
newHex,
newValue.charAt(0) != '-')
);
txtValueTop.setText(newValue);
updateInProgress = false;
updateUI();
}
else txtValueTop.setText(oldValue);
updateInProgress = false;
}
});
grid.add(txtValueTop, 2, 0, 1, 1);
txtBytesTop = new TextField(representation.getValue(cboTopEndianness.getValue().equals(BIG_ENDIAN)));
grid.add(txtBytesTop, 0, 1, 3, 1);
Button btnDecrementValue = new Button("Decrement value (-)");
btnDecrementValue.setOnAction(e -> {
BigInteger biv = new BigInteger(txtValueTop.getText());
biv = biv.subtract(BigInteger.ONE);
biv = new BigInteger(representation.clampValue(getTypeFromSelection(cboTopType.getValue()), biv.toString()));
representation.setValueFromDecimal(biv.toString(), getTypeFromSelection(cboTopType.getValue()));
txtValueTop.setText(biv.toString());
updateUI();
});
grid.add(btnDecrementValue, 0, 2, 1, 1);
Button btnIncrementValue = new Button("Increment value (+)");
btnIncrementValue.setOnAction(e -> {
BigInteger biv = new BigInteger(txtValueTop.getText());
biv = biv.add(BigInteger.ONE);
biv = new BigInteger(representation.clampValue(getTypeFromSelection(cboTopType.getValue()), biv.toString()));
representation.setValueFromDecimal(biv.toString(), getTypeFromSelection(cboTopType.getValue()));
txtValueTop.setText(biv.toString());
updateUI();
});
grid.add(btnIncrementValue, 1, 2, 1, 1);
// Value lines
canvas = new Canvas(500, 80);
canvas.widthProperty().bind(window.widthProperty().multiply(0.9));
canvas.heightProperty().bind(window.heightProperty().divide(8));
gc = canvas.getGraphicsContext2D();
canvas.widthProperty().addListener(observable -> drawVisualization(
canvas.getWidth(), canvas.getHeight(), gc, txtBytesTop.getText(),
txtValueTop.getText(),
VariableType.toString(getTypeFromSelection(cboTopType.getValue()))));
canvas.heightProperty().addListener(observable -> drawVisualization(
canvas.getWidth(), canvas.getHeight(), gc, txtBytesTop.getText(),
txtValueTop.getText(),
VariableType.toString(getTypeFromSelection(cboTopType.getValue()))));
grid.add(canvas, 0, 3, 3, 1);
canvasBottom = new Canvas(500, 80);
canvasBottom.widthProperty().bind(window.widthProperty().multiply(0.9));
canvasBottom.heightProperty().bind(window.heightProperty().divide(8));
gcBottom = canvasBottom.getGraphicsContext2D();
canvasBottom.widthProperty().addListener(observable -> drawVisualization(
canvasBottom.getWidth(), canvasBottom.getHeight(), gcBottom, txtBytesBottom.getText(),
txtValueBottom.getText(),
VariableType.toString(getTypeFromSelection(cboBottomType.getValue()))));
canvasBottom.heightProperty().addListener(observable -> drawVisualization(
canvasBottom.getWidth()-10, canvasBottom.getHeight()-10, gcBottom, txtBytesBottom.getText(),
txtValueBottom.getText(),
VariableType.toString(getTypeFromSelection(cboBottomType.getValue()))));
grid.add(canvasBottom, 0, 4, 3, 1);
// Bottom part
Label lblInterpret = new Label("Interpret As:");
grid.add(lblInterpret, 0, 5, 1, 1);
cboBottomType = new ComboBox<>();
cboBottomType.getItems().addAll("signed char", "unsigned char", "signed short",
"unsigned short", "signed int", "unsigned int", "signed long", "unsigned long");
cboBottomType.getSelectionModel().select(getIndexFromType(type));
cboBottomType.setOnAction(e -> updateUI());
grid.add(cboBottomType, 0, 6, 1, 1);
cboBottomEndianness = new ComboBox<>();
cboBottomEndianness.getItems().addAll(BIG_ENDIAN, LITTLE_ENDIAN);
cboBottomEndianness.getSelectionModel().select(0);
cboBottomEndianness.setOnAction(e -> updateUI());
grid.add(cboBottomEndianness, 1, 6, 1, 1);
txtValueBottom = new TextField(value);
txtValueBottom.setEditable(false);
grid.add(txtValueBottom, 2, 6, 1, 1);
txtBytesBottom = new TextField(representation.getValue(cboTopEndianness.getValue().equals(BIG_ENDIAN)));
grid.add(txtBytesBottom, 0, 7, 3, 1);
// View menu
Menu viewMenu = new Menu("View");
MenuItem menuIncreaseFontSize = new MenuItem("Increase Font Size");
menuIncreaseFontSize.setOnAction(e -> {
if (fontSize < 5.0) fontSize += 0.1;
grid.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
});
menuIncreaseFontSize.setAccelerator(new KeyCodeCombination(KeyCode.EQUALS, KeyCombination.CONTROL_DOWN));
viewMenu.getItems().add(menuIncreaseFontSize);
MenuItem menuDecreaseFontSize = new MenuItem("Decrease Font Size");
menuDecreaseFontSize.setOnAction(e -> {
if (fontSize > 0.1) fontSize -= 0.1;
grid.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
});
menuDecreaseFontSize.setAccelerator(new KeyCodeCombination(KeyCode.MINUS, KeyCombination.CONTROL_DOWN));
viewMenu.getItems().add(menuDecreaseFontSize);
//Main menu bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(viewMenu);
container.setTop(menuBar);
container.setCenter(grid);
scene = new Scene(container, 800, 600);
// scene size change listeners
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
grid.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
}
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
grid.setStyle("-fx-font-size: " + UIUtils.calculateFontSize(fontSize, scene.getWidth(), scene.getHeight()));
}
});
window.setScene(scene);
window.showAndWait();
}
private static void updateUI() {
updateInProgress = true;
txtBytesTop.setText(representation.getValue());
// Figure out what bottom hex should be
String bottomHex = txtBytesTop.getText();
VariableType bottomType = getTypeFromSelection(cboBottomType.getValue());
bottomHex =
representation.resizeHex(bottomType,
bottomHex,
(txtValueTop.getText().charAt(0) != '-') ||
bottomType == VariableType.UNSIGNED_CHAR ||
bottomType == VariableType.UNSIGNED_INT ||
bottomType == VariableType.UNSIGNED_LONG ||
bottomType == VariableType.UNSIGNED_SHORT);
if (!cboBottomEndianness.getValue().equals(cboTopEndianness.getValue()))
bottomHex = representation.reverseBytes(bottomHex);
txtBytesBottom.setText(bottomHex);
txtValueBottom.setText(representation.convertHexToDecimal(
bottomHex, getTypeFromSelection(cboBottomType.getValue())));
drawVisualization(canvas.getWidth(),
canvas.getHeight(),
gc,
txtBytesTop.getText(),
txtValueTop.getText(),
VariableType.toString(getTypeFromSelection(cboTopType.getValue())));
drawVisualization(canvasBottom.getWidth(),
canvasBottom.getHeight(),
gcBottom,
txtBytesBottom.getText(),
txtValueBottom.getText(),
VariableType.toString(getTypeFromSelection(cboBottomType.getValue())));
updateInProgress = false;
}
private static int getIndexFromType(VariableType type) {
switch (type) {
case SIGNED_CHAR:
return 0;
case UNSIGNED_CHAR:
return 1;
case SIGNED_SHORT:
return 2;
case UNSIGNED_SHORT:
return 3;
case SIGNED_INT:
return 4;
case UNSIGNED_INT:
return 5;
case SIGNED_LONG:
return 6;
case UNSIGNED_LONG:
return 7;
default:
assert false;
return 8;
}
}
private static VariableType getTypeFromSelection(String selection) {
if (selection.equals("signed char")) return VariableType.SIGNED_CHAR;
else if (selection.equals("unsigned char")) return VariableType.UNSIGNED_CHAR;
else if (selection.equals("signed short")) return VariableType.SIGNED_SHORT;
else if (selection.equals("unsigned short")) return VariableType.UNSIGNED_SHORT;
else if (selection.equals("signed int")) return VariableType.SIGNED_INT;
else if (selection.equals("unsigned int")) return VariableType.UNSIGNED_INT;
else if (selection.equals("signed long")) return VariableType.SIGNED_LONG;
else if (selection.equals("unsigned long")) return VariableType.UNSIGNED_LONG;
assert false;
return VariableType.STRING;
}
private static String getMaxSizeByType(String type) {
if (type.equals("signed char")) return "0x7F=2^8";
else if (type.equals("unsigned char")) return "0xFF=2^8";
else if (type.equals("signed short")) return "0x7FFF=2^(16-1)-1";
else if (type.equals("unsigned short")) return "0xFFFF=2^16";
else if (type.equals("signed int")) return "0x7FFFFFFF=2^(32-1)-1";
else if (type.equals("unsigned int")) return "0xFFFFFFFF=2^32";
else if (type.equals("signed long")) {
if (UIUtils.architecture == 64) return "0x7FFFFFFFFFFFFFFF=2^(64-1)-1";
else if (UIUtils.architecture == 32) return "0x7FFFFFFF=2^(32-1)-1";
}
else if (type.equals("unsigned long")) {
if (UIUtils.architecture == 64) return "0xFFFFFFFFFFFFFFFF=2^64";
else if (UIUtils.architecture == 32) return "0xFFFFFFFF=2^32";
}
assert false;
return "Unknown";
}
private static String getMinSizeByType(String type) {
if (type.equals("signed char")) return "0x00";
else if (type.equals("unsigned char")) return "0x80=2^(8-1)";
else if (type.equals("signed short")) return "0x8000=-2^(16-1)";
else if (type.equals("unsigned short")) return "0x0000=0";
else if (type.equals("signed int")) return "0x80000000=-2^(32-1)";
else if (type.equals("unsigned int")) return "0x00000000=0";
else if (type.equals("signed long")) {
if (UIUtils.architecture == 64) return "0x8000000000000000=-2^(64-1)";
else if (UIUtils.architecture == 32) return "0x80000000=-2^(32-1)";
}
else if (type.equals("unsigned long")) {
if (UIUtils.architecture == 64) return "0x0000000000000000=0";
else if (UIUtils.architecture == 32) return "0x00000000=0";
}
assert false;
return "Unknown";
}
private static void drawVisualization(double width,
double height,
GraphicsContext gc,
String value,
String decValue,
String type)
{
BigDecimal min = BigDecimal.ZERO;
BigDecimal max = BigDecimal.ZERO;
// figure out where we are between min and max values
if (type.contains("unsigned")) {
min = new BigDecimal("0");
if (type.contains("char")) max = new BigDecimal("255");
else if (type.contains("short")) max = new BigDecimal("65535");
else if (type.contains("int")) max = new BigDecimal("4294967295");
else if (type.contains("long")) {
if (UIUtils.architecture == 64) max = new BigDecimal("18446744073709551615");
else if (UIUtils.architecture == 32) max = new BigDecimal("4294967295");
}
}
else {
if (type.contains("char")) {
min = new BigDecimal("-128");
max = new BigDecimal("127");
}
else if (type.contains("short")) {
min = new BigDecimal("-32768");
max = new BigDecimal("32767");
}
else if (type.contains("int")) {
min = new BigDecimal("-2147483648");
max = new BigDecimal("2147483647");
}
else if (type.contains("long")) {
if (UIUtils.architecture == 64) {
min = new BigDecimal("-9223372036854775808");
max = new BigDecimal("9223372036854775807");
}
else if (UIUtils.architecture == 32) {
min = new BigDecimal("-2147483648");
max = new BigDecimal("2147483647");
}
}
}
BigDecimal val = new BigDecimal(decValue);
min = min.abs();
max = max.add(min);
val = val.add(min);
BigDecimal propIntermediate = val.divide(max, 5, RoundingMode.HALF_UP);
double proportion = propIntermediate.doubleValue();
// draw
gc.clearRect(0, 0, width, height);
gc.setLineWidth(5);
gc.setFont(Font.font("Sans", FontWeight.BOLD, 20));
gc.setTextAlign(TextAlignment.LEFT);
/* if (!type.contains("unsigned")) {
gc.setFill(Color.rgb(255, 85, 0));
gc.fillText("Negative", 5, 15);
gc.setFill(Color.rgb(30, 180, 30));
gc.fillText("Positive", 5, 35);
}
gc.setFill(Color.BLUE);
gc.fillText("Current value", 5, 55); /* */
if (!type.contains("unsigned")) gc.setStroke(Color.rgb(235, 85, 0));
else gc.setStroke(Color.rgb(30, 180, 30));
gc.strokeLine(0, height/2.0, width/2.0, height/2.0);
gc.setStroke(Color.rgb(30, 180, 30));
gc.strokeLine(width/2.0, height/2.0, width, height/2.0);
gc.setStroke(Color.BLUE);
gc.setFill(Color.BLUE);
gc.setTextAlign(TextAlignment.CENTER);
if (proportion > 0.9) gc.setTextAlign(TextAlignment.RIGHT);
else if (proportion < 0.1) gc.setTextAlign(TextAlignment.LEFT);
gc.fillText("0x" + value, width*proportion, height/2.0-10);
gc.setStroke(Color.rgb(255, 85, 0));
gc.setFill(Color.rgb(255, 85, 0));
gc.setTextAlign(TextAlignment.LEFT);
gc.fillText(getMinSizeByType(type), 0, height/2.0+20);
gc.setTextAlign(TextAlignment.RIGHT);
if (!type.contains("unsigned")) gc.fillText("-1", width/2.0-10, height/2.0+20);
gc.setStroke(Color.rgb(30, 180, 30));
gc.setFill(Color.rgb(30, 180, 30));
gc.setTextAlign(TextAlignment.RIGHT);
gc.fillText(getMaxSizeByType(type), width, height/2.0+20);
gc.setTextAlign(TextAlignment.LEFT);
if (!type.contains("unsigned")) gc.fillText("0", width/2.0+10, height/2.0+20);
gc.setFill(Color.BLUE);
gc.setStroke(Color.BLUE);
gc.strokeLine(width*proportion-10, height/2.0, width*proportion+10, height/2.0);
}
}