scientific-method-engine 13.7.0__tar.gz → 14.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/PKG-INFO +3 -3
  2. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/README.md +2 -2
  3. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/pyproject.toml +1 -1
  4. scientific_method_engine-14.0.0/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +400 -0
  5. scientific_method_engine-14.0.0/src/scientific_method_engine/ghidra/scientificmethod/Xxh3.java +189 -0
  6. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/reports.py +33 -16
  7. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/trace.py +37 -13
  8. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_successor_rule.py +27 -6
  9. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_x86.py +100 -0
  10. scientific_method_engine-13.7.0/src/scientific_method_engine/ghidra/ExportFunctionInventory.java +0 -50
  11. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/.gitignore +0 -0
  12. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/LICENSE +0 -0
  13. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/__init__.py +0 -0
  14. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/__main__.py +0 -0
  15. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/cli.py +0 -0
  16. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ClearNoReturnFunctions.java +0 -0
  17. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/CreateFunctions.java +0 -0
  18. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ExportBoundedFlow.java +0 -0
  19. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ExportCallEdges.java +0 -0
  20. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ExportFunctionFingerprints.java +0 -0
  21. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/MergeFallThroughFragment.java +0 -0
  22. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/RecoverCitedFunctions.java +0 -0
  23. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/RepairReturningCallers.java +0 -0
  24. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportCallArguments.java +0 -0
  25. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportCallPaths.java +0 -0
  26. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportCallSitesWithScalars.java +0 -0
  27. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportCallsToRange.java +0 -0
  28. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportConstantFirstArgumentCalls.java +0 -0
  29. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportDataBytes.java +0 -0
  30. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportDecompileMatches.java +0 -0
  31. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportDecompileWindow.java +0 -0
  32. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportFilePatternInMemory.java +0 -0
  33. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportFirstArgumentCallSummary.java +0 -0
  34. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportFunctionScalarConstants.java +0 -0
  35. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportFunctionSummary.java +0 -0
  36. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportInstructionContext.java +0 -0
  37. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportInstructionWindow.java +0 -0
  38. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlockForFileOffset.java +0 -0
  39. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportMemoryBlocks.java +0 -0
  40. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportRandomnessCandidates.java +0 -0
  41. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportRangeBoundaries.java +0 -0
  42. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportReferences.java +0 -0
  43. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportScalarConstants.java +0 -0
  44. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportStringReferences.java +0 -0
  45. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/ReportSymbolReferences.java +0 -0
  46. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/scientificmethod/FirstArgumentLookBack.java +0 -0
  47. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/scientificmethod/InstructionStart.java +0 -0
  48. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/ghidra/scientificmethod/OperandConstants.java +0 -0
  49. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/__init__.py +0 -0
  50. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/argument_frames.py +0 -0
  51. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/dispatch.py +0 -0
  52. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/effect_order.py +0 -0
  53. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/image.py +0 -0
  54. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/inventory.py +0 -0
  55. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/loops.py +0 -0
  56. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/machine.py +0 -0
  57. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/memory_scopes.py +0 -0
  58. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/pcode.py +0 -0
  59. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/pcode_backend.py +0 -0
  60. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/pe.py +0 -0
  61. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/reach.py +0 -0
  62. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/relational.py +0 -0
  63. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/result_flow.py +0 -0
  64. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/src/scientific_method_engine/x86/values.py +0 -0
  65. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/oracle.py +0 -0
  66. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_continuation_budget.py +0 -0
  67. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_dispatch.py +0 -0
  68. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_effect_order.py +0 -0
  69. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_enter_frames.py +0 -0
  70. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_entry_frame.py +0 -0
  71. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_indirect_far_transfers.py +0 -0
  72. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_interrupt_models.py +0 -0
  73. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_inventory_check.py +0 -0
  74. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_loops.py +0 -0
  75. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_memory_scopes.py +0 -0
  76. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_nested_frame_request.py +0 -0
  77. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_oracle.py +0 -0
  78. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_pe.py +0 -0
  79. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_reach.py +0 -0
  80. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_relational_controls.py +0 -0
  81. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_return_checks.py +0 -0
  82. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_table_continuations.py +0 -0
  83. {scientific_method_engine-13.7.0 → scientific_method_engine-14.0.0}/tests/test_term_limit.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: scientific-method-engine
3
- Version: 13.7.0
3
+ Version: 14.0.0
4
4
  Summary: Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code.
5
5
  Project-URL: Source, https://github.com/kibertoad/refurbished-dinosaurs-toolkit/tree/main/packages/scientific-method-engine
6
6
  Author: kibertoad
@@ -53,7 +53,7 @@ for the other report scripts, a result count equal to the cap means the same.
53
53
  script's own result lines ([the Ghidra workflow](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md)
54
54
  says what a run must show). The scripts compile against Ghidra 12.1. `ReportConstantFirstArgumentCalls`,
55
55
  `ReportFirstArgumentCallSummary`, `ReportCallSitesWithScalars`, `ReportScalarConstants`,
56
- `ReportFunctionScalarConstants`, `ReportInstructionWindow` and `ExportBoundedFlow` load shared helpers from the `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
56
+ `ReportFunctionScalarConstants`, `ReportInstructionWindow`, `ExportBoundedFlow` and `ExportFunctionInventory` load shared helpers from the `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
57
57
  or copy that subdirectory along with the scripts.
58
58
 
59
59
  Reading code and data:
@@ -93,7 +93,7 @@ Exporting for comparison (each writes one file and refuses to overwrite where no
93
93
  | Script | Arguments | Writes |
94
94
  |---|---|---|
95
95
  | `ExportBoundedFlow` | entry where an instruction starts, instruction limit (1..10000, the most instruction records exported), output path under `analysis/original/` | instruction metadata of one bounded flow as JSON, with `limitReached` (the walk stopped at the limit with flow left unread) and `noInstruction` (flow targets where no instruction starts). An entry where no instruction starts fails the script and writes nothing |
96
- | `ExportFunctionInventory` | output TSV path (must not exist) | every function's start and body size, the inventory `inventory-check` compares call targets with |
96
+ | `ExportFunctionInventory` | output path `coverage/<build>/<file>.tsv` (must not exist, nor its `.provenance.tsv`), identifier of the database snapshot the export reads | the function inventory that `standard-coverage` and `inventory-check` read: each function's start in the Standard's notation, its body size and, for a body that is not one range from the start, its ranges; and beside it a `.provenance.tsv` with the imported file's `xxh3` and SHA-256, the Ghidra version, the snapshot and the SHA-256 of the script and of its Xxh3 helper. A body byte it cannot place in the notation fails the export, which then writes nothing ([Exporting a function inventory](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md#exporting-a-function-inventory)) |
97
97
  | `ExportCallEdges` | output JSON path (must not exist), function limit (1..128), one or more function entries | the call and tail-jump edges of the functions Ghidra reaches breadth-first from the entries, with file offsets, Ghidra's flow type (after any flow override), `fallsThrough` (whether Ghidra continues to the next instruction at the site, after any fall-through override) and `fallsThroughTo`/`fallsThroughToAddress` (where a fall-through override sends Ghidra instead, or null), as the `ghidraCallEdges` input of `callees` |
98
98
  | `ExportFunctionFingerprints` | output TSV path (replaced only when the export completes) | per-function and per-instruction fingerprints with addresses normalized, for matching functions across versions |
99
99
 
@@ -37,7 +37,7 @@ for the other report scripts, a result count equal to the cap means the same.
37
37
  script's own result lines ([the Ghidra workflow](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md)
38
38
  says what a run must show). The scripts compile against Ghidra 12.1. `ReportConstantFirstArgumentCalls`,
39
39
  `ReportFirstArgumentCallSummary`, `ReportCallSitesWithScalars`, `ReportScalarConstants`,
40
- `ReportFunctionScalarConstants`, `ReportInstructionWindow` and `ExportBoundedFlow` load shared helpers from the `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
40
+ `ReportFunctionScalarConstants`, `ReportInstructionWindow`, `ExportBoundedFlow` and `ExportFunctionInventory` load shared helpers from the `scientificmethod/` directory beside them, so pass the directory the command prints as `-scriptPath`,
41
41
  or copy that subdirectory along with the scripts.
42
42
 
43
43
  Reading code and data:
@@ -77,7 +77,7 @@ Exporting for comparison (each writes one file and refuses to overwrite where no
77
77
  | Script | Arguments | Writes |
78
78
  |---|---|---|
79
79
  | `ExportBoundedFlow` | entry where an instruction starts, instruction limit (1..10000, the most instruction records exported), output path under `analysis/original/` | instruction metadata of one bounded flow as JSON, with `limitReached` (the walk stopped at the limit with flow left unread) and `noInstruction` (flow targets where no instruction starts). An entry where no instruction starts fails the script and writes nothing |
80
- | `ExportFunctionInventory` | output TSV path (must not exist) | every function's start and body size, the inventory `inventory-check` compares call targets with |
80
+ | `ExportFunctionInventory` | output path `coverage/<build>/<file>.tsv` (must not exist, nor its `.provenance.tsv`), identifier of the database snapshot the export reads | the function inventory that `standard-coverage` and `inventory-check` read: each function's start in the Standard's notation, its body size and, for a body that is not one range from the start, its ranges; and beside it a `.provenance.tsv` with the imported file's `xxh3` and SHA-256, the Ghidra version, the snapshot and the SHA-256 of the script and of its Xxh3 helper. A body byte it cannot place in the notation fails the export, which then writes nothing ([Exporting a function inventory](https://github.com/kibertoad/refurbished-dinosaurs-toolkit/blob/main/docs/ghidra-workflow.md#exporting-a-function-inventory)) |
81
81
  | `ExportCallEdges` | output JSON path (must not exist), function limit (1..128), one or more function entries | the call and tail-jump edges of the functions Ghidra reaches breadth-first from the entries, with file offsets, Ghidra's flow type (after any flow override), `fallsThrough` (whether Ghidra continues to the next instruction at the site, after any fall-through override) and `fallsThroughTo`/`fallsThroughToAddress` (where a fall-through override sends Ghidra instead, or null), as the `ghidraCallEdges` input of `callees` |
82
82
  | `ExportFunctionFingerprints` | output TSV path (replaced only when the export completes) | per-function and per-instruction fingerprints with addresses normalized, for matching functions across versions |
83
83
 
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
  [project]
6
6
  name = "scientific-method-engine"
7
7
  # The release workflow writes the published version from the package's release tag.
8
- version = "13.7.0"
8
+ version = "14.0.0"
9
9
  description = "Bounded instruction-derived x86 evidence reports for segmented MZ/FBOV and PE32/i386 code."
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.12"
@@ -0,0 +1,400 @@
1
+ // Exports every function's start, size and body ranges in the Standard's notation, with a provenance file.
2
+ // @category Restoration
3
+
4
+ import java.io.BufferedWriter;
5
+ import java.io.InputStream;
6
+ import java.nio.charset.StandardCharsets;
7
+ import java.nio.file.Files;
8
+ import java.nio.file.Path;
9
+ import java.security.MessageDigest;
10
+ import java.util.ArrayList;
11
+ import java.util.HexFormat;
12
+ import java.util.LinkedHashMap;
13
+ import java.util.List;
14
+ import java.util.Map;
15
+
16
+ import generic.jar.ResourceFile;
17
+ import ghidra.app.script.GhidraScript;
18
+ import ghidra.framework.Application;
19
+ import ghidra.program.database.mem.FileBytes;
20
+ import ghidra.program.model.address.Address;
21
+ import ghidra.program.model.address.AddressRange;
22
+ import ghidra.program.model.address.AddressSet;
23
+ import ghidra.program.model.address.AddressSetView;
24
+ import ghidra.program.model.address.AddressSpace;
25
+ import ghidra.program.model.address.SegmentedAddress;
26
+ import ghidra.program.model.address.SegmentedAddressSpace;
27
+ import ghidra.program.model.listing.Function;
28
+ import ghidra.program.model.listing.FunctionIterator;
29
+ import ghidra.program.model.mem.Memory;
30
+ import ghidra.program.model.mem.MemoryBlock;
31
+ import ghidra.program.model.mem.MemoryBlockSourceInfo;
32
+ import scientificmethod.Xxh3;
33
+
34
+ public class ExportFunctionInventory extends GhidraScript {
35
+ private static final long MAX_FILE = 256L * 1024 * 1024;
36
+ private static final int MAX_PROBLEM_LINES = 1000;
37
+
38
+ private enum Notation { SEGMENTED, FLAT32, FLAT64 }
39
+
40
+ /**
41
+ * A placed stretch of a body: a half-open range of linear addresses or of file offsets, with the
42
+ * segment its start is written in (0 outside the segmented notation).
43
+ */
44
+ private record Piece(boolean offset, long start, long end, String startText, long segment) {
45
+ }
46
+
47
+ private Notation notation;
48
+ /** Whether the Standard lets this program's overlay code be written by file offset: only an MZ file. */
49
+ private boolean fileOffsets;
50
+ private FileBytes imported;
51
+ private final List<String> problems = new ArrayList<>();
52
+
53
+ @Override
54
+ protected void run() throws Exception {
55
+ String[] arguments = getScriptArgs();
56
+ if (arguments.length != 2) {
57
+ throw new IllegalArgumentException(
58
+ "Supply the output inventory path, ending in .tsv, and the identifier of the database snapshot it reads.");
59
+ }
60
+ Path output = Path.of(arguments[0]).toAbsolutePath().normalize();
61
+ String name = output.getFileName().toString();
62
+ if (!name.endsWith(".tsv") || name.endsWith(".provenance.tsv") || name.endsWith(".regions.tsv")) {
63
+ throw new IllegalArgumentException("The inventory path must end in .tsv and not be a side file: " + output);
64
+ }
65
+ Path provenance = output.resolveSibling(name.substring(0, name.length() - 4) + ".provenance.tsv");
66
+ String snapshot = arguments[1];
67
+ if (snapshot.isEmpty() || snapshot.length() > 200 || snapshot.chars().anyMatch(Character::isISOControl)) {
68
+ throw new IllegalArgumentException("The snapshot identifier must be 1 to 200 characters with no tab or line break.");
69
+ }
70
+ if (Files.exists(output)) {
71
+ throw new IllegalArgumentException("Output already exists: " + output);
72
+ }
73
+ if (Files.exists(provenance)) {
74
+ // The provenance moves into place first, so one without its inventory is left by an export
75
+ // that stopped between the two moves. It is not deleted here, since it may be the user's own.
76
+ throw new IllegalArgumentException("Output already exists: " + provenance + ". With no inventory beside"
77
+ + " it, an earlier export stopped before moving the inventory into place; remove it and export again.");
78
+ }
79
+
80
+ notation = notation();
81
+ Map<String, String> source = provenance(snapshot);
82
+
83
+ // Every row is built before anything is written, so a function that cannot be placed refuses
84
+ // the whole export and no inventory with a smaller denominator appears.
85
+ Memory memory = currentProgram.getMemory();
86
+ List<String> rows = new ArrayList<>();
87
+ Map<String, Address> starts = new LinkedHashMap<>();
88
+ long bytes = 0;
89
+ long withRanges = 0;
90
+ FunctionIterator functions = currentProgram.getFunctionManager().getFunctions(true);
91
+ while (functions.hasNext()) {
92
+ if (monitor.isCancelled()) {
93
+ throw new InterruptedException("Function inventory export cancelled.");
94
+ }
95
+ Function function = functions.next();
96
+ Address entry = function.getEntryPoint();
97
+ List<Piece> pieces = place(memory, entry, function.getBody(), true);
98
+ Piece first = place(memory, entry, new AddressSet(entry), false).stream().findFirst().orElse(null);
99
+ if (first == null || pieces.isEmpty()) {
100
+ continue; // The body's problems include the byte at the start, or every byte of the body.
101
+ }
102
+ // Ghidra keeps a body in one address space, and a space is an overlay or not as a whole, so
103
+ // every piece is placed the way the start is. A 64-bit address is compared unsigned.
104
+ pieces.sort((a, b) -> Long.compareUnsigned(a.start(), b.start()));
105
+ List<Piece> body = new ArrayList<>();
106
+ for (Piece piece : pieces) {
107
+ Piece last = body.isEmpty() ? null : body.get(body.size() - 1);
108
+ if (last != null && Long.compareUnsigned(piece.start(), last.end()) < 0) {
109
+ problems.add("Function at " + entry + ": body bytes at " + piece.startText()
110
+ + " are placed where another part of the body already is, as when an overlay block views"
111
+ + " the same file bytes twice.");
112
+ } else if (last != null && last.end() == piece.start()) {
113
+ body.set(body.size() - 1,
114
+ new Piece(last.offset(), last.start(), piece.end(), last.startText(), last.segment()));
115
+ } else {
116
+ body.add(piece);
117
+ }
118
+ }
119
+ long size = body.stream().mapToLong(p -> p.end() - p.start()).sum();
120
+ Address earlier = starts.putIfAbsent(first.startText(), entry);
121
+ if (earlier != null) {
122
+ problems.add("Function at " + entry + " starts at " + first.startText()
123
+ + ", which the function at " + earlier + " already starts at.");
124
+ }
125
+ String ranges = "";
126
+ if (body.size() > 1 || body.get(0).start() != first.start()) {
127
+ List<String> written = new ArrayList<>();
128
+ for (Piece piece : body) {
129
+ written.add(piece.startText() + ".." + endText(piece));
130
+ }
131
+ ranges = String.join(" ", written);
132
+ withRanges++;
133
+ }
134
+ rows.add(first.startText() + "\t" + size + "\t" + ranges);
135
+ bytes += size;
136
+ }
137
+
138
+ if (!problems.isEmpty()) {
139
+ for (String line : problems.subList(0, Math.min(problems.size(), MAX_PROBLEM_LINES))) {
140
+ printerr(line);
141
+ }
142
+ if (problems.size() > MAX_PROBLEM_LINES) {
143
+ printerr("Output stopped after " + MAX_PROBLEM_LINES + " of " + problems.size() + " problems.");
144
+ }
145
+ throw new IllegalStateException((problems.size() == 1 ? "1 problem keeps" : problems.size() + " problems keep")
146
+ + " the inventory from listing every function's body in the Standard's notation. Nothing was written.");
147
+ }
148
+
149
+ Files.createDirectories(output.getParent());
150
+ Path inventoryPart = null;
151
+ Path provenancePart = null;
152
+ try {
153
+ inventoryPart = Files.createTempFile(output.getParent(), "inventory-", ".partial");
154
+ provenancePart = Files.createTempFile(output.getParent(), "provenance-", ".partial");
155
+ try (BufferedWriter writer = Files.newBufferedWriter(inventoryPart, StandardCharsets.UTF_8)) {
156
+ writer.write("start\tsize\tranges\n");
157
+ for (String row : rows) {
158
+ writer.write(row);
159
+ writer.write('\n');
160
+ }
161
+ }
162
+ try (BufferedWriter writer = Files.newBufferedWriter(provenancePart, StandardCharsets.UTF_8)) {
163
+ for (Map.Entry<String, String> line : source.entrySet()) {
164
+ writer.write(line.getKey() + "\t" + line.getValue() + "\n");
165
+ }
166
+ }
167
+ // No replacement. The inventory moves last, so a run that stops before it leaves no
168
+ // inventory, and a provenance file without its inventory is removed.
169
+ Files.move(provenancePart, provenance);
170
+ try {
171
+ Files.move(inventoryPart, output);
172
+ } catch (Exception e) {
173
+ Files.deleteIfExists(provenance);
174
+ throw e;
175
+ }
176
+ } finally {
177
+ if (inventoryPart != null) {
178
+ Files.deleteIfExists(inventoryPart);
179
+ }
180
+ if (provenancePart != null) {
181
+ Files.deleteIfExists(provenancePart);
182
+ }
183
+ }
184
+ println("Exported " + rows.size() + " functions with " + bytes + " body bytes, " + withRanges
185
+ + " of them with a ranges column, to " + output + ", and their provenance to " + provenance);
186
+ }
187
+
188
+ /** The Standard's address notation for this program, or an exception naming why it has none here. */
189
+ private Notation notation() {
190
+ AddressSpace space = currentProgram.getAddressFactory().getDefaultAddressSpace();
191
+ String format = currentProgram.getExecutableFormat();
192
+ if (space instanceof SegmentedAddressSpace) {
193
+ if (format != null && format.contains("(NE)")) {
194
+ throw new IllegalStateException("This is an NE program. Ghidra places NE segments at paragraphs of"
195
+ + " its own choosing, and this script does not convert them to the Standard's NE segments.");
196
+ }
197
+ fileOffsets = format != null && format.contains("(MZ)");
198
+ return Notation.SEGMENTED;
199
+ }
200
+ if (space.getSize() == 32) {
201
+ return Notation.FLAT32;
202
+ }
203
+ if (space.getSize() == 64 && format != null && format.contains("(ELF)")) {
204
+ return Notation.FLAT64;
205
+ }
206
+ throw new IllegalStateException("The Standard has no notation for a " + space.getSize()
207
+ + "-bit address space in a " + format + " program.");
208
+ }
209
+
210
+ /**
211
+ * The provenance lines: the imported file's xxh3 and SHA-256, the analyzer and its version, the
212
+ * snapshot the researcher names, and the SHA-256 of this script and of its Xxh3 helper. The file's bytes are those the program
213
+ * holds, checked against the SHA-256 Ghidra recorded when it imported the file.
214
+ */
215
+ private Map<String, String> provenance(String snapshot) throws Exception {
216
+ String recorded = currentProgram.getExecutableSHA256();
217
+ if (recorded == null || recorded.isEmpty()) {
218
+ throw new IllegalStateException("The program has no recorded SHA-256 of the file it was imported from.");
219
+ }
220
+ byte[] file = null;
221
+ for (FileBytes candidate : currentProgram.getMemory().getAllFileBytes()) {
222
+ if (candidate.getSize() > MAX_FILE) {
223
+ continue;
224
+ }
225
+ byte[] content = new byte[(int) candidate.getSize()];
226
+ if (candidate.getOriginalBytes(0, content) == content.length
227
+ && sha256(content).equalsIgnoreCase(recorded)) {
228
+ imported = candidate;
229
+ file = content;
230
+ break;
231
+ }
232
+ }
233
+ if (file == null) {
234
+ throw new IllegalStateException("No file bytes the program holds, of at most 256 MiB, match the SHA-256 "
235
+ + recorded + " recorded for the imported file, so the file cannot be identified.");
236
+ }
237
+ ResourceFile script = getSourceFile();
238
+ if (script == null || !script.exists()) {
239
+ throw new IllegalStateException("This script cannot read its own source to record its revision.");
240
+ }
241
+ byte[] scriptBytes = readAll(script);
242
+ // The xxh3 value comes from the helper, so its revision is recorded as well.
243
+ ResourceFile helper = new ResourceFile(new ResourceFile(script.getParentFile(), "scientificmethod"), "Xxh3.java");
244
+ if (!helper.exists()) {
245
+ throw new IllegalStateException("This script cannot read the source of its helper " + helper
246
+ + " to record its revision.");
247
+ }
248
+ byte[] helperBytes = readAll(helper);
249
+ Map<String, String> lines = new LinkedHashMap<>();
250
+ lines.put("xxh3", Xxh3.hexDigest(file));
251
+ lines.put("sha256", sha256(file));
252
+ lines.put("tool", "Ghidra");
253
+ lines.put("tool_version", Application.getApplicationVersion());
254
+ lines.put("snapshot", snapshot);
255
+ lines.put("script", "ExportFunctionInventory");
256
+ lines.put("script_sha256", sha256(scriptBytes));
257
+ lines.put("helper", "scientificmethod/Xxh3.java");
258
+ lines.put("helper_sha256", sha256(helperBytes));
259
+ return lines;
260
+ }
261
+
262
+ private static byte[] readAll(ResourceFile file) throws Exception {
263
+ try (InputStream in = file.getInputStream()) {
264
+ return in.readAllBytes();
265
+ }
266
+ }
267
+
268
+ /**
269
+ * The body split into placed pieces. A byte in an overlay block is placed by its offset in the
270
+ * imported file, which the Standard allows only in an MZ file; any other byte by its address. With
271
+ * record, a byte that cannot be placed is recorded as a problem with the reason.
272
+ */
273
+ private List<Piece> place(Memory memory, Address entry, AddressSetView body, boolean record) {
274
+ List<Piece> pieces = new ArrayList<>();
275
+ int before = problems.size();
276
+ AddressSet left = new AddressSet(body);
277
+ for (MemoryBlock block : memory.getBlocks()) {
278
+ if (left.isEmpty()) {
279
+ break;
280
+ }
281
+ AddressSet part = left.intersectRange(block.getStart(), block.getEnd());
282
+ if (part.isEmpty()) {
283
+ continue;
284
+ }
285
+ left.delete(part);
286
+ if (!block.getStart().getAddressSpace().isOverlaySpace()) {
287
+ for (AddressRange range : part) {
288
+ Piece piece = addressPiece(range);
289
+ if (piece == null) {
290
+ problem(entry, range, noWrittenForm(range));
291
+ } else {
292
+ pieces.add(piece);
293
+ }
294
+ }
295
+ continue;
296
+ }
297
+ if (!fileOffsets) {
298
+ for (AddressRange range : part) {
299
+ problem(entry, range, "is in overlay block " + block.getName()
300
+ + ", and the Standard places code by file offset only in an MZ file");
301
+ }
302
+ continue;
303
+ }
304
+ for (MemoryBlockSourceInfo info : block.getSourceInfos()) {
305
+ AddressSet sourced = part.intersectRange(info.getMinAddress(), info.getMaxAddress());
306
+ part.delete(sourced);
307
+ for (AddressRange range : sourced) {
308
+ FileBytes from = info.getFileBytes().orElse(null);
309
+ if (from == null || !from.equals(imported)) {
310
+ problem(entry, range, "is in overlay block " + block.getName() + ", whose bytes there come "
311
+ + (from == null ? "from no file" : "from " + from.getFilename() + ", not the imported file"));
312
+ continue;
313
+ }
314
+ long offset = info.getFileBytesOffset(range.getMinAddress());
315
+ pieces.add(new Piece(true, offset, offset + range.getLength(), String.format("0x%02X", offset), 0));
316
+ }
317
+ }
318
+ for (AddressRange range : part) {
319
+ problem(entry, range, "is in overlay block " + block.getName() + ", which gives it no source");
320
+ }
321
+ }
322
+ for (AddressRange range : left) {
323
+ problem(entry, range, "is in no memory block");
324
+ }
325
+ if (!record) {
326
+ problems.subList(before, problems.size()).clear();
327
+ }
328
+ return pieces;
329
+ }
330
+
331
+ /**
332
+ * The piece an address range in a non-overlay block makes, or null when it is outside the program's
333
+ * default address space or its start or end has no written form.
334
+ */
335
+ private Piece addressPiece(AddressRange range) {
336
+ Address start = range.getMinAddress();
337
+ if (!start.getAddressSpace().equals(currentProgram.getAddressFactory().getDefaultAddressSpace())) {
338
+ return null;
339
+ }
340
+ long linear = start.getOffset();
341
+ String text;
342
+ long segment = 0;
343
+ if (notation == Notation.SEGMENTED) {
344
+ if (!(start instanceof SegmentedAddress segmented)) {
345
+ return null;
346
+ }
347
+ segment = segmented.getSegment();
348
+ text = String.format("%04X:%04X", segment, segmented.getSegmentOffset());
349
+ } else {
350
+ text = String.format(notation == Notation.FLAT32 ? "0x%08X" : "0x%016X", linear);
351
+ }
352
+ Piece piece = new Piece(false, linear, linear + range.getLength(), text, segment);
353
+ return endText(piece) == null ? null : piece;
354
+ }
355
+
356
+ /** Why addressPiece has no piece for a range. */
357
+ private String noWrittenForm(AddressRange range) {
358
+ AddressSpace space = range.getMinAddress().getAddressSpace();
359
+ if (!space.equals(currentProgram.getAddressFactory().getDefaultAddressSpace())) {
360
+ return "is in address space " + space.getName() + ", which the Standard's notation does not cover";
361
+ }
362
+ if (notation == Notation.SEGMENTED) {
363
+ return "cannot be written in four hex digits of segment and offset";
364
+ }
365
+ return "ends past the highest address " + (notation == Notation.FLAT32 ? "8" : "16") + " hex digits can write";
366
+ }
367
+
368
+ /**
369
+ * The exclusive end of a piece. A segmented end keeps the start's segment while its offset fits
370
+ * in four hex digits and moves to the lowest segment that holds it otherwise; null past FFFF:FFFF.
371
+ */
372
+ private String endText(Piece piece) {
373
+ if (piece.offset()) {
374
+ return String.format("0x%02X", piece.end());
375
+ }
376
+ if (notation == Notation.FLAT32) {
377
+ return piece.end() >= 0x1_0000_0000L ? null : String.format("0x%08X", piece.end());
378
+ }
379
+ if (notation == Notation.FLAT64) {
380
+ // A body that ends at the top of the space ends at 2^64, which wraps to 0.
381
+ return Long.compareUnsigned(piece.end(), piece.start()) <= 0 ? null : String.format("0x%016X", piece.end());
382
+ }
383
+ long segment = piece.segment();
384
+ long offset = piece.end() - segment * 16;
385
+ if (offset > 0xFFFF) {
386
+ segment = (piece.end() - 0xFFFF + 15) / 16;
387
+ offset = piece.end() - segment * 16;
388
+ }
389
+ return segment > 0xFFFF ? null : String.format("%04X:%04X", segment, offset);
390
+ }
391
+
392
+ private void problem(Address entry, AddressRange range, String why) {
393
+ problems.add("Function at " + entry + ": body " + range.getMinAddress() + ".." + range.getMaxAddress()
394
+ + " (" + range.getLength() + " bytes) " + why + ".");
395
+ }
396
+
397
+ private static String sha256(byte[] bytes) throws Exception {
398
+ return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
399
+ }
400
+ }
@@ -0,0 +1,189 @@
1
+ package scientificmethod;
2
+
3
+ /**
4
+ * The 128-bit XXH3 hash with seed 0 and the default secret: the xxh3 the Standard records for a file
5
+ * and the engine compares a source with. It follows the reference algorithm of xxHash 0.8 and
6
+ * returns the 32 lower-case hex digits that xxhash's xxh3_128_hexdigest and xxhsum -H2 print.
7
+ */
8
+ public final class Xxh3 {
9
+ private static final long PRIME32_1 = 0x9E3779B1L;
10
+ private static final long PRIME32_2 = 0x85EBCA77L;
11
+ private static final long PRIME32_3 = 0xC2B2AE3DL;
12
+ private static final long PRIME64_1 = 0x9E3779B185EBCA87L;
13
+ private static final long PRIME64_2 = 0xC2B2AE3D27D4EB4FL;
14
+ private static final long PRIME64_3 = 0x165667B19E3779F9L;
15
+ private static final long PRIME64_4 = 0x85EBCA77C2B2AE63L;
16
+ private static final long PRIME64_5 = 0x27D4EB2F165667C5L;
17
+ private static final long PRIME_MX1 = 0x165667919E3779F9L;
18
+ private static final long PRIME_MX2 = 0x9FB21C651E98DF25L;
19
+ private static final int STRIPE = 64;
20
+ private static final int STRIPES_PER_BLOCK = (192 - STRIPE) / 8;
21
+ private static final int BLOCK = STRIPE * STRIPES_PER_BLOCK;
22
+ private static final byte[] SECRET = hex(
23
+ "b8fe6c3923a44bbe7c01812cf721ad1cded46de9839097db7240a4a4b7b3671f"
24
+ + "cb79e64eccc0e578825ad07dccff7221b8084674f743248ee03590e6813a264c"
25
+ + "3c2852bb91c300cb88d0658b1b532ea371644897a20df94e3819ef46a9deacd8"
26
+ + "a8fa763fe39c343ff9dcbbc7c70b4f1d8a51e04bcdb45931c89f7ec9d9787364"
27
+ + "eac5ac8334d3ebc3c581a0fffa1363eb170ddd51b7f0da49d316552629d4689e"
28
+ + "2b16be587d47a1fc8ff8b8d17ad031ce45cb3a8f95160428afd7fbcabb4b407e");
29
+
30
+ private Xxh3() {
31
+ }
32
+
33
+ /** The hash of the bytes as 32 lower-case hex digits, the high half first. */
34
+ public static String hexDigest(byte[] input) {
35
+ long[] h = hash(input);
36
+ return String.format("%016x%016x", h[1], h[0]);
37
+ }
38
+
39
+ /** The hash of the bytes as its low and high halves. */
40
+ public static long[] hash(byte[] in) {
41
+ int len = in.length;
42
+ if (len == 0) {
43
+ return new long[] {
44
+ xxh64Avalanche(le64(SECRET, 64) ^ le64(SECRET, 72)),
45
+ xxh64Avalanche(le64(SECRET, 80) ^ le64(SECRET, 88))};
46
+ }
47
+ if (len <= 3) {
48
+ long low = (in[0] & 0xFFL) << 16 | (in[len >> 1] & 0xFFL) << 24 | (in[len - 1] & 0xFFL) | (long) len << 8;
49
+ long high = Integer.toUnsignedLong(Integer.rotateLeft(Integer.reverseBytes((int) low), 13));
50
+ return new long[] {
51
+ xxh64Avalanche(low ^ ((le32(SECRET, 0) ^ le32(SECRET, 4)) & 0xFFFFFFFFL)),
52
+ xxh64Avalanche(high ^ ((le32(SECRET, 8) ^ le32(SECRET, 12)) & 0xFFFFFFFFL))};
53
+ }
54
+ if (len <= 8) {
55
+ long keyed = (le32(in, 0) + (le32(in, len - 4) << 32)) ^ (le64(SECRET, 16) ^ le64(SECRET, 24));
56
+ long multiplier = PRIME64_1 + ((long) len << 2);
57
+ long low = keyed * multiplier;
58
+ long high = Math.unsignedMultiplyHigh(keyed, multiplier);
59
+ high += low << 1;
60
+ low ^= high >>> 3;
61
+ low ^= low >>> 35;
62
+ low *= PRIME_MX2;
63
+ low ^= low >>> 28;
64
+ return new long[] {low, avalanche(high)};
65
+ }
66
+ if (len <= 16) {
67
+ long inputHigh = le64(in, len - 8) ^ (le64(SECRET, 48) ^ le64(SECRET, 56));
68
+ long keyed = le64(in, 0) ^ le64(in, len - 8) ^ (le64(SECRET, 32) ^ le64(SECRET, 40));
69
+ long low = keyed * PRIME64_1;
70
+ long high = Math.unsignedMultiplyHigh(keyed, PRIME64_1);
71
+ low += (long) (len - 1) << 54;
72
+ high += inputHigh + (inputHigh & 0xFFFFFFFFL) * (PRIME32_2 - 1);
73
+ low ^= Long.reverseBytes(high);
74
+ long resultLow = low * PRIME64_2;
75
+ long resultHigh = Math.unsignedMultiplyHigh(low, PRIME64_2) + high * PRIME64_2;
76
+ return new long[] {avalanche(resultLow), avalanche(resultHigh)};
77
+ }
78
+ if (len <= 240) {
79
+ long[] acc = {len * PRIME64_1, 0};
80
+ if (len <= 128) {
81
+ if (len > 32) {
82
+ if (len > 64) {
83
+ if (len > 96) {
84
+ mix32(acc, in, 48, len - 64, 96);
85
+ }
86
+ mix32(acc, in, 32, len - 48, 64);
87
+ }
88
+ mix32(acc, in, 16, len - 32, 32);
89
+ }
90
+ mix32(acc, in, 0, len - 16, 0);
91
+ } else {
92
+ for (int i = 0; i < 4; i++) {
93
+ mix32(acc, in, 32 * i, 32 * i + 16, 32 * i);
94
+ }
95
+ acc[0] = avalanche(acc[0]);
96
+ acc[1] = avalanche(acc[1]);
97
+ for (int i = 4; i < len / 32; i++) {
98
+ mix32(acc, in, 32 * i, 32 * i + 16, 3 + 32 * (i - 4));
99
+ }
100
+ mix32(acc, in, len - 16, len - 32, 136 - 17 - 16);
101
+ }
102
+ long low = acc[0] + acc[1];
103
+ long high = acc[0] * PRIME64_1 + acc[1] * PRIME64_4 + len * PRIME64_2;
104
+ return new long[] {avalanche(low), -avalanche(high)};
105
+ }
106
+ long[] acc = {PRIME32_3, PRIME64_1, PRIME64_2, PRIME64_3, PRIME64_4, PRIME32_2, PRIME64_5, PRIME32_1};
107
+ int blocks = (len - 1) / BLOCK;
108
+ for (int n = 0; n < blocks; n++) {
109
+ for (int s = 0; s < STRIPES_PER_BLOCK; s++) {
110
+ accumulate(acc, in, n * BLOCK + s * STRIPE, s * 8);
111
+ }
112
+ for (int i = 0; i < 8; i++) {
113
+ long a = acc[i];
114
+ a ^= a >>> 47;
115
+ a ^= le64(SECRET, 192 - STRIPE + 8 * i);
116
+ acc[i] = a * PRIME32_1;
117
+ }
118
+ }
119
+ int stripes = ((len - 1) - BLOCK * blocks) / STRIPE;
120
+ for (int s = 0; s < stripes; s++) {
121
+ accumulate(acc, in, blocks * BLOCK + s * STRIPE, s * 8);
122
+ }
123
+ accumulate(acc, in, len - STRIPE, 192 - STRIPE - 7);
124
+ return new long[] {merge(acc, 11, len * PRIME64_1), merge(acc, 192 - STRIPE - 11, ~(len * PRIME64_2))};
125
+ }
126
+
127
+ private static void accumulate(long[] acc, byte[] in, int at, int secretAt) {
128
+ for (int i = 0; i < 8; i++) {
129
+ long value = le64(in, at + 8 * i);
130
+ long key = value ^ le64(SECRET, secretAt + 8 * i);
131
+ acc[i ^ 1] += value;
132
+ acc[i] += (key & 0xFFFFFFFFL) * (key >>> 32);
133
+ }
134
+ }
135
+
136
+ private static long merge(long[] acc, int secretAt, long start) {
137
+ long result = start;
138
+ for (int i = 0; i < 4; i++) {
139
+ result += fold(acc[2 * i] ^ le64(SECRET, secretAt + 16 * i), acc[2 * i + 1] ^ le64(SECRET, secretAt + 16 * i + 8));
140
+ }
141
+ return avalanche(result);
142
+ }
143
+
144
+ private static void mix32(long[] acc, byte[] in, int first, int second, int secretAt) {
145
+ acc[0] += mix16(in, first, secretAt);
146
+ acc[0] ^= le64(in, second) + le64(in, second + 8);
147
+ acc[1] += mix16(in, second, secretAt + 16);
148
+ acc[1] ^= le64(in, first) + le64(in, first + 8);
149
+ }
150
+
151
+ private static long mix16(byte[] in, int at, int secretAt) {
152
+ return fold(le64(in, at) ^ le64(SECRET, secretAt), le64(in, at + 8) ^ le64(SECRET, secretAt + 8));
153
+ }
154
+
155
+ /** The low and high halves of the 128-bit product, combined by exclusive or. */
156
+ private static long fold(long a, long b) {
157
+ return (a * b) ^ Math.unsignedMultiplyHigh(a, b);
158
+ }
159
+
160
+ private static long avalanche(long h) {
161
+ h ^= h >>> 37;
162
+ h *= PRIME_MX1;
163
+ return h ^ (h >>> 32);
164
+ }
165
+
166
+ private static long xxh64Avalanche(long h) {
167
+ h ^= h >>> 33;
168
+ h *= PRIME64_2;
169
+ h ^= h >>> 29;
170
+ h *= PRIME64_3;
171
+ return h ^ (h >>> 32);
172
+ }
173
+
174
+ private static long le32(byte[] b, int at) {
175
+ return (b[at] & 0xFFL) | (b[at + 1] & 0xFFL) << 8 | (b[at + 2] & 0xFFL) << 16 | (b[at + 3] & 0xFFL) << 24;
176
+ }
177
+
178
+ private static long le64(byte[] b, int at) {
179
+ return le32(b, at) | le32(b, at + 4) << 32;
180
+ }
181
+
182
+ private static byte[] hex(String text) {
183
+ byte[] bytes = new byte[text.length() / 2];
184
+ for (int i = 0; i < bytes.length; i++) {
185
+ bytes[i] = (byte) Integer.parseInt(text.substring(2 * i, 2 * i + 2), 16);
186
+ }
187
+ return bytes;
188
+ }
189
+ }