Skip to content

Commit 981f8c3

Browse files
authored
Clang format ran (#35)
1 parent a442670 commit 981f8c3

File tree

8 files changed

+86
-68
lines changed

8 files changed

+86
-68
lines changed

totp/cli/cli.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
5050
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
5151
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
5252
totp_cli_command_move_handle(plugin_state, args, cli);
53-
} else if(
54-
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
53+
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
5554
totp_cli_command_pin_handle(plugin_state, args, cli);
5655
} else {
5756
totp_cli_print_unknown_command(cmd);

totp/cli/commands/list/list.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
5353
return;
5454
}
5555

56-
ListNode* node = plugin_state->tokens_list;
57-
5856
TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
5957
TOTP_CLI_PRINTF("| %-*s | %-*s | %-*s | %-s |\r\n", 3, "#", 27, "Name", 6, "Algo", "Digits");
6058
TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
6159
uint16_t index = 1;
62-
while(node != NULL) {
60+
TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
6361
TokenInfo* token_info = (TokenInfo*)node->data;
6462
token_info_get_digits_count(token_info);
6563
TOTP_CLI_PRINTF(
@@ -68,8 +66,7 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
6866
token_info->name,
6967
get_algo_as_cstr(token_info->algo),
7068
get_digits_as_int(token_info->digits));
71-
node = node->next;
7269
index++;
73-
}
70+
});
7471
TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
7572
}

totp/cli/commands/pin/pin.c

+57-51
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,30 @@ void totp_cli_command_pin_docopt_commands() {
1818
}
1919

2020
void totp_cli_command_pin_docopt_usage() {
21-
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) "\r\n");
21+
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN " " DOCOPT_REQUIRED(
22+
TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) "\r\n");
23+
}
24+
25+
static inline uint8_t totp_cli_key_to_pin_code(uint8_t key) {
26+
uint8_t code = 0;
27+
switch(key) {
28+
case 0x44: // left
29+
code = PinCodeArrowLeft;
30+
break;
31+
case 0x41: // up
32+
code = PinCodeArrowUp;
33+
break;
34+
case 0x43: // right
35+
code = PinCodeArrowRight;
36+
break;
37+
case 0x42: // down
38+
code = PinCodeArrowDown;
39+
break;
40+
default:
41+
break;
42+
}
43+
44+
return code;
2245
}
2346

2447
static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
@@ -30,28 +53,10 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
3053
if(c == CliSymbolAsciiEsc) {
3154
uint8_t c2;
3255
uint8_t c3;
33-
if (cli_read_timeout(cli, &c2, 1, 0) == 1 &&
34-
cli_read_timeout(cli, &c3, 1, 0) == 1 &&
35-
c2 == 0x5b) {
36-
uint8_t code = 0;
37-
switch (c3) {
38-
case 0x44: // left
39-
code = PinCodeArrowLeft;
40-
break;
41-
case 0x41: // up
42-
code = PinCodeArrowUp;
43-
break;
44-
case 0x43: // right
45-
code = PinCodeArrowRight;
46-
break;
47-
case 0x42: // down
48-
code = PinCodeArrowDown;
49-
break;
50-
default:
51-
break;
52-
}
53-
54-
if (code > 0) {
56+
if(cli_read_timeout(cli, &c2, 1, 0) == 1 && cli_read_timeout(cli, &c3, 1, 0) == 1 &&
57+
c2 == 0x5b) {
58+
uint8_t code = totp_cli_key_to_pin_code(c3);
59+
if(code > 0) {
5560
pin[*pin_length] = code;
5661
*pin_length = *pin_length + 1;
5762
putc('*', stdout);
@@ -63,7 +68,7 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
6368
TOTP_CLI_PRINTF("Cancelled by user\r\n");
6469
return false;
6570
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
66-
if (*pin_length > 0) {
71+
if(*pin_length > 0) {
6772
*pin_length = *pin_length - 1;
6873
pin[*pin_length] = 0;
6974
TOTP_CLI_DELETE_LAST_CHAR();
@@ -85,36 +90,32 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
8590
bool do_change = false;
8691
bool do_remove = false;
8792
UNUSED(do_remove);
88-
do {
89-
if (!args_read_string_and_trim(args, temp_str)) {
90-
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
91-
break;
92-
}
93-
94-
if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_SET) == 0) {
93+
if(args_read_string_and_trim(args, temp_str)) {
94+
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_SET) == 0) {
9595
do_change = true;
96-
} else if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) == 0) {
96+
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) == 0) {
9797
do_remove = true;
9898
} else {
9999
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
100-
break;
101100
}
102-
} while (false);
101+
} else {
102+
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
103+
}
103104

104-
if ((do_change || do_remove) && totp_cli_ensure_authenticated(plugin_state, cli)) {
105+
if((do_change || do_remove) && totp_cli_ensure_authenticated(plugin_state, cli)) {
105106
bool load_generate_token_scene = false;
106107
do {
107108
uint8_t old_iv[TOTP_IV_SIZE];
108109
memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
109110
uint8_t new_pin[TOTP_IV_SIZE];
110111
uint8_t new_pin_length = 0;
111-
if (do_change) {
112-
if (!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length) ||
113-
!totp_cli_ensure_authenticated(plugin_state, cli)) {
112+
if(do_change) {
113+
if(!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length) ||
114+
!totp_cli_ensure_authenticated(plugin_state, cli)) {
114115
memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
115116
break;
116117
}
117-
} else if (do_remove) {
118+
} else if(do_remove) {
118119
new_pin_length = 0;
119120
memset(&new_pin[0], 0, TOTP_IV_SIZE);
120121
}
@@ -128,34 +129,39 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
128129

129130
memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
130131
memset(&plugin_state->base_iv[0], 0, TOTP_IV_SIZE);
131-
if (plugin_state->crypto_verify_data != NULL) {
132+
if(plugin_state->crypto_verify_data != NULL) {
132133
free(plugin_state->crypto_verify_data);
133134
plugin_state->crypto_verify_data = NULL;
134135
}
135136

136-
totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? &new_pin[0] : NULL, new_pin_length);
137-
ListNode* node = plugin_state->tokens_list;
138-
while (node != NULL) {
137+
totp_crypto_seed_iv(
138+
plugin_state, new_pin_length > 0 ? &new_pin[0] : NULL, new_pin_length);
139+
140+
TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
139141
TokenInfo* token_info = node->data;
140-
size_t plain_token_length;
141-
uint8_t* plain_token = totp_crypto_decrypt(token_info->token, token_info->token_length, &old_iv[0], &plain_token_length);
142+
size_t plain_token_length;
143+
uint8_t* plain_token = totp_crypto_decrypt(
144+
token_info->token, token_info->token_length, &old_iv[0], &plain_token_length);
142145
free(token_info->token);
143-
token_info->token = totp_crypto_encrypt(plain_token, plain_token_length, &plugin_state->iv[0], &token_info->token_length);
146+
token_info->token = totp_crypto_encrypt(
147+
plain_token,
148+
plain_token_length,
149+
&plugin_state->iv[0],
150+
&token_info->token_length);
144151
memset_s(plain_token, plain_token_length, 0, plain_token_length);
145152
free(plain_token);
146-
node = node->next;
147-
}
153+
});
148154

149155
totp_full_save_config_file(plugin_state);
150156

151157
TOTP_CLI_DELETE_LAST_LINE();
152158

153-
if (do_change) {
159+
if(do_change) {
154160
TOTP_CLI_PRINTF("PIN has been successfully changed\r\n");
155-
} else if (do_remove) {
161+
} else if(do_remove) {
156162
TOTP_CLI_PRINTF("PIN has been successfully removed\r\n");
157163
}
158-
} while (false);
164+
} while(false);
159165

160166
if(load_generate_token_scene) {
161167
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);

totp/lib/list/list.h

+8
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,11 @@ void list_free(ListNode* head);
9292
assert(list_add(head, item) != NULL); \
9393
} \
9494
} while(false)
95+
96+
#define TOTP_LIST_FOREACH(head, node, action) \
97+
do { \
98+
ListNode* node = head; \
99+
while(node != NULL) { \
100+
action node = node->next; \
101+
} \
102+
} while(false)

totp/services/config/config.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,11 @@ void totp_full_save_config_file(const PluginState* const plugin_state) {
213213
flipper_format_write_float(
214214
fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
215215
flipper_format_write_bool(fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1);
216-
ListNode* node = plugin_state->tokens_list;
217-
while(node != NULL) {
216+
217+
TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
218218
const TokenInfo* token_info = node->data;
219219
totp_config_file_save_new_token_i(fff_data_file, token_info);
220-
node = node->next;
221-
}
220+
});
222221

223222
totp_close_config_file(fff_data_file);
224223
totp_close_storage();

totp/ui/scenes/generate_token/totp_scene_generate_token.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
196196
->data);
197197

198198
if(tokenInfo->token != NULL && tokenInfo->token_length > 0) {
199-
furi_mutex_acquire(scene_state->type_code_worker_context->string_sync, FuriWaitForever);
199+
furi_mutex_acquire(
200+
scene_state->type_code_worker_context->string_sync, FuriWaitForever);
200201
size_t key_length;
201202
uint8_t* key = totp_crypto_decrypt(
202203
tokenInfo->token, tokenInfo->token_length, &plugin_state->iv[0], &key_length);
@@ -215,7 +216,8 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
215216
memset_s(key, key_length, 0, key_length);
216217
free(key);
217218
} else {
218-
furi_mutex_acquire(scene_state->type_code_worker_context->string_sync, FuriWaitForever);
219+
furi_mutex_acquire(
220+
scene_state->type_code_worker_context->string_sync, FuriWaitForever);
219221
i_token_to_str(0, scene_state->last_code, tokenInfo->digits);
220222
}
221223

@@ -288,7 +290,8 @@ bool totp_scene_generate_token_handle_event(
288290
SceneState* scene_state;
289291
if(event->input.type == InputTypeLong && event->input.key == InputKeyDown) {
290292
scene_state = (SceneState*)plugin_state->current_scene_state;
291-
totp_type_code_worker_notify(scene_state->type_code_worker_context, TotpTypeCodeWorkerEvtType);
293+
totp_type_code_worker_notify(
294+
scene_state->type_code_worker_context, TotpTypeCodeWorkerEvtType);
292295
notification_message(plugin_state->notification, &notification_sequence_badusb);
293296
return true;
294297
}

totp/workers/type_code/type_code.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ static int32_t totp_type_code_worker_callback(void* context) {
6363

6464
while(true) {
6565
uint32_t flags = furi_thread_flags_wait(
66-
TotpTypeCodeWorkerEvtStop | TotpTypeCodeWorkerEvtType, FuriFlagWaitAny, FuriWaitForever);
66+
TotpTypeCodeWorkerEvtStop | TotpTypeCodeWorkerEvtType,
67+
FuriFlagWaitAny,
68+
FuriWaitForever);
6769
furi_check((flags & FuriFlagError) == 0); //-V562
6870
if(flags & TotpTypeCodeWorkerEvtStop) break;
6971

@@ -104,7 +106,9 @@ void totp_type_code_worker_stop(TotpTypeCodeWorkerContext* context) {
104106
free(context);
105107
}
106108

107-
void totp_type_code_worker_notify(TotpTypeCodeWorkerContext* context, TotpTypeCodeWorkerEvtFlags event) {
109+
void totp_type_code_worker_notify(
110+
TotpTypeCodeWorkerContext* context,
111+
TotpTypeCodeWorkerEvtFlags event) {
108112
furi_assert(context != NULL);
109113
furi_thread_flags_set(furi_thread_get_id(context->thread), event);
110114
}

totp/workers/type_code/type_code.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ typedef enum {
2020

2121
TotpTypeCodeWorkerContext* totp_type_code_worker_start();
2222
void totp_type_code_worker_stop(TotpTypeCodeWorkerContext* context);
23-
void totp_type_code_worker_notify(TotpTypeCodeWorkerContext* context, TotpTypeCodeWorkerEvtFlags event);
23+
void totp_type_code_worker_notify(
24+
TotpTypeCodeWorkerContext* context,
25+
TotpTypeCodeWorkerEvtFlags event);

0 commit comments

Comments
 (0)