Skip to content

Commit b7f60ae

Browse files
committed
Changed default count to 16. Fixed parsing bugs.
1 parent 63d522e commit b7f60ae

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/params.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void Params::validate_count(struct argp_state* state) {
2525
if(!raw.count.empty()) {
2626
count = std::stoi(raw.count);
2727
} else {
28-
count = 32;
28+
count = 16;
2929
}
3030

3131
if(count < 1 || count > 64) {
@@ -89,17 +89,17 @@ void Params::validate_output_format(struct argp_state* state) {
8989

9090
void Params::validate_output_for_input(struct argp_state* state) {
9191
// Any input format works with hex output format.
92-
if(output_format->key == Format::Key::hex) {
92+
if(dynamic_cast<FormatHex*>(output_format) != NULL) {
9393
return;
9494
}
9595

9696
// Random input works with any output format.
97-
if(input_format->key == Format::Key::random) {
97+
if(dynamic_cast<FormatRandom*>(input_format) != NULL) {
9898
return;
9999
}
100100

101101
// Hex input works with any output format.
102-
if(input_format->key == Format::Key::hex) {
102+
if(dynamic_cast<FormatHex*>(input_format) != NULL) {
103103
return;
104104
}
105105

@@ -108,11 +108,10 @@ void Params::validate_output_for_input(struct argp_state* state) {
108108
}
109109

110110
void Params::validate_ints_specific(struct argp_state* state) {
111-
Format* f = output_format;
112-
if(f->key == Format::Key::ints) {
113-
auto fi = dynamic_cast<FormatInts*>(f);
114-
int low = fi->low;
115-
int high = fi->low;
111+
auto f = dynamic_cast<FormatInts*>(output_format);
112+
if(f != NULL) {
113+
int low = f->low;
114+
int high = f->high;
116115
if(!raw.ints_low.empty()) {
117116
low = std::stoi(raw.ints_low);
118117
}
@@ -122,8 +121,8 @@ void Params::validate_ints_specific(struct argp_state* state) {
122121
if(!(0 <= low && low < high && high <= 255)) {
123122
argp_error(state, "--low and --high must specify a range in [0-255].");
124123
}
125-
fi->low = low;
126-
fi->high = high;
124+
f->low = low;
125+
f->high = high;
127126
} else {
128127
if(!raw.ints_low.empty()) {
129128
argp_error(state, "Option --low can only be used with the \"ints\" output format.");
@@ -135,13 +134,13 @@ void Params::validate_ints_specific(struct argp_state* state) {
135134
}
136135

137136
void Params::validate_bip39_specific(struct argp_state* state) {
138-
if(output_format->key != Format::Key::bip39) { return; }
137+
if(dynamic_cast<FormatBIP39*>(output_format) == NULL) { return; }
139138
if(!FormatBIP39::is_seed_length_valid(count)) {
140139
argp_error(state, "For BIP39 COUNT must be in [12-32] and even.");
141140
}
142141
}
143142

144-
void parse_group_spec(const std::string &string, group_descriptor* group, struct argp_state* state) {
143+
group_descriptor parse_group_spec(const std::string &string, struct argp_state* state) {
145144
size_t threshold;
146145
size_t count;
147146
auto items = sscanf(string.c_str(), "%zd-of-%zd", &threshold, &count);
@@ -151,35 +150,40 @@ void parse_group_spec(const std::string &string, group_descriptor* group, struct
151150
if(!(0 < threshold && threshold <= count && count <= 16)) {
152151
argp_error(state, "Invalid group specifier \"%s\": 1 <= N <= M <= 16", string.c_str());
153152
}
154-
group->threshold = threshold;
155-
group->count = count;
156-
group->passwords = NULL;
153+
group_descriptor g;
154+
g.threshold = threshold;
155+
g.count = count;
156+
g.passwords = NULL;
157+
return g;
157158
}
158159

159160
void Params::validate_slip39_specific(struct argp_state* state) {
160-
auto groups_count = raw.slip39_groups.size();
161+
auto raw_groups_count = raw.slip39_groups.size();
161162

162-
if(output_format->key != Format::Key::slip39) {
163-
if(groups_count > 0) {
163+
auto of = dynamic_cast<FormatSLIP39*>(output_format);
164+
if(of == NULL) {
165+
if(raw_groups_count > 0) {
164166
argp_error(state, "Option --group can only be used with the \"slip39\" output format.");
165167
}
166168
if(!raw.slip39_groups_threshold.empty()) {
167169
argp_error(state, "Option --group-threshold can only be used with the \"slip39\" output format.");
168170
}
169171
return;
170172
}
173+
171174
if(!FormatSLIP39::is_seed_length_valid(count)) {
172175
argp_error(state, "For BIP39 COUNT must be in [16-32] and even.");
173176
}
174177

175178
std::vector<group_descriptor> groups;
176-
if(groups_count > MAX_GROUPS) {
179+
if(raw_groups_count > MAX_GROUPS) {
177180
argp_error(state, "There must be no more than %d groups.", MAX_GROUPS);
178-
} else if(groups_count == 0) {
179-
groups.push_back( (group_descriptor){1, 1} );
181+
} else if(raw_groups_count == 0) {
182+
groups.push_back( {1, 1} );
180183
} else {
181-
for(auto i = 0; i < groups_count; i++) {
182-
parse_group_spec(raw.slip39_groups[i], &groups[i], state);
184+
for(auto g: raw.slip39_groups) {
185+
auto group = parse_group_spec(g, state);
186+
groups.push_back(group);
183187
}
184188
}
185189

@@ -189,10 +193,10 @@ void Params::validate_slip39_specific(struct argp_state* state) {
189193
} else {
190194
groups_threshold = std::stoi(raw.slip39_groups_threshold);
191195
}
192-
if(!(0 < groups_threshold && groups_threshold <= groups_count)) {
196+
if(!(0 < groups_threshold && groups_threshold <= groups.size())) {
193197
argp_error(state, "Group threshold must be <= the number of groups.");
194198
}
195-
auto of = dynamic_cast<FormatSLIP39*>(output_format);
199+
196200
of->groups_threshold = groups_threshold;
197201
of->groups = groups;
198202
}
@@ -213,7 +217,7 @@ static void parse_input_opt(Params* p, const char* arg, struct argp_state* state
213217

214218
static int parse_opt(int key, char* arg, struct argp_state* state) {
215219
auto p = static_cast<Params*>(state->input);
216-
auto raw = p->raw;
220+
auto& raw = p->raw;
217221

218222
switch (key) {
219223
case ARGP_KEY_INIT: break;

src/utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ std::string data_to_ints(const std::vector<uint8_t> &in,
9090
result += separator;
9191
}
9292

93-
result += std::to_string(in[i]);
93+
result += std::to_string(data[i] + low);
9494
}
9595

9696
return result;

0 commit comments

Comments
 (0)