Skip to content

Commit c536ad8

Browse files
committed
Converting to C++.
1 parent 058bbda commit c536ad8

File tree

9 files changed

+113
-145
lines changed

9 files changed

+113
-145
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "lldb",
88
"request": "launch",
99
"program": "${workspaceFolder}/src/seedtool",
10-
"args": ["--out","slip39","--group","2-of-3"],
10+
"args": [],
1111
"cwd": "${workspaceFolder}/src",
1212
"preLaunchTask": "Build"
1313
}

.vscode/settings.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"files.associations": {
3-
"Makefile.in": "makefile",
4-
"*.h": "c"
3+
"Makefile.in": "makefile"
54
}
6-
}
5+
}

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ PACKAGE_STRING='bc-seedtool-cli 1.0'
582582
PACKAGE_BUGREPORT=''
583583
PACKAGE_URL=''
584584

585-
ac_unique_file="src/seedtool.c"
585+
ac_unique_file="src/seedtool.cpp"
586586
# Factoring default headers for most tests.
587587
ac_includes_default="\
588588
#include <stdio.h>

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
AC_PREREQ([2.69])
55
AC_INIT([bc-seedtool-cli], [1.0])
6-
AC_CONFIG_SRCDIR([src/seedtool.c])
6+
AC_CONFIG_SRCDIR([src/seedtool.cpp])
77
AC_CONFIG_HEADERS([config.h])
88

99
# Checks for programs.

src/Makefile.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ bindir = @bindir@
2121
srcdir = @srcdir@
2222
VPATH = @srcdir@
2323

24-
CFLAGS = -g -O0
24+
COMPILER = g++
25+
CFLAGS = --debug -O0
26+
CXXFLAGS = -std=c++11 -stdlib=libc++ --debug -O0
2527

2628
toolname = seedtool
2729

@@ -47,7 +49,7 @@ OBJS = \
4749
format-random.o \
4850
format-slip39.o
4951

50-
LDFLAGS = -lbc-crypto-base -lbc-shamir -lbc-slip39 -lbc-bip39 -largp
52+
LDFLAGS = -lstdc++ -lbc-crypto-base -lbc-shamir -lbc-slip39 -lbc-bip39 -largp
5153

5254
$(toolname): $(OBJS)
5355

src/format.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#ifndef FORMAT_H
99
#define FORMAT_H
1010

11-
struct params;
11+
class params;
1212
struct format;
1313

1414
typedef void (*format_func)(format*);

src/params.cpp

Lines changed: 58 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,78 +15,49 @@
1515
#include "utils.h"
1616
#include "formats-all.h"
1717

18-
static raw_params* raw_params_new() {
19-
return (raw_params*)calloc(sizeof(raw_params), 1);
18+
void raw_params::add_group(const char* g) {
19+
if(slip39_groups_count == MAX_RAW_GROUPS) { return; }
20+
slip39_groups[slip39_groups_count] = g;
21+
slip39_groups_count++;
2022
}
2123

22-
static void raw_params_dispose(raw_params* p) {
23-
free(p);
24+
void raw_params::add_arg(const char* a) {
25+
if(args_count == MAX_ARGS) { return; }
26+
args[args_count] = a;
27+
args_count++;
2428
}
2529

26-
static void raw_params_add_group(raw_params* p, const char* g) {
27-
if(p->slip39_groups_count == MAX_RAW_GROUPS) { return; }
28-
p->slip39_groups[p->slip39_groups_count] = g;
29-
p->slip39_groups_count++;
30+
params::~params() {
31+
delete raw;
32+
format_dispose(input_format);
33+
format_dispose(output_format);
34+
free(input);
35+
free(output);
36+
free(seed);
3037
}
3138

32-
static void raw_params_add_arg(raw_params* p, const char* a) {
33-
if(p->args_count == MAX_ARGS) { return; }
34-
p->args[p->args_count] = a;
35-
p->args_count++;
36-
}
37-
38-
static params* params_new() {
39-
params* p = (params*)calloc(sizeof(params), 1);
40-
p->raw = raw_params_new();
41-
42-
// // ints-specific
43-
// p->low = 0;
44-
// p->high = 9;
45-
46-
// // SLIP39-specific
47-
// p->groups = calloc(1, sizeof(group_descriptor));
48-
// p->groups[0] = (group_descriptor){1, 1, NULL};
49-
// p->groups_count = 1;
50-
// p->groups_threshold = 1;
51-
52-
return p;
53-
}
54-
55-
void params_dispose(params* p) {
56-
if (p == NULL) { return; }
57-
raw_params_dispose(p->raw);
58-
format_dispose(p->input_format);
59-
format_dispose(p->output_format);
60-
free(p->input);
61-
free(p->output);
62-
free(p->seed);
63-
free(p);
64-
}
65-
66-
static void params_validate_count(params* p, struct argp_state* state) {
67-
if(p->raw->count != NULL) {
68-
p->count = atoi(p->raw->count);
39+
void params::validate_count(struct argp_state* state) {
40+
if(raw->count != NULL) {
41+
count = atoi(raw->count);
6942
} else {
70-
p->count = 32;
43+
count = 32;
7144
}
7245

73-
if(p->count < 1 || p->count > 64) {
46+
if(count < 1 || count > 64) {
7447
argp_error(state, "COUNT must be in [1-64].");
7548
}
7649
}
7750

78-
static void params_validate_deterministic(params* p, struct argp_state* state) {
79-
if(p->raw->random_deterministic != NULL) {
80-
seed_deterministic_string(p->raw->random_deterministic);
81-
p->rng = deterministic_random;
51+
void params::validate_deterministic(struct argp_state* state) {
52+
if(raw->random_deterministic != NULL) {
53+
seed_deterministic_string(raw->random_deterministic);
54+
rng = deterministic_random;
8255
} else {
83-
p->rng = crypto_random;
56+
rng = crypto_random;
8457
}
8558
}
8659

87-
static void params_validate_input_format(params* p, struct argp_state* state) {
88-
raw_params* raw = p->raw;
89-
60+
void params::validate_input_format(struct argp_state* state) {
9061
format* f = NULL;
9162
if(raw->input_format == NULL) {
9263
f = format_random_new();
@@ -108,12 +79,10 @@ static void params_validate_input_format(params* p, struct argp_state* state) {
10879
break;
10980
}
11081
}
111-
p->input_format = f;
82+
input_format = f;
11283
}
11384

114-
static void params_validate_output_format(params* p, struct argp_state* state) {
115-
raw_params* raw = p->raw;
116-
85+
void params::validate_output_format(struct argp_state* state) {
11786
format* f = NULL;
11887
if(raw->output_format == NULL) {
11988
f = format_hex_new();
@@ -134,33 +103,31 @@ static void params_validate_output_format(params* p, struct argp_state* state) {
134103
break;
135104
}
136105
}
137-
p->output_format = f;
106+
output_format = f;
138107
}
139108

140-
static void params_validate_output_for_input(params* p, struct argp_state* state) {
109+
void params::validate_output_for_input(struct argp_state* state) {
141110
// Any input format works with hex output format.
142-
if(p->output_format->key == format_key_hex) {
111+
if(output_format->key == format_key_hex) {
143112
return;
144113
}
145114

146115
// Random input works with any output format.
147-
if(p->input_format->key == format_key_random) {
116+
if(input_format->key == format_key_random) {
148117
return;
149118
}
150119

151120
// Hex input works with any output format.
152-
if(p->input_format->key == format_key_hex) {
121+
if(input_format->key == format_key_hex) {
153122
return;
154123
}
155124

156125
argp_error(state, "Input format %s cannot be used with output format %s",
157-
p->input_format->name, p->output_format->name);
126+
input_format->name, output_format->name);
158127
}
159128

160-
static void params_validate_ints_specific(params* p, struct argp_state* state) {
161-
raw_params* raw = p->raw;
162-
163-
format* f = p->output_format;
129+
void params::validate_ints_specific(struct argp_state* state) {
130+
format* f = output_format;
164131
if(f->key == format_key_ints) {
165132
int low = format_ints_get_low(f);
166133
int high = format_ints_get_high(f);
@@ -185,9 +152,9 @@ static void params_validate_ints_specific(params* p, struct argp_state* state) {
185152
}
186153
}
187154

188-
static void params_validate_bip39_specific(params* p, struct argp_state* state) {
189-
if(p->output_format->key != format_key_bip39) { return; }
190-
if(!format_bip39_is_seed_length_valid(p->count)) {
155+
void params::validate_bip39_specific(struct argp_state* state) {
156+
if(output_format->key != format_key_bip39) { return; }
157+
if(!format_bip39_is_seed_length_valid(count)) {
191158
argp_error(state, "For BIP39 COUNT must be in [12-32] and even.");
192159
}
193160
}
@@ -207,12 +174,10 @@ void parse_group_spec(const char* string, group_descriptor* group, struct argp_s
207174
group->passwords = NULL;
208175
}
209176

210-
static void params_validate_slip39_specific(params* p, struct argp_state* state) {
211-
raw_params* raw = p->raw;
212-
177+
void params::validate_slip39_specific(struct argp_state* state) {
213178
int groups_count = raw->slip39_groups_count;
214179

215-
if(p->output_format->key != format_key_slip39) {
180+
if(output_format->key != format_key_slip39) {
216181
if(groups_count > 0) {
217182
argp_error(state, "Option --group can only be used with the \"slip39\" output format.");
218183
}
@@ -221,7 +186,7 @@ static void params_validate_slip39_specific(params* p, struct argp_state* state)
221186
}
222187
return;
223188
}
224-
if(!format_slip39_is_seed_length_valid(p->count)) {
189+
if(!format_slip39_is_seed_length_valid(count)) {
225190
argp_error(state, "For BIP39 COUNT must be in [16-32] and even.");
226191
}
227192

@@ -246,19 +211,19 @@ static void params_validate_slip39_specific(params* p, struct argp_state* state)
246211
if(!(0 < groups_threshold && groups_threshold <= groups_count)) {
247212
argp_error(state, "Group threshold must be <= the number of groups.");
248213
}
249-
format_slip39_set_groups_threshold(p->output_format, groups_threshold);
250-
format_slip39_set_groups(p->output_format, groups, groups_count);
214+
format_slip39_set_groups_threshold(output_format, groups_threshold);
215+
format_slip39_set_groups(output_format, groups, groups_count);
251216
}
252217

253-
static void params_validate(params* p, struct argp_state* state) {
254-
params_validate_count(p, state);
255-
params_validate_deterministic(p, state);
256-
params_validate_input_format(p, state);
257-
params_validate_output_format(p, state);
258-
params_validate_output_for_input(p, state);
259-
params_validate_ints_specific(p, state);
260-
params_validate_bip39_specific(p, state);
261-
params_validate_slip39_specific(p, state);
218+
void params::validate(struct argp_state* state) {
219+
validate_count(state);
220+
validate_deterministic(state);
221+
validate_input_format(state);
222+
validate_output_format(state);
223+
validate_output_for_input(state);
224+
validate_ints_specific(state);
225+
validate_bip39_specific(state);
226+
validate_slip39_specific(state);
262227
}
263228

264229
static void parse_input_opt(params* p, const char* arg, struct argp_state* state) {
@@ -276,11 +241,11 @@ static int parse_opt(int key, char* arg, struct argp_state* state) {
276241
case 'l': raw->ints_low = arg; break;
277242
case 'h': raw->ints_high = arg; break;
278243
case 'd': raw->random_deterministic = arg; break;
279-
case 'g': raw_params_add_group(raw, arg); break;
244+
case 'g': raw->add_group(arg); break;
280245
case 't': raw->slip39_groups_threshold = arg; break;
281-
case ARGP_KEY_ARG: raw_params_add_arg(raw, arg); break;
246+
case ARGP_KEY_ARG: raw->add_arg(arg); break;
282247
case ARGP_KEY_END: {
283-
params_validate(p, state);
248+
p->validate(state);
284249
}
285250
break;
286251
}
@@ -317,7 +282,8 @@ char doc[] = "Converts cryptographic seeds between various forms.";
317282
struct argp argp = { options, parse_opt, "INPUT", doc };
318283

319284
params* params_parse( int argc, char *argv[] ) {
320-
params* p = params_new();
285+
printf("Hello, world!\n");
286+
params* p = new params();
321287
argp_parse( &argp, argc, argv, 0, 0, p );
322288
return p;
323289
}

0 commit comments

Comments
 (0)