From 7c2aeb26a87199f0f523b4addf79d3b392a140ca Mon Sep 17 00:00:00 2001 From: Joe DF Date: Mon, 22 Sep 2014 18:30:28 -0400 Subject: Update +enocdefile() --- .gitignore | 3 ++ base64.c | 83 +++++++++++++++++++++++++++++--------------- base64.h | 39 +++++++++++++++++++++ test.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 184 insertions(+), 55 deletions(-) create mode 100644 base64.h diff --git a/.gitignore b/.gitignore index 60ca861..3af8d89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +~* +debug.bat +compile.bat *.exe *.lnk *.url 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>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 diff --git a/base64.h b/base64.h new file mode 100644 index 0000000..7cec63d --- /dev/null +++ b/base64.h @@ -0,0 +1,39 @@ +/* + base64.c - by Joe DF (joedf@ahkscript.org) + Released under the MIT License + + Revision: 18:30 2014-09-22 + + Thank you for inspiration: + http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode +*/ + +#include + +//Base64 char table function - used internally for decoding +unsigned int b64_int(unsigned int ch); + +// 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); + +// in_size : the number bytes to be decoded. +// Returns the recommended memory size to be allocated for the output buffer +unsigned int b64d_size(unsigned int in_size); + +// 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); + +// 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); + +// file-version b64_encode +// Input : filenames +// returns size of output +unsigned int b64_encodef(char *InFile, char *OutFile); \ No newline at end of file diff --git a/test.c b/test.c index 1a07c9c..b4d34dc 100644 --- a/test.c +++ b/test.c @@ -2,6 +2,8 @@ #include #include +#include "base64.h" + //Test values /////////////////////////////////////////////////////////////// #define STRING_A "9TrJ" @@ -21,7 +23,9 @@ int test_b64_encode(); int test_b64_decode(); -int hexputs(unsigned int* data, unsigned int len); +int test_b64_encodef(); +int test_b64_decodef(); +int hexputs(const int* data, int len); int compare(int *a, int *b, int l); char *status(int boolean); @@ -36,6 +40,8 @@ int main() { 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("\n[END]"); return 0; @@ -58,16 +64,20 @@ int test_b64_encode() { unsigned char *out_a = malloc(out_size_a); unsigned char *out_b = malloc(out_size_b); unsigned char *out_c = malloc(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); - 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("%s\t%s\n",status(!strcmp(out_a,STRING_A)),out_a); - printf("%s\t%s\n",status(!strcmp(out_b,STRING_B)),out_b); - printf("%s\t%s\n",status(!strcmp(out_c,STRING_C)),out_c); - - return 0; + printf("%s\t%s\n",status(strcmp(out_a,STRING_A)==0),out_a); + printf("%s\t%s\n",status(strcmp(out_b,STRING_B)==0),out_b); + printf("%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() { @@ -80,33 +90,83 @@ int test_b64_decode() { int len_b = strlen(test_b); int len_c = strlen(test_c); - int out_size_a = b64d_size(len_a) + 1; - int out_size_b = b64d_size(len_b) + 1; - int out_size_c = b64d_size(len_c) + 1; - - unsigned int *out_a = malloc(out_size_a); - unsigned int *out_b = malloc(out_size_b); - unsigned int *out_c = malloc(out_size_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); + + //printf("%i,%i,%i\n",out_size_a,out_size_b,out_size_c); + //wierd ~100 bytes memory problem? - 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); + unsigned int *out_a = malloc(out_size_a+100); + unsigned int *out_b = malloc(out_size_b+100); + unsigned int *out_c = malloc(out_size_c+100); + + 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); int r_a[] = HEXNUM_A; int r_b[] = HEXNUM_B; int r_c[] = HEXNUM_C; - printf("%s\t",status(compare(r_a,out_a,3))); hexputs(out_a,out_size_a); - printf("%s\t",status(compare(r_b,out_b,4))); hexputs(out_b,out_size_b); - printf("%s\t",status(compare(r_c,out_c,5))); hexputs(out_c,out_size_c); - - return 0; + //getchar(); + + printf("%s\t",status(compare(r_a,out_a,NELEMS(r_a)))); hexputs(out_a,out_size_a); + printf("%s\t",status(compare(r_b,out_b,NELEMS(r_b)))); hexputs(out_b,out_size_b); + printf("%s\t",status(compare(r_c,out_c,NELEMS(r_c)))); hexputs(out_c,out_size_c); + + //printf("addr_a = %p\n",out_a); + //printf("addr_b = %p\n",out_b); + //printf("addr_c = %p\n",out_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_A; + unsigned int size_a = NELEMS(test_a); + + for (i=0;i