about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFulton Browne <git@fulton.software>2021-05-15 21:16:20 +0000
committerFulton Browne <git@fulton.software>2021-05-15 21:16:20 +0000
commita7354ac7c6e4ddcf36e3d60851c98caa4e2d7dc1 (patch)
treed886bc906c3d3b5797fc62e694cb3b76fa0add73
parent2e82773e3f043ce07469425225185fb17def312c (diff)
downloadbase64-a7354ac7c6e4ddcf36e3d60851c98caa4e2d7dc1.tar.gz
added plan9 encode
-rw-r--r--BuildRun.bat20
-rw-r--r--b64f.c8
-rw-r--r--base64.c105
-rw-r--r--base64.h14
-rw-r--r--base64encode.c56
-rwxr-xr-xbuildrun.sh22
-rw-r--r--test.c288
7 files changed, 69 insertions, 444 deletions
diff --git a/BuildRun.bat b/BuildRun.bat
deleted file mode 100644
index 9e9697f..0000000
--- a/BuildRun.bat
+++ /dev/null
@@ -1,20 +0,0 @@
-@echo off
-mkdir bin 2>NUL
-cls
-echo compiling...
-mkdir bin 2>nul
-gcc test.c base64.c -o bin\test.exe
-gcc b64f.c base64.c -s -o bin\b64f.exe
-echo running...
-bin\test.exe
-echo.
-echo ----------------[ Testing file - encoding/decoding ]----------------
-echo Encoding test image "picture.png" to "picture.b64.txt"...
-bin\b64f.exe e picture.png picture.b64.txt
-echo.
-echo Decoding test image from "picture.b64.txt" to "picture.b64.png"...
-bin\b64f.exe d picture.b64.txt picture.b64.png
-echo.
-echo Done.
-echo See files manually if the programmed works correctly.
-pause
\ No newline at end of file
diff --git a/b64f.c b/b64f.c
index aa17963..0a66bcf 100644
--- a/b64f.c
+++ b/b64f.c
@@ -6,9 +6,11 @@
 
 int lower(int a);
 
-void main(int argc,char** argv) {
+void
+main(int argc,char** argv)
+{
 	
-	char opt = ' ';
+	char 2 = ' ';
 	if (argc > 1)
 		opt = lower(argv[1][0]);
 	
@@ -40,7 +42,7 @@ void main(int argc,char** argv) {
 			puts("\nENCODING from text to base64");
 			
 				int tlen = strlen(argv[2]);
-				char *b64 = (char *)malloc(1 + (sizeof(char) * b64e_size(tlen)));
+				char *b64 = (char *)malloc(1 + ((unsigned)(sizeof(char) * b64e_size(tlen)));
 				if (b64 == NULL) {
 					printf("Error: cannot allocate memory for output\n");
 					exits("1");  /* End program with error status. */
diff --git a/base64.c b/base64.c
index 8a57246..253bf4e 100644
--- a/base64.c
+++ b/base64.c
@@ -7,9 +7,6 @@
 	Thank you for inspiration:
 	http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode
 */
-#include <u.h>
-#include <libc.h>
-#include <stdio.h>
 #include "base64.h"
 
 //Base64 char table - used internally for encoding
@@ -55,13 +52,14 @@ unsigned int b64d_size(unsigned int in_size) {
 	return ((3*in_size)/4);
 }
 
-unsigned int b64_encode(const unsigned char* in, unsigned int in_len, unsigned char* out) {
+unsigned int 
+b64_encode(char* in, unsigned int in_len, char* out){
 
 	unsigned int i=0, j=0, k=0, s[3];
 	
-	for (i=0;i<in_len;i++) {
+	for (i=0;i<in_len;i++){
 		s[j++]=*(in+i);
-		if (j==3) {
+		if (j==3){
 			out[k+0] = b64_chr[ (s[0]&255)>>2 ];
 			out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ];
 			out[k+2] = b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ];
@@ -88,7 +86,8 @@ unsigned int b64_encode(const unsigned char* in, unsigned int in_len, unsigned c
 	return k;
 }
 
-unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned char* out) {
+unsigned int
+b64_decode(char* in, unsigned int in_len, char* out){
 
 	unsigned int i=0, j=0, k=0, s[4];
 	
@@ -112,95 +111,3 @@ unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned c
 	
 	return k;
 }
-
-unsigned int b64_encodef(char *InFile, char *OutFile) {
-
-	FILE *pInFile = fopen(InFile,"rb");
-	FILE *pOutFile = fopen(OutFile,"wb");
-	
-	unsigned int i=0;
-	unsigned int j=0;
-	unsigned int c=0;
-	unsigned int s[4];
-	
-	if ((pInFile==NULL) || (pOutFile==NULL) ) {
-		if (pInFile!=NULL){fclose(pInFile);}
-		if (pOutFile!=NULL){fclose(pOutFile);}
-		return 0;
-	}
-	
-	while(c!=EOF) {
-		c=fgetc(pInFile);
-		if (c==EOF)
-		break;
-		s[j++]=c;
-		if (j==3) {
-			fputc(b64_chr[ (s[0]&255)>>2 ],pOutFile);
-			fputc(b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ],pOutFile);
-			fputc(b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ],pOutFile);
-			fputc(b64_chr[ s[2]&0x3F ],pOutFile);
-			j=0; i+=4;
-		}
-	}
-	
-	if (j) {
-		if (j==1)
-			s[1] = 0;
-		fputc(b64_chr[ (s[0]&255)>>2 ],pOutFile);
-		fputc(b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ],pOutFile);
-		if (j==2)
-			fputc(b64_chr[ ((s[1]&0x0F)<<2) ],pOutFile);
-		else
-			fputc('=',pOutFile);
-		fputc('=',pOutFile);
-		i+=4;
-	}
-	
-	fclose(pInFile);
-	fclose(pOutFile);
-	
-	return i;
-}
-
-unsigned int b64_decodef(char *InFile, char *OutFile) {
-
-	FILE *pInFile = fopen(InFile,"rb");
-	FILE *pOutFile = fopen(OutFile,"wb");
-	
-	unsigned int c=0;
-	unsigned int j=0;
-	unsigned int k=0;
-	unsigned int s[4];
-	
-	if ((pInFile==NULL) || (pOutFile==NULL) ) {
-		if (pInFile!=NULL){fclose(pInFile);}
-		if (pOutFile!=NULL){fclose(pOutFile);}
-		return 0;
-	}
-	
-	while(c!=EOF) {
-		c=fgetc(pInFile);
-		if (c==EOF)
-		break;
-		s[j++]=b64_int(c);
-		if (j==4) {
-			fputc(((s[0]&255)<<2)+((s[1]&0x30)>>4),pOutFile);
-			if (s[2]!=64) {
-				fputc(((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2),pOutFile);
-				if ((s[3]!=64)) {
-					fputc(((s[2]&0x03)<<6)+(s[3]),pOutFile); k+=3;
-				} else {
-					k+=2;
-				}
-			} else {
-				k+=1;
-			}
-			j=0;
-		}
-	}
-
-	fclose(pInFile);
-	fclose(pOutFile);
-	
-	return k;
-}
diff --git a/base64.h b/base64.h
index eb0dfbb..7f7e319 100644
--- a/base64.h
+++ b/base64.h
@@ -24,20 +24,10 @@ unsigned int b64d_size(unsigned int in_size);
 // 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
 // returns size of output including null byte
-unsigned int b64_encode(unsigned char* in, unsigned int in_len, unsigned char* out);
+unsigned int b64_encode(char* in, unsigned int in_len, char* out);
 
 // in : buffer of base64 string 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
 // returns size of output excluding null byte
-unsigned int b64_decode(unsigned char* in, unsigned int in_len, unsigned char* out);
-
-// file-version b64_encode
-// Input : filenames
-// returns size of output
-unsigned int b64_encodef(char *InFile, char *OutFile);
-
-// file-version b64_decode
-// Input : filenames
-// returns size of output
-unsigned int b64_decodef(char *InFile, char *OutFile);
+unsigned int b64_decode(char* in, unsigned int in_len, char* out);
diff --git a/base64encode.c b/base64encode.c
new file mode 100644
index 0000000..b6f0fe6
--- /dev/null
+++ b/base64encode.c
@@ -0,0 +1,56 @@
+#include <u.h>
+#include <libc.h>
+#include "base64.h"
+
+void
+usage(void)
+{
+	fprint(2, "base64encode [file]\n");
+	exits("usage");
+}
+
+static void 
+encode(int fd, char *name)
+{
+	int n;
+	long tot;
+	char *buf, *outbuf;
+	buf = nil;
+	tot = 0;
+	for(;;){
+		buf = realloc(buf, tot+8192);
+		if(buf == nil)
+			sysfatal("realloc: %r");
+		if((n = read(fd, buf+tot, 8192)) < 0)
+			sysfatal("read: %r");
+		if(n == 0)
+			break;
+		tot += n;
+	}
+	buf[tot] = 0;
+	outbuf =  malloc(1 + (sizeof(char) * b64d_size(strlen(buf))));
+	b64_encode(buf, strlen(buf), outbuf);
+	if((n=write(1,  outbuf, strlen(outbuf))) != strlen(outbuf))
+		sysfatal("writing bytes failed");
+}
+
+void
+main(int argc, char **argv)
+{
+	int fd;
+	char *file;
+	fd = 0;
+	file = "stdin";
+	ARGBEGIN{
+	default:
+		usage();
+	}ARGEND
+	if(argc != 0 && argc != 1)
+		usage();
+	if(argc == 1){
+		file = argv[0];
+ 		if((fd = open(file, OREAD)) < 0)
+			sysfatal("can'topen %s", file);
+	}
+	encode(fd, file);
+}
\ No newline at end of file
diff --git a/buildrun.sh b/buildrun.sh
deleted file mode 100755
index 4394f1e..0000000
--- a/buildrun.sh
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/bin/bash
-{ set +x; } 2>/dev/null
-mkdir bin 2>/dev/null
-clear
-echo compiling...
-gcc test.c base64.c -o bin/test
-gcc b64f.c base64.c -s -o bin/b64f
-echo running...
-bin/test
-echo
-echo ----------------[ Testing file - encoding/decoding ]----------------
-echo Encoding test image "picture.png" to "picture.b64.txt"...
-bin/b64f e picture.png picture.b64.txt
-echo
-echo Decoding test image from "picture.b64.txt" to "picture.b64.png"...
-bin/b64f d picture.b64.txt picture.b64.png
-echo Done.
-echo Check the files manually to see if the program works correctly.
-
-
-
-
diff --git a/test.c b/test.c
deleted file mode 100644
index b08b546..0000000
--- a/test.c
+++ /dev/null
@@ -1,288 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "base64.h"
-
-//Test values
-///////////////////////////////////////////////////////////////
-#define STRING_A "9TrJ"
-#define STRING_B "9TrJUQ=="
-#define STRING_C "9TrJUfA="
-
-#define HEXNUM_A {0xF5,0x3A,0xC9}
-#define HEXNUM_B {0xF5,0x3A,0xC9,0x51}
-#define HEXNUM_C {0xF5,0x3A,0xC9,0x51,0xF0}
-
-#define HEXSTR_A "0xF5 0x3A 0xC9"
-#define HEXSTR_B "0xF5 0x3A 0xC9 0x51"
-#define HEXSTR_C "0xF5 0x3A 0xC9 0x51 0xF0"
-
-#define TEXT_STR "test1234567"
-#define TEXT_B64 "dGVzdDEyMzQ1Njc="
-///////////////////////////////////////////////////////////////
-
-#define NELEMS(x)  (sizeof(x) / sizeof(x[0]))
-
-//#define STATUS(x)  ((x>0)?"[PASS]":"[FAIL]")
-#define STATUS(x)  score(x)
-#define PERCENT(a,b)  ((float)((float)a/(float)b)*100)
-
-int test_b64_encode();
-int test_b64_decode();
-int test_b64_encodef();
-int test_b64_decodef();
-int test_b64_text_encode();
-int test_b64_text_decode();
-
-int hexputs(const char* data, int len);
-int hexprint(const char* data, int len);
-int compare(char *a, char *b, int l);
-void deepCompare(int pass, char *a, char *b, int len);
-char *score(int x);
-
-int testScore = 0;
-int testTotal = 0;
-
-int main() {
-	
-	puts("\nbase64.c [Test Data]");
-	puts("------------------------------------");
-	printf("%s           : %s\n",HEXSTR_A,STRING_A);
-	printf("%s      : %s\n",HEXSTR_B,STRING_B);
-	printf("%s : %s\n",HEXSTR_C,STRING_C);
-	printf("%s              : %s\n",TEXT_STR,TEXT_B64);
-	puts("\nTesting b64_encode() ...\n");
-	test_b64_encode();
-	puts("\nTesting b64_decode() ...\n");
-	test_b64_decode();
-	puts("\nTesting test_b64_encodef() ...\n");
-	printf("%s\n",STATUS(test_b64_encodef()));
-	puts("\nTesting test_b64_decodef() ...\n");
-	printf("%s\n",STATUS(test_b64_decodef()));
-	puts("\nTesting test_b64_text_encode() ...\n");
-	test_b64_text_encode();
-	puts("\nTesting test_b64_text_decode() ...\n");
-	test_b64_text_decode();
-	puts("------------------------------------");
-	printf("\n[END] Test score: %g%% (%d/%d)\n",PERCENT(testScore,testTotal),testScore,testTotal);
-
-	return 0;
-}
-
-int test_b64_encode() {
-	
-	char test_a[] = HEXNUM_A;
-	char test_b[] = HEXNUM_B;
-	char test_c[] = HEXNUM_C;
-
-	int size_a = NELEMS(test_a);
-	int size_b = NELEMS(test_b);
-	int size_c = NELEMS(test_c);
-
-	int out_size_a = b64e_size(size_a) + 1;
-	int out_size_b = b64e_size(size_b) + 1;
-	int out_size_c = b64e_size(size_c) + 1;
-
-	unsigned char *out_a = malloc( (sizeof(char) * out_size_a) );
-	unsigned char *out_b = malloc( (sizeof(char) * out_size_b) );
-	unsigned char *out_c = malloc( (sizeof(char) * out_size_c) );
-	
-	out_size_a = b64_encode(test_a,size_a,out_a);
-	out_size_b = b64_encode(test_b,size_b,out_b);
-	out_size_c = b64_encode(test_c,size_c,out_c);
-
-	printf("\t%s\t%s\n",STATUS(strcmp(out_a,STRING_A)==0),out_a);
-	printf("\t%s\t%s\n",STATUS(strcmp(out_b,STRING_B)==0),out_b);
-	printf("\t%s\t%s\n",STATUS(strcmp(out_c,STRING_C)==0),out_c);
-	
-	free(out_a);
-	free(out_b);
-	free(out_c);
-	
-	return 0;
-}
-
-int test_b64_decode() {
-	
-	char test_a[] = STRING_A;
-	char test_b[] = STRING_B;
-	char test_c[] = STRING_C;
-
-	int len_a = strlen(test_a);
-	int len_b = strlen(test_b);
-	int len_c = strlen(test_c);
-
-	int out_size_a = b64d_size(len_a);
-	int out_size_b = b64d_size(len_b);
-	int out_size_c = b64d_size(len_c);
-
-	unsigned char *out_a = malloc( (sizeof(char) * out_size_a) +1);
-	unsigned char *out_b = malloc( (sizeof(char) * out_size_b) +1);
-	unsigned char *out_c = malloc( (sizeof(char) * out_size_c) +1);
-	
-	out_size_a = b64_decode(test_a,len_a,out_a);
-	out_size_b = b64_decode(test_b,len_b,out_b);
-	out_size_c = b64_decode(test_c,len_c,out_c);
-	
-	char r_a[] = HEXNUM_A;
-	char r_b[] = HEXNUM_B;
-	char r_c[] = HEXNUM_C;
-	
-	printf("\t%s\t",STATUS(compare(r_a,out_a,NELEMS(r_a)))); hexputs(out_a,out_size_a);
-	printf("\t%s\t",STATUS(compare(r_b,out_b,NELEMS(r_b)))); hexputs(out_b,out_size_b);
-	printf("\t%s\t",STATUS(compare(r_c,out_c,NELEMS(r_c)))); hexputs(out_c,out_size_c);
-	
-	free(out_a);
-	free(out_b);
-	free(out_c);
-	
-	return 0;
-}
-
-int test_b64_encodef() {
-	
-	FILE *pFile;
-	pFile = fopen("B64_TEST01A.tmp","wb");
-	if (pFile==NULL)
-		return 0;
-	
-	int i, j=0;
-	unsigned int test_a[] = HEXNUM_B;
-	unsigned int size_a = NELEMS(test_a);
-	
-	for (i=0;i<size_a;i++) {
-		fputc(test_a[i],pFile);
-	}
-	fclose(pFile);
-	
-	j = b64_encodef("B64_TEST01A.tmp","B64_TEST01B.tmp");
-	remove("B64_TEST01A.tmp");
-	
-	if (!j)
-		return 0;
-	
-	pFile = fopen("B64_TEST01B.tmp","rb");
-	if (pFile==NULL)
-		return 0;
-	
-	char *out = malloc(j+1);
-	fgets(out,j+1,pFile);
-	fclose(pFile);
-	remove("B64_TEST01B.tmp");
-	printf("\tComparing \"%s\" to \"%s\" : ",STRING_B,out);
-	if (strcmp(STRING_B,out)==0)
-		return 1;
-	
-	return 0;
-}
-
-int test_b64_decodef() {
-	
-	FILE *pFile;
-	pFile = fopen("B64_TEST02A.tmp","wb");
-	if (pFile==NULL)
-		return 0;
-	
-	int j=0;
-	
-	fputs(STRING_B,pFile);
-	fclose(pFile);
-	
-	j = b64_decodef("B64_TEST02A.tmp","B64_TEST02B.tmp");
-	remove("B64_TEST02A.tmp");
-	
-	if (!j)
-		return 0;
-	
-	pFile = fopen("B64_TEST02B.tmp","rb");
-	if (pFile==NULL)
-		return 0;
-	
-	char c, l=0, out[j+1];
-	while(c!=EOF) {
-		c=fgetc(pFile);
-		if (c==EOF)
-		break;
-		out[l++] = c;
-	}
-	fclose(pFile);
-	remove("B64_TEST02B.tmp");
-	printf("\tComparing \"%s\" to \"",HEXSTR_B); hexprint(out,j); printf("\" : ");
-	char r_b[] = HEXNUM_B;
-	if (compare(r_b,out,j))
-		return 1;
-	
-	return 0;
-}
-
-int test_b64_text_encode() {
-	char *test_str = TEXT_STR;
-	int length = strlen(test_str);
-	unsigned char *out_a = (char *)malloc(1 + (sizeof(char) * b64e_size(length)));
-	int out_size_a = b64_encode(test_str,length,out_a);
-	
-	int test_passed = (strcmp(out_a,TEXT_B64)==0);
-	printf("\t%s\t%s\n",STATUS(test_passed),out_a);
-
-	deepCompare(test_passed,out_a,TEXT_B64,out_size_a);
-	
-	free(out_a);
-}
-
-int test_b64_text_decode() {
-	char *test_str = TEXT_B64;
-	int length = strlen(test_str) + 1;
-	unsigned char *out_a = malloc( b64d_size(length)+1 );
-	int out_size_a = b64_decode(test_str,length,out_a);
-	out_a[out_size_a] = '\0';
-	
-	int test_passed = (strcmp(out_a,TEXT_STR)==0);
-	printf("\t%s\t%s\n",STATUS(test_passed),out_a);
-
-	deepCompare(test_passed,out_a,TEXT_STR,out_size_a);
-	
-	free(out_a);
-}
-
-int hexputs(const char* data, int len) {
-	hexprint(data,len);
-	printf("\n");
-	return 0;
-}
-
-int hexprint(const char* data, int len) {
-	int i;
-	for (i=0;i<len;i++) {
-		printf("0x%X",data[i]&255);
-		if (i<len-1)
-			printf(" ");
-	}
-	return 0;
-}
-
-int compare(char *a, char *b, int l) {
-	int i;
-	for (i=0;i<l;i++) {
-		if (a[i]!=b[i])
-			return 0;
-	}
-	return 1;
-}
-
-void deepCompare(int pass, char *a, char *b, int len){
-	if (!pass) {
-		for(int j=0;j<len;j++){
-			printf("\t\'%c\' == \'%c\'",a[j],b[j]);
-			if (a[j] != b[j])
-				printf(" <- Error.");
-			printf("\n");
-		}
-	}
-}
-
-char *score(int x) {
-	testScore+=(!!x)*1;
-	testTotal+=1;
-	return ((x>0)?"[PASS]":"[FAIL]");
-}
\ No newline at end of file