Skip to content

Commit c11a722

Browse files
authored
Refactoring & bugfixes (#142)
* Another round of refactoring * * FlipC.org assets added * Few bugfixes * * Updated firmware references * CLang format ran
1 parent 3458f0a commit c11a722

27 files changed

+493
-187
lines changed

cli/commands/add/add.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ enum TotpIteratorUpdateTokenResultsEx {
2020
TotpIteratorUpdateTokenResultInvalidArguments = 3
2121
};
2222

23-
static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* token_info, const void* context) {
23+
static TotpIteratorUpdateTokenResult
24+
add_token_handler(TokenInfo* token_info, const void* context) {
2425
const struct TotpAddContext* context_t = context;
2526

2627
// Reading token name
@@ -32,7 +33,7 @@ static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* token_info, co
3233

3334
// Read optional arguments
3435
bool mask_user_input = true;
35-
PlainTokenSecretEncoding token_secret_encoding = PLAIN_TOKEN_ENCODING_BASE32;
36+
PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
3637
while(args_read_string_and_trim(context_t->args, temp_str)) {
3738
bool parsed = false;
3839
if(!totp_cli_try_read_algo(token_info, temp_str, context_t->args, &parsed) &&
@@ -71,7 +72,7 @@ static TotpIteratorUpdateTokenResult add_token_handler(TokenInfo* token_info, co
7172

7273
furi_string_secure_free(temp_str);
7374

74-
if (!secret_set) {
75+
if(!secret_set) {
7576
return TotpIteratorUpdateTokenResultInvalidSecret;
7677
}
7778

@@ -148,24 +149,27 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
148149
return;
149150
}
150151

151-
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
152+
TokenInfoIteratorContext* iterator_context =
153+
totp_config_get_token_iterator_context(plugin_state);
152154

153155
TOTP_CLI_LOCK_UI(plugin_state);
154156

155-
struct TotpAddContext add_context = { .args = args, .cli = cli, .iv = &plugin_state->iv[0] };
156-
TotpIteratorUpdateTokenResult add_result = totp_token_info_iterator_add_new_token(iterator_context, &add_token_handler, &add_context);
157+
struct TotpAddContext add_context = {.args = args, .cli = cli, .iv = &plugin_state->iv[0]};
158+
TotpIteratorUpdateTokenResult add_result =
159+
totp_token_info_iterator_add_new_token(iterator_context, &add_token_handler, &add_context);
157160

158161
if(add_result == TotpIteratorUpdateTokenResultSuccess) {
159162
TOTP_CLI_PRINTF_SUCCESS(
160-
"Token \"%s\" has been successfully added\r\n",
161-
furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name));
162-
} else if (add_result == TotpIteratorUpdateTokenResultCancelled) {
163+
"Token \"%s\" has been successfully added\r\n",
164+
furi_string_get_cstr(
165+
totp_token_info_iterator_get_current_token(iterator_context)->name));
166+
} else if(add_result == TotpIteratorUpdateTokenResultCancelled) {
163167
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
164-
} else if (add_result == TotpIteratorUpdateTokenResultInvalidArguments) {
168+
} else if(add_result == TotpIteratorUpdateTokenResultInvalidArguments) {
165169
totp_cli_print_invalid_arguments();
166-
} else if (add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
170+
} else if(add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
167171
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
168-
} else if (add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
172+
} else if(add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
169173
totp_cli_print_error_updating_config_file();
170174
}
171175

cli/commands/delete/delete.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
3636
return;
3737
}
3838

39-
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
39+
TokenInfoIteratorContext* iterator_context =
40+
totp_config_get_token_iterator_context(plugin_state);
4041

4142
int token_number;
4243
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
43-
(size_t)token_number >
44-
totp_token_info_iterator_get_total_count(iterator_context)) {
44+
(size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
4545
totp_cli_print_invalid_arguments();
4646
return;
4747
}
@@ -62,7 +62,8 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
6262

6363
TOTP_CLI_LOCK_UI(plugin_state);
6464

65-
size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
65+
size_t original_token_index =
66+
totp_token_info_iterator_get_current_token_index(iterator_context);
6667
totp_token_info_iterator_go_to(iterator_context, token_number - 1);
6768
const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
6869
const char* token_info_name = furi_string_get_cstr(token_info->name);

cli/commands/details/details.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
} while(false)
1919

2020
static void print_automation_features(const TokenInfo* token_info) {
21-
if(token_info->automation_features == TOKEN_AUTOMATION_FEATURE_NONE) {
21+
if(token_info->automation_features == TokenAutomationFeatureNone) {
2222
TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", "Automation features", "None");
2323
return;
2424
}
2525

2626
bool header_printed = false;
27-
if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) {
27+
if(token_info->automation_features & TokenAutomationFeatureEnterAtTheEnd) {
2828
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Enter> key at the end", header_printed);
2929
}
3030

31-
if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) {
31+
if(token_info->automation_features & TokenAutomationFeatureTabAtTheEnd) {
3232
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Tab> key at the end", header_printed);
3333
}
3434

35-
if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TYPE_SLOWER) {
35+
if(token_info->automation_features & TokenAutomationFeatureTypeSlower) {
3636
TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type slower", header_printed);
3737
}
3838
}
@@ -54,7 +54,8 @@ void totp_cli_command_details_handle(PluginState* plugin_state, FuriString* args
5454
}
5555

5656
int token_number;
57-
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
57+
TokenInfoIteratorContext* iterator_context =
58+
totp_config_get_token_iterator_context(plugin_state);
5859
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
5960
(size_t)token_number > totp_token_info_iterator_get_total_count(iterator_context)) {
6061
totp_cli_print_invalid_arguments();
@@ -63,7 +64,8 @@ void totp_cli_command_details_handle(PluginState* plugin_state, FuriString* args
6364

6465
TOTP_CLI_LOCK_UI(plugin_state);
6566

66-
size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
67+
size_t original_token_index =
68+
totp_token_info_iterator_get_current_token_index(iterator_context);
6769
if(totp_token_info_iterator_go_to(iterator_context, token_number - 1)) {
6870
const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
6971

cli/commands/list/list.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
2121
return;
2222
}
2323

24-
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
24+
TokenInfoIteratorContext* iterator_context =
25+
totp_config_get_token_iterator_context(plugin_state);
2526
size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
2627
if(total_count <= 0) {
2728
TOTP_CLI_PRINTF("There are no tokens");

cli/commands/move/move.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
3333
}
3434

3535
int token_number;
36-
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
36+
TokenInfoIteratorContext* iterator_context =
37+
totp_config_get_token_iterator_context(plugin_state);
3738
size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
3839
if(!args_read_int_and_trim(args, &token_number) || token_number < 1 ||
3940
(size_t)token_number > total_count) {
@@ -59,7 +60,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
5960
size_t token_index = token_number - 1;
6061
size_t new_token_index = new_token_number - 1;
6162

62-
size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
63+
size_t original_token_index =
64+
totp_token_info_iterator_get_current_token_index(iterator_context);
6365

6466
totp_cli_print_processing();
6567

@@ -68,7 +70,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
6870
totp_cli_delete_last_line();
6971
TOTP_CLI_PRINTF_SUCCESS(
7072
"Token \"%s\" has been successfully updated\r\n",
71-
furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name));
73+
furi_string_get_cstr(
74+
totp_token_info_iterator_get_current_token(iterator_context)->name));
7275
} else {
7376
totp_cli_delete_last_line();
7477
totp_cli_print_error_updating_config_file();

cli/commands/update/update.c

+29-21
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ enum TotpIteratorUpdateTokenResultsEx {
2222
TotpIteratorUpdateTokenResultInvalidArguments = 3
2323
};
2424

25-
static bool
26-
totp_cli_try_read_name(TokenInfo* token_info, const FuriString* arg, FuriString* args, bool* parsed) {
25+
static bool totp_cli_try_read_name(
26+
TokenInfo* token_info,
27+
const FuriString* arg,
28+
FuriString* args,
29+
bool* parsed) {
2730
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_NAME_PREFIX) == 0) {
2831
if(!args_read_probably_quoted_string_and_trim(args, token_info->name) ||
2932
furi_string_empty(token_info->name)) {
@@ -48,14 +51,15 @@ static bool totp_cli_try_read_change_secret_flag(const FuriString* arg, bool* pa
4851
return false;
4952
}
5053

51-
static TotpIteratorUpdateTokenResult update_token_handler(TokenInfo* token_info, const void* context) {
54+
static TotpIteratorUpdateTokenResult
55+
update_token_handler(TokenInfo* token_info, const void* context) {
5256
const struct TotpUpdateContext* context_t = context;
5357

5458
// Read optional arguments
5559
FuriString* temp_str = furi_string_alloc();
5660
bool mask_user_input = true;
5761
bool update_token_secret = false;
58-
PlainTokenSecretEncoding token_secret_encoding = PLAIN_TOKEN_ENCODING_BASE32;
62+
PlainTokenSecretEncoding token_secret_encoding = PlainTokenSecretEncodingBase32;
5963
while(args_read_string_and_trim(context_t->args, temp_str)) {
6064
bool parsed = false;
6165
if(!totp_cli_try_read_name(token_info, temp_str, context_t->args, &parsed) &&
@@ -82,17 +86,17 @@ static TotpIteratorUpdateTokenResult update_token_handler(TokenInfo* token_info,
8286
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]\r\n");
8387
bool token_secret_read = totp_cli_read_line(context_t->cli, temp_str, mask_user_input);
8488
totp_cli_delete_last_line();
85-
if (!token_secret_read) {
89+
if(!token_secret_read) {
8690
furi_string_secure_free(temp_str);
8791
return TotpIteratorUpdateTokenResultCancelled;
8892
}
8993

90-
if (!token_info_set_secret(
91-
token_info,
92-
furi_string_get_cstr(temp_str),
93-
furi_string_size(temp_str),
94-
token_secret_encoding,
95-
context_t->iv)) {
94+
if(!token_info_set_secret(
95+
token_info,
96+
furi_string_get_cstr(temp_str),
97+
furi_string_size(temp_str),
98+
token_secret_encoding,
99+
context_t->iv)) {
96100
furi_string_secure_free(temp_str);
97101
return TotpIteratorUpdateTokenResultInvalidSecret;
98102
}
@@ -131,7 +135,8 @@ void totp_cli_command_update_handle(PluginState* plugin_state, FuriString* args,
131135
return;
132136
}
133137

134-
TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
138+
TokenInfoIteratorContext* iterator_context =
139+
totp_config_get_token_iterator_context(plugin_state);
135140

136141
int token_number;
137142
if(!args_read_int_and_trim(args, &token_number) || token_number <= 0 ||
@@ -145,23 +150,26 @@ void totp_cli_command_update_handle(PluginState* plugin_state, FuriString* args,
145150
size_t previous_index = totp_token_info_iterator_get_current_token_index(iterator_context);
146151
totp_token_info_iterator_go_to(iterator_context, token_number - 1);
147152

148-
struct TotpUpdateContext update_context = { .args = args, .cli = cli, .iv = &plugin_state->iv[0] };
149-
TotpIteratorUpdateTokenResult update_result = totp_token_info_iterator_update_current_token(iterator_context, &update_token_handler, &update_context);
150-
153+
struct TotpUpdateContext update_context = {
154+
.args = args, .cli = cli, .iv = &plugin_state->iv[0]};
155+
TotpIteratorUpdateTokenResult update_result = totp_token_info_iterator_update_current_token(
156+
iterator_context, &update_token_handler, &update_context);
157+
151158
if(update_result == TotpIteratorUpdateTokenResultSuccess) {
152159
TOTP_CLI_PRINTF_SUCCESS(
153160
"Token \"%s\" has been successfully updated\r\n",
154-
furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name));
155-
} else if (update_result == TotpIteratorUpdateTokenResultInvalidArguments) {
161+
furi_string_get_cstr(
162+
totp_token_info_iterator_get_current_token(iterator_context)->name));
163+
} else if(update_result == TotpIteratorUpdateTokenResultInvalidArguments) {
156164
totp_cli_print_invalid_arguments();
157-
} else if (update_result == TotpIteratorUpdateTokenResultCancelled) {
165+
} else if(update_result == TotpIteratorUpdateTokenResultCancelled) {
158166
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
159-
} else if (update_result == TotpIteratorUpdateTokenResultInvalidSecret) {
167+
} else if(update_result == TotpIteratorUpdateTokenResultInvalidSecret) {
160168
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
161-
} else if (update_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
169+
} else if(update_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
162170
totp_cli_print_error_updating_config_file();
163171
}
164-
172+
165173
totp_token_info_iterator_go_to(iterator_context, previous_index);
166174
TOTP_CLI_UNLOCK_UI(plugin_state);
167175
}

cli/common_command_arguments.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ bool totp_cli_try_read_plain_token_secret_encoding(
121121
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX);
122122
} else {
123123
if(furi_string_cmpi_str(arg, PLAIN_TOKEN_ENCODING_BASE32_NAME) == 0) {
124-
*secret_encoding = PLAIN_TOKEN_ENCODING_BASE32;
124+
*secret_encoding = PlainTokenSecretEncodingBase32;
125125
*parsed = true;
126126
} else if(furi_string_cmpi_str(arg, PLAIN_TOKEN_ENCODING_BASE64_NAME) == 0) {
127-
*secret_encoding = PLAIN_TOKEN_ENCODING_BASE64;
127+
*secret_encoding = PlainTokenSecretEncodingBase64;
128128
*parsed = true;
129129
} else {
130130
TOTP_CLI_PRINTF_ERROR(

lib/base64/base64.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Base64 encoding/decoding (RFC1341)
33
* Copyright (c) 2005, Jouni Malinen <[email protected]>
4+
* Modified and optimized for Flipepr Zero device purposes by Alex Kopachov (@akopachov)
45
*
56
* This program is free software; you can redistribute it and/or modify
67
* it under the terms of the GNU General Public License version 2 as

services/config/config.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void totp_close_config_file(FlipperFormat* file) {
6868
*/
6969
static char* totp_config_file_backup_i(Storage* storage) {
7070
if(!storage_dir_exists(storage, CONFIG_FILE_BACKUP_DIR) &&
71-
!storage_simply_mkdir(storage, CONFIG_FILE_BACKUP_DIR)) {
71+
!storage_simply_mkdir(storage, CONFIG_FILE_BACKUP_DIR)) {
7272
return NULL;
7373
}
7474

@@ -434,9 +434,7 @@ bool totp_config_file_load(PluginState* const plugin_state) {
434434
plugin_state->config_file_context->config_file = fff_data_file;
435435
plugin_state->config_file_context->token_info_iterator_context =
436436
totp_token_info_iterator_alloc(
437-
storage,
438-
plugin_state->config_file_context->config_file,
439-
plugin_state->iv);
437+
storage, plugin_state->config_file_context->config_file, plugin_state->iv);
440438
result = true;
441439
} while(false);
442440

@@ -493,8 +491,7 @@ bool totp_config_file_update_encryption(
493491
PluginState* plugin_state,
494492
const uint8_t* new_pin,
495493
uint8_t new_pin_length) {
496-
FlipperFormat* config_file =
497-
plugin_state->config_file_context->config_file;
494+
FlipperFormat* config_file = plugin_state->config_file_context->config_file;
498495
Stream* stream = flipper_format_get_raw_stream(config_file);
499496
size_t original_offset = stream_tell(stream);
500497
if(!stream_rewind(stream)) {
@@ -513,12 +510,12 @@ bool totp_config_file_update_encryption(
513510

514511
CryptoSeedIVResult seed_result =
515512
totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
516-
if(seed_result & CRYPTO_SEED_IV_RESULT_FLAG_SUCCESS &&
517-
seed_result & CRYPTO_SEED_IV_RESULT_FLAG_NEW_CRYPTO_VERIFY_DATA) {
513+
if(seed_result & CryptoSeedIVResultFlagSuccess &&
514+
seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
518515
if(!totp_config_file_update_crypto_signatures(plugin_state)) {
519516
return false;
520517
}
521-
} else if(seed_result == CRYPTO_SEED_IV_RESULT_FAILED) {
518+
} else if(seed_result == CryptoSeedIVResultFailed) {
522519
return false;
523520
}
524521

services/config/migrations/common_migration.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool totp_config_migrate_to_latest(
9898
const uint32_t default_algo = SHA1;
9999
flipper_format_write_uint32(
100100
fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &default_algo, 1);
101-
const uint32_t default_digits = TOTP_6_DIGITS;
101+
const uint32_t default_digits = TotpSixDigitsCount;
102102
flipper_format_write_uint32(
103103
fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
104104
}
@@ -120,7 +120,7 @@ bool totp_config_migrate_to_latest(
120120
flipper_format_write_string(
121121
fff_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
122122
} else {
123-
const uint32_t default_automation_features = TOKEN_AUTOMATION_FEATURE_NONE;
123+
const uint32_t default_automation_features = TokenAutomationFeatureNone;
124124
flipper_format_write_uint32(
125125
fff_data_file,
126126
TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES,
@@ -132,7 +132,7 @@ bool totp_config_migrate_to_latest(
132132
Stream* stream = flipper_format_get_raw_stream(fff_data_file);
133133
size_t current_pos = stream_tell(stream);
134134
size_t total_size = stream_size(stream);
135-
if (current_pos < total_size) {
135+
if(current_pos < total_size) {
136136
stream_delete(stream, total_size - current_pos);
137137
}
138138

services/config/migrations/common_migration.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#include <flipper_format/flipper_format.h>
44

5+
/**
6+
* @brief Migrates config file to the latest version
7+
* @param fff_data_file original config file to be migrated
8+
* @param fff_backup_data_file backup copy of original config file
9+
* @return \c true if operation succeeded; \c false otherwise
10+
*/
511
bool totp_config_migrate_to_latest(
612
FlipperFormat* fff_data_file,
7-
FlipperFormat* fff_backup_data_file);
13+
FlipperFormat* fff_backup_data_file);

0 commit comments

Comments
 (0)