about summary refs log tree commit diff stats
path: root/base64.c
diff options
context:
space:
mode:
Diffstat (limited to 'base64.c')
-rw-r--r--base64.c83
1 files changed, 55 insertions, 28 deletions
diff --git a/base64.c b/base64.c
index a69b7be..f159089 100644
--- a/base64.c
+++ b/base64.c
@@ -2,24 +2,17 @@
 	base64.c - by Joe DF (joedf@ahkscript.org)
 	Released under the MIT License
 	
-	Revision: 00:06 2014-09-22
+	See "base64.h", for more information.
 	
 	Thank you for inspiration:
 	http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode
 */
 
-
-unsigned int b64_int(unsigned int ch);
-unsigned int b64e_size(unsigned int in_size);
-unsigned int b64d_size(unsigned int in_size);
-unsigned int b64_encode(const unsigned int* in, unsigned int in_len, unsigned char* out);
-unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned int* out);
-
+#include "base64.h"
 
 //Base64 char table - used internally for encoding
 unsigned char b64_chr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
-//Base64 char table function - used internally for decoding
 unsigned int b64_int(unsigned int ch) {
 
 	// ASCII to base64_int
@@ -30,44 +23,36 @@ unsigned int b64_int(unsigned int ch) {
 	// 47     Slash (/)   >>  63
 	// 61     Equal (=)   >>  64~
 	if (ch==43)
-		return 62;
+	return 62;
 	if (ch==47)
-		return 63;
+	return 63;
 	if (ch==61)
-		return 64;
+	return 64;
 	if ((ch>47) && (ch<58))
-		return ch + 4;
+	return ch + 4;
 	if ((ch>64) && (ch<91))
-		return ch - 'A';
+	return ch - 'A';
 	if ((ch>96) && (ch<123))
-		return (ch - 'a') + 26;
+	return (ch - 'a') + 26;
 	return 0;
 }
 
-// in_size : the number bytes to be encoded.
-// Returns the recommended memory size to be allocated for the output buffer excluding the null byte
 unsigned int b64e_size(unsigned int in_size) {
 
 	// size equals 4*floor((1/3)*(in_size+2));
 	int i, j = 0;
 	for (i=0;i<in_size;i++) {
 		if (i % 3 == 0)
-			j += 1;
+		j += 1;
 	}
 	return (4*j);
 }
 
-// in_size : the number bytes to be decoded.
-// Returns the recommended memory size to be allocated for the output buffer excluding the null byte
 unsigned int b64d_size(unsigned int in_size) {
 
 	return ((3*in_size)/4);
 }
 
-// in : buffer of "raw" binary 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
-// returns size of output including null byte
 unsigned int b64_encode(const unsigned int* in, unsigned int in_len, unsigned char* out) {
 
 	unsigned int i=0, j=0, k=0, s[3];
@@ -101,10 +86,6 @@ unsigned int b64_encode(const unsigned int* in, unsigned int in_len, unsigned ch
 	return k;
 }
 
-// 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(const unsigned char* in, unsigned int in_len, unsigned int* out) {
 
 	unsigned int i=0, j=0, k=0, s[4];
@@ -129,3 +110,49 @@ unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned i
 	
 	return k;
 }
+
+unsigned int b64_encodef(char *InFile, char *OutFile) {
+	
+	FILE *pInFile = fopen(InFile,"rb");
+	FILE *pOutFile = fopen(OutFile,"wb");
+	if ( (pInFile==NULL) || (pOutFile==NULL) )
+	return 0;
+	
+	unsigned int i=0, j=0, c=0, s[3];
+	
+	while(c!=EOF) {
+		c=fgetc(pInFile);
+		if (c==EOF)
+		break;
+		s[j++]=c;
+		if (j==3) {
+			fputc(b64_chr[ s[0]>>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]>>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);
+	}
+	
+	fclose(pInFile);
+	fclose(pOutFile);
+	
+	return i;
+}
+/*
+unsigned int b64_decodef(char *InFile, char *OutFile) {
+	
+}
+*/
\ No newline at end of file