This commit is contained in:
Fulton Browne 2021-05-15 00:33:11 +00:00
parent 88450878b4
commit 2e82773e3f
3 changed files with 15 additions and 16 deletions

18
b64f.c
View File

@ -1,12 +1,12 @@
#include <u.h>
#include <libc.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h" #include "base64.h"
int lower(int a); int lower(int a);
int main(int argc,char** argv) { void main(int argc,char** argv) {
char opt = ' '; char opt = ' ';
if (argc > 1) if (argc > 1)
@ -23,14 +23,14 @@ int main(int argc,char** argv) {
printf("\tUse the following to decode:\n\t%s b(ase64) IN_Base64\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..."); puts("\nERROR: insufficient or incorrect parameters...");
return 1; exits("1");
} }
int bcoded = 0; int bcoded = 0;
switch(opt) { switch(opt) {
case 'd': case 'd':
puts("\nDECODING"); puts("\nDECODING");
bcoded = b64_decodef(argv[2],argv[3]); bcoded = b64_decode(argv[2],argv[3]);
break; break;
case 'e': case 'e':
puts("\nENCODING"); puts("\nENCODING");
@ -43,7 +43,7 @@ int main(int argc,char** argv) {
char *b64 = (char *)malloc(1 + (sizeof(char) * b64e_size(tlen))); char *b64 = (char *)malloc(1 + (sizeof(char) * b64e_size(tlen)));
if (b64 == NULL) { if (b64 == NULL) {
printf("Error: cannot allocate memory for output\n"); printf("Error: cannot allocate memory for output\n");
exit(1); /* End program with error status. */ exits("1"); /* End program with error status. */
} }
bcoded = b64_encode(argv[2],tlen,b64); bcoded = b64_encode(argv[2],tlen,b64);
@ -57,10 +57,10 @@ int main(int argc,char** argv) {
char *txt = malloc(1 + (sizeof(char) * b64d_size(blen))); char *txt = malloc(1 + (sizeof(char) * b64d_size(blen)));
if (txt == NULL) { if (txt == NULL) {
printf("Error: cannot allocate memory for output\n"); printf("Error: cannot allocate memory for output\n");
exit(1); /* End program with error status. */ exits("1"); /* End program with error status. */
} }
bcoded = b64_decode(argv[2],blen,txt); //bcoded = b64_decode(argv[2],blen,txt);
txt[bcoded] = '\0'; txt[bcoded] = '\0';
printf("Decoded text from base64: %s", txt); printf("Decoded text from base64: %s", txt);
free(txt); free(txt);
@ -71,8 +71,6 @@ int main(int argc,char** argv) {
} }
printf("\nBytes encoded/decoded: %i\n",bcoded); printf("\nBytes encoded/decoded: %i\n",bcoded);
return 0;
} }
int lower(int a) { //https://stackoverflow.com/a/15709023/883015 int lower(int a) { //https://stackoverflow.com/a/15709023/883015

View File

@ -7,7 +7,9 @@
Thank you for inspiration: Thank you for inspiration:
http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode
*/ */
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include "base64.h" #include "base64.h"
//Base64 char table - used internally for encoding //Base64 char table - used internally for encoding

View File

@ -1,4 +1,4 @@
/* /*
base64.c - by Joe DF (joedf@ahkscript.org) base64.c - by Joe DF (joedf@ahkscript.org)
Released under the MIT License Released under the MIT License
@ -8,7 +8,6 @@
http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode
*/ */
#include <stdio.h>
//Base64 char table function - used internally for decoding //Base64 char table function - used internally for decoding
unsigned int b64_int(unsigned int ch); unsigned int b64_int(unsigned int ch);
@ -25,13 +24,13 @@ unsigned int b64d_size(unsigned int in_size);
// in_len : number of bytes to be encoded. // in_len : number of bytes to be encoded.
// out : pointer to buffer with enough memory, user is responsible for memory allocation, receives null-terminated string // out : pointer to buffer with enough memory, user is responsible for memory allocation, receives null-terminated string
// returns size of output including null byte // returns size of output including null byte
unsigned int b64_encode(const unsigned char* in, unsigned int in_len, unsigned char* out); unsigned int b64_encode(unsigned char* in, unsigned int in_len, unsigned char* out);
// in : buffer of base64 string to be decoded. // in : buffer of base64 string to be decoded.
// in_len : number of bytes to be decoded. // in_len : number of bytes to be decoded.
// out : pointer to buffer with enough memory, user is responsible for memory allocation, receives "raw" binary // out : pointer to buffer with enough memory, user is responsible for memory allocation, receives "raw" binary
// returns size of output excluding null byte // returns size of output excluding null byte
unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned char* out); unsigned int b64_decode(unsigned char* in, unsigned int in_len, unsigned char* out);
// file-version b64_encode // file-version b64_encode
// Input : filenames // Input : filenames