diff --git a/filesystem/filesystem.c b/filesystem/filesystem.c index e67cb8ccd..4aef4d060 100644 --- a/filesystem/filesystem.c +++ b/filesystem/filesystem.c @@ -318,6 +318,26 @@ int filesystem_cmd_b64encode(int argc, char *argv[]) { return 0; } +int filesystem_cmd_b64decode(int argc, char *argv[]) { + (void) argc; + char *buf = malloc(strlen(argv[1]) * 1.5 + 1); + unsigned int decoded_size = b64_decode((unsigned char *)argv[1], strlen(argv[1]), (unsigned char *)buf); + if (!decoded_size) { + printf("No bytes decoded.\r\n"); + return 0; + } + if (!strcmp(argv[2], ">>")) { + if (filesystem_file_exists(argv[3])) { + filesystem_append_file(argv[3], buf, decoded_size); + } else { + printf("b64decode: %s: No such file\r\n", argv[3]); + } + } else if (!strcmp(argv[2], ">")) { + filesystem_write_file(argv[3], buf, decoded_size); + } + return 0; +} + int filesystem_cmd_df(int argc, char *argv[]) { (void) argc; (void) argv; diff --git a/filesystem/filesystem.h b/filesystem/filesystem.h index 111f3f3b0..f48a57c32 100644 --- a/filesystem/filesystem.h +++ b/filesystem/filesystem.h @@ -99,6 +99,7 @@ bool filesystem_append_file(char *filename, char *text, int32_t length); int filesystem_cmd_ls(int argc, char *argv[]); int filesystem_cmd_cat(int argc, char *argv[]); int filesystem_cmd_b64encode(int argc, char *argv[]); +int filesystem_cmd_b64decode(int argc, char *argv[]); int filesystem_cmd_df(int argc, char *argv[]); int filesystem_cmd_rm(int argc, char *argv[]); int filesystem_cmd_format(int argc, char *argv[]); diff --git a/shell/shell_cmd_list.c b/shell/shell_cmd_list.c index b83460f4e..554dc45ae 100644 --- a/shell/shell_cmd_list.c +++ b/shell/shell_cmd_list.c @@ -79,6 +79,13 @@ shell_command_t g_shell_commands[] = { .max_args = 1, .cb = filesystem_cmd_b64encode, }, + { + .name = "b64decode", + .help = "usage: b64decode {>, >>} ", + .min_args = 3, + .max_args = 3, + .cb = filesystem_cmd_b64decode, + }, { .name = "df", .help = "print filesystem free space",