b64_encodef() was returning wrong size

b64_decodef() not yet working.
This commit is contained in:
Joe DF 2015-06-12 00:34:32 -04:00
parent 096766661a
commit 6dfd3e629a
3 changed files with 78 additions and 8 deletions

View File

@ -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;
}
*/

View File

@ -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

48
test.c
View File

@ -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;