我自己作了BASE64的编码器~
对于普通的文件都能编码和解码(我把自己的编码器都编了也能解)
但是在LINUX下的终端的A.OUT这样的文件却不能,主要是A.OUT里有好多的 @ ^这样的东东,我不明白为什么,请高人指点下~我用的是fread()函数取的流~但是却取不出来~
有兴趣的可以 在LINUX的终端里 vi a.out看下~
主要问题是怎么把里面的 全部东西取出来~
[此贴子已经被作者于2007-7-16 13:06:40编辑过]
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
unsigned char old_char[1024*1024];
int three_char[24];
unsigned char four_char[1024*1024];
unsigned char old_64_char[1024*1024];
unsigned char three_char2[1024*1024];
int four_char2[24];
void get_char1(unsigned char old_char[],int &move,char* ar,int &math)
{
unsigned char* p;
unsigned char* q;
int i=0;
unsigned char test[1024*1024];
FILE* fp;
fp=fopen(ar,"rb");
while(!feof(fp))
{
fread(test,1,1,fp);
i++;
}
math=i-1;
printf("math=%d\n",math);
fseek(fp,0,SEEK_SET);
fread(old_char,math,1,fp);
fclose(fp);
fp=fopen("test2.cfg","wb");
fwrite(old_char,math,1,fp);
fclose(fp);
p=old_char;
/* while(*p!='\0')
{
i++;
p++;
}*/
if(math%3==2)
{
old_char[math]=0;
move=1;
}
if(math%3==1)
{
old_char[math]=0;
old_char[math+1]=0;
move=2;
}
printf("move=%d\n",move);
}
void change_into_21(unsigned char &x,unsigned char &y,unsigned char &z,int three_char[])
{
int i;
int k=1;
for(i=7;i>=0;i--)
{
three_char[i]=(x&k)/k;
k=k<<1;
}
k=1;
for(i=15;i>=8;i--)
{
three_char[i]=(y&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=16;i--)
{
three_char[i]=(z&k)/k;
k=k<<1;
}
}
void change_3_into_4(int three_char[],unsigned char four_char[],int &k,int &move,int &math)
{
char base64[65]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};
int i;
int j=0;
int u=0;
unsigned char* p;
for(i=0;i<=23;)
{
u=0;
for(j=0;j<=5;j++)
{
u=u+three_char[i+j]*(int)pow(2,5-j);
}
four_char[k]=base64[u];
k++;
i=i+6;
}
/* printf("\n");
p=four_char;
while(*p!='\0')
{
p++;
}
if(move==2)
{
*p='=';
*(p+1)='=';
}
if(move==1)
{
*p='=';
}*/
}
void base_64(unsigned char old_char[],int three_char[],unsigned char four_char[],unsigned char &x,unsigned char &y,unsigned char &z,int &k,int &move,int &math)
{
int i=0;
unsigned char* p;
for(i=0;i<=(math+move-1);)
{
x=old_char[i];
y=old_char[i+1];
z=old_char[i+2];
change_into_21(x,y,z,three_char);
i=i+3;
change_3_into_4(three_char,four_char,k,move,math);
}
p=four_char;
while(*p!='\0')
{
p++;
}
if(move==2)
{
*p='=';
*(p+1)='=';
}
if(move==1)
{
*p='=';
}
}
void save_char(unsigned char four_char[],char* ar,int &math,int &move)
{
FILE* fp;
fp=fopen(ar,"w+b");
int i=0;
while(four_char[i]!='\0')
{
i++;
}
i;
printf("i=%d",i);
fwrite(four_char,i,1,fp);
fclose(fp);
}
void save_char2(unsigned char three_char2[],char* ar,int &move,int &k)
{
if(move==2)
{
three_char2[k-1]=three_char2[k];
three_char2[k-2]=three_char2[k];
}
if(move==1)
{
three_char2[k-1]=three_char2[k];
}
FILE* fp;
fp=fopen(ar,"w+b");
fwrite(three_char2,k-move,1,fp);
fclose(fp);
}
void get_char2(unsigned char old_64_char[],int &move,char* ar)
{
FILE* fp;
unsigned char old_char[1024*1024];
unsigned char* p;
unsigned char* q;
int i=0;
fp=fopen(ar,"rb");
fread(old_char,1024*1024,1,fp);
//printf("%s",old_char);
fclose(fp);
p=old_char;
q=old_64_char;
while(*p!='\0')
{
if(*p!='=')
{
*q=*p;
}
if(*p=='=')
{
move++;
}
p++;
q++;
i++;
}
printf("i(k/3*4)+move=%d\n",i);
}
void change_into_22(unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,int four_char2[])
{
char base_64[65]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};
int i=0;
int k=1;
int j=0;
char* p;
p=base_64;
int a,b,c,d;
j=0;
p=base_64;
while(*p!='\0')
{
if(*p==w)
{
a=j;
}
if(*p==x)
{
b=j;
}
if(*p==y)
{
c=j;
}
if(*p==z)
{
d=j;
}
j++;
p++;
}
k=1;
for(i=5;i>=0;i--)
{
four_char2[i]=(a&k)/k;
k=k<<1;
}
k=1;
for(i=11;i>=6;i--)
{
four_char2[i]=(b&k)/k;
k=k<<1;
}
k=1;
for(i=17;i>=12;i--)
{
four_char2[i]=(c&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=18;i--)
{
four_char2[i]=(d&k)/k;
k=k<<1;
}
}
void change_4_into_3(int four_char2[],unsigned char three_char2[],int &k,int &move)
{
int i;
int j=0;
int u=0;
unsigned char* p;
for(i=0;i<=23;)
{
u=0;
for(j=0;j<=7;j++)
{
u=u+four_char2[i+j]*(int)pow(2,7-j);
}
three_char2[k]=char(u);
k++;
i=i+8;
}
}
void base_644(unsigned char old_64_char[],int move,unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,int &k,int four_char2[],unsigned char three_char2[])
{
int i=0;
while(old_64_char[i]!='\0')
{
w=old_64_char[i];
x=old_64_char[i+1];
y=old_64_char[i+2];
z=old_64_char[i+3];
change_into_22(w,x,y,z,four_char2);
i=i+4;
change_4_into_3(four_char2,three_char2,k,move);
}
}
main(int argc,char *argv[])
{
int math;
int i=0;
for(i=0;i<=argc;i++)
{
if(strcmp(argv[i],"-e")==0)
{
unsigned char x,y,z;
int k=0;
int move=0;
get_char1(old_char,move,argv[i+1],math);
base_64(old_char,three_char,four_char,x,y,z,k,move,math);
save_char(four_char,argv[i+2],math,move);
printf("math=%d",math);
printf("move=%d",move);
printf("%s",four_char);
int j=0;
while(four_char[j]!='\0')
j++;
printf("j=%d\n",j);
printf("%s",four_char);
break;
}
if(strcmp(argv[i],"-d")==0)
{
int k=0;
int move=0;
unsigned char w,x,y,z;
get_char2(old_64_char,move,argv[i+1]);
base_644(old_64_char,move,w,x,y,z,k,four_char2,three_char2);
save_char2(three_char2,argv[i+2],move,k);
//printf("%s",old_64_char);
k=k-2;
printf("finalnum=%d\n",k);
printf("move=%d",move);
break;
}
}
}
改进完的版本~可以不定义数组大小的 1次取3个 直到取完流为止
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
unsigned char old_char[4];
unsigned char four_char[5];
int three_char[24];
unsigned char w,x,y,z;
void get_char1(unsigned char old_char[],char *ar,int &offset)
{
FILE* fp;
unsigned char* p;
p=old_char;
while(*p!='\0')
{
*p=0;
p++;
}
fp=fopen(ar,"rb");
fseek(fp,offset,SEEK_SET);
fread(old_char,3,1,fp);
//printf("%s\n",old_char);
fclose(fp);
offset=offset+3;
}
void change_into_21(unsigned char &x,unsigned char &y,unsigned char &z,int three_char[])
{
int i;
int k=1;
for(i=7;i>=0;i--)
{
three_char[i]=(x&k)/k;
k=k<<1;
}
k=1;
for(i=15;i>=8;i--)
{
three_char[i]=(y&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=16;i--)
{
three_char[i]=(z&k)/k;
k=k<<1;
}
//for(i=0;i<=23;i++)
//printf("%d",three_char[i]);
//if(i==23)
//printf("\n");
}
void change_3_into_4(int three_char[],unsigned char four_char[])
{
char base_64[65]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};
int i;
int k=0;
int j=0;
int u;
for(i=0;i<=23;)
{
u=0;
for(j=0;j<=5;j++)
{
u=u+three_char[i+j]*(int)pow(2,5-j);
}
//printf("u=%d\n",u);
four_char[k]=base_64[u];
k++;
i=i+6;
}
}
void save_char(unsigned char four_char[],char *ar,int &offset)
{
FILE* fp;
fp=fopen(ar,"a+b");
fwrite(four_char,4,1,fp);
fputc('\n',fp); /* */
fclose(fp);
printf("%s",four_char);
}
void base_64(unsigned char &x,unsigned char &y,unsigned char &z,unsigned char four_char[],int three_char[])
{
x=old_char[0];
y=old_char[1];
z=old_char[2];
change_into_21(x,y,z,three_char);
change_3_into_4(three_char,four_char);
}
void move_char(int &move,unsigned char four_char[],char *ar)
{
FILE* fp;
unsigned char* p;
char a='=';
fp=fopen(ar,"a+b");
for(move;move>0;move--)
{
fputc(a,fp);
}
fclose(fp);
}
void get_char2(unsigned char four_char[],char *ar,int &offset)
{
FILE* fp;
int i=0;
unsigned char c;
fp=fopen(ar,"rb");
for(i=0;i<=3;i++)
{
fseek(fp,offset,SEEK_SET);
if((c=fgetc(fp))!='\n')
{
four_char[i]=c;
++offset;
}
if(fgetc(fp)=='\n')
{
offset++;
fseek(fp,offset,SEEK_SET);
}
}
/* printf("\n");
printf("#1=%c,",four_char[0]);
printf("#2=%c,",four_char[1]);
printf("#3=%c,",four_char[2]);
printf("#4=%c",four_char[3]);
printf("\n");*/
fclose(fp);
}
void change_into_22(unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,unsigned char four_char[],int three_char[])
{
char base_64[65]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};
int i=0;
int k=1;
int j=0;
char* p;
int a,b,c,d;
p=base_64;
while(*p!='\0')
{
if(*p==w)
{
a=j;
}
if(*p==x)
{
b=j;
}
if(*p==y)
{
c=j;
}
if(*p==z)
{
d=j;
}
j++;
p++;
}
// printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);
k=1;
for(i=5;i>=0;i--)
{
three_char[i]=(a&k)/k;
k=k<<1;
}
k=1;
for(i=11;i>=6;i--)
{
three_char[i]=(b&k)/k;
k=k<<1;
}
k=1;
for(i=17;i>=12;i--)
{
three_char[i]=(c&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=18;i--)
{
three_char[i]=(d&k)/k;
k=k<<1;
}
}
void change_4_into_3(int three_char[],unsigned char old_char[])
{
int i=0;
int j=0;
int u=0;
int k=0;
k=0;
for(i=0;i<=23;)
{
u=0;
for(j=0;j<=7;j++)
{
u=u+three_char[i+j]*(int)pow(2,7-j);
}
old_char[k]=char(u);
k++;
i=i+8;
}
}
void base_644(unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,unsigned char four_char[],int three_char[])
{
w=four_char[0];
x=four_char[1];
y=four_char[2];
z=four_char[3];
change_into_22(w,x,y,z,four_char,three_char);
change_4_into_3(three_char,old_char);
}
void save_char2(unsigned char old_char[],char *ar)
{
FILE* fp;
fp=fopen(ar,"a+b");
fwrite(old_char,3,1,fp);
fclose(fp);
}
void move_char2(unsigned char old_char[],char *ar,int &move)
{
FILE *fp;
fp=fopen(ar,"a+b");
if(move==1)
fwrite(old_char,2,1,fp);
if(move==2)
fwrite(old_char,1,1,fp);
if(move==0)
fwrite(old_char,3,1,fp);
fclose(fp);
}
main(int argc,char *argv[])
{
int i=0;
FILE* fp;
FILE* ff;
int offset=0;
unsigned char c;
int j=0;
int move=0;
for(i=0;i<=argc;i++)
{
if(strcmp(argv[i],"-e")==0)
{
fp=fopen(argv[i+1],"rb");
while(!feof(fp))
{
j++;
fgetc(fp);
}
if((j-1)%3==1)
{
j=(j-1)/3+1;
move=move+2;
}
if((j-1)%3==2)
{
j=(j-1)/3+1;
move++;
}
if((j-1)%3==0)
j=(j-1)/3;
printf("j=%d",j);
for(j;j>0;j--)
{
get_char1(old_char,argv[i+1],offset);
base_64(x,y,z,four_char,three_char);
save_char(four_char,argv[i+2],offset);
}
move_char(move,four_char,argv[i+2]);
fclose(fp);
break;
}
if(strcmp(argv[i],"-d")==0)
{
ff=fopen(argv[i+1],"rb");
while(!feof(ff))
{
c=fgetc(ff);
if(c!='='&&c!='\n')
j++;
if(c=='=')
move++;
}
fclose(ff);
j=(j-1)/4;
printf("j=%d,move=%d",j,move);
for(j;j>0;j--)
{
get_char2(four_char,argv[i+1],offset);
base_644(w,x,y,z,four_char,three_char);
if(j!=1)
save_char2(old_char,argv[i+2]);
if(j==1)
move_char2(old_char,argv[i+2],move);
}
printf(" %d",offset);
break;
}
}
}