This repository was archived by the owner on Oct 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJCProfiler.java
More file actions
97 lines (83 loc) · 3.76 KB
/
JCProfiler.java
File metadata and controls
97 lines (83 loc) · 3.76 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
package opencryptoutils;
import java.io.IOException;
import org.apache.commons.cli.*;
/**
*
* @author Petr Svenda
*/
public class JCProfiler {
private Options opts = new Options();
private static final String CLI_HEADER = "\nJCProfiler, a javacard profiler utility.\n\n";
private static final String CLI_FOOTER = "\nMIT Licensed\nCopyright (c) 2017 Petr Svenda <petr@svenda.com>, OpenCryptoProject";
public static void main(String[] args) {
JCProfiler app = new JCProfiler();
app.run(args);
}
/**
* @param args the command line arguments
*/
private void run(String[] args) {
try {
CommandLine cli = parseArgs(args);
//if help, print and quit
if (cli.hasOption("help")) {
help();
return;
}
if (cli.hasOption("setTraps")) {
PerfCodeGenerator gen = new PerfCodeGenerator();
String methodBaseName = cli.getOptionValue("methodBaseName", "EC_GEN");
String startConstString = cli.getOptionValue("trapIDStartConst", "7770");
short startTrapIDConst = Short.parseShort(startConstString, 16);
PerfCodeConfig cfg = new PerfCodeConfig("not_set", methodBaseName, methodBaseName, 0, startTrapIDConst);
String baseDir = cli.getOptionValue("baseDir", "");
gen.generatePersonalizedProfiler(cfg, baseDir);
}
} catch (MissingArgumentException maex) {
System.err.println("Option, " + maex.getOption().getOpt() + " requires an argument: " + maex.getOption().getArgName());
} catch (NumberFormatException nfex) {
System.err.println("Not a number. " + nfex.getMessage());
} catch (ParseException | IOException ex) {
System.err.println(ex.getMessage());
} finally {
}
}
/**
* Parses command-line options.
*
* @param args cli arguments
* @return parsed CommandLine object
* @throws ParseException if there are any problems encountered while
* parsing the command line tokens
*/
private CommandLine parseArgs(String[] args) throws ParseException {
/*
* Actions:
* -st / --setTraps
*
* Options:
* -bd / --baseDir [base_directory]
* -mbd / --methodBaseName [name]
* -tsc / --trapIDStartConst [start_constant] <b>
*
*/
OptionGroup actions = new OptionGroup();
actions.setRequired(true);
actions.addOption(Option.builder("h").longOpt("help").desc("Print help.").build());
actions.addOption(Option.builder("st").longOpt("setTraps").desc("Parse input source code files, search for template performance traps and generates both card-side and client-side files for performance profiling.").build());
opts.addOptionGroup(actions);
opts.addOption(Option.builder("tsc").longOpt("trapIDStartConst").desc("Initial start value (short) for trapID constants.").hasArg().argName("start_constant").build());
opts.addOption(Option.builder("bd").longOpt("baseDir").desc("Base directory with template files").hasArg().argName("base_directory").required(true).build());
opts.addOption(Option.builder("mbd").longOpt("methodBaseName").desc("Base name of method to be profiled.").hasArg().argName("name").required(true).build());
CommandLineParser parser = new DefaultParser();
return parser.parse(opts, args);
}
/**
* Prints help.
*/
private void help() {
HelpFormatter help = new HelpFormatter();
help.setOptionComparator(null);
help.printHelp("JCProfiler.jar", CLI_HEADER, opts, CLI_FOOTER, true);
}
}