Alexander Kopachov 3 лет назад
Родитель
Сommit
fbb9d22e21

+ 1 - 2
cli/cli.c

@@ -50,8 +50,7 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
         furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
         furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
         totp_cli_command_move_handle(plugin_state, args, cli);
-    } else if(
-        furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
+    } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
         totp_cli_command_pin_handle(plugin_state, args, cli);
     } else {
         totp_cli_print_unknown_command(cmd);

+ 2 - 5
cli/commands/list/list.c

@@ -53,13 +53,11 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
         return;
     }
 
-    ListNode* node = plugin_state->tokens_list;
-
     TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
     TOTP_CLI_PRINTF("| %-*s | %-*s | %-*s | %-s |\r\n", 3, "#", 27, "Name", 6, "Algo", "Digits");
     TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
     uint16_t index = 1;
-    while(node != NULL) {
+    TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
         TokenInfo* token_info = (TokenInfo*)node->data;
         token_info_get_digits_count(token_info);
         TOTP_CLI_PRINTF(
@@ -68,8 +66,7 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
             token_info->name,
             get_algo_as_cstr(token_info->algo),
             get_digits_as_int(token_info->digits));
-        node = node->next;
         index++;
-    }
+    });
     TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
 }

+ 57 - 51
cli/commands/pin/pin.c

@@ -18,7 +18,30 @@ void totp_cli_command_pin_docopt_commands() {
 }
 
 void totp_cli_command_pin_docopt_usage() {
-    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");
+    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");
+}
+
+static inline uint8_t totp_cli_key_to_pin_code(uint8_t key) {
+    uint8_t code = 0;
+    switch(key) {
+    case 0x44: // left
+        code = PinCodeArrowLeft;
+        break;
+    case 0x41: // up
+        code = PinCodeArrowUp;
+        break;
+    case 0x43: // right
+        code = PinCodeArrowRight;
+        break;
+    case 0x42: // down
+        code = PinCodeArrowDown;
+        break;
+    default:
+        break;
+    }
+
+    return code;
 }
 
 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) {
         if(c == CliSymbolAsciiEsc) {
             uint8_t c2;
             uint8_t c3;
-            if (cli_read_timeout(cli, &c2, 1, 0) == 1 &&
-                cli_read_timeout(cli, &c3, 1, 0) == 1 &&
-                c2 == 0x5b) {
-                uint8_t code = 0;
-                switch (c3) {
-                    case 0x44: // left
-                        code = PinCodeArrowLeft;
-                        break;
-                    case 0x41: // up
-                        code = PinCodeArrowUp;
-                        break;
-                    case 0x43: // right
-                        code = PinCodeArrowRight;
-                        break;
-                    case 0x42: // down
-                        code = PinCodeArrowDown;
-                        break;
-                    default:
-                    break;
-                }
-
-                if (code > 0) {
+            if(cli_read_timeout(cli, &c2, 1, 0) == 1 && cli_read_timeout(cli, &c3, 1, 0) == 1 &&
+               c2 == 0x5b) {
+                uint8_t code = totp_cli_key_to_pin_code(c3);
+                if(code > 0) {
                     pin[*pin_length] = code;
                     *pin_length = *pin_length + 1;
                     putc('*', stdout);
@@ -63,7 +68,7 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
             TOTP_CLI_PRINTF("Cancelled by user\r\n");
             return false;
         } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
-            if (*pin_length > 0) {
+            if(*pin_length > 0) {
                 *pin_length = *pin_length - 1;
                 pin[*pin_length] = 0;
                 TOTP_CLI_DELETE_LAST_CHAR();
@@ -85,36 +90,32 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
     bool do_change = false;
     bool do_remove = false;
     UNUSED(do_remove);
-    do {
-        if (!args_read_string_and_trim(args, temp_str)) {
-            TOTP_CLI_PRINT_INVALID_ARGUMENTS();
-            break;
-        }
-
-        if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_SET) == 0) {
+    if(args_read_string_and_trim(args, temp_str)) {
+        if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_SET) == 0) {
             do_change = true;
-        } else if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) == 0) {
+        } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) == 0) {
             do_remove = true;
         } else {
             TOTP_CLI_PRINT_INVALID_ARGUMENTS();
-            break;
         }
-    } while (false);
+    } else {
+        TOTP_CLI_PRINT_INVALID_ARGUMENTS();
+    }
 
-    if ((do_change || do_remove) && totp_cli_ensure_authenticated(plugin_state, cli)) {
+    if((do_change || do_remove) && totp_cli_ensure_authenticated(plugin_state, cli)) {
         bool load_generate_token_scene = false;
         do {
             uint8_t old_iv[TOTP_IV_SIZE];
             memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
             uint8_t new_pin[TOTP_IV_SIZE];
             uint8_t new_pin_length = 0;
-            if (do_change) {
-                if (!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length) ||
-                    !totp_cli_ensure_authenticated(plugin_state, cli)) {
+            if(do_change) {
+                if(!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length) ||
+                   !totp_cli_ensure_authenticated(plugin_state, cli)) {
                     memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
                     break;
                 }
-            } else if (do_remove) {
+            } else if(do_remove) {
                 new_pin_length = 0;
                 memset(&new_pin[0], 0, TOTP_IV_SIZE);
             }
@@ -128,34 +129,39 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
 
             memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
             memset(&plugin_state->base_iv[0], 0, TOTP_IV_SIZE);
-            if (plugin_state->crypto_verify_data != NULL) {
+            if(plugin_state->crypto_verify_data != NULL) {
                 free(plugin_state->crypto_verify_data);
                 plugin_state->crypto_verify_data = NULL;
             }
 
-            totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? &new_pin[0] : NULL, new_pin_length);
-            ListNode* node = plugin_state->tokens_list;
-            while (node != NULL) {
+            totp_crypto_seed_iv(
+                plugin_state, new_pin_length > 0 ? &new_pin[0] : NULL, new_pin_length);
+
+            TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
                 TokenInfo* token_info = node->data;
-                size_t plain_token_length; 
-                uint8_t* plain_token = totp_crypto_decrypt(token_info->token, token_info->token_length, &old_iv[0], &plain_token_length);
+                size_t plain_token_length;
+                uint8_t* plain_token = totp_crypto_decrypt(
+                    token_info->token, token_info->token_length, &old_iv[0], &plain_token_length);
                 free(token_info->token);
-                token_info->token = totp_crypto_encrypt(plain_token, plain_token_length, &plugin_state->iv[0], &token_info->token_length);
+                token_info->token = totp_crypto_encrypt(
+                    plain_token,
+                    plain_token_length,
+                    &plugin_state->iv[0],
+                    &token_info->token_length);
                 memset_s(plain_token, plain_token_length, 0, plain_token_length);
                 free(plain_token);
-                node = node->next;
-            }
+            });
 
             totp_full_save_config_file(plugin_state);
 
             TOTP_CLI_DELETE_LAST_LINE();
 
-            if (do_change) {
+            if(do_change) {
                 TOTP_CLI_PRINTF("PIN has been successfully changed\r\n");
-            } else if (do_remove) {
+            } else if(do_remove) {
                 TOTP_CLI_PRINTF("PIN has been successfully removed\r\n");
             }
-        } while (false);
+        } while(false);
 
         if(load_generate_token_scene) {
             totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);

+ 8 - 0
lib/list/list.h

@@ -92,3 +92,11 @@ void list_free(ListNode* head);
             assert(list_add(head, item) != NULL); \
         }                                         \
     } while(false)
+
+#define TOTP_LIST_FOREACH(head, node, action) \
+    do {                                      \
+        ListNode* node = head;                \
+        while(node != NULL) {                 \
+            action node = node->next;         \
+        }                                     \
+    } while(false)

+ 3 - 4
services/config/config.c

@@ -213,12 +213,11 @@ void totp_full_save_config_file(const PluginState* const plugin_state) {
     flipper_format_write_float(
         fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
     flipper_format_write_bool(fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1);
-    ListNode* node = plugin_state->tokens_list;
-    while(node != NULL) {
+
+    TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
         const TokenInfo* token_info = node->data;
         totp_config_file_save_new_token_i(fff_data_file, token_info);
-        node = node->next;
-    }
+    });
 
     totp_close_config_file(fff_data_file);
     totp_close_storage();

+ 6 - 3
ui/scenes/generate_token/totp_scene_generate_token.c

@@ -196,7 +196,8 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
                              ->data);
 
         if(tokenInfo->token != NULL && tokenInfo->token_length > 0) {
-            furi_mutex_acquire(scene_state->type_code_worker_context->string_sync, FuriWaitForever);
+            furi_mutex_acquire(
+                scene_state->type_code_worker_context->string_sync, FuriWaitForever);
             size_t key_length;
             uint8_t* key = totp_crypto_decrypt(
                 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_
             memset_s(key, key_length, 0, key_length);
             free(key);
         } else {
-            furi_mutex_acquire(scene_state->type_code_worker_context->string_sync, FuriWaitForever);
+            furi_mutex_acquire(
+                scene_state->type_code_worker_context->string_sync, FuriWaitForever);
             i_token_to_str(0, scene_state->last_code, tokenInfo->digits);
         }
 
@@ -288,7 +290,8 @@ bool totp_scene_generate_token_handle_event(
     SceneState* scene_state;
     if(event->input.type == InputTypeLong && event->input.key == InputKeyDown) {
         scene_state = (SceneState*)plugin_state->current_scene_state;
-        totp_type_code_worker_notify(scene_state->type_code_worker_context, TotpTypeCodeWorkerEvtType);
+        totp_type_code_worker_notify(
+            scene_state->type_code_worker_context, TotpTypeCodeWorkerEvtType);
         notification_message(plugin_state->notification, &notification_sequence_badusb);
         return true;
     }

+ 6 - 2
workers/type_code/type_code.c

@@ -63,7 +63,9 @@ static int32_t totp_type_code_worker_callback(void* context) {
 
     while(true) {
         uint32_t flags = furi_thread_flags_wait(
-            TotpTypeCodeWorkerEvtStop | TotpTypeCodeWorkerEvtType, FuriFlagWaitAny, FuriWaitForever);
+            TotpTypeCodeWorkerEvtStop | TotpTypeCodeWorkerEvtType,
+            FuriFlagWaitAny,
+            FuriWaitForever);
         furi_check((flags & FuriFlagError) == 0); //-V562
         if(flags & TotpTypeCodeWorkerEvtStop) break;
 
@@ -104,7 +106,9 @@ void totp_type_code_worker_stop(TotpTypeCodeWorkerContext* context) {
     free(context);
 }
 
-void totp_type_code_worker_notify(TotpTypeCodeWorkerContext* context, TotpTypeCodeWorkerEvtFlags event) {
+void totp_type_code_worker_notify(
+    TotpTypeCodeWorkerContext* context,
+    TotpTypeCodeWorkerEvtFlags event) {
     furi_assert(context != NULL);
     furi_thread_flags_set(furi_thread_get_id(context->thread), event);
 }

+ 3 - 1
workers/type_code/type_code.h

@@ -20,4 +20,6 @@ typedef enum {
 
 TotpTypeCodeWorkerContext* totp_type_code_worker_start();
 void totp_type_code_worker_stop(TotpTypeCodeWorkerContext* context);
-void totp_type_code_worker_notify(TotpTypeCodeWorkerContext* context, TotpTypeCodeWorkerEvtFlags event);
+void totp_type_code_worker_notify(
+    TotpTypeCodeWorkerContext* context,
+    TotpTypeCodeWorkerEvtFlags event);