From 916030dbf8ec72230192b5f9b7d80ce047dfc68d Mon Sep 17 00:00:00 2001 From: Joe DF Date: Thu, 7 Mar 2019 17:01:24 -0500 Subject: update to b64f tool, added textmode, needs fixing types to work... --- .gitignore | 2 ++ b64f.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ccf166a..6b5a4f3 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,5 @@ data.7z data.7z.b64 picture.og.png data.7z.b64.deco +picture.b64.txt +picture.b64.png diff --git a/b64f.c b/b64f.c index 4e8399e..9904720 100644 --- a/b64f.c +++ b/b64f.c @@ -1,4 +1,6 @@ #include +#include +#include #include "base64.h" @@ -6,15 +8,24 @@ int lower(int a); int main(int argc,char** argv) { - puts("\nbase64.c [Encode/Decode]"); - puts("------------------------------------"); - printf("Use the following to encode:\n\t%s e(ncode) IN_filepath OUT_filepath\n",argv[0]); - printf("Use the following to decode:\n\t%s d(ecode) IN_filepath OUT_filepath\n",argv[0]); - if (argc < 4) { - puts("\nERROR: not enough parameters..."); + char opt = ' '; + if (argc > 1) + opt = lower(argv[1][0]); + + if ( ((argc < 4) && (opt!='b' && opt!='t')) ) { + puts("\nbase64.c [Encode/Decode]"); + puts("------------------------------------"); + printf("File mode\n"); + printf("\tUse the following to encode:\n\t%s e(ncode) IN_filepath OUT_filepath\n",argv[0]); + printf("\tUse the following to decode:\n\t%s d(ecode) IN_filepath OUT_filepath\n",argv[0]); + printf("\nText mode (outputs to stdout):\n"); + printf("\tUse the following to encode:\n\t%s t(ext) IN_Text\n",argv[0]); + printf("\tUse the following to decode:\n\t%s b(ase64) IN_Base64\n",argv[0]); + + puts("\nERROR: insufficient or incorrect parameters..."); return 1; } - char opt = lower(argv[1][0]); + int bcoded = 0; switch(opt) { case 'd': @@ -25,6 +36,35 @@ int main(int argc,char** argv) { puts("\nENCODING"); bcoded = b64_encodef(argv[2],argv[3]); break; + case 't': + puts("\nENCODING from text to base64"); + + int tlen = strlen(argv[2]); + char *b64 = (char *)malloc(1 + (sizeof(char) * b64e_size(tlen))); + if (b64 == NULL) { + printf("Error: cannot allocate memory for output\n"); + exit(1); /* End program with error status. */ + } + + bcoded = b64_encode(argv[2],tlen,b64); + printf("Encoded base64 from text: %s", b64); + free(b64); + break; + case 'b': + puts("\nDECODING from base64 to text"); + + int blen = strlen(argv[2]); + char *txt = malloc(1 + (sizeof(char) * b64d_size(blen))); + if (txt == NULL) { + printf("Error: cannot allocate memory for output\n"); + exit(1); /* End program with error status. */ + } + + bcoded = b64_decode(argv[2],blen,txt); + txt[blen] = '\0'; + printf("Decoded text from base64: %s", txt); + free(txt); + break; default: puts("\nINVALID OPTION"); bcoded = -1; -- cgit 1.4.1-2-gfad0