Skip to content

Commit f906a73

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

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ $(distdir): FORCE
5858
cp $(srcdir)/src/Makefile.in $(distdir)/src
5959
cp $(srcdir)/.gitignore $(distdir)
6060
cp $(srcdir)/src/*.c $(distdir)/src
61+
cp $(srcdir)/src/*.cpp $(distdir)/src
6162
cp $(srcdir)/src/*.h $(distdir)/src
6263

6364
.PHONY: distcheck

src/params.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void params::validate_input_format(struct argp_state* state) {
6262
if(raw->input_format == NULL) {
6363
f = format_random_new();
6464
} else {
65-
format_key k = format_key_for_string(raw->input_format);
65+
auto k = format_key_for_string(raw->input_format);
6666
switch(k) {
6767
case format_key_random: f = format_random_new(); break;
6868
case format_key_hex: f = format_hex_new(); break;
@@ -87,7 +87,7 @@ void params::validate_output_format(struct argp_state* state) {
8787
if(raw->output_format == NULL) {
8888
f = format_hex_new();
8989
} else {
90-
format_key k = format_key_for_string(raw->output_format);
90+
auto k = format_key_for_string(raw->output_format);
9191
switch(k) {
9292
case format_key_hex: f = format_hex_new(); break;
9393
case format_key_bits: f = format_bits_new(); break;
@@ -162,7 +162,7 @@ void params::validate_bip39_specific(struct argp_state* state) {
162162
void parse_group_spec(const char* string, group_descriptor* group, struct argp_state* state) {
163163
size_t threshold;
164164
size_t count;
165-
int items = sscanf(string, "%zd-of-%zd", &threshold, &count);
165+
auto items = sscanf(string, "%zd-of-%zd", &threshold, &count);
166166
if(items != 2) {
167167
argp_error(state, "Could not parse group specifier: \"%s\"", string);
168168
}
@@ -175,7 +175,7 @@ void parse_group_spec(const char* string, group_descriptor* group, struct argp_s
175175
}
176176

177177
void params::validate_slip39_specific(struct argp_state* state) {
178-
int groups_count = raw->slip39_groups_count;
178+
auto groups_count = raw->slip39_groups_count;
179179

180180
if(output_format->key != format_key_slip39) {
181181
if(groups_count > 0) {
@@ -197,7 +197,7 @@ void params::validate_slip39_specific(struct argp_state* state) {
197197
groups[0] = (group_descriptor){1, 1};
198198
groups_count = 1;
199199
} else {
200-
for(int i = 0; i < groups_count; i++) {
200+
for(auto i = 0; i < groups_count; i++) {
201201
parse_group_spec(raw->slip39_groups[i], &groups[i], state);
202202
}
203203
}
@@ -230,8 +230,8 @@ static void parse_input_opt(params* p, const char* arg, struct argp_state* state
230230
}
231231

232232
static int parse_opt(int key, char* arg, struct argp_state* state) {
233-
params* p = (params*)state->input;
234-
raw_params* raw = p->raw;
233+
auto p = static_cast<params*>(state->input);
234+
auto raw = p->raw;
235235

236236
switch (key) {
237237
case ARGP_KEY_INIT: break;
@@ -275,15 +275,14 @@ struct argp_option options[] = {
275275
{ 0 }
276276
};
277277

278-
const char* argp_program_version = "1.0";
278+
auto argp_program_version = "1.0";
279279
// const char* argp_program_bug_address = "[email protected]";
280280

281-
char doc[] = "Converts cryptographic seeds between various forms.";
281+
auto doc = "Converts cryptographic seeds between various forms.";
282282
struct argp argp = { options, parse_opt, "INPUT", doc };
283283

284284
params* params_parse( int argc, char *argv[] ) {
285-
printf("Hello, world!\n");
286-
params* p = new params();
285+
auto p = new params();
287286
argp_parse( &argp, argc, argv, 0, 0, p );
288287
return p;
289288
}

src/seedtool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "format.h"
1111

1212
int main( int argc, char *argv[] ) {
13-
params* p = params_parse(argc, argv);
13+
auto p = params_parse(argc, argv);
1414
p->input_format->process_input(p->input_format, p);
1515
p->output_format->process_output(p->output_format, p);
1616

src/utils.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
char* data_to_hex(uint8_t* in, size_t insz)
1919
{
20-
char* out = (char*)malloc(insz * 2 + 1);
21-
uint8_t* pin = in;
22-
const char * hex = "0123456789abcdef";
23-
char* pout = out;
20+
auto out = (char*)malloc(insz * 2 + 1);
21+
auto pin = in;
22+
auto hex = "0123456789abcdef";
23+
auto pout = out;
2424
for(; pin < in + insz; pout += 2, pin++){
2525
pout[0] = hex[(*pin>>4) & 0xF];
2626
pout[1] = hex[ *pin & 0xF];
@@ -55,7 +55,7 @@ size_t hex_to_data(const char *hex, uint8_t **out) {
5555
return 0;
5656
}
5757

58-
size_t len = strlen(hex);
58+
auto len = strlen(hex);
5959
if (len % 2 != 0)
6060
return 0;
6161
len /= 2;
@@ -77,16 +77,16 @@ bool equal_strings(const char* a, const char* b) {
7777
}
7878

7979
uint8_t* alloc_uint8_buffer(size_t len, uint8_t value) {
80-
uint8_t* buf = (uint8_t*)malloc(len);
81-
for(int i = 0; i < len; i++) {
80+
auto buf = (uint8_t*)malloc(len);
81+
for(auto i = 0; i < len; i++) {
8282
buf[i] = value;
8383
}
8484
return buf;
8585
}
8686

8787
uint16_t* alloc_uint16_buffer(size_t len, uint16_t value) {
88-
uint16_t* buf = (uint16_t*)malloc(len * sizeof(uint16_t));
89-
for(int i = 0; i < len; i++) {
88+
auto buf = (uint16_t*)malloc(len * sizeof(uint16_t));
89+
for(auto i = 0; i < len; i++) {
9090
buf[i] = value;
9191
}
9292
return buf;
@@ -97,7 +97,7 @@ bool equal_uint8_buffers(const uint8_t* buf1, size_t len1, const uint8_t* buf2,
9797
return false;
9898
}
9999

100-
for(int i = 0; i < len1; i++) {
100+
for(auto i = 0; i < len1; i++) {
101101
if (buf1[i] != buf2[i]) {
102102
return false;
103103
}
@@ -111,7 +111,7 @@ bool equal_uint16_buffers(const uint16_t* buf1, size_t len1, const uint16_t* buf
111111
return false;
112112
}
113113

114-
for(int i = 0; i < len1; i++) {
114+
for(auto i = 0; i < len1; i++) {
115115
if (buf1[i] != buf2[i]) {
116116
return false;
117117
}
@@ -123,9 +123,9 @@ bool equal_uint16_buffers(const uint16_t* buf1, size_t len1, const uint16_t* buf
123123
uint8_t* data_to_base(size_t base, const uint8_t* buf, size_t count) {
124124
assert(2 <= base && base <= 256);
125125

126-
uint8_t* out = (uint8_t*)malloc(count);
126+
auto out = (uint8_t*)malloc(count);
127127
if (base < 256) {
128-
for(int i = 0; i < count; i++) {
128+
for(auto i = 0; i < count; i++) {
129129
out[i] = roundf(buf[i] / 255.0 * (base - 1));
130130
}
131131
} else {
@@ -135,21 +135,21 @@ uint8_t* data_to_base(size_t base, const uint8_t* buf, size_t count) {
135135
}
136136

137137
char* data_to_alphabet(const uint8_t* in, size_t count, size_t base, char* (to_alphabet)(size_t)) {
138-
uint8_t* data = data_to_base(base, in, count);
138+
auto data = data_to_base(base, in, count);
139139

140140
size_t len = 0;
141141
for(int i = 0; i < count; i++) {
142142
size_t d = data[i];
143143
assert(d < base);
144-
char* a = to_alphabet(d);
144+
auto a = to_alphabet(d);
145145
len += strlen(a);
146146
free(a);
147147
}
148148

149-
char* string = (char*)malloc(len + 1);
149+
auto string = (char*)malloc(len + 1);
150150
string[0] = '\0';
151-
for(int i = 0; i < count; i++) {
152-
char* a = to_alphabet(data[i]);
151+
for(auto i = 0; i < count; i++) {
152+
auto a = to_alphabet(data[i]);
153153
strcat(string, a);
154154
free(a);
155155
}
@@ -167,13 +167,13 @@ char* data_to_ints(const uint8_t* in, size_t count, size_t low, size_t high, con
167167
if(!(0 <= low && low < high && high <= 255)) { return NULL; }
168168

169169
size_t base = high - low + 1;
170-
uint8_t* data = data_to_base(base, in, count);
170+
auto data = data_to_base(base, in, count);
171171

172-
size_t separator_len = strlen(separator);
172+
auto separator_len = strlen(separator);
173173

174174
size_t len = 0;
175175
char buf[10];
176-
for(int i = 0; i < count; i++) {
176+
for(auto i = 0; i < count; i++) {
177177
data[i] += low;
178178

179179
if(i > 0) {
@@ -184,9 +184,9 @@ char* data_to_ints(const uint8_t* in, size_t count, size_t low, size_t high, con
184184
len += strlen(buf) + separator_len;
185185
}
186186

187-
char* string = (char*)malloc(len + 1);
187+
auto string = (char*)malloc(len + 1);
188188
string[0] = '\0';
189-
for(int i = 0; i < count; i++) {
189+
for(auto i = 0; i < count; i++) {
190190
if(i > 0) {
191191
strcat(string, separator);
192192
}

0 commit comments

Comments
 (0)