diff options
author | Joe DF <joedf@live.ca> | 2015-06-12 00:34:32 -0400 |
---|---|---|
committer | Joe DF <joedf@live.ca> | 2015-06-12 00:34:32 -0400 |
commit | 6dfd3e629a7a60c6d61eaece15bd98aac595ee0d (patch) | |
tree | eed35a0973ba5d025594477dc4fe2bba197b5c74 | |
parent | 096766661a31ad756285f699e917dff86a5d56a3 (diff) | |
download | base64-6dfd3e629a7a60c6d61eaece15bd98aac595ee0d.tar.gz |
b64_encodef() was returning wrong size
b64_decodef() not yet working.
-rw-r--r-- | base64.c | 36 | ||||
-rw-r--r-- | base64.h | 2 | ||||
-rw-r--r-- | test.c | 48 |
3 files changed, 78 insertions, 8 deletions
diff --git a/base64.c b/base64.c index f159089..73546d1 100644 --- a/base64.c +++ b/base64.c @@ -144,6 +144,7 @@ unsigned int b64_encodef(char *InFile, char *OutFile) { else fputc('=',pOutFile); fputc('=',pOutFile); + i+=4; } fclose(pInFile); @@ -151,8 +152,39 @@ unsigned int b64_encodef(char *InFile, char *OutFile) { return i; } -/* + unsigned int b64_decodef(char *InFile, char *OutFile) { + + FILE *pInFile = fopen(InFile,"rb"); + FILE *pOutFile = fopen(OutFile,"wb"); + if ( (pInFile==NULL) || (pOutFile==NULL) ) + return 0; + + unsigned int c=0, j=0, k=0, s[4]; + while(c!=EOF) { + c=fgetc(pInFile); + if (c==EOF) + break; + s[j++]=b64_int(c); + if (j==4) { + fputc((s[0]<<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; } -*/ \ No newline at end of file diff --git a/base64.h b/base64.h index 7cec63d..d7a1072 100644 --- a/base64.h +++ b/base64.h @@ -2,7 +2,7 @@ base64.c - by Joe DF (joedf@ahkscript.org) Released under the MIT License - Revision: 18:30 2014-09-22 + Revision: 2015-06-12 00:02:32 Thank you for inspiration: http://www.codeproject.com/Tips/813146/Fast-base-functions-for-encode-decode diff --git a/test.c b/test.c index 5c0888e..757beaa 100644 --- a/test.c +++ b/test.c @@ -43,6 +43,8 @@ int main() { 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("\n[END]"); return 0; @@ -135,7 +137,7 @@ int test_b64_encodef() { return 0; int i, j=0; - unsigned int test_a[] = HEXNUM_A; + unsigned int test_a[] = HEXNUM_B; unsigned int size_a = NELEMS(test_a); for (i=0;i<size_a;i++) { @@ -144,7 +146,7 @@ int test_b64_encodef() { fclose(pFile); j = b64_encodef("B64_TEST01A.tmp","B64_TEST01B.tmp"); - remove("B64_TEST01A.tmp"); + //remove("B64_TEST01A.tmp"); if (!j) return 0; @@ -156,9 +158,45 @@ int test_b64_encodef() { char *out = malloc(j+1); fgets(out,j+1,pFile); fclose(pFile); - remove("B64_TEST01B.tmp"); - printf("Comparing \"%s\" to \"%s\" : ",STRING_A,out); - if (strcmp(STRING_A,out)==0) + //remove("B64_TEST01B.tmp"); + printf("Comparing \"%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 *out = malloc(j+1); + fgets(out,j+1,pFile); + fclose(pFile); + //remove("B64_TEST02B.tmp"); + printf("Comparing \"%s\" with : ",HEXSTR_B); hexputs((int*)out,j); + + int r_b[] = HEXNUM_B; + + if (compare((int*)HEXSTR_B,(int*)out,NELEMS(r_b))) return 1; return 0; |